Heinrich Schuchardt | 12083ba | 2020-11-04 22:00:48 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright 2020, Heinrich Schuchardt <xypron.glpk@gmx.de> |
| 4 | * |
| 5 | * dtbdump.efi saves the device tree provided as a configuration table |
| 6 | * to a file. |
| 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <efi_api.h> |
Heinrich Schuchardt | 4591079 | 2020-12-13 19:13:57 +0100 | [diff] [blame] | 11 | #include <efi_dt_fixup.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame^] | 12 | #include <linux/libfdt.h> |
Heinrich Schuchardt | 12083ba | 2020-11-04 22:00:48 +0100 | [diff] [blame] | 13 | |
| 14 | #define BUFFER_SIZE 64 |
| 15 | #define ESC 0x17 |
Heinrich Schuchardt | 4591079 | 2020-12-13 19:13:57 +0100 | [diff] [blame] | 16 | |
| 17 | #define efi_size_in_pages(size) ((size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT) |
Heinrich Schuchardt | 12083ba | 2020-11-04 22:00:48 +0100 | [diff] [blame] | 18 | |
| 19 | static struct efi_simple_text_output_protocol *cerr; |
| 20 | static struct efi_simple_text_output_protocol *cout; |
| 21 | static struct efi_simple_text_input_protocol *cin; |
| 22 | static struct efi_boot_services *bs; |
| 23 | static const efi_guid_t fdt_guid = EFI_FDT_GUID; |
| 24 | static const efi_guid_t loaded_image_guid = EFI_LOADED_IMAGE_PROTOCOL_GUID; |
| 25 | static const efi_guid_t guid_simple_file_system_protocol = |
| 26 | EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID; |
Heinrich Schuchardt | 4591079 | 2020-12-13 19:13:57 +0100 | [diff] [blame] | 27 | static efi_handle_t handle; |
| 28 | static struct efi_system_table *systable; |
| 29 | static const efi_guid_t efi_dt_fixup_protocol_guid = EFI_DT_FIXUP_PROTOCOL_GUID; |
| 30 | static const efi_guid_t efi_file_info_guid = EFI_FILE_INFO_GUID; |
| 31 | |
| 32 | /** |
Heinrich Schuchardt | 9d4c19e | 2021-01-19 10:06:00 +0100 | [diff] [blame] | 33 | * print() - print string |
| 34 | * |
| 35 | * @string: text |
| 36 | */ |
| 37 | static void print(u16 *string) |
| 38 | { |
| 39 | cout->output_string(cout, string); |
| 40 | } |
| 41 | |
| 42 | /** |
Heinrich Schuchardt | 4591079 | 2020-12-13 19:13:57 +0100 | [diff] [blame] | 43 | * error() - print error string |
| 44 | * |
| 45 | * @string: error text |
| 46 | */ |
| 47 | static void error(u16 *string) |
| 48 | { |
| 49 | cout->set_attribute(cout, EFI_LIGHTRED | EFI_BACKGROUND_BLACK); |
Heinrich Schuchardt | 9d4c19e | 2021-01-19 10:06:00 +0100 | [diff] [blame] | 50 | print(string); |
Heinrich Schuchardt | 4591079 | 2020-12-13 19:13:57 +0100 | [diff] [blame] | 51 | cout->set_attribute(cout, EFI_LIGHTBLUE | EFI_BACKGROUND_BLACK); |
| 52 | } |
Heinrich Schuchardt | 12083ba | 2020-11-04 22:00:48 +0100 | [diff] [blame] | 53 | |
| 54 | /** |
Heinrich Schuchardt | 2e88aad | 2021-01-17 05:13:21 +0100 | [diff] [blame] | 55 | * efi_input_yn() - get answer to yes/no question |
| 56 | * |
| 57 | * Return: |
| 58 | * y or Y |
| 59 | * EFI_SUCCESS |
| 60 | * n or N |
| 61 | * EFI_ACCESS_DENIED |
| 62 | * ESC |
| 63 | * EFI_ABORTED |
| 64 | */ |
| 65 | static efi_status_t efi_input_yn(void) |
| 66 | { |
| 67 | struct efi_input_key key = {0}; |
| 68 | efi_uintn_t index; |
| 69 | efi_status_t ret; |
| 70 | |
| 71 | /* Drain the console input */ |
| 72 | ret = cin->reset(cin, true); |
| 73 | for (;;) { |
| 74 | ret = bs->wait_for_event(1, &cin->wait_for_key, &index); |
| 75 | if (ret != EFI_SUCCESS) |
| 76 | continue; |
| 77 | ret = cin->read_key_stroke(cin, &key); |
| 78 | if (ret != EFI_SUCCESS) |
| 79 | continue; |
| 80 | switch (key.scan_code) { |
| 81 | case 0x17: /* Escape */ |
| 82 | return EFI_ABORTED; |
| 83 | default: |
| 84 | break; |
| 85 | } |
| 86 | /* Convert to lower case */ |
| 87 | switch (key.unicode_char | 0x20) { |
| 88 | case 'y': |
| 89 | return EFI_SUCCESS; |
| 90 | case 'n': |
| 91 | return EFI_ACCESS_DENIED; |
| 92 | default: |
| 93 | break; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * efi_input() - read string from console |
Heinrich Schuchardt | 12083ba | 2020-11-04 22:00:48 +0100 | [diff] [blame] | 100 | * |
| 101 | * @buffer: input buffer |
| 102 | * @buffer_size: buffer size |
| 103 | * Return: status code |
| 104 | */ |
| 105 | static efi_status_t efi_input(u16 *buffer, efi_uintn_t buffer_size) |
| 106 | { |
| 107 | struct efi_input_key key = {0}; |
| 108 | efi_uintn_t index; |
| 109 | efi_uintn_t pos = 0; |
| 110 | u16 outbuf[2] = L" "; |
| 111 | efi_status_t ret; |
| 112 | |
| 113 | /* Drain the console input */ |
| 114 | ret = cin->reset(cin, true); |
Heinrich Schuchardt | 4591079 | 2020-12-13 19:13:57 +0100 | [diff] [blame] | 115 | *buffer = 0; |
Heinrich Schuchardt | 12083ba | 2020-11-04 22:00:48 +0100 | [diff] [blame] | 116 | for (;;) { |
| 117 | ret = bs->wait_for_event(1, &cin->wait_for_key, &index); |
| 118 | if (ret != EFI_SUCCESS) |
| 119 | continue; |
| 120 | ret = cin->read_key_stroke(cin, &key); |
| 121 | if (ret != EFI_SUCCESS) |
| 122 | continue; |
| 123 | switch (key.scan_code) { |
| 124 | case 0x17: /* Escape */ |
Heinrich Schuchardt | 9d4c19e | 2021-01-19 10:06:00 +0100 | [diff] [blame] | 125 | print(L"\r\nAborted\r\n"); |
Heinrich Schuchardt | 12083ba | 2020-11-04 22:00:48 +0100 | [diff] [blame] | 126 | return EFI_ABORTED; |
| 127 | default: |
| 128 | break; |
| 129 | } |
| 130 | switch (key.unicode_char) { |
| 131 | case 0x08: /* Backspace */ |
| 132 | if (pos) { |
| 133 | buffer[pos--] = 0; |
Heinrich Schuchardt | 9d4c19e | 2021-01-19 10:06:00 +0100 | [diff] [blame] | 134 | print(L"\b \b"); |
Heinrich Schuchardt | 12083ba | 2020-11-04 22:00:48 +0100 | [diff] [blame] | 135 | } |
| 136 | break; |
| 137 | case 0x0a: /* Linefeed */ |
| 138 | case 0x0d: /* Carriage return */ |
Heinrich Schuchardt | 9d4c19e | 2021-01-19 10:06:00 +0100 | [diff] [blame] | 139 | print(L"\r\n"); |
Heinrich Schuchardt | 12083ba | 2020-11-04 22:00:48 +0100 | [diff] [blame] | 140 | return EFI_SUCCESS; |
| 141 | default: |
| 142 | break; |
| 143 | } |
| 144 | /* Ignore surrogate codes */ |
| 145 | if (key.unicode_char >= 0xD800 && key.unicode_char <= 0xDBFF) |
| 146 | continue; |
| 147 | if (key.unicode_char >= 0x20 && |
| 148 | pos < buffer_size - 1) { |
| 149 | *outbuf = key.unicode_char; |
| 150 | buffer[pos++] = key.unicode_char; |
Heinrich Schuchardt | 4591079 | 2020-12-13 19:13:57 +0100 | [diff] [blame] | 151 | buffer[pos] = 0; |
Heinrich Schuchardt | 9d4c19e | 2021-01-19 10:06:00 +0100 | [diff] [blame] | 152 | print(outbuf); |
Heinrich Schuchardt | 12083ba | 2020-11-04 22:00:48 +0100 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | * Convert FDT value to host endianness. |
| 159 | * |
| 160 | * @val FDT value |
| 161 | * @return converted value |
| 162 | */ |
| 163 | static u32 f2h(fdt32_t val) |
| 164 | { |
| 165 | char *buf = (char *)&val; |
| 166 | char i; |
| 167 | |
| 168 | /* Swap the bytes */ |
| 169 | i = buf[0]; buf[0] = buf[3]; buf[3] = i; |
| 170 | i = buf[1]; buf[1] = buf[2]; buf[2] = i; |
| 171 | return *(u32 *)buf; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * get_dtb() - get device tree |
| 176 | * |
| 177 | * @systable: system table |
| 178 | * Return: device tree or NULL |
| 179 | */ |
| 180 | void *get_dtb(struct efi_system_table *systable) |
| 181 | { |
| 182 | void *dtb = NULL; |
| 183 | efi_uintn_t i; |
| 184 | |
| 185 | for (i = 0; i < systable->nr_tables; ++i) { |
| 186 | if (!memcmp(&systable->tables[i].guid, &fdt_guid, |
| 187 | sizeof(efi_guid_t))) { |
| 188 | dtb = systable->tables[i].table; |
| 189 | break; |
| 190 | } |
| 191 | } |
| 192 | return dtb; |
| 193 | } |
| 194 | |
| 195 | /** |
Heinrich Schuchardt | 4591079 | 2020-12-13 19:13:57 +0100 | [diff] [blame] | 196 | * skip_whitespace() - skip over leading whitespace |
Heinrich Schuchardt | 12083ba | 2020-11-04 22:00:48 +0100 | [diff] [blame] | 197 | * |
Heinrich Schuchardt | 4591079 | 2020-12-13 19:13:57 +0100 | [diff] [blame] | 198 | * @pos: UTF-16 string |
| 199 | * Return: pointer to first non-whitespace |
Heinrich Schuchardt | 12083ba | 2020-11-04 22:00:48 +0100 | [diff] [blame] | 200 | */ |
Heinrich Schuchardt | 4591079 | 2020-12-13 19:13:57 +0100 | [diff] [blame] | 201 | u16 *skip_whitespace(u16 *pos) |
Heinrich Schuchardt | 12083ba | 2020-11-04 22:00:48 +0100 | [diff] [blame] | 202 | { |
Heinrich Schuchardt | 4591079 | 2020-12-13 19:13:57 +0100 | [diff] [blame] | 203 | for (; *pos && *pos <= 0x20; ++pos) |
| 204 | ; |
| 205 | return pos; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * starts_with() - check if @string starts with @keyword |
| 210 | * |
| 211 | * @string: string to search for keyword |
| 212 | * @keyword: keyword to be searched |
| 213 | * Return: true fi @string starts with the keyword |
| 214 | */ |
| 215 | bool starts_with(u16 *string, u16 *keyword) |
| 216 | { |
| 217 | for (; *keyword; ++string, ++keyword) { |
| 218 | if (*string != *keyword) |
| 219 | return false; |
| 220 | } |
| 221 | return true; |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * do_help() - print help |
| 226 | */ |
| 227 | void do_help(void) |
| 228 | { |
Heinrich Schuchardt | 9d4c19e | 2021-01-19 10:06:00 +0100 | [diff] [blame] | 229 | error(L"load <dtb> - load device-tree from file\r\n"); |
| 230 | error(L"save <dtb> - save device-tree to file\r\n"); |
| 231 | error(L"exit - exit the shell\r\n"); |
Heinrich Schuchardt | 4591079 | 2020-12-13 19:13:57 +0100 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /** |
| 235 | * do_load() - load and install device-tree |
| 236 | * |
| 237 | * @filename: file name |
| 238 | * Return: status code |
| 239 | */ |
| 240 | efi_status_t do_load(u16 *filename) |
| 241 | { |
| 242 | struct efi_dt_fixup_protocol *dt_fixup_prot; |
Heinrich Schuchardt | 12083ba | 2020-11-04 22:00:48 +0100 | [diff] [blame] | 243 | struct efi_loaded_image *loaded_image; |
| 244 | struct efi_simple_file_system_protocol *file_system; |
Heinrich Schuchardt | 4591079 | 2020-12-13 19:13:57 +0100 | [diff] [blame] | 245 | struct efi_file_handle *root = NULL, *file = NULL; |
| 246 | u64 addr = 0; |
| 247 | struct efi_file_info *info; |
Heinrich Schuchardt | 12083ba | 2020-11-04 22:00:48 +0100 | [diff] [blame] | 248 | struct fdt_header *dtb; |
Heinrich Schuchardt | 4591079 | 2020-12-13 19:13:57 +0100 | [diff] [blame] | 249 | efi_uintn_t buffer_size; |
| 250 | efi_uintn_t pages; |
| 251 | efi_status_t ret, ret2; |
Heinrich Schuchardt | 12083ba | 2020-11-04 22:00:48 +0100 | [diff] [blame] | 252 | |
Heinrich Schuchardt | 4591079 | 2020-12-13 19:13:57 +0100 | [diff] [blame] | 253 | ret = bs->locate_protocol(&efi_dt_fixup_protocol_guid, NULL, |
| 254 | (void **)&dt_fixup_prot); |
| 255 | if (ret != EFI_SUCCESS) { |
Heinrich Schuchardt | 9d4c19e | 2021-01-19 10:06:00 +0100 | [diff] [blame] | 256 | error(L"Device-tree fix-up protocol not found\r\n"); |
Heinrich Schuchardt | 4591079 | 2020-12-13 19:13:57 +0100 | [diff] [blame] | 257 | return ret; |
| 258 | } |
Heinrich Schuchardt | 12083ba | 2020-11-04 22:00:48 +0100 | [diff] [blame] | 259 | |
Heinrich Schuchardt | 4591079 | 2020-12-13 19:13:57 +0100 | [diff] [blame] | 260 | filename = skip_whitespace(filename); |
| 261 | |
| 262 | ret = bs->open_protocol(handle, &loaded_image_guid, |
| 263 | (void **)&loaded_image, NULL, NULL, |
| 264 | EFI_OPEN_PROTOCOL_GET_PROTOCOL); |
| 265 | if (ret != EFI_SUCCESS) { |
Heinrich Schuchardt | 9d4c19e | 2021-01-19 10:06:00 +0100 | [diff] [blame] | 266 | error(L"Loaded image protocol not found\r\n"); |
Heinrich Schuchardt | 4591079 | 2020-12-13 19:13:57 +0100 | [diff] [blame] | 267 | return ret; |
| 268 | } |
| 269 | /* Open the simple file system protocol */ |
| 270 | ret = bs->open_protocol(loaded_image->device_handle, |
| 271 | &guid_simple_file_system_protocol, |
| 272 | (void **)&file_system, NULL, NULL, |
| 273 | EFI_OPEN_PROTOCOL_GET_PROTOCOL); |
| 274 | if (ret != EFI_SUCCESS) { |
Heinrich Schuchardt | 9d4c19e | 2021-01-19 10:06:00 +0100 | [diff] [blame] | 275 | error(L"Failed to open simple file system protocol\r\n"); |
Heinrich Schuchardt | 4591079 | 2020-12-13 19:13:57 +0100 | [diff] [blame] | 276 | goto out; |
| 277 | } |
| 278 | |
| 279 | /* Open volume */ |
| 280 | ret = file_system->open_volume(file_system, &root); |
| 281 | if (ret != EFI_SUCCESS) { |
Heinrich Schuchardt | 9d4c19e | 2021-01-19 10:06:00 +0100 | [diff] [blame] | 282 | error(L"Failed to open volume\r\n"); |
Heinrich Schuchardt | 4591079 | 2020-12-13 19:13:57 +0100 | [diff] [blame] | 283 | goto out; |
| 284 | } |
| 285 | |
| 286 | /* Open file */ |
| 287 | ret = root->open(root, &file, filename, EFI_FILE_MODE_READ, 0); |
| 288 | if (ret != EFI_SUCCESS) { |
Heinrich Schuchardt | 9d4c19e | 2021-01-19 10:06:00 +0100 | [diff] [blame] | 289 | error(L"File not found\r\n"); |
Heinrich Schuchardt | 4591079 | 2020-12-13 19:13:57 +0100 | [diff] [blame] | 290 | goto out; |
| 291 | } |
| 292 | /* Get file size */ |
| 293 | buffer_size = 0; |
| 294 | ret = file->getinfo(file, &efi_file_info_guid, &buffer_size, NULL); |
| 295 | if (ret != EFI_BUFFER_TOO_SMALL) { |
Heinrich Schuchardt | 9d4c19e | 2021-01-19 10:06:00 +0100 | [diff] [blame] | 296 | error(L"Can't get file info size\r\n"); |
Heinrich Schuchardt | 4591079 | 2020-12-13 19:13:57 +0100 | [diff] [blame] | 297 | goto out; |
| 298 | } |
| 299 | ret = bs->allocate_pool(EFI_LOADER_DATA, buffer_size, (void **)&info); |
| 300 | if (ret != EFI_SUCCESS) { |
Heinrich Schuchardt | 9d4c19e | 2021-01-19 10:06:00 +0100 | [diff] [blame] | 301 | error(L"Out of memory\r\n"); |
Heinrich Schuchardt | 4591079 | 2020-12-13 19:13:57 +0100 | [diff] [blame] | 302 | goto out; |
| 303 | } |
| 304 | ret = file->getinfo(file, &efi_file_info_guid, &buffer_size, info); |
| 305 | if (ret != EFI_SUCCESS) { |
Heinrich Schuchardt | 9d4c19e | 2021-01-19 10:06:00 +0100 | [diff] [blame] | 306 | error(L"Can't get file info\r\n"); |
Heinrich Schuchardt | 4591079 | 2020-12-13 19:13:57 +0100 | [diff] [blame] | 307 | goto out; |
| 308 | } |
| 309 | buffer_size = info->file_size; |
| 310 | pages = efi_size_in_pages(buffer_size); |
| 311 | ret = bs->free_pool(info); |
| 312 | if (ret != EFI_SUCCESS) |
Heinrich Schuchardt | 9d4c19e | 2021-01-19 10:06:00 +0100 | [diff] [blame] | 313 | error(L"Can't free memory pool\r\n"); |
Heinrich Schuchardt | 4591079 | 2020-12-13 19:13:57 +0100 | [diff] [blame] | 314 | /* Read file */ |
| 315 | ret = bs->allocate_pages(EFI_ALLOCATE_ANY_PAGES, |
| 316 | EFI_ACPI_RECLAIM_MEMORY, |
| 317 | pages, &addr); |
| 318 | if (ret != EFI_SUCCESS) { |
Heinrich Schuchardt | 9d4c19e | 2021-01-19 10:06:00 +0100 | [diff] [blame] | 319 | error(L"Out of memory\r\n"); |
Heinrich Schuchardt | 4591079 | 2020-12-13 19:13:57 +0100 | [diff] [blame] | 320 | goto out; |
| 321 | } |
| 322 | dtb = (struct fdt_header *)(uintptr_t)addr; |
| 323 | ret = file->read(file, &buffer_size, dtb); |
| 324 | if (ret != EFI_SUCCESS) { |
Heinrich Schuchardt | 9d4c19e | 2021-01-19 10:06:00 +0100 | [diff] [blame] | 325 | error(L"Can't read file\r\n"); |
Heinrich Schuchardt | 4591079 | 2020-12-13 19:13:57 +0100 | [diff] [blame] | 326 | goto out; |
| 327 | } |
| 328 | /* Fixup file, expecting EFI_BUFFER_TOO_SMALL */ |
| 329 | ret = dt_fixup_prot->fixup(dt_fixup_prot, dtb, &buffer_size, |
| 330 | EFI_DT_APPLY_FIXUPS | EFI_DT_RESERVE_MEMORY | |
| 331 | EFI_DT_INSTALL_TABLE); |
| 332 | if (ret == EFI_BUFFER_TOO_SMALL) { |
| 333 | /* Read file into larger buffer */ |
| 334 | ret = bs->free_pages(addr, pages); |
| 335 | if (ret != EFI_SUCCESS) |
Heinrich Schuchardt | 9d4c19e | 2021-01-19 10:06:00 +0100 | [diff] [blame] | 336 | error(L"Can't free memory pages\r\n"); |
Heinrich Schuchardt | 4591079 | 2020-12-13 19:13:57 +0100 | [diff] [blame] | 337 | pages = efi_size_in_pages(buffer_size); |
| 338 | ret = bs->allocate_pages(EFI_ALLOCATE_ANY_PAGES, |
| 339 | EFI_ACPI_RECLAIM_MEMORY, |
| 340 | pages, &addr); |
| 341 | if (ret != EFI_SUCCESS) { |
Heinrich Schuchardt | 9d4c19e | 2021-01-19 10:06:00 +0100 | [diff] [blame] | 342 | error(L"Out of memory\r\n"); |
Heinrich Schuchardt | 4591079 | 2020-12-13 19:13:57 +0100 | [diff] [blame] | 343 | goto out; |
| 344 | } |
| 345 | dtb = (struct fdt_header *)(uintptr_t)addr; |
| 346 | ret = file->setpos(file, 0); |
| 347 | if (ret != EFI_SUCCESS) { |
Heinrich Schuchardt | 9d4c19e | 2021-01-19 10:06:00 +0100 | [diff] [blame] | 348 | error(L"Can't position file\r\n"); |
Heinrich Schuchardt | 4591079 | 2020-12-13 19:13:57 +0100 | [diff] [blame] | 349 | goto out; |
| 350 | } |
| 351 | ret = file->read(file, &buffer_size, dtb); |
| 352 | if (ret != EFI_SUCCESS) { |
Heinrich Schuchardt | 9d4c19e | 2021-01-19 10:06:00 +0100 | [diff] [blame] | 353 | error(L"Can't read file\r\n"); |
Heinrich Schuchardt | 4591079 | 2020-12-13 19:13:57 +0100 | [diff] [blame] | 354 | goto out; |
| 355 | } |
| 356 | buffer_size = pages << EFI_PAGE_SHIFT; |
| 357 | ret = dt_fixup_prot->fixup( |
| 358 | dt_fixup_prot, dtb, &buffer_size, |
| 359 | EFI_DT_APPLY_FIXUPS | EFI_DT_RESERVE_MEMORY | |
| 360 | EFI_DT_INSTALL_TABLE); |
| 361 | } |
| 362 | if (ret == EFI_SUCCESS) |
Heinrich Schuchardt | 9d4c19e | 2021-01-19 10:06:00 +0100 | [diff] [blame] | 363 | print(L"device-tree installed\r\n"); |
Heinrich Schuchardt | 4591079 | 2020-12-13 19:13:57 +0100 | [diff] [blame] | 364 | else |
Heinrich Schuchardt | 9d4c19e | 2021-01-19 10:06:00 +0100 | [diff] [blame] | 365 | error(L"Device-tree fix-up failed\r\n"); |
Heinrich Schuchardt | 4591079 | 2020-12-13 19:13:57 +0100 | [diff] [blame] | 366 | out: |
| 367 | if (addr) { |
| 368 | ret2 = bs->free_pages(addr, pages); |
| 369 | if (ret2 != EFI_SUCCESS) |
Heinrich Schuchardt | 9d4c19e | 2021-01-19 10:06:00 +0100 | [diff] [blame] | 370 | error(L"Can't free memory pages\r\n"); |
Heinrich Schuchardt | 4591079 | 2020-12-13 19:13:57 +0100 | [diff] [blame] | 371 | } |
| 372 | if (file) { |
| 373 | ret2 = file->close(file); |
| 374 | if (ret2 != EFI_SUCCESS) |
Heinrich Schuchardt | 9d4c19e | 2021-01-19 10:06:00 +0100 | [diff] [blame] | 375 | error(L"Can't close file\r\n"); |
Heinrich Schuchardt | 4591079 | 2020-12-13 19:13:57 +0100 | [diff] [blame] | 376 | } |
| 377 | if (root) { |
| 378 | ret2 = root->close(root); |
| 379 | if (ret2 != EFI_SUCCESS) |
Heinrich Schuchardt | 9d4c19e | 2021-01-19 10:06:00 +0100 | [diff] [blame] | 380 | error(L"Can't close volume\r\n"); |
Heinrich Schuchardt | 4591079 | 2020-12-13 19:13:57 +0100 | [diff] [blame] | 381 | } |
| 382 | return ret; |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * do_save() - save current device-tree |
| 387 | * |
| 388 | * @filename: file name |
| 389 | * Return: status code |
| 390 | */ |
| 391 | efi_status_t do_save(u16 *filename) |
| 392 | { |
| 393 | struct efi_loaded_image *loaded_image; |
| 394 | struct efi_simple_file_system_protocol *file_system; |
| 395 | efi_uintn_t dtb_size; |
| 396 | struct efi_file_handle *root, *file; |
| 397 | struct fdt_header *dtb; |
| 398 | efi_uintn_t ret; |
Heinrich Schuchardt | 12083ba | 2020-11-04 22:00:48 +0100 | [diff] [blame] | 399 | |
| 400 | dtb = get_dtb(systable); |
| 401 | if (!dtb) { |
Heinrich Schuchardt | 9d4c19e | 2021-01-19 10:06:00 +0100 | [diff] [blame] | 402 | error(L"DTB not found\r\n"); |
Heinrich Schuchardt | 12083ba | 2020-11-04 22:00:48 +0100 | [diff] [blame] | 403 | return EFI_NOT_FOUND; |
| 404 | } |
| 405 | if (f2h(dtb->magic) != FDT_MAGIC) { |
Heinrich Schuchardt | 9d4c19e | 2021-01-19 10:06:00 +0100 | [diff] [blame] | 406 | error(L"Wrong device tree magic\r\n"); |
Heinrich Schuchardt | 12083ba | 2020-11-04 22:00:48 +0100 | [diff] [blame] | 407 | return EFI_NOT_FOUND; |
| 408 | } |
| 409 | dtb_size = f2h(dtb->totalsize); |
| 410 | |
Heinrich Schuchardt | 4591079 | 2020-12-13 19:13:57 +0100 | [diff] [blame] | 411 | filename = skip_whitespace(filename); |
Heinrich Schuchardt | 12083ba | 2020-11-04 22:00:48 +0100 | [diff] [blame] | 412 | |
| 413 | ret = bs->open_protocol(handle, &loaded_image_guid, |
| 414 | (void **)&loaded_image, NULL, NULL, |
| 415 | EFI_OPEN_PROTOCOL_GET_PROTOCOL); |
| 416 | if (ret != EFI_SUCCESS) { |
Heinrich Schuchardt | 9d4c19e | 2021-01-19 10:06:00 +0100 | [diff] [blame] | 417 | error(L"Loaded image protocol not found\r\n"); |
Heinrich Schuchardt | 12083ba | 2020-11-04 22:00:48 +0100 | [diff] [blame] | 418 | return ret; |
| 419 | } |
| 420 | |
| 421 | /* Open the simple file system protocol */ |
| 422 | ret = bs->open_protocol(loaded_image->device_handle, |
| 423 | &guid_simple_file_system_protocol, |
| 424 | (void **)&file_system, NULL, NULL, |
| 425 | EFI_OPEN_PROTOCOL_GET_PROTOCOL); |
| 426 | if (ret != EFI_SUCCESS) { |
Heinrich Schuchardt | 9d4c19e | 2021-01-19 10:06:00 +0100 | [diff] [blame] | 427 | error(L"Failed to open simple file system protocol\r\n"); |
Heinrich Schuchardt | 12083ba | 2020-11-04 22:00:48 +0100 | [diff] [blame] | 428 | return ret; |
| 429 | } |
| 430 | |
| 431 | /* Open volume */ |
| 432 | ret = file_system->open_volume(file_system, &root); |
| 433 | if (ret != EFI_SUCCESS) { |
Heinrich Schuchardt | 9d4c19e | 2021-01-19 10:06:00 +0100 | [diff] [blame] | 434 | error(L"Failed to open volume\r\n"); |
Heinrich Schuchardt | 12083ba | 2020-11-04 22:00:48 +0100 | [diff] [blame] | 435 | return ret; |
| 436 | } |
Heinrich Schuchardt | 2e88aad | 2021-01-17 05:13:21 +0100 | [diff] [blame] | 437 | /* Check if file already exists */ |
| 438 | ret = root->open(root, &file, filename, EFI_FILE_MODE_READ, 0); |
| 439 | if (ret == EFI_SUCCESS) { |
| 440 | file->close(file); |
Heinrich Schuchardt | 9d4c19e | 2021-01-19 10:06:00 +0100 | [diff] [blame] | 441 | print(L"Overwrite existing file (y/n)? "); |
Heinrich Schuchardt | 2e88aad | 2021-01-17 05:13:21 +0100 | [diff] [blame] | 442 | ret = efi_input_yn(); |
Heinrich Schuchardt | 9d4c19e | 2021-01-19 10:06:00 +0100 | [diff] [blame] | 443 | print(L"\r\n"); |
Heinrich Schuchardt | 2e88aad | 2021-01-17 05:13:21 +0100 | [diff] [blame] | 444 | if (ret != EFI_SUCCESS) { |
| 445 | root->close(root); |
Heinrich Schuchardt | 9d4c19e | 2021-01-19 10:06:00 +0100 | [diff] [blame] | 446 | error(L"Aborted by user\r\n"); |
Heinrich Schuchardt | 2e88aad | 2021-01-17 05:13:21 +0100 | [diff] [blame] | 447 | return ret; |
| 448 | } |
| 449 | } |
| 450 | |
Heinrich Schuchardt | 12083ba | 2020-11-04 22:00:48 +0100 | [diff] [blame] | 451 | /* Create file */ |
| 452 | ret = root->open(root, &file, filename, |
| 453 | EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | |
| 454 | EFI_FILE_MODE_CREATE, EFI_FILE_ARCHIVE); |
| 455 | if (ret == EFI_SUCCESS) { |
| 456 | /* Write file */ |
| 457 | ret = file->write(file, &dtb_size, dtb); |
| 458 | if (ret != EFI_SUCCESS) |
Heinrich Schuchardt | 9d4c19e | 2021-01-19 10:06:00 +0100 | [diff] [blame] | 459 | error(L"Failed to write file\r\n"); |
Heinrich Schuchardt | 12083ba | 2020-11-04 22:00:48 +0100 | [diff] [blame] | 460 | file->close(file); |
| 461 | } else { |
Heinrich Schuchardt | 9d4c19e | 2021-01-19 10:06:00 +0100 | [diff] [blame] | 462 | error(L"Failed to open file\r\n"); |
Heinrich Schuchardt | 12083ba | 2020-11-04 22:00:48 +0100 | [diff] [blame] | 463 | } |
| 464 | root->close(root); |
| 465 | |
| 466 | if (ret == EFI_SUCCESS) { |
Heinrich Schuchardt | 9d4c19e | 2021-01-19 10:06:00 +0100 | [diff] [blame] | 467 | print(filename); |
| 468 | print(L" written\r\n"); |
Heinrich Schuchardt | 12083ba | 2020-11-04 22:00:48 +0100 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | return ret; |
| 472 | } |
Heinrich Schuchardt | 4591079 | 2020-12-13 19:13:57 +0100 | [diff] [blame] | 473 | |
| 474 | /** |
| 475 | * efi_main() - entry point of the EFI application. |
| 476 | * |
| 477 | * @handle: handle of the loaded image |
| 478 | * @systab: system table |
| 479 | * @return: status code |
| 480 | */ |
| 481 | efi_status_t EFIAPI efi_main(efi_handle_t image_handle, |
| 482 | struct efi_system_table *systab) |
| 483 | { |
| 484 | handle = image_handle; |
| 485 | systable = systab; |
| 486 | cerr = systable->std_err; |
| 487 | cout = systable->con_out; |
| 488 | cin = systable->con_in; |
| 489 | bs = systable->boottime; |
| 490 | |
| 491 | cout->set_attribute(cout, EFI_LIGHTBLUE | EFI_BACKGROUND_BLACK); |
| 492 | cout->clear_screen(cout); |
| 493 | cout->set_attribute(cout, EFI_WHITE | EFI_BACKGROUND_BLACK); |
Heinrich Schuchardt | 9d4c19e | 2021-01-19 10:06:00 +0100 | [diff] [blame] | 494 | print(L"DTB Dump\r\n========\r\n\r\n"); |
Heinrich Schuchardt | 4591079 | 2020-12-13 19:13:57 +0100 | [diff] [blame] | 495 | cout->set_attribute(cout, EFI_LIGHTBLUE | EFI_BACKGROUND_BLACK); |
| 496 | |
| 497 | for (;;) { |
| 498 | u16 command[BUFFER_SIZE]; |
| 499 | u16 *pos; |
| 500 | efi_uintn_t ret; |
| 501 | |
Heinrich Schuchardt | 9d4c19e | 2021-01-19 10:06:00 +0100 | [diff] [blame] | 502 | print(L"=> "); |
Heinrich Schuchardt | 4591079 | 2020-12-13 19:13:57 +0100 | [diff] [blame] | 503 | ret = efi_input(command, sizeof(command)); |
| 504 | if (ret == EFI_ABORTED) |
| 505 | break; |
| 506 | pos = skip_whitespace(command); |
| 507 | if (starts_with(pos, L"exit")) |
| 508 | break; |
| 509 | else if (starts_with(pos, L"load ")) |
| 510 | do_load(pos + 5); |
| 511 | else if (starts_with(pos, L"save ")) |
| 512 | do_save(pos + 5); |
| 513 | else |
| 514 | do_help(); |
| 515 | } |
| 516 | |
| 517 | cout->set_attribute(cout, EFI_LIGHTGRAY | EFI_BACKGROUND_BLACK); |
| 518 | cout->clear_screen(cout); |
| 519 | return EFI_SUCCESS; |
| 520 | } |