Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 | // SPDX-License-Identifier: GPL-2.0+ /* * Copyright (c) 2011 The Chromium OS Authors. All rights reserved. */ #include <cbfs.h> #include <log.h> #include <malloc.h> #include <linux/errno.h> #include <linux/string.h> #include <asm/byteorder.h> /* Offset of master header from the start of a coreboot ROM */ #define MASTER_HDR_OFFSET 0x38 static const u32 good_magic = 0x4f524243; static const u8 good_file_magic[] = "LARCHIVE"; /** * struct cbfs_priv - Private data for this driver * * @initialised: true if this CBFS has been inited * @start: Start position of CBFS in memory, typically memory-mapped SPI flash * @header: Header read from the CBFS, byte-swapped so U-Boot can access it * @file_cache: List of file headers read from CBFS * @result: Success/error result */ struct cbfs_priv { bool initialized; void *start; struct cbfs_header header; struct cbfs_cachenode *file_cache; enum cbfs_result result; }; static struct cbfs_priv cbfs_s; const char *file_cbfs_error(void) { switch (cbfs_s.result) { case CBFS_SUCCESS: return "Success"; case CBFS_NOT_INITIALIZED: return "CBFS not initialized"; case CBFS_BAD_HEADER: return "Bad CBFS header"; case CBFS_BAD_FILE: return "Bad CBFS file"; case CBFS_FILE_NOT_FOUND: return "File not found"; default: return "Unknown"; } } enum cbfs_result cbfs_get_result(void) { return cbfs_s.result; } /* Do endian conversion on the CBFS header structure. */ static void swap_header(struct cbfs_header *dest, struct cbfs_header *src) { dest->magic = be32_to_cpu(src->magic); dest->version = be32_to_cpu(src->version); dest->rom_size = be32_to_cpu(src->rom_size); dest->boot_block_size = be32_to_cpu(src->boot_block_size); dest->align = be32_to_cpu(src->align); dest->offset = be32_to_cpu(src->offset); } /* Do endian conversion on a CBFS file header. */ static void swap_file_header(struct cbfs_fileheader *dest, const struct cbfs_fileheader *src) { memcpy(&dest->magic, &src->magic, sizeof(dest->magic)); dest->len = be32_to_cpu(src->len); dest->type = be32_to_cpu(src->type); dest->attributes_offset = be32_to_cpu(src->attributes_offset); dest->offset = be32_to_cpu(src->offset); } /** * fill_node() - Fill a node struct with information from the CBFS * * @node: Node to fill * @start: Pointer to the start of the CBFS file in memory * @header: Pointer to the header information (in our enddianess) * Return: 0 if OK, -EBADF if the header is too small */ static int fill_node(struct cbfs_cachenode *node, void *start, struct cbfs_fileheader *header) { uint name_len; uint offset; /* Check the header is large enough */ if (header->offset < sizeof(struct cbfs_fileheader)) return -EBADF; node->next = NULL; node->type = header->type; node->data = start + header->offset; node->data_length = header->len; name_len = header->offset - sizeof(struct cbfs_fileheader); node->name = start + sizeof(struct cbfs_fileheader); node->name_length = name_len; node->attr_offset = header->attributes_offset; node->comp_algo = CBFS_COMPRESS_NONE; node->decomp_size = 0; for (offset = node->attr_offset; offset < header->offset;) { const struct cbfs_file_attribute *attr; uint tag, len; attr = start + offset; tag = be32_to_cpu(attr->tag); len = be32_to_cpu(attr->len); if (tag == CBFS_FILE_ATTR_TAG_COMPRESSION) { struct cbfs_file_attr_compression *comp; comp = start + offset; node->comp_algo = be32_to_cpu(comp->compression); node->decomp_size = be32_to_cpu(comp->decompressed_size); } offset += len; } return 0; } /* * Given a starting position in memory, scan forward, bounded by a size, and * find the next valid CBFS file. No memory is allocated by this function. The * caller is responsible for allocating space for the new file structure. * * @param start The location in memory to start from. * @param size The size of the memory region to search. * @param align The alignment boundaries to check on. * @param node A pointer to the file structure to load. * @param used A pointer to the count of of bytes scanned through, * including the file if one is found. * * Return: 0 if a file is found, -ENOENT if one isn't, -EBADF if a bad header * is found. */ static int file_cbfs_next_file(struct cbfs_priv *priv, void *start, int size, int align, struct cbfs_cachenode *node, int *used) { struct cbfs_fileheader header; *used = 0; while (size >= align) { const struct cbfs_fileheader *file_header = start; int ret; /* Check if there's a file here. */ if (memcmp(good_file_magic, &file_header->magic, sizeof(file_header->magic))) { *used += align; size -= align; start += align; continue; } swap_file_header(&header, file_header); if (header.offset >= size) return log_msg_ret("range", -E2BIG); ret = fill_node(node, start, &header); if (ret) { priv->result = CBFS_BAD_FILE; return log_msg_ret("fill", ret); } *used += ALIGN(header.len, align); return 0; } return -ENOENT; } /* Look through a CBFS instance and copy file metadata into regular memory. */ static int file_cbfs_fill_cache(struct cbfs_priv *priv, int size, int align) { struct cbfs_cachenode *cache_node; struct cbfs_cachenode *node; struct cbfs_cachenode **cache_tail = &priv->file_cache; void *start; /* Clear out old information. */ cache_node = priv->file_cache; while (cache_node) { struct cbfs_cachenode *old_node = cache_node; cache_node = cache_node->next; free(old_node); } priv->file_cache = NULL; start = priv->start; while (size >= align) { int used; int ret; node = malloc(sizeof(struct cbfs_cachenode)); if (!node) return -ENOMEM; ret = file_cbfs_next_file(priv, start, size, align, node, &used); if (ret < 0) { free(node); if (ret == -ENOENT) break; return ret; } *cache_tail = node; cache_tail = &node->next; size -= used; start += used; } priv->result = CBFS_SUCCESS; return 0; } /** * load_header() - Load the CBFS header * * Get the CBFS header out of the ROM and do endian conversion. * * @priv: Private data, which is inited by this function * @addr: Address of CBFS header in memory-mapped SPI flash * Return: 0 if OK, -ENXIO if the header is bad */ static int load_header(struct cbfs_priv *priv, ulong addr) { struct cbfs_header *header = &priv->header; struct cbfs_header *header_in_rom; memset(priv, '\0', sizeof(*priv)); header_in_rom = (struct cbfs_header *)addr; swap_header(header, header_in_rom); if (header->magic != good_magic || header->offset > header->rom_size - header->boot_block_size) { priv->result = CBFS_BAD_HEADER; return -ENXIO; } return 0; } /** * file_cbfs_load_header() - Get the CBFS header out of the ROM, given the end * * @priv: Private data, which is inited by this function * @end_of_rom: Address of the last byte of the ROM (typically 0xffffffff) * Return: 0 if OK, -ENXIO if the header is bad */ static int file_cbfs_load_header(struct cbfs_priv *priv, ulong end_of_rom) { int offset = *(u32 *)(end_of_rom - 3); int ret; ret = load_header(priv, end_of_rom + offset + 1); if (ret) return ret; priv->start = (void *)(end_of_rom + 1 - priv->header.rom_size); return 0; } /** * cbfs_load_header_ptr() - Get the CBFS header out of the ROM, given the base * * @priv: Private data, which is inited by this function * @base: Address of the first byte of the ROM (e.g. 0xff000000) * Return: 0 if OK, -ENXIO if the header is bad */ static int cbfs_load_header_ptr(struct cbfs_priv *priv, ulong base) { int ret; ret = load_header(priv, base + MASTER_HDR_OFFSET); if (ret) return ret; priv->start = (void *)base; return 0; } static int cbfs_init(struct cbfs_priv *priv, ulong end_of_rom) { int ret; ret = file_cbfs_load_header(priv, end_of_rom); if (ret) return ret; ret = file_cbfs_fill_cache(priv, priv->header.rom_size, priv->header.align); if (ret) return ret; priv->initialized = true; return 0; } int file_cbfs_init(ulong end_of_rom) { return cbfs_init(&cbfs_s, end_of_rom); } int cbfs_init_mem(ulong base, ulong size, bool require_hdr, struct cbfs_priv **privp) { struct cbfs_priv priv_s, *priv = &priv_s; int ret; /* * Use a local variable to start with until we know that the * CBFS is * valid. Note that size is detected from the header, if present, * meaning the parameter is ignored. */ ret = cbfs_load_header_ptr(priv, base); if (ret) { if (require_hdr || size == CBFS_SIZE_UNKNOWN) return ret; memset(priv, '\0', sizeof(struct cbfs_priv)); priv->header.rom_size = size; priv->header.align = CBFS_ALIGN_SIZE; priv->start = (void *)base; } ret = file_cbfs_fill_cache(priv, priv->header.rom_size, priv->header.align); if (ret) return log_msg_ret("fill", ret); priv->initialized = true; priv = malloc(sizeof(priv_s)); if (!priv) return -ENOMEM; memcpy(priv, &priv_s, sizeof(priv_s)); *privp = priv; return 0; } const struct cbfs_header *file_cbfs_get_header(void) { struct cbfs_priv *priv = &cbfs_s; if (priv->initialized) { priv->result = CBFS_SUCCESS; return &priv->header; } else { priv->result = CBFS_NOT_INITIALIZED; return NULL; } } const struct cbfs_cachenode *cbfs_get_first(const struct cbfs_priv *priv) { return priv->file_cache; } void cbfs_get_next(const struct cbfs_cachenode **filep) { if (*filep) *filep = (*filep)->next; } const struct cbfs_cachenode *file_cbfs_get_first(void) { struct cbfs_priv *priv = &cbfs_s; if (!priv->initialized) { priv->result = CBFS_NOT_INITIALIZED; return NULL; } else { priv->result = CBFS_SUCCESS; return priv->file_cache; } } void file_cbfs_get_next(const struct cbfs_cachenode **file) { struct cbfs_priv *priv = &cbfs_s; if (!priv->initialized) { priv->result = CBFS_NOT_INITIALIZED; *file = NULL; return; } if (*file) *file = (*file)->next; priv->result = CBFS_SUCCESS; } const struct cbfs_cachenode *cbfs_find_file(struct cbfs_priv *priv, const char *name) { struct cbfs_cachenode *cache_node = priv->file_cache; if (!priv->initialized) { priv->result = CBFS_NOT_INITIALIZED; return NULL; } while (cache_node) { if (!strcmp(name, cache_node->name)) break; cache_node = cache_node->next; } if (!cache_node) priv->result = CBFS_FILE_NOT_FOUND; else priv->result = CBFS_SUCCESS; return cache_node; } const struct cbfs_cachenode *file_cbfs_find(const char *name) { return cbfs_find_file(&cbfs_s, name); } static int find_uncached(struct cbfs_priv *priv, const char *name, void *start, struct cbfs_cachenode *node) { int size = priv->header.rom_size; int align = priv->header.align; while (size >= align) { int used; int ret; ret = file_cbfs_next_file(priv, start, size, align, node, &used); if (ret == -ENOENT) break; else if (ret) return ret; if (!strcmp(name, node->name)) return 0; size -= used; start += used; } priv->result = CBFS_FILE_NOT_FOUND; return -ENOENT; } int file_cbfs_find_uncached(ulong end_of_rom, const char *name, struct cbfs_cachenode *node) { struct cbfs_priv priv; void *start; int ret; ret = file_cbfs_load_header(&priv, end_of_rom); if (ret) return ret; start = priv.start; return find_uncached(&priv, name, start, node); } int file_cbfs_find_uncached_base(ulong base, const char *name, struct cbfs_cachenode *node) { struct cbfs_priv priv; int ret; ret = cbfs_load_header_ptr(&priv, base); if (ret) return ret; return find_uncached(&priv, name, (void *)base, node); } const char *file_cbfs_name(const struct cbfs_cachenode *file) { cbfs_s.result = CBFS_SUCCESS; return file->name; } u32 file_cbfs_size(const struct cbfs_cachenode *file) { cbfs_s.result = CBFS_SUCCESS; return file->data_length; } u32 file_cbfs_type(const struct cbfs_cachenode *file) { cbfs_s.result = CBFS_SUCCESS; return file->type; } long file_cbfs_read(const struct cbfs_cachenode *file, void *buffer, unsigned long maxsize) { u32 size; size = file->data_length; if (maxsize && size > maxsize) size = maxsize; memcpy(buffer, file->data, size); cbfs_s.result = CBFS_SUCCESS; return size; } |