blob: 3d9abf0fbeeca9f79edbafcbfad89fc9e456fa55 [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);
54 return NULL;
55 }
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);
66 memcpy(dest, ptr, count);
67 ram_unmap(media, ptr);
68 return count;
69}
70
71static int ram_close(struct cbfs_media *media) {
72 return 0;
73}
74
Hung-Te Lind01d0362013-01-25 12:42:40 +080075int init_cbfs_ram_media(struct cbfs_media *media, void *start, size_t size) {
76 // TODO Find a way to release unused media. Maybe adding media->destroy.
77 struct ram_media *m = (struct ram_media*)malloc(sizeof(*m));
78 m->start = start;
79 m->size = size;
80 media->context = (void*)m;
81 media->open = ram_open;
82 media->close = ram_close;
83 media->map = ram_map;
84 media->unmap = ram_unmap;
85 media->read = ram_read;
86 return 0;
87}
88
89// Legacy setup_cbfs_from_*.
90static int is_default_cbfs_media_initialized;
91static struct cbfs_media default_cbfs_media;
92
Hung-Te Lind01d0362013-01-25 12:42:40 +080093int setup_cbfs_from_ram(void *start, uint32_t size) {
94 int result = init_cbfs_ram_media(&default_cbfs_media, start, size);
95 if (result == 0)
96 is_default_cbfs_media_initialized = 1;
97 return result;
98}
99
100extern int libpayload_init_default_cbfs_media(struct cbfs_media *media);
Hung-Te Lind01d0362013-01-25 12:42:40 +0800101int setup_cbfs_from_flash(void) {
102 int result = libpayload_init_default_cbfs_media(&default_cbfs_media);
103 if (result == 0)
104 is_default_cbfs_media_initialized = 1;
105 return result;
106}
107
108int init_default_cbfs_media(struct cbfs_media *media) {
109 int result = 0;
110 if (is_default_cbfs_media_initialized != 1) {
111 result = setup_cbfs_from_flash();
112 }
113 if (result == 0)
114 memcpy(media, &default_cbfs_media, sizeof(*media));
115 return result;
116}