1 | /*
|
---|
2 | * SPDX-FileCopyrightText: 2007 Michal Kebrt
|
---|
3 | * SPDX-FileCopyrightText: 2018 Jiří Zárevúcky
|
---|
4 | *
|
---|
5 | * SPDX-License-Identifier: BSD-3-Clause
|
---|
6 | */
|
---|
7 |
|
---|
8 | #include <payload.h>
|
---|
9 |
|
---|
10 | #include <align.h>
|
---|
11 | #include <printf.h>
|
---|
12 | #include <arch/arch.h>
|
---|
13 | #include <tar.h>
|
---|
14 | #include <gzip.h>
|
---|
15 | #include <stdbool.h>
|
---|
16 | #include <memstr.h>
|
---|
17 | #include <errno.h>
|
---|
18 | #include <str.h>
|
---|
19 | #include <halt.h>
|
---|
20 |
|
---|
21 | static const char *ext(const char *s)
|
---|
22 | {
|
---|
23 | const char *last = s;
|
---|
24 |
|
---|
25 | while (*s) {
|
---|
26 | if (*s == '.')
|
---|
27 | last = s;
|
---|
28 |
|
---|
29 | s++;
|
---|
30 | }
|
---|
31 |
|
---|
32 | if (*last == '.')
|
---|
33 | return last;
|
---|
34 |
|
---|
35 | return NULL;
|
---|
36 | }
|
---|
37 |
|
---|
38 | static void basename(char *s)
|
---|
39 | {
|
---|
40 | char *e = (char *) ext(s);
|
---|
41 | if ((e != NULL) && (str_cmp(e, ".gz") == 0))
|
---|
42 | *e = '\0';
|
---|
43 | }
|
---|
44 |
|
---|
45 | static bool overlaps(uint8_t *start1, uint8_t *end1,
|
---|
46 | uint8_t *start2, uint8_t *end2)
|
---|
47 | {
|
---|
48 | return !(end1 <= start2 || end2 <= start1);
|
---|
49 | }
|
---|
50 |
|
---|
51 | static bool extract_component(uint8_t **cstart, uint8_t *cend,
|
---|
52 | uint8_t *ustart, uint8_t *uend, uintptr_t actual_ustart,
|
---|
53 | void (*clear_cache)(void *, size_t), task_t *task)
|
---|
54 | {
|
---|
55 | const char *name;
|
---|
56 | size_t packed_size;
|
---|
57 |
|
---|
58 | if (!tar_info(*cstart, cend, &name, &packed_size))
|
---|
59 | return false;
|
---|
60 |
|
---|
61 | const uint8_t *data = *cstart + TAR_BLOCK_SIZE;
|
---|
62 | *cstart += TAR_BLOCK_SIZE + ALIGN_UP(packed_size, TAR_BLOCK_SIZE);
|
---|
63 |
|
---|
64 | bool gz = gzip_check(data, packed_size);
|
---|
65 | size_t unpacked_size = gz ? gzip_size(data, packed_size) : packed_size;
|
---|
66 |
|
---|
67 | /* Components must be page-aligned. */
|
---|
68 | uint8_t *new_ustart = (uint8_t *) ALIGN_UP((uintptr_t) ustart, PAGE_SIZE);
|
---|
69 | actual_ustart += new_ustart - ustart;
|
---|
70 | ustart = new_ustart;
|
---|
71 | uint8_t *comp_end = ustart + unpacked_size;
|
---|
72 |
|
---|
73 | /* Check limits and overlap. */
|
---|
74 | if (overlaps(ustart, comp_end, loader_start, loader_end)) {
|
---|
75 | /* Move the component after bootloader. */
|
---|
76 | printf("%s would overlap bootloader, moving to %p.\n", name, loader_end);
|
---|
77 | uint8_t *new_ustart = (uint8_t *) ALIGN_UP((uintptr_t) loader_end, PAGE_SIZE);
|
---|
78 | actual_ustart += new_ustart - ustart;
|
---|
79 | ustart = new_ustart;
|
---|
80 | comp_end = ustart + unpacked_size;
|
---|
81 | }
|
---|
82 |
|
---|
83 | if (comp_end > uend) {
|
---|
84 | printf("Not enough available memory for remaining components"
|
---|
85 | " (at least %zd more required).\n", comp_end - uend);
|
---|
86 | halt();
|
---|
87 | }
|
---|
88 |
|
---|
89 | printf(" %p|%p: %s image (%zu/%zu bytes)\n", (void *) actual_ustart,
|
---|
90 | ustart, name, unpacked_size, packed_size);
|
---|
91 |
|
---|
92 | if (task) {
|
---|
93 | task->addr = (void *) actual_ustart;
|
---|
94 | task->size = unpacked_size;
|
---|
95 | str_cpy(task->name, BOOTINFO_TASK_NAME_BUFLEN, name);
|
---|
96 | /* Remove .gz extension */
|
---|
97 | if (gz)
|
---|
98 | basename(task->name);
|
---|
99 | }
|
---|
100 |
|
---|
101 | if (gz) {
|
---|
102 | int rc = gzip_expand(data, packed_size, ustart, unpacked_size);
|
---|
103 | if (rc != EOK) {
|
---|
104 | printf("\n%s: Inflating error %d\n", name, rc);
|
---|
105 | halt();
|
---|
106 | }
|
---|
107 | } else {
|
---|
108 | memcpy(ustart, data, unpacked_size);
|
---|
109 | }
|
---|
110 |
|
---|
111 | if (clear_cache)
|
---|
112 | clear_cache(ustart, unpacked_size);
|
---|
113 |
|
---|
114 | return true;
|
---|
115 | }
|
---|
116 |
|
---|
117 | /* @return Bytes needed for unpacked payload. */
|
---|
118 | size_t payload_unpacked_size(void)
|
---|
119 | {
|
---|
120 | size_t sz = 0;
|
---|
121 | uint8_t *start = payload_start;
|
---|
122 | const char *name;
|
---|
123 | size_t packed_size;
|
---|
124 |
|
---|
125 | while (tar_info(start, payload_end, &name, &packed_size)) {
|
---|
126 | sz = ALIGN_UP(sz, PAGE_SIZE);
|
---|
127 | if (gzip_check(start + TAR_BLOCK_SIZE, packed_size))
|
---|
128 | sz += gzip_size(start + TAR_BLOCK_SIZE, packed_size);
|
---|
129 | else
|
---|
130 | sz += packed_size;
|
---|
131 |
|
---|
132 | start += TAR_BLOCK_SIZE + ALIGN_UP(packed_size, TAR_BLOCK_SIZE);
|
---|
133 | }
|
---|
134 |
|
---|
135 | return sz;
|
---|
136 | }
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Extract the payload (kernel, loader, init binaries and the initrd image).
|
---|
140 | *
|
---|
141 | * @param bootinfo Pointer to the structure where the actual placement
|
---|
142 | * of components is recorded.
|
---|
143 | *
|
---|
144 | * @param kernel_dest Address of the kernel in the bootloader's address space.
|
---|
145 | * Kernel is the only part of the payload that has a fixed
|
---|
146 | * location and cannot be moved. If the kernel doesn't fit
|
---|
147 | * or would overlap bootloader, bootloader halts.
|
---|
148 | *
|
---|
149 | * @param mem_end End of usable contiguous memory.
|
---|
150 | * The caller guarantees that the entire area between
|
---|
151 | * kernel_start and mem_end is free and safe to write to,
|
---|
152 | * save possibly for the interval [loader_start, loader_end).
|
---|
153 | * All components are placed in this area. If there is not
|
---|
154 | * enough space for all components, bootloader halts.
|
---|
155 | *
|
---|
156 | * @param kernel_start Address the kernel will have in the kernel's own
|
---|
157 | * address space.
|
---|
158 | *
|
---|
159 | * @param clear_cache Caller-provided function for assuring cache coherence,
|
---|
160 | * whatever that means for a given platform. May be NULL.
|
---|
161 | */
|
---|
162 | void extract_payload(taskmap_t *tmap, uint8_t *kernel_dest, uint8_t *mem_end,
|
---|
163 | uintptr_t kernel_start, void (*clear_cache)(void *, size_t))
|
---|
164 | {
|
---|
165 | task_t task;
|
---|
166 | memset(&task, 0, sizeof(task));
|
---|
167 |
|
---|
168 | printf("Boot loader: %p -> %p\n", loader_start, loader_end);
|
---|
169 | printf("Payload: %p -> %p\n", payload_start, payload_end);
|
---|
170 | printf("Kernel load address: %p\n", kernel_dest);
|
---|
171 | printf("Kernel start: %p\n", (void *) kernel_start);
|
---|
172 | printf("RAM end: %p (%zd bytes available)\n", mem_end,
|
---|
173 | mem_end - kernel_dest);
|
---|
174 |
|
---|
175 | size_t payload_size = payload_end - payload_start;
|
---|
176 | uint8_t *real_payload_start;
|
---|
177 | uint8_t *real_payload_end;
|
---|
178 |
|
---|
179 | if (overlaps(kernel_dest, mem_end, payload_start, payload_end)) {
|
---|
180 | /*
|
---|
181 | * First, move the payload to the very end of available memory,
|
---|
182 | * to make space for the unpacked data.
|
---|
183 | */
|
---|
184 | real_payload_start = (uint8_t *) ALIGN_DOWN((uintptr_t)(mem_end - payload_size), PAGE_SIZE);
|
---|
185 | real_payload_end = real_payload_start + payload_size;
|
---|
186 | memmove(real_payload_start, payload_start, payload_size);
|
---|
187 |
|
---|
188 | printf("Moved payload: %p -> %p\n", real_payload_start, real_payload_end);
|
---|
189 | } else {
|
---|
190 | real_payload_start = payload_start;
|
---|
191 | real_payload_end = payload_end;
|
---|
192 | }
|
---|
193 |
|
---|
194 | printf("\nInflating components ... \n");
|
---|
195 |
|
---|
196 | uint8_t *end = mem_end;
|
---|
197 |
|
---|
198 | if (real_payload_end > kernel_dest && real_payload_start < mem_end)
|
---|
199 | end = real_payload_start;
|
---|
200 |
|
---|
201 | /* Kernel is always first. */
|
---|
202 | if (!extract_component(&real_payload_start, real_payload_end,
|
---|
203 | kernel_dest, end, kernel_start, clear_cache, &task)) {
|
---|
204 | printf("There is no kernel.\n");
|
---|
205 | halt();
|
---|
206 | }
|
---|
207 |
|
---|
208 | if ((uintptr_t) task.addr != kernel_start) {
|
---|
209 | printf("Couldn't load kernel at the requested address.\n");
|
---|
210 | halt();
|
---|
211 | }
|
---|
212 |
|
---|
213 | tmap->cnt = 0;
|
---|
214 |
|
---|
215 | for (int i = 0; i <= TASKMAP_MAX_RECORDS; i++) {
|
---|
216 | /*
|
---|
217 | * `task` holds the location and size of the previous component.
|
---|
218 | */
|
---|
219 | uintptr_t actual_dest =
|
---|
220 | ALIGN_UP((uintptr_t) task.addr + task.size, PAGE_SIZE);
|
---|
221 | uint8_t *dest = kernel_dest + (actual_dest - kernel_start);
|
---|
222 |
|
---|
223 | if (real_payload_end > dest && real_payload_start < mem_end)
|
---|
224 | end = real_payload_start;
|
---|
225 |
|
---|
226 | if (!extract_component(&real_payload_start, real_payload_end,
|
---|
227 | dest, end, actual_dest, clear_cache, &task))
|
---|
228 | break;
|
---|
229 |
|
---|
230 | if (i >= TASKMAP_MAX_RECORDS) {
|
---|
231 | printf("More components than the maximum of %d.\n",
|
---|
232 | TASKMAP_MAX_RECORDS);
|
---|
233 | halt();
|
---|
234 | }
|
---|
235 |
|
---|
236 | tmap->tasks[i] = task;
|
---|
237 | tmap->cnt = i + 1;
|
---|
238 | }
|
---|
239 |
|
---|
240 | printf("Done.\n");
|
---|
241 | }
|
---|