Skip to contentSkip to conversation

kexec-tools: Fix option/argument parsing

RFC PATCH Superseded Matt Evans 4 participants 9 messages May 13, 2010 – May 14, 2010

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Matt Evans

author
#0 lore raw

RFC PATCH 1/1 kexec-tools: Fix option/argument parsing

Hi,

In playing with kexec-tools I've noticed various problems with the argument passing, meaning one has to be careful to use certain forms of arguments and present them in a certain order.

The arguments end up being parsed three times, each getting more specific than the last. main() looks for general args, arch_process_options() looks for options common to all loaders for the arch, and then finally many file_type load() methods check for options specific to that filetype.

As GNU getopt works, it re-orders the argv[] pushing any args it doesn't recognise to the end. This includes arguments to valid options which are simply not recognised the first time around.

For example, this does not work:

  # ./kexec -l --append "init=/bin/foo" /boot/vmlinux
  Cannot open `init=/bin/foo': No such file or directory

but this does:

  # ./kexec -l --append="init=/bin/foo" /boot/vmlinux
  <joy>

Using the --opt<space>arg variant results in the first non-option argument in main() being "init=/bin/foo" which is then used as the kernel filename, failing. Also, the order affects things in unintuitive ways:

  # ./kexec -l /boot/vmlinux --append "init=/bin/foo" 
<appears to load correctly, but command line appended with "/boot/vmlinux"!>

This behaviour is avoided by using the --opt= forms, but getopt does allow both and hence the user can have a fairly frustrating experience. (Doing something unexpected (ex. 3) is more annoying than an error exit (ex. 1) in many cases.)

The fix presented here is to create a new #define to encapsulate all possible options for an architecture build. The intention is that this set includes all possible loaders' all possible options.

main() walks through the options and silently ignores any non-general options (BUT respects that "--arg foo" should remain together, as getopt_long() does now recognise it). arch_process_options() walks through them again and responds to any arch-specific options, again ignoring but respecting any non-arch options. Finally the loader may look for its options, and find them in-order and present. Any outside of this combined set are complained-about as usual.

So, comments please. Is this a reasonable way to do it or is there an obvious better way I've missed? :-) So far I have been able to test on x86(32,64) and ppc(32,64) but none of the others. :(

Cheers,

Matt

---
The argument parsing is currently a bit broken as main()'s getopt_long() knows nothing about either the architecture-specific options or, even more specifically, the architecture-and-loader-specific options.

This patch introduces new #defines for all architectures, KEXEC_ALL_OPTIONS and KEXEC_ALL_OPT_STR. These contain all possible options for a given build, and the getopt_long() passes in main() and arch_process_options() will now recognise arch- and loader-specific options; these will not be re-ordered in argv[], there is no confusion over which argv[] entry is the kernel filename, and using '--opt=foo' and '--opt foo' both work.

All architectures have command line options (and #define OPT_BLAHs) consolidated into their include/arch/option.h files. x86_64 builds parts of i386/ as well, so now both share a single option.h file (with a symlink).

---
 kexec/arch/alpha/include/arch/options.h  |    9 +++++
 kexec/arch/arm/include/arch/options.h    |   29 +++++++++++++++++
 kexec/arch/arm/kexec-zImage-arm.c        |    3 +-
 kexec/arch/cris/include/arch/options.h   |   32 ++++++++++++++++--
 kexec/arch/cris/kexec-elf-cris.c         |    4 +-
 kexec/arch/i386/include/arch/options.h   |   51 ++++++++++++++++++++++++++++++
 kexec/arch/i386/kexec-beoboot-x86.c      |    3 +-
 kexec/arch/i386/kexec-bzImage.c          |    6 +--
 kexec/arch/i386/kexec-elf-x86.c          |    7 +---
 kexec/arch/i386/kexec-multiboot-x86.c    |    5 +--
 kexec/arch/i386/kexec-x86.c              |    4 +-
 kexec/arch/ia64/include/arch/options.h   |   33 ++++++++++++++++++-
 kexec/arch/ia64/kexec-elf-ia64.c         |    7 +---
 kexec/arch/mips/include/arch/options.h   |   27 +++++++++++++++-
 kexec/arch/mips/kexec-elf-mips.c         |    3 +-
 kexec/arch/ppc/include/arch/options.h    |   34 ++++++++++++++++++++
 kexec/arch/ppc/kexec-dol-ppc.c           |    4 +-
 kexec/arch/ppc/kexec-elf-ppc.c           |    7 +---
 kexec/arch/ppc/kexec-uImage-ppc.c        |    6 +--
 kexec/arch/ppc64/include/arch/options.h  |   37 ++++++++++++++++++++-
 kexec/arch/ppc64/kexec-elf-ppc64.c       |   12 ++++---
 kexec/arch/ppc64/kexec-ppc64.c           |   12 +++----
 kexec/arch/s390/include/arch/options.h   |   28 ++++++++++++++++-
 kexec/arch/s390/kexec-image.c            |    4 +--
 kexec/arch/sh/include/arch/options.h     |   26 +++++++++++++--
 kexec/arch/sh/kexec-sh.c                 |    7 +++-
 kexec/arch/x86_64/include/arch/options.h |   23 +-------------
 kexec/arch/x86_64/kexec-elf-x86_64.c     |    7 +---
 kexec/arch/x86_64/kexec-x86_64.c         |    4 +-
 kexec/kexec.c                            |    4 +-
 30 files changed, 340 insertions(+), 98 deletions(-)
 mode change 100644 => 120000 kexec/arch/x86_64/include/arch/options.h
kexec/arch/alpha/include/arch/options.h +9 -0
index 11557a4..a012c8a 100644
@@ -3,9 +3,18 @@
 
 #define OPT_ARCH_MAX   (OPT_MAX+0)
 
+/* Options relevant to the architecture (excluding loader-specific ones),
+ * in this case none:
+ */
 #define KEXEC_ARCH_OPTIONS \
 	KEXEC_OPTIONS \
 
 #define KEXEC_ARCH_OPT_STR KEXEC_OPT_STR ""
 
+/* See the other architectures for details of these; Alpha has no
+ * loader-specific options yet.
+ */
+#define KEXEC_ALL_OPTIONS KEXEC_ARCH_OPTIONS
+#define KEXEC_ALL_OPT_STR KEXEC_ARCH_OPT_STR
+
 #endif /* KEXEC_ARCH_ALPHA_OPTIONS_H */
kexec/arch/arm/include/arch/options.h +29 -0
index 248230b..a76539e 100644
@@ -3,9 +3,38 @@
 
 #define OPT_ARCH_MAX   (OPT_MAX+0)
 
+#define OPT_APPEND	'a'
+#define OPT_RAMDISK	'r'
+
+/* Options relevant to the architecture (excluding loader-specific ones),
+ * in this case none:
+ */
 #define KEXEC_ARCH_OPTIONS \
 	KEXEC_OPTIONS \
 
 #define KEXEC_ARCH_OPT_STR KEXEC_OPT_STR ""
 
+/* The following two #defines list ALL of the options added by all of the
+ * architecture's loaders.
+ * o	main() uses this complete list to scan for its options, ignoring
+ *	arch-specific/loader-specific ones.
+ * o	Then, arch_process_options() uses this complete list to scan for its
+ *	options, ignoring general/loader-specific ones.
+ * o	Then, the file_type[n].load re-scans for options, using
+ *	KEXEC_ARCH_OPTIONS plus its loader-specific options subset.
+ *	Any unrecognised options cause an error here.
+ *
+ * This is done so that main()'s/arch_process_options()'s getopt_long() calls
+ * don't choose a kernel filename from random arguments to options they don't
+ * recognise -- as they now recognise (if not act upon) all possible options.
+ */
+#define KEXEC_ALL_OPTIONS \
+	KEXEC_ARCH_OPTIONS
+	{ "command-line",	1, 0, OPT_APPEND },
+	{ "append",		1, 0, OPT_APPEND },
+	{ "initrd",		1, 0, OPT_RAMDISK },
+	{ "ramdisk",		1, 0, OPT_RAMDISK },
+
+#define KEXEC_ALL_OPT_STR KEXEC_ARCH_OPT_STR "a:r:"
+
 #endif /* KEXEC_ARCH_ARM_OPTIONS_H */
kexec/arch/arm/kexec-zImage-arm.c +1 -2
index 1a446d9..e060f6e 100644
@@ -219,8 +219,7 @@ int zImage_arm_load(int argc, char **argv, const char *buf, off_t len,
 	off_t ramdisk_length;
 	off_t ramdisk_offset;
 	int opt;
-#define OPT_APPEND	'a'
-#define OPT_RAMDISK	'r'
+	/* See options.h -- add any more there, too. */
 	static const struct option options[] = {
 		KEXEC_ARCH_OPTIONS
 		{ "command-line",	1, 0, OPT_APPEND },
kexec/arch/cris/include/arch/options.h +28 -4
index bc5f706..1c1a029 100644
@@ -1,11 +1,35 @@
-#ifndef KEXEC_ARCH_MIPS_OPTIONS_H
-#define KEXEC_ARCH_MIPS_OPTIONS_H
+#ifndef KEXEC_ARCH_CRIS_OPTIONS_H
+#define KEXEC_ARCH_CRIS_OPTIONS_H
 
-#define OPT_ARCH_MAX   (OPT_MAX+0)
+#define OPT_ARCH_MAX	(OPT_MAX+0)
+#define OPT_APPEND	(OPT_ARCH_MAX+0)
 
+/* Options relevant to the architecture (excluding loader-specific ones),
+ * in this case none:
+ */
 #define KEXEC_ARCH_OPTIONS \
 	KEXEC_OPTIONS \
 
 #define KEXEC_ARCH_OPT_STR KEXEC_OPT_STR ""
 
-#endif /* KEXEC_ARCH_MIPS_OPTIONS_H */
+/* The following two #defines list ALL of the options added by all of the
+ * architecture's loaders.
+ * o	main() uses this complete list to scan for its options, ignoring
+ *	arch-specific/loader-specific ones.
+ * o	Then, arch_process_options() uses this complete list to scan for its
+ *	options, ignoring general/loader-specific ones.
+ * o	Then, the file_type[n].load re-scans for options, using
+ *	KEXEC_ARCH_OPTIONS plus its loader-specific options subset.
+ *	Any unrecognised options cause an error here.
+ *
+ * This is done so that main()'s/arch_process_options()'s getopt_long() calls
+ * don't choose a kernel filename from random arguments to options they don't
+ * recognise -- as they now recognise (if not act upon) all possible options.
+ */
+#define KEXEC_ALL_OPTIONS \
+	KEXEC_ARCH_OPTIONS \
+	{"append", 1, 0, OPT_APPEND},
+
+#define KEXEC_ALL_OPT_STR KEXEC_ARCH_OPT_STR
+
+#endif /* KEXEC_ARCH_CRIS_OPTIONS_H */
kexec/arch/cris/kexec-elf-cris.c +2 -2
index 4b56a20..b48b7b3 100644
@@ -40,8 +40,6 @@
 #include <arch/options.h>
 #include "kexec-cris.h"
 
-#define OPT_APPEND      (OPT_ARCH_MAX+0)
-
 int elf_cris_probe(const char *buf, off_t len)
 {
 	struct mem_ehdr ehdr;
@@ -89,8 +87,10 @@ int elf_cris_load(int argc, char **argv, const char *buf, off_t len,
 		unsigned int regs[16];
 	} cris_regframe;
 
+	/* See options.h -- add any more there, too. */
 	static const struct option options[] = {
 		KEXEC_ARCH_OPTIONS
+		{"append", 1, 0, OPT_APPEND},
 		{ 0, 0, 0, 0 },
 	};
 
kexec/arch/i386/include/arch/options.h +51 -0

68 lines not on this page · raw message

kexec/arch/i386/kexec-beoboot-x86.c +2 -1
index 27c5a2c..6d459ae 100644
@@ -84,7 +84,8 @@ int beoboot_load(int argc, char **argv, const char *buf, off_t UNUSED(len),
 	int debug, real_mode_entry;
 	int opt;
 	int result;
-#define OPT_REAL_MODE	(OPT_ARCH_MAX+0)
+
+	/* See options.h -- add any more there, too. */
 	static const struct option options[] = {
 		KEXEC_ARCH_OPTIONS
 		{ "debug",		0, 0, OPT_DEBUG },
kexec/arch/i386/kexec-bzImage.c +2 -4
index 53a50b3..ae18689 100644
@@ -341,10 +341,8 @@ int bzImage_load(int argc, char **argv, const char *buf, off_t len,
 	int debug, real_mode_entry;
 	int opt;
 	int result;
-#define OPT_APPEND		(OPT_ARCH_MAX+0)
-#define OPT_REUSE_CMDLINE	(OPT_ARCH_MAX+1)
-#define OPT_RAMDISK		(OPT_ARCH_MAX+2)
-#define OPT_REAL_MODE		(OPT_ARCH_MAX+3)
+
+	/* See options.h -- add any more there, too. */
 	static const struct option options[] = {
 		KEXEC_ARCH_OPTIONS
 		{ "debug",		0, 0, OPT_DEBUG },
kexec/arch/i386/kexec-elf-x86.c +1 -6
index 2cb9d11..2e9cf9a 100644
@@ -99,13 +99,8 @@ int elf_x86_load(int argc, char **argv, const char *buf, off_t len,
 #define ARG_STYLE_LINUX 1
 #define ARG_STYLE_NONE  2
 	int opt;
-#define OPT_APPEND		(OPT_ARCH_MAX+0)
-#define OPT_REUSE_CMDLINE	(OPT_ARCH_MAX+1)
-#define OPT_RAMDISK		(OPT_ARCH_MAX+2)
-#define OPT_ARGS_ELF    	(OPT_ARCH_MAX+3)
-#define OPT_ARGS_LINUX  	(OPT_ARCH_MAX+4)
-#define OPT_ARGS_NONE   	(OPT_ARCH_MAX+5)
 
+	/* See options.h -- add any more there, too. */
 	static const struct option options[] = {
 		KEXEC_ARCH_OPTIONS
 		{ "command-line",	1, NULL, OPT_APPEND },
kexec/arch/i386/kexec-multiboot-x86.c +1 -4
index 970de2a..23dab7b 100644
@@ -157,10 +157,7 @@ int multiboot_x86_load(int argc, char **argv, const char *buf, off_t len,
 	uint32_t u;
 	int opt;
 	int modules, mod_command_line_space;
-#define OPT_CL  		(OPT_ARCH_MAX+0)
-#define OPT_REUSE_CMDLINE	(OPT_ARCH_MAX+1)
-#define OPT_MOD 		(OPT_ARCH_MAX+2)
-#define OPT_VGA 		(OPT_ARCH_MAX+3)
+	/* See options.h -- add any more there, too. */
 	static const struct option options[] = {
 		KEXEC_ARCH_OPTIONS
 		{ "command-line",		1, 0, OPT_CL },
kexec/arch/i386/kexec-x86.c +2 -2
index d33a14b..5c701aa 100644
@@ -69,10 +69,10 @@ struct arch_options_t arch_options = {
 int arch_process_options(int argc, char **argv)
 {
 	static const struct option options[] = {
-		KEXEC_ARCH_OPTIONS
+		KEXEC_ALL_OPTIONS
 		{ 0, 			0, NULL, 0 },
 	};
-	static const char short_options[] = KEXEC_ARCH_OPT_STR;
+	static const char short_options[] = KEXEC_ALL_OPT_STR;
 	int opt;
 	unsigned long value;
 	char *end;
kexec/arch/ia64/include/arch/options.h +32 -1
index 3053576..e8754ad 100644
@@ -1,11 +1,42 @@
 #ifndef KEXEC_ARCH_IA64_OPTIONS_H
 #define KEXEC_ARCH_IA64_OPTIONS_H
 
-#define OPT_ARCH_MAX   (OPT_MAX+0)
+#define OPT_ARCH_MAX	(OPT_MAX+0)
+#define OPT_APPEND	(OPT_ARCH_MAX+0)
+#define OPT_RAMDISK	(OPT_ARCH_MAX+1)
+#define OPT_NOIO	(OPT_ARCH_MAX+2)
+#define OPT_VMM		(OPT_ARCH_MAX+3)
 
+/* Options relevant to the architecture (excluding loader-specific ones),
+ * in this case none:
+ */
 #define KEXEC_ARCH_OPTIONS \
 	KEXEC_OPTIONS \
 
 #define KEXEC_ARCH_OPT_STR KEXEC_OPT_STR ""
 
+/* The following two #defines list ALL of the options added by all of the
+ * architecture's loaders.
+ * o	main() uses this complete list to scan for its options, ignoring
+ *	arch-specific/loader-specific ones.
+ * o	Then, arch_process_options() uses this complete list to scan for its
+ *	options, ignoring general/loader-specific ones.
+ * o	Then, the file_type[n].load re-scans for options, using
+ *	KEXEC_ARCH_OPTIONS plus its loader-specific options subset.
+ *	Any unrecognised options cause an error here.
+ *
+ * This is done so that main()'s/arch_process_options()'s getopt_long() calls
+ * don't choose a kernel filename from random arguments to options they don't
+ * recognise -- as they now recognise (if not act upon) all possible options.
+ */
+#define KEXEC_ALL_OPTIONS			\
+	KEXEC_ARCH_OPTIONS			\
+	{"command-line", 1, 0, OPT_APPEND},	\
+	{"append",	 1, 0, OPT_APPEND},	\
+	{"initrd",	 1, 0, OPT_RAMDISK},	\
+	{"noio",	 0, 0, OPT_NOIO},	\
+	{"vmm",		 1, 0, OPT_VMM},	\
+
+#define KEXEC_ALL_OPT_STR KEXEC_OPT_STR
+
 #endif /* KEXEC_ARCH_IA64_OPTIONS_H */
kexec/arch/ia64/kexec-elf-ia64.c +2 -5
index 3bb5a4d..8da061e 100644
@@ -45,11 +45,6 @@
 #include "crashdump-ia64.h"
 #include <arch/options.h>
 
-#define OPT_APPEND	(OPT_ARCH_MAX+0)
-#define OPT_RAMDISK	(OPT_ARCH_MAX+1)
-#define OPT_NOIO	(OPT_ARCH_MAX+2)
-#define OPT_VMM		(OPT_ARCH_MAX+3)
-
 static const int probe_debug = 0;
 extern unsigned long saved_efi_memmap_size;
 
@@ -136,6 +131,8 @@ int elf_ia64_load(int argc, char **argv, const char *buf, off_t len,
 	int result;
 	int opt;
 	char *efi_memmap_buf, *boot_param;
+
+	/* See options.h -- add any more there, too. */
 	static const struct option options[] = {
 		KEXEC_ARCH_OPTIONS
 		{"command-line", 1, 0, OPT_APPEND},
kexec/arch/mips/include/arch/options.h +26 -1
index bc5f706..07b4f63 100644
@@ -1,11 +1,36 @@
 #ifndef KEXEC_ARCH_MIPS_OPTIONS_H
 #define KEXEC_ARCH_MIPS_OPTIONS_H
 
-#define OPT_ARCH_MAX   (OPT_MAX+0)
+#define OPT_ARCH_MAX	(OPT_MAX+0)
+#define OPT_APPEND	(OPT_ARCH_MAX+0)
 
+/* Options relevant to the architecture (excluding loader-specific ones),
+ * in this case none:
+ */
 #define KEXEC_ARCH_OPTIONS \
 	KEXEC_OPTIONS \
 
 #define KEXEC_ARCH_OPT_STR KEXEC_OPT_STR ""
 
+/* The following two #defines list ALL of the options added by all of the
+ * architecture's loaders.
+ * o	main() uses this complete list to scan for its options, ignoring
+ *	arch-specific/loader-specific ones.
+ * o	Then, arch_process_options() uses this complete list to scan for its
+ *	options, ignoring general/loader-specific ones.
+ * o	Then, the file_type[n].load re-scans for options, using
+ *	KEXEC_ARCH_OPTIONS plus its loader-specific options subset.
+ *	Any unrecognised options cause an error here.
+ *
+ * This is done so that main()'s/arch_process_options()'s getopt_long() calls
+ * don't choose a kernel filename from random arguments to options they don't
+ * recognise -- as they now recognise (if not act upon) all possible options.
+ */
+#define KEXEC_ALL_OPTIONS			\
+	KEXEC_ARCH_OPTIONS		       	\
+	{"command-line", 1, 0, OPT_APPEND},	\
+	{"append",	 1, 0, OPT_APPEND},
+
+#define KEXEC_ALL_OPT_STR KEXEC_ARCH_OPT_STR
+
 #endif /* KEXEC_ARCH_MIPS_OPTIONS_H */
kexec/arch/mips/kexec-elf-mips.c +2 -1
index ce6bf0c..a9c865e 100644
@@ -35,7 +35,6 @@ static const int probe_debug = 0;
 #define BOOTLOADER         "kexec"
 #define MAX_COMMAND_LINE   256
 #define UPSZ(X) ((sizeof(X) + 3) & ~3)
-#define OPT_APPEND	(OPT_ARCH_MAX+0)
 static char cmdline_buf[256] = "kexec ";
 
 int elf_mips_probe(const char *buf, off_t len)
@@ -82,6 +81,8 @@ int elf_mips_load(int argc, char **argv, const char *buf, off_t len,
 	unsigned long cmdline_addr;
 	size_t i;
 	unsigned long bss_start = 0, bss_size = 0;
+
+	/* See options.h if adding any more options. */
 	static const struct option options[] = {
 		KEXEC_ARCH_OPTIONS
 		{"command-line", 1, 0, OPT_APPEND},
kexec/arch/ppc/include/arch/options.h +34 -0
index df14db5..f646ccc 100644
@@ -3,9 +3,43 @@
 
 #define OPT_ARCH_MAX   (OPT_MAX+0)
 
+/* All 'local' loader options: */
+#define OPT_APPEND      (OPT_ARCH_MAX+0)
+#define OPT_GAMECUBE    (OPT_ARCH_MAX+1)
+#define OPT_DTB         (OPT_ARCH_MAX+2)
+#define OPT_NODES       (OPT_ARCH_MAX+3)
+
+/* Options relevant to the architecture (excluding loader-specific ones),
+ * in this case none:
+ */
 #define KEXEC_ARCH_OPTIONS \
 	KEXEC_OPTIONS \
 
 #define KEXEC_ARCH_OPT_STR KEXEC_OPT_STR ""
 
+/* The following two #defines list ALL of the options added by all of the
+ * architecture's loaders.
+ * o	main() uses this complete list to scan for its options, ignoring
+ *	arch-specific/loader-specific ones.
+ * o	Then, arch_process_options() uses this complete list to scan for its
+ *	options, ignoring general/loader-specific ones.
+ * o	Then, the file_type[n].load re-scans for options, using
+ *	KEXEC_ARCH_OPTIONS plus its loader-specific options subset.
+ *	Any unrecognised options cause an error here.
+ *
+ * This is done so that main()'s/arch_process_options()'s getopt_long() calls
+ * don't choose a kernel filename from random arguments to options they don't
+ * recognise -- as they now recognise (if not act upon) all possible options.
+ */
+#define KEXEC_ALL_OPTIONS \
+	KEXEC_ARCH_OPTIONS \
+	{"command-line", 1, 0, OPT_APPEND},\
+	{"append",	 1, 0, OPT_APPEND},\
+	{"gamecube",	 1, 0, OPT_GAMECUBE},\
+	{"dtb",	    1, 0, OPT_DTB},\
+	{"reuse-node",	   1, 0, OPT_NODES},\
+	{"debug",	 0, 0, OPT_DEBUG},
+
+#define KEXEC_ALL_OPT_STR KEXEC_OPT_STR
+
 #endif /* KEXEC_ARCH_PPC_OPTIONS_H */
kexec/arch/ppc/kexec-dol-ppc.c +2 -2
index 83c122a..8de5293 100644
@@ -335,8 +335,8 @@ int dol_ppc_load(int argc, char **argv, const char *buf, off_t UNUSED(len),
 	unsigned long lowest_start;
 	int i, j, k;
 	int opt;
-#define OPT_APPEND      (OPT_ARCH_MAX+0)
 
+	/* See options.h -- add any more there, too. */
         static const struct option options[] = {
                 KEXEC_ARCH_OPTIONS
                 {"debug",        0, 0, OPT_DEBUG},
@@ -344,7 +344,7 @@ int dol_ppc_load(int argc, char **argv, const char *buf, off_t UNUSED(len),
                 {"append",       1, 0, OPT_APPEND},
                 {0, 0, 0, 0},
         };
-	static const char short_options[] = KEXEC_ARCH_OPT_STR "d";
+	static const char short_options[] = KEXEC_ARCH_OPT_STR;
 
 	/*
 	 * Parse the command line arguments
kexec/arch/ppc/kexec-elf-ppc.c +2 -5
index a54a5d5..cb2c07a 100644
@@ -113,10 +113,7 @@ static void gamecube_hack_addresses(struct mem_ehdr *ehdr)
 	}
 }
 
-#define OPT_APPEND	(OPT_ARCH_MAX+0)
-#define OPT_GAMECUBE	(OPT_ARCH_MAX+1)
-#define OPT_DTB		(OPT_ARCH_MAX+2)
-#define OPT_NODES	(OPT_ARCH_MAX+3)
+/* See options.h -- add any more there, too. */
 static const struct option options[] = {
 	KEXEC_ARCH_OPTIONS
 	{"command-line", 1, 0, OPT_APPEND},
@@ -126,7 +123,7 @@ static const struct option options[] = {
 	{"reuse-node",     1, 0, OPT_NODES},
 	{0, 0, 0, 0},
 };
-static const char short_options[] = KEXEC_ARCH_OPT_STR "d";
+static const char short_options[] = KEXEC_ARCH_OPT_STR;
 
 void elf_ppc_usage(void)
 {
kexec/arch/ppc/kexec-uImage-ppc.c +2 -4
index 0a655a3..45cde2f 100644
@@ -14,9 +14,7 @@
 #include "fixup_dtb.h"
 #include <kexec-uImage.h>
 
-#define OPT_APPEND      (OPT_ARCH_MAX+0)
-#define OPT_DTB         (OPT_ARCH_MAX+1)
-#define OPT_NODES       (OPT_ARCH_MAX+2)
+/* See options.h -- add any more there, too. */
 static const struct option options[] = {
 	KEXEC_ARCH_OPTIONS
 	{"command-line",	1, 0, OPT_APPEND},
@@ -25,7 +23,7 @@ static const struct option options[] = {
 	{"reuse-node",	1, 0, OPT_NODES},
 	{0, 0, 0, 0},
 };
-static const char short_options[] = KEXEC_ARCH_OPT_STR "d";
+static const char short_options[] = KEXEC_ARCH_OPT_STR;
 
 void uImage_ppc_usage(void)
 {
kexec/arch/ppc64/include/arch/options.h +35 -2
index 973cd4f..8f11c83 100644
@@ -1,13 +1,46 @@
 #ifndef KEXEC_ARCH_PPC64_OPTIONS_H
 #define KEXEC_ARCH_PPC64_OPTIONS_H
 
-#define OPT_ARCH_MAX   (OPT_MAX+0)
-#define OPT_ELF64_CORE  (OPT_MAX+1)
+#define OPT_ELF64_CORE		(OPT_MAX+0)
+#define OPT_ARCH_MAX		(OPT_MAX+1)
 
+/* All 'local' loader options: */
+#define OPT_APPEND		(OPT_ARCH_MAX+0)
+#define OPT_RAMDISK		(OPT_ARCH_MAX+1)
+#define OPT_DEVICETREEBLOB	(OPT_ARCH_MAX+2)
+#define OPT_ARGS_IGNORE		(OPT_ARCH_MAX+3)
+
+/* Options relevant to the architecture (excluding loader-specific ones): */
 #define KEXEC_ARCH_OPTIONS \
 	KEXEC_OPTIONS \
 	{ "elf64-core-headers", 0, 0, OPT_ELF64_CORE }, \
 
 #define KEXEC_ARCH_OPT_STR KEXEC_OPT_STR ""
 
+/* The following two #defines list ALL of the options added by all of the
+ * architecture's loaders.
+ * o	main() uses this complete list to scan for its options, ignoring
+ *	arch-specific/loader-specific ones.
+ * o	Then, arch_process_options() uses this complete list to scan for its
+ *	options, ignoring general/loader-specific ones.
+ * o	Then, the file_type[n].load re-scans for options, using
+ *	KEXEC_ARCH_OPTIONS plus its loader-specific options subset.
+ *	Any unrecognised options cause an error here.
+ *
+ * This is done so that main()'s/arch_process_options()'s getopt_long() calls
+ * don't choose a kernel filename from random arguments to options they don't
+ * recognise -- as they now recognise (if not act upon) all possible options.
+ */
+#define KEXEC_ALL_OPTIONS \
+	KEXEC_ARCH_OPTIONS				\
+	{ "command-line",       1, NULL, OPT_APPEND },	\
+	{ "append",             1, NULL, OPT_APPEND },	\
+	{ "ramdisk",            1, NULL, OPT_RAMDISK },	\
+	{ "initrd",             1, NULL, OPT_RAMDISK },		\
+	{ "devicetreeblob",     1, NULL, OPT_DEVICETREEBLOB },	\
+	{ "args-linux",         0, NULL, OPT_ARGS_IGNORE },
+
+#define KEXEC_ALL_OPT_STR KEXEC_OPT_STR
+
+
 #endif /* KEXEC_ARCH_PPC64_OPTIONS_H */
kexec/arch/ppc64/kexec-elf-ppc64.c +7 -5
index d65a5d5..ab6da7c 100644
@@ -93,11 +93,7 @@ int elf_ppc64_load(int argc, char **argv, const char *buf, off_t len,
 	uint32_t my_run_at_load;
 	unsigned int slave_code[256/sizeof (unsigned int)], master_entry;
 
-#define OPT_APPEND     (OPT_ARCH_MAX+0)
-#define OPT_RAMDISK     (OPT_ARCH_MAX+1)
-#define OPT_DEVICETREEBLOB     (OPT_ARCH_MAX+2)
-#define OPT_ARGS_IGNORE		(OPT_ARCH_MAX+3)
-
+	/* See options.h -- add any more there, too. */
 	static const struct option options[] = {
 		KEXEC_ARCH_OPTIONS
 		{ "command-line",       1, NULL, OPT_APPEND },
@@ -347,5 +343,11 @@ int elf_ppc64_load(int argc, char **argv, const char *buf, off_t len,
 
 void elf_ppc64_usage(void)
 {
+	fprintf(stderr, "     --command-line=<Command line> command line to append.\n");
+	fprintf(stderr, "     --append=<Command line> same as --command-line.\n");
+	fprintf(stderr, "     --ramdisk=<filename> Initial RAM disk.\n");
+	fprintf(stderr, "     --initrd=<filename> same as --ramdisk.\n");
+	fprintf(stderr, "     --devicetreeblob=<filename> Specify device tree blob file.\n");
+
 	fprintf(stderr, "elf support is still broken\n");
 }
kexec/arch/ppc64/kexec-ppc64.c +5 -7
index f9dc65b..48ea421 100644
@@ -735,11 +735,6 @@ int file_types = sizeof(file_type) / sizeof(file_type[0]);
 
 void arch_usage(void)
 {
-	fprintf(stderr, "     --command-line=<Command line> command line to append.\n");
-	fprintf(stderr, "     --append=<Command line> same as --command-line.\n");
-	fprintf(stderr, "     --ramdisk=<filename> Initial RAM disk.\n");
-	fprintf(stderr, "     --initrd=<filename> same as --ramdisk.\n");
-	fprintf(stderr, "     --devicetreeblob=<filename> Specify device tree blob file.\n");
 	fprintf(stderr, "     --elf64-core-headers Prepare core headers in ELF64 format\n");
 }
 
@@ -749,11 +744,14 @@ struct arch_options_t arch_options = {
 
 int arch_process_options(int argc, char **argv)
 {
+	/* We look for all options so getopt_long doesn't start reordering
+	 * argv[] before file_type[n].load() gets a look in.
+	 */
 	static const struct option options[] = {
-		KEXEC_ARCH_OPTIONS
+		KEXEC_ALL_OPTIONS
 		{ 0, 0, NULL, 0 },
 	};
-	static const char short_options[] = KEXEC_ARCH_OPT_STR;
+	static const char short_options[] = KEXEC_ALL_OPT_STR;
 	int opt;
 
 	opterr = 0; /* Don't complain about unrecognized options here */
kexec/arch/s390/include/arch/options.h +27 -1
index 2979617..b57539f 100644
@@ -1,11 +1,37 @@
 #ifndef KEXEC_ARCH_S390_OPTIONS_H
 #define KEXEC_ARCH_S390_OPTIONS_H
 
-#define OPT_ARCH_MAX   (OPT_MAX+0)
+#define OPT_ARCH_MAX	(OPT_MAX+0)
+#define OPT_APPEND	OPT_MAX+0
+#define OPT_RAMDISK	OPT_MAX+1
 
+/* Options relevant to the architecture (excluding loader-specific ones),
+ * in this case none:
+ */
 #define KEXEC_ARCH_OPTIONS \
 	KEXEC_OPTIONS \
 
 #define KEXEC_ARCH_OPT_STR KEXEC_OPT_STR ""
 
+/* The following two #defines list ALL of the options added by all of the
+ * architecture's loaders.
+ * o	main() uses this complete list to scan for its options, ignoring
+ *	arch-specific/loader-specific ones.
+ * o	Then, arch_process_options() uses this complete list to scan for its
+ *	options, ignoring general/loader-specific ones.
+ * o	Then, the file_type[n].load re-scans for options, using
+ *	KEXEC_ARCH_OPTIONS plus its loader-specific options subset.
+ *	Any unrecognised options cause an error here.
+ *
+ * This is done so that main()'s/arch_process_options()'s getopt_long() calls
+ * don't choose a kernel filename from random arguments to options they don't
+ * recognise -- as they now recognise (if not act upon) all possible options.
+ */
+#define KEXEC_ALL_OPTIONS				\
+	KEXEC_ARCH_OPTIONS				\
+	{"command-line",     1, 0, OPT_APPEND},		\
+	{"initrd",	     1, 0, OPT_RAMDISK},
+
+#define KEXEC_ALL_OPT_STR KEXEC_ARCH_OPT_STR
+
 #endif /* KEXEC_ARCH_S390_OPTIONS_H */
kexec/arch/s390/kexec-image.c +1 -3
index 13e550d..7ef8e29 100644
@@ -19,9 +19,7 @@
 #include <getopt.h>
 #include "../../kexec.h"
 #include "kexec-s390.h"
-
-#define OPT_APPEND     OPT_MAX+0
-#define OPT_RAMDISK    OPT_MAX+1
+#include <arch/options.h>
 
 int
 image_s390_load(int argc, char **argv, const char *kernel_buf,
kexec/arch/sh/include/arch/options.h +23 -3
index e02960d..4bdd6a3 100644
@@ -7,16 +7,36 @@
 #define OPT_NBSD_HOWTO   (OPT_ARCH_MAX+3)
 #define OPT_NBSD_MROOT   (OPT_ARCH_MAX+4)
 
-
+/* Options relevant to the architecture (excluding loader-specific ones): */
 #define KEXEC_ARCH_OPTIONS \
 	KEXEC_OPTIONS \
         {"command-line",   1, 0, OPT_APPEND}, \
         {"append",         1, 0, OPT_APPEND}, \
         {"empty-zero",     1, 0, OPT_APPEND}, \
         {"howto",          1, 0, OPT_NBSD_HOWTO}, \
-        {"miniroot",       1, 0, OPT_NBSD_MROOT}, \
-
+        {"miniroot",       1, 0, OPT_NBSD_MROOT},
+/* These options seem to be loader-specific rather than cris-specific, so
+ * ought to be moved to KEXEC_ALL_OPTIONS below and parsed in the relevant 
+ * loader, e.g. kexec-netbsd-sh.c
+ */
 
 #define KEXEC_ARCH_OPT_STR KEXEC_OPT_STR ""
 
+/* The following two #defines list ALL of the options added by all of the
+ * architecture's loaders.
+ * o	main() uses this complete list to scan for its options, ignoring
+ *	arch-specific/loader-specific ones.
+ * o	Then, arch_process_options() uses this complete list to scan for its
+ *	options, ignoring general/loader-specific ones.
+ * o	Then, the file_type[n].load re-scans for options, using
+ *	KEXEC_ARCH_OPTIONS plus its loader-specific options subset.
+ *	Any unrecognised options cause an error here.
+ *
+ * This is done so that main()'s/arch_process_options()'s getopt_long() calls
+ * don't choose a kernel filename from random arguments to options they don't
+ * recognise -- as they now recognise (if not act upon) all possible options.
+ */
+#define KEXEC_ALL_OPTIONS KEXEC_ARCH_OPTIONS
+#define KEXEC_ALL_OPT_STR KEXEC_ARCH_OPT_STR
+
 #endif /* KEXEC_ARCH_SH_OPTIONS_H */
kexec/arch/sh/kexec-sh.c +6 -1
index 1ad3d38..4b21ee8 100644
@@ -97,8 +97,13 @@ void arch_usage(void)
 
 int arch_process_options(int argc, char **argv)
 {
+	/* The common options amongst loaders (e.g. --append) should be read
+	 * here, and the loader-specific options (e.g. NetBSD stuff) should
+	 * then be re-parsed in the loader.
+	 * (e.g. in kexec-netbsd-sh.c, for example.)
+	 */
 	static const struct option options[] = {
-		KEXEC_ARCH_OPTIONS
+		KEXEC_ALL_OPTIONS
 		{ 0, 			0, NULL, 0 },
 	};
 	static const char short_options[] = KEXEC_ARCH_OPT_STR;
kexec/arch/x86_64/include/arch/options.h deleted +0 -22
deleted file mode 100644
index 75065b9..0000000
@@ -1,22 +0,0 @@
-#ifndef KEXEC_ARCH_X86_64_OPTIONS_H
-#define KEXEC_ARCH_X86_64_OPTIONS_H
-
-#define OPT_RESET_VGA      (OPT_MAX+0)
-#define OPT_SERIAL         (OPT_MAX+1)
-#define OPT_SERIAL_BAUD    (OPT_MAX+2)
-#define OPT_CONSOLE_VGA    (OPT_MAX+3)
-#define OPT_CONSOLE_SERIAL (OPT_MAX+4)
-#define OPT_ARCH_MAX       (OPT_MAX+5)
-
-#define KEXEC_ARCH_OPTIONS \
-	KEXEC_OPTIONS \
-	{ "reset-vga",	    0, 0, OPT_RESET_VGA }, \
-	{ "serial",	    1, 0, OPT_SERIAL }, \
-	{ "serial-baud",    1, 0, OPT_SERIAL_BAUD }, \
-	{ "console-vga",    0, 0, OPT_CONSOLE_VGA }, \
-	{ "console-serial", 0, 0, OPT_CONSOLE_SERIAL }, \
-
-#define KEXEC_ARCH_OPT_STR KEXEC_OPT_STR ""
-
-#endif /* KEXEC_ARCH_X86_64_OPTIONS_H */
-
kexec/arch/x86_64/include/arch/options.h new file +1 -0
new file mode 120000
index 0000000..047b0f9
@@ -0,0 +1 @@
+../../../i386/include/arch/options.h
\ No newline at end of file
kexec/arch/x86_64/kexec-elf-x86_64.c +1 -6
index 20c7d77..a8204bc 100644
@@ -98,13 +98,8 @@ int elf_x86_64_load(int argc, char **argv, const char *buf, off_t len,
 #define ARG_STYLE_LINUX 1
 #define ARG_STYLE_NONE  2
 	int opt;
-#define OPT_APPEND		(OPT_ARCH_MAX+0)
-#define OPT_REUSE_CMDLINE	(OPT_ARCH_MAX+1)
-#define OPT_RAMDISK		(OPT_ARCH_MAX+2)
-#define OPT_ARGS_ELF		(OPT_ARCH_MAX+3)
-#define OPT_ARGS_LINUX		(OPT_ARCH_MAX+4)
-#define OPT_ARGS_NONE		(OPT_ARCH_MAX+5)
 
+	/* See options.h and add any new options there too! */
 	static const struct option options[] = {
 		KEXEC_ARCH_OPTIONS
 		{ "command-line",	1, NULL, OPT_APPEND },
kexec/arch/x86_64/kexec-x86_64.c +2 -2
index 49bfd36..a4f2d10 100644
@@ -74,10 +74,10 @@ static struct {
 int arch_process_options(int argc, char **argv)
 {
 	static const struct option options[] = {
-		KEXEC_ARCH_OPTIONS
+		KEXEC_ALL_OPTIONS
 		{ 0, 			0, NULL, 0 },
 	};
-	static const char short_options[] = KEXEC_ARCH_OPT_STR;
+	static const char short_options[] = KEXEC_ALL_OPT_STR;
 	int opt;
 	unsigned long value;
 	char *end;
kexec/kexec.c +2 -2
index e9a13a7..f4cf49f 100644
@@ -1062,10 +1062,10 @@ int main(int argc, char *argv[])
 	int result = 0;
 	int fileind;
 	static const struct option options[] = {
-		KEXEC_ARCH_OPTIONS
+		KEXEC_ALL_OPTIONS
 		{ 0, 0, 0, 0},
 	};
-	static const char short_options[] = KEXEC_OPT_STR;
+	static const char short_options[] = KEXEC_ALL_OPT_STR;
 
 	arch_init();
 
Signature · 7 lines
1.6.3.3


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
#1 lore raw
Quoted 7 lines · Matt Evans

Hi,

In playing with kexec-tools I've noticed various problems with the argument passing, meaning one has to be careful to use certain forms of arguments and present them in a certain order.

Quoted 8 lines

The arguments end up being parsed three times, each getting more specific than the last. main() looks for general args, arch_process_options() looks for options common to all loaders for the arch, and then finally many file_type load() methods check for options specific to that filetype.

As GNU getopt works, it re-orders the argv[] pushing any args it doesn't recognise to the end. This includes arguments to valid options which are simply not recognised the first time around.

This is a long standing problem; I found it years ago. My patch saved a copy of the original argc and then passed that original version to each phase instead of the mangled vector.

I hope your suggestion is accepted or prompts a solution, as the problem can be quite vexing...

Signature · 7 lines
David N. Lombard, Intel, Irvine, CA
I do not speak for Intel Corporation; all comments are strictly my own.

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
#2 lore raw
In reply to Matt Evans
Quoted 48 lines · Matt Evans

Hi,

In playing with kexec-tools I've noticed various problems with the argument passing, meaning one has to be careful to use certain forms of arguments and present them in a certain order.

The arguments end up being parsed three times, each getting more specific than the last. main() looks for general args, arch_process_options() looks for options common to all loaders for the arch, and then finally many file_type load() methods check for options specific to that filetype.

As GNU getopt works, it re-orders the argv[] pushing any args it doesn't recognise to the end. This includes arguments to valid options which are simply not recognised the first time around.

For example, this does not work:

  # ./kexec -l --append "init=/bin/foo" /boot/vmlinux
  Cannot open `init=/bin/foo': No such file or directory

but this does:

  # ./kexec -l --append="init=/bin/foo" /boot/vmlinux
  <joy>

Using the --opt<space>arg variant results in the first non-option argument in main() being "init=/bin/foo" which is then used as the kernel filename, failing. Also, the order affects things in unintuitive ways:

  # ./kexec -l /boot/vmlinux --append "init=/bin/foo" 
<appears to load correctly, but command line appended with "/boot/vmlinux"!>

This behaviour is avoided by using the --opt= forms, but getopt does allow both and hence the user can have a fairly frustrating experience. (Doing something unexpected (ex. 3) is more annoying than an error exit (ex. 1) in many cases.)

The fix presented here is to create a new #define to encapsulate all possible options for an architecture build. The intention is that this set includes all possible loaders' all possible options.

main() walks through the options and silently ignores any non-general options (BUT respects that "--arg foo" should remain together, as getopt_long() does now recognise it). arch_process_options() walks through them again and responds to any arch-specific options, again ignoring but respecting any non-arch options. Finally the loader may look for its options, and find them in-order and present. Any outside of this combined set are complained-about as usual.

Quoted 3 lines

So, comments please. Is this a reasonable way to do it or is there an obvious better way I've missed? :-) So far I have been able to test on x86(32,64) and ppc(32,64) but none of the others. :(

This seems reasonable to me.

I've compiled tested the code on all architectures except cris (I don't have my build environment at the moment). I found a minor problem on arm which I have noted below.

[snip]

Quoted 28 lines
kexec/arch/arm/include/arch/options.h +15 -0
index 248230b..a76539e 100644
@@ -3,9 +3,38 @@
 
 #define OPT_ARCH_MAX   (OPT_MAX+0)
 
+#define OPT_APPEND	'a'
+#define OPT_RAMDISK	'r'
+
+/* Options relevant to the architecture (excluding loader-specific ones),
+ * in this case none:
+ */
 #define KEXEC_ARCH_OPTIONS \
 	KEXEC_OPTIONS \
 
 #define KEXEC_ARCH_OPT_STR KEXEC_OPT_STR ""
 
+/* The following two #defines list ALL of the options added by all of the
+ * architecture's loaders.
+ * o	main() uses this complete list to scan for its options, ignoring
+ *	arch-specific/loader-specific ones.
+ * o	Then, arch_process_options() uses this complete list to scan for its
+ *	options, ignoring general/loader-specific ones.
+ * o	Then, the file_type[n].load re-scans for options, using
+ *	KEXEC_ARCH_OPTIONS plus its loader-specific options subset.
+ *	Any unrecognised options cause an error here.

kexec/arch/arm/include/arch/options.h line 35

Quoted 10 lines
kexec/arch/arm/include/arch/options.h +10 -0
+ *
+ * This is done so that main()'s/arch_process_options()'s getopt_long() calls
+ * don't choose a kernel filename from random arguments to options they don't
+ * recognise -- as they now recognise (if not act upon) all possible options.
+ */
+#define KEXEC_ALL_OPTIONS \
+	KEXEC_ARCH_OPTIONS
+	{ "command-line",	1, 0, OPT_APPEND },
+	{ "append",		1, 0, OPT_APPEND },
+	{ "initrd",		1, 0, OPT_RAMDISK },

The above 4 lines seem to be missing a trailing '\'

kexec/arch/arm/include/arch/options.h line 40

Quoted 5 lines
kexec/arch/arm/include/arch/options.h +4 -0
+	{ "ramdisk",		1, 0, OPT_RAMDISK },
+
+#define KEXEC_ALL_OPT_STR KEXEC_ARCH_OPT_STR "a:r:"
+
 #endif /* KEXEC_ARCH_ARM_OPTIONS_H */

[snip]

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

#3 lore raw
Quoted 33 lines · In message <[email protected]> you wrote:
Quoted 32 lines · Matt Evans

Hi,

In playing with kexec-tools I've noticed various problems with the argument passing, meaning one has to be careful to use certain forms of arguments and present them in a certain order.

The arguments end up being parsed three times, each getting more specific than the last. main() looks for general args, arch_process_options() looks for options common to all loaders for the arch, and then finally many file_type load() methods check for options specific to that filetype.

As GNU getopt works, it re-orders the argv[] pushing any args it doesn't recognise to the end. This includes arguments to valid options which are simply not recognised the first time around.

For example, this does not work:

  # ./kexec -l --append "init=/bin/foo" /boot/vmlinux
  Cannot open `init=/bin/foo': No such file or directory

but this does:

  # ./kexec -l --append="init=/bin/foo" /boot/vmlinux
  <joy>

Using the --opt<space>arg variant results in the first non-option argument in main() being "init=/bin/foo" which is then used as the kernel filename, failing. Also, the order affects things in unintuitive ways:

  # ./kexec -l /boot/vmlinux --append "init=/bin/foo" 
<appears to load correctly, but command line appended with "/boot/vmlinux"!
Quoted 6 lines
Quoted 6 lines

This behaviour is avoided by using the --opt= forms, but getopt does allow both and hence the user can have a fairly frustrating experience. (Doing something unexpected (ex. 3) is more annoying than an error exit (ex. 1) in many cases.)

The fix presented here is to create a new #define to encapsulate all possib

le

Quoted 15 lines
Quoted 11 lines

options for an architecture build. The intention is that this set includes all possible loaders' all possible options.

main() walks through the options and silently ignores any non-general options (BUT respects that "--arg foo" should remain together, as getopt_long() does now recognise it). arch_process_options() walks through them again and responds to any arch-specific options, again ignoring but respecting any non-arch options. Finally the loader may look for its options, and find them in-order and present. Any outside of this combined set are complained-about as usual.

Quoted 3 lines

So, comments please. Is this a reasonable way to do it or is there an obvious better way I've missed? :-) So far I have been able to test on x86(32,64) and ppc(32,64) but none of the others. :(

Quoted 5 lines

This seems reasonable to me.

I've compiled tested the code on all architectures except cris (I don't have my build environment at the moment). I found a minor problem on arm which I have noted below.

I suspect it'll break someones kexec scripts, so maybe we take this patch (or something like it) but bump up the release revision to 2.1?

Mikey

Quoted 4 lines

[snip]

kexec/arch/arm/include

Quoted 1 line
kexec/arch/arm/include renamed from kexec/arch/arm/include/arch/options.h +0 -0

/arch/options.h

Quoted 16 lines
Quoted 16 lines

index 248230b..a76539e 100644

kexec/arch/arm/include/arch/options.h +6 -0
@@ -3,9 +3,38 @@
 
 #define OPT_ARCH_MAX   (OPT_MAX+0)
 
+#define OPT_APPEND	'a'
+#define OPT_RAMDISK	'r'
+
+/* Options relevant to the architecture (excluding loader-specific ones),
+ * in this case none:
+ */
 #define KEXEC_ARCH_OPTIONS \
 	KEXEC_OPTIONS \
 
Quoted 5 lines

kexec/arch/arm/include/arch/options.h

Quoted 5 lines
kexec/arch/arm/include/arch/options.h +3 -0
 #define KEXEC_ARCH_OPT_STR KEXEC_OPT_STR ""
 
+/* The following two #defines list ALL of the options added by all of the
+ * architecture's loaders.
+ * o	main() uses this complete list to scan for its options, ignorin

g

Quoted 2 lines

kexec/arch/arm/include/arch/options.h line 21

Quoted 2 lines
kexec/arch/arm/include/arch/options.h +2 -0
+ *	arch-specific/loader-specific ones.
+ * o	Then, arch_process_options() uses this complete list to scan fo

r its

Quoted 6 lines

kexec/arch/arm/include/arch/options.h

Quoted 6 lines
kexec/arch/arm/include/arch/options.h +6 -0
+ *	options, ignoring general/loader-specific ones.
+ * o	Then, the file_type[n].load re-scans for options, using
+ *	KEXEC_ARCH_OPTIONS plus its loader-specific options subset.
+ *	Any unrecognised options cause an error here.
+ *
+ * This is done so that main()'s/arch_process_options()'s getopt_long() ca

lls

Quoted 1 line

kexec/arch/arm/include/arch/options.h line 28

Quoted 1 line
kexec/arch/arm/include/arch/options.h +1 -0
+ * don't choose a kernel filename from random arguments to options they do

n't

Quoted 1 line

kexec/arch/arm/include/arch/options.h line 29

Quoted 1 line
kexec/arch/arm/include/arch/options.h +1 -0
+ * recognise -- as they now recognise (if not act upon) all possible optio

ns.

Quoted 9 lines

kexec/arch/arm/include/arch/options.h line 35

Quoted 6 lines
kexec/arch/arm/include/arch/options.h +6 -0
+ */
+#define KEXEC_ALL_OPTIONS \
+	KEXEC_ARCH_OPTIONS
+	{ "command-line",	1, 0, OPT_APPEND },
+	{ "append",		1, 0, OPT_APPEND },
+	{ "initrd",		1, 0, OPT_RAMDISK },

The above 4 lines seem to be missing a trailing '\'

Quoted 8 lines
Quoted 5 lines
kexec/arch/arm/include/arch/options.h +4 -0
+	{ "ramdisk",		1, 0, OPT_RAMDISK },
+
+#define KEXEC_ALL_OPT_STR KEXEC_ARCH_OPT_STR "a:r:"
+
 #endif /* KEXEC_ARCH_ARM_OPTIONS_H */

[snip]

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

author
#4 lore raw
Quoted 53 lines · Michael Neuling
Quoted 29 lines · In message <[email protected]> you wrote:
Quoted 28 lines · Matt Evans

Hi,

In playing with kexec-tools I've noticed various problems with the argument passing, meaning one has to be careful to use certain forms of arguments and present them in a certain order.

The arguments end up being parsed three times, each getting more specific than the last. main() looks for general args, arch_process_options() looks for options common to all loaders for the arch, and then finally many file_type load() methods check for options specific to that filetype.

As GNU getopt works, it re-orders the argv[] pushing any args it doesn't recognise to the end. This includes arguments to valid options which are simply not recognised the first time around.

For example, this does not work:

  # ./kexec -l --append "init=/bin/foo" /boot/vmlinux
  Cannot open `init=/bin/foo': No such file or directory

but this does:

  # ./kexec -l --append="init=/bin/foo" /boot/vmlinux
  <joy>

Using the --opt<space>arg variant results in the first non-option argument in main() being "init=/bin/foo" which is then used as the kernel filename, failing. Also, the order affects things in unintuitive ways:

Quoted 8 lines
Quoted 8 lines
  # ./kexec -l /boot/vmlinux --append "init=/bin/foo" 
<appears to load correctly, but command line appended with "/boot/vmlinux"!
This behaviour is avoided by using the --opt= forms, but getopt does allow
both and hence the user can have a fairly frustrating experience.  (Doing
something unexpected (ex. 3) is more annoying than an error exit (ex. 1)
in many cases.)

The fix presented here is to create a new #define to encapsulate all possib

le

Quoted 14 lines
Quoted 14 lines

options for an architecture build. The intention is that this set includes all possible loaders' all possible options.

main() walks through the options and silently ignores any non-general options (BUT respects that "--arg foo" should remain together, as getopt_long() does now recognise it). arch_process_options() walks through them again and responds to any arch-specific options, again ignoring but respecting any non-arch options. Finally the loader may look for its options, and find them in-order and present. Any outside of this combined set are complained-about as usual.

So, comments please. Is this a reasonable way to do it or is there an obvious better way I've missed? :-) So far I have been able to test on x86(32,64) and ppc(32,64) but none of the others. :(

Quoted 8 lines
Quoted 5 lines

This seems reasonable to me.

I've compiled tested the code on all architectures except cris (I don't have my build environment at the moment). I found a minor problem on arm which I have noted below.

I suspect it'll break someones kexec scripts, so maybe we take this patch (or something like it) but bump up the release revision to 2.1?

It /should/ not, as I haven't changed existing working behaviour -- unless someone with a script is relying on existing broken behaviour? I guess we've all been burned at least once by saying, "but SURELY nobody would be doing that", but hey. :)

Cheers,

Matt

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

#5 lore raw
Quoted 7 lines · In message <[email protected]> you wrote:
Quoted 6 lines · Michael Neuling
Quoted 5 lines · In message <[email protected]> you wrote:
Quoted 4 lines · Matt Evans

Hi,

In playing with kexec-tools I've noticed various problems with the argume

nt

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

passing, meaning one has to be careful to use certain forms of arguments and present them in a certain order.

The arguments end up being parsed three times, each getting more specific than the last. main() looks for general args, arch_process_options() loo

ks

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

for options common to all loaders for the arch, and then finally many file_type load() methods check for options specific to that filetype.

As GNU getopt works, it re-orders the argv[] pushing any args it doesn't recognise to the end. This includes arguments to valid options which are simply not recognised the first time around.

For example, this does not work:

  # ./kexec -l --append "init=/bin/foo" /boot/vmlinux
  Cannot open `init=/bin/foo': No such file or directory
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

but this does:

  # ./kexec -l --append="init=/bin/foo" /boot/vmlinux
  <joy>

Using the --opt<space>arg variant results in the first non-option argumen

t

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

in main() being "init=/bin/foo" which is then used as the kernel filename

,

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

failing. Also, the order affects things in unintuitive ways:

  # ./kexec -l /boot/vmlinux --append "init=/bin/foo" 
<appears to load correctly, but command line appended with "/boot/vmlinux

"!

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

This behaviour is avoided by using the --opt= forms, but getopt does allo

w

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

both and hence the user can have a fairly frustrating experience. (Doing something unexpected (ex. 3) is more annoying than an error exit (ex. 1) in many cases.)

The fix presented here is to create a new #define to encapsulate all poss

ib

Quoted 2 lines
Quoted 2 lines

le

Quoted 1 line
Quoted 1 line

options for an architecture build. The intention is that this set includ

es

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

all possible loaders' all possible options.

main() walks through the options and silently ignores any non-general options (BUT respects that "--arg foo" should remain together, as getopt_long() does now recognise it). arch_process_options() walks throu

gh

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

them again and responds to any arch-specific options, again ignoring but respecting any non-arch options. Finally the loader may look for its options, and find them in-order and present. Any outside of this combine

d

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
Quoted 5 lines

set are complained-about as usual.

So, comments please. Is this a reasonable way to do it or is there an obvious better way I've missed? :-) So far I have been able to test on x86(32,64) and ppc(32,64) but none of the others. :(

This seems reasonable to me.

I've compiled tested the code on all architectures except cris (I don't have my build environment at the moment). I found a minor problem on arm which I have noted below.

Quoted 7 lines
Quoted 2 lines

I suspect it'll break someones kexec scripts, so maybe we take this patch (or something like it) but bump up the release revision to 2.1?

It /should/ not, as I haven't changed existing working behaviour -- unless someone with a script is relying on existing broken behaviour? I guess we've all been burned at least once by saying, "but SURELY nobody would be doing that", but hey. :)

Yeah, that's what I'm concerned about. Users are pretty dumb ;-)

Mikey

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

author
#8 lore raw
In reply to Simon Horman
Quoted 53 lines · Simon Horman
Quoted 48 lines · Matt Evans

Hi,

In playing with kexec-tools I've noticed various problems with the argument passing, meaning one has to be careful to use certain forms of arguments and present them in a certain order.

The arguments end up being parsed three times, each getting more specific than the last. main() looks for general args, arch_process_options() looks for options common to all loaders for the arch, and then finally many file_type load() methods check for options specific to that filetype.

As GNU getopt works, it re-orders the argv[] pushing any args it doesn't recognise to the end. This includes arguments to valid options which are simply not recognised the first time around.

For example, this does not work:

  # ./kexec -l --append "init=/bin/foo" /boot/vmlinux
  Cannot open `init=/bin/foo': No such file or directory

but this does:

  # ./kexec -l --append="init=/bin/foo" /boot/vmlinux
  <joy>

Using the --opt<space>arg variant results in the first non-option argument in main() being "init=/bin/foo" which is then used as the kernel filename, failing. Also, the order affects things in unintuitive ways:

  # ./kexec -l /boot/vmlinux --append "init=/bin/foo" 
<appears to load correctly, but command line appended with "/boot/vmlinux"!>

This behaviour is avoided by using the --opt= forms, but getopt does allow both and hence the user can have a fairly frustrating experience. (Doing something unexpected (ex. 3) is more annoying than an error exit (ex. 1) in many cases.)

The fix presented here is to create a new #define to encapsulate all possible options for an architecture build. The intention is that this set includes all possible loaders' all possible options.

main() walks through the options and silently ignores any non-general options (BUT respects that "--arg foo" should remain together, as getopt_long() does now recognise it). arch_process_options() walks through them again and responds to any arch-specific options, again ignoring but respecting any non-arch options. Finally the loader may look for its options, and find them in-order and present. Any outside of this combined set are complained-about as usual.

Quoted 3 lines

So, comments please. Is this a reasonable way to do it or is there an obvious better way I've missed? :-) So far I have been able to test on x86(32,64) and ppc(32,64) but none of the others. :(

Quoted 5 lines

This seems reasonable to me.

I've compiled tested the code on all architectures except cris (I don't have my build environment at the moment). I found a minor problem on arm which I have noted below.

That's superb, thanks very much for compiling them! And thanks also for catching the cut-n-paste-no-brain-involved ARM build problem below. I'll repost.

Cheers,

Matt

Quoted 44 lines

[snip]

Quoted 28 lines
kexec/arch/arm/include/arch/options.h +15 -0
index 248230b..a76539e 100644
@@ -3,9 +3,38 @@
 
 #define OPT_ARCH_MAX   (OPT_MAX+0)
 
+#define OPT_APPEND	'a'
+#define OPT_RAMDISK	'r'
+
+/* Options relevant to the architecture (excluding loader-specific ones),
+ * in this case none:
+ */
 #define KEXEC_ARCH_OPTIONS \
 	KEXEC_OPTIONS \
 
 #define KEXEC_ARCH_OPT_STR KEXEC_OPT_STR ""
 
+/* The following two #defines list ALL of the options added by all of the
+ * architecture's loaders.
+ * o	main() uses this complete list to scan for its options, ignoring
+ *	arch-specific/loader-specific ones.
+ * o	Then, arch_process_options() uses this complete list to scan for its
+ *	options, ignoring general/loader-specific ones.
+ * o	Then, the file_type[n].load re-scans for options, using
+ *	KEXEC_ARCH_OPTIONS plus its loader-specific options subset.
+ *	Any unrecognised options cause an error here.

kexec/arch/arm/include/arch/options.h line 35

Quoted 10 lines
kexec/arch/arm/include/arch/options.h +10 -0
+ *
+ * This is done so that main()'s/arch_process_options()'s getopt_long() calls
+ * don't choose a kernel filename from random arguments to options they don't
+ * recognise -- as they now recognise (if not act upon) all possible options.
+ */
+#define KEXEC_ALL_OPTIONS \
+	KEXEC_ARCH_OPTIONS
+	{ "command-line",	1, 0, OPT_APPEND },
+	{ "append",		1, 0, OPT_APPEND },
+	{ "initrd",		1, 0, OPT_RAMDISK },

The above 4 lines seem to be missing a trailing '\'

Quoted 7 lines
Quoted 5 lines
kexec/arch/arm/include/arch/options.h +4 -0
+	{ "ramdisk",		1, 0, OPT_RAMDISK },
+
+#define KEXEC_ALL_OPT_STR KEXEC_ARCH_OPT_STR "a:r:"
+
 #endif /* KEXEC_ARCH_ARM_OPTIONS_H */

[snip]

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

#6 lore raw
In reply to Michael Neuling
Quoted 7 lines · Michael Neuling
Quoted 6 lines · In message <[email protected]> you wrote:
Quoted 5 lines · Matt Evans

In playing with kexec-tools I've noticed various problems with the argument passing, meaning one has to be careful to use certain forms of arguments and present them in a certain order.

[deletia]

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

This behaviour is avoided by using the --opt= forms, but getopt does allow both and hence the user can have a fairly frustrating experience. (Doing something unexpected (ex. 3) is more annoying than an error exit (ex. 1) in many cases.)

Quoted 9 lines
Quoted 5 lines

This seems reasonable to me.

I've compiled tested the code on all architectures except cris (I don't have my build environment at the moment). I found a minor problem on arm which I have noted below.

I suspect it'll break someones kexec scripts, so maybe we take this patch (or something like it) but bump up the release revision to 2.1?

How?

Command lines that previously worked will continue to work. Command lines that should have worked, but didn't, will now work. Command lines that shouldn't have worked will still not work.

The only scripts that may fail are those doing negative testing to check for a form that *should* have been allowed--quite clearly, any such negative testing was incorrect.

Signature · 7 lines
David N. Lombard, Intel, Irvine, CA
I do not speak for Intel Corporation; all comments are strictly my own.

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
#7 lore raw
Quoted 5 lines · In message <[email protected]> you wrote:
Quoted 4 lines · Michael Neuling
Quoted 3 lines · In message <[email protected]> you wrote:
Quoted 2 lines · Matt Evans

In playing with kexec-tools I've noticed various problems with the argu

ment

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

passing, meaning one has to be careful to use certain forms of argument

s

Quoted 5 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

and present them in a certain order.

[deletia]

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

This behaviour is avoided by using the --opt= forms, but getopt does al

low

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

both and hence the user can have a fairly frustrating experience. (Doi

ng

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

something unexpected (ex. 3) is more annoying than an error exit (ex. 1

)

Quoted 14 lines
Quoted 12 lines
Quoted 8 lines
Quoted 2 lines

in many cases.)

This seems reasonable to me.

I've compiled tested the code on all architectures except cris (I don't have my build environment at the moment). I found a minor problem on arm which I have noted below.

I suspect it'll break someones kexec scripts, so maybe we take this patch (or something like it) but bump up the release revision to 2.1?

How?

Quoted 7 lines

Command lines that previously worked will continue to work. Command lines that should have worked, but didn't, will now work. Command lines that shouldn't have worked will still not work.

The only scripts that may fail are those doing negative testing to check for a form that *should* have been allowed--quite clearly, any such negative testing was incorrect.

Yeah, and like I said, users are pretty dumb, so put 1 and 1 together... :-)

That being said, I don't have a strong opinion. If others think it's unlikely enough that anyone will hit it, then let's keep the release numbering as it is.

... and I think we all agree we need the patch.

Mikey

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