Ronald G. Minnich | e0e784a | 2014-11-26 19:25:47 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
Aaron Durbin | c6588c5 | 2015-05-15 13:15:34 -0500 | [diff] [blame^] | 4 | * Copyright 2015 Google Inc. |
Ronald G. Minnich | e0e784a | 2014-11-26 19:25:47 +0000 | [diff] [blame] | 5 | * |
| 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 Georgi | b890a12 | 2015-03-26 15:17:45 +0100 | [diff] [blame] | 18 | * Foundation, Inc. |
Ronald G. Minnich | e0e784a | 2014-11-26 19:25:47 +0000 | [diff] [blame] | 19 | */ |
Aaron Durbin | c6588c5 | 2015-05-15 13:15:34 -0500 | [diff] [blame^] | 20 | #include <boot_device.h> |
Ronald G. Minnich | e0e784a | 2014-11-26 19:25:47 +0000 | [diff] [blame] | 21 | #include <cbfs.h> |
Aaron Durbin | c6588c5 | 2015-05-15 13:15:34 -0500 | [diff] [blame^] | 22 | #include <console/console.h> |
Ronald G. Minnich | e0e784a | 2014-11-26 19:25:47 +0000 | [diff] [blame] | 23 | #include <string.h> |
| 24 | |
Aaron Durbin | c6588c5 | 2015-05-15 13:15:34 -0500 | [diff] [blame^] | 25 | /* This assumes that the CBFS resides at 0x0, which is true for the default |
| 26 | * configuration. */ |
| 27 | static const struct mem_region_device gboot_dev = |
| 28 | MEM_REGION_DEV_INIT(NULL, CONFIG_ROM_SIZE); |
Ronald G. Minnich | e0e784a | 2014-11-26 19:25:47 +0000 | [diff] [blame] | 29 | |
Aaron Durbin | c6588c5 | 2015-05-15 13:15:34 -0500 | [diff] [blame^] | 30 | const struct region_device *boot_device_ro(void) |
| 31 | { |
| 32 | return &gboot_dev.rdev; |
| 33 | } |
Ronald G. Minnich | e0e784a | 2014-11-26 19:25:47 +0000 | [diff] [blame] | 34 | |
| 35 | static int rom_media_open(struct cbfs_media *media) { |
| 36 | return 0; |
| 37 | } |
| 38 | |
| 39 | static void *rom_media_map(struct cbfs_media *media, size_t offset, size_t count) { |
Aaron Durbin | c6588c5 | 2015-05-15 13:15:34 -0500 | [diff] [blame^] | 40 | const struct region_device *boot_dev; |
Ronald G. Minnich | e0e784a | 2014-11-26 19:25:47 +0000 | [diff] [blame] | 41 | void *ptr; |
Ronald G. Minnich | e0e784a | 2014-11-26 19:25:47 +0000 | [diff] [blame] | 42 | |
Aaron Durbin | c6588c5 | 2015-05-15 13:15:34 -0500 | [diff] [blame^] | 43 | 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. Minnich | e0e784a | 2014-11-26 19:25:47 +0000 | [diff] [blame] | 51 | return ptr; |
| 52 | } |
| 53 | |
| 54 | static void *rom_media_unmap(struct cbfs_media *media, const void *address) { |
Aaron Durbin | c6588c5 | 2015-05-15 13:15:34 -0500 | [diff] [blame^] | 55 | const struct region_device *boot_dev; |
| 56 | |
| 57 | boot_dev = media->context; |
| 58 | |
| 59 | rdev_munmap(boot_dev, (void *)address); |
| 60 | |
Ronald G. Minnich | e0e784a | 2014-11-26 19:25:47 +0000 | [diff] [blame] | 61 | return NULL; |
| 62 | } |
| 63 | |
| 64 | static size_t rom_media_read(struct cbfs_media *media, void *dest, size_t offset, |
| 65 | size_t count) { |
Aaron Durbin | c6588c5 | 2015-05-15 13:15:34 -0500 | [diff] [blame^] | 66 | 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. Minnich | e0e784a | 2014-11-26 19:25:47 +0000 | [diff] [blame] | 73 | return count; |
| 74 | } |
| 75 | |
| 76 | static int rom_media_close(struct cbfs_media *media) { |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | static int init_rom_media_cbfs(struct cbfs_media *media) { |
Aaron Durbin | c6588c5 | 2015-05-15 13:15:34 -0500 | [diff] [blame^] | 81 | boot_device_init(); |
| 82 | media->context = (void *)boot_device_ro(); |
Ronald G. Minnich | e0e784a | 2014-11-26 19:25:47 +0000 | [diff] [blame] | 83 | 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 | |
| 91 | int init_default_cbfs_media(struct cbfs_media *media) { |
| 92 | return init_rom_media_cbfs(media); |
| 93 | } |