Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 1 | /* |
| 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 Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 14 | */ |
| 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 Durbin | 4f3bb80 | 2014-03-26 22:57:55 -0500 | [diff] [blame] | 24 | /* trampoline */ |
Stefan Reinauer | 5dda4df | 2015-12-01 17:58:08 -0800 | [diff] [blame] | 25 | extern unsigned char trampoline[]; |
| 26 | extern unsigned int trampoline_len; |
Aaron Durbin | 4f3bb80 | 2014-03-26 22:57:55 -0500 | [diff] [blame] | 27 | |
| 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 | |
| 40 | struct 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 Boucher | 6310ccc | 2015-05-07 21:12:28 -0700 | [diff] [blame] | 50 | enum comp_algo algo; |
Aaron Durbin | 4f3bb80 | 2014-03-26 22:57:55 -0500 | [diff] [blame] | 51 | comp_func_ptr compress; |
| 52 | struct buffer output; |
| 53 | size_t offset; |
| 54 | struct cbfs_payload_segment *out_seg; |
| 55 | }; |
| 56 | |
Sol Boucher | 6310ccc | 2015-05-07 21:12:28 -0700 | [diff] [blame] | 57 | static int bzp_init(struct bzpayload *bzp, enum comp_algo algo) |
Aaron Durbin | 4f3bb80 | 2014-03-26 22:57:55 -0500 | [diff] [blame] | 58 | { |
| 59 | memset(bzp, 0, sizeof(*bzp)); |
| 60 | |
| 61 | /* |
| 62 | * Need at least the terminating entry segment. |
| 63 | */ |
| 64 | bzp->num_segments = 1; |
| 65 | |
Aaron Durbin | 4f3bb80 | 2014-03-26 22:57:55 -0500 | [diff] [blame] | 66 | 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 | |
| 76 | static 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 | |
| 91 | static 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 Durbin | b275757 | 2014-05-08 11:54:25 -0500 | [diff] [blame] | 98 | static int bzp_add_trampoline(struct bzpayload *bzp) |
| 99 | { |
Stefan Reinauer | 5dda4df | 2015-12-01 17:58:08 -0800 | [diff] [blame] | 100 | bzp_add_segment(bzp, &bzp->trampoline, trampoline, |
Stefan Reinauer | 16c7e0f | 2015-12-01 17:58:58 -0800 | [diff] [blame] | 101 | trampoline_len); |
Aaron Durbin | b275757 | 2014-05-08 11:54:25 -0500 | [diff] [blame] | 102 | return 0; |
| 103 | } |
Aaron Durbin | 4f3bb80 | 2014-03-26 22:57:55 -0500 | [diff] [blame] | 104 | |
| 105 | static 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 | |
| 115 | static 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 | |
| 122 | static 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 | |
| 133 | static 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 | |
| 154 | static 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 Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 187 | /* 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 | */ |
| 200 | int parse_bzImage_to_payload(const struct buffer *input, |
| 201 | struct buffer *output, const char *initrd_name, |
Sol Boucher | 6310ccc | 2015-05-07 21:12:28 -0700 | [diff] [blame] | 202 | char *cmdline, enum comp_algo algo) |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 203 | { |
Aaron Durbin | 4f3bb80 | 2014-03-26 22:57:55 -0500 | [diff] [blame] | 204 | struct bzpayload bzp; |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 205 | unsigned int initrd_base = 64*1024*1024; |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 206 | struct linux_header *hdr = (struct linux_header *)input->data; |
| 207 | unsigned int setup_size = 4 * 512; |
Aaron Durbin | 4f3bb80 | 2014-03-26 22:57:55 -0500 | [diff] [blame] | 208 | |
| 209 | if (bzp_init(&bzp, algo) != 0) |
| 210 | return -1; |
| 211 | |
Aaron Durbin | b275757 | 2014-05-08 11:54:25 -0500 | [diff] [blame] | 212 | if (bzp_add_trampoline(&bzp) != 0) |
| 213 | return -1; |
| 214 | |
Aaron Durbin | 4f3bb80 | 2014-03-26 22:57:55 -0500 | [diff] [blame] | 215 | 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 Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 221 | if (hdr->setup_sects != 0) { |
| 222 | setup_size = (hdr->setup_sects + 1) * 512; |
Ronald G. Minnich | fa74e47 | 2016-09-28 10:43:12 -0700 | [diff] [blame] | 223 | } else { |
| 224 | WARN("hdr->setup_sects is 0, which could cause boot problems.\n"); |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | /* Setup parameter block. Imitate FILO. */ |
| 228 | struct linux_params params; |
Werner Zeh | bbf5089 | 2016-03-02 17:45:53 +0100 | [diff] [blame] | 229 | |
| 230 | memset(¶ms, 0, sizeof(struct linux_params)); |
Ronald G. Minnich | fa74e47 | 2016-09-28 10:43:12 -0700 | [diff] [blame] | 231 | |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 232 | params.mount_root_rdonly = hdr->root_flags; |
| 233 | params.orig_root_dev = hdr->root_dev; |
Ronald G. Minnich | fa74e47 | 2016-09-28 10:43:12 -0700 | [diff] [blame] | 234 | params.init_size = hdr->init_size; |
| 235 | |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 236 | /* Sensible video defaults. Might be overridden on runtime by coreboot tables. */ |
| 237 | params.orig_video_mode = 3; |
| 238 | params.orig_video_cols = 80; |
| 239 | params.orig_video_lines = 25; |
| 240 | params.orig_video_isVGA = 1; |
| 241 | params.orig_video_points = 16; |
| 242 | |
| 243 | params.loader_type = 0xff; /* Unregistered Linux loader */ |
| 244 | |
| 245 | if (cmdline != NULL) { |
| 246 | if (hdr->protocol_version < 0x202) { |
| 247 | params.cl_magic = CL_MAGIC_VALUE; |
| 248 | params.cl_offset = COMMAND_LINE_LOC - LINUX_PARAM_LOC; |
| 249 | } else { |
| 250 | params.cmd_line_ptr = COMMAND_LINE_LOC; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | unsigned long kernel_base = 0x100000; |
Manoj Gupta | cf84619 | 2016-11-14 11:19:30 -0800 | [diff] [blame] | 255 | if ((hdr->protocol_version < 0x200) || !(hdr->loadflags & 1)) { |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 256 | kernel_base = 0x1000; /* zImage kernel */ |
| 257 | } |
| 258 | /* kernel prefers an address, so listen */ |
| 259 | if ((hdr->protocol_version >= 0x20a) && (!(hdr->pref_address >> 32))) { |
| 260 | kernel_base = hdr->pref_address; |
| 261 | } |
| 262 | if (hdr->protocol_version >= 0x205) { |
| 263 | params.relocatable_kernel = hdr->relocatable_kernel; |
| 264 | params.kernel_alignment = hdr->kernel_alignment; |
| 265 | if (hdr->relocatable_kernel != 0) { |
| 266 | /* 16 MB should be way outside coreboot's playground, |
| 267 | * so if possible (relocatable kernel) use that to |
| 268 | * avoid a trampoline copy. */ |
| 269 | kernel_base = ALIGN(16*1024*1024, params.kernel_alignment); |
Ronald G. Minnich | fa74e47 | 2016-09-28 10:43:12 -0700 | [diff] [blame] | 270 | if (hdr->init_size == 0) { |
| 271 | ERROR("init_size 0 for relocatable kernel\n"); |
| 272 | return -1; |
| 273 | } |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
| 277 | /* We have a trampoline and use that, but it can simply use |
| 278 | * this information for its jump to real Linux. */ |
| 279 | params.kernel_start = kernel_base; |
| 280 | |
Aaron Durbin | 4f3bb80 | 2014-03-26 22:57:55 -0500 | [diff] [blame] | 281 | if (bzp_add_kernel(&bzp, input, setup_size) != 0) |
| 282 | return -1; |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 283 | |
Aaron Durbin | 4f3bb80 | 2014-03-26 22:57:55 -0500 | [diff] [blame] | 284 | if (buffer_size(&bzp.initrd) != 0) { |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 285 | /* TODO: this is a bit of a hack. Linux recommends to store |
| 286 | * initrd near to end-of-mem, but that's hard to do on build |
| 287 | * time. It definitely fails to read the image if it's too |
| 288 | * close to the kernel, so give it some room. |
| 289 | */ |
Aaron Durbin | 4f3bb80 | 2014-03-26 22:57:55 -0500 | [diff] [blame] | 290 | initrd_base = kernel_base + buffer_size(&bzp.kernel); |
Timothy Pearson | c1dc9d7 | 2016-07-31 00:23:11 -0500 | [diff] [blame] | 291 | initrd_base = ALIGN(initrd_base, 64*1024*1024); |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 292 | |
| 293 | params.initrd_start = initrd_base; |
Aaron Durbin | 4f3bb80 | 2014-03-26 22:57:55 -0500 | [diff] [blame] | 294 | params.initrd_size = buffer_size(&bzp.initrd); |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 295 | } |
| 296 | |
Aaron Durbin | 4f3bb80 | 2014-03-26 22:57:55 -0500 | [diff] [blame] | 297 | if (bzp_add_params(&bzp, ¶ms) != 0) |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 298 | return -1; |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 299 | |
Aaron Durbin | 4f3bb80 | 2014-03-26 22:57:55 -0500 | [diff] [blame] | 300 | if (bzp_init_output(&bzp, input->name) != 0) |
| 301 | return -1; |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 302 | |
| 303 | /* parameter block */ |
Aaron Durbin | 4f3bb80 | 2014-03-26 22:57:55 -0500 | [diff] [blame] | 304 | bzp_output_segment(&bzp, &bzp.parameters, |
| 305 | PAYLOAD_SEGMENT_DATA, LINUX_PARAM_LOC); |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 306 | |
| 307 | /* code block */ |
Aaron Durbin | 4f3bb80 | 2014-03-26 22:57:55 -0500 | [diff] [blame] | 308 | bzp_output_segment(&bzp, &bzp.kernel, |
| 309 | PAYLOAD_SEGMENT_CODE, kernel_base); |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 310 | |
| 311 | /* trampoline */ |
Aaron Durbin | 4f3bb80 | 2014-03-26 22:57:55 -0500 | [diff] [blame] | 312 | bzp_output_segment(&bzp, &bzp.trampoline, |
Curt Brune | 3c12cb0 | 2014-08-29 10:43:36 -0700 | [diff] [blame] | 313 | PAYLOAD_SEGMENT_CODE, TRAMPOLINE_ENTRY_LOC); |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 314 | |
Aaron Durbin | 4f3bb80 | 2014-03-26 22:57:55 -0500 | [diff] [blame] | 315 | /* cmdline */ |
| 316 | bzp_output_segment(&bzp, &bzp.cmdline, |
| 317 | PAYLOAD_SEGMENT_DATA, COMMAND_LINE_LOC); |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 318 | |
Aaron Durbin | 4f3bb80 | 2014-03-26 22:57:55 -0500 | [diff] [blame] | 319 | /* initrd */ |
| 320 | bzp_output_segment(&bzp, &bzp.initrd, |
| 321 | PAYLOAD_SEGMENT_DATA, initrd_base); |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 322 | |
Aaron Durbin | 4f3bb80 | 2014-03-26 22:57:55 -0500 | [diff] [blame] | 323 | /* Terminating entry segment. */ |
Curt Brune | 3c12cb0 | 2014-08-29 10:43:36 -0700 | [diff] [blame] | 324 | bzp_output_segment(&bzp, NULL, PAYLOAD_SEGMENT_ENTRY, TRAMPOLINE_ENTRY_LOC); |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 325 | |
Aaron Durbin | 4f3bb80 | 2014-03-26 22:57:55 -0500 | [diff] [blame] | 326 | /* Set size of buffer taking into account potential compression. */ |
| 327 | buffer_set_size(&bzp.output, bzp.offset); |
| 328 | /* Make passed-in output buffer be valid. */ |
| 329 | buffer_clone(output, &bzp.output); |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 330 | |
Aaron Durbin | 4f3bb80 | 2014-03-26 22:57:55 -0500 | [diff] [blame] | 331 | /* Serialize the segments with the correct encoding. */ |
| 332 | xdr_segs(output, bzp.segs, bzp.num_segments); |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 333 | return 0; |
| 334 | } |