Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Firmware Interface Table support. |
| 3 | * |
| 4 | * Copyright (C) 2012 Google Inc. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include <stdint.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <stdio.h> |
| 24 | |
| 25 | #include "common.h" |
Ronald G. Minnich | a8a133d | 2013-12-30 13:16:18 -0800 | [diff] [blame^] | 26 | #include "elf.h" |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 27 | #include "cbfs.h" |
| 28 | #include "cbfs_image.h" |
| 29 | #include "fit.h" |
| 30 | |
| 31 | /* FIXME: This code assumes it is being executed on a little endian machine. */ |
| 32 | |
| 33 | #define FIT_POINTER_LOCATION 0xffffffc0 |
| 34 | #define FIT_TABLE_LOWEST_ADDRESS ((uint32_t)(-(16 << 20))) |
| 35 | #define FIT_ENTRY_CHECKSUM_VALID 0x80 |
| 36 | #define FIT_TYPE_HEADER 0x0 |
| 37 | #define FIT_HEADER_VERSION 0x0100 |
| 38 | #define FIT_HEADER_ADDRESS "_FIT_ " |
| 39 | #define FIT_TYPE_MICROCODE 0x1 |
| 40 | #define FIT_MICROCODE_VERSION 0x0100 |
| 41 | |
| 42 | struct fit_entry { |
| 43 | uint64_t address; |
| 44 | uint32_t size_reserved; |
| 45 | uint16_t version; |
| 46 | uint8_t type_checksum_valid; |
| 47 | uint8_t checksum; |
| 48 | } __attribute__ ((packed)); |
| 49 | |
| 50 | struct fit_table { |
| 51 | struct fit_entry header; |
| 52 | struct fit_entry entries[0]; |
| 53 | } __attribute__ ((packed)); |
| 54 | |
| 55 | struct microcode_header { |
| 56 | uint32_t version; |
| 57 | uint32_t revision; |
| 58 | uint32_t date; |
| 59 | uint32_t processor_signature; |
| 60 | uint32_t checksum; |
| 61 | uint32_t loader_revision; |
| 62 | uint32_t processor_flags; |
| 63 | uint32_t data_size; |
| 64 | uint32_t total_size; |
| 65 | uint8_t reserved[12]; |
| 66 | } __attribute__ ((packed)); |
| 67 | |
| 68 | struct microcode_entry { |
| 69 | int offset; |
| 70 | int size; |
| 71 | }; |
| 72 | |
| 73 | static inline void *rom_buffer_pointer(struct cbfs_image *image, int offset) |
| 74 | { |
| 75 | return &image->buffer.data[offset]; |
| 76 | } |
| 77 | |
| 78 | static inline int fit_entry_size_bytes(struct fit_entry *entry) |
| 79 | { |
| 80 | return (entry->size_reserved & 0xffffff) << 4; |
| 81 | } |
| 82 | |
| 83 | static inline void fit_entry_update_size(struct fit_entry *entry, |
| 84 | int size_bytes) |
| 85 | { |
| 86 | /* Size is multiples of 16 bytes. */ |
| 87 | entry->size_reserved = (size_bytes >> 4) & 0xffffff; |
| 88 | } |
| 89 | |
| 90 | static inline void fit_entry_add_size(struct fit_entry *entry, |
| 91 | int size_bytes) |
| 92 | { |
| 93 | int size = fit_entry_size_bytes(entry); |
| 94 | size += size_bytes; |
| 95 | fit_entry_update_size(entry, size); |
| 96 | } |
| 97 | |
| 98 | static inline int fit_entry_type(struct fit_entry *entry) |
| 99 | { |
| 100 | return entry->type_checksum_valid & ~FIT_ENTRY_CHECKSUM_VALID; |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | * Get an offset from a host pointer. This function assumes the ROM is located |
| 105 | * in the host address space at [4G - romsize -> 4G). It also assume all |
| 106 | * pointers have values within this address range. |
| 107 | */ |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 108 | static inline int ptr_to_offset(uint32_t theromsize, uint32_t host_ptr) |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 109 | { |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 110 | return (int)(theromsize + host_ptr); |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /* |
| 114 | * Get a pointer from an offset. This function assumes the ROM is located |
| 115 | * in the host address space at [4G - romsize -> 4G). It also assume all |
| 116 | * pointers have values within this address range. |
| 117 | */ |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 118 | static inline uint32_t offset_to_ptr(uint32_t theromsize, int offset) |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 119 | { |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 120 | return -(theromsize - (uint32_t )offset); |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static struct fit_table *locate_fit_table(struct cbfs_image *image) |
| 124 | { |
| 125 | struct fit_table *table; |
| 126 | uint32_t *fit_pointer; |
| 127 | |
| 128 | fit_pointer = rom_buffer_pointer(image, |
| 129 | ptr_to_offset(image->buffer.size, FIT_POINTER_LOCATION)); |
| 130 | |
| 131 | /* Ensure pointer is below 4GiB and within 16MiB of 4GiB */ |
| 132 | if (fit_pointer[1] != 0 || fit_pointer[0] < FIT_TABLE_LOWEST_ADDRESS) |
| 133 | return NULL; |
| 134 | |
| 135 | table = rom_buffer_pointer(image, |
| 136 | ptr_to_offset(image->buffer.size, *fit_pointer)); |
| 137 | |
| 138 | /* Check that the address field has the proper signature. */ |
| 139 | if (strncmp((const char *)&table->header.address, FIT_HEADER_ADDRESS, |
| 140 | sizeof(table->header.address))) |
| 141 | return NULL; |
| 142 | |
| 143 | if (table->header.version != FIT_HEADER_VERSION) |
| 144 | return NULL; |
| 145 | |
| 146 | if (fit_entry_type(&table->header) != FIT_TYPE_HEADER) |
| 147 | return NULL; |
| 148 | |
| 149 | /* Assume that the FIT table only contains the header */ |
| 150 | if (fit_entry_size_bytes(&table->header) != sizeof(struct fit_entry)) |
| 151 | return NULL; |
| 152 | |
| 153 | return table; |
| 154 | } |
| 155 | |
| 156 | static void update_fit_checksum(struct fit_table *fit) |
| 157 | { |
| 158 | int size_bytes; |
| 159 | uint8_t *buffer; |
| 160 | uint8_t result; |
| 161 | int i; |
| 162 | |
| 163 | fit->header.checksum = 0; |
| 164 | size_bytes = fit_entry_size_bytes(&fit->header); |
| 165 | result = 0; |
| 166 | buffer = (void *)fit; |
| 167 | for (i = 0; i < size_bytes; i++) |
| 168 | result += buffer[i]; |
| 169 | fit->header.checksum = -result; |
| 170 | } |
| 171 | |
| 172 | static void add_microcodde_entries(struct cbfs_image *image, |
| 173 | struct fit_table *fit, |
| 174 | struct microcode_entry *mcus, int num_mcus) |
| 175 | { |
| 176 | int i; |
| 177 | |
| 178 | for (i = 0; i < num_mcus; i++) { |
| 179 | struct fit_entry *entry = &fit->entries[i]; |
| 180 | struct microcode_entry *mcu = &mcus[i]; |
| 181 | |
| 182 | entry->address = offset_to_ptr(image->buffer.size, mcu->offset); |
| 183 | fit_entry_update_size(entry, mcu->size); |
| 184 | entry->version = FIT_MICROCODE_VERSION; |
| 185 | entry->type_checksum_valid = FIT_TYPE_MICROCODE; |
| 186 | entry->checksum = 0; |
| 187 | fit_entry_add_size(&fit->header, sizeof(struct fit_entry)); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | static int parse_microcode_blob(struct cbfs_image *image, |
| 192 | struct cbfs_file *mcode_file, |
| 193 | struct microcode_entry *mcus, int *total_mcus) |
| 194 | { |
| 195 | int num_mcus; |
| 196 | int current_offset; |
| 197 | int file_length; |
| 198 | |
| 199 | current_offset = (int)((char *)mcode_file - image->buffer.data); |
| 200 | current_offset += ntohl(mcode_file->offset); |
| 201 | file_length = ntohl(mcode_file->len); |
| 202 | |
| 203 | num_mcus = 0; |
| 204 | while (file_length > sizeof(struct microcode_header)) |
| 205 | { |
| 206 | struct microcode_header *mcu_header; |
| 207 | |
| 208 | mcu_header = rom_buffer_pointer(image, current_offset); |
| 209 | |
Aaron Durbin | 8a0cb8d | 2013-05-07 11:14:01 -0500 | [diff] [blame] | 210 | /* Quickly sanity check a prospective microcode update. */ |
| 211 | if (mcu_header->total_size < sizeof(*mcu_header)) |
| 212 | break; |
| 213 | |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 214 | /* FIXME: Should the checksum be validated? */ |
| 215 | mcus[num_mcus].offset = current_offset; |
| 216 | mcus[num_mcus].size = mcu_header->total_size; |
| 217 | |
| 218 | /* Proceed to next payload. */ |
| 219 | current_offset += mcus[num_mcus].size; |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 220 | file_length -= mcus[num_mcus].size; |
Alexandru Gagniuc | f87c20a | 2013-12-08 17:46:40 -0600 | [diff] [blame] | 221 | num_mcus++; |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 222 | |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 223 | /* Reached limit of FIT entries. */ |
| 224 | if (num_mcus == *total_mcus) |
| 225 | break; |
| 226 | if (file_length < sizeof(struct microcode_header)) |
| 227 | break; |
| 228 | } |
| 229 | |
| 230 | /* Update how many microcode updates we found. */ |
| 231 | *total_mcus = num_mcus; |
| 232 | |
| 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | int fit_update_table(struct cbfs_image *image, int empty_entries, |
| 237 | const char *microcode_blob_name) |
| 238 | { |
| 239 | struct fit_table *fit; |
| 240 | struct cbfs_file *mcode_file; |
| 241 | struct microcode_entry *mcus; |
| 242 | int ret = 0; |
| 243 | // struct rom_image image = { .rom = rom, .size = romsize, }; |
| 244 | |
| 245 | fit = locate_fit_table(image); |
| 246 | |
| 247 | if (!fit) { |
| 248 | ERROR("FIT not found.\n"); |
| 249 | return 1; |
| 250 | } |
| 251 | |
| 252 | mcode_file = cbfs_get_entry(image, microcode_blob_name); |
| 253 | if (!mcode_file) { |
| 254 | ERROR("File '%s' not found in CBFS.\n", |
| 255 | microcode_blob_name); |
| 256 | return 1; |
| 257 | } |
| 258 | |
| 259 | mcus = malloc(sizeof(*mcus) * empty_entries); |
| 260 | |
| 261 | if (!mcus) { |
| 262 | ERROR("Couldn't allocate memory for microcode update entries.\n"); |
| 263 | return 1; |
| 264 | } |
| 265 | |
| 266 | if (parse_microcode_blob(image, mcode_file, mcus, &empty_entries)) { |
| 267 | ERROR("Couldn't parse microcode blob.\n"); |
| 268 | ret = 1; |
| 269 | goto out; |
| 270 | } |
| 271 | |
| 272 | add_microcodde_entries(image, fit, mcus, empty_entries); |
| 273 | update_fit_checksum(fit); |
| 274 | |
| 275 | out: |
| 276 | free(mcus); |
| 277 | return ret; |
| 278 | } |