^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * FDT related Helper functions used by the EFI stub on multiple
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * architectures. This should be #included by the EFI stub
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * implementation files.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright 2013 Linaro Limited; author Roy Franz
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/efi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/libfdt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <asm/efi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include "efistub.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define EFI_DT_ADDR_CELLS_DEFAULT 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define EFI_DT_SIZE_CELLS_DEFAULT 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) static void fdt_update_cell_size(void *fdt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) int offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) offset = fdt_path_offset(fdt, "/");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) /* Set the #address-cells and #size-cells values for an empty tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) fdt_setprop_u32(fdt, offset, "#address-cells", EFI_DT_ADDR_CELLS_DEFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) fdt_setprop_u32(fdt, offset, "#size-cells", EFI_DT_SIZE_CELLS_DEFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) static efi_status_t update_fdt(void *orig_fdt, unsigned long orig_fdt_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) void *fdt, int new_fdt_size, char *cmdline_ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) u64 initrd_addr, u64 initrd_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) int node, num_rsv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) u32 fdt_val32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) u64 fdt_val64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) /* Do some checks on provided FDT, if it exists: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) if (orig_fdt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) if (fdt_check_header(orig_fdt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) efi_err("Device Tree header not valid!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) return EFI_LOAD_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * We don't get the size of the FDT if we get if from a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * configuration table:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) if (orig_fdt_size && fdt_totalsize(orig_fdt) > orig_fdt_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) efi_err("Truncated device tree! foo!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) return EFI_LOAD_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (orig_fdt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) status = fdt_open_into(orig_fdt, fdt, new_fdt_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) status = fdt_create_empty_tree(fdt, new_fdt_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if (status == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * Any failure from the following function is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * non-critical:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) fdt_update_cell_size(fdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (status != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) goto fdt_set_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * Delete all memory reserve map entries. When booting via UEFI,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * kernel will use the UEFI memory map to find reserved regions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) num_rsv = fdt_num_mem_rsv(fdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) while (num_rsv-- > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) fdt_del_mem_rsv(fdt, num_rsv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) node = fdt_subnode_offset(fdt, 0, "chosen");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) if (node < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) node = fdt_add_subnode(fdt, 0, "chosen");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (node < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) /* 'node' is an error code when negative: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) status = node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) goto fdt_set_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (cmdline_ptr != NULL && strlen(cmdline_ptr) > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) status = fdt_setprop(fdt, node, "bootargs", cmdline_ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) strlen(cmdline_ptr) + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) goto fdt_set_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) /* Set initrd address/end in device tree, if present */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (initrd_size != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) u64 initrd_image_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) u64 initrd_image_start = cpu_to_fdt64(initrd_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) status = fdt_setprop_var(fdt, node, "linux,initrd-start", initrd_image_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) goto fdt_set_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) initrd_image_end = cpu_to_fdt64(initrd_addr + initrd_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) status = fdt_setprop_var(fdt, node, "linux,initrd-end", initrd_image_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) goto fdt_set_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) /* Add FDT entries for EFI runtime services in chosen node. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) node = fdt_subnode_offset(fdt, 0, "chosen");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) fdt_val64 = cpu_to_fdt64((u64)(unsigned long)efi_system_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) status = fdt_setprop_var(fdt, node, "linux,uefi-system-table", fdt_val64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) goto fdt_set_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) fdt_val64 = U64_MAX; /* placeholder */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) status = fdt_setprop_var(fdt, node, "linux,uefi-mmap-start", fdt_val64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) goto fdt_set_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) fdt_val32 = U32_MAX; /* placeholder */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) status = fdt_setprop_var(fdt, node, "linux,uefi-mmap-size", fdt_val32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) goto fdt_set_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) status = fdt_setprop_var(fdt, node, "linux,uefi-mmap-desc-size", fdt_val32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) goto fdt_set_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) status = fdt_setprop_var(fdt, node, "linux,uefi-mmap-desc-ver", fdt_val32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) goto fdt_set_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && !efi_nokaslr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) efi_status_t efi_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) efi_status = efi_get_random_bytes(sizeof(fdt_val64),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) (u8 *)&fdt_val64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) if (efi_status == EFI_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) status = fdt_setprop_var(fdt, node, "kaslr-seed", fdt_val64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) goto fdt_set_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) /* Shrink the FDT back to its minimum size: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) fdt_pack(fdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return EFI_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) fdt_set_fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) if (status == -FDT_ERR_NOSPACE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) return EFI_BUFFER_TOO_SMALL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) return EFI_LOAD_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) static efi_status_t update_fdt_memmap(void *fdt, struct efi_boot_memmap *map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) int node = fdt_path_offset(fdt, "/chosen");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) u64 fdt_val64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) u32 fdt_val32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if (node < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) return EFI_LOAD_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) fdt_val64 = cpu_to_fdt64((unsigned long)*map->map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) err = fdt_setprop_inplace_var(fdt, node, "linux,uefi-mmap-start", fdt_val64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) return EFI_LOAD_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) fdt_val32 = cpu_to_fdt32(*map->map_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) err = fdt_setprop_inplace_var(fdt, node, "linux,uefi-mmap-size", fdt_val32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) return EFI_LOAD_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) fdt_val32 = cpu_to_fdt32(*map->desc_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) err = fdt_setprop_inplace_var(fdt, node, "linux,uefi-mmap-desc-size", fdt_val32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) return EFI_LOAD_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) fdt_val32 = cpu_to_fdt32(*map->desc_ver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) err = fdt_setprop_inplace_var(fdt, node, "linux,uefi-mmap-desc-ver", fdt_val32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) return EFI_LOAD_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) return EFI_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) struct exit_boot_struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) efi_memory_desc_t *runtime_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) int *runtime_entry_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) void *new_fdt_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) static efi_status_t exit_boot_func(struct efi_boot_memmap *map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) void *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) struct exit_boot_struct *p = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * Update the memory map with virtual addresses. The function will also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) * populate @runtime_map with copies of just the EFI_MEMORY_RUNTIME
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) * entries so that we can pass it straight to SetVirtualAddressMap()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) efi_get_virtmap(*map->map, *map->map_size, *map->desc_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) p->runtime_map, p->runtime_entry_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) return update_fdt_memmap(p->new_fdt_addr, map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) #ifndef MAX_FDT_SIZE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) # define MAX_FDT_SIZE SZ_2M
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) * Allocate memory for a new FDT, then add EFI, commandline, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) * initrd related fields to the FDT. This routine increases the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) * FDT allocation size until the allocated memory is large
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) * enough. EFI allocations are in EFI_PAGE_SIZE granules,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) * which are fixed at 4K bytes, so in most cases the first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) * allocation should succeed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) * EFI boot services are exited at the end of this function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) * There must be no allocations between the get_memory_map()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) * call and the exit_boot_services() call, so the exiting of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) * boot services is very tightly tied to the creation of the FDT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) * with the final memory map in it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) efi_status_t allocate_new_fdt_and_exit_boot(void *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) unsigned long *new_fdt_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) unsigned long max_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) u64 initrd_addr, u64 initrd_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) char *cmdline_ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) unsigned long fdt_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) unsigned long fdt_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) unsigned long map_size, desc_size, buff_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) u32 desc_ver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) unsigned long mmap_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) efi_memory_desc_t *memory_map, *runtime_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) efi_status_t status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) int runtime_entry_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) struct efi_boot_memmap map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) struct exit_boot_struct priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) map.map = &runtime_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) map.map_size = &map_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) map.desc_size = &desc_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) map.desc_ver = &desc_ver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) map.key_ptr = &mmap_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) map.buff_size = &buff_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) * Get a copy of the current memory map that we will use to prepare
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) * the input for SetVirtualAddressMap(). We don't have to worry about
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) * subsequent allocations adding entries, since they could not affect
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) * the number of EFI_MEMORY_RUNTIME regions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) status = efi_get_memory_map(&map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) if (status != EFI_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) efi_err("Unable to retrieve UEFI memory map.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) efi_info("Exiting boot services and installing virtual address map...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) map.map = &memory_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) status = efi_allocate_pages(MAX_FDT_SIZE, new_fdt_addr, max_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) if (status != EFI_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) efi_err("Unable to allocate memory for new device tree.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) * Now that we have done our final memory allocation (and free)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) * we can get the memory map key needed for exit_boot_services().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) status = efi_get_memory_map(&map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) if (status != EFI_SUCCESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) goto fail_free_new_fdt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) status = update_fdt((void *)fdt_addr, fdt_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) (void *)*new_fdt_addr, MAX_FDT_SIZE, cmdline_ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) initrd_addr, initrd_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) if (status != EFI_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) efi_err("Unable to construct new device tree.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) goto fail_free_new_fdt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) runtime_entry_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) priv.runtime_map = runtime_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) priv.runtime_entry_count = &runtime_entry_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) priv.new_fdt_addr = (void *)*new_fdt_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) status = efi_exit_boot_services(handle, &map, &priv, exit_boot_func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) if (status == EFI_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) efi_set_virtual_address_map_t *svam;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) if (efi_novamap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) return EFI_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) /* Install the new virtual address map */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) svam = efi_system_table->runtime->set_virtual_address_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) status = svam(runtime_entry_count * desc_size, desc_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) desc_ver, runtime_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) * We are beyond the point of no return here, so if the call to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) * SetVirtualAddressMap() failed, we need to signal that to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) * incoming kernel but proceed normally otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) if (status != EFI_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) int l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) * Set the virtual address field of all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) * EFI_MEMORY_RUNTIME entries to 0. This will signal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) * the incoming kernel that no virtual translation has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) * been installed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) for (l = 0; l < map_size; l += desc_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) efi_memory_desc_t *p = (void *)memory_map + l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) if (p->attribute & EFI_MEMORY_RUNTIME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) p->virt_addr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) return EFI_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) efi_err("Exit boot services failed.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) fail_free_new_fdt:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) efi_free(MAX_FDT_SIZE, *new_fdt_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) efi_system_table->boottime->free_pool(runtime_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) return EFI_LOAD_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) void *get_fdt(unsigned long *fdt_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) void *fdt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) fdt = get_efi_config_table(DEVICE_TREE_GUID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) if (!fdt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) if (fdt_check_header(fdt) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) efi_err("Invalid header detected on UEFI supplied FDT, ignoring ...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) *fdt_size = fdt_totalsize(fdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) return fdt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) }