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, |
| 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; |
| 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 Durbin | 4f3bb80 | 2014-03-26 22:57:55 -0500 | [diff] [blame] | 270 | if (bzp_add_kernel(&bzp, input, setup_size) != 0) |
| 271 | return -1; |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 272 | |
Aaron Durbin | 4f3bb80 | 2014-03-26 22:57:55 -0500 | [diff] [blame] | 273 | if (buffer_size(&bzp.initrd) != 0) { |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 274 | /* 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 Durbin | 4f3bb80 | 2014-03-26 22:57:55 -0500 | [diff] [blame] | 279 | initrd_base = kernel_base + buffer_size(&bzp.kernel); |
| 280 | initrd_base = ALIGN(initrd_base, 16*1024*1024); |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 281 | |
| 282 | params.initrd_start = initrd_base; |
Aaron Durbin | 4f3bb80 | 2014-03-26 22:57:55 -0500 | [diff] [blame] | 283 | params.initrd_size = buffer_size(&bzp.initrd); |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 284 | } |
| 285 | |
Aaron Durbin | 4f3bb80 | 2014-03-26 22:57:55 -0500 | [diff] [blame] | 286 | if (bzp_add_params(&bzp, ¶ms) != 0) |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 287 | return -1; |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 288 | |
Aaron Durbin | 4f3bb80 | 2014-03-26 22:57:55 -0500 | [diff] [blame] | 289 | if (bzp_init_output(&bzp, input->name) != 0) |
| 290 | return -1; |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 291 | |
| 292 | /* parameter block */ |
Aaron Durbin | 4f3bb80 | 2014-03-26 22:57:55 -0500 | [diff] [blame] | 293 | bzp_output_segment(&bzp, &bzp.parameters, |
| 294 | PAYLOAD_SEGMENT_DATA, LINUX_PARAM_LOC); |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 295 | |
| 296 | /* code block */ |
Aaron Durbin | 4f3bb80 | 2014-03-26 22:57:55 -0500 | [diff] [blame] | 297 | bzp_output_segment(&bzp, &bzp.kernel, |
| 298 | PAYLOAD_SEGMENT_CODE, kernel_base); |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 299 | |
| 300 | /* trampoline */ |
Aaron Durbin | 4f3bb80 | 2014-03-26 22:57:55 -0500 | [diff] [blame] | 301 | bzp_output_segment(&bzp, &bzp.trampoline, |
Curt Brune | 3c12cb0 | 2014-08-29 10:43:36 -0700 | [diff] [blame] | 302 | PAYLOAD_SEGMENT_CODE, TRAMPOLINE_ENTRY_LOC); |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 303 | |
Aaron Durbin | 4f3bb80 | 2014-03-26 22:57:55 -0500 | [diff] [blame] | 304 | /* cmdline */ |
| 305 | bzp_output_segment(&bzp, &bzp.cmdline, |
| 306 | PAYLOAD_SEGMENT_DATA, COMMAND_LINE_LOC); |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 307 | |
Aaron Durbin | 4f3bb80 | 2014-03-26 22:57:55 -0500 | [diff] [blame] | 308 | /* initrd */ |
| 309 | bzp_output_segment(&bzp, &bzp.initrd, |
| 310 | PAYLOAD_SEGMENT_DATA, initrd_base); |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 311 | |
Aaron Durbin | 4f3bb80 | 2014-03-26 22:57:55 -0500 | [diff] [blame] | 312 | /* Terminating entry segment. */ |
Curt Brune | 3c12cb0 | 2014-08-29 10:43:36 -0700 | [diff] [blame] | 313 | bzp_output_segment(&bzp, NULL, PAYLOAD_SEGMENT_ENTRY, 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 | /* 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 Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 319 | |
Aaron Durbin | 4f3bb80 | 2014-03-26 22:57:55 -0500 | [diff] [blame] | 320 | /* Serialize the segments with the correct encoding. */ |
| 321 | xdr_segs(output, bzp.segs, bzp.num_segments); |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 322 | return 0; |
| 323 | } |