blob: 33a5e1afb17b64a026ffef3d9306530ce2767b52 [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.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
18 */
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23
24#include "common.h"
25#include "cbfs.h"
26#include "linux.h"
27
Aaron Durbin4f3bb802014-03-26 22:57:55 -050028/* trampoline */
29extern void *trampoline_start;
30extern long trampoline_size;
31
32/*
33 * Current max number of segments include:
34 *
35 * 1. parameters
36 * 2. kernel
37 * 3. trampoline
38 * 4. optional cmdline
39 * 5. optional initrd
40 * 6. terminating entry segment
41 */
42#define MAX_NUM_SEGMENTS 6
43
44struct bzpayload {
45 /* Input variables. */
46 int num_segments;
47 struct cbfs_payload_segment segs[MAX_NUM_SEGMENTS];
48 struct buffer parameters;
49 struct buffer kernel;
50 struct buffer trampoline;
51 struct buffer cmdline;
52 struct buffer initrd;
53 /* Output variables. */
54 comp_algo algo;
55 comp_func_ptr compress;
56 struct buffer output;
57 size_t offset;
58 struct cbfs_payload_segment *out_seg;
59};
60
61static int bzp_init(struct bzpayload *bzp, comp_algo algo)
62{
63 memset(bzp, 0, sizeof(*bzp));
64
65 /*
66 * Need at least the terminating entry segment.
67 */
68 bzp->num_segments = 1;
69
Aaron Durbin4f3bb802014-03-26 22:57:55 -050070 bzp->algo = algo;
71 bzp->compress = compression_function(algo);
72 if (bzp->compress == NULL) {
73 ERROR("Invalid compression algorithm specified.\n");
74 return -1;
75 }
76
77 return 0;
78}
79
80static int bzp_add_initrd(struct bzpayload *bzp, const char *fname)
81{
82 if (fname == NULL)
83 return 0;
84
85 if (buffer_from_file(&bzp->initrd, fname)) {
86 ERROR("could not open initrd.\n");
87 return -1;
88 }
89
90 bzp->num_segments++;
91
92 return 0;
93}
94
95static void bzp_add_segment(struct bzpayload *bzp, struct buffer *b, void *data,
96 size_t size)
97{
98 buffer_init(b, NULL, data, size);
99 bzp->num_segments++;
100}
101
Aaron Durbinb2757572014-05-08 11:54:25 -0500102static int bzp_add_trampoline(struct bzpayload *bzp)
103{
104 bzp_add_segment(bzp, &bzp->trampoline, trampoline_start,
105 trampoline_size);
106 return 0;
107}
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500108
109static int bzp_add_cmdline(struct bzpayload *bzp, char *cmdline)
110{
111 if (cmdline == NULL)
112 return 0;
113
114 bzp_add_segment(bzp, &bzp->cmdline, cmdline, strlen(cmdline) + 1);
115
116 return 0;
117}
118
119static int bzp_add_params(struct bzpayload *bzp, struct linux_params *params)
120{
121 bzp_add_segment(bzp, &bzp->parameters, params, sizeof(*params));
122
123 return 0;
124}
125
126static int bzp_add_kernel(struct bzpayload *bzp, const struct buffer *in,
127 size_t setup_size)
128{
129 char *input = buffer_get(in);
130 size_t kern_sz = buffer_size(in) - setup_size;
131
132 bzp_add_segment(bzp, &bzp->kernel, &input[setup_size], kern_sz);
133
134 return 0;
135}
136
137static int bzp_init_output(struct bzpayload *bzp, const char *name)
138{
139 size_t sz = 0;
140
141 sz += buffer_size(&bzp->parameters);
142 sz += buffer_size(&bzp->kernel);
143 sz += buffer_size(&bzp->trampoline);
144 sz += buffer_size(&bzp->cmdline);
145 sz += buffer_size(&bzp->initrd);
146
147 bzp->offset = bzp->num_segments * sizeof(struct cbfs_payload_segment);
148 sz += bzp->offset;
149
150 if (buffer_create(&bzp->output, sz, name) != 0)
151 return -1;
152
153 bzp->out_seg = &bzp->segs[0];
154
155 return 0;
156}
157
158static void bzp_output_segment(struct bzpayload *bzp, struct buffer *b,
159 uint32_t type, uint64_t load_addr)
160{
161 struct buffer out;
162 struct cbfs_payload_segment *seg;
163 int len = 0;
164
165 /* Don't process empty buffers. */
166 if (b != NULL && buffer_size(b) == 0)
167 return;
168
169 seg = bzp->out_seg;
170 seg->type = type;
171 seg->load_addr = load_addr;
172 bzp->out_seg++;
173
174 /* No buffer associated with segment. */
175 if (b == NULL)
176 return;
177
178 /* Use a temp buffer for easier management. */
179 buffer_splice(&out, &bzp->output, bzp->offset, buffer_size(b));
180
181 seg->mem_len = buffer_size(b);
182 seg->offset = bzp->offset;
183 bzp->compress(buffer_get(b), buffer_size(b), buffer_get(&out), &len);
184 seg->compression = bzp->algo;
185 seg->len = len;
186
187 /* Update output offset. */
188 bzp->offset += len;
189}
190
Patrick Georgide36d332013-08-27 20:22:21 +0200191/* TODO:
192 * handle special arguments
193 * mem= argument - only affects loading decisions (kernel + initrd), not e820 -> build time
194 * vga= argument (FILO ignores this)
195 * add support for more parameters to trampoline:
196 * alt_mem_k, ext_mem_k (not strictly necessary since e820 takes precedence)
197 * framebuffer/console values
198 *
199 * larger work:
200 * is compress() safe to use in a size constrained buffer? ie. do(es) the
201 * compression algorithm(s) stop once the compression result reaches input
202 * size (ie. incompressible data)?
203 */
204int parse_bzImage_to_payload(const struct buffer *input,
205 struct buffer *output, const char *initrd_name,
206 char *cmdline, comp_algo algo)
207{
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500208 struct bzpayload bzp;
Patrick Georgide36d332013-08-27 20:22:21 +0200209 unsigned int initrd_base = 64*1024*1024;
Patrick Georgide36d332013-08-27 20:22:21 +0200210 struct linux_header *hdr = (struct linux_header *)input->data;
211 unsigned int setup_size = 4 * 512;
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500212
213 if (bzp_init(&bzp, algo) != 0)
214 return -1;
215
Aaron Durbinb2757572014-05-08 11:54:25 -0500216 if (bzp_add_trampoline(&bzp) != 0)
217 return -1;
218
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500219 if (bzp_add_initrd(&bzp, initrd_name) != 0)
220 return -1;
221
222 if (bzp_add_cmdline(&bzp, cmdline) != 0)
223 return -1;
224
Patrick Georgide36d332013-08-27 20:22:21 +0200225 if (hdr->setup_sects != 0) {
226 setup_size = (hdr->setup_sects + 1) * 512;
227 }
228
229 /* Setup parameter block. Imitate FILO. */
230 struct linux_params params;
231 params.mount_root_rdonly = hdr->root_flags;
232 params.orig_root_dev = hdr->root_dev;
233 /* Sensible video defaults. Might be overridden on runtime by coreboot tables. */
234 params.orig_video_mode = 3;
235 params.orig_video_cols = 80;
236 params.orig_video_lines = 25;
237 params.orig_video_isVGA = 1;
238 params.orig_video_points = 16;
239
240 params.loader_type = 0xff; /* Unregistered Linux loader */
241
242 if (cmdline != NULL) {
243 if (hdr->protocol_version < 0x202) {
244 params.cl_magic = CL_MAGIC_VALUE;
245 params.cl_offset = COMMAND_LINE_LOC - LINUX_PARAM_LOC;
246 } else {
247 params.cmd_line_ptr = COMMAND_LINE_LOC;
248 }
249 }
250
251 unsigned long kernel_base = 0x100000;
252 if ((hdr->protocol_version >= 0x200) && (!hdr->loadflags & 1)) {
253 kernel_base = 0x1000; /* zImage kernel */
254 }
255 /* kernel prefers an address, so listen */
256 if ((hdr->protocol_version >= 0x20a) && (!(hdr->pref_address >> 32))) {
257 kernel_base = hdr->pref_address;
258 }
259 if (hdr->protocol_version >= 0x205) {
260 params.relocatable_kernel = hdr->relocatable_kernel;
261 params.kernel_alignment = hdr->kernel_alignment;
262 if (hdr->relocatable_kernel != 0) {
263 /* 16 MB should be way outside coreboot's playground,
264 * so if possible (relocatable kernel) use that to
265 * avoid a trampoline copy. */
266 kernel_base = ALIGN(16*1024*1024, params.kernel_alignment);
267 }
268 }
269
270 /* We have a trampoline and use that, but it can simply use
271 * this information for its jump to real Linux. */
272 params.kernel_start = kernel_base;
273
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500274 if (bzp_add_kernel(&bzp, input, setup_size) != 0)
275 return -1;
Patrick Georgide36d332013-08-27 20:22:21 +0200276
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500277 if (buffer_size(&bzp.initrd) != 0) {
Patrick Georgide36d332013-08-27 20:22:21 +0200278 /* TODO: this is a bit of a hack. Linux recommends to store
279 * initrd near to end-of-mem, but that's hard to do on build
280 * time. It definitely fails to read the image if it's too
281 * close to the kernel, so give it some room.
282 */
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500283 initrd_base = kernel_base + buffer_size(&bzp.kernel);
284 initrd_base = ALIGN(initrd_base, 16*1024*1024);
Patrick Georgide36d332013-08-27 20:22:21 +0200285
286 params.initrd_start = initrd_base;
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500287 params.initrd_size = buffer_size(&bzp.initrd);
Patrick Georgide36d332013-08-27 20:22:21 +0200288 }
289
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500290 if (bzp_add_params(&bzp, &params) != 0)
Patrick Georgide36d332013-08-27 20:22:21 +0200291 return -1;
Patrick Georgide36d332013-08-27 20:22:21 +0200292
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500293 if (bzp_init_output(&bzp, input->name) != 0)
294 return -1;
Patrick Georgide36d332013-08-27 20:22:21 +0200295
296 /* parameter block */
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500297 bzp_output_segment(&bzp, &bzp.parameters,
298 PAYLOAD_SEGMENT_DATA, LINUX_PARAM_LOC);
Patrick Georgide36d332013-08-27 20:22:21 +0200299
300 /* code block */
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500301 bzp_output_segment(&bzp, &bzp.kernel,
302 PAYLOAD_SEGMENT_CODE, kernel_base);
Patrick Georgide36d332013-08-27 20:22:21 +0200303
304 /* trampoline */
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500305 uint64_t entrypoint = 0x40000; /*TODO: any better place? */
306 bzp_output_segment(&bzp, &bzp.trampoline,
307 PAYLOAD_SEGMENT_CODE, entrypoint);
Patrick Georgide36d332013-08-27 20:22:21 +0200308
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500309 /* cmdline */
310 bzp_output_segment(&bzp, &bzp.cmdline,
311 PAYLOAD_SEGMENT_DATA, COMMAND_LINE_LOC);
Patrick Georgide36d332013-08-27 20:22:21 +0200312
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500313 /* initrd */
314 bzp_output_segment(&bzp, &bzp.initrd,
315 PAYLOAD_SEGMENT_DATA, initrd_base);
Patrick Georgide36d332013-08-27 20:22:21 +0200316
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500317 /* Terminating entry segment. */
318 bzp_output_segment(&bzp, NULL, PAYLOAD_SEGMENT_ENTRY, entrypoint);
Patrick Georgide36d332013-08-27 20:22:21 +0200319
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500320 /* Set size of buffer taking into account potential compression. */
321 buffer_set_size(&bzp.output, bzp.offset);
322 /* Make passed-in output buffer be valid. */
323 buffer_clone(output, &bzp.output);
Patrick Georgide36d332013-08-27 20:22:21 +0200324
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500325 /* Serialize the segments with the correct encoding. */
326 xdr_segs(output, bzp.segs, bzp.num_segments);
Patrick Georgide36d332013-08-27 20:22:21 +0200327 return 0;
328}
329
330