Index: grub2-cvs/loader/powerpc/ieee1275/linux.c =================================================================== --- grub2-cvs.orig/loader/powerpc/ieee1275/linux.c 2006-10-31 17:05:16.000000000 -0600 +++ grub2-cvs/loader/powerpc/ieee1275/linux.c 2006-10-31 17:05:19.000000000 -0600 @@ -19,6 +19,7 @@ */ #include +#include #include #include #include @@ -27,10 +28,12 @@ #include #include +#define ELF32_LOADMASK (0xc0000000UL) +#define ELF64_LOADMASK (0xc000000000000000ULL) + static grub_dl_t my_mod; static int loaded; -static int vmlinux; static grub_addr_t initrd_addr; static grub_size_t initrd_size; @@ -97,124 +100,131 @@ grub_linux_unload (void) return err; } -void -grub_rescue_cmd_linux (int argc, char *argv[]) +static grub_err_t +grub_linux_load32 (grub_elf_t elf) { - grub_file_t file = 0; - Elf32_Ehdr ehdr; - Elf32_Phdr *phdrs = 0; - int i; - int offset = 0; - grub_addr_t entry; + Elf32_Addr entry; int found_addr = 0; - int size; - char *dest; - - grub_dl_ref (my_mod); - - if (argc == 0) - { - grub_error (GRUB_ERR_BAD_ARGUMENT, "no kernel specified"); - goto fail; - } - - file = grub_file_open (argv[0]); - if (! file) - goto fail; - - if (grub_file_read (file, (char *) &ehdr, sizeof (ehdr)) != sizeof (ehdr)) - { - grub_error (GRUB_ERR_READ_ERROR, "cannot read the linux elf header"); - goto fail; - } - if (grub_dl_check_header (&ehdr, sizeof(ehdr))) - { - grub_error (GRUB_ERR_UNKNOWN_OS, "No valid ELF header found"); - goto fail; - } + /* 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); + if (linux_size == 0) + return grub_errno; + /* Pad it; the kernel scribbles over memory beyond its load address. */ + linux_size += 0x100000; - if (ehdr.e_type != ET_EXEC) + /* 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_error (GRUB_ERR_UNKNOWN_OS, - "This ELF file is not of the right type\n"); - goto fail; + 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) + return grub_error (GRUB_ERR_OUT_OF_MEMORY, "Could not claim memory."); - /* Read the sections. */ - entry = ehdr.e_entry; - if (entry == 0xc0000000) - { - entry = 0x01400000; - vmlinux = 1; + /* Now load the segments into the area we claimed. */ + auto int offset_phdr (Elf32_Phdr *phdr, grub_addr_t *addr); + int offset_phdr (Elf32_Phdr *phdr, grub_addr_t *addr) + { + /* 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; + return 0; } - else - vmlinux = 0; - - phdrs = (Elf32_Phdr *) grub_malloc (ehdr.e_phnum * ehdr.e_phentsize); - grub_file_read (file, (void *) phdrs, ehdr.e_phnum * ehdr.e_phentsize); - - /* Release the previously used memory. */ - grub_loader_unset (); + return grub_elf32_load (elf, offset_phdr); +} - /* Determine the amount of memory that is required. */ - linux_size = 0; - for (i = 0; i < ehdr.e_phnum; i++) - { - Elf32_Phdr *phdr = phdrs + i; - /* XXX: Is this calculation correct? */ - linux_size += phdr->p_memsz + phdr->p_filesz; - } +static grub_err_t +grub_linux_load64 (grub_elf_t elf) +{ + Elf64_Addr entry; + int found_addr = 0; - /* Reserve memory for the kernel. */ + /* Linux's entry point incorrectly contains a virtual address. */ + entry = elf->ehdr.ehdr64.e_entry & ~ELF64_LOADMASK; + if (entry == 0) + entry = 0x01400000; + + linux_size = grub_elf64_size (elf); + if (linux_size == 0) + return grub_errno; + /* Pad it; the kernel scribbles over memory beyond its load address. */ linux_size += 0x100000; - /* For some vmlinux kernels the address set above won't work. Just - try some other addresses just like yaboot does. */ + /* 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", + 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) - { - grub_error (GRUB_ERR_OUT_OF_MEMORY, "Can not claim memory"); - goto fail; + return grub_error (GRUB_ERR_OUT_OF_MEMORY, "Could not claim memory."); + + /* Now load the segments into the area we claimed. */ + auto int offset_phdr (Elf64_Phdr *phdr, grub_addr_t *addr); + int offset_phdr (Elf64_Phdr *phdr, grub_addr_t *addr) + { + /* 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; + return 0; } - entry = linux_addr; + return grub_elf64_load (elf, offset_phdr); +} - /* Load every loadable segment in memory. */ - for (i = 0; i < ehdr.e_phnum; i++) - { - Elf32_Phdr *phdr = phdrs + i; +void +grub_rescue_cmd_linux (int argc, char *argv[]) +{ + grub_elf_t elf = 0; + int i; + int size; + char *dest; - if (phdr->p_type == PT_LOAD) - { - void *segment_addr = ((char *) entry) + offset; + grub_dl_ref (my_mod); - if (grub_file_seek (file, phdr->p_offset) == (grub_off_t) -1) - { - grub_error (GRUB_ERR_BAD_OS, "Invalid offset in program header"); - goto fail; - } + if (argc == 0) + { + grub_error (GRUB_ERR_BAD_ARGUMENT, "no kernel specified"); + goto out; + } - grub_dprintf ("loader", "Loading segment %d at %p, size 0x%x\n", i, - segment_addr, phdr->p_filesz); + elf = grub_elf_open (argv[0]); + if (! elf) + goto out; - if (grub_file_read (file, segment_addr, phdr->p_filesz) - != (grub_ssize_t) phdr->p_filesz) - goto fail; + if (elf->ehdr.ehdr32.e_type != ET_EXEC) + { + grub_error (GRUB_ERR_UNKNOWN_OS, + "This ELF file is not of the right type\n"); + goto out; + } - if (phdr->p_filesz < phdr->p_memsz) - grub_memset ((char *) (((char *) entry) + offset) + phdr->p_filesz, 0, - phdr->p_memsz - phdr->p_filesz); + /* Release the previously used memory. */ + grub_loader_unset (); - offset += phdr->p_filesz; - } + if (grub_elf_is_elf32 (elf)) + grub_linux_load32 (elf); + else + if (grub_elf_is_elf64 (elf)) + grub_linux_load64 (elf); + else + { + grub_error (GRUB_ERR_BAD_FILE_TYPE, "Unknown ELF class"); + goto out; } size = sizeof ("BOOT_IMAGE=") + grub_strlen (argv[0]); @@ -223,7 +233,7 @@ grub_rescue_cmd_linux (int argc, char *a linux_args = grub_malloc (size); if (! linux_args) - goto fail; + goto out; /* Specify the boot file. */ dest = grub_stpcpy (linux_args, "BOOT_IMAGE="); @@ -235,12 +245,10 @@ grub_rescue_cmd_linux (int argc, char *a dest = grub_stpcpy (dest, argv[i]); } - fail: +out: - if (file) - grub_file_close (file); - - grub_free (phdrs); + if (elf) + grub_elf_close (elf); if (grub_errno != GRUB_ERR_NONE) { @@ -254,8 +262,6 @@ grub_rescue_cmd_linux (int argc, char *a initrd_addr = 0; loaded = 1; } - - return; } void