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