xref: /aosp_15_r20/external/coreboot/src/drivers/vpd/vpd.c (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
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,
20 	CROSVPD_CBMEM_VERSION = 0x0001,
21 };
22 
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).
31 	 */
32 };
33 
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;
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  */
init_vpd_rdev(const char * fmap_name,struct region_device * rdev)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_WARNING, "%s: No %s FMAP section.\n", __func__,
54 			fmap_name);
55 		goto fail;
56 	}
57 
58 	size = region_device_sz(rdev);
59 
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;
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 
init_vpd_rdevs_from_cbmem(void)95 static int init_vpd_rdevs_from_cbmem(void)
96 {
97 	if (!ENV_HAS_CBMEM)
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 
init_vpd_rdevs(void)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 
cbmem_add_cros_vpd(int is_recovery)125 static void cbmem_add_cros_vpd(int is_recovery)
126 {
127 	struct vpd_cbmem *cbmem;
128 
129 	timestamp_add_now(TS_COPYVPD_START);
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;
148 	cbmem->version = CROSVPD_CBMEM_VERSION;
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 		}
157 		timestamp_add_now(TS_COPYVPD_RO_END);
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 		}
166 		timestamp_add_now(TS_COPYVPD_RW_END);
167 	}
168 
169 	init_vpd_rdevs_from_cbmem();
170 }
171 
vpd_gets_callback(const uint8_t * key,uint32_t key_len,const uint8_t * value,uint32_t value_len,void * arg)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 
vpd_find_in(struct region_device * rdev,struct vpd_gets_arg * arg)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 
vpd_find(const char * key,int * size,enum vpd_region region)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 ||
216 			region == VPD_RW_THEN_RO))
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 
vpd_gets(const char * key,char * buffer,int size,enum vpd_region region)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  */
vpd_get_bool(const char * key,enum vpd_region region,uint8_t * val)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  */
vpd_get_int(const char * const key,const enum vpd_region region,int * const val)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 
298 CBMEM_CREATION_HOOK(cbmem_add_cros_vpd);
299