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