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> |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 6 | #include <fmap.h> |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 7 | #include <program_loading.h> |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 8 | #include <string.h> |
Julius Werner | 4f7a361 | 2016-01-20 18:01:15 -0800 | [diff] [blame] | 9 | #include <timestamp.h> |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 10 | |
Patrick Rudolph | 28cee59 | 2018-03-08 15:43:12 +0100 | [diff] [blame] | 11 | #include "vpd.h" |
Hung-Te Lin | c345570 | 2019-05-27 11:02:00 +0800 | [diff] [blame] | 12 | #include "vpd_decode.h" |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 13 | #include "vpd_tables.h" |
| 14 | |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 15 | /* Currently we only support Google VPD 2.0, which has a fixed offset. */ |
| 16 | enum { |
| 17 | CROSVPD_CBMEM_MAGIC = 0x43524f53, |
| 18 | CROSVPD_CBMEM_VERSION = 0x0001, |
| 19 | }; |
| 20 | |
| 21 | struct vpd_cbmem { |
| 22 | uint32_t magic; |
| 23 | uint32_t version; |
| 24 | uint32_t ro_size; |
| 25 | uint32_t rw_size; |
| 26 | uint8_t blob[0]; |
| 27 | /* The blob contains both RO and RW data. It starts with RO (0 .. |
| 28 | * ro_size) and then RW (ro_size .. ro_size+rw_size). |
| 29 | */ |
| 30 | }; |
| 31 | |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 32 | struct vpd_gets_arg { |
| 33 | const uint8_t *key; |
| 34 | const uint8_t *value; |
| 35 | int32_t key_len, value_len; |
| 36 | int matched; |
| 37 | }; |
| 38 | |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 39 | static struct region_device ro_vpd, rw_vpd; |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 40 | |
Jonathan Zhang | b5392f9 | 2019-07-16 14:37:23 -0700 | [diff] [blame] | 41 | /* |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 42 | * Initializes a region_device to represent the requested VPD 2.0 formatted |
| 43 | * region on flash. On errors rdev->size will be set to 0. |
Jonathan Zhang | b5392f9 | 2019-07-16 14:37:23 -0700 | [diff] [blame] | 44 | */ |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 45 | 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] | 46 | { |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 47 | struct google_vpd_info info; |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 48 | int32_t size; |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 49 | |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 50 | if (fmap_locate_area_as_rdev(fmap_name, rdev)) { |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 51 | printk(BIOS_ERR, "%s: No %s FMAP section.\n", __func__, |
| 52 | fmap_name); |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 53 | goto fail; |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 54 | } |
| 55 | |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 56 | size = region_device_sz(rdev); |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 57 | |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 58 | if ((size < GOOGLE_VPD_2_0_OFFSET + sizeof(info)) || |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 59 | rdev_chain(rdev, rdev, GOOGLE_VPD_2_0_OFFSET, |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 60 | size - GOOGLE_VPD_2_0_OFFSET)) { |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 61 | 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] | 62 | __func__, size); |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 63 | goto fail; |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 64 | } |
| 65 | |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 66 | /* 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] | 67 | if (rdev_readat(rdev, &info, 0, sizeof(info)) != sizeof(info)) { |
Julius Werner | ffc2260 | 2016-01-21 11:12:38 -0800 | [diff] [blame] | 68 | printk(BIOS_ERR, "ERROR: Failed to read %s header.\n", |
| 69 | fmap_name); |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 70 | goto fail; |
Julius Werner | ffc2260 | 2016-01-21 11:12:38 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | if (memcmp(info.header.magic, VPD_INFO_MAGIC, sizeof(info.header.magic)) |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 74 | == 0) { |
| 75 | if (rdev_chain(rdev, rdev, sizeof(info), info.size)) { |
| 76 | printk(BIOS_ERR, "ERROR: %s info size too large.\n", |
| 77 | fmap_name); |
| 78 | goto fail; |
| 79 | } |
Julius Werner | ffc2260 | 2016-01-21 11:12:38 -0800 | [diff] [blame] | 80 | } else if (info.header.tlv.type == VPD_TYPE_TERMINATOR || |
| 81 | info.header.tlv.type == VPD_TYPE_IMPLICIT_TERMINATOR) { |
| 82 | printk(BIOS_WARNING, "WARNING: %s is uninitialized or empty.\n", |
| 83 | fmap_name); |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 84 | goto fail; |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 85 | } |
| 86 | |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 87 | return; |
| 88 | |
| 89 | fail: |
| 90 | memset(rdev, 0, sizeof(*rdev)); |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 91 | } |
| 92 | |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 93 | static int init_vpd_rdevs_from_cbmem(void) |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 94 | { |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 95 | if (!cbmem_possibly_online()) |
| 96 | return -1; |
Jonathan Zhang | b5392f9 | 2019-07-16 14:37:23 -0700 | [diff] [blame] | 97 | |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 98 | struct vpd_cbmem *cbmem = cbmem_find(CBMEM_ID_VPD); |
| 99 | if (!cbmem) |
| 100 | return -1; |
| 101 | |
| 102 | rdev_chain(&ro_vpd, &addrspace_32bit.rdev, |
| 103 | (uintptr_t)cbmem->blob, cbmem->ro_size); |
| 104 | rdev_chain(&rw_vpd, &addrspace_32bit.rdev, |
| 105 | (uintptr_t)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) |
Jonathan Zhang | b5392f9 | 2019-07-16 14:37:23 -0700 | [diff] [blame] | 115 | return; |
| 116 | |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 117 | if (init_vpd_rdevs_from_cbmem() != 0) { |
| 118 | init_vpd_rdev("RO_VPD", &ro_vpd); |
| 119 | init_vpd_rdev("RW_VPD", &rw_vpd); |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 120 | } |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 121 | |
| 122 | done = true; |
Jonathan Zhang | b5392f9 | 2019-07-16 14:37:23 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 125 | static void cbmem_add_cros_vpd(int is_recovery) |
Jonathan Zhang | b5392f9 | 2019-07-16 14:37:23 -0700 | [diff] [blame] | 126 | { |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 127 | struct vpd_cbmem *cbmem; |
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 | timestamp_add_now(TS_START_COPYVPD); |
| 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, "ERROR: Couldn't read RO VPD\n"); |
| 155 | cbmem->ro_size = ro_size = 0; |
| 156 | } |
| 157 | timestamp_add_now(TS_END_COPYVPD_RO); |
| 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, "ERROR: Couldn't read RW VPD\n"); |
| 164 | cbmem->rw_size = rw_size = 0; |
| 165 | } |
| 166 | timestamp_add_now(TS_END_COPYVPD_RW); |
| 167 | } |
| 168 | |
| 169 | init_vpd_rdevs_from_cbmem(); |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 170 | } |
| 171 | |
Hung-Te Lin | c345570 | 2019-05-27 11:02:00 +0800 | [diff] [blame] | 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) |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 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) |
Hung-Te Lin | c345570 | 2019-05-27 11:02:00 +0800 | [diff] [blame] | 179 | /* Returns VPD_DECODE_OK to continue parsing. */ |
| 180 | return VPD_DECODE_OK; |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 181 | |
| 182 | result->matched = 1; |
| 183 | result->value = value; |
| 184 | result->value_len = value_len; |
Hung-Te Lin | c345570 | 2019-05-27 11:02:00 +0800 | [diff] [blame] | 185 | /* Returns VPD_DECODE_FAIL to stop parsing. */ |
| 186 | return VPD_DECODE_FAIL; |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 187 | } |
| 188 | |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 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 | |
Patrick Rudolph | 28cee59 | 2018-03-08 15:43:12 +0100 | [diff] [blame] | 203 | 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] | 204 | { |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 205 | struct vpd_gets_arg arg = {0}; |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 206 | |
| 207 | arg.key = (const uint8_t *)key; |
| 208 | arg.key_len = strlen(key); |
| 209 | |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 210 | init_vpd_rdevs(); |
Jonathan Zhang | b5392f9 | 2019-07-16 14:37:23 -0700 | [diff] [blame] | 211 | |
Jonathan Zhang | 9b110bf | 2020-05-28 13:35:44 -0700 | [diff] [blame^] | 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)) |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 217 | vpd_find_in(&ro_vpd, &arg); |
| 218 | |
Jonathan Zhang | 9b110bf | 2020-05-28 13:35:44 -0700 | [diff] [blame^] | 219 | if (!arg.matched && (region == VPD_RW || region == VPD_RO_THEN_RW)) |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 220 | vpd_find_in(&rw_vpd, &arg); |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 221 | |
| 222 | if (!arg.matched) |
| 223 | return NULL; |
| 224 | |
Vadim Bendebury | 127c339 | 2014-10-22 17:39:24 -0700 | [diff] [blame] | 225 | *size = arg.value_len; |
| 226 | return arg.value; |
| 227 | } |
| 228 | |
Patrick Rudolph | 28cee59 | 2018-03-08 15:43:12 +0100 | [diff] [blame] | 229 | 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] | 230 | { |
| 231 | const void *string_address; |
| 232 | int string_size; |
| 233 | |
Patrick Rudolph | 28cee59 | 2018-03-08 15:43:12 +0100 | [diff] [blame] | 234 | string_address = vpd_find(key, &string_size, region); |
Vadim Bendebury | 127c339 | 2014-10-22 17:39:24 -0700 | [diff] [blame] | 235 | |
| 236 | if (!string_address) |
| 237 | return NULL; |
| 238 | |
Jonathan Zhang | b5392f9 | 2019-07-16 14:37:23 -0700 | [diff] [blame] | 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'; |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 243 | return buffer; |
| 244 | } |
| 245 | |
Jonathan Zhang | b5392f9 | 2019-07-16 14:37:23 -0700 | [diff] [blame] | 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 | } |
Julius Werner | 5f17458 | 2020-05-26 19:03:31 -0700 | [diff] [blame] | 276 | |
| 277 | ROMSTAGE_CBMEM_INIT_HOOK(cbmem_add_cros_vpd) |