Debian Codegraph @

src/lib/ux_locales.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <cbfs.h>
4 #include <commonlib/bsd/helpers.h>
5 #include <console/console.h>
6 #include <security/vboot/misc.h>
7 #include <stddef.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <ux_locales.h>
11 #include <vb2_api.h>
12
13 #define LANG_ID_MAX 100
14 #define LANG_ID_LEN 3
15
16 #define PRERAM_LOCALES_VERSION_BYTE 0x01
17 #define PRERAM_LOCALES_NAME "preram_locales"
18
19 /* We need different delimiters to deal with the case where 'string_name' is the same as
20 'localized_string'. */
21 #define DELIM_STR 0x00
22 #define DELIM_NAME 0x01
23
24 /* Mapping of different default UX local message based on message ID */
25 static const struct {
26 const char *ux_locale_name;
27 const char *ux_locale_fallback_text;
28 } ux_locale_msg_list[] = {
29 [UX_LOCALE_MSG_MEMORY_TRAINING] = {
30 "memory_training_desc",
31 "Your device is finishing an update. This may take 1-2 minutes.\n"
32 "Please do not turn off your device."
33 },
34 [UX_LOCALE_MSG_LOW_BATTERY] = {
35 "low_battery_desc",
36 "Battery low. Shutting down."
37 },
38 };
39
40 /*
41 * Devices which support early vga have the capability to show localized text in
42 * Code Page 437 encoding. (see src/drivers/pc80/vga/vga_font_8x16.c)
43 *
44 * preram_locales located in CBFS is an uncompressed file located in either RO
45 * or RW CBFS. It contains the localization information in the following format:
46 *
47 * [PRERAM_LOCALES_VERSION_BYTE]
48 * [string_name_1] [\x00]
49 * [language_id_1] [\x00] [localized_string_1] [\x00]
50 * [language_id_2] [\x00] [localized_string_2] [\x00] ...
51 * [\x01]
52 * [string_name_2] [\x00] ...
53 *
54 * This file contains tools to locate the file and search for localized strings
55 * with specific language ID.
56 */
57
58 /* Cached state for map (locales_get_map) and unmap (ux_locales_unmap). */
59 struct preram_locales_state {
60 void *data;
61 size_t size;
62 bool initialized;
63 };
64
65 static struct preram_locales_state cached_state;
66
67 void ux_locales_unmap(void)
68 {
69 if (cached_state.initialized) {
70 if (cached_state.data)
71 cbfs_unmap(cached_state.data);
72 cached_state.initialized = false;
73 cached_state.size = 0;
74 cached_state.data = NULL;
75 }
76 }
77
78 /* Get the map address of preram_locales. */
79 static void *locales_get_map(size_t *size_out, bool unmap)
80 {
81 if (cached_state.initialized) {
82 *size_out = cached_state.size;
83 return cached_state.data;
84 }
85 cached_state.initialized = true;
86 cached_state.data = cbfs_ro_map(PRERAM_LOCALES_NAME,
87 &cached_state.size);
88 *size_out = cached_state.size;
89 return cached_state.data;
90 }
91
92 /* Move to the next string in the data. Strings are separated by delim. */
93 static size_t move_next(const char *data, size_t offset, size_t size, char delim)
94 {
95 while (offset < size && data[offset] != delim)
96 offset++;
97 /* If we found delim, move to the start of the next string. */
98 if (offset < size)
99 offset++;
100 return offset;
101 }
102
103 /* Find the next occurrence of the specific string. Strings are separated by delim. */
104 static size_t search_for(const char *data, size_t offset, size_t size,
105 const char *str, char delim)
106 {
107 while (offset < size) {
108 if (!strncmp(data + offset, str, size - offset))
109 return offset;
110 offset = move_next(data, offset, size, delim);
111 }
112 return size;
113 }
114
115 /* Find the next occurrence of the string_name, which should always follow a DELIM_NAME. */
116 static inline size_t search_for_name(const char *data, size_t offset, size_t size,
117 const char *name)
118 {
119 return search_for(data, offset, size, name, DELIM_NAME);
120 }
121
122 /* Find the next occurrence of the integer ID, where ID is less than 100. */
123 static size_t search_for_id(const char *data, size_t offset, size_t size,
124 int id)
125 {
126 if (id >= LANG_ID_MAX)
127 return offset;
128 char int_to_str[LANG_ID_LEN] = {};
129 snprintf(int_to_str, LANG_ID_LEN, "%d", id);
130 return search_for(data, offset, size, int_to_str, DELIM_STR);
131 }
132
133 const char *ux_locales_get_text(enum ux_locale_msg msg_id)
134 {
135 const char *data;
136 size_t size, offset, name_offset, next_name_offset, next;
137 uint32_t lang_id = 0; /* default language English (0) */
138 unsigned char version;
139 const char *name;
140 const char *fallback_text;
141
142 if (msg_id >= UX_LOCALE_MSG_NUM) {
143 printk(BIOS_ERR, "%s: Unknown message id = %d.\n", __func__, msg_id);
144 return "Trying to display an unknown message?";
145 }
146
147 name = ux_locale_msg_list[msg_id].ux_locale_name;
148 fallback_text = ux_locale_msg_list[msg_id].ux_locale_fallback_text;
149
150 data = locales_get_map(&size, false);
151 if (!data || size == 0) {
152 printk(BIOS_ERR, "%s: %s not found.\n", __func__,
153 PRERAM_LOCALES_NAME);
154 return fallback_text;
155 }
156
157 if (CONFIG(VBOOT)) {
158 /* Get the language ID from vboot API. */
159 lang_id = vb2api_get_locale_id(vboot_get_context());
160 /* Validity check: Language ID should smaller than LANG_ID_MAX. */
161 if (lang_id >= LANG_ID_MAX) {
162 printk(BIOS_WARNING, "%s: ID %d too big; fallback to 0.\n",
163 __func__, lang_id);
164 lang_id = 0;
165 }
166 }
167
168 printk(BIOS_INFO, "%s: Search for %s with language ID: %u\n",
169 __func__, name, lang_id);
170
171 /* Check if the version byte is the expected version. */
172 version = (unsigned char)data[0];
173 if (version != PRERAM_LOCALES_VERSION_BYTE) {
174 printk(BIOS_ERR, "%s: The version %u is not the expected one %u\n",
175 __func__, version, PRERAM_LOCALES_VERSION_BYTE);
176 return fallback_text;
177 }
178
179 /* Search for name. Skip the version byte. */
180 offset = search_for_name(data, 1, size, name);
181 if (offset >= size) {
182 printk(BIOS_ERR, "%s: Name %s not found.\n", __func__, name);
183 return fallback_text;
184 }
185 name_offset = offset;
186
187 /* Search for language ID. We should not search beyond the range of the current
188 string_name. */
189 next_name_offset = move_next(data, offset, size, DELIM_NAME);
190 assert(next_name_offset <= size);
191 offset = search_for_id(data, name_offset, next_name_offset, lang_id);
192 /* Language ID not supported; fallback to English if the current language is not
193 English (0). */
194 if (offset >= next_name_offset) {
195 /* Since we only support a limited charset, it is very normal that a language
196 is not supported and we fallback here silently. */
197 if (lang_id != 0)
198 offset = search_for_id(data, name_offset, next_name_offset, 0);
199 if (offset >= next_name_offset) {
200 printk(BIOS_ERR, "%s: Neither %d nor 0 found.\n", __func__, lang_id);
201 return fallback_text;
202 }
203 }
204
205 /* Move to the corresponding localized_string. */
206 offset = move_next(data, offset, next_name_offset, DELIM_STR);
207 if (offset >= next_name_offset)
208 return fallback_text;
209
210 /* Validity check that the returned string must be NULL terminated. */
211 next = move_next(data, offset, next_name_offset, DELIM_STR) - 1;
212 if (next >= next_name_offset || data[next] != '\0') {
213 printk(BIOS_ERR, "%s: %s is not NULL terminated.\n",
214 __func__, PRERAM_LOCALES_NAME);
215 return fallback_text;
216 }
217
218 return data + offset;
219 }