Source-Changes-HG archive

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

[src-draft/trunk]: src/sys/arch/evbarm/fdt Teach arm fdt kernel to use /chose...



details:   https://anonhg.NetBSD.org/src-all/rev/8aca40bfebf8
branches:  trunk
changeset: 932707:8aca40bfebf8
user:      Taylor R Campbell <riastradh%NetBSD.org@localhost>
date:      Mon May 11 02:12:09 2020 +0000

description:
Teach arm fdt kernel to use /chosen/netbsd,efirng data.

Feed it in as a separate random source, with zero entropy since this
is a best-effort fallback for devices we really don't know anything
about.

diffstat:

 sys/arch/evbarm/fdt/fdt_machdep.c |  71 +++++++++++++++++++++++++++++++++++++-
 1 files changed, 69 insertions(+), 2 deletions(-)

diffs (110 lines):

diff -r 4809cbfeb97d -r 8aca40bfebf8 sys/arch/evbarm/fdt/fdt_machdep.c
--- a/sys/arch/evbarm/fdt/fdt_machdep.c Mon May 11 02:11:09 2020 +0000
+++ b/sys/arch/evbarm/fdt/fdt_machdep.c Mon May 11 02:12:09 2020 +0000
@@ -65,6 +65,7 @@
 #include <sys/md5.h>
 #include <sys/pserialize.h>
 #include <sys/rnd.h>
+#include <sys/rndsource.h>
 
 #include <net/if.h>
 #include <net/if_dl.h>
@@ -119,7 +120,8 @@
 const uint8_t *fdt_addr_r __attribute__((__section__(".data")));
 
 static uint64_t initrd_start, initrd_end;
-static uint64_t rndseed_start, rndseed_end;
+static uint64_t rndseed_start, rndseed_end; /* our on-disk seed */
+static uint64_t efirng_start, efirng_end;   /* firmware's EFI RNG output */
 
 #include <libfdt.h>
 #include <dev/fdt/fdtvar.h>
@@ -451,6 +453,69 @@
        rnd_seed(rndseed, rndseed_size);
 }
 
+static void
+fdt_probe_efirng(uint64_t *pstart, uint64_t *pend)
+{
+       int chosen, len;
+       const void *start_data, *end_data;
+
+       *pstart = *pend = 0;
+       chosen = OF_finddevice("/chosen");
+       if (chosen < 0)
+               return;
+
+       start_data = fdtbus_get_prop(chosen, "netbsd,efirng-start", &len);
+       end_data = fdtbus_get_prop(chosen, "netbsd,efirng-end", NULL);
+       if (start_data == NULL || end_data == NULL)
+               return;
+
+       switch (len) {
+       case 4:
+               *pstart = be32dec(start_data);
+               *pend = be32dec(end_data);
+               break;
+       case 8:
+               *pstart = be64dec(start_data);
+               *pend = be64dec(end_data);
+               break;
+       default:
+               printf("Unsupported len %d for /chosen/efirng-start\n", len);
+               return;
+       }
+}
+
+static struct krndsource efirng_source;
+
+static void
+fdt_setup_efirng(void)
+{
+       const uint64_t efirng_size = efirng_end - efirng_start;
+       const paddr_t startpa = trunc_page(efirng_start);
+       const paddr_t endpa = round_page(efirng_end);
+       paddr_t pa;
+       vaddr_t va;
+       void *efirng;
+
+       if (efirng_size == 0)
+               return;
+
+       va = uvm_km_alloc(kernel_map, endpa - startpa, 0,
+           UVM_KMF_VAONLY | UVM_KMF_NOWAIT);
+       if (va == 0) {
+               printf("Failed to allocate VA for efirng\n");
+               return;
+       }
+       efirng = (void *)va;
+
+       for (pa = startpa; pa < endpa; pa += PAGE_SIZE, va += PAGE_SIZE)
+               pmap_kenter_pa(va, pa, VM_PROT_READ|VM_PROT_WRITE, 0);
+       pmap_update(pmap_kernel());
+
+       rnd_attach_source(&efirng_source, "efirng", RND_TYPE_RNG,
+           RND_FLAG_DEFAULT);
+       rnd_add_data(&efirng_source, efirng, efirng_size, 0);
+}
+
 #ifdef EFI_RUNTIME
 static void
 fdt_map_efi_runtime(const char *prop, enum arm_efirt_mem_type type)
@@ -579,8 +644,9 @@
        /* Parse ramdisk info */
        fdt_probe_initrd(&initrd_start, &initrd_end);
 
-       /* Parse rndseed */
+       /* Parse our on-disk rndseed and the firmware's RNG from EFI */
        fdt_probe_rndseed(&rndseed_start, &rndseed_end);
+       fdt_probe_efirng(&efirng_start, &efirng_end);
 
        /*
         * Populate bootconfig structure for the benefit of
@@ -699,6 +765,7 @@
        fdtbus_intr_init();
 
        fdt_setup_rndseed();
+       fdt_setup_efirng();
 }
 
 void



Home | Main Index | Thread Index | Old Index