blob: 34b21e9054b15c526474273a76204792c92cee84 [file] [log] [blame]
Patrick Georgide36d332013-08-27 20:22:21 +02001/*
2 * cbfs-payload-linux
3 *
4 * Copyright (C) 2013 Patrick Georgi <patrick@georgi-clan.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Patrick Georgide36d332013-08-27 20:22:21 +020014 */
15
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19
20#include "common.h"
21#include "cbfs.h"
22#include "linux.h"
23
Aaron Durbin4f3bb802014-03-26 22:57:55 -050024/* trampoline */
Stefan Reinauer5dda4df2015-12-01 17:58:08 -080025extern unsigned char trampoline[];
26extern unsigned int trampoline_len;
Aaron Durbin4f3bb802014-03-26 22:57:55 -050027
28/*
29 * Current max number of segments include:
30 *
31 * 1. parameters
32 * 2. kernel
33 * 3. trampoline
34 * 4. optional cmdline
35 * 5. optional initrd
36 * 6. terminating entry segment
37 */
38#define MAX_NUM_SEGMENTS 6
39
40struct bzpayload {
41 /* Input variables. */
42 int num_segments;
43 struct cbfs_payload_segment segs[MAX_NUM_SEGMENTS];
44 struct buffer parameters;
45 struct buffer kernel;
46 struct buffer trampoline;
47 struct buffer cmdline;
48 struct buffer initrd;
49 /* Output variables. */
Sol Boucher6310ccc2015-05-07 21:12:28 -070050 enum comp_algo algo;
Aaron Durbin4f3bb802014-03-26 22:57:55 -050051 comp_func_ptr compress;
52 struct buffer output;
53 size_t offset;
54 struct cbfs_payload_segment *out_seg;
55};
56
Sol Boucher6310ccc2015-05-07 21:12:28 -070057static int bzp_init(struct bzpayload *bzp, enum comp_algo algo)
Aaron Durbin4f3bb802014-03-26 22:57:55 -050058{
59 memset(bzp, 0, sizeof(*bzp));
60
61 /*
62 * Need at least the terminating entry segment.
63 */
64 bzp->num_segments = 1;
65
Aaron Durbin4f3bb802014-03-26 22:57:55 -050066 bzp->algo = algo;
67 bzp->compress = compression_function(algo);
68 if (bzp->compress == NULL) {
69 ERROR("Invalid compression algorithm specified.\n");
70 return -1;
71 }
72
73 return 0;
74}
75
76static int bzp_add_initrd(struct bzpayload *bzp, const char *fname)
77{
78 if (fname == NULL)
79 return 0;
80
81 if (buffer_from_file(&bzp->initrd, fname)) {
82 ERROR("could not open initrd.\n");
83 return -1;
84 }
85
86 bzp->num_segments++;
87
88 return 0;
89}
90
91static void bzp_add_segment(struct bzpayload *bzp, struct buffer *b, void *data,
92 size_t size)
93{
94 buffer_init(b, NULL, data, size);
95 bzp->num_segments++;
96}
97
Aaron Durbinb2757572014-05-08 11:54:25 -050098static int bzp_add_trampoline(struct bzpayload *bzp)
99{
Stefan Reinauer5dda4df2015-12-01 17:58:08 -0800100 bzp_add_segment(bzp, &bzp->trampoline, trampoline,
Stefan Reinauer16c7e0f2015-12-01 17:58:58 -0800101 trampoline_len);
Aaron Durbinb2757572014-05-08 11:54:25 -0500102 return 0;
103}
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500104
105static int bzp_add_cmdline(struct bzpayload *bzp, char *cmdline)
106{
107 if (cmdline == NULL)
108 return 0;
109
110 bzp_add_segment(bzp, &bzp->cmdline, cmdline, strlen(cmdline) + 1);
111
112 return 0;
113}
114
115static int bzp_add_params(struct bzpayload *bzp, struct linux_params *params)
116{
117 bzp_add_segment(bzp, &bzp->parameters, params, sizeof(*params));
118
119 return 0;
120}
121
122static int bzp_add_kernel(struct bzpayload *bzp, const struct buffer *in,
123 size_t setup_size)
124{
125 char *input = buffer_get(in);
126 size_t kern_sz = buffer_size(in) - setup_size;
127
128 bzp_add_segment(bzp, &bzp->kernel, &input[setup_size], kern_sz);
129
130 return 0;
131}
132
133static int bzp_init_output(struct bzpayload *bzp, const char *name)
134{
135 size_t sz = 0;
136
137 sz += buffer_size(&bzp->parameters);
138 sz += buffer_size(&bzp->kernel);
139 sz += buffer_size(&bzp->trampoline);
140 sz += buffer_size(&bzp->cmdline);
141 sz += buffer_size(&bzp->initrd);
142
143 bzp->offset = bzp->num_segments * sizeof(struct cbfs_payload_segment);
144 sz += bzp->offset;
145
146 if (buffer_create(&bzp->output, sz, name) != 0)
147 return -1;
148
149 bzp->out_seg = &bzp->segs[0];
150
151 return 0;
152}
153
154static void bzp_output_segment(struct bzpayload *bzp, struct buffer *b,
155 uint32_t type, uint64_t load_addr)
156{
157 struct buffer out;
158 struct cbfs_payload_segment *seg;
159 int len = 0;
160
161 /* Don't process empty buffers. */
162 if (b != NULL && buffer_size(b) == 0)
163 return;
164
165 seg = bzp->out_seg;
166 seg->type = type;
167 seg->load_addr = load_addr;
168 bzp->out_seg++;
169
170 /* No buffer associated with segment. */
171 if (b == NULL)
172 return;
173
174 /* Use a temp buffer for easier management. */
175 buffer_splice(&out, &bzp->output, bzp->offset, buffer_size(b));
176
177 seg->mem_len = buffer_size(b);
178 seg->offset = bzp->offset;
179 bzp->compress(buffer_get(b), buffer_size(b), buffer_get(&out), &len);
180 seg->compression = bzp->algo;
181 seg->len = len;
182
183 /* Update output offset. */
184 bzp->offset += len;
185}
186
Patrick Georgide36d332013-08-27 20:22:21 +0200187/* TODO:
188 * handle special arguments
189 * mem= argument - only affects loading decisions (kernel + initrd), not e820 -> build time
190 * vga= argument (FILO ignores this)
191 * add support for more parameters to trampoline:
192 * alt_mem_k, ext_mem_k (not strictly necessary since e820 takes precedence)
193 * framebuffer/console values
194 *
195 * larger work:
196 * is compress() safe to use in a size constrained buffer? ie. do(es) the
197 * compression algorithm(s) stop once the compression result reaches input
198 * size (ie. incompressible data)?
199 */
200int parse_bzImage_to_payload(const struct buffer *input,
201 struct buffer *output, const char *initrd_name,
Sol Boucher6310ccc2015-05-07 21:12:28 -0700202 char *cmdline, enum comp_algo algo)
Patrick Georgide36d332013-08-27 20:22:21 +0200203{
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500204 struct bzpayload bzp;
Patrick Georgide36d332013-08-27 20:22:21 +0200205 unsigned int initrd_base = 64*1024*1024;
Patrick Georgide36d332013-08-27 20:22:21 +0200206 struct linux_header *hdr = (struct linux_header *)input->data;
207 unsigned int setup_size = 4 * 512;
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500208
209 if (bzp_init(&bzp, algo) != 0)
210 return -1;
211
Aaron Durbinb2757572014-05-08 11:54:25 -0500212 if (bzp_add_trampoline(&bzp) != 0)
213 return -1;
214
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500215 if (bzp_add_initrd(&bzp, initrd_name) != 0)
216 return -1;
217
218 if (bzp_add_cmdline(&bzp, cmdline) != 0)
219 return -1;
220
Patrick Georgide36d332013-08-27 20:22:21 +0200221 if (hdr->setup_sects != 0) {
222 setup_size = (hdr->setup_sects + 1) * 512;
223 }
224
225 /* Setup parameter block. Imitate FILO. */
226 struct linux_params params;
227 params.mount_root_rdonly = hdr->root_flags;
228 params.orig_root_dev = hdr->root_dev;
229 /* Sensible video defaults. Might be overridden on runtime by coreboot tables. */
230 params.orig_video_mode = 3;
231 params.orig_video_cols = 80;
232 params.orig_video_lines = 25;
233 params.orig_video_isVGA = 1;
234 params.orig_video_points = 16;
235
236 params.loader_type = 0xff; /* Unregistered Linux loader */
237
238 if (cmdline != NULL) {
239 if (hdr->protocol_version < 0x202) {
240 params.cl_magic = CL_MAGIC_VALUE;
241 params.cl_offset = COMMAND_LINE_LOC - LINUX_PARAM_LOC;
242 } else {
243 params.cmd_line_ptr = COMMAND_LINE_LOC;
244 }
245 }
246
247 unsigned long kernel_base = 0x100000;
248 if ((hdr->protocol_version >= 0x200) && (!hdr->loadflags & 1)) {
249 kernel_base = 0x1000; /* zImage kernel */
250 }
251 /* kernel prefers an address, so listen */
252 if ((hdr->protocol_version >= 0x20a) && (!(hdr->pref_address >> 32))) {
253 kernel_base = hdr->pref_address;
254 }
255 if (hdr->protocol_version >= 0x205) {
256 params.relocatable_kernel = hdr->relocatable_kernel;
257 params.kernel_alignment = hdr->kernel_alignment;
258 if (hdr->relocatable_kernel != 0) {
259 /* 16 MB should be way outside coreboot's playground,
260 * so if possible (relocatable kernel) use that to
261 * avoid a trampoline copy. */
262 kernel_base = ALIGN(16*1024*1024, params.kernel_alignment);
263 }
264 }
265
266 /* We have a trampoline and use that, but it can simply use
267 * this information for its jump to real Linux. */
268 params.kernel_start = kernel_base;
269
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500270 if (bzp_add_kernel(&bzp, input, setup_size) != 0)
271 return -1;
Patrick Georgide36d332013-08-27 20:22:21 +0200272
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500273 if (buffer_size(&bzp.initrd) != 0) {
Patrick Georgide36d332013-08-27 20:22:21 +0200274 /* TODO: this is a bit of a hack. Linux recommends to store
275 * initrd near to end-of-mem, but that's hard to do on build
276 * time. It definitely fails to read the image if it's too
277 * close to the kernel, so give it some room.
278 */
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500279 initrd_base = kernel_base + buffer_size(&bzp.kernel);
280 initrd_base = ALIGN(initrd_base, 16*1024*1024);
Patrick Georgide36d332013-08-27 20:22:21 +0200281
282 params.initrd_start = initrd_base;
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500283 params.initrd_size = buffer_size(&bzp.initrd);
Patrick Georgide36d332013-08-27 20:22:21 +0200284 }
285
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500286 if (bzp_add_params(&bzp, &params) != 0)
Patrick Georgide36d332013-08-27 20:22:21 +0200287 return -1;
Patrick Georgide36d332013-08-27 20:22:21 +0200288
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500289 if (bzp_init_output(&bzp, input->name) != 0)
290 return -1;
Patrick Georgide36d332013-08-27 20:22:21 +0200291
292 /* parameter block */
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500293 bzp_output_segment(&bzp, &bzp.parameters,
294 PAYLOAD_SEGMENT_DATA, LINUX_PARAM_LOC);
Patrick Georgide36d332013-08-27 20:22:21 +0200295
296 /* code block */
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500297 bzp_output_segment(&bzp, &bzp.kernel,
298 PAYLOAD_SEGMENT_CODE, kernel_base);
Patrick Georgide36d332013-08-27 20:22:21 +0200299
300 /* trampoline */
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500301 bzp_output_segment(&bzp, &bzp.trampoline,
Curt Brune3c12cb02014-08-29 10:43:36 -0700302 PAYLOAD_SEGMENT_CODE, TRAMPOLINE_ENTRY_LOC);
Patrick Georgide36d332013-08-27 20:22:21 +0200303
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500304 /* cmdline */
305 bzp_output_segment(&bzp, &bzp.cmdline,
306 PAYLOAD_SEGMENT_DATA, COMMAND_LINE_LOC);
Patrick Georgide36d332013-08-27 20:22:21 +0200307
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500308 /* initrd */
309 bzp_output_segment(&bzp, &bzp.initrd,
310 PAYLOAD_SEGMENT_DATA, initrd_base);
Patrick Georgide36d332013-08-27 20:22:21 +0200311
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500312 /* Terminating entry segment. */
Curt Brune3c12cb02014-08-29 10:43:36 -0700313 bzp_output_segment(&bzp, NULL, PAYLOAD_SEGMENT_ENTRY, TRAMPOLINE_ENTRY_LOC);
Patrick Georgide36d332013-08-27 20:22:21 +0200314
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500315 /* Set size of buffer taking into account potential compression. */
316 buffer_set_size(&bzp.output, bzp.offset);
317 /* Make passed-in output buffer be valid. */
318 buffer_clone(output, &bzp.output);
Patrick Georgide36d332013-08-27 20:22:21 +0200319
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500320 /* Serialize the segments with the correct encoding. */
321 xdr_segs(output, bzp.segs, bzp.num_segments);
Patrick Georgide36d332013-08-27 20:22:21 +0200322 return 0;
323}