diff mbox series

[v2,02/14] efi: add central copy of grub_efi_find_mmap_size

Message ID 20170803100432.29913-3-leif.lindholm@linaro.org
State Superseded
Headers show
Series efi: improved correctness, arm unification, and cleanup | expand

Commit Message

Leif Lindholm Aug. 3, 2017, 10:04 a.m. UTC
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 | 44 ++++++++++++++++++++++++++++++++++++++++++++
 include/grub/efi/efi.h  |  1 +
 2 files changed, 45 insertions(+)

-- 
2.11.0


_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

Comments

Vladimir 'phcoder' Serbinenko Aug. 3, 2017, 3:19 p.m. UTC | #1
Looks good. But out of context the name is not descriptive enough. Can we
add a comment near declaration to clarify what this functions is for?

Le Thu, Aug 3, 2017 à 12:07 PM, Leif Lindholm <leif.lindholm@linaro.org> a
écrit :

> 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 | 44 ++++++++++++++++++++++++++++++++++++++++++++

>  include/grub/efi/efi.h  |  1 +

>  2 files changed, 45 insertions(+)

>

> diff --git a/grub-core/kern/efi/mm.c b/grub-core/kern/efi/mm.c

> index 20a47aaf5..31ca703ec 100644

> --- a/grub-core/kern/efi/mm.c

> +++ b/grub-core/kern/efi/mm.c

> @@ -217,6 +217,50 @@ grub_efi_finish_boot_services (grub_efi_uintn_t

> *outbuf_size, void *outbuf,

>    return GRUB_ERR_NONE;

>  }

>

> +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 e9c601f34..3984de083 100644

> --- a/include/grub/efi/efi.h

> +++ b/include/grub/efi/efi.h

> @@ -42,6 +42,7 @@ EXPORT_FUNC(grub_efi_allocate_pages)

> (grub_efi_physical_address_t address,

>                                       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

>

>

> _______________________________________________

> Grub-devel mailing list

> Grub-devel@gnu.org

> https://lists.gnu.org/mailman/listinfo/grub-devel

>
_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel
Leif Lindholm Aug. 8, 2017, 3:56 p.m. UTC | #2
On Thu, Aug 03, 2017 at 03:19:25PM +0000, Vladimir 'phcoder' Serbinenko wrote:
> Looks good. But out of context the name is not descriptive enough. Can we

> add a comment near declaration to clarify what this functions is for?


Sure - will do.

/
    Leif

> Le Thu, Aug 3, 2017 à 12:07 PM, Leif Lindholm <leif.lindholm@linaro.org> a

> écrit :

> 

> > 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 | 44 ++++++++++++++++++++++++++++++++++++++++++++

> >  include/grub/efi/efi.h  |  1 +

> >  2 files changed, 45 insertions(+)

> >

> > diff --git a/grub-core/kern/efi/mm.c b/grub-core/kern/efi/mm.c

> > index 20a47aaf5..31ca703ec 100644

> > --- a/grub-core/kern/efi/mm.c

> > +++ b/grub-core/kern/efi/mm.c

> > @@ -217,6 +217,50 @@ grub_efi_finish_boot_services (grub_efi_uintn_t

> > *outbuf_size, void *outbuf,

> >    return GRUB_ERR_NONE;

> >  }

> >

> > +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 e9c601f34..3984de083 100644

> > --- a/include/grub/efi/efi.h

> > +++ b/include/grub/efi/efi.h

> > @@ -42,6 +42,7 @@ EXPORT_FUNC(grub_efi_allocate_pages)

> > (grub_efi_physical_address_t address,

> >                                       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

> >

> >

> > _______________________________________________

> > Grub-devel mailing list

> > Grub-devel@gnu.org

> > https://lists.gnu.org/mailman/listinfo/grub-devel

> >


> _______________________________________________

> Grub-devel mailing list

> Grub-devel@gnu.org

> https://lists.gnu.org/mailman/listinfo/grub-devel



_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel
diff mbox series

Patch

diff --git a/grub-core/kern/efi/mm.c b/grub-core/kern/efi/mm.c
index 20a47aaf5..31ca703ec 100644
--- a/grub-core/kern/efi/mm.c
+++ b/grub-core/kern/efi/mm.c
@@ -217,6 +217,50 @@  grub_efi_finish_boot_services (grub_efi_uintn_t *outbuf_size, void *outbuf,
   return GRUB_ERR_NONE;
 }
 
+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 e9c601f34..3984de083 100644
--- a/include/grub/efi/efi.h
+++ b/include/grub/efi/efi.h
@@ -42,6 +42,7 @@  EXPORT_FUNC(grub_efi_allocate_pages) (grub_efi_physical_address_t address,
 				      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,