Debian Codegraph @

util/cbfstool/cbfs-payload-linux.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6
7 #include "common.h"
8 #include "cbfs.h"
9 #include "linux.h"
10
11 /* trampoline */
12 extern unsigned char trampoline[];
13 extern unsigned int trampoline_len;
14
15 /*
16 * Current max number of segments include:
17 *
18 * 1. parameters
19 * 2. kernel
20 * 3. trampoline
21 * 4. optional cmdline
22 * 5. optional initrd
23 * 6. terminating entry segment
24 */
25 #define MAX_NUM_SEGMENTS 6
26
27 struct bzpayload {
28 /* Input variables. */
29 int num_segments;
30 struct cbfs_payload_segment segs[MAX_NUM_SEGMENTS];
31 struct buffer parameters;
32 struct buffer kernel;
33 struct buffer trampoline;
34 struct buffer cmdline;
35 struct buffer initrd;
36 /* Output variables. */
37 struct buffer output;
38 size_t offset;
39 struct cbfs_payload_segment *out_seg;
40 };
41
42 static int bzp_init(struct bzpayload *bzp)
43 {
44 memset(bzp, 0, sizeof(*bzp));
45
46 /*
47 * Need at least the terminating entry segment.
48 */
49 bzp->num_segments = 1;
50
51 return 0;
52 }
53
54 static int bzp_add_initrd(struct bzpayload *bzp, const char *fname)
55 {
56 if (fname == NULL)
57 return 0;
58
59 if (buffer_from_file(&bzp->initrd, fname)) {
60 ERROR("could not open initrd.\n");
61 return -1;
62 }
63
64 bzp->num_segments++;
65
66 return 0;
67 }
68
69 static void bzp_add_segment(struct bzpayload *bzp, struct buffer *b, void *data,
70 size_t size)
71 {
72 buffer_init(b, NULL, data, size);
73 bzp->num_segments++;
74 }
75
76 static int bzp_add_trampoline(struct bzpayload *bzp)
77 {
78 bzp_add_segment(bzp, &bzp->trampoline, trampoline,
79 trampoline_len);
80 return 0;
81 }
82
83 static int bzp_add_cmdline(struct bzpayload *bzp, char *cmdline)
84 {
85 if (cmdline == NULL)
86 return 0;
87
88 bzp_add_segment(bzp, &bzp->cmdline, cmdline, strlen(cmdline) + 1);
89
90 return 0;
91 }
92
93 static int bzp_add_params(struct bzpayload *bzp, struct linux_params *params)
94 {
95 bzp_add_segment(bzp, &bzp->parameters, params, sizeof(*params));
96
97 return 0;
98 }
99
100 static int bzp_add_kernel(struct bzpayload *bzp, const struct buffer *in,
101 size_t setup_size)
102 {
103 char *input = buffer_get(in);
104 size_t kern_sz = buffer_size(in) - setup_size;
105
106 bzp_add_segment(bzp, &bzp->kernel, &input[setup_size], kern_sz);
107
108 return 0;
109 }
110
111 static int bzp_init_output(struct bzpayload *bzp, const char *name)
112 {
113 size_t sz = 0;
114
115 sz += buffer_size(&bzp->parameters);
116 sz += buffer_size(&bzp->kernel);
117 sz += buffer_size(&bzp->trampoline);
118 sz += buffer_size(&bzp->cmdline);
119 sz += buffer_size(&bzp->initrd);
120
121 bzp->offset = bzp->num_segments * sizeof(struct cbfs_payload_segment);
122 sz += bzp->offset;
123
124 if (buffer_create(&bzp->output, sz, name) != 0)
125 return -1;
126
127 bzp->out_seg = &bzp->segs[0];
128
129 return 0;
130 }
131
132 static int bzp_output_segment(struct bzpayload *bzp, struct buffer *b,
133 uint32_t type, uint64_t load_addr,
134 enum cbfs_compression algo)
135 {
136 struct buffer out;
137 struct cbfs_payload_segment *seg;
138 int len = 0;
139
140 /* Don't process empty buffers. */
141 if (b != NULL && buffer_size(b) == 0) {
142 ERROR("%s(): Input buffer is empty\n", __func__);
143 return -1;
144 }
145
146 seg = bzp->out_seg;
147 seg->type = type;
148 seg->load_addr = load_addr;
149 bzp->out_seg++;
150
151 /* No buffer associated with segment. */
152 if (b == NULL) {
153 if (type == PAYLOAD_SEGMENT_ENTRY)
154 return 0; // entry segments don't have a buffer attached
155 else
156 return -1;
157 }
158
159 /* Use a temp buffer for easier management. */
160 buffer_splice(&out, &bzp->output, bzp->offset, buffer_size(b));
161
162 seg->mem_len = buffer_size(b);
163 seg->offset = bzp->offset;
164
165 comp_func_ptr compress_func = compression_function(algo);
166 if (compress_func(buffer_get(b), buffer_size(b), buffer_get(&out), &len)) {
167 WARN("Compression failed or would make the data bigger "
168 "- disabled.\n");
169 algo = CBFS_COMPRESS_NONE;
170 len = buffer_size(b);
171 memcpy(buffer_get(&out), buffer_get(b), len);
172 }
173
174 seg->compression = algo;
175 seg->len = len;
176
177 /* Update output offset. */
178 bzp->offset += len;
179
180 return 0; // success
181 }
182
183 /* TODO:
184 * handle special arguments
185 * mem= argument - only affects loading decisions (kernel + initrd), not e820 -> build time
186 * vga= argument (FILO ignores this)
187 * add support for more parameters to trampoline:
188 * alt_mem_k, ext_mem_k (not strictly necessary since e820 takes precedence)
189 * framebuffer/console values
190 *
191 * larger work:
192 * is compress() safe to use in a size constrained buffer? ie. do(es) the
193 * compression algorithm(s) stop once the compression result reaches input
194 * size (ie. incompressible data)?
195 */
196 int parse_bzImage_to_payload(const struct buffer *input,
197 struct buffer *output, const char *initrd_name,
198 char *cmdline, enum cbfs_compression algo)
199 {
200 struct bzpayload bzp;
201 unsigned int initrd_base = 64*1024*1024;
202 struct linux_header *hdr = (struct linux_header *)input->data;
203 unsigned int setup_size = 4 * 512;
204
205 if (compression_function(algo) == NULL) {
206 ERROR("Invalid compression algorithm specified.\n");
207 return -1;
208 }
209
210 if (bzp_init(&bzp) != 0)
211 return -1;
212
213 if (bzp_add_trampoline(&bzp) != 0)
214 return -1;
215
216 if (bzp_add_initrd(&bzp, initrd_name) != 0)
217 return -1;
218
219 if (bzp_add_cmdline(&bzp, cmdline) != 0)
220 return -1;
221
222 if (hdr->setup_sects != 0) {
223 setup_size = (hdr->setup_sects + 1) * 512;
224 } else {
225 WARN("hdr->setup_sects is 0, which could cause boot problems.\n");
226 }
227
228 /* Setup parameter block. Imitate FILO. */
229 struct linux_params params;
230
231 memset(&params, 0, sizeof(struct linux_params));
232
233 params.mount_root_rdonly = hdr->root_flags;
234 params.orig_root_dev = hdr->root_dev;
235 params.init_size = hdr->init_size;
236
237 /* Sensible video defaults. Might be overridden on runtime by coreboot tables. */
238 params.orig_video_mode = 3;
239 params.orig_video_cols = 80;
240 params.orig_video_lines = 25;
241 params.orig_video_isVGA = 1;
242 params.orig_video_points = 16;
243
244 params.loader_type = 0xff; /* Unregistered Linux loader */
245
246 if (cmdline != NULL) {
247 if (hdr->protocol_version < 0x202) {
248 params.cl_magic = CL_MAGIC_VALUE;
249 params.cl_offset = COMMAND_LINE_LOC - LINUX_PARAM_LOC;
250 } else {
251 params.cmd_line_ptr = COMMAND_LINE_LOC;
252 }
253 }
254
255 unsigned long kernel_base = 0x100000;
256 if ((hdr->protocol_version < 0x200) || !(hdr->loadflags & 1)) {
257 kernel_base = 0x1000; /* zImage kernel */
258 }
259 /* kernel prefers an address, so listen */
260 if ((hdr->protocol_version >= 0x20a) && (!(hdr->pref_address >> 32))) {
261 kernel_base = hdr->pref_address;
262 }
263 if (hdr->protocol_version >= 0x205) {
264 params.relocatable_kernel = hdr->relocatable_kernel;
265 params.kernel_alignment = hdr->kernel_alignment;
266 if (hdr->relocatable_kernel != 0) {
267 /* 16 MB should be way outside coreboot's playground,
268 * so if possible (relocatable kernel) use that to
269 * avoid a trampoline copy. */
270 kernel_base = ALIGN_UP(16*1024*1024, params.kernel_alignment);
271 if (hdr->init_size == 0) {
272 ERROR("init_size 0 for relocatable kernel\n");
273 return -1;
274 }
275 }
276 }
277
278 /* We have a trampoline and use that, but it can simply use
279 * this information for its jump to real Linux. */
280 params.kernel_start = kernel_base;
281
282 /* To make decisions based on the protocol version,
283 copy that as well. */
284 params.param_block_version = hdr->protocol_version;
285
286 if (bzp_add_kernel(&bzp, input, setup_size) != 0)
287 return -1;
288
289 if (buffer_size(&bzp.initrd) != 0) {
290 /* TODO: this is a bit of a hack. Linux recommends to store
291 * initrd near to end-of-mem, but that's hard to do on build
292 * time. It definitely fails to read the image if it's too
293 * close to the kernel, so give it some room.
294 */
295 initrd_base = kernel_base + buffer_size(&bzp.kernel);
296 initrd_base = ALIGN_UP(initrd_base, 64*1024*1024);
297
298 params.initrd_start = initrd_base;
299 params.initrd_size = buffer_size(&bzp.initrd);
300 }
301
302 if (bzp_add_params(&bzp, &params) != 0)
303 return -1;
304
305 if (bzp_init_output(&bzp, input->name) != 0)
306 return -1;
307
308 int ret;
309
310 /* parameter block */
311 ret = bzp_output_segment(&bzp, &bzp.parameters,
312 PAYLOAD_SEGMENT_DATA, LINUX_PARAM_LOC, algo);
313 if (ret) {
314 ERROR("%s(): Failed to write Linux zero page into segment\n", __func__);
315 return ret;
316 }
317
318 /* code block: There is no point in compressing the bzImage (it is already compressed)*/
319 ret = bzp_output_segment(&bzp, &bzp.kernel,
320 PAYLOAD_SEGMENT_CODE, kernel_base, CBFS_COMPRESS_NONE);
321 if (ret) {
322 ERROR("%s(): Failed to write Linux kernel into segment\n", __func__);
323 return ret;
324 }
325
326 /* trampoline */
327 ret = bzp_output_segment(&bzp, &bzp.trampoline,
328 PAYLOAD_SEGMENT_CODE, TRAMPOLINE_ENTRY_LOC, algo);
329 if (ret) {
330 ERROR("%s(): Failed to write Linux trampoline into segment\n", __func__);
331 return ret;
332 }
333
334 /* cmdline */
335 if (buffer_size(&bzp.cmdline) != 0) {
336 ret = bzp_output_segment(&bzp, &bzp.cmdline,
337 PAYLOAD_SEGMENT_DATA, COMMAND_LINE_LOC, algo);
338 if (ret) {
339 ERROR("%s(): Failed to write Linux cmdline into segment\n", __func__);
340 return ret;
341 }
342 }
343
344 if (buffer_size(&bzp.initrd) != 0) {
345 /* initrd */
346 ret = bzp_output_segment(&bzp, &bzp.initrd,
347 PAYLOAD_SEGMENT_DATA, initrd_base, algo);
348 if (ret) {
349 ERROR("%s(): Failed to write Linux initrd into segment\n", __func__);
350 return ret;
351 }
352 }
353
354 /* Terminating entry segment. */
355 ret = bzp_output_segment(&bzp, NULL, PAYLOAD_SEGMENT_ENTRY, TRAMPOLINE_ENTRY_LOC, algo);
356 if (ret) {
357 ERROR("%s(): Failed to write entry segment\n", __func__);
358 return ret;
359 }
360
361 /* Set size of buffer taking into account potential compression. */
362 buffer_set_size(&bzp.output, bzp.offset);
363 /* Make passed-in output buffer be valid. */
364 buffer_clone(output, &bzp.output);
365
366 /* Serialize the segments with the correct encoding. */
367 xdr_segs(output, bzp.segs, bzp.num_segments);
368 return 0;
369 }