fix load of RHEL ans SUSE images by correctly calculate ELF entry point
PATCH 1/1 fix load of RHEL ans SUSE images by correctly calculate ELF entry point
This Patch loads kernel LOAD segment at the address fixed by PhysAddr with alignment Align at the ELF program header. Even if this address is in use other address with the same alignment is found "linuxadr" (since linux can relocate itself). The linux main function is then find in the adress linuxaddr + entry + (PhysAddr- Entry), where Entry is the Entry point address in the ELF header. (PhysAddr- Entry) gives the offset where the linux entry point is located after linuxaddr.
Signature · 5 lines
Best Regards, Manoel Rebelo Abranches Software engineer IBM - Linux Technology Center - Brazil
=== modified file 'ChangeLog'
ChangeLog +11 -0
@@ -1,3 +1,14 @@ +2010-09-27 Manoel Rebelo Abranches <[email protected]> + + Correct allocate kernel segment and entry point address. That corrects + booting of some images, like RHEL and SLES. + + * grub-core/loader/powerpc/ieee1275/linux.c (grub_linux_load32): + Allocate the kernel segment of type LOAD at the PhysAddr address and + using alignment in the program header. Also uses ELF header entry point + minus PhysAddr as offset to linux main function. + (grub_linux_load64): Likewise. + 2010-09-26 Robert Millan <[email protected]> Support degraded ZFS arrays in "grub-probe -t device" resolution.
=== modified file 'grub-core/loader/powerpc/ieee1275/linux.c'
loader/powerpc/ieee1275/linux.c +69 -66
164 lines not on this page · raw message
This is another version to the same fix.
The first patch 01_mem.patch creates a new function to allocate memory using the "available" property in the "memory" node.
The second patch 02_offset.patch correctly finds the linux entry point, also assumes that more segments can exist (made accordingly with sparc implementation)
The patch was made passing -Bbp to diff as suggested by Vladimir. I tested with RHEL/SUSE images and some RHEL/SUSE kernel I had and worked correctly. Before the images failed to boot. Thank you for reviewing this.
Quoted 13 lines · Manoel Rebelo Abraches
This Patch loads kernel LOAD segment at the address fixed by PhysAddr with alignment Align at the ELF program header. Even if this address is in use other address with the same alignment is found "linuxadr" (since linux can relocate itself). The linux main function is then find in the adress linuxaddr + entry + (PhysAddr- Entry), where Entry is the Entry point address in the ELF header. (PhysAddr- Entry) gives the offset where the linux entry point is located after linuxaddr.
_______________________________________________
Grub-devel mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/grub-devel
Signature · 4 lines
Best Regards, Manoel R. Abranches IBM Linux Technology Center Brazil
=== modified file 'grub-core/kern/elf.c'
kern/elf.c +12 -4
@@ -171,11 +171,12 @@ grub_elf32_phdr_iterate (grub_elf_t elf,/* Calculate the amount of memory spanned by the segments. */ grub_size_t -grub_elf32_size (grub_elf_t elf,Elf32_Addr *base) +grub_elf32_size (grub_elf_t elf,Elf32_Addr *base,grub_uint32_t *max_align) {Elf32_Addr segments_start = (Elf32_Addr) -1 ;Elf32_Addr segments_end =0 ;int nr_phdrs =0 ; +grub_uint32_t curr_align =0 ;/* Run through the program headers to calculate the total memory size we * should claim. */
@@ -192,6 +193,8 @@ grub_elf32_size (grub_elf_t elf, Elf32_A segments_start = phdr->p_paddr;if (phdr->p_paddr + phdr->p_memsz > segments_end) segments_end = phdr->p_paddr + phdr->p_memsz; +if (curr_align < phdr->p_align) + curr_align = phdr->p_align;return 0 ; }
@@ -215,7 +218,8 @@ grub_elf32_size (grub_elf_t elf, Elf32_Aif (base) *base = segments_start; - +if (max_align) + *max_align = curr_align;return segments_end - segments_start; }
@@ -357,11 +360,12 @@ grub_elf64_phdr_iterate (grub_elf_t elf,/* Calculate the amount of memory spanned by the segments. */ grub_size_t -grub_elf64_size (grub_elf_t elf,Elf64_Addr *base) +grub_elf64_size (grub_elf_t elf,Elf64_Addr *base,grub_uint64_t *max_align) {Elf64_Addr segments_start = (Elf64_Addr) -1 ;Elf64_Addr segments_end =0 ;int nr_phdrs =0 ; +grub_uint64_t curr_align =0 ;/* Run through the program headers to calculate the total memory size we * should claim. */
@@ -378,6 +382,8 @@ grub_elf64_size (grub_elf_t elf, Elf64_A segments_start = phdr->p_paddr;if (phdr->p_paddr + phdr->p_memsz > segments_end) segments_end = phdr->p_paddr + phdr->p_memsz; +if (curr_align < phdr->p_align) + curr_align = phdr->p_align;return 0 ; }
@@ -401,7 +407,8 @@ grub_elf64_size (grub_elf_t elf, Elf64_Aif (base) *base = segments_start; - +if (max_align) + *max_align = curr_align;return segments_end - segments_start; }
=== modified file 'grub-core/loader/mips/linux.c'
loader/mips/linux.c +2 -2
@@ -83,7 +83,7 @@ grub_linux_load32 (grub_elf_t elf, void/* Linux's entry point incorrectly contains a virtual address. */ entry_addr = elf->ehdr.ehdr32.e_entry; - linux_size =grub_elf32_size (elf, &base); + linux_size =grub_elf32_size (elf, &base,0 );if (linux_size ==0 )return grub_errno; target_addr = base;
@@ -138,7 +138,7 @@ grub_linux_load64 (grub_elf_t elf, void/* Linux's entry point incorrectly contains a virtual address. */ entry_addr = elf->ehdr.ehdr64.e_entry; - linux_size =grub_elf64_size (elf, &base); + linux_size =grub_elf64_size (elf, &base,0 );if (linux_size ==0 )return grub_errno; target_addr = base;
=== modified file 'grub-core/loader/powerpc/ieee1275/linux.c'
loader/powerpc/ieee1275/linux.c +27 -22
95 lines not on this page · raw message
=== modified file 'grub-core/loader/sparc64/ieee1275/linux.c'
loader/sparc64/ieee1275/linux.c +1 -1
@@ -247,7 +247,7 @@ grub_linux_load64 (grub_elf_t elf) linux_entry = elf->ehdr.ehdr64.e_entry; linux_addr =0x40004000 ; off =0x4000 ; - linux_size =grub_elf64_size (elf,0 ); + linux_size =grub_elf64_size (elf,0 ,0 );if (linux_size ==0 )return grub_errno;
=== modified file 'include/grub/elfload.h'
grub/elfload.h +2 -2
@@ -46,12 +46,12 @@ grub_elf_t grub_elf_file (grub_file_t);grub_err_t grub_elf_close (grub_elf_t );int grub_elf_is_elf32 (grub_elf_t ); -grub_size_t grub_elf32_size (grub_elf_t ,Elf32_Addr *); +grub_size_t grub_elf32_size (grub_elf_t ,Elf32_Addr *,grub_uint32_t *);grub_err_t grub_elf32_load (grub_elf_t ,grub_elf32_load_hook_t ,grub_addr_t *,grub_size_t *);int grub_elf_is_elf64 (grub_elf_t ); -grub_size_t grub_elf64_size (grub_elf_t ,Elf64_Addr *); +grub_size_t grub_elf64_size (grub_elf_t ,Elf64_Addr *,grub_uint64_t *);grub_err_t grub_elf64_load (grub_elf_t ,grub_elf64_load_hook_t ,grub_addr_t *,grub_size_t *); grub_err_t
=== modified file 'grub-core/loader/powerpc/ieee1275/linux.c'
loader/powerpc/ieee1275/linux.c +48 -31
127 lines not on this page · raw message
ChangeLog +16 -0
@@ -1,3 +1,19 @@ +2010-10-18 Manoel Rebelo Abranches <[email protected]> + + Find the correct linux offset in PowerPC loader. + + * include/grub/elfload.h (grub_elf32_size): New parameter. All users updated. + Return maximum segments alignment. + (grub_elf64_size): Likewise. + * kern/elf.c (grub_elf32_size): New parameter. All users updated. + Return maximum segments alignment. + (grub_elf64_size): Likewise. + * grub-core/loader/powerpc/ieee1275/linux.c: + (grub_linux_claimmap_iterate): New function. Uses the "available"property + in the "memory" node for memory allocation for kernel in the PowerPC loader. + (grub_linux_load32): Correctly find linux entry point offset. + (grub_linux_load64): Likewise. + 2010-10-18 Grégoire Sutre <[email protected]> Make mktemp invocations portable.
Fixed some issues reported by Vladimir:
Initial alignment value 1 instead of 0.
Verify error by returning address being -1 since 0 may be valid in memory allocated directly by the Open Firmware. Use 'end' instead of (addr + len) to search for available memory.
change hardcoded value '1' to GRUB_MEMORY_AVAILABLE.
Quoted 29 lines · Manoel Rebelo Abraches
This is another version to the same fix.
The first patch 01_mem.patch creates a new function to allocate memory using the "available" property in the "memory" node.
The second patch 02_offset.patch correctly finds the linux entry point, also assumes that more segments can exist (made accordingly with sparc implementation)The patch was made passing -Bbp to diff as suggested by Vladimir. I tested with RHEL/SUSE images and some RHEL/SUSE kernel I had and worked correctly. Before the images failed to boot. Thank you for reviewing this.
Quoted 9 lines · Manoel Rebelo Abraches
This Patch loads kernel LOAD segment at the address fixed by PhysAddr with alignment Align at the ELF program header. Even if this address is in use other address with the same alignment is found "linuxadr" (since linux can relocate itself). The linux main function is then find in the adress linuxaddr + entry + (PhysAddr- Entry), where Entry is the Entry point address in the ELF header. (PhysAddr- Entry) gives the offset where the linux entry point is located after linuxaddr.
Quoted 4 lines
_______________________________________________
Grub-devel mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/grub-devel
Quoted 4 lines
_______________________________________________
Grub-devel mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/grub-devel
=== modified file 'grub-core/loader/powerpc/ieee1275/linux.c'
loader/powerpc/ieee1275/linux.c +52 -34
@@ -26,6 +26,7 @@#include <grub/ieee1275/ieee1275.h> #include <grub/command.h> #include <grub/i18n.h> +#include <grub/memory.h> #define ELF32_LOADMASK (0xc0000000UL)#define ELF64_LOADMASK (0xc000000000000000ULL)
@@ -45,6 +46,51 @@typedef void (*kernel_entry_t ) (void *,unsigned long ,int (void *),unsigned long ,unsigned long ); +static grub_addr_t +grub_linux_claimmap_iterate (grub_addr_t target,grub_size_t size, +grub_size_t align) +{ +grub_addr_t found_addr = (grub_addr_t) -1 ; + + auto int NESTED_FUNC_ATTR alloc_mem (grub_uint64_t addr, grub_uint64_t len, +grub_memory_type_t type); + int NESTED_FUNC_ATTR alloc_mem (grub_uint64_t addr, grub_uint64_t len, +grub_memory_type_t type) + { +grub_uint64_t end = addr + len; + addr =ALIGN_UP (addr, align); + target =ALIGN_UP (target, align); + +/* Target above the memory chunk. */ +if (type !=GRUB_MEMORY_AVAILABLE || target > end) +return 0 ; + +/* Target inside the memory chunk. */ +if (target >= addr && target < end && size <= end - target) + { +if (grub_claimmap (target, size) ==GRUB_ERR_NONE ) + { + found_addr = target; +return 1 ; + } + } +/* Target below the memory chunk. */ +if (target < addr && addr + size <= end) + { +if (grub_claimmap (addr, size) ==GRUB_ERR_NONE ) + { + found_addr = addr; +return 1 ; + } + } +return 0 ; + } + +grub_machine_mmap_iterate (alloc_mem); + +return found_addr; +} +static grub_err_t grub_linux_boot (void ) {
@@ -103,7 +149,6 @@ grub_linux_load32 (grub_elf_t elf) {Elf32_Addr entry; -int found_addr =0 ;/* Linux's entry point incorrectly contains a virtual address. */ entry = elf->ehdr.ehdr32.e_entry & ~ELF32_LOADMASK ;
@@ -119,15 +164,8 @@/* On some systems, firmware occupies the memory we're trying to use. * Happily, Linux can be loaded anywhere (it relocates itself). Iterate * until we find an open area. */ -for (linux_addr = entry; linux_addr < entry +200 *0x100000 ; linux_addr +=0x100000 ) - { -grub_dprintf ("loader" ,"Attempting to claim at 0x%x, size 0x%x.\n" , - linux_addr, linux_size); - found_addr =grub_claimmap (linux_addr, linux_size); -if (found_addr !=-1 ) -break ; - } -if (found_addr ==-1 ) + linux_addr =grub_linux_claimmap_iterate (entry, linux_size,0x100000 ); +if (linux_addr == (grub_addr_t) -1 )return grub_error (GRUB_ERR_OUT_OF_MEMORY ,"couldn't claim memory" );/* Now load the segments into the area we claimed. */
@@ -153,7 +191,6 @@ grub_linux_load64 (grub_elf_t elf) {Elf64_Addr entry; -int found_addr =0 ;/* Linux's entry point incorrectly contains a virtual address. */ entry = elf->ehdr.ehdr64.e_entry & ~ELF64_LOADMASK ;
@@ -169,15 +206,8 @@/* On some systems, firmware occupies the memory we're trying to use. * Happily, Linux can be loaded anywhere (it relocates itself). Iterate * until we find an open area. */ -for (linux_addr = entry; linux_addr < entry +200 *0x100000 ; linux_addr +=0x100000 ) - { -grub_dprintf ("loader" ,"Attempting to claim at 0x%x, size 0x%x.\n" , - linux_addr, linux_size); - found_addr =grub_claimmap (linux_addr, linux_size); -if (found_addr !=-1 ) -break ; - } -if (found_addr ==-1 ) + linux_addr =grub_linux_claimmap_iterate (entry, linux_size,0x100000 ); +if (linux_addr == (grub_addr_t) -1 )return grub_error (GRUB_ERR_OUT_OF_MEMORY ,"couldn't claim memory" );/* Now load the segments into the area we claimed. */
@@ -287,7 +317,6 @@grub_ssize_t size;grub_addr_t first_addr;grub_addr_t addr; -int found_addr =0 ;if (argc ==0 ) {
@@ -311,20 +340,9 @@/* Attempt to claim at a series of addresses until successful in the same way that grub_rescue_cmd_linux does. */ -for (addr = first_addr; addr < first_addr +200 *0x100000 ; addr +=0x100000 ) - { -grub_dprintf ("loader" ,"Attempting to claim at 0x%x, size 0x%x.\n" , - addr, size); - found_addr =grub_claimmap (addr, size); -if (found_addr !=-1 ) -break ; - } - -if (found_addr ==-1 ) - { -grub_error (GRUB_ERR_OUT_OF_MEMORY ,"cannot claim memory" ); + addr =grub_linux_claimmap_iterate (first_addr, size,0x100000 ); +if (addr == (grub_addr_t) -1 ) goto fail; - }grub_dprintf ("loader" ,"Loading initrd at 0x%x, size 0x%x\n" , addr, size);
=== modified file 'ChangeLog'
ChangeLog +14 -0
@@ -1,3 +1,17 @@ +2010-10-18 Manoel Rebelo Abranches <[email protected]> + + * include/grub/elfload.h (grub_elf32_size): New parameter. All users updated. + Return maximum segments alignment. + (grub_elf64_size): Likewise. + * kern/elf.c (grub_elf32_size): New parameter. All users updated. + Return maximum segments alignment. + (grub_elf64_size): Likewise. + * grub-core/loader/powerpc/ieee1275/linux.c: + (grub_linux_claimmap_iterate): New function. Uses the "available"property + in the "memory" node for memory allocation for kernel in the PowerPC loader. + (grub_linux_load32): Correctly find linux entry point offset. + (grub_linux_load64): Likewise. + 2010-10-26 Vladimir Serbinenko <[email protected]> * util/grub-setup.c (setup): Refuse to do a cross-disk embeddingless
=== modified file 'grub-core/kern/elf.c'
kern/elf.c +12 -4
@@ -171,11 +171,12 @@/* Calculate the amount of memory spanned by the segments. */ grub_size_t -grub_elf32_size (grub_elf_t elf,Elf32_Addr *base) +grub_elf32_size (grub_elf_t elf,Elf32_Addr *base,grub_uint32_t *max_align) {Elf32_Addr segments_start = (Elf32_Addr) -1 ;Elf32_Addr segments_end =0 ;int nr_phdrs =0 ; +grub_uint32_t curr_align =1 ;/* Run through the program headers to calculate the total memory size we * should claim. */
@@ -192,6 +193,8 @@ segments_start = phdr->p_paddr;if (phdr->p_paddr + phdr->p_memsz > segments_end) segments_end = phdr->p_paddr + phdr->p_memsz; +if (curr_align < phdr->p_align) + curr_align = phdr->p_align;return 0 ; }
@@ -215,7 +218,8 @@if (base) *base = segments_start; - +if (max_align) + *max_align = curr_align;return segments_end - segments_start; }
@@ -357,11 +360,12 @@/* Calculate the amount of memory spanned by the segments. */ grub_size_t -grub_elf64_size (grub_elf_t elf,Elf64_Addr *base) +grub_elf64_size (grub_elf_t elf,Elf64_Addr *base,grub_uint64_t *max_align) {Elf64_Addr segments_start = (Elf64_Addr) -1 ;Elf64_Addr segments_end =0 ;int nr_phdrs =0 ; +grub_uint64_t curr_align =1 ;/* Run through the program headers to calculate the total memory size we * should claim. */
@@ -378,6 +382,8 @@ segments_start = phdr->p_paddr;if (phdr->p_paddr + phdr->p_memsz > segments_end) segments_end = phdr->p_paddr + phdr->p_memsz; +if (curr_align < phdr->p_align) + curr_align = phdr->p_align;return 0 ; }
@@ -401,7 +407,8 @@if (base) *base = segments_start; - +if (max_align) + *max_align = curr_align;return segments_end - segments_start; }
=== modified file 'grub-core/loader/mips/linux.c'
loader/mips/linux.c +2 -2
@@ -83,7 +83,7 @@/* Linux's entry point incorrectly contains a virtual address. */ entry_addr = elf->ehdr.ehdr32.e_entry; - linux_size =grub_elf32_size (elf, &base); + linux_size =grub_elf32_size (elf, &base,0 );if (linux_size ==0 )return grub_errno; target_addr = base;
@@ -138,7 +138,7 @@/* Linux's entry point incorrectly contains a virtual address. */ entry_addr = elf->ehdr.ehdr64.e_entry; - linux_size =grub_elf64_size (elf, &base); + linux_size =grub_elf64_size (elf, &base,0 );if (linux_size ==0 )return grub_errno; target_addr = base;
=== modified file 'grub-core/loader/powerpc/ieee1275/linux.c'
loader/powerpc/ieee1275/linux.c +29 -24
99 lines not on this page · raw message
=== modified file 'grub-core/loader/sparc64/ieee1275/linux.c'
loader/sparc64/ieee1275/linux.c +1 -1
@@ -247,7 +247,7 @@ linux_entry = elf->ehdr.ehdr64.e_entry; linux_addr =0x40004000 ; off =0x4000 ; - linux_size =grub_elf64_size (elf,0 ); + linux_size =grub_elf64_size (elf,0 ,0 );if (linux_size ==0 )return grub_errno;
=== modified file 'include/grub/elfload.h'
grub/elfload.h +2 -2
@@ -46,12 +46,12 @@grub_err_t grub_elf_close (grub_elf_t );int grub_elf_is_elf32 (grub_elf_t ); -grub_size_t grub_elf32_size (grub_elf_t ,Elf32_Addr *); +grub_size_t grub_elf32_size (grub_elf_t ,Elf32_Addr *,grub_uint32_t *);grub_err_t grub_elf32_load (grub_elf_t ,grub_elf32_load_hook_t ,grub_addr_t *,grub_size_t *);int grub_elf_is_elf64 (grub_elf_t ); -grub_size_t grub_elf64_size (grub_elf_t ,Elf64_Addr *); +grub_size_t grub_elf64_size (grub_elf_t ,Elf64_Addr *,grub_uint64_t *);grub_err_t grub_elf64_load (grub_elf_t ,grub_elf64_load_hook_t ,grub_addr_t *,grub_size_t *); grub_err_t
Quoted 8 lines · Manoel Rebelo Abranches
Fixed some issues reported by Vladimir:
Initial alignment value 1 instead of 0.
Verify error by returning address being -1 since 0 may be valid in memory allocated directly by the Open Firmware.
Use 'end' instead of (addr + len) to search for available memory. change hardcoded value '1' toGRUB_MEMORY_AVAILABLE.
Go ahead.
Quoted 41 lines · Manoel Rebelo Abraches
Quoted 29 lines
This is another version to the same fix.
The first patch 01_mem.patch creates a new function to allocate memory using the "available" property in the "memory" node.
The second patch 02_offset.patch correctly finds the linux entry point, also assumes that more segments can exist (made accordingly with sparc implementation)The patch was made passing -Bbp to diff as suggested by Vladimir. I tested with RHEL/SUSE images and some RHEL/SUSE kernel I had and worked correctly. Before the images failed to boot. Thank you for reviewing this.
Quoted 9 lines · Manoel Rebelo Abraches
This Patch loads kernel LOAD segment at the address fixed by PhysAddr with alignment Align at the ELF program header. Even if this address is in use other address with the same alignment is found "linuxadr" (since linux can relocate itself). The linux main function is then find in the adress linuxaddr + entry + (PhysAddr- Entry), where Entry is the Entry point address in the ELF header. (PhysAddr- Entry) gives the offset where the linux entry point is located after linuxaddr.
Quoted 4 lines
_______________________________________________
Grub-devel mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/grub-develQuoted 4 lines
_______________________________________________
Grub-devel mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/grub-devel_______________________________________________
Grub-devel mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/grub-devel
Signature · 2 lines
Regards Vladimir 'φ-coder/phcoder' Serbinenko