| 1 |
|
| 2 |
|
| 3 |
#include <commonlib/bsd/compression.h> |
| 4 |
#include <console/console.h> |
| 5 |
#include <bootmem.h> |
| 6 |
#include <program_loading.h> |
| 7 |
#include <string.h> |
| 8 |
#include <lib.h> |
| 9 |
#include <fit.h> |
| 10 |
#include <endian.h> |
| 11 |
|
| 12 |
#define MAX_KERNEL_SIZE (64*MiB) |
| 13 |
|
| 14 |
struct arm64_kernel_header { |
| 15 |
u32 code0; |
| 16 |
u32 code1; |
| 17 |
u64 text_offset; |
| 18 |
u64 image_size; |
| 19 |
u64 flags; |
| 20 |
u64 res2; |
| 21 |
u64 res3; |
| 22 |
u64 res4; |
| 23 |
u32 magic; |
| 24 |
#define KERNEL_HEADER_MAGIC 0x644d5241 |
| 25 |
u32 res5; |
| 26 |
}; |
| 27 |
|
| 28 |
static struct { |
| 29 |
union { |
| 30 |
struct arm64_kernel_header header; |
| 31 |
u8 raw[sizeof(struct arm64_kernel_header) + 0x100]; |
| 32 |
}; |
| 33 |
#define SCRATCH_CANARY_VALUE 0xdeadbeef |
| 34 |
u32 canary; |
| 35 |
} scratch; |
| 36 |
|
| 37 |
|
| 38 |
static bool decompress_kernel_header(const struct fit_image_node *node) |
| 39 |
{ |
| 40 |
|
| 41 |
scratch.canary = SCRATCH_CANARY_VALUE; |
| 42 |
switch (node->compression) { |
| 43 |
case CBFS_COMPRESS_NONE: |
| 44 |
memcpy(scratch.raw, node->data, sizeof(scratch.raw)); |
| 45 |
break; |
| 46 |
case CBFS_COMPRESS_LZMA: |
| 47 |
ulzman(node->data, node->size, |
| 48 |
scratch.raw, sizeof(scratch.raw)); |
| 49 |
break; |
| 50 |
case CBFS_COMPRESS_LZ4: |
| 51 |
ulz4fn(node->data, node->size, |
| 52 |
scratch.raw, sizeof(scratch.raw)); |
| 53 |
break; |
| 54 |
default: |
| 55 |
printk(BIOS_ERR, "Unsupported compression algorithm!\n"); |
| 56 |
return false; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
if (scratch.canary != SCRATCH_CANARY_VALUE) |
| 61 |
die("ERROR: Partial decompression ran over scratchbuf!\n"); |
| 62 |
|
| 63 |
if (scratch.header.magic != KERNEL_HEADER_MAGIC) { |
| 64 |
printk(BIOS_ERR, "Invalid kernel magic: %#.8x\n != %#.8x\n", |
| 65 |
scratch.header.magic, KERNEL_HEADER_MAGIC); |
| 66 |
return false; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
if (!scratch.header.image_size) |
| 77 |
scratch.header.text_offset = cpu_to_le64(0x80000); |
| 78 |
|
| 79 |
return true; |
| 80 |
} |
| 81 |
|
| 82 |
static size_t get_kernel_size(const struct fit_image_node *node) |
| 83 |
{ |
| 84 |
if (scratch.header.image_size) |
| 85 |
return le64_to_cpu(scratch.header.image_size); |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
printk(BIOS_WARNING, "FIT: image_size not set in kernel header.\n" |
| 95 |
"Leaving additional %u MiB of free space after kernel.\n", |
| 96 |
MAX_KERNEL_SIZE >> 20); |
| 97 |
|
| 98 |
return node->size + MAX_KERNEL_SIZE; |
| 99 |
} |
| 100 |
|
| 101 |
static bool fit_place_kernel(const struct range_entry *r, void *arg) |
| 102 |
{ |
| 103 |
struct region *region = arg; |
| 104 |
resource_t start; |
| 105 |
|
| 106 |
if (range_entry_tag(r) != BM_MEM_RAM) |
| 107 |
return true; |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
start = ALIGN_DOWN(range_entry_base(r), 2 * MiB) + |
| 121 |
le64_to_cpu(scratch.header.text_offset); |
| 122 |
|
| 123 |
if (start < range_entry_base(r)) |
| 124 |
start += 2 * MiB; |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
if (start + region->size < range_entry_end(r)) { |
| 130 |
region->offset = (size_t)start; |
| 131 |
return false; |
| 132 |
} |
| 133 |
|
| 134 |
return true; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
static bool fit_place_mem(const struct range_entry *r, void *arg) |
| 145 |
{ |
| 146 |
struct region *region = arg; |
| 147 |
resource_t start; |
| 148 |
|
| 149 |
if (range_entry_tag(r) != BM_MEM_RAM) |
| 150 |
return true; |
| 151 |
|
| 152 |
|
| 153 |
start = ALIGN_UP(MAX(region->offset, range_entry_base(r)), 1 * MiB); |
| 154 |
|
| 155 |
if (start + region->size < range_entry_end(r)) { |
| 156 |
region->offset = (size_t)start; |
| 157 |
return false; |
| 158 |
} |
| 159 |
|
| 160 |
return true; |
| 161 |
} |
| 162 |
|
| 163 |
bool fit_payload_arch(struct prog *payload, struct fit_config_node *config, |
| 164 |
struct region *kernel, |
| 165 |
struct region *fdt, |
| 166 |
struct region *initrd) |
| 167 |
{ |
| 168 |
bool place_anywhere; |
| 169 |
void *arg = NULL; |
| 170 |
|
| 171 |
if (!decompress_kernel_header(config->kernel)) { |
| 172 |
printk(BIOS_CRIT, "Payload doesn't look like an ARM64" |
| 173 |
" kernel Image.\n"); |
| 174 |
return false; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
kernel->size = get_kernel_size(config->kernel); |
| 179 |
printk(BIOS_DEBUG, "FIT: Using kernel size of 0x%zx bytes\n", |
| 180 |
kernel->size); |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
if (!bootmem_walk(fit_place_kernel, kernel)) |
| 191 |
return false; |
| 192 |
|
| 193 |
|
| 194 |
bootmem_add_range(kernel->offset, kernel->size, BM_MEM_PAYLOAD); |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
place_anywhere = !!(le64_to_cpu(scratch.header.flags) & (1 << 3)); |
| 205 |
printk(BIOS_DEBUG, "FIT: Placing FDT and INITRD %s\n", |
| 206 |
place_anywhere ? "anywhere" : "on top of kernel"); |
| 207 |
|
| 208 |
|
| 209 |
if (config->ramdisk) { |
| 210 |
if (place_anywhere) |
| 211 |
initrd->offset = 0; |
| 212 |
else |
| 213 |
initrd->offset = kernel->offset + kernel->size; |
| 214 |
|
| 215 |
if (!bootmem_walk(fit_place_mem, initrd)) |
| 216 |
return false; |
| 217 |
|
| 218 |
bootmem_add_range(initrd->offset, initrd->size, BM_MEM_PAYLOAD); |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
if (place_anywhere) |
| 223 |
fdt->offset = 0; |
| 224 |
else |
| 225 |
fdt->offset = kernel->offset + kernel->size; |
| 226 |
|
| 227 |
if (!bootmem_walk(fit_place_mem, fdt)) |
| 228 |
return false; |
| 229 |
|
| 230 |
bootmem_add_range(fdt->offset, fdt->size, BM_MEM_PAYLOAD); |
| 231 |
|
| 232 |
|
| 233 |
arg = (void *)fdt->offset; |
| 234 |
|
| 235 |
prog_set_entry(payload, (void *)kernel->offset, arg); |
| 236 |
|
| 237 |
bootmem_dump_ranges(); |
| 238 |
|
| 239 |
return true; |
| 240 |
} |