Skip to contentSkip to conversation

Handling command line option for core.img and g2ldr

PATCH In review Bean 4 participants 11 messages Feb 25, 2008 – Mar 5, 2008 3 messages not in the archive

lore
Share & tools
lore

Bean

author
#0 lore raw

PATCH 1/1 Handling command line option for core.img and g2ldr

Hi,

This patch allow passing command line options to core.img or g2ldr:

For example, in grub2:

multiboot /core.img prefix=(hd0,1)/boot/grub
multiboot /core.img root=(hd0,1)
linux /g2ldr prefix=(hd0,1)/boot/grub
linux /g2ldr root=(hd0,1)

prefix will take preference over root.

you can also set other variables, for example:

multiboot /core.img root=(hd0,1) debug=all

This patch also set the boot device in the multiboot loader, for example:

set root=(hd0,1)
multiboot /core.img
boot

It will show the menu instead of rescue mode.

2008-02-25 Bean <[email protected]>

* boot/i386/pc/lnxboot.S (real_code): Copy cmdline to safe location and setup variable grub_boot_cmdline.

* include/grub/i386/pc/kernel.h (GRUB_KERNEL_MACHINE_BOOT_CMDLINE): New macro.
(GRUB_KERNEL_MACHINE_PREFIX): Changed to 0x24. (GRUB_KENREL_MACHINE_RAW_SIZE): Changed to 0x4C0. (grub_boot_cmdline): New variable.

* kern/i386/pc/init.c (make_install_device): Check for root variable. (grub_machine_set_prefix): Check for cmdline.

* kern/i386/pc/startup.S (grub_boot_cmdline): New variable. (multiboot_trampoline): Copy command line.

* loader/i386/pc/multiboot.c (grub_multiboot_get_bootdev): New function. (grub_multiboot): Set boot device.

Signature · 1 line
Bean
boot/i386/pc/lnxboot.S +41 -12
index 2a913ba..451e832 100644
@@ -38,6 +38,7 @@
 
 data_start:
 	xorl	%ebp, %ebp
+	xorl	%edi, %edi
 	jmp	linux_next
 
 	. = data_start + 0x1F1
@@ -115,7 +116,7 @@ gdt_dst2:
 	.long	0, 0, 0, 0		/* More space for the BIOS.  */
 
 reg_edx:
-	.byte	0x80, 0xFF, 0xFF, 0xFF
+	.byte	0x80, 0, 0xFF, 0xFF
 
 data_leng:
 	.long	0
@@ -123,6 +124,7 @@ data_leng:
 linux_init:
 
 	movw	%cs:(reg_edx - start), %dx
+	movl	%cs:(cmd_line_ptr - start), %edi
 	movl	%cs:(code32_start - start), %ebp
 
 linux_next:
@@ -140,25 +142,48 @@ normalize:
 	lret				/* Jump to real_code.  */
 
 real_code:
-	subw	$0x20, %ax
-	movw	%ax, %ds
-	movw	(setup_sects - data_start), %cx
-	shlw	$7, %cx
-
 	/* Setup stack.  */
 
-	xorw	%si, %si
-	movw	%si, %ss
+	xorw	%ax, %ax
+	movw	%ax, %ss
 	movw	$(CODE_ADDR), %sp
 
+	cld
+	movw	$(CODE_ADDR >> 4), %ax
+	movw	%ax, %es
+
+	/* Copy command line.  */
+	orl	%edi, %edi
+	jz	2f
+
+	movw	%di, %si
+	andw	$0xF, %si
+	shrl	$4, %edi
+	movw	%di, %ds
+	movw	$(CODE_LENG), %di
+
+1:
+	lodsb	%ds:(%si)
+	stosb	%es:(%di)
+	orb	%al, %al
+	jnz	1b
+
+	xorl	%edi, %edi
+	movw	%es, %di
+	addw	$(CODE_LENG >> 4), %di
+	shll	$4, %edi
+
+2:
+
+	pushl	%edi
+
 	/* Move itself to 0:CODE_ADDR.  */
 
-	cld
 	movw	%cs, %ax
 	movw	%ax, %ds
-	movw	$(CODE_ADDR >> 4), %ax
-	movw	%ax, %es
-	movw	%si, %di
+	movw	$(CODE_LENG >> 2), %cx
+	xorw	%si, %si
+	xorw	%di, %di
 
 	rep
 	movsl
@@ -201,12 +226,16 @@ real_code_2:
 2:
 	call	move_memory
 
+	popl	%ss:(DATA_ADDR + GRUB_KERNEL_MACHINE_BOOT_CMDLINE)
+
 	movsbl	%dh, %eax
 	movl	%eax, %ss:(DATA_ADDR + GRUB_KERNEL_MACHINE_INSTALL_DOS_PART)
 
 	movsbl	(reg_edx + 2 - start), %eax
 	movl	%eax, %ss:(DATA_ADDR + GRUB_KERNEL_MACHINE_INSTALL_BSD_PART)
 
+	movb	$0xFF, %dh
+
 	ljmp	$(DATA_ADDR >> 4), $0
 
 /*
include/grub/i386/pc/kernel.h +8 -2
index 848dad1..f7bf7ff 100644
@@ -37,14 +37,17 @@
 /* The offset of GRUB_MEMDISK_IMAGE_SIZE.  */
 #define GRUB_KERNEL_MACHINE_MEMDISK_IMAGE_SIZE	0x1c
 
+/* The offset of GRUB_BOOT_CMDLINE.  */
+#define GRUB_KERNEL_MACHINE_BOOT_CMDLINE	0x20
+
 /* The offset of GRUB_PREFIX.  */
-#define GRUB_KERNEL_MACHINE_PREFIX		0x20
+#define GRUB_KERNEL_MACHINE_PREFIX		0x24
 
 /* End of the data section. */
 #define GRUB_KERNEL_MACHINE_DATA_END		0x50
 
 /* The size of the first region which won't be compressed.  */
-#define GRUB_KERNEL_MACHINE_RAW_SIZE		0x4A0
+#define GRUB_KERNEL_MACHINE_RAW_SIZE		0x4C0
 
 #ifndef ASM_FILE
 
@@ -66,6 +69,9 @@ extern grub_int32_t grub_install_bsd_part;
 /* The size of memory disk image, if present.  */
 extern grub_int32_t grub_memdisk_image_size;
 
+/* The boot command line options.  */
+extern char *grub_boot_cmdline;
+
 /* The prefix which points to the directory where GRUB modules and its
    configuration file are located.  */
 extern char grub_prefix[];
kern/i386/pc/init.c +35 -3
index 7237492..91c2c4e 100644
@@ -31,6 +31,7 @@
 #include <grub/env.h>
 #include <grub/cache.h>
 #include <grub/time.h>
+#include <grub/parser.h>
 
 struct mem_region
 {
@@ -63,9 +64,15 @@ static char *
 make_install_device (void)
 {
   /* XXX: This should be enough.  */
-  char dev[100];
+  char dev[100], *p;
 
-  if (grub_memdisk_image_size)
+  p = grub_env_get ("root");
+  if (p)
+    {
+      grub_sprintf (dev, "%s%s", p, grub_prefix);
+      grub_strcpy (grub_prefix, dev);
+    }
+  else if (grub_memdisk_image_size)
     {
       grub_sprintf (dev, "(memdisk)%s", grub_prefix);
       grub_strcpy (grub_prefix, dev);
@@ -248,7 +255,32 @@ void
 grub_machine_set_prefix (void)
 {
   /* Initialize the prefix.  */
-  grub_env_set ("prefix", make_install_device ());
+  if ((grub_boot_cmdline) && (grub_boot_cmdline[0]))
+    {
+      int argc;
+      char **argv;
+
+      if (! grub_parser_split_cmdline (grub_boot_cmdline, 0, &argc, &argv))
+        {
+          int i;
+
+          for (i = 0; i <= argc; i++)
+            {
+              char *p;
+
+              p = grub_strchr (argv[i], '=');
+              if (p)
+                {
+                  *(p++) = 0;
+                  grub_env_set (argv[i], p);
+                }
+            }
+          grub_free (argv);
+        }
+    }
+
+  if (! grub_env_get ("prefix"))
+    grub_env_set ("prefix", make_install_device ());
 }
 
 void
kern/i386/pc/startup.S +14 -0
index bf3d4ee..e31dbe7 100644
@@ -98,6 +98,8 @@ VARIABLE(grub_install_bsd_part)
 	.long	0xFFFFFFFF
 VARIABLE(grub_memdisk_image_size)
 	.long	0
+VARIABLE(grub_boot_cmdline)
+	.long	0
 VARIABLE(grub_prefix)
 	/* to be filled by grub-mkimage */
 
@@ -149,6 +151,18 @@ multiboot_entry:
 	
 multiboot_trampoline:
 	/* fill the boot information */
+	movl	16(%ebx), %esi
+	orl	%esi, %esi
+	jz	2f
+	movl	$(GRUB_MEMORY_MACHINE_REAL_STACK + 0x10), %edi
+	movl	%edi, EXT_C(grub_boot_cmdline)
+	cld
+1:
+	lodsb	%ds:(%esi)
+	stosb	%es:(%edi)
+	orb	%al, %al
+	jnz	1b
+2:
 	movl	%edx, %eax
 	shrl	$8, %eax
 	xorl	%ebx, %ebx
loader/i386/pc/multiboot.c +50 -0

68 lines not on this page · raw message

#1 lore raw

Quoting Bean <[email protected]>:

Quoted 1 line

This patch allow passing command line options to core.img or g2ldr:

Isn't it an overkill? When do you think it would be really needed in practice? I don't feel good about adding code just because we can.

Quoted 1 line

This patch also set the boot device in the multiboot loader, for example:

Now that's good! We should follow the standards we want others to accept. Would not it be better to keep only this part?

Signature · 2 lines
Regards,
Pavel Roskin

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): 1203948493.802.15.camel@dv

author
#4 lore raw
Quoted 10 lines · Pavel Roskin
Quoted 7 lines · Bean
Quoted 6 lines · Pavel Roskin

Quoting Bean <[email protected]>:

Quoted 1 line

This patch allow passing command line options to core.img or g2ldr:

Isn't it an overkill? When do you think it would be really needed in practice? I don't feel good about adding code just because we can.

Sorry, you lost the list from cc. Please feel free to re-add it.

oh, i didn't notice it.

Quoted 5 lines
Quoted 4 lines

Sometimes, the root device is complicated and can't be set using the rootdev, for example, (ata0,1), (memdisk). In this case, we need to set it explicitly using the root variable. Also, we can set the debug variable to see debug information.

Quoted 7 lines

OK, fair enough, but I still don't see a scenario when it's needed.

The best thing would be if this functionality could be used for the normal boot somehow. Maybe some settings could be specified as variables when GRUB is installed.

I think the main reason for this is to load grub2 from other environment, like grub legacy. Allow setting variable have its advantage, for example, we can load two version of grub2, each with different setting:

title boot grub2
kernel /core.img

title boot grub2 (debug)
kernel /core.img debug=all

Also, some device, like (cd0), can't be set using traditional method, we must set the root variable in command line.

title boot grub2
kernel /core.img root=(cd0)

Quoted 4 lines
Quoted 2 lines

For linux kernel g2ldr, there is no way to set the root device, we must use command line.

I'm not sure it's relevant here. It's not even part of GRUB, is it?

g2ldr is created using lnxboot.img header:

cat lnxboot.img core.img > g2ldr

It's used to load grub2 from loaders that support linux kernel, but not multiboot, such as lilo, syslinux, etc.

Quoted 8 lines

To sum up, I'm fine with any improvements to the loaders, allowing them to pass more information to the kernels or whatever they load.

I have reservations about changes that affect the GRUB core (not the "normal" module), especially those requiring additional memory allocation. I think this should be justified with some real world example. Or there should be a way to improve the existing code.

I don't know, is there other way to pass information to grub2 ?

Signature · 1 line
Bean
#5 lore raw
In reply to Pavel Roskin
Quoted 6 lines · Pavel Roskin

Quoting Bean <[email protected]>:

Quoted 1 line

This patch allow passing command line options to core.img or g2ldr:

Isn't it an overkill? When do you think it would be really needed in practice? I don't feel good about adding code just because we can.

I agree with Pavel. Size is in init.c is very important. The MBI structure already contains the info for GRUB to figure out $prefix and $root, and other variables can be set by user after GRUB has started. So what is the purpose of an additional interface?

Signature · 5 lines
Robert Millan

<GPLv2> I know my rights; I want my phone call!
<DRM> What use is a phone call… if you are unable to speak?
(as seen on /.)

A message that is not in the archive (sent off-list or not yet received): [email protected]

#7 lore raw

[ readding CC to grub-devel ]

Quoted 14 lines · Bean
Quoted 5 lines · Robert Millan
Quoted 4 lines · Pavel Roskin

Quoting Bean <[email protected]>:

Quoted 1 line

This patch allow passing command line options to core.img or g2ldr:

Quoted 7 lines
Quoted 2 lines

Isn't it an overkill? When do you think it would be really needed in practice? I don't feel good about adding code just because we can.

I agree with Pavel. Size is in init.c is very important. The MBI structure already contains the info for GRUB to figure out $prefix and $root, and other variables can be set by user after GRUB has started. So what is the purpose of an additional interface?

Quoted 8 lines

The problem of MBI boot device is that it's quite limited, it can only represent (hdx,y,z) and (fdx,y,z). But in fact, the root device can be of other form. For example, cd boot device (cd0). The current implementation use some sort of heuristic to decide whether it's cd drive, but it's not guaranteed to work. Moreover, if we use ata instead of biosdisk, then the boot device is (atax,y,z), which can't be represented properly. Also , it couldn't handle the situation where root device is not the same as boot device.

There are worse situations in fact (in that they're more common), like RAID and LVM drives (which can be the boot device too).

But I don't think we need parser code for that. The path part of the prefix (/boot/grub, but sometimes /grub) is already hardcoded in by grub-mkimage, so why not extend that to storing the whole path? It's just a few bytes more (typically 4), which is much less than code to parse cmdline params.

Signature · 5 lines
Robert Millan

<GPLv2> I know my rights; I want my phone call!
<DRM> What use is a phone call… if you are unable to speak?
(as seen on /.)
author
#8 lore raw
Quoted 29 lines · Robert Millan

[ readding CC to grub-devel ]

Quoted 14 lines · Bean
Quoted 5 lines · Robert Millan
Quoted 4 lines · Pavel Roskin

Quoting Bean <[email protected]>:

Quoted 1 line

This patch allow passing command line options to core.img or g2ldr:

Quoted 7 lines
Quoted 2 lines

Isn't it an overkill? When do you think it would be really needed in practice? I don't feel good about adding code just because we can.

I agree with Pavel. Size is in init.c is very important. The MBI structure already contains the info for GRUB to figure out $prefix and $root, and other variables can be set by user after GRUB has started. So what is the purpose of an additional interface?

Quoted 8 lines

The problem of MBI boot device is that it's quite limited, it can only represent (hdx,y,z) and (fdx,y,z). But in fact, the root device can be of other form. For example, cd boot device (cd0). The current implementation use some sort of heuristic to decide whether it's cd drive, but it's not guaranteed to work. Moreover, if we use ata instead of biosdisk, then the boot device is (atax,y,z), which can't be represented properly. Also , it couldn't handle the situation where root device is not the same as boot device.

Quoted 7 lines

There are worse situations in fact (in that they're more common), like RAID and LVM drives (which can be the boot device too).

But I don't think we need parser code for that. The path part of the prefix (/boot/grub, but sometimes /grub) is already hardcoded in by grub-mkimage, so why not extend that to storing the whole path? It's just a few bytes more (typically 4), which is much less than code to parse cmdline params.

I think this could work, it actually avoid a lot of trouble by storing the device name as well. A little setback is that we need to create different core.img for different boot device, perhaps we can allow both absolute and relative boot device. if the boot device starts with (, then it's absolute, otherwise it's relative, we can get the full boot device using current method.

Signature · 1 line
Bean
#9 lore raw
Quoted 11 lines · Bean
Quoted 8 lines

There are worse situations in fact (in that they're more common), like RAID and LVM drives (which can be the boot device too).

But I don't think we need parser code for that. The path part of the prefix (/boot/grub, but sometimes /grub) is already hardcoded in by grub-mkimage, so why not extend that to storing the whole path? It's just a few bytes more (typically 4), which is much less than code to parse cmdline params.

I think this could work, it actually avoid a lot of trouble by storing the device name as well.

One thing I overlooked is that it wouldn't allow for 0xFF effect (i.e. get the root drive from bios), which is also desirable (or even is that the default?).

Quoted 2 lines

A little setback is that we need to create
different core.img for different boot device,

I don't think that's a big problem since core.img is generated every time in grub-install (but in most cases, 0xFF setup will handle it, is that correct?).

Signature · 5 lines
Robert Millan

<GPLv2> I know my rights; I want my phone call!
<DRM> What use is a phone call… if you are unable to speak?
(as seen on /.)
#10 lore raw
Quoted 17 lines · Robert Millan
Quoted 11 lines · Bean
Quoted 8 lines

There are worse situations in fact (in that they're more common), like
RAID and LVM drives (which can be the boot device too).

But I don't think we need parser code for that. The path part of the
prefix (/boot/grub, but sometimes /grub) is already hardcoded in by grub-mkimage, so why not extend that to storing the whole path? It's just a few bytes more (typically 4), which is much less than code to parse cmdline params.

I think this could work, it actually avoid a lot of trouble by storing the device name as well.

One thing I overlooked is that it wouldn't allow for 0xFF effect (i.e. get the root drive from bios), which is also desirable (or even is that the default?).

Quoted 6 lines
Quoted 2 lines

A little setback is that we need to create
different core.img for different boot device,

I don't think that's a big problem since core.img is generated every time in grub-install (but in most cases, 0xFF setup will handle it, is that correct?).

No, no, no. It is one of the most important features in GRUB that you can boot it, even if a disk is migrated. We must avoid hardcoding whenever possible.

Okuji

author
#11 lore raw

Hi,

Perhaps i can check in the part that sets boot device for multiboot loader first.

Signature · 1 line
Bean
#12 lore raw
Quoted 4 lines · Bean

Hi,

Perhaps i can check in the part that sets boot device for multiboot loader first.

I think we all agree that this part is desirable :-)

Signature · 5 lines
Robert Millan

<GPLv2> I know my rights; I want my phone call!
<DRM> What use is a phone call… if you are unable to speak?
(as seen on /.)
author
#13 lore raw

Committed.

Signature · 1 line
Bean