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 | |
| 9 | #include <cbfs.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <string.h> |
| 12 | |
| 13 | #include "cros_vpd.h" |
| 14 | #include "fmap.h" |
| 15 | #include "lib_vpd.h" |
| 16 | #include "vpd_tables.h" |
| 17 | |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 18 | /* Currently we only support Google VPD 2.0, which has a fixed offset. */ |
| 19 | enum { |
| 20 | GOOGLE_VPD_2_0_OFFSET = 0x600, |
| 21 | }; |
| 22 | |
| 23 | struct vpd_gets_arg { |
| 24 | const uint8_t *key; |
| 25 | const uint8_t *value; |
| 26 | int32_t key_len, value_len; |
| 27 | int matched; |
| 28 | }; |
| 29 | |
| 30 | static int cros_vpd_load(uint8_t **vpd_address, int32_t *vpd_size) |
| 31 | { |
Julius Werner | dbe0df1 | 2014-06-06 16:10:56 -0700 | [diff] [blame] | 32 | MAYBE_STATIC int cached = 0; |
| 33 | MAYBE_STATIC uint8_t *cached_address = NULL; |
| 34 | MAYBE_STATIC int32_t cached_size = 0; |
| 35 | MAYBE_STATIC int result = -1; |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 36 | struct google_vpd_info info; |
| 37 | int32_t base; |
| 38 | |
| 39 | const struct fmap_area *area; |
| 40 | struct cbfs_media media; |
| 41 | |
| 42 | if (cached) { |
| 43 | *vpd_address = cached_address; |
| 44 | *vpd_size = cached_size; |
| 45 | return result; |
| 46 | } |
| 47 | |
| 48 | cached = 1; |
| 49 | area = find_fmap_area(fmap_find(), "RO_VPD"); |
| 50 | if (!area) { |
| 51 | printk(BIOS_ERR, "%s: No RO_VPD FMAP section.\n", __func__); |
| 52 | return result; |
| 53 | } |
| 54 | if (area->size <= GOOGLE_VPD_2_0_OFFSET + sizeof(info)) { |
| 55 | printk(BIOS_ERR, "%s: Too small (%d) for Google VPD 2.0.\n", |
| 56 | __func__, area->size); |
| 57 | return result; |
| 58 | } |
| 59 | |
| 60 | base = area->offset + GOOGLE_VPD_2_0_OFFSET; |
| 61 | cached_size = area->size - GOOGLE_VPD_2_0_OFFSET; |
| 62 | init_default_cbfs_media(&media); |
| 63 | media.open(&media); |
| 64 | |
| 65 | /* Try if we can find a google_vpd_info, otherwise read whole VPD. */ |
| 66 | if (media.read(&media, &info, base, sizeof(info)) == sizeof(info) && |
| 67 | memcmp(info.header.magic, VPD_INFO_MAGIC, sizeof(info.header.magic)) |
| 68 | == 0 && cached_size >= info.size + sizeof(info)) { |
| 69 | base += sizeof(info); |
| 70 | cached_size = info.size; |
| 71 | } |
| 72 | |
| 73 | cached_address = media.map(&media, base, cached_size); |
| 74 | media.close(&media); |
| 75 | if (cached_address) { |
| 76 | *vpd_address = cached_address; |
| 77 | *vpd_size = cached_size; |
| 78 | printk(BIOS_DEBUG, "%s: Got VPD: %#x+%#x\n", __func__, base, |
| 79 | cached_size); |
| 80 | result = 0; |
| 81 | } |
| 82 | return result; |
| 83 | } |
| 84 | |
| 85 | static int vpd_gets_callback(const uint8_t *key, int32_t key_len, |
| 86 | const uint8_t *value, int32_t value_len, |
| 87 | void *arg) |
| 88 | { |
| 89 | struct vpd_gets_arg *result = (struct vpd_gets_arg *)arg; |
| 90 | if (key_len != result->key_len || |
| 91 | memcmp(key, result->key, key_len) != 0) |
| 92 | /* Returns VPD_OK to continue parsing. */ |
| 93 | return VPD_OK; |
| 94 | |
| 95 | result->matched = 1; |
| 96 | result->value = value; |
| 97 | result->value_len = value_len; |
| 98 | /* Returns VPD_FAIL to stop parsing. */ |
| 99 | return VPD_FAIL; |
| 100 | } |
| 101 | |
Vadim Bendebury | 127c339 | 2014-10-22 17:39:24 -0700 | [diff] [blame^] | 102 | const void *cros_vpd_find(const char *key, int *size) |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 103 | { |
| 104 | uint8_t *vpd_address = NULL; |
| 105 | int32_t vpd_size = 0; |
| 106 | struct vpd_gets_arg arg = {0}; |
| 107 | int consumed = 0; |
| 108 | |
| 109 | if (cros_vpd_load(&vpd_address, &vpd_size) != 0) { |
| 110 | return NULL; |
| 111 | } |
| 112 | |
| 113 | arg.key = (const uint8_t *)key; |
| 114 | arg.key_len = strlen(key); |
| 115 | |
| 116 | while (VPD_OK == decodeVpdString(vpd_size, vpd_address, &consumed, |
| 117 | vpd_gets_callback, &arg)) { |
| 118 | /* Iterate until found or no more entries. */ |
| 119 | } |
| 120 | |
| 121 | if (!arg.matched) |
| 122 | return NULL; |
| 123 | |
Vadim Bendebury | 127c339 | 2014-10-22 17:39:24 -0700 | [diff] [blame^] | 124 | *size = arg.value_len; |
| 125 | return arg.value; |
| 126 | } |
| 127 | |
| 128 | char *cros_vpd_gets(const char *key, char *buffer, int size) |
| 129 | { |
| 130 | const void *string_address; |
| 131 | int string_size; |
| 132 | |
| 133 | string_address = cros_vpd_find(key, &string_size); |
| 134 | |
| 135 | if (!string_address) |
| 136 | return NULL; |
| 137 | |
| 138 | if (size > (string_size + 1)) { |
| 139 | strcpy(buffer, string_address); |
| 140 | } else { |
| 141 | memcpy(buffer, string_address, size - 1); |
| 142 | buffer[size - 1] = '\0'; |
| 143 | } |
Hung-Te Lin | 6eaaafa | 2014-02-21 16:21:00 +0800 | [diff] [blame] | 144 | return buffer; |
| 145 | } |
| 146 | |