ec/google/chromeec: Define ACPI_NOTIFY_CROS_EC_MKBP constant
[coreboot2.git] / src / drivers / vpd / vpd.c
blob9e12967cf33cf20cd6d2ae973ef6f536b354eaf5
1 /* SPDX-License-Identifier: BSD-3-Clause */
3 #include <assert.h>
4 #include <console/console.h>
5 #include <cbmem.h>
6 #include <ctype.h>
7 #include <fmap.h>
8 #include <program_loading.h>
9 #include <string.h>
10 #include <timestamp.h>
11 #include <types.h>
13 #include "vpd.h"
14 #include "vpd_decode.h"
15 #include "vpd_tables.h"
17 /* Currently we only support Google VPD 2.0, which has a fixed offset. */
18 enum {
19 CROSVPD_CBMEM_MAGIC = 0x43524f53,
20 CROSVPD_CBMEM_VERSION = 0x0001,
23 struct vpd_cbmem {
24 uint32_t magic;
25 uint32_t version;
26 uint32_t ro_size;
27 uint32_t rw_size;
28 uint8_t blob[];
29 /* The blob contains both RO and RW data. It starts with RO (0 ..
30 * ro_size) and then RW (ro_size .. ro_size+rw_size).
34 struct vpd_gets_arg {
35 const uint8_t *key;
36 const uint8_t *value;
37 int32_t key_len, value_len;
38 int matched;
41 static struct region_device ro_vpd, rw_vpd;
44 * Initializes a region_device to represent the requested VPD 2.0 formatted
45 * region on flash. On errors rdev->size will be set to 0.
47 static void init_vpd_rdev(const char *fmap_name, struct region_device *rdev)
49 struct google_vpd_info info;
50 int32_t size;
52 if (fmap_locate_area_as_rdev(fmap_name, rdev)) {
53 printk(BIOS_WARNING, "%s: No %s FMAP section.\n", __func__,
54 fmap_name);
55 goto fail;
58 size = region_device_sz(rdev);
60 if ((size < GOOGLE_VPD_2_0_OFFSET + sizeof(info)) ||
61 rdev_chain(rdev, rdev, GOOGLE_VPD_2_0_OFFSET,
62 size - GOOGLE_VPD_2_0_OFFSET)) {
63 printk(BIOS_ERR, "%s: Too small (%d) for Google VPD 2.0.\n",
64 __func__, size);
65 goto fail;
68 /* Try if we can find a google_vpd_info, otherwise read whole VPD. */
69 if (rdev_readat(rdev, &info, 0, sizeof(info)) != sizeof(info)) {
70 printk(BIOS_ERR, "Failed to read %s header.\n",
71 fmap_name);
72 goto fail;
75 if (memcmp(info.header.magic, VPD_INFO_MAGIC, sizeof(info.header.magic))
76 == 0) {
77 if (rdev_chain(rdev, rdev, sizeof(info), info.size)) {
78 printk(BIOS_ERR, "%s info size too large.\n",
79 fmap_name);
80 goto fail;
82 } else if (info.header.tlv.type == VPD_TYPE_TERMINATOR ||
83 info.header.tlv.type == VPD_TYPE_IMPLICIT_TERMINATOR) {
84 printk(BIOS_WARNING, "%s is uninitialized or empty.\n",
85 fmap_name);
86 goto fail;
89 return;
91 fail:
92 memset(rdev, 0, sizeof(*rdev));
95 static int init_vpd_rdevs_from_cbmem(void)
97 if (!ENV_HAS_CBMEM)
98 return -1;
100 struct vpd_cbmem *cbmem = cbmem_find(CBMEM_ID_VPD);
101 if (!cbmem)
102 return -1;
104 rdev_chain_mem(&ro_vpd, cbmem->blob, cbmem->ro_size);
105 rdev_chain_mem(&rw_vpd, cbmem->blob + cbmem->ro_size, cbmem->rw_size);
107 return 0;
110 static void init_vpd_rdevs(void)
112 static bool done = false;
114 if (done)
115 return;
117 if (init_vpd_rdevs_from_cbmem() != 0) {
118 init_vpd_rdev("RO_VPD", &ro_vpd);
119 init_vpd_rdev("RW_VPD", &rw_vpd);
122 done = true;
125 static void cbmem_add_cros_vpd(int is_recovery)
127 struct vpd_cbmem *cbmem;
129 timestamp_add_now(TS_COPYVPD_START);
131 init_vpd_rdevs();
133 /* Return if no VPD at all */
134 if (region_device_sz(&ro_vpd) == 0 && region_device_sz(&rw_vpd) == 0)
135 return;
137 size_t ro_size = region_device_sz(&ro_vpd);
138 size_t rw_size = region_device_sz(&rw_vpd);
140 cbmem = cbmem_add(CBMEM_ID_VPD, sizeof(*cbmem) + ro_size + rw_size);
141 if (!cbmem) {
142 printk(BIOS_ERR, "%s: Failed to allocate CBMEM (%zu+%zu).\n",
143 __func__, ro_size, rw_size);
144 return;
147 cbmem->magic = CROSVPD_CBMEM_MAGIC;
148 cbmem->version = CROSVPD_CBMEM_VERSION;
149 cbmem->ro_size = ro_size;
150 cbmem->rw_size = rw_size;
152 if (ro_size) {
153 if (rdev_readat(&ro_vpd, cbmem->blob, 0, ro_size) != ro_size) {
154 printk(BIOS_ERR, "Couldn't read RO VPD\n");
155 cbmem->ro_size = ro_size = 0;
157 timestamp_add_now(TS_COPYVPD_RO_END);
160 if (rw_size) {
161 if (rdev_readat(&rw_vpd, cbmem->blob + ro_size, 0, rw_size)
162 != rw_size) {
163 printk(BIOS_ERR, "Couldn't read RW VPD\n");
164 cbmem->rw_size = rw_size = 0;
166 timestamp_add_now(TS_COPYVPD_RW_END);
169 init_vpd_rdevs_from_cbmem();
172 static int vpd_gets_callback(const uint8_t *key, uint32_t key_len,
173 const uint8_t *value, uint32_t value_len,
174 void *arg)
176 struct vpd_gets_arg *result = (struct vpd_gets_arg *)arg;
177 if (key_len != result->key_len ||
178 memcmp(key, result->key, key_len) != 0)
179 /* Returns VPD_DECODE_OK to continue parsing. */
180 return VPD_DECODE_OK;
182 result->matched = 1;
183 result->value = value;
184 result->value_len = value_len;
185 /* Returns VPD_DECODE_FAIL to stop parsing. */
186 return VPD_DECODE_FAIL;
189 static void vpd_find_in(struct region_device *rdev, struct vpd_gets_arg *arg)
191 if (region_device_sz(rdev) == 0)
192 return;
194 uint32_t consumed = 0;
195 void *mapping = rdev_mmap_full(rdev);
196 while (vpd_decode_string(region_device_sz(rdev), mapping,
197 &consumed, vpd_gets_callback, arg) == VPD_DECODE_OK) {
198 /* Iterate until found or no more entries. */
200 rdev_munmap(rdev, mapping);
203 const void *vpd_find(const char *key, int *size, enum vpd_region region)
205 struct vpd_gets_arg arg = {0};
207 arg.key = (const uint8_t *)key;
208 arg.key_len = strlen(key);
210 init_vpd_rdevs();
212 if (region == VPD_RW_THEN_RO)
213 vpd_find_in(&rw_vpd, &arg);
215 if (!arg.matched && (region == VPD_RO || region == VPD_RO_THEN_RW ||
216 region == VPD_RW_THEN_RO))
217 vpd_find_in(&ro_vpd, &arg);
219 if (!arg.matched && (region == VPD_RW || region == VPD_RO_THEN_RW))
220 vpd_find_in(&rw_vpd, &arg);
222 if (!arg.matched)
223 return NULL;
225 *size = arg.value_len;
226 return arg.value;
229 char *vpd_gets(const char *key, char *buffer, int size, enum vpd_region region)
231 const void *string_address;
232 int string_size;
234 string_address = vpd_find(key, &string_size, region);
236 if (!string_address)
237 return NULL;
239 assert(size > 0);
240 int copy_size = MIN(size - 1, string_size);
241 memcpy(buffer, string_address, copy_size);
242 buffer[copy_size] = '\0';
243 return buffer;
247 * Find value of boolean type vpd key.
249 * During the process, necessary checking is done, such as making
250 * sure the value length is 1, and value is either '1' or '0'.
252 bool vpd_get_bool(const char *key, enum vpd_region region, uint8_t *val)
254 int size;
255 const char *value;
257 value = vpd_find(key, &size, region);
258 if (!value) {
259 printk(BIOS_CRIT, "problem returning from vpd_find.\n");
260 return false;
263 if (size != 1)
264 return false;
266 /* Make sure the value is either '1' or '0' */
267 if (*value == '1') {
268 *val = 1;
269 return true;
270 } else if (*value == '0') {
271 *val = 0;
272 return true;
273 } else
274 return false;
278 * Find value of integer type by vpd key.
280 * Expects to find a decimal string, trailing chars are ignored.
281 * Returns true if the key is found and the value is not too long and
282 * starts with a decimal digit. Leaves `val` untouched if unsuccessful.
284 bool vpd_get_int(const char *const key, const enum vpd_region region, int *const val)
286 char value[11];
288 if (!vpd_gets(key, value, sizeof(value), region))
289 return false;
291 if (!isdigit(*value))
292 return false;
294 *val = (int)atol(value);
295 return true;
298 CBMEM_CREATION_HOOK(cbmem_add_cros_vpd);