blob: a234a7c12a49f35e4c342063d8a4218b950e086f [file] [log] [blame]
Andrey Petrov97389702016-02-25 14:15:37 -08001/*
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
22static 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
37enum 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
60void 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
Aaron Durbin9b28f0022016-03-08 11:20:53 -060081/* TODO: this won't work for SoC's that need to XIP certain modules. */
Andrey Petrov97389702016-02-25 14:15:37 -080082enum cb_err fsp_load_binary(struct fsp_header *hdr,
83 const char *name,
84 struct range_entry *range)
85{
86 struct cbfsf file_desc;
87 struct region_device file_data;
88 void *membase;
89
90 if (cbfs_boot_locate(&file_desc, name, NULL)) {
91 printk(BIOS_ERR, "Could not locate %s in CBFS\n", name);
92 return CB_ERR;
93 }
94
95 cbfs_file_data(&file_data, &file_desc);
96
97 /* Map just enough of the file to be able to parse the header. */
98 membase = rdev_mmap(&file_data, FSP_HDR_OFFSET, FSP_HDR_LEN);
Aaron Durbin40672be2016-03-08 11:01:17 -060099
100 if (membase == NULL) {
101 printk(BIOS_ERR, "Could not mmap() '%s' FSP header.\n", name);
102 return CB_ERR;
103 }
104
Andrey Petrov97389702016-02-25 14:15:37 -0800105 if (fsp_identify(hdr, membase) != CB_SUCCESS) {
Aaron Durbin40672be2016-03-08 11:01:17 -0600106 rdev_munmap(&file_data, membase);
Andrey Petrov97389702016-02-25 14:15:37 -0800107 printk(BIOS_ERR, "%s did not have a valid FSP header\n", name);
108 return CB_ERR;
109 }
110
Aaron Durbin40672be2016-03-08 11:01:17 -0600111 rdev_munmap(&file_data, membase);
112
Andrey Petrov97389702016-02-25 14:15:37 -0800113 fsp_print_header_info(hdr);
114
115 /* Check if size specified in the header matches the cbfs file size */
116 if (region_device_sz(&file_data) < hdr->image_size) {
117 printk(BIOS_ERR, "%s size bigger than cbfs file.\n", name);
118 return CB_ERR;
119 }
120
121 /* Check if the binary load address is within expected range */
Aaron Durbin9b28f0022016-03-08 11:20:53 -0600122 /* TODO: this doesn't check the current running program footprint. */
Andrey Petrov97389702016-02-25 14:15:37 -0800123 if (range_entry_base(range) > hdr->image_base ||
124 range_entry_end(range) <= hdr->image_base + hdr->image_size) {
125 printk(BIOS_ERR, "%s is outside of allowed range\n", name);
126 return CB_ERR;
127 }
128
129 /* Load binary into memory. */
130 if (rdev_readat(&file_data, (void *)hdr->image_base, 0, hdr->image_size) < 0)
131 return CB_ERR;
132
133 return CB_SUCCESS;
134}