From: Jan Janssen Date: Tue, 24 May 2022 08:07:07 +0000 (+0200) Subject: boot: Use strcmp8/16 X-Git-Tag: v252-rc1~892^2~13 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ec436bdf1698b1dcc40b4ab96c5d097fb68583d3;p=thirdparty%2Fsystemd.git boot: Use strcmp8/16 This also replaces streq and similar functions for consistency. Note that streq16 is null pointer safe, so streq_ptr can be safely replaced too. --- diff --git a/src/boot/efi/bcd.c b/src/boot/efi/bcd.c index 14d74ae3872..11f2a81c10c 100644 --- a/src/boot/efi/bcd.c +++ b/src/boot/efi/bcd.c @@ -17,7 +17,6 @@ # define UINT32 uint32_t # define UINT64 uint64_t # define UINTN size_t -# define strncaseeqa(a, b, n) strncaseeq((a), (b), (n)) # define TEST_STATIC static #endif @@ -136,7 +135,7 @@ static const Key *get_subkey(const UINT8 *bcd, UINT32 bcd_len, UINT32 offset, co return NULL; for (UINT16 i = 0; i < subkey->n_entries; i++) { - if (!strncaseeqa(name, subkey->entries[i].name_hint, sizeof(subkey->entries[i].name_hint))) + if (!strncaseeq8((char *) name, (char *) subkey->entries[i].name_hint, sizeof(subkey->entries[i].name_hint))) continue; const Key *key = get_key(bcd, bcd_len, subkey->entries[i].key_offset, name); @@ -165,7 +164,7 @@ static const Key *get_key(const UINT8 *bcd, UINT32 bcd_len, UINT32 offset, const return NULL; if (*name) { - if (strncaseeqa(name, key->key_name, key->key_name_len) && strlen8((const char *) name) == key->key_name_len) + if (strncaseeq8((char *) name, (char *) key->key_name, key->key_name_len) && strlen8((const char *) name) == key->key_name_len) name += key->key_name_len; else return NULL; @@ -210,7 +209,7 @@ static const KeyValue *get_key_value(const UINT8 *bcd, UINT32 bcd_len, const Key if (BAD_OFFSET(kv->data_offset, kv->data_size, bcd_len)) continue; - if (strncaseeqa(name, kv->name, kv->name_len) && strlen8((const char *) name) == kv->name_len) + if (strncaseeq8((char *)name, (char *) kv->name, kv->name_len) && strlen8((const char *) name) == kv->name_len) return kv; } diff --git a/src/boot/efi/boot.c b/src/boot/efi/boot.c index 52be6828967..9a4dda5dd72 100644 --- a/src/boot/efi/boot.c +++ b/src/boot/efi/boot.c @@ -317,7 +317,7 @@ static BOOLEAN line_edit( case KEYPRESS(0, 0, CHAR_CARRIAGE_RETURN): 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 (!streq(line, *line_in)) { + if (!streq16(line, *line_in)) { FreePool(*line_in); *line_in = TAKE_PTR(line); } @@ -554,7 +554,7 @@ static void print_status(Config *config, CHAR16 *loaded_image_path) { Print(L" config entry: %" PRIuN L"/%" PRIuN L"\n", i + 1, config->entry_count); ps_string(L" id: %s\n", entry->id); ps_string(L" title: %s\n", entry->title); - ps_string(L" title show: %s\n", streq_ptr(entry->title, entry->title_show) ? NULL : entry->title_show); + ps_string(L" title show: %s\n", streq16(entry->title, entry->title_show) ? NULL : entry->title_show); ps_string(L" sort key: %s\n", entry->sort_key); ps_string(L" version: %s\n", entry->version); ps_string(L" machine-id: %s\n", entry->machine_id); @@ -1154,10 +1154,10 @@ static void config_defaults_load_from_file(Config *config, CHAR8 *content) { assert(content); while ((line = line_get_key_value(content, (CHAR8 *)" \t", &pos, &key, &value))) { - if (strcmpa((CHAR8 *)"timeout", key) == 0) { - if (strcmpa((CHAR8*) "menu-force", value) == 0) + if (streq8((char *) key, "timeout")) { + if (streq8((char *) value, "menu-force")) config->timeout_sec_config = TIMEOUT_MENU_FORCE; - else if (strcmpa((CHAR8*) "menu-hidden", value) == 0) + else if (streq8((char *) value, "menu-hidden")) config->timeout_sec_config = TIMEOUT_MENU_HIDDEN; else { _cleanup_freepool_ CHAR16 *s = NULL; @@ -1169,8 +1169,8 @@ static void config_defaults_load_from_file(Config *config, CHAR8 *content) { continue; } - if (strcmpa((CHAR8 *)"default", key) == 0) { - if (value[0] == '@' && strcmpa((CHAR8 *)"@saved", value) != 0) { + if (streq8((char *) key, "default")) { + if (value[0] == '@' && !streq8((char *) value, "@saved")) { log_error_stall(L"Unsupported special entry identifier: %a", value); continue; } @@ -1179,47 +1179,47 @@ static void config_defaults_load_from_file(Config *config, CHAR8 *content) { continue; } - if (strcmpa((CHAR8 *)"editor", key) == 0) { + if (streq8((char *) key, "editor")) { err = parse_boolean(value, &config->editor); if (EFI_ERROR(err)) log_error_stall(L"Error parsing 'editor' config option: %a", value); continue; } - if (strcmpa((CHAR8 *)"auto-entries", key) == 0) { + if (streq8((char *) key, "auto-entries")) { err = parse_boolean(value, &config->auto_entries); if (EFI_ERROR(err)) log_error_stall(L"Error parsing 'auto-entries' config option: %a", value); continue; } - if (strcmpa((CHAR8 *)"auto-firmware", key) == 0) { + if (streq8((char *) key, "auto-firmware")) { err = parse_boolean(value, &config->auto_firmware); if (EFI_ERROR(err)) log_error_stall(L"Error parsing 'auto-firmware' config option: %a", value); continue; } - if (strcmpa((CHAR8 *)"beep", key) == 0) { + if (streq8((char *) key, "beep")) { err = parse_boolean(value, &config->beep); if (EFI_ERROR(err)) log_error_stall(L"Error parsing 'beep' config option: %a", value); continue; } - if (strcmpa((CHAR8 *)"reboot-for-bitlocker", key) == 0) { + if (streq8((char *) key, "reboot-for-bitlocker")) { err = parse_boolean(value, &config->reboot_for_bitlocker); if (EFI_ERROR(err)) log_error_stall(L"Error parsing 'reboot-for-bitlocker' config option: %a", value); continue; } - if (strcmpa((CHAR8 *)"console-mode", key) == 0) { - if (strcmpa((CHAR8 *)"auto", value) == 0) + if (streq8((char *) key, "console-mode")) { + if (streq8((char *) value, "auto")) config->console_mode = CONSOLE_MODE_AUTO; - else if (strcmpa((CHAR8 *)"max", value) == 0) + else if (streq8((char *) value, "max")) config->console_mode = CONSOLE_MODE_FIRMWARE_MAX; - else if (strcmpa((CHAR8 *)"keep", value) == 0) + else if (streq8((char *) value, "keep")) config->console_mode = CONSOLE_MODE_KEEP; else { _cleanup_freepool_ CHAR16 *s = NULL; @@ -1230,12 +1230,12 @@ static void config_defaults_load_from_file(Config *config, CHAR8 *content) { continue; } - if (strcmpa((CHAR8*) "random-seed-mode", key) == 0) { - if (strcmpa((CHAR8*) "off", value) == 0) + if (streq8((char *) key, "random-seed-mode")) { + if (streq8((char *) value, "off")) config->random_seed_mode = RANDOM_SEED_OFF; - else if (strcmpa((CHAR8*) "with-system-token", value) == 0) + else if (streq8((char *) value, "with-system-token")) config->random_seed_mode = RANDOM_SEED_WITH_SYSTEM_TOKEN; - else if (strcmpa((CHAR8*) "always", value) == 0) + else if (streq8((char *) value, "always")) config->random_seed_mode = RANDOM_SEED_ALWAYS; else { BOOLEAN on; @@ -1415,7 +1415,7 @@ static void config_entry_bump_counters(ConfigEntry *entry, EFI_FILE *root_dir) { efivar_set(LOADER_GUID, L"LoaderBootCountPath", new_path, 0); /* If the file we just renamed is the loader path, then let's update that. */ - if (StrCmp(entry->loader, old_path) == 0) { + if (streq16(entry->loader, old_path)) { FreePool(entry->loader); entry->loader = TAKE_PTR(new_path); } @@ -1450,31 +1450,31 @@ static void config_entry_add_type1( }; while ((line = line_get_key_value(content, (CHAR8 *)" \t", &pos, &key, &value))) { - if (strcmpa((CHAR8 *)"title", key) == 0) { + if (streq8((char *) key, "title")) { FreePool(entry->title); entry->title = xstra_to_str(value); continue; } - if (strcmpa((CHAR8 *)"sort-key", key) == 0) { + if (streq8((char *) key, "sort-key")) { FreePool(entry->sort_key); entry->sort_key = xstra_to_str(value); continue; } - if (strcmpa((CHAR8 *)"version", key) == 0) { + if (streq8((char *) key, "version")) { FreePool(entry->version); entry->version = xstra_to_str(value); continue; } - if (strcmpa((CHAR8 *)"machine-id", key) == 0) { + if (streq8((char *) key, "machine-id")) { FreePool(entry->machine_id); entry->machine_id = xstra_to_str(value); continue; } - if (strcmpa((CHAR8 *)"linux", key) == 0) { + if (streq8((char *) key, "linux")) { FreePool(entry->loader); entry->type = LOADER_LINUX; entry->loader = xstra_to_path(value); @@ -1482,35 +1482,35 @@ static void config_entry_add_type1( continue; } - if (strcmpa((CHAR8 *)"efi", key) == 0) { + if (streq8((char *) key, "efi")) { entry->type = LOADER_EFI; FreePool(entry->loader); entry->loader = xstra_to_path(value); /* do not add an entry for ourselves */ - if (loaded_image_path && StriCmp(entry->loader, loaded_image_path) == 0) { + if (loaded_image_path && strcaseeq16(entry->loader, loaded_image_path)) { entry->type = LOADER_UNDEFINED; break; } continue; } - if (strcmpa((CHAR8 *)"architecture", key) == 0) { + if (streq8((char *) key, "architecture")) { /* do not add an entry for an EFI image of architecture not matching with that of the image */ - if (strcmpa((CHAR8 *)EFI_MACHINE_TYPE_NAME, value) != 0) { + if (!streq8((char *) value, EFI_MACHINE_TYPE_NAME)) { entry->type = LOADER_UNDEFINED; break; } continue; } - if (strcmpa((CHAR8 *)"devicetree", key) == 0) { + if (streq8((char *) key, "devicetree")) { FreePool(entry->devicetree); entry->devicetree = xstra_to_path(value); continue; } - if (strcmpa((CHAR8 *)"initrd", key) == 0) { + if (streq8((char *) key, "initrd")) { entry->initrd = xreallocate_pool( entry->initrd, n_initrd == 0 ? 0 : (n_initrd + 1) * sizeof(UINT16 *), @@ -1520,7 +1520,7 @@ static void config_entry_add_type1( continue; } - if (strcmpa((CHAR8 *)"options", key) == 0) { + if (streq8((char *) key, "options")) { _cleanup_freepool_ CHAR16 *new = NULL; new = xstra_to_str(value); @@ -1606,8 +1606,8 @@ static void config_load_defaults(Config *config, EFI_FILE *root_dir) { (void) efivar_get(LOADER_GUID, L"LoaderEntryDefault", &config->entry_default_efivar); - config->use_saved_entry = streq_ptr(config->entry_default_config, L"@saved"); - config->use_saved_entry_efivar = streq_ptr(config->entry_default_efivar, L"@saved"); + config->use_saved_entry = streq16(config->entry_default_config, L"@saved"); + config->use_saved_entry_efivar = streq16(config->entry_default_efivar, L"@saved"); if (config->use_saved_entry || config->use_saved_entry_efivar) (void) efivar_get(LOADER_GUID, L"LoaderEntryLastBooted", &config->entry_saved); } @@ -1676,12 +1676,12 @@ static INTN config_entry_compare(const ConfigEntry *a, const ConfigEntry *b) { return r; if (a->sort_key && b->sort_key) { - r = strcmp(a->sort_key, b->sort_key); + r = strcmp16(a->sort_key, b->sort_key); if (r != 0) return r; /* If multiple installations of the same OS are around, group by machine ID */ - r = strcmp_ptr(a->machine_id, b->machine_id); + r = strcmp16(a->machine_id, b->machine_id); if (r != 0) return r; @@ -1773,7 +1773,7 @@ static BOOLEAN entries_unique(ConfigEntry **entries, BOOLEAN *unique, UINTN entr for (UINTN i = 0; i < entry_count; i++) for (UINTN k = i + 1; k < entry_count; k++) { - if (StrCmp(entries[i]->title_show, entries[k]->title_show) != 0) + if (!streq16(entries[i]->title_show, entries[k]->title_show)) continue; is_unique = unique[i] = unique[k] = FALSE; @@ -1897,7 +1897,7 @@ static ConfigEntry *config_entry_add_loader_auto( * * If the default loader is not us, it might be shim. It would * chainload GRUBX64.EFI in that case, which might be us.*/ - if (StriCmp(loader, loaded_image_path) == 0 || + if (strcaseeq16(loader, loaded_image_path) || is_sd_boot(root_dir, loader) || is_sd_boot(root_dir, L"\\EFI\\BOOT\\GRUB" EFI_MACHINE_TYPE_NAME L".EFI")) return NULL; @@ -2021,7 +2021,7 @@ static EFI_STATUS boot_windows_bitlocker(void) { if (buf_size < offset + sizeof(CHAR16)) continue; - if (streq((CHAR16 *) (buf + offset), L"Windows Boot Manager")) { + if (streq16((CHAR16 *) (buf + offset), L"Windows Boot Manager")) { err = efivar_set_raw( EFI_GLOBAL_GUID, L"BootNext", @@ -2132,49 +2132,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 (strcmpa((const CHAR8*) "PRETTY_NAME", key) == 0) { + if (streq8((char *) key, "PRETTY_NAME")) { FreePool(os_pretty_name); os_pretty_name = xstra_to_str(value); continue; } - if (strcmpa((const CHAR8*) "IMAGE_ID", key) == 0) { + if (streq8((char *) key, "IMAGE_ID")) { FreePool(os_image_id); os_image_id = xstra_to_str(value); continue; } - if (strcmpa((const CHAR8*) "NAME", key) == 0) { + if (streq8((char *) key, "NAME")) { FreePool(os_name); os_name = xstra_to_str(value); continue; } - if (strcmpa((const CHAR8*) "ID", key) == 0) { + if (streq8((char *) key, "ID")) { FreePool(os_id); os_id = xstra_to_str(value); continue; } - if (strcmpa((const CHAR8*) "IMAGE_VERSION", key) == 0) { + if (streq8((char *) key, "IMAGE_VERSION")) { FreePool(os_image_version); os_image_version = xstra_to_str(value); continue; } - if (strcmpa((const CHAR8*) "VERSION", key) == 0) { + if (streq8((char *) key, "VERSION")) { FreePool(os_version); os_version = xstra_to_str(value); continue; } - if (strcmpa((const CHAR8*) "VERSION_ID", key) == 0) { + if (streq8((char *) key, "VERSION_ID")) { FreePool(os_version_id); os_version_id = xstra_to_str(value); continue; } - if (strcmpa((const CHAR8*) "BUILD_ID", key) == 0) { + if (streq8((char *) key, "BUILD_ID")) { FreePool(os_build_id); os_build_id = xstra_to_str(value); continue; @@ -2454,12 +2454,12 @@ static void save_selected_entry(const Config *config, const ConfigEntry *entry) (void) efivar_set(LOADER_GUID, L"LoaderEntrySelected", entry->id, 0); /* Do not save or delete if this was a oneshot boot. */ - if (streq_ptr(config->entry_oneshot, entry->id)) + if (streq16(config->entry_oneshot, entry->id)) return; if (config->use_saved_entry_efivar || (!config->entry_default_efivar && config->use_saved_entry)) { /* Avoid unnecessary NVRAM writes. */ - if (streq_ptr(config->entry_saved, entry->id)) + if (streq16(config->entry_saved, entry->id)) return; (void) efivar_set(LOADER_GUID, L"LoaderEntryLastBooted", entry->id, EFI_VARIABLE_NON_VOLATILE); diff --git a/src/boot/efi/splash.c b/src/boot/efi/splash.c index 71bd008d961..e0d075c911f 100644 --- a/src/boot/efi/splash.c +++ b/src/boot/efi/splash.c @@ -271,7 +271,7 @@ EFI_STATUS graphics_splash(const UINT8 *content, UINTN len, const EFI_GRAPHICS_O assert(content); if (!background) { - if (StriCmp(L"Apple", ST->FirmwareVendor) == 0) { + if (strcaseeq16(u"Apple", ST->FirmwareVendor)) { pixel.Red = 0xc0; pixel.Green = 0xc0; pixel.Blue = 0xc0; diff --git a/src/boot/efi/util.c b/src/boot/efi/util.c index 80d83f25b43..6e4e914f434 100644 --- a/src/boot/efi/util.c +++ b/src/boot/efi/util.c @@ -12,22 +12,14 @@ EFI_STATUS parse_boolean(const CHAR8 *v, BOOLEAN *b) { if (!v) return EFI_INVALID_PARAMETER; - if (strcmpa(v, (CHAR8 *)"1") == 0 || - strcmpa(v, (CHAR8 *)"yes") == 0 || - strcmpa(v, (CHAR8 *)"y") == 0 || - strcmpa(v, (CHAR8 *)"true") == 0 || - strcmpa(v, (CHAR8 *)"t") == 0 || - strcmpa(v, (CHAR8 *)"on") == 0) { + if (streq8((char *) v, "1") || streq8((char *) v, "yes") || streq8((char *) v, "y") || + streq8((char *) v, "true") || streq8((char *) v, "t") || streq8((char *) v, "on")) { *b = TRUE; return EFI_SUCCESS; } - if (strcmpa(v, (CHAR8 *)"0") == 0 || - strcmpa(v, (CHAR8 *)"no") == 0 || - strcmpa(v, (CHAR8 *)"n") == 0 || - strcmpa(v, (CHAR8 *)"false") == 0 || - strcmpa(v, (CHAR8 *)"f") == 0 || - strcmpa(v, (CHAR8 *)"off") == 0) { + if (streq8((char *) v, "0") || streq8((char *) v, "no") || streq8((char *) v, "n") || + streq8((char *) v, "false") || streq8((char *) v, "f") || streq8((char *) v, "off")) { *b = FALSE; return EFI_SUCCESS; } @@ -572,27 +564,6 @@ EFI_STATUS readdir_harder( return EFI_SUCCESS; } -INTN strncasecmpa(const CHAR8 *a, const CHAR8 *b, UINTN maxlen) { - if (!a || !b) - return CMP(a, b); - - while (maxlen > 0) { - CHAR8 ca = *a, cb = *b; - if (ca >= 'A' && ca <= 'Z') - ca += 'a' - 'A'; - if (cb >= 'A' && cb <= 'Z') - cb += 'a' - 'A'; - if (!ca || ca != cb) - return ca - cb; - - a++; - b++; - maxlen--; - } - - return 0; -} - CHAR8 *xstrndup8(const CHAR8 *p, UINTN sz) { CHAR8 *n; diff --git a/src/boot/efi/util.h b/src/boot/efi/util.h index b92dc18ed48..277ae9bcebd 100644 --- a/src/boot/efi/util.h +++ b/src/boot/efi/util.h @@ -121,10 +121,6 @@ EFI_STATUS get_file_info_harder(EFI_FILE *handle, EFI_FILE_INFO **ret, UINTN *re EFI_STATUS readdir_harder(EFI_FILE *handle, EFI_FILE_INFO **buffer, UINTN *buffer_size); CHAR8 *xstrndup8(const CHAR8 *p, UINTN sz); -INTN strncasecmpa(const CHAR8 *a, const CHAR8 *b, UINTN maxlen); -static inline BOOLEAN strncaseeqa(const CHAR8 *a, const CHAR8 *b, UINTN maxlen) { - return strncasecmpa(a, b, maxlen) == 0; -} BOOLEAN is_ascii(const CHAR16 *f); diff --git a/src/fundamental/string-util-fundamental.h b/src/fundamental/string-util-fundamental.h index c9f712463c1..d7a67356abe 100644 --- a/src/fundamental/string-util-fundamental.h +++ b/src/fundamental/string-util-fundamental.h @@ -12,10 +12,11 @@ #include "macro-fundamental.h" #ifdef SD_BOOT -# define strlen(a) strlen16((a)) -# define strcmp(a, b) StrCmp((a), (b)) -# define strncmp(a, b, n) StrnCmp((a), (b), (n)) -# define strcasecmp(a, b) StriCmp((a), (b)) +# define strlen strlen16 +# define strcmp strcmp16 +# define strncmp strncmp16 +# define strcasecmp strcasecmp16 +# define strncasecmp strncasecmp16 # define STR_C(str) (L ## str) # define memcmp(a, b, n) CompareMem(a, b, n) #else @@ -25,9 +26,7 @@ #define streq(a,b) (strcmp((a),(b)) == 0) #define strneq(a, b, n) (strncmp((a), (b), (n)) == 0) #define strcaseeq(a,b) (strcasecmp((a),(b)) == 0) -#ifndef SD_BOOT #define strncaseeq(a, b, n) (strncasecmp((a), (b), (n)) == 0) -#endif static inline sd_int strcmp_ptr(const sd_char *a, const sd_char *b) { if (a && b)