Move the shared file-system utilities to efi_app_common: * open_file_system() - open the simple file system protocol, first trying the partition the image was loaded from, then the UEFI system partition. * get_config_table() - look up an EFI configuration table by GUID. * save_file() - write a buffer to a named file, prompting for confirmation if the file already exists.
get_config_table() supersedes the private get_dtb() in dtbdump and get_dbg_info() in dbginfodump; both call-sites are updated to use it. dtbdump's do_save() and initrddump's do_save() are simplified to use the common save_file(), eliminating duplicate file-open/write/close sequences. smbiosdump's private get_config_table(), open_file_system() and save_file() are removed; the common versions take their place. GUID declarations that are no longer referenced after these removals are dropped from each app's file-scope variables. Signed-off-by: Heinrich Schuchardt <[email protected]> --- v2: new patch --- lib/efi_loader/dbginfodump.c | 22 +----- lib/efi_loader/dtbdump.c | 120 +----------------------------- lib/efi_loader/efi_app_common.c | 105 +++++++++++++++++++++++++++ lib/efi_loader/efi_app_common.h | 27 +++++++ lib/efi_loader/initrddump.c | 69 +----------------- lib/efi_loader/smbiosdump.c | 125 -------------------------------- 6 files changed, 141 insertions(+), 327 deletions(-) diff --git a/lib/efi_loader/dbginfodump.c b/lib/efi_loader/dbginfodump.c index 4808fe254fe..b54c3830a66 100644 --- a/lib/efi_loader/dbginfodump.c +++ b/lib/efi_loader/dbginfodump.c @@ -91,26 +91,6 @@ static void do_help(void) error(u"exit - exit the shell\r\n"); } -/** - * get_dbg_info_table() - get debug info table - * - * Return: debug info table or NULL - */ -static void *get_dbg_info(void) -{ - void *dbg = NULL; - efi_uintn_t i; - - for (i = 0; i < systable->nr_tables; ++i) { - if (!memcmp(&systable->tables[i].guid, &dbg_info_guid, - sizeof(efi_guid_t))) { - dbg = systable->tables[i].table; - break; - } - } - return dbg; -} - /** * print_info() - print loaded image protocol */ @@ -144,7 +124,7 @@ static efi_status_t do_dump(void) struct dbg_info_header *dbg; u32 count; - dbg = get_dbg_info(); + dbg = get_config_table(&dbg_info_guid); if (!dbg) { error(u"Debug info table not found\r\n"); return EFI_NOT_FOUND; diff --git a/lib/efi_loader/dtbdump.c b/lib/efi_loader/dtbdump.c index 559c64be7f7..0d56a41ecf6 100644 --- a/lib/efi_loader/dtbdump.c +++ b/lib/efi_loader/dtbdump.c @@ -7,7 +7,6 @@ */ #include <efi_dt_fixup.h> -#include <part.h> #include <linux/libfdt.h> #include "efi_app_common.h" @@ -18,14 +17,10 @@ static struct efi_boot_services *bs; static const efi_guid_t fdt_guid = EFI_FDT_GUID; -static const efi_guid_t loaded_image_guid = EFI_LOADED_IMAGE_PROTOCOL_GUID; -static const efi_guid_t guid_simple_file_system_protocol = - EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID; static efi_handle_t handle; static struct efi_system_table *systable; static const efi_guid_t efi_dt_fixup_protocol_guid = EFI_DT_FIXUP_PROTOCOL_GUID; static const efi_guid_t efi_file_info_guid = EFI_FILE_INFO_GUID; -static const efi_guid_t efi_system_partition_guid = PARTITION_SYSTEM_GUID; /** * print_char() - print character @@ -66,27 +61,6 @@ static u32 f2h(fdt32_t val) return *(u32 *)buf; } -/** - * get_dtb() - get device tree - * - * @systable: system table - * Return: device tree or NULL - */ -static void *get_dtb(struct efi_system_table *systable) -{ - void *dtb = NULL; - efi_uintn_t i; - - for (i = 0; i < systable->nr_tables; ++i) { - if (!memcmp(&systable->tables[i].guid, &fdt_guid, - sizeof(efi_guid_t))) { - dtb = systable->tables[i].table; - break; - } - } - return dtb; -} - /** * do_help() - print help */ @@ -98,52 +72,6 @@ static void do_help(void) error(u"exit - exit the shell\r\n"); } -/** - * open_file_system() - open simple file system protocol - * - * file_system: interface of the simple file system protocol - * Return: status code - */ -static efi_status_t -open_file_system(struct efi_simple_file_system_protocol **file_system) -{ - struct efi_loaded_image *loaded_image; - efi_status_t ret; - efi_handle_t *handle_buffer = NULL; - efi_uintn_t count; - - ret = bs->open_protocol(handle, &loaded_image_guid, - (void **)&loaded_image, NULL, NULL, - EFI_OPEN_PROTOCOL_GET_PROTOCOL); - if (ret != EFI_SUCCESS) { - error(u"Loaded image protocol not found\r\n"); - return ret; - } - - /* Open the simple file system protocol on the same partition */ - ret = bs->open_protocol(loaded_image->device_handle, - &guid_simple_file_system_protocol, - (void **)file_system, NULL, NULL, - EFI_OPEN_PROTOCOL_GET_PROTOCOL); - if (ret == EFI_SUCCESS) - return ret; - - /* Open the simple file system protocol on the UEFI system partition */ - ret = bs->locate_handle_buffer(BY_PROTOCOL, &efi_system_partition_guid, - NULL, &count, &handle_buffer); - if (ret == EFI_SUCCESS && handle_buffer) - ret = bs->open_protocol(handle_buffer[0], - &guid_simple_file_system_protocol, - (void **)file_system, NULL, NULL, - EFI_OPEN_PROTOCOL_GET_PROTOCOL); - if (ret != EFI_SUCCESS) - error(u"Failed to open simple file system protocol\r\n"); - if (handle_buffer) - bs->free_pool(handle_buffer); - - return ret; -} - /** * do_load() - load and install device-tree * @@ -289,13 +217,11 @@ out: */ static efi_status_t do_save(u16 *filename) { - struct efi_simple_file_system_protocol *file_system; - efi_uintn_t dtb_size; - struct efi_file_handle *root, *file; struct fdt_header *dtb; + efi_uintn_t dtb_size; efi_uintn_t ret; - dtb = get_dtb(systable); + dtb = get_config_table(&fdt_guid); if (!dtb) { error(u"DTB not found\r\n"); return EFI_NOT_FOUND; @@ -308,45 +234,7 @@ static efi_status_t do_save(u16 *filename) filename = skip_whitespace(filename); - ret = open_file_system(&file_system); - if (ret != EFI_SUCCESS) - return ret; - - /* Open volume */ - ret = file_system->open_volume(file_system, &root); - if (ret != EFI_SUCCESS) { - error(u"Failed to open volume\r\n"); - return ret; - } - /* Check if file already exists */ - ret = root->open(root, &file, filename, EFI_FILE_MODE_READ, 0); - if (ret == EFI_SUCCESS) { - file->close(file); - print(u"Overwrite existing file (y/n)? "); - ret = efi_input_yn(); - print(u"\r\n"); - if (ret != EFI_SUCCESS) { - root->close(root); - error(u"Aborted by user\r\n"); - return ret; - } - } - - /* Create file */ - ret = root->open(root, &file, filename, - EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | - EFI_FILE_MODE_CREATE, EFI_FILE_ARCHIVE); - if (ret == EFI_SUCCESS) { - /* Write file */ - ret = file->write(file, &dtb_size, dtb); - if (ret != EFI_SUCCESS) - error(u"Failed to write file\r\n"); - file->close(file); - } else { - error(u"Failed to open file\r\n"); - } - root->close(root); - + ret = save_file(filename, dtb, dtb_size); if (ret == EFI_SUCCESS) { print(filename); print(u" written\r\n"); @@ -479,7 +367,7 @@ static efi_status_t do_dump(void) const char *strings; u32 level = 0; - fdt = get_dtb(systable); + fdt = get_config_table(&fdt_guid); if (!fdt) { error(u"DTB not found\r\n"); return EFI_NOT_FOUND; diff --git a/lib/efi_loader/efi_app_common.c b/lib/efi_loader/efi_app_common.c index 6bd52e08d28..af9799557fc 100644 --- a/lib/efi_loader/efi_app_common.c +++ b/lib/efi_loader/efi_app_common.c @@ -6,6 +6,7 @@ */ #include <efi_api.h> +#include <part.h> #include "efi_app_common.h" static efi_handle_t app_handle; @@ -15,6 +16,9 @@ struct efi_boot_services *bs; static bool nocolor; static struct efi_system_table *systable; static const efi_guid_t loaded_image_guid = EFI_LOADED_IMAGE_PROTOCOL_GUID; +static const efi_guid_t guid_simple_file_system_protocol = + EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID; +static const efi_guid_t efi_system_partition_guid = PARTITION_SYSTEM_GUID; static u16 *get_load_options(void) { @@ -199,3 +203,104 @@ efi_status_t efi_input(u16 *buffer, efi_uintn_t buffer_size) } } } + +void *get_config_table(const efi_guid_t *guid) +{ + size_t i; + + for (i = 0; i < systable->nr_tables; ++i) { + if (!memcmp(guid, &systable->tables[i].guid, 16)) + return systable->tables[i].table; + } + + return NULL; +} + +efi_status_t +open_file_system(struct efi_simple_file_system_protocol **file_system) +{ + struct efi_loaded_image *loaded_image; + efi_status_t ret; + efi_handle_t *handle_buffer = NULL; + efi_uintn_t count; + + ret = bs->open_protocol(app_handle, &loaded_image_guid, + (void **)&loaded_image, NULL, NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL); + if (ret != EFI_SUCCESS) { + error(u"Loaded image protocol not found\r\n"); + return ret; + } + + /* Open the simple file system protocol on the same partition */ + ret = bs->open_protocol(loaded_image->device_handle, + &guid_simple_file_system_protocol, + (void **)file_system, NULL, NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL); + if (ret == EFI_SUCCESS) + return ret; + + /* Open the simple file system protocol on the UEFI system partition */ + ret = bs->locate_handle_buffer(BY_PROTOCOL, &efi_system_partition_guid, + NULL, &count, &handle_buffer); + if (ret == EFI_SUCCESS && handle_buffer) + ret = bs->open_protocol(handle_buffer[0], + &guid_simple_file_system_protocol, + (void **)file_system, NULL, NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL); + if (ret != EFI_SUCCESS) + error(u"Failed to open simple file system protocol\r\n"); + if (handle_buffer) + bs->free_pool(handle_buffer); + + return ret; +} + +efi_status_t save_file(u16 *filename, void *buf, efi_uintn_t size) +{ + efi_uintn_t ret; + struct efi_simple_file_system_protocol *file_system; + struct efi_file_handle *root, *file; + + ret = open_file_system(&file_system); + if (ret != EFI_SUCCESS) + return ret; + + /* Open volume */ + ret = file_system->open_volume(file_system, &root); + if (ret != EFI_SUCCESS) { + error(u"Failed to open volume\r\n"); + return ret; + } + /* Check if file already exists */ + ret = root->open(root, &file, filename, EFI_FILE_MODE_READ, 0); + if (ret == EFI_SUCCESS) { + file->close(file); + print(u"Overwrite existing file (y/n)? "); + ret = efi_input_yn(); + print(u"\r\n"); + if (ret != EFI_SUCCESS) { + root->close(root); + error(u"Aborted by user\r\n"); + return ret; + } + } + + /* Create file */ + ret = root->open(root, &file, filename, + EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | + EFI_FILE_MODE_CREATE, + EFI_FILE_ARCHIVE); + if (ret == EFI_SUCCESS) { + /* Write file */ + ret = file->write(file, &size, buf); + if (ret != EFI_SUCCESS) + error(u"Failed to write file\r\n"); + file->close(file); + } else { + error(u"Failed to open file\r\n"); + } + root->close(root); + + return ret; +} diff --git a/lib/efi_loader/efi_app_common.h b/lib/efi_loader/efi_app_common.h index ff37f3413eb..77fe100afd9 100644 --- a/lib/efi_loader/efi_app_common.h +++ b/lib/efi_loader/efi_app_common.h @@ -101,4 +101,31 @@ efi_status_t efi_input_yn(void); */ efi_status_t efi_input(u16 *buffer, efi_uintn_t buffer_size); +/** + * get_config_table() - get configuration table + * + * @guid: GUID of the configuration table + * Return: pointer to configuration table or NULL + */ +void *get_config_table(const efi_guid_t *guid); + +/** + * open_file_system() - open simple file system protocol + * + * @file_system: interface of the simple file system protocol + * Return: status code + */ +efi_status_t +open_file_system(struct efi_simple_file_system_protocol **file_system); + +/** + * save_file() - save buffer to a file on the EFI system partition + * + * @filename: file name + * @buf: buffer to write + * @size: size of the buffer + * Return: status code + */ +efi_status_t save_file(u16 *filename, void *buf, efi_uintn_t size); + #endif /* _EFI_APP_COMMON_H */ diff --git a/lib/efi_loader/initrddump.c b/lib/efi_loader/initrddump.c index 0a025c1a841..c3e6f2b683f 100644 --- a/lib/efi_loader/initrddump.c +++ b/lib/efi_loader/initrddump.c @@ -19,9 +19,6 @@ static struct efi_system_table *systable; static struct efi_boot_services *bs; -static const efi_guid_t loaded_image_guid = EFI_LOADED_IMAGE_PROTOCOL_GUID; -static const efi_guid_t guid_simple_file_system_protocol = - EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID; static const efi_guid_t load_file2_guid = EFI_LOAD_FILE2_PROTOCOL_GUID; static efi_handle_t handle; @@ -143,9 +140,6 @@ static efi_status_t do_load(void) */ static efi_status_t do_save(u16 *filename) { - struct efi_loaded_image *loaded_image; - struct efi_simple_file_system_protocol *file_system; - struct efi_file_handle *root, *file; void *initrd; efi_uintn_t initrd_size; efi_uintn_t ret; @@ -156,68 +150,13 @@ static efi_status_t do_save(u16 *filename) filename = skip_whitespace(filename); - ret = bs->open_protocol(handle, &loaded_image_guid, - (void **)&loaded_image, NULL, NULL, - EFI_OPEN_PROTOCOL_GET_PROTOCOL); - if (ret != EFI_SUCCESS) { - error(u"Loaded image protocol not found\r\n"); - goto out; - } - - /* Open the simple file system protocol */ - ret = bs->open_protocol(loaded_image->device_handle, - &guid_simple_file_system_protocol, - (void **)&file_system, NULL, NULL, - EFI_OPEN_PROTOCOL_GET_PROTOCOL); - if (ret != EFI_SUCCESS) { - error(u"Failed to open simple file system protocol\r\n"); - goto out; - } - - /* Open volume */ - ret = file_system->open_volume(file_system, &root); - if (ret != EFI_SUCCESS) { - error(u"Failed to open volume\r\n"); - goto out; - } - /* Check if file already exists */ - ret = root->open(root, &file, filename, EFI_FILE_MODE_READ, 0); - if (ret == EFI_SUCCESS) { - file->close(file); - efi_drain_input(); - print(u"Overwrite existing file (y/n)? "); - ret = efi_input_yn(); - print(u"\r\n"); - if (ret != EFI_SUCCESS) { - root->close(root); - error(u"Aborted by user\r\n"); - goto out; - } - } - - /* Create file */ - ret = root->open(root, &file, filename, - EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | - EFI_FILE_MODE_CREATE, EFI_FILE_ARCHIVE); + ret = save_file(filename, initrd, initrd_size); if (ret == EFI_SUCCESS) { - /* Write file */ - ret = file->write(file, &initrd_size, initrd); - if (ret != EFI_SUCCESS) { - error(u"Failed to write file\r\n"); - } else { - print(filename); - print(u" written\r\n"); - } - file->close(file); - } else { - error(u"Failed to open file\r\n"); + print(filename); + print(u" written\r\n"); } - root->close(root); -out: - if (initrd) - bs->free_pages((uintptr_t)initrd, - efi_size_in_pages(initrd_size)); + bs->free_pages((uintptr_t)initrd, efi_size_in_pages(initrd_size)); return ret; } diff --git a/lib/efi_loader/smbiosdump.c b/lib/efi_loader/smbiosdump.c index 351e103fb97..8aa6f594a07 100644 --- a/lib/efi_loader/smbiosdump.c +++ b/lib/efi_loader/smbiosdump.c @@ -8,7 +8,6 @@ * clearing of the screen. */ -#include <part.h> #include <smbios.h> #include <string.h> #include "efi_app_common.h" @@ -20,56 +19,6 @@ static efi_handle_t handle; static struct efi_system_table *systable; static const efi_guid_t smbios_guid = SMBIOS_TABLE_GUID; static const efi_guid_t smbios3_guid = SMBIOS3_TABLE_GUID; -static const efi_guid_t loaded_image_guid = EFI_LOADED_IMAGE_PROTOCOL_GUID; -static const efi_guid_t guid_simple_file_system_protocol = - EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID; -static const efi_guid_t efi_system_partition_guid = PARTITION_SYSTEM_GUID; - -/** - * open_file_system() - open simple file system protocol - * - * file_system: interface of the simple file system protocol - * Return: status code - */ -static efi_status_t -open_file_system(struct efi_simple_file_system_protocol **file_system) -{ - struct efi_loaded_image *loaded_image; - efi_status_t ret; - efi_handle_t *handle_buffer = NULL; - efi_uintn_t count; - - ret = bs->open_protocol(handle, &loaded_image_guid, - (void **)&loaded_image, NULL, NULL, - EFI_OPEN_PROTOCOL_GET_PROTOCOL); - if (ret != EFI_SUCCESS) { - error(u"Loaded image protocol not found\r\n"); - return ret; - } - - /* Open the simple file system protocol on the same partition */ - ret = bs->open_protocol(loaded_image->device_handle, - &guid_simple_file_system_protocol, - (void **)file_system, NULL, NULL, - EFI_OPEN_PROTOCOL_GET_PROTOCOL); - if (ret == EFI_SUCCESS) - return ret; - - /* Open the simple file system protocol on the UEFI system partition */ - ret = bs->locate_handle_buffer(BY_PROTOCOL, &efi_system_partition_guid, - NULL, &count, &handle_buffer); - if (ret == EFI_SUCCESS && handle_buffer) - ret = bs->open_protocol(handle_buffer[0], - &guid_simple_file_system_protocol, - (void **)file_system, NULL, NULL, - EFI_OPEN_PROTOCOL_GET_PROTOCOL); - if (ret != EFI_SUCCESS) - error(u"Failed to open simple file system protocol\r\n"); - if (handle_buffer) - bs->free_pool(handle_buffer); - - return ret; -} /** * do_help() - print help @@ -81,24 +30,6 @@ static void do_help(void) error(u"exit - exit the shell\r\n"); } -/** - * get_config_table() - get configuration table - * - * @guid: GUID of the configuration table - * Return: pointer to configuration table or NULL - */ -static void *get_config_table(const efi_guid_t *guid) -{ - size_t i; - - for (i = 0; i < systable->nr_tables; ++i) { - if (!memcmp(guid, &systable->tables[i].guid, 16)) - return systable->tables[i].table; - } - - return NULL; -} - /** * checksum() - calculate checksum * @@ -211,62 +142,6 @@ static efi_status_t do_check(void) return EFI_SUCCESS; } -/** - * save_file() - save file to EFI system partition - * - * @filename: file name - * @buf: buffer to write - * @size: size of the buffer - */ -static efi_status_t save_file(u16 *filename, void *buf, efi_uintn_t size) -{ - efi_uintn_t ret; - struct efi_simple_file_system_protocol *file_system; - struct efi_file_handle *root, *file; - - ret = open_file_system(&file_system); - if (ret != EFI_SUCCESS) - return ret; - - /* Open volume */ - ret = file_system->open_volume(file_system, &root); - if (ret != EFI_SUCCESS) { - error(u"Failed to open volume\r\n"); - return ret; - } - /* Check if file already exists */ - ret = root->open(root, &file, filename, EFI_FILE_MODE_READ, 0); - if (ret == EFI_SUCCESS) { - file->close(file); - print(u"Overwrite existing file (y/n)? "); - ret = efi_input_yn(); - print(u"\r\n"); - if (ret != EFI_SUCCESS) { - root->close(root); - error(u"Aborted by user\r\n"); - bs->free_pool(buf); - return ret; - } - } - - /* Create file */ - ret = root->open(root, &file, filename, - EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | - EFI_FILE_MODE_CREATE, EFI_FILE_ARCHIVE); - if (ret == EFI_SUCCESS) { - /* Write file */ - ret = file->write(file, &size, buf); - if (ret != EFI_SUCCESS) - error(u"Failed to write file\r\n"); - file->close(file); - } else { - error(u"Failed to open file\r\n"); - } - root->close(root); - - return ret; -} - /** * do_save() - save SMBIOS table * -- 2.53.0

