grub-devel.gnu.org archive mirror
 help / color / mirror / Atom feed
From: Leif Lindholm <leif.lindholm@linaro.org>
To: grub-devel@gnu.org
Cc: Vladimir 'phcoder' Serbinenko <phcoder@gmail.com>,
	Daniel Kiper <dkiper@net-space.pl>
Subject: [PATCH 3/4] efi: add central copy of grub_efi_find_mmap_size
Date: Tue,  5 Sep 2017 21:41:13 +0100	[thread overview]
Message-ID: <20170905204114.9462-4-leif.lindholm@linaro.org> (raw)
In-Reply-To: <20170905204114.9462-1-leif.lindholm@linaro.org>

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 <leif.lindholm@linaro.org>
---
 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



  parent reply	other threads:[~2017-09-05 20:41 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-09-05 20:41 [PATCH 0/4] x86,efi: prerequisites for ARM* cleanup series Leif Lindholm
2017-09-05 20:41 ` [PATCH 1/4] i386/x86_64: make linux header definitions arch-specific Leif Lindholm
2017-09-13  3:45   ` Daniel Kiper
2017-09-13  8:43     ` Leif Lindholm
2017-09-05 20:41 ` [PATCH 2/4] arm64/efi: move EFI_PAGE definitions to efi/memory.h Leif Lindholm
2017-09-13  4:00   ` Daniel Kiper
2017-09-13  8:45     ` Leif Lindholm
2017-09-05 20:41 ` Leif Lindholm [this message]
2017-09-13  4:39   ` [PATCH 3/4] efi: add central copy of grub_efi_find_mmap_size Daniel Kiper
2017-09-13  8:41     ` Leif Lindholm
2017-09-05 20:41 ` [PATCH 4/4] loader: drop local implementations of find_efi_mmap_size Leif Lindholm
2017-09-13  4:45   ` Daniel Kiper

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170905204114.9462-4-leif.lindholm@linaro.org \
    --to=leif.lindholm@linaro.org \
    --cc=dkiper@net-space.pl \
    --cc=grub-devel@gnu.org \
    --cc=phcoder@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).