/* returns a pointer to CBFS master header, or CBFS_HEADER_INVALID_ADDRESS * on failure */ const struct cbfs_header *cbfs_get_header(struct cbfs_media *media) { int32_t rel_offset; const struct cbfs_header *header; struct cbfs_media default_media; if (media == CBFS_DEFAULT_MEDIA) { media = &default_media; if (init_default_cbfs_media(media) != 0) { ERROR("Failed to initialize default media.\n"); return CBFS_HEADER_INVALID_ADDRESS; } } media->open(media); if (!media->read(media, &rel_offset, (size_t)(0 - sizeof(int32_t)), sizeof(int32_t))) { ERROR("Could not read CBFS master header offset!\n"); return CBFS_HEADER_INVALID_ADDRESS; } header = media->map(media, (size_t)rel_offset, sizeof(*header)); DEBUG("CBFS header at %#zx (-%#zx from end of image).\n", (size_t)rel_offset, (size_t)-rel_offset); media->close(media); if (header == CBFS_MEDIA_INVALID_MAP_ADDRESS) { ERROR("Failed to load CBFS header from %#zx(-%#zx)\n", (size_t)rel_offset, (size_t)-rel_offset); return CBFS_HEADER_INVALID_ADDRESS; } if (CBFS_HEADER_MAGIC != ntohl(header->magic)) { ERROR("Could not find valid CBFS master header at %#zx(-%#zx): " "magic %#.8x vs %#.8x.\n", (size_t)rel_offset, (size_t)-rel_offset, CBFS_HEADER_MAGIC, ntohl(header->magic)); if (header->magic == 0xffffffff) { ERROR("Maybe ROM is not mapped properly?\n"); } return CBFS_HEADER_INVALID_ADDRESS; } return header; }
const void *agesawrapper_locate_module (const CHAR8 name[8]) { struct cbfs_media media; struct cbfs_file* file; const void* agesa; const AMD_IMAGE_HEADER* image; const AMD_MODULE_HEADER* module; size_t file_size; if (init_default_cbfs_media(&media)) return NULL; file = cbfs_get_file(&media, (const char*)CONFIG_CBFS_AGESA_NAME); if (!file) return NULL; agesa = cbfs_get_file_content(&media, (const char*)CONFIG_CBFS_AGESA_NAME, ntohl(file->type), &file_size); if (!agesa) return NULL; image = LibAmdLocateImage(agesa, agesa + file_size - 1, 4096, name); module = (AMD_MODULE_HEADER*)image->ModuleInfoOffset; return module; }
/* returns a pointer to CBFS master header, or CBFS_HEADER_INVALID_ADDRESS * on failure */ const struct cbfs_header *cbfs_get_header(struct cbfs_media *media) { const struct cbfs_header *header; struct cbfs_media default_media; if (media == CBFS_DEFAULT_MEDIA) { media = &default_media; if (init_default_cbfs_media(media) != 0) { ERROR("Failed to initialize default media.\n"); return CBFS_HEADER_INVALID_ADDRESS; } } media->open(media); DEBUG("CBFS_HEADER_ROM_ADDRESS: 0x%x/0x%x\n", CBFS_HEADER_ROM_ADDRESS, CONFIG_ROM_SIZE); header = media->map(media, CBFS_HEADER_ROM_ADDRESS, sizeof(*header)); media->close(media); if (header == CBFS_MEDIA_INVALID_MAP_ADDRESS) { ERROR("Failed to load CBFS header from 0x%x\n", CBFS_HEADER_ROM_ADDRESS); return CBFS_HEADER_INVALID_ADDRESS; } if (CBFS_HEADER_MAGIC != ntohl(header->magic)) { ERROR("Could not find valid CBFS master header at %x: " "%x vs %x.\n", CBFS_HEADER_ROM_ADDRESS, CBFS_HEADER_MAGIC, ntohl(header->magic)); if (header->magic == 0xffffffff) { ERROR("Maybe ROM is not mapped properly?\n"); } return CBFS_HEADER_INVALID_ADDRESS; } return header; }
void *cbfs_get_file_content(struct cbfs_media *media, const char *name, int type, size_t *sz) { struct cbfs_media default_media; if (media == CBFS_DEFAULT_MEDIA) { media = &default_media; if (init_default_cbfs_media(media) != 0) { ERROR("Failed to initialize default media.\n"); return NULL; } } struct cbfs_file *file = cbfs_get_file(media, name); if (sz) *sz = 0; if (file == NULL) { ERROR("Could not find file '%s'.\n", name); return NULL; } if (ntohl(file->type) != type) { ERROR("File '%s' is of type %x, but we requested %x.\n", name, ntohl(file->type), type); return NULL; } void *file_content = (void *)CBFS_SUBHEADER(file); struct cbfs_file_attribute *attr = cbfs_file_find_attr(file, CBFS_FILE_ATTR_TAG_COMPRESSION); size_t final_size = ntohl(file->len); int compression_algo = CBFS_COMPRESS_NONE; if (attr) { struct cbfs_file_attr_compression *comp = (struct cbfs_file_attr_compression *)attr; compression_algo = ntohl(comp->compression); DEBUG("File '%s' is compressed (alg=%d)\n", name, compression_algo); final_size = ntohl(comp->decompressed_size); } void *dst = malloc(final_size); if (dst == NULL) goto err; if (!cbfs_decompress(compression_algo, file_content, dst, final_size)) goto err; if (sz) *sz = final_size; media->unmap(media, file); return dst; err: media->unmap(media, file); free(dst); return NULL; }
/* public API starts here*/ struct cbfs_file *cbfs_get_file(struct cbfs_media *media, const char *name) { const char *vardata; uint32_t offset, cbfs_end, vardata_len; struct cbfs_file file, *file_ptr; struct cbfs_media default_media; if (media == CBFS_DEFAULT_MEDIA) { media = &default_media; if (init_default_cbfs_media(media) != 0) { ERROR("Failed to initialize default media.\n"); return NULL; } } if (get_cbfs_range(&offset, &cbfs_end, media)) { ERROR("Failed to find cbfs range\n"); return NULL; } DEBUG("CBFS location: 0x%x~0x%x\n", offset, cbfs_end); DEBUG("Looking for '%s' starting from 0x%x.\n", name, offset); media->open(media); while (offset < cbfs_end && media->read(media, &file, offset, sizeof(file)) == sizeof(file)) { if (memcmp(CBFS_FILE_MAGIC, file.magic, sizeof(file.magic)) != 0) { uint32_t new_align = CBFS_ALIGNMENT; if (offset % CBFS_ALIGNMENT) new_align += CBFS_ALIGNMENT - (offset % CBFS_ALIGNMENT); ERROR("ERROR: No file header found at 0x%xx - " "try next aligned address: 0x%x.\n", offset, offset + new_align); offset += new_align; continue; } vardata_len = ntohl(file.offset) - sizeof(file); DEBUG(" - load entry 0x%x variable data (%d bytes)...\n", offset, vardata_len); // load file name (arbitrary length). vardata = (const char*)media->map( media, offset + sizeof(file), vardata_len); if (vardata == CBFS_MEDIA_INVALID_MAP_ADDRESS) { ERROR("ERROR: Failed to get filename: 0x%x.\n", offset); } else if (strcmp(vardata, name) == 0) { int file_offset = ntohl(file.offset), file_len = ntohl(file.len); DEBUG("Found file (offset=0x%x, len=%d).\n", offset + file_offset, file_len); media->unmap(media, vardata); file_ptr = media->map(media, offset, file_offset + file_len); media->close(media); return file_ptr; } else { DEBUG(" (unmatched file @0x%x: %s)\n", offset, vardata); media->unmap(media, vardata); } // Move to next file. offset += ntohl(file.len) + ntohl(file.offset); if (offset % CBFS_ALIGNMENT) offset += CBFS_ALIGNMENT - (offset % CBFS_ALIGNMENT); } media->close(media); LOG("WARNING: '%s' not found.\n", name); return NULL; }
/* public API starts here*/ struct cbfs_file *cbfs_get_file(struct cbfs_media *media, const char *name) { const char *vardata; uint32_t offset, romsize, vardata_len; const struct cbfs_header *header; struct cbfs_file file, *file_ptr; struct cbfs_media default_media; if (media == CBFS_DEFAULT_MEDIA) { media = &default_media; if (init_default_cbfs_media(media) != 0) { ERROR("Failed to initialize default media.\n"); return NULL; } } if (CBFS_HEADER_INVALID_ADDRESS == (header = cbfs_get_header(media))) return NULL; // Logical offset (for source media) of first file. offset = ntohl(header->offset); romsize = ntohl(header->romsize); // TODO Add a "size" in CBFS header for a platform independent way to // determine the end of CBFS data. #if IS_ENABLED(CONFIG_LP_ARCH_X86) // resolve actual length of ROM used for CBFS components // the bootblock size was not taken into account romsize -= ntohl(header->bootblocksize); // fine tune the length to handle alignment positioning. // using (bootblock size) % align, to derive the // number of bytes the bootblock is off from the alignment size. if ((ntohl(header->bootblocksize) % CBFS_ALIGNMENT)) romsize -= (CBFS_ALIGNMENT - (ntohl(header->bootblocksize) % CBFS_ALIGNMENT)); else romsize -= 1; #endif DEBUG("CBFS location: 0x%x~0x%x\n", offset, romsize); DEBUG("Looking for '%s' starting from 0x%x.\n", name, offset); media->open(media); while (offset < romsize && media->read(media, &file, offset, sizeof(file)) == sizeof(file)) { if (memcmp(CBFS_FILE_MAGIC, file.magic, sizeof(file.magic)) != 0) { uint32_t new_align = CBFS_ALIGNMENT; if (offset % CBFS_ALIGNMENT) new_align += CBFS_ALIGNMENT - (offset % CBFS_ALIGNMENT); ERROR("ERROR: No file header found at 0x%xx - " "try next aligned address: 0x%x.\n", offset, offset + new_align); offset += new_align; continue; } vardata_len = ntohl(file.offset) - sizeof(file); DEBUG(" - load entry 0x%x variable data (%d bytes)...\n", offset, vardata_len); // load file name (arbitrary length). vardata = (const char*)media->map( media, offset + sizeof(file), vardata_len); if (vardata == CBFS_MEDIA_INVALID_MAP_ADDRESS) { ERROR("ERROR: Failed to get filename: 0x%x.\n", offset); } else if (strcmp(vardata, name) == 0) { int file_offset = ntohl(file.offset), file_len = ntohl(file.len); DEBUG("Found file (offset=0x%x, len=%d).\n", offset + file_offset, file_len); media->unmap(media, vardata); file_ptr = media->map(media, offset, file_offset + file_len); media->close(media); return file_ptr; } else { DEBUG(" (unmatched file @0x%x: %s)\n", offset, vardata); media->unmap(media, vardata); } // Move to next file. offset += ntohl(file.len) + ntohl(file.offset); if (offset % CBFS_ALIGNMENT) offset += CBFS_ALIGNMENT - (offset % CBFS_ALIGNMENT); } media->close(media); LOG("WARNING: '%s' not found.\n", name); return NULL; }