1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2020, Heinrich Schuchardt <xypron.glpk@gmx.de>
5 * initrddump.efi saves the initial RAM disk provided via the
6 * EFI_LOAD_FILE2_PROTOCOL.
8 * Specifying 'nocolor' as load option data suppresses colored output and
9 * clearing of the screen.
14 #include <efi_load_initrd.h>
16 #define BUFFER_SIZE 64
19 #define efi_size_in_pages(size) (((size) + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT)
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;
34 * Device path defined by Linux to identify the handle providing the
35 * EFI_LOAD_FILE2_PROTOCOL used for loading the initial ramdisk.
37 static const struct efi_initrd_dp initrd_dp = {
40 DEVICE_PATH_TYPE_MEDIA_DEVICE,
41 DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
42 sizeof(initrd_dp.vendor),
44 EFI_INITRD_MEDIA_GUID,
48 DEVICE_PATH_SUB_TYPE_END,
49 sizeof(initrd_dp.end),
54 * color() - set foreground color
56 * @color: foreground color
58 static void color(u8 color)
61 cout->set_attribute(cout, color | EFI_BACKGROUND_BLACK);
65 * print() - print string
69 static void print(u16 *string)
71 cout->output_string(cout, string);
75 * cls() - clear screen
82 cout->clear_screen(cout);
86 * error() - print error string
90 static void error(u16 *string)
98 * printx() - print hexadecimal number
100 * @val: value to print;
101 * @prec: minimum number of digits to print
103 static void printx(u64 val, u32 prec)
110 for (i = 2 * sizeof(val) - 1; i >= 0; --i) {
111 c = (val >> (4 * i)) & 0x0f;
112 if (c || pos != buf || !i || i < prec) {
124 * efi_drain_input() - drain console input
126 static void efi_drain_input(void)
128 cin->reset(cin, true);
132 * efi_input_yn() - get answer to yes/no question
142 static efi_status_t efi_input_yn(void)
144 struct efi_input_key key = {0};
149 ret = bs->wait_for_event(1, &cin->wait_for_key, &index);
150 if (ret != EFI_SUCCESS)
152 ret = cin->read_key_stroke(cin, &key);
153 if (ret != EFI_SUCCESS)
155 switch (key.scan_code) {
156 case 0x17: /* Escape */
161 /* Convert to lower case */
162 switch (key.unicode_char | 0x20) {
166 return EFI_ACCESS_DENIED;
174 * efi_input() - read string from console
176 * @buffer: input buffer
177 * @buffer_size: buffer size
178 * Return: status code
180 static efi_status_t efi_input(u16 *buffer, efi_uintn_t buffer_size)
182 struct efi_input_key key = {0};
185 u16 outbuf[2] = u" ";
190 ret = bs->wait_for_event(1, &cin->wait_for_key, &index);
191 if (ret != EFI_SUCCESS)
193 ret = cin->read_key_stroke(cin, &key);
194 if (ret != EFI_SUCCESS)
196 switch (key.scan_code) {
197 case 0x17: /* Escape */
198 print(u"\r\nAborted\r\n");
203 switch (key.unicode_char) {
204 case 0x08: /* Backspace */
210 case 0x0a: /* Linefeed */
211 case 0x0d: /* Carriage return */
217 /* Ignore surrogate codes */
218 if (key.unicode_char >= 0xD800 && key.unicode_char <= 0xDBFF)
220 if (key.unicode_char >= 0x20 &&
221 pos < buffer_size - 1) {
222 *outbuf = key.unicode_char;
223 buffer[pos++] = key.unicode_char;
231 * skip_whitespace() - skip over leading whitespace
233 * @pos: UTF-16 string
234 * Return: pointer to first non-whitespace
236 static u16 *skip_whitespace(u16 *pos)
238 for (; *pos && *pos <= 0x20; ++pos)
244 * starts_with() - check if @string starts with @keyword
246 * @string: string to search for keyword
247 * @keyword: keyword to be searched
248 * Return: true if @string starts with the keyword
250 static bool starts_with(u16 *string, u16 *keyword)
252 if (!string || !keyword)
255 for (; *keyword; ++string, ++keyword) {
256 if (*string != *keyword)
263 * do_help() - print help
265 static void do_help(void)
267 error(u"load - show length and CRC32 of initial RAM disk\r\n");
268 error(u"save <initrd> - save initial RAM disk to file\r\n");
269 error(u"exit - exit the shell\r\n");
273 * get_initrd() - read initial RAM disk via EFI_LOAD_FILE2_PROTOCOL
275 * @initrd: on return buffer with initial RAM disk
276 * @initrd_size: size of initial RAM disk
277 * Return: status code
279 static efi_status_t get_initrd(void **initrd, efi_uintn_t *initrd_size)
281 struct efi_device_path *dp = (struct efi_device_path *)&initrd_dp;
282 struct efi_load_file_protocol *load_file2_prot;
289 ret = bs->locate_device_path(&load_file2_guid, &dp, &handle);
290 if (ret != EFI_SUCCESS) {
291 error(u"Load File2 protocol not found\r\n");
294 ret = bs->handle_protocol(handle, &load_file2_guid,
295 (void **)&load_file2_prot);
296 ret = load_file2_prot->load_file(load_file2_prot, dp, false,
298 if (ret != EFI_BUFFER_TOO_SMALL) {
299 error(u"Load File2 protocol does not provide file length\r\n");
300 return EFI_LOAD_ERROR;
302 ret = bs->allocate_pages(EFI_ALLOCATE_ANY_PAGES, EFI_LOADER_DATA,
303 efi_size_in_pages(*initrd_size), &buffer);
304 if (ret != EFI_SUCCESS) {
305 error(u"Out of memory\r\n");
308 *initrd = (void *)(uintptr_t)buffer;
309 ret = load_file2_prot->load_file(load_file2_prot, dp, false,
310 initrd_size, *initrd);
311 if (ret != EFI_SUCCESS) {
312 error(u"Load File2 protocol failed to provide file\r\n");
313 bs->free_pages(buffer, efi_size_in_pages(*initrd_size));
314 return EFI_LOAD_ERROR;
320 * do_load() - load initial RAM disk and display CRC32 and length
322 * @filename: file name
323 * Return: status code
325 static efi_status_t do_load(void)
328 efi_uintn_t initrd_size;
332 ret = get_initrd(&initrd, &initrd_size);
333 if (ret != EFI_SUCCESS)
335 print(u"length: 0x");
336 printx(initrd_size, 1);
339 ret = bs->calculate_crc32(initrd, initrd_size, &crc32);
340 if (ret != EFI_SUCCESS) {
341 error(u"Calculating CRC32 failed\r\n");
342 return EFI_LOAD_ERROR;
352 * do_save() - save initial RAM disk
354 * @filename: file name
355 * Return: status code
357 static efi_status_t do_save(u16 *filename)
359 struct efi_loaded_image *loaded_image;
360 struct efi_simple_file_system_protocol *file_system;
361 struct efi_file_handle *root, *file;
363 efi_uintn_t initrd_size;
366 ret = get_initrd(&initrd, &initrd_size);
367 if (ret != EFI_SUCCESS)
370 filename = skip_whitespace(filename);
372 ret = bs->open_protocol(handle, &loaded_image_guid,
373 (void **)&loaded_image, NULL, NULL,
374 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
375 if (ret != EFI_SUCCESS) {
376 error(u"Loaded image protocol not found\r\n");
380 /* Open the simple file system protocol */
381 ret = bs->open_protocol(loaded_image->device_handle,
382 &guid_simple_file_system_protocol,
383 (void **)&file_system, NULL, NULL,
384 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
385 if (ret != EFI_SUCCESS) {
386 error(u"Failed to open simple file system protocol\r\n");
391 ret = file_system->open_volume(file_system, &root);
392 if (ret != EFI_SUCCESS) {
393 error(u"Failed to open volume\r\n");
396 /* Check if file already exists */
397 ret = root->open(root, &file, filename, EFI_FILE_MODE_READ, 0);
398 if (ret == EFI_SUCCESS) {
401 print(u"Overwrite existing file (y/n)? ");
402 ret = efi_input_yn();
404 if (ret != EFI_SUCCESS) {
406 error(u"Aborted by user\r\n");
412 ret = root->open(root, &file, filename,
413 EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE |
414 EFI_FILE_MODE_CREATE, EFI_FILE_ARCHIVE);
415 if (ret == EFI_SUCCESS) {
417 ret = file->write(file, &initrd_size, initrd);
418 if (ret != EFI_SUCCESS) {
419 error(u"Failed to write file\r\n");
422 print(u" written\r\n");
426 error(u"Failed to open file\r\n");
432 bs->free_pages((uintptr_t)initrd,
433 efi_size_in_pages(initrd_size));
438 * get_load_options() - get load options
440 * Return: load options or NULL
442 u16 *get_load_options(void)
445 struct efi_loaded_image *loaded_image;
447 ret = bs->open_protocol(handle, &loaded_image_guid,
448 (void **)&loaded_image, NULL, NULL,
449 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
450 if (ret != EFI_SUCCESS) {
451 error(u"Loaded image protocol not found\r\n");
455 if (!loaded_image->load_options_size || !loaded_image->load_options)
458 return loaded_image->load_options;
462 * efi_main() - entry point of the EFI application.
464 * @handle: handle of the loaded image
465 * @systab: system table
466 * Return: status code
468 efi_status_t EFIAPI efi_main(efi_handle_t image_handle,
469 struct efi_system_table *systab)
473 handle = image_handle;
475 cerr = systable->std_err;
476 cout = systable->con_out;
477 cin = systable->con_in;
478 bs = systable->boottime;
479 load_options = get_load_options();
481 if (starts_with(load_options, u"nocolor"))
486 print(u"INITRD Dump\r\n===========\r\n\r\n");
487 color(EFI_LIGHTBLUE);
490 u16 command[BUFFER_SIZE];
496 ret = efi_input(command, sizeof(command));
497 if (ret == EFI_ABORTED)
499 pos = skip_whitespace(command);
500 if (starts_with(pos, u"exit"))
502 else if (starts_with(pos, u"load"))
504 else if (starts_with(pos, u"save "))
510 color(EFI_LIGHTGRAY);