boot: Use xmalloc

This drops the unused xnew0 and xallocate_zero_pool as there is only two
users of it. _cleanup_freepool_ will be phased out once the types in the
declarations are changed/renamed.
This commit is contained in:
Jan Janssen
2022-06-09 12:50:13 +02:00
parent 9148312fab
commit 0af26643d0
11 changed files with 72 additions and 83 deletions
+42 -43
View File
@@ -318,7 +318,7 @@ static BOOLEAN line_edit(
case KEYPRESS(0, CHAR_CARRIAGE_RETURN, 0): /* EZpad Mini 4s firmware sends malformed events */
case KEYPRESS(0, CHAR_CARRIAGE_RETURN, CHAR_CARRIAGE_RETURN): /* Teclast X98+ II firmware sends malformed events */
if (!streq16(line, *line_in)) {
FreePool(*line_in);
free(*line_in);
*line_in = TAKE_PTR(line);
}
return TRUE;
@@ -741,7 +741,7 @@ static BOOLEAN menu_run(
}
if (timeout_remain > 0) {
FreePool(status);
free(status);
status = xpool_print(L"Boot in %u s.", timeout_remain);
}
@@ -872,7 +872,7 @@ static BOOLEAN menu_run(
case KEYPRESS(0, 0, 'd'):
case KEYPRESS(0, 0, 'D'):
if (config->idx_default_efivar != idx_highlight) {
FreePool(config->entry_default_efivar);
free(config->entry_default_efivar);
config->entry_default_efivar = xstrdup16(config->entries[idx_highlight]->id);
config->idx_default_efivar = idx_highlight;
status = xstrdup16(u"Default boot entry selected.");
@@ -1037,11 +1037,10 @@ static void config_add_entry(Config *config, ConfigEntry *entry) {
assert(config->entry_count < IDX_MAX);
if ((config->entry_count & 15) == 0) {
UINTN i = config->entry_count + 16;
config->entries = xreallocate_pool(
config->entries = xrealloc(
config->entries,
sizeof(void *) * config->entry_count,
sizeof(void *) * i);
sizeof(void *) * (config->entry_count + 16));
}
config->entries[config->entry_count++] = entry;
}
@@ -1050,20 +1049,20 @@ static void config_entry_free(ConfigEntry *entry) {
if (!entry)
return;
FreePool(entry->id);
FreePool(entry->title_show);
FreePool(entry->title);
FreePool(entry->sort_key);
FreePool(entry->version);
FreePool(entry->machine_id);
FreePool(entry->loader);
FreePool(entry->devicetree);
FreePool(entry->options);
free(entry->id);
free(entry->title_show);
free(entry->title);
free(entry->sort_key);
free(entry->version);
free(entry->machine_id);
free(entry->loader);
free(entry->devicetree);
free(entry->options);
strv_free(entry->initrd);
FreePool(entry->path);
FreePool(entry->current_name);
FreePool(entry->next_name);
FreePool(entry);
free(entry->path);
free(entry->current_name);
free(entry->next_name);
free(entry);
}
static inline void config_entry_freep(ConfigEntry **entry) {
@@ -1174,7 +1173,7 @@ static void config_defaults_load_from_file(Config *config, CHAR8 *content) {
log_error_stall(L"Unsupported special entry identifier: %a", value);
continue;
}
FreePool(config->entry_default_config);
free(config->entry_default_config);
config->entry_default_config = xstra_to_str(value);
continue;
}
@@ -1416,7 +1415,7 @@ static void config_entry_bump_counters(ConfigEntry *entry, EFI_FILE *root_dir) {
/* If the file we just renamed is the loader path, then let's update that. */
if (streq16(entry->loader, old_path)) {
FreePool(entry->loader);
free(entry->loader);
entry->loader = TAKE_PTR(new_path);
}
}
@@ -1451,31 +1450,31 @@ static void config_entry_add_type1(
while ((line = line_get_key_value(content, (CHAR8 *)" \t", &pos, &key, &value))) {
if (streq8((char *) key, "title")) {
FreePool(entry->title);
free(entry->title);
entry->title = xstra_to_str(value);
continue;
}
if (streq8((char *) key, "sort-key")) {
FreePool(entry->sort_key);
free(entry->sort_key);
entry->sort_key = xstra_to_str(value);
continue;
}
if (streq8((char *) key, "version")) {
FreePool(entry->version);
free(entry->version);
entry->version = xstra_to_str(value);
continue;
}
if (streq8((char *) key, "machine-id")) {
FreePool(entry->machine_id);
free(entry->machine_id);
entry->machine_id = xstra_to_str(value);
continue;
}
if (streq8((char *) key, "linux")) {
FreePool(entry->loader);
free(entry->loader);
entry->type = LOADER_LINUX;
entry->loader = xstra_to_path(value);
entry->key = 'l';
@@ -1484,7 +1483,7 @@ static void config_entry_add_type1(
if (streq8((char *) key, "efi")) {
entry->type = LOADER_EFI;
FreePool(entry->loader);
free(entry->loader);
entry->loader = xstra_to_path(value);
/* do not add an entry for ourselves */
@@ -1505,13 +1504,13 @@ static void config_entry_add_type1(
}
if (streq8((char *) key, "devicetree")) {
FreePool(entry->devicetree);
free(entry->devicetree);
entry->devicetree = xstra_to_path(value);
continue;
}
if (streq8((char *) key, "initrd")) {
entry->initrd = xreallocate_pool(
entry->initrd = xrealloc(
entry->initrd,
n_initrd == 0 ? 0 : (n_initrd + 1) * sizeof(UINT16 *),
(n_initrd + 2) * sizeof(UINT16 *));
@@ -1528,7 +1527,7 @@ static void config_entry_add_type1(
CHAR16 *s;
s = xpool_print(L"%s %s", entry->options, new);
FreePool(entry->options);
free(entry->options);
entry->options = s;
} else
entry->options = TAKE_PTR(new);
@@ -2141,49 +2140,49 @@ static void config_entry_add_unified(
/* read properties from the embedded os-release file */
while ((line = line_get_key_value(content, (CHAR8 *)"=", &pos, &key, &value))) {
if (streq8((char *) key, "PRETTY_NAME")) {
FreePool(os_pretty_name);
free(os_pretty_name);
os_pretty_name = xstra_to_str(value);
continue;
}
if (streq8((char *) key, "IMAGE_ID")) {
FreePool(os_image_id);
free(os_image_id);
os_image_id = xstra_to_str(value);
continue;
}
if (streq8((char *) key, "NAME")) {
FreePool(os_name);
free(os_name);
os_name = xstra_to_str(value);
continue;
}
if (streq8((char *) key, "ID")) {
FreePool(os_id);
free(os_id);
os_id = xstra_to_str(value);
continue;
}
if (streq8((char *) key, "IMAGE_VERSION")) {
FreePool(os_image_version);
free(os_image_version);
os_image_version = xstra_to_str(value);
continue;
}
if (streq8((char *) key, "VERSION")) {
FreePool(os_version);
free(os_version);
os_version = xstra_to_str(value);
continue;
}
if (streq8((char *) key, "VERSION_ID")) {
FreePool(os_version_id);
free(os_version_id);
os_version_id = xstra_to_str(value);
continue;
}
if (streq8((char *) key, "BUILD_ID")) {
FreePool(os_build_id);
free(os_build_id);
os_build_id = xstra_to_str(value);
continue;
}
@@ -2308,7 +2307,7 @@ static EFI_STATUS initrd_prepare(
UINTN new_size, read_size = info->FileSize;
if (__builtin_add_overflow(size, read_size, &new_size))
return EFI_OUT_OF_RESOURCES;
initrd = xreallocate_pool(initrd, size, new_size);
initrd = xrealloc(initrd, size, new_size);
err = handle->Read(handle, &read_size, initrd + size);
if (EFI_ERROR(err))
@@ -2422,9 +2421,9 @@ static void config_free(Config *config) {
assert(config);
for (UINTN i = 0; i < config->entry_count; i++)
config_entry_free(config->entries[i]);
FreePool(config->entries);
FreePool(config->entry_default_config);
FreePool(config->entry_oneshot);
free(config->entries);
free(config->entry_default_config);
free(config->entry_oneshot);
}
static void config_write_entries_to_variable(Config *config) {
@@ -2437,7 +2436,7 @@ static void config_write_entries_to_variable(Config *config) {
for (UINTN i = 0; i < config->entry_count; i++)
sz += strsize16(config->entries[i]->id);
p = buffer = xallocate_pool(sz);
p = buffer = xmalloc(sz);
for (UINTN i = 0; i < config->entry_count; i++) {
UINTN l;
+4 -4
View File
@@ -111,7 +111,7 @@ static EFI_STATUS pack_cpio_one(
if (*cpio_buffer_size > UINTN_MAX - l) /* overflow check */
return EFI_OUT_OF_RESOURCES;
a = xreallocate_pool(*cpio_buffer, *cpio_buffer_size, *cpio_buffer_size + l);
a = xrealloc(*cpio_buffer, *cpio_buffer_size, *cpio_buffer_size + l);
*cpio_buffer = a;
a = (CHAR8*) *cpio_buffer + *cpio_buffer_size;
@@ -195,7 +195,7 @@ static EFI_STATUS pack_cpio_dir(
if (*cpio_buffer_size > UINTN_MAX - l) /* overflow check */
return EFI_OUT_OF_RESOURCES;
*cpio_buffer = a = xreallocate_pool(*cpio_buffer, *cpio_buffer_size, *cpio_buffer_size + l);
*cpio_buffer = a = xrealloc(*cpio_buffer, *cpio_buffer_size, *cpio_buffer_size + l);
a = (CHAR8*) *cpio_buffer + *cpio_buffer_size;
memcpy(a, "070701", 6); /* magic ID */
@@ -297,7 +297,7 @@ static EFI_STATUS pack_cpio_trailer(
assert(cpio_buffer_size);
assert_cc(sizeof(trailer) % 4 == 0);
*cpio_buffer = xreallocate_pool(*cpio_buffer, *cpio_buffer_size, *cpio_buffer_size + sizeof(trailer));
*cpio_buffer = xrealloc(*cpio_buffer, *cpio_buffer_size, *cpio_buffer_size + sizeof(trailer));
memcpy((UINT8*) *cpio_buffer + *cpio_buffer_size, trailer, sizeof(trailer));
*cpio_buffer_size += sizeof(trailer);
@@ -401,7 +401,7 @@ EFI_STATUS pack_cpio(
return log_oom();
m = n_items + 16;
items = xreallocate_pool(items, n_allocated * sizeof(UINT16*), m * sizeof(UINT16*));
items = xrealloc(items, n_allocated * sizeof(UINT16*), m * sizeof(UINT16*));
n_allocated = m;
}
-1
View File
@@ -7,7 +7,6 @@
#ifdef SD_BOOT
# include "util.h"
# define xmalloc(n) xallocate_pool(n)
#else
# include <stdlib.h>
# include "macro.h"
+2 -2
View File
@@ -103,7 +103,7 @@ EFI_STATUS initrd_register(
&EfiLoadFile2Protocol, loader,
NULL);
if (EFI_ERROR(err))
FreePool(loader);
free(loader);
return err;
}
@@ -135,6 +135,6 @@ EFI_STATUS initrd_unregister(EFI_HANDLE initrd_handle) {
return err;
initrd_handle = NULL;
FreePool(loader);
free(loader);
return EFI_SUCCESS;
}
+4 -2
View File
@@ -26,7 +26,8 @@ static EFI_STATUS tpm1_measure_to_pcr_and_event_log(
assert(description);
desc_len = strsize16(description);
tcg_event = xallocate_zero_pool(offsetof(TCG_PCR_EVENT, Event) + desc_len);
tcg_event = xmalloc(offsetof(TCG_PCR_EVENT, Event) + desc_len);
memset(tcg_event, 0, offsetof(TCG_PCR_EVENT, Event) + desc_len);
*tcg_event = (TCG_PCR_EVENT) {
.EventSize = desc_len,
.PCRIndex = pcrindex,
@@ -57,7 +58,8 @@ static EFI_STATUS tpm2_measure_to_pcr_and_event_log(
assert(description);
desc_len = strsize16(description);
tcg_event = xallocate_zero_pool(offsetof(EFI_TCG2_EVENT, Event) + desc_len);
tcg_event = xmalloc(offsetof(EFI_TCG2_EVENT, Event) + desc_len);
memset(tcg_event, 0, offsetof(EFI_TCG2_EVENT, Event) + desc_len);
*tcg_event = (EFI_TCG2_EVENT) {
.Size = offsetof(EFI_TCG2_EVENT, Event) + desc_len,
.Header.HeaderSize = sizeof(EFI_TCG2_EVENT_HEADER),
+1 -1
View File
@@ -318,7 +318,7 @@ EFI_STATUS pe_file_locate_sections(
return EFI_LOAD_ERROR;
section_table_len = pe.FileHeader.NumberOfSections * sizeof(struct PeSectionHeader);
section_table = xallocate_pool(section_table_len);
section_table = xmalloc(section_table_len);
if (!section_table)
return EFI_OUT_OF_RESOURCES;
+3 -3
View File
@@ -32,7 +32,7 @@ static EFI_STATUS acquire_rng(UINTN size, void **ret) {
if (!rng)
return EFI_UNSUPPORTED;
data = xallocate_pool(size);
data = xmalloc(size);
err = rng->GetRNG(rng, NULL, size, data);
if (EFI_ERROR(err))
@@ -99,7 +99,7 @@ static EFI_STATUS hash_many(
/* Hashes the specified parameters in counter mode, generating n hash values, with the counter in the
* range counter_start…counter_start+n-1. */
output = xallocate_pool(n * HASH_VALUE_SIZE);
output = xmalloc_multiply(HASH_VALUE_SIZE, n);
for (UINTN i = 0; i < n; i++)
hash_once(old_seed, rng, size,
@@ -274,7 +274,7 @@ EFI_STATUS process_random_seed(EFI_FILE *root_dir, RandomSeedMode mode) {
if (size > RANDOM_MAX_SIZE_MAX)
return log_error_status_stall(EFI_INVALID_PARAMETER, L"Random seed file is too large.");
seed = xallocate_pool(size);
seed = xmalloc(size);
rsize = size;
err = handle->Read(handle, &rsize, seed);
+1 -1
View File
@@ -214,7 +214,7 @@ EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table) {
if ((!secure_boot_enabled() || cmdline_len == 0) && loaded_image->LoadOptionsSize > 0 &&
*(CHAR16 *) loaded_image->LoadOptions > 0x1F) {
cmdline_len = (loaded_image->LoadOptionsSize / sizeof(CHAR16)) * sizeof(CHAR8);
cmdline = cmdline_owned = xallocate_pool(cmdline_len);
cmdline = cmdline_owned = xmalloc(cmdline_len);
for (UINTN i = 0; i < cmdline_len; i++)
cmdline[i] = ((CHAR16 *) loaded_image->LoadOptions)[i];
+12 -12
View File
@@ -115,7 +115,7 @@ EFI_STATUS efivar_get(const EFI_GUID *vendor, const CHAR16 *name, CHAR16 **value
}
/* Make sure a terminating NUL is available at the end */
val = xallocate_pool(size + sizeof(CHAR16));
val = xmalloc(size + sizeof(CHAR16));
memcpy(val, buf, size);
val[size / sizeof(CHAR16) - 1] = 0; /* NUL terminate */
@@ -189,7 +189,7 @@ EFI_STATUS efivar_get_raw(const EFI_GUID *vendor, const CHAR16 *name, CHAR8 **bu
assert(name);
l = sizeof(CHAR16 *) * EFI_MAXIMUM_VARIABLE_SIZE;
buf = xallocate_pool(l);
buf = xmalloc(l);
err = RT->GetVariable((CHAR16 *) name, (EFI_GUID *) vendor, NULL, &l, buf);
if (!EFI_ERROR(err)) {
@@ -391,7 +391,7 @@ EFI_STATUS file_read(EFI_FILE *dir, const CHAR16 *name, UINTN off, UINTN size, C
/* Allocate some extra bytes to guarantee the result is NUL-terminated for CHAR8 and CHAR16 strings. */
UINTN extra = size % sizeof(CHAR16) + sizeof(CHAR16);
buf = xallocate_pool(size + extra);
buf = xmalloc(size + extra);
err = handle->Read(handle, &size, buf);
if (EFI_ERROR(err))
return err;
@@ -486,11 +486,11 @@ EFI_STATUS get_file_info_harder(
/* A lot like LibFileInfo() but with useful error propagation */
fi = xallocate_pool(size);
fi = xmalloc(size);
err = handle->GetInfo(handle, &GenericFileInfo, &size, fi);
if (err == EFI_BUFFER_TOO_SMALL) {
FreePool(fi);
fi = xallocate_pool(size); /* GetInfo tells us the required size, let's use that now */
free(fi);
fi = xmalloc(size); /* GetInfo tells us the required size, let's use that now */
err = handle->GetInfo(handle, &GenericFileInfo, &size, fi);
}
@@ -527,15 +527,15 @@ EFI_STATUS readdir_harder(
* file name length.
* As a side effect, most readdir_harder() calls will now be slightly faster. */
sz = sizeof(EFI_FILE_INFO) + 256 * sizeof(CHAR16);
*buffer = xallocate_pool(sz);
*buffer = xmalloc(sz);
*buffer_size = sz;
} else
sz = *buffer_size;
err = handle->Read(handle, &sz, *buffer);
if (err == EFI_BUFFER_TOO_SMALL) {
FreePool(*buffer);
*buffer = xallocate_pool(sz);
free(*buffer);
*buffer = xmalloc(sz);
*buffer_size = sz;
err = handle->Read(handle, &sz, *buffer);
}
@@ -544,7 +544,7 @@ EFI_STATUS readdir_harder(
if (sz == 0) {
/* End of directory */
FreePool(*buffer);
free(*buffer);
*buffer = NULL;
*buffer_size = 0;
}
@@ -568,9 +568,9 @@ CHAR16 **strv_free(CHAR16 **v) {
return NULL;
for (CHAR16 **i = v; *i; i++)
FreePool(*i);
free(*i);
FreePool(v);
free(v);
return NULL;
}
+1 -12
View File
@@ -72,19 +72,8 @@ static inline void *xrealloc(void *p, size_t old_size, size_t new_size) {
return r;
}
#define xnew_alloc(type, n, alloc) \
({ \
UINTN _alloc_size; \
assert_se(!__builtin_mul_overflow(sizeof(type), (n), &_alloc_size)); \
(type *) alloc(_alloc_size); \
})
#define xallocate_pool(size) ASSERT_SE_PTR(AllocatePool(size))
#define xallocate_zero_pool(size) ASSERT_SE_PTR(AllocateZeroPool(size))
#define xreallocate_pool(p, old_size, new_size) ASSERT_SE_PTR(ReallocatePool((p), (old_size), (new_size)))
#define xpool_print(fmt, ...) ((CHAR16 *) ASSERT_SE_PTR(PoolPrint((fmt), ##__VA_ARGS__)))
#define xnew(type, n) xnew_alloc(type, (n), xallocate_pool)
#define xnew0(type, n) xnew_alloc(type, (n), xallocate_zero_pool)
#define xnew(type, n) ((type *) xmalloc_multiply(sizeof(type), (n)))
EFI_STATUS parse_boolean(const CHAR8 *v, BOOLEAN *b);
+2 -2
View File
@@ -17,7 +17,7 @@ static EFI_DEVICE_PATH *path_chop(EFI_DEVICE_PATH *path, EFI_DEVICE_PATH *node)
assert(node);
UINTN len = (UINT8 *) node - (UINT8 *) path;
EFI_DEVICE_PATH *chopped = xallocate_pool(len + END_DEVICE_PATH_LENGTH);
EFI_DEVICE_PATH *chopped = xmalloc(len + END_DEVICE_PATH_LENGTH);
memcpy(chopped, path, len);
SetDevicePathEndNode((EFI_DEVICE_PATH *) ((UINT8 *) chopped + len));
@@ -101,7 +101,7 @@ static EFI_STATUS try_gpt(
/* Now load the GPT entry table */
size = ALIGN_TO((UINTN) gpt.gpt_header.SizeOfPartitionEntry * (UINTN) gpt.gpt_header.NumberOfPartitionEntries, 512);
entries = xallocate_pool(size);
entries = xmalloc(size);
err = block_io->ReadBlocks(
block_io,