cbfs: Allow file traversal with any CBFS
[platform/kernel/u-boot.git] / fs / cbfs / cbfs.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
4  */
5
6 #include <common.h>
7 #include <cbfs.h>
8 #include <log.h>
9 #include <malloc.h>
10 #include <asm/byteorder.h>
11
12 /* Offset of master header from the start of a coreboot ROM */
13 #define MASTER_HDR_OFFSET       0x38
14
15 static const u32 good_magic = 0x4f524243;
16 static const u8 good_file_magic[] = "LARCHIVE";
17
18 /**
19  * struct cbfs_priv - Private data for this driver
20  *
21  * @initialised: true if this CBFS has been inited
22  * @start: Start position of CBFS in memory, typically memory-mapped SPI flash
23  * @header: Header read from the CBFS, byte-swapped so U-Boot can access it
24  * @file_cache: List of file headers read from CBFS
25  * @result: Success/error result
26  */
27 struct cbfs_priv {
28         bool initialized;
29         void *start;
30         struct cbfs_header header;
31         struct cbfs_cachenode *file_cache;
32         enum cbfs_result result;
33 };
34
35 static struct cbfs_priv cbfs_s;
36
37 const char *file_cbfs_error(void)
38 {
39         switch (cbfs_s.result) {
40         case CBFS_SUCCESS:
41                 return "Success";
42         case CBFS_NOT_INITIALIZED:
43                 return "CBFS not initialized";
44         case CBFS_BAD_HEADER:
45                 return "Bad CBFS header";
46         case CBFS_BAD_FILE:
47                 return "Bad CBFS file";
48         case CBFS_FILE_NOT_FOUND:
49                 return "File not found";
50         default:
51                 return "Unknown";
52         }
53 }
54
55 enum cbfs_result cbfs_get_result(void)
56 {
57         return cbfs_s.result;
58 }
59
60 /* Do endian conversion on the CBFS header structure. */
61 static void swap_header(struct cbfs_header *dest, struct cbfs_header *src)
62 {
63         dest->magic = be32_to_cpu(src->magic);
64         dest->version = be32_to_cpu(src->version);
65         dest->rom_size = be32_to_cpu(src->rom_size);
66         dest->boot_block_size = be32_to_cpu(src->boot_block_size);
67         dest->align = be32_to_cpu(src->align);
68         dest->offset = be32_to_cpu(src->offset);
69 }
70
71 /* Do endian conversion on a CBFS file header. */
72 static void swap_file_header(struct cbfs_fileheader *dest,
73                              const struct cbfs_fileheader *src)
74 {
75         memcpy(&dest->magic, &src->magic, sizeof(dest->magic));
76         dest->len = be32_to_cpu(src->len);
77         dest->type = be32_to_cpu(src->type);
78         dest->attributes_offset = be32_to_cpu(src->attributes_offset);
79         dest->offset = be32_to_cpu(src->offset);
80 }
81
82 /*
83  * Given a starting position in memory, scan forward, bounded by a size, and
84  * find the next valid CBFS file. No memory is allocated by this function. The
85  * caller is responsible for allocating space for the new file structure.
86  *
87  * @param start         The location in memory to start from.
88  * @param size          The size of the memory region to search.
89  * @param align         The alignment boundaries to check on.
90  * @param node  A pointer to the file structure to load.
91  * @param used          A pointer to the count of of bytes scanned through,
92  *                      including the file if one is found.
93  *
94  * @return 0 if a file is found, -ENOENT if one isn't, -EBADF if a bad header
95  *      is found.
96  */
97 static int file_cbfs_next_file(struct cbfs_priv *priv, void *start, int size,
98                                int align, struct cbfs_cachenode *node,
99                                int *used)
100 {
101         struct cbfs_fileheader header;
102
103         *used = 0;
104
105         while (size >= align) {
106                 const struct cbfs_fileheader *file_header = start;
107                 u32 name_len;
108                 u32 step;
109
110                 /* Check if there's a file here. */
111                 if (memcmp(good_file_magic, &file_header->magic,
112                            sizeof(file_header->magic))) {
113                         *used += align;
114                         size -= align;
115                         start += align;
116                         continue;
117                 }
118
119                 swap_file_header(&header, file_header);
120                 if (header.offset < sizeof(struct cbfs_fileheader)) {
121                         priv->result = CBFS_BAD_FILE;
122                         return -EBADF;
123                 }
124                 node->next = NULL;
125                 node->type = header.type;
126                 node->data = start + header.offset;
127                 node->data_length = header.len;
128                 name_len = header.offset - sizeof(struct cbfs_fileheader);
129                 node->name = (char *)file_header +
130                                 sizeof(struct cbfs_fileheader);
131                 node->name_length = name_len;
132                 node->attr_offset = header.attributes_offset;
133
134                 step = header.len;
135                 if (step % align)
136                         step = step + align - step % align;
137
138                 *used += step;
139                 return 0;
140         }
141
142         return -ENOENT;
143 }
144
145 /* Look through a CBFS instance and copy file metadata into regular memory. */
146 static int file_cbfs_fill_cache(struct cbfs_priv *priv, int size, int align)
147 {
148         struct cbfs_cachenode *cache_node;
149         struct cbfs_cachenode *node;
150         struct cbfs_cachenode **cache_tail = &priv->file_cache;
151         void *start;
152
153         /* Clear out old information. */
154         cache_node = priv->file_cache;
155         while (cache_node) {
156                 struct cbfs_cachenode *old_node = cache_node;
157                 cache_node = cache_node->next;
158                 free(old_node);
159         }
160         priv->file_cache = NULL;
161
162         start = priv->start;
163         while (size >= align) {
164                 int used;
165                 int ret;
166
167                 node = (struct cbfs_cachenode *)
168                                 malloc(sizeof(struct cbfs_cachenode));
169                 if (!node)
170                         return -ENOMEM;
171                 ret = file_cbfs_next_file(priv, start, size, align, node,
172                                           &used);
173
174                 if (ret < 0) {
175                         free(node);
176                         if (ret == -ENOENT)
177                                 break;
178                         return ret;
179                 }
180                 *cache_tail = node;
181                 cache_tail = &node->next;
182
183                 size -= used;
184                 start += used;
185         }
186         priv->result = CBFS_SUCCESS;
187
188         return 0;
189 }
190
191 /**
192  * load_header() - Load the CBFS header
193  *
194  * Get the CBFS header out of the ROM and do endian conversion.
195  *
196  * @priv: Private data, which is inited by this function
197  * @addr: Address of CBFS header in memory-mapped SPI flash
198  * @return 0 if OK, -ENXIO if the header is bad
199  */
200 static int load_header(struct cbfs_priv *priv, ulong addr)
201 {
202         struct cbfs_header *header = &priv->header;
203         struct cbfs_header *header_in_rom;
204
205         memset(priv, '\0', sizeof(*priv));
206         header_in_rom = (struct cbfs_header *)addr;
207         swap_header(header, header_in_rom);
208
209         if (header->magic != good_magic || header->offset >
210                         header->rom_size - header->boot_block_size) {
211                 priv->result = CBFS_BAD_HEADER;
212                 return -ENXIO;
213         }
214
215         return 0;
216 }
217
218 /**
219  * file_cbfs_load_header() - Get the CBFS header out of the ROM, given the end
220  *
221  * @priv: Private data, which is inited by this function
222  * @end_of_rom: Address of the last byte of the ROM (typically 0xffffffff)
223  * @return 0 if OK, -ENXIO if the header is bad
224  */
225 static int file_cbfs_load_header(struct cbfs_priv *priv, ulong end_of_rom)
226 {
227         int offset = *(u32 *)(end_of_rom - 3);
228         int ret;
229
230         ret = load_header(priv, end_of_rom + offset + 1);
231         if (ret)
232                 return ret;
233         priv->start = (void *)(end_of_rom + 1 - priv->header.rom_size);
234
235         return 0;
236 }
237
238 /**
239  * cbfs_load_header_ptr() - Get the CBFS header out of the ROM, given the base
240  *
241  * @priv: Private data, which is inited by this function
242  * @base: Address of the first byte of the ROM (e.g. 0xff000000)
243  * @return 0 if OK, -ENXIO if the header is bad
244  */
245 static int cbfs_load_header_ptr(struct cbfs_priv *priv, ulong base)
246 {
247         int ret;
248
249         ret = load_header(priv, base + MASTER_HDR_OFFSET);
250         if (ret)
251                 return ret;
252         priv->start = (void *)base;
253
254         return 0;
255 }
256
257 static int cbfs_init(struct cbfs_priv *priv, ulong end_of_rom)
258 {
259         int ret;
260
261         ret = file_cbfs_load_header(priv, end_of_rom);
262         if (ret)
263                 return ret;
264
265         ret = file_cbfs_fill_cache(priv, priv->header.rom_size,
266                                    priv->header.align);
267         if (ret)
268                 return ret;
269         priv->initialized = true;
270
271         return 0;
272 }
273
274 int file_cbfs_init(ulong end_of_rom)
275 {
276         return cbfs_init(&cbfs_s, end_of_rom);
277 }
278
279 int cbfs_init_mem(ulong base, ulong size, bool require_hdr,
280                   struct cbfs_priv **privp)
281 {
282         struct cbfs_priv priv_s, *priv = &priv_s;
283         int ret;
284
285         /*
286          * Use a local variable to start with until we know that the * CBFS is
287          * valid. Note that size is detected from the header, if present,
288          * meaning the parameter is ignored.
289          */
290         ret = cbfs_load_header_ptr(priv, base);
291         if (ret) {
292                 if (require_hdr || size == CBFS_SIZE_UNKNOWN)
293                         return ret;
294                 memset(priv, '\0', sizeof(struct cbfs_priv));
295                 priv->header.rom_size = size;
296                 priv->header.align = CBFS_ALIGN_SIZE;
297                 priv->start = (void *)base;
298         }
299
300         ret = file_cbfs_fill_cache(priv, priv->header.rom_size,
301                                    priv->header.align);
302         if (ret)
303                 return log_msg_ret("fill", ret);
304
305         priv->initialized = true;
306         priv = malloc(sizeof(priv_s));
307         if (!priv)
308                 return -ENOMEM;
309         memcpy(priv, &priv_s, sizeof(priv_s));
310         *privp = priv;
311
312         return 0;
313 }
314
315 const struct cbfs_header *file_cbfs_get_header(void)
316 {
317         struct cbfs_priv *priv = &cbfs_s;
318
319         if (priv->initialized) {
320                 priv->result = CBFS_SUCCESS;
321                 return &priv->header;
322         } else {
323                 priv->result = CBFS_NOT_INITIALIZED;
324                 return NULL;
325         }
326 }
327
328 const struct cbfs_cachenode *cbfs_get_first(const struct cbfs_priv *priv)
329 {
330         return priv->file_cache;
331 }
332
333 void cbfs_get_next(const struct cbfs_cachenode **filep)
334 {
335         if (*filep)
336                 *filep = (*filep)->next;
337 }
338
339 const struct cbfs_cachenode *file_cbfs_get_first(void)
340 {
341         struct cbfs_priv *priv = &cbfs_s;
342
343         if (!priv->initialized) {
344                 priv->result = CBFS_NOT_INITIALIZED;
345                 return NULL;
346         } else {
347                 priv->result = CBFS_SUCCESS;
348                 return priv->file_cache;
349         }
350 }
351
352 void file_cbfs_get_next(const struct cbfs_cachenode **file)
353 {
354         struct cbfs_priv *priv = &cbfs_s;
355
356         if (!priv->initialized) {
357                 priv->result = CBFS_NOT_INITIALIZED;
358                 *file = NULL;
359                 return;
360         }
361
362         if (*file)
363                 *file = (*file)->next;
364         priv->result = CBFS_SUCCESS;
365 }
366
367 const struct cbfs_cachenode *cbfs_find_file(struct cbfs_priv *priv,
368                                             const char *name)
369 {
370         struct cbfs_cachenode *cache_node = priv->file_cache;
371
372         if (!priv->initialized) {
373                 priv->result = CBFS_NOT_INITIALIZED;
374                 return NULL;
375         }
376
377         while (cache_node) {
378                 if (!strcmp(name, cache_node->name))
379                         break;
380                 cache_node = cache_node->next;
381         }
382         if (!cache_node)
383                 priv->result = CBFS_FILE_NOT_FOUND;
384         else
385                 priv->result = CBFS_SUCCESS;
386
387         return cache_node;
388 }
389
390 const struct cbfs_cachenode *file_cbfs_find(const char *name)
391 {
392         return cbfs_find_file(&cbfs_s, name);
393 }
394
395 static int find_uncached(struct cbfs_priv *priv, const char *name, void *start,
396                          struct cbfs_cachenode *node)
397 {
398         int size = priv->header.rom_size;
399         int align = priv->header.align;
400
401         while (size >= align) {
402                 int used;
403                 int ret;
404
405                 ret = file_cbfs_next_file(priv, start, size, align, node,
406                                           &used);
407                 if (ret == -ENOENT)
408                         break;
409                 else if (ret)
410                         return ret;
411                 if (!strcmp(name, node->name))
412                         return 0;
413
414                 size -= used;
415                 start += used;
416         }
417         priv->result = CBFS_FILE_NOT_FOUND;
418
419         return -ENOENT;
420 }
421
422 int file_cbfs_find_uncached(ulong end_of_rom, const char *name,
423                             struct cbfs_cachenode *node)
424 {
425         struct cbfs_priv priv;
426         void *start;
427         int ret;
428
429         ret = file_cbfs_load_header(&priv, end_of_rom);
430         if (ret)
431                 return ret;
432         start = priv.start;
433
434         return find_uncached(&priv, name, start, node);
435 }
436
437 int file_cbfs_find_uncached_base(ulong base, const char *name,
438                                  struct cbfs_cachenode *node)
439 {
440         struct cbfs_priv priv;
441         int ret;
442
443         ret = cbfs_load_header_ptr(&priv, base);
444         if (ret)
445                 return ret;
446
447         return find_uncached(&priv, name, (void *)base, node);
448 }
449
450 const char *file_cbfs_name(const struct cbfs_cachenode *file)
451 {
452         cbfs_s.result = CBFS_SUCCESS;
453
454         return file->name;
455 }
456
457 u32 file_cbfs_size(const struct cbfs_cachenode *file)
458 {
459         cbfs_s.result = CBFS_SUCCESS;
460
461         return file->data_length;
462 }
463
464 u32 file_cbfs_type(const struct cbfs_cachenode *file)
465 {
466         cbfs_s.result = CBFS_SUCCESS;
467
468         return file->type;
469 }
470
471 long file_cbfs_read(const struct cbfs_cachenode *file, void *buffer,
472                     unsigned long maxsize)
473 {
474         u32 size;
475
476         size = file->data_length;
477         if (maxsize && size > maxsize)
478                 size = maxsize;
479
480         memcpy(buffer, file->data, size);
481         cbfs_s.result = CBFS_SUCCESS;
482
483         return size;
484 }