]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
boot: Use xmalloc
authorJan Janssen <medhefgo@web.de>
Sat, 28 May 2022 17:36:21 +0000 (19:36 +0200)
committerJan Janssen <medhefgo@web.de>
Thu, 9 Jun 2022 10:50:13 +0000 (12:50 +0200)
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.

src/boot/efi/boot.c
src/boot/efi/cpio.c
src/boot/efi/efi-string.c
src/boot/efi/initrd.c
src/boot/efi/measure.c
src/boot/efi/pe.c
src/boot/efi/random-seed.c
src/boot/efi/stub.c
src/boot/efi/util.c
src/boot/efi/util.h
src/boot/efi/xbootldr.c

index dd5dad71f322cbd9fd936858c88cf8c28a3835ec..f00f004eaa82eae8bc690ecc9136b0b4fd6c03c2 100644 (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;
index aebffb51b7c317fbb1539bf7d6b7e7757145e7b9..454d79e11808cc5066139edf8078ec90ec22bff1 100644 (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;
                 }
 
index 505830e310b9b2a5be2a72d758feb259dd232d5f..80ef0ff076f9c5b582ac029d0b7be90b60d684ec 100644 (file)
@@ -7,7 +7,6 @@
 
 #ifdef SD_BOOT
 #  include "util.h"
-#  define xmalloc(n) xallocate_pool(n)
 #else
 #  include <stdlib.h>
 #  include "macro.h"
index 2d0984990bf8abc686bb20a91f8434335df59a6a..1b4e746483001886a1b74315aa89730188bb24a2 100644 (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;
 }
index b388d4bc4050a5dcc30002b32fae4244f4f76bee..def42fece4f179de5298a57a91c28d51cae6510a 100644 (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),
index f9bd57f0ce5e658cddffb3a79700938e1390daaf..c1a4064896196f1eb1cd52f09a87ad75be7b91ed 100644 (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;
 
index f1221ae2a760529055c44fcf937d6ddeb4751e95..116ccfe515b53ea10a21d8b2c0cb96d73275c3e2 100644 (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);
index f335d3782d58b4accde45233807107dff2064bf5..9fe521efd40508c7e5c8360cc8561cc42ad56c42 100644 (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];
index 7d607e04c7d9ff8004ca4568d0700b909f4707b4..69bca420aeb54cd9aafe12047533c8dc854527f2 100644 (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;
 }
 
index 2245c21ce764f59058078b6972395ffdc916c5be..8e28f5b1bea0a2b9a531165a304476855800947c 100644 (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);
 
index 84e443135c3846c1ceb2832d25cd0b626b1be7fa..ade2e7f3d377448bdd1443570baf1b8ca59d6dc2 100644 (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,