• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #include <boot_device.h>
4 #include <commonlib/region.h>
5 #include <console/console.h>
6 #include <fmap.h>
7 #include <intelblocks/fast_spi.h>
8 #include <spi_flash.h>
9 
10 /*
11  * BIOS region on the flash is mapped right below 4GiB in the address
12  * space. However, 256KiB right below 4GiB is decoded by read-only SRAM and not
13  * boot media.
14  *
15  *                                                +-----------+ 0
16  *                                                |           |
17  *                                                |           |
18  *                                                |           |
19  *                                                |           |
20  *                                                |           |
21  *                                                |           |
22  *                                                |           |
23  *                                                |           |
24  *                  +--------+                    |           |
25  *                  |  IFD   |                    |           |
26  * bios_start +---> +--------+------------------> +-----------+ 4GiB - bios_size
27  *     ^            |        |          ^         |           |
28  *     |            |        |          |         |           |
29  *     |            |        |  bios_mapped_size  |    BIOS   |
30  *     |            |  BIOS  |          |         |           |
31  * bios_size        |        |          |         |           |
32  *     |            |        |          v         |           |
33  *     |            |        +------------------> +-----------+ 4GiB - 256KiB
34  *     |            |        |                    | Read only |
35  *     v            |        |                    |    SRAM   |
36  * bios_end   +---> +--------+                    +-----------+ 4GiB
37  *                  | Device |
38  *                  |   ext  |
39  *                  +--------+
40  *
41  */
42 
43 static size_t bios_size;
44 
45 static struct region_device shadow_dev;
46 static struct xlate_region_device real_dev;
47 static struct xlate_window real_dev_window;
48 
bios_mmap_init(void)49 static void bios_mmap_init(void)
50 {
51 	size_t size, start, bios_mapped_size;
52 	uintptr_t base;
53 
54 	size = bios_size;
55 
56 	/* If bios_size is initialized, then bail out. */
57 	if (size != 0)
58 		return;
59 	start = fast_spi_get_bios_region(&size);
60 
61 	/* BIOS region is mapped right below 4G. */
62 	base = 4ULL * GiB - size;
63 
64 	/*
65 	 * The 256 KiB right below 4G are decoded by readonly SRAM,
66 	 * not boot media.
67 	 */
68 	bios_mapped_size = size - 256 * KiB;
69 
70 	rdev_chain_mem(&shadow_dev, (void *)base, bios_mapped_size);
71 
72 	xlate_window_init(&real_dev_window, &shadow_dev, start, bios_mapped_size);
73 	xlate_region_device_ro_init(&real_dev, 1, &real_dev_window, CONFIG_ROM_SIZE);
74 
75 	bios_size = size;
76 
77 	/* Check that the CBFS lies within the memory mapped area. It's too
78 	   easy to forget the SRAM mapping when crafting an FMAP file. */
79 	struct region cbfs_region;
80 	if (!fmap_locate_area("COREBOOT", &cbfs_region) &&
81 	    !region_is_subregion(&real_dev_window.sub_region, &cbfs_region))
82 		printk(BIOS_CRIT,
83 		       "ERROR: CBFS @ %zx size %zx exceeds mem-mapped area @ %zx size %zx\n",
84 		       region_offset(&cbfs_region), region_sz(&cbfs_region),
85 		       start, bios_mapped_size);
86 }
87 
boot_device_ro(void)88 const struct region_device *boot_device_ro(void)
89 {
90 	bios_mmap_init();
91 
92 	return &real_dev.rdev;
93 }
94 
spi_flash_get_mmap_windows(struct flash_mmap_window * table)95 uint32_t spi_flash_get_mmap_windows(struct flash_mmap_window *table)
96 {
97 	bios_mmap_init();
98 
99 	table->flash_base = region_offset(&real_dev_window.sub_region);
100 	table->host_base = (uintptr_t)rdev_mmap_full(&shadow_dev);
101 	table->size = region_sz(&real_dev_window.sub_region);
102 
103 	return 1;
104 }
105