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 | |
| 7 | #include <console/console.h> |
| 8 | |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame^] | 9 | #include <fmap.h> |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 10 | #include <stdlib.h> |
| 11 | #include <string.h> |
| 12 | |
| 13 | #include "cros_vpd.h" |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 14 | #include "lib_vpd.h" |
| 15 | #include "vpd_tables.h" |
| 16 | |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 17 | /* Currently we only support Google VPD 2.0, which has a fixed offset. */ |
| 18 | enum { |
| 19 | GOOGLE_VPD_2_0_OFFSET = 0x600, |
| 20 | }; |
| 21 | |
| 22 | struct 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 | |
| 29 | static int cros_vpd_load(uint8_t **vpd_address, int32_t *vpd_size) |
| 30 | { |
Julius Werner | dbe0df1 | 2014-06-06 16:10:56 -0700 | [diff] [blame] | 31 | 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 Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 35 | struct google_vpd_info info; |
| 36 | int32_t base; |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame^] | 37 | struct region_device vpd; |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 38 | |
| 39 | if (cached) { |
| 40 | *vpd_address = cached_address; |
| 41 | *vpd_size = cached_size; |
| 42 | return result; |
| 43 | } |
| 44 | |
| 45 | cached = 1; |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame^] | 46 | if (fmap_locate_area_as_rdev("RO_VPD", &vpd)) { |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 47 | printk(BIOS_ERR, "%s: No RO_VPD FMAP section.\n", __func__); |
| 48 | return result; |
| 49 | } |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame^] | 50 | |
| 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 Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 57 | printk(BIOS_ERR, "%s: Too small (%d) for Google VPD 2.0.\n", |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame^] | 58 | __func__, cached_size); |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 59 | return result; |
| 60 | } |
| 61 | |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 62 | /* Try if we can find a google_vpd_info, otherwise read whole VPD. */ |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame^] | 63 | if (rdev_readat(&vpd, &info, base, sizeof(info)) == sizeof(info) && |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 64 | 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 Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame^] | 70 | cached_address = rdev_mmap(&vpd, base, cached_size); |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 71 | 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 | |
| 81 | static 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 Bendebury | 127c339 | 2014-10-22 17:39:24 -0700 | [diff] [blame] | 98 | const void *cros_vpd_find(const char *key, int *size) |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 99 | { |
| 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 Bendebury | 127c339 | 2014-10-22 17:39:24 -0700 | [diff] [blame] | 120 | *size = arg.value_len; |
| 121 | return arg.value; |
| 122 | } |
| 123 | |
| 124 | char *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 Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 140 | return buffer; |
| 141 | } |
| 142 | |