Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | // SPDX-License-Identifier: GPL-2.0+ /* * efi_selftest_fdt * * Copyright (c) 2018 Heinrich Schuchardt <xypron.glpk@gmx.de> * Copyright (c) 2022 Ventana Micro Systems Inc * * Check the device tree, test the RISCV_EFI_BOOT_PROTOCOL. */ #include <efi_riscv.h> #include <efi_selftest.h> #include <linux/libfdt.h> static const struct efi_system_table *systemtab; static const struct efi_boot_services *boottime; static const char *fdt; /* This should be sufficient for */ #define BUFFERSIZE 0x100000 static const efi_guid_t fdt_guid = EFI_FDT_GUID; static const efi_guid_t acpi_guid = EFI_ACPI_TABLE_GUID; static const efi_guid_t riscv_efi_boot_protocol_guid = RISCV_EFI_BOOT_PROTOCOL_GUID; /** * f2h() - convert FDT value to host endianness. * * UEFI code is always low endian. The FDT is big endian. * * @val: FDT value * Return: converted value */ static uint32_t f2h(fdt32_t val) { char *buf = (char *)&val; char i; /* Swap the bytes */ i = buf[0]; buf[0] = buf[3]; buf[3] = i; i = buf[1]; buf[1] = buf[2]; buf[2] = i; return val; } /** * get_property() - return value of a property of an FDT node * * A property of the root node or one of its direct children can be * retrieved. * * @property name of the property * @node name of the node or NULL for root node * Return: value of the property */ static char *get_property(const u16 *property, const u16 *node) { struct fdt_header *header = (struct fdt_header *)fdt; const fdt32_t *end; const fdt32_t *pos; const char *strings; size_t level = 0; const char *nodelabel = NULL; if (!header) { efi_st_error("Missing device tree\n"); return NULL; } if (f2h(header->magic) != FDT_MAGIC) { efi_st_error("Wrong device tree magic\n"); return NULL; } pos = (fdt32_t *)(fdt + f2h(header->off_dt_struct)); end = &pos[f2h(header->totalsize) >> 2]; strings = fdt + f2h(header->off_dt_strings); for (; pos < end;) { switch (f2h(pos[0])) { case FDT_BEGIN_NODE: { const char *c = (char *)&pos[1]; size_t i; if (level == 1) nodelabel = c; ++level; for (i = 0; c[i]; ++i) ; pos = &pos[2 + (i >> 2)]; break; } case FDT_PROP: { struct fdt_property *prop = (struct fdt_property *)pos; const char *label = &strings[f2h(prop->nameoff)]; efi_status_t ret; /* Check if this is the property to be returned */ if (!efi_st_strcmp_16_8(property, label) && ((level == 1 && !node) || (level == 2 && node && !efi_st_strcmp_16_8(node, nodelabel)))) { char *str; efi_uintn_t len = f2h(prop->len); if (!len) return NULL; /* * The string might not be 0 terminated. * It is safer to make a copy. */ ret = boottime->allocate_pool( EFI_LOADER_DATA, len + 1, (void **)&str); if (ret != EFI_SUCCESS) { efi_st_error("AllocatePool failed\n"); return NULL; } boottime->copy_mem(str, &pos[3], len); str[len] = 0; return str; } pos = &pos[3 + ((f2h(prop->len) + 3) >> 2)]; break; } case FDT_NOP: ++pos; break; case FDT_END_NODE: --level; ++pos; break; case FDT_END: return NULL; default: efi_st_error("Invalid device tree token\n"); return NULL; } } efi_st_error("Missing FDT_END token\n"); return NULL; } /* * Setup unit test. * * @handle: handle of the loaded image * @systable: system table * Return: EFI_ST_SUCCESS for success */ static int setup(const efi_handle_t img_handle, const struct efi_system_table *systable) { void *acpi; systemtab = systable; boottime = systable->boottime; acpi = efi_st_get_config_table(&acpi_guid); fdt = efi_st_get_config_table(&fdt_guid); if (!fdt) { efi_st_error("Missing device tree\n"); return EFI_ST_FAILURE; } if (acpi) { efi_st_error("Found ACPI table and device tree\n"); return EFI_ST_FAILURE; } return EFI_ST_SUCCESS; } __maybe_unused static efi_status_t get_boot_hartid(efi_uintn_t *efi_hartid) { efi_status_t ret; struct riscv_efi_boot_protocol *prot; /* Get RISC-V boot protocol */ ret = boottime->locate_protocol(&riscv_efi_boot_protocol_guid, NULL, (void **)&prot); if (ret != EFI_SUCCESS) { efi_st_error("RISC-V Boot Protocol not available\n"); return EFI_ST_FAILURE; } /* Get boot hart ID from EFI protocol */ ret = prot->get_boot_hartid(prot, efi_hartid); if (ret != EFI_SUCCESS) { efi_st_error("Could not retrieve boot hart ID\n"); return EFI_ST_FAILURE; } return EFI_ST_SUCCESS; } /* * Execute unit test. * * Return: EFI_ST_SUCCESS for success */ static int execute(void) { char *str; efi_status_t ret; str = get_property(u"compatible", NULL); if (str) { efi_st_printf("compatible: %s\n", str); ret = boottime->free_pool(str); if (ret != EFI_SUCCESS) { efi_st_error("FreePool failed\n"); return EFI_ST_FAILURE; } } else { efi_st_error("Missing property 'compatible'\n"); return EFI_ST_FAILURE; } str = get_property(u"serial-number", NULL); if (str) { efi_st_printf("serial-number: %s\n", str); ret = boottime->free_pool(str); if (ret != EFI_SUCCESS) { efi_st_error("FreePool failed\n"); return EFI_ST_FAILURE; } } if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL_MEASURE_DTB)) { str = get_property(u"kaslr-seed", u"chosen"); if (str) { efi_st_error("kaslr-seed with measured fdt\n"); return EFI_ST_FAILURE; } } if (IS_ENABLED(CONFIG_RISCV)) { u32 fdt_hartid; str = get_property(u"boot-hartid", u"chosen"); if (!str) { efi_st_error("boot-hartid missing in devicetree\n"); return EFI_ST_FAILURE; } fdt_hartid = f2h(*(fdt32_t *)str); efi_st_printf("boot-hartid: %u\n", fdt_hartid); ret = boottime->free_pool(str); if (ret != EFI_SUCCESS) { efi_st_error("FreePool failed\n"); return EFI_ST_FAILURE; } if (IS_ENABLED(CONFIG_EFI_RISCV_BOOT_PROTOCOL)) { efi_uintn_t efi_hartid; int r; r = get_boot_hartid(&efi_hartid); if (r != EFI_ST_SUCCESS) return r; /* Boot hart ID should be same */ if (efi_hartid != fdt_hartid) { efi_st_error("boot-hartid differs: prot 0x%p, DT 0x%.8x\n", (void *)(uintptr_t)efi_hartid, fdt_hartid); return EFI_ST_FAILURE; } } } return EFI_ST_SUCCESS; } EFI_UNIT_TEST(fdt) = { .name = "device tree", .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT, .setup = setup, .execute = execute, }; |