blob: c4fe8dc9332571440dca9877d0c524e4376f2e37 [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>
Aaron Durbina33c6d72016-03-31 12:48:30 -050020#include <program_loading.h>
Andrey Petrov97389702016-02-25 14:15:37 -080021#include <string.h>
22
23static bool looks_like_fsp_header(const uint8_t *raw_hdr)
24{
25 if (memcmp(raw_hdr, FSP_HDR_SIGNATURE, 4)) {
26 printk(BIOS_ALERT, "Did not find a valid FSP signature\n");
27 return false;
28 }
29
30 if (read32(raw_hdr + 4) != FSP_HDR_LEN) {
31 printk(BIOS_ALERT, "FSP header has invalid length\n");
32 return false;
33 }
34
35 return true;
36}
37
38enum cb_err fsp_identify(struct fsp_header *hdr, const void *fsp_blob)
39{
40 const uint8_t *raw_hdr = fsp_blob;
41
42 if (!looks_like_fsp_header(raw_hdr))
43 return CB_ERR;
44
45 hdr->revision = read8(raw_hdr + 11);
46 hdr->fsp_revision = read32(raw_hdr + 12);
47 memcpy(hdr->image_id, raw_hdr + 16, ARRAY_SIZE(hdr->image_id));
48 hdr->image_id[ARRAY_SIZE(hdr->image_id) - 1] = '\0';
49 hdr->image_size = read32(raw_hdr + 24);
50 hdr->image_base = read32(raw_hdr + 28);
51 hdr->image_attribute = read32(raw_hdr + 32);
52 hdr->cfg_region_offset = read32(raw_hdr + 36);
53 hdr->cfg_region_size = read32(raw_hdr + 40);
54 hdr->notify_phase_entry_offset = read32(raw_hdr + 56);
55 hdr->memory_init_entry_offset = read32(raw_hdr + 60);
56 hdr->silicon_init_entry_offset = read32(raw_hdr + 68);
57
58 return CB_SUCCESS;
59}
60
61void fsp_print_header_info(const struct fsp_header *hdr)
62{
63 printk(BIOS_DEBUG, "Revision %u, image ID: %s, base 0x%lx + 0x%zx\n",
64 hdr->revision, hdr->image_id, hdr->image_base, hdr->image_size);
65 printk(BIOS_DEBUG, "\tConfig region 0x%zx + 0x%zx\n",
66 hdr->cfg_region_offset, hdr->cfg_region_size);
67
68 if (hdr->image_attribute & FSP_HDR_ATTRIB_FSPM) {
69 printk(BIOS_DEBUG, "\tMemory init offset 0x%zx\n",
70 hdr->memory_init_entry_offset);
71 }
72
73 if (hdr->image_attribute & FSP_HDR_ATTRIB_FSPS) {
74 printk(BIOS_DEBUG, "\tSilicon init offset 0x%zx\n",
75 hdr->silicon_init_entry_offset);
76 printk(BIOS_DEBUG, "\tNotify phase offset 0x%zx\n",
77 hdr->notify_phase_entry_offset);
78 }
79
80}
81
Aaron Durbin9b28f0022016-03-08 11:20:53 -060082/* TODO: this won't work for SoC's that need to XIP certain modules. */
Andrey Petrov97389702016-02-25 14:15:37 -080083enum cb_err fsp_load_binary(struct fsp_header *hdr,
84 const char *name,
85 struct range_entry *range)
86{
87 struct cbfsf file_desc;
88 struct region_device file_data;
89 void *membase;
90
91 if (cbfs_boot_locate(&file_desc, name, NULL)) {
92 printk(BIOS_ERR, "Could not locate %s in CBFS\n", name);
93 return CB_ERR;
94 }
95
96 cbfs_file_data(&file_data, &file_desc);
97
98 /* Map just enough of the file to be able to parse the header. */
99 membase = rdev_mmap(&file_data, FSP_HDR_OFFSET, FSP_HDR_LEN);
Aaron Durbin40672be2016-03-08 11:01:17 -0600100
101 if (membase == NULL) {
102 printk(BIOS_ERR, "Could not mmap() '%s' FSP header.\n", name);
103 return CB_ERR;
104 }
105
Andrey Petrov97389702016-02-25 14:15:37 -0800106 if (fsp_identify(hdr, membase) != CB_SUCCESS) {
Aaron Durbin40672be2016-03-08 11:01:17 -0600107 rdev_munmap(&file_data, membase);
Andrey Petrov97389702016-02-25 14:15:37 -0800108 printk(BIOS_ERR, "%s did not have a valid FSP header\n", name);
109 return CB_ERR;
110 }
111
Aaron Durbin40672be2016-03-08 11:01:17 -0600112 rdev_munmap(&file_data, membase);
113
Andrey Petrov97389702016-02-25 14:15:37 -0800114 fsp_print_header_info(hdr);
115
116 /* Check if size specified in the header matches the cbfs file size */
117 if (region_device_sz(&file_data) < hdr->image_size) {
118 printk(BIOS_ERR, "%s size bigger than cbfs file.\n", name);
119 return CB_ERR;
120 }
121
122 /* Check if the binary load address is within expected range */
Aaron Durbin9b28f0022016-03-08 11:20:53 -0600123 /* TODO: this doesn't check the current running program footprint. */
Andrey Petrov97389702016-02-25 14:15:37 -0800124 if (range_entry_base(range) > hdr->image_base ||
125 range_entry_end(range) <= hdr->image_base + hdr->image_size) {
126 printk(BIOS_ERR, "%s is outside of allowed range\n", name);
127 return CB_ERR;
128 }
129
130 /* Load binary into memory. */
131 if (rdev_readat(&file_data, (void *)hdr->image_base, 0, hdr->image_size) < 0)
132 return CB_ERR;
133
Aaron Durbina33c6d72016-03-31 12:48:30 -0500134 /* Signal that FSP component has been loaded. */
135 arch_segment_loaded(hdr->image_base, hdr->image_size, SEG_FINAL);
136
Andrey Petrov97389702016-02-25 14:15:37 -0800137 return CB_SUCCESS;
138}