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 | |
Jonathan Zhang | b5392f9 | 2019-07-16 14:37:23 -0700 | [diff] [blame^] | 7 | #include <arch/early_variables.h> |
| 8 | #include <assert.h> |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 9 | #include <console/console.h> |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 10 | #include <cbmem.h> |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 11 | #include <fmap.h> |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 12 | #include <stdlib.h> |
| 13 | #include <string.h> |
Julius Werner | 4f7a361 | 2016-01-20 18:01:15 -0800 | [diff] [blame] | 14 | #include <timestamp.h> |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 15 | |
Patrick Rudolph | 28cee59 | 2018-03-08 15:43:12 +0100 | [diff] [blame] | 16 | #include "vpd.h" |
Hung-Te Lin | c345570 | 2019-05-27 11:02:00 +0800 | [diff] [blame] | 17 | #include "vpd_decode.h" |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 18 | #include "vpd_tables.h" |
| 19 | |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 20 | struct vpd_gets_arg { |
| 21 | const uint8_t *key; |
| 22 | const uint8_t *value; |
| 23 | int32_t key_len, value_len; |
| 24 | int matched; |
| 25 | }; |
| 26 | |
Jonathan Zhang | b5392f9 | 2019-07-16 14:37:23 -0700 | [diff] [blame^] | 27 | struct vpd_blob g_vpd_blob CAR_GLOBAL = {0}; |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 28 | |
Jonathan Zhang | b5392f9 | 2019-07-16 14:37:23 -0700 | [diff] [blame^] | 29 | /* |
| 30 | * returns the size of data in a VPD 2.0 formatted fmap region, or 0. |
| 31 | * Also sets *base as the region's base address. |
| 32 | */ |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 33 | 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] | 34 | { |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 35 | struct google_vpd_info info; |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 36 | struct region_device vpd; |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 37 | int32_t size; |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 38 | |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 39 | if (fmap_locate_area_as_rdev(fmap_name, &vpd)) { |
| 40 | printk(BIOS_ERR, "%s: No %s FMAP section.\n", __func__, |
| 41 | fmap_name); |
| 42 | return 0; |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 43 | } |
| 44 | |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 45 | size = region_device_sz(&vpd); |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 46 | |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 47 | if ((size < GOOGLE_VPD_2_0_OFFSET + sizeof(info)) || |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 48 | rdev_chain(&vpd, &vpd, GOOGLE_VPD_2_0_OFFSET, |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 49 | size - GOOGLE_VPD_2_0_OFFSET)) { |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 50 | 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] | 51 | __func__, size); |
| 52 | return 0; |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 53 | } |
| 54 | |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 55 | /* Try if we can find a google_vpd_info, otherwise read whole VPD. */ |
Julius Werner | ffc2260 | 2016-01-21 11:12:38 -0800 | [diff] [blame] | 56 | if (rdev_readat(&vpd, &info, *base, sizeof(info)) != sizeof(info)) { |
| 57 | printk(BIOS_ERR, "ERROR: Failed to read %s header.\n", |
| 58 | fmap_name); |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | if (memcmp(info.header.magic, VPD_INFO_MAGIC, sizeof(info.header.magic)) |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 63 | == 0 && size >= info.size + sizeof(info)) { |
| 64 | *base += sizeof(info); |
| 65 | size = info.size; |
Julius Werner | ffc2260 | 2016-01-21 11:12:38 -0800 | [diff] [blame] | 66 | } else if (info.header.tlv.type == VPD_TYPE_TERMINATOR || |
| 67 | info.header.tlv.type == VPD_TYPE_IMPLICIT_TERMINATOR) { |
| 68 | printk(BIOS_WARNING, "WARNING: %s is uninitialized or empty.\n", |
| 69 | fmap_name); |
| 70 | size = 0; |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 71 | } else { |
| 72 | size -= GOOGLE_VPD_2_0_OFFSET; |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 73 | } |
| 74 | |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 75 | return size; |
| 76 | } |
| 77 | |
Jonathan Zhang | b5392f9 | 2019-07-16 14:37:23 -0700 | [diff] [blame^] | 78 | static void vpd_get_blob(void) |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 79 | { |
Jonathan Zhang | b5392f9 | 2019-07-16 14:37:23 -0700 | [diff] [blame^] | 80 | int32_t ro_vpd_base = 0; |
| 81 | int32_t rw_vpd_base = 0; |
| 82 | int32_t ro_vpd_size = get_vpd_size("RO_VPD", &ro_vpd_base); |
| 83 | int32_t rw_vpd_size = get_vpd_size("RW_VPD", &rw_vpd_base); |
| 84 | |
| 85 | /* Return if no VPD at all */ |
| 86 | if (ro_vpd_size == 0 && rw_vpd_size == 0) |
| 87 | return; |
| 88 | |
| 89 | struct vpd_blob *blob = car_get_var_ptr(&g_vpd_blob); |
| 90 | if (!blob) |
| 91 | return; |
| 92 | blob->ro_base = NULL; |
| 93 | blob->ro_size = 0; |
| 94 | blob->rw_base = NULL; |
| 95 | blob->rw_size = 0; |
| 96 | |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 97 | struct region_device vpd; |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 98 | |
| 99 | if (ro_vpd_size) { |
| 100 | if (fmap_locate_area_as_rdev("RO_VPD", &vpd)) { |
| 101 | /* shouldn't happen, but let's be extra defensive */ |
| 102 | printk(BIOS_ERR, "%s: No RO_VPD FMAP section.\n", |
| 103 | __func__); |
| 104 | return; |
| 105 | } |
| 106 | rdev_chain(&vpd, &vpd, GOOGLE_VPD_2_0_OFFSET, |
| 107 | region_device_sz(&vpd) - GOOGLE_VPD_2_0_OFFSET); |
Jonathan Zhang | b5392f9 | 2019-07-16 14:37:23 -0700 | [diff] [blame^] | 108 | blob->ro_base = (uint8_t *)(rdev_mmap_full(&vpd) + |
| 109 | sizeof(struct google_vpd_info)); |
| 110 | blob->ro_size = ro_vpd_size; |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 111 | } |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 112 | if (rw_vpd_size) { |
| 113 | if (fmap_locate_area_as_rdev("RW_VPD", &vpd)) { |
| 114 | /* shouldn't happen, but let's be extra defensive */ |
| 115 | printk(BIOS_ERR, "%s: No RW_VPD FMAP section.\n", |
| 116 | __func__); |
| 117 | return; |
| 118 | } |
| 119 | rdev_chain(&vpd, &vpd, GOOGLE_VPD_2_0_OFFSET, |
| 120 | region_device_sz(&vpd) - GOOGLE_VPD_2_0_OFFSET); |
Jonathan Zhang | b5392f9 | 2019-07-16 14:37:23 -0700 | [diff] [blame^] | 121 | blob->rw_base = (uint8_t *)(rdev_mmap_full(&vpd) + |
| 122 | sizeof(struct google_vpd_info)); |
| 123 | blob->rw_size = rw_vpd_size; |
Hung-Te Lin | b15a0d0 | 2015-07-13 15:49:57 +0800 | [diff] [blame] | 124 | } |
Jonathan Zhang | b5392f9 | 2019-07-16 14:37:23 -0700 | [diff] [blame^] | 125 | blob->initialized = true; |
| 126 | } |
| 127 | |
| 128 | const struct vpd_blob *vpd_load_blob(void) |
| 129 | { |
| 130 | struct vpd_blob *blob = NULL; |
| 131 | |
| 132 | blob = car_get_var_ptr(&g_vpd_blob); |
| 133 | |
| 134 | if (blob && blob->initialized == false) |
| 135 | vpd_get_blob(); |
| 136 | |
| 137 | return blob; |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 138 | } |
| 139 | |
Hung-Te Lin | c345570 | 2019-05-27 11:02:00 +0800 | [diff] [blame] | 140 | static int vpd_gets_callback(const uint8_t *key, uint32_t key_len, |
| 141 | const uint8_t *value, uint32_t value_len, |
| 142 | void *arg) |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 143 | { |
| 144 | struct vpd_gets_arg *result = (struct vpd_gets_arg *)arg; |
| 145 | if (key_len != result->key_len || |
| 146 | memcmp(key, result->key, key_len) != 0) |
Hung-Te Lin | c345570 | 2019-05-27 11:02:00 +0800 | [diff] [blame] | 147 | /* Returns VPD_DECODE_OK to continue parsing. */ |
| 148 | return VPD_DECODE_OK; |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 149 | |
| 150 | result->matched = 1; |
| 151 | result->value = value; |
| 152 | result->value_len = value_len; |
Hung-Te Lin | c345570 | 2019-05-27 11:02:00 +0800 | [diff] [blame] | 153 | /* Returns VPD_DECODE_FAIL to stop parsing. */ |
| 154 | return VPD_DECODE_FAIL; |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 155 | } |
| 156 | |
Patrick Rudolph | 28cee59 | 2018-03-08 15:43:12 +0100 | [diff] [blame] | 157 | const void *vpd_find(const char *key, int *size, enum vpd_region region) |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 158 | { |
Jonathan Zhang | b5392f9 | 2019-07-16 14:37:23 -0700 | [diff] [blame^] | 159 | struct vpd_blob blob = {0}; |
| 160 | |
| 161 | vpd_get_buffers(&blob); |
| 162 | if (blob.ro_size == 0 && blob.rw_size == 0) |
| 163 | return NULL; |
| 164 | |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 165 | struct vpd_gets_arg arg = {0}; |
Hung-Te Lin | c345570 | 2019-05-27 11:02:00 +0800 | [diff] [blame] | 166 | uint32_t consumed = 0; |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 167 | |
| 168 | arg.key = (const uint8_t *)key; |
| 169 | arg.key_len = strlen(key); |
| 170 | |
Jonathan Zhang | b5392f9 | 2019-07-16 14:37:23 -0700 | [diff] [blame^] | 171 | if ((region == VPD_ANY || region == VPD_RO) && blob.ro_size != 0) { |
| 172 | while (vpd_decode_string(blob.ro_size, blob.ro_base, |
| 173 | &consumed, vpd_gets_callback, &arg) == VPD_DECODE_OK) { |
| 174 | /* Iterate until found or no more entries. */ |
Patrick Rudolph | 28cee59 | 2018-03-08 15:43:12 +0100 | [diff] [blame] | 175 | } |
Hung-Te Lin | c345570 | 2019-05-27 11:02:00 +0800 | [diff] [blame] | 176 | } |
Jonathan Zhang | b5392f9 | 2019-07-16 14:37:23 -0700 | [diff] [blame^] | 177 | |
| 178 | if ((!arg.matched && region != VPD_RO) && blob.rw_size != 0) { |
| 179 | while (vpd_decode_string(blob.rw_size, blob.rw_base, |
| 180 | &consumed, vpd_gets_callback, &arg) == VPD_DECODE_OK) { |
| 181 | /* Iterate until found or no more entries. */ |
Patrick Rudolph | 28cee59 | 2018-03-08 15:43:12 +0100 | [diff] [blame] | 182 | } |
Hung-Te Lin | c345570 | 2019-05-27 11:02:00 +0800 | [diff] [blame] | 183 | } |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 184 | |
| 185 | if (!arg.matched) |
| 186 | return NULL; |
| 187 | |
Vadim Bendebury | 127c339 | 2014-10-22 17:39:24 -0700 | [diff] [blame] | 188 | *size = arg.value_len; |
| 189 | return arg.value; |
| 190 | } |
| 191 | |
Patrick Rudolph | 28cee59 | 2018-03-08 15:43:12 +0100 | [diff] [blame] | 192 | char *vpd_gets(const char *key, char *buffer, int size, enum vpd_region region) |
Vadim Bendebury | 127c339 | 2014-10-22 17:39:24 -0700 | [diff] [blame] | 193 | { |
| 194 | const void *string_address; |
| 195 | int string_size; |
| 196 | |
Patrick Rudolph | 28cee59 | 2018-03-08 15:43:12 +0100 | [diff] [blame] | 197 | string_address = vpd_find(key, &string_size, region); |
Vadim Bendebury | 127c339 | 2014-10-22 17:39:24 -0700 | [diff] [blame] | 198 | |
| 199 | if (!string_address) |
| 200 | return NULL; |
| 201 | |
Jonathan Zhang | b5392f9 | 2019-07-16 14:37:23 -0700 | [diff] [blame^] | 202 | assert(size > 0); |
| 203 | int copy_size = MIN(size - 1, string_size); |
| 204 | memcpy(buffer, string_address, copy_size); |
| 205 | buffer[copy_size] = '\0'; |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 206 | return buffer; |
| 207 | } |
| 208 | |
Jonathan Zhang | b5392f9 | 2019-07-16 14:37:23 -0700 | [diff] [blame^] | 209 | /* |
| 210 | * Find value of boolean type vpd key. |
| 211 | * |
| 212 | * During the process, necessary checking is done, such as making |
| 213 | * sure the value length is 1, and value is either '1' or '0'. |
| 214 | */ |
| 215 | bool vpd_get_bool(const char *key, enum vpd_region region, uint8_t *val) |
| 216 | { |
| 217 | int size; |
| 218 | const char *value; |
| 219 | |
| 220 | value = vpd_find(key, &size, region); |
| 221 | if (!value) { |
| 222 | printk(BIOS_CRIT, "problem returning from vpd_find.\n"); |
| 223 | return false; |
| 224 | } |
| 225 | |
| 226 | if (size != 1) |
| 227 | return false; |
| 228 | |
| 229 | /* Make sure the value is either '1' or '0' */ |
| 230 | if (*value == '1') { |
| 231 | *val = 1; |
| 232 | return true; |
| 233 | } else if (*value == '0') { |
| 234 | *val = 0; |
| 235 | return true; |
| 236 | } else |
| 237 | return false; |
| 238 | } |