1 ;; -----------------------------------------------------------------------
3 ;; Copyright 1994-2006 H. Peter Anvin - All Rights Reserved
5 ;; This program is free software; you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation, Inc., 53 Temple Place Ste 330,
8 ;; Boston MA 02111-1307, USA; either version 2 of the License, or
9 ;; (at your option) any later version; incorporated herein by reference.
11 ;; -----------------------------------------------------------------------
16 ;; Common code for running a Linux kernel
20 ; Hook macros, that may or may not be defined
22 %ifndef HAVE_SPECIAL_APPEND
23 %macro SPECIAL_APPEND 0
27 %ifndef HAVE_UNLOAD_PREP
33 ; A Linux kernel consists of three parts: boot sector, setup code, and
34 ; kernel code. The boot sector is never executed when using an external
35 ; booting utility, but it contains some status bytes that are necessary.
37 ; First check that our kernel is at least 1K, or else it isn't long
38 ; enough to have the appropriate headers.
40 ; We used to require the kernel to be 64K or larger, but it has gotten
41 ; popular to use the Linux kernel format for other things, which may
44 ; Additionally, we used to have a test for 8 MB or smaller. Equally
50 cmp ax,1024 ; Bootsect + 1 setup sect
58 ; Now start transferring the kernel
60 push word real_mode_seg
63 movzx eax,ax ; Fix this by using a 32-bit
64 shl edx,16 ; register for the kernel size
69 mov [KernelSects],eax ; Total sectors in kernel
72 ; Now, if we transfer these straight, we'll hit 64K boundaries. Hence we
73 ; have to see if we're loading more than 64K, and if so, load it step by
78 ; Start by loading the bootsector/setup code, to see if we need to
79 ; do something funky. It should fit in the first 32K (loading 64K won't
80 ; work since we might have funny stuff up near the end of memory).
81 ; If we have larger than 32K clusters, yes, we're hosed.
83 call abort_check ; Check for abort key
84 mov ecx,8000h >> SECTOR_SHIFT ; Half a moby (32K)
91 pop si ; Cluster pointer on stack
93 cmp word [es:bs_bootsign],0AA55h
94 jne kernel_corrupt ; Boot sec signature missing
97 ; Save the cluster pointer for later...
101 ; Get the BIOS' idea of what the size of high memory is.
105 ; Construct the command line (append options have already been copied)
109 mov si,boot_image ; BOOT_IMAGE=
110 mov cx,boot_image_len
112 mov si,KernelCName ; Unmangled kernel name
113 mov cx,[KernelCNameLen]
118 SPECIAL_APPEND ; Module-specific hook
120 mov si,[CmdOptPtr] ; Options from user input
124 ; Scan through the command line for anything that looks like we might be
125 ; interested in. The original version of this code automatically assumed
126 ; the first option was BOOT_IMAGE=, but that is no longer certain.
130 mov [InitRDPtr],ax ; No initrd= option (yet)
131 push es ; Set DS <- real_mode_seg
145 cmp eax,'keep' ; Is it "keeppxe"?
147 cmp dword [si+3],'ppxe'
149 cmp byte [si+7],' ' ; Must be whitespace or EOS
151 or byte [cs:KeepPXE],1
154 push es ; Save ES -> real_mode_seg
156 pop es ; Set ES <- normal DS
158 mov cx,initrd_cmd_len
164 mov [cs:InitRDPtr],si
168 mov [cs:InitRDPtr],ax
169 .not_initrd: pop es ; Restore ES -> real_mode_seg
170 skip_this_opt: lodsb ; Load from command line
174 jmp short get_next_opt
179 cmp eax,'=nor' ; vga=normal
182 cmp eax,'=ext' ; vga=ext
185 cmp eax,'=ask' ; vga=ask
187 call parseint ; vga=<number>
188 jc skip_this_opt ; Not an integer
189 vc0: mov [bs_vidmode],bx ; Set video mode
190 jmp short skip_this_opt
194 jc skip_this_opt ; Not an integer
195 %if HIGHMEM_SLOP != 0
198 mov [cs:HighMemSize],ebx
199 jmp short skip_this_opt
201 push cs ; Restore standard DS
204 mov [CmdLineLen],si ; Length including final null
206 ; Now check if we have a large kernel, which needs to be loaded high
208 mov dword [RamdiskMax], HIGHMEM_MAX ; Default initrd limit
209 cmp dword [es:su_header],HEADER_ID ; New setup code ID
210 jne old_kernel ; Old kernel, load low
211 cmp word [es:su_version],0200h ; Setup code version 2.0
212 jb old_kernel ; Old kernel, load low
213 cmp word [es:su_version],0201h ; Version 2.01+?
214 jb new_kernel ; If 2.00, skip this step
215 mov word [es:su_heapend],linux_stack ; Set up the heap
216 or byte [es:su_loadflags],80h ; Let the kernel know we care
217 cmp word [es:su_version],0203h ; Version 2.03+?
218 jb new_kernel ; Not 2.03+
219 mov eax,[es:su_ramdisk_max]
220 mov [RamdiskMax],eax ; Set the ramdisk limit
223 ; We definitely have a new-style kernel. Let the kernel know who we are,
224 ; and that we are clueful
227 mov byte [es:su_loader],my_id ; Show some ID
228 movzx ax,byte [es:bs_setupsecs] ; Variable # of setup sectors
231 mov [es:su_ramdisklen],eax ; No initrd loaded yet
234 ; About to load the kernel. This is a modern kernel, so use the boot flags
237 mov al,[es:su_loadflags]
240 ; Load the kernel. We always load it at 100000h even if we're supposed to
241 ; load it "low"; for a "low" load we copy it down to low memory right before
245 mov si,KernelCName ; Print kernel name part of
246 call cwritestr ; "Loading" message
247 mov si,dotdot_msg ; Print dots
250 mov eax,[HighMemSize]
251 sub eax,100000h ; Load address
253 jb no_high_mem ; Not enough high memory
255 ; Move the stuff beyond the setup code to high memory at 100000h
257 movzx esi,word [SetupSecs] ; Setup sectors
258 inc si ; plus 1 boot sector
259 shl si,9 ; Convert to bytes
261 sub ecx,esi ; Number of bytes to copy
263 add esi,(real_mode_seg << 4) ; Pointer to source
264 mov edi,100000h ; Copy to address 100000h
266 call bcopy ; Transfer to high memory
268 ; On exit EDI -> where to load the rest
270 mov si,dot_msg ; Progress report
274 pop ecx ; Number of bytes in the initial portion
275 pop si ; Restore file handle/cluster pointer
277 sub eax,8000h ; Amount of kernel not yet loaded
278 jbe high_load_done ; Zero left (tiny kernel)
280 xor dx,dx ; No padding needed
281 mov bx,dot_pause ; Print dots...
282 call load_high ; Copy the file
286 mov ax,real_mode_seg ; Set to real mode seg
293 ; Now see if we have an initial RAMdisk; if so, do requisite computation
294 ; We know we have a new kernel; the old_kernel code already will have objected
295 ; if we tried to load initrd using an old kernel
298 cmp word [InitRDPtr],0
300 call parse_load_initrd
303 ; Abandon hope, ye that enter here! We do no longer permit aborts.
305 call abort_check ; Last chance!!
310 call vgaclearmode ; We can't trust ourselves after this
312 UNLOAD_PREP ; Module-specific hook
315 ; Now, if we were supposed to load "low", copy the kernel down to 10000h
316 ; and the real mode stuff to 90000h. We assume that all bzImage kernels are
317 ; capable of starting their setup from a different address.
323 ; Copy command line. Unfortunately, the kernel boot protocol requires
324 ; the command line to exist in the 9xxxxh range even if the rest of the
327 cli ; In case of hooked interrupts
328 test byte [LoadFlags],LOAD_HIGH
330 cmp word [fs:su_version],0202h ; Support new cmdline protocol?
332 ; New cmdline protocol
333 ; Store 32-bit (flat) pointer to command line
334 mov dword [fs:su_cmd_line_ptr],(real_mode_seg << 4) + cmd_line_here
335 jmp short in_proper_place
339 ; Copy command line up to 90000h
341 mov ax,9000h ; Note AL <- 0
345 mov [fs:kern_cmd_magic],word CMD_MAGIC ; Store magic
346 mov [fs:kern_cmd_offset],di ; Store pointer
351 mov cx,255 ; Protocol < 0x202 has 255 as hard limit
354 stosb ; Final null, note AL == 0 already
359 test byte [LoadFlags],LOAD_HIGH
360 jnz in_proper_place ; If high load, we're done
363 ; Loading low; we can't assume it's safe to run in place.
365 ; Copy real_mode stuff up to 90000h
370 inc cx ; Setup + boot sector
371 shl cx,7 ; Sectors -> dwords
374 fs rep movsd ; Copy setup + boot sector
376 ; Some kernels in the 1.2 ballpark but pre-bzImage have more than 4
377 ; setup sectors, but the boot protocol had not yet been defined. They
378 ; rely on a signature to figure out if they need to copy stuff from
379 ; the "protected mode" kernel area. Unfortunately, we used that area
380 ; as a transfer buffer, so it's going to find the signature there.
381 ; Hence, zero the low 32K beyond the setup area.
384 inc di ; Setup + boot sector
385 mov cx,32768/512 ; Sectors/32K
386 sub cx,di ; Remaining sectors
387 shl di,9 ; Sectors -> bytes
388 shl cx,7 ; Sectors -> dwords
390 rep stosd ; Clear region
392 ; Copy the kernel down to the "low" location
400 ; Now everything is where it needs to be...
402 ; When we get here, es points to the final segment, either
403 ; 9000h or real_mode_seg
408 ; If the default root device is set to FLOPPY (0000h), change to
411 cmp word [es:bs_rootdev],byte 0
413 mov word [es:bs_rootdev],0200h
417 ; Copy the disk table to high memory, then re-initialize the floppy
420 %if IS_SYSLINUX || IS_MDSLINUX
425 mov [cs:fdctab],word linux_fdctab ; Save new floppy tab pos
429 ; Linux wants the floppy motor shut off before starting the kernel,
430 ; at least bootsect.S seems to imply so.
438 ; If we're debugging, wait for a keypress so we can read any debug messages
445 ; Set up segment registers and the Linux real-mode stack
446 ; Note: es == the real mode segment
456 ; We're done... now RUN THAT KERNEL!!!!
457 ; Setup segment == real mode segment + 020h; we need to jump to offset
458 ; zero in the real mode segment.
466 ; Load an older kernel. Older kernels always have 4 setup sectors, can't have
467 ; initrd, and are always loaded low.
470 cmp word [InitRDPtr],0 ; Old kernel can't have initrd
475 mov word [SetupSecs],4 ; Always 4 setup sectors
476 mov byte [LoadFlags],0 ; Always low
482 ; Parse an initrd= option and load the initrds. Note that we load
483 ; from the high end of memory first, so we parse this option from
492 pop es ; DS == real_mode_seg, ES == CS
494 mov si,[cs:InitRDPtr]
499 ; Now SI points to one character beyond the
500 ; byte that ended this option.
505 ; DS:SI points to a termination byte
508 xchg al,[si] ; Zero-terminate
509 push si ; Save ending byte address
510 push ax ; Save ending byte
514 cmp si,[cs:InitRDPtr]
524 mov di,InitRD ; Target buffer for mangled name
531 mov [di],al ; Restore ending byte
533 cmp si,[cs:InitRDPtr]
541 ; Load RAM disk into high memory
543 ; Input: InitRD - set to the mangled name of the initrd
548 mov ax,cs ; CS == DS == ES
553 call unmangle_name ; Create human-readable name
555 mov [InitRDCNameLen],di
557 call searchdir ; Look for it in directory
562 mov cx,ax ; ECX <- ram disk length
567 push ecx ; Bytes to load
568 cmp dword [es:su_ramdisklen],0
569 je .nopadding ; Don't pad the last initrd
573 add [es:su_ramdisklen],ecx
574 mov edx,[HighMemSize] ; End of memory
576 mov eax,[RamdiskMax] ; Highest address allowed by kernel
579 mov edx,eax ; Adjust to fit inside limit
582 and dx,0F000h ; Round down to 4K boundary
583 sub edx,ecx ; Subtract size of ramdisk
584 and dx,0F000h ; Round down to 4K boundary
585 cmp edx,[KernelEnd] ; Are we hitting the kernel image?
588 mov [es:su_ramdiskat],edx ; Load address
589 mov [RamdiskMax],edx ; Next initrd loaded here
591 mov edi,edx ; initrd load address
593 mov si,crlfloading_msg ; Write "Loading "
595 mov si,InitRDCName ; Write ramdisk name
597 mov si,dotdot_msg ; Write dots
601 pop eax ; Bytes to load
602 mov dx,0FFFh ; Pad to page
603 mov bx,dot_pause ; Print dots...
604 call load_high ; Load the file
608 jmp crlf ; Print carriage return and return
618 no_high_mem: ; Error routine
625 boot_image db 'BOOT_IMAGE='
626 boot_image_len equ $-boot_image
630 RamdiskMax resd 1 ; Highest address for ramdisk
631 KernelSize resd 1 ; Size of kernel in bytes
632 KernelSects resd 1 ; Size of kernel in sectors
633 KernelEnd resd 1 ; Ending address of the kernel image
634 CmdLineLen resw 1 ; Length of command line including null
635 SetupSecs resw 1 ; Number of setup sectors
636 InitRDPtr resw 1 ; Pointer to initrd= option in command line
637 LoadFlags resb 1 ; Loadflags from kernel