Received: from mailman by lists.gnu.org with archive (Exim 4.43) id 1P8CWI-0003wp-V2 for mharc-grub-devel@gnu.org; Tue, 19 Oct 2010 09:46:27 -0400 Received: from [140.186.70.92] (port=50642 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1P8CWE-0003sf-JJ for grub-devel@gnu.org; Tue, 19 Oct 2010 09:46:24 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1P8CWC-0000S6-Fl for grub-devel@gnu.org; Tue, 19 Oct 2010 09:46:22 -0400 Received: from e24smtp03.br.ibm.com ([32.104.18.24]:48769) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1P8CWB-0000RA-Rf for grub-devel@gnu.org; Tue, 19 Oct 2010 09:46:20 -0400 Received: from d24relay01.br.ibm.com (d24relay01.br.ibm.com [9.8.31.16]) by e24smtp03.br.ibm.com (8.14.4/8.13.1) with ESMTP id o9JDdloj020126 for ; Tue, 19 Oct 2010 11:39:47 -0200 Received: from d24av01.br.ibm.com (d24av01.br.ibm.com [9.8.31.91]) by d24relay01.br.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id o9JDjKp72756720 for ; Tue, 19 Oct 2010 10:45:20 -0300 Received: from d24av01.br.ibm.com (loopback [127.0.0.1]) by d24av01.br.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id o9JDk329010143 for ; Tue, 19 Oct 2010 11:46:03 -0200 Received: from [9.8.15.55] ([9.8.15.55]) by d24av01.br.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id o9JDk3m6010115 for ; Tue, 19 Oct 2010 11:46:03 -0200 From: Manoel Rebelo Abraches To: The development of GNU GRUB In-Reply-To: <1285572572.6491.9.camel@abranches-laptop> References: <1285572572.6491.9.camel@abranches-laptop> Content-Type: multipart/mixed; boundary="=-qz4vgldtsK5PRnshdN+s" Date: Tue, 19 Oct 2010 11:46:12 -0200 Message-ID: <1287495972.8425.10.camel@manoel-desktop> Mime-Version: 1.0 X-Mailer: Evolution 2.30.3 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) Subject: Re: [PATCH] fix load of RHEL ans SUSE images by correctly calculate ELF entry point X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: The development of GNU GRUB List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 19 Oct 2010 13:46:24 -0000 --=-qz4vgldtsK5PRnshdN+s Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit 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. On Mon, 2010-09-27 at 04:29 -0300, Manoel Rebelo Abraches wrote: > 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 > Grub-devel@gnu.org > http://lists.gnu.org/mailman/listinfo/grub-devel -- Best Regards, Manoel R. Abranches IBM Linux Technology Center Brazil --=-qz4vgldtsK5PRnshdN+s Content-Disposition: attachment; filename="02_offset.patch" Content-Type: text/x-patch; name="02_offset.patch"; charset="UTF-8" Content-Transfer-Encoding: 7bit === modified file 'grub-core/kern/elf.c' --- grub-core/kern/elf.c 2010-09-05 11:05:36 +0000 +++ grub-core/kern/elf.c 2010-10-19 07:07:00 +0000 @@ -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_A if (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_A 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' --- grub-core/loader/mips/linux.c 2010-09-18 22:04:31 +0000 +++ grub-core/loader/mips/linux.c 2010-10-19 07:07:00 +0000 @@ -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' --- grub-core/loader/powerpc/ieee1275/linux.c 2010-10-18 02:47:23 +0000 +++ grub-core/loader/powerpc/ieee1275/linux.c 2010-10-19 07:07:00 +0000 @@ -147,25 +147,29 @@ grub_linux_unload (void) static grub_err_t grub_linux_load32 (grub_elf_t elf) { - Elf32_Addr entry; + Elf32_Addr base_addr; + grub_addr_t seg_addr; + grub_uint32_t align; + int offset; - /* Linux's entry point incorrectly contains a virtual address. */ - entry = elf->ehdr.ehdr32.e_entry & ~ELF32_LOADMASK; - if (entry == 0) - entry = 0x01400000; - - linux_size = grub_elf32_size (elf, 0); + linux_size = grub_elf32_size (elf, &base_addr, &align); if (linux_size == 0) return grub_errno; /* Pad it; the kernel scribbles over memory beyond its load address. */ linux_size += 0x100000; + offset = elf->ehdr.ehdr32.e_entry - base_addr; + /* Linux's incorrectly contains a virtual address. */ + base_addr &= ~ELF32_LOADMASK; + /* 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. */ - if (!(linux_addr = grub_linux_claimmap_iterate (entry, linux_size, 0x100000))) + if (!(seg_addr = grub_linux_claimmap_iterate (base_addr & ~ELF32_LOADMASK, linux_size, align))) return grub_error (GRUB_ERR_OUT_OF_MEMORY, "couldn't claim memory"); + linux_addr = seg_addr + offset; + /* Now load the segments into the area we claimed. */ auto grub_err_t offset_phdr (Elf32_Phdr *phdr, grub_addr_t *addr, int *do_load); grub_err_t offset_phdr (Elf32_Phdr *phdr, grub_addr_t *addr, int *do_load) @@ -177,9 +181,7 @@ grub_linux_load32 (grub_elf_t elf) } *do_load = 1; - /* Linux's program headers incorrectly contain virtual addresses. - * Translate those to physical, and offset to the area we claimed. */ - *addr = (phdr->p_paddr & ~ELF32_LOADMASK) + linux_addr; + *addr = (phdr->p_paddr - base_addr) + seg_addr; return 0; } return grub_elf32_load (elf, offset_phdr, 0, 0); @@ -188,25 +190,29 @@ grub_linux_load32 (grub_elf_t elf) static grub_err_t grub_linux_load64 (grub_elf_t elf) { - Elf64_Addr entry; - - /* Linux's entry point incorrectly contains a virtual address. */ - entry = elf->ehdr.ehdr64.e_entry & ~ELF64_LOADMASK; - if (entry == 0) - entry = 0x01400000; + Elf64_Addr base_addr; + grub_addr_t seg_addr; + grub_uint64_t align; + int offset; - linux_size = grub_elf64_size (elf, 0); + linux_size = grub_elf64_size (elf, &base_addr, &align); if (linux_size == 0) return grub_errno; /* Pad it; the kernel scribbles over memory beyond its load address. */ linux_size += 0x100000; + offset = elf->ehdr.ehdr64.e_entry - base_addr; + /* Linux's incorrectly contains a virtual address. */ + base_addr &= ~ELF64_LOADMASK; + /* 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. */ - if (!(linux_addr = grub_linux_claimmap_iterate (entry, linux_size, 0x100000))) + if (!(seg_addr = grub_linux_claimmap_iterate (base_addr & ~ELF64_LOADMASK, linux_size, align))) return grub_error (GRUB_ERR_OUT_OF_MEMORY, "couldn't claim memory"); + linux_addr = seg_addr + offset; + /* Now load the segments into the area we claimed. */ auto grub_err_t offset_phdr (Elf64_Phdr *phdr, grub_addr_t *addr, int *do_load); grub_err_t offset_phdr (Elf64_Phdr *phdr, grub_addr_t *addr, int *do_load) @@ -217,9 +223,8 @@ grub_linux_load64 (grub_elf_t elf) return 0; } *do_load = 1; - /* Linux's program headers incorrectly contain virtual addresses. - * Translate those to physical, and offset to the area we claimed. */ - *addr = (phdr->p_paddr & ~ELF64_LOADMASK) + linux_addr; + + *addr = (phdr->p_paddr - base_addr) + seg_addr; return 0; } return grub_elf64_load (elf, offset_phdr, 0, 0); === modified file 'grub-core/loader/sparc64/ieee1275/linux.c' --- grub-core/loader/sparc64/ieee1275/linux.c 2010-09-18 22:04:31 +0000 +++ grub-core/loader/sparc64/ieee1275/linux.c 2010-10-19 07:07:00 +0000 @@ -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' --- include/grub/elfload.h 2010-01-20 10:48:36 +0000 +++ include/grub/elfload.h 2010-10-19 07:07:00 +0000 @@ -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 --=-qz4vgldtsK5PRnshdN+s Content-Disposition: attachment; filename="01_mem.patch" Content-Type: text/x-patch; name="01_mem.patch"; charset="UTF-8" Content-Transfer-Encoding: 7bit === modified file 'grub-core/loader/powerpc/ieee1275/linux.c' --- grub-core/loader/powerpc/ieee1275/linux.c 2010-09-05 11:05:36 +0000 +++ grub-core/loader/powerpc/ieee1275/linux.c 2010-10-18 02:47:23 +0000 @@ -26,6 +26,7 @@ #include #include #include +#include #define ELF32_LOADMASK (0xc0000000UL) #define ELF64_LOADMASK (0xc000000000000000ULL) @@ -45,6 +46,50 @@ static char *linux_args; 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 = 0; + 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 addr_ant; + addr_ant = addr; + addr = ALIGN_UP (addr, align); + target = ALIGN_UP (target, align); + len -= addr - addr_ant; + + /*target above the memory chunk */ + if (type != 1 || target > addr + len) + return 0; + + /*target inside the memory chunk */ + if (target >= addr && target < addr + len && size <= addr + len - target) + { + if (grub_claimmap (target, size) == GRUB_ERR_NONE) + { + found = target; + return 1; + } + } + /*target below the memory chunk */ + if (target < addr && size <= len) + { + if (grub_claimmap (addr, size) == GRUB_ERR_NONE) + { + found = addr; + return 1; + } + } + return 0; + } + grub_machine_mmap_iterate (alloc_mem); + return found; +} + static grub_err_t grub_linux_boot (void) { @@ -103,7 +148,6 @@ static grub_err_t 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 +163,7 @@ grub_linux_load32 (grub_elf_t elf) /* 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) + if (!(linux_addr = grub_linux_claimmap_iterate (entry, linux_size, 0x100000))) return grub_error (GRUB_ERR_OUT_OF_MEMORY, "couldn't claim memory"); /* Now load the segments into the area we claimed. */ @@ -153,7 +189,6 @@ static grub_err_t 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 +204,7 @@ grub_linux_load64 (grub_elf_t elf) /* 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) + if (!(linux_addr = grub_linux_claimmap_iterate (entry, linux_size, 0x100000))) return grub_error (GRUB_ERR_OUT_OF_MEMORY, "couldn't claim memory"); /* Now load the segments into the area we claimed. */ @@ -287,7 +314,6 @@ grub_cmd_initrd (grub_command_t cmd __at grub_ssize_t size; grub_addr_t first_addr; grub_addr_t addr; - int found_addr = 0; if (argc == 0) { @@ -311,16 +337,7 @@ grub_cmd_initrd (grub_command_t cmd __at /* 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) + if (!(addr = grub_linux_claimmap_iterate (first_addr, size, 0x100000))) { grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot claim memory"); goto fail; --=-qz4vgldtsK5PRnshdN+s Content-Type: text/x-patch; name="changelog.patch"; charset="UTF-8" Content-Description: Content-Disposition: inline; filename="changelog.patch" Content-Transfer-Encoding: quoted-printable X-MIME-Autoconverted: from 8bit to quoted-printable by e24smtp03.br.ibm.com id o9JDdloj020126 --- ChangeLog 2010-10-19 11:38:51.000000000 -0200 +++ ChangeLog 2010-10-19 11:16:12.000000000 -0200 @@ -1,3 +1,19 @@ +2010-10-18 Manoel Rebelo Abranches +=09 + Find the correct linux offset in PowerPC loader. + + * include/grub/elfload.h (grub_elf32_size): New parameter. All users up= dated. + 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"proper= ty=20 + in the "memory" node for memory allocation for kernel in the PowerPC lo= ader. + (grub_linux_load32): Correctly find linux entry point offset. + (grub_linux_load64): Likewise. + 2010-10-18 Gr=C3=A9goire Sutre =20 Make mktemp invocations portable. --=-qz4vgldtsK5PRnshdN+s--