Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
Guillaume GARDET | 647ffed | 2016-06-17 11:45:37 +0200 | [diff] [blame] | 6 | #include <common.h> |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 7 | #include <cbfs.h> |
Simon Glass | ba06a1f | 2020-05-24 17:38:24 -0600 | [diff] [blame] | 8 | #include <log.h> |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 9 | #include <malloc.h> |
| 10 | #include <asm/byteorder.h> |
| 11 | |
Simon Glass | e78f9bb | 2020-05-24 17:38:17 -0600 | [diff] [blame] | 12 | /* Offset of master header from the start of a coreboot ROM */ |
| 13 | #define MASTER_HDR_OFFSET 0x38 |
| 14 | |
Simon Glass | 7b5ee96 | 2019-08-14 19:56:11 -0600 | [diff] [blame] | 15 | static const u32 good_magic = 0x4f524243; |
| 16 | static const u8 good_file_magic[] = "LARCHIVE"; |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 17 | |
Simon Glass | ec36d7a | 2020-05-24 17:38:20 -0600 | [diff] [blame] | 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 | */ |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 27 | struct cbfs_priv { |
Simon Glass | 2029a6d | 2020-05-24 17:38:14 -0600 | [diff] [blame] | 28 | bool initialized; |
Simon Glass | ec36d7a | 2020-05-24 17:38:20 -0600 | [diff] [blame] | 29 | void *start; |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 30 | struct cbfs_header header; |
| 31 | struct cbfs_cachenode *file_cache; |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 32 | enum cbfs_result result; |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | static struct cbfs_priv cbfs_s; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 36 | |
| 37 | const char *file_cbfs_error(void) |
| 38 | { |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 39 | switch (cbfs_s.result) { |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 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 | |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 55 | enum cbfs_result cbfs_get_result(void) |
| 56 | { |
| 57 | return cbfs_s.result; |
| 58 | } |
| 59 | |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 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); |
Simon Glass | 87d1504 | 2019-07-08 13:18:22 -0600 | [diff] [blame] | 78 | dest->attributes_offset = be32_to_cpu(src->attributes_offset); |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 79 | dest->offset = be32_to_cpu(src->offset); |
| 80 | } |
| 81 | |
Simon Glass | 1567db0 | 2021-03-15 18:00:14 +1300 | [diff] [blame] | 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; |
Simon Glass | 2d507d2 | 2021-03-15 18:00:16 +1300 | [diff] [blame] | 94 | uint offset; |
Simon Glass | 1567db0 | 2021-03-15 18:00:14 +1300 | [diff] [blame] | 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; |
Simon Glass | 2d507d2 | 2021-03-15 18:00:16 +1300 | [diff] [blame] | 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 | } |
Simon Glass | 1567db0 | 2021-03-15 18:00:14 +1300 | [diff] [blame] | 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 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. |
Simon Glass | 7a02c81 | 2021-03-15 18:00:10 +1300 | [diff] [blame] | 141 | * @param node A pointer to the file structure to load. |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 142 | * @param used A pointer to the count of of bytes scanned through, |
| 143 | * including the file if one is found. |
| 144 | * |
Simon Glass | c72d4a8 | 2020-05-24 17:38:15 -0600 | [diff] [blame] | 145 | * @return 0 if a file is found, -ENOENT if one isn't, -EBADF if a bad header |
| 146 | * is found. |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 147 | */ |
Simon Glass | 6ca4c38 | 2020-05-24 17:38:19 -0600 | [diff] [blame] | 148 | static int file_cbfs_next_file(struct cbfs_priv *priv, void *start, int size, |
Simon Glass | 7a02c81 | 2021-03-15 18:00:10 +1300 | [diff] [blame] | 149 | int align, struct cbfs_cachenode *node, |
Simon Glass | c72d4a8 | 2020-05-24 17:38:15 -0600 | [diff] [blame] | 150 | int *used) |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 151 | { |
| 152 | struct cbfs_fileheader header; |
| 153 | |
| 154 | *used = 0; |
| 155 | |
| 156 | while (size >= align) { |
Simon Glass | 6ca4c38 | 2020-05-24 17:38:19 -0600 | [diff] [blame] | 157 | const struct cbfs_fileheader *file_header = start; |
Simon Glass | 1567db0 | 2021-03-15 18:00:14 +1300 | [diff] [blame] | 158 | int ret; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 159 | |
| 160 | /* Check if there's a file here. */ |
Simon Glass | efbd7b1 | 2019-08-14 19:56:15 -0600 | [diff] [blame] | 161 | if (memcmp(good_file_magic, &file_header->magic, |
| 162 | sizeof(file_header->magic))) { |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 163 | *used += align; |
| 164 | size -= align; |
| 165 | start += align; |
| 166 | continue; |
| 167 | } |
| 168 | |
Simon Glass | efbd7b1 | 2019-08-14 19:56:15 -0600 | [diff] [blame] | 169 | swap_file_header(&header, file_header); |
Simon Glass | 4410086 | 2021-05-13 19:39:27 -0600 | [diff] [blame] | 170 | if (header.offset >= size) |
| 171 | return log_msg_ret("range", -E2BIG); |
Simon Glass | 1567db0 | 2021-03-15 18:00:14 +1300 | [diff] [blame] | 172 | ret = fill_node(node, start, &header); |
| 173 | if (ret) { |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 174 | priv->result = CBFS_BAD_FILE; |
Simon Glass | 1567db0 | 2021-03-15 18:00:14 +1300 | [diff] [blame] | 175 | return log_msg_ret("fill", ret); |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 176 | } |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 177 | |
Simon Glass | 0352edd | 2021-03-15 18:00:15 +1300 | [diff] [blame] | 178 | *used += ALIGN(header.len, align); |
Simon Glass | c72d4a8 | 2020-05-24 17:38:15 -0600 | [diff] [blame] | 179 | return 0; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 180 | } |
Simon Glass | c72d4a8 | 2020-05-24 17:38:15 -0600 | [diff] [blame] | 181 | |
| 182 | return -ENOENT; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | /* Look through a CBFS instance and copy file metadata into regular memory. */ |
Simon Glass | ec36d7a | 2020-05-24 17:38:20 -0600 | [diff] [blame] | 186 | static int file_cbfs_fill_cache(struct cbfs_priv *priv, int size, int align) |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 187 | { |
| 188 | struct cbfs_cachenode *cache_node; |
Simon Glass | 7a02c81 | 2021-03-15 18:00:10 +1300 | [diff] [blame] | 189 | struct cbfs_cachenode *node; |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 190 | struct cbfs_cachenode **cache_tail = &priv->file_cache; |
Simon Glass | ec36d7a | 2020-05-24 17:38:20 -0600 | [diff] [blame] | 191 | void *start; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 192 | |
| 193 | /* Clear out old information. */ |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 194 | cache_node = priv->file_cache; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 195 | while (cache_node) { |
Simon Glass | efbd7b1 | 2019-08-14 19:56:15 -0600 | [diff] [blame] | 196 | struct cbfs_cachenode *old_node = cache_node; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 197 | cache_node = cache_node->next; |
Simon Glass | efbd7b1 | 2019-08-14 19:56:15 -0600 | [diff] [blame] | 198 | free(old_node); |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 199 | } |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 200 | priv->file_cache = NULL; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 201 | |
Simon Glass | ec36d7a | 2020-05-24 17:38:20 -0600 | [diff] [blame] | 202 | start = priv->start; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 203 | while (size >= align) { |
Simon Glass | c72d4a8 | 2020-05-24 17:38:15 -0600 | [diff] [blame] | 204 | int used; |
Simon Glass | 650b241 | 2020-05-24 17:38:12 -0600 | [diff] [blame] | 205 | int ret; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 206 | |
Simon Glass | 7907c4b | 2021-03-15 18:00:17 +1300 | [diff] [blame] | 207 | node = malloc(sizeof(struct cbfs_cachenode)); |
Simon Glass | 7a02c81 | 2021-03-15 18:00:10 +1300 | [diff] [blame] | 208 | if (!node) |
Simon Glass | c72d4a8 | 2020-05-24 17:38:15 -0600 | [diff] [blame] | 209 | return -ENOMEM; |
Simon Glass | 7a02c81 | 2021-03-15 18:00:10 +1300 | [diff] [blame] | 210 | ret = file_cbfs_next_file(priv, start, size, align, node, |
Simon Glass | 650b241 | 2020-05-24 17:38:12 -0600 | [diff] [blame] | 211 | &used); |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 212 | |
Simon Glass | 650b241 | 2020-05-24 17:38:12 -0600 | [diff] [blame] | 213 | if (ret < 0) { |
Simon Glass | 7a02c81 | 2021-03-15 18:00:10 +1300 | [diff] [blame] | 214 | free(node); |
Simon Glass | c72d4a8 | 2020-05-24 17:38:15 -0600 | [diff] [blame] | 215 | if (ret == -ENOENT) |
| 216 | break; |
| 217 | return ret; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 218 | } |
Simon Glass | 7a02c81 | 2021-03-15 18:00:10 +1300 | [diff] [blame] | 219 | *cache_tail = node; |
| 220 | cache_tail = &node->next; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 221 | |
| 222 | size -= used; |
| 223 | start += used; |
| 224 | } |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 225 | priv->result = CBFS_SUCCESS; |
Simon Glass | c72d4a8 | 2020-05-24 17:38:15 -0600 | [diff] [blame] | 226 | |
| 227 | return 0; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Simon Glass | 5154100 | 2020-05-24 17:38:18 -0600 | [diff] [blame] | 230 | /** |
| 231 | * load_header() - Load the CBFS header |
| 232 | * |
| 233 | * Get the CBFS header out of the ROM and do endian conversion. |
| 234 | * |
| 235 | * @priv: Private data, which is inited by this function |
| 236 | * @addr: Address of CBFS header in memory-mapped SPI flash |
| 237 | * @return 0 if OK, -ENXIO if the header is bad |
| 238 | */ |
| 239 | static int load_header(struct cbfs_priv *priv, ulong addr) |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 240 | { |
Simon Glass | 9ad16d6 | 2020-05-24 17:38:16 -0600 | [diff] [blame] | 241 | struct cbfs_header *header = &priv->header; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 242 | struct cbfs_header *header_in_rom; |
| 243 | |
Simon Glass | 5154100 | 2020-05-24 17:38:18 -0600 | [diff] [blame] | 244 | memset(priv, '\0', sizeof(*priv)); |
| 245 | header_in_rom = (struct cbfs_header *)addr; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 246 | swap_header(header, header_in_rom); |
| 247 | |
| 248 | if (header->magic != good_magic || header->offset > |
| 249 | header->rom_size - header->boot_block_size) { |
Simon Glass | 9ad16d6 | 2020-05-24 17:38:16 -0600 | [diff] [blame] | 250 | priv->result = CBFS_BAD_HEADER; |
Simon Glass | 5154100 | 2020-05-24 17:38:18 -0600 | [diff] [blame] | 251 | return -ENXIO; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 252 | } |
Simon Glass | 5154100 | 2020-05-24 17:38:18 -0600 | [diff] [blame] | 253 | |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 254 | return 0; |
| 255 | } |
| 256 | |
Simon Glass | 5154100 | 2020-05-24 17:38:18 -0600 | [diff] [blame] | 257 | /** |
| 258 | * file_cbfs_load_header() - Get the CBFS header out of the ROM, given the end |
| 259 | * |
| 260 | * @priv: Private data, which is inited by this function |
| 261 | * @end_of_rom: Address of the last byte of the ROM (typically 0xffffffff) |
| 262 | * @return 0 if OK, -ENXIO if the header is bad |
| 263 | */ |
| 264 | static int file_cbfs_load_header(struct cbfs_priv *priv, ulong end_of_rom) |
Simon Glass | aa4274e | 2019-08-14 19:56:14 -0600 | [diff] [blame] | 265 | { |
Simon Glass | 5154100 | 2020-05-24 17:38:18 -0600 | [diff] [blame] | 266 | int offset = *(u32 *)(end_of_rom - 3); |
Simon Glass | ec36d7a | 2020-05-24 17:38:20 -0600 | [diff] [blame] | 267 | int ret; |
| 268 | |
| 269 | ret = load_header(priv, end_of_rom + offset + 1); |
| 270 | if (ret) |
| 271 | return ret; |
| 272 | priv->start = (void *)(end_of_rom + 1 - priv->header.rom_size); |
Simon Glass | aa4274e | 2019-08-14 19:56:14 -0600 | [diff] [blame] | 273 | |
Simon Glass | ec36d7a | 2020-05-24 17:38:20 -0600 | [diff] [blame] | 274 | return 0; |
Simon Glass | 5154100 | 2020-05-24 17:38:18 -0600 | [diff] [blame] | 275 | } |
Simon Glass | aa4274e | 2019-08-14 19:56:14 -0600 | [diff] [blame] | 276 | |
Simon Glass | 5154100 | 2020-05-24 17:38:18 -0600 | [diff] [blame] | 277 | /** |
| 278 | * cbfs_load_header_ptr() - Get the CBFS header out of the ROM, given the base |
| 279 | * |
| 280 | * @priv: Private data, which is inited by this function |
| 281 | * @base: Address of the first byte of the ROM (e.g. 0xff000000) |
| 282 | * @return 0 if OK, -ENXIO if the header is bad |
| 283 | */ |
| 284 | static int cbfs_load_header_ptr(struct cbfs_priv *priv, ulong base) |
| 285 | { |
Simon Glass | ec36d7a | 2020-05-24 17:38:20 -0600 | [diff] [blame] | 286 | int ret; |
| 287 | |
| 288 | ret = load_header(priv, base + MASTER_HDR_OFFSET); |
| 289 | if (ret) |
| 290 | return ret; |
| 291 | priv->start = (void *)base; |
| 292 | |
| 293 | return 0; |
Simon Glass | aa4274e | 2019-08-14 19:56:14 -0600 | [diff] [blame] | 294 | } |
| 295 | |
Simon Glass | 0492317 | 2020-05-24 17:38:21 -0600 | [diff] [blame] | 296 | static int cbfs_init(struct cbfs_priv *priv, ulong end_of_rom) |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 297 | { |
Simon Glass | 0492317 | 2020-05-24 17:38:21 -0600 | [diff] [blame] | 298 | int ret; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 299 | |
Simon Glass | 0492317 | 2020-05-24 17:38:21 -0600 | [diff] [blame] | 300 | ret = file_cbfs_load_header(priv, end_of_rom); |
| 301 | if (ret) |
| 302 | return ret; |
| 303 | |
| 304 | ret = file_cbfs_fill_cache(priv, priv->header.rom_size, |
| 305 | priv->header.align); |
| 306 | if (ret) |
| 307 | return ret; |
| 308 | priv->initialized = true; |
| 309 | |
| 310 | return 0; |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 311 | } |
| 312 | |
Simon Glass | 0492317 | 2020-05-24 17:38:21 -0600 | [diff] [blame] | 313 | int file_cbfs_init(ulong end_of_rom) |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 314 | { |
Simon Glass | 0492317 | 2020-05-24 17:38:21 -0600 | [diff] [blame] | 315 | return cbfs_init(&cbfs_s, end_of_rom); |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Simon Glass | 9464909 | 2021-03-15 18:00:12 +1300 | [diff] [blame] | 318 | int cbfs_init_mem(ulong base, ulong size, bool require_hdr, |
| 319 | struct cbfs_priv **privp) |
Simon Glass | aa4274e | 2019-08-14 19:56:14 -0600 | [diff] [blame] | 320 | { |
| 321 | struct cbfs_priv priv_s, *priv = &priv_s; |
| 322 | int ret; |
| 323 | |
| 324 | /* |
Simon Glass | 9464909 | 2021-03-15 18:00:12 +1300 | [diff] [blame] | 325 | * Use a local variable to start with until we know that the * CBFS is |
| 326 | * valid. Note that size is detected from the header, if present, |
| 327 | * meaning the parameter is ignored. |
Simon Glass | aa4274e | 2019-08-14 19:56:14 -0600 | [diff] [blame] | 328 | */ |
Simon Glass | 5154100 | 2020-05-24 17:38:18 -0600 | [diff] [blame] | 329 | ret = cbfs_load_header_ptr(priv, base); |
Simon Glass | 9464909 | 2021-03-15 18:00:12 +1300 | [diff] [blame] | 330 | if (ret) { |
| 331 | if (require_hdr || size == CBFS_SIZE_UNKNOWN) |
| 332 | return ret; |
| 333 | memset(priv, '\0', sizeof(struct cbfs_priv)); |
| 334 | priv->header.rom_size = size; |
| 335 | priv->header.align = CBFS_ALIGN_SIZE; |
| 336 | priv->start = (void *)base; |
| 337 | } |
Simon Glass | aa4274e | 2019-08-14 19:56:14 -0600 | [diff] [blame] | 338 | |
Simon Glass | ba06a1f | 2020-05-24 17:38:24 -0600 | [diff] [blame] | 339 | ret = file_cbfs_fill_cache(priv, priv->header.rom_size, |
| 340 | priv->header.align); |
| 341 | if (ret) |
| 342 | return log_msg_ret("fill", ret); |
Simon Glass | aa4274e | 2019-08-14 19:56:14 -0600 | [diff] [blame] | 343 | |
Simon Glass | 2029a6d | 2020-05-24 17:38:14 -0600 | [diff] [blame] | 344 | priv->initialized = true; |
Simon Glass | aa4274e | 2019-08-14 19:56:14 -0600 | [diff] [blame] | 345 | priv = malloc(sizeof(priv_s)); |
| 346 | if (!priv) |
| 347 | return -ENOMEM; |
| 348 | memcpy(priv, &priv_s, sizeof(priv_s)); |
| 349 | *privp = priv; |
| 350 | |
| 351 | return 0; |
| 352 | } |
| 353 | |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 354 | const struct cbfs_header *file_cbfs_get_header(void) |
| 355 | { |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 356 | struct cbfs_priv *priv = &cbfs_s; |
| 357 | |
| 358 | if (priv->initialized) { |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 359 | priv->result = CBFS_SUCCESS; |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 360 | return &priv->header; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 361 | } else { |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 362 | priv->result = CBFS_NOT_INITIALIZED; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 363 | return NULL; |
| 364 | } |
| 365 | } |
| 366 | |
Simon Glass | f8142fb | 2021-03-15 18:00:13 +1300 | [diff] [blame] | 367 | const struct cbfs_cachenode *cbfs_get_first(const struct cbfs_priv *priv) |
| 368 | { |
| 369 | return priv->file_cache; |
| 370 | } |
| 371 | |
| 372 | void cbfs_get_next(const struct cbfs_cachenode **filep) |
| 373 | { |
| 374 | if (*filep) |
| 375 | *filep = (*filep)->next; |
| 376 | } |
| 377 | |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 378 | const struct cbfs_cachenode *file_cbfs_get_first(void) |
| 379 | { |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 380 | struct cbfs_priv *priv = &cbfs_s; |
| 381 | |
| 382 | if (!priv->initialized) { |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 383 | priv->result = CBFS_NOT_INITIALIZED; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 384 | return NULL; |
| 385 | } else { |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 386 | priv->result = CBFS_SUCCESS; |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 387 | return priv->file_cache; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | |
| 391 | void file_cbfs_get_next(const struct cbfs_cachenode **file) |
| 392 | { |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 393 | struct cbfs_priv *priv = &cbfs_s; |
| 394 | |
| 395 | if (!priv->initialized) { |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 396 | priv->result = CBFS_NOT_INITIALIZED; |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 397 | *file = NULL; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 398 | return; |
| 399 | } |
| 400 | |
| 401 | if (*file) |
| 402 | *file = (*file)->next; |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 403 | priv->result = CBFS_SUCCESS; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 406 | const struct cbfs_cachenode *cbfs_find_file(struct cbfs_priv *priv, |
| 407 | const char *name) |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 408 | { |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 409 | struct cbfs_cachenode *cache_node = priv->file_cache; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 410 | |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 411 | if (!priv->initialized) { |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 412 | priv->result = CBFS_NOT_INITIALIZED; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 413 | return NULL; |
| 414 | } |
| 415 | |
| 416 | while (cache_node) { |
| 417 | if (!strcmp(name, cache_node->name)) |
| 418 | break; |
| 419 | cache_node = cache_node->next; |
| 420 | } |
| 421 | if (!cache_node) |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 422 | priv->result = CBFS_FILE_NOT_FOUND; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 423 | else |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 424 | priv->result = CBFS_SUCCESS; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 425 | |
| 426 | return cache_node; |
| 427 | } |
| 428 | |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 429 | const struct cbfs_cachenode *file_cbfs_find(const char *name) |
| 430 | { |
| 431 | return cbfs_find_file(&cbfs_s, name); |
| 432 | } |
| 433 | |
Simon Glass | 9c42215 | 2020-05-24 17:38:22 -0600 | [diff] [blame] | 434 | static int find_uncached(struct cbfs_priv *priv, const char *name, void *start, |
| 435 | struct cbfs_cachenode *node) |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 436 | { |
Simon Glass | 9c42215 | 2020-05-24 17:38:22 -0600 | [diff] [blame] | 437 | int size = priv->header.rom_size; |
| 438 | int align = priv->header.align; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 439 | |
| 440 | while (size >= align) { |
Simon Glass | c72d4a8 | 2020-05-24 17:38:15 -0600 | [diff] [blame] | 441 | int used; |
Simon Glass | 9c42215 | 2020-05-24 17:38:22 -0600 | [diff] [blame] | 442 | int ret; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 443 | |
Simon Glass | 9c42215 | 2020-05-24 17:38:22 -0600 | [diff] [blame] | 444 | ret = file_cbfs_next_file(priv, start, size, align, node, |
Simon Glass | 650b241 | 2020-05-24 17:38:12 -0600 | [diff] [blame] | 445 | &used); |
Simon Glass | c72d4a8 | 2020-05-24 17:38:15 -0600 | [diff] [blame] | 446 | if (ret == -ENOENT) |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 447 | break; |
Simon Glass | c72d4a8 | 2020-05-24 17:38:15 -0600 | [diff] [blame] | 448 | else if (ret) |
Simon Glass | 9c42215 | 2020-05-24 17:38:22 -0600 | [diff] [blame] | 449 | return ret; |
| 450 | if (!strcmp(name, node->name)) |
| 451 | return 0; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 452 | |
| 453 | size -= used; |
| 454 | start += used; |
| 455 | } |
Simon Glass | 9c42215 | 2020-05-24 17:38:22 -0600 | [diff] [blame] | 456 | priv->result = CBFS_FILE_NOT_FOUND; |
| 457 | |
| 458 | return -ENOENT; |
| 459 | } |
| 460 | |
| 461 | int file_cbfs_find_uncached(ulong end_of_rom, const char *name, |
| 462 | struct cbfs_cachenode *node) |
| 463 | { |
| 464 | struct cbfs_priv priv; |
| 465 | void *start; |
| 466 | int ret; |
| 467 | |
| 468 | ret = file_cbfs_load_header(&priv, end_of_rom); |
| 469 | if (ret) |
| 470 | return ret; |
| 471 | start = priv.start; |
| 472 | |
| 473 | return find_uncached(&priv, name, start, node); |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Simon Glass | 333a97d | 2020-05-24 17:38:23 -0600 | [diff] [blame] | 476 | int file_cbfs_find_uncached_base(ulong base, const char *name, |
| 477 | struct cbfs_cachenode *node) |
| 478 | { |
| 479 | struct cbfs_priv priv; |
| 480 | int ret; |
| 481 | |
| 482 | ret = cbfs_load_header_ptr(&priv, base); |
| 483 | if (ret) |
| 484 | return ret; |
| 485 | |
| 486 | return find_uncached(&priv, name, (void *)base, node); |
| 487 | } |
| 488 | |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 489 | const char *file_cbfs_name(const struct cbfs_cachenode *file) |
| 490 | { |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 491 | cbfs_s.result = CBFS_SUCCESS; |
| 492 | |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 493 | return file->name; |
| 494 | } |
| 495 | |
| 496 | u32 file_cbfs_size(const struct cbfs_cachenode *file) |
| 497 | { |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 498 | cbfs_s.result = CBFS_SUCCESS; |
| 499 | |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 500 | return file->data_length; |
| 501 | } |
| 502 | |
| 503 | u32 file_cbfs_type(const struct cbfs_cachenode *file) |
| 504 | { |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 505 | cbfs_s.result = CBFS_SUCCESS; |
| 506 | |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 507 | return file->type; |
| 508 | } |
| 509 | |
| 510 | long file_cbfs_read(const struct cbfs_cachenode *file, void *buffer, |
| 511 | unsigned long maxsize) |
| 512 | { |
| 513 | u32 size; |
| 514 | |
| 515 | size = file->data_length; |
| 516 | if (maxsize && size > maxsize) |
| 517 | size = maxsize; |
| 518 | |
| 519 | memcpy(buffer, file->data, size); |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 520 | cbfs_s.result = CBFS_SUCCESS; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 521 | |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 522 | return size; |
| 523 | } |