coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
boot_device_rw_nommap.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <boot_device.h>
4 #include <spi_flash.h>
5 #include <spi-generic.h>
6 #include <stdint.h>
7 
8 static struct spi_flash sfg;
9 static bool sfg_init_done;
10 
11 static ssize_t spi_readat(const struct region_device *rd, void *b,
12  size_t offset, size_t size)
13 {
14  if (spi_flash_read(&sfg, offset, size, b))
15  return -1;
16 
17  return size;
18 }
19 
20 static ssize_t spi_writeat(const struct region_device *rd, const void *b,
21  size_t offset, size_t size)
22 {
23  if (spi_flash_write(&sfg, offset, size, b))
24  return -1;
25 
26  return size;
27 }
28 
29 static ssize_t spi_eraseat(const struct region_device *rd,
30  size_t offset, size_t size)
31 {
33  return -1;
34 
35  return size;
36 }
37 
38 static const struct region_device_ops spi_ops = {
39  .readat = spi_readat,
40  .writeat = spi_writeat,
41  .eraseat = spi_eraseat,
42 };
43 
44 static const struct region_device spi_rw =
45  REGION_DEV_INIT(&spi_ops, 0, CONFIG_ROM_SIZE);
46 
47 static void boot_device_rw_init(void)
48 {
49  const int bus = CONFIG_BOOT_DEVICE_SPI_FLASH_BUS;
50  const int cs = 0;
51 
52  if (sfg_init_done == true)
53  return;
54 
55  /* Ensure any necessary setup is performed by the drivers. */
56  spi_init();
57 
58  if (!spi_flash_probe(bus, cs, &sfg))
59  sfg_init_done = true;
60 }
61 
62 const struct region_device *boot_device_rw(void)
63 {
64  /* Probe for the SPI flash device if not already done. */
66 
67  if (sfg_init_done != true)
68  return NULL;
69 
70  return &spi_rw;
71 }
72 
73 const struct spi_flash *boot_device_spi_flash(void)
74 {
76 
77  if (sfg_init_done != true)
78  return NULL;
79 
80  return &sfg;
81 }
82 
83 int boot_device_wp_region(const struct region_device *rd,
84  const enum bootdev_prot_type type)
85 {
86  uint32_t ctrlr_pr;
87 
88  /* Ensure boot device has been initialized at least once. */
90 
91  const struct spi_flash *boot_dev = boot_device_spi_flash();
92 
93  if (boot_dev == NULL)
94  return -1;
95 
96  if (type == MEDIA_WP) {
98  region_device_region(rd)) != 1) {
101  if (CONFIG(BOOTMEDIA_SPI_LOCK_REBOOT))
103  else if (CONFIG(BOOTMEDIA_SPI_LOCK_PIN))
105  else if (CONFIG(BOOTMEDIA_SPI_LOCK_PERMANENT))
107 
110  }
111 
112  /* Already write protected */
113  return 0;
114  }
115 
116  switch (type) {
117  case CTRLR_WP:
118  ctrlr_pr = WRITE_PROTECT;
119  break;
120  case CTRLR_RP:
121  ctrlr_pr = READ_PROTECT;
122  break;
123  case CTRLR_RWP:
124  ctrlr_pr = READ_WRITE_PROTECT;
125  break;
126  default:
127  return -1;
128  }
129 
131  region_device_region(rd), ctrlr_pr);
132 }
static const struct mem_region_device boot_dev
Definition: rom_media.c:6
bootdev_prot_type
Definition: boot_device.h:18
@ CTRLR_RWP
Definition: boot_device.h:21
@ CTRLR_WP
Definition: boot_device.h:19
@ CTRLR_RP
Definition: boot_device.h:20
@ MEDIA_WP
Definition: boot_device.h:22
static const struct region_device_ops spi_ops
static ssize_t spi_readat(const struct region_device *rd, void *b, size_t offset, size_t size)
const struct region_device * boot_device_rw(void)
static void boot_device_rw_init(void)
const struct spi_flash * boot_device_spi_flash(void)
static bool sfg_init_done
int boot_device_wp_region(const struct region_device *rd, const enum bootdev_prot_type type)
static struct spi_flash sfg
static const struct region_device spi_rw
static ssize_t spi_writeat(const struct region_device *rd, const void *b, size_t offset, size_t size)
static ssize_t spi_eraseat(const struct region_device *rd, size_t offset, size_t size)
void boot_device_init(void)
Definition: cbfs_spi.c:82
@ CONFIG
Definition: dsi_common.h:201
static size_t offset
Definition: flashconsole.c:16
unsigned int type
Definition: edid.c:57
#define REGION_DEV_INIT(ops_, offset_, size_)
Definition: region.h:87
static const struct region * region_device_region(const struct region_device *rdev)
Definition: region.h:126
static void lock(void *unused)
Definition: lockdown.c:58
void __weak spi_init(void)
Definition: spi-generic.c:117
@ WRITE_PROTECT
Definition: spi-generic.h:108
@ READ_WRITE_PROTECT
Definition: spi-generic.h:109
@ READ_PROTECT
Definition: spi-generic.h:107
int spi_flash_ctrlr_protect_region(const struct spi_flash *flash, const struct region *region, const enum ctrlr_prot_type type)
Definition: spi_flash.c:719
int spi_flash_set_write_protected(const struct spi_flash *flash, const struct region *region, const enum spi_flash_status_reg_lockdown mode)
Definition: spi_flash.c:598
int spi_flash_probe(unsigned int bus, unsigned int cs, struct spi_flash *flash)
Definition: spi_flash.c:486
int spi_flash_read(const struct spi_flash *flash, u32 offset, size_t len, void *buf)
Definition: spi_flash.c:531
int spi_flash_is_write_protected(const struct spi_flash *flash, const struct region *region)
Definition: spi_flash.c:576
int spi_flash_write(const struct spi_flash *flash, u32 offset, size_t len, const void *buf)
Definition: spi_flash.c:537
int spi_flash_erase(const struct spi_flash *flash, u32 offset, size_t len)
Definition: spi_flash.c:553
spi_flash_status_reg_lockdown
Definition: spi_flash.h:28
@ SPI_WRITE_PROTECTION_PERMANENT
Definition: spi_flash.h:33
@ SPI_WRITE_PROTECTION_PIN
Definition: spi_flash.h:31
@ SPI_WRITE_PROTECTION_REBOOT
Definition: spi_flash.h:32
#define NULL
Definition: stddef.h:19
__SIZE_TYPE__ ssize_t
Definition: stddef.h:13
unsigned int uint32_t
Definition: stdint.h:14
Definition: device.h:76
ssize_t(* readat)(const struct region_device *, void *, size_t, size_t)
Definition: region.h:70
u32 size
Definition: spi_flash.h:95