2012-12-15 00:16:21 +01:00
|
|
|
/*
|
|
|
|
* Firmware Interface Table support.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; version 2 of the License.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2015-05-06 23:44:40 +02:00
|
|
|
#include <stdio.h>
|
2012-12-15 00:16:21 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2017-07-13 02:20:27 +02:00
|
|
|
#include <compiler.h>
|
2012-12-15 00:16:21 +01:00
|
|
|
|
|
|
|
#include "fit.h"
|
|
|
|
|
|
|
|
/* FIXME: This code assumes it is being executed on a little endian machine. */
|
|
|
|
|
|
|
|
#define FIT_POINTER_LOCATION 0xffffffc0
|
|
|
|
#define FIT_TABLE_LOWEST_ADDRESS ((uint32_t)(-(16 << 20)))
|
|
|
|
#define FIT_ENTRY_CHECKSUM_VALID 0x80
|
|
|
|
#define FIT_TYPE_HEADER 0x0
|
|
|
|
#define FIT_HEADER_VERSION 0x0100
|
|
|
|
#define FIT_HEADER_ADDRESS "_FIT_ "
|
|
|
|
#define FIT_TYPE_MICROCODE 0x1
|
|
|
|
#define FIT_MICROCODE_VERSION 0x0100
|
|
|
|
|
|
|
|
struct fit_entry {
|
|
|
|
uint64_t address;
|
|
|
|
uint32_t size_reserved;
|
|
|
|
uint16_t version;
|
|
|
|
uint8_t type_checksum_valid;
|
|
|
|
uint8_t checksum;
|
2017-07-13 02:20:27 +02:00
|
|
|
} __packed;
|
2012-12-15 00:16:21 +01:00
|
|
|
|
|
|
|
struct fit_table {
|
|
|
|
struct fit_entry header;
|
2015-03-06 00:38:03 +01:00
|
|
|
struct fit_entry entries[];
|
2017-07-13 02:20:27 +02:00
|
|
|
} __packed;
|
2012-12-15 00:16:21 +01:00
|
|
|
|
|
|
|
struct microcode_header {
|
|
|
|
uint32_t version;
|
|
|
|
uint32_t revision;
|
|
|
|
uint32_t date;
|
|
|
|
uint32_t processor_signature;
|
|
|
|
uint32_t checksum;
|
|
|
|
uint32_t loader_revision;
|
|
|
|
uint32_t processor_flags;
|
|
|
|
uint32_t data_size;
|
|
|
|
uint32_t total_size;
|
|
|
|
uint8_t reserved[12];
|
2017-07-13 02:20:27 +02:00
|
|
|
} __packed;
|
2012-12-15 00:16:21 +01:00
|
|
|
|
|
|
|
struct microcode_entry {
|
|
|
|
int offset;
|
|
|
|
int size;
|
|
|
|
};
|
|
|
|
|
2015-05-06 23:44:40 +02:00
|
|
|
static inline void *rom_buffer_pointer(struct buffer *buffer, int offset)
|
2012-12-15 00:16:21 +01:00
|
|
|
{
|
2015-05-06 23:44:40 +02:00
|
|
|
return &buffer->data[offset];
|
2012-12-15 00:16:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int fit_entry_size_bytes(struct fit_entry *entry)
|
|
|
|
{
|
|
|
|
return (entry->size_reserved & 0xffffff) << 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void fit_entry_update_size(struct fit_entry *entry,
|
|
|
|
int size_bytes)
|
|
|
|
{
|
|
|
|
/* Size is multiples of 16 bytes. */
|
|
|
|
entry->size_reserved = (size_bytes >> 4) & 0xffffff;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void fit_entry_add_size(struct fit_entry *entry,
|
|
|
|
int size_bytes)
|
|
|
|
{
|
|
|
|
int size = fit_entry_size_bytes(entry);
|
|
|
|
size += size_bytes;
|
|
|
|
fit_entry_update_size(entry, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int fit_entry_type(struct fit_entry *entry)
|
|
|
|
{
|
|
|
|
return entry->type_checksum_valid & ~FIT_ENTRY_CHECKSUM_VALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get an offset from a host pointer. This function assumes the ROM is located
|
|
|
|
* in the host address space at [4G - romsize -> 4G). It also assume all
|
|
|
|
* pointers have values within this address range.
|
|
|
|
*/
|
2015-05-06 23:44:40 +02:00
|
|
|
static inline int ptr_to_offset(fit_offset_converter_t helper,
|
|
|
|
const struct buffer *region, uint32_t host_ptr)
|
2012-12-15 00:16:21 +01:00
|
|
|
{
|
2015-05-06 23:44:40 +02:00
|
|
|
return helper(region, -host_ptr);
|
2012-12-15 00:16:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get a pointer from an offset. This function assumes the ROM is located
|
|
|
|
* in the host address space at [4G - romsize -> 4G). It also assume all
|
|
|
|
* pointers have values within this address range.
|
|
|
|
*/
|
2015-05-06 23:44:40 +02:00
|
|
|
static inline uint32_t offset_to_ptr(fit_offset_converter_t helper,
|
|
|
|
const struct buffer *region, int offset)
|
2012-12-15 00:16:21 +01:00
|
|
|
{
|
2015-05-06 23:44:40 +02:00
|
|
|
return -helper(region, offset);
|
2012-12-15 00:16:21 +01:00
|
|
|
}
|
|
|
|
|
2018-06-04 19:32:46 +02:00
|
|
|
static int fit_table_verified(struct fit_table *table)
|
|
|
|
{
|
|
|
|
/* Check that the address field has the proper signature. */
|
|
|
|
if (strncmp((const char *)&table->header.address, FIT_HEADER_ADDRESS,
|
|
|
|
sizeof(table->header.address)))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (table->header.version != FIT_HEADER_VERSION)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (fit_entry_type(&table->header) != FIT_TYPE_HEADER)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Assume that the FIT table only contains the header */
|
|
|
|
if (fit_entry_size_bytes(&table->header) != sizeof(struct fit_entry))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-05-06 23:44:40 +02:00
|
|
|
static struct fit_table *locate_fit_table(fit_offset_converter_t offset_helper,
|
|
|
|
struct buffer *buffer)
|
2012-12-15 00:16:21 +01:00
|
|
|
{
|
|
|
|
struct fit_table *table;
|
|
|
|
uint32_t *fit_pointer;
|
|
|
|
|
2015-05-06 23:44:40 +02:00
|
|
|
fit_pointer = rom_buffer_pointer(buffer,
|
|
|
|
ptr_to_offset(offset_helper, buffer,
|
|
|
|
FIT_POINTER_LOCATION));
|
2012-12-15 00:16:21 +01:00
|
|
|
|
|
|
|
/* Ensure pointer is below 4GiB and within 16MiB of 4GiB */
|
|
|
|
if (fit_pointer[1] != 0 || fit_pointer[0] < FIT_TABLE_LOWEST_ADDRESS)
|
|
|
|
return NULL;
|
|
|
|
|
2015-05-06 23:44:40 +02:00
|
|
|
table = rom_buffer_pointer(buffer,
|
|
|
|
ptr_to_offset(offset_helper, buffer, *fit_pointer));
|
2018-06-04 19:32:46 +02:00
|
|
|
if (!fit_table_verified(table))
|
2012-12-15 00:16:21 +01:00
|
|
|
return NULL;
|
2018-06-04 19:32:46 +02:00
|
|
|
else
|
|
|
|
return table;
|
2012-12-15 00:16:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void update_fit_checksum(struct fit_table *fit)
|
|
|
|
{
|
|
|
|
int size_bytes;
|
|
|
|
uint8_t *buffer;
|
|
|
|
uint8_t result;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
fit->header.checksum = 0;
|
|
|
|
size_bytes = fit_entry_size_bytes(&fit->header);
|
|
|
|
result = 0;
|
|
|
|
buffer = (void *)fit;
|
|
|
|
for (i = 0; i < size_bytes; i++)
|
|
|
|
result += buffer[i];
|
|
|
|
fit->header.checksum = -result;
|
|
|
|
}
|
|
|
|
|
2018-06-04 19:32:46 +02:00
|
|
|
static void update_fit_ucode_entry(struct fit_table *fit,
|
|
|
|
struct fit_entry *entry, uint64_t mcu_addr)
|
|
|
|
{
|
|
|
|
entry->address = mcu_addr;
|
|
|
|
/*
|
|
|
|
* While loading MCU, its size is not referred from FIT and
|
|
|
|
* rather from the MCU header, hence we can assign zero here
|
|
|
|
*/
|
|
|
|
entry->size_reserved = 0x0000;
|
|
|
|
/* Checksum valid should be cleared for MCU */
|
|
|
|
entry->type_checksum_valid = 0;
|
|
|
|
entry->version = FIT_MICROCODE_VERSION;
|
|
|
|
entry->checksum = 0;
|
|
|
|
fit_entry_add_size(&fit->header, sizeof(struct fit_entry));
|
|
|
|
}
|
|
|
|
|
2015-05-06 23:44:40 +02:00
|
|
|
static void add_microcodde_entries(struct fit_table *fit,
|
|
|
|
const struct cbfs_image *image,
|
|
|
|
int num_mcus, struct microcode_entry *mcus,
|
2018-06-04 19:32:46 +02:00
|
|
|
fit_offset_converter_t offset_helper,
|
|
|
|
uint32_t first_mcu_addr)
|
2012-12-15 00:16:21 +01:00
|
|
|
{
|
2018-06-04 19:32:46 +02:00
|
|
|
int i = 0;
|
|
|
|
/*
|
|
|
|
* Check if an entry has to be forced into the FIT at index 0.
|
|
|
|
* first_mcu_addr is an address (in ROM) that will point to a
|
|
|
|
* microcode patch.
|
|
|
|
*/
|
|
|
|
if (first_mcu_addr) {
|
|
|
|
struct fit_entry *entry = &fit->entries[0];
|
|
|
|
update_fit_ucode_entry(fit, entry, first_mcu_addr);
|
|
|
|
i = 1;
|
|
|
|
}
|
2012-12-15 00:16:21 +01:00
|
|
|
|
2018-06-04 19:32:46 +02:00
|
|
|
struct microcode_entry *mcu = &mcus[0];
|
|
|
|
for (; i < num_mcus; i++) {
|
2012-12-15 00:16:21 +01:00
|
|
|
struct fit_entry *entry = &fit->entries[i];
|
2018-06-04 19:32:46 +02:00
|
|
|
update_fit_ucode_entry(fit, entry, offset_to_ptr(offset_helper,
|
|
|
|
&image->buffer, mcu->offset));
|
|
|
|
mcu++;
|
2012-12-15 00:16:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-23 17:44:46 +02:00
|
|
|
static void cbfs_file_get_header(struct buffer *buf, struct cbfs_file *file)
|
|
|
|
{
|
|
|
|
bgets(buf, &file->magic, sizeof(file->magic));
|
|
|
|
file->len = xdr_be.get32(buf);
|
|
|
|
file->type = xdr_be.get32(buf);
|
|
|
|
file->attributes_offset = xdr_be.get32(buf);
|
|
|
|
file->offset = xdr_be.get32(buf);
|
|
|
|
}
|
|
|
|
|
2014-02-05 02:35:44 +01:00
|
|
|
static int fit_header(void *ptr, uint32_t *current_offset, uint32_t *file_length)
|
|
|
|
{
|
|
|
|
struct buffer buf;
|
|
|
|
struct cbfs_file header;
|
|
|
|
buf.data = ptr;
|
|
|
|
buf.size = sizeof(header);
|
|
|
|
cbfs_file_get_header(&buf, &header);
|
|
|
|
*current_offset = header.offset;
|
|
|
|
*file_length = header.len;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-15 00:16:21 +01:00
|
|
|
static int parse_microcode_blob(struct cbfs_image *image,
|
2018-06-04 19:32:46 +02:00
|
|
|
struct cbfs_file *mcode_file,
|
|
|
|
struct microcode_entry *mcus,
|
|
|
|
int total_entries, int *mcus_found)
|
2012-12-15 00:16:21 +01:00
|
|
|
{
|
|
|
|
int num_mcus;
|
2014-02-05 02:35:44 +01:00
|
|
|
uint32_t current_offset;
|
|
|
|
uint32_t file_length;
|
2012-12-15 00:16:21 +01:00
|
|
|
|
2014-02-05 02:35:44 +01:00
|
|
|
fit_header(mcode_file, ¤t_offset, &file_length);
|
2014-12-27 12:08:54 +01:00
|
|
|
current_offset += (int)((char *)mcode_file - image->buffer.data);
|
2012-12-15 00:16:21 +01:00
|
|
|
|
|
|
|
num_mcus = 0;
|
|
|
|
while (file_length > sizeof(struct microcode_header))
|
|
|
|
{
|
2015-05-06 23:44:40 +02:00
|
|
|
const struct microcode_header *mcu_header;
|
2012-12-15 00:16:21 +01:00
|
|
|
|
2015-05-06 23:44:40 +02:00
|
|
|
mcu_header = rom_buffer_pointer(&image->buffer, current_offset);
|
2012-12-15 00:16:21 +01:00
|
|
|
|
2018-06-13 15:39:57 +02:00
|
|
|
/* Newer microcode updates include a size field, whereas older
|
|
|
|
* containers set it at 0 and are exactly 2048 bytes long */
|
|
|
|
uint32_t total_size = mcu_header->total_size
|
|
|
|
? mcu_header->total_size : 2048;
|
|
|
|
|
2013-05-07 18:14:01 +02:00
|
|
|
/* Quickly sanity check a prospective microcode update. */
|
2018-06-13 15:39:57 +02:00
|
|
|
if (total_size < sizeof(*mcu_header))
|
2013-05-07 18:14:01 +02:00
|
|
|
break;
|
|
|
|
|
2012-12-15 00:16:21 +01:00
|
|
|
/* FIXME: Should the checksum be validated? */
|
|
|
|
mcus[num_mcus].offset = current_offset;
|
2018-06-13 15:39:57 +02:00
|
|
|
mcus[num_mcus].size = total_size;
|
2012-12-15 00:16:21 +01:00
|
|
|
|
|
|
|
/* Proceed to next payload. */
|
|
|
|
current_offset += mcus[num_mcus].size;
|
|
|
|
file_length -= mcus[num_mcus].size;
|
2013-12-09 00:46:40 +01:00
|
|
|
num_mcus++;
|
2012-12-15 00:16:21 +01:00
|
|
|
|
|
|
|
/* Reached limit of FIT entries. */
|
2018-06-04 19:32:46 +02:00
|
|
|
if (num_mcus == total_entries)
|
2012-12-15 00:16:21 +01:00
|
|
|
break;
|
|
|
|
if (file_length < sizeof(struct microcode_header))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update how many microcode updates we found. */
|
2018-06-04 19:32:46 +02:00
|
|
|
*mcus_found = num_mcus;
|
2012-12-15 00:16:21 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-06 23:44:40 +02:00
|
|
|
int fit_update_table(struct buffer *bootblock, struct cbfs_image *image,
|
|
|
|
const char *microcode_blob_name, int empty_entries,
|
2018-06-04 19:32:46 +02:00
|
|
|
fit_offset_converter_t offset_fn, uint32_t topswap_size,
|
|
|
|
uint32_t first_mcu_addr)
|
2012-12-15 00:16:21 +01:00
|
|
|
{
|
2018-06-04 19:32:46 +02:00
|
|
|
struct fit_table *fit, *fit2;
|
2012-12-15 00:16:21 +01:00
|
|
|
struct cbfs_file *mcode_file;
|
|
|
|
struct microcode_entry *mcus;
|
2018-06-04 19:32:46 +02:00
|
|
|
int mcus_found;
|
|
|
|
|
2012-12-15 00:16:21 +01:00
|
|
|
int ret = 0;
|
|
|
|
// struct rom_image image = { .rom = rom, .size = romsize, };
|
|
|
|
|
2015-05-06 23:44:40 +02:00
|
|
|
fit = locate_fit_table(offset_fn, bootblock);
|
2012-12-15 00:16:21 +01:00
|
|
|
|
|
|
|
if (!fit) {
|
|
|
|
ERROR("FIT not found.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
mcode_file = cbfs_get_entry(image, microcode_blob_name);
|
|
|
|
if (!mcode_file) {
|
|
|
|
ERROR("File '%s' not found in CBFS.\n",
|
|
|
|
microcode_blob_name);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
mcus = malloc(sizeof(*mcus) * empty_entries);
|
|
|
|
|
|
|
|
if (!mcus) {
|
|
|
|
ERROR("Couldn't allocate memory for microcode update entries.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-06-04 19:32:46 +02:00
|
|
|
if (parse_microcode_blob(image, mcode_file, mcus, empty_entries,
|
|
|
|
&mcus_found)) {
|
2012-12-15 00:16:21 +01:00
|
|
|
ERROR("Couldn't parse microcode blob.\n");
|
|
|
|
ret = 1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2018-06-04 19:32:46 +02:00
|
|
|
add_microcodde_entries(fit, image, mcus_found, mcus, offset_fn, 0);
|
|
|
|
|
2012-12-15 00:16:21 +01:00
|
|
|
update_fit_checksum(fit);
|
|
|
|
|
2018-06-04 19:32:46 +02:00
|
|
|
/* A second fit is exactly topswap size away from the bottom one */
|
|
|
|
if (topswap_size) {
|
|
|
|
|
|
|
|
fit2 = (struct fit_table *)((uintptr_t)fit - topswap_size);
|
|
|
|
|
|
|
|
if (!fit_table_verified(fit2)) {
|
|
|
|
ERROR("second FIT is invalid\n");
|
|
|
|
ret = 1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/* Check if we have room for first entry */
|
|
|
|
if (first_mcu_addr) {
|
|
|
|
if (mcus_found >= empty_entries) {
|
|
|
|
ERROR("No room, blob mcus = %d, total entries = %d\n",
|
|
|
|
mcus_found, empty_entries);
|
|
|
|
ret = 1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/* Add 1 for the first entry */
|
|
|
|
mcus_found++;
|
|
|
|
}
|
|
|
|
/* Add entries in the second FIT */
|
|
|
|
add_microcodde_entries(fit2, image, mcus_found, mcus,
|
|
|
|
offset_fn, first_mcu_addr);
|
|
|
|
update_fit_checksum(fit2);
|
|
|
|
}
|
2012-12-15 00:16:21 +01:00
|
|
|
out:
|
|
|
|
free(mcus);
|
|
|
|
return ret;
|
|
|
|
}
|