All of lore.kernel.org
 help / color / mirror / Atom feed
From: Youling Tang <youling.tang@linux.dev>
To: Simon Horman <horms@kernel.org>
Cc: Simon Horman <horms@verge.net.au>,
	kexec@lists.infradead.org, Huacai Chen <chenhuacai@kernel.org>,
	youling.tang@linux.dev, Youling Tang <tangyouling@kylinos.cn>
Subject: [PATCH 2/2] LoongArch: Refactor command line processing
Date: Thu, 25 Sep 2025 14:32:41 +0800	[thread overview]
Message-ID: <20250925063241.337897-2-youling.tang@linux.dev> (raw)
In-Reply-To: <20250925063241.337897-1-youling.tang@linux.dev>

From: Youling Tang <tangyouling@kylinos.cn>

Refactor the cmdline_add_xxx code flow, and simultaneously display
the content of parameters such as initrd in hexadecimal format to
improve readability.

Signed-off-by: Youling Tang <tangyouling@kylinos.cn>
---
 kexec/arch/loongarch/kexec-loongarch.c | 138 ++++++++++---------------
 1 file changed, 55 insertions(+), 83 deletions(-)

diff --git a/kexec/arch/loongarch/kexec-loongarch.c b/kexec/arch/loongarch/kexec-loongarch.c
index 240202f..c2503de 100644
--- a/kexec/arch/loongarch/kexec-loongarch.c
+++ b/kexec/arch/loongarch/kexec-loongarch.c
@@ -35,83 +35,49 @@
 #define _O_BINARY 0
 #endif
 
-#define CMDLINE_PREFIX "kexec "
-static char cmdline[COMMAND_LINE_SIZE] = CMDLINE_PREFIX;
+/* Add the "kexec" command line parameter to command line. */
+static void cmdline_add_loader(unsigned long *cmdline_tmplen, char *modified_cmdline)
+{
+	int loader_strlen;
+
+	loader_strlen = sprintf(modified_cmdline + (*cmdline_tmplen), "kexec ");
+	*cmdline_tmplen += loader_strlen;
+}
 
-/* Adds "initrd=start,size" parameters to command line. */
-static int cmdline_add_initrd(char *cmdline, unsigned long addr,
-		unsigned long size)
+/* Add the "initrd=start,size" command line parameter to command line. */
+static void cmdline_add_initrd(unsigned long *cmdline_tmplen, char *modified_cmdline,
+			       unsigned long initrd_base, unsigned long initrd_size)
 {
-	int cmdlen, len;
-	char str[50], *ptr;
-
-	ptr = str;
-	strcpy(str, " initrd=");
-	ptr += strlen(str);
-	ultoa(addr, ptr);
-	strcat(str, ",");
-	ptr = str + strlen(str);
-	ultoa(size, ptr);
-	len = strlen(str);
-	cmdlen = strlen(cmdline) + len;
-	if (cmdlen > (COMMAND_LINE_SIZE - 1))
-		die("Command line overflow\n");
-	strcat(cmdline, str);
+	int initrd_strlen;
 
-	return 0;
+	initrd_strlen = sprintf(modified_cmdline + (*cmdline_tmplen), "initrd=0x%lx,0x%lx ",
+				initrd_base, initrd_size);
+	*cmdline_tmplen += initrd_strlen;
 }
 
-/* Adds the appropriate "mem=size@start" options to command line, indicating the
- * memory region the new kernel can use to boot into. */
-static int cmdline_add_mem(char *cmdline, unsigned long addr,
-		unsigned long size)
+/*
+ * Add the "mem=size@start" command line parameter to command line, indicating the
+ * memory region the new kernel can use to boot into.
+ */
+static void cmdline_add_mem(unsigned long *cmdline_tmplen, char *modified_cmdline,
+			    unsigned long mem_start, unsigned long mem_sz)
 {
-	int cmdlen, len;
-	char str[50], *ptr;
-
-	addr = addr/1024;
-	size = size/1024;
-	ptr = str;
-	strcpy(str, " mem=");
-	ptr += strlen(str);
-	ultoa(size, ptr);
-	strcat(str, "K@");
-	ptr = str + strlen(str);
-	ultoa(addr, ptr);
-	strcat(str, "K");
-	len = strlen(str);
-	cmdlen = strlen(cmdline) + len;
-	if (cmdlen > (COMMAND_LINE_SIZE - 1))
-		die("Command line overflow\n");
-	strcat(cmdline, str);
+	int mem_strlen = 0;
 
-	return 0;
+	mem_strlen = sprintf(modified_cmdline + (*cmdline_tmplen), "mem=0x%lx@0x%lx ",
+			     mem_sz, mem_start);
+	*cmdline_tmplen += mem_strlen;
 }
 
-/* Adds the "elfcorehdr=size@start" command line parameter to command line. */
-static int cmdline_add_elfcorehdr(char *cmdline, unsigned long addr,
-			unsigned long size)
+/* Add the "elfcorehdr=size@start" command line parameter to command line. */
+static void cmdline_add_elfcorehdr(unsigned long *cmdline_tmplen, char *modified_cmdline,
+				   unsigned long elfcorehdr_start, unsigned long elfcorehdr_sz)
 {
-	int cmdlen, len;
-	char str[50], *ptr;
-
-	addr = addr/1024;
-	size = size/1024;
-	ptr = str;
-	strcpy(str, " elfcorehdr=");
-	ptr += strlen(str);
-	ultoa(size, ptr);
-	strcat(str, "K@");
-	ptr = str + strlen(str);
-	ultoa(addr, ptr);
-	strcat(str, "K");
-	len = strlen(str);
-	cmdlen = strlen(cmdline) + len;
-	if (cmdlen > (COMMAND_LINE_SIZE - 1))
-		die("Command line overflow\n");
-	strcat(cmdline, str);
+	int elfcorehdr_strlen = 0;
 
-	return 0;
+	elfcorehdr_strlen = sprintf(modified_cmdline + (*cmdline_tmplen), "elfcorehdr=0x%lx@0x%lx ",
+				    elfcorehdr_sz, elfcorehdr_start);
+	*cmdline_tmplen += elfcorehdr_strlen;
 }
 
 /* Return a sorted list of memory ranges. */
@@ -329,18 +295,17 @@ int loongarch_load_other_segments(struct kexec_info *info, unsigned long hole_mi
 	unsigned long initrd_min, hole_max;
 	char *initrd_buf = NULL;
 	unsigned long pagesize = getpagesize();
+	unsigned long cmdline_tmplen = 0;
+	char *cmdline = NULL;
 	int i;
 
-	if (arch_options.command_line) {
-		if (strlen(arch_options.command_line) >
-		    sizeof(cmdline) - 1) {
-			fprintf(stderr,
-				"Kernel command line too long for kernel!\n");
-			return EFAILED;
-		}
+	cmdline = calloc(1, COMMAND_LINE_SIZE);
+	if (!cmdline)
+		return EFAILED;
 
-		strncat(cmdline, arch_options.command_line, sizeof(cmdline) - 1);
-	}
+	/* Ensure it's null terminated */
+	cmdline_add_loader(&cmdline_tmplen, cmdline);
+	cmdline[COMMAND_LINE_SIZE - 1] = '\0';
 
 	/* Put the other segments after the image. */
 
@@ -360,21 +325,29 @@ int loongarch_load_other_segments(struct kexec_info *info, unsigned long hole_mi
 						pagesize), hole_max, -1);
 		dbgprintf("initrd_base: %lx, initrd_size: %lx\n", initrd_base, initrd_size);
 
-		cmdline_add_initrd(cmdline, initrd_base, initrd_size);
+		cmdline_add_initrd(&cmdline_tmplen, cmdline, initrd_base, initrd_size);
 	}
 
 	if (info->kexec_flags & KEXEC_ON_CRASH) {
-		cmdline_add_elfcorehdr(cmdline, elfcorehdr_mem.start,
-				elfcorehdr_mem.end - elfcorehdr_mem.start + 1);
+		cmdline_add_elfcorehdr(&cmdline_tmplen, cmdline, elfcorehdr_mem.start,
+				       elfcorehdr_mem.end - elfcorehdr_mem.start + 1);
 
 		for(i = 0;i < usablemem_rgns.size; i++) {
-			cmdline_add_mem(cmdline, crash_reserved_mem[i].start,
-			crash_reserved_mem[i].end -
-			crash_reserved_mem[i].start + 1);
+			cmdline_add_mem(&cmdline_tmplen, cmdline, crash_reserved_mem[i].start,
+					crash_reserved_mem[i].end - crash_reserved_mem[i].start + 1);
+		}
+	}
+
+	if (arch_options.command_line) {
+		if (strlen(arch_options.command_line) + cmdline_tmplen > COMMAND_LINE_SIZE) {
+			fprintf(stderr, "Kernel command line too long for kernel!\n");
+			free(cmdline);
+			return EFAILED;
 		}
+		memcpy(cmdline + cmdline_tmplen, arch_options.command_line,
+		       strlen(arch_options.command_line));
 	}
 
-	cmdline[sizeof(cmdline) - 1] = '\0';
 	add_buffer(info, cmdline, sizeof(cmdline), sizeof(cmdline),
 		sizeof(void *), _ALIGN_UP(hole_min, getpagesize()),
 		hole_max, 1);
@@ -382,7 +355,6 @@ int loongarch_load_other_segments(struct kexec_info *info, unsigned long hole_mi
 	dbgprintf("%s:%d: command_line: %s\n", __func__, __LINE__, cmdline);
 
 	return 0;
-
 }
 
 int arch_compat_trampoline(struct kexec_info *UNUSED(info))
-- 
2.48.1



  reply	other threads:[~2025-09-25  6:33 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-25  6:32 [PATCH 1/2] Remove the "mem=" parameter when using the --reuse-cmdline option in kdump operation Youling Tang
2025-09-25  6:32 ` Youling Tang [this message]
2025-09-25  9:22   ` [PATCH 2/2] LoongArch: Refactor command line processing Dave Young
     [not found]     ` <5ec31e96-7157-4300-af36-daec2cee5831@linux.dev>
2025-09-25 10:23       ` Dave Young
2025-11-12  3:05         ` Youling Tang
2025-09-25  9:11 ` [PATCH 1/2] Remove the "mem=" parameter when using the --reuse-cmdline option in kdump operation Dave Young
2025-09-25  9:25   ` Youling Tang
2025-09-25  9:34     ` Dave Young

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250925063241.337897-2-youling.tang@linux.dev \
    --to=youling.tang@linux.dev \
    --cc=chenhuacai@kernel.org \
    --cc=horms@kernel.org \
    --cc=horms@verge.net.au \
    --cc=kexec@lists.infradead.org \
    --cc=tangyouling@kylinos.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.