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