Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

efi/arm: Rewrite FDT param discovery routines

The efi_get_fdt_params() routine uses the early OF device tree
traversal helpers, that iterate over each node in the DT and invoke
a caller provided callback that can inspect the node's contents and
look for the required data. This requires a special param struct to
be passed around, with pointers into param enumeration structs that
contain (and duplicate) property names and offsets into yet another
struct that carries the collected data.

Since we know the data we look for is either under /hypervisor/uefi
or under /chosen, it is much simpler to use the libfdt routines, and
just try to grab a reference to either node directly, and read each
property in sequence.

Signed-off-by: Ard Biesheuvel <ardb@kernel.org>

+91 -123
+91 -123
drivers/firmware/efi/fdtparams.c
··· 5 5 #include <linux/module.h> 6 6 #include <linux/init.h> 7 7 #include <linux/efi.h> 8 - #include <linux/of.h> 8 + #include <linux/libfdt.h> 9 9 #include <linux/of_fdt.h> 10 10 11 - #include <asm/early_ioremap.h> 11 + #include <asm/unaligned.h> 12 12 13 - #define UEFI_PARAM(name, prop, field) \ 14 - { \ 15 - { name }, \ 16 - { prop }, \ 17 - offsetof(struct efi_fdt_params, field), \ 18 - sizeof_field(struct efi_fdt_params, field) \ 19 - } 13 + enum { 14 + SYSTAB, 15 + MMBASE, 16 + MMSIZE, 17 + DCSIZE, 18 + DCVERS, 20 19 21 - struct efi_fdt_params { 22 - u64 system_table; 23 - u64 mmap; 24 - u32 mmap_size; 25 - u32 desc_size; 26 - u32 desc_ver; 20 + PARAMCOUNT 27 21 }; 28 22 29 - struct params { 30 - const char name[32]; 31 - const char propname[32]; 32 - int offset; 33 - int size; 23 + static __initconst const char name[][22] = { 24 + [SYSTAB] = "System Table ", 25 + [MMBASE] = "MemMap Address ", 26 + [MMSIZE] = "MemMap Size ", 27 + [DCSIZE] = "MemMap Desc. Size ", 28 + [DCVERS] = "MemMap Desc. Version ", 34 29 }; 35 30 36 - static __initdata struct params fdt_params[] = { 37 - UEFI_PARAM("System Table", "linux,uefi-system-table", system_table), 38 - UEFI_PARAM("MemMap Address", "linux,uefi-mmap-start", mmap), 39 - UEFI_PARAM("MemMap Size", "linux,uefi-mmap-size", mmap_size), 40 - UEFI_PARAM("MemMap Desc. Size", "linux,uefi-mmap-desc-size", desc_size), 41 - UEFI_PARAM("MemMap Desc. Version", "linux,uefi-mmap-desc-ver", desc_ver) 42 - }; 43 - 44 - static __initdata struct params xen_fdt_params[] = { 45 - UEFI_PARAM("System Table", "xen,uefi-system-table", system_table), 46 - UEFI_PARAM("MemMap Address", "xen,uefi-mmap-start", mmap), 47 - UEFI_PARAM("MemMap Size", "xen,uefi-mmap-size", mmap_size), 48 - UEFI_PARAM("MemMap Desc. Size", "xen,uefi-mmap-desc-size", desc_size), 49 - UEFI_PARAM("MemMap Desc. Version", "xen,uefi-mmap-desc-ver", desc_ver) 50 - }; 51 - 52 - #define EFI_FDT_PARAMS_SIZE ARRAY_SIZE(fdt_params) 53 - 54 - static __initdata struct { 55 - const char *uname; 56 - const char *subnode; 57 - struct params *params; 31 + static __initconst const struct { 32 + const char path[17]; 33 + const char params[PARAMCOUNT][26]; 58 34 } dt_params[] = { 59 - { "hypervisor", "uefi", xen_fdt_params }, 60 - { "chosen", NULL, fdt_params }, 35 + { 36 + #ifdef CONFIG_XEN // <-------17------> 37 + .path = "/hypervisor/uefi", 38 + .params = { 39 + [SYSTAB] = "xen,uefi-system-table", 40 + [MMBASE] = "xen,uefi-mmap-start", 41 + [MMSIZE] = "xen,uefi-mmap-size", 42 + [DCSIZE] = "xen,uefi-mmap-desc-size", 43 + [DCVERS] = "xen,uefi-mmap-desc-ver", 44 + } 45 + }, { 46 + #endif 47 + .path = "/chosen", 48 + .params = { // <-----------26-----------> 49 + [SYSTAB] = "linux,uefi-system-table", 50 + [MMBASE] = "linux,uefi-mmap-start", 51 + [MMSIZE] = "linux,uefi-mmap-size", 52 + [DCSIZE] = "linux,uefi-mmap-desc-size", 53 + [DCVERS] = "linux,uefi-mmap-desc-ver", 54 + } 55 + } 61 56 }; 62 57 63 - struct param_info { 64 - int found; 65 - void *params; 66 - const char *missing; 67 - }; 68 - 69 - static int __init __find_uefi_params(unsigned long node, 70 - struct param_info *info, 71 - struct params *params) 58 + static int __init efi_get_fdt_prop(const void *fdt, int node, const char *pname, 59 + const char *rname, void *var, int size) 72 60 { 73 61 const void *prop; 74 - void *dest; 62 + int len; 75 63 u64 val; 76 - int i, len; 77 64 78 - for (i = 0; i < EFI_FDT_PARAMS_SIZE; i++) { 79 - prop = of_get_flat_dt_prop(node, params[i].propname, &len); 80 - if (!prop) { 81 - info->missing = params[i].name; 82 - return 0; 83 - } 65 + prop = fdt_getprop(fdt, node, pname, &len); 66 + if (!prop) 67 + return 1; 84 68 85 - dest = info->params + params[i].offset; 86 - info->found++; 69 + val = (len == 4) ? (u64)be32_to_cpup(prop) : get_unaligned_be64(prop); 87 70 88 - val = of_read_number(prop, len / sizeof(u32)); 71 + if (size == 8) 72 + *(u64 *)var = val; 73 + else 74 + *(u32 *)var = (val < U32_MAX) ? val : U32_MAX; // saturate 89 75 90 - if (params[i].size == sizeof(u32)) 91 - *(u32 *)dest = val; 92 - else 93 - *(u64 *)dest = val; 94 - 95 - if (efi_enabled(EFI_DBG)) 96 - pr_info(" %s: 0x%0*llx\n", params[i].name, 97 - params[i].size * 2, val); 98 - } 99 - 100 - return 1; 101 - } 102 - 103 - static int __init fdt_find_uefi_params(unsigned long node, const char *uname, 104 - int depth, void *data) 105 - { 106 - struct param_info *info = data; 107 - int i; 108 - 109 - for (i = 0; i < ARRAY_SIZE(dt_params); i++) { 110 - const char *subnode = dt_params[i].subnode; 111 - 112 - if (depth != 1 || strcmp(uname, dt_params[i].uname) != 0) { 113 - info->missing = dt_params[i].params[0].name; 114 - continue; 115 - } 116 - 117 - if (subnode) { 118 - int err = of_get_flat_dt_subnode_by_name(node, subnode); 119 - 120 - if (err < 0) 121 - return 0; 122 - 123 - node = err; 124 - } 125 - 126 - return __find_uefi_params(node, info, dt_params[i].params); 127 - } 76 + if (efi_enabled(EFI_DBG)) 77 + pr_info(" %s: 0x%0*llx\n", rname, size * 2, val); 128 78 129 79 return 0; 130 80 } 131 81 132 - u64 __init efi_get_fdt_params(struct efi_memory_map_data *memmap) 82 + u64 __init efi_get_fdt_params(struct efi_memory_map_data *mm) 133 83 { 134 - struct efi_fdt_params params; 135 - struct param_info info; 136 - int ret; 84 + const void *fdt = initial_boot_params; 85 + unsigned long systab; 86 + int i, j, node; 87 + struct { 88 + void *var; 89 + int size; 90 + } target[] = { 91 + [SYSTAB] = { &systab, sizeof(systab) }, 92 + [MMBASE] = { &mm->phys_map, sizeof(mm->phys_map) }, 93 + [MMSIZE] = { &mm->size, sizeof(mm->size) }, 94 + [DCSIZE] = { &mm->desc_size, sizeof(mm->desc_size) }, 95 + [DCVERS] = { &mm->desc_version, sizeof(mm->desc_version) }, 96 + }; 137 97 138 - pr_info("Getting EFI parameters from FDT:\n"); 98 + BUILD_BUG_ON(ARRAY_SIZE(target) != ARRAY_SIZE(name)); 99 + BUILD_BUG_ON(ARRAY_SIZE(target) != ARRAY_SIZE(dt_params[0].params)); 139 100 140 - info.found = 0; 141 - info.params = &params; 101 + for (i = 0; i < ARRAY_SIZE(dt_params); i++) { 102 + node = fdt_path_offset(fdt, dt_params[i].path); 103 + if (node < 0) 104 + continue; 142 105 143 - ret = of_scan_flat_dt(fdt_find_uefi_params, &info); 144 - if (!info.found) { 145 - pr_info("UEFI not found.\n"); 146 - return 0; 147 - } else if (!ret) { 148 - pr_err("Can't find '%s' in device tree!\n", info.missing); 149 - return 0; 106 + if (efi_enabled(EFI_DBG)) 107 + pr_info("Getting UEFI parameters from %s in DT:\n", 108 + dt_params[i].path); 109 + 110 + for (j = 0; j < ARRAY_SIZE(target); j++) { 111 + const char *pname = dt_params[i].params[j]; 112 + 113 + if (!efi_get_fdt_prop(fdt, node, pname, name[j], 114 + target[j].var, target[j].size)) 115 + continue; 116 + if (!j) 117 + goto notfound; 118 + pr_err("Can't find property '%s' in DT!\n", pname); 119 + return 0; 120 + } 121 + return systab; 150 122 } 151 - 152 - memmap->desc_version = params.desc_ver; 153 - memmap->desc_size = params.desc_size; 154 - memmap->size = params.mmap_size; 155 - memmap->phys_map = params.mmap; 156 - 157 - return params.system_table; 123 + notfound: 124 + pr_info("UEFI not found.\n"); 125 + return 0; 158 126 }