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 | 1567db0 | 2021-03-15 18:00:14 +1300 | [diff] [blame] | 170 | ret = fill_node(node, start, &header); |
| 171 | if (ret) { |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 172 | priv->result = CBFS_BAD_FILE; |
Simon Glass | 1567db0 | 2021-03-15 18:00:14 +1300 | [diff] [blame] | 173 | return log_msg_ret("fill", ret); |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 174 | } |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 175 | |
Simon Glass | 0352edd | 2021-03-15 18:00:15 +1300 | [diff] [blame] | 176 | *used += ALIGN(header.len, align); |
Simon Glass | c72d4a8 | 2020-05-24 17:38:15 -0600 | [diff] [blame] | 177 | return 0; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 178 | } |
Simon Glass | c72d4a8 | 2020-05-24 17:38:15 -0600 | [diff] [blame] | 179 | |
| 180 | return -ENOENT; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | /* Look through a CBFS instance and copy file metadata into regular memory. */ |
Simon Glass | ec36d7a | 2020-05-24 17:38:20 -0600 | [diff] [blame] | 184 | 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] | 185 | { |
| 186 | struct cbfs_cachenode *cache_node; |
Simon Glass | 7a02c81 | 2021-03-15 18:00:10 +1300 | [diff] [blame] | 187 | struct cbfs_cachenode *node; |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 188 | struct cbfs_cachenode **cache_tail = &priv->file_cache; |
Simon Glass | ec36d7a | 2020-05-24 17:38:20 -0600 | [diff] [blame] | 189 | void *start; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 190 | |
| 191 | /* Clear out old information. */ |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 192 | cache_node = priv->file_cache; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 193 | while (cache_node) { |
Simon Glass | efbd7b1 | 2019-08-14 19:56:15 -0600 | [diff] [blame] | 194 | struct cbfs_cachenode *old_node = cache_node; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 195 | cache_node = cache_node->next; |
Simon Glass | efbd7b1 | 2019-08-14 19:56:15 -0600 | [diff] [blame] | 196 | free(old_node); |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 197 | } |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 198 | priv->file_cache = NULL; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 199 | |
Simon Glass | ec36d7a | 2020-05-24 17:38:20 -0600 | [diff] [blame] | 200 | start = priv->start; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 201 | while (size >= align) { |
Simon Glass | c72d4a8 | 2020-05-24 17:38:15 -0600 | [diff] [blame] | 202 | int used; |
Simon Glass | 650b241 | 2020-05-24 17:38:12 -0600 | [diff] [blame] | 203 | int ret; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 204 | |
Simon Glass | 7907c4b | 2021-03-15 18:00:17 +1300 | [diff] [blame] | 205 | node = malloc(sizeof(struct cbfs_cachenode)); |
Simon Glass | 7a02c81 | 2021-03-15 18:00:10 +1300 | [diff] [blame] | 206 | if (!node) |
Simon Glass | c72d4a8 | 2020-05-24 17:38:15 -0600 | [diff] [blame] | 207 | return -ENOMEM; |
Simon Glass | 7a02c81 | 2021-03-15 18:00:10 +1300 | [diff] [blame] | 208 | ret = file_cbfs_next_file(priv, start, size, align, node, |
Simon Glass | 650b241 | 2020-05-24 17:38:12 -0600 | [diff] [blame] | 209 | &used); |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 210 | |
Simon Glass | 650b241 | 2020-05-24 17:38:12 -0600 | [diff] [blame] | 211 | if (ret < 0) { |
Simon Glass | 7a02c81 | 2021-03-15 18:00:10 +1300 | [diff] [blame] | 212 | free(node); |
Simon Glass | c72d4a8 | 2020-05-24 17:38:15 -0600 | [diff] [blame] | 213 | if (ret == -ENOENT) |
| 214 | break; |
| 215 | return ret; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 216 | } |
Simon Glass | 7a02c81 | 2021-03-15 18:00:10 +1300 | [diff] [blame] | 217 | *cache_tail = node; |
| 218 | cache_tail = &node->next; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 219 | |
| 220 | size -= used; |
| 221 | start += used; |
| 222 | } |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 223 | priv->result = CBFS_SUCCESS; |
Simon Glass | c72d4a8 | 2020-05-24 17:38:15 -0600 | [diff] [blame] | 224 | |
| 225 | return 0; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Simon Glass | 5154100 | 2020-05-24 17:38:18 -0600 | [diff] [blame] | 228 | /** |
| 229 | * load_header() - Load the CBFS header |
| 230 | * |
| 231 | * Get the CBFS header out of the ROM and do endian conversion. |
| 232 | * |
| 233 | * @priv: Private data, which is inited by this function |
| 234 | * @addr: Address of CBFS header in memory-mapped SPI flash |
| 235 | * @return 0 if OK, -ENXIO if the header is bad |
| 236 | */ |
| 237 | static int load_header(struct cbfs_priv *priv, ulong addr) |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 238 | { |
Simon Glass | 9ad16d6 | 2020-05-24 17:38:16 -0600 | [diff] [blame] | 239 | struct cbfs_header *header = &priv->header; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 240 | struct cbfs_header *header_in_rom; |
| 241 | |
Simon Glass | 5154100 | 2020-05-24 17:38:18 -0600 | [diff] [blame] | 242 | memset(priv, '\0', sizeof(*priv)); |
| 243 | header_in_rom = (struct cbfs_header *)addr; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 244 | swap_header(header, header_in_rom); |
| 245 | |
| 246 | if (header->magic != good_magic || header->offset > |
| 247 | header->rom_size - header->boot_block_size) { |
Simon Glass | 9ad16d6 | 2020-05-24 17:38:16 -0600 | [diff] [blame] | 248 | priv->result = CBFS_BAD_HEADER; |
Simon Glass | 5154100 | 2020-05-24 17:38:18 -0600 | [diff] [blame] | 249 | return -ENXIO; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 250 | } |
Simon Glass | 5154100 | 2020-05-24 17:38:18 -0600 | [diff] [blame] | 251 | |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 252 | return 0; |
| 253 | } |
| 254 | |
Simon Glass | 5154100 | 2020-05-24 17:38:18 -0600 | [diff] [blame] | 255 | /** |
| 256 | * file_cbfs_load_header() - Get the CBFS header out of the ROM, given the end |
| 257 | * |
| 258 | * @priv: Private data, which is inited by this function |
| 259 | * @end_of_rom: Address of the last byte of the ROM (typically 0xffffffff) |
| 260 | * @return 0 if OK, -ENXIO if the header is bad |
| 261 | */ |
| 262 | 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] | 263 | { |
Simon Glass | 5154100 | 2020-05-24 17:38:18 -0600 | [diff] [blame] | 264 | int offset = *(u32 *)(end_of_rom - 3); |
Simon Glass | ec36d7a | 2020-05-24 17:38:20 -0600 | [diff] [blame] | 265 | int ret; |
| 266 | |
| 267 | ret = load_header(priv, end_of_rom + offset + 1); |
| 268 | if (ret) |
| 269 | return ret; |
| 270 | priv->start = (void *)(end_of_rom + 1 - priv->header.rom_size); |
Simon Glass | aa4274e | 2019-08-14 19:56:14 -0600 | [diff] [blame] | 271 | |
Simon Glass | ec36d7a | 2020-05-24 17:38:20 -0600 | [diff] [blame] | 272 | return 0; |
Simon Glass | 5154100 | 2020-05-24 17:38:18 -0600 | [diff] [blame] | 273 | } |
Simon Glass | aa4274e | 2019-08-14 19:56:14 -0600 | [diff] [blame] | 274 | |
Simon Glass | 5154100 | 2020-05-24 17:38:18 -0600 | [diff] [blame] | 275 | /** |
| 276 | * cbfs_load_header_ptr() - Get the CBFS header out of the ROM, given the base |
| 277 | * |
| 278 | * @priv: Private data, which is inited by this function |
| 279 | * @base: Address of the first byte of the ROM (e.g. 0xff000000) |
| 280 | * @return 0 if OK, -ENXIO if the header is bad |
| 281 | */ |
| 282 | static int cbfs_load_header_ptr(struct cbfs_priv *priv, ulong base) |
| 283 | { |
Simon Glass | ec36d7a | 2020-05-24 17:38:20 -0600 | [diff] [blame] | 284 | int ret; |
| 285 | |
| 286 | ret = load_header(priv, base + MASTER_HDR_OFFSET); |
| 287 | if (ret) |
| 288 | return ret; |
| 289 | priv->start = (void *)base; |
| 290 | |
| 291 | return 0; |
Simon Glass | aa4274e | 2019-08-14 19:56:14 -0600 | [diff] [blame] | 292 | } |
| 293 | |
Simon Glass | 0492317 | 2020-05-24 17:38:21 -0600 | [diff] [blame] | 294 | static int cbfs_init(struct cbfs_priv *priv, ulong end_of_rom) |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 295 | { |
Simon Glass | 0492317 | 2020-05-24 17:38:21 -0600 | [diff] [blame] | 296 | int ret; |
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 | ret = file_cbfs_load_header(priv, end_of_rom); |
| 299 | if (ret) |
| 300 | return ret; |
| 301 | |
| 302 | ret = file_cbfs_fill_cache(priv, priv->header.rom_size, |
| 303 | priv->header.align); |
| 304 | if (ret) |
| 305 | return ret; |
| 306 | priv->initialized = true; |
| 307 | |
| 308 | return 0; |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 309 | } |
| 310 | |
Simon Glass | 0492317 | 2020-05-24 17:38:21 -0600 | [diff] [blame] | 311 | int file_cbfs_init(ulong end_of_rom) |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 312 | { |
Simon Glass | 0492317 | 2020-05-24 17:38:21 -0600 | [diff] [blame] | 313 | return cbfs_init(&cbfs_s, end_of_rom); |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Simon Glass | 9464909 | 2021-03-15 18:00:12 +1300 | [diff] [blame] | 316 | int cbfs_init_mem(ulong base, ulong size, bool require_hdr, |
| 317 | struct cbfs_priv **privp) |
Simon Glass | aa4274e | 2019-08-14 19:56:14 -0600 | [diff] [blame] | 318 | { |
| 319 | struct cbfs_priv priv_s, *priv = &priv_s; |
| 320 | int ret; |
| 321 | |
| 322 | /* |
Simon Glass | 9464909 | 2021-03-15 18:00:12 +1300 | [diff] [blame] | 323 | * Use a local variable to start with until we know that the * CBFS is |
| 324 | * valid. Note that size is detected from the header, if present, |
| 325 | * meaning the parameter is ignored. |
Simon Glass | aa4274e | 2019-08-14 19:56:14 -0600 | [diff] [blame] | 326 | */ |
Simon Glass | 5154100 | 2020-05-24 17:38:18 -0600 | [diff] [blame] | 327 | ret = cbfs_load_header_ptr(priv, base); |
Simon Glass | 9464909 | 2021-03-15 18:00:12 +1300 | [diff] [blame] | 328 | if (ret) { |
| 329 | if (require_hdr || size == CBFS_SIZE_UNKNOWN) |
| 330 | return ret; |
| 331 | memset(priv, '\0', sizeof(struct cbfs_priv)); |
| 332 | priv->header.rom_size = size; |
| 333 | priv->header.align = CBFS_ALIGN_SIZE; |
| 334 | priv->start = (void *)base; |
| 335 | } |
Simon Glass | aa4274e | 2019-08-14 19:56:14 -0600 | [diff] [blame] | 336 | |
Simon Glass | ba06a1f | 2020-05-24 17:38:24 -0600 | [diff] [blame] | 337 | ret = file_cbfs_fill_cache(priv, priv->header.rom_size, |
| 338 | priv->header.align); |
| 339 | if (ret) |
| 340 | return log_msg_ret("fill", ret); |
Simon Glass | aa4274e | 2019-08-14 19:56:14 -0600 | [diff] [blame] | 341 | |
Simon Glass | 2029a6d | 2020-05-24 17:38:14 -0600 | [diff] [blame] | 342 | priv->initialized = true; |
Simon Glass | aa4274e | 2019-08-14 19:56:14 -0600 | [diff] [blame] | 343 | priv = malloc(sizeof(priv_s)); |
| 344 | if (!priv) |
| 345 | return -ENOMEM; |
| 346 | memcpy(priv, &priv_s, sizeof(priv_s)); |
| 347 | *privp = priv; |
| 348 | |
| 349 | return 0; |
| 350 | } |
| 351 | |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 352 | const struct cbfs_header *file_cbfs_get_header(void) |
| 353 | { |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 354 | struct cbfs_priv *priv = &cbfs_s; |
| 355 | |
| 356 | if (priv->initialized) { |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 357 | priv->result = CBFS_SUCCESS; |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 358 | return &priv->header; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 359 | } else { |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 360 | priv->result = CBFS_NOT_INITIALIZED; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 361 | return NULL; |
| 362 | } |
| 363 | } |
| 364 | |
Simon Glass | f8142fb | 2021-03-15 18:00:13 +1300 | [diff] [blame] | 365 | const struct cbfs_cachenode *cbfs_get_first(const struct cbfs_priv *priv) |
| 366 | { |
| 367 | return priv->file_cache; |
| 368 | } |
| 369 | |
| 370 | void cbfs_get_next(const struct cbfs_cachenode **filep) |
| 371 | { |
| 372 | if (*filep) |
| 373 | *filep = (*filep)->next; |
| 374 | } |
| 375 | |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 376 | const struct cbfs_cachenode *file_cbfs_get_first(void) |
| 377 | { |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 378 | struct cbfs_priv *priv = &cbfs_s; |
| 379 | |
| 380 | if (!priv->initialized) { |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 381 | priv->result = CBFS_NOT_INITIALIZED; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 382 | return NULL; |
| 383 | } else { |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 384 | priv->result = CBFS_SUCCESS; |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 385 | return priv->file_cache; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | |
| 389 | void file_cbfs_get_next(const struct cbfs_cachenode **file) |
| 390 | { |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 391 | struct cbfs_priv *priv = &cbfs_s; |
| 392 | |
| 393 | if (!priv->initialized) { |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 394 | priv->result = CBFS_NOT_INITIALIZED; |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 395 | *file = NULL; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 396 | return; |
| 397 | } |
| 398 | |
| 399 | if (*file) |
| 400 | *file = (*file)->next; |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 401 | priv->result = CBFS_SUCCESS; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 404 | const struct cbfs_cachenode *cbfs_find_file(struct cbfs_priv *priv, |
| 405 | const char *name) |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 406 | { |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 407 | struct cbfs_cachenode *cache_node = priv->file_cache; |
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 | if (!priv->initialized) { |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 410 | priv->result = CBFS_NOT_INITIALIZED; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 411 | return NULL; |
| 412 | } |
| 413 | |
| 414 | while (cache_node) { |
| 415 | if (!strcmp(name, cache_node->name)) |
| 416 | break; |
| 417 | cache_node = cache_node->next; |
| 418 | } |
| 419 | if (!cache_node) |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 420 | priv->result = CBFS_FILE_NOT_FOUND; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 421 | else |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 422 | priv->result = CBFS_SUCCESS; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 423 | |
| 424 | return cache_node; |
| 425 | } |
| 426 | |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 427 | const struct cbfs_cachenode *file_cbfs_find(const char *name) |
| 428 | { |
| 429 | return cbfs_find_file(&cbfs_s, name); |
| 430 | } |
| 431 | |
Simon Glass | 9c42215 | 2020-05-24 17:38:22 -0600 | [diff] [blame] | 432 | static int find_uncached(struct cbfs_priv *priv, const char *name, void *start, |
| 433 | struct cbfs_cachenode *node) |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 434 | { |
Simon Glass | 9c42215 | 2020-05-24 17:38:22 -0600 | [diff] [blame] | 435 | int size = priv->header.rom_size; |
| 436 | int align = priv->header.align; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 437 | |
| 438 | while (size >= align) { |
Simon Glass | c72d4a8 | 2020-05-24 17:38:15 -0600 | [diff] [blame] | 439 | int used; |
Simon Glass | 9c42215 | 2020-05-24 17:38:22 -0600 | [diff] [blame] | 440 | int ret; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 441 | |
Simon Glass | 9c42215 | 2020-05-24 17:38:22 -0600 | [diff] [blame] | 442 | ret = file_cbfs_next_file(priv, start, size, align, node, |
Simon Glass | 650b241 | 2020-05-24 17:38:12 -0600 | [diff] [blame] | 443 | &used); |
Simon Glass | c72d4a8 | 2020-05-24 17:38:15 -0600 | [diff] [blame] | 444 | if (ret == -ENOENT) |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 445 | break; |
Simon Glass | c72d4a8 | 2020-05-24 17:38:15 -0600 | [diff] [blame] | 446 | else if (ret) |
Simon Glass | 9c42215 | 2020-05-24 17:38:22 -0600 | [diff] [blame] | 447 | return ret; |
| 448 | if (!strcmp(name, node->name)) |
| 449 | return 0; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 450 | |
| 451 | size -= used; |
| 452 | start += used; |
| 453 | } |
Simon Glass | 9c42215 | 2020-05-24 17:38:22 -0600 | [diff] [blame] | 454 | priv->result = CBFS_FILE_NOT_FOUND; |
| 455 | |
| 456 | return -ENOENT; |
| 457 | } |
| 458 | |
| 459 | int file_cbfs_find_uncached(ulong end_of_rom, const char *name, |
| 460 | struct cbfs_cachenode *node) |
| 461 | { |
| 462 | struct cbfs_priv priv; |
| 463 | void *start; |
| 464 | int ret; |
| 465 | |
| 466 | ret = file_cbfs_load_header(&priv, end_of_rom); |
| 467 | if (ret) |
| 468 | return ret; |
| 469 | start = priv.start; |
| 470 | |
| 471 | return find_uncached(&priv, name, start, node); |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Simon Glass | 333a97d | 2020-05-24 17:38:23 -0600 | [diff] [blame] | 474 | int file_cbfs_find_uncached_base(ulong base, const char *name, |
| 475 | struct cbfs_cachenode *node) |
| 476 | { |
| 477 | struct cbfs_priv priv; |
| 478 | int ret; |
| 479 | |
| 480 | ret = cbfs_load_header_ptr(&priv, base); |
| 481 | if (ret) |
| 482 | return ret; |
| 483 | |
| 484 | return find_uncached(&priv, name, (void *)base, node); |
| 485 | } |
| 486 | |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 487 | const char *file_cbfs_name(const struct cbfs_cachenode *file) |
| 488 | { |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 489 | cbfs_s.result = CBFS_SUCCESS; |
| 490 | |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 491 | return file->name; |
| 492 | } |
| 493 | |
| 494 | u32 file_cbfs_size(const struct cbfs_cachenode *file) |
| 495 | { |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 496 | cbfs_s.result = CBFS_SUCCESS; |
| 497 | |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 498 | return file->data_length; |
| 499 | } |
| 500 | |
| 501 | u32 file_cbfs_type(const struct cbfs_cachenode *file) |
| 502 | { |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 503 | cbfs_s.result = CBFS_SUCCESS; |
| 504 | |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 505 | return file->type; |
| 506 | } |
| 507 | |
| 508 | long file_cbfs_read(const struct cbfs_cachenode *file, void *buffer, |
| 509 | unsigned long maxsize) |
| 510 | { |
| 511 | u32 size; |
| 512 | |
| 513 | size = file->data_length; |
| 514 | if (maxsize && size > maxsize) |
| 515 | size = maxsize; |
| 516 | |
| 517 | memcpy(buffer, file->data, size); |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 518 | cbfs_s.result = CBFS_SUCCESS; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 519 | |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 520 | return size; |
| 521 | } |