[63a045c] | 1 | /*
|
---|
| 2 | * Copyright (c) 2007 Michal Kebrt
|
---|
| 3 | * Copyright (c) 2018 Jiří Zárevúcky
|
---|
| 4 | * All rights reserved.
|
---|
| 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 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
| 30 | #include <payload.h>
|
---|
| 31 |
|
---|
| 32 | #include <align.h>
|
---|
| 33 | #include <printf.h>
|
---|
| 34 | #include <arch/arch.h>
|
---|
| 35 | #include <tar.h>
|
---|
| 36 | #include <gzip.h>
|
---|
| 37 | #include <stdbool.h>
|
---|
| 38 | #include <memstr.h>
|
---|
| 39 | #include <errno.h>
|
---|
| 40 | #include <str.h>
|
---|
| 41 | #include <halt.h>
|
---|
| 42 |
|
---|
[dcc2c5d] | 43 | static const char *ext(const char *s)
|
---|
[63a045c] | 44 | {
|
---|
[dcc2c5d] | 45 | const char *last = s;
|
---|
[63a045c] | 46 |
|
---|
| 47 | while (*s) {
|
---|
| 48 | if (*s == '.')
|
---|
| 49 | last = s;
|
---|
| 50 |
|
---|
| 51 | s++;
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | if (*last == '.')
|
---|
[dcc2c5d] | 55 | return last;
|
---|
| 56 |
|
---|
| 57 | return NULL;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | static void basename(char *s)
|
---|
| 61 | {
|
---|
| 62 | char *e = (char *) ext(s);
|
---|
[d091007] | 63 | if ((e != NULL) && (str_cmp(e, ".gz") == 0))
|
---|
[dcc2c5d] | 64 | *e = '\0';
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[63a045c] | 67 | static bool overlaps(uint8_t *start1, uint8_t *end1,
|
---|
| 68 | uint8_t *start2, uint8_t *end2)
|
---|
| 69 | {
|
---|
| 70 | return !(end1 <= start2 || end2 <= start1);
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | static bool extract_component(uint8_t **cstart, uint8_t *cend,
|
---|
| 74 | uint8_t *ustart, uint8_t *uend, uintptr_t actual_ustart,
|
---|
| 75 | void (*clear_cache)(void *, size_t), task_t *task)
|
---|
| 76 | {
|
---|
| 77 | const char *name;
|
---|
[dcc2c5d] | 78 | size_t packed_size;
|
---|
[63a045c] | 79 |
|
---|
[dcc2c5d] | 80 | if (!tar_info(*cstart, cend, &name, &packed_size))
|
---|
[63a045c] | 81 | return false;
|
---|
| 82 |
|
---|
[d091007] | 83 | const uint8_t *data = *cstart + TAR_BLOCK_SIZE;
|
---|
[dcc2c5d] | 84 | *cstart += TAR_BLOCK_SIZE + ALIGN_UP(packed_size, TAR_BLOCK_SIZE);
|
---|
| 85 |
|
---|
[d091007] | 86 | bool gz = gzip_check(data, packed_size);
|
---|
| 87 | size_t unpacked_size = gz ? gzip_size(data, packed_size) : packed_size;
|
---|
[63a045c] | 88 |
|
---|
| 89 | /* Components must be page-aligned. */
|
---|
| 90 | uint8_t *new_ustart = (uint8_t *) ALIGN_UP((uintptr_t) ustart, PAGE_SIZE);
|
---|
| 91 | actual_ustart += new_ustart - ustart;
|
---|
| 92 | ustart = new_ustart;
|
---|
[dcc2c5d] | 93 | uint8_t *comp_end = ustart + unpacked_size;
|
---|
[63a045c] | 94 |
|
---|
| 95 | /* Check limits and overlap. */
|
---|
| 96 | if (overlaps(ustart, comp_end, loader_start, loader_end)) {
|
---|
| 97 | /* Move the component after bootloader. */
|
---|
| 98 | printf("%s would overlap bootloader, moving to %p.\n", name, loader_end);
|
---|
| 99 | uint8_t *new_ustart = (uint8_t *) ALIGN_UP((uintptr_t) loader_end, PAGE_SIZE);
|
---|
| 100 | actual_ustart += new_ustart - ustart;
|
---|
| 101 | ustart = new_ustart;
|
---|
[dcc2c5d] | 102 | comp_end = ustart + unpacked_size;
|
---|
[63a045c] | 103 | }
|
---|
| 104 |
|
---|
| 105 | if (comp_end > uend) {
|
---|
| 106 | printf("Not enough available memory for remaining components"
|
---|
| 107 | " (at least %zd more required).\n", comp_end - uend);
|
---|
| 108 | halt();
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | printf(" %p|%p: %s image (%zu/%zu bytes)\n", (void *) actual_ustart,
|
---|
[dcc2c5d] | 112 | ustart, name, unpacked_size, packed_size);
|
---|
[63a045c] | 113 |
|
---|
| 114 | if (task) {
|
---|
| 115 | task->addr = (void *) actual_ustart;
|
---|
[dcc2c5d] | 116 | task->size = unpacked_size;
|
---|
[63a045c] | 117 | str_cpy(task->name, BOOTINFO_TASK_NAME_BUFLEN, name);
|
---|
| 118 | /* Remove .gz extension */
|
---|
[dcc2c5d] | 119 | if (gz)
|
---|
| 120 | basename(task->name);
|
---|
[63a045c] | 121 | }
|
---|
| 122 |
|
---|
[dcc2c5d] | 123 | if (gz) {
|
---|
| 124 | int rc = gzip_expand(data, packed_size, ustart, unpacked_size);
|
---|
| 125 | if (rc != EOK) {
|
---|
| 126 | printf("\n%s: Inflating error %d\n", name, rc);
|
---|
| 127 | halt();
|
---|
| 128 | }
|
---|
| 129 | } else {
|
---|
| 130 | memcpy(ustart, data, unpacked_size);
|
---|
[63a045c] | 131 | }
|
---|
| 132 |
|
---|
| 133 | if (clear_cache)
|
---|
[dcc2c5d] | 134 | clear_cache(ustart, unpacked_size);
|
---|
| 135 |
|
---|
[63a045c] | 136 | return true;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
[dcc2c5d] | 139 | /* @return Bytes needed for unpacked payload. */
|
---|
| 140 | size_t payload_unpacked_size(void)
|
---|
[63a045c] | 141 | {
|
---|
| 142 | size_t sz = 0;
|
---|
| 143 | uint8_t *start = payload_start;
|
---|
| 144 | const char *name;
|
---|
[dcc2c5d] | 145 | size_t packed_size;
|
---|
[63a045c] | 146 |
|
---|
[dcc2c5d] | 147 | while (tar_info(start, payload_end, &name, &packed_size)) {
|
---|
[63a045c] | 148 | sz = ALIGN_UP(sz, PAGE_SIZE);
|
---|
[d091007] | 149 | if (gzip_check(start + TAR_BLOCK_SIZE, packed_size))
|
---|
[dcc2c5d] | 150 | sz += gzip_size(start + TAR_BLOCK_SIZE, packed_size);
|
---|
| 151 | else
|
---|
| 152 | sz += packed_size;
|
---|
[63a045c] | 153 |
|
---|
[dcc2c5d] | 154 | start += TAR_BLOCK_SIZE + ALIGN_UP(packed_size, TAR_BLOCK_SIZE);
|
---|
[63a045c] | 155 | }
|
---|
| 156 |
|
---|
| 157 | return sz;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | /**
|
---|
| 161 | * Extract the payload (kernel, loader, init binaries and the initrd image).
|
---|
| 162 | *
|
---|
| 163 | * @param bootinfo Pointer to the structure where the actual placement
|
---|
| 164 | * of components is recorded.
|
---|
| 165 | *
|
---|
| 166 | * @param kernel_dest Address of the kernel in the bootloader's address space.
|
---|
| 167 | * Kernel is the only part of the payload that has a fixed
|
---|
| 168 | * location and cannot be moved. If the kernel doesn't fit
|
---|
| 169 | * or would overlap bootloader, bootloader halts.
|
---|
| 170 | *
|
---|
| 171 | * @param mem_end End of usable contiguous memory.
|
---|
| 172 | * The caller guarantees that the entire area between
|
---|
| 173 | * kernel_start and mem_end is free and safe to write to,
|
---|
| 174 | * save possibly for the interval [loader_start, loader_end).
|
---|
| 175 | * All components are placed in this area. If there is not
|
---|
| 176 | * enough space for all components, bootloader halts.
|
---|
| 177 | *
|
---|
| 178 | * @param kernel_start Address the kernel will have in the kernel's own
|
---|
| 179 | * address space.
|
---|
| 180 | *
|
---|
| 181 | * @param clear_cache Caller-provided function for assuring cache coherence,
|
---|
| 182 | * whatever that means for a given platform. May be NULL.
|
---|
| 183 | */
|
---|
| 184 | void extract_payload(taskmap_t *tmap, uint8_t *kernel_dest, uint8_t *mem_end,
|
---|
| 185 | uintptr_t kernel_start, void (*clear_cache)(void *, size_t))
|
---|
| 186 | {
|
---|
| 187 | task_t task;
|
---|
| 188 | memset(&task, 0, sizeof(task));
|
---|
| 189 |
|
---|
| 190 | printf("Boot loader: %p -> %p\n", loader_start, loader_end);
|
---|
| 191 | printf("Payload: %p -> %p\n", payload_start, payload_end);
|
---|
| 192 | printf("Kernel load address: %p\n", kernel_dest);
|
---|
| 193 | printf("Kernel start: %p\n", (void *) kernel_start);
|
---|
| 194 | printf("RAM end: %p (%zd bytes available)\n", mem_end,
|
---|
| 195 | mem_end - kernel_dest);
|
---|
| 196 |
|
---|
| 197 | size_t payload_size = payload_end - payload_start;
|
---|
| 198 | uint8_t *real_payload_start;
|
---|
| 199 | uint8_t *real_payload_end;
|
---|
| 200 |
|
---|
| 201 | if (overlaps(kernel_dest, mem_end, payload_start, payload_end)) {
|
---|
| 202 | /*
|
---|
| 203 | * First, move the payload to the very end of available memory,
|
---|
[dcc2c5d] | 204 | * to make space for the unpacked data.
|
---|
[63a045c] | 205 | */
|
---|
| 206 | real_payload_start = (uint8_t *) ALIGN_DOWN((uintptr_t)(mem_end - payload_size), PAGE_SIZE);
|
---|
| 207 | real_payload_end = real_payload_start + payload_size;
|
---|
| 208 | memmove(real_payload_start, payload_start, payload_size);
|
---|
| 209 |
|
---|
| 210 | printf("Moved payload: %p -> %p\n", real_payload_start, real_payload_end);
|
---|
| 211 | } else {
|
---|
| 212 | real_payload_start = payload_start;
|
---|
| 213 | real_payload_end = payload_end;
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | printf("\nInflating components ... \n");
|
---|
| 217 |
|
---|
| 218 | uint8_t *end = mem_end;
|
---|
| 219 |
|
---|
| 220 | if (real_payload_end > kernel_dest && real_payload_start < mem_end)
|
---|
| 221 | end = real_payload_start;
|
---|
| 222 |
|
---|
| 223 | /* Kernel is always first. */
|
---|
| 224 | if (!extract_component(&real_payload_start, real_payload_end,
|
---|
| 225 | kernel_dest, end, kernel_start, clear_cache, &task)) {
|
---|
| 226 | printf("There is no kernel.\n");
|
---|
| 227 | halt();
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | if ((uintptr_t) task.addr != kernel_start) {
|
---|
| 231 | printf("Couldn't load kernel at the requested address.\n");
|
---|
| 232 | halt();
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | tmap->cnt = 0;
|
---|
| 236 |
|
---|
| 237 | for (int i = 0; i <= TASKMAP_MAX_RECORDS; i++) {
|
---|
| 238 | /*
|
---|
| 239 | * `task` holds the location and size of the previous component.
|
---|
| 240 | */
|
---|
| 241 | uintptr_t actual_dest =
|
---|
| 242 | ALIGN_UP((uintptr_t) task.addr + task.size, PAGE_SIZE);
|
---|
| 243 | uint8_t *dest = kernel_dest + (actual_dest - kernel_start);
|
---|
| 244 |
|
---|
| 245 | if (real_payload_end > dest && real_payload_start < mem_end)
|
---|
| 246 | end = real_payload_start;
|
---|
| 247 |
|
---|
| 248 | if (!extract_component(&real_payload_start, real_payload_end,
|
---|
| 249 | dest, end, actual_dest, clear_cache, &task))
|
---|
| 250 | break;
|
---|
| 251 |
|
---|
| 252 | if (i >= TASKMAP_MAX_RECORDS) {
|
---|
| 253 | printf("More components than the maximum of %d.\n",
|
---|
| 254 | TASKMAP_MAX_RECORDS);
|
---|
| 255 | halt();
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | tmap->tasks[i] = task;
|
---|
| 259 | tmap->cnt = i + 1;
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | printf("Done.\n");
|
---|
| 263 | }
|
---|