coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
boot_dev.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include "psp_verstage.h"
4 
5 #include <bl_uapp/bl_errorcodes_public.h>
6 #include <bl_uapp/bl_syscall_public.h>
7 #include <boot_device.h>
8 #include <commonlib/region.h>
9 #include <console/console.h>
10 #include <string.h>
11 
12 #define DEST_BUF_ALIGNMENT 16
13 
14 static void *boot_dev_mmap(const struct region_device *rd, size_t offset, size_t size __unused)
15 {
16  const struct mem_region_device *mdev;
17 
18  mdev = container_of(rd, __typeof__(*mdev), rdev);
19 
20  return &(mdev->base[offset]);
21 }
22 
23 static int boot_dev_munmap(const struct region_device *rd __unused, void *mapping __unused)
24 {
25  return 0;
26 }
27 
28 static ssize_t boot_dev_dma_readat(const struct region_device *rd, void *dest,
29  size_t offset, size_t size)
30 {
31  size_t memcpy_size = ALIGN_UP((uintptr_t)dest, DEST_BUF_ALIGNMENT) - (uintptr_t)dest;
32  const struct mem_region_device *mdev = container_of(rd, __typeof__(*mdev), rdev);
33  int ret;
34 
35  if (memcpy_size > size)
36  memcpy_size = size;
37  /* Alignment requirement is only on dest buffer for CCP DMA. So do a memcpy
38  until the destination buffer alignment requirement is met. */
39  if (memcpy_size)
40  memcpy(dest, &(mdev->base[offset]), memcpy_size);
41 
42  dest = ((char *)dest + memcpy_size);
43  offset += memcpy_size;
44  size -= memcpy_size;
45  if (!size)
46  return memcpy_size;
47 
48  ret = svc_ccp_dma((uint32_t)offset, dest, (uint32_t)size);
49  if (ret != BL_OK) {
50  printk(BIOS_ERR, "%s: Failed dest:%p offset:%zu size:%zu ret:%d\n",
51  __func__, dest, offset, size, ret);
52  return -1;
53  }
54 
55  return size + memcpy_size;
56 }
57 
58 static ssize_t boot_dev_readat(const struct region_device *rd, void *dest,
59  size_t offset, size_t size)
60 {
61  const struct mem_region_device *mdev = container_of(rd, __typeof__(*mdev), rdev);
62 
63  if (CONFIG(PSP_VERSTAGE_CCP_DMA))
64  return boot_dev_dma_readat(rd, dest, offset, size);
65 
66  memcpy(dest, &(mdev->base[offset]), size);
67  return size;
68 }
69 
72  .munmap = boot_dev_munmap,
73  .readat = boot_dev_readat,
74 };
75 
76 static struct mem_region_device boot_dev = {
77  .rdev = REGION_DEV_INIT(&boot_dev_rdev_ro_ops, 0, CONFIG_ROM_SIZE),
78 };
79 
80 const struct region_device *boot_device_ro(void)
81 {
82  if (!boot_dev.base)
83  boot_dev.base = (void *)map_spi_rom();
84  return &boot_dev.rdev;
85 }
void * memcpy(void *dest, const void *src, size_t n)
Definition: memcpy.c:7
const struct region_device_ops boot_dev_rdev_ro_ops
Definition: boot_dev.c:70
static void * boot_dev_mmap(const struct region_device *rd, size_t offset, size_t size __unused)
Definition: boot_dev.c:14
static ssize_t boot_dev_readat(const struct region_device *rd, void *dest, size_t offset, size_t size)
Definition: boot_dev.c:58
static int boot_dev_munmap(const struct region_device *rd __unused, void *mapping __unused)
Definition: boot_dev.c:23
static ssize_t boot_dev_dma_readat(const struct region_device *rd, void *dest, size_t offset, size_t size)
Definition: boot_dev.c:28
const struct region_device * boot_device_ro(void)
Definition: boot_dev.c:80
static struct mem_region_device boot_dev
Definition: boot_dev.c:76
#define DEST_BUF_ALIGNMENT
Definition: boot_dev.c:12
#define ALIGN_UP(x, a)
Definition: helpers.h:17
static struct mmap_helper_region_device mdev
Definition: cbfs_spi.c:79
uint32_t svc_ccp_dma(uint32_t spi_rom_offset, void *dest, uint32_t size)
Definition: svc.c:130
uintptr_t * map_spi_rom(void)
Definition: fch.c:91
#define printk(level,...)
Definition: stdlib.h:16
@ CONFIG
Definition: dsi_common.h:201
static struct region_device rdev
Definition: flashconsole.c:14
static size_t offset
Definition: flashconsole.c:16
#define __unused
Definition: helpers.h:38
#define container_of(ptr, type, member)
container_of - cast a member of a structure out to the containing structure
Definition: helpers.h:33
#define BIOS_ERR
BIOS_ERR - System in incomplete state.
Definition: loglevel.h:72
#define REGION_DEV_INIT(ops_, offset_, size_)
Definition: region.h:87
__SIZE_TYPE__ ssize_t
Definition: stddef.h:13
unsigned int uint32_t
Definition: stdint.h:14
unsigned long uintptr_t
Definition: stdint.h:21
struct region_device rdev
Definition: region.h:184
void *(* mmap)(const struct region_device *, size_t, size_t)
Definition: region.h:68