| 1 |
|
| 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 |
|
| 20 |
|
| 21 |
#define DELIM_STR 0x00 |
| 22 |
#define DELIM_NAME 0x01 |
| 23 |
|
| 24 |
|
| 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 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 98 |
if (offset < size) |
| 99 |
offset++; |
| 100 |
return offset; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 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 |
|
| 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 |
|
| 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; |
| 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 |
|
| 159 |
lang_id = vb2api_get_locale_id(vboot_get_context()); |
| 160 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 188 |
|
| 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 |
|
| 193 |
|
| 194 |
if (offset >= next_name_offset) { |
| 195 |
|
| 196 |
|
| 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 |
|
| 206 |
offset = move_next(data, offset, next_name_offset, DELIM_STR); |
| 207 |
if (offset >= next_name_offset) |
| 208 |
return fallback_text; |
| 209 |
|
| 210 |
|
| 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 |
} |