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