[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);
|
---|
| 63 | if (str_cmp(e, ".gz") == 0)
|
---|
| 64 | *e = '\0';
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | static bool isgzip(const char *s)
|
---|
| 68 | {
|
---|
| 69 | return str_cmp(ext(s), ".gz") == 0;
|
---|
[63a045c] | 70 | }
|
---|
| 71 |
|
---|
| 72 | static bool overlaps(uint8_t *start1, uint8_t *end1,
|
---|
| 73 | uint8_t *start2, uint8_t *end2)
|
---|
| 74 | {
|
---|
| 75 | return !(end1 <= start2 || end2 <= start1);
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | static bool extract_component(uint8_t **cstart, uint8_t *cend,
|
---|
| 79 | uint8_t *ustart, uint8_t *uend, uintptr_t actual_ustart,
|
---|
| 80 | void (*clear_cache)(void *, size_t), task_t *task)
|
---|
| 81 | {
|
---|
| 82 | const char *name;
|
---|
| 83 | const uint8_t *data;
|
---|
[dcc2c5d] | 84 | size_t packed_size;
|
---|
| 85 | size_t unpacked_size;
|
---|
[63a045c] | 86 |
|
---|
[dcc2c5d] | 87 | if (!tar_info(*cstart, cend, &name, &packed_size))
|
---|
[63a045c] | 88 | return false;
|
---|
| 89 |
|
---|
| 90 | data = *cstart + TAR_BLOCK_SIZE;
|
---|
[dcc2c5d] | 91 | *cstart += TAR_BLOCK_SIZE + ALIGN_UP(packed_size, TAR_BLOCK_SIZE);
|
---|
| 92 |
|
---|
| 93 | bool gz = isgzip(name);
|
---|
[63a045c] | 94 |
|
---|
[dcc2c5d] | 95 | unpacked_size = gz ? gzip_size(data, packed_size) : packed_size;
|
---|
[63a045c] | 96 |
|
---|
| 97 | /* Components must be page-aligned. */
|
---|
| 98 | uint8_t *new_ustart = (uint8_t *) ALIGN_UP((uintptr_t) ustart, PAGE_SIZE);
|
---|
| 99 | actual_ustart += new_ustart - ustart;
|
---|
| 100 | ustart = new_ustart;
|
---|
[dcc2c5d] | 101 | uint8_t *comp_end = ustart + unpacked_size;
|
---|
[63a045c] | 102 |
|
---|
| 103 | /* Check limits and overlap. */
|
---|
| 104 | if (overlaps(ustart, comp_end, loader_start, loader_end)) {
|
---|
| 105 | /* Move the component after bootloader. */
|
---|
| 106 | printf("%s would overlap bootloader, moving to %p.\n", name, loader_end);
|
---|
| 107 | uint8_t *new_ustart = (uint8_t *) ALIGN_UP((uintptr_t) loader_end, PAGE_SIZE);
|
---|
| 108 | actual_ustart += new_ustart - ustart;
|
---|
| 109 | ustart = new_ustart;
|
---|
[dcc2c5d] | 110 | comp_end = ustart + unpacked_size;
|
---|
[63a045c] | 111 | }
|
---|
| 112 |
|
---|
| 113 | if (comp_end > uend) {
|
---|
| 114 | printf("Not enough available memory for remaining components"
|
---|
| 115 | " (at least %zd more required).\n", comp_end - uend);
|
---|
| 116 | halt();
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | printf(" %p|%p: %s image (%zu/%zu bytes)\n", (void *) actual_ustart,
|
---|
[dcc2c5d] | 120 | ustart, name, unpacked_size, packed_size);
|
---|
[63a045c] | 121 |
|
---|
| 122 | if (task) {
|
---|
| 123 | task->addr = (void *) actual_ustart;
|
---|
[dcc2c5d] | 124 | task->size = unpacked_size;
|
---|
[63a045c] | 125 | str_cpy(task->name, BOOTINFO_TASK_NAME_BUFLEN, name);
|
---|
| 126 | /* Remove .gz extension */
|
---|
[dcc2c5d] | 127 | if (gz)
|
---|
| 128 | basename(task->name);
|
---|
[63a045c] | 129 | }
|
---|
| 130 |
|
---|
[dcc2c5d] | 131 | if (gz) {
|
---|
| 132 | int rc = gzip_expand(data, packed_size, ustart, unpacked_size);
|
---|
| 133 | if (rc != EOK) {
|
---|
| 134 | printf("\n%s: Inflating error %d\n", name, rc);
|
---|
| 135 | halt();
|
---|
| 136 | }
|
---|
| 137 | } else {
|
---|
| 138 | memcpy(ustart, data, unpacked_size);
|
---|
[63a045c] | 139 | }
|
---|
| 140 |
|
---|
| 141 | if (clear_cache)
|
---|
[dcc2c5d] | 142 | clear_cache(ustart, unpacked_size);
|
---|
| 143 |
|
---|
[63a045c] | 144 | return true;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
[dcc2c5d] | 147 | /* @return Bytes needed for unpacked payload. */
|
---|
| 148 | size_t payload_unpacked_size(void)
|
---|
[63a045c] | 149 | {
|
---|
| 150 | size_t sz = 0;
|
---|
| 151 | uint8_t *start = payload_start;
|
---|
| 152 | const char *name;
|
---|
[dcc2c5d] | 153 | size_t packed_size;
|
---|
[63a045c] | 154 |
|
---|
[dcc2c5d] | 155 | while (tar_info(start, payload_end, &name, &packed_size)) {
|
---|
[63a045c] | 156 | sz = ALIGN_UP(sz, PAGE_SIZE);
|
---|
[dcc2c5d] | 157 | if (isgzip(name))
|
---|
| 158 | sz += gzip_size(start + TAR_BLOCK_SIZE, packed_size);
|
---|
| 159 | else
|
---|
| 160 | sz += packed_size;
|
---|
[63a045c] | 161 |
|
---|
[dcc2c5d] | 162 | start += TAR_BLOCK_SIZE + ALIGN_UP(packed_size, TAR_BLOCK_SIZE);
|
---|
[63a045c] | 163 | }
|
---|
| 164 |
|
---|
| 165 | return sz;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | /**
|
---|
| 169 | * Extract the payload (kernel, loader, init binaries and the initrd image).
|
---|
| 170 | *
|
---|
| 171 | * @param bootinfo Pointer to the structure where the actual placement
|
---|
| 172 | * of components is recorded.
|
---|
| 173 | *
|
---|
| 174 | * @param kernel_dest Address of the kernel in the bootloader's address space.
|
---|
| 175 | * Kernel is the only part of the payload that has a fixed
|
---|
| 176 | * location and cannot be moved. If the kernel doesn't fit
|
---|
| 177 | * or would overlap bootloader, bootloader halts.
|
---|
| 178 | *
|
---|
| 179 | * @param mem_end End of usable contiguous memory.
|
---|
| 180 | * The caller guarantees that the entire area between
|
---|
| 181 | * kernel_start and mem_end is free and safe to write to,
|
---|
| 182 | * save possibly for the interval [loader_start, loader_end).
|
---|
| 183 | * All components are placed in this area. If there is not
|
---|
| 184 | * enough space for all components, bootloader halts.
|
---|
| 185 | *
|
---|
| 186 | * @param kernel_start Address the kernel will have in the kernel's own
|
---|
| 187 | * address space.
|
---|
| 188 | *
|
---|
| 189 | * @param clear_cache Caller-provided function for assuring cache coherence,
|
---|
| 190 | * whatever that means for a given platform. May be NULL.
|
---|
| 191 | */
|
---|
| 192 | void extract_payload(taskmap_t *tmap, uint8_t *kernel_dest, uint8_t *mem_end,
|
---|
| 193 | uintptr_t kernel_start, void (*clear_cache)(void *, size_t))
|
---|
| 194 | {
|
---|
| 195 | task_t task;
|
---|
| 196 | memset(&task, 0, sizeof(task));
|
---|
| 197 |
|
---|
| 198 | printf("Boot loader: %p -> %p\n", loader_start, loader_end);
|
---|
| 199 | printf("Payload: %p -> %p\n", payload_start, payload_end);
|
---|
| 200 | printf("Kernel load address: %p\n", kernel_dest);
|
---|
| 201 | printf("Kernel start: %p\n", (void *) kernel_start);
|
---|
| 202 | printf("RAM end: %p (%zd bytes available)\n", mem_end,
|
---|
| 203 | mem_end - kernel_dest);
|
---|
| 204 |
|
---|
| 205 | size_t payload_size = payload_end - payload_start;
|
---|
| 206 | uint8_t *real_payload_start;
|
---|
| 207 | uint8_t *real_payload_end;
|
---|
| 208 |
|
---|
| 209 | if (overlaps(kernel_dest, mem_end, payload_start, payload_end)) {
|
---|
| 210 | /*
|
---|
| 211 | * First, move the payload to the very end of available memory,
|
---|
[dcc2c5d] | 212 | * to make space for the unpacked data.
|
---|
[63a045c] | 213 | */
|
---|
| 214 | real_payload_start = (uint8_t *) ALIGN_DOWN((uintptr_t)(mem_end - payload_size), PAGE_SIZE);
|
---|
| 215 | real_payload_end = real_payload_start + payload_size;
|
---|
| 216 | memmove(real_payload_start, payload_start, payload_size);
|
---|
| 217 |
|
---|
| 218 | printf("Moved payload: %p -> %p\n", real_payload_start, real_payload_end);
|
---|
| 219 | } else {
|
---|
| 220 | real_payload_start = payload_start;
|
---|
| 221 | real_payload_end = payload_end;
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | printf("\nInflating components ... \n");
|
---|
| 225 |
|
---|
| 226 | uint8_t *end = mem_end;
|
---|
| 227 |
|
---|
| 228 | if (real_payload_end > kernel_dest && real_payload_start < mem_end)
|
---|
| 229 | end = real_payload_start;
|
---|
| 230 |
|
---|
| 231 | /* Kernel is always first. */
|
---|
| 232 | if (!extract_component(&real_payload_start, real_payload_end,
|
---|
| 233 | kernel_dest, end, kernel_start, clear_cache, &task)) {
|
---|
| 234 | printf("There is no kernel.\n");
|
---|
| 235 | halt();
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | if ((uintptr_t) task.addr != kernel_start) {
|
---|
| 239 | printf("Couldn't load kernel at the requested address.\n");
|
---|
| 240 | halt();
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | tmap->cnt = 0;
|
---|
| 244 |
|
---|
| 245 | for (int i = 0; i <= TASKMAP_MAX_RECORDS; i++) {
|
---|
| 246 | /*
|
---|
| 247 | * `task` holds the location and size of the previous component.
|
---|
| 248 | */
|
---|
| 249 | uintptr_t actual_dest =
|
---|
| 250 | ALIGN_UP((uintptr_t) task.addr + task.size, PAGE_SIZE);
|
---|
| 251 | uint8_t *dest = kernel_dest + (actual_dest - kernel_start);
|
---|
| 252 |
|
---|
| 253 | if (real_payload_end > dest && real_payload_start < mem_end)
|
---|
| 254 | end = real_payload_start;
|
---|
| 255 |
|
---|
| 256 | if (!extract_component(&real_payload_start, real_payload_end,
|
---|
| 257 | dest, end, actual_dest, clear_cache, &task))
|
---|
| 258 | break;
|
---|
| 259 |
|
---|
| 260 | if (i >= TASKMAP_MAX_RECORDS) {
|
---|
| 261 | printf("More components than the maximum of %d.\n",
|
---|
| 262 | TASKMAP_MAX_RECORDS);
|
---|
| 263 | halt();
|
---|
| 264 | }
|
---|
| 265 |
|
---|
| 266 | tmap->tasks[i] = task;
|
---|
| 267 | tmap->cnt = i + 1;
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 | printf("Done.\n");
|
---|
| 271 | }
|
---|