Received: from list by lists.gnu.org with archive (Exim 4.71) id 1RvAoL-0002rH-7t for mharc-grub-devel@gnu.org; Wed, 08 Feb 2012 11:56:01 -0500 Received: from eggs.gnu.org ([140.186.70.92]:50926) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RvAoH-0002qU-LY for grub-devel@gnu.org; Wed, 08 Feb 2012 11:56:00 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RvAoD-0001OP-Ex for grub-devel@gnu.org; Wed, 08 Feb 2012 11:55:57 -0500 Received: from mx1.redhat.com ([209.132.183.28]:6323) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RvAoD-0001OL-6w for grub-devel@gnu.org; Wed, 08 Feb 2012 11:55:53 -0500 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id q18Gtqhe018182 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 8 Feb 2012 11:55:52 -0500 Received: from cavan.codon.org.uk (ovpn-113-122.phx2.redhat.com [10.3.113.122]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id q18GtoOB020780 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NO); Wed, 8 Feb 2012 11:55:52 -0500 Received: from nat-pool-rdu.redhat.com ([66.187.233.202] helo=2530p.boston.devel.redhat.com) by cavan.codon.org.uk with esmtpsa (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.72) (envelope-from ) id 1RvAo8-00012Z-9l; Wed, 08 Feb 2012 16:55:48 +0000 From: Matthew Garrett To: grub-devel@gnu.org Subject: [PATCH V3 3/3] Update Linux loader to follow the kernel's preferences Date: Wed, 8 Feb 2012 11:55:02 -0500 Message-Id: <1328720102-4914-3-git-send-email-mjg@redhat.com> In-Reply-To: <1328720102-4914-1-git-send-email-mjg@redhat.com> References: <1328720102-4914-1-git-send-email-mjg@redhat.com> X-SA-Do-Not-Run: Yes X-SA-Exim-Connect-IP: 66.187.233.202 X-SA-Exim-Mail-From: mjg@redhat.com X-SA-Exim-Scanned: No (on cavan.codon.org.uk); SAEximRunCond expanded to false X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.132.183.28 Cc: Matthew Garrett X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.14 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: Wed, 08 Feb 2012 16:56:00 -0000 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. --- ChangeLog | 8 +++++ grub-core/loader/i386/linux.c | 67 ++++++++++++++++++++++++++++++++++------ 2 files changed, 65 insertions(+), 10 deletions(-) diff --git a/ChangeLog b/ChangeLog index 8bef256..162f82b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,13 @@ 2012-02-08 Matthew Garrett + * 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-08 Matthew Garrett + * 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. diff --git a/grub-core/loader/i386/linux.c b/grub-core/loader/i386/linux.c index 67a4533..3eb8fa0 100644 --- a/grub-core/loader/i386/linux.c +++ b/grub-core/loader/i386/linux.c @@ -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 code for linux as well as a memory map buffer. */ static grub_err_t -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_t real_size, mmap_size; grub_err_t err; @@ -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