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