blob: b47b898ab83ac79d56fb91b1a9ec27eebcaeda5b [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 Durbin9b28f0022016-03-08 11:20:53 -0600107/* TODO: this won't work for SoC's that need to XIP certain modules. */
Andrey Petrov97389702016-02-25 14:15:37 -0800108enum cb_err fsp_load_binary(struct fsp_header *hdr,
109 const char *name,
110 struct range_entry *range)
111{
112 struct cbfsf file_desc;
113 struct region_device file_data;
114 void *membase;
115
116 if (cbfs_boot_locate(&file_desc, name, NULL)) {
117 printk(BIOS_ERR, "Could not locate %s in CBFS\n", name);
118 return CB_ERR;
119 }
120
121 cbfs_file_data(&file_data, &file_desc);
122
123 /* Map just enough of the file to be able to parse the header. */
124 membase = rdev_mmap(&file_data, FSP_HDR_OFFSET, FSP_HDR_LEN);
Aaron Durbin40672be2016-03-08 11:01:17 -0600125
126 if (membase == NULL) {
127 printk(BIOS_ERR, "Could not mmap() '%s' FSP header.\n", name);
128 return CB_ERR;
129 }
130
Andrey Petrov97389702016-02-25 14:15:37 -0800131 if (fsp_identify(hdr, membase) != CB_SUCCESS) {
Aaron Durbin40672be2016-03-08 11:01:17 -0600132 rdev_munmap(&file_data, membase);
Andrey Petrov97389702016-02-25 14:15:37 -0800133 printk(BIOS_ERR, "%s did not have a valid FSP header\n", name);
134 return CB_ERR;
135 }
136
Aaron Durbin40672be2016-03-08 11:01:17 -0600137 rdev_munmap(&file_data, membase);
138
Andrey Petrov97389702016-02-25 14:15:37 -0800139 fsp_print_header_info(hdr);
140
141 /* Check if size specified in the header matches the cbfs file size */
142 if (region_device_sz(&file_data) < hdr->image_size) {
143 printk(BIOS_ERR, "%s size bigger than cbfs file.\n", name);
144 return CB_ERR;
145 }
146
147 /* Check if the binary load address is within expected range */
Aaron Durbin9b28f0022016-03-08 11:20:53 -0600148 /* TODO: this doesn't check the current running program footprint. */
Andrey Petrov97389702016-02-25 14:15:37 -0800149 if (range_entry_base(range) > hdr->image_base ||
150 range_entry_end(range) <= hdr->image_base + hdr->image_size) {
151 printk(BIOS_ERR, "%s is outside of allowed range\n", name);
152 return CB_ERR;
153 }
154
155 /* Load binary into memory. */
156 if (rdev_readat(&file_data, (void *)hdr->image_base, 0, hdr->image_size) < 0)
157 return CB_ERR;
158
Aaron Durbina33c6d72016-03-31 12:48:30 -0500159 /* Signal that FSP component has been loaded. */
Aaron Durbin096f4572016-03-31 13:49:00 -0500160 prog_segment_loaded(hdr->image_base, hdr->image_size, SEG_FINAL);
Aaron Durbina33c6d72016-03-31 12:48:30 -0500161
Andrey Petrov97389702016-02-25 14:15:37 -0800162 return CB_SUCCESS;
163}
Andrey Petrov901e43c2016-06-22 19:22:30 -0700164
165void fsp_handle_reset(enum fsp_status status)
166{
167 switch(status) {
168 case FSP_STATUS_RESET_REQUIRED_COLD:
169 hard_reset();
170 break;
171 case FSP_STATUS_RESET_REQUIRED_WARM:
172 soft_reset();
173 break;
174 case FSP_STATUS_RESET_REQUIRED_GLOBAL_RESET:
175 global_reset();
176 break;
177 default:
178 break;
179 }
180}
181
182bool fsp_reset_requested(enum fsp_status status)
183{
184 return (status >= FSP_STATUS_RESET_REQUIRED_COLD &&
185 status <= FSP_STATUS_RESET_REQUIRED_GLOBAL_RESET);
186}