From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from list by lists.gnu.org with archive (Exim 4.71) id 1dpKf8-00076B-3A for mharc-grub-devel@gnu.org; Tue, 05 Sep 2017 16:41:34 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43657) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dpKf1-00073D-4n for grub-devel@gnu.org; Tue, 05 Sep 2017 16:41:31 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dpKew-0003Fo-Ci for grub-devel@gnu.org; Tue, 05 Sep 2017 16:41:27 -0400 Received: from mail-wr0-x22b.google.com ([2a00:1450:400c:c0c::22b]:34289) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dpKew-0003FM-6f for grub-devel@gnu.org; Tue, 05 Sep 2017 16:41:22 -0400 Received: by mail-wr0-x22b.google.com with SMTP id a47so9041528wra.1 for ; Tue, 05 Sep 2017 13:41:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=BZ9Px02dTLL9ncj4JBS6kvzCHiLbj6l7QNDhSw7fMLg=; b=DjhjRKgKnWOMF8ha5miPIASL9KLvmfW111FVOE4mIUGO0qTI/FVz39dgFYrqVhMZTU DMwYuJEWIP2VI5MntILH96GkZqlcxoh3AbY7oT2+bzenUgYe631Fa/H9yxGuq37bcnW3 L7pIOcSbd478R/6vP7PjMP+fBNGsvoXfvtrkw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=BZ9Px02dTLL9ncj4JBS6kvzCHiLbj6l7QNDhSw7fMLg=; b=dRcPjNbZ1pQfAr8kN/RzdBg880PRjdGH9Bk3HBCZAPiREtUnuVPO1esY7bgdCc+kvf gq6FF+Rg8AQVnXaX/ukVuPDc8BKLz4E3X6h+mkzcML4wm1BUIKOtQKlJ2Mxl9Mlg6b+L oCbFAfSMXOj/hjv84EI47talxs5a2Y6DJZ+IuNDz9xoReJsjnVcB+2giWkiKiU45t1uO IJxvXkveGanMq0jtwGow0dTP4Yz78LppeVS7b1pN0dKOw+j/t6z5LzYsxk7XPseptnp+ coQuAYJQwAa67QG9jgmZSDJLWIGAA8EpCsB4KWegLs4UlP52v/3k10LbnipyOi83IvHU VtFA== X-Gm-Message-State: AHPjjUiBVVGj284tvQFYmGyQlca2Mro5rpuAtB884MldbZ4xHEIEelpW 8fEyRiWytwLdhgmEj9O6mw== X-Google-Smtp-Source: ADKCNb699eC03BzKPYm0pLNEA2hMdPL5hy+l31QXNva6JfMh8sq8wMX0z6kyWlY2Mh4OjcbXOC/2Dg== X-Received: by 10.223.165.137 with SMTP id g9mr171972wrc.269.1504644080885; Tue, 05 Sep 2017 13:41:20 -0700 (PDT) Received: from vanye.hemma.eciton.net (cpc92316-cmbg19-2-0-cust118.5-4.cable.virginm.net. [82.12.0.119]) by smtp.gmail.com with ESMTPSA id k52sm2291077wrf.62.2017.09.05.13.41.20 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 05 Sep 2017 13:41:20 -0700 (PDT) From: Leif Lindholm To: grub-devel@gnu.org Cc: Vladimir 'phcoder' Serbinenko , Daniel Kiper Subject: [PATCH 3/4] efi: add central copy of grub_efi_find_mmap_size Date: Tue, 5 Sep 2017 21:41:13 +0100 Message-Id: <20170905204114.9462-4-leif.lindholm@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170905204114.9462-1-leif.lindholm@linaro.org> References: <20170905204114.9462-1-leif.lindholm@linaro.org> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:400c:c0c::22b X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 05 Sep 2017 20:41:32 -0000 There are several implementations of this function in the tree. Add a central version in grub-core/efi/mm.c. Taken from grub-core/loader/i386/linux.c, changing some hard-coded constants to use macros from efi/memory.h. Signed-off-by: Leif Lindholm --- grub-core/kern/efi/mm.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ include/grub/efi/efi.h | 1 + 2 files changed, 48 insertions(+) diff --git a/grub-core/kern/efi/mm.c b/grub-core/kern/efi/mm.c index ac2a4c556..8795aa1e0 100644 --- a/grub-core/kern/efi/mm.c +++ b/grub-core/kern/efi/mm.c @@ -218,6 +218,53 @@ grub_efi_finish_boot_services (grub_efi_uintn_t *outbuf_size, void *outbuf, return GRUB_ERR_NONE; } +/* To obtain the UEFI memory map, we must pass a buffer of sufficient size + to hold the entire map. This function returns a sane start value for + buffer size. */ +grub_efi_uintn_t +grub_efi_find_mmap_size (void) +{ + static grub_efi_uintn_t mmap_size = 0; + + if (mmap_size != 0) + return mmap_size; + + mmap_size = 1 * GRUB_EFI_PAGE_SIZE; + while (1) + { + int ret; + grub_efi_memory_descriptor_t *mmap; + grub_efi_uintn_t desc_size; + grub_efi_uintn_t cur_mmap_size = mmap_size; + + mmap = grub_malloc (cur_mmap_size); + if (! mmap) + return 0; + + ret = grub_efi_get_memory_map (&cur_mmap_size, mmap, 0, &desc_size, 0); + grub_free (mmap); + + if (ret < 0) + { + grub_error (GRUB_ERR_IO, "cannot get memory map"); + return 0; + } + else if (ret > 0) + break; + + if (mmap_size < cur_mmap_size) + mmap_size = cur_mmap_size; + mmap_size += GRUB_EFI_PAGE_SIZE; + } + + /* Increase the size a bit for safety, because GRUB allocates more on + later, and EFI itself may allocate more. */ + mmap_size += 3 * GRUB_EFI_PAGE_SIZE; + + mmap_size = ALIGN_UP (mmap_size, GRUB_EFI_PAGE_SIZE); + return mmap_size; +} + /* Get the memory map as defined in the EFI spec. Return 1 if successful, return 0 if partial, or return -1 if an error occurs. */ int diff --git a/include/grub/efi/efi.h b/include/grub/efi/efi.h index 3fa082816..8f1f36c8c 100644 --- a/include/grub/efi/efi.h +++ b/include/grub/efi/efi.h @@ -49,6 +49,7 @@ void * EXPORT_FUNC(grub_efi_allocate_any_pages) (grub_efi_uintn_t pages); void EXPORT_FUNC(grub_efi_free_pages) (grub_efi_physical_address_t address, grub_efi_uintn_t pages); +grub_efi_uintn_t EXPORT_FUNC(grub_efi_find_mmap_size) (void); int EXPORT_FUNC(grub_efi_get_memory_map) (grub_efi_uintn_t *memory_map_size, grub_efi_memory_descriptor_t *memory_map, -- 2.11.0