1 /* ----------------------------------------------------------------------- *
3 * Copyright 2007-2009 H. Peter Anvin - All Rights Reserved
4 * Copyright 2009-2011 Intel Corporation; author: H. Peter Anvin
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or
11 * sell copies of the Software, and to permit persons to whom
12 * the Software is furnished to do so, subject to the following
15 * The above copyright notice and this permission notice shall
16 * be included in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
27 * ----------------------------------------------------------------------- */
32 * Load a Linux kernel (Image/zImage/bzImage).
41 #include <suffix_number.h>
42 #include <syslinux/align.h>
43 #include <syslinux/linux.h>
44 #include <syslinux/bootrm.h>
45 #include <syslinux/movebits.h>
49 uint8_t boot_sector_1[0x0020];
50 uint16_t old_cmd_line_magic;
51 uint16_t old_cmd_line_offset;
52 uint8_t boot_sector_2[0x01f1 - 0x0024];
63 uint32_t realmode_swtch;
65 uint16_t kernel_version;
66 uint8_t type_of_loader;
68 uint16_t setup_move_size;
69 uint32_t code32_start;
70 uint32_t ramdisk_image;
71 uint32_t ramdisk_size;
72 uint32_t bootsect_kludge;
73 uint16_t heap_end_ptr;
75 uint32_t cmd_line_ptr;
76 uint32_t initrd_addr_max;
77 uint32_t kernel_alignment;
78 uint8_t relocatable_kernel;
80 uint32_t cmdline_max_len;
81 uint32_t hardware_subarch;
82 uint64_t hardware_subarch_data;
83 uint32_t payload_offset;
84 uint32_t payload_length;
86 uint64_t pref_address;
90 #define BOOT_MAGIC 0xAA55
91 #define LINUX_MAGIC ('H' + ('d' << 8) + ('r' << 16) + ('S' << 24))
92 #define OLD_CMDLINE_MAGIC 0xA33F
95 #define LOAD_HIGH 0x01
96 #define CAN_USE_HEAP 0x80
99 * Find the last instance of a particular command line argument
100 * (which should include the final =; do not use for boolean arguments)
101 * Note: the resulting string is typically not null-terminated.
103 static const char *find_argument(const char *cmdline, const char *argument)
105 const char *found = NULL;
106 const char *p = cmdline;
107 bool was_space = true;
108 size_t la = strlen(argument);
113 } else if (was_space) {
114 if (!memcmp(p, argument, la))
124 /* Truncate to 32 bits, with saturate */
125 static inline uint32_t saturate32(unsigned long long v)
127 return (v > 0xffffffff) ? 0xffffffff : (uint32_t) v;
130 /* Get the combined size of the initramfs */
131 static addr_t initramfs_size(struct initramfs *initramfs)
133 struct initramfs *ip;
139 for (ip = initramfs->next; ip->len; ip = ip->next) {
140 size = (size + ip->align - 1) & ~(ip->align - 1); /* Alignment */
147 /* Create the appropriate mappings for the initramfs */
148 static int map_initramfs(struct syslinux_movelist **fraglist,
149 struct syslinux_memmap **mmap,
150 struct initramfs *initramfs, addr_t addr)
152 struct initramfs *ip;
153 addr_t next_addr, len, pad;
155 for (ip = initramfs->next; ip->len; ip = ip->next) {
157 next_addr = addr + len;
159 /* If this isn't the last entry, extend the zero-pad region
160 to enforce the alignment of the next chunk. */
162 pad = -next_addr & (ip->next->align - 1);
168 if (syslinux_add_movelist(fraglist, addr, (addr_t) ip->data, len))
171 if (len > ip->data_len) {
172 if (syslinux_add_memmap(mmap, addr + ip->data_len,
173 len - ip->data_len, SMT_ZERO))
182 int syslinux_boot_linux(void *kernel_buf, size_t kernel_size,
183 struct initramfs *initramfs,
184 struct setup_data *setup_data,
187 struct linux_header hdr, *whdr;
188 size_t real_mode_size, prot_mode_size;
189 addr_t real_mode_base, prot_mode_base;
191 size_t cmdline_size, cmdline_offset;
192 struct setup_data *sdp;
193 struct syslinux_rm_regs regs;
194 struct syslinux_movelist *fraglist = NULL;
195 struct syslinux_memmap *mmap = NULL;
196 struct syslinux_memmap *amap = NULL;
198 uint32_t memlimit = 0;
199 uint16_t video_mode = 0;
202 cmdline_size = strlen(cmdline) + 1;
204 if (kernel_size < 2 * 512)
207 /* Look for specific command-line arguments we care about */
208 if ((arg = find_argument(cmdline, "mem=")))
209 memlimit = saturate32(suffix_number(arg));
211 if ((arg = find_argument(cmdline, "vga="))) {
212 switch (arg[0] | 0x20) {
213 case 'a': /* "ask" */
216 case 'e': /* "ext" */
219 case 'n': /* "normal" */
222 case 'c': /* "current" */
226 video_mode = strtoul(arg, NULL, 0);
231 /* Copy the header into private storage */
232 /* Use whdr to modify the actual kernel header */
233 memcpy(&hdr, kernel_buf, sizeof hdr);
234 whdr = (struct linux_header *)kernel_buf;
236 if (hdr.boot_flag != BOOT_MAGIC)
239 if (hdr.header != LINUX_MAGIC) {
240 hdr.version = 0x0100; /* Very old kernel */
244 whdr->vid_mode = video_mode;
246 if (!hdr.setup_sects)
249 if (hdr.version < 0x0203)
250 hdr.initrd_addr_max = 0x37ffffff;
252 if (!memlimit && memlimit - 1 > hdr.initrd_addr_max)
253 memlimit = hdr.initrd_addr_max + 1; /* Zero for no limit */
255 if (hdr.version < 0x0205 || !(hdr.loadflags & LOAD_HIGH))
256 hdr.relocatable_kernel = 0;
258 if (hdr.version < 0x0206)
259 hdr.cmdline_max_len = 256;
261 if (cmdline_size > hdr.cmdline_max_len) {
262 cmdline_size = hdr.cmdline_max_len;
263 cmdline[cmdline_size - 1] = '\0';
266 if (hdr.version < 0x0202 || !(hdr.loadflags & 0x01))
267 cmdline_offset = (0x9ff0 - cmdline_size) & ~15;
269 cmdline_offset = 0x10000;
271 real_mode_size = (hdr.setup_sects + 1) << 9;
272 real_mode_base = (hdr.loadflags & LOAD_HIGH) ? 0x10000 : 0x90000;
273 prot_mode_base = (hdr.loadflags & LOAD_HIGH) ? 0x100000 : 0x10000;
274 prot_mode_size = kernel_size - real_mode_size;
276 if (hdr.version < 0x020a) {
278 * The 3* here is a total fudge factor... it's supposed to
279 * account for the fact that the kernel needs to be
280 * decompressed, and then followed by the BSS and BRK regions.
281 * This doesn't, however, account for the fact that the kernel
282 * is decompressed into a whole other place, either.
284 hdr.init_size = 3 * prot_mode_size;
287 if (!(hdr.loadflags & LOAD_HIGH) && prot_mode_size > 512 * 1024)
288 goto bail; /* Kernel cannot be loaded low */
290 if (initramfs && hdr.version < 0x0200)
291 goto bail; /* initrd/initramfs not supported */
293 if (hdr.version >= 0x0200) {
294 whdr->type_of_loader = 0x30; /* SYSLINUX unknown module */
295 if (hdr.version >= 0x0201) {
296 whdr->heap_end_ptr = cmdline_offset - 0x0200;
297 whdr->loadflags |= CAN_USE_HEAP;
299 if (hdr.version >= 0x0202) {
300 whdr->cmd_line_ptr = real_mode_base + cmdline_offset;
302 whdr->old_cmd_line_magic = OLD_CMDLINE_MAGIC;
303 whdr->old_cmd_line_offset = cmdline_offset;
304 /* Be paranoid and round up to a multiple of 16 */
305 whdr->setup_move_size = (cmdline_offset + cmdline_size + 15) & ~15;
309 /* Get the memory map */
310 mmap = syslinux_memory_map(); /* Memory map for shuffle_boot */
311 amap = syslinux_dup_memmap(mmap); /* Keep track of available memory */
315 dprintf("Initial memory map:\n");
316 syslinux_dump_memmap(mmap);
318 /* If the user has specified a memory limit, mark that as unavailable.
319 Question: should we mark this off-limit in the mmap as well (meaning
320 it's unavailable to the boot loader, which probably has already touched
321 some of it), or just in the amap? */
323 if (syslinux_add_memmap(&amap, memlimit, -memlimit, SMT_RESERVED))
326 /* Place the kernel in memory */
328 /* First, find a suitable place for the protected-mode code */
329 if (syslinux_memmap_type(amap, prot_mode_base, prot_mode_size)
331 const struct syslinux_memmap *mp;
332 if (!hdr.relocatable_kernel)
333 goto bail; /* Can't relocate - no hope */
336 for (mp = amap; mp; mp = mp->next) {
339 end = mp->next->start;
341 if (mp->type != SMT_FREE)
344 if (end <= prot_mode_base)
345 continue; /* Only relocate upwards */
347 if (start <= prot_mode_base)
348 start = prot_mode_base;
350 start = ALIGN_UP(start, hdr.kernel_alignment);
354 if (end - start >= hdr.init_size) {
355 whdr->code32_start += start - prot_mode_base;
356 prot_mode_base = start;
367 if (syslinux_memmap_type(amap, real_mode_base,
368 cmdline_offset + cmdline_size) != SMT_FREE) {
369 const struct syslinux_memmap *mp;
372 for (mp = amap; mp; mp = mp->next) {
375 end = mp->next->start;
377 if (mp->type != SMT_FREE)
380 if (start < real_mode_base)
381 start = real_mode_base; /* Lowest address we'll use */
382 if (end > 640 * 1024)
385 start = ALIGN_UP(start, 16);
386 if (start > 0x90000 || start >= end)
389 if (end - start >= cmdline_offset + cmdline_size) {
390 real_mode_base = start;
397 if (syslinux_add_movelist(&fraglist, real_mode_base, (addr_t) kernel_buf,
400 if (syslinux_add_memmap
401 (&amap, real_mode_base, cmdline_offset + cmdline_size, SMT_ALLOC))
404 /* Zero region between real mode code and cmdline */
405 if (syslinux_add_memmap(&mmap, real_mode_base + real_mode_size,
406 cmdline_offset - real_mode_size, SMT_ZERO))
410 if (syslinux_add_movelist(&fraglist, real_mode_base + cmdline_offset,
411 (addr_t) cmdline, cmdline_size))
414 /* Protected-mode code */
415 if (syslinux_add_movelist(&fraglist, prot_mode_base,
416 (addr_t) kernel_buf + real_mode_size,
419 if (syslinux_add_memmap(&amap, prot_mode_base, prot_mode_size, SMT_ALLOC))
422 /* Figure out the size of the initramfs, and where to put it.
423 We should put it at the highest possible address which is
424 <= hdr.initrd_addr_max, which fits the entire initramfs. */
426 irf_size = initramfs_size(initramfs); /* Handles initramfs == NULL */
429 addr_t best_addr = 0;
430 struct syslinux_memmap *ml;
431 const addr_t align_mask = INITRAMFS_MAX_ALIGN - 1;
434 for (ml = amap; ml->type != SMT_END; ml = ml->next) {
435 addr_t adj_start = (ml->start + align_mask) & ~align_mask;
436 addr_t adj_end = ml->next->start & ~align_mask;
437 if (ml->type == SMT_FREE && adj_end - adj_start >= irf_size)
438 best_addr = (adj_end - irf_size) & ~align_mask;
442 goto bail; /* Insufficient memory for initramfs */
444 whdr->ramdisk_image = best_addr;
445 whdr->ramdisk_size = irf_size;
447 if (syslinux_add_memmap(&amap, best_addr, irf_size, SMT_ALLOC))
450 if (map_initramfs(&fraglist, &mmap, initramfs, best_addr))
456 uint64_t *prev_ptr = &whdr->setup_data;
458 for (sdp = setup_data->next; sdp != setup_data; sdp = sdp->next) {
459 struct syslinux_memmap *ml;
460 const addr_t align_mask = 15; /* Header is 16 bytes */
461 addr_t best_addr = 0;
462 size_t size = sdp->hdr.len + sizeof(sdp->hdr);
464 if (!sdp->data || !sdp->hdr.len)
467 for (ml = amap; ml->type != SMT_END; ml = ml->next) {
468 addr_t adj_start = (ml->start + align_mask) & ~align_mask;
469 addr_t adj_end = ml->next->start & ~align_mask;
471 if (ml->type == SMT_FREE && adj_end - adj_start >= size)
472 best_addr = (adj_end - size) & ~align_mask;
478 *prev_ptr = best_addr;
479 prev_ptr = &sdp->hdr.next;
481 if (syslinux_add_memmap(&amap, best_addr, size, SMT_ALLOC))
483 if (syslinux_add_movelist(&fraglist, best_addr,
484 (addr_t)&sdp->hdr, sizeof sdp->hdr))
486 if (syslinux_add_movelist(&fraglist, best_addr + sizeof sdp->hdr,
487 (addr_t)sdp->data, sdp->hdr.len))
492 /* Set up the registers on entry */
493 memset(®s, 0, sizeof regs);
494 regs.es = regs.ds = regs.ss = regs.fs = regs.gs = real_mode_base >> 4;
495 regs.cs = (real_mode_base >> 4) + 0x20;
497 /* Linux is OK with sp = 0 = 64K, but perhaps other things aren't... */
498 regs.esp.w[0] = min(cmdline_offset, (size_t) 0xfff0);
500 dprintf("Final memory map:\n");
501 syslinux_dump_memmap(mmap);
503 dprintf("Final available map:\n");
504 syslinux_dump_memmap(amap);
506 dprintf("Initial movelist:\n");
507 syslinux_dump_movelist(fraglist);
509 syslinux_shuffle_boot_rm(fraglist, mmap, 0, ®s);
512 syslinux_free_movelist(fraglist);
513 syslinux_free_memmap(mmap);
514 syslinux_free_memmap(amap);