blob: c6dd339f6191b03706ca4a7d5ddb098752e8504f [file] [log] [blame]
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +08001/*
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 Linb15a0d02015-07-13 15:49:57 +08008#include <cbmem.h>
Aaron Durbin0424c952015-03-28 23:56:22 -05009#include <fmap.h>
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +080010#include <stdlib.h>
11#include <string.h>
Julius Werner4f7a3612016-01-20 18:01:15 -080012#include <timestamp.h>
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +080013
Patrick Rudolph28cee592018-03-08 15:43:12 +010014#include "vpd.h"
Hung-Te Linc3455702019-05-27 11:02:00 +080015#include "vpd_decode.h"
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +080016#include "vpd_tables.h"
17
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +080018/* Currently we only support Google VPD 2.0, which has a fixed offset. */
19enum {
20 GOOGLE_VPD_2_0_OFFSET = 0x600,
Hung-Te Linb15a0d02015-07-13 15:49:57 +080021 CROSVPD_CBMEM_MAGIC = 0x43524f53,
22 CROSVPD_CBMEM_VERSION = 0x0001,
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +080023};
24
25struct 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 Linb15a0d02015-07-13 15:49:57 +080032struct 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 */
44static int32_t get_vpd_size(const char *fmap_name, int32_t *base)
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +080045{
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +080046 struct google_vpd_info info;
Aaron Durbin0424c952015-03-28 23:56:22 -050047 struct region_device vpd;
Hung-Te Linb15a0d02015-07-13 15:49:57 +080048 int32_t size;
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +080049
Hung-Te Linb15a0d02015-07-13 15:49:57 +080050 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 Lin6eaaafa2014-02-21 16:21:00 +080054 }
55
Hung-Te Linb15a0d02015-07-13 15:49:57 +080056 size = region_device_sz(&vpd);
Aaron Durbin0424c952015-03-28 23:56:22 -050057
Hung-Te Linb15a0d02015-07-13 15:49:57 +080058 if ((size < GOOGLE_VPD_2_0_OFFSET + sizeof(info)) ||
Aaron Durbin0424c952015-03-28 23:56:22 -050059 rdev_chain(&vpd, &vpd, GOOGLE_VPD_2_0_OFFSET,
Hung-Te Linb15a0d02015-07-13 15:49:57 +080060 size - GOOGLE_VPD_2_0_OFFSET)) {
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +080061 printk(BIOS_ERR, "%s: Too small (%d) for Google VPD 2.0.\n",
Hung-Te Linb15a0d02015-07-13 15:49:57 +080062 __func__, size);
63 return 0;
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +080064 }
65
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +080066 /* Try if we can find a google_vpd_info, otherwise read whole VPD. */
Julius Wernerffc22602016-01-21 11:12:38 -080067 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 Linb15a0d02015-07-13 15:49:57 +080074 == 0 && size >= info.size + sizeof(info)) {
75 *base += sizeof(info);
76 size = info.size;
Julius Wernerffc22602016-01-21 11:12:38 -080077 } 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 Linb15a0d02015-07-13 15:49:57 +080082 } else {
83 size -= GOOGLE_VPD_2_0_OFFSET;
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +080084 }
85
Hung-Te Linb15a0d02015-07-13 15:49:57 +080086 return size;
87}
88
89static 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 Werner4f7a3612016-01-20 18:01:15 -080096 timestamp_add_now(TS_START_COPYVPD);
97
Hung-Te Linb15a0d02015-07-13 15:49:57 +080098 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 Lin6eaaafa2014-02-21 16:21:00 +0800111 }
Hung-Te Linb15a0d02015-07-13 15:49:57 +0800112
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 Werner4f7a3612016-01-20 18:01:15 -0800138 timestamp_add_now(TS_END_COPYVPD_RO);
Hung-Te Linb15a0d02015-07-13 15:49:57 +0800139 }
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 Werner4f7a3612016-01-20 18:01:15 -0800159 timestamp_add_now(TS_END_COPYVPD_RW);
Hung-Te Linb15a0d02015-07-13 15:49:57 +0800160 }
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +0800161}
162
Hung-Te Linc3455702019-05-27 11:02:00 +0800163static 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 Lin6eaaafa2014-02-21 16:21:00 +0800166{
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 Linc3455702019-05-27 11:02:00 +0800170 /* Returns VPD_DECODE_OK to continue parsing. */
171 return VPD_DECODE_OK;
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +0800172
173 result->matched = 1;
174 result->value = value;
175 result->value_len = value_len;
Hung-Te Linc3455702019-05-27 11:02:00 +0800176 /* Returns VPD_DECODE_FAIL to stop parsing. */
177 return VPD_DECODE_FAIL;
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +0800178}
179
Patrick Rudolph28cee592018-03-08 15:43:12 +0100180const void *vpd_find(const char *key, int *size, enum vpd_region region)
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +0800181{
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +0800182 struct vpd_gets_arg arg = {0};
Hung-Te Linc3455702019-05-27 11:02:00 +0800183 uint32_t consumed = 0;
Hung-Te Linb15a0d02015-07-13 15:49:57 +0800184 const struct vpd_cbmem *vpd;
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +0800185
Hung-Te Linb15a0d02015-07-13 15:49:57 +0800186 vpd = cbmem_find(CBMEM_ID_VPD);
187 if (!vpd || !vpd->ro_size)
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +0800188 return NULL;
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +0800189
190 arg.key = (const uint8_t *)key;
191 arg.key_len = strlen(key);
192
Hung-Te Linc3455702019-05-27 11:02:00 +0800193 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 Rudolph28cee592018-03-08 15:43:12 +0100198 }
Hung-Te Linc3455702019-05-27 11:02:00 +0800199 }
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 Rudolph28cee592018-03-08 15:43:12 +0100206 }
Hung-Te Linc3455702019-05-27 11:02:00 +0800207 }
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +0800208
209 if (!arg.matched)
210 return NULL;
211
Vadim Bendebury127c3392014-10-22 17:39:24 -0700212 *size = arg.value_len;
213 return arg.value;
214}
215
Patrick Rudolph28cee592018-03-08 15:43:12 +0100216char *vpd_gets(const char *key, char *buffer, int size, enum vpd_region region)
Vadim Bendebury127c3392014-10-22 17:39:24 -0700217{
218 const void *string_address;
219 int string_size;
220
Patrick Rudolph28cee592018-03-08 15:43:12 +0100221 string_address = vpd_find(key, &string_size, region);
Vadim Bendebury127c3392014-10-22 17:39:24 -0700222
223 if (!string_address)
224 return NULL;
225
226 if (size > (string_size + 1)) {
Stephen Barber31f8a2f2015-06-19 12:56:34 -0700227 memcpy(buffer, string_address, string_size);
228 buffer[string_size] = '\0';
Vadim Bendebury127c3392014-10-22 17:39:24 -0700229 } else {
230 memcpy(buffer, string_address, size - 1);
231 buffer[size - 1] = '\0';
232 }
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +0800233 return buffer;
234}
235
Hung-Te Linb15a0d02015-07-13 15:49:57 +0800236RAMSTAGE_CBMEM_INIT_HOOK(cbmem_add_cros_vpd)