Andrey Petrov | 9738970 | 2016-02-25 14:15:37 -0800 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2015 Intel Corp. |
| 5 | * (Written by Alexandru Gagniuc <alexandrux.gagniuc@intel.com> for Intel Corp.) |
| 6 | * (Written by Andrey Petrov <andrey.petrov@intel.com> for Intel Corp.) |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | */ |
| 13 | |
| 14 | #include <arch/io.h> |
| 15 | #include <cbfs.h> |
| 16 | #include <console/console.h> |
| 17 | #include <fsp/util.h> |
| 18 | #include <lib.h> |
| 19 | #include <memrange.h> |
| 20 | #include <string.h> |
| 21 | |
| 22 | static bool looks_like_fsp_header(const uint8_t *raw_hdr) |
| 23 | { |
| 24 | if (memcmp(raw_hdr, FSP_HDR_SIGNATURE, 4)) { |
| 25 | printk(BIOS_ALERT, "Did not find a valid FSP signature\n"); |
| 26 | return false; |
| 27 | } |
| 28 | |
| 29 | if (read32(raw_hdr + 4) != FSP_HDR_LEN) { |
| 30 | printk(BIOS_ALERT, "FSP header has invalid length\n"); |
| 31 | return false; |
| 32 | } |
| 33 | |
| 34 | return true; |
| 35 | } |
| 36 | |
| 37 | enum cb_err fsp_identify(struct fsp_header *hdr, const void *fsp_blob) |
| 38 | { |
| 39 | const uint8_t *raw_hdr = fsp_blob; |
| 40 | |
| 41 | if (!looks_like_fsp_header(raw_hdr)) |
| 42 | return CB_ERR; |
| 43 | |
| 44 | hdr->revision = read8(raw_hdr + 11); |
| 45 | hdr->fsp_revision = read32(raw_hdr + 12); |
| 46 | memcpy(hdr->image_id, raw_hdr + 16, ARRAY_SIZE(hdr->image_id)); |
| 47 | hdr->image_id[ARRAY_SIZE(hdr->image_id) - 1] = '\0'; |
| 48 | hdr->image_size = read32(raw_hdr + 24); |
| 49 | hdr->image_base = read32(raw_hdr + 28); |
| 50 | hdr->image_attribute = read32(raw_hdr + 32); |
| 51 | hdr->cfg_region_offset = read32(raw_hdr + 36); |
| 52 | hdr->cfg_region_size = read32(raw_hdr + 40); |
| 53 | hdr->notify_phase_entry_offset = read32(raw_hdr + 56); |
| 54 | hdr->memory_init_entry_offset = read32(raw_hdr + 60); |
| 55 | hdr->silicon_init_entry_offset = read32(raw_hdr + 68); |
| 56 | |
| 57 | return CB_SUCCESS; |
| 58 | } |
| 59 | |
| 60 | void fsp_print_header_info(const struct fsp_header *hdr) |
| 61 | { |
| 62 | printk(BIOS_DEBUG, "Revision %u, image ID: %s, base 0x%lx + 0x%zx\n", |
| 63 | hdr->revision, hdr->image_id, hdr->image_base, hdr->image_size); |
| 64 | printk(BIOS_DEBUG, "\tConfig region 0x%zx + 0x%zx\n", |
| 65 | hdr->cfg_region_offset, hdr->cfg_region_size); |
| 66 | |
| 67 | if (hdr->image_attribute & FSP_HDR_ATTRIB_FSPM) { |
| 68 | printk(BIOS_DEBUG, "\tMemory init offset 0x%zx\n", |
| 69 | hdr->memory_init_entry_offset); |
| 70 | } |
| 71 | |
| 72 | if (hdr->image_attribute & FSP_HDR_ATTRIB_FSPS) { |
| 73 | printk(BIOS_DEBUG, "\tSilicon init offset 0x%zx\n", |
| 74 | hdr->silicon_init_entry_offset); |
| 75 | printk(BIOS_DEBUG, "\tNotify phase offset 0x%zx\n", |
| 76 | hdr->notify_phase_entry_offset); |
| 77 | } |
| 78 | |
| 79 | } |
| 80 | |
| 81 | enum cb_err fsp_load_binary(struct fsp_header *hdr, |
| 82 | const char *name, |
| 83 | struct range_entry *range) |
| 84 | { |
| 85 | struct cbfsf file_desc; |
| 86 | struct region_device file_data; |
| 87 | void *membase; |
| 88 | |
| 89 | if (cbfs_boot_locate(&file_desc, name, NULL)) { |
| 90 | printk(BIOS_ERR, "Could not locate %s in CBFS\n", name); |
| 91 | return CB_ERR; |
| 92 | } |
| 93 | |
| 94 | cbfs_file_data(&file_data, &file_desc); |
| 95 | |
| 96 | /* Map just enough of the file to be able to parse the header. */ |
| 97 | membase = rdev_mmap(&file_data, FSP_HDR_OFFSET, FSP_HDR_LEN); |
Aaron Durbin | 40672be | 2016-03-08 11:01:17 -0600 | [diff] [blame^] | 98 | |
| 99 | if (membase == NULL) { |
| 100 | printk(BIOS_ERR, "Could not mmap() '%s' FSP header.\n", name); |
| 101 | return CB_ERR; |
| 102 | } |
| 103 | |
Andrey Petrov | 9738970 | 2016-02-25 14:15:37 -0800 | [diff] [blame] | 104 | if (fsp_identify(hdr, membase) != CB_SUCCESS) { |
Aaron Durbin | 40672be | 2016-03-08 11:01:17 -0600 | [diff] [blame^] | 105 | rdev_munmap(&file_data, membase); |
Andrey Petrov | 9738970 | 2016-02-25 14:15:37 -0800 | [diff] [blame] | 106 | printk(BIOS_ERR, "%s did not have a valid FSP header\n", name); |
| 107 | return CB_ERR; |
| 108 | } |
| 109 | |
Aaron Durbin | 40672be | 2016-03-08 11:01:17 -0600 | [diff] [blame^] | 110 | rdev_munmap(&file_data, membase); |
| 111 | |
Andrey Petrov | 9738970 | 2016-02-25 14:15:37 -0800 | [diff] [blame] | 112 | fsp_print_header_info(hdr); |
| 113 | |
| 114 | /* Check if size specified in the header matches the cbfs file size */ |
| 115 | if (region_device_sz(&file_data) < hdr->image_size) { |
| 116 | printk(BIOS_ERR, "%s size bigger than cbfs file.\n", name); |
| 117 | return CB_ERR; |
| 118 | } |
| 119 | |
| 120 | /* Check if the binary load address is within expected range */ |
| 121 | if (range_entry_base(range) > hdr->image_base || |
| 122 | range_entry_end(range) <= hdr->image_base + hdr->image_size) { |
| 123 | printk(BIOS_ERR, "%s is outside of allowed range\n", name); |
| 124 | return CB_ERR; |
| 125 | } |
| 126 | |
| 127 | /* Load binary into memory. */ |
| 128 | if (rdev_readat(&file_data, (void *)hdr->image_base, 0, hdr->image_size) < 0) |
| 129 | return CB_ERR; |
| 130 | |
| 131 | return CB_SUCCESS; |
| 132 | } |