source: mainline/boot/generic/src/payload.c@ 63a045c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 63a045c was 63a045c, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Unify handling of compressed init data and use regular tar + gzip to achieve it

There are two issues this commit solves.

First is that architecture-specific code duplicates most of the init binary
handling in each architecture, each with miniscule and confusing variations.
After this commit, the init binary expansion is almost entirely handled by
unified generic code.

Second is that the way we used to generate the incorporated data is somewhat
convoluted. Previously we have a Python script which generates a zip archive
with individual deflate-compressed files and accompanying header and C files
which contain structures describing the archive contents.
The zip file is then extracted and the individual deflate-compressed files are
included in the binary via assembler code.
Since gas doesn't take particular care to be consistent between architectures,
the assembly portions are also not uniform and the build script needs to know
particulars of the architecture's assembly.

Instead of doing that, after this commit we first gzip each included file, then
we pack the gzipped files into a tar archive, and then we include the archive
into the binary using objcopy.
Linker script provides symbols for the start and end of the archive,
and the payload is in a self-describing format, so there is no need for any
generated code.

Note that we are doing the opposite of the conventional .tar.gz format.
It would be somewhat inconvenient to use .tar.gz since the uncompressed files
need to be aligned to page size, so we'd have to first decompress the entire
payload to determine the final position of the files (and hence the required
amount of memory).

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