Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/stand/efiboot Pass a hash of the MBR and the booted part...



details:   https://anonhg.NetBSD.org/src/rev/7dac83851ec5
branches:  trunk
changeset: 366143:7dac83851ec5
user:      jmcneill <jmcneill%NetBSD.org@localhost>
date:      Mon Aug 27 09:51:32 2018 +0000

description:
Pass a hash of the MBR and the booted partition to the kernel.

diffstat:

 sys/stand/efiboot/efiblock.c |  27 ++++++++++++++++++++++++---
 sys/stand/efiboot/efiblock.h |   4 +++-
 sys/stand/efiboot/efifdt.c   |  31 +++++++++++++++++++++++++++++--
 sys/stand/efiboot/exec.c     |   5 ++---
 4 files changed, 58 insertions(+), 9 deletions(-)

diffs (183 lines):

diff -r 0c0a4814e635 -r 7dac83851ec5 sys/stand/efiboot/efiblock.c
--- a/sys/stand/efiboot/efiblock.c      Mon Aug 27 08:53:19 2018 +0000
+++ b/sys/stand/efiboot/efiblock.c      Mon Aug 27 09:51:32 2018 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: efiblock.c,v 1.1 2018/08/26 21:28:18 jmcneill Exp $ */
+/* $NetBSD: efiblock.c,v 1.2 2018/08/27 09:51:32 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2016 Kimihiro Nonaka <nonaka%netbsd.org@localhost>
@@ -30,12 +30,14 @@
 #define FSTYPENAMES
 
 #include <sys/param.h>
+#include <sys/md5.h>
 
 #include "efiboot.h"
 #include "efiblock.h"
 
 static EFI_HANDLE *efi_block;
 static UINTN efi_nblock;
+static struct efi_block_part *efi_block_booted = NULL;
 
 static TAILQ_HEAD(, efi_block_dev) efi_block_devs = TAILQ_HEAD_INITIALIZER(efi_block_devs);
 
@@ -85,8 +87,18 @@
        return ENOENT;
 }
 
+static void
+efi_block_generate_hash_mbr(struct efi_block_part *bpart, struct mbr_sector *mbr)
+{
+       MD5_CTX md5ctx;
+
+       MD5Init(&md5ctx);
+       MD5Update(&md5ctx, (void *)mbr, sizeof(*mbr));
+       MD5Final(bpart->hash, &md5ctx);
+}
+
 static int
-efi_block_find_partitions_disklabel(struct efi_block_dev *bdev, uint32_t start, uint32_t size)
+efi_block_find_partitions_disklabel(struct efi_block_dev *bdev, struct mbr_sector *mbr, uint32_t start, uint32_t size)
 {
        struct efi_block_part *bpart;
        struct disklabel d;
@@ -133,6 +145,7 @@
                bpart->type = EFI_BLOCK_PART_DISKLABEL;
                bpart->disklabel.secsize = le32toh(d.d_secsize);
                bpart->disklabel.part = *p;
+               efi_block_generate_hash_mbr(bpart, mbr);
                TAILQ_INSERT_TAIL(&bdev->partitions, bpart, entries);
        }
 
@@ -171,7 +184,7 @@
                if (le32toh(mbr_part->mbrp_size) == 0)
                        continue;
                if (mbr_part->mbrp_type == MBR_PTYPE_NETBSD) {
-                       efi_block_find_partitions_disklabel(bdev, le32toh(mbr_part->mbrp_start), le32toh(mbr_part->mbrp_size));
+                       efi_block_find_partitions_disklabel(bdev, &mbr, le32toh(mbr_part->mbrp_start), le32toh(mbr_part->mbrp_size));
                        break;
                }
        }
@@ -278,6 +291,12 @@
        }
 }
 
+struct efi_block_part *
+efi_block_boot_part(void)
+{
+       return efi_block_booted;
+}
+
 int
 efi_block_open(struct open_file *f, ...)
 {
@@ -309,6 +328,8 @@
 
        *file = path;
 
+       efi_block_booted = bpart;
+
        return 0;
 }
 
diff -r 0c0a4814e635 -r 7dac83851ec5 sys/stand/efiboot/efiblock.h
--- a/sys/stand/efiboot/efiblock.h      Mon Aug 27 08:53:19 2018 +0000
+++ b/sys/stand/efiboot/efiblock.h      Mon Aug 27 09:51:32 2018 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: efiblock.h,v 1.1 2018/08/26 21:28:18 jmcneill Exp $ */
+/* $NetBSD: efiblock.h,v 1.2 2018/08/27 09:51:32 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2018 Jared McNeill <jmcneill%invisible.ca@localhost>
@@ -59,12 +59,14 @@
        union {
                struct efi_block_part_disklabel disklabel;
        };
+       uint8_t hash[16];
 
        TAILQ_ENTRY(efi_block_part) entries;
 };
 
 void efi_block_probe(void);
 void efi_block_show(void);
+struct efi_block_part *efi_block_boot_part(void);
 
 int efi_block_open(struct open_file *, ...);
 int efi_block_close(struct open_file *);
diff -r 0c0a4814e635 -r 7dac83851ec5 sys/stand/efiboot/efifdt.c
--- a/sys/stand/efiboot/efifdt.c        Mon Aug 27 08:53:19 2018 +0000
+++ b/sys/stand/efiboot/efifdt.c        Mon Aug 27 09:51:32 2018 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: efifdt.c,v 1.2 2018/08/24 23:20:41 jmcneill Exp $ */
+/* $NetBSD: efifdt.c,v 1.3 2018/08/27 09:51:32 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2018 Jared McNeill <jmcneill%invisible.ca@localhost>
@@ -28,6 +28,7 @@
 
 #include "efiboot.h"
 #include "efifdt.h"
+#include "efiblock.h"
 
 #include <libfdt.h>
 
@@ -38,6 +39,7 @@
 #define        FDT_MEMORY_NODE_PATH    "/memory"
 #define        FDT_MEMORY_NODE_NAME    "memory"
 #define        FDT_CHOSEN_NODE_PATH    "/chosen"
+#define        FDT_CHOSEN_NODE_NAME    "chosen"
 
 #define        FDT_MEMORY_USABLE(_md)  \
        ((_md)->Type == EfiLoaderCode || (_md)->Type == EfiLoaderData || \
@@ -140,5 +142,30 @@
 void
 efi_fdt_bootargs(const char *bootargs)
 {
-       fdt_setprop_string(fdt_data, fdt_path_offset(fdt_data, FDT_CHOSEN_NODE_PATH), "bootargs", bootargs);
+       struct efi_block_part *bpart = efi_block_boot_part();
+       int chosen;
+
+       chosen = fdt_path_offset(fdt_data, FDT_CHOSEN_NODE_PATH);
+       if (chosen < 0)
+               chosen = fdt_add_subnode(fdt_data, fdt_path_offset(fdt_data, "/"), FDT_CHOSEN_NODE_NAME);
+       if (chosen < 0)
+               panic("FDT: Failed to craete " FDT_CHOSEN_NODE_PATH " node");
+
+       if (*bootargs)
+               fdt_setprop_string(fdt_data, chosen, "bootargs", bootargs);
+
+       if (bpart) {
+               switch (bpart->type) {
+               case EFI_BLOCK_PART_DISKLABEL:
+                       fdt_setprop(fdt_data, chosen, "netbsd,mbr",
+                           bpart->hash, sizeof(bpart->hash));
+                       fdt_setprop_u32(fdt_data, chosen, "netbsd,partition",
+                           bpart->index);
+                       break;
+               default:
+                       break;
+               }
+       }
+
+       fdt_pack(fdt_data);
 }
diff -r 0c0a4814e635 -r 7dac83851ec5 sys/stand/efiboot/exec.c
--- a/sys/stand/efiboot/exec.c  Mon Aug 27 08:53:19 2018 +0000
+++ b/sys/stand/efiboot/exec.c  Mon Aug 27 09:51:32 2018 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: exec.c,v 1.1 2018/08/24 02:01:06 jmcneill Exp $ */
+/* $NetBSD: exec.c,v 1.2 2018/08/27 09:51:32 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2018 Jared McNeill <jmcneill%invisible.ca@localhost>
@@ -74,8 +74,7 @@
        close(fd);
 
        if (efi_fdt_size() > 0) {
-               if (args && *args)
-                       efi_fdt_bootargs(args);
+               efi_fdt_bootargs(args);
                efi_fdt_memory_map();   
        }
 



Home | Main Index | Thread Index | Old Index