blob: 7dbfc51bc07860e14945201dbd9f7034efd40a51 [file] [log] [blame]
Aaron Durbin6b0d0d62012-12-14 17:16:21 -06001/*
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"
26#include "cbfs.h"
27#include "cbfs_image.h"
28#include "fit.h"
29
30/* FIXME: This code assumes it is being executed on a little endian machine. */
31
32#define FIT_POINTER_LOCATION 0xffffffc0
33#define FIT_TABLE_LOWEST_ADDRESS ((uint32_t)(-(16 << 20)))
34#define FIT_ENTRY_CHECKSUM_VALID 0x80
35#define FIT_TYPE_HEADER 0x0
36#define FIT_HEADER_VERSION 0x0100
37#define FIT_HEADER_ADDRESS "_FIT_ "
38#define FIT_TYPE_MICROCODE 0x1
39#define FIT_MICROCODE_VERSION 0x0100
40
41struct fit_entry {
42 uint64_t address;
43 uint32_t size_reserved;
44 uint16_t version;
45 uint8_t type_checksum_valid;
46 uint8_t checksum;
47} __attribute__ ((packed));
48
49struct fit_table {
50 struct fit_entry header;
Sol Boucher0e539312015-03-05 15:38:03 -080051 struct fit_entry entries[];
Aaron Durbin6b0d0d62012-12-14 17:16:21 -060052} __attribute__ ((packed));
53
54struct microcode_header {
55 uint32_t version;
56 uint32_t revision;
57 uint32_t date;
58 uint32_t processor_signature;
59 uint32_t checksum;
60 uint32_t loader_revision;
61 uint32_t processor_flags;
62 uint32_t data_size;
63 uint32_t total_size;
64 uint8_t reserved[12];
65} __attribute__ ((packed));
66
67struct microcode_entry {
68 int offset;
69 int size;
70};
71
72static inline void *rom_buffer_pointer(struct cbfs_image *image, int offset)
73{
74 return &image->buffer.data[offset];
75}
76
77static inline int fit_entry_size_bytes(struct fit_entry *entry)
78{
79 return (entry->size_reserved & 0xffffff) << 4;
80}
81
82static inline void fit_entry_update_size(struct fit_entry *entry,
83 int size_bytes)
84{
85 /* Size is multiples of 16 bytes. */
86 entry->size_reserved = (size_bytes >> 4) & 0xffffff;
87}
88
89static inline void fit_entry_add_size(struct fit_entry *entry,
90 int size_bytes)
91{
92 int size = fit_entry_size_bytes(entry);
93 size += size_bytes;
94 fit_entry_update_size(entry, size);
95}
96
97static inline int fit_entry_type(struct fit_entry *entry)
98{
99 return entry->type_checksum_valid & ~FIT_ENTRY_CHECKSUM_VALID;
100}
101
102/*
103 * Get an offset from a host pointer. This function assumes the ROM is located
104 * in the host address space at [4G - romsize -> 4G). It also assume all
105 * pointers have values within this address range.
106 */
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800107static inline int ptr_to_offset(uint32_t theromsize, uint32_t host_ptr)
Aaron Durbin6b0d0d62012-12-14 17:16:21 -0600108{
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800109 return (int)(theromsize + host_ptr);
Aaron Durbin6b0d0d62012-12-14 17:16:21 -0600110}
111
112/*
113 * Get a pointer from an offset. This function assumes the ROM is located
114 * in the host address space at [4G - romsize -> 4G). It also assume all
115 * pointers have values within this address range.
116 */
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800117static inline uint32_t offset_to_ptr(uint32_t theromsize, int offset)
Aaron Durbin6b0d0d62012-12-14 17:16:21 -0600118{
Ronald G. Minnichaa2f7392013-12-03 11:13:35 -0800119 return -(theromsize - (uint32_t )offset);
Aaron Durbin6b0d0d62012-12-14 17:16:21 -0600120}
121
122static struct fit_table *locate_fit_table(struct cbfs_image *image)
123{
124 struct fit_table *table;
125 uint32_t *fit_pointer;
126
127 fit_pointer = rom_buffer_pointer(image,
128 ptr_to_offset(image->buffer.size, FIT_POINTER_LOCATION));
129
130 /* Ensure pointer is below 4GiB and within 16MiB of 4GiB */
131 if (fit_pointer[1] != 0 || fit_pointer[0] < FIT_TABLE_LOWEST_ADDRESS)
132 return NULL;
133
134 table = rom_buffer_pointer(image,
135 ptr_to_offset(image->buffer.size, *fit_pointer));
136
137 /* Check that the address field has the proper signature. */
138 if (strncmp((const char *)&table->header.address, FIT_HEADER_ADDRESS,
139 sizeof(table->header.address)))
140 return NULL;
141
142 if (table->header.version != FIT_HEADER_VERSION)
143 return NULL;
144
145 if (fit_entry_type(&table->header) != FIT_TYPE_HEADER)
146 return NULL;
147
148 /* Assume that the FIT table only contains the header */
149 if (fit_entry_size_bytes(&table->header) != sizeof(struct fit_entry))
150 return NULL;
151
152 return table;
153}
154
155static void update_fit_checksum(struct fit_table *fit)
156{
157 int size_bytes;
158 uint8_t *buffer;
159 uint8_t result;
160 int i;
161
162 fit->header.checksum = 0;
163 size_bytes = fit_entry_size_bytes(&fit->header);
164 result = 0;
165 buffer = (void *)fit;
166 for (i = 0; i < size_bytes; i++)
167 result += buffer[i];
168 fit->header.checksum = -result;
169}
170
171static void add_microcodde_entries(struct cbfs_image *image,
172 struct fit_table *fit,
173 struct microcode_entry *mcus, int num_mcus)
174{
175 int i;
176
177 for (i = 0; i < num_mcus; i++) {
178 struct fit_entry *entry = &fit->entries[i];
179 struct microcode_entry *mcu = &mcus[i];
180
181 entry->address = offset_to_ptr(image->buffer.size, mcu->offset);
182 fit_entry_update_size(entry, mcu->size);
183 entry->version = FIT_MICROCODE_VERSION;
184 entry->type_checksum_valid = FIT_TYPE_MICROCODE;
185 entry->checksum = 0;
186 fit_entry_add_size(&fit->header, sizeof(struct fit_entry));
187 }
188}
189
Ronald G. Minnich3fcde222014-02-04 17:35:44 -0800190static int fit_header(void *ptr, uint32_t *current_offset, uint32_t *file_length)
191{
192 struct buffer buf;
193 struct cbfs_file header;
194 buf.data = ptr;
195 buf.size = sizeof(header);
196 cbfs_file_get_header(&buf, &header);
197 *current_offset = header.offset;
198 *file_length = header.len;
199 return 0;
200}
201
Aaron Durbin6b0d0d62012-12-14 17:16:21 -0600202static int parse_microcode_blob(struct cbfs_image *image,
203 struct cbfs_file *mcode_file,
204 struct microcode_entry *mcus, int *total_mcus)
205{
206 int num_mcus;
Ronald G. Minnich3fcde222014-02-04 17:35:44 -0800207 uint32_t current_offset;
208 uint32_t file_length;
Aaron Durbin6b0d0d62012-12-14 17:16:21 -0600209
Ronald G. Minnich3fcde222014-02-04 17:35:44 -0800210 fit_header(mcode_file, &current_offset, &file_length);
Kyösti Mälkkif9b8ed82014-12-27 13:08:54 +0200211 current_offset += (int)((char *)mcode_file - image->buffer.data);
Aaron Durbin6b0d0d62012-12-14 17:16:21 -0600212
213 num_mcus = 0;
214 while (file_length > sizeof(struct microcode_header))
215 {
216 struct microcode_header *mcu_header;
217
218 mcu_header = rom_buffer_pointer(image, current_offset);
219
Aaron Durbin8a0cb8d2013-05-07 11:14:01 -0500220 /* Quickly sanity check a prospective microcode update. */
221 if (mcu_header->total_size < sizeof(*mcu_header))
222 break;
223
Aaron Durbin6b0d0d62012-12-14 17:16:21 -0600224 /* FIXME: Should the checksum be validated? */
225 mcus[num_mcus].offset = current_offset;
226 mcus[num_mcus].size = mcu_header->total_size;
227
228 /* Proceed to next payload. */
229 current_offset += mcus[num_mcus].size;
Aaron Durbin6b0d0d62012-12-14 17:16:21 -0600230 file_length -= mcus[num_mcus].size;
Alexandru Gagniucf87c20a2013-12-08 17:46:40 -0600231 num_mcus++;
Aaron Durbin6b0d0d62012-12-14 17:16:21 -0600232
Aaron Durbin6b0d0d62012-12-14 17:16:21 -0600233 /* Reached limit of FIT entries. */
234 if (num_mcus == *total_mcus)
235 break;
236 if (file_length < sizeof(struct microcode_header))
237 break;
238 }
239
240 /* Update how many microcode updates we found. */
241 *total_mcus = num_mcus;
242
243 return 0;
244}
245
246int fit_update_table(struct cbfs_image *image, int empty_entries,
247 const char *microcode_blob_name)
248{
249 struct fit_table *fit;
250 struct cbfs_file *mcode_file;
251 struct microcode_entry *mcus;
252 int ret = 0;
253 // struct rom_image image = { .rom = rom, .size = romsize, };
254
255 fit = locate_fit_table(image);
256
257 if (!fit) {
258 ERROR("FIT not found.\n");
259 return 1;
260 }
261
262 mcode_file = cbfs_get_entry(image, microcode_blob_name);
263 if (!mcode_file) {
264 ERROR("File '%s' not found in CBFS.\n",
265 microcode_blob_name);
266 return 1;
267 }
268
269 mcus = malloc(sizeof(*mcus) * empty_entries);
270
271 if (!mcus) {
272 ERROR("Couldn't allocate memory for microcode update entries.\n");
273 return 1;
274 }
275
276 if (parse_microcode_blob(image, mcode_file, mcus, &empty_entries)) {
277 ERROR("Couldn't parse microcode blob.\n");
278 ret = 1;
279 goto out;
280 }
281
282 add_microcodde_entries(image, fit, mcus, empty_entries);
283 update_fit_checksum(fit);
284
285out:
286 free(mcus);
287 return ret;
288}