Merge pull request #23932 from medhefgo/boot-misc

boot: Miscellaneous cleanups
This commit is contained in:
Yu Watanabe
2022-07-07 19:59:05 +09:00
committed by GitHub
10 changed files with 68 additions and 76 deletions

View File

@@ -1806,7 +1806,7 @@ static void config_title_generate(Config *config) {
static bool is_sd_boot(EFI_FILE *root_dir, const char16_t *loader_path) {
EFI_STATUS err;
const char *sections[] = {
static const char * const sections[] = {
".sdmagic",
NULL
};
@@ -1935,7 +1935,7 @@ static EFI_STATUS boot_windows_bitlocker(void) {
/* Look for BitLocker magic string on all block drives. */
bool found = false;
for (UINTN i = 0; i < n_handles; i++) {
EFI_BLOCK_IO *block_io;
EFI_BLOCK_IO_PROTOCOL *block_io;
err = BS->HandleProtocol(handles[i], &BlockIoProtocol, (void **) &block_io);
if (err != EFI_SUCCESS || block_io->Media->BlockSize < 512 || block_io->Media->BlockSize > 4096)
continue;
@@ -2082,7 +2082,7 @@ static void config_entry_add_unified(
continue;
/* look for .osrel and .cmdline sections in the .efi binary */
err = pe_file_locate_sections(linux_dir, f->FileName, (const char **) sections, offs, szs);
err = pe_file_locate_sections(linux_dir, f->FileName, sections, offs, szs);
if (err != EFI_SUCCESS || szs[SECTION_OSREL] == 0)
continue;
@@ -2328,7 +2328,7 @@ static EFI_STATUS image_start(
if (err != EFI_SUCCESS)
return log_error_status_stall(err, L"Error registering initrd: %r", err);
EFI_LOADED_IMAGE *loaded_image;
EFI_LOADED_IMAGE_PROTOCOL *loaded_image;
err = BS->HandleProtocol(image, &LoadedImageProtocol, (void **) &loaded_image);
if (err != EFI_SUCCESS)
return log_error_status_stall(err, L"Error getting LoadedImageProtocol handle: %r", err);
@@ -2430,7 +2430,7 @@ static void save_selected_entry(const Config *config, const ConfigEntry *entry)
}
static void export_variables(
EFI_LOADED_IMAGE *loaded_image,
EFI_LOADED_IMAGE_PROTOCOL *loaded_image,
const char16_t *loaded_image_path,
uint64_t init_usec) {
@@ -2472,7 +2472,7 @@ static void export_variables(
static void config_load_all_entries(
Config *config,
EFI_LOADED_IMAGE *loaded_image,
EFI_LOADED_IMAGE_PROTOCOL *loaded_image,
const char16_t *loaded_image_path,
EFI_FILE *root_dir) {
@@ -2527,7 +2527,7 @@ static void config_load_all_entries(
}
EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table) {
EFI_LOADED_IMAGE *loaded_image;
EFI_LOADED_IMAGE_PROTOCOL *loaded_image;
_cleanup_(file_closep) EFI_FILE *root_dir = NULL;
_cleanup_(config_free) Config config = {};
char16_t *loaded_image_path;

View File

@@ -305,7 +305,7 @@ static EFI_STATUS pack_cpio_trailer(
}
EFI_STATUS pack_cpio(
EFI_LOADED_IMAGE *loaded_image,
EFI_LOADED_IMAGE_PROTOCOL *loaded_image,
const char16_t *dropin_dir,
const char16_t *match_suffix,
const char *target_dir_prefix,
@@ -325,7 +325,6 @@ EFI_STATUS pack_cpio(
_cleanup_freepool_ void *buffer = NULL;
uint32_t inode = 1; /* inode counter, so that each item gets a new inode */
EFI_STATUS err;
EFI_FILE_IO_INTERFACE *volume;
assert(loaded_image);
assert(target_dir_prefix);
@@ -339,21 +338,14 @@ EFI_STATUS pack_cpio(
return EFI_SUCCESS;
}
err = BS->HandleProtocol(loaded_image->DeviceHandle,
&FileSystemProtocol, (void*)&volume);
/* Error will be unsupported if the bootloader doesn't implement the
* file system protocol on its file handles.
*/
err = open_volume(loaded_image->DeviceHandle, &root);
if (err == EFI_UNSUPPORTED) {
/* Error will be unsupported if the bootloader doesn't implement the file system protocol on
* its file handles. */
*ret_buffer = NULL;
*ret_buffer_size = 0;
return EFI_SUCCESS;
}
if (err != EFI_SUCCESS)
return log_error_status_stall(
err, L"Unable to load file system protocol: %r", err);
err = volume->OpenVolume(volume, &root);
if (err != EFI_SUCCESS)
return log_error_status_stall(
err, L"Unable to open root directory: %r", err);

View File

@@ -5,7 +5,7 @@
#include <uchar.h>
EFI_STATUS pack_cpio(
EFI_LOADED_IMAGE *loaded_image,
EFI_LOADED_IMAGE_PROTOCOL *loaded_image,
const char16_t *dropin_dir,
const char16_t *match_suffix,
const char *target_dir_prefix,

View File

@@ -8,7 +8,7 @@
static EFI_STATUS load_one_driver(
EFI_HANDLE parent_image,
EFI_LOADED_IMAGE *loaded_image,
EFI_LOADED_IMAGE_PROTOCOL *loaded_image,
const char16_t *fname) {
_cleanup_(unload_imagep) EFI_HANDLE image = NULL;
@@ -74,7 +74,7 @@ static EFI_STATUS reconnect(void) {
EFI_STATUS load_drivers(
EFI_HANDLE parent_image,
EFI_LOADED_IMAGE *loaded_image,
EFI_LOADED_IMAGE_PROTOCOL *loaded_image,
EFI_FILE *root_dir) {
_cleanup_(file_closep) EFI_FILE *drivers_dir = NULL;

View File

@@ -5,5 +5,5 @@
EFI_STATUS load_drivers(
EFI_HANDLE parent_image,
EFI_LOADED_IMAGE *loaded_image,
EFI_LOADED_IMAGE_PROTOCOL *loaded_image,
EFI_FILE *root_dir);

View File

@@ -16,7 +16,7 @@
#include "pe.h"
#include "util.h"
static EFI_LOADED_IMAGE * loaded_image_free(EFI_LOADED_IMAGE *img) {
static EFI_LOADED_IMAGE_PROTOCOL *loaded_image_free(EFI_LOADED_IMAGE_PROTOCOL *img) {
if (!img)
return NULL;
mfree(img->LoadOptions);
@@ -28,7 +28,7 @@ static EFI_STATUS loaded_image_register(
const void *linux_buffer, UINTN linux_length,
EFI_HANDLE *ret_image) {
EFI_LOADED_IMAGE *loaded_image = NULL;
EFI_LOADED_IMAGE_PROTOCOL *loaded_image = NULL;
EFI_STATUS err;
assert(cmdline || cmdline_len > 0);
@@ -36,8 +36,8 @@ static EFI_STATUS loaded_image_register(
assert(ret_image);
/* create and install new LoadedImage Protocol */
loaded_image = xnew(EFI_LOADED_IMAGE, 1);
*loaded_image = (EFI_LOADED_IMAGE) {
loaded_image = xnew(EFI_LOADED_IMAGE_PROTOCOL, 1);
*loaded_image = (EFI_LOADED_IMAGE_PROTOCOL) {
.ImageBase = (void *) linux_buffer,
.ImageSize = linux_length
};

View File

@@ -30,7 +30,7 @@
# define TARGET_MACHINE_TYPE_COMPATIBILITY 0
#endif
struct DosFileHeader {
typedef struct DosFileHeader {
uint8_t Magic[2];
uint16_t LastSize;
uint16_t nBlocks;
@@ -50,9 +50,9 @@ struct DosFileHeader {
uint16_t OEMInfo;
uint16_t reserved2[10];
uint32_t ExeHeader;
} _packed_;
} _packed_ DosFileHeader;
struct CoffFileHeader {
typedef struct CoffFileHeader {
uint16_t Machine;
uint16_t NumberOfSections;
uint32_t TimeDateStamp;
@@ -60,12 +60,12 @@ struct CoffFileHeader {
uint32_t NumberOfSymbols;
uint16_t SizeOfOptionalHeader;
uint16_t Characteristics;
} _packed_;
} _packed_ CoffFileHeader;
#define OPTHDR32_MAGIC 0x10B /* PE32 OptionalHeader */
#define OPTHDR64_MAGIC 0x20B /* PE32+ OptionalHeader */
struct PeOptionalHeader {
typedef struct PeOptionalHeader {
uint16_t Magic;
uint8_t LinkerMajor;
uint8_t LinkerMinor;
@@ -96,15 +96,15 @@ struct PeOptionalHeader {
uint16_t Subsystem;
uint16_t DllCharacteristics;
/* fields with different sizes for 32/64 omitted */
} _packed_;
} _packed_ PeOptionalHeader;
struct PeFileHeader {
typedef struct PeFileHeader {
uint8_t Magic[4];
struct CoffFileHeader FileHeader;
struct PeOptionalHeader OptionalHeader;
} _packed_;
CoffFileHeader FileHeader;
PeOptionalHeader OptionalHeader;
} _packed_ PeFileHeader;
struct PeSectionHeader {
typedef struct PeSectionHeader {
uint8_t Name[8];
uint32_t VirtualSize;
uint32_t VirtualAddress;
@@ -115,14 +115,14 @@ struct PeSectionHeader {
uint16_t NumberOfRelocations;
uint16_t NumberOfLinenumbers;
uint32_t Characteristics;
} _packed_;
} _packed_ PeSectionHeader;
static inline bool verify_dos(const struct DosFileHeader *dos) {
static inline bool verify_dos(const DosFileHeader *dos) {
assert(dos);
return memcmp(dos->Magic, DOS_FILE_MAGIC, STRLEN(DOS_FILE_MAGIC)) == 0;
}
static inline bool verify_pe(const struct PeFileHeader *pe, bool allow_compatibility) {
static inline bool verify_pe(const PeFileHeader *pe, bool allow_compatibility) {
assert(pe);
return memcmp(pe->Magic, PE_FILE_MAGIC, STRLEN(PE_FILE_MAGIC)) == 0 &&
(pe->FileHeader.Machine == TARGET_MACHINE_TYPE ||
@@ -132,16 +132,16 @@ static inline bool verify_pe(const struct PeFileHeader *pe, bool allow_compatibi
IN_SET(pe->OptionalHeader.Magic, OPTHDR32_MAGIC, OPTHDR64_MAGIC);
}
static inline UINTN section_table_offset(const struct DosFileHeader *dos, const struct PeFileHeader *pe) {
static inline UINTN section_table_offset(const DosFileHeader *dos, const PeFileHeader *pe) {
assert(dos);
assert(pe);
return dos->ExeHeader + offsetof(struct PeFileHeader, OptionalHeader) + pe->FileHeader.SizeOfOptionalHeader;
return dos->ExeHeader + offsetof(PeFileHeader, OptionalHeader) + pe->FileHeader.SizeOfOptionalHeader;
}
static void locate_sections(
const struct PeSectionHeader section_table[],
const PeSectionHeader section_table[],
UINTN n_table,
const char **sections,
const char * const sections[],
UINTN *addrs,
UINTN *offsets,
UINTN *sizes) {
@@ -151,7 +151,7 @@ static void locate_sections(
assert(sizes);
for (UINTN i = 0; i < n_table; i++) {
const struct PeSectionHeader *sect = section_table + i;
const PeSectionHeader *sect = section_table + i;
for (UINTN j = 0; sections[j]; j++) {
if (memcmp(sect->Name, sections[j], strlen8(sections[j])) != 0)
@@ -166,7 +166,7 @@ static void locate_sections(
}
}
static uint32_t get_compatibility_entry_address(const struct DosFileHeader *dos, const struct PeFileHeader *pe) {
static uint32_t get_compatibility_entry_address(const DosFileHeader *dos, const PeFileHeader *pe) {
UINTN addr = 0, size = 0;
static const char *sections[] = { ".compat", NULL };
@@ -174,7 +174,7 @@ static uint32_t get_compatibility_entry_address(const struct DosFileHeader *dos,
* booting a 64bit kernel on 32bit EFI that is otherwise running on a 64bit CPU. The locations of any
* such compat entry points are located in a special PE section. */
locate_sections((const struct PeSectionHeader *) ((const uint8_t *) dos + section_table_offset(dos, pe)),
locate_sections((const PeSectionHeader *) ((const uint8_t *) dos + section_table_offset(dos, pe)),
pe->FileHeader.NumberOfSections,
sections,
&addr,
@@ -215,17 +215,17 @@ EFI_STATUS pe_alignment_info(
uint32_t *ret_size_of_image,
uint32_t *ret_section_alignment) {
const struct DosFileHeader *dos;
const struct PeFileHeader *pe;
const DosFileHeader *dos;
const PeFileHeader *pe;
assert(base);
assert(ret_entry_point_address);
dos = (const struct DosFileHeader *) base;
dos = (const DosFileHeader *) base;
if (!verify_dos(dos))
return EFI_LOAD_ERROR;
pe = (const struct PeFileHeader*) ((const uint8_t *)base + dos->ExeHeader);
pe = (const PeFileHeader *) ((const uint8_t *) base + dos->ExeHeader);
if (!verify_pe(pe, /* allow_compatibility= */ true))
return EFI_LOAD_ERROR;
@@ -247,13 +247,9 @@ EFI_STATUS pe_alignment_info(
return EFI_SUCCESS;
}
EFI_STATUS pe_memory_locate_sections(
const char *base,
const char **sections,
UINTN *addrs,
UINTN *sizes) {
const struct DosFileHeader *dos;
const struct PeFileHeader *pe;
EFI_STATUS pe_memory_locate_sections(const void *base, const char * const sections[], UINTN *addrs, UINTN *sizes) {
const DosFileHeader *dos;
const PeFileHeader *pe;
UINTN offset;
assert(base);
@@ -261,17 +257,21 @@ EFI_STATUS pe_memory_locate_sections(
assert(addrs);
assert(sizes);
dos = (const struct DosFileHeader*)base;
dos = (const DosFileHeader *) base;
if (!verify_dos(dos))
return EFI_LOAD_ERROR;
pe = (const struct PeFileHeader*)&base[dos->ExeHeader];
pe = (const PeFileHeader *) ((uint8_t *) base + dos->ExeHeader);
if (!verify_pe(pe, /* allow_compatibility= */ false))
return EFI_LOAD_ERROR;
offset = section_table_offset(dos, pe);
locate_sections((struct PeSectionHeader*)&base[offset], pe->FileHeader.NumberOfSections,
sections, addrs, NULL, sizes);
locate_sections((PeSectionHeader *) ((uint8_t *) base + offset),
pe->FileHeader.NumberOfSections,
sections,
addrs,
NULL,
sizes);
return EFI_SUCCESS;
}
@@ -279,13 +279,13 @@ EFI_STATUS pe_memory_locate_sections(
EFI_STATUS pe_file_locate_sections(
EFI_FILE *dir,
const char16_t *path,
const char **sections,
const char * const sections[],
UINTN *offsets,
UINTN *sizes) {
_cleanup_freepool_ struct PeSectionHeader *section_table = NULL;
_cleanup_freepool_ PeSectionHeader *section_table = NULL;
_cleanup_(file_closep) EFI_FILE *handle = NULL;
struct DosFileHeader dos;
struct PeFileHeader pe;
DosFileHeader dos;
PeFileHeader pe;
UINTN len, section_table_len;
EFI_STATUS err;
@@ -317,7 +317,7 @@ EFI_STATUS pe_file_locate_sections(
if (len != sizeof(pe) || !verify_pe(&pe, /* allow_compatibility= */ false))
return EFI_LOAD_ERROR;
section_table_len = pe.FileHeader.NumberOfSections * sizeof(struct PeSectionHeader);
section_table_len = pe.FileHeader.NumberOfSections * sizeof(PeSectionHeader);
section_table = xmalloc(section_table_len);
if (!section_table)
return EFI_OUT_OF_RESOURCES;

View File

@@ -5,15 +5,15 @@
#include <uchar.h>
EFI_STATUS pe_memory_locate_sections(
const char *base,
const char **sections,
const void *base,
const char * const sections[],
UINTN *addrs,
UINTN *sizes);
EFI_STATUS pe_file_locate_sections(
EFI_FILE *dir,
const char16_t *path,
const char **sections,
const char * const sections[],
UINTN *offsets,
UINTN *sizes);

View File

@@ -101,7 +101,7 @@ static EFI_STATUS combine_initrd(
return EFI_SUCCESS;
}
static void export_variables(EFI_LOADED_IMAGE *loaded_image) {
static void export_variables(EFI_LOADED_IMAGE_PROTOCOL *loaded_image) {
char16_t uuid[37];
assert(loaded_image);
@@ -173,7 +173,7 @@ EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table) {
_cleanup_freepool_ void *sysext_initrd = NULL;
EFI_PHYSICAL_ADDRESS linux_base, initrd_base, dt_base;
_cleanup_(devicetree_cleanup) struct devicetree_state dt_state = {};
EFI_LOADED_IMAGE *loaded_image;
EFI_LOADED_IMAGE_PROTOCOL *loaded_image;
UINTN addrs[_SECTION_MAX] = {};
UINTN szs[_SECTION_MAX] = {};
char *cmdline = NULL;
@@ -195,7 +195,7 @@ EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table) {
if (err != EFI_SUCCESS)
return log_error_status_stall(err, L"Error getting a LoadedImageProtocol handle: %r", err);
err = pe_memory_locate_sections(loaded_image->ImageBase, (const char **) sections, addrs, szs);
err = pe_memory_locate_sections(loaded_image->ImageBase, sections, addrs, szs);
if (err != EFI_SUCCESS || szs[SECTION_LINUX] == 0) {
if (err == EFI_SUCCESS)
err = EFI_NOT_FOUND;

View File

@@ -85,7 +85,7 @@ static bool verify_gpt(union GptHeaderBuffer *gpt_header_buffer, EFI_LBA lba_exp
}
static EFI_STATUS try_gpt(
EFI_BLOCK_IO *block_io,
EFI_BLOCK_IO_PROTOCOL *block_io,
EFI_LBA lba,
EFI_LBA *ret_backup_lba, /* May be changed even on error! */
HARDDRIVE_DEVICE_PATH *ret_hd) {
@@ -195,7 +195,7 @@ static EFI_STATUS find_device(EFI_HANDLE *device, EFI_DEVICE_PATH **ret_device_p
EFI_DEVICE_PATH *p = disk_path = path_chop(partition_path, part_node);
EFI_HANDLE disk_handle;
EFI_BLOCK_IO *block_io;
EFI_BLOCK_IO_PROTOCOL *block_io;
err = BS->LocateDevicePath(&BlockIoProtocol, &p, &disk_handle);
if (err != EFI_SUCCESS)
return err;