Skip to contentSkip to conversation

arm64: Add purgatory printing

PATCH Superseded [email protected] 3 participants 4 messages Sep 30, 2020 – Oct 1, 2020

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 arm64: Add purgatory printing

From: Matthias Brugger <[email protected]>

Add option to allow purgatory printing on arm64 hardware by passing the console name which should be used. Based on a patch by Geoff Levand.

---

Changes in v3:
- check return value of snprintf

Changes in v2:
- use sizeof(buffer)
- user serial command option instead of console
- reduce buffer to size 10
- use snprintf
- fix error handling for wrong serial console parameter
- Update help information

 kexec/arch/arm64/include/arch/options.h |  6 ++-
 kexec/arch/arm64/kexec-arm64.c          | 71 +++++++++++++++++++++++++
 purgatory/arch/arm64/purgatory-arm64.c  | 17 +++++-
 3 files changed, 92 insertions(+), 2 deletions(-)
kexec/arch/arm64/include/arch/options.h +5 -1
index a17d933..8c695f3 100644
@@ -5,7 +5,8 @@
 #define OPT_DTB			((OPT_MAX)+1)
 #define OPT_INITRD		((OPT_MAX)+2)
 #define OPT_REUSE_CMDLINE	((OPT_MAX)+3)
-#define OPT_ARCH_MAX		((OPT_MAX)+4)
+#define OPT_SERIAL		((OPT_MAX)+4)
+#define OPT_ARCH_MAX		((OPT_MAX)+5)
 
 #define KEXEC_ARCH_OPTIONS \
 	KEXEC_OPTIONS \
@@ -13,6 +14,7 @@
 	{ "command-line",  1, NULL, OPT_APPEND }, \
 	{ "dtb",           1, NULL, OPT_DTB }, \
 	{ "initrd",        1, NULL, OPT_INITRD }, \
+	{ "serial",        1, NULL, OPT_SERIAL }, \
 	{ "ramdisk",       1, NULL, OPT_INITRD }, \
 	{ "reuse-cmdline", 0, NULL, OPT_REUSE_CMDLINE }, \
 
@@ -25,6 +27,7 @@ static const char arm64_opts_usage[] __attribute__ ((unused)) =
 "     --command-line=STRING Set the kernel command line to STRING.\n"
 "     --dtb=FILE            Use FILE as the device tree blob.\n"
 "     --initrd=FILE         Use FILE as the kernel initial ramdisk.\n"
+"     --serial=STRING       Name of console used for purgatory printing. (e.g. ttyAMA0)\n"
 "     --ramdisk=FILE        Use FILE as the kernel initial ramdisk.\n"
 "     --reuse-cmdline       Use kernel command line from running system.\n";
 
@@ -32,6 +35,7 @@ struct arm64_opts {
 	const char *command_line;
 	const char *dtb;
 	const char *initrd;
+	const char *console;
 };
 
 extern struct arm64_opts arm64_opts;
kexec/arch/arm64/kexec-arm64.c +71 -0

107 lines not on this page · raw message

purgatory/arch/arm64/purgatory-arm64.c +16 -1
index fe50fcf..b4d8578 100644
@@ -5,15 +5,30 @@
 #include <stdint.h>
 #include <purgatory.h>
 
+/* Symbols set by kexec. */
+
+uint8_t *arm64_sink __attribute__ ((section ("data")));
+extern void (*arm64_kernel_entry)(uint64_t, uint64_t, uint64_t, uint64_t);
+extern uint64_t arm64_dtb_addr;
+
 void putchar(int ch)
 {
-	/* Nothing for now */
+	if (!arm64_sink)
+		return;
+
+	*arm64_sink = ch;
+
+	if (ch == '\n')
+		*arm64_sink = '\r';
 }
 
 void post_verification_setup_arch(void)
 {
+	printf("purgatory: booting kernel now\n");
 }
 
 void setup_arch(void)
 {
+	printf("purgatory: entry=%lx\n", (unsigned long)arm64_kernel_entry);
+	printf("purgatory: dtb=%lx\n", arm64_dtb_addr);
 }
Signature · 7 lines
2.28.0


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
#1 lore raw

Hi,

Quoted 18 lines · On 9/30/20 3:36 AM, [email protected] wrote:
kexec/arch/arm64/include/arch/options.h +15 -0
 
+static uint64_t find_purgatory_sink(const char *console)
+{
+	int fd, ret;
+	char device[255], mem[255];
+	struct stat sb;
+	char buffer[10];
+	uint64_t iomem = 0x0;
+
+	if (!console)
+		return 0;
+
+	ret = snprintf(device, sizeof(device), "/sys/class/tty/%s", console);
+	if (ret < 0 || ret >= sizeof(device)) {
+		fprintf(stderr, "snprintf failed: %s\n", strerror(errno));
+		return 0;

kexec/arch/arm64/include/arch/options.h

Quoted 10 lines
kexec/arch/arm64/include/arch/options.h +10 -0
+	}
+
+	if (stat(device, &sb) || !S_ISDIR(sb.st_mode)) {
+		fprintf(stderr, "kexec: %s: No valid console found for %s\n",
+			__func__, device);
+		return 0;
+	}
+
+	snprintf(mem, sizeof(mem), "%s%s", device, "/iomem_base");
+	if (ret < 0 || ret >= sizeof(mem)) {

Seems this is is just doing the same check again...

kexec/arch/arm64/include/arch/options.h

Quoted 3 lines
kexec/arch/arm64/include/arch/options.h +3 -0
+		fprintf(stderr, "snprintf failed: %s\n", strerror(errno));
+		return 0;
+	}

-Geoff

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#2 lore raw
Quoted 25 lines · Geoff Levand

Hi,

Quoted 22 lines · On 9/30/20 3:36 AM, [email protected] wrote:
kexec/arch/arm64/include/arch/options.h +19 -0
 
+static uint64_t find_purgatory_sink(const char *console)
+{
+	int fd, ret;
+	char device[255], mem[255];
+	struct stat sb;
+	char buffer[10];
+	uint64_t iomem = 0x0;
+
+	if (!console)
+		return 0;
+
+	ret = snprintf(device, sizeof(device), "/sys/class/tty/%s", console);
+	if (ret < 0 || ret >= sizeof(device)) {
+		fprintf(stderr, "snprintf failed: %s\n", strerror(errno));
+		return 0;
+	}
+
+	if (stat(device, &sb) || !S_ISDIR(sb.st_mode)) {
+		fprintf(stderr, "kexec: %s: No valid console found for %s\n",
Quoted 9 lines

kexec/arch/arm64/include/arch/options.h

Quoted 6 lines
kexec/arch/arm64/include/arch/options.h +6 -0
+			__func__, device);
+		return 0;
+	}
+
+	snprintf(mem, sizeof(mem), "%s%s", device, "/iomem_base");
+	if (ret < 0 || ret >= sizeof(mem)) {

Seems this is is just doing the same check again...

Well we have to check snprintf outcome here again. Also sizeof(device) == sizeof(mem) we could have the case where the first snprintf fits into the 255 character, while the second, where we add the "/iomem_base" does not.

Or didn't I understand you correctly?

Regards,
Matthias

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#3 lore raw

Hi Matthias,

Quoted 34 lines · Matthias Brugger
Quoted 23 lines · Geoff Levand
Quoted 22 lines · On 9/30/20 3:36 AM, [email protected] wrote:
kexec/arch/arm64/include/arch/options.h +19 -0
 
+static uint64_t find_purgatory_sink(const char *console)
+{
+    int fd, ret;
+    char device[255], mem[255];
+    struct stat sb;
+    char buffer[10];
+    uint64_t iomem = 0x0;
+
+    if (!console)
+        return 0;
+
+    ret = snprintf(device, sizeof(device), "/sys/class/tty/%s", console);
+    if (ret < 0 || ret >= sizeof(device)) {
+        fprintf(stderr, "snprintf failed: %s\n", strerror(errno));
+        return 0;
+    }
+
+    if (stat(device, &sb) || !S_ISDIR(sb.st_mode)) {
+        fprintf(stderr, "kexec: %s: No valid console found for %s\n",
Quoted 9 lines
Quoted 6 lines

+            __func__, device);
+        return 0;
+    }
+
+    snprintf(mem, sizeof(mem), "%s%s", device, "/iomem_base"); +    if (ret < 0 || ret >= sizeof(mem)) {

Seems this is is just doing the same check again...

Quoted 3 lines

Well we have to check snprintf outcome here again. Also sizeof(device) == sizeof(mem) we could have the case where the first snprintf fits into the 255 character, while the second, where we add the "/iomem_base" does not.

Or didn't I understand you correctly?

You forgot to assign ret a new value, so it's just doing the same thing again...

-Geoff

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec