Skip to contentSkip to conversation

Normal mode linux and multiboot loaders

PATCH Marco Gerards 2 participants 5 messages Sep 13, 2004 – Sep 15, 2004

lore
Share & tools
lore
#0 lore raw

Hi,

As I have promised, I have written two modules to make it possible to use the linux and multiboot loaders in GNU/Linux. It is a really simple patch so I assume it is ok and check it in Wednesday.

It also fixed a bug in ext2 that I noticed when using a GNU/Hurd ext2 filesystem. And I noticed `__attribute__ ((unused))' in linux.c for the initrd function. The arguments are used so I removed this.

Thanks,
Marco

2004-09-13 Marco Gerards <[email protected]>

Add linux.mod' and multiboot.mod' so linux and multiboot kernels can be loaded from normal mode.

* conf/i386-pc.mk (pkgdata_MODULES): Add linux.mod' and multiboot.mod'.
(linux_mod_SOURCES, linux_mod_CFLAGS, multiboot_mod_SOURCES) (multiboot_mod_CFLAGS): New variables.
* loader/i386/pc/linux_normal.c: New file.
* loader/i386/pc/multiboot_normal.c: Likewise.

* loader/i386/pc/linux.c (grub_rescue_cmd_initrd): Don't use the attribute `unused'.

* fs/ext2.c (grub_ext2_iterate_dir): Fix typos in inode type. Use fdiro' to read the mode information from instead of diro'.

Index: conf/i386-pc.rmk
===================================================================
RCS file: /cvsroot/grub/grub2/conf/i386-pc.rmk,v
retrieving revision 1.20

i386-pc.rmk +10 -2
diff -u -p -u -p -r1.20 i386-pc.rmk
@@ -78,9 +78,9 @@ grub_emu_LDFLAGS = -lncurses
 genmoddep_SOURCES = util/genmoddep.c
 
 # Modules.
-pkgdata_MODULES = _chain.mod _linux.mod fat.mod ufs.mod ext2.mod minix.mod \
+pkgdata_MODULES = _chain.mod _linux.mod linux.mod fat.mod ufs.mod ext2.mod minix.mod \
 	hfs.mod jfs.mod normal.mod hello.mod vga.mod font.mod _multiboot.mod ls.mod \
-	boot.mod cmp.mod cat.mod terminal.mod fshelp.mod chain.mod
+	boot.mod cmp.mod cat.mod terminal.mod fshelp.mod chain.mod multiboot.mod
 
 # For _chain.mod.
 _chain_mod_SOURCES = loader/i386/pc/chainloader.c
@@ -121,6 +121,10 @@ jfs_mod_CFLAGS = $(COMMON_CFLAGS)
 # For _linux.mod.
 _linux_mod_SOURCES = loader/i386/pc/linux.c
 _linux_mod_CFLAGS = $(COMMON_CFLAGS)
+ 
+# For linux.mod.
+linux_mod_SOURCES = loader/i386/pc/linux_normal.c
+linux_mod_CFLAGS = $(COMMON_CFLAGS)
 
 # For normal.mod.
 normal_mod_SOURCES = normal/cmdline.c normal/command.c normal/main.c \
@@ -163,3 +167,7 @@ font_mod_CFLAGS = $(COMMON_CFLAGS)
 # For _multiboot.mod.
 _multiboot_mod_SOURCES = loader/i386/pc/multiboot.c
 _multiboot_mod_CFLAGS = $(COMMON_CFLAGS)
+
+# For multiboot.mod.
+multiboot_mod_SOURCES = loader/i386/pc/multiboot_normal.c
+multiboot_mod_CFLAGS = $(COMMON_CFLAGS)

Index: fs/ext2.c
===================================================================
RCS file: /cvsroot/grub/grub2/fs/ext2.c,v
retrieving revision 1.9

ext2.c +5 -5
diff -u -p -u -p -r1.9 ext2.c
@@ -451,14 +451,14 @@ grub_ext2_iterate_dir (grub_fshelp_node_
 	      
 	      fdiro->inode_read = 1;
 	      
-	      if ((grub_le_to_cpu16 (diro->inode.mode)
+	      if ((grub_le_to_cpu16 (fdiro->inode.mode)
 		   & FILETYPE_INO_MASK) == FILETYPE_INO_DIRECTORY)
 		type = GRUB_FSHELP_DIR;
-	      else if ((grub_le_to_cpu16 (diro->inode.mode)
-			& FILETYPE_INO_MASK) == FILETYPE_INO_DIRECTORY)
+	      else if ((grub_le_to_cpu16 (fdiro->inode.mode)
+			& FILETYPE_INO_MASK) == FILETYPE_INO_SYMLINK)
 		type = GRUB_FSHELP_SYMLINK;
-	      else if ((grub_le_to_cpu16 (diro->inode.mode)
-			& FILETYPE_INO_MASK) == FILETYPE_INO_DIRECTORY)
+	      else if ((grub_le_to_cpu16 (fdiro->inode.mode)
+			& FILETYPE_INO_MASK) == FILETYPE_INO_REG)
 		type = GRUB_FSHELP_REG;
 	    }
 	  

Index: loader/i386/pc/linux.c
===================================================================
RCS file: /cvsroot/grub/grub2/loader/i386/pc/linux.c,v retrieving revision 1.6

i386/pc/linux.c +1 -2
diff -u -p -u -p -r1.6 linux.c
@@ -290,8 +290,7 @@ grub_rescue_cmd_linux (int argc, char *a
 }
 
 void
-grub_rescue_cmd_initrd (int argc __attribute__ ((unused)),
-			char *argv[] __attribute__ ((unused)))
+grub_rescue_cmd_initrd (int argc, char *argv[])
 {
   grub_file_t file = 0;
   grub_ssize_t size;

Index: loader/i386/pc/linux_normal.c
===================================================================
RCS file: loader/i386/pc/linux_normal.c

i386/pc/linux_normal.c new file +61 -0

61 lines not on this page · raw message

Index: loader/i386/pc/multiboot_normal.c
===================================================================
RCS file: loader/i386/pc/multiboot_normal.c

i386/pc/multiboot_normal.c new file +61 -0

61 lines not on this page · raw message

#1 lore raw
Quoted 3 lines · Marco Gerards

As I have promised, I have written two modules to make it possible to use the linux and multiboot loaders in GNU/Linux. It is a really simple patch so I assume it is ok and check it in Wednesday.

Great.

Quoted 3 lines

It also fixed a bug in ext2 that I noticed when using a GNU/Hurd ext2 filesystem. And I noticed `__attribute__ ((unused))' in linux.c for the initrd function. The arguments are used so I removed this.

You are right.

Okuji

#2 lore raw
In reply to Marco Gerards
Quoted 3 lines · Marco Gerards

As I have promised, I have written two modules to make it possible to use the linux and multiboot loaders in GNU/Linux. It is a really simple patch so I assume it is ok and check it in Wednesday.

Now I have tested this a bit more. I found a small problem with fshelp and the command line parser when using GNU/Hurd. Because everything can be used as multiboot argument I disabled the argument parser for this command. I've also disabled the code that replace environment variables because GNU/Hurd uses variables with the same syntax in its bootscripts and it conflicts. :/

I will wait a bit longer with applying this patch so you can comment on these latest changes if you want to.

As a bonus, the shellscript I use to create a GRUB 2 bootfloppy. I hope it is as useful to you as it was to me. Feel free to modify it so it can be included with GRUB 2 to produce floppy/hd images. As you can see I suck at shellscripting and I am not able to produce something useful in a reasonable time.

I can upload a GRUB 2 floppy somewhere including a grub.cfg and GRUB 2 patched with this patch so people can test GRUB 2. Please tell me if you want that and I will do so.

Thanks,
Marco

The script:

#!/bin/sh

IMAGE=grub2-floppy-i386.ext2fs
MNT=/mnt/floppy

rm $IMAGE
dd if=/dev/zero of=$IMAGE bs=512 count=2880|| exit

/sbin/mkfs.ext2 -F $IMAGE
./grub-mkimage -v -d . -o core.img ext2
sudo mount $IMAGE $MNT -o loop
mkdir $MNT/grub
cp *.img *.mod $MNT/grub

echo '(fd0)' $PWD/$IMAGE > tmp_device.map ./grub-setup -d $MNT/grub -m tmp_device.map -r '(fd0)' '(fd0)' rm tmp_device.map

cat >$MNT/grub/grub.cfg <<EOF
#GNU/Linux
insmod linux
insmod boot
insmod multiboot

title GNU/Linux
linux (hd0,4)/boot/vmlinuz-2.2.20 root=/dev/hda5
dsf

title GNU/Hurd
# GNU/Hurd
multiboot (hd0,2)/boot/gnumach root=device:hd0s3
module (hd0,2)/hurd/ext2fs.static --multiboot-command-line=\${kernel-command-line} --host-priv-port=\${host-port} --device-master-port=\${device-port} --exec-server-task=\${exec-task} -T typed \${root} \$(task-create) \$(task-resume) module (hd0,2)/lib/ld.so.1 /hurd/exec \$(exec-task=task-create)
gdsf
EOF

cat $MNT/grub/grub.cfg
sudo umount $MNT

The new patch:

2004-09-13 Marco Gerards <[email protected]>

Add linux.mod' and multiboot.mod' so linux and multiboot kernels can be loaded from normal mode.

* conf/i386-pc.mk (pkgdata_MODULES): Add linux.mod' and multiboot.mod'.
(linux_mod_SOURCES, linux_mod_CFLAGS, multiboot_mod_SOURCES) (multiboot_mod_CFLAGS): New variables.
* loader/i386/pc/linux_normal.c: New file.
* loader/i386/pc/multiboot_normal.c: Likewise.

* loader/i386/pc/linux.c (grub_rescue_cmd_initrd): Don't use the attribute `unused'.

* fs/ext2.c (grub_ext2_iterate_dir): Fix typos in inode type. Use fdiro' to read the mode information from instead of diro'.

* fs/fshelp.c (grub_fshelp_find_file): Set type to foundtype after looking up a symlink.

* include/grub/normal.h (GRUB_COMMAND_FLAG_NO_ARG_PARSE): New macro.
* normal/command.c (grub_command_execute): Don't parse the arguments when `GRUB_COMMAND_FLAG_NO_ARG_PARSE' is set in the flags of the command.

* grub/grub2/kern/misc.c (grub_split_cmdline): Disable envvar parsing code because it conflicts with multiboot.

* normal/menu.c (grub_menu_run): Fix typo.

Index: conf/i386-pc.rmk
===================================================================
RCS file: /cvsroot/grub/grub2/conf/i386-pc.rmk,v
retrieving revision 1.20

i386-pc.rmk +10 -2
diff -u -p -u -p -r1.20 i386-pc.rmk
@@ -78,9 +78,9 @@ grub_emu_LDFLAGS = -lncurses
 genmoddep_SOURCES = util/genmoddep.c
 
 # Modules.
-pkgdata_MODULES = _chain.mod _linux.mod fat.mod ufs.mod ext2.mod minix.mod \
+pkgdata_MODULES = _chain.mod _linux.mod linux.mod fat.mod ufs.mod ext2.mod minix.mod \
 	hfs.mod jfs.mod normal.mod hello.mod vga.mod font.mod _multiboot.mod ls.mod \
-	boot.mod cmp.mod cat.mod terminal.mod fshelp.mod chain.mod
+	boot.mod cmp.mod cat.mod terminal.mod fshelp.mod chain.mod multiboot.mod
 
 # For _chain.mod.
 _chain_mod_SOURCES = loader/i386/pc/chainloader.c
@@ -121,6 +121,10 @@ jfs_mod_CFLAGS = $(COMMON_CFLAGS)
 # For _linux.mod.
 _linux_mod_SOURCES = loader/i386/pc/linux.c
 _linux_mod_CFLAGS = $(COMMON_CFLAGS)
+ 
+# For linux.mod.
+linux_mod_SOURCES = loader/i386/pc/linux_normal.c
+linux_mod_CFLAGS = $(COMMON_CFLAGS)
 
 # For normal.mod.
 normal_mod_SOURCES = normal/cmdline.c normal/command.c normal/main.c \
@@ -163,3 +167,7 @@ font_mod_CFLAGS = $(COMMON_CFLAGS)
 # For _multiboot.mod.
 _multiboot_mod_SOURCES = loader/i386/pc/multiboot.c
 _multiboot_mod_CFLAGS = $(COMMON_CFLAGS)
+
+# For multiboot.mod.
+multiboot_mod_SOURCES = loader/i386/pc/multiboot_normal.c
+multiboot_mod_CFLAGS = $(COMMON_CFLAGS)

Index: fs/ext2.c
===================================================================
RCS file: /cvsroot/grub/grub2/fs/ext2.c,v
retrieving revision 1.9

ext2.c +5 -5
diff -u -p -u -p -r1.9 ext2.c
@@ -451,14 +451,14 @@ grub_ext2_iterate_dir (grub_fshelp_node_
 	      
 	      fdiro->inode_read = 1;
 	      
-	      if ((grub_le_to_cpu16 (diro->inode.mode)
+	      if ((grub_le_to_cpu16 (fdiro->inode.mode)
 		   & FILETYPE_INO_MASK) == FILETYPE_INO_DIRECTORY)
 		type = GRUB_FSHELP_DIR;
-	      else if ((grub_le_to_cpu16 (diro->inode.mode)
-			& FILETYPE_INO_MASK) == FILETYPE_INO_DIRECTORY)
+	      else if ((grub_le_to_cpu16 (fdiro->inode.mode)
+			& FILETYPE_INO_MASK) == FILETYPE_INO_SYMLINK)
 		type = GRUB_FSHELP_SYMLINK;
-	      else if ((grub_le_to_cpu16 (diro->inode.mode)
-			& FILETYPE_INO_MASK) == FILETYPE_INO_DIRECTORY)
+	      else if ((grub_le_to_cpu16 (fdiro->inode.mode)
+			& FILETYPE_INO_MASK) == FILETYPE_INO_REG)
 		type = GRUB_FSHELP_REG;
 	    }
 	  

Index: fs/fshelp.c
===================================================================
RCS file: /cvsroot/grub/grub2/fs/fshelp.c,v
retrieving revision 1.1

fshelp.c +1 -0
diff -u -p -u -p -r1.1 fshelp.c
@@ -171,6 +171,7 @@ grub_fshelp_find_file (const char *path,
 	      
 	      /* Lookup the node the symlink points to.  */
 	      find_file (symlink, oldnode, &currnode);
+	      type = foundtype;
 	      grub_free (symlink);
 	      
 	      if (grub_errno)

Index: include/grub/normal.h
===================================================================
RCS file: /cvsroot/grub/grub2/include/grub/normal.h,v retrieving revision 1.8

grub/normal.h +2 -0
diff -u -p -u -p -r1.8 normal.h
@@ -39,6 +39,8 @@
 #define GRUB_COMMAND_FLAG_TITLE		0x4
 /* Don't print the command on booting.  */
 #define GRUB_COMMAND_FLAG_NO_ECHO	0x8
+/* Don't print the command on booting.  */
+#define GRUB_COMMAND_FLAG_NO_ARG_PARSE	0x10
 
 /* The command description.  */
 struct grub_command

Index: kern/misc.c
===================================================================
RCS file: /cvsroot/grub/grub2/kern/misc.c,v
retrieving revision 1.15

misc.c +6 -0
diff -u -p -u -p -r1.15 misc.c
@@ -771,6 +771,7 @@ grub_split_cmdline (const char *cmdline,
       unput = 1;
     }
 
+#if 0
   /* Read a variable name from the commandline and insert its content
      into the buffer.  */
   void getenvvar (void)
@@ -806,6 +807,7 @@ grub_split_cmdline (const char *cmdline,
       for (p = val; *p; p++)
 	*(bp++) = *p;
     }
+#endif
 
   /* Read one argument.  Return 1 if no variables can be read anymore,
      otherwise return 0.  If there is an error, return 1, the caller
@@ -838,11 +840,13 @@ grub_split_cmdline (const char *cmdline,
 		else if (c == '"')
 		  break;
 		/* Read a variable.  */
+#if 0
 		if (c == '$')
 		  {
 		    getenvvar ();
 		    continue;
 		  }
+#endif
 		*(bp++) = c;
 	      }
 	    break;
@@ -875,6 +879,7 @@ grub_split_cmdline (const char *cmdline,
 		    c = getchar ();
 		    continue;
 		  }
+#if 0
 		/* Read a variable.  */
 		if (c == '$')
 		  {
@@ -882,6 +887,7 @@ grub_split_cmdline (const char *cmdline,
 		    c = getchar ();
 		    continue;
 		  }
+#endif
 		*(bp++) = c;
 		c = getchar ();
 	      }

Index: loader/i386/pc/linux.c
===================================================================
RCS file: /cvsroot/grub/grub2/loader/i386/pc/linux.c,v retrieving revision 1.6

i386/pc/linux.c +1 -2
diff -u -p -u -p -r1.6 linux.c
@@ -290,8 +290,7 @@ grub_rescue_cmd_linux (int argc, char *a
 }
 
 void
-grub_rescue_cmd_initrd (int argc __attribute__ ((unused)),
-			char *argv[] __attribute__ ((unused)))
+grub_rescue_cmd_initrd (int argc, char *argv[])
 {
   grub_file_t file = 0;
   grub_ssize_t size;

Index: loader/i386/pc/linux_normal.c
===================================================================
RCS file: loader/i386/pc/linux_normal.c

i386/pc/linux_normal.c new file +61 -0

61 lines not on this page · raw message

Index: loader/i386/pc/multiboot_normal.c
===================================================================
RCS file: loader/i386/pc/multiboot_normal.c

i386/pc/multiboot_normal.c new file +61 -0

61 lines not on this page · raw message

Index: normal/command.c
===================================================================
RCS file: /cvsroot/grub/grub2/normal/command.c,v
retrieving revision 1.6

command.c +8 -2
diff -u -p -u -p -r1.6 command.c
@@ -160,8 +160,14 @@ grub_command_execute (char *cmdline)
 
   state = grub_malloc (sizeof (struct grub_arg_list) * maxargs);
   grub_memset (state, 0, sizeof (struct grub_arg_list) * maxargs);
-  if (grub_arg_parse (cmd, num, &args[1], state, &arglist, &numargs))
-    ret = (cmd->func) (state, numargs, arglist);
+  if (! (cmd->flags & GRUB_COMMAND_FLAG_NO_ARG_PARSE))
+    {
+      if (grub_arg_parse (cmd, num, &args[1], state, &arglist, &numargs))
+	ret = (cmd->func) (state, numargs, arglist);
+    }
+  else
+    ret = (cmd->func) (state, num, &args[1]);
+  
   grub_free (state);
 
   if (pager && (! grub_strcmp (pager, "1")))

Index: normal/menu.c
===================================================================
RCS file: /cvsroot/grub/grub2/normal/menu.c,v
retrieving revision 1.6

menu.c +1 -1
diff -u -p -u -p -r1.6 menu.c
@@ -357,7 +357,7 @@ grub_menu_run (grub_menu_t menu, int nes
       run_menu_entry (e);
 
       /* Deal with a fallback entry.  */
-      /* FIXME: Mutiple fallback entries like GRUB Legacy.  */
+      /* FIXME: Multiple fallback entries like GRUB Legacy.  */
       if (menu->fallback_entry >= 0)
 	{
 	  grub_print_error ();
#3 lore raw
Quoted 6 lines · Marco Gerards

Now I have tested this a bit more. I found a small problem with fshelp and the command line parser when using GNU/Hurd. Because everything can be used as multiboot argument I disabled the argument parser for this command. I've also disabled the code that replace environment variables because GNU/Hurd uses variables with the same syntax in its bootscripts and it conflicts. :/

Honestly speaking, I think Hurd should change. Since arguments to a Multiboot kernel do not have to be shell-like, Hurd can use a very different way, while GRUB has a good reason to behave like a shell.

For example, Ruby uses #{...} for substitutions in strings. Hurd can use the same, and this does not conflict with GRUB. Or, Python's %(...)s is also nice.

If Hurd does not want to change, you should quote arguments rather than changing the semantics. I don't like inconsistency.

Quoted 3 lines

I can upload a GRUB 2 floppy somewhere including a grub.cfg and GRUB 2 patched with this patch so people can test GRUB 2. Please tell me if you want that and I will do so.

I don't think binary distributions are useful at the moment, since GRUB 2 is not stable. But it is nice to provide a script to make an image easily.

Okuji

#4 lore raw
Quoted 2 lines · Yoshinori K. Okuji

If Hurd does not want to change, you should quote arguments rather than changing the semantics. I don't like inconsistency.

Right. So for now we will assume arguments like these are quoted. I will remove this part of the patch so thinks stay as they are.

Quoted 7 lines
Quoted 3 lines

I can upload a GRUB 2 floppy somewhere including a grub.cfg and GRUB 2 patched with this patch so people can test GRUB 2. Please tell me if you want that and I will do so.

I don't think binary distributions are useful at the moment, since GRUB 2 is not stable. But it is nice to provide a script to make an image easily.

Ok.

Thanks,
Marco