[v3,2/6] efi: add grub_efi_get_ram_base() function for arm64

Message ID 20180627171720.27028-3-leif.lindholm@linaro.org
State Superseded
Headers show
Series
  • efi: arm linux loader unification and correctness
Related show

Commit Message

Leif Lindholm June 27, 2018, 5:17 p.m.
Since ARM platforms do not have a common memory map, add a helper
function that finds the lowest address region with the EFI_MEMORY_WB
attribute set in the UEFI memory map.

Required for the arm64 efi linux loader to restrict the initrd
location to where it will be accessible by the kernel at runtime.

Signed-off-by: Leif Lindholm <leif.lindholm@linaro.org>

---
 grub-core/kern/efi/mm.c | 36 ++++++++++++++++++++++++++++++++++++
 include/grub/efi/efi.h  |  3 +++
 2 files changed, 39 insertions(+)

-- 
2.11.0


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

Comments

Daniel Kiper July 6, 2018, 3:12 p.m. | #1
On Wed, Jun 27, 2018 at 06:17:16PM +0100, Leif Lindholm wrote:
> Since ARM platforms do not have a common memory map, add a helper

> function that finds the lowest address region with the EFI_MEMORY_WB

> attribute set in the UEFI memory map.

>

> Required for the arm64 efi linux loader to restrict the initrd

> location to where it will be accessible by the kernel at runtime.

>

> Signed-off-by: Leif Lindholm <leif.lindholm@linaro.org>

> ---

>  grub-core/kern/efi/mm.c | 36 ++++++++++++++++++++++++++++++++++++

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

>  2 files changed, 39 insertions(+)

>

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

> index fd39d23b4..10ffa2c9b 100644

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

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

> @@ -629,3 +629,39 @@ grub_efi_mm_init (void)

>    grub_efi_free_pages ((grub_addr_t) memory_map,

>  		       2 * BYTES_TO_PAGES (MEMORY_MAP_SIZE));

>  }

> +

> +#if defined (__aarch64__)

> +grub_err_t

> +grub_efi_get_ram_base(grub_addr_t *base_addr)

> +{

> +  grub_efi_memory_descriptor_t *memory_map;

> +  grub_efi_memory_descriptor_t *desc;


     grub_efi_memory_descriptor_t *desc, *memory_map;

> +  grub_efi_uintn_t mmap_size;

> +  grub_efi_uintn_t desc_size;


     grub_efi_uintn_t desc_size, mmap_size;

> +  int ret;

> +

> +  mmap_size = grub_efi_find_mmap_size();

> +

> +  memory_map = grub_malloc (mmap_size);

> +  if (! memory_map)

> +    return GRUB_ERR_OUT_OF_MEMORY;

> +  ret = grub_efi_get_memory_map (&mmap_size, memory_map, NULL,

> +				 &desc_size, NULL);

> +

> +  if (ret < 1)

> +    return GRUB_ERR_BUG;

> +

> +  for (desc = memory_map, *base_addr = GRUB_UINT_MAX;

> +       (grub_addr_t) desc < ((grub_addr_t) memory_map + mmap_size);

> +       desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size))

> +    {

> +      if (desc->attribute & GRUB_EFI_MEMORY_WB)

           *base_addr = grub_min (*base_addr, desc->physical_start);

> +	if (desc->physical_start < *base_addr)

> +	  *base_addr = desc->physical_start;

> +    }


And then you can drop these curly brackets.

Daniel

_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel
Michel Hermier July 6, 2018, 4:17 p.m. | #2
Le ven. 6 juil. 2018 à 17:17, Daniel Kiper <daniel.kiper@oracle.com> a
écrit :

> On Wed, Jun 27, 2018 at 06:17:16PM +0100, Leif Lindholm wrote:

> > Since ARM platforms do not have a common memory map, add a helper

> > function that finds the lowest address region with the EFI_MEMORY_WB

> > attribute set in the UEFI memory map.

> >

> > Required for the arm64 efi linux loader to restrict the initrd

> > location to where it will be accessible by the kernel at runtime.

> >

> > Signed-off-by: Leif Lindholm <leif.lindholm@linaro.org>

> > ---

> >  grub-core/kern/efi/mm.c | 36 ++++++++++++++++++++++++++++++++++++

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

> >  2 files changed, 39 insertions(+)

> >

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

> > index fd39d23b4..10ffa2c9b 100644

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

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

> > @@ -629,3 +629,39 @@ grub_efi_mm_init (void)

> >    grub_efi_free_pages ((grub_addr_t) memory_map,

> >                      2 * BYTES_TO_PAGES (MEMORY_MAP_SIZE));

> >  }

> > +

> > +#if defined (__aarch64__)

> > +grub_err_t

> > +grub_efi_get_ram_base(grub_addr_t *base_addr)

> > +{

> > +  grub_efi_memory_descriptor_t *memory_map;

> > +  grub_efi_memory_descriptor_t *desc;

>

>      grub_efi_memory_descriptor_t *desc, *memory_map;

>


For readability and ease of understanding, I think mmap_size should be
renamed to memory_map_size (or the other way around) to explicit/uniformize
naming.


> > +  grub_efi_uintn_t mmap_size;

> > +  grub_efi_uintn_t desc_size;

>

>      grub_efi_uintn_t desc_size, mmap_size;

>

> > +  int ret;

> > +

> > +  mmap_size = grub_efi_find_mmap_size();

> > +

> > +  memory_map = grub_malloc (mmap_size);

> > +  if (! memory_map)

> > +    return GRUB_ERR_OUT_OF_MEMORY;

> > +  ret = grub_efi_get_memory_map (&mmap_size, memory_map, NULL,

> > +                              &desc_size, NULL);

> > +

> > +  if (ret < 1)

> > +    return GRUB_ERR_BUG;

> > +

> > +  for (desc = memory_map, *base_addr = GRUB_UINT_MAX;

> > +       (grub_addr_t) desc < ((grub_addr_t) memory_map + mmap_size);

> > +       desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size))

> > +    {

> > +      if (desc->attribute & GRUB_EFI_MEMORY_WB)

>            *base_addr = grub_min (*base_addr, desc->physical_start);

>

> > +     if (desc->physical_start < *base_addr)

> > +       *base_addr = desc->physical_start;

> > +    }

>

> And then you can drop these curly brackets.

>

> Daniel

>

> _______________________________________________

> Grub-devel mailing list

> Grub-devel@gnu.org

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

>
<div dir="auto"><div><br><br><div class="gmail_quote"><div dir="ltr">Le ven. 6 juil. 2018 à 17:17, Daniel Kiper &lt;<a href="mailto:daniel.kiper@oracle.com">daniel.kiper@oracle.com</a>&gt; a écrit :<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On Wed, Jun 27, 2018 at 06:17:16PM +0100, Leif Lindholm wrote:<br>
&gt; Since ARM platforms do not have a common memory map, add a helper<br>
&gt; function that finds the lowest address region with the EFI_MEMORY_WB<br>
&gt; attribute set in the UEFI memory map.<br>
&gt;<br>
&gt; Required for the arm64 efi linux loader to restrict the initrd<br>
&gt; location to where it will be accessible by the kernel at runtime.<br>
&gt;<br>
&gt; Signed-off-by: Leif Lindholm &lt;<a href="mailto:leif.lindholm@linaro.org" target="_blank" rel="noreferrer">leif.lindholm@linaro.org</a>&gt;<br>
&gt; ---<br>
&gt;  grub-core/kern/efi/mm.c | 36 ++++++++++++++++++++++++++++++++++++<br>
&gt;  include/grub/efi/efi.h  |  3 +++<br>
&gt;  2 files changed, 39 insertions(+)<br>
&gt;<br>
&gt; diff --git a/grub-core/kern/efi/mm.c b/grub-core/kern/efi/mm.c<br>
&gt; index fd39d23b4..10ffa2c9b 100644<br>
&gt; --- a/grub-core/kern/efi/mm.c<br>
&gt; +++ b/grub-core/kern/efi/mm.c<br>
&gt; @@ -629,3 +629,39 @@ grub_efi_mm_init (void)<br>
&gt;    grub_efi_free_pages ((grub_addr_t) memory_map,<br>
&gt;                      2 * BYTES_TO_PAGES (MEMORY_MAP_SIZE));<br>
&gt;  }<br>
&gt; +<br>
&gt; +#if defined (__aarch64__)<br>
&gt; +grub_err_t<br>
&gt; +grub_efi_get_ram_base(grub_addr_t *base_addr)<br>
&gt; +{<br>
&gt; +  grub_efi_memory_descriptor_t *memory_map;<br>
&gt; +  grub_efi_memory_descriptor_t *desc;<br>
<br>
     grub_efi_memory_descriptor_t *desc, *memory_map;<br></blockquote></div></div><div dir="auto"><br></div><div dir="auto">For readability and ease of understanding, I think mmap_size should be renamed to memory_map_size (or the other way around) to explicit/uniformize naming.</div><div dir="auto"><br></div><div dir="auto"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
&gt; +  grub_efi_uintn_t mmap_size;<br>
&gt; +  grub_efi_uintn_t desc_size;<br>
<br>
     grub_efi_uintn_t desc_size, mmap_size;<br>
<br>
&gt; +  int ret;<br>
&gt; +<br>
&gt; +  mmap_size = grub_efi_find_mmap_size();<br>
&gt; +<br>
&gt; +  memory_map = grub_malloc (mmap_size);<br>
&gt; +  if (! memory_map)<br>
&gt; +    return GRUB_ERR_OUT_OF_MEMORY;<br>
&gt; +  ret = grub_efi_get_memory_map (&amp;mmap_size, memory_map, NULL,<br>
&gt; +                              &amp;desc_size, NULL);<br>
&gt; +<br>
&gt; +  if (ret &lt; 1)<br>
&gt; +    return GRUB_ERR_BUG;<br>
&gt; +<br>
&gt; +  for (desc = memory_map, *base_addr = GRUB_UINT_MAX;<br>
&gt; +       (grub_addr_t) desc &lt; ((grub_addr_t) memory_map + mmap_size);<br>
&gt; +       desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size))<br>
&gt; +    {<br>
&gt; +      if (desc-&gt;attribute &amp; GRUB_EFI_MEMORY_WB)<br>
           *base_addr = grub_min (*base_addr, desc-&gt;physical_start);<br>
<br>
&gt; +     if (desc-&gt;physical_start &lt; *base_addr)<br>
&gt; +       *base_addr = desc-&gt;physical_start;<br>
&gt; +    }<br>
<br>
And then you can drop these curly brackets.<br>
<br>
Daniel<br>
<br>
_______________________________________________<br>
Grub-devel mailing list<br>
<a href="mailto:Grub-devel@gnu.org" target="_blank" rel="noreferrer">Grub-devel@gnu.org</a><br>
<a href="https://lists.gnu.org/mailman/listinfo/grub-devel" rel="noreferrer noreferrer" target="_blank">https://lists.gnu.org/mailman/listinfo/grub-devel</a><br>
</blockquote></div></div></div>
_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel
Leif Lindholm July 6, 2018, 4:24 p.m. | #3
On Fri, Jul 06, 2018 at 05:12:27PM +0200, Daniel Kiper wrote:
> On Wed, Jun 27, 2018 at 06:17:16PM +0100, Leif Lindholm wrote:

> > Since ARM platforms do not have a common memory map, add a helper

> > function that finds the lowest address region with the EFI_MEMORY_WB

> > attribute set in the UEFI memory map.

> >

> > Required for the arm64 efi linux loader to restrict the initrd

> > location to where it will be accessible by the kernel at runtime.

> >

> > Signed-off-by: Leif Lindholm <leif.lindholm@linaro.org>

> > ---

> >  grub-core/kern/efi/mm.c | 36 ++++++++++++++++++++++++++++++++++++

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

> >  2 files changed, 39 insertions(+)

> >

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

> > index fd39d23b4..10ffa2c9b 100644

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

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

> > @@ -629,3 +629,39 @@ grub_efi_mm_init (void)

> >    grub_efi_free_pages ((grub_addr_t) memory_map,

> >  		       2 * BYTES_TO_PAGES (MEMORY_MAP_SIZE));

> >  }

> > +

> > +#if defined (__aarch64__)

> > +grub_err_t

> > +grub_efi_get_ram_base(grub_addr_t *base_addr)

> > +{

> > +  grub_efi_memory_descriptor_t *memory_map;

> > +  grub_efi_memory_descriptor_t *desc;

> 

>      grub_efi_memory_descriptor_t *desc, *memory_map;

> 

> > +  grub_efi_uintn_t mmap_size;

> > +  grub_efi_uintn_t desc_size;

> 

>      grub_efi_uintn_t desc_size, mmap_size;

> 

> > +  int ret;

> > +

> > +  mmap_size = grub_efi_find_mmap_size();

> > +

> > +  memory_map = grub_malloc (mmap_size);

> > +  if (! memory_map)

> > +    return GRUB_ERR_OUT_OF_MEMORY;

> > +  ret = grub_efi_get_memory_map (&mmap_size, memory_map, NULL,

> > +				 &desc_size, NULL);

> > +

> > +  if (ret < 1)

> > +    return GRUB_ERR_BUG;

> > +

> > +  for (desc = memory_map, *base_addr = GRUB_UINT_MAX;

> > +       (grub_addr_t) desc < ((grub_addr_t) memory_map + mmap_size);

> > +       desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size))

> > +    {

> > +      if (desc->attribute & GRUB_EFI_MEMORY_WB)

>            *base_addr = grub_min (*base_addr, desc->physical_start);

> 

> > +	if (desc->physical_start < *base_addr)

> > +	  *base_addr = desc->physical_start;

> > +    }

> 

> And then you can drop these curly brackets.


Sure, I'll fold these changes into v4.

/
    Leif

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

Patch

diff --git a/grub-core/kern/efi/mm.c b/grub-core/kern/efi/mm.c
index fd39d23b4..10ffa2c9b 100644
--- a/grub-core/kern/efi/mm.c
+++ b/grub-core/kern/efi/mm.c
@@ -629,3 +629,39 @@  grub_efi_mm_init (void)
   grub_efi_free_pages ((grub_addr_t) memory_map,
 		       2 * BYTES_TO_PAGES (MEMORY_MAP_SIZE));
 }
+
+#if defined (__aarch64__)
+grub_err_t
+grub_efi_get_ram_base(grub_addr_t *base_addr)
+{
+  grub_efi_memory_descriptor_t *memory_map;
+  grub_efi_memory_descriptor_t *desc;
+  grub_efi_uintn_t mmap_size;
+  grub_efi_uintn_t desc_size;
+  int ret;
+
+  mmap_size = grub_efi_find_mmap_size();
+
+  memory_map = grub_malloc (mmap_size);
+  if (! memory_map)
+    return GRUB_ERR_OUT_OF_MEMORY;
+  ret = grub_efi_get_memory_map (&mmap_size, memory_map, NULL,
+				 &desc_size, NULL);
+
+  if (ret < 1)
+    return GRUB_ERR_BUG;
+
+  for (desc = memory_map, *base_addr = GRUB_UINT_MAX;
+       (grub_addr_t) desc < ((grub_addr_t) memory_map + mmap_size);
+       desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size))
+    {
+      if (desc->attribute & GRUB_EFI_MEMORY_WB)
+	if (desc->physical_start < *base_addr)
+	  *base_addr = desc->physical_start;
+    }
+
+  grub_free(memory_map);
+
+  return GRUB_ERR_NONE;
+}
+#endif
diff --git a/include/grub/efi/efi.h b/include/grub/efi/efi.h
index 1021273c1..57db74b57 100644
--- a/include/grub/efi/efi.h
+++ b/include/grub/efi/efi.h
@@ -93,6 +93,9 @@  extern void (*EXPORT_VAR(grub_efi_net_config)) (grub_efi_handle_t hnd,
 #if defined(__arm__) || defined(__aarch64__)
 void *EXPORT_FUNC(grub_efi_get_firmware_fdt)(void);
 #endif
+#if defined(__aarch64__)
+grub_err_t EXPORT_FUNC(grub_efi_get_ram_base)(grub_addr_t *);
+#endif
 
 grub_addr_t grub_efi_modules_addr (void);