grub-mkimage and module loading
This is a more complete patch for PPC grub-mkimage that I posted a while back. Important points:
- move grub_load_modules() into i386/pc as grub_arch_load_modules()
- implement PPC's grub_arch_load_modules() using an in-memory structure telling us how many modules to expect
- the structure contains a version field to avoid grub-mkimage/grub binary layout mismatches
- util/powerpc/ieee1275/grub-mkimage.c is a fair amount of code, but it's used almost exactly like the i386 version
- added a couple helper functions to the util/misc.c code
I think there is still some PC cleanup needed, e.g. grub_end_addr and grub_add_unused_region() should also be moved into i386 code, but my patch is already fairly large, and I'd prefer a PC person were involved in this additional cleanup anyways.
This patch has been tested and is needed to load modules on PPC. Please comment soon so we can get PPC modules working in the main tree...
-Hollis
Index: boot/powerpc/ieee1275/crt0.S
===================================================================
RCS file: /cvsroot/grub/grub2/boot/powerpc/ieee1275/crt0.S,v retrieving revision 1.4
powerpc/ieee1275/crt0.S +0 -15
@@ -18,21 +18,6 @@ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ - .section ".note" - .align 2 -.note_section_header: - .long 8 - .long 24 - .long 0x1275 - .string "PowerPC" -.note_descriptor: - .long 0x0 /* real-mode */ - .long 0xffffffff /* real-base */ - .long 0xffffffff /* real-size */ - .long 0xffffffff /* virt-base */ - .long 0xffffffff /* virt-size */ - .long 0x00030000 /* load-base */ - .extern __bss_start .extern _end
Index: conf/powerpc-ieee1275.rmk
===================================================================
RCS file: /cvsroot/grub/grub2/conf/powerpc-ieee1275.rmk,v retrieving revision 1.18
powerpc-ieee1275.rmk +5 -1
@@ -24,9 +24,13 @@ kernel_syms.lst: $(addprefix include/gru pkgdata_PROGRAMS = grubof # Utilities. -bin_UTILITIES = grub-emu +bin_UTILITIES = grub-emu grub-mkimage noinst_UTILITIES = genmoddep +# For grub-mkimage. +grub_mkimage_SOURCES = util/powerpc/ieee1275/grub-mkimage.c util/misc.c \ + util/resolve.c + # For grub-emu grub_emu_SOURCES = kern/main.c kern/device.c \ kern/disk.c kern/dl.c kern/file.c kern/fs.c kern/err.c \
Index: include/grub/kernel.h
===================================================================
RCS file: /cvsroot/grub/grub2/include/grub/kernel.h,v retrieving revision 1.4
grub/kernel.h +8 -5
@@ -31,8 +31,14 @@ struct grub_module_headergrub_size_t size; }; -/* The start address of the kernel. */ -extern grub_addr_t grub_start_addr; +#define MODINFO_VERSION 1 +struct grub_module_info { +grub_uint32_t version; +grub_uint32_t num_modules; +struct grub_module_header module_data[0 ]; +}; + +extern void grub_arch_load_modules (void );/* The end address of the kernel. */ extern grub_addr_t grub_end_addr;
@@ -49,9 +55,6 @@ void grub_main (void);/* The machine-specific initialization. This must initialize memory. */ void grub_machine_init (void ); -/* Return the end address of the core image. */ -grub_addr_t grub_get_end_addr (void ); -/* Register all the exported symbols. This is automatically generated. */ void grub_register_exported_symbols (void );
Index: include/grub/util/misc.h
===================================================================
RCS file: /cvsroot/grub/grub2/include/grub/util/misc.h,v retrieving revision 1.5
grub/util/misc.h +4 -0
@@ -34,9 +34,13 @@ void *xrealloc (void *ptr, size_t size);char *xstrdup (const char *str);char *grub_util_get_path (const char *dir,const char *file); +size_t grub_util_get_fp_size (FILE *fp);size_t grub_util_get_image_size (const char *path); +void grub_util_read_at (void *img,size_t len,off_t offset,FILE *fp);char *grub_util_read_image (const char *path);void grub_util_load_image (const char *path,char *buf);void grub_util_write_image (const char *img,size_t size,FILE *out); +void grub_util_write_image_at (const void *img,size_t size,off_t offset, +FILE *out);#endif /* ! GRUB_UTIL_MISC_HEADER */
Index: kern/main.c
===================================================================
RCS file: /cvsroot/grub/grub2/kern/main.c,v
retrieving revision 1.8
main.c +1 -24
@@ -29,29 +29,6 @@#include <grub/device.h> #include <grub/env.h> -/* Return the end of the core image. */ -grub_addr_t -grub_get_end_addr (void ) -{ -return grub_total_module_size + grub_end_addr; -} - -/* Load all modules in core. */ -static void -grub_load_modules (void ) -{ -struct grub_module_header *header; - -for (header = (struct grub_module_header *) grub_end_addr; - header < (struct grub_module_header *)grub_get_end_addr (); - header = (struct grub_module_header *) ((char *) header + header->size)) - { -if (!grub_dl_load_core ((char *) header + header->offset, - (header->size - header->offset))) -grub_fatal ("%s" , grub_errmsg); - } -} -/* Add the region where modules reside into dynamic memory. */ static void grub_add_unused_region (void )
@@ -110,7 +87,7 @@ grub_main (void)/* Load pre-loaded modules and free the space. */ grub_register_exported_symbols (); -grub_load_modules (); +grub_arch_load_modules ();grub_add_unused_region ();/* Load the normal mode module. */
Index: kern/i386/pc/init.c
===================================================================
RCS file: /cvsroot/grub/grub2/kern/i386/pc/init.c,v retrieving revision 1.9
i386/pc/init.c +23 -0
@@ -233,3 +233,26 @@ grub_machine_init (void)/* Initialize the prefix. */ grub_env_set ("prefix" ,make_install_device ()); } + +/* Return the end of the core image. */ +static grub_addr_t +grub_get_end_addr (void ) +{ +return grub_total_module_size + grub_end_addr; +} + +/* Load all modules in core. */ +void +grub_arch_load_modules (void ) +{ +struct grub_module_header *header; + +for (header = (struct grub_module_header *) grub_end_addr; + header < (struct grub_module_header *)grub_get_end_addr (); + header = (struct grub_module_header *) ((char *) header + header->size)) + { +if (!grub_dl_load_core ((char *) header + header->offset, + (header->size - header->offset))) +grub_fatal ("%s" , grub_errmsg); + } +}
Index: kern/powerpc/ieee1275/init.c
===================================================================
RCS file: /cvsroot/grub/grub2/kern/powerpc/ieee1275/init.c,v retrieving revision 1.11
powerpc/ieee1275/init.c +23 -0
@@ -31,6 +31,7 @@#include <grub/misc.h> #include <grub/machine/init.h> #include <grub/machine/time.h> +#include <grub/machine/kernel.h> /* XXX: Modules are not yet supported. */ grub_addr_t grub_end_addr =-1 ;
@@ -84,3 +85,25 @@ grub_get_rtc (void) {return 0 ; } + +/* Load all modules in core. */ +void +grub_arch_load_modules (void ) +{ +struct grub_module_info *base = (struct grub_module_info *)MODULE_BASE ; +struct grub_module_header *header = base->module_data; +unsigned int i; + +if (base->version !=MODINFO_VERSION ) +grub_fatal ("module version mismatch: wanted %d; found %d\n" , +MODINFO_VERSION , base->version); + +for (i =0 ; i < base->num_modules; i++) + { +if (!grub_dl_load_core ((char *) header + header->offset, + (header->size - header->offset))) +grub_fatal ("%s" , grub_errmsg); + header = (struct grub_module_header *) ((char *) header + header->size); + } +} +
Index: util/misc.c
===================================================================
RCS file: /cvsroot/grub/grub2/util/misc.c,v
retrieving revision 1.9
misc.c +37 -5
77 lines not on this page · raw message
Index: util/powerpc/ieee1275/grub-mkimage.c
===================================================================
RCS file: util/powerpc/ieee1275/grub-mkimage.c
powerpc/ieee1275/grub-mkimage.c new file +296 -0
296 lines not on this page · raw message
Index: include/grub/powerpc/ieee1275/kernel.h
===================================================================
RCS file: include/grub/powerpc/ieee1275/kernel.h
grub/powerpc/ieee1275/kernel.h new file +26 -0
@@ -0,0 +1,26 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2002 Free Software Foundation, Inc. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#ifndef GRUB_KERNEL_MACHINE_HEADER +#define GRUB_KERNEL_MACHINE_HEADER 1 + +/* Where grub-mkimage places the core modules in memory. */ +#define MODULE_BASE 0x00300000 + +#endif
Quoted 5 lines · Hollis Blanchard
This is a more complete patch for PPC grub-mkimage that I posted a while back. Important points:
- movegrub_load_modules()into i386/pc asgrub_arch_load_modules()
- implement PPC'sgrub_arch_load_modules()using an in-memory structure telling us how many modules to expect
I would prefer a grub_load_modules and a function to return the address and size of the modules in memory. I explained this in my other email.
Anyway, I'd prefer this to work the same on every machine and this code to be shared.
Quoted 2 lines
- the structure contains a version field to avoid grub-mkimage/grub binary layout mismatches
I don't think versioning is required.
Quoted 4 lines
I think there is still some PC cleanup needed, e.g.
grub_end_addrandgrub_add_unused_region()should also be moved into i386 code, but my patch is already fairly large, and I'd prefer a PC person were involved in this additional cleanup anyways.
Ok. Can you at least make sure it compiles on the PC and try not to break it?
Quoted 2 lines
This patch has been tested and is needed to load modules on PPC. Please comment soon so we can get PPC modules working in the main tree...
I assume you will make a changelog entry when the patch is more or less ok? It's easier to review a patch using a changelog entry.
Quoted 13 lines
Index:
include/grub/kernel.h
===================================================================
RCS file: /cvsroot/grub/grub2/include/grub/kernel.h,v retrieving revision 1.4
diff -u -p -r1.4kernel.hgrub/kernel.h +0
-2@@ -31,8 +31,14 @@ struct grub_module_headergrub_size_t size; }; -/* The start address of the kernel. */ -extern grub_addr_t grub_start_addr;
...
grub/kernel.h void grub_main (void); · line 53
Quoted 2 lines
grub/kernel.h +0
-2-/* Return the end address of the core image. */ -grub_addr_t grub_get_end_addr (void );
Doesn't this break the PC port?
Quoted 1 line
+/* TODO: endianness */
Better move this to the TODO list.
Quoted 1 line
+void
load_note(Elf32_Phdr*phdr, const char *dir, FILE *out)
GCS nitpick: put the return type on a separate line:
voidload_note (Elf32_Phdr *phdr, const char *dir, FILE *out)
Same for all other functions, btw.
grub/kernel.h
Quoted 4 lines
grub/kernel.h +4
-0+void load_modules (Elf32_Phdr *phdr,const char *dir,char *mods[],FILE *out) +{ +char *module_img; +struct grub_util_path_list *path_list, *p;
Please use separate lines for every declaration:
struct grub_util_path_list *path_list;
struct grub_util_path_list *p;
Thanks,
Marco
Quoted 9 lines · Marco Gerards
Quoted 6 lines · Hollis Blanchard
This is a more complete patch for PPC grub-mkimage that I posted a while back. Important points:
- movegrub_load_modules()into i386/pc asgrub_arch_load_modules()
- implement PPC'sgrub_arch_load_modules()using an in-memory structure
telling us how many modules to expect
Quoted 3 lines
I would prefer a
grub_load_modulesand a function to return the address and size of the modules in memory. I explained this in my other email.
I suggested the same thing. But I'd really rather have someone who understands the PC side to make such changes. I don't have the equipment or the interest to rework the PC code. This patch has almost no impact on the PC side; sharing the code will be more invasive.
Quoted 2 lines
Anyway, I'd prefer this to work the same on every machine and this code to be shared.
I understand. I will see what I can do.
Quoted 4 lines
Quoted 2 lines
- the structure contains a version field to avoid grub-mkimage/grub binary layout mismatches
I don't think versioning is required.
Fine.
Quoted 7 lines
Quoted 4 lines
I think there is still some PC cleanup needed, e.g.
grub_end_addrandgrub_add_unused_region()should also be moved into i386 code, but my patch is already fairly large, and I'd prefer a PC person were involved in this additional cleanup anyways.Ok. Can you at least make sure it compiles on the PC and try not to break it?
I have tried not to break it. Even compile-testing is going to be pretty inconvenient I think, but I'll see...
Quoted 6 lines
Quoted 3 lines
This patch has been tested and is needed to load modules on PPC. Please comment soon so we can get PPC modules working in the main tree...
I assume you will make a changelog entry when the patch is more or less ok? It's easier to review a patch using a changelog entry.
Yes, but there was no sense in me making a changelog description when the patch was just going to be ripped apart anyways. :)
Quoted 16 lines
Quoted 13 lines
Index:
include/grub/kernel.h
===================================================================
RCS file: /cvsroot/grub/grub2/include/grub/kernel.h,v retrieving revision 1.4
diff -u -p -r1.4kernel.hgrub/kernel.h +0
-2@@ -31,8 +31,14 @@ struct grub_module_headergrub_size_t size; }; -/* The start address of the kernel. */ -extern grub_addr_t grub_start_addr;...
Quoted 4 lines
grub/kernel.h void grub_main (void); · line 53
Quoted 2 lines
grub/kernel.h +0
-2-/* Return the end address of the core image. */ -grub_addr_t grub_get_end_addr (void );Doesn't this break the PC port?
grub_get_end_addr() has only a single caller, and was moved with that caller.
grub_start_addr is never used anywhere.
Quoted 3 lines
Quoted 1 line
+/* TODO: endianness */
Better move this to the TODO list.
Ok.
Quoted 8 lines
Quoted 1 line
+void
load_note(Elf32_Phdr*phdr, const char *dir, FILE *out)GCS nitpick: put the return type on a separate line:
void
load_note(Elf32_Phdr*phdr, const char *dir, FILE *out)Same for all other functions, btw.
Ok.
Quoted 7 lines
grub/kernel.h
Quoted 5 lines
+void
load_modules(Elf32_Phdr*phdr, const char *dir, char *mods[], FILE *out)grub/kernel.h +3
-0+{ +char *module_img; +struct grub_util_path_list *path_list, *p;Please use separate lines for every declaration:
Ok.
-Hollis
Quoted 4 lines · Hollis Blanchard
I suggested the same thing. But I'd really rather have someone who understands the PC side to make such changes. I don't have the equipment or the interest to rework the PC code. This patch has almost no impact on the PC side; sharing the code will be more invasive.
Ok. I can do that, no problem. In that case, please don't move the function out of kern/main.c but just write the PPC parts. In that case breaking the PC port is not a problem because I will immediately start fixing. I can apply the patches short after each other so there will not be any problem.
Quoted 4 lines
Quoted 2 lines
Anyway, I'd prefer this to work the same on every machine and this code to be shared.
I understand. I will see what I can do.
Leave that to me as well if that is more comfortable for you.
Quoted 5 lines
Quoted 2 lines
I assume you will make a changelog entry when the patch is more or less ok? It's easier to review a patch using a changelog entry.
Yes, but there was no sense in me making a changelog description when the patch was just going to be ripped apart anyways. :)
LOL!
Thanks,
Marco
Re: Pentium II boot failure
Quoted 7 lines · Marco Gerards
Quoted 4 lines · Hollis Blanchard
I suggested the same thing. But I'd really rather have someone who understands the PC side to make such changes. I don't have the equipment or the interest to rework the PC code. This patch has almost no impact on the PC side; sharing the code will be more invasive.
Quoted 5 lines
Ok. I can do that, no problem. In that case, please don't move the function out of
kern/main.cbut just write the PPC parts. In that case breaking the PC port is not a problem because I will immediately start fixing. I can apply the patches short after each other so there will not be any problem.
I pulled a Pentium II out of the closet for testing, but unfortunately even stock CVS failed to boot to the GRUB prompt. I did a build, then
./grub-mkimage -d . -o image *.mod
dd if=image of=/dev/fd0
reboot
That should work, right? This system just hangs with nothing on the screen except for the last BIOS messages, and the floppy light goes off. The same thing happens when I use kernel.img without adding modules.
At least I could compile-test... I hope to send the updated patch out in a few minutes.
-Hollis
Re: Pentium II boot failure
Quoted 9 lines · Hollis Blanchard
I pulled a Pentium II out of the closet for testing, but unfortunately even stock CVS failed to boot to the GRUB prompt. I did a build, then
./grub-mkimage -d . -o image *.mod
dd if=image of=/dev/fd0
reboot
That should work, right? This system just hangs with nothing on the screen except for the last BIOS messages, and the floppy light goes off. The same thing happens when I use kernel.img without adding modules.
This sounds like a problem Vincent has. Anyway, it does not matter that much. At least it compiles, I can test if it also works.
Thanks,
Marco
Re: Pentium II boot failure
Quoted 14 lines · Marco Gerards
Quoted 9 lines · Hollis Blanchard
I pulled a Pentium II out of the closet for testing, but unfortunately even stock CVS failed to boot to the GRUB prompt. I did a build, then
./grub-mkimage -d . -o image *.mod
dd if=image of=/dev/fd0
reboot
That should work, right? This system just hangs with nothing on the screen except for the last BIOS messages, and the floppy light goes off. The same thing happens when I use kernel.img without adding modules.
Quoted 2 lines
This sounds like a problem Vincent has. Anyway, it does not matter that much. At least it compiles, I can test if it also works.
Actually, the problem I had was that grub2 gives that error during stage 1 :
GRUB Read Error
and is because an asm instruction was removed in stage 1 which was a workaround for buggy BIOSes like mine.
Does your computer works with grub legacy ? there are not much changes between legacy's stage 1 and grub 2's one.
Vincent Pelletier
Re: Pentium II boot failure
Quoted 14 lines · Vincent Pelletier
Quoted 10 lines · Marco Gerards
Quoted 9 lines · Hollis Blanchard
I pulled a Pentium II out of the closet for testing, but unfortunately even stock CVS failed to boot to the GRUB prompt. I did a build, then
./grub-mkimage -d . -o image *.mod
dd if=image of=/dev/fd0
reboot
That should work, right? This system just hangs with nothing on the screen except for the last BIOS messages, and the floppy light goes off. The same thing happens when I use kernel.img without adding modules.Quoted 2 lines
This sounds like a problem Vincent has. Anyway, it does not matter that much. At least it compiles, I can test if it also works.
Quoted 7 lines
Actually, the problem I had was that grub2 gives that error during stage 1 :
GRUB Read Error
and is because an asm instruction was removed in stage 1 which was a workaround for buggy BIOSes like mine.Does your computer works with grub legacy ? there are not much changes between legacy's stage 1 and grub 2's one.
GRUB Legacy, i.e. ftp://alpha.gnu.org/gnu/grub/grub-0.95-i386-pc.ext2fs , does boot. It could be a problem with my compiler I guess... it's a stock Debian compiler though.
-Hollis