cbfs: Support reading compression information
[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  * fill_node() - Fill a node struct with information from the CBFS
84  *
85  * @node: Node to fill
86  * @start: Pointer to the start of the CBFS file in memory
87  * @header: Pointer to the header information (in our enddianess)
88  * @return 0 if OK, -EBADF if the header is too small
89  */
90 static int fill_node(struct cbfs_cachenode *node, void *start,
91                      struct cbfs_fileheader *header)
92 {
93         uint name_len;
94         uint offset;
95
96         /* Check the header is large enough */
97         if (header->offset < sizeof(struct cbfs_fileheader))
98                 return -EBADF;
99
100         node->next = NULL;
101         node->type = header->type;
102         node->data = start + header->offset;
103         node->data_length = header->len;
104         name_len = header->offset - sizeof(struct cbfs_fileheader);
105         node->name = start + sizeof(struct cbfs_fileheader);
106         node->name_length = name_len;
107         node->attr_offset = header->attributes_offset;
108         node->comp_algo = CBFS_COMPRESS_NONE;
109         node->decomp_size = 0;
110
111         for (offset = node->attr_offset; offset < header->offset;) {
112                 const struct cbfs_file_attribute *attr;
113                 uint tag, len;
114
115                 attr = start + offset;
116                 tag = be32_to_cpu(attr->tag);
117                 len = be32_to_cpu(attr->len);
118                 if (tag == CBFS_FILE_ATTR_TAG_COMPRESSION) {
119                         struct cbfs_file_attr_compression *comp;
120
121                         comp = start + offset;
122                         node->comp_algo = be32_to_cpu(comp->compression);
123                         node->decomp_size =
124                                 be32_to_cpu(comp->decompressed_size);
125                 }
126
127                 offset += len;
128         }
129
130         return 0;
131 }
132
133 /*
134  * Given a starting position in memory, scan forward, bounded by a size, and
135  * find the next valid CBFS file. No memory is allocated by this function. The
136  * caller is responsible for allocating space for the new file structure.
137  *
138  * @param start         The location in memory to start from.
139  * @param size          The size of the memory region to search.
140  * @param align         The alignment boundaries to check on.
141  * @param node  A pointer to the file structure to load.
142  * @param used          A pointer to the count of of bytes scanned through,
143  *                      including the file if one is found.
144  *
145  * @return 0 if a file is found, -ENOENT if one isn't, -EBADF if a bad header
146  *      is found.
147  */
148 static int file_cbfs_next_file(struct cbfs_priv *priv, void *start, int size,
149                                int align, struct cbfs_cachenode *node,
150                                int *used)
151 {
152         struct cbfs_fileheader header;
153
154         *used = 0;
155
156         while (size >= align) {
157                 const struct cbfs_fileheader *file_header = start;
158                 int ret;
159
160                 /* Check if there's a file here. */
161                 if (memcmp(good_file_magic, &file_header->magic,
162                            sizeof(file_header->magic))) {
163                         *used += align;
164                         size -= align;
165                         start += align;
166                         continue;
167                 }
168
169                 swap_file_header(&header, file_header);
170                 ret = fill_node(node, start, &header);
171                 if (ret) {
172                         priv->result = CBFS_BAD_FILE;
173                         return log_msg_ret("fill", ret);
174                 }
175
176                 *used += ALIGN(header.len, align);
177                 return 0;
178         }
179
180         return -ENOENT;
181 }
182
183 /* Look through a CBFS instance and copy file metadata into regular memory. */
184 static int file_cbfs_fill_cache(struct cbfs_priv *priv, int size, int align)
185 {
186         struct cbfs_cachenode *cache_node;
187         struct cbfs_cachenode *node;
188         struct cbfs_cachenode **cache_tail = &priv->file_cache;
189         void *start;
190
191         /* Clear out old information. */
192         cache_node = priv->file_cache;
193         while (cache_node) {
194                 struct cbfs_cachenode *old_node = cache_node;
195                 cache_node = cache_node->next;
196                 free(old_node);
197         }
198         priv->file_cache = NULL;
199
200         start = priv->start;
201         while (size >= align) {
202                 int used;
203                 int ret;
204
205                 node = (struct cbfs_cachenode *)
206                                 malloc(sizeof(struct cbfs_cachenode));
207                 if (!node)
208                         return -ENOMEM;
209                 ret = file_cbfs_next_file(priv, start, size, align, node,
210                                           &used);
211
212                 if (ret < 0) {
213                         free(node);
214                         if (ret == -ENOENT)
215                                 break;
216                         return ret;
217                 }
218                 *cache_tail = node;
219                 cache_tail = &node->next;
220
221                 size -= used;
222                 start += used;
223         }
224         priv->result = CBFS_SUCCESS;
225
226         return 0;
227 }
228
229 /**
230  * load_header() - Load the CBFS header
231  *
232  * Get the CBFS header out of the ROM and do endian conversion.
233  *
234  * @priv: Private data, which is inited by this function
235  * @addr: Address of CBFS header in memory-mapped SPI flash
236  * @return 0 if OK, -ENXIO if the header is bad
237  */
238 static int load_header(struct cbfs_priv *priv, ulong addr)
239 {
240         struct cbfs_header *header = &priv->header;
241         struct cbfs_header *header_in_rom;
242
243         memset(priv, '\0', sizeof(*priv));
244         header_in_rom = (struct cbfs_header *)addr;
245         swap_header(header, header_in_rom);
246
247         if (header->magic != good_magic || header->offset >
248                         header->rom_size - header->boot_block_size) {
249                 priv->result = CBFS_BAD_HEADER;
250                 return -ENXIO;
251         }
252
253         return 0;
254 }
255
256 /**
257  * file_cbfs_load_header() - Get the CBFS header out of the ROM, given the end
258  *
259  * @priv: Private data, which is inited by this function
260  * @end_of_rom: Address of the last byte of the ROM (typically 0xffffffff)
261  * @return 0 if OK, -ENXIO if the header is bad
262  */
263 static int file_cbfs_load_header(struct cbfs_priv *priv, ulong end_of_rom)
264 {
265         int offset = *(u32 *)(end_of_rom - 3);
266         int ret;
267
268         ret = load_header(priv, end_of_rom + offset + 1);
269         if (ret)
270                 return ret;
271         priv->start = (void *)(end_of_rom + 1 - priv->header.rom_size);
272
273         return 0;
274 }
275
276 /**
277  * cbfs_load_header_ptr() - Get the CBFS header out of the ROM, given the base
278  *
279  * @priv: Private data, which is inited by this function
280  * @base: Address of the first byte of the ROM (e.g. 0xff000000)
281  * @return 0 if OK, -ENXIO if the header is bad
282  */
283 static int cbfs_load_header_ptr(struct cbfs_priv *priv, ulong base)
284 {
285         int ret;
286
287         ret = load_header(priv, base + MASTER_HDR_OFFSET);
288         if (ret)
289                 return ret;
290         priv->start = (void *)base;
291
292         return 0;
293 }
294
295 static int cbfs_init(struct cbfs_priv *priv, ulong end_of_rom)
296 {
297         int ret;
298
299         ret = file_cbfs_load_header(priv, end_of_rom);
300         if (ret)
301                 return ret;
302
303         ret = file_cbfs_fill_cache(priv, priv->header.rom_size,
304                                    priv->header.align);
305         if (ret)
306                 return ret;
307         priv->initialized = true;
308
309         return 0;
310 }
311
312 int file_cbfs_init(ulong end_of_rom)
313 {
314         return cbfs_init(&cbfs_s, end_of_rom);
315 }
316
317 int cbfs_init_mem(ulong base, ulong size, bool require_hdr,
318                   struct cbfs_priv **privp)
319 {
320         struct cbfs_priv priv_s, *priv = &priv_s;
321         int ret;
322
323         /*
324          * Use a local variable to start with until we know that the * CBFS is
325          * valid. Note that size is detected from the header, if present,
326          * meaning the parameter is ignored.
327          */
328         ret = cbfs_load_header_ptr(priv, base);
329         if (ret) {
330                 if (require_hdr || size == CBFS_SIZE_UNKNOWN)
331                         return ret;
332                 memset(priv, '\0', sizeof(struct cbfs_priv));
333                 priv->header.rom_size = size;
334                 priv->header.align = CBFS_ALIGN_SIZE;
335                 priv->start = (void *)base;
336         }
337
338         ret = file_cbfs_fill_cache(priv, priv->header.rom_size,
339                                    priv->header.align);
340         if (ret)
341                 return log_msg_ret("fill", ret);
342
343         priv->initialized = true;
344         priv = malloc(sizeof(priv_s));
345         if (!priv)
346                 return -ENOMEM;
347         memcpy(priv, &priv_s, sizeof(priv_s));
348         *privp = priv;
349
350         return 0;
351 }
352
353 const struct cbfs_header *file_cbfs_get_header(void)
354 {
355         struct cbfs_priv *priv = &cbfs_s;
356
357         if (priv->initialized) {
358                 priv->result = CBFS_SUCCESS;
359                 return &priv->header;
360         } else {
361                 priv->result = CBFS_NOT_INITIALIZED;
362                 return NULL;
363         }
364 }
365
366 const struct cbfs_cachenode *cbfs_get_first(const struct cbfs_priv *priv)
367 {
368         return priv->file_cache;
369 }
370
371 void cbfs_get_next(const struct cbfs_cachenode **filep)
372 {
373         if (*filep)
374                 *filep = (*filep)->next;
375 }
376
377 const struct cbfs_cachenode *file_cbfs_get_first(void)
378 {
379         struct cbfs_priv *priv = &cbfs_s;
380
381         if (!priv->initialized) {
382                 priv->result = CBFS_NOT_INITIALIZED;
383                 return NULL;
384         } else {
385                 priv->result = CBFS_SUCCESS;
386                 return priv->file_cache;
387         }
388 }
389
390 void file_cbfs_get_next(const struct cbfs_cachenode **file)
391 {
392         struct cbfs_priv *priv = &cbfs_s;
393
394         if (!priv->initialized) {
395                 priv->result = CBFS_NOT_INITIALIZED;
396                 *file = NULL;
397                 return;
398         }
399
400         if (*file)
401                 *file = (*file)->next;
402         priv->result = CBFS_SUCCESS;
403 }
404
405 const struct cbfs_cachenode *cbfs_find_file(struct cbfs_priv *priv,
406                                             const char *name)
407 {
408         struct cbfs_cachenode *cache_node = priv->file_cache;
409
410         if (!priv->initialized) {
411                 priv->result = CBFS_NOT_INITIALIZED;
412                 return NULL;
413         }
414
415         while (cache_node) {
416                 if (!strcmp(name, cache_node->name))
417                         break;
418                 cache_node = cache_node->next;
419         }
420         if (!cache_node)
421                 priv->result = CBFS_FILE_NOT_FOUND;
422         else
423                 priv->result = CBFS_SUCCESS;
424
425         return cache_node;
426 }
427
428 const struct cbfs_cachenode *file_cbfs_find(const char *name)
429 {
430         return cbfs_find_file(&cbfs_s, name);
431 }
432
433 static int find_uncached(struct cbfs_priv *priv, const char *name, void *start,
434                          struct cbfs_cachenode *node)
435 {
436         int size = priv->header.rom_size;
437         int align = priv->header.align;
438
439         while (size >= align) {
440                 int used;
441                 int ret;
442
443                 ret = file_cbfs_next_file(priv, start, size, align, node,
444                                           &used);
445                 if (ret == -ENOENT)
446                         break;
447                 else if (ret)
448                         return ret;
449                 if (!strcmp(name, node->name))
450                         return 0;
451
452                 size -= used;
453                 start += used;
454         }
455         priv->result = CBFS_FILE_NOT_FOUND;
456
457         return -ENOENT;
458 }
459
460 int file_cbfs_find_uncached(ulong end_of_rom, const char *name,
461                             struct cbfs_cachenode *node)
462 {
463         struct cbfs_priv priv;
464         void *start;
465         int ret;
466
467         ret = file_cbfs_load_header(&priv, end_of_rom);
468         if (ret)
469                 return ret;
470         start = priv.start;
471
472         return find_uncached(&priv, name, start, node);
473 }
474
475 int file_cbfs_find_uncached_base(ulong base, const char *name,
476                                  struct cbfs_cachenode *node)
477 {
478         struct cbfs_priv priv;
479         int ret;
480
481         ret = cbfs_load_header_ptr(&priv, base);
482         if (ret)
483                 return ret;
484
485         return find_uncached(&priv, name, (void *)base, node);
486 }
487
488 const char *file_cbfs_name(const struct cbfs_cachenode *file)
489 {
490         cbfs_s.result = CBFS_SUCCESS;
491
492         return file->name;
493 }
494
495 u32 file_cbfs_size(const struct cbfs_cachenode *file)
496 {
497         cbfs_s.result = CBFS_SUCCESS;
498
499         return file->data_length;
500 }
501
502 u32 file_cbfs_type(const struct cbfs_cachenode *file)
503 {
504         cbfs_s.result = CBFS_SUCCESS;
505
506         return file->type;
507 }
508
509 long file_cbfs_read(const struct cbfs_cachenode *file, void *buffer,
510                     unsigned long maxsize)
511 {
512         u32 size;
513
514         size = file->data_length;
515         if (maxsize && size > maxsize)
516                 size = maxsize;
517
518         memcpy(buffer, file->data, size);
519         cbfs_s.result = CBFS_SUCCESS;
520
521         return size;
522 }