Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 The Chromium OS Authors. All rights reserved. |
| 3 | * Use of this source code is governed by a BSD-style license that can be |
| 4 | * found in the LICENSE file. |
| 5 | */ |
| 6 | |
| 7 | #include <console/console.h> |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 8 | #include <cbmem.h> |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 9 | #include <fmap.h> |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 10 | #include <stdlib.h> |
| 11 | #include <string.h> |
Julius Werner | 4f7a361 | 2016-01-20 18:01:15 -0800 | [diff] [blame] | 12 | #include <timestamp.h> |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 13 | |
Patrick Rudolph | 28cee59 | 2018-03-08 15:43:12 +0100 | [diff] [blame] | 14 | #include "vpd.h" |
Hung-Te Lin | c345570 | 2019-05-27 11:02:00 +0800 | [diff] [blame^] | 15 | #include "vpd_decode.h" |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 16 | #include "vpd_tables.h" |
| 17 | |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 18 | /* Currently we only support Google VPD 2.0, which has a fixed offset. */ |
| 19 | enum { |
| 20 | GOOGLE_VPD_2_0_OFFSET = 0x600, |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 21 | CROSVPD_CBMEM_MAGIC = 0x43524f53, |
| 22 | CROSVPD_CBMEM_VERSION = 0x0001, |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 23 | }; |
| 24 | |
| 25 | struct vpd_gets_arg { |
| 26 | const uint8_t *key; |
| 27 | const uint8_t *value; |
| 28 | int32_t key_len, value_len; |
| 29 | int matched; |
| 30 | }; |
| 31 | |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 32 | struct vpd_cbmem { |
| 33 | uint32_t magic; |
| 34 | uint32_t version; |
| 35 | uint32_t ro_size; |
| 36 | uint32_t rw_size; |
| 37 | uint8_t blob[0]; |
| 38 | /* The blob contains both RO and RW data. It starts with RO (0 .. |
| 39 | * ro_size) and then RW (ro_size .. ro_size+rw_size). |
| 40 | */ |
| 41 | }; |
| 42 | |
| 43 | /* returns the size of data in a VPD 2.0 formatted fmap region, or 0 */ |
| 44 | static int32_t get_vpd_size(const char *fmap_name, int32_t *base) |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 45 | { |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 46 | struct google_vpd_info info; |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 47 | struct region_device vpd; |
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 | |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 50 | if (fmap_locate_area_as_rdev(fmap_name, &vpd)) { |
| 51 | printk(BIOS_ERR, "%s: No %s FMAP section.\n", __func__, |
| 52 | fmap_name); |
| 53 | return 0; |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 54 | } |
| 55 | |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 56 | size = region_device_sz(&vpd); |
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)) || |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 59 | rdev_chain(&vpd, &vpd, 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); |
| 63 | return 0; |
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 | ffc2260 | 2016-01-21 11:12:38 -0800 | [diff] [blame] | 67 | if (rdev_readat(&vpd, &info, *base, sizeof(info)) != sizeof(info)) { |
| 68 | printk(BIOS_ERR, "ERROR: Failed to read %s header.\n", |
| 69 | fmap_name); |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | if (memcmp(info.header.magic, VPD_INFO_MAGIC, sizeof(info.header.magic)) |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 74 | == 0 && size >= info.size + sizeof(info)) { |
| 75 | *base += sizeof(info); |
| 76 | size = info.size; |
Julius Werner | ffc2260 | 2016-01-21 11:12:38 -0800 | [diff] [blame] | 77 | } else if (info.header.tlv.type == VPD_TYPE_TERMINATOR || |
| 78 | info.header.tlv.type == VPD_TYPE_IMPLICIT_TERMINATOR) { |
| 79 | printk(BIOS_WARNING, "WARNING: %s is uninitialized or empty.\n", |
| 80 | fmap_name); |
| 81 | size = 0; |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 82 | } else { |
| 83 | size -= GOOGLE_VPD_2_0_OFFSET; |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 84 | } |
| 85 | |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 86 | return size; |
| 87 | } |
| 88 | |
| 89 | static void cbmem_add_cros_vpd(int is_recovery) |
| 90 | { |
| 91 | struct region_device vpd; |
| 92 | struct vpd_cbmem *cbmem; |
| 93 | int32_t ro_vpd_base = 0, rw_vpd_base = 0; |
| 94 | int32_t ro_vpd_size, rw_vpd_size; |
| 95 | |
Julius Werner | 4f7a361 | 2016-01-20 18:01:15 -0800 | [diff] [blame] | 96 | timestamp_add_now(TS_START_COPYVPD); |
| 97 | |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 98 | ro_vpd_size = get_vpd_size("RO_VPD", &ro_vpd_base); |
| 99 | rw_vpd_size = get_vpd_size("RW_VPD", &rw_vpd_base); |
| 100 | |
| 101 | /* no VPD at all? nothing to do then */ |
| 102 | if ((ro_vpd_size == 0) && (rw_vpd_size == 0)) |
| 103 | return; |
| 104 | |
| 105 | cbmem = cbmem_add(CBMEM_ID_VPD, sizeof(*cbmem) + ro_vpd_size + |
| 106 | rw_vpd_size); |
| 107 | if (!cbmem) { |
| 108 | printk(BIOS_ERR, "%s: Failed to allocate CBMEM (%u+%u).\n", |
| 109 | __func__, ro_vpd_size, rw_vpd_size); |
| 110 | return; |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 111 | } |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 112 | |
| 113 | cbmem->magic = CROSVPD_CBMEM_MAGIC; |
| 114 | cbmem->version = CROSVPD_CBMEM_VERSION; |
| 115 | cbmem->ro_size = 0; |
| 116 | cbmem->rw_size = 0; |
| 117 | |
| 118 | if (ro_vpd_size) { |
| 119 | if (fmap_locate_area_as_rdev("RO_VPD", &vpd)) { |
| 120 | /* shouldn't happen, but let's be extra defensive */ |
| 121 | printk(BIOS_ERR, "%s: No RO_VPD FMAP section.\n", |
| 122 | __func__); |
| 123 | return; |
| 124 | } |
| 125 | rdev_chain(&vpd, &vpd, GOOGLE_VPD_2_0_OFFSET, |
| 126 | region_device_sz(&vpd) - GOOGLE_VPD_2_0_OFFSET); |
| 127 | |
| 128 | |
| 129 | if (rdev_readat(&vpd, cbmem->blob, ro_vpd_base, ro_vpd_size) == |
| 130 | ro_vpd_size) { |
| 131 | cbmem->ro_size = ro_vpd_size; |
| 132 | } else { |
| 133 | printk(BIOS_ERR, |
| 134 | "%s: Reading RO_VPD FMAP section failed.\n", |
| 135 | __func__); |
| 136 | ro_vpd_size = 0; |
| 137 | } |
Julius Werner | 4f7a361 | 2016-01-20 18:01:15 -0800 | [diff] [blame] | 138 | timestamp_add_now(TS_END_COPYVPD_RO); |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | if (rw_vpd_size) { |
| 142 | if (fmap_locate_area_as_rdev("RW_VPD", &vpd)) { |
| 143 | /* shouldn't happen, but let's be extra defensive */ |
| 144 | printk(BIOS_ERR, "%s: No RW_VPD FMAP section.\n", |
| 145 | __func__); |
| 146 | return; |
| 147 | } |
| 148 | rdev_chain(&vpd, &vpd, GOOGLE_VPD_2_0_OFFSET, |
| 149 | region_device_sz(&vpd) - GOOGLE_VPD_2_0_OFFSET); |
| 150 | |
| 151 | if (rdev_readat(&vpd, cbmem->blob + ro_vpd_size, rw_vpd_base, |
| 152 | rw_vpd_size) == rw_vpd_size) { |
| 153 | cbmem->rw_size = rw_vpd_size; |
| 154 | } else { |
| 155 | printk(BIOS_ERR, |
| 156 | "%s: Reading RW_VPD FMAP section failed.\n", |
| 157 | __func__); |
| 158 | } |
Julius Werner | 4f7a361 | 2016-01-20 18:01:15 -0800 | [diff] [blame] | 159 | timestamp_add_now(TS_END_COPYVPD_RW); |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 160 | } |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 161 | } |
| 162 | |
Hung-Te Lin | c345570 | 2019-05-27 11:02:00 +0800 | [diff] [blame^] | 163 | static int vpd_gets_callback(const uint8_t *key, uint32_t key_len, |
| 164 | const uint8_t *value, uint32_t value_len, |
| 165 | void *arg) |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 166 | { |
| 167 | struct vpd_gets_arg *result = (struct vpd_gets_arg *)arg; |
| 168 | if (key_len != result->key_len || |
| 169 | memcmp(key, result->key, key_len) != 0) |
Hung-Te Lin | c345570 | 2019-05-27 11:02:00 +0800 | [diff] [blame^] | 170 | /* Returns VPD_DECODE_OK to continue parsing. */ |
| 171 | return VPD_DECODE_OK; |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 172 | |
| 173 | result->matched = 1; |
| 174 | result->value = value; |
| 175 | result->value_len = value_len; |
Hung-Te Lin | c345570 | 2019-05-27 11:02:00 +0800 | [diff] [blame^] | 176 | /* Returns VPD_DECODE_FAIL to stop parsing. */ |
| 177 | return VPD_DECODE_FAIL; |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 178 | } |
| 179 | |
Patrick Rudolph | 28cee59 | 2018-03-08 15:43:12 +0100 | [diff] [blame] | 180 | 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] | 181 | { |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 182 | struct vpd_gets_arg arg = {0}; |
Hung-Te Lin | c345570 | 2019-05-27 11:02:00 +0800 | [diff] [blame^] | 183 | uint32_t consumed = 0; |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 184 | const struct vpd_cbmem *vpd; |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 185 | |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 186 | vpd = cbmem_find(CBMEM_ID_VPD); |
| 187 | if (!vpd || !vpd->ro_size) |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 188 | return NULL; |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 189 | |
| 190 | arg.key = (const uint8_t *)key; |
| 191 | arg.key_len = strlen(key); |
| 192 | |
Hung-Te Lin | c345570 | 2019-05-27 11:02:00 +0800 | [diff] [blame^] | 193 | if (region == VPD_ANY || region == VPD_RO) { |
| 194 | while (vpd_decode_string( |
| 195 | vpd->ro_size, vpd->blob, &consumed, |
| 196 | vpd_gets_callback, &arg) == VPD_DECODE_OK) { |
| 197 | /* Iterate until found or no more entries. */ |
Patrick Rudolph | 28cee59 | 2018-03-08 15:43:12 +0100 | [diff] [blame] | 198 | } |
Hung-Te Lin | c345570 | 2019-05-27 11:02:00 +0800 | [diff] [blame^] | 199 | } |
| 200 | if (!arg.matched && region != VPD_RO) { |
| 201 | while (vpd_decode_string( |
| 202 | vpd->rw_size, vpd->blob + vpd->ro_size, |
| 203 | &consumed, vpd_gets_callback, |
| 204 | &arg) == VPD_DECODE_OK) { |
| 205 | /* Iterate until found or no more entries. */ |
Patrick Rudolph | 28cee59 | 2018-03-08 15:43:12 +0100 | [diff] [blame] | 206 | } |
Hung-Te Lin | c345570 | 2019-05-27 11:02:00 +0800 | [diff] [blame^] | 207 | } |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 208 | |
| 209 | if (!arg.matched) |
| 210 | return NULL; |
| 211 | |
Vadim Bendebury | 127c339 | 2014-10-22 17:39:24 -0700 | [diff] [blame] | 212 | *size = arg.value_len; |
| 213 | return arg.value; |
| 214 | } |
| 215 | |
Patrick Rudolph | 28cee59 | 2018-03-08 15:43:12 +0100 | [diff] [blame] | 216 | 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] | 217 | { |
| 218 | const void *string_address; |
| 219 | int string_size; |
| 220 | |
Patrick Rudolph | 28cee59 | 2018-03-08 15:43:12 +0100 | [diff] [blame] | 221 | string_address = vpd_find(key, &string_size, region); |
Vadim Bendebury | 127c339 | 2014-10-22 17:39:24 -0700 | [diff] [blame] | 222 | |
| 223 | if (!string_address) |
| 224 | return NULL; |
| 225 | |
| 226 | if (size > (string_size + 1)) { |
Stephen Barber | 31f8a2f | 2015-06-19 12:56:34 -0700 | [diff] [blame] | 227 | memcpy(buffer, string_address, string_size); |
| 228 | buffer[string_size] = '\0'; |
Vadim Bendebury | 127c339 | 2014-10-22 17:39:24 -0700 | [diff] [blame] | 229 | } else { |
| 230 | memcpy(buffer, string_address, size - 1); |
| 231 | buffer[size - 1] = '\0'; |
| 232 | } |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 233 | return buffer; |
| 234 | } |
| 235 | |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 236 | RAMSTAGE_CBMEM_INIT_HOOK(cbmem_add_cros_vpd) |