coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
vpd.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: BSD-3-Clause */
2 
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>
12 
13 #include "vpd.h"
14 #include "vpd_decode.h"
15 #include "vpd_tables.h"
16 
17 /* Currently we only support Google VPD 2.0, which has a fixed offset. */
18 enum {
19  CROSVPD_CBMEM_MAGIC = 0x43524f53,
21 };
22 
23 struct vpd_cbmem {
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).
31  */
32 };
33 
34 struct vpd_gets_arg {
35  const uint8_t *key;
36  const uint8_t *value;
38  int matched;
39 };
40 
41 static struct region_device ro_vpd, rw_vpd;
42 
43 /*
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.
46  */
47 static void init_vpd_rdev(const char *fmap_name, struct region_device *rdev)
48 {
49  struct google_vpd_info info;
50  int32_t size;
51 
52  if (fmap_locate_area_as_rdev(fmap_name, rdev)) {
53  printk(BIOS_ERR, "%s: No %s FMAP section.\n", __func__,
54  fmap_name);
55  goto fail;
56  }
57 
59 
60  if ((size < GOOGLE_VPD_2_0_OFFSET + sizeof(info)) ||
63  printk(BIOS_ERR, "%s: Too small (%d) for Google VPD 2.0.\n",
64  __func__, size);
65  goto fail;
66  }
67 
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;
73  }
74 
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;
81  }
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;
87  }
88 
89  return;
90 
91 fail:
92  memset(rdev, 0, sizeof(*rdev));
93 }
94 
95 static int init_vpd_rdevs_from_cbmem(void)
96 {
97  if (!cbmem_possibly_online())
98  return -1;
99 
100  struct vpd_cbmem *cbmem = cbmem_find(CBMEM_ID_VPD);
101  if (!cbmem)
102  return -1;
103 
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);
106 
107  return 0;
108 }
109 
110 static void init_vpd_rdevs(void)
111 {
112  static bool done = false;
113 
114  if (done)
115  return;
116 
117  if (init_vpd_rdevs_from_cbmem() != 0) {
118  init_vpd_rdev("RO_VPD", &ro_vpd);
119  init_vpd_rdev("RW_VPD", &rw_vpd);
120  }
121 
122  done = true;
123 }
124 
125 static void cbmem_add_cros_vpd(int is_recovery)
126 {
127  struct vpd_cbmem *cbmem;
128 
130 
131  init_vpd_rdevs();
132 
133  /* Return if no VPD at all */
134  if (region_device_sz(&ro_vpd) == 0 && region_device_sz(&rw_vpd) == 0)
135  return;
136 
137  size_t ro_size = region_device_sz(&ro_vpd);
138  size_t rw_size = region_device_sz(&rw_vpd);
139 
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;
145  }
146 
147  cbmem->magic = CROSVPD_CBMEM_MAGIC;
149  cbmem->ro_size = ro_size;
150  cbmem->rw_size = rw_size;
151 
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;
156  }
158  }
159 
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;
165  }
167  }
168 
170 }
171 
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)
175 {
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;
181 
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;
187 }
188 
189 static void vpd_find_in(struct region_device *rdev, struct vpd_gets_arg *arg)
190 {
191  if (region_device_sz(rdev) == 0)
192  return;
193 
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. */
199  }
200  rdev_munmap(rdev, mapping);
201 }
202 
203 const void *vpd_find(const char *key, int *size, enum vpd_region region)
204 {
205  struct vpd_gets_arg arg = {0};
206 
207  arg.key = (const uint8_t *)key;
208  arg.key_len = strlen(key);
209 
210  init_vpd_rdevs();
211 
212  if (region == VPD_RW_THEN_RO)
213  vpd_find_in(&rw_vpd, &arg);
214 
215  if (!arg.matched && (region == VPD_RO || region == VPD_RO_THEN_RW ||
217  vpd_find_in(&ro_vpd, &arg);
218 
219  if (!arg.matched && (region == VPD_RW || region == VPD_RO_THEN_RW))
220  vpd_find_in(&rw_vpd, &arg);
221 
222  if (!arg.matched)
223  return NULL;
224 
225  *size = arg.value_len;
226  return arg.value;
227 }
228 
229 char *vpd_gets(const char *key, char *buffer, int size, enum vpd_region region)
230 {
231  const void *string_address;
232  int string_size;
233 
234  string_address = vpd_find(key, &string_size, region);
235 
236  if (!string_address)
237  return NULL;
238 
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;
244 }
245 
246 /*
247  * Find value of boolean type vpd key.
248  *
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'.
251  */
252 bool vpd_get_bool(const char *key, enum vpd_region region, uint8_t *val)
253 {
254  int size;
255  const char *value;
256 
257  value = vpd_find(key, &size, region);
258  if (!value) {
259  printk(BIOS_CRIT, "problem returning from vpd_find.\n");
260  return false;
261  }
262 
263  if (size != 1)
264  return false;
265 
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;
275 }
276 
277 /*
278  * Find value of integer type by vpd key.
279  *
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.
283  */
284 bool vpd_get_int(const char *const key, const enum vpd_region region, int *const val)
285 {
286  char value[11];
287 
288  if (!vpd_gets(key, value, sizeof(value), region))
289  return false;
290 
291  if (!isdigit(*value))
292  return false;
293 
294  *val = (int)atol(value);
295  return true;
296 }
297 
pte_t value
Definition: mmu.c:91
void * memcpy(void *dest, const void *src, size_t n)
Definition: memcpy.c:7
void * memset(void *dstpp, int c, size_t len)
Definition: memset.c:12
#define assert(statement)
Definition: assert.h:74
#define MIN(a, b)
Definition: helpers.h:37
static int cbmem_possibly_online(void)
Definition: cbmem.h:158
void * cbmem_add(u32 id, u64 size)
Definition: imd_cbmem.c:144
void * cbmem_find(u32 id)
Definition: imd_cbmem.c:166
#define ROMSTAGE_CBMEM_INIT_HOOK(init_fn_)
Definition: cbmem.h:134
#define CBMEM_ID_VPD
Definition: cbmem_id.h:68
#define printk(level,...)
Definition: stdlib.h:16
static int isdigit(int c)
Definition: ctype.h:20
static struct smmstore_params_info info
Definition: ramstage.c:12
vpd_region
Definition: vpd.h:10
@ VPD_RO_THEN_RW
Definition: vpd.h:13
@ VPD_RO
Definition: vpd.h:11
@ VPD_RW
Definition: vpd.h:12
@ VPD_RW_THEN_RO
Definition: vpd.h:14
#define GOOGLE_VPD_2_0_OFFSET
Definition: vpd.h:8
static struct region_device rdev
Definition: flashconsole.c:14
int fmap_locate_area_as_rdev(const char *name, struct region_device *area)
Definition: fmap.c:144
struct bootblock_arg arg
Definition: decompressor.c:22
void timestamp_add_now(enum timestamp_id id)
Definition: timestamp.c:141
#define BIOS_CRIT
BIOS_CRIT - Recovery unlikely.
Definition: loglevel.h:56
#define BIOS_ERR
BIOS_ERR - System in incomplete state.
Definition: loglevel.h:72
#define BIOS_WARNING
BIOS_WARNING - Bad configuration.
Definition: loglevel.h:86
result
Definition: mrc_cache.c:35
u8 buffer[C2P_BUFFER_MAXSIZE]
Definition: psp_smm.c:18
int rdev_munmap(const struct region_device *rd, void *mapping)
Definition: region.c:65
static size_t region_device_sz(const struct region_device *rdev)
Definition: region.h:132
int rdev_chain_mem(struct region_device *child, const void *base, size_t size)
Definition: region.c:293
static void * rdev_mmap_full(const struct region_device *rd)
Definition: region.h:148
int rdev_chain(struct region_device *child, const struct region_device *parent, size_t offset, size_t size)
Definition: region.c:136
ssize_t rdev_readat(const struct region_device *rd, void *b, size_t offset, size_t size)
Definition: region.c:77
#define NULL
Definition: stddef.h:19
unsigned int uint32_t
Definition: stdint.h:14
signed int int32_t
Definition: stdint.h:13
unsigned char uint8_t
Definition: stdint.h:8
int memcmp(const void *s1, const void *s2, size_t n)
Definition: memcmp.c:3
size_t strlen(const char *src)
Definition: string.c:42
long atol(const char *str)
Definition: string.c:200
uint32_t size
Definition: vpd_tables.h:30
Definition: region.h:76
Definition: vpd.c:23
uint32_t version
Definition: vpd.c:25
uint32_t magic
Definition: vpd.c:24
uint32_t ro_size
Definition: vpd.c:26
uint32_t rw_size
Definition: vpd.c:27
uint8_t blob[0]
Definition: vpd.c:28
const uint8_t * value
Definition: vpd.c:36
int matched
Definition: vpd.c:38
int32_t value_len
Definition: vpd.c:37
const uint8_t * key
Definition: vpd.c:35
int32_t key_len
Definition: vpd.c:37
u8 val
Definition: sys.c:300
@ TS_COPYVPD_RO_END
@ TS_COPYVPD_RW_END
@ TS_COPYVPD_START
char * vpd_gets(const char *key, char *buffer, int size, enum vpd_region region)
Definition: vpd.c:229
@ CROSVPD_CBMEM_VERSION
Definition: vpd.c:20
@ CROSVPD_CBMEM_MAGIC
Definition: vpd.c:19
static struct region_device ro_vpd rw_vpd
Definition: vpd.c:41
static void init_vpd_rdevs(void)
Definition: vpd.c:110
static void cbmem_add_cros_vpd(int is_recovery)
Definition: vpd.c:125
static void init_vpd_rdev(const char *fmap_name, struct region_device *rdev)
Definition: vpd.c:47
const void * vpd_find(const char *key, int *size, enum vpd_region region)
Definition: vpd.c:203
static int init_vpd_rdevs_from_cbmem(void)
Definition: vpd.c:95
bool vpd_get_int(const char *const key, const enum vpd_region region, int *const val)
Definition: vpd.c:284
static int vpd_gets_callback(const uint8_t *key, uint32_t key_len, const uint8_t *value, uint32_t value_len, void *arg)
Definition: vpd.c:172
bool vpd_get_bool(const char *key, enum vpd_region region, uint8_t *val)
Definition: vpd.c:252
static void vpd_find_in(struct region_device *rdev, struct vpd_gets_arg *arg)
Definition: vpd.c:189
int vpd_decode_string(const u32 max_len, const u8 *input_buf, u32 *consumed, vpd_decode_callback callback, void *callback_arg)
Definition: vpd_decode.c:56
@ VPD_TYPE_IMPLICIT_TERMINATOR
Definition: vpd_decode.h:22
@ VPD_TYPE_TERMINATOR
Definition: vpd_decode.h:19
@ VPD_DECODE_OK
Definition: vpd_decode.h:14
@ VPD_DECODE_FAIL
Definition: vpd_decode.h:15
#define VPD_INFO_MAGIC
Definition: vpd_tables.h:11