blob: d5469f68905b3ec6db24ee7ddcf55cc1b6daafc1 [file] [log] [blame]
Hung-Te Lind01d0362013-01-25 12:42:40 +08001/*
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 Durbin580d11f2013-05-24 13:40:51 -050030#include <cbfs_ram.h>
Hung-Te Lind01d0362013-01-25 12:42:40 +080031#include <string.h>
32#include <stdio.h>
33#include <stdlib.h>
34
35// Implementation of a media source based on given memory buffer.
36struct ram_media {
37 char *start;
38 size_t size;
39};
40
41static int ram_open(struct cbfs_media *media) {
42 return 0;
43}
44
45static void *ram_map(struct cbfs_media *media, size_t offset, size_t count) {
46 struct ram_media *m = (struct ram_media*)media->context;
Patrick Georgidb2e3aa2013-03-09 10:52:50 +010047 /* 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 Reinauere21f5e12013-03-25 15:13:20 -070052 printf("ERROR: ram_map: request out of range (0x%zx+0x%zx)\n",
Hung-Te Lind01d0362013-01-25 12:42:40 +080053 offset, count);
Julius Werner51421632015-01-06 21:34:19 -080054 return CBFS_MEDIA_INVALID_MAP_ADDRESS;
Hung-Te Lind01d0362013-01-25 12:42:40 +080055 }
56 return (void*)(m->start + offset);
57}
58
59static void *ram_unmap(struct cbfs_media *media, const void *address) {
60 return NULL;
61}
62
63static 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 Werner51421632015-01-06 21:34:19 -080066 if (ptr == CBFS_MEDIA_INVALID_MAP_ADDRESS)
67 return 0;
Hung-Te Lind01d0362013-01-25 12:42:40 +080068 memcpy(dest, ptr, count);
69 ram_unmap(media, ptr);
70 return count;
71}
72
73static int ram_close(struct cbfs_media *media) {
74 return 0;
75}
76
Hung-Te Lind01d0362013-01-25 12:42:40 +080077int 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_*.
92static int is_default_cbfs_media_initialized;
93static struct cbfs_media default_cbfs_media;
94
Hung-Te Lind01d0362013-01-25 12:42:40 +080095int 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
102extern int libpayload_init_default_cbfs_media(struct cbfs_media *media);
Hung-Te Lind01d0362013-01-25 12:42:40 +0800103int 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
110int 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}