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> |
| 8 | #include <malloc.h> |
| 9 | #include <asm/byteorder.h> |
| 10 | |
Simon Glass | fc7b9e1 | 2019-08-14 19:56:11 -0600 | [diff] [blame] | 11 | static const u32 good_magic = 0x4f524243; |
| 12 | static const u8 good_file_magic[] = "LARCHIVE"; |
Simon Glass | 02e4af6 | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 13 | |
| 14 | struct cbfs_priv { |
Simon Glass | 381e113 | 2020-05-24 17:38:14 -0600 | [diff] [blame^] | 15 | bool initialized; |
Simon Glass | 02e4af6 | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 16 | struct cbfs_header header; |
| 17 | struct cbfs_cachenode *file_cache; |
Simon Glass | c7f1693 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 18 | enum cbfs_result result; |
Simon Glass | 02e4af6 | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 19 | }; |
| 20 | |
| 21 | static struct cbfs_priv cbfs_s; |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 22 | |
| 23 | const char *file_cbfs_error(void) |
| 24 | { |
Simon Glass | c7f1693 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 25 | switch (cbfs_s.result) { |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 26 | case CBFS_SUCCESS: |
| 27 | return "Success"; |
| 28 | case CBFS_NOT_INITIALIZED: |
| 29 | return "CBFS not initialized"; |
| 30 | case CBFS_BAD_HEADER: |
| 31 | return "Bad CBFS header"; |
| 32 | case CBFS_BAD_FILE: |
| 33 | return "Bad CBFS file"; |
| 34 | case CBFS_FILE_NOT_FOUND: |
| 35 | return "File not found"; |
| 36 | default: |
| 37 | return "Unknown"; |
| 38 | } |
| 39 | } |
| 40 | |
Simon Glass | c7f1693 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 41 | enum cbfs_result cbfs_get_result(void) |
| 42 | { |
| 43 | return cbfs_s.result; |
| 44 | } |
| 45 | |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 46 | /* Do endian conversion on the CBFS header structure. */ |
| 47 | static void swap_header(struct cbfs_header *dest, struct cbfs_header *src) |
| 48 | { |
| 49 | dest->magic = be32_to_cpu(src->magic); |
| 50 | dest->version = be32_to_cpu(src->version); |
| 51 | dest->rom_size = be32_to_cpu(src->rom_size); |
| 52 | dest->boot_block_size = be32_to_cpu(src->boot_block_size); |
| 53 | dest->align = be32_to_cpu(src->align); |
| 54 | dest->offset = be32_to_cpu(src->offset); |
| 55 | } |
| 56 | |
| 57 | /* Do endian conversion on a CBFS file header. */ |
| 58 | static void swap_file_header(struct cbfs_fileheader *dest, |
| 59 | const struct cbfs_fileheader *src) |
| 60 | { |
| 61 | memcpy(&dest->magic, &src->magic, sizeof(dest->magic)); |
| 62 | dest->len = be32_to_cpu(src->len); |
| 63 | dest->type = be32_to_cpu(src->type); |
Simon Glass | ababe10 | 2019-07-08 13:18:22 -0600 | [diff] [blame] | 64 | dest->attributes_offset = be32_to_cpu(src->attributes_offset); |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 65 | dest->offset = be32_to_cpu(src->offset); |
| 66 | } |
| 67 | |
| 68 | /* |
| 69 | * Given a starting position in memory, scan forward, bounded by a size, and |
| 70 | * find the next valid CBFS file. No memory is allocated by this function. The |
| 71 | * caller is responsible for allocating space for the new file structure. |
| 72 | * |
| 73 | * @param start The location in memory to start from. |
| 74 | * @param size The size of the memory region to search. |
| 75 | * @param align The alignment boundaries to check on. |
Simon Glass | ad79d60 | 2019-08-14 19:56:15 -0600 | [diff] [blame] | 76 | * @param new_node A pointer to the file structure to load. |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 77 | * @param used A pointer to the count of of bytes scanned through, |
| 78 | * including the file if one is found. |
| 79 | * |
| 80 | * @return 1 if a file is found, 0 if one isn't. |
| 81 | */ |
Simon Glass | 02e4af6 | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 82 | static int file_cbfs_next_file(struct cbfs_priv *priv, u8 *start, u32 size, |
Simon Glass | ad79d60 | 2019-08-14 19:56:15 -0600 | [diff] [blame] | 83 | u32 align, struct cbfs_cachenode *new_node, |
Simon Glass | 02e4af6 | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 84 | u32 *used) |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 85 | { |
| 86 | struct cbfs_fileheader header; |
| 87 | |
| 88 | *used = 0; |
| 89 | |
| 90 | while (size >= align) { |
Simon Glass | ad79d60 | 2019-08-14 19:56:15 -0600 | [diff] [blame] | 91 | const struct cbfs_fileheader *file_header = |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 92 | (const struct cbfs_fileheader *)start; |
| 93 | u32 name_len; |
| 94 | u32 step; |
| 95 | |
| 96 | /* Check if there's a file here. */ |
Simon Glass | ad79d60 | 2019-08-14 19:56:15 -0600 | [diff] [blame] | 97 | if (memcmp(good_file_magic, &file_header->magic, |
| 98 | sizeof(file_header->magic))) { |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 99 | *used += align; |
| 100 | size -= align; |
| 101 | start += align; |
| 102 | continue; |
| 103 | } |
| 104 | |
Simon Glass | ad79d60 | 2019-08-14 19:56:15 -0600 | [diff] [blame] | 105 | swap_file_header(&header, file_header); |
Christian Gmeiner | 9914c73 | 2018-12-22 01:55:48 -0800 | [diff] [blame] | 106 | if (header.offset < sizeof(struct cbfs_fileheader)) { |
Simon Glass | c7f1693 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 107 | priv->result = CBFS_BAD_FILE; |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 108 | return -1; |
| 109 | } |
Simon Glass | ad79d60 | 2019-08-14 19:56:15 -0600 | [diff] [blame] | 110 | new_node->next = NULL; |
| 111 | new_node->type = header.type; |
| 112 | new_node->data = start + header.offset; |
| 113 | new_node->data_length = header.len; |
Yaroslav K | cc7ed26 | 2016-08-08 20:32:15 -0700 | [diff] [blame] | 114 | name_len = header.offset - sizeof(struct cbfs_fileheader); |
Simon Glass | ad79d60 | 2019-08-14 19:56:15 -0600 | [diff] [blame] | 115 | new_node->name = (char *)file_header + |
Yaroslav K | cc7ed26 | 2016-08-08 20:32:15 -0700 | [diff] [blame] | 116 | sizeof(struct cbfs_fileheader); |
Simon Glass | ad79d60 | 2019-08-14 19:56:15 -0600 | [diff] [blame] | 117 | new_node->name_length = name_len; |
| 118 | new_node->attributes_offset = header.attributes_offset; |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 119 | |
| 120 | step = header.len; |
| 121 | if (step % align) |
| 122 | step = step + align - step % align; |
| 123 | |
| 124 | *used += step; |
| 125 | return 1; |
| 126 | } |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | /* Look through a CBFS instance and copy file metadata into regular memory. */ |
Simon Glass | 02e4af6 | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 131 | static void file_cbfs_fill_cache(struct cbfs_priv *priv, u8 *start, u32 size, |
| 132 | u32 align) |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 133 | { |
| 134 | struct cbfs_cachenode *cache_node; |
Simon Glass | ad79d60 | 2019-08-14 19:56:15 -0600 | [diff] [blame] | 135 | struct cbfs_cachenode *new_node; |
Simon Glass | 02e4af6 | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 136 | struct cbfs_cachenode **cache_tail = &priv->file_cache; |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 137 | |
| 138 | /* Clear out old information. */ |
Simon Glass | 02e4af6 | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 139 | cache_node = priv->file_cache; |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 140 | while (cache_node) { |
Simon Glass | ad79d60 | 2019-08-14 19:56:15 -0600 | [diff] [blame] | 141 | struct cbfs_cachenode *old_node = cache_node; |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 142 | cache_node = cache_node->next; |
Simon Glass | ad79d60 | 2019-08-14 19:56:15 -0600 | [diff] [blame] | 143 | free(old_node); |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 144 | } |
Simon Glass | 02e4af6 | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 145 | priv->file_cache = NULL; |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 146 | |
| 147 | while (size >= align) { |
Simon Glass | ea38ee9 | 2020-05-24 17:38:12 -0600 | [diff] [blame] | 148 | int ret; |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 149 | u32 used; |
| 150 | |
Simon Glass | ad79d60 | 2019-08-14 19:56:15 -0600 | [diff] [blame] | 151 | new_node = (struct cbfs_cachenode *) |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 152 | malloc(sizeof(struct cbfs_cachenode)); |
Simon Glass | ea38ee9 | 2020-05-24 17:38:12 -0600 | [diff] [blame] | 153 | ret = file_cbfs_next_file(priv, start, size, align, new_node, |
| 154 | &used); |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 155 | |
Simon Glass | ea38ee9 | 2020-05-24 17:38:12 -0600 | [diff] [blame] | 156 | if (ret < 0) { |
Simon Glass | ad79d60 | 2019-08-14 19:56:15 -0600 | [diff] [blame] | 157 | free(new_node); |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 158 | return; |
Simon Glass | ea38ee9 | 2020-05-24 17:38:12 -0600 | [diff] [blame] | 159 | } else if (ret == 0) { |
Simon Glass | ad79d60 | 2019-08-14 19:56:15 -0600 | [diff] [blame] | 160 | free(new_node); |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 161 | break; |
| 162 | } |
Simon Glass | ad79d60 | 2019-08-14 19:56:15 -0600 | [diff] [blame] | 163 | *cache_tail = new_node; |
| 164 | cache_tail = &new_node->next; |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 165 | |
| 166 | size -= used; |
| 167 | start += used; |
| 168 | } |
Simon Glass | c7f1693 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 169 | priv->result = CBFS_SUCCESS; |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /* Get the CBFS header out of the ROM and do endian conversion. */ |
Simon Glass | 45637db | 2020-05-24 17:38:13 -0600 | [diff] [blame] | 173 | static int file_cbfs_load_header(ulong end_of_rom, struct cbfs_header *header) |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 174 | { |
| 175 | struct cbfs_header *header_in_rom; |
Andre Heider | 4468317 | 2018-02-15 07:40:11 +0100 | [diff] [blame] | 176 | int32_t offset = *(u32 *)(end_of_rom - 3); |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 177 | |
Andre Heider | 4468317 | 2018-02-15 07:40:11 +0100 | [diff] [blame] | 178 | header_in_rom = (struct cbfs_header *)(end_of_rom + offset + 1); |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 179 | swap_header(header, header_in_rom); |
| 180 | |
| 181 | if (header->magic != good_magic || header->offset > |
| 182 | header->rom_size - header->boot_block_size) { |
Simon Glass | c7f1693 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 183 | cbfs_s.result = CBFS_BAD_HEADER; |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 184 | return 1; |
| 185 | } |
| 186 | return 0; |
| 187 | } |
| 188 | |
Simon Glass | 630b2f3 | 2019-08-14 19:56:14 -0600 | [diff] [blame] | 189 | static int cbfs_load_header_ptr(struct cbfs_priv *priv, ulong base, |
| 190 | struct cbfs_header *header) |
| 191 | { |
| 192 | struct cbfs_header *header_in_rom; |
| 193 | |
| 194 | header_in_rom = (struct cbfs_header *)base; |
| 195 | swap_header(header, header_in_rom); |
| 196 | |
| 197 | if (header->magic != good_magic || header->offset > |
| 198 | header->rom_size - header->boot_block_size) { |
| 199 | priv->result = CBFS_BAD_HEADER; |
| 200 | return -EFAULT; |
| 201 | } |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
Simon Glass | 45637db | 2020-05-24 17:38:13 -0600 | [diff] [blame] | 206 | static void cbfs_init(struct cbfs_priv *priv, ulong end_of_rom) |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 207 | { |
| 208 | u8 *start_of_rom; |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 209 | |
Simon Glass | 381e113 | 2020-05-24 17:38:14 -0600 | [diff] [blame^] | 210 | priv->initialized = false; |
Simon Glass | 02e4af6 | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 211 | |
| 212 | if (file_cbfs_load_header(end_of_rom, &priv->header)) |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 213 | return; |
| 214 | |
Simon Glass | 02e4af6 | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 215 | start_of_rom = (u8 *)(end_of_rom + 1 - priv->header.rom_size); |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 216 | |
Simon Glass | 02e4af6 | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 217 | file_cbfs_fill_cache(priv, start_of_rom, priv->header.rom_size, |
| 218 | priv->header.align); |
Simon Glass | c7f1693 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 219 | if (priv->result == CBFS_SUCCESS) |
Simon Glass | 381e113 | 2020-05-24 17:38:14 -0600 | [diff] [blame^] | 220 | priv->initialized = true; |
Simon Glass | 02e4af6 | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 221 | } |
| 222 | |
Simon Glass | 45637db | 2020-05-24 17:38:13 -0600 | [diff] [blame] | 223 | void file_cbfs_init(ulong end_of_rom) |
Simon Glass | 02e4af6 | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 224 | { |
| 225 | cbfs_init(&cbfs_s, end_of_rom); |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Simon Glass | 630b2f3 | 2019-08-14 19:56:14 -0600 | [diff] [blame] | 228 | int cbfs_init_mem(ulong base, ulong size, struct cbfs_priv **privp) |
| 229 | { |
| 230 | struct cbfs_priv priv_s, *priv = &priv_s; |
| 231 | int ret; |
| 232 | |
| 233 | /* |
| 234 | * Use a local variable to start with until we know that the CBFS is |
| 235 | * valid. Assume that a master header appears at the start, at offset |
| 236 | * 0x38. |
| 237 | */ |
| 238 | ret = cbfs_load_header_ptr(priv, base + 0x38, &priv->header); |
| 239 | if (ret) |
| 240 | return ret; |
| 241 | |
| 242 | file_cbfs_fill_cache(priv, (u8 *)base, priv->header.rom_size, |
| 243 | priv->header.align); |
| 244 | if (priv->result != CBFS_SUCCESS) |
| 245 | return -EINVAL; |
| 246 | |
Simon Glass | 381e113 | 2020-05-24 17:38:14 -0600 | [diff] [blame^] | 247 | priv->initialized = true; |
Simon Glass | 630b2f3 | 2019-08-14 19:56:14 -0600 | [diff] [blame] | 248 | priv = malloc(sizeof(priv_s)); |
| 249 | if (!priv) |
| 250 | return -ENOMEM; |
| 251 | memcpy(priv, &priv_s, sizeof(priv_s)); |
| 252 | *privp = priv; |
| 253 | |
| 254 | return 0; |
| 255 | } |
| 256 | |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 257 | const struct cbfs_header *file_cbfs_get_header(void) |
| 258 | { |
Simon Glass | 02e4af6 | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 259 | struct cbfs_priv *priv = &cbfs_s; |
| 260 | |
| 261 | if (priv->initialized) { |
Simon Glass | c7f1693 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 262 | priv->result = CBFS_SUCCESS; |
Simon Glass | 02e4af6 | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 263 | return &priv->header; |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 264 | } else { |
Simon Glass | c7f1693 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 265 | priv->result = CBFS_NOT_INITIALIZED; |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 266 | return NULL; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | const struct cbfs_cachenode *file_cbfs_get_first(void) |
| 271 | { |
Simon Glass | 02e4af6 | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 272 | struct cbfs_priv *priv = &cbfs_s; |
| 273 | |
| 274 | if (!priv->initialized) { |
Simon Glass | c7f1693 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 275 | priv->result = CBFS_NOT_INITIALIZED; |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 276 | return NULL; |
| 277 | } else { |
Simon Glass | c7f1693 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 278 | priv->result = CBFS_SUCCESS; |
Simon Glass | 02e4af6 | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 279 | return priv->file_cache; |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
| 283 | void file_cbfs_get_next(const struct cbfs_cachenode **file) |
| 284 | { |
Simon Glass | 02e4af6 | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 285 | struct cbfs_priv *priv = &cbfs_s; |
| 286 | |
| 287 | if (!priv->initialized) { |
Simon Glass | c7f1693 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 288 | priv->result = CBFS_NOT_INITIALIZED; |
Simon Glass | 02e4af6 | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 289 | *file = NULL; |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 290 | return; |
| 291 | } |
| 292 | |
| 293 | if (*file) |
| 294 | *file = (*file)->next; |
Simon Glass | c7f1693 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 295 | priv->result = CBFS_SUCCESS; |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Simon Glass | 02e4af6 | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 298 | const struct cbfs_cachenode *cbfs_find_file(struct cbfs_priv *priv, |
| 299 | const char *name) |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 300 | { |
Simon Glass | 02e4af6 | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 301 | struct cbfs_cachenode *cache_node = priv->file_cache; |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 302 | |
Simon Glass | 02e4af6 | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 303 | if (!priv->initialized) { |
Simon Glass | c7f1693 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 304 | priv->result = CBFS_NOT_INITIALIZED; |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 305 | return NULL; |
| 306 | } |
| 307 | |
| 308 | while (cache_node) { |
| 309 | if (!strcmp(name, cache_node->name)) |
| 310 | break; |
| 311 | cache_node = cache_node->next; |
| 312 | } |
| 313 | if (!cache_node) |
Simon Glass | c7f1693 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 314 | priv->result = CBFS_FILE_NOT_FOUND; |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 315 | else |
Simon Glass | c7f1693 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 316 | priv->result = CBFS_SUCCESS; |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 317 | |
| 318 | return cache_node; |
| 319 | } |
| 320 | |
Simon Glass | 02e4af6 | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 321 | const struct cbfs_cachenode *file_cbfs_find(const char *name) |
| 322 | { |
| 323 | return cbfs_find_file(&cbfs_s, name); |
| 324 | } |
| 325 | |
Simon Glass | 45637db | 2020-05-24 17:38:13 -0600 | [diff] [blame] | 326 | const struct cbfs_cachenode *file_cbfs_find_uncached(ulong end_of_rom, |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 327 | const char *name) |
| 328 | { |
Simon Glass | 02e4af6 | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 329 | struct cbfs_priv *priv = &cbfs_s; |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 330 | u8 *start; |
| 331 | u32 size; |
| 332 | u32 align; |
| 333 | static struct cbfs_cachenode node; |
| 334 | |
Simon Glass | 02e4af6 | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 335 | if (file_cbfs_load_header(end_of_rom, &priv->header)) |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 336 | return NULL; |
| 337 | |
Simon Glass | 02e4af6 | 2019-08-14 19:56:12 -0600 | [diff] [blame] | 338 | start = (u8 *)(end_of_rom + 1 - priv->header.rom_size); |
| 339 | size = priv->header.rom_size; |
| 340 | align = priv->header.align; |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 341 | |
| 342 | while (size >= align) { |
Simon Glass | ea38ee9 | 2020-05-24 17:38:12 -0600 | [diff] [blame] | 343 | int ret; |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 344 | u32 used; |
| 345 | |
Simon Glass | ea38ee9 | 2020-05-24 17:38:12 -0600 | [diff] [blame] | 346 | ret = file_cbfs_next_file(priv, start, size, align, &node, |
| 347 | &used); |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 348 | |
Simon Glass | ea38ee9 | 2020-05-24 17:38:12 -0600 | [diff] [blame] | 349 | if (ret < 0) |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 350 | return NULL; |
Simon Glass | ea38ee9 | 2020-05-24 17:38:12 -0600 | [diff] [blame] | 351 | else if (ret == 0) |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 352 | break; |
| 353 | |
| 354 | if (!strcmp(name, node.name)) |
| 355 | return &node; |
| 356 | |
| 357 | size -= used; |
| 358 | start += used; |
| 359 | } |
Simon Glass | c7f1693 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 360 | cbfs_s.result = CBFS_FILE_NOT_FOUND; |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 361 | return NULL; |
| 362 | } |
| 363 | |
| 364 | const char *file_cbfs_name(const struct cbfs_cachenode *file) |
| 365 | { |
Simon Glass | c7f1693 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 366 | cbfs_s.result = CBFS_SUCCESS; |
| 367 | |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 368 | return file->name; |
| 369 | } |
| 370 | |
| 371 | u32 file_cbfs_size(const struct cbfs_cachenode *file) |
| 372 | { |
Simon Glass | c7f1693 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 373 | cbfs_s.result = CBFS_SUCCESS; |
| 374 | |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 375 | return file->data_length; |
| 376 | } |
| 377 | |
| 378 | u32 file_cbfs_type(const struct cbfs_cachenode *file) |
| 379 | { |
Simon Glass | c7f1693 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 380 | cbfs_s.result = CBFS_SUCCESS; |
| 381 | |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 382 | return file->type; |
| 383 | } |
| 384 | |
| 385 | long file_cbfs_read(const struct cbfs_cachenode *file, void *buffer, |
| 386 | unsigned long maxsize) |
| 387 | { |
| 388 | u32 size; |
| 389 | |
| 390 | size = file->data_length; |
| 391 | if (maxsize && size > maxsize) |
| 392 | size = maxsize; |
| 393 | |
| 394 | memcpy(buffer, file->data, size); |
Simon Glass | c7f1693 | 2019-08-14 19:56:13 -0600 | [diff] [blame] | 395 | cbfs_s.result = CBFS_SUCCESS; |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 396 | |
Gabe Black | 84cd932 | 2012-10-12 14:26:11 +0000 | [diff] [blame] | 397 | return size; |
| 398 | } |