| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "devicetree.h" |
| 4 |
#include "proto/dt-fixup.h" |
| 5 |
#include "util.h" |
| 6 |
|
| 7 |
#define FDT_V1_SIZE (7*4) |
| 8 |
|
| 9 |
static EFI_STATUS devicetree_allocate(struct devicetree_state *state, size_t size) { |
| 10 |
size_t pages = DIV_ROUND_UP(size, EFI_PAGE_SIZE); |
| 11 |
EFI_STATUS err; |
| 12 |
|
| 13 |
assert(state); |
| 14 |
|
| 15 |
err = BS->AllocatePages(AllocateAnyPages, EfiACPIReclaimMemory, pages, &state->addr); |
| 16 |
if (err != EFI_SUCCESS) |
| 17 |
return err; |
| 18 |
|
| 19 |
state->pages = pages; |
| 20 |
return err; |
| 21 |
} |
| 22 |
|
| 23 |
static size_t devicetree_allocated(const struct devicetree_state *state) { |
| 24 |
assert(state); |
| 25 |
return state->pages * EFI_PAGE_SIZE; |
| 26 |
} |
| 27 |
|
| 28 |
static EFI_STATUS devicetree_fixup(struct devicetree_state *state, size_t len) { |
| 29 |
EFI_DT_FIXUP_PROTOCOL *fixup; |
| 30 |
size_t size; |
| 31 |
EFI_STATUS err; |
| 32 |
|
| 33 |
assert(state); |
| 34 |
|
| 35 |
err = BS->LocateProtocol(MAKE_GUID_PTR(EFI_DT_FIXUP_PROTOCOL), NULL, (void **) &fixup); |
| 36 |
/* Skip fixup if we cannot locate device tree fixup protocol */ |
| 37 |
if (err != EFI_SUCCESS) |
| 38 |
return EFI_SUCCESS; |
| 39 |
|
| 40 |
size = devicetree_allocated(state); |
| 41 |
err = fixup->Fixup(fixup, PHYSICAL_ADDRESS_TO_POINTER(state->addr), &size, |
| 42 |
EFI_DT_APPLY_FIXUPS | EFI_DT_RESERVE_MEMORY); |
| 43 |
if (err == EFI_BUFFER_TOO_SMALL) { |
| 44 |
EFI_PHYSICAL_ADDRESS oldaddr = state->addr; |
| 45 |
size_t oldpages = state->pages; |
| 46 |
void *oldptr = PHYSICAL_ADDRESS_TO_POINTER(state->addr); |
| 47 |
|
| 48 |
err = devicetree_allocate(state, size); |
| 49 |
if (err != EFI_SUCCESS) |
| 50 |
return err; |
| 51 |
|
| 52 |
memcpy(PHYSICAL_ADDRESS_TO_POINTER(state->addr), oldptr, len); |
| 53 |
err = BS->FreePages(oldaddr, oldpages); |
| 54 |
if (err != EFI_SUCCESS) |
| 55 |
return err; |
| 56 |
|
| 57 |
size = devicetree_allocated(state); |
| 58 |
err = fixup->Fixup(fixup, PHYSICAL_ADDRESS_TO_POINTER(state->addr), &size, |
| 59 |
EFI_DT_APPLY_FIXUPS | EFI_DT_RESERVE_MEMORY); |
| 60 |
} |
| 61 |
|
| 62 |
return err; |
| 63 |
} |
| 64 |
|
| 65 |
EFI_STATUS devicetree_install(struct devicetree_state *state, EFI_FILE *root_dir, char16_t *name) { |
| 66 |
_cleanup_file_close_ EFI_FILE *handle = NULL; |
| 67 |
_cleanup_free_ EFI_FILE_INFO *info = NULL; |
| 68 |
size_t len; |
| 69 |
EFI_STATUS err; |
| 70 |
|
| 71 |
assert(state); |
| 72 |
assert(root_dir); |
| 73 |
assert(name); |
| 74 |
|
| 75 |
/* Capture the original value for the devicetree table. NULL is not an error in this case so we don't |
| 76 |
* need to check the return value. NULL simply means the system fw had no devicetree initially (and |
| 77 |
* is the correct value to use to return to the initial state if needed). */ |
| 78 |
state->orig = find_configuration_table(MAKE_GUID_PTR(EFI_DTB_TABLE)); |
| 79 |
|
| 80 |
err = root_dir->Open(root_dir, &handle, name, EFI_FILE_MODE_READ, EFI_FILE_READ_ONLY); |
| 81 |
if (err != EFI_SUCCESS) |
| 82 |
return err; |
| 83 |
|
| 84 |
err = get_file_info(handle, &info, NULL); |
| 85 |
if (err != EFI_SUCCESS) |
| 86 |
return err; |
| 87 |
if (info->FileSize < FDT_V1_SIZE || info->FileSize > 32 * 1024 * 1024) |
| 88 |
/* 32MB device tree blob doesn't seem right */ |
| 89 |
return EFI_INVALID_PARAMETER; |
| 90 |
|
| 91 |
len = info->FileSize; |
| 92 |
|
| 93 |
err = devicetree_allocate(state, len); |
| 94 |
if (err != EFI_SUCCESS) |
| 95 |
return err; |
| 96 |
|
| 97 |
err = handle->Read(handle, &len, PHYSICAL_ADDRESS_TO_POINTER(state->addr)); |
| 98 |
if (err != EFI_SUCCESS) |
| 99 |
return err; |
| 100 |
|
| 101 |
err = devicetree_fixup(state, len); |
| 102 |
if (err != EFI_SUCCESS) |
| 103 |
return err; |
| 104 |
|
| 105 |
return BS->InstallConfigurationTable( |
| 106 |
MAKE_GUID_PTR(EFI_DTB_TABLE), PHYSICAL_ADDRESS_TO_POINTER(state->addr)); |
| 107 |
} |
| 108 |
|
| 109 |
static const char* devicetree_get_compatible(const void *dtb) { |
| 110 |
if ((uintptr_t) dtb % alignof(FdtHeader) != 0) |
| 111 |
return NULL; |
| 112 |
|
| 113 |
const FdtHeader *dt_header = ASSERT_PTR(dtb); |
| 114 |
|
| 115 |
if (be32toh(dt_header->magic) != UINT32_C(0xd00dfeed)) |
| 116 |
return NULL; |
| 117 |
|
| 118 |
uint32_t dt_size = be32toh(dt_header->total_size); |
| 119 |
uint32_t struct_off = be32toh(dt_header->off_dt_struct); |
| 120 |
uint32_t struct_size = be32toh(dt_header->size_dt_struct); |
| 121 |
uint32_t strings_off = be32toh(dt_header->off_dt_strings); |
| 122 |
uint32_t strings_size = be32toh(dt_header->size_dt_strings); |
| 123 |
uint32_t end; |
| 124 |
|
| 125 |
if (PTR_TO_SIZE(dtb) > SIZE_MAX - dt_size) |
| 126 |
return NULL; |
| 127 |
|
| 128 |
if (!ADD_SAFE(&end, strings_off, strings_size) || end > dt_size) |
| 129 |
return NULL; |
| 130 |
const char *strings_block = (const char *) ((const uint8_t *) dt_header + strings_off); |
| 131 |
|
| 132 |
if (struct_off % sizeof(uint32_t) != 0) |
| 133 |
return NULL; |
| 134 |
|
| 135 |
if (struct_size % sizeof(uint32_t) != 0 || |
| 136 |
!ADD_SAFE(&end, struct_off, struct_size) || |
| 137 |
end > strings_off) |
| 138 |
return NULL; |
| 139 |
const uint32_t *cursor = (const uint32_t *) ((const uint8_t *) dt_header + struct_off); |
| 140 |
|
| 141 |
size_t size_words = struct_size / sizeof(uint32_t); |
| 142 |
size_t len, name_off, len_words, s; |
| 143 |
|
| 144 |
for (size_t i = 0; i < size_words; i++) { |
| 145 |
switch (be32toh(cursor[i])) { |
| 146 |
case FDT_BEGIN_NODE: |
| 147 |
if (i + 1 >= size_words || cursor[++i] != 0) |
| 148 |
return NULL; |
| 149 |
break; |
| 150 |
case FDT_NOP: |
| 151 |
break; |
| 152 |
case FDT_PROP: |
| 153 |
/* At least 3 words should present: len, name_off, c (nul-terminated string always has non-zero length) */ |
| 154 |
if (i + 3 >= size_words) |
| 155 |
return NULL; |
| 156 |
len = be32toh(cursor[++i]); |
| 157 |
name_off = be32toh(cursor[++i]); |
| 158 |
len_words = DIV_ROUND_UP(len, sizeof(uint32_t)); |
| 159 |
|
| 160 |
if (ADD_SAFE(&s, name_off, STRLEN("compatible")) && |
| 161 |
s < strings_size && streq8(strings_block + name_off, "compatible")) { |
| 162 |
const char *c = (const char *) &cursor[++i]; |
| 163 |
if (len == 0 || i + len_words > size_words || c[len - 1] != '\0') |
| 164 |
c = NULL; |
| 165 |
|
| 166 |
return c; |
| 167 |
} |
| 168 |
i += len_words; |
| 169 |
break; |
| 170 |
default: |
| 171 |
return NULL; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
return NULL; |
| 176 |
} |
| 177 |
|
| 178 |
bool firmware_devicetree_exists(void) { |
| 179 |
return !!find_configuration_table(MAKE_GUID_PTR(EFI_DTB_TABLE)); |
| 180 |
} |
| 181 |
|
| 182 |
/* This function checks if the firmware provided DeviceTree |
| 183 |
* and a UKI provided DeviceTree contain the same first entry |
| 184 |
* on their respective "compatible" fields (which usually defines |
| 185 |
* the actual device model). More specifically, given the FW/UKI |
| 186 |
* "compatible" property pair: |
| 187 |
* |
| 188 |
* compatible = "string1", "string2"; |
| 189 |
* compatible = "string1", "string3"; |
| 190 |
* |
| 191 |
* the function reports a match, while for |
| 192 |
* |
| 193 |
* compatible = "string1", "string3"; |
| 194 |
* compatible = "string2", "string1"; |
| 195 |
* |
| 196 |
* it reports a mismatch. |
| 197 |
* |
| 198 |
* Other entries might refer to SoC and therefore can't be used for matching |
| 199 |
*/ |
| 200 |
EFI_STATUS devicetree_match(const void *uki_dtb, size_t uki_dtb_length) { |
| 201 |
const void *fw_dtb = find_configuration_table(MAKE_GUID_PTR(EFI_DTB_TABLE)); |
| 202 |
if (!fw_dtb) |
| 203 |
return EFI_UNSUPPORTED; |
| 204 |
|
| 205 |
const char *fw_compat = devicetree_get_compatible(fw_dtb); |
| 206 |
if (!fw_compat) |
| 207 |
return EFI_UNSUPPORTED; |
| 208 |
|
| 209 |
return devicetree_match_by_compatible(uki_dtb, uki_dtb_length, fw_compat); |
| 210 |
} |
| 211 |
|
| 212 |
EFI_STATUS devicetree_match_by_compatible(const void *uki_dtb, size_t uki_dtb_length, const char *compat) { |
| 213 |
if ((uintptr_t) uki_dtb % alignof(FdtHeader) != 0) |
| 214 |
return EFI_INVALID_PARAMETER; |
| 215 |
|
| 216 |
const FdtHeader *dt_header = ASSERT_PTR(uki_dtb); |
| 217 |
|
| 218 |
if (uki_dtb_length < sizeof(FdtHeader) || |
| 219 |
uki_dtb_length < be32toh(dt_header->total_size)) |
| 220 |
return EFI_INVALID_PARAMETER; |
| 221 |
|
| 222 |
if (!compat) |
| 223 |
return EFI_INVALID_PARAMETER; |
| 224 |
|
| 225 |
const char *dt_compat = devicetree_get_compatible(uki_dtb); |
| 226 |
if (!dt_compat) |
| 227 |
return EFI_INVALID_PARAMETER; |
| 228 |
|
| 229 |
/* Only matches the first compatible string from each DT */ |
| 230 |
return streq8(dt_compat, compat) ? EFI_SUCCESS : EFI_NOT_FOUND; |
| 231 |
} |
| 232 |
|
| 233 |
EFI_STATUS devicetree_install_from_memory( |
| 234 |
struct devicetree_state *state, const void *dtb_buffer, size_t dtb_length) { |
| 235 |
|
| 236 |
EFI_STATUS err; |
| 237 |
|
| 238 |
assert(state); |
| 239 |
assert(dtb_buffer && dtb_length > 0); |
| 240 |
|
| 241 |
/* Capture the original value for the devicetree table. NULL is not an error in this case so we don't |
| 242 |
* need to check the return value. NULL simply means the system fw had no devicetree initially (and |
| 243 |
* is the correct value to use to return to the initial state if needed). */ |
| 244 |
state->orig = find_configuration_table(MAKE_GUID_PTR(EFI_DTB_TABLE)); |
| 245 |
|
| 246 |
err = devicetree_allocate(state, dtb_length); |
| 247 |
if (err != EFI_SUCCESS) |
| 248 |
return err; |
| 249 |
|
| 250 |
memcpy(PHYSICAL_ADDRESS_TO_POINTER(state->addr), dtb_buffer, dtb_length); |
| 251 |
|
| 252 |
err = devicetree_fixup(state, dtb_length); |
| 253 |
if (err != EFI_SUCCESS) |
| 254 |
return err; |
| 255 |
|
| 256 |
return BS->InstallConfigurationTable( |
| 257 |
MAKE_GUID_PTR(EFI_DTB_TABLE), PHYSICAL_ADDRESS_TO_POINTER(state->addr)); |
| 258 |
} |
| 259 |
|
| 260 |
void devicetree_cleanup(struct devicetree_state *state) { |
| 261 |
EFI_STATUS err; |
| 262 |
|
| 263 |
if (!state->pages) |
| 264 |
return; |
| 265 |
|
| 266 |
err = BS->InstallConfigurationTable(MAKE_GUID_PTR(EFI_DTB_TABLE), state->orig); |
| 267 |
/* don't free the current device tree if we can't reinstate the old one */ |
| 268 |
if (err != EFI_SUCCESS) |
| 269 |
return; |
| 270 |
|
| 271 |
BS->FreePages(state->addr, state->pages); |
| 272 |
state->pages = 0; |
| 273 |
} |