blob: 0a285d9b6776ae289317e0e03afacbf0af9aaa59 [file] [log] [blame]
Hsuan Ting Chen3c2cdb62023-05-02 17:55:50 +08001/* 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
Hsuan Ting Chenf4e3f152023-07-06 15:58:42 +080013
14#define PRERAM_LOCALES_VERSION_BYTE 0x01
Hsuan Ting Chen3c2cdb62023-05-02 17:55:50 +080015#define PRERAM_LOCALES_NAME "preram_locales"
16
Hsuan Ting Chenf4e3f152023-07-06 15:58:42 +080017/* We need different delimiters to deal with the case where 'string_name' is the same as
18 'localized_string'. */
19#define DELIM_STR 0x00
20#define DELIM_NAME 0x01
21
Hsuan Ting Chen3c2cdb62023-05-02 17:55:50 +080022/*
23 * Devices which support early vga have the capability to show localized text in
24 * Code Page 437 encoding. (see src/drivers/pc80/vga/vga_font_8x16.c)
25 *
26 * preram_locales located in CBFS is an uncompressed file located in either RO
27 * or RW CBFS. It contains the localization information in the following format:
28 *
Hsuan Ting Chenf4e3f152023-07-06 15:58:42 +080029 * [PRERAM_LOCALES_VERSION_BYTE]
Hsuan Ting Chen3c2cdb62023-05-02 17:55:50 +080030 * [string_name_1] [\x00]
31 * [language_id_1] [\x00] [localized_string_1] [\x00]
32 * [language_id_2] [\x00] [localized_string_2] [\x00] ...
Hsuan Ting Chenf4e3f152023-07-06 15:58:42 +080033 * [\x01]
Hsuan Ting Chen3c2cdb62023-05-02 17:55:50 +080034 * [string_name_2] [\x00] ...
35 *
36 * This file contains tools to locate the file and search for localized strings
37 * with specific language ID.
38 */
39
40/* Cached state for map (locales_get_map) and unmap (ux_locales_unmap). */
41struct preram_locales_state {
42 void *data;
43 size_t size;
44 bool initialized;
45};
46
47static struct preram_locales_state cached_state;
48
49void ux_locales_unmap(void)
50{
51 if (cached_state.initialized) {
52 if (cached_state.data)
53 cbfs_unmap(cached_state.data);
54 cached_state.initialized = false;
55 cached_state.size = 0;
56 cached_state.data = NULL;
57 }
58}
59
60/* Get the map address of preram_locales. */
61static void *locales_get_map(size_t *size_out, bool unmap)
62{
63 if (cached_state.initialized) {
64 *size_out = cached_state.size;
65 return cached_state.data;
66 }
67 cached_state.initialized = true;
68 cached_state.data = cbfs_ro_map(PRERAM_LOCALES_NAME,
69 &cached_state.size);
70 *size_out = cached_state.size;
71 return cached_state.data;
72}
73
Hsuan Ting Chenf4e3f152023-07-06 15:58:42 +080074/* Move to the next string in the data. Strings are separated by delim. */
75static size_t move_next(const char *data, size_t offset, size_t size, char delim)
Hsuan Ting Chen3c2cdb62023-05-02 17:55:50 +080076{
Hsuan Ting Chenf4e3f152023-07-06 15:58:42 +080077 while (offset < size && data[offset] != delim)
Hsuan Ting Chen3c2cdb62023-05-02 17:55:50 +080078 offset++;
Hsuan Ting Chenf4e3f152023-07-06 15:58:42 +080079 /* If we found delim, move to the start of the next string. */
80 if (offset < size)
81 offset++;
Hsuan Ting Chen3c2cdb62023-05-02 17:55:50 +080082 return offset;
83}
84
Hsuan Ting Chenf4e3f152023-07-06 15:58:42 +080085/* Find the next occurrence of the specific string. Strings are separated by delim. */
Hsuan Ting Chen3c2cdb62023-05-02 17:55:50 +080086static size_t search_for(const char *data, size_t offset, size_t size,
Hsuan Ting Chenf4e3f152023-07-06 15:58:42 +080087 const char *str, char delim)
Hsuan Ting Chen3c2cdb62023-05-02 17:55:50 +080088{
Hsuan Ting Chen3c2cdb62023-05-02 17:55:50 +080089 while (offset < size) {
90 if (!strncmp(data + offset, str, size - offset))
91 return offset;
Hsuan Ting Chenf4e3f152023-07-06 15:58:42 +080092 offset = move_next(data, offset, size, delim);
Hsuan Ting Chen3c2cdb62023-05-02 17:55:50 +080093 }
94 return size;
95}
96
Hsuan Ting Chenf4e3f152023-07-06 15:58:42 +080097/* Find the next occurrence of the string_name, which should always follow a DELIM_NAME. */
98static inline size_t search_for_name(const char *data, size_t offset, size_t size,
99 const char *name)
100{
101 return search_for(data, offset, size, name, DELIM_NAME);
102}
103
Hsuan Ting Chen3c2cdb62023-05-02 17:55:50 +0800104/* Find the next occurrence of the integer ID, where ID is less than 100. */
105static size_t search_for_id(const char *data, size_t offset, size_t size,
106 int id)
107{
108 if (id >= LANG_ID_MAX)
109 return offset;
110 char int_to_str[LANG_ID_LEN] = {};
111 snprintf(int_to_str, LANG_ID_LEN, "%d", id);
Hsuan Ting Chenf4e3f152023-07-06 15:58:42 +0800112 return search_for(data, offset, size, int_to_str, DELIM_STR);
Hsuan Ting Chen3c2cdb62023-05-02 17:55:50 +0800113}
114
115const char *ux_locales_get_text(const char *name)
116{
117 const char *data;
Hsuan Ting Chenf4e3f152023-07-06 15:58:42 +0800118 size_t size, offset, name_offset, next_name_offset, next;
Subrata Banik7f7ebb72023-11-14 13:09:52 +0530119 uint32_t lang_id = 0; /* default language English (0) */
Hsuan Ting Chenf4e3f152023-07-06 15:58:42 +0800120 unsigned char version;
Hsuan Ting Chen3c2cdb62023-05-02 17:55:50 +0800121
122 data = locales_get_map(&size, false);
Hsuan Ting Chenf4e3f152023-07-06 15:58:42 +0800123 if (!data || size == 0) {
Hsuan Ting Chen8f4b0152023-07-06 14:15:31 +0800124 printk(BIOS_ERR, "%s: %s not found.\n", __func__,
Hsuan Ting Chen3c2cdb62023-05-02 17:55:50 +0800125 PRERAM_LOCALES_NAME);
126 return NULL;
127 }
128
Subrata Banik7f7ebb72023-11-14 13:09:52 +0530129 if (CONFIG(VBOOT)) {
130 /* Get the language ID from vboot API. */
131 lang_id = vb2api_get_locale_id(vboot_get_context());
132 /* Validity check: Language ID should smaller than LANG_ID_MAX. */
133 if (lang_id >= LANG_ID_MAX) {
134 printk(BIOS_WARNING, "%s: ID %d too big; fallback to 0.\n",
135 __func__, lang_id);
136 lang_id = 0;
137 }
Hsuan Ting Chen3c2cdb62023-05-02 17:55:50 +0800138 }
139
140 printk(BIOS_INFO, "%s: Search for %s with language ID: %u\n",
141 __func__, name, lang_id);
142
Hsuan Ting Chenf4e3f152023-07-06 15:58:42 +0800143 /* Check if the version byte is the expected version. */
144 version = (unsigned char)data[0];
145 if (version != PRERAM_LOCALES_VERSION_BYTE) {
146 printk(BIOS_ERR, "%s: The version %u is not the expected one %u\n",
147 __func__, version, PRERAM_LOCALES_VERSION_BYTE);
148 return NULL;
149 }
150
151 /* Search for name. Skip the version byte. */
152 offset = search_for_name(data, 1, size, name);
Hsuan Ting Chen3c2cdb62023-05-02 17:55:50 +0800153 if (offset >= size) {
Hsuan Ting Chen8f4b0152023-07-06 14:15:31 +0800154 printk(BIOS_ERR, "%s: Name %s not found.\n", __func__, name);
Hsuan Ting Chen3c2cdb62023-05-02 17:55:50 +0800155 return NULL;
156 }
157 name_offset = offset;
158
Hsuan Ting Chenf4e3f152023-07-06 15:58:42 +0800159 /* Search for language ID. We should not search beyond the range of the current
160 string_name. */
161 next_name_offset = move_next(data, offset, size, DELIM_NAME);
162 assert(next_name_offset <= size);
163 offset = search_for_id(data, name_offset, next_name_offset, lang_id);
164 /* Language ID not supported; fallback to English if the current language is not
165 English (0). */
166 if (offset >= next_name_offset) {
167 /* Since we only support a limited charset, it is very normal that a language
168 is not supported and we fallback here silently. */
Hsuan Ting Chen3c2cdb62023-05-02 17:55:50 +0800169 if (lang_id != 0)
Hsuan Ting Chenf4e3f152023-07-06 15:58:42 +0800170 offset = search_for_id(data, name_offset, next_name_offset, 0);
171 if (offset >= next_name_offset) {
172 printk(BIOS_ERR, "%s: Neither %d nor 0 found.\n", __func__, lang_id);
Hsuan Ting Chen3c2cdb62023-05-02 17:55:50 +0800173 return NULL;
174 }
175 }
176
Hsuan Ting Chenf4e3f152023-07-06 15:58:42 +0800177 /* Move to the corresponding localized_string. */
178 offset = move_next(data, offset, next_name_offset, DELIM_STR);
179 if (offset >= next_name_offset)
Hsuan Ting Chen3c2cdb62023-05-02 17:55:50 +0800180 return NULL;
181
182 /* Validity check that the returned string must be NULL terminated. */
Hsuan Ting Chenf4e3f152023-07-06 15:58:42 +0800183 next = move_next(data, offset, next_name_offset, DELIM_STR) - 1;
184 if (next >= next_name_offset || data[next] != '\0') {
Hsuan Ting Chen8f4b0152023-07-06 14:15:31 +0800185 printk(BIOS_ERR, "%s: %s is not NULL terminated.\n",
Hsuan Ting Chen3c2cdb62023-05-02 17:55:50 +0800186 __func__, PRERAM_LOCALES_NAME);
187 return NULL;
188 }
189
190 return data + offset;
191}