Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the libpayload project. |
| 3 | * |
| 4 | * Copyright (C) 2013 Google, Inc. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * 3. The name of the author may not be used to endorse or promote products |
| 15 | * derived from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 27 | * SUCH DAMAGE. |
| 28 | */ |
| 29 | #include <cbfs.h> |
Aaron Durbin | 580d11f | 2013-05-24 13:40:51 -0500 | [diff] [blame] | 30 | #include <cbfs_ram.h> |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 31 | #include <string.h> |
| 32 | #include <stdio.h> |
| 33 | #include <stdlib.h> |
| 34 | |
| 35 | // Implementation of a media source based on given memory buffer. |
| 36 | struct ram_media { |
| 37 | char *start; |
| 38 | size_t size; |
| 39 | }; |
| 40 | |
| 41 | static int ram_open(struct cbfs_media *media) { |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | static void *ram_map(struct cbfs_media *media, size_t offset, size_t count) { |
| 46 | struct ram_media *m = (struct ram_media*)media->context; |
Patrick Georgi | db2e3aa | 2013-03-09 10:52:50 +0100 | [diff] [blame] | 47 | /* assume addressing from top of image in this case */ |
| 48 | if (offset > 0xf0000000) { |
| 49 | offset = m->size + offset; |
| 50 | } |
| 51 | if (offset + count > m->size) { |
Stefan Reinauer | e21f5e1 | 2013-03-25 15:13:20 -0700 | [diff] [blame] | 52 | printf("ERROR: ram_map: request out of range (0x%zx+0x%zx)\n", |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 53 | offset, count); |
Julius Werner | 5142163 | 2015-01-06 21:34:19 -0800 | [diff] [blame] | 54 | return CBFS_MEDIA_INVALID_MAP_ADDRESS; |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 55 | } |
| 56 | return (void*)(m->start + offset); |
| 57 | } |
| 58 | |
| 59 | static void *ram_unmap(struct cbfs_media *media, const void *address) { |
| 60 | return NULL; |
| 61 | } |
| 62 | |
| 63 | static size_t ram_read(struct cbfs_media *media, void *dest, size_t offset, |
| 64 | size_t count) { |
| 65 | void *ptr = ram_map(media, offset, count); |
Julius Werner | 5142163 | 2015-01-06 21:34:19 -0800 | [diff] [blame] | 66 | if (ptr == CBFS_MEDIA_INVALID_MAP_ADDRESS) |
| 67 | return 0; |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 68 | memcpy(dest, ptr, count); |
| 69 | ram_unmap(media, ptr); |
| 70 | return count; |
| 71 | } |
| 72 | |
| 73 | static int ram_close(struct cbfs_media *media) { |
| 74 | return 0; |
| 75 | } |
| 76 | |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 77 | int init_cbfs_ram_media(struct cbfs_media *media, void *start, size_t size) { |
| 78 | // TODO Find a way to release unused media. Maybe adding media->destroy. |
| 79 | struct ram_media *m = (struct ram_media*)malloc(sizeof(*m)); |
| 80 | m->start = start; |
| 81 | m->size = size; |
| 82 | media->context = (void*)m; |
| 83 | media->open = ram_open; |
| 84 | media->close = ram_close; |
| 85 | media->map = ram_map; |
| 86 | media->unmap = ram_unmap; |
| 87 | media->read = ram_read; |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | // Legacy setup_cbfs_from_*. |
| 92 | static int is_default_cbfs_media_initialized; |
| 93 | static struct cbfs_media default_cbfs_media; |
| 94 | |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 95 | int setup_cbfs_from_ram(void *start, uint32_t size) { |
| 96 | int result = init_cbfs_ram_media(&default_cbfs_media, start, size); |
| 97 | if (result == 0) |
| 98 | is_default_cbfs_media_initialized = 1; |
| 99 | return result; |
| 100 | } |
| 101 | |
| 102 | extern int libpayload_init_default_cbfs_media(struct cbfs_media *media); |
Hung-Te Lin | d01d036 | 2013-01-25 12:42:40 +0800 | [diff] [blame] | 103 | int setup_cbfs_from_flash(void) { |
| 104 | int result = libpayload_init_default_cbfs_media(&default_cbfs_media); |
| 105 | if (result == 0) |
| 106 | is_default_cbfs_media_initialized = 1; |
| 107 | return result; |
| 108 | } |
| 109 | |
| 110 | int init_default_cbfs_media(struct cbfs_media *media) { |
| 111 | int result = 0; |
| 112 | if (is_default_cbfs_media_initialized != 1) { |
| 113 | result = setup_cbfs_from_flash(); |
| 114 | } |
| 115 | if (result == 0) |
| 116 | memcpy(media, &default_cbfs_media, sizeof(*media)); |
| 117 | return result; |
| 118 | } |