6b13f685e
김민수
BSP 최초 추가
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
/*
* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include <cbfs.h>
#include <malloc.h>
#include <asm/byteorder.h>
enum cbfs_result file_cbfs_result;
const char *file_cbfs_error(void)
{
switch (file_cbfs_result) {
case CBFS_SUCCESS:
return "Success";
case CBFS_NOT_INITIALIZED:
return "CBFS not initialized";
case CBFS_BAD_HEADER:
return "Bad CBFS header";
case CBFS_BAD_FILE:
return "Bad CBFS file";
case CBFS_FILE_NOT_FOUND:
return "File not found";
default:
return "Unknown";
}
}
static const u32 good_magic = 0x4f524243;
static const u8 good_file_magic[] = "LARCHIVE";
static int initialized;
static struct cbfs_header cbfs_header;
static struct cbfs_cachenode *file_cache;
/* Do endian conversion on the CBFS header structure. */
static void swap_header(struct cbfs_header *dest, struct cbfs_header *src)
{
dest->magic = be32_to_cpu(src->magic);
dest->version = be32_to_cpu(src->version);
dest->rom_size = be32_to_cpu(src->rom_size);
dest->boot_block_size = be32_to_cpu(src->boot_block_size);
dest->align = be32_to_cpu(src->align);
dest->offset = be32_to_cpu(src->offset);
}
/* Do endian conversion on a CBFS file header. */
static void swap_file_header(struct cbfs_fileheader *dest,
const struct cbfs_fileheader *src)
{
memcpy(&dest->magic, &src->magic, sizeof(dest->magic));
dest->len = be32_to_cpu(src->len);
dest->type = be32_to_cpu(src->type);
dest->checksum = be32_to_cpu(src->checksum);
dest->offset = be32_to_cpu(src->offset);
}
/*
* Given a starting position in memory, scan forward, bounded by a size, and
* find the next valid CBFS file. No memory is allocated by this function. The
* caller is responsible for allocating space for the new file structure.
*
* @param start The location in memory to start from.
* @param size The size of the memory region to search.
* @param align The alignment boundaries to check on.
* @param newNode A pointer to the file structure to load.
* @param used A pointer to the count of of bytes scanned through,
* including the file if one is found.
*
* @return 1 if a file is found, 0 if one isn't.
*/
static int file_cbfs_next_file(u8 *start, u32 size, u32 align,
struct cbfs_cachenode *newNode, u32 *used)
{
struct cbfs_fileheader header;
*used = 0;
while (size >= align) {
const struct cbfs_fileheader *fileHeader =
(const struct cbfs_fileheader *)start;
u32 name_len;
u32 step;
/* Check if there's a file here. */
if (memcmp(good_file_magic, &(fileHeader->magic),
sizeof(fileHeader->magic))) {
*used += align;
size -= align;
start += align;
continue;
}
swap_file_header(&header, fileHeader);
if (header.offset < sizeof(const struct cbfs_cachenode *) ||
header.offset > header.len) {
file_cbfs_result = CBFS_BAD_FILE;
return -1;
}
newNode->next = NULL;
newNode->type = header.type;
newNode->data = start + header.offset;
newNode->data_length = header.len;
name_len = header.offset - sizeof(struct cbfs_cachenode *);
newNode->name = (char *)fileHeader +
sizeof(struct cbfs_cachenode *);
newNode->name_length = name_len;
newNode->checksum = header.checksum;
step = header.len;
if (step % align)
step = step + align - step % align;
*used += step;
return 1;
}
return 0;
}
/* Look through a CBFS instance and copy file metadata into regular memory. */
static void file_cbfs_fill_cache(u8 *start, u32 size, u32 align)
{
struct cbfs_cachenode *cache_node;
struct cbfs_cachenode *newNode;
struct cbfs_cachenode **cache_tail = &file_cache;
/* Clear out old information. */
cache_node = file_cache;
while (cache_node) {
struct cbfs_cachenode *oldNode = cache_node;
cache_node = cache_node->next;
free(oldNode);
}
file_cache = NULL;
while (size >= align) {
int result;
u32 used;
newNode = (struct cbfs_cachenode *)
malloc(sizeof(struct cbfs_cachenode));
result = file_cbfs_next_file(start, size, align,
newNode, &used);
if (result < 0) {
free(newNode);
return;
} else if (result == 0) {
free(newNode);
break;
}
*cache_tail = newNode;
cache_tail = &newNode->next;
size -= used;
start += used;
}
file_cbfs_result = CBFS_SUCCESS;
}
/* Get the CBFS header out of the ROM and do endian conversion. */
static int file_cbfs_load_header(uintptr_t end_of_rom,
struct cbfs_header *header)
{
struct cbfs_header *header_in_rom;
header_in_rom = (struct cbfs_header *)(uintptr_t)
*(u32 *)(end_of_rom - 3);
swap_header(header, header_in_rom);
if (header->magic != good_magic || header->offset >
header->rom_size - header->boot_block_size) {
file_cbfs_result = CBFS_BAD_HEADER;
return 1;
}
return 0;
}
void file_cbfs_init(uintptr_t end_of_rom)
{
u8 *start_of_rom;
initialized = 0;
if (file_cbfs_load_header(end_of_rom, &cbfs_header))
return;
start_of_rom = (u8 *)(end_of_rom + 1 - cbfs_header.rom_size);
file_cbfs_fill_cache(start_of_rom + cbfs_header.offset,
cbfs_header.rom_size, cbfs_header.align);
if (file_cbfs_result == CBFS_SUCCESS)
initialized = 1;
}
const struct cbfs_header *file_cbfs_get_header(void)
{
if (initialized) {
file_cbfs_result = CBFS_SUCCESS;
return &cbfs_header;
} else {
file_cbfs_result = CBFS_NOT_INITIALIZED;
return NULL;
}
}
const struct cbfs_cachenode *file_cbfs_get_first(void)
{
if (!initialized) {
file_cbfs_result = CBFS_NOT_INITIALIZED;
return NULL;
} else {
file_cbfs_result = CBFS_SUCCESS;
return file_cache;
}
}
void file_cbfs_get_next(const struct cbfs_cachenode **file)
{
if (!initialized) {
file_cbfs_result = CBFS_NOT_INITIALIZED;
file = NULL;
return;
}
if (*file)
*file = (*file)->next;
file_cbfs_result = CBFS_SUCCESS;
}
const struct cbfs_cachenode *file_cbfs_find(const char *name)
{
struct cbfs_cachenode *cache_node = file_cache;
if (!initialized) {
file_cbfs_result = CBFS_NOT_INITIALIZED;
return NULL;
}
while (cache_node) {
if (!strcmp(name, cache_node->name))
break;
cache_node = cache_node->next;
}
if (!cache_node)
file_cbfs_result = CBFS_FILE_NOT_FOUND;
else
file_cbfs_result = CBFS_SUCCESS;
return cache_node;
}
const struct cbfs_cachenode *file_cbfs_find_uncached(uintptr_t end_of_rom,
const char *name)
{
u8 *start;
u32 size;
u32 align;
static struct cbfs_cachenode node;
if (file_cbfs_load_header(end_of_rom, &cbfs_header))
return NULL;
start = (u8 *)(end_of_rom + 1 - cbfs_header.rom_size);
size = cbfs_header.rom_size;
align = cbfs_header.align;
while (size >= align) {
int result;
u32 used;
result = file_cbfs_next_file(start, size, align, &node, &used);
if (result < 0)
return NULL;
else if (result == 0)
break;
if (!strcmp(name, node.name))
return &node;
size -= used;
start += used;
}
file_cbfs_result = CBFS_FILE_NOT_FOUND;
return NULL;
}
const char *file_cbfs_name(const struct cbfs_cachenode *file)
{
file_cbfs_result = CBFS_SUCCESS;
return file->name;
}
u32 file_cbfs_size(const struct cbfs_cachenode *file)
{
file_cbfs_result = CBFS_SUCCESS;
return file->data_length;
}
u32 file_cbfs_type(const struct cbfs_cachenode *file)
{
file_cbfs_result = CBFS_SUCCESS;
return file->type;
}
long file_cbfs_read(const struct cbfs_cachenode *file, void *buffer,
unsigned long maxsize)
{
u32 size;
size = file->data_length;
if (maxsize && size > maxsize)
size = maxsize;
memcpy(buffer, file->data, size);
file_cbfs_result = CBFS_SUCCESS;
return size;
}
|