Skip to contentSkip to conversation

Move ELF program header

PATCH In review Bean 3 participants 18 messages Jun 2, 2008 – Jun 17, 2008

lore
Share & tools
lore

Bean

author
#0 lore raw

PATCH 1/1 Move ELF program header

Hi,

This patch move the elf program header to the beginning, it should solve the problem with coreboot.

Signature · 1 line
Bean
util/elf/grub-mkimage.c +23 -11
index ca138dd..51c4e54 100644
@@ -166,8 +166,8 @@ add_segments (char *dir, FILE *out, int chrp, char *mods[])
   FILE *in;
   char *kernel_path;
   grub_addr_t grub_end = 0;
-  off_t phdroff;
-  int i;
+  off_t offset;
+  int i, phdr_size;
 
   /* Read ELF header.  */
   kernel_path = grub_util_get_path (dir, "kernel.elf");
@@ -177,8 +177,21 @@ add_segments (char *dir, FILE *out, int chrp, char *mods[])
 
   grub_util_read_at (&ehdr, sizeof (ehdr), 0, in);
   
-  phdrs = xmalloc (grub_target_to_host16 (ehdr.e_phentsize)
-		   * (grub_target_to_host16 (ehdr.e_phnum) + 2));
+  offset = ALIGN_UP (sizeof (ehdr), sizeof (long));
+  ehdr.e_phoff = grub_host_to_target32 (offset);
+
+  phdr_size = (grub_target_to_host16 (ehdr.e_phentsize) *
+               grub_target_to_host16 (ehdr.e_phnum));
+
+  if (mods[0] != NULL)
+    phdr_size += grub_target_to_host16 (ehdr.e_phentsize);
+
+  if (chrp)
+    phdr_size += grub_target_to_host16 (ehdr.e_phentsize);
+
+  phdrs = xmalloc (phdr_size);
+  offset += ALIGN_UP (phdr_size, sizeof (long));
+
   /* Copy all existing segments.  */
   for (i = 0; i < grub_target_to_host16 (ehdr.e_phnum); i++)
     {
@@ -207,8 +220,11 @@ add_segments (char *dir, FILE *out, int chrp, char *mods[])
   
       grub_util_read_at (segment_img, grub_target_to_host32 (phdr->p_filesz),
 			 grub_target_to_host32 (phdr->p_offset), in);
+
+      phdr->p_offset = grub_host_to_target32 (offset);
       grub_util_write_image_at (segment_img, grub_target_to_host32 (phdr->p_filesz),
-				grub_target_to_host32 (phdr->p_offset), out);
+				offset, out);
+      offset += ALIGN_UP (grub_target_to_host32 (phdr->p_filesz), sizeof (long));
 
       free (segment_img);
     }
@@ -249,14 +265,10 @@ add_segments (char *dir, FILE *out, int chrp, char *mods[])
   ehdr.e_shnum = 0;
   ehdr.e_shstrndx = 0;
 
-  /* Append entire segment table to the file.  */
-  phdroff = ALIGN_UP (grub_util_get_fp_size (out), sizeof (long));
-  grub_util_write_image_at (phdrs, grub_target_to_host16 (ehdr.e_phentsize)
-			    * grub_target_to_host16 (ehdr.e_phnum), phdroff,
-			    out);
+  /* Write entire segment table to the file.  */
+  grub_util_write_image_at (phdrs, phdr_size, grub_target_to_host32 (ehdr.e_phoff), out);
 
   /* Write ELF header.  */
-  ehdr.e_phoff = grub_host_to_target32 (phdroff);
   grub_util_write_image_at (&ehdr, sizeof (ehdr), 0, out);
 
   free (phdrs);
author
#1 lore raw
Quoted 8 lines · Bean

Hi,

This patch move the elf program header to the beginning, it should solve the problem with coreboot.

--
Bean

Any comment ?

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

Hi,

This patch move the elf program header to the beginning, it should solve the problem with coreboot.

--
Bean

Any comment ?

I don't have time to check atm, but please make sure it doesn't break ieee1275 support.

Signature · 5 lines
Robert Millan

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

Hi,

This patch move the elf program header to the beginning, it should solve the problem with coreboot.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

--
Bean

Any comment ?

I don't have time to check atm, but please make sure it doesn't break ieee1275 support.

Hi,

I test it with olpc and it works. I don't have ppc, would someone check it ?

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

I test it with olpc and it works. I don't have ppc, would someone check it ?

I could make CVS GRUB compile on PowerPC after removing floating point code, but I could not get the rescue image to boot in qemu, with or without the patch. I know that it worked before (it would show "out of memory" due to Open Hack'Ware limitations, but at least the GRUB code was loaded and running).

I'm afraid I won't be able to spend any more time on the PowerPC port. If anyone cares, they would need to fix a lot of stuff.

Signature · 2 lines
Regards,
Pavel Roskin
author
#5 lore raw
Quoted 4 lines · Pavel Roskin
Quoted 1 line · Bean

I test it with olpc and it works. I don't have ppc, would someone check it ?

Quoted 8 lines

I could make CVS GRUB compile on PowerPC after removing floating point code, but I could not get the rescue image to boot in qemu, with or without the patch. I know that it worked before (it would show "out of memory" due to Open Hack'Ware limitations, but at least the GRUB code was loaded and running).

I'm afraid I won't be able to spend any more time on the PowerPC port. If anyone cares, they would need to fix a lot of stuff.

Hi,

I see, you're using qemu to test ppc, maybe I can try it some time. Is the emulation good, how close to the real hardware ?

btw, do you remember which version works ? We can track down the problem from there.

Signature · 1 line
Bean
#6 lore raw

Quoting Bean <[email protected]>:

Quoted 2 lines

I see, you're using qemu to test ppc, maybe I can try it some time. Is the emulation good, how close to the real hardware ?

qemu uses so called Open Hack'Ware, which doesn't have proper terminal support for the menu and doesn't report available memory in the way GRUB expects. The later causes "out of memory" messages, but it can be worked around in GRUB by hardcoding memory size.

Quoted 2 lines

btw, do you remember which version works ? We can track down the problem from there.

It was working when grub-mkrescue was added for PowerPC. According to the logs, that's January 31, 2008.

It looks like my G3 PowerMac died yesterday and won't turn on anymore.
The same thing happened to my G4 PowerMac several months ago. So, I
guess qemu remains the only option for me.

Signature · 2 lines
Regards,
Pavel Roskin
author
#7 lore raw
Quoted 13 lines · Pavel Roskin

Quoting Bean <[email protected]>:

Quoted 2 lines

I see, you're using qemu to test ppc, maybe I can try it some time. Is the emulation good, how close to the real hardware ?

qemu uses so called Open Hack'Ware, which doesn't have proper terminal support for the menu and doesn't report available memory in the way GRUB expects. The later causes "out of memory" messages, but it can be worked around in GRUB by hardcoding memory size.

Quoted 2 lines

btw, do you remember which version works ? We can track down the problem from there.

Quoted 6 lines

It was working when grub-mkrescue was added for PowerPC. According to the logs, that's January 31, 2008.

It looks like my G3 PowerMac died yesterday and won't turn on anymore. The same thing happened to my G4 PowerMac several months ago. So, I guess qemu remains the only option for me.

Hi,

Oh, this problem is caused by grub_ofconsole_setcursor. Open Hackware doesn't like the cursor-on call, if I comment it out, it will continue to run until the "out of memory" error.

Signature · 1 line
Bean
#8 lore raw
Quoted 6 lines · Bean

Hi,

Oh, this problem is caused by grub_ofconsole_setcursor. Open Hackware doesn't like the cursor-on call, if I comment it out, it will continue to run until the "out of memory" error.

We could have a flag in kern/ieee1275/cmain.c for this. There's a stanza for Open Hackware already.

Signature · 5 lines
Robert Millan

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

Hi,

Oh, this problem is caused by grub_ofconsole_setcursor. Open Hackware doesn't like the cursor-on call, if I comment it out, it will continue to run until the "out of memory" error.

We could have a flag in kern/ieee1275/cmain.c for this. There's a stanza for Open Hackware already.

Hi,

That flag is not enough, actually, Open Hackware don't support interpret command at all, perhaps we need to a new flag GRUB_IEEE1275_FLAG_CANNOT_INTERPRET.

Signature · 1 line
Bean
#10 lore raw
In reply to Robert Millan
Quoted 10 lines · Robert Millan
Quoted 6 lines · Bean

Hi,

Oh, this problem is caused by grub_ofconsole_setcursor. Open Hackware doesn't like the cursor-on call, if I comment it out, it will continue to run until the "out of memory" error.

We could have a flag in kern/ieee1275/cmain.c for this. There's a stanza for Open Hackware already.

Actually, the real OpenFirmware on PowerMac G3 is also complaining about "cursor on", but it's not fatal. Open Hackware doesn't have Forth interpreter, so it recognizes Forth commands by checksums and panics for unknown checksums.

I could resurrect my Mac by replugging everything on the motherboard.

The real OpenFirmware fails to recognize modules embedded into the image by grub-mkimage. I remember I spent some time fixing it, but it's broken again.

Signature · 2 lines
Regards,
Pavel Roskin
#11 lore raw
Quoted 4 lines · Pavel Roskin

The real OpenFirmware fails to recognize modules embedded into the image by grub-mkimage. I remember I spent some time fixing it, but it's broken again.

That's strange, it does work on i386 with the "real" OpenFirmware (I think you're referring to the same implementation)

Signature · 5 lines
Robert Millan

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

The real OpenFirmware fails to recognize modules embedded into the image by grub-mkimage. I remember I spent some time fixing it, but it's broken again.

That's strange, it does work on i386 with the "real" OpenFirmware (I think you're referring to the same implementation)

I don't know if it's the same implementation. It's whatever Apple put on their G3 machines.

Signature · 2 lines
Regards,
Pavel Roskin
author
#13 lore raw
In reply to Pavel Roskin
Quoted 3 lines · Pavel Roskin

The real OpenFirmware fails to recognize modules embedded into the image by grub-mkimage. I remember I spent some time fixing it, but it's broken again.

Hi,

Are you using the native compiler, or cross compile from i386 ? It could have some effect.

Signature · 1 line
Bean
#14 lore raw
Quoted 9 lines · Bean
Quoted 3 lines · Pavel Roskin

The real OpenFirmware fails to recognize modules embedded into the image by grub-mkimage. I remember I spent some time fixing it, but it's broken again.

Hi,

Are you using the native compiler, or cross compile from i386 ? It could have some effect.

I'm compiling natively on PowerPC.

Signature · 2 lines
Regards,
Pavel Roskin
author
#15 lore raw
Quoted 6 lines · Pavel Roskin
Quoted 5 lines · Bean
Quoted 3 lines · Pavel Roskin

The real OpenFirmware fails to recognize modules embedded into the image by grub-mkimage. I remember I spent some time fixing it, but it's broken again.

Quoted 6 lines
Quoted 4 lines

Hi,

Are you using the native compiler, or cross compile from i386 ? It could have some effect.

I'm compiling natively on PowerPC.

It could be an endian problem. Check the elf header, see if the fields are ok.

Signature · 1 line
Bean
#16 lore raw
Quoted 6 lines · Bean
Quoted 4 lines
Quoted 2 lines

Are you using the native compiler, or cross compile from i386 ? It could have some effect.

I'm compiling natively on PowerPC.

It could be an endian problem. Check the elf header, see if the fields are ok.

That's the headers, I don't see anything wrong. The sections are missing, but it has always been like that. The version is current from CVS.

$ ./grub-mkimage -o grub.img -d . *.mod
$ objdump -x grub.img

grub.img: file format elf32-powerpc
grub.img
architecture: powerpc:common, flags 0x00000102:
EXEC_P, D_PAGED
start address 0x00010000

Program Header:

    LOAD off    0x000000c0 vaddr 0x00010000 paddr 0x00010000 align 2**4
         filesz 0x0000bf04 memsz 0x000133a8 flags rwx
    LOAD off    0x0000bfc4 vaddr 0x100000b4 paddr 0x100000b4 align 2**2
         filesz 0x00000024 memsz 0x00000024 flags r--
    NOTE off    0x0000bfc4 vaddr 0x100000b4 paddr 0x100000b4 align 2**2
         filesz 0x00000024 memsz 0x00000024 flags r--
   STACK off    0x00000000 vaddr 0x00000000 paddr 0x00000000 align 2**2
         filesz 0x00000000 memsz 0x00000000 flags rwx
    LOAD off    0x0000bfe8 vaddr 0x10009000 paddr 0x10009000 align 2**2
         filesz 0x00052104 memsz 0x00052104 flags rwx

Sections:
Idx Name Size VMA LMA File off Algn
SYMBOL TABLE:
no symbols

The image loads, complains about "cursor-on", shows "Welcome to GRUB" and the prompt. "lsmod" shows an empty list.

Signature · 2 lines
Regards,
Pavel Roskin
author
#17 lore raw
Quoted 33 lines · Pavel Roskin
Quoted 6 lines · Bean
Quoted 4 lines
Quoted 2 lines

Are you using the native compiler, or cross compile from i386 ? It could have some effect.

I'm compiling natively on PowerPC.

It could be an endian problem. Check the elf header, see if the fields are ok.

That's the headers, I don't see anything wrong. The sections are missing, but it has always been like that. The version is current from CVS.

$ ./grub-mkimage -o grub.img -d . *.mod
$ objdump -x grub.img

grub.img: file format elf32-powerpc
grub.img
architecture: powerpc:common, flags 0x00000102:
EXEC_P, D_PAGED
start address 0x00010000

Program Header:

   LOAD off    0x000000c0 vaddr 0x00010000 paddr 0x00010000 align 2**4
        filesz 0x0000bf04 memsz 0x000133a8 flags rwx
   LOAD off    0x0000bfc4 vaddr 0x100000b4 paddr 0x100000b4 align 2**2
        filesz 0x00000024 memsz 0x00000024 flags r--
   NOTE off    0x0000bfc4 vaddr 0x100000b4 paddr 0x100000b4 align 2**2
        filesz 0x00000024 memsz 0x00000024 flags r--
  STACK off    0x00000000 vaddr 0x00000000 paddr 0x00000000 align 2**2
        filesz 0x00000000 memsz 0x00000000 flags rwx
   LOAD off    0x0000bfe8 vaddr 0x10009000 paddr 0x10009000 align 2**2
        filesz 0x00052104 memsz 0x00052104 flags rwx
Quoted 8 lines

Sections:
Idx Name Size VMA LMA File off Algn
SYMBOL TABLE:
no symbols

The image loads, complains about "cursor-on", shows "Welcome to GRUB" and the prompt. "lsmod" shows an empty list.

Hi,

You can also add some printf in kern/elf.c to see if grub can parse it properly, for example, grub_elf_check_header is a common place where problem occurs.

Signature · 1 line
Bean