| 1 |
|
| 2 |
|
| 3 |
#include <commonlib/bsd/compression.h> |
| 4 |
#include <console/console.h> |
| 5 |
#include <bootmem.h> |
| 6 |
#include <program_loading.h> |
| 7 |
#include <fit.h> |
| 8 |
#include <endian.h> |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
#define MAX_KERNEL_SIZE (64*MiB) |
| 13 |
|
| 14 |
#if CONFIG(ARCH_RISCV_RV32) |
| 15 |
#define SECTION_ALIGN (4 * MiB) |
| 16 |
#endif |
| 17 |
#if CONFIG(ARCH_RISCV_RV64) |
| 18 |
#define SECTION_ALIGN (2 * MiB) |
| 19 |
#endif |
| 20 |
|
| 21 |
static size_t get_kernel_size(const struct fit_image_node *node) |
| 22 |
{ |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
printk(BIOS_INFO, |
| 32 |
"FIT: Leaving additional %u MiB of free space after kernel.\n", |
| 33 |
MAX_KERNEL_SIZE >> 20); |
| 34 |
|
| 35 |
return node->size + MAX_KERNEL_SIZE; |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
static bool fit_place_mem(const struct range_entry *r, void *arg) |
| 44 |
{ |
| 45 |
struct region *region = arg; |
| 46 |
resource_t start; |
| 47 |
|
| 48 |
if (range_entry_tag(r) != BM_MEM_RAM) |
| 49 |
return true; |
| 50 |
|
| 51 |
|
| 52 |
start = ALIGN_UP(MAX(region->offset, range_entry_base(r)), SECTION_ALIGN); |
| 53 |
|
| 54 |
if (start + region->size < range_entry_end(r)) { |
| 55 |
region->offset = (size_t)start; |
| 56 |
return false; |
| 57 |
} |
| 58 |
|
| 59 |
return true; |
| 60 |
} |
| 61 |
|
| 62 |
bool fit_payload_arch(struct prog *payload, struct fit_config_node *config, |
| 63 |
struct region *kernel, |
| 64 |
struct region *fdt, |
| 65 |
struct region *initrd) |
| 66 |
{ |
| 67 |
void *arg = NULL; |
| 68 |
|
| 69 |
if (!config->fdt || !fdt) { |
| 70 |
printk(BIOS_CRIT, "Providing a valid FDT is mandatory to " |
| 71 |
"boot a RISC-V kernel!\n"); |
| 72 |
return false; |
| 73 |
|
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
kernel->size = get_kernel_size(config->kernel); |
| 78 |
printk(BIOS_DEBUG, "FIT: Using kernel size of 0x%zx bytes\n", |
| 79 |
kernel->size); |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
kernel->offset = 0; |
| 89 |
if (!bootmem_walk(fit_place_mem, kernel)) |
| 90 |
return false; |
| 91 |
|
| 92 |
|
| 93 |
bootmem_add_range(kernel->offset, kernel->size, BM_MEM_PAYLOAD); |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
if (config->ramdisk) { |
| 99 |
initrd->offset = kernel->offset + kernel->size; |
| 100 |
|
| 101 |
if (!bootmem_walk(fit_place_mem, initrd)) |
| 102 |
return false; |
| 103 |
|
| 104 |
bootmem_add_range(initrd->offset, initrd->size, BM_MEM_PAYLOAD); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
fdt->offset = kernel->offset + kernel->size; |
| 109 |
|
| 110 |
if (!bootmem_walk(fit_place_mem, fdt)) |
| 111 |
return false; |
| 112 |
|
| 113 |
bootmem_add_range(fdt->offset, fdt->size, BM_MEM_PAYLOAD); |
| 114 |
|
| 115 |
|
| 116 |
arg = (void *)fdt->offset; |
| 117 |
|
| 118 |
prog_set_entry(payload, (void *)kernel->offset, arg); |
| 119 |
|
| 120 |
bootmem_dump_ranges(); |
| 121 |
|
| 122 |
return true; |
| 123 |
} |