util/superiotool/smsc.c: Add some register dumps
[coreboot.git] / util / cbfstool / fit.c
blobaeb1755032a8166f5fd8e4e65d611bd003be9e54
1 /*
2 * Firmware Interface Table support.
4 * Copyright (C) 2012 Google Inc.
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.
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.
16 #include <stdint.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
21 #include "fit.h"
23 /* FIXME: This code assumes it is being executed on a little endian machine. */
25 #define FIT_POINTER_LOCATION 0xffffffc0
26 #define FIT_TABLE_LOWEST_ADDRESS ((uint32_t)(-(16 << 20)))
27 #define FIT_ENTRY_CHECKSUM_VALID 0x80
28 #define FIT_TYPE_HEADER 0x0
29 #define FIT_HEADER_VERSION 0x0100
30 #define FIT_HEADER_ADDRESS "_FIT_ "
31 #define FIT_TYPE_MICROCODE 0x1
32 #define FIT_MICROCODE_VERSION 0x0100
34 struct fit_entry {
35 uint64_t address;
36 uint32_t size_reserved;
37 uint16_t version;
38 uint8_t type_checksum_valid;
39 uint8_t checksum;
40 } __packed;
42 struct fit_table {
43 struct fit_entry header;
44 struct fit_entry entries[];
45 } __packed;
47 struct microcode_header {
48 uint32_t version;
49 uint32_t revision;
50 uint32_t date;
51 uint32_t processor_signature;
52 uint32_t checksum;
53 uint32_t loader_revision;
54 uint32_t processor_flags;
55 uint32_t data_size;
56 uint32_t total_size;
57 uint8_t reserved[12];
58 } __packed;
60 struct microcode_entry {
61 int offset;
62 int size;
65 static inline void *rom_buffer_pointer(struct buffer *buffer, int offset)
67 return &buffer->data[offset];
70 static inline int fit_entry_size_bytes(struct fit_entry *entry)
72 return (entry->size_reserved & 0xffffff) << 4;
75 static inline void fit_entry_update_size(struct fit_entry *entry,
76 int size_bytes)
78 /* Size is multiples of 16 bytes. */
79 entry->size_reserved = (size_bytes >> 4) & 0xffffff;
82 static inline void fit_entry_add_size(struct fit_entry *entry,
83 int size_bytes)
85 int size = fit_entry_size_bytes(entry);
86 size += size_bytes;
87 fit_entry_update_size(entry, size);
90 static inline int fit_entry_type(struct fit_entry *entry)
92 return entry->type_checksum_valid & ~FIT_ENTRY_CHECKSUM_VALID;
96 * Get an offset from a host pointer. This function assumes the ROM is located
97 * in the host address space at [4G - romsize -> 4G). It also assume all
98 * pointers have values within this address range.
100 static inline int ptr_to_offset(fit_offset_converter_t helper,
101 const struct buffer *region, uint32_t host_ptr)
103 return helper(region, -host_ptr);
107 * Get a pointer from an offset. This function assumes the ROM is located
108 * in the host address space at [4G - romsize -> 4G). It also assume all
109 * pointers have values within this address range.
111 static inline uint32_t offset_to_ptr(fit_offset_converter_t helper,
112 const struct buffer *region, int offset)
114 return -helper(region, offset);
117 static int fit_table_verified(struct fit_table *table)
119 /* Check that the address field has the proper signature. */
120 if (strncmp((const char *)&table->header.address, FIT_HEADER_ADDRESS,
121 sizeof(table->header.address)))
122 return 0;
124 if (table->header.version != FIT_HEADER_VERSION)
125 return 0;
127 if (fit_entry_type(&table->header) != FIT_TYPE_HEADER)
128 return 0;
130 /* Assume that the FIT table only contains the header */
131 if (fit_entry_size_bytes(&table->header) != sizeof(struct fit_entry))
132 return 0;
134 return 1;
137 static struct fit_table *locate_fit_table(fit_offset_converter_t offset_helper,
138 struct buffer *buffer)
140 struct fit_table *table;
141 uint32_t *fit_pointer;
143 fit_pointer = rom_buffer_pointer(buffer,
144 ptr_to_offset(offset_helper, buffer,
145 FIT_POINTER_LOCATION));
147 /* Ensure pointer is below 4GiB and within 16MiB of 4GiB */
148 if (fit_pointer[1] != 0 || fit_pointer[0] < FIT_TABLE_LOWEST_ADDRESS)
149 return NULL;
151 table = rom_buffer_pointer(buffer,
152 ptr_to_offset(offset_helper, buffer, *fit_pointer));
153 if (!fit_table_verified(table))
154 return NULL;
155 else
156 return table;
159 static void update_fit_checksum(struct fit_table *fit)
161 int size_bytes;
162 uint8_t *buffer;
163 uint8_t result;
164 int i;
166 fit->header.checksum = 0;
167 size_bytes = fit_entry_size_bytes(&fit->header);
168 result = 0;
169 buffer = (void *)fit;
170 for (i = 0; i < size_bytes; i++)
171 result += buffer[i];
172 fit->header.checksum = -result;
175 static void update_fit_ucode_entry(struct fit_table *fit,
176 struct fit_entry *entry, uint64_t mcu_addr)
178 entry->address = mcu_addr;
180 * While loading MCU, its size is not referred from FIT and
181 * rather from the MCU header, hence we can assign zero here
183 entry->size_reserved = 0x0000;
184 entry->type_checksum_valid = FIT_TYPE_MICROCODE;
185 entry->version = FIT_MICROCODE_VERSION;
186 entry->checksum = 0;
187 fit_entry_add_size(&fit->header, sizeof(struct fit_entry));
190 static void add_microcodde_entries(struct fit_table *fit,
191 const struct cbfs_image *image,
192 int num_mcus, struct microcode_entry *mcus,
193 fit_offset_converter_t offset_helper,
194 uint32_t first_mcu_addr)
196 int i = 0;
198 * Check if an entry has to be forced into the FIT at index 0.
199 * first_mcu_addr is an address (in ROM) that will point to a
200 * microcode patch.
202 if (first_mcu_addr) {
203 struct fit_entry *entry = &fit->entries[0];
204 update_fit_ucode_entry(fit, entry, first_mcu_addr);
205 i = 1;
208 struct microcode_entry *mcu = &mcus[0];
209 for (; i < num_mcus; i++) {
210 struct fit_entry *entry = &fit->entries[i];
211 update_fit_ucode_entry(fit, entry, offset_to_ptr(offset_helper,
212 &image->buffer, mcu->offset));
213 mcu++;
217 static void cbfs_file_get_header(struct buffer *buf, struct cbfs_file *file)
219 bgets(buf, &file->magic, sizeof(file->magic));
220 file->len = xdr_be.get32(buf);
221 file->type = xdr_be.get32(buf);
222 file->attributes_offset = xdr_be.get32(buf);
223 file->offset = xdr_be.get32(buf);
226 static int fit_header(void *ptr, uint32_t *current_offset, uint32_t *file_length)
228 struct buffer buf;
229 struct cbfs_file header;
230 buf.data = ptr;
231 buf.size = sizeof(header);
232 cbfs_file_get_header(&buf, &header);
233 *current_offset = header.offset;
234 *file_length = header.len;
235 return 0;
238 static int parse_microcode_blob(struct cbfs_image *image,
239 struct cbfs_file *mcode_file,
240 struct microcode_entry *mcus,
241 int total_entries, int *mcus_found)
243 int num_mcus;
244 uint32_t current_offset;
245 uint32_t file_length;
247 fit_header(mcode_file, &current_offset, &file_length);
248 current_offset += (int)((char *)mcode_file - image->buffer.data);
250 num_mcus = 0;
251 while (file_length > sizeof(struct microcode_header))
253 const struct microcode_header *mcu_header;
255 mcu_header = rom_buffer_pointer(&image->buffer, current_offset);
257 /* Newer microcode updates include a size field, whereas older
258 * containers set it at 0 and are exactly 2048 bytes long */
259 uint32_t total_size = mcu_header->total_size
260 ? mcu_header->total_size : 2048;
262 /* Quickly sanity check a prospective microcode update. */
263 if (total_size < sizeof(*mcu_header))
264 break;
266 /* FIXME: Should the checksum be validated? */
267 mcus[num_mcus].offset = current_offset;
268 mcus[num_mcus].size = total_size;
270 /* Proceed to next payload. */
271 current_offset += mcus[num_mcus].size;
272 file_length -= mcus[num_mcus].size;
273 num_mcus++;
275 /* Reached limit of FIT entries. */
276 if (num_mcus == total_entries)
277 break;
278 if (file_length < sizeof(struct microcode_header))
279 break;
282 /* Update how many microcode updates we found. */
283 *mcus_found = num_mcus;
285 return 0;
288 int fit_update_table(struct buffer *bootblock, struct cbfs_image *image,
289 const char *microcode_blob_name, int empty_entries,
290 fit_offset_converter_t offset_fn, uint32_t topswap_size,
291 uint32_t first_mcu_addr)
293 struct fit_table *fit, *fit2;
294 struct cbfs_file *mcode_file;
295 struct microcode_entry *mcus;
296 int mcus_found;
298 int ret = 0;
299 // struct rom_image image = { .rom = rom, .size = romsize, };
301 fit = locate_fit_table(offset_fn, bootblock);
303 if (!fit) {
304 ERROR("FIT not found.\n");
305 return 1;
308 mcode_file = cbfs_get_entry(image, microcode_blob_name);
309 if (!mcode_file) {
310 ERROR("File '%s' not found in CBFS.\n",
311 microcode_blob_name);
312 return 1;
315 mcus = malloc(sizeof(*mcus) * empty_entries);
317 if (!mcus) {
318 ERROR("Couldn't allocate memory for microcode update entries.\n");
319 return 1;
322 if (parse_microcode_blob(image, mcode_file, mcus, empty_entries,
323 &mcus_found)) {
324 ERROR("Couldn't parse microcode blob.\n");
325 ret = 1;
326 goto out;
329 add_microcodde_entries(fit, image, mcus_found, mcus, offset_fn, 0);
331 update_fit_checksum(fit);
333 /* A second fit is exactly topswap size away from the bottom one */
334 if (topswap_size) {
336 fit2 = (struct fit_table *)((uintptr_t)fit - topswap_size);
338 if (!fit_table_verified(fit2)) {
339 ERROR("second FIT is invalid\n");
340 ret = 1;
341 goto out;
343 /* Check if we have room for first entry */
344 if (first_mcu_addr) {
345 if (mcus_found >= empty_entries) {
346 ERROR("No room, blob mcus = %d, total entries = %d\n",
347 mcus_found, empty_entries);
348 ret = 1;
349 goto out;
351 /* Add 1 for the first entry */
352 mcus_found++;
354 /* Add entries in the second FIT */
355 add_microcodde_entries(fit2, image, mcus_found, mcus,
356 offset_fn, first_mcu_addr);
357 update_fit_checksum(fit2);
359 out:
360 free(mcus);
361 return ret;