blob: ba903d16a07cc5085a65be4ec6f23526f015cdbb [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 Glassa2c528f2020-05-24 17:38:17 -060011/* Offset of master header from the start of a coreboot ROM */
12#define MASTER_HDR_OFFSET 0x38
13
Simon Glassfc7b9e12019-08-14 19:56:11 -060014static const u32 good_magic = 0x4f524243;
15static const u8 good_file_magic[] = "LARCHIVE";
Simon Glass02e4af62019-08-14 19:56:12 -060016
Simon Glassc685f8b2020-05-24 17:38:20 -060017/**
18 * struct cbfs_priv - Private data for this driver
19 *
20 * @initialised: true if this CBFS has been inited
21 * @start: Start position of CBFS in memory, typically memory-mapped SPI flash
22 * @header: Header read from the CBFS, byte-swapped so U-Boot can access it
23 * @file_cache: List of file headers read from CBFS
24 * @result: Success/error result
25 */
Simon Glass02e4af62019-08-14 19:56:12 -060026struct cbfs_priv {
Simon Glass381e1132020-05-24 17:38:14 -060027 bool initialized;
Simon Glassc685f8b2020-05-24 17:38:20 -060028 void *start;
Simon Glass02e4af62019-08-14 19:56:12 -060029 struct cbfs_header header;
30 struct cbfs_cachenode *file_cache;
Simon Glassc7f16932019-08-14 19:56:13 -060031 enum cbfs_result result;
Simon Glass02e4af62019-08-14 19:56:12 -060032};
33
34static struct cbfs_priv cbfs_s;
Gabe Black84cd9322012-10-12 14:26:11 +000035
36const char *file_cbfs_error(void)
37{
Simon Glassc7f16932019-08-14 19:56:13 -060038 switch (cbfs_s.result) {
Gabe Black84cd9322012-10-12 14:26:11 +000039 case CBFS_SUCCESS:
40 return "Success";
41 case CBFS_NOT_INITIALIZED:
42 return "CBFS not initialized";
43 case CBFS_BAD_HEADER:
44 return "Bad CBFS header";
45 case CBFS_BAD_FILE:
46 return "Bad CBFS file";
47 case CBFS_FILE_NOT_FOUND:
48 return "File not found";
49 default:
50 return "Unknown";
51 }
52}
53
Simon Glassc7f16932019-08-14 19:56:13 -060054enum cbfs_result cbfs_get_result(void)
55{
56 return cbfs_s.result;
57}
58
Gabe Black84cd9322012-10-12 14:26:11 +000059/* Do endian conversion on the CBFS header structure. */
60static void swap_header(struct cbfs_header *dest, struct cbfs_header *src)
61{
62 dest->magic = be32_to_cpu(src->magic);
63 dest->version = be32_to_cpu(src->version);
64 dest->rom_size = be32_to_cpu(src->rom_size);
65 dest->boot_block_size = be32_to_cpu(src->boot_block_size);
66 dest->align = be32_to_cpu(src->align);
67 dest->offset = be32_to_cpu(src->offset);
68}
69
70/* Do endian conversion on a CBFS file header. */
71static void swap_file_header(struct cbfs_fileheader *dest,
72 const struct cbfs_fileheader *src)
73{
74 memcpy(&dest->magic, &src->magic, sizeof(dest->magic));
75 dest->len = be32_to_cpu(src->len);
76 dest->type = be32_to_cpu(src->type);
Simon Glassababe102019-07-08 13:18:22 -060077 dest->attributes_offset = be32_to_cpu(src->attributes_offset);
Gabe Black84cd9322012-10-12 14:26:11 +000078 dest->offset = be32_to_cpu(src->offset);
79}
80
81/*
82 * Given a starting position in memory, scan forward, bounded by a size, and
83 * find the next valid CBFS file. No memory is allocated by this function. The
84 * caller is responsible for allocating space for the new file structure.
85 *
86 * @param start The location in memory to start from.
87 * @param size The size of the memory region to search.
88 * @param align The alignment boundaries to check on.
Simon Glassad79d602019-08-14 19:56:15 -060089 * @param new_node A pointer to the file structure to load.
Gabe Black84cd9322012-10-12 14:26:11 +000090 * @param used A pointer to the count of of bytes scanned through,
91 * including the file if one is found.
92 *
Simon Glassc7bef7c2020-05-24 17:38:15 -060093 * @return 0 if a file is found, -ENOENT if one isn't, -EBADF if a bad header
94 * is found.
Gabe Black84cd9322012-10-12 14:26:11 +000095 */
Simon Glasse82ab512020-05-24 17:38:19 -060096static int file_cbfs_next_file(struct cbfs_priv *priv, void *start, int size,
Simon Glassc7bef7c2020-05-24 17:38:15 -060097 int align, struct cbfs_cachenode *new_node,
98 int *used)
Gabe Black84cd9322012-10-12 14:26:11 +000099{
100 struct cbfs_fileheader header;
101
102 *used = 0;
103
104 while (size >= align) {
Simon Glasse82ab512020-05-24 17:38:19 -0600105 const struct cbfs_fileheader *file_header = start;
Gabe Black84cd9322012-10-12 14:26:11 +0000106 u32 name_len;
107 u32 step;
108
109 /* Check if there's a file here. */
Simon Glassad79d602019-08-14 19:56:15 -0600110 if (memcmp(good_file_magic, &file_header->magic,
111 sizeof(file_header->magic))) {
Gabe Black84cd9322012-10-12 14:26:11 +0000112 *used += align;
113 size -= align;
114 start += align;
115 continue;
116 }
117
Simon Glassad79d602019-08-14 19:56:15 -0600118 swap_file_header(&header, file_header);
Christian Gmeiner9914c732018-12-22 01:55:48 -0800119 if (header.offset < sizeof(struct cbfs_fileheader)) {
Simon Glassc7f16932019-08-14 19:56:13 -0600120 priv->result = CBFS_BAD_FILE;
Simon Glassc7bef7c2020-05-24 17:38:15 -0600121 return -EBADF;
Gabe Black84cd9322012-10-12 14:26:11 +0000122 }
Simon Glassad79d602019-08-14 19:56:15 -0600123 new_node->next = NULL;
124 new_node->type = header.type;
125 new_node->data = start + header.offset;
126 new_node->data_length = header.len;
Yaroslav Kcc7ed262016-08-08 20:32:15 -0700127 name_len = header.offset - sizeof(struct cbfs_fileheader);
Simon Glassad79d602019-08-14 19:56:15 -0600128 new_node->name = (char *)file_header +
Yaroslav Kcc7ed262016-08-08 20:32:15 -0700129 sizeof(struct cbfs_fileheader);
Simon Glassad79d602019-08-14 19:56:15 -0600130 new_node->name_length = name_len;
131 new_node->attributes_offset = header.attributes_offset;
Gabe Black84cd9322012-10-12 14:26:11 +0000132
133 step = header.len;
134 if (step % align)
135 step = step + align - step % align;
136
137 *used += step;
Simon Glassc7bef7c2020-05-24 17:38:15 -0600138 return 0;
Gabe Black84cd9322012-10-12 14:26:11 +0000139 }
Simon Glassc7bef7c2020-05-24 17:38:15 -0600140
141 return -ENOENT;
Gabe Black84cd9322012-10-12 14:26:11 +0000142}
143
144/* Look through a CBFS instance and copy file metadata into regular memory. */
Simon Glassc685f8b2020-05-24 17:38:20 -0600145static int file_cbfs_fill_cache(struct cbfs_priv *priv, int size, int align)
Gabe Black84cd9322012-10-12 14:26:11 +0000146{
147 struct cbfs_cachenode *cache_node;
Simon Glassad79d602019-08-14 19:56:15 -0600148 struct cbfs_cachenode *new_node;
Simon Glass02e4af62019-08-14 19:56:12 -0600149 struct cbfs_cachenode **cache_tail = &priv->file_cache;
Simon Glassc685f8b2020-05-24 17:38:20 -0600150 void *start;
Gabe Black84cd9322012-10-12 14:26:11 +0000151
152 /* Clear out old information. */
Simon Glass02e4af62019-08-14 19:56:12 -0600153 cache_node = priv->file_cache;
Gabe Black84cd9322012-10-12 14:26:11 +0000154 while (cache_node) {
Simon Glassad79d602019-08-14 19:56:15 -0600155 struct cbfs_cachenode *old_node = cache_node;
Gabe Black84cd9322012-10-12 14:26:11 +0000156 cache_node = cache_node->next;
Simon Glassad79d602019-08-14 19:56:15 -0600157 free(old_node);
Gabe Black84cd9322012-10-12 14:26:11 +0000158 }
Simon Glass02e4af62019-08-14 19:56:12 -0600159 priv->file_cache = NULL;
Gabe Black84cd9322012-10-12 14:26:11 +0000160
Simon Glassc685f8b2020-05-24 17:38:20 -0600161 start = priv->start;
Gabe Black84cd9322012-10-12 14:26:11 +0000162 while (size >= align) {
Simon Glassc7bef7c2020-05-24 17:38:15 -0600163 int used;
Simon Glassea38ee92020-05-24 17:38:12 -0600164 int ret;
Gabe Black84cd9322012-10-12 14:26:11 +0000165
Simon Glassad79d602019-08-14 19:56:15 -0600166 new_node = (struct cbfs_cachenode *)
Gabe Black84cd9322012-10-12 14:26:11 +0000167 malloc(sizeof(struct cbfs_cachenode));
Simon Glassc7bef7c2020-05-24 17:38:15 -0600168 if (!new_node)
169 return -ENOMEM;
Simon Glassea38ee92020-05-24 17:38:12 -0600170 ret = file_cbfs_next_file(priv, start, size, align, new_node,
171 &used);
Gabe Black84cd9322012-10-12 14:26:11 +0000172
Simon Glassea38ee92020-05-24 17:38:12 -0600173 if (ret < 0) {
Simon Glassad79d602019-08-14 19:56:15 -0600174 free(new_node);
Simon Glassc7bef7c2020-05-24 17:38:15 -0600175 if (ret == -ENOENT)
176 break;
177 return ret;
Gabe Black84cd9322012-10-12 14:26:11 +0000178 }
Simon Glassad79d602019-08-14 19:56:15 -0600179 *cache_tail = new_node;
180 cache_tail = &new_node->next;
Gabe Black84cd9322012-10-12 14:26:11 +0000181
182 size -= used;
183 start += used;
184 }
Simon Glassc7f16932019-08-14 19:56:13 -0600185 priv->result = CBFS_SUCCESS;
Simon Glassc7bef7c2020-05-24 17:38:15 -0600186
187 return 0;
Gabe Black84cd9322012-10-12 14:26:11 +0000188}
189
Simon Glass9dc23552020-05-24 17:38:18 -0600190/**
191 * load_header() - Load the CBFS header
192 *
193 * Get the CBFS header out of the ROM and do endian conversion.
194 *
195 * @priv: Private data, which is inited by this function
196 * @addr: Address of CBFS header in memory-mapped SPI flash
197 * @return 0 if OK, -ENXIO if the header is bad
198 */
199static int load_header(struct cbfs_priv *priv, ulong addr)
Gabe Black84cd9322012-10-12 14:26:11 +0000200{
Simon Glass54e19252020-05-24 17:38:16 -0600201 struct cbfs_header *header = &priv->header;
Gabe Black84cd9322012-10-12 14:26:11 +0000202 struct cbfs_header *header_in_rom;
203
Simon Glass9dc23552020-05-24 17:38:18 -0600204 memset(priv, '\0', sizeof(*priv));
205 header_in_rom = (struct cbfs_header *)addr;
Gabe Black84cd9322012-10-12 14:26:11 +0000206 swap_header(header, header_in_rom);
207
208 if (header->magic != good_magic || header->offset >
209 header->rom_size - header->boot_block_size) {
Simon Glass54e19252020-05-24 17:38:16 -0600210 priv->result = CBFS_BAD_HEADER;
Simon Glass9dc23552020-05-24 17:38:18 -0600211 return -ENXIO;
Gabe Black84cd9322012-10-12 14:26:11 +0000212 }
Simon Glass9dc23552020-05-24 17:38:18 -0600213
Gabe Black84cd9322012-10-12 14:26:11 +0000214 return 0;
215}
216
Simon Glass9dc23552020-05-24 17:38:18 -0600217/**
218 * file_cbfs_load_header() - Get the CBFS header out of the ROM, given the end
219 *
220 * @priv: Private data, which is inited by this function
221 * @end_of_rom: Address of the last byte of the ROM (typically 0xffffffff)
222 * @return 0 if OK, -ENXIO if the header is bad
223 */
224static int file_cbfs_load_header(struct cbfs_priv *priv, ulong end_of_rom)
225{
226 int offset = *(u32 *)(end_of_rom - 3);
Simon Glassc685f8b2020-05-24 17:38:20 -0600227 int ret;
Simon Glass9dc23552020-05-24 17:38:18 -0600228
Simon Glassc685f8b2020-05-24 17:38:20 -0600229 ret = load_header(priv, end_of_rom + offset + 1);
230 if (ret)
231 return ret;
232 priv->start = (void *)(end_of_rom + 1 - priv->header.rom_size);
233
234 return 0;
Simon Glass9dc23552020-05-24 17:38:18 -0600235}
236
237/**
238 * cbfs_load_header_ptr() - Get the CBFS header out of the ROM, given the base
239 *
240 * @priv: Private data, which is inited by this function
241 * @base: Address of the first byte of the ROM (e.g. 0xff000000)
242 * @return 0 if OK, -ENXIO if the header is bad
243 */
Simon Glassa2c528f2020-05-24 17:38:17 -0600244static int cbfs_load_header_ptr(struct cbfs_priv *priv, ulong base)
Simon Glass630b2f32019-08-14 19:56:14 -0600245{
Simon Glassc685f8b2020-05-24 17:38:20 -0600246 int ret;
247
248 ret = load_header(priv, base + MASTER_HDR_OFFSET);
249 if (ret)
250 return ret;
251 priv->start = (void *)base;
252
253 return 0;
Simon Glass630b2f32019-08-14 19:56:14 -0600254}
255
Simon Glass0e7b6312020-05-24 17:38:21 -0600256static int cbfs_init(struct cbfs_priv *priv, ulong end_of_rom)
Gabe Black84cd9322012-10-12 14:26:11 +0000257{
Simon Glass0e7b6312020-05-24 17:38:21 -0600258 int ret;
Gabe Black84cd9322012-10-12 14:26:11 +0000259
Simon Glass0e7b6312020-05-24 17:38:21 -0600260 ret = file_cbfs_load_header(priv, end_of_rom);
261 if (ret)
262 return ret;
263
264 ret = file_cbfs_fill_cache(priv, priv->header.rom_size,
265 priv->header.align);
266 if (ret)
267 return ret;
268 priv->initialized = true;
269
270 return 0;
Simon Glass02e4af62019-08-14 19:56:12 -0600271}
272
Simon Glass0e7b6312020-05-24 17:38:21 -0600273int file_cbfs_init(ulong end_of_rom)
Simon Glass02e4af62019-08-14 19:56:12 -0600274{
Simon Glass0e7b6312020-05-24 17:38:21 -0600275 return cbfs_init(&cbfs_s, end_of_rom);
Gabe Black84cd9322012-10-12 14:26:11 +0000276}
277
Simon Glass630b2f32019-08-14 19:56:14 -0600278int cbfs_init_mem(ulong base, ulong size, struct cbfs_priv **privp)
279{
280 struct cbfs_priv priv_s, *priv = &priv_s;
281 int ret;
282
283 /*
284 * Use a local variable to start with until we know that the CBFS is
Simon Glass9dc23552020-05-24 17:38:18 -0600285 * valid.
Simon Glass630b2f32019-08-14 19:56:14 -0600286 */
Simon Glass9dc23552020-05-24 17:38:18 -0600287 ret = cbfs_load_header_ptr(priv, base);
Simon Glass630b2f32019-08-14 19:56:14 -0600288 if (ret)
289 return ret;
290
Simon Glassc685f8b2020-05-24 17:38:20 -0600291 file_cbfs_fill_cache(priv, priv->header.rom_size, priv->header.align);
Simon Glass630b2f32019-08-14 19:56:14 -0600292 if (priv->result != CBFS_SUCCESS)
293 return -EINVAL;
294
Simon Glass381e1132020-05-24 17:38:14 -0600295 priv->initialized = true;
Simon Glass630b2f32019-08-14 19:56:14 -0600296 priv = malloc(sizeof(priv_s));
297 if (!priv)
298 return -ENOMEM;
299 memcpy(priv, &priv_s, sizeof(priv_s));
300 *privp = priv;
301
302 return 0;
303}
304
Gabe Black84cd9322012-10-12 14:26:11 +0000305const struct cbfs_header *file_cbfs_get_header(void)
306{
Simon Glass02e4af62019-08-14 19:56:12 -0600307 struct cbfs_priv *priv = &cbfs_s;
308
309 if (priv->initialized) {
Simon Glassc7f16932019-08-14 19:56:13 -0600310 priv->result = CBFS_SUCCESS;
Simon Glass02e4af62019-08-14 19:56:12 -0600311 return &priv->header;
Gabe Black84cd9322012-10-12 14:26:11 +0000312 } else {
Simon Glassc7f16932019-08-14 19:56:13 -0600313 priv->result = CBFS_NOT_INITIALIZED;
Gabe Black84cd9322012-10-12 14:26:11 +0000314 return NULL;
315 }
316}
317
318const struct cbfs_cachenode *file_cbfs_get_first(void)
319{
Simon Glass02e4af62019-08-14 19:56:12 -0600320 struct cbfs_priv *priv = &cbfs_s;
321
322 if (!priv->initialized) {
Simon Glassc7f16932019-08-14 19:56:13 -0600323 priv->result = CBFS_NOT_INITIALIZED;
Gabe Black84cd9322012-10-12 14:26:11 +0000324 return NULL;
325 } else {
Simon Glassc7f16932019-08-14 19:56:13 -0600326 priv->result = CBFS_SUCCESS;
Simon Glass02e4af62019-08-14 19:56:12 -0600327 return priv->file_cache;
Gabe Black84cd9322012-10-12 14:26:11 +0000328 }
329}
330
331void file_cbfs_get_next(const struct cbfs_cachenode **file)
332{
Simon Glass02e4af62019-08-14 19:56:12 -0600333 struct cbfs_priv *priv = &cbfs_s;
334
335 if (!priv->initialized) {
Simon Glassc7f16932019-08-14 19:56:13 -0600336 priv->result = CBFS_NOT_INITIALIZED;
Simon Glass02e4af62019-08-14 19:56:12 -0600337 *file = NULL;
Gabe Black84cd9322012-10-12 14:26:11 +0000338 return;
339 }
340
341 if (*file)
342 *file = (*file)->next;
Simon Glassc7f16932019-08-14 19:56:13 -0600343 priv->result = CBFS_SUCCESS;
Gabe Black84cd9322012-10-12 14:26:11 +0000344}
345
Simon Glass02e4af62019-08-14 19:56:12 -0600346const struct cbfs_cachenode *cbfs_find_file(struct cbfs_priv *priv,
347 const char *name)
Gabe Black84cd9322012-10-12 14:26:11 +0000348{
Simon Glass02e4af62019-08-14 19:56:12 -0600349 struct cbfs_cachenode *cache_node = priv->file_cache;
Gabe Black84cd9322012-10-12 14:26:11 +0000350
Simon Glass02e4af62019-08-14 19:56:12 -0600351 if (!priv->initialized) {
Simon Glassc7f16932019-08-14 19:56:13 -0600352 priv->result = CBFS_NOT_INITIALIZED;
Gabe Black84cd9322012-10-12 14:26:11 +0000353 return NULL;
354 }
355
356 while (cache_node) {
357 if (!strcmp(name, cache_node->name))
358 break;
359 cache_node = cache_node->next;
360 }
361 if (!cache_node)
Simon Glassc7f16932019-08-14 19:56:13 -0600362 priv->result = CBFS_FILE_NOT_FOUND;
Gabe Black84cd9322012-10-12 14:26:11 +0000363 else
Simon Glassc7f16932019-08-14 19:56:13 -0600364 priv->result = CBFS_SUCCESS;
Gabe Black84cd9322012-10-12 14:26:11 +0000365
366 return cache_node;
367}
368
Simon Glass02e4af62019-08-14 19:56:12 -0600369const struct cbfs_cachenode *file_cbfs_find(const char *name)
370{
371 return cbfs_find_file(&cbfs_s, name);
372}
373
Simon Glass924e3462020-05-24 17:38:22 -0600374static int find_uncached(struct cbfs_priv *priv, const char *name, void *start,
375 struct cbfs_cachenode *node)
Gabe Black84cd9322012-10-12 14:26:11 +0000376{
Simon Glass924e3462020-05-24 17:38:22 -0600377 int size = priv->header.rom_size;
378 int align = priv->header.align;
Gabe Black84cd9322012-10-12 14:26:11 +0000379
380 while (size >= align) {
Simon Glassc7bef7c2020-05-24 17:38:15 -0600381 int used;
Simon Glass924e3462020-05-24 17:38:22 -0600382 int ret;
Gabe Black84cd9322012-10-12 14:26:11 +0000383
Simon Glass924e3462020-05-24 17:38:22 -0600384 ret = file_cbfs_next_file(priv, start, size, align, node,
Simon Glassea38ee92020-05-24 17:38:12 -0600385 &used);
Simon Glassc7bef7c2020-05-24 17:38:15 -0600386 if (ret == -ENOENT)
Gabe Black84cd9322012-10-12 14:26:11 +0000387 break;
Simon Glassc7bef7c2020-05-24 17:38:15 -0600388 else if (ret)
Simon Glass924e3462020-05-24 17:38:22 -0600389 return ret;
390 if (!strcmp(name, node->name))
391 return 0;
Gabe Black84cd9322012-10-12 14:26:11 +0000392
393 size -= used;
394 start += used;
395 }
Simon Glass924e3462020-05-24 17:38:22 -0600396 priv->result = CBFS_FILE_NOT_FOUND;
397
398 return -ENOENT;
399}
400
401int file_cbfs_find_uncached(ulong end_of_rom, const char *name,
402 struct cbfs_cachenode *node)
403{
404 struct cbfs_priv priv;
405 void *start;
406 int ret;
407
408 ret = file_cbfs_load_header(&priv, end_of_rom);
409 if (ret)
410 return ret;
411 start = priv.start;
412
413 return find_uncached(&priv, name, start, node);
Gabe Black84cd9322012-10-12 14:26:11 +0000414}
415
416const char *file_cbfs_name(const struct cbfs_cachenode *file)
417{
Simon Glassc7f16932019-08-14 19:56:13 -0600418 cbfs_s.result = CBFS_SUCCESS;
419
Gabe Black84cd9322012-10-12 14:26:11 +0000420 return file->name;
421}
422
423u32 file_cbfs_size(const struct cbfs_cachenode *file)
424{
Simon Glassc7f16932019-08-14 19:56:13 -0600425 cbfs_s.result = CBFS_SUCCESS;
426
Gabe Black84cd9322012-10-12 14:26:11 +0000427 return file->data_length;
428}
429
430u32 file_cbfs_type(const struct cbfs_cachenode *file)
431{
Simon Glassc7f16932019-08-14 19:56:13 -0600432 cbfs_s.result = CBFS_SUCCESS;
433
Gabe Black84cd9322012-10-12 14:26:11 +0000434 return file->type;
435}
436
437long file_cbfs_read(const struct cbfs_cachenode *file, void *buffer,
438 unsigned long maxsize)
439{
440 u32 size;
441
442 size = file->data_length;
443 if (maxsize && size > maxsize)
444 size = maxsize;
445
446 memcpy(buffer, file->data, size);
Simon Glassc7f16932019-08-14 19:56:13 -0600447 cbfs_s.result = CBFS_SUCCESS;
Gabe Black84cd9322012-10-12 14:26:11 +0000448
Gabe Black84cd9322012-10-12 14:26:11 +0000449 return size;
450}