Hsuan Ting Chen | 3c2cdb6 | 2023-05-02 17:55:50 +0800 | [diff] [blame^] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | |
| 3 | #include <cbfs.h> |
| 4 | #include <console/console.h> |
| 5 | #include <security/vboot/misc.h> |
| 6 | #include <stddef.h> |
| 7 | #include <string.h> |
| 8 | #include <ux_locales.h> |
| 9 | #include <vb2_api.h> |
| 10 | |
| 11 | #define LANG_ID_MAX 100 |
| 12 | #define LANG_ID_LEN 3 |
| 13 | #define PRERAM_LOCALES_NAME "preram_locales" |
| 14 | |
| 15 | /* |
| 16 | * Devices which support early vga have the capability to show localized text in |
| 17 | * Code Page 437 encoding. (see src/drivers/pc80/vga/vga_font_8x16.c) |
| 18 | * |
| 19 | * preram_locales located in CBFS is an uncompressed file located in either RO |
| 20 | * or RW CBFS. It contains the localization information in the following format: |
| 21 | * |
| 22 | * [string_name_1] [\x00] |
| 23 | * [language_id_1] [\x00] [localized_string_1] [\x00] |
| 24 | * [language_id_2] [\x00] [localized_string_2] [\x00] ... |
| 25 | * [string_name_2] [\x00] ... |
| 26 | * |
| 27 | * This file contains tools to locate the file and search for localized strings |
| 28 | * with specific language ID. |
| 29 | */ |
| 30 | |
| 31 | /* Cached state for map (locales_get_map) and unmap (ux_locales_unmap). */ |
| 32 | struct preram_locales_state { |
| 33 | void *data; |
| 34 | size_t size; |
| 35 | bool initialized; |
| 36 | }; |
| 37 | |
| 38 | static struct preram_locales_state cached_state; |
| 39 | |
| 40 | void ux_locales_unmap(void) |
| 41 | { |
| 42 | if (cached_state.initialized) { |
| 43 | if (cached_state.data) |
| 44 | cbfs_unmap(cached_state.data); |
| 45 | cached_state.initialized = false; |
| 46 | cached_state.size = 0; |
| 47 | cached_state.data = NULL; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /* Get the map address of preram_locales. */ |
| 52 | static void *locales_get_map(size_t *size_out, bool unmap) |
| 53 | { |
| 54 | if (cached_state.initialized) { |
| 55 | *size_out = cached_state.size; |
| 56 | return cached_state.data; |
| 57 | } |
| 58 | cached_state.initialized = true; |
| 59 | cached_state.data = cbfs_ro_map(PRERAM_LOCALES_NAME, |
| 60 | &cached_state.size); |
| 61 | *size_out = cached_state.size; |
| 62 | return cached_state.data; |
| 63 | } |
| 64 | |
| 65 | /* Move to the next string in the data. Strings are separated by 0x00. */ |
| 66 | static size_t move_next(const char *data, size_t offset, size_t size) |
| 67 | { |
| 68 | if (offset >= size) |
| 69 | return size; |
| 70 | while (offset < size && data[offset] != '\0') |
| 71 | offset++; |
| 72 | offset++; |
| 73 | return offset; |
| 74 | } |
| 75 | |
| 76 | /* Find the next occurrence of the specific string. */ |
| 77 | static size_t search_for(const char *data, size_t offset, size_t size, |
| 78 | const char *str) |
| 79 | { |
| 80 | if (offset >= size) |
| 81 | return size; |
| 82 | while (offset < size) { |
| 83 | if (!strncmp(data + offset, str, size - offset)) |
| 84 | return offset; |
| 85 | offset = move_next(data, offset, size); |
| 86 | } |
| 87 | return size; |
| 88 | } |
| 89 | |
| 90 | /* Find the next occurrence of the integer ID, where ID is less than 100. */ |
| 91 | static size_t search_for_id(const char *data, size_t offset, size_t size, |
| 92 | int id) |
| 93 | { |
| 94 | if (id >= LANG_ID_MAX) |
| 95 | return offset; |
| 96 | char int_to_str[LANG_ID_LEN] = {}; |
| 97 | snprintf(int_to_str, LANG_ID_LEN, "%d", id); |
| 98 | return search_for(data, offset, size, int_to_str); |
| 99 | } |
| 100 | |
| 101 | const char *ux_locales_get_text(const char *name) |
| 102 | { |
| 103 | const char *data; |
| 104 | size_t size, offset, name_offset, next; |
| 105 | uint32_t lang_id; |
| 106 | |
| 107 | data = locales_get_map(&size, false); |
| 108 | if (!data) { |
| 109 | printk(BIOS_INFO, "%s: %s not found.\n", __func__, |
| 110 | PRERAM_LOCALES_NAME); |
| 111 | return NULL; |
| 112 | } |
| 113 | |
| 114 | /* Get the language ID from vboot API. */ |
| 115 | lang_id = vb2api_get_locale_id(vboot_get_context()); |
| 116 | /* Validity check: Language ID should smaller than LANG_ID_MAX. */ |
| 117 | if (lang_id >= LANG_ID_MAX) { |
| 118 | printk(BIOS_INFO, "%s: ID %d too big; fallback to 0.\n", |
| 119 | __func__, lang_id); |
| 120 | lang_id = 0; |
| 121 | } |
| 122 | |
| 123 | printk(BIOS_INFO, "%s: Search for %s with language ID: %u\n", |
| 124 | __func__, name, lang_id); |
| 125 | |
| 126 | offset = 0; |
| 127 | /* Search for name. */ |
| 128 | offset = search_for(data, offset, size, name); |
| 129 | if (offset >= size) { |
| 130 | printk(BIOS_INFO, "%s: Name %s not found.\n", __func__, name); |
| 131 | return NULL; |
| 132 | } |
| 133 | name_offset = offset; |
| 134 | |
| 135 | /* Search for language ID. */ |
| 136 | offset = search_for_id(data, name_offset, size, lang_id); |
| 137 | /* Language ID not supported; fallback to English if the current |
| 138 | * language is not English (0). */ |
| 139 | if (offset >= size) { |
| 140 | /* |
| 141 | * Since we only support a limited charset, it is very normal |
| 142 | * that a language is not supported and we fallback here |
| 143 | * silently. |
| 144 | */ |
| 145 | if (lang_id != 0) |
| 146 | offset = search_for_id(data, name_offset, size, 0); |
| 147 | if (offset >= size) { |
| 148 | printk(BIOS_INFO, "%s: Neither %d nor 0 found.\n", |
| 149 | __func__, lang_id); |
| 150 | return NULL; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | offset = move_next(data, offset, size); |
| 155 | if (offset >= size) |
| 156 | return NULL; |
| 157 | |
| 158 | /* Validity check that the returned string must be NULL terminated. */ |
| 159 | next = move_next(data, offset, size) - 1; |
| 160 | if (next >= size || data[next] != '\0') { |
| 161 | printk(BIOS_INFO, "%s: %s is not NULL terminated.\n", |
| 162 | __func__, PRERAM_LOCALES_NAME); |
| 163 | return NULL; |
| 164 | } |
| 165 | |
| 166 | return data + offset; |
| 167 | } |