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> |
| 8 | #include <malloc.h> |
| 9 | #include <asm/byteorder.h> |
| 10 | |
Simon Glass | e78f9bb | 2020-05-24 17:38:17 -0600 | [diff] [blame] | 11 | /* Offset of master header from the start of a coreboot ROM */ |
| 12 | #define MASTER_HDR_OFFSET 0x38 |
| 13 | |
Simon Glass | 7b5ee96 | 2019-08-14 19:56:11 -0600 | [diff] [blame] | 14 | static const u32 good_magic = 0x4f524243; |
| 15 | static const u8 good_file_magic[] = "LARCHIVE"; |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 16 | |
Simon Glass | ec36d7a | 2020-05-24 17:38:20 -0600 | [diff] [blame] | 17 | /** |
| 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 Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 26 | struct cbfs_priv { |
Simon Glass | 2029a6d | 2020-05-24 17:38:14 -0600 | [diff] [blame] | 27 | bool initialized; |
Simon Glass | ec36d7a | 2020-05-24 17:38:20 -0600 | [diff] [blame] | 28 | void *start; |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 29 | struct cbfs_header header; |
| 30 | struct cbfs_cachenode *file_cache; |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 31 | enum cbfs_result result; |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | static struct cbfs_priv cbfs_s; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 35 | |
| 36 | const char *file_cbfs_error(void) |
| 37 | { |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 38 | switch (cbfs_s.result) { |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 39 | 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 Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 54 | enum cbfs_result cbfs_get_result(void) |
| 55 | { |
| 56 | return cbfs_s.result; |
| 57 | } |
| 58 | |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 59 | /* Do endian conversion on the CBFS header structure. */ |
| 60 | static 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. */ |
| 71 | static 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 Glass | 87d1504 | 2019-07-08 13:18:22 -0600 | [diff] [blame] | 77 | dest->attributes_offset = be32_to_cpu(src->attributes_offset); |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 78 | 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 Glass | efbd7b1 | 2019-08-14 19:56:15 -0600 | [diff] [blame] | 89 | * @param new_node A pointer to the file structure to load. |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 90 | * @param used A pointer to the count of of bytes scanned through, |
| 91 | * including the file if one is found. |
| 92 | * |
Simon Glass | c72d4a8 | 2020-05-24 17:38:15 -0600 | [diff] [blame] | 93 | * @return 0 if a file is found, -ENOENT if one isn't, -EBADF if a bad header |
| 94 | * is found. |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 95 | */ |
Simon Glass | 6ca4c38 | 2020-05-24 17:38:19 -0600 | [diff] [blame] | 96 | static int file_cbfs_next_file(struct cbfs_priv *priv, void *start, int size, |
Simon Glass | c72d4a8 | 2020-05-24 17:38:15 -0600 | [diff] [blame] | 97 | int align, struct cbfs_cachenode *new_node, |
| 98 | int *used) |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 99 | { |
| 100 | struct cbfs_fileheader header; |
| 101 | |
| 102 | *used = 0; |
| 103 | |
| 104 | while (size >= align) { |
Simon Glass | 6ca4c38 | 2020-05-24 17:38:19 -0600 | [diff] [blame] | 105 | const struct cbfs_fileheader *file_header = start; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 106 | u32 name_len; |
| 107 | u32 step; |
| 108 | |
| 109 | /* Check if there's a file here. */ |
Simon Glass | efbd7b1 | 2019-08-14 19:56:15 -0600 | [diff] [blame] | 110 | if (memcmp(good_file_magic, &file_header->magic, |
| 111 | sizeof(file_header->magic))) { |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 112 | *used += align; |
| 113 | size -= align; |
| 114 | start += align; |
| 115 | continue; |
| 116 | } |
| 117 | |
Simon Glass | efbd7b1 | 2019-08-14 19:56:15 -0600 | [diff] [blame] | 118 | swap_file_header(&header, file_header); |
Christian Gmeiner | 81ff3bb | 2018-12-22 01:55:48 -0800 | [diff] [blame] | 119 | if (header.offset < sizeof(struct cbfs_fileheader)) { |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 120 | priv->result = CBFS_BAD_FILE; |
Simon Glass | c72d4a8 | 2020-05-24 17:38:15 -0600 | [diff] [blame] | 121 | return -EBADF; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 122 | } |
Simon Glass | efbd7b1 | 2019-08-14 19:56:15 -0600 | [diff] [blame] | 123 | 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 K | 9999bb8 | 2016-08-08 20:32:15 -0700 | [diff] [blame] | 127 | name_len = header.offset - sizeof(struct cbfs_fileheader); |
Simon Glass | efbd7b1 | 2019-08-14 19:56:15 -0600 | [diff] [blame] | 128 | new_node->name = (char *)file_header + |
Yaroslav K | 9999bb8 | 2016-08-08 20:32:15 -0700 | [diff] [blame] | 129 | sizeof(struct cbfs_fileheader); |
Simon Glass | efbd7b1 | 2019-08-14 19:56:15 -0600 | [diff] [blame] | 130 | new_node->name_length = name_len; |
| 131 | new_node->attributes_offset = header.attributes_offset; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 132 | |
| 133 | step = header.len; |
| 134 | if (step % align) |
| 135 | step = step + align - step % align; |
| 136 | |
| 137 | *used += step; |
Simon Glass | c72d4a8 | 2020-05-24 17:38:15 -0600 | [diff] [blame] | 138 | return 0; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 139 | } |
Simon Glass | c72d4a8 | 2020-05-24 17:38:15 -0600 | [diff] [blame] | 140 | |
| 141 | return -ENOENT; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /* Look through a CBFS instance and copy file metadata into regular memory. */ |
Simon Glass | ec36d7a | 2020-05-24 17:38:20 -0600 | [diff] [blame] | 145 | 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] | 146 | { |
| 147 | struct cbfs_cachenode *cache_node; |
Simon Glass | efbd7b1 | 2019-08-14 19:56:15 -0600 | [diff] [blame] | 148 | struct cbfs_cachenode *new_node; |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 149 | struct cbfs_cachenode **cache_tail = &priv->file_cache; |
Simon Glass | ec36d7a | 2020-05-24 17:38:20 -0600 | [diff] [blame] | 150 | void *start; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 151 | |
| 152 | /* Clear out old information. */ |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 153 | cache_node = priv->file_cache; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 154 | while (cache_node) { |
Simon Glass | efbd7b1 | 2019-08-14 19:56:15 -0600 | [diff] [blame] | 155 | struct cbfs_cachenode *old_node = cache_node; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 156 | cache_node = cache_node->next; |
Simon Glass | efbd7b1 | 2019-08-14 19:56:15 -0600 | [diff] [blame] | 157 | free(old_node); |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 158 | } |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 159 | priv->file_cache = NULL; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 160 | |
Simon Glass | ec36d7a | 2020-05-24 17:38:20 -0600 | [diff] [blame] | 161 | start = priv->start; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 162 | while (size >= align) { |
Simon Glass | c72d4a8 | 2020-05-24 17:38:15 -0600 | [diff] [blame] | 163 | int used; |
Simon Glass | 650b241 | 2020-05-24 17:38:12 -0600 | [diff] [blame] | 164 | int ret; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 165 | |
Simon Glass | efbd7b1 | 2019-08-14 19:56:15 -0600 | [diff] [blame] | 166 | new_node = (struct cbfs_cachenode *) |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 167 | malloc(sizeof(struct cbfs_cachenode)); |
Simon Glass | c72d4a8 | 2020-05-24 17:38:15 -0600 | [diff] [blame] | 168 | if (!new_node) |
| 169 | return -ENOMEM; |
Simon Glass | 650b241 | 2020-05-24 17:38:12 -0600 | [diff] [blame] | 170 | ret = file_cbfs_next_file(priv, start, size, align, new_node, |
| 171 | &used); |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 172 | |
Simon Glass | 650b241 | 2020-05-24 17:38:12 -0600 | [diff] [blame] | 173 | if (ret < 0) { |
Simon Glass | efbd7b1 | 2019-08-14 19:56:15 -0600 | [diff] [blame] | 174 | free(new_node); |
Simon Glass | c72d4a8 | 2020-05-24 17:38:15 -0600 | [diff] [blame] | 175 | if (ret == -ENOENT) |
| 176 | break; |
| 177 | return ret; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 178 | } |
Simon Glass | efbd7b1 | 2019-08-14 19:56:15 -0600 | [diff] [blame] | 179 | *cache_tail = new_node; |
| 180 | cache_tail = &new_node->next; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 181 | |
| 182 | size -= used; |
| 183 | start += used; |
| 184 | } |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 185 | priv->result = CBFS_SUCCESS; |
Simon Glass | c72d4a8 | 2020-05-24 17:38:15 -0600 | [diff] [blame] | 186 | |
| 187 | return 0; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Simon Glass | 5154100 | 2020-05-24 17:38:18 -0600 | [diff] [blame] | 190 | /** |
| 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 | */ |
| 199 | static int load_header(struct cbfs_priv *priv, ulong addr) |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 200 | { |
Simon Glass | 9ad16d6 | 2020-05-24 17:38:16 -0600 | [diff] [blame] | 201 | struct cbfs_header *header = &priv->header; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 202 | struct cbfs_header *header_in_rom; |
| 203 | |
Simon Glass | 5154100 | 2020-05-24 17:38:18 -0600 | [diff] [blame] | 204 | memset(priv, '\0', sizeof(*priv)); |
| 205 | header_in_rom = (struct cbfs_header *)addr; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 206 | swap_header(header, header_in_rom); |
| 207 | |
| 208 | if (header->magic != good_magic || header->offset > |
| 209 | header->rom_size - header->boot_block_size) { |
Simon Glass | 9ad16d6 | 2020-05-24 17:38:16 -0600 | [diff] [blame] | 210 | priv->result = CBFS_BAD_HEADER; |
Simon Glass | 5154100 | 2020-05-24 17:38:18 -0600 | [diff] [blame] | 211 | return -ENXIO; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 212 | } |
Simon Glass | 5154100 | 2020-05-24 17:38:18 -0600 | [diff] [blame] | 213 | |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 214 | return 0; |
| 215 | } |
| 216 | |
Simon Glass | 5154100 | 2020-05-24 17:38:18 -0600 | [diff] [blame] | 217 | /** |
| 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 | */ |
| 224 | 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] | 225 | { |
Simon Glass | 5154100 | 2020-05-24 17:38:18 -0600 | [diff] [blame] | 226 | int offset = *(u32 *)(end_of_rom - 3); |
Simon Glass | ec36d7a | 2020-05-24 17:38:20 -0600 | [diff] [blame] | 227 | int ret; |
| 228 | |
| 229 | 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); |
Simon Glass | aa4274e | 2019-08-14 19:56:14 -0600 | [diff] [blame] | 233 | |
Simon Glass | ec36d7a | 2020-05-24 17:38:20 -0600 | [diff] [blame] | 234 | return 0; |
Simon Glass | 5154100 | 2020-05-24 17:38:18 -0600 | [diff] [blame] | 235 | } |
Simon Glass | aa4274e | 2019-08-14 19:56:14 -0600 | [diff] [blame] | 236 | |
Simon Glass | 5154100 | 2020-05-24 17:38:18 -0600 | [diff] [blame] | 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 | */ |
| 244 | static int cbfs_load_header_ptr(struct cbfs_priv *priv, ulong base) |
| 245 | { |
Simon Glass | ec36d7a | 2020-05-24 17:38:20 -0600 | [diff] [blame] | 246 | 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 Glass | aa4274e | 2019-08-14 19:56:14 -0600 | [diff] [blame] | 254 | } |
| 255 | |
Simon Glass | 0492317 | 2020-05-24 17:38:21 -0600 | [diff] [blame] | 256 | static int cbfs_init(struct cbfs_priv *priv, ulong end_of_rom) |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 257 | { |
Simon Glass | 0492317 | 2020-05-24 17:38:21 -0600 | [diff] [blame] | 258 | int ret; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 259 | |
Simon Glass | 0492317 | 2020-05-24 17:38:21 -0600 | [diff] [blame] | 260 | 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 Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 271 | } |
| 272 | |
Simon Glass | 0492317 | 2020-05-24 17:38:21 -0600 | [diff] [blame] | 273 | int file_cbfs_init(ulong end_of_rom) |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 274 | { |
Simon Glass | 0492317 | 2020-05-24 17:38:21 -0600 | [diff] [blame] | 275 | return cbfs_init(&cbfs_s, end_of_rom); |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Simon Glass | aa4274e | 2019-08-14 19:56:14 -0600 | [diff] [blame] | 278 | int 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 Glass | 5154100 | 2020-05-24 17:38:18 -0600 | [diff] [blame] | 285 | * valid. |
Simon Glass | aa4274e | 2019-08-14 19:56:14 -0600 | [diff] [blame] | 286 | */ |
Simon Glass | 5154100 | 2020-05-24 17:38:18 -0600 | [diff] [blame] | 287 | ret = cbfs_load_header_ptr(priv, base); |
Simon Glass | aa4274e | 2019-08-14 19:56:14 -0600 | [diff] [blame] | 288 | if (ret) |
| 289 | return ret; |
| 290 | |
Simon Glass | ec36d7a | 2020-05-24 17:38:20 -0600 | [diff] [blame] | 291 | file_cbfs_fill_cache(priv, priv->header.rom_size, priv->header.align); |
Simon Glass | aa4274e | 2019-08-14 19:56:14 -0600 | [diff] [blame] | 292 | if (priv->result != CBFS_SUCCESS) |
| 293 | return -EINVAL; |
| 294 | |
Simon Glass | 2029a6d | 2020-05-24 17:38:14 -0600 | [diff] [blame] | 295 | priv->initialized = true; |
Simon Glass | aa4274e | 2019-08-14 19:56:14 -0600 | [diff] [blame] | 296 | 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 Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 305 | const struct cbfs_header *file_cbfs_get_header(void) |
| 306 | { |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 307 | struct cbfs_priv *priv = &cbfs_s; |
| 308 | |
| 309 | if (priv->initialized) { |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 310 | priv->result = CBFS_SUCCESS; |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 311 | return &priv->header; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 312 | } else { |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 313 | priv->result = CBFS_NOT_INITIALIZED; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 314 | return NULL; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | const struct cbfs_cachenode *file_cbfs_get_first(void) |
| 319 | { |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 320 | struct cbfs_priv *priv = &cbfs_s; |
| 321 | |
| 322 | if (!priv->initialized) { |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 323 | priv->result = CBFS_NOT_INITIALIZED; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 324 | return NULL; |
| 325 | } else { |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 326 | priv->result = CBFS_SUCCESS; |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 327 | return priv->file_cache; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | |
| 331 | void file_cbfs_get_next(const struct cbfs_cachenode **file) |
| 332 | { |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 333 | struct cbfs_priv *priv = &cbfs_s; |
| 334 | |
| 335 | if (!priv->initialized) { |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 336 | priv->result = CBFS_NOT_INITIALIZED; |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 337 | *file = NULL; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 338 | return; |
| 339 | } |
| 340 | |
| 341 | if (*file) |
| 342 | *file = (*file)->next; |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 343 | priv->result = CBFS_SUCCESS; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 346 | const struct cbfs_cachenode *cbfs_find_file(struct cbfs_priv *priv, |
| 347 | const char *name) |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 348 | { |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 349 | struct cbfs_cachenode *cache_node = priv->file_cache; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 350 | |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 351 | if (!priv->initialized) { |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 352 | priv->result = CBFS_NOT_INITIALIZED; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 353 | 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 Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 362 | priv->result = CBFS_FILE_NOT_FOUND; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 363 | else |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 364 | priv->result = CBFS_SUCCESS; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 365 | |
| 366 | return cache_node; |
| 367 | } |
| 368 | |
Simon Glass | bc94f0a | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 369 | const struct cbfs_cachenode *file_cbfs_find(const char *name) |
| 370 | { |
| 371 | return cbfs_find_file(&cbfs_s, name); |
| 372 | } |
| 373 | |
Simon Glass | 9c42215 | 2020-05-24 17:38:22 -0600 | [diff] [blame] | 374 | static int find_uncached(struct cbfs_priv *priv, const char *name, void *start, |
| 375 | struct cbfs_cachenode *node) |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 376 | { |
Simon Glass | 9c42215 | 2020-05-24 17:38:22 -0600 | [diff] [blame] | 377 | int size = priv->header.rom_size; |
| 378 | int align = priv->header.align; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 379 | |
| 380 | while (size >= align) { |
Simon Glass | c72d4a8 | 2020-05-24 17:38:15 -0600 | [diff] [blame] | 381 | int used; |
Simon Glass | 9c42215 | 2020-05-24 17:38:22 -0600 | [diff] [blame] | 382 | int ret; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 383 | |
Simon Glass | 9c42215 | 2020-05-24 17:38:22 -0600 | [diff] [blame] | 384 | ret = file_cbfs_next_file(priv, start, size, align, node, |
Simon Glass | 650b241 | 2020-05-24 17:38:12 -0600 | [diff] [blame] | 385 | &used); |
Simon Glass | c72d4a8 | 2020-05-24 17:38:15 -0600 | [diff] [blame] | 386 | if (ret == -ENOENT) |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 387 | break; |
Simon Glass | c72d4a8 | 2020-05-24 17:38:15 -0600 | [diff] [blame] | 388 | else if (ret) |
Simon Glass | 9c42215 | 2020-05-24 17:38:22 -0600 | [diff] [blame] | 389 | return ret; |
| 390 | if (!strcmp(name, node->name)) |
| 391 | return 0; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 392 | |
| 393 | size -= used; |
| 394 | start += used; |
| 395 | } |
Simon Glass | 9c42215 | 2020-05-24 17:38:22 -0600 | [diff] [blame] | 396 | priv->result = CBFS_FILE_NOT_FOUND; |
| 397 | |
| 398 | return -ENOENT; |
| 399 | } |
| 400 | |
| 401 | int 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 Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Simon Glass | 333a97d | 2020-05-24 17:38:23 -0600 | [diff] [blame^] | 416 | int file_cbfs_find_uncached_base(ulong base, const char *name, |
| 417 | struct cbfs_cachenode *node) |
| 418 | { |
| 419 | struct cbfs_priv priv; |
| 420 | int ret; |
| 421 | |
| 422 | ret = cbfs_load_header_ptr(&priv, base); |
| 423 | if (ret) |
| 424 | return ret; |
| 425 | |
| 426 | return find_uncached(&priv, name, (void *)base, node); |
| 427 | } |
| 428 | |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 429 | const char *file_cbfs_name(const struct cbfs_cachenode *file) |
| 430 | { |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 431 | cbfs_s.result = CBFS_SUCCESS; |
| 432 | |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 433 | return file->name; |
| 434 | } |
| 435 | |
| 436 | u32 file_cbfs_size(const struct cbfs_cachenode *file) |
| 437 | { |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 438 | cbfs_s.result = CBFS_SUCCESS; |
| 439 | |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 440 | return file->data_length; |
| 441 | } |
| 442 | |
| 443 | u32 file_cbfs_type(const struct cbfs_cachenode *file) |
| 444 | { |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 445 | cbfs_s.result = CBFS_SUCCESS; |
| 446 | |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 447 | return file->type; |
| 448 | } |
| 449 | |
| 450 | long file_cbfs_read(const struct cbfs_cachenode *file, void *buffer, |
| 451 | unsigned long maxsize) |
| 452 | { |
| 453 | u32 size; |
| 454 | |
| 455 | size = file->data_length; |
| 456 | if (maxsize && size > maxsize) |
| 457 | size = maxsize; |
| 458 | |
| 459 | memcpy(buffer, file->data, size); |
Simon Glass | f152bf0 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 460 | cbfs_s.result = CBFS_SUCCESS; |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 461 | |
Gabe Black | 7f8574c | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 462 | return size; |
| 463 | } |