blob: fed1d823444015907fdf3162ff50b719cee892e9 [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
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>
12
13#include "cros_vpd.h"
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +080014#include "lib_vpd.h"
15#include "vpd_tables.h"
16
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +080017/* Currently we only support Google VPD 2.0, which has a fixed offset. */
18enum {
19 GOOGLE_VPD_2_0_OFFSET = 0x600,
20};
21
22struct vpd_gets_arg {
23 const uint8_t *key;
24 const uint8_t *value;
25 int32_t key_len, value_len;
26 int matched;
27};
28
29static int cros_vpd_load(uint8_t **vpd_address, int32_t *vpd_size)
30{
Julius Wernerdbe0df12014-06-06 16:10:56 -070031 MAYBE_STATIC int cached = 0;
32 MAYBE_STATIC uint8_t *cached_address = NULL;
33 MAYBE_STATIC int32_t cached_size = 0;
34 MAYBE_STATIC int result = -1;
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +080035 struct google_vpd_info info;
36 int32_t base;
Aaron Durbin0424c952015-03-28 23:56:22 -050037 struct region_device vpd;
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +080038
39 if (cached) {
40 *vpd_address = cached_address;
41 *vpd_size = cached_size;
42 return result;
43 }
44
45 cached = 1;
Aaron Durbin0424c952015-03-28 23:56:22 -050046 if (fmap_locate_area_as_rdev("RO_VPD", &vpd)) {
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +080047 printk(BIOS_ERR, "%s: No RO_VPD FMAP section.\n", __func__);
48 return result;
49 }
Aaron Durbin0424c952015-03-28 23:56:22 -050050
51 base = 0;
52 cached_size = region_device_sz(&vpd);
53
54 if ((cached_size < GOOGLE_VPD_2_0_OFFSET + sizeof(info)) ||
55 rdev_chain(&vpd, &vpd, GOOGLE_VPD_2_0_OFFSET,
56 cached_size - GOOGLE_VPD_2_0_OFFSET)) {
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +080057 printk(BIOS_ERR, "%s: Too small (%d) for Google VPD 2.0.\n",
Aaron Durbin0424c952015-03-28 23:56:22 -050058 __func__, cached_size);
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +080059 return result;
60 }
61
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +080062 /* Try if we can find a google_vpd_info, otherwise read whole VPD. */
Aaron Durbin0424c952015-03-28 23:56:22 -050063 if (rdev_readat(&vpd, &info, base, sizeof(info)) == sizeof(info) &&
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +080064 memcmp(info.header.magic, VPD_INFO_MAGIC, sizeof(info.header.magic))
65 == 0 && cached_size >= info.size + sizeof(info)) {
66 base += sizeof(info);
67 cached_size = info.size;
68 }
69
Aaron Durbin0424c952015-03-28 23:56:22 -050070 cached_address = rdev_mmap(&vpd, base, cached_size);
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +080071 if (cached_address) {
72 *vpd_address = cached_address;
73 *vpd_size = cached_size;
74 printk(BIOS_DEBUG, "%s: Got VPD: %#x+%#x\n", __func__, base,
75 cached_size);
76 result = 0;
77 }
78 return result;
79}
80
81static int vpd_gets_callback(const uint8_t *key, int32_t key_len,
82 const uint8_t *value, int32_t value_len,
83 void *arg)
84{
85 struct vpd_gets_arg *result = (struct vpd_gets_arg *)arg;
86 if (key_len != result->key_len ||
87 memcmp(key, result->key, key_len) != 0)
88 /* Returns VPD_OK to continue parsing. */
89 return VPD_OK;
90
91 result->matched = 1;
92 result->value = value;
93 result->value_len = value_len;
94 /* Returns VPD_FAIL to stop parsing. */
95 return VPD_FAIL;
96}
97
Vadim Bendebury127c3392014-10-22 17:39:24 -070098const void *cros_vpd_find(const char *key, int *size)
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +080099{
100 uint8_t *vpd_address = NULL;
101 int32_t vpd_size = 0;
102 struct vpd_gets_arg arg = {0};
103 int consumed = 0;
104
105 if (cros_vpd_load(&vpd_address, &vpd_size) != 0) {
106 return NULL;
107 }
108
109 arg.key = (const uint8_t *)key;
110 arg.key_len = strlen(key);
111
112 while (VPD_OK == decodeVpdString(vpd_size, vpd_address, &consumed,
113 vpd_gets_callback, &arg)) {
114 /* Iterate until found or no more entries. */
115 }
116
117 if (!arg.matched)
118 return NULL;
119
Vadim Bendebury127c3392014-10-22 17:39:24 -0700120 *size = arg.value_len;
121 return arg.value;
122}
123
124char *cros_vpd_gets(const char *key, char *buffer, int size)
125{
126 const void *string_address;
127 int string_size;
128
129 string_address = cros_vpd_find(key, &string_size);
130
131 if (!string_address)
132 return NULL;
133
134 if (size > (string_size + 1)) {
135 strcpy(buffer, string_address);
136 } else {
137 memcpy(buffer, string_address, size - 1);
138 buffer[size - 1] = '\0';
139 }
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +0800140 return buffer;
141}
142