Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the libpayload project. |
| 3 | * |
| 4 | * Copyright (C) 2011 secunet Security Networks AG |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * 3. The name of the author may not be used to endorse or promote products |
| 15 | * derived from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 27 | * SUCH DAMAGE. |
| 28 | */ |
| 29 | |
| 30 | /* The CBFS core requires a couple of #defines or functions to adapt it to the target environment: |
| 31 | * |
| 32 | * CBFS_CORE_WITH_LZMA (must be #define) |
| 33 | * if defined, ulzma() must exist for decompression of data streams |
| 34 | * |
| 35 | * phys_to_virt(x), virt_to_phys(x) |
| 36 | * translate physical addresses to virtual and vice versa |
| 37 | * can be idempotent if no mapping is necessary. |
| 38 | * |
| 39 | * ERROR(x...) |
| 40 | * print an error message x (in printf format) |
| 41 | * |
| 42 | * LOG(x...) |
| 43 | * print a debug message x (in printf format) |
| 44 | * |
| 45 | * romstart() |
| 46 | * returns the start address of the ROM image, or 0xffffffff if ROM is |
| 47 | * top-aligned. This is a physical address. |
| 48 | * |
| 49 | * romend() |
| 50 | * returns the highest address of the ROM image + 1, for use if |
| 51 | * romstart() == 0xffffffff. This is a physical address. |
| 52 | */ |
| 53 | |
| 54 | #include <cbfs_core.h> |
| 55 | |
| 56 | |
| 57 | /* returns pointer to master header or 0xffffffff if not found */ |
Patrick Georgi | b09e748 | 2011-08-18 11:36:03 +0200 | [diff] [blame] | 58 | struct cbfs_header *get_cbfs_header(void) |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 59 | { |
| 60 | struct cbfs_header *header; |
| 61 | |
| 62 | /* find header */ |
| 63 | if (romstart() == 0xffffffff) { |
| 64 | header = (struct cbfs_header*)phys_to_virt(*(uint32_t*)phys_to_virt(romend() + CBFS_HEADPTR_ADDR)); |
| 65 | } else { |
| 66 | // FIXME: where's the master header on ARM (our current bottom-aligned platform)? |
| 67 | header = NULL; |
| 68 | } |
| 69 | if (CBFS_HEADER_MAGIC != ntohl(header->magic)) { |
| 70 | ERROR("Could not find valid CBFS master header at %p: %x vs %x.\n", header, CBFS_HEADER_MAGIC, ntohl(header->magic)); |
| 71 | if (header->magic == 0xffffffff) { |
| 72 | ERROR("Maybe ROM is not mapped properly?\n"); |
| 73 | } |
| 74 | return (void*)0xffffffff; |
| 75 | } |
| 76 | return header; |
| 77 | } |
| 78 | |
| 79 | // by must be power-of-two |
| 80 | #define CBFS_ALIGN(val, by) (typeof(val))((uint32_t)(val + by - 1) & (uint32_t)~(by - 1)) |
| 81 | #define CBFS_ALIGN_UP(val, by) CBFS_ALIGN(val + 1, by) |
| 82 | |
| 83 | /* public API starts here*/ |
| 84 | struct cbfs_file *cbfs_find(const char *name) |
| 85 | { |
| 86 | struct cbfs_header *header = get_cbfs_header(); |
| 87 | if (header == (void*)0xffffffff) return NULL; |
| 88 | |
| 89 | LOG("Searching for %s\n", name); |
| 90 | |
| 91 | void *data, *dataend, *origdata; |
| 92 | /* find first entry */ |
| 93 | if (romstart() == 0xffffffff) { |
| 94 | data = (void*)phys_to_virt(romend()) - ntohl(header->romsize) + ntohl(header->offset); |
| 95 | dataend = (void*)phys_to_virt(romend()); |
| 96 | } else { |
| 97 | data = (void*)phys_to_virt(romstart()) + ntohl(header->offset); |
| 98 | dataend = (void*)phys_to_virt(romstart()) + ntohl(header->romsize); |
| 99 | } |
Florian Zumbiehl | 2d4fece | 2011-11-01 20:17:11 +0100 | [diff] [blame] | 100 | dataend -= ntohl(header->bootblocksize); |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 101 | |
| 102 | int align = ntohl(header->align); |
| 103 | |
| 104 | origdata = data; |
| 105 | while ((data < (dataend - 1)) && (data >= origdata)) { |
| 106 | struct cbfs_file *file = data; |
| 107 | if (memcmp(CBFS_FILE_MAGIC, file->magic, strlen(CBFS_FILE_MAGIC)) != 0) { |
| 108 | // no file header found. corruption? |
| 109 | // proceed in aligned steps to resynchronize |
Florian Zumbiehl | 2d4fece | 2011-11-01 20:17:11 +0100 | [diff] [blame] | 110 | LOG("ERROR: No file header found at %p, attempting to recover by searching for header\n", data); |
Patrick Georgi | 6de1ee4a | 2011-07-21 15:43:14 +0200 | [diff] [blame] | 111 | data = phys_to_virt(CBFS_ALIGN_UP(virt_to_phys(data), align)); |
| 112 | continue; |
| 113 | } |
| 114 | LOG("Check %s\n", CBFS_NAME(file)); |
| 115 | if (strcmp(CBFS_NAME(file), name) == 0) { |
| 116 | return file; |
| 117 | } |
| 118 | void *olddata = data; |
| 119 | data = phys_to_virt(CBFS_ALIGN(virt_to_phys(data) + ntohl(file->len) + ntohl(file->offset), align)); |
| 120 | if (olddata > data) { |
| 121 | LOG("Something is wrong here. File chain moved from %p to %p\n", olddata, data); |
| 122 | return NULL; |
| 123 | } |
| 124 | } |
| 125 | return NULL; |
| 126 | } |
| 127 | |
| 128 | void *cbfs_get_file(const char *name) |
| 129 | { |
| 130 | struct cbfs_file *file = cbfs_find(name); |
| 131 | |
| 132 | if (file == NULL) { |
| 133 | ERROR("Could not find file '%s'.\n", name); |
| 134 | return NULL; |
| 135 | } |
| 136 | |
| 137 | return (void*)CBFS_SUBHEADER(file); |
| 138 | } |
| 139 | |
| 140 | void *cbfs_find_file(const char *name, int type) |
| 141 | { |
| 142 | struct cbfs_file *file = cbfs_find(name); |
| 143 | |
| 144 | if (file == NULL) { |
| 145 | ERROR("Could not find file '%s'.\n", name); |
| 146 | return NULL; |
| 147 | } |
| 148 | |
| 149 | if (ntohl(file->type) != type) { |
| 150 | ERROR("File '%s' is of type %x, but we requested %x.\n", name, ntohl(file->type), type); |
| 151 | return NULL; |
| 152 | } |
| 153 | |
| 154 | return (void*)CBFS_SUBHEADER(file); |
| 155 | } |
| 156 | |
| 157 | int cbfs_decompress(int algo, void *src, void *dst, int len) |
| 158 | { |
| 159 | switch (algo) { |
| 160 | case CBFS_COMPRESS_NONE: |
| 161 | memcpy(dst, src, len); |
| 162 | return 0; |
| 163 | #ifdef CBFS_CORE_WITH_LZMA |
| 164 | case CBFS_COMPRESS_LZMA: |
| 165 | if (ulzma(src, dst) != 0) { |
| 166 | return 0; |
| 167 | } |
| 168 | return -1; |
| 169 | #endif |
| 170 | default: |
| 171 | ERROR("tried to decompress %d bytes with algorithm #%x, but that algorithm id is unsupported.\n", len, algo); |
| 172 | return -1; |
| 173 | } |
| 174 | } |
| 175 | |