blob: 631a834160309d318845f3e3b4109ec14a45c27c [file] [log] [blame]
Andrey Petrov5672dcd2016-02-12 15:12:43 -08001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2015 Intel Corp.
5 * (Written by Andrey Petrov <andrey.petrov@intel.com> for Intel Corp.)
6 * (Written by Alexandru Gagniuc <alexandrux.gagniuc@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.
Martin Rothebabfad2016-04-10 11:09:16 -060012 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
Andrey Petrov5672dcd2016-02-12 15:12:43 -080017 */
18
19#include <boot_device.h>
20#include <cbfs.h>
21#include <commonlib/region.h>
22#include <console/console.h>
23#include <fmap.h>
Barnali Sarkare70142c2017-03-28 16:32:33 +053024#include <intelblocks/fast_spi.h>
Andrey Petrov5672dcd2016-02-12 15:12:43 -080025
26/*
Furquan Shaikh0be3da52016-06-19 23:20:43 -070027 * BIOS region on the flash is mapped right below 4GiB in the address
28 * space. However, 256KiB right below 4GiB is decoded by read-only SRAM and not
29 * boot media.
30 *
Lee Leahy07441b52017-03-09 10:59:25 -080031 * +-----------+ 0
32 * | |
33 * | |
34 * | |
35 * | |
36 * | |
37 * | |
38 * | |
39 * | |
40 * +--------+ | |
41 * | IFD | | |
42 * bios_start +---> +--------+------------------> +-----------+ 4GiB - bios_size
43 * ^ | | ^ | |
44 * | | | | | |
45 * | | | bios_mapped_size | BIOS |
46 * | | BIOS | | | |
47 * bios_size | | | | |
48 * | | | v | |
49 * | | +------------------> +-----------+ 4GiB - 256KiB
50 * | | | | Read only |
51 * v | | | SRAM |
52 * bios_end +---> +--------+ +-----------+ 4GiB
53 * | Device |
54 * | ext |
55 * +--------+
Furquan Shaikh0be3da52016-06-19 23:20:43 -070056 *
Andrey Petrov5672dcd2016-02-12 15:12:43 -080057 */
Andrey Petrov5672dcd2016-02-12 15:12:43 -080058
Arthur Heymans6d6945b2018-12-29 14:00:46 +010059static size_t bios_size;
Andrey Petrov5672dcd2016-02-12 15:12:43 -080060
Arthur Heymans6d6945b2018-12-29 14:00:46 +010061static struct mem_region_device shadow_dev;
62static struct xlate_region_device real_dev;
Furquan Shaikh0be3da52016-06-19 23:20:43 -070063
64static void bios_mmap_init(void)
65{
Barnali Sarkare70142c2017-03-28 16:32:33 +053066 size_t size, start, bios_mapped_size;
Subrata Banikc7908502017-03-23 11:52:59 +053067 uintptr_t base;
Furquan Shaikh0be3da52016-06-19 23:20:43 -070068
Arthur Heymans6d6945b2018-12-29 14:00:46 +010069 size = bios_size;
Furquan Shaikh0be3da52016-06-19 23:20:43 -070070
71 /* If bios_size is initialized, then bail out. */
72 if (size != 0)
73 return;
Barnali Sarkare70142c2017-03-28 16:32:33 +053074 start = fast_spi_get_bios_region(&size);
Furquan Shaikh0be3da52016-06-19 23:20:43 -070075
Furquan Shaikh0be3da52016-06-19 23:20:43 -070076 /* BIOS region is mapped right below 4G. */
77 base = 4ULL * GiB - size;
78
79 /*
80 * The 256 KiB right below 4G are decoded by readonly SRAM,
81 * not boot media.
82 */
83 bios_mapped_size = size - 256 * KiB;
84
Arthur Heymans6d6945b2018-12-29 14:00:46 +010085 mem_region_device_ro_init(&shadow_dev, (void *)base,
Furquan Shaikh0be3da52016-06-19 23:20:43 -070086 bios_mapped_size);
87
Arthur Heymans6d6945b2018-12-29 14:00:46 +010088 xlate_region_device_ro_init(&real_dev, &shadow_dev.rdev,
Furquan Shaikh0be3da52016-06-19 23:20:43 -070089 start, bios_mapped_size,
90 CONFIG_ROM_SIZE);
91
Arthur Heymans6d6945b2018-12-29 14:00:46 +010092 bios_size = size;
Furquan Shaikh0be3da52016-06-19 23:20:43 -070093}
Andrey Petrov5672dcd2016-02-12 15:12:43 -080094
95const struct region_device *boot_device_ro(void)
96{
Furquan Shaikh0be3da52016-06-19 23:20:43 -070097 bios_mmap_init();
98
Arthur Heymans6d6945b2018-12-29 14:00:46 +010099 return &real_dev.rdev;
Andrey Petrov5672dcd2016-02-12 15:12:43 -0800100}
101
102static int iafw_boot_region_properties(struct cbfs_props *props)
103{
Nico Huber56dd2d62018-12-05 18:06:17 +0100104 struct region *real_dev_reg;
Andrey Petrov5672dcd2016-02-12 15:12:43 -0800105 struct region regn;
106
107 /* use fmap to locate CBFS area */
108 if (fmap_locate_area("COREBOOT", &regn))
109 return -1;
110
111 props->offset = region_offset(&regn);
112 props->size = region_sz(&regn);
113
Nico Huber56dd2d62018-12-05 18:06:17 +0100114 /* Check that we are within the memory mapped area. It's too
115 easy to forget the SRAM mapping when crafting an FMAP file. */
Arthur Heymans6d6945b2018-12-29 14:00:46 +0100116 real_dev_reg = &real_dev.sub_region;
Nico Huber56dd2d62018-12-05 18:06:17 +0100117 if (region_is_subregion(real_dev_reg, &regn)) {
118 printk(BIOS_DEBUG, "CBFS @ %zx size %zx\n",
119 props->offset, props->size);
120 } else {
121 printk(BIOS_CRIT,
122 "ERROR: CBFS @ %zx size %zx exceeds mem-mapped area @ %zx size %zx\n",
123 props->offset, props->size,
124 region_offset(real_dev_reg), region_sz(real_dev_reg));
125 }
Andrey Petrov5672dcd2016-02-12 15:12:43 -0800126
127 return 0;
128}
129
130/*
Julius Werner32e13c02019-11-08 13:06:20 -0800131 * Named cbfs_default_locator so that it overrides the default, but incompatible
132 * locator in cbfs.c
Andrey Petrov5672dcd2016-02-12 15:12:43 -0800133 */
Julius Werner32e13c02019-11-08 13:06:20 -0800134const struct cbfs_locator cbfs_default_locator = {
Andrey Petrov5672dcd2016-02-12 15:12:43 -0800135 .name = "IAFW Locator",
136 .locate = iafw_boot_region_properties,
137};