Skip to contentSkip to conversation

loader/efi/fdt: Ensure FDT blob is 8-byte aligned

PATCH In review [email protected] 3 participants 5 messages Feb 25 – Feb 26

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 loader/efi/fdt: Ensure FDT blob is 8-byte aligned

From: Mikhail Lappo <[email protected]>

libfdt requires the FDT blob to be 8-byte aligned. The alignment is validated in fdt_check_header(), and misaligned blobs may be rejected or cause failures on architectures that enforce strict alignment.

Currently the blob is allocated with grub_malloc(), which does not guarantee 8-byte alignment. Replace it with grub_memalign(8, size) to ensure the required alignment.

---
 grub-core/loader/efi/fdt.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
grub-core/loader/efi/fdt.c +2 -1
index e510b3491..f25d362d3 100644
@@ -38,6 +38,7 @@ static void *fdt;
 #define FDT_ADDR_SIZE_EXTRA ((2 * grub_fdt_prop_entry_size (sizeof(grub_uint32_t))) + \
                              sizeof (FDT_ADDR_CELLS_STRING) + \
                              sizeof (FDT_SIZE_CELLS_STRING))
+#define FDT_BLOB_ALIGNMENT 8
 
 static const struct grub_arg_option options_fdtdump[] = {
   {"prop",	'p', 0, N_("Get property."), N_("prop"), ARG_TYPE_STRING},
@@ -145,7 +146,7 @@ grub_cmd_devicetree (grub_command_t cmd __attribute__ ((unused)),
     goto out;
 
   size = grub_file_size (dtb);
-  blob = grub_malloc (size);
+  blob = grub_memalign (FDT_BLOB_ALIGNMENT, size);
   if (!blob)
     goto out;
 
Signature · 7 lines
2.44.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#1 lore raw

Add maintainers into CC

Quoted 39 lines · On Wed, Feb 25, 2026 at 2:07 PM <[email protected]> wrote:

From: Mikhail Lappo <[email protected]>

libfdt requires the FDT blob to be 8-byte aligned. The alignment is validated in fdt_check_header(), and misaligned blobs may be rejected or cause failures on architectures that enforce strict alignment.

Currently the blob is allocated with grub_malloc(), which does not guarantee 8-byte alignment. Replace it with grub_memalign(8, size) to ensure the required alignment.

---
 grub-core/loader/efi/fdt.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
grub-core/loader/efi/fdt.c +1 -0
index e510b3491..f25d362d3 100644
@@ -38,6 +38,7 @@ static void *fdt;
 #define FDT_ADDR_SIZE_EXTRA ((2 * grub_fdt_prop_entry_size
 (sizeof(grub_uint32_t))) + \
                              sizeof (FDT_ADDR_CELLS_STRING) + \
                              sizeof (FDT_SIZE_CELLS_STRING))
+#define FDT_BLOB_ALIGNMENT 8
 
 static const struct grub_arg_option options_fdtdump[] = {
   {"prop",     'p', 0, N_("Get property."), N_("prop"), ARG_TYPE_STRING},
grub-core/loader/efi/fdt.c +1 -2
@@ -145,7 +146,7 @@ grub_cmd_devicetree (grub_command_t cmd __attribute__
 ((unused)),
     goto out;
 
   size = grub_file_size (dtb);
-  blob = grub_malloc (size);
+  blob = grub_memalign (FDT_BLOB_ALIGNMENT, size);
   if (!blob)
     goto out;
 
--
Quoted 3 lines

2.44.0

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#2 lore raw
Quoted 38 lines · Mikle Lappo

Add maintainers into CC

On Wed, Feb 25, 2026 at 2:07 PM <[email protected] <mailto:[email protected]>> wrote:

    From: Mikhail Lappo <[email protected] <mailto:[email protected]>>

    libfdt requires the FDT blob to be 8-byte aligned. The alignment is
    validated in fdt_check_header(), and misaligned blobs may be rejected
    or cause failures on architectures that enforce strict alignment.

    Currently the blob is allocated with grub_malloc(), which does not
    guarantee 8-byte alignment. Replace it with grub_memalign(8, size)
    to ensure the required alignment.

    Signed-off-by: Mikhail Lappo <[email protected]
    <mailto:[email protected]>>
    ---
      grub-core/loader/efi/fdt.c | 3 ++-
      1 file changed, 2 insertions(+), 1 deletion(-)

    diff --git a/grub-core/loader/efi/fdt.c b/grub-core/loader/efi/fdt.c
    index e510b3491..f25d362d3 100644
    --- a/grub-core/loader/efi/fdt.c
    +++ b/grub-core/loader/efi/fdt.c
    @@ -38,6 +38,7 @@ static void *fdt;
      #define FDT_ADDR_SIZE_EXTRA ((2 * grub_fdt_prop_entry_size
    (sizeof(grub_uint32_t))) + \
                                   sizeof (FDT_ADDR_CELLS_STRING) + \
                                   sizeof (FDT_SIZE_CELLS_STRING))
    +#define FDT_BLOB_ALIGNMENT 8

      static const struct grub_arg_option options_fdtdump[] = {
        {"prop",     'p', 0, N_("Get property."), N_("prop"),
    ARG_TYPE_STRING},
    @@ -145,7 +146,7 @@ grub_cmd_devicetree (grub_command_t cmd
    __attribute__ ((unused)),
          goto out;
Quoted 9 lines
        size = grub_file_size (dtb);
    -  blob = grub_malloc (size);
    +  blob = grub_memalign (FDT_BLOB_ALIGNMENT, size);
        if (!blob)
          goto out;

    -- 
    2.44.0

Isn't #define GRUB_MM_ALIGN_LOG2 setting a minimum alignment of 16 bytes on 32-bit and 32 bytes on 64-bit systems?

Please, have a look at the code starting here:

grub-core/kern/mm.c:478: align = (align >> GRUB_MM_ALIGN_LOG2);

Best regards

Heinrich

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#3 lore raw

Good morning Heinrich,

thanks for pointing this out, I overlooked it.
I enabled GRUB2 logs and I also see that DTB is loaded aligned.

devicetree <target_name>.dtb
kern/disk.c:196:disk: Opening tftp,128.0.0.1'... disk/efi/efidisk.c:482:efidisk: opening tftp kern/disk.c:281:disk: Opening tftp,128.0.0.1' failed. kern/disk.c:299:disk: Closing `tftp'. tpm/grub_tpm.c:95:tpm: extending pcr#15 from file type 44 ??mmands/efi/tpm.c:281:tpm: log_event, pcr = 15, size = 0x43b3, ? kern/verifiers.c:88:verify: file: <target_name>.dtb type: 44 commands/efi/tpm.c:281:tpm: log_event, pcr = 9, size = 0x43b3, <target_name>.dtb
loader/efi/linux.c:689:linux: filename <target_name>.dtb, size 17331 loader/efi/linux.c:649:loader: load_dtbloader/efi/linux.c:654:loader:
Loading
device tree to 0x6fbbd600

Still I get FDT_ERR_ALIGNMENT in fdt_check_header when booting... Unfortunately my Grub2 comes as binary from BSP and I can't recompile nor really debug it.

I'll update my CR if I find anything or close it if not.

Best regards,
Mikhail

Quoted 54 lines · Heinrich Schuchardt
Quoted 6 lines · Mikle Lappo

Add maintainers into CC

On Wed, Feb 25, 2026 at 2:07 PM <[email protected] <mailto:[email protected]>> wrote:

From: Mikhail Lappo <[email protected] <mailto:

[email protected]>>

Quoted 33 lines
    libfdt requires the FDT blob to be 8-byte aligned. The alignment is
    validated in fdt_check_header(), and misaligned blobs may be rejected
    or cause failures on architectures that enforce strict alignment.

    Currently the blob is allocated with grub_malloc(), which does not
    guarantee 8-byte alignment. Replace it with grub_memalign(8, size)
    to ensure the required alignment.

    Signed-off-by: Mikhail Lappo <[email protected]
    <mailto:[email protected]>>
    ---
      grub-core/loader/efi/fdt.c | 3 ++-
      1 file changed, 2 insertions(+), 1 deletion(-)

    diff --git a/grub-core/loader/efi/fdt.c b/grub-core/loader/efi/fdt.c
    index e510b3491..f25d362d3 100644
    --- a/grub-core/loader/efi/fdt.c
    +++ b/grub-core/loader/efi/fdt.c
    @@ -38,6 +38,7 @@ static void *fdt;
      #define FDT_ADDR_SIZE_EXTRA ((2 * grub_fdt_prop_entry_size
    (sizeof(grub_uint32_t))) + \
                                   sizeof (FDT_ADDR_CELLS_STRING) + \
                                   sizeof (FDT_SIZE_CELLS_STRING))
    +#define FDT_BLOB_ALIGNMENT 8

      static const struct grub_arg_option options_fdtdump[] = {
        {"prop",     'p', 0, N_("Get property."), N_("prop"),
    ARG_TYPE_STRING},
    @@ -145,7 +146,7 @@ grub_cmd_devicetree (grub_command_t cmd
    __attribute__ ((unused)),
          goto out;
Quoted 9 lines
        size = grub_file_size (dtb);
    -  blob = grub_malloc (size);
    +  blob = grub_memalign (FDT_BLOB_ALIGNMENT, size);
        if (!blob)
          goto out;

    --
    2.44.0

Isn't #define GRUB_MM_ALIGN_LOG2 setting a minimum alignment of 16 bytes on 32-bit and 32 bytes on 64-bit systems?

Quoted 8 lines

Please, have a look at the code starting here:

grub-core/kern/mm.c:478: align = (align >> GRUB_MM_ALIGN_LOG2);

Best regards

Heinrich

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

Reviewed-by on v1 1/1
#4 lore raw
In reply to [email protected]

Regards
Vladimir 'phcoder' Serbinenko

Quoted 8 lines · Le mer. 25 févr. 2026, 16:48, <[email protected]> a écrit :

From: Mikhail Lappo <[email protected]>

libfdt requires the FDT blob to be 8-byte aligned. The alignment is validated in fdt_check_header(), and misaligned blobs may be rejected or cause failures on architectures that enforce strict alignment.

Currently the blob is allocated with grub_malloc(), which does not guarantee 8-byte alignment.

Actually it does. But I agree that explicit alignment is better. Or did you experience malloc returning anything less than 16-byte aligned?

Quoted 35 lines

Replace it with grub_memalign(8, size)
to ensure the required alignment.

---
 grub-core/loader/efi/fdt.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
grub-core/loader/efi/fdt.c +1 -0
index e510b3491..f25d362d3 100644
@@ -38,6 +38,7 @@ static void *fdt;
 #define FDT_ADDR_SIZE_EXTRA ((2 * grub_fdt_prop_entry_size
 (sizeof(grub_uint32_t))) + \
                              sizeof (FDT_ADDR_CELLS_STRING) + \
                              sizeof (FDT_SIZE_CELLS_STRING))
+#define FDT_BLOB_ALIGNMENT 8
 
 static const struct grub_arg_option options_fdtdump[] = {
   {"prop",     'p', 0, N_("Get property."), N_("prop"), ARG_TYPE_STRING},
grub-core/loader/efi/fdt.c +1 -2
@@ -145,7 +146,7 @@ grub_cmd_devicetree (grub_command_t cmd __attribute__
 ((unused)),
     goto out;
 
   size = grub_file_size (dtb);
-  blob = grub_malloc (size);
+  blob = grub_memalign (FDT_BLOB_ALIGNMENT, size);
   if (!blob)
     goto out;
 
--

2.44.0

Quoted 5 lines

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel