blob: 12fa23858fec2bcbb56e67a6357fb80ec8704881 [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
70 buffer_init(&bzp->trampoline, NULL, trampoline_start, trampoline_size);
71
72 bzp->algo = algo;
73 bzp->compress = compression_function(algo);
74 if (bzp->compress == NULL) {
75 ERROR("Invalid compression algorithm specified.\n");
76 return -1;
77 }
78
79 return 0;
80}
81
82static int bzp_add_initrd(struct bzpayload *bzp, const char *fname)
83{
84 if (fname == NULL)
85 return 0;
86
87 if (buffer_from_file(&bzp->initrd, fname)) {
88 ERROR("could not open initrd.\n");
89 return -1;
90 }
91
92 bzp->num_segments++;
93
94 return 0;
95}
96
97static void bzp_add_segment(struct bzpayload *bzp, struct buffer *b, void *data,
98 size_t size)
99{
100 buffer_init(b, NULL, data, size);
101 bzp->num_segments++;
102}
103
104
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,
202 char *cmdline, comp_algo algo)
203{
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
212 if (bzp_add_initrd(&bzp, initrd_name) != 0)
213 return -1;
214
215 if (bzp_add_cmdline(&bzp, cmdline) != 0)
216 return -1;
217
Patrick Georgide36d332013-08-27 20:22:21 +0200218 if (hdr->setup_sects != 0) {
219 setup_size = (hdr->setup_sects + 1) * 512;
220 }
221
222 /* Setup parameter block. Imitate FILO. */
223 struct linux_params params;
224 params.mount_root_rdonly = hdr->root_flags;
225 params.orig_root_dev = hdr->root_dev;
226 /* Sensible video defaults. Might be overridden on runtime by coreboot tables. */
227 params.orig_video_mode = 3;
228 params.orig_video_cols = 80;
229 params.orig_video_lines = 25;
230 params.orig_video_isVGA = 1;
231 params.orig_video_points = 16;
232
233 params.loader_type = 0xff; /* Unregistered Linux loader */
234
235 if (cmdline != NULL) {
236 if (hdr->protocol_version < 0x202) {
237 params.cl_magic = CL_MAGIC_VALUE;
238 params.cl_offset = COMMAND_LINE_LOC - LINUX_PARAM_LOC;
239 } else {
240 params.cmd_line_ptr = COMMAND_LINE_LOC;
241 }
242 }
243
244 unsigned long kernel_base = 0x100000;
245 if ((hdr->protocol_version >= 0x200) && (!hdr->loadflags & 1)) {
246 kernel_base = 0x1000; /* zImage kernel */
247 }
248 /* kernel prefers an address, so listen */
249 if ((hdr->protocol_version >= 0x20a) && (!(hdr->pref_address >> 32))) {
250 kernel_base = hdr->pref_address;
251 }
252 if (hdr->protocol_version >= 0x205) {
253 params.relocatable_kernel = hdr->relocatable_kernel;
254 params.kernel_alignment = hdr->kernel_alignment;
255 if (hdr->relocatable_kernel != 0) {
256 /* 16 MB should be way outside coreboot's playground,
257 * so if possible (relocatable kernel) use that to
258 * avoid a trampoline copy. */
259 kernel_base = ALIGN(16*1024*1024, params.kernel_alignment);
260 }
261 }
262
263 /* We have a trampoline and use that, but it can simply use
264 * this information for its jump to real Linux. */
265 params.kernel_start = kernel_base;
266
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500267 if (bzp_add_kernel(&bzp, input, setup_size) != 0)
268 return -1;
Patrick Georgide36d332013-08-27 20:22:21 +0200269
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500270 if (buffer_size(&bzp.initrd) != 0) {
Patrick Georgide36d332013-08-27 20:22:21 +0200271 /* TODO: this is a bit of a hack. Linux recommends to store
272 * initrd near to end-of-mem, but that's hard to do on build
273 * time. It definitely fails to read the image if it's too
274 * close to the kernel, so give it some room.
275 */
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500276 initrd_base = kernel_base + buffer_size(&bzp.kernel);
277 initrd_base = ALIGN(initrd_base, 16*1024*1024);
Patrick Georgide36d332013-08-27 20:22:21 +0200278
279 params.initrd_start = initrd_base;
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500280 params.initrd_size = buffer_size(&bzp.initrd);
Patrick Georgide36d332013-08-27 20:22:21 +0200281 }
282
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500283 if (bzp_add_params(&bzp, &params) != 0)
Patrick Georgide36d332013-08-27 20:22:21 +0200284 return -1;
Patrick Georgide36d332013-08-27 20:22:21 +0200285
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500286 if (bzp_init_output(&bzp, input->name) != 0)
287 return -1;
Patrick Georgide36d332013-08-27 20:22:21 +0200288
289 /* parameter block */
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500290 bzp_output_segment(&bzp, &bzp.parameters,
291 PAYLOAD_SEGMENT_DATA, LINUX_PARAM_LOC);
Patrick Georgide36d332013-08-27 20:22:21 +0200292
293 /* code block */
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500294 bzp_output_segment(&bzp, &bzp.kernel,
295 PAYLOAD_SEGMENT_CODE, kernel_base);
Patrick Georgide36d332013-08-27 20:22:21 +0200296
297 /* trampoline */
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500298 uint64_t entrypoint = 0x40000; /*TODO: any better place? */
299 bzp_output_segment(&bzp, &bzp.trampoline,
300 PAYLOAD_SEGMENT_CODE, entrypoint);
Patrick Georgide36d332013-08-27 20:22:21 +0200301
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500302 /* cmdline */
303 bzp_output_segment(&bzp, &bzp.cmdline,
304 PAYLOAD_SEGMENT_DATA, COMMAND_LINE_LOC);
Patrick Georgide36d332013-08-27 20:22:21 +0200305
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500306 /* initrd */
307 bzp_output_segment(&bzp, &bzp.initrd,
308 PAYLOAD_SEGMENT_DATA, initrd_base);
Patrick Georgide36d332013-08-27 20:22:21 +0200309
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500310 /* Terminating entry segment. */
311 bzp_output_segment(&bzp, NULL, PAYLOAD_SEGMENT_ENTRY, entrypoint);
Patrick Georgide36d332013-08-27 20:22:21 +0200312
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500313 /* Set size of buffer taking into account potential compression. */
314 buffer_set_size(&bzp.output, bzp.offset);
315 /* Make passed-in output buffer be valid. */
316 buffer_clone(output, &bzp.output);
Patrick Georgide36d332013-08-27 20:22:21 +0200317
Aaron Durbin4f3bb802014-03-26 22:57:55 -0500318 /* Serialize the segments with the correct encoding. */
319 xdr_segs(output, bzp.segs, bzp.num_segments);
Patrick Georgide36d332013-08-27 20:22:21 +0200320 return 0;
321}
322
323