blob: 942ea09e1b37836d445d7069aa34a9a0f2412b93 [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 Petrov901e43c2016-06-22 19:22:30 -070021#include <reset.h>
Andrey Petrov97389702016-02-25 14:15:37 -080022#include <string.h>
23
24static bool looks_like_fsp_header(const uint8_t *raw_hdr)
25{
26 if (memcmp(raw_hdr, FSP_HDR_SIGNATURE, 4)) {
27 printk(BIOS_ALERT, "Did not find a valid FSP signature\n");
28 return false;
29 }
30
31 if (read32(raw_hdr + 4) != FSP_HDR_LEN) {
32 printk(BIOS_ALERT, "FSP header has invalid length\n");
33 return false;
34 }
35
36 return true;
37}
38
39enum cb_err fsp_identify(struct fsp_header *hdr, const void *fsp_blob)
40{
41 const uint8_t *raw_hdr = fsp_blob;
42
43 if (!looks_like_fsp_header(raw_hdr))
44 return CB_ERR;
45
Andrey Petrovd5a6eb42016-05-04 17:30:16 -070046 hdr->spec_version = read8(raw_hdr + 10);
Andrey Petrov97389702016-02-25 14:15:37 -080047 hdr->revision = read8(raw_hdr + 11);
48 hdr->fsp_revision = read32(raw_hdr + 12);
49 memcpy(hdr->image_id, raw_hdr + 16, ARRAY_SIZE(hdr->image_id));
50 hdr->image_id[ARRAY_SIZE(hdr->image_id) - 1] = '\0';
51 hdr->image_size = read32(raw_hdr + 24);
52 hdr->image_base = read32(raw_hdr + 28);
Andrey Petrovd5a6eb42016-05-04 17:30:16 -070053 hdr->image_attribute = read16(raw_hdr + 32);
54 hdr->component_attribute = read16(raw_hdr + 34);
Andrey Petrov97389702016-02-25 14:15:37 -080055 hdr->cfg_region_offset = read32(raw_hdr + 36);
56 hdr->cfg_region_size = read32(raw_hdr + 40);
57 hdr->notify_phase_entry_offset = read32(raw_hdr + 56);
58 hdr->memory_init_entry_offset = read32(raw_hdr + 60);
59 hdr->silicon_init_entry_offset = read32(raw_hdr + 68);
60
61 return CB_SUCCESS;
62}
63
64void fsp_print_header_info(const struct fsp_header *hdr)
65{
Andrey Petrovd5a6eb42016-05-04 17:30:16 -070066 union {
67 uint32_t val;
68 struct {
69 uint8_t bld_num;
70 uint8_t revision;
71 uint8_t minor;
72 uint8_t major;
73 } rev;
74 } revision;
75
76 revision.val = hdr->fsp_revision;
77
78 printk(BIOS_DEBUG, "Spec version: v%u.%u\n", (hdr->spec_version >> 4 ),
79 hdr->spec_version & 0xf);
80 printk(BIOS_DEBUG, "Revision: %u.%u.%u, Build Number %u\n",
81 revision.rev.major,
82 revision.rev.minor,
83 revision.rev.revision,
84 revision.rev.bld_num);
85 printk(BIOS_DEBUG, "Type: %s/%s\n",
86 (hdr->component_attribute & 1 ) ? "release" : "debug",
87 (hdr->component_attribute & 2 ) ? "test" : "official");
88 printk(BIOS_DEBUG, "image ID: %s, base 0x%lx + 0x%zx\n",
89 hdr->image_id, hdr->image_base, hdr->image_size);
Andrey Petrov97389702016-02-25 14:15:37 -080090 printk(BIOS_DEBUG, "\tConfig region 0x%zx + 0x%zx\n",
91 hdr->cfg_region_offset, hdr->cfg_region_size);
92
Andrey Petrovd5a6eb42016-05-04 17:30:16 -070093 if ((hdr->component_attribute >> 12) == FSP_HDR_ATTRIB_FSPM) {
Andrey Petrov97389702016-02-25 14:15:37 -080094 printk(BIOS_DEBUG, "\tMemory init offset 0x%zx\n",
95 hdr->memory_init_entry_offset);
96 }
97
Andrey Petrovd5a6eb42016-05-04 17:30:16 -070098 if ((hdr->component_attribute >> 12) == FSP_HDR_ATTRIB_FSPS) {
Andrey Petrov97389702016-02-25 14:15:37 -080099 printk(BIOS_DEBUG, "\tSilicon init offset 0x%zx\n",
100 hdr->silicon_init_entry_offset);
101 printk(BIOS_DEBUG, "\tNotify phase offset 0x%zx\n",
102 hdr->notify_phase_entry_offset);
103 }
104
105}
106
Aaron Durbina413e5e2016-07-17 23:06:03 -0500107enum cb_err fsp_validate_component(struct fsp_header *hdr,
108 const struct region_device *rdev)
109{
110 void *membase;
111
112 /* Map just enough of the file to be able to parse the header. */
113 membase = rdev_mmap(rdev, FSP_HDR_OFFSET, FSP_HDR_LEN);
114
115 if (membase == NULL) {
116 printk(BIOS_ERR, "Could not mmap() FSP header.\n");
117 return CB_ERR;
118 }
119
120 if (fsp_identify(hdr, membase) != CB_SUCCESS) {
121 rdev_munmap(rdev, membase);
122 printk(BIOS_ERR, "No valid FSP header\n");
123 return CB_ERR;
124 }
125
126 rdev_munmap(rdev, membase);
127
128 fsp_print_header_info(hdr);
129
130 /* Check if size specified in the header matches the cbfs file size */
131 if (region_device_sz(rdev) < hdr->image_size) {
132 printk(BIOS_ERR, "Component size bigger than cbfs file.\n");
133 return CB_ERR;
134 }
135
136 return CB_SUCCESS;
137}
138
Aaron Durbin9b28f0022016-03-08 11:20:53 -0600139/* TODO: this won't work for SoC's that need to XIP certain modules. */
Andrey Petrov97389702016-02-25 14:15:37 -0800140enum cb_err fsp_load_binary(struct fsp_header *hdr,
141 const char *name,
142 struct range_entry *range)
143{
144 struct cbfsf file_desc;
145 struct region_device file_data;
Andrey Petrov97389702016-02-25 14:15:37 -0800146
147 if (cbfs_boot_locate(&file_desc, name, NULL)) {
148 printk(BIOS_ERR, "Could not locate %s in CBFS\n", name);
149 return CB_ERR;
150 }
151
152 cbfs_file_data(&file_data, &file_desc);
153
Aaron Durbina413e5e2016-07-17 23:06:03 -0500154 if (fsp_validate_component(hdr, &file_data) != CB_SUCCESS)
Aaron Durbin40672be2016-03-08 11:01:17 -0600155 return CB_ERR;
Aaron Durbin40672be2016-03-08 11:01:17 -0600156
Aaron Durbina413e5e2016-07-17 23:06:03 -0500157 /* Check if the component load address is within expected range */
Aaron Durbin9b28f0022016-03-08 11:20:53 -0600158 /* TODO: this doesn't check the current running program footprint. */
Andrey Petrov97389702016-02-25 14:15:37 -0800159 if (range_entry_base(range) > hdr->image_base ||
160 range_entry_end(range) <= hdr->image_base + hdr->image_size) {
161 printk(BIOS_ERR, "%s is outside of allowed range\n", name);
162 return CB_ERR;
163 }
164
165 /* Load binary into memory. */
166 if (rdev_readat(&file_data, (void *)hdr->image_base, 0, hdr->image_size) < 0)
167 return CB_ERR;
168
Aaron Durbina33c6d72016-03-31 12:48:30 -0500169 /* Signal that FSP component has been loaded. */
Aaron Durbin096f4572016-03-31 13:49:00 -0500170 prog_segment_loaded(hdr->image_base, hdr->image_size, SEG_FINAL);
Aaron Durbina33c6d72016-03-31 12:48:30 -0500171
Andrey Petrov97389702016-02-25 14:15:37 -0800172 return CB_SUCCESS;
173}
Andrey Petrov901e43c2016-06-22 19:22:30 -0700174
175void fsp_handle_reset(enum fsp_status status)
176{
177 switch(status) {
178 case FSP_STATUS_RESET_REQUIRED_COLD:
179 hard_reset();
180 break;
181 case FSP_STATUS_RESET_REQUIRED_WARM:
182 soft_reset();
183 break;
184 case FSP_STATUS_RESET_REQUIRED_GLOBAL_RESET:
185 global_reset();
186 break;
187 default:
188 break;
189 }
190}
191
192bool fsp_reset_requested(enum fsp_status status)
193{
194 return (status >= FSP_STATUS_RESET_REQUIRED_COLD &&
195 status <= FSP_STATUS_RESET_REQUIRED_GLOBAL_RESET);
196}