Jacob Garber | 10999ea6 | 2020-05-18 13:36:58 -0600 | [diff] [blame] | 1 | /* SPDX-License-Identifier: BSD-3-Clause */ |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 2 | |
Jonathan Zhang | b5392f9 | 2019-07-16 14:37:23 -0700 | [diff] [blame] | 3 | #include <assert.h> |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 4 | #include <console/console.h> |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 5 | #include <cbmem.h> |
Nico Huber | c00ffef | 2020-09-27 16:21:22 +0200 | [diff] [blame^] | 6 | #include <ctype.h> |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 7 | #include <fmap.h> |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 8 | #include <program_loading.h> |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 9 | #include <string.h> |
Julius Werner | 4f7a361 | 2016-01-20 18:01:15 -0800 | [diff] [blame] | 10 | #include <timestamp.h> |
Nico Huber | c00ffef | 2020-09-27 16:21:22 +0200 | [diff] [blame^] | 11 | #include <types.h> |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 12 | |
Patrick Rudolph | 28cee59 | 2018-03-08 15:43:12 +0100 | [diff] [blame] | 13 | #include "vpd.h" |
Hung-Te Lin | c345570 | 2019-05-27 11:02:00 +0800 | [diff] [blame] | 14 | #include "vpd_decode.h" |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 15 | #include "vpd_tables.h" |
| 16 | |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 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[0]; |
| 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 | |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 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 | |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 41 | static struct region_device ro_vpd, rw_vpd; |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 42 | |
Jonathan Zhang | b5392f9 | 2019-07-16 14:37:23 -0700 | [diff] [blame] | 43 | /* |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 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. |
Jonathan Zhang | b5392f9 | 2019-07-16 14:37:23 -0700 | [diff] [blame] | 46 | */ |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 47 | static void init_vpd_rdev(const char *fmap_name, struct region_device *rdev) |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 48 | { |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 49 | struct google_vpd_info info; |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 50 | int32_t size; |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 51 | |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 52 | if (fmap_locate_area_as_rdev(fmap_name, rdev)) { |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 53 | printk(BIOS_ERR, "%s: No %s FMAP section.\n", __func__, |
| 54 | fmap_name); |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 55 | goto fail; |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 56 | } |
| 57 | |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 58 | size = region_device_sz(rdev); |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 59 | |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 60 | if ((size < GOOGLE_VPD_2_0_OFFSET + sizeof(info)) || |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 61 | rdev_chain(rdev, rdev, GOOGLE_VPD_2_0_OFFSET, |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 62 | size - GOOGLE_VPD_2_0_OFFSET)) { |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 63 | printk(BIOS_ERR, "%s: Too small (%d) for Google VPD 2.0.\n", |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 64 | __func__, size); |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 65 | goto fail; |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 66 | } |
| 67 | |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 68 | /* Try if we can find a google_vpd_info, otherwise read whole VPD. */ |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 69 | if (rdev_readat(rdev, &info, 0, sizeof(info)) != sizeof(info)) { |
Julius Werner | ffc2260 | 2016-01-21 11:12:38 -0800 | [diff] [blame] | 70 | printk(BIOS_ERR, "ERROR: Failed to read %s header.\n", |
| 71 | fmap_name); |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 72 | goto fail; |
Julius Werner | ffc2260 | 2016-01-21 11:12:38 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | if (memcmp(info.header.magic, VPD_INFO_MAGIC, sizeof(info.header.magic)) |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 76 | == 0) { |
| 77 | if (rdev_chain(rdev, rdev, sizeof(info), info.size)) { |
| 78 | printk(BIOS_ERR, "ERROR: %s info size too large.\n", |
| 79 | fmap_name); |
| 80 | goto fail; |
| 81 | } |
Julius Werner | ffc2260 | 2016-01-21 11:12:38 -0800 | [diff] [blame] | 82 | } else if (info.header.tlv.type == VPD_TYPE_TERMINATOR || |
| 83 | info.header.tlv.type == VPD_TYPE_IMPLICIT_TERMINATOR) { |
| 84 | printk(BIOS_WARNING, "WARNING: %s is uninitialized or empty.\n", |
| 85 | fmap_name); |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 86 | goto fail; |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 87 | } |
| 88 | |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 89 | return; |
| 90 | |
| 91 | fail: |
| 92 | memset(rdev, 0, sizeof(*rdev)); |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 93 | } |
| 94 | |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 95 | static int init_vpd_rdevs_from_cbmem(void) |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 96 | { |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 97 | if (!cbmem_possibly_online()) |
| 98 | return -1; |
Jonathan Zhang | b5392f9 | 2019-07-16 14:37:23 -0700 | [diff] [blame] | 99 | |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 100 | struct vpd_cbmem *cbmem = cbmem_find(CBMEM_ID_VPD); |
| 101 | if (!cbmem) |
| 102 | return -1; |
| 103 | |
| 104 | rdev_chain(&ro_vpd, &addrspace_32bit.rdev, |
| 105 | (uintptr_t)cbmem->blob, cbmem->ro_size); |
| 106 | rdev_chain(&rw_vpd, &addrspace_32bit.rdev, |
| 107 | (uintptr_t)cbmem->blob + cbmem->ro_size, cbmem->rw_size); |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | static void init_vpd_rdevs(void) |
| 113 | { |
| 114 | static bool done = false; |
| 115 | |
| 116 | if (done) |
Jonathan Zhang | b5392f9 | 2019-07-16 14:37:23 -0700 | [diff] [blame] | 117 | return; |
| 118 | |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 119 | if (init_vpd_rdevs_from_cbmem() != 0) { |
| 120 | init_vpd_rdev("RO_VPD", &ro_vpd); |
| 121 | init_vpd_rdev("RW_VPD", &rw_vpd); |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 122 | } |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 123 | |
| 124 | done = true; |
Jonathan Zhang | b5392f9 | 2019-07-16 14:37:23 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 127 | static void cbmem_add_cros_vpd(int is_recovery) |
Jonathan Zhang | b5392f9 | 2019-07-16 14:37:23 -0700 | [diff] [blame] | 128 | { |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 129 | struct vpd_cbmem *cbmem; |
Jonathan Zhang | b5392f9 | 2019-07-16 14:37:23 -0700 | [diff] [blame] | 130 | |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 131 | timestamp_add_now(TS_START_COPYVPD); |
| 132 | |
| 133 | init_vpd_rdevs(); |
| 134 | |
| 135 | /* Return if no VPD at all */ |
| 136 | if (region_device_sz(&ro_vpd) == 0 && region_device_sz(&rw_vpd) == 0) |
| 137 | return; |
| 138 | |
| 139 | size_t ro_size = region_device_sz(&ro_vpd); |
| 140 | size_t rw_size = region_device_sz(&rw_vpd); |
| 141 | |
| 142 | cbmem = cbmem_add(CBMEM_ID_VPD, sizeof(*cbmem) + ro_size + rw_size); |
| 143 | if (!cbmem) { |
| 144 | printk(BIOS_ERR, "%s: Failed to allocate CBMEM (%zu+%zu).\n", |
| 145 | __func__, ro_size, rw_size); |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | cbmem->magic = CROSVPD_CBMEM_MAGIC; |
| 150 | cbmem->version = CROSVPD_CBMEM_VERSION; |
| 151 | cbmem->ro_size = ro_size; |
| 152 | cbmem->rw_size = rw_size; |
| 153 | |
| 154 | if (ro_size) { |
| 155 | if (rdev_readat(&ro_vpd, cbmem->blob, 0, ro_size) != ro_size) { |
| 156 | printk(BIOS_ERR, "ERROR: Couldn't read RO VPD\n"); |
| 157 | cbmem->ro_size = ro_size = 0; |
| 158 | } |
| 159 | timestamp_add_now(TS_END_COPYVPD_RO); |
| 160 | } |
| 161 | |
| 162 | if (rw_size) { |
| 163 | if (rdev_readat(&rw_vpd, cbmem->blob + ro_size, 0, rw_size) |
| 164 | != rw_size) { |
| 165 | printk(BIOS_ERR, "ERROR: Couldn't read RW VPD\n"); |
| 166 | cbmem->rw_size = rw_size = 0; |
| 167 | } |
| 168 | timestamp_add_now(TS_END_COPYVPD_RW); |
| 169 | } |
| 170 | |
| 171 | init_vpd_rdevs_from_cbmem(); |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 172 | } |
| 173 | |
Hung-Te Lin | c345570 | 2019-05-27 11:02:00 +0800 | [diff] [blame] | 174 | static int vpd_gets_callback(const uint8_t *key, uint32_t key_len, |
| 175 | const uint8_t *value, uint32_t value_len, |
| 176 | void *arg) |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 177 | { |
| 178 | struct vpd_gets_arg *result = (struct vpd_gets_arg *)arg; |
| 179 | if (key_len != result->key_len || |
| 180 | memcmp(key, result->key, key_len) != 0) |
Hung-Te Lin | c345570 | 2019-05-27 11:02:00 +0800 | [diff] [blame] | 181 | /* Returns VPD_DECODE_OK to continue parsing. */ |
| 182 | return VPD_DECODE_OK; |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 183 | |
| 184 | result->matched = 1; |
| 185 | result->value = value; |
| 186 | result->value_len = value_len; |
Hung-Te Lin | c345570 | 2019-05-27 11:02:00 +0800 | [diff] [blame] | 187 | /* Returns VPD_DECODE_FAIL to stop parsing. */ |
| 188 | return VPD_DECODE_FAIL; |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 189 | } |
| 190 | |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 191 | static void vpd_find_in(struct region_device *rdev, struct vpd_gets_arg *arg) |
| 192 | { |
| 193 | if (region_device_sz(rdev) == 0) |
| 194 | return; |
| 195 | |
| 196 | uint32_t consumed = 0; |
| 197 | void *mapping = rdev_mmap_full(rdev); |
| 198 | while (vpd_decode_string(region_device_sz(rdev), mapping, |
| 199 | &consumed, vpd_gets_callback, arg) == VPD_DECODE_OK) { |
| 200 | /* Iterate until found or no more entries. */ |
| 201 | } |
| 202 | rdev_munmap(rdev, mapping); |
| 203 | } |
| 204 | |
Patrick Rudolph | 28cee59 | 2018-03-08 15:43:12 +0100 | [diff] [blame] | 205 | const void *vpd_find(const char *key, int *size, enum vpd_region region) |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 206 | { |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 207 | struct vpd_gets_arg arg = {0}; |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 208 | |
| 209 | arg.key = (const uint8_t *)key; |
| 210 | arg.key_len = strlen(key); |
| 211 | |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 212 | init_vpd_rdevs(); |
Jonathan Zhang | b5392f9 | 2019-07-16 14:37:23 -0700 | [diff] [blame] | 213 | |
Jonathan Zhang | 9b110bf | 2020-05-28 13:35:44 -0700 | [diff] [blame] | 214 | if (region == VPD_RW_THEN_RO) |
| 215 | vpd_find_in(&rw_vpd, &arg); |
| 216 | |
| 217 | if (!arg.matched && (region == VPD_RO || region == VPD_RO_THEN_RW || |
| 218 | region == VPD_RW_THEN_RO)) |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 219 | vpd_find_in(&ro_vpd, &arg); |
| 220 | |
Jonathan Zhang | 9b110bf | 2020-05-28 13:35:44 -0700 | [diff] [blame] | 221 | if (!arg.matched && (region == VPD_RW || region == VPD_RO_THEN_RW)) |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 222 | vpd_find_in(&rw_vpd, &arg); |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 223 | |
| 224 | if (!arg.matched) |
| 225 | return NULL; |
| 226 | |
Vadim Bendebury | 127c339 | 2014-10-22 17:39:24 -0700 | [diff] [blame] | 227 | *size = arg.value_len; |
| 228 | return arg.value; |
| 229 | } |
| 230 | |
Patrick Rudolph | 28cee59 | 2018-03-08 15:43:12 +0100 | [diff] [blame] | 231 | char *vpd_gets(const char *key, char *buffer, int size, enum vpd_region region) |
Vadim Bendebury | 127c339 | 2014-10-22 17:39:24 -0700 | [diff] [blame] | 232 | { |
| 233 | const void *string_address; |
| 234 | int string_size; |
| 235 | |
Patrick Rudolph | 28cee59 | 2018-03-08 15:43:12 +0100 | [diff] [blame] | 236 | string_address = vpd_find(key, &string_size, region); |
Vadim Bendebury | 127c339 | 2014-10-22 17:39:24 -0700 | [diff] [blame] | 237 | |
| 238 | if (!string_address) |
| 239 | return NULL; |
| 240 | |
Jonathan Zhang | b5392f9 | 2019-07-16 14:37:23 -0700 | [diff] [blame] | 241 | assert(size > 0); |
| 242 | int copy_size = MIN(size - 1, string_size); |
| 243 | memcpy(buffer, string_address, copy_size); |
| 244 | buffer[copy_size] = '\0'; |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 245 | return buffer; |
| 246 | } |
| 247 | |
Jonathan Zhang | b5392f9 | 2019-07-16 14:37:23 -0700 | [diff] [blame] | 248 | /* |
| 249 | * Find value of boolean type vpd key. |
| 250 | * |
| 251 | * During the process, necessary checking is done, such as making |
| 252 | * sure the value length is 1, and value is either '1' or '0'. |
| 253 | */ |
| 254 | bool vpd_get_bool(const char *key, enum vpd_region region, uint8_t *val) |
| 255 | { |
| 256 | int size; |
| 257 | const char *value; |
| 258 | |
| 259 | value = vpd_find(key, &size, region); |
| 260 | if (!value) { |
| 261 | printk(BIOS_CRIT, "problem returning from vpd_find.\n"); |
| 262 | return false; |
| 263 | } |
| 264 | |
| 265 | if (size != 1) |
| 266 | return false; |
| 267 | |
| 268 | /* Make sure the value is either '1' or '0' */ |
| 269 | if (*value == '1') { |
| 270 | *val = 1; |
| 271 | return true; |
| 272 | } else if (*value == '0') { |
| 273 | *val = 0; |
| 274 | return true; |
| 275 | } else |
| 276 | return false; |
| 277 | } |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 278 | |
Nico Huber | c00ffef | 2020-09-27 16:21:22 +0200 | [diff] [blame^] | 279 | /* |
| 280 | * Find value of integer type by vpd key. |
| 281 | * |
| 282 | * Expects to find a decimal string, trailing chars are ignored. |
| 283 | * Returns true if the key is found and the value is not too long and |
| 284 | * starts with a decimal digit. Leaves `val` untouched if unsuccessful. |
| 285 | */ |
| 286 | bool vpd_get_int(const char *const key, const enum vpd_region region, int *const val) |
| 287 | { |
| 288 | char value[11]; |
| 289 | |
| 290 | if (!vpd_gets(key, value, sizeof(value), region)) |
| 291 | return false; |
| 292 | |
| 293 | if (!isdigit(*value)) |
| 294 | return false; |
| 295 | |
| 296 | *val = (int)atol(value); |
| 297 | return true; |
| 298 | } |
| 299 | |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 300 | ROMSTAGE_CBMEM_INIT_HOOK(cbmem_add_cros_vpd) |