blob: e826d3621874f55a5fd7cc9d406b649469f272e2 [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>
8
Hung-Te Linb15a0d02015-07-13 15:49:57 +08009#include <cbmem.h>
Aaron Durbin0424c952015-03-28 23:56:22 -050010#include <fmap.h>
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +080011#include <stdlib.h>
12#include <string.h>
Julius Werner4f7a3612016-01-20 18:01:15 -080013#include <timestamp.h>
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +080014
15#include "cros_vpd.h"
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +080016#include "lib_vpd.h"
17#include "vpd_tables.h"
18
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +080019/* Currently we only support Google VPD 2.0, which has a fixed offset. */
20enum {
21 GOOGLE_VPD_2_0_OFFSET = 0x600,
Hung-Te Linb15a0d02015-07-13 15:49:57 +080022 CROSVPD_CBMEM_MAGIC = 0x43524f53,
23 CROSVPD_CBMEM_VERSION = 0x0001,
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +080024};
25
26struct 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 Linb15a0d02015-07-13 15:49:57 +080033struct 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 */
45static int32_t get_vpd_size(const char *fmap_name, int32_t *base)
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +080046{
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +080047 struct google_vpd_info info;
Aaron Durbin0424c952015-03-28 23:56:22 -050048 struct region_device vpd;
Hung-Te Linb15a0d02015-07-13 15:49:57 +080049 int32_t size;
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +080050
Hung-Te Linb15a0d02015-07-13 15:49:57 +080051 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 Lin6eaaafa2014-02-21 16:21:00 +080055 }
56
Hung-Te Linb15a0d02015-07-13 15:49:57 +080057 size = region_device_sz(&vpd);
Aaron Durbin0424c952015-03-28 23:56:22 -050058
Hung-Te Linb15a0d02015-07-13 15:49:57 +080059 if ((size < GOOGLE_VPD_2_0_OFFSET + sizeof(info)) ||
Aaron Durbin0424c952015-03-28 23:56:22 -050060 rdev_chain(&vpd, &vpd, GOOGLE_VPD_2_0_OFFSET,
Hung-Te Linb15a0d02015-07-13 15:49:57 +080061 size - GOOGLE_VPD_2_0_OFFSET)) {
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +080062 printk(BIOS_ERR, "%s: Too small (%d) for Google VPD 2.0.\n",
Hung-Te Linb15a0d02015-07-13 15:49:57 +080063 __func__, size);
64 return 0;
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +080065 }
66
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +080067 /* Try if we can find a google_vpd_info, otherwise read whole VPD. */
Hung-Te Linb15a0d02015-07-13 15:49:57 +080068 if (rdev_readat(&vpd, &info, *base, sizeof(info)) == sizeof(info) &&
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +080069 memcmp(info.header.magic, VPD_INFO_MAGIC, sizeof(info.header.magic))
Hung-Te Linb15a0d02015-07-13 15:49:57 +080070 == 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 Lin6eaaafa2014-02-21 16:21:00 +080075 }
76
Hung-Te Linb15a0d02015-07-13 15:49:57 +080077 return size;
78}
79
80static 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 Werner4f7a3612016-01-20 18:01:15 -080087 timestamp_add_now(TS_START_COPYVPD);
88
Hung-Te Linb15a0d02015-07-13 15:49:57 +080089 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 Lin6eaaafa2014-02-21 16:21:00 +0800102 }
Hung-Te Linb15a0d02015-07-13 15:49:57 +0800103
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 Werner4f7a3612016-01-20 18:01:15 -0800129 timestamp_add_now(TS_END_COPYVPD_RO);
Hung-Te Linb15a0d02015-07-13 15:49:57 +0800130 }
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 Werner4f7a3612016-01-20 18:01:15 -0800150 timestamp_add_now(TS_END_COPYVPD_RW);
Hung-Te Linb15a0d02015-07-13 15:49:57 +0800151 }
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +0800152}
153
154static 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 Bendebury127c3392014-10-22 17:39:24 -0700171const void *cros_vpd_find(const char *key, int *size)
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +0800172{
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +0800173 struct vpd_gets_arg arg = {0};
174 int consumed = 0;
Hung-Te Linb15a0d02015-07-13 15:49:57 +0800175 const struct vpd_cbmem *vpd;
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +0800176
Hung-Te Linb15a0d02015-07-13 15:49:57 +0800177 vpd = cbmem_find(CBMEM_ID_VPD);
178 if (!vpd || !vpd->ro_size)
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +0800179 return NULL;
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +0800180
181 arg.key = (const uint8_t *)key;
182 arg.key_len = strlen(key);
183
Hung-Te Linb15a0d02015-07-13 15:49:57 +0800184 while (VPD_OK == decodeVpdString(vpd->ro_size, vpd->blob, &consumed,
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +0800185 vpd_gets_callback, &arg)) {
186 /* Iterate until found or no more entries. */
187 }
188
189 if (!arg.matched)
190 return NULL;
191
Vadim Bendebury127c3392014-10-22 17:39:24 -0700192 *size = arg.value_len;
193 return arg.value;
194}
195
196char *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 Barber31f8a2f2015-06-19 12:56:34 -0700207 memcpy(buffer, string_address, string_size);
208 buffer[string_size] = '\0';
Vadim Bendebury127c3392014-10-22 17:39:24 -0700209 } else {
210 memcpy(buffer, string_address, size - 1);
211 buffer[size - 1] = '\0';
212 }
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +0800213 return buffer;
214}
215
Hung-Te Linb15a0d02015-07-13 15:49:57 +0800216RAMSTAGE_CBMEM_INIT_HOOK(cbmem_add_cros_vpd)