blob: 2eb6cf9dbb080f7e5515c5ecedb14122271edc0c [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
Andrey Petrovd5a6eb42016-05-04 17:30:16 -070045 hdr->spec_version = read8(raw_hdr + 10);
Andrey Petrov97389702016-02-25 14:15:37 -080046 hdr->revision = read8(raw_hdr + 11);
47 hdr->fsp_revision = read32(raw_hdr + 12);
48 memcpy(hdr->image_id, raw_hdr + 16, ARRAY_SIZE(hdr->image_id));
49 hdr->image_id[ARRAY_SIZE(hdr->image_id) - 1] = '\0';
50 hdr->image_size = read32(raw_hdr + 24);
51 hdr->image_base = read32(raw_hdr + 28);
Andrey Petrovd5a6eb42016-05-04 17:30:16 -070052 hdr->image_attribute = read16(raw_hdr + 32);
53 hdr->component_attribute = read16(raw_hdr + 34);
Andrey Petrov97389702016-02-25 14:15:37 -080054 hdr->cfg_region_offset = read32(raw_hdr + 36);
55 hdr->cfg_region_size = read32(raw_hdr + 40);
56 hdr->notify_phase_entry_offset = read32(raw_hdr + 56);
57 hdr->memory_init_entry_offset = read32(raw_hdr + 60);
58 hdr->silicon_init_entry_offset = read32(raw_hdr + 68);
59
60 return CB_SUCCESS;
61}
62
63void fsp_print_header_info(const struct fsp_header *hdr)
64{
Andrey Petrovd5a6eb42016-05-04 17:30:16 -070065 union {
66 uint32_t val;
67 struct {
68 uint8_t bld_num;
69 uint8_t revision;
70 uint8_t minor;
71 uint8_t major;
72 } rev;
73 } revision;
74
75 revision.val = hdr->fsp_revision;
76
77 printk(BIOS_DEBUG, "Spec version: v%u.%u\n", (hdr->spec_version >> 4 ),
78 hdr->spec_version & 0xf);
79 printk(BIOS_DEBUG, "Revision: %u.%u.%u, Build Number %u\n",
80 revision.rev.major,
81 revision.rev.minor,
82 revision.rev.revision,
83 revision.rev.bld_num);
84 printk(BIOS_DEBUG, "Type: %s/%s\n",
85 (hdr->component_attribute & 1 ) ? "release" : "debug",
86 (hdr->component_attribute & 2 ) ? "test" : "official");
87 printk(BIOS_DEBUG, "image ID: %s, base 0x%lx + 0x%zx\n",
88 hdr->image_id, hdr->image_base, hdr->image_size);
Andrey Petrov97389702016-02-25 14:15:37 -080089 printk(BIOS_DEBUG, "\tConfig region 0x%zx + 0x%zx\n",
90 hdr->cfg_region_offset, hdr->cfg_region_size);
91
Andrey Petrovd5a6eb42016-05-04 17:30:16 -070092 if ((hdr->component_attribute >> 12) == FSP_HDR_ATTRIB_FSPM) {
Andrey Petrov97389702016-02-25 14:15:37 -080093 printk(BIOS_DEBUG, "\tMemory init offset 0x%zx\n",
94 hdr->memory_init_entry_offset);
95 }
96
Andrey Petrovd5a6eb42016-05-04 17:30:16 -070097 if ((hdr->component_attribute >> 12) == FSP_HDR_ATTRIB_FSPS) {
Andrey Petrov97389702016-02-25 14:15:37 -080098 printk(BIOS_DEBUG, "\tSilicon init offset 0x%zx\n",
99 hdr->silicon_init_entry_offset);
100 printk(BIOS_DEBUG, "\tNotify phase offset 0x%zx\n",
101 hdr->notify_phase_entry_offset);
102 }
103
104}
105
Aaron Durbin9b28f0022016-03-08 11:20:53 -0600106/* TODO: this won't work for SoC's that need to XIP certain modules. */
Andrey Petrov97389702016-02-25 14:15:37 -0800107enum cb_err fsp_load_binary(struct fsp_header *hdr,
108 const char *name,
109 struct range_entry *range)
110{
111 struct cbfsf file_desc;
112 struct region_device file_data;
113 void *membase;
114
115 if (cbfs_boot_locate(&file_desc, name, NULL)) {
116 printk(BIOS_ERR, "Could not locate %s in CBFS\n", name);
117 return CB_ERR;
118 }
119
120 cbfs_file_data(&file_data, &file_desc);
121
122 /* Map just enough of the file to be able to parse the header. */
123 membase = rdev_mmap(&file_data, FSP_HDR_OFFSET, FSP_HDR_LEN);
Aaron Durbin40672be2016-03-08 11:01:17 -0600124
125 if (membase == NULL) {
126 printk(BIOS_ERR, "Could not mmap() '%s' FSP header.\n", name);
127 return CB_ERR;
128 }
129
Andrey Petrov97389702016-02-25 14:15:37 -0800130 if (fsp_identify(hdr, membase) != CB_SUCCESS) {
Aaron Durbin40672be2016-03-08 11:01:17 -0600131 rdev_munmap(&file_data, membase);
Andrey Petrov97389702016-02-25 14:15:37 -0800132 printk(BIOS_ERR, "%s did not have a valid FSP header\n", name);
133 return CB_ERR;
134 }
135
Aaron Durbin40672be2016-03-08 11:01:17 -0600136 rdev_munmap(&file_data, membase);
137
Andrey Petrov97389702016-02-25 14:15:37 -0800138 fsp_print_header_info(hdr);
139
140 /* Check if size specified in the header matches the cbfs file size */
141 if (region_device_sz(&file_data) < hdr->image_size) {
142 printk(BIOS_ERR, "%s size bigger than cbfs file.\n", name);
143 return CB_ERR;
144 }
145
146 /* Check if the binary load address is within expected range */
Aaron Durbin9b28f0022016-03-08 11:20:53 -0600147 /* TODO: this doesn't check the current running program footprint. */
Andrey Petrov97389702016-02-25 14:15:37 -0800148 if (range_entry_base(range) > hdr->image_base ||
149 range_entry_end(range) <= hdr->image_base + hdr->image_size) {
150 printk(BIOS_ERR, "%s is outside of allowed range\n", name);
151 return CB_ERR;
152 }
153
154 /* Load binary into memory. */
155 if (rdev_readat(&file_data, (void *)hdr->image_base, 0, hdr->image_size) < 0)
156 return CB_ERR;
157
Aaron Durbina33c6d72016-03-31 12:48:30 -0500158 /* Signal that FSP component has been loaded. */
Aaron Durbin096f4572016-03-31 13:49:00 -0500159 prog_segment_loaded(hdr->image_base, hdr->image_size, SEG_FINAL);
Aaron Durbina33c6d72016-03-31 12:48:30 -0500160
Andrey Petrov97389702016-02-25 14:15:37 -0800161 return CB_SUCCESS;
162}