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> |
| 13 | |
| 14 | #include "cros_vpd.h" |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 15 | #include "lib_vpd.h" |
| 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. */ |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 67 | if (rdev_readat(&vpd, &info, *base, sizeof(info)) == sizeof(info) && |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 68 | memcmp(info.header.magic, VPD_INFO_MAGIC, sizeof(info.header.magic)) |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 69 | == 0 && size >= info.size + sizeof(info)) { |
| 70 | *base += sizeof(info); |
| 71 | size = info.size; |
| 72 | } else { |
| 73 | size -= GOOGLE_VPD_2_0_OFFSET; |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 74 | } |
| 75 | |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 76 | return size; |
| 77 | } |
| 78 | |
| 79 | static void cbmem_add_cros_vpd(int is_recovery) |
| 80 | { |
| 81 | struct region_device vpd; |
| 82 | struct vpd_cbmem *cbmem; |
| 83 | int32_t ro_vpd_base = 0, rw_vpd_base = 0; |
| 84 | int32_t ro_vpd_size, rw_vpd_size; |
| 85 | |
| 86 | ro_vpd_size = get_vpd_size("RO_VPD", &ro_vpd_base); |
| 87 | rw_vpd_size = get_vpd_size("RW_VPD", &rw_vpd_base); |
| 88 | |
| 89 | /* no VPD at all? nothing to do then */ |
| 90 | if ((ro_vpd_size == 0) && (rw_vpd_size == 0)) |
| 91 | return; |
| 92 | |
| 93 | cbmem = cbmem_add(CBMEM_ID_VPD, sizeof(*cbmem) + ro_vpd_size + |
| 94 | rw_vpd_size); |
| 95 | if (!cbmem) { |
| 96 | printk(BIOS_ERR, "%s: Failed to allocate CBMEM (%u+%u).\n", |
| 97 | __func__, ro_vpd_size, rw_vpd_size); |
| 98 | return; |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 99 | } |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 100 | |
| 101 | cbmem->magic = CROSVPD_CBMEM_MAGIC; |
| 102 | cbmem->version = CROSVPD_CBMEM_VERSION; |
| 103 | cbmem->ro_size = 0; |
| 104 | cbmem->rw_size = 0; |
| 105 | |
| 106 | if (ro_vpd_size) { |
| 107 | if (fmap_locate_area_as_rdev("RO_VPD", &vpd)) { |
| 108 | /* shouldn't happen, but let's be extra defensive */ |
| 109 | printk(BIOS_ERR, "%s: No RO_VPD FMAP section.\n", |
| 110 | __func__); |
| 111 | return; |
| 112 | } |
| 113 | rdev_chain(&vpd, &vpd, GOOGLE_VPD_2_0_OFFSET, |
| 114 | region_device_sz(&vpd) - GOOGLE_VPD_2_0_OFFSET); |
| 115 | |
| 116 | |
| 117 | if (rdev_readat(&vpd, cbmem->blob, ro_vpd_base, ro_vpd_size) == |
| 118 | ro_vpd_size) { |
| 119 | cbmem->ro_size = ro_vpd_size; |
| 120 | } else { |
| 121 | printk(BIOS_ERR, |
| 122 | "%s: Reading RO_VPD FMAP section failed.\n", |
| 123 | __func__); |
| 124 | ro_vpd_size = 0; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | if (rw_vpd_size) { |
| 129 | if (fmap_locate_area_as_rdev("RW_VPD", &vpd)) { |
| 130 | /* shouldn't happen, but let's be extra defensive */ |
| 131 | printk(BIOS_ERR, "%s: No RW_VPD FMAP section.\n", |
| 132 | __func__); |
| 133 | return; |
| 134 | } |
| 135 | rdev_chain(&vpd, &vpd, GOOGLE_VPD_2_0_OFFSET, |
| 136 | region_device_sz(&vpd) - GOOGLE_VPD_2_0_OFFSET); |
| 137 | |
| 138 | if (rdev_readat(&vpd, cbmem->blob + ro_vpd_size, rw_vpd_base, |
| 139 | rw_vpd_size) == rw_vpd_size) { |
| 140 | cbmem->rw_size = rw_vpd_size; |
| 141 | } else { |
| 142 | printk(BIOS_ERR, |
| 143 | "%s: Reading RW_VPD FMAP section failed.\n", |
| 144 | __func__); |
| 145 | } |
| 146 | } |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | static int vpd_gets_callback(const uint8_t *key, int32_t key_len, |
| 150 | const uint8_t *value, int32_t value_len, |
| 151 | void *arg) |
| 152 | { |
| 153 | struct vpd_gets_arg *result = (struct vpd_gets_arg *)arg; |
| 154 | if (key_len != result->key_len || |
| 155 | memcmp(key, result->key, key_len) != 0) |
| 156 | /* Returns VPD_OK to continue parsing. */ |
| 157 | return VPD_OK; |
| 158 | |
| 159 | result->matched = 1; |
| 160 | result->value = value; |
| 161 | result->value_len = value_len; |
| 162 | /* Returns VPD_FAIL to stop parsing. */ |
| 163 | return VPD_FAIL; |
| 164 | } |
| 165 | |
Vadim Bendebury | 127c339 | 2014-10-22 17:39:24 -0700 | [diff] [blame] | 166 | const void *cros_vpd_find(const char *key, int *size) |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 167 | { |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 168 | struct vpd_gets_arg arg = {0}; |
| 169 | int consumed = 0; |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 170 | const struct vpd_cbmem *vpd; |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 171 | |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 172 | vpd = cbmem_find(CBMEM_ID_VPD); |
| 173 | if (!vpd || !vpd->ro_size) |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 174 | return NULL; |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 175 | |
| 176 | arg.key = (const uint8_t *)key; |
| 177 | arg.key_len = strlen(key); |
| 178 | |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 179 | while (VPD_OK == decodeVpdString(vpd->ro_size, vpd->blob, &consumed, |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 180 | vpd_gets_callback, &arg)) { |
| 181 | /* Iterate until found or no more entries. */ |
| 182 | } |
| 183 | |
| 184 | if (!arg.matched) |
| 185 | return NULL; |
| 186 | |
Vadim Bendebury | 127c339 | 2014-10-22 17:39:24 -0700 | [diff] [blame] | 187 | *size = arg.value_len; |
| 188 | return arg.value; |
| 189 | } |
| 190 | |
| 191 | char *cros_vpd_gets(const char *key, char *buffer, int size) |
| 192 | { |
| 193 | const void *string_address; |
| 194 | int string_size; |
| 195 | |
| 196 | string_address = cros_vpd_find(key, &string_size); |
| 197 | |
| 198 | if (!string_address) |
| 199 | return NULL; |
| 200 | |
| 201 | if (size > (string_size + 1)) { |
Stephen Barber | 31f8a2f | 2015-06-19 12:56:34 -0700 | [diff] [blame] | 202 | memcpy(buffer, string_address, string_size); |
| 203 | buffer[string_size] = '\0'; |
Vadim Bendebury | 127c339 | 2014-10-22 17:39:24 -0700 | [diff] [blame] | 204 | } else { |
| 205 | memcpy(buffer, string_address, size - 1); |
| 206 | buffer[size - 1] = '\0'; |
| 207 | } |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 208 | return buffer; |
| 209 | } |
| 210 | |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 211 | RAMSTAGE_CBMEM_INIT_HOOK(cbmem_add_cros_vpd) |