1
0
mirror of https://github.com/systemd/systemd.git synced 2025-01-18 10:04:04 +03:00

Merge pull request #23982 from medhefgo/boot-misc

boot: Misc cleanups
This commit is contained in:
Luca Boccassi 2022-07-12 22:43:14 +01:00 committed by GitHub
commit 8880c3be82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 52 additions and 67 deletions

View File

@ -1,18 +1,9 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#ifdef SD_BOOT
# include <efi.h>
# include "macro-fundamental.h"
# include "util.h"
# define TEST_STATIC
#else
/* Provide our own "EFI API" if we are running as a unit test. */
# include "efi-string.h"
# include "string-util-fundamental.h"
#include <stdalign.h>
# define UINTN size_t
# define TEST_STATIC static
#endif
#include "bcd.h"
#include "efi-string.h"
enum {
SIG_BASE_BLOCK = 1718052210, /* regf */
@ -177,7 +168,7 @@ static const KeyValue *get_key_value(const uint8_t *bcd, uint32_t bcd_len, const
return NULL;
if (BAD_OFFSET(key->key_values_offset, sizeof(uint32_t) * (uint64_t) key->n_key_values, bcd_len) ||
(UINTN)(bcd + key->key_values_offset) % sizeof(uint32_t) != 0)
(uintptr_t) (bcd + key->key_values_offset) % alignof(uint32_t) != 0)
return NULL;
const uint32_t *key_value_list = (const uint32_t *) (bcd + key->key_values_offset);
@ -227,7 +218,7 @@ static const KeyValue *get_key_value(const uint8_t *bcd, uint32_t bcd_len, const
* (it always has the GUID 9dea862c-5cdd-4e70-acc1-f32b344d4795). If it contains more than
* one GUID, the BCD is multi-boot and we stop looking. Otherwise we take that GUID, look it
* up, and return its description property. */
TEST_STATIC char16_t *get_bcd_title(uint8_t *bcd, UINTN bcd_len) {
char16_t *get_bcd_title(uint8_t *bcd, size_t bcd_len) {
assert(bcd);
if (HIVE_CELL_OFFSET >= bcd_len)
@ -263,13 +254,13 @@ TEST_STATIC char16_t *get_bcd_title(uint8_t *bcd, UINTN bcd_len) {
char order_guid[sizeof("{00000000-0000-0000-0000-000000000000}\0")];
if (displayorder_value->data_type != REG_MULTI_SZ ||
displayorder_value->data_size != sizeof(char16_t[sizeof(order_guid)]) ||
(UINTN)(bcd + displayorder_value->data_offset) % sizeof(char16_t) != 0)
(uintptr_t) (bcd + displayorder_value->data_offset) % alignof(char16_t) != 0)
/* BCD is multi-boot. */
return NULL;
/* Keys are stored as ASCII in registry hives if the data fits (and GUIDS always should). */
char16_t *order_guid_utf16 = (char16_t *) (bcd + displayorder_value->data_offset);
for (UINTN i = 0; i < sizeof(order_guid) - 2; i++) {
for (size_t i = 0; i < sizeof(order_guid) - 2; i++) {
char16_t c = order_guid_utf16[i];
switch (c) {
case '-':
@ -305,7 +296,7 @@ TEST_STATIC char16_t *get_bcd_title(uint8_t *bcd, UINTN bcd_len) {
if (description_value->data_type != REG_SZ ||
description_value->data_size < sizeof(char16_t) ||
description_value->data_size % sizeof(char16_t) != 0 ||
(UINTN)(bcd + description_value->data_offset) % sizeof(char16_t))
(uintptr_t) (bcd + description_value->data_offset) % alignof(char16_t))
return NULL;
/* The data should already be NUL-terminated. */

View File

@ -1,7 +1,7 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
#include <efi.h>
#include <stdint.h>
#include <uchar.h>
char16_t *get_bcd_title(uint8_t *bcd, UINTN bcd_len);
char16_t *get_bcd_title(uint8_t *bcd, size_t bcd_len);

View File

@ -429,7 +429,7 @@ static char16_t *update_timeout_efivar(uint32_t *t, bool inc) {
}
static bool unicode_supported(void) {
static INTN cache = -1;
static int cache = -1;
if (cache < 0)
/* Basic unicode box drawing support is mandated by the spec, but it does
@ -1328,7 +1328,7 @@ static void config_entry_parse_tries(
static void config_entry_bump_counters(ConfigEntry *entry, EFI_FILE *root_dir) {
_cleanup_free_ char16_t* old_path = NULL, *new_path = NULL;
_cleanup_(file_closep) EFI_FILE *handle = NULL;
_cleanup_freepool_ EFI_FILE_INFO *file_info = NULL;
_cleanup_free_ EFI_FILE_INFO *file_info = NULL;
UINTN file_info_size;
EFI_STATUS err;
@ -1575,7 +1575,7 @@ static void config_load_entries(
const char16_t *loaded_image_path) {
_cleanup_(file_closep) EFI_FILE *entries_dir = NULL;
_cleanup_freepool_ EFI_FILE_INFO *f = NULL;
_cleanup_free_ EFI_FILE_INFO *f = NULL;
UINTN f_size = 0;
EFI_STATUS err;
@ -1886,7 +1886,7 @@ static ConfigEntry *config_entry_add_loader_auto(
static void config_entry_add_osx(Config *config) {
EFI_STATUS err;
UINTN n_handles = 0;
_cleanup_freepool_ EFI_HANDLE *handles = NULL;
_cleanup_free_ EFI_HANDLE *handles = NULL;
assert(config);
@ -1917,7 +1917,7 @@ static void config_entry_add_osx(Config *config) {
}
static EFI_STATUS boot_windows_bitlocker(void) {
_cleanup_freepool_ EFI_HANDLE *handles = NULL;
_cleanup_free_ EFI_HANDLE *handles = NULL;
UINTN n_handles;
EFI_STATUS err;
@ -1955,7 +1955,7 @@ static EFI_STATUS boot_windows_bitlocker(void) {
if (!found)
return EFI_NOT_FOUND;
_cleanup_freepool_ uint16_t *boot_order = NULL;
_cleanup_free_ uint16_t *boot_order = NULL;
UINTN boot_order_size;
/* There can be gaps in Boot#### entries. Instead of iterating over the full
@ -2031,7 +2031,7 @@ static void config_entry_add_unified(
EFI_FILE *root_dir) {
_cleanup_(file_closep) EFI_FILE *linux_dir = NULL;
_cleanup_freepool_ EFI_FILE_INFO *f = NULL;
_cleanup_free_ EFI_FILE_INFO *f = NULL;
UINTN f_size = 0;
EFI_STATUS err;
@ -2238,7 +2238,7 @@ static EFI_STATUS initrd_prepare(
EFI_STATUS err;
UINTN size = 0;
_cleanup_freepool_ uint8_t *initrd = NULL;
_cleanup_free_ uint8_t *initrd = NULL;
STRV_FOREACH(i, entry->initrd) {
_cleanup_free_ char16_t *o = options;
@ -2252,7 +2252,7 @@ static EFI_STATUS initrd_prepare(
if (err != EFI_SUCCESS)
return err;
_cleanup_freepool_ EFI_FILE_INFO *info = NULL;
_cleanup_free_ EFI_FILE_INFO *info = NULL;
err = get_file_info_harder(handle, &info, NULL);
if (err != EFI_SUCCESS)
return err;
@ -2288,7 +2288,7 @@ static EFI_STATUS image_start(
_cleanup_(devicetree_cleanup) struct devicetree_state dtstate = {};
_cleanup_(unload_imagep) EFI_HANDLE image = NULL;
_cleanup_freepool_ EFI_DEVICE_PATH *path = NULL;
_cleanup_free_ EFI_DEVICE_PATH *path = NULL;
EFI_STATUS err;
assert(entry);
@ -2307,7 +2307,7 @@ static EFI_STATUS image_start(
return log_error_status_stall(err, L"Error making file device path: %r", err);
UINTN initrd_size = 0;
_cleanup_freepool_ void *initrd = NULL;
_cleanup_free_ void *initrd = NULL;
_cleanup_free_ char16_t *options_initrd = NULL;
err = initrd_prepare(image_root, entry, &options_initrd, &initrd, &initrd_size);
if (err != EFI_SUCCESS)

View File

@ -320,9 +320,9 @@ EFI_STATUS pack_cpio(
_cleanup_(file_closep) EFI_FILE *root = NULL, *extra_dir = NULL;
UINTN dirent_size = 0, buffer_size = 0, n_items = 0, n_allocated = 0;
_cleanup_free_ char16_t *rel_dropin_dir = NULL;
_cleanup_freepool_ EFI_FILE_INFO *dirent = NULL;
_cleanup_free_ EFI_FILE_INFO *dirent = NULL;
_cleanup_(strv_freep) char16_t **items = NULL;
_cleanup_freepool_ void *buffer = NULL;
_cleanup_free_ void *buffer = NULL;
uint32_t inode = 1; /* inode counter, so that each item gets a new inode */
EFI_STATUS err;

View File

@ -73,7 +73,7 @@ static EFI_STATUS devicetree_fixup(struct devicetree_state *state, UINTN len) {
EFI_STATUS devicetree_install(struct devicetree_state *state, EFI_FILE *root_dir, char16_t *name) {
_cleanup_(file_closep) EFI_FILE *handle = NULL;
_cleanup_freepool_ EFI_FILE_INFO *info = NULL;
_cleanup_free_ EFI_FILE_INFO *info = NULL;
UINTN len;
EFI_STATUS err;

View File

@ -12,7 +12,7 @@ static EFI_STATUS load_one_driver(
const char16_t *fname) {
_cleanup_(unload_imagep) EFI_HANDLE image = NULL;
_cleanup_freepool_ EFI_DEVICE_PATH *path = NULL;
_cleanup_free_ EFI_DEVICE_PATH *path = NULL;
_cleanup_free_ char16_t *spath = NULL;
EFI_STATUS err;
@ -51,7 +51,7 @@ static EFI_STATUS load_one_driver(
}
static EFI_STATUS reconnect(void) {
_cleanup_freepool_ EFI_HANDLE *handles = NULL;
_cleanup_free_ EFI_HANDLE *handles = NULL;
UINTN n_handles = 0;
EFI_STATUS err;
@ -78,7 +78,7 @@ EFI_STATUS load_drivers(
EFI_FILE *root_dir) {
_cleanup_(file_closep) EFI_FILE *drivers_dir = NULL;
_cleanup_freepool_ EFI_FILE_INFO *dirent = NULL;
_cleanup_free_ EFI_FILE_INFO *dirent = NULL;
UINTN dirent_size = 0, n_succeeded = 0;
EFI_STATUS err;

View File

@ -17,7 +17,7 @@ static EFI_STATUS tpm1_measure_to_pcr_and_event_log(
UINTN buffer_size,
const char16_t *description) {
_cleanup_freepool_ TCG_PCR_EVENT *tcg_event = NULL;
_cleanup_free_ TCG_PCR_EVENT *tcg_event = NULL;
EFI_PHYSICAL_ADDRESS event_log_last;
uint32_t event_number = 1;
UINTN desc_len;
@ -51,7 +51,7 @@ static EFI_STATUS tpm2_measure_to_pcr_and_event_log(
uint64_t buffer_size,
const char16_t *description) {
_cleanup_freepool_ EFI_TCG2_EVENT *tcg_event = NULL;
_cleanup_free_ EFI_TCG2_EVENT *tcg_event = NULL;
UINTN desc_len;
assert(tcg);

View File

@ -282,7 +282,7 @@ EFI_STATUS pe_file_locate_sections(
const char * const sections[],
UINTN *offsets,
UINTN *sizes) {
_cleanup_freepool_ PeSectionHeader *section_table = NULL;
_cleanup_free_ PeSectionHeader *section_table = NULL;
_cleanup_(file_closep) EFI_FILE *handle = NULL;
DosFileHeader dos;
PeFileHeader pe;

View File

@ -18,7 +18,7 @@
#define HASH_VALUE_SIZE 32
static EFI_STATUS acquire_rng(UINTN size, void **ret) {
_cleanup_freepool_ void *data = NULL;
_cleanup_free_ void *data = NULL;
EFI_RNG_PROTOCOL *rng;
EFI_STATUS err;
@ -90,7 +90,7 @@ static EFI_STATUS hash_many(
UINTN n,
void **ret) {
_cleanup_freepool_ void *output = NULL;
_cleanup_free_ void *output = NULL;
assert(old_seed);
assert(system_token_size == 0 || system_token);
@ -122,7 +122,7 @@ static EFI_STATUS mangle_random_seed(
void **ret_new_seed,
void **ret_for_kernel) {
_cleanup_freepool_ void *new_seed = NULL, *for_kernel = NULL;
_cleanup_free_ void *new_seed = NULL, *for_kernel = NULL;
EFI_STATUS err;
UINTN n;
@ -230,10 +230,10 @@ static void validate_sha256(void) {
}
EFI_STATUS process_random_seed(EFI_FILE *root_dir, RandomSeedMode mode) {
_cleanup_freepool_ void *seed = NULL, *new_seed = NULL, *rng = NULL, *for_kernel = NULL, *system_token = NULL;
_cleanup_free_ void *seed = NULL, *new_seed = NULL, *rng = NULL, *for_kernel = NULL, *system_token = NULL;
_cleanup_(file_closep) EFI_FILE *handle = NULL;
UINTN size, rsize, wsize, system_token_size = 0;
_cleanup_freepool_ EFI_FILE_INFO *info = NULL;
_cleanup_free_ EFI_FILE_INFO *info = NULL;
uint64_t uefi_monotonic_counter = 0;
EFI_STATUS err;

View File

@ -260,7 +260,7 @@ EFI_STATUS graphics_splash(const uint8_t *content, UINTN len, const EFI_GRAPHICS
struct bmp_dib *dib;
struct bmp_map *map;
const uint8_t *pixmap;
_cleanup_freepool_ void *blt = NULL;
_cleanup_free_ void *blt = NULL;
UINTN x_pos = 0;
UINTN y_pos = 0;
EFI_STATUS err;

View File

@ -169,8 +169,8 @@ EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table) {
UINTN cmdline_len = 0, linux_size, initrd_size, dt_size;
UINTN credential_initrd_size = 0, global_credential_initrd_size = 0, sysext_initrd_size = 0;
_cleanup_freepool_ void *credential_initrd = NULL, *global_credential_initrd = NULL;
_cleanup_freepool_ void *sysext_initrd = NULL;
_cleanup_free_ void *credential_initrd = NULL, *global_credential_initrd = NULL;
_cleanup_free_ void *sysext_initrd = NULL;
EFI_PHYSICAL_ADDRESS linux_base, initrd_base, dt_base;
_cleanup_(devicetree_cleanup) struct devicetree_state dt_state = {};
EFI_LOADED_IMAGE_PROTOCOL *loaded_image;

View File

@ -1,6 +1,7 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include "alloc-util.h"
#include "bcd.h"
#include "compress.h"
#include "fileio.h"
#include "tests.h"

View File

@ -241,7 +241,7 @@ void efivar_set_time_usec(const EFI_GUID *vendor, const char16_t *name, uint64_t
efivar_set(vendor, name, str, 0);
}
static INTN utf8_to_16(const char *stra, char16_t *c) {
static int utf8_to_16(const char *stra, char16_t *c) {
char16_t unichar;
UINTN len;
@ -309,7 +309,7 @@ char16_t *xstra_to_str(const char *stra) {
strlen = 0;
i = 0;
while (i < len) {
INTN utf8len;
int utf8len;
utf8len = utf8_to_16(stra + i, str + strlen);
if (utf8len <= 0) {
@ -340,7 +340,7 @@ char16_t *xstra_to_path(const char *stra) {
strlen = 1;
i = 0;
while (i < len) {
INTN utf8len;
int utf8len;
utf8len = utf8_to_16(stra + i, str + strlen);
if (utf8len <= 0) {
@ -378,7 +378,7 @@ EFI_STATUS file_read(EFI_FILE *dir, const char16_t *name, UINTN off, UINTN size,
return err;
if (size == 0) {
_cleanup_freepool_ EFI_FILE_INFO *info = NULL;
_cleanup_free_ EFI_FILE_INFO *info = NULL;
err = get_file_info_harder(handle, &info, NULL);
if (err != EFI_SUCCESS)
@ -483,7 +483,7 @@ EFI_STATUS get_file_info_harder(
UINTN *ret_size) {
UINTN size = offsetof(EFI_FILE_INFO, FileName) + 256;
_cleanup_freepool_ EFI_FILE_INFO *fi = NULL;
_cleanup_free_ EFI_FILE_INFO *fi = NULL;
EFI_STATUS err;
assert(handle);
@ -585,7 +585,7 @@ EFI_STATUS open_directory(
EFI_FILE **ret) {
_cleanup_(file_closep) EFI_FILE *dir = NULL;
_cleanup_freepool_ EFI_FILE_INFO *file_info = NULL;
_cleanup_free_ EFI_FILE_INFO *file_info = NULL;
EFI_STATUS err;
assert(root);

View File

@ -41,7 +41,6 @@ static inline void freep(void *p) {
free(*(void **) p);
}
#define _cleanup_freepool_ _cleanup_free_
#define _cleanup_free_ _cleanup_(freep)
_malloc_ _alloc_(1) _returns_nonnull_ _warn_unused_result_
@ -144,22 +143,16 @@ EFI_STATUS open_directory(EFI_FILE *root_dir, const char16_t *path, EFI_FILE **r
/* Conversion between EFI_PHYSICAL_ADDRESS and pointers is not obvious. The former is always 64bit, even on
* 32bit archs. And gcc complains if we cast a pointer to an integer of a different size. Hence let's do the
* conversion indirectly: first into UINTN (which is defined by UEFI to have the same size as a pointer), and
* then extended to EFI_PHYSICAL_ADDRESS. */
* conversion indirectly: first into uintptr_t and then extended to EFI_PHYSICAL_ADDRESS. */
static inline EFI_PHYSICAL_ADDRESS POINTER_TO_PHYSICAL_ADDRESS(const void *p) {
return (EFI_PHYSICAL_ADDRESS) (UINTN) p;
return (EFI_PHYSICAL_ADDRESS) (uintptr_t) p;
}
static inline void *PHYSICAL_ADDRESS_TO_POINTER(EFI_PHYSICAL_ADDRESS addr) {
#if __SIZEOF_POINTER__ == 4
/* On 32bit systems the address might not be convertible (as pointers are 32bit but
* EFI_PHYSICAL_ADDRESS 64bit) */
assert(addr <= UINT32_MAX);
#elif __SIZEOF_POINTER__ != 8
#error "Unexpected pointer size"
#endif
return (void*) (UINTN) addr;
assert(addr <= UINTPTR_MAX);
return (void *) (uintptr_t) addr;
}
uint64_t get_os_indications_supported(void);

View File

@ -90,7 +90,7 @@ static EFI_STATUS try_gpt(
EFI_LBA *ret_backup_lba, /* May be changed even on error! */
HARDDRIVE_DEVICE_PATH *ret_hd) {
_cleanup_freepool_ EFI_PARTITION_ENTRY *entries = NULL;
_cleanup_free_ EFI_PARTITION_ENTRY *entries = NULL;
union GptHeaderBuffer gpt;
EFI_STATUS err;
uint32_t crc32;
@ -191,7 +191,7 @@ static EFI_STATUS find_device(EFI_HANDLE *device, EFI_DEVICE_PATH **ret_device_p
return EFI_NOT_FOUND;
/* Chop off the partition part, leaving us with the full path to the disk itself. */
_cleanup_freepool_ EFI_DEVICE_PATH *disk_path = NULL;
_cleanup_free_ EFI_DEVICE_PATH *disk_path = NULL;
EFI_DEVICE_PATH *p = disk_path = path_chop(partition_path, part_node);
EFI_HANDLE disk_handle;
@ -254,7 +254,7 @@ static EFI_STATUS find_device(EFI_HANDLE *device, EFI_DEVICE_PATH **ret_device_p
}
EFI_STATUS xbootldr_open(EFI_HANDLE *device, EFI_HANDLE *ret_device, EFI_FILE **ret_root_dir) {
_cleanup_freepool_ EFI_DEVICE_PATH *partition_path = NULL;
_cleanup_free_ EFI_DEVICE_PATH *partition_path = NULL;
EFI_HANDLE new_device;
EFI_FILE *root_dir;
EFI_STATUS err;