blob: d9a8562e5c885fc00094fa81f99e7300eb5cb8bd [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Gabe Black84cd9322012-10-12 14:26:11 +00002/*
3 * Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
Gabe Black84cd9322012-10-12 14:26:11 +00004 */
5
Guillaume GARDET7a77e902016-06-17 11:45:37 +02006#include <common.h>
Gabe Black84cd9322012-10-12 14:26:11 +00007#include <cbfs.h>
8#include <malloc.h>
9#include <asm/byteorder.h>
10
Simon Glassfc7b9e12019-08-14 19:56:11 -060011static const u32 good_magic = 0x4f524243;
12static const u8 good_file_magic[] = "LARCHIVE";
Simon Glass02e4af62019-08-14 19:56:12 -060013
14struct cbfs_priv {
15 int initialized;
16 struct cbfs_header header;
17 struct cbfs_cachenode *file_cache;
Simon Glassc7f16932019-08-14 19:56:13 -060018 enum cbfs_result result;
Simon Glass02e4af62019-08-14 19:56:12 -060019};
20
21static struct cbfs_priv cbfs_s;
Gabe Black84cd9322012-10-12 14:26:11 +000022
23const char *file_cbfs_error(void)
24{
Simon Glassc7f16932019-08-14 19:56:13 -060025 switch (cbfs_s.result) {
Gabe Black84cd9322012-10-12 14:26:11 +000026 case CBFS_SUCCESS:
27 return "Success";
28 case CBFS_NOT_INITIALIZED:
29 return "CBFS not initialized";
30 case CBFS_BAD_HEADER:
31 return "Bad CBFS header";
32 case CBFS_BAD_FILE:
33 return "Bad CBFS file";
34 case CBFS_FILE_NOT_FOUND:
35 return "File not found";
36 default:
37 return "Unknown";
38 }
39}
40
Simon Glassc7f16932019-08-14 19:56:13 -060041enum cbfs_result cbfs_get_result(void)
42{
43 return cbfs_s.result;
44}
45
Gabe Black84cd9322012-10-12 14:26:11 +000046/* Do endian conversion on the CBFS header structure. */
47static void swap_header(struct cbfs_header *dest, struct cbfs_header *src)
48{
49 dest->magic = be32_to_cpu(src->magic);
50 dest->version = be32_to_cpu(src->version);
51 dest->rom_size = be32_to_cpu(src->rom_size);
52 dest->boot_block_size = be32_to_cpu(src->boot_block_size);
53 dest->align = be32_to_cpu(src->align);
54 dest->offset = be32_to_cpu(src->offset);
55}
56
57/* Do endian conversion on a CBFS file header. */
58static void swap_file_header(struct cbfs_fileheader *dest,
59 const struct cbfs_fileheader *src)
60{
61 memcpy(&dest->magic, &src->magic, sizeof(dest->magic));
62 dest->len = be32_to_cpu(src->len);
63 dest->type = be32_to_cpu(src->type);
Simon Glassababe102019-07-08 13:18:22 -060064 dest->attributes_offset = be32_to_cpu(src->attributes_offset);
Gabe Black84cd9322012-10-12 14:26:11 +000065 dest->offset = be32_to_cpu(src->offset);
66}
67
68/*
69 * Given a starting position in memory, scan forward, bounded by a size, and
70 * find the next valid CBFS file. No memory is allocated by this function. The
71 * caller is responsible for allocating space for the new file structure.
72 *
73 * @param start The location in memory to start from.
74 * @param size The size of the memory region to search.
75 * @param align The alignment boundaries to check on.
76 * @param newNode A pointer to the file structure to load.
77 * @param used A pointer to the count of of bytes scanned through,
78 * including the file if one is found.
79 *
80 * @return 1 if a file is found, 0 if one isn't.
81 */
Simon Glass02e4af62019-08-14 19:56:12 -060082static int file_cbfs_next_file(struct cbfs_priv *priv, u8 *start, u32 size,
83 u32 align, struct cbfs_cachenode *newNode,
84 u32 *used)
Gabe Black84cd9322012-10-12 14:26:11 +000085{
86 struct cbfs_fileheader header;
87
88 *used = 0;
89
90 while (size >= align) {
91 const struct cbfs_fileheader *fileHeader =
92 (const struct cbfs_fileheader *)start;
93 u32 name_len;
94 u32 step;
95
96 /* Check if there's a file here. */
97 if (memcmp(good_file_magic, &(fileHeader->magic),
98 sizeof(fileHeader->magic))) {
99 *used += align;
100 size -= align;
101 start += align;
102 continue;
103 }
104
105 swap_file_header(&header, fileHeader);
Christian Gmeiner9914c732018-12-22 01:55:48 -0800106 if (header.offset < sizeof(struct cbfs_fileheader)) {
Simon Glassc7f16932019-08-14 19:56:13 -0600107 priv->result = CBFS_BAD_FILE;
Gabe Black84cd9322012-10-12 14:26:11 +0000108 return -1;
109 }
110 newNode->next = NULL;
111 newNode->type = header.type;
112 newNode->data = start + header.offset;
113 newNode->data_length = header.len;
Yaroslav Kcc7ed262016-08-08 20:32:15 -0700114 name_len = header.offset - sizeof(struct cbfs_fileheader);
Gabe Black84cd9322012-10-12 14:26:11 +0000115 newNode->name = (char *)fileHeader +
Yaroslav Kcc7ed262016-08-08 20:32:15 -0700116 sizeof(struct cbfs_fileheader);
Gabe Black84cd9322012-10-12 14:26:11 +0000117 newNode->name_length = name_len;
Simon Glassababe102019-07-08 13:18:22 -0600118 newNode->attributes_offset = header.attributes_offset;
Gabe Black84cd9322012-10-12 14:26:11 +0000119
120 step = header.len;
121 if (step % align)
122 step = step + align - step % align;
123
124 *used += step;
125 return 1;
126 }
127 return 0;
128}
129
130/* Look through a CBFS instance and copy file metadata into regular memory. */
Simon Glass02e4af62019-08-14 19:56:12 -0600131static void file_cbfs_fill_cache(struct cbfs_priv *priv, u8 *start, u32 size,
132 u32 align)
Gabe Black84cd9322012-10-12 14:26:11 +0000133{
134 struct cbfs_cachenode *cache_node;
135 struct cbfs_cachenode *newNode;
Simon Glass02e4af62019-08-14 19:56:12 -0600136 struct cbfs_cachenode **cache_tail = &priv->file_cache;
Gabe Black84cd9322012-10-12 14:26:11 +0000137
138 /* Clear out old information. */
Simon Glass02e4af62019-08-14 19:56:12 -0600139 cache_node = priv->file_cache;
Gabe Black84cd9322012-10-12 14:26:11 +0000140 while (cache_node) {
141 struct cbfs_cachenode *oldNode = cache_node;
142 cache_node = cache_node->next;
143 free(oldNode);
144 }
Simon Glass02e4af62019-08-14 19:56:12 -0600145 priv->file_cache = NULL;
Gabe Black84cd9322012-10-12 14:26:11 +0000146
147 while (size >= align) {
148 int result;
149 u32 used;
150
151 newNode = (struct cbfs_cachenode *)
152 malloc(sizeof(struct cbfs_cachenode));
Simon Glass02e4af62019-08-14 19:56:12 -0600153 result = file_cbfs_next_file(priv, start, size, align, newNode,
154 &used);
Gabe Black84cd9322012-10-12 14:26:11 +0000155
156 if (result < 0) {
157 free(newNode);
158 return;
159 } else if (result == 0) {
160 free(newNode);
161 break;
162 }
163 *cache_tail = newNode;
164 cache_tail = &newNode->next;
165
166 size -= used;
167 start += used;
168 }
Simon Glassc7f16932019-08-14 19:56:13 -0600169 priv->result = CBFS_SUCCESS;
Gabe Black84cd9322012-10-12 14:26:11 +0000170}
171
172/* Get the CBFS header out of the ROM and do endian conversion. */
173static int file_cbfs_load_header(uintptr_t end_of_rom,
174 struct cbfs_header *header)
175{
176 struct cbfs_header *header_in_rom;
Andre Heider44683172018-02-15 07:40:11 +0100177 int32_t offset = *(u32 *)(end_of_rom - 3);
Gabe Black84cd9322012-10-12 14:26:11 +0000178
Andre Heider44683172018-02-15 07:40:11 +0100179 header_in_rom = (struct cbfs_header *)(end_of_rom + offset + 1);
Gabe Black84cd9322012-10-12 14:26:11 +0000180 swap_header(header, header_in_rom);
181
182 if (header->magic != good_magic || header->offset >
183 header->rom_size - header->boot_block_size) {
Simon Glassc7f16932019-08-14 19:56:13 -0600184 cbfs_s.result = CBFS_BAD_HEADER;
Gabe Black84cd9322012-10-12 14:26:11 +0000185 return 1;
186 }
187 return 0;
188}
189
Simon Glass02e4af62019-08-14 19:56:12 -0600190static void cbfs_init(struct cbfs_priv *priv, uintptr_t end_of_rom)
Gabe Black84cd9322012-10-12 14:26:11 +0000191{
192 u8 *start_of_rom;
Gabe Black84cd9322012-10-12 14:26:11 +0000193
Simon Glass02e4af62019-08-14 19:56:12 -0600194 priv->initialized = 0;
195
196 if (file_cbfs_load_header(end_of_rom, &priv->header))
Gabe Black84cd9322012-10-12 14:26:11 +0000197 return;
198
Simon Glass02e4af62019-08-14 19:56:12 -0600199 start_of_rom = (u8 *)(end_of_rom + 1 - priv->header.rom_size);
Gabe Black84cd9322012-10-12 14:26:11 +0000200
Simon Glass02e4af62019-08-14 19:56:12 -0600201 file_cbfs_fill_cache(priv, start_of_rom, priv->header.rom_size,
202 priv->header.align);
Simon Glassc7f16932019-08-14 19:56:13 -0600203 if (priv->result == CBFS_SUCCESS)
Simon Glass02e4af62019-08-14 19:56:12 -0600204 priv->initialized = 1;
205}
206
207void file_cbfs_init(uintptr_t end_of_rom)
208{
209 cbfs_init(&cbfs_s, end_of_rom);
Gabe Black84cd9322012-10-12 14:26:11 +0000210}
211
212const struct cbfs_header *file_cbfs_get_header(void)
213{
Simon Glass02e4af62019-08-14 19:56:12 -0600214 struct cbfs_priv *priv = &cbfs_s;
215
216 if (priv->initialized) {
Simon Glassc7f16932019-08-14 19:56:13 -0600217 priv->result = CBFS_SUCCESS;
Simon Glass02e4af62019-08-14 19:56:12 -0600218 return &priv->header;
Gabe Black84cd9322012-10-12 14:26:11 +0000219 } else {
Simon Glassc7f16932019-08-14 19:56:13 -0600220 priv->result = CBFS_NOT_INITIALIZED;
Gabe Black84cd9322012-10-12 14:26:11 +0000221 return NULL;
222 }
223}
224
225const struct cbfs_cachenode *file_cbfs_get_first(void)
226{
Simon Glass02e4af62019-08-14 19:56:12 -0600227 struct cbfs_priv *priv = &cbfs_s;
228
229 if (!priv->initialized) {
Simon Glassc7f16932019-08-14 19:56:13 -0600230 priv->result = CBFS_NOT_INITIALIZED;
Gabe Black84cd9322012-10-12 14:26:11 +0000231 return NULL;
232 } else {
Simon Glassc7f16932019-08-14 19:56:13 -0600233 priv->result = CBFS_SUCCESS;
Simon Glass02e4af62019-08-14 19:56:12 -0600234 return priv->file_cache;
Gabe Black84cd9322012-10-12 14:26:11 +0000235 }
236}
237
238void file_cbfs_get_next(const struct cbfs_cachenode **file)
239{
Simon Glass02e4af62019-08-14 19:56:12 -0600240 struct cbfs_priv *priv = &cbfs_s;
241
242 if (!priv->initialized) {
Simon Glassc7f16932019-08-14 19:56:13 -0600243 priv->result = CBFS_NOT_INITIALIZED;
Simon Glass02e4af62019-08-14 19:56:12 -0600244 *file = NULL;
Gabe Black84cd9322012-10-12 14:26:11 +0000245 return;
246 }
247
248 if (*file)
249 *file = (*file)->next;
Simon Glassc7f16932019-08-14 19:56:13 -0600250 priv->result = CBFS_SUCCESS;
Gabe Black84cd9322012-10-12 14:26:11 +0000251}
252
Simon Glass02e4af62019-08-14 19:56:12 -0600253const struct cbfs_cachenode *cbfs_find_file(struct cbfs_priv *priv,
254 const char *name)
Gabe Black84cd9322012-10-12 14:26:11 +0000255{
Simon Glass02e4af62019-08-14 19:56:12 -0600256 struct cbfs_cachenode *cache_node = priv->file_cache;
Gabe Black84cd9322012-10-12 14:26:11 +0000257
Simon Glass02e4af62019-08-14 19:56:12 -0600258 if (!priv->initialized) {
Simon Glassc7f16932019-08-14 19:56:13 -0600259 priv->result = CBFS_NOT_INITIALIZED;
Gabe Black84cd9322012-10-12 14:26:11 +0000260 return NULL;
261 }
262
263 while (cache_node) {
264 if (!strcmp(name, cache_node->name))
265 break;
266 cache_node = cache_node->next;
267 }
268 if (!cache_node)
Simon Glassc7f16932019-08-14 19:56:13 -0600269 priv->result = CBFS_FILE_NOT_FOUND;
Gabe Black84cd9322012-10-12 14:26:11 +0000270 else
Simon Glassc7f16932019-08-14 19:56:13 -0600271 priv->result = CBFS_SUCCESS;
Gabe Black84cd9322012-10-12 14:26:11 +0000272
273 return cache_node;
274}
275
Simon Glass02e4af62019-08-14 19:56:12 -0600276const struct cbfs_cachenode *file_cbfs_find(const char *name)
277{
278 return cbfs_find_file(&cbfs_s, name);
279}
280
Gabe Black84cd9322012-10-12 14:26:11 +0000281const struct cbfs_cachenode *file_cbfs_find_uncached(uintptr_t end_of_rom,
282 const char *name)
283{
Simon Glass02e4af62019-08-14 19:56:12 -0600284 struct cbfs_priv *priv = &cbfs_s;
Gabe Black84cd9322012-10-12 14:26:11 +0000285 u8 *start;
286 u32 size;
287 u32 align;
288 static struct cbfs_cachenode node;
289
Simon Glass02e4af62019-08-14 19:56:12 -0600290 if (file_cbfs_load_header(end_of_rom, &priv->header))
Gabe Black84cd9322012-10-12 14:26:11 +0000291 return NULL;
292
Simon Glass02e4af62019-08-14 19:56:12 -0600293 start = (u8 *)(end_of_rom + 1 - priv->header.rom_size);
294 size = priv->header.rom_size;
295 align = priv->header.align;
Gabe Black84cd9322012-10-12 14:26:11 +0000296
297 while (size >= align) {
298 int result;
299 u32 used;
300
Simon Glass02e4af62019-08-14 19:56:12 -0600301 result = file_cbfs_next_file(priv, start, size, align, &node,
302 &used);
Gabe Black84cd9322012-10-12 14:26:11 +0000303
304 if (result < 0)
305 return NULL;
306 else if (result == 0)
307 break;
308
309 if (!strcmp(name, node.name))
310 return &node;
311
312 size -= used;
313 start += used;
314 }
Simon Glassc7f16932019-08-14 19:56:13 -0600315 cbfs_s.result = CBFS_FILE_NOT_FOUND;
Gabe Black84cd9322012-10-12 14:26:11 +0000316 return NULL;
317}
318
319const char *file_cbfs_name(const struct cbfs_cachenode *file)
320{
Simon Glassc7f16932019-08-14 19:56:13 -0600321 cbfs_s.result = CBFS_SUCCESS;
322
Gabe Black84cd9322012-10-12 14:26:11 +0000323 return file->name;
324}
325
326u32 file_cbfs_size(const struct cbfs_cachenode *file)
327{
Simon Glassc7f16932019-08-14 19:56:13 -0600328 cbfs_s.result = CBFS_SUCCESS;
329
Gabe Black84cd9322012-10-12 14:26:11 +0000330 return file->data_length;
331}
332
333u32 file_cbfs_type(const struct cbfs_cachenode *file)
334{
Simon Glassc7f16932019-08-14 19:56:13 -0600335 cbfs_s.result = CBFS_SUCCESS;
336
Gabe Black84cd9322012-10-12 14:26:11 +0000337 return file->type;
338}
339
340long file_cbfs_read(const struct cbfs_cachenode *file, void *buffer,
341 unsigned long maxsize)
342{
343 u32 size;
344
345 size = file->data_length;
346 if (maxsize && size > maxsize)
347 size = maxsize;
348
349 memcpy(buffer, file->data, size);
Simon Glassc7f16932019-08-14 19:56:13 -0600350 cbfs_s.result = CBFS_SUCCESS;
Gabe Black84cd9322012-10-12 14:26:11 +0000351
Gabe Black84cd9322012-10-12 14:26:11 +0000352 return size;
353}