blob: 8215454745a53a76e96c0b2930c54d474455b02e [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>
13
14#include "cros_vpd.h"
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +080015#include "lib_vpd.h"
16#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. */
Hung-Te Linb15a0d02015-07-13 15:49:57 +080067 if (rdev_readat(&vpd, &info, *base, sizeof(info)) == sizeof(info) &&
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +080068 memcmp(info.header.magic, VPD_INFO_MAGIC, sizeof(info.header.magic))
Hung-Te Linb15a0d02015-07-13 15:49:57 +080069 == 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 Lin6eaaafa2014-02-21 16:21:00 +080074 }
75
Hung-Te Linb15a0d02015-07-13 15:49:57 +080076 return size;
77}
78
79static 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 Lin6eaaafa2014-02-21 16:21:00 +080099 }
Hung-Te Linb15a0d02015-07-13 15:49:57 +0800100
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 Lin6eaaafa2014-02-21 16:21:00 +0800147}
148
149static 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 Bendebury127c3392014-10-22 17:39:24 -0700166const void *cros_vpd_find(const char *key, int *size)
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +0800167{
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +0800168 struct vpd_gets_arg arg = {0};
169 int consumed = 0;
Hung-Te Linb15a0d02015-07-13 15:49:57 +0800170 const struct vpd_cbmem *vpd;
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +0800171
Hung-Te Linb15a0d02015-07-13 15:49:57 +0800172 vpd = cbmem_find(CBMEM_ID_VPD);
173 if (!vpd || !vpd->ro_size)
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +0800174 return NULL;
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +0800175
176 arg.key = (const uint8_t *)key;
177 arg.key_len = strlen(key);
178
Hung-Te Linb15a0d02015-07-13 15:49:57 +0800179 while (VPD_OK == decodeVpdString(vpd->ro_size, vpd->blob, &consumed,
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +0800180 vpd_gets_callback, &arg)) {
181 /* Iterate until found or no more entries. */
182 }
183
184 if (!arg.matched)
185 return NULL;
186
Vadim Bendebury127c3392014-10-22 17:39:24 -0700187 *size = arg.value_len;
188 return arg.value;
189}
190
191char *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 Barber31f8a2f2015-06-19 12:56:34 -0700202 memcpy(buffer, string_address, string_size);
203 buffer[string_size] = '\0';
Vadim Bendebury127c3392014-10-22 17:39:24 -0700204 } else {
205 memcpy(buffer, string_address, size - 1);
206 buffer[size - 1] = '\0';
207 }
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +0800208 return buffer;
209}
210
Hung-Te Linb15a0d02015-07-13 15:49:57 +0800211RAMSTAGE_CBMEM_INIT_HOOK(cbmem_add_cros_vpd)