blob: f18030c49fdc6196b820fb62bdc9a4811810a47f [file] [log] [blame]
Ronald G. Minniche0e784a2014-11-26 19:25:47 +00001/*
2 * This file is part of the coreboot project.
3 *
Aaron Durbinc6588c52015-05-15 13:15:34 -05004 * Copyright 2015 Google Inc.
Ronald G. Minniche0e784a2014-11-26 19:25:47 +00005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; version 2 of
9 * the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
Patrick Georgib890a122015-03-26 15:17:45 +010018 * Foundation, Inc.
Ronald G. Minniche0e784a2014-11-26 19:25:47 +000019 */
Aaron Durbinc6588c52015-05-15 13:15:34 -050020#include <boot_device.h>
Ronald G. Minniche0e784a2014-11-26 19:25:47 +000021#include <cbfs.h>
Aaron Durbinc6588c52015-05-15 13:15:34 -050022#include <console/console.h>
Ronald G. Minniche0e784a2014-11-26 19:25:47 +000023#include <string.h>
24
Aaron Durbinc6588c52015-05-15 13:15:34 -050025/* This assumes that the CBFS resides at 0x0, which is true for the default
26 * configuration. */
27static const struct mem_region_device gboot_dev =
28 MEM_REGION_DEV_INIT(NULL, CONFIG_ROM_SIZE);
Ronald G. Minniche0e784a2014-11-26 19:25:47 +000029
Aaron Durbinc6588c52015-05-15 13:15:34 -050030const struct region_device *boot_device_ro(void)
31{
32 return &gboot_dev.rdev;
33}
Ronald G. Minniche0e784a2014-11-26 19:25:47 +000034
35static int rom_media_open(struct cbfs_media *media) {
36 return 0;
37}
38
39static void *rom_media_map(struct cbfs_media *media, size_t offset, size_t count) {
Aaron Durbinc6588c52015-05-15 13:15:34 -050040 const struct region_device *boot_dev;
Ronald G. Minniche0e784a2014-11-26 19:25:47 +000041 void *ptr;
Ronald G. Minniche0e784a2014-11-26 19:25:47 +000042
Aaron Durbinc6588c52015-05-15 13:15:34 -050043 printk(BIOS_INFO, "%s: media %p, offset %lx, size %ld.\n", __func__, media, offset, count);
44 boot_dev = media->context;
45
46 ptr = rdev_mmap(boot_dev, offset, count);
47
48 if (ptr == NULL)
49 return (void *)-1;
50
Ronald G. Minniche0e784a2014-11-26 19:25:47 +000051 return ptr;
52}
53
54static void *rom_media_unmap(struct cbfs_media *media, const void *address) {
Aaron Durbinc6588c52015-05-15 13:15:34 -050055 const struct region_device *boot_dev;
56
57 boot_dev = media->context;
58
59 rdev_munmap(boot_dev, (void *)address);
60
Ronald G. Minniche0e784a2014-11-26 19:25:47 +000061 return NULL;
62}
63
64static size_t rom_media_read(struct cbfs_media *media, void *dest, size_t offset,
65 size_t count) {
Aaron Durbinc6588c52015-05-15 13:15:34 -050066 const struct region_device *boot_dev;
67
68 boot_dev = media->context;
69
70 if (rdev_readat(boot_dev, dest, offset, count) < 0)
71 return 0;
72
Ronald G. Minniche0e784a2014-11-26 19:25:47 +000073 return count;
74}
75
76static int rom_media_close(struct cbfs_media *media) {
77 return 0;
78}
79
80static int init_rom_media_cbfs(struct cbfs_media *media) {
Aaron Durbinc6588c52015-05-15 13:15:34 -050081 boot_device_init();
82 media->context = (void *)boot_device_ro();
Ronald G. Minniche0e784a2014-11-26 19:25:47 +000083 media->open = rom_media_open;
84 media->close = rom_media_close;
85 media->map = rom_media_map;
86 media->unmap = rom_media_unmap;
87 media->read = rom_media_read;
88 return 0;
89}
90
91int init_default_cbfs_media(struct cbfs_media *media) {
92 return init_rom_media_cbfs(media);
93}