Update the Linux boot protocol
PATCH v2 1/3 Update the Linux boot protocol
The Linux boot header includes information on the kernel's desired load address and alignment. Add support for that.
--- ChangeLog | 6 ++++++ include/grub/i386/linux.h | 28 +++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 5 deletions(-)
ChangeLog +6 -0
@@ -1,3 +1,9 @@ +2012-02-06 Matthew Garrett <[email protected]> + + * include/grub/i386/linux.h (linux_kernel_header): Update to + boot protocol 2.10. + (linux_kernel_params): Likewise + 2012-02-05 Vladimir Serbinenko <[email protected]> * grub-core/commands/minicmd.c (GRUB_MOD_INIT): Add missing SIZE
include/grub/i386/linux.h +23 -5
@@ -86,7 +86,7 @@ enumGRUB_VIDEO_LINUX_TYPE_SIMPLE =0x70 /* Linear framebuffer without any additional functions. */ }; -/* For the Linux/i386 boot protocol version 2.03. */ +/* For the Linux/i386 boot protocol version 2.10. */ struct linux_kernel_header {grub_uint8_t code1[0x0020 ];
@@ -131,8 +131,16 @@ struct linux_kernel_headergrub_uint32_t initrd_addr_max;/* Highest address for initrd */ grub_uint32_t kernel_alignment;grub_uint8_t relocatable; -grub_uint8_t pad[3 ]; +grub_uint8_t min_alignment; +grub_uint8_t pad[2 ];grub_uint32_t cmdline_size; +grub_uint32_t hardware_subarch; +grub_uint64_t hardware_subarch_data; +grub_uint32_t payload_offset; +grub_uint32_t payload_length; +grub_uint64_t setup_data; +grub_uint64_t pref_address; +grub_uint64_t init_size; } __attribute__ ((packed));/* Boot parameters for Linux based on 2.6.12. This is used by the setup
@@ -276,10 +284,20 @@ struct linux_kernel_paramsgrub_uint32_t ramdisk_size;/* initrd size */ grub_uint32_t bootsect_kludge;/* obsolete */ grub_uint16_t heap_end_ptr;/* Free memory after setup end */ -grub_uint16_t pad1;/* Unused */ +grub_uint8_t ext_loader_ver;/* Extended loader version */ +grub_uint8_t ext_loader_type;/* Extended loader type */ grub_uint32_t cmd_line_ptr;/* Points to the kernel command line */ - -grub_uint8_t pad2[164 ];/* 22c */ +grub_uint32_t initrd_addr_max;/* Maximum initrd address */ +grub_uint32_t kernel_alignment;/* Alignment of the kernel */ +grub_uint8_t relocatable_kernel;/* Is the kernel relocatable */ +grub_uint8_t pad1[3 ]; +grub_uint32_t cmdline_size;/* Size of the kernel command line */ +grub_uint32_t hardware_subarch; +grub_uint64_t hardware_subarch_data; +grub_uint32_t payload_offset; +grub_uint32_t payload_length; +grub_uint64_t setup_data; +grub_uint8_t pad2[120 ];/* 258 */ struct grub_e820_mmap e820_map[GRUB_E820_MAX_ENTRY ];/* 2d0 */ } __attribute__ ((packed));
Signature · 1 line
1.7.7.6
PATCH v2 2/3 Add support for avoiding firmware in relocations
EFI and OF both support firmware regions which may be in use during loading. Add support for avoiding these.
---
Updated to allow us to relocate on top of loader data - this should be safe.
ChangeLog | 10 ++++++ grub-core/lib/efi/relocator.c | 51 +++++++++++++++++++++++++++++++++ grub-core/lib/ieee1275/relocator.c | 27 +++++++++++++++++ grub-core/lib/relocator.c | 12 +++++++- grub-core/loader/i386/bsd.c | 19 +++++++----- grub-core/loader/i386/bsdXX.c | 8 ++-- grub-core/loader/i386/linux.c | 4 +- grub-core/loader/i386/multiboot_mbi.c | 2 +- grub-core/loader/i386/pc/freedos.c | 2 +- grub-core/loader/i386/pc/linux.c | 4 +- grub-core/loader/i386/pc/ntldr.c | 4 +- grub-core/loader/mips/linux.c | 6 ++-- grub-core/loader/multiboot_elfxx.c | 2 +- grub-core/loader/multiboot_mbi2.c | 2 +- grub-core/loader/xnu.c | 2 +- grub-core/loader/xnu_resume.c | 2 +- include/grub/relocator.h | 3 +- include/grub/relocator_private.h | 2 + 18 files changed, 133 insertions(+), 29 deletions(-)
ChangeLog +10 -0
@@ -1,5 +1,15 @@ 2012-02-06 Matthew Garrett <[email protected]> + * grub-core/lib/efi/relocator.c (grub_relocator_alloc_chunk_addr): + Add argument to fail allocation when target address overlaps + firmware regions. All users updated. + * grub-core/lib/efi/relocator.c (grub_relocator_firmware_overlaps): + New function + * grub-core/lib/ieee1275/relocator.c (grub_relocator_firmware_overlaps): + Likewise + +2012-02-06 Matthew Garrett <[email protected]> + * include/grub/i386/linux.h (linux_kernel_header): Update to boot protocol 2.10. (linux_kernel_params): Likewise
grub-core/lib/efi/relocator.c +51 -0
57 lines not on this page · raw message
grub-core/lib/ieee1275/relocator.c +27 -0
@@ -42,6 +42,33 @@ grub_relocator_firmware_get_max_events (void)return 2 * counter; } +unsigned +grub_relocator_firmware_overlaps (grub_phys_addr_t target,grub_size_t size) +{ +int overlap =0 ; + auto int NESTED_FUNC_ATTR check (grub_uint64_t addr, grub_uint64_t len, +grub_memory_type_t type); + int NESTED_FUNC_ATTR check (grub_uint64_t addr, grub_uint64_t len, +grub_memory_type_t type) + { +grub_uint64_t end = addr + len; + +if (type ==GRUB_MEMORY_AVAILABLE ) +return 0 ; + +if (target >= addr && target <= end) + overlap =1 ; + +if (target + size >= addr && target + size <= end) + overlap =1 ; + +return 0 ; + } + +grub_machine_mmap_iterate (check); +return overlap; +} +unsigned grub_relocator_firmware_fill_events (struct grub_relocator_mmap_event *events) {
grub-core/lib/relocator.c +11 -1
@@ -1203,7 +1203,8 @@ adjust_limits (struct grub_relocator *rel,grub_err_t grub_relocator_alloc_chunk_addr (struct grub_relocator *rel,grub_relocator_chunk_t *out, -grub_phys_addr_t target,grub_size_t size) +grub_phys_addr_t target,grub_size_t size, +int avoid_firmware) {struct grub_relocator_chunk *chunk;grub_phys_addr_t min_addr =0 , max_addr;
@@ -1213,6 +1214,15 @@ grub_relocator_alloc_chunk_addr (struct grub_relocator *rel,adjust_limits (rel, &min_addr, &max_addr, target, target); +if (avoid_firmware) + { +#if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS +if (grub_relocator_firmware_overlaps (target, size)) + +return grub_error (GRUB_ERR_BAD_ARGUMENT ,"target overlaps with firmware" ); +#endif + } +for (chunk = rel->chunks; chunk; chunk = chunk->next)if ((chunk->target <= target && target < chunk->target + chunk->size) || (target <= chunk->target && chunk->target < target + size))
grub-core/loader/i386/bsd.c +11 -8
@@ -606,7 +606,7 @@ grub_freebsd_boot (void) {grub_relocator_chunk_t ch; err =grub_relocator_alloc_chunk_addr (relocator, &ch, - kern_end, p_size); + kern_end, p_size,0 );if (err)return err; p =get_virtual_current_address (ch);
@@ -788,7 +788,7 @@ grub_openbsd_boot (void) err =grub_relocator_alloc_chunk_addr (relocator, &ch, buf_target, tag_buf_len +sizeof (struct grub_openbsd_bootargs ) - +9 *sizeof (grub_uint32_t)); + +9 *sizeof (grub_uint32_t),0 );if (err)return err; buf0 =get_virtual_current_address (ch);
@@ -1079,7 +1079,8 @@ grub_netbsd_boot (void) err =grub_relocator_alloc_chunk_addr (relocator, &ch, arg_target, tag_buf_len +sizeof (struct grub_netbsd_bootinfo ) - + tag_count *sizeof (grub_uint32_t)); + + tag_count *sizeof (grub_uint32_t), +0 );if (err)return err; curarg =get_virtual_current_address (ch);
@@ -1219,7 +1220,8 @@ grub_bsd_load_aout (grub_file_t file, const char *filename)grub_relocator_chunk_t ch; err =grub_relocator_alloc_chunk_addr (relocator, &ch, - kern_start, kern_end - kern_start); + kern_start, kern_end - kern_start, +0 );if (err)return err; kern_chunk_src =get_virtual_current_address (ch);
@@ -1330,7 +1332,8 @@ grub_bsd_load_elf (grub_elf_t elf)if (err)return err; err =grub_relocator_alloc_chunk_addr (relocator, &ch, - kern_start, kern_end - kern_start); + kern_start, kern_end - kern_start, +0 );if (err)return err;
@@ -1373,7 +1376,7 @@ grub_bsd_load_elf (grub_elf_t elf)grub_relocator_chunk_t ch; err =grub_relocator_alloc_chunk_addr (relocator, &ch, kern_start, - kern_end - kern_start); + kern_end - kern_start,0 );if (err)return err; kern_chunk_src =get_virtual_current_address (ch);
@@ -1837,7 +1840,7 @@ grub_cmd_freebsd_module (grub_command_t cmd __attribute__ ((unused)), {grub_relocator_chunk_t ch; err =grub_relocator_alloc_chunk_addr (relocator, &ch, kern_end, - file->size); + file->size,0 );if (err) goto fail; src =get_virtual_current_address (ch);
@@ -1888,7 +1891,7 @@ grub_netbsd_module_load (char *filename, grub_uint32_t type) {grub_relocator_chunk_t ch; err =grub_relocator_alloc_chunk_addr (relocator, &ch, kern_end, - file->size); + file->size,0 );if (err) goto fail;
grub-core/loader/i386/bsdXX.c +4 -4
@@ -106,7 +106,7 @@ SUFFIX (grub_freebsd_load_elfmodule_obj) (struct grub_relocator *relocator, {grub_relocator_chunk_t ch; err =grub_relocator_alloc_chunk_addr (relocator, &ch, - module, chunk_size); + module, chunk_size,0 );if (err)return err; chunk_src =get_virtual_current_address (ch);
@@ -204,7 +204,7 @@ SUFFIX (grub_freebsd_load_elfmodule) (struct grub_relocator *relocator,grub_relocator_chunk_t ch; err =grub_relocator_alloc_chunk_addr (relocator, &ch, - module, chunk_size); + module, chunk_size,0 );if (err)return err;
@@ -319,7 +319,7 @@ SUFFIX (grub_freebsd_load_elf_meta) (struct grub_relocator *relocator, {grub_relocator_chunk_t ch; err =grub_relocator_alloc_chunk_addr (relocator, &ch, - symtarget, chunk_size); + symtarget, chunk_size,0 );if (err)return err; sym_chunk =get_virtual_current_address (ch);
@@ -434,7 +434,7 @@ SUFFIX (grub_netbsd_load_elf_meta) (struct grub_relocator *relocator, {grub_relocator_chunk_t ch; err =grub_relocator_alloc_chunk_addr (relocator, &ch, - symtarget, chunk_size); + symtarget, chunk_size,0 );if (err)return err; sym_chunk =get_virtual_current_address (ch);
grub-core/loader/i386/linux.c +2 -2
@@ -266,7 +266,7 @@ allocate_pages (grub_size_t prot_size) err =grub_relocator_alloc_chunk_addr (relocator, &ch, real_mode_target, (real_size + mmap_size - + efi_mmap_size)); + + efi_mmap_size),0 );if (err) goto fail; real_mode_mem =get_virtual_current_address (ch);
@@ -278,7 +278,7 @@ allocate_pages (grub_size_t prot_size) {grub_relocator_chunk_t ch; err =grub_relocator_alloc_chunk_addr (relocator, &ch, - prot_mode_target, prot_size); + prot_mode_target, prot_size,0 );if (err) goto fail; prot_mode_mem =get_virtual_current_address (ch);
grub-core/loader/i386/multiboot_mbi.c +1 -1
@@ -119,7 +119,7 @@ grub_multiboot_load (grub_file_t file) err =grub_relocator_alloc_chunk_addr (grub_multiboot_relocator, &ch, header->load_addr, - code_size); + code_size,0 );if (err) {grub_dprintf ("multiboot_loader" ,"Error loading aout kludge\n" );
grub-core/loader/i386/pc/freedos.c +1 -1
@@ -101,7 +101,7 @@ grub_cmd_freedos (grub_command_t cmd __attribute__ ((unused)), {grub_relocator_chunk_t ch; err =grub_relocator_alloc_chunk_addr (rel, &ch,GRUB_FREEDOS_SEGMENT <<4 , - kernelsyssize); + kernelsyssize,0 );if (err) goto fail; kernelsys =get_virtual_current_address (ch);
grub-core/loader/i386/pc/linux.c +2 -2
@@ -273,7 +273,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), err =grub_relocator_alloc_chunk_addr (relocator, &ch, grub_linux_real_target,GRUB_LINUX_CL_OFFSET - + maximal_cmdline_size); + + maximal_cmdline_size,0 );if (err)return err; grub_linux_real_chunk =get_virtual_current_address (ch);
@@ -317,7 +317,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),grub_relocator_chunk_t ch; err =grub_relocator_alloc_chunk_addr (relocator, &ch, grub_linux_prot_target, - grub_linux16_prot_size); + grub_linux16_prot_size,0 );if (err)return err; grub_linux_prot_chunk =get_virtual_current_address (ch);
grub-core/loader/i386/pc/ntldr.c +2 -2
@@ -97,7 +97,7 @@ grub_cmd_ntldr (grub_command_t cmd __attribute__ ((unused)), {grub_relocator_chunk_t ch; err =grub_relocator_alloc_chunk_addr (rel, &ch,0x7C00 , -GRUB_DISK_SECTOR_SIZE ); +GRUB_DISK_SECTOR_SIZE ,0 );if (err) goto fail; bs =get_virtual_current_address (ch);
@@ -124,7 +124,7 @@ grub_cmd_ntldr (grub_command_t cmd __attribute__ ((unused)), {grub_relocator_chunk_t ch; err =grub_relocator_alloc_chunk_addr (rel, &ch,GRUB_NTLDR_SEGMENT <<4 , - ntldrsize); + ntldrsize,0 );if (err) goto fail; ntldr =get_virtual_current_address (ch);
grub-core/loader/mips/linux.c +3 -3
@@ -87,7 +87,7 @@ grub_linux_boot (void) err =grub_relocator_alloc_chunk_addr (relocator, &ch, ((16 <<20 ) -264 ), -grub_strlen (params) +1 +8 ); +grub_strlen (params) +1 +8 ,0 );if (err)return err; memsize =get_virtual_current_address (ch);
@@ -159,7 +159,7 @@ grub_linux_load32 (grub_elf_t elf, void **extra_mem, grub_size_t extra_size)grub_relocator_chunk_t ch; err =grub_relocator_alloc_chunk_addr (relocator, &ch, target_addr &0x1fffffff , - linux_size); + linux_size,0 );if (err)return err; playground =get_virtual_current_address (ch);
@@ -214,7 +214,7 @@ grub_linux_load64 (grub_elf_t elf, void **extra_mem, grub_size_t extra_size)grub_relocator_chunk_t ch; err =grub_relocator_alloc_chunk_addr (relocator, &ch, target_addr &0x1fffffff , - linux_size); + linux_size,0 );if (err)return err; playground =get_virtual_current_address (ch);
grub-core/loader/multiboot_elfxx.c +1 -1
@@ -102,7 +102,7 @@ CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, void *buffer)grub_relocator_chunk_t ch; err =grub_relocator_alloc_chunk_addr (grub_multiboot_relocator, &ch,phdr (i)->p_paddr, -phdr (i)->p_memsz); +phdr (i)->p_memsz,0 );if (err) {grub_dprintf ("multiboot_loader" ,"Error loading phdr %d\n" , i);
grub-core/loader/multiboot_mbi2.c +1 -1
@@ -227,7 +227,7 @@ grub_multiboot_load (grub_file_t file) err =grub_relocator_alloc_chunk_addr (grub_multiboot_relocator, &ch, addr_tag->load_addr, - code_size); + code_size,0 );if (err) {grub_dprintf ("multiboot_loader" ,"Error loading aout kludge\n" );
grub-core/loader/xnu.c +1 -1
@@ -60,7 +60,7 @@ grub_xnu_heap_malloc (int size, void **src, grub_addr_t *target) err =grub_relocator_alloc_chunk_addr (grub_xnu_relocator, &ch, grub_xnu_heap_target_start - + grub_xnu_heap_size, size); + + grub_xnu_heap_size, size,0 );if (err)return err;
grub-core/loader/xnu_resume.c +1 -1
@@ -110,7 +110,7 @@ grub_xnu_resume (char *imagename) {grub_relocator_chunk_t ch; err =grub_relocator_alloc_chunk_addr (grub_xnu_relocator, &ch, codedest, - codesize +GRUB_XNU_PAGESIZE ); + codesize +GRUB_XNU_PAGESIZE ,0 );if (err) {grub_file_close (file);
include/grub/relocator.h +2 -1
@@ -33,7 +33,8 @@ struct grub_relocator *grub_relocator_new (void);grub_err_t grub_relocator_alloc_chunk_addr (struct grub_relocator *rel,grub_relocator_chunk_t *out, -grub_phys_addr_t target,grub_size_t size); +grub_phys_addr_t target,grub_size_t size, +int avoid_firmware);void *get_virtual_current_address (grub_relocator_chunk_t in);
include/grub/relocator_private.h +2 -0
@@ -104,6 +104,8 @@ struct grub_relocator_mmap_event#if GRUB_RELOCATOR_HAVE_FIRMWARE_REQUESTS int grub_relocator_firmware_alloc_region (grub_phys_addr_t start,grub_size_t size); +unsigned grub_relocator_firmware_overlaps (grub_phys_addr_t start, +grub_size_t size);unsigned grub_relocator_firmware_fill_events (struct grub_relocator_mmap_event *events);unsigned grub_relocator_firmware_get_max_events (void ); void grub_relocator_firmware_free_region (grub_phys_addr_t start,
Signature · 1 line
1.7.7.6
PATCH v2 3/3 Update Linux loader to follow the kernel's preferences
In reply to Matthew GarrettWe should attempt to load the kernel at its preferred address, and if we can't do that then we should at lesat align it correctly. When doing so we should also make sure to avoid putting the kernel on top of any regions being used by the firmware.
---
I managed to screw up the rebase, so this is a fixed version. Also, the code now follows the old path if the kernel isn't relocatable - we can't move it, so better to try it and just hope that everything works out.
ChangeLog | 8 +++++ grub-core/loader/i386/linux.c | 67 ++++++++++++++++++++++++++++++++++------ 2 files changed, 65 insertions(+), 10 deletions(-)
ChangeLog +8 -0
@@ -1,5 +1,13 @@ 2012-02-06 Matthew Garrett <[email protected]> + * grub-core/loader/i386/linux.c (allocate_pages): Attempt to obtain + appropriately aligned memory if the desired target is unavailable + (grub_cmd_linux): Update to match newer Linux boot protocols, and + attempt to load the kernel at its preferred address rather than + hardcoding. + +2012-02-06 Matthew Garrett <[email protected]> + * grub-core/lib/efi/relocator.c (grub_relocator_alloc_chunk_addr): Add argument to fail allocation when target address overlaps firmware regions. All users updated.
grub-core/loader/i386/linux.c +57 -10
125 lines not on this page · raw message
Signature · 1 line
1.7.7.6
Re: [PATCH V2 3/3] Update Linux loader to follow the kernel's preferences
Quoted 172 lines · Matthew Garrett
We should attempt to load the kernel at its preferred address, and if we can't do that then we should at lesat align it correctly. When doing so we should also make sure to avoid putting the kernel on top of any regions being used by the firmware.
---I managed to screw up the rebase, so this is a fixed version. Also, the code now follows the old path if the kernel isn't relocatable - we can't move it, so better to try it and just hope that everything works out.
ChangeLog | 8 +++++
grub-core/loader/i386/linux.c| 67 ++++++++++++++++++++++++++++++++++------2 files changed, 65 insertions(+), 10 deletions(-)ChangeLog +0
-0@@ -1,5 +1,13 @@2012-02-06 Matthew Garrett <[email protected]>
+ *
grub-core/loader/i386/linux.c(allocate_pages): Attempt to obtain
+ appropriately aligned memory if the desired target is unavailable
+ (grub_cmd_linux): Update to match newer Linux boot protocols, and
+ attempt to load the kernel at its preferred address rather than
+ hardcoding.ChangeLog +3
-0+ +2012-02-06 Matthew Garrett <[email protected]> +*
grub-core/lib/efi/relocator.c(grub_relocator_alloc_chunk_addr):
Add argument to fail allocation when target address overlaps firmware regions. All users updated.grub-core/loader/i386/linux.c +1
-1@@ -183,13 +183,14 @@ free_pages (void) grub_relocator_unload (relocator); relocator = NULL; real_mode_mem = prot_mode_mem = initrd_mem = 0; - real_mode_target = prot_mode_target = initrd_mem_target = 0; + real_mode_target = initrd_mem_target = 0; } /* Allocate pages for the real mode code and the protected mode codefor linux as well as a memory map buffer. */
staticgrub_err_tgrub-core/loader/i386/linux.c +2
-1-allocate_pages (grub_size_t prot_size) +allocate_pages (grub_size_t prot_size, grub_size_t *align, + grub_size_t min_align, int relocatable){
grub_size_treal_size,mmap_size;
grub_err_terr;grub-core/loader/i386/linux.c +53
-7@@ -269,18 +270,38 @@ allocate_pages (grub_size_t prot_size) + efi_mmap_size), 0); if (err) goto fail; + + grub_errno = GRUB_ERR_NONE; real_mode_mem = get_virtual_current_address (ch); } efi_mmap_buf = (grub_uint8_t *) real_mode_mem + real_size + mmap_size; - prot_mode_target = GRUB_LINUX_BZIMAGE_ADDR; - { grub_relocator_chunk_t ch; err = grub_relocator_alloc_chunk_addr (relocator, &ch, - prot_mode_target, prot_size, 0); + prot_mode_target, prot_size, + relocatable); + if (err) + { + unsigned int i; + for (i = *align; i >= min_align; i--) + { + err = grub_relocator_alloc_chunk_align (relocator, &ch, + 0x1000000, 0xffffffff, + prot_size, 1 << i, + GRUB_RELOCATOR_PREFERENCE_LOW); + if (!err) + { + *align = i; + prot_mode_target = get_physical_target_address (ch); + break; + } + } + } + if (err) goto fail; + prot_mode_mem = get_virtual_current_address (ch); }@@ -631,8 +652,9 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), struct linux_kernel_header lh; struct linux_kernel_params *params; grub_uint8_t setup_sects; - grub_size_t real_size, prot_size; + grub_size_t real_size, prot_size, prot_file_size, align = 0, min_align = 0; grub_ssize_t len; + int relocatable = 0; int i; grub_dl_ref (my_mod);@@ -705,9 +727,32 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), setup_sects = GRUB_LINUX_DEFAULT_SETUP_SECTS; real_size = setup_sects << GRUB_DISK_SECTOR_BITS; - prot_size = grub_file_size (file) - real_size - GRUB_DISK_SECTOR_SIZE; - if (allocate_pages (prot_size)) + if (grub_le_to_cpu16 (lh.version) >= 0x205) + { + for (align = 0; align < 32; align++) + { + if (grub_le_to_cpu32 (lh.kernel_alignment) & (1 << align)) + break; + } + relocatable = grub_le_to_cpu32 (lh.relocatable); + } + + if (grub_le_to_cpu16 (lh.version) >= 0x020a) + { + min_align = lh.min_alignment; + prot_size = grub_le_to_cpu32 (lh.init_size); + prot_mode_target = grub_le_to_cpu64 (lh.pref_address); + } + else + { + min_align = 0; + prot_size = grub_file_size (file) - real_size - GRUB_DISK_SECTOR_SIZE; + prot_mode_target = grub_le_to_cpu32 (lh.code32_start); + } + + prot_file_size = grub_file_size (file) - real_size - GRUB_DISK_SECTOR_SIZE; + if (allocate_pages (prot_size, &align, min_align, relocatable)) goto fail; params = (struct linux_kernel_params *) real_mode_mem;@@ -715,6 +760,8 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), grub_memcpy (¶ms->setup_sects, &lh.setup_sects, sizeof (lh) - 0x1F1); params->ps_mouse = params->padding10 = 0; + params->code32_start = prot_mode_target; + params->kernel_alignment = (1 << align); len = 0x400 - sizeof (lh); if (grub_file_read (file, (char *) real_mode_mem + sizeof (lh), len) != len)@@ -774,7 +821,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), grub_file_seek (file, real_size + GRUB_DISK_SECTOR_SIZE); grub_dprintf ("linux", "bzImage, setup=0x%x, size=0x%x\n", - (unsigned) real_size, (unsigned) prot_size); + (unsigned) real_size, (unsigned) prot_file_size); /* Look for memory size and video mode specified on the command line. */ linux_mem_size = 0;@@ -911,7 +958,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),maximal_cmdline_size
- (sizeof (LINUX_IMAGE) - 1));- len =
prot_size;
+ len =prot_file_size;
if (grub_file_read(file,prot_mode_mem, len) != len && !grub_errno)grub_error(GRUB_ERR_FILE_READ_ERROR,N_("premature end of file %s"), argv[0]);
--
1.7.7.6
Quoted 4 lines
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Boot fine in VirtualBox X64 UEFI (UEFI 2.1 Tianocore OVMF). Patch V1 3/3 failed with premature end of file error.
Regards.
Keshav
Re: [PATCH V2 3/3] Update Linux loader to follow the kernel's preferences
Quoted 181 lines · Keshav P R
Quoted 172 lines · Matthew Garrett
We should attempt to load the kernel at its preferred address, and if we can't do that then we should at lesat align it correctly. When doing so we should also make sure to avoid putting the kernel on top of any regions being used by the firmware.
---I managed to screw up the rebase, so this is a fixed version. Also, the code now follows the old path if the kernel isn't relocatable - we can't move it, so better to try it and just hope that everything works out.
ChangeLog | 8 +++++
grub-core/loader/i386/linux.c| 67 ++++++++++++++++++++++++++++++++++------2 files changed, 65 insertions(+), 10 deletions(-)ChangeLog +0
-0@@ -1,5 +1,13 @@2012-02-06 Matthew Garrett <[email protected]>
+ *
grub-core/loader/i386/linux.c(allocate_pages): Attempt to obtain
+ appropriately aligned memory if the desired target is unavailable
+ (grub_cmd_linux): Update to match newer Linux boot protocols, and
+ attempt to load the kernel at its preferred address rather than
+ hardcoding.ChangeLog +3
-0+ +2012-02-06 Matthew Garrett <[email protected]> +*
grub-core/lib/efi/relocator.c(grub_relocator_alloc_chunk_addr):
Add argument to fail allocation when target address overlaps firmware regions. All users updated.grub-core/loader/i386/linux.c +1
-1@@ -183,13 +183,14 @@ free_pages (void) grub_relocator_unload (relocator); relocator = NULL; real_mode_mem = prot_mode_mem = initrd_mem = 0; - real_mode_target = prot_mode_target = initrd_mem_target = 0; + real_mode_target = initrd_mem_target = 0; } /* Allocate pages for the real mode code and the protected mode codefor linux as well as a memory map buffer. */
staticgrub_err_tgrub-core/loader/i386/linux.c +2
-1-allocate_pages (grub_size_t prot_size) +allocate_pages (grub_size_t prot_size, grub_size_t *align, + grub_size_t min_align, int relocatable){
grub_size_treal_size,mmap_size;
grub_err_terr;grub-core/loader/i386/linux.c +53
-7@@ -269,18 +270,38 @@ allocate_pages (grub_size_t prot_size) + efi_mmap_size), 0); if (err) goto fail; + + grub_errno = GRUB_ERR_NONE; real_mode_mem = get_virtual_current_address (ch); } efi_mmap_buf = (grub_uint8_t *) real_mode_mem + real_size + mmap_size; - prot_mode_target = GRUB_LINUX_BZIMAGE_ADDR; - { grub_relocator_chunk_t ch; err = grub_relocator_alloc_chunk_addr (relocator, &ch, - prot_mode_target, prot_size, 0); + prot_mode_target, prot_size, + relocatable); + if (err) + { + unsigned int i; + for (i = *align; i >= min_align; i--) + { + err = grub_relocator_alloc_chunk_align (relocator, &ch, + 0x1000000, 0xffffffff, + prot_size, 1 << i, + GRUB_RELOCATOR_PREFERENCE_LOW); + if (!err) + { + *align = i; + prot_mode_target = get_physical_target_address (ch); + break; + } + } + } + if (err) goto fail; + prot_mode_mem = get_virtual_current_address (ch); }@@ -631,8 +652,9 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), struct linux_kernel_header lh; struct linux_kernel_params *params; grub_uint8_t setup_sects; - grub_size_t real_size, prot_size; + grub_size_t real_size, prot_size, prot_file_size, align = 0, min_align = 0; grub_ssize_t len; + int relocatable = 0; int i; grub_dl_ref (my_mod);@@ -705,9 +727,32 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), setup_sects = GRUB_LINUX_DEFAULT_SETUP_SECTS; real_size = setup_sects << GRUB_DISK_SECTOR_BITS; - prot_size = grub_file_size (file) - real_size - GRUB_DISK_SECTOR_SIZE; - if (allocate_pages (prot_size)) + if (grub_le_to_cpu16 (lh.version) >= 0x205) + { + for (align = 0; align < 32; align++) + { + if (grub_le_to_cpu32 (lh.kernel_alignment) & (1 << align)) + break; + } + relocatable = grub_le_to_cpu32 (lh.relocatable); + } + + if (grub_le_to_cpu16 (lh.version) >= 0x020a) + { + min_align = lh.min_alignment; + prot_size = grub_le_to_cpu32 (lh.init_size); + prot_mode_target = grub_le_to_cpu64 (lh.pref_address); + } + else + { + min_align = 0; + prot_size = grub_file_size (file) - real_size - GRUB_DISK_SECTOR_SIZE; + prot_mode_target = grub_le_to_cpu32 (lh.code32_start); + } + + prot_file_size = grub_file_size (file) - real_size - GRUB_DISK_SECTOR_SIZE; + if (allocate_pages (prot_size, &align, min_align, relocatable)) goto fail; params = (struct linux_kernel_params *) real_mode_mem;@@ -715,6 +760,8 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), grub_memcpy (¶ms->setup_sects, &lh.setup_sects, sizeof (lh) - 0x1F1); params->ps_mouse = params->padding10 = 0; + params->code32_start = prot_mode_target; + params->kernel_alignment = (1 << align); len = 0x400 - sizeof (lh); if (grub_file_read (file, (char *) real_mode_mem + sizeof (lh), len) != len)@@ -774,7 +821,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), grub_file_seek (file, real_size + GRUB_DISK_SECTOR_SIZE); grub_dprintf ("linux", "bzImage, setup=0x%x, size=0x%x\n", - (unsigned) real_size, (unsigned) prot_size); + (unsigned) real_size, (unsigned) prot_file_size); /* Look for memory size and video mode specified on the command line. */ linux_mem_size = 0;@@ -911,7 +958,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),maximal_cmdline_size
- (sizeof (LINUX_IMAGE) - 1));- len =
prot_size;
+ len =prot_file_size;
if (grub_file_read(file,prot_mode_mem, len) != len && !grub_errno)grub_error(GRUB_ERR_FILE_READ_ERROR,N_("premature end of file %s"), argv[0]);
--
1.7.7.6Quoted 4 lines
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-develBoot fine in VirtualBox X64 UEFI (UEFI 2.1 Tianocore OVMF). Patch V1 3/3 failed with premature end of file error.
Quoted 3 lines
Regards.
Keshav
Compile error for grub2-bios (i386-pc target)
gcc -DHAVE_CONFIG_H -I. -I.. -Wall -W -I../include -I../include -DGRUB_MACHINE_PCBIOS=1 -DGRUB_MACHINE=I386_PC -DGRUB_TARGET_CPU_I386=1 -m32 -nostdinc -isystem /usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/include -DGRUB_FILE=\"loader/i386/pc/plan9.c\" -I. -I. -I.. -I.. -I../include
-I../include -Os -Wall -W -Wshadow -Wpointer-arith
-Wmissing-prototypes -Wundef -Wstrict-prototypes -g -falign-jumps=1 -falign-loops=1 -falign-functions=1 -mno-mmx -mno-sse -mno-sse2 -mno-3dnow -fno-dwarf2-cfi-asm -fno-asynchronous-unwind-tables -m32 -fno-stack-protector -mno-stack-arg-probe -Wno-trampolines -DUSE_ASCII_FAILBACK=1 -DHAVE_UNIFONT_WIDTHSPEC=1 -mrtd -mregparm=3
-ffreestanding -MT loader/i386/pc/plan9_module-plan9.o -MD -MP
-MF loader/i386/pc/.deps-core/plan9_module-plan9.Tpo -c -o loader/i386/pc/plan9_module-plan9.o test -f 'loader/i386/pc/plan9.c' || echo './'loader/i386/pc/plan9.c loader/i386/pc/plan9.c: In function 'grub_cmd_plan9':loader/i386/pc/plan9.c:420:9: error: too few arguments to function 'grub_relocator_alloc_chunk_addr' ../include/grub/relocator.h:34:1: note: declared here loader/i386/pc/plan9.c:451:9: error: too few arguments to function 'grub_relocator_alloc_chunk_addr' ../include/grub/relocator.h:34:1: note: declared here make[3]: *** [loader/i386/pc/plan9_module-plan9.o] Error 1
with all the three V2 patches applied in order (didn't try V1). No error in x86_64-efi compile.
- Keshav
Re: [PATCH V2 3/3] Update Linux loader to follow the kernel's preferences
Quoted 12 lines · Keshav P R
loader/i386/pc/plan9_module-plan9.otest -f 'loader/i386/pc/plan9.c' || echo './'loader/i386/pc/plan9.cloader/i386/pc/plan9.c: In function 'grub_cmd_plan9':loader/i386/pc/plan9.c:420:9: error: too few arguments to function 'grub_relocator_alloc_chunk_addr'../include/grub/relocator.h:34:1: note: declared hereloader/i386/pc/plan9.c:451:9: error: too few arguments to function 'grub_relocator_alloc_chunk_addr'../include/grub/relocator.h:34:1: note: declared here make[3]: *** [loader/i386/pc/plan9_module-plan9.o] Error 1
Quoted 2 lines
with all the three V2 patches applied in order (didn't try V1). No error in x86_64-efi compile.
Sigh. Sorry, I missed that the plan9 loader had been added. Just add a 0 as an additional argument to grub_relocator_alloc_chunk_addr() in plan9.c.
Signature · 1 line
Matthew Garrett | [email protected]
Re: [PATCH V2 3/3] Update Linux loader to follow the kernel's preferences
Quoted 13 lines · Matthew Garrett
Quoted 12 lines · Keshav P R
loader/i386/pc/plan9_module-plan9.otest -f 'loader/i386/pc/plan9.c' || echo './'loader/i386/pc/plan9.cloader/i386/pc/plan9.c: In function 'grub_cmd_plan9':loader/i386/pc/plan9.c:420:9: error: too few arguments to function 'grub_relocator_alloc_chunk_addr'../include/grub/relocator.h:34:1: note: declared hereloader/i386/pc/plan9.c:451:9: error: too few arguments to function 'grub_relocator_alloc_chunk_addr'../include/grub/relocator.h:34:1: note: declared here make[3]: *** [loader/i386/pc/plan9_module-plan9.o] Error 1
Quoted 7 lines
Quoted 2 lines
with all the three V2 patches applied in order (didn't try V1). No error in x86_64-efi compile.
Sigh. Sorry, I missed that the plan9 loader had been added. Just add a 0 as an additional argument to
grub_relocator_alloc_chunk_addr()inplan9.c.
Any other files that require this change?
Quoted 2 lines
--
Matthew Garrett | [email protected]
Re: [PATCH V2 3/3] Update Linux loader to follow the kernel's preferences
Quoted 17 lines · Keshav P R
Quoted 16 lines · Matthew Garrett
Quoted 14 lines · Keshav P R
loader/i386/pc/plan9_module-plan9.otest -f 'loader/i386/pc/plan9.c' || echo './'loader/i386/pc/plan9.cloader/i386/pc/plan9.c: In function 'grub_cmd_plan9':loader/i386/pc/plan9.c:420:9: error: too few arguments to function 'grub_relocator_alloc_chunk_addr'../include/grub/relocator.h:34:1: note: declared hereloader/i386/pc/plan9.c:451:9: error: too few arguments to function 'grub_relocator_alloc_chunk_addr'../include/grub/relocator.h:34:1: note: declared here make[3]: *** [loader/i386/pc/plan9_module-plan9.o] Error 1with all the three V2 patches applied in order (didn't try V1). No error in x86_64-efi compile.
Quoted 7 lines
Quoted 4 lines
Sigh. Sorry, I missed that the plan9 loader had been added. Just add a 0 as an additional argument to
grub_relocator_alloc_chunk_addr()inplan9.c.Any other files that require this change?
After applying the patch, "git grep -n grub_relocator_alloc_chunk_addr" shows, among them the to-be-changed files (I think)
grub-core/loader/i386/coreboot/chainloader.c:71: err = grub_relocator_alloc_chunk_addr (relocator, &ch,grub-core/loader/i386/pc/chainloader.c:168: err =grub_relocator_alloc_chunk_addr (rel, &ch, 0x7C00,grub-core/loader/i386/pc/chainloader.c:173: err =grub_relocator_alloc_chunk_addr (rel, &ch,grub-core/loader/i386/pc/plan9.c:419: err =grub_relocator_alloc_chunk_addr (rel, &ch, GRUB_PLAN9_CONFIG_ADDR,grub-core/loader/i386/pc/plan9.c:450: err =grub_relocator_alloc_chunk_addr (rel, &ch, GRUB_PLAN9_TARGET,grub-core/loader/i386/pc/pxechainloader.c:133: err =grub_relocator_alloc_chunk_addr (rel, &ch, 0x7c00, imagesize);
Quoted 2 lines
Quoted 2 lines
--
Matthew Garrett | [email protected]
Re: [PATCH V2 3/3] Update Linux loader to follow the kernel's preferences
Quoted 30 lines · Keshav P R
Quoted 17 lines · Keshav P R
Quoted 16 lines · Matthew Garrett
Quoted 14 lines · Keshav P R
loader/i386/pc/plan9_module-plan9.otest -f 'loader/i386/pc/plan9.c' || echo './'loader/i386/pc/plan9.cloader/i386/pc/plan9.c: In function 'grub_cmd_plan9':loader/i386/pc/plan9.c:420:9: error: too few arguments to function 'grub_relocator_alloc_chunk_addr'../include/grub/relocator.h:34:1: note: declared hereloader/i386/pc/plan9.c:451:9: error: too few arguments to function 'grub_relocator_alloc_chunk_addr'../include/grub/relocator.h:34:1: note: declared here make[3]: *** [loader/i386/pc/plan9_module-plan9.o] Error 1with all the three V2 patches applied in order (didn't try V1). No error in x86_64-efi compile.
Quoted 7 lines
Quoted 4 lines
Sigh. Sorry, I missed that the plan9 loader had been added. Just add a 0 as an additional argument to
grub_relocator_alloc_chunk_addr()inplan9.c.Any other files that require this change?
After applying the patch, "git grep -n
grub_relocator_alloc_chunk_addr" shows, among them the to-be-changed files (I think)
Quoted 16 lines
grub-core/loader/i386/coreboot/chainloader.c:71: err =grub_relocator_alloc_chunk_addr(relocator, &ch,grub-core/loader/i386/pc/chainloader.c:168: err =grub_relocator_alloc_chunk_addr(rel, &ch,0x7C00,grub-core/loader/i386/pc/chainloader.c:173: err =grub_relocator_alloc_chunk_addr(rel, &ch,grub-core/loader/i386/pc/plan9.c:419: err =grub_relocator_alloc_chunk_addr(rel, &ch,GRUB_PLAN9_CONFIG_ADDR,grub-core/loader/i386/pc/plan9.c:450: err =grub_relocator_alloc_chunk_addr(rel, &ch,GRUB_PLAN9_TARGET,grub-core/loader/i386/pc/pxechainloader.c:133: err =grub_relocator_alloc_chunk_addr(rel, &ch,0x7c00, imagesize);Quoted 2 lines
Quoted 2 lines
--
Matthew Garrett | [email protected]
Patch attached that fixes the remaining files (atleast i386-pc build succeeds)
- Keshav
grub-core/loader/i386/coreboot/chainloader.c +1 -1
@@ -69,7 +69,7 @@ grub_chain_elf32_hook (Elf32_Phdr * phdr, grub_addr_t * addr, int *do_load) *do_load =1 ; err =grub_relocator_alloc_chunk_addr (relocator, &ch, - phdr->p_paddr, phdr->p_memsz); + phdr->p_paddr, phdr->p_memsz,0 );if (err)return err;
grub-core/loader/i386/pc/chainloader.c +2 -2
@@ -166,13 +166,13 @@ grub_chainloader_cmd (const char *filename, grub_chainloader_flags_t flags)grub_err_t err; err =grub_relocator_alloc_chunk_addr (rel, &ch,0x7C00 , -GRUB_DISK_SECTOR_SIZE ); +GRUB_DISK_SECTOR_SIZE ,0 );if (err) goto fail; bs =get_virtual_current_address (ch); err =grub_relocator_alloc_chunk_addr (rel, &ch,GRUB_MEMORY_MACHINE_PART_TABLE_ADDR , -64 ); +64 ,0 );if (err) goto fail; ptable =get_virtual_current_address (ch);
grub-core/loader/i386/pc/plan9.c +2 -2
@@ -417,7 +417,7 @@ grub_cmd_plan9 (grub_extcmd_context_t ctxt, int argc, char *argv[])grub_relocator_chunk_t ch;grub_err_t err; err =grub_relocator_alloc_chunk_addr (rel, &ch,GRUB_PLAN9_CONFIG_ADDR , - configsize); + configsize,0 );if (err) goto fail; config =get_virtual_current_address (ch);
@@ -448,7 +448,7 @@ grub_cmd_plan9 (grub_extcmd_context_t ctxt, int argc, char *argv[])grub_err_t err; err =grub_relocator_alloc_chunk_addr (rel, &ch,GRUB_PLAN9_TARGET , - memsize); + memsize,0 );if (err) goto fail; mem =get_virtual_current_address (ch);
grub-core/loader/i386/pc/pxechainloader.c +1 -1
@@ -130,7 +130,7 @@ grub_cmd_pxechain (grub_command_t cmd __attribute__ ((unused)), imagesize =grub_file_size (file); {grub_relocator_chunk_t ch; - err =grub_relocator_alloc_chunk_addr (rel, &ch,0x7c00 , imagesize); + err =grub_relocator_alloc_chunk_addr (rel, &ch,0x7c00 , imagesize,0 );if (err) goto fail; image =get_virtual_current_address (ch);
Re: [PATCH V2 3/3] Update Linux loader to follow the kernel's preferences
Quoted 45 lines · Keshav P R
Quoted 44 lines · Keshav P R
Quoted 17 lines · Keshav P R
Quoted 16 lines · Matthew Garrett
Quoted 14 lines · Keshav P R
loader/i386/pc/plan9_module-plan9.otest -f 'loader/i386/pc/plan9.c' || echo './'loader/i386/pc/plan9.cloader/i386/pc/plan9.c: In function 'grub_cmd_plan9':loader/i386/pc/plan9.c:420:9: error: too few arguments to function 'grub_relocator_alloc_chunk_addr'../include/grub/relocator.h:34:1: note: declared hereloader/i386/pc/plan9.c:451:9: error: too few arguments to function 'grub_relocator_alloc_chunk_addr'../include/grub/relocator.h:34:1: note: declared here make[3]: *** [loader/i386/pc/plan9_module-plan9.o] Error 1with all the three V2 patches applied in order (didn't try V1). No error in x86_64-efi compile.
Quoted 7 lines
Quoted 4 lines
Sigh. Sorry, I missed that the plan9 loader had been added. Just add a 0 as an additional argument to
grub_relocator_alloc_chunk_addr()inplan9.c.Any other files that require this change?
After applying the patch, "git grep -n
grub_relocator_alloc_chunk_addr" shows, among them the to-be-changed files (I think)
grub-core/loader/i386/coreboot/chainloader.c:71: err =grub_relocator_alloc_chunk_addr(relocator, &ch,grub-core/loader/i386/pc/chainloader.c:168: err =grub_relocator_alloc_chunk_addr(rel, &ch,0x7C00,grub-core/loader/i386/pc/chainloader.c:173: err =grub_relocator_alloc_chunk_addr(rel, &ch,grub-core/loader/i386/pc/plan9.c:419: err =grub_relocator_alloc_chunk_addr(rel, &ch,GRUB_PLAN9_CONFIG_ADDR,grub-core/loader/i386/pc/plan9.c:450: err =grub_relocator_alloc_chunk_addr(rel, &ch,GRUB_PLAN9_TARGET,grub-core/loader/i386/pc/pxechainloader.c:133: err =grub_relocator_alloc_chunk_addr(rel, &ch,0x7c00, imagesize);
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
--
Matthew Garrett | [email protected]Patch attached that fixes the remaining files (atleast i386-pc build succeeds)
Just got the V3 patches in my mailbox with the changes added. Thanks for the patch.
- Keshav