H. Peter Anvin | 6260731 | 2007-07-11 12:18:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * header.S |
| 3 | * |
| 4 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 5 | * |
| 6 | * Based on bootsect.S and setup.S |
| 7 | * modified by more people than can be counted |
| 8 | * |
| 9 | * Rewritten as a common file by H. Peter Anvin (Apr 2007) |
| 10 | * |
| 11 | * BIG FAT NOTE: We're in real mode using 64k segments. Therefore segment |
| 12 | * addresses must be multiplied by 16 to obtain their respective linear |
| 13 | * addresses. To avoid confusion, linear addresses are written using leading |
| 14 | * hex while segment addresses are written as segment:offset. |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | #include <asm/segment.h> |
| 19 | #include <linux/utsrelease.h> |
| 20 | #include <asm/boot.h> |
| 21 | #include <asm/e820.h> |
Jeremy Fitzhardinge | 0341c14 | 2009-02-13 11:14:01 -0800 | [diff] [blame] | 22 | #include <asm/page_types.h> |
H. Peter Anvin | 6260731 | 2007-07-11 12:18:54 -0700 | [diff] [blame] | 23 | #include <asm/setup.h> |
| 24 | #include "boot.h" |
Ian Campbell | 099e137 | 2008-02-13 20:54:58 +0000 | [diff] [blame] | 25 | #include "offsets.h" |
H. Peter Anvin | 6260731 | 2007-07-11 12:18:54 -0700 | [diff] [blame] | 26 | |
H. Peter Anvin | 6260731 | 2007-07-11 12:18:54 -0700 | [diff] [blame] | 27 | BOOTSEG = 0x07C0 /* original address of boot-sector */ |
H. Peter Anvin | 5e47c47 | 2009-03-11 10:55:33 -0700 | [diff] [blame] | 28 | SYSSEG = 0x1000 /* historical load address >> 4 */ |
H. Peter Anvin | 6260731 | 2007-07-11 12:18:54 -0700 | [diff] [blame] | 29 | |
| 30 | #ifndef SVGA_MODE |
| 31 | #define SVGA_MODE ASK_VGA |
| 32 | #endif |
| 33 | |
| 34 | #ifndef RAMDISK |
| 35 | #define RAMDISK 0 |
| 36 | #endif |
| 37 | |
| 38 | #ifndef ROOT_RDONLY |
| 39 | #define ROOT_RDONLY 1 |
| 40 | #endif |
| 41 | |
| 42 | .code16 |
| 43 | .section ".bstext", "ax" |
| 44 | |
| 45 | .global bootsect_start |
| 46 | bootsect_start: |
| 47 | |
| 48 | # Normalize the start address |
| 49 | ljmp $BOOTSEG, $start2 |
| 50 | |
| 51 | start2: |
| 52 | movw %cs, %ax |
| 53 | movw %ax, %ds |
| 54 | movw %ax, %es |
| 55 | movw %ax, %ss |
| 56 | xorw %sp, %sp |
| 57 | sti |
| 58 | cld |
| 59 | |
| 60 | movw $bugger_off_msg, %si |
| 61 | |
| 62 | msg_loop: |
| 63 | lodsb |
| 64 | andb %al, %al |
| 65 | jz bs_die |
| 66 | movb $0xe, %ah |
| 67 | movw $7, %bx |
| 68 | int $0x10 |
| 69 | jmp msg_loop |
| 70 | |
| 71 | bs_die: |
| 72 | # Allow the user to press a key, then reboot |
| 73 | xorw %ax, %ax |
| 74 | int $0x16 |
| 75 | int $0x19 |
| 76 | |
| 77 | # int 0x19 should never return. In case it does anyway, |
| 78 | # invoke the BIOS reset code... |
| 79 | ljmp $0xf000,$0xfff0 |
| 80 | |
| 81 | .section ".bsdata", "a" |
| 82 | bugger_off_msg: |
| 83 | .ascii "Direct booting from floppy is no longer supported.\r\n" |
| 84 | .ascii "Please use a boot loader program instead.\r\n" |
| 85 | .ascii "\n" |
| 86 | .ascii "Remove disk and press any key to reboot . . .\r\n" |
| 87 | .byte 0 |
| 88 | |
| 89 | |
| 90 | # Kernel attributes; used by setup. This is part 1 of the |
| 91 | # header, from the old boot sector. |
| 92 | |
| 93 | .section ".header", "a" |
| 94 | .globl hdr |
| 95 | hdr: |
H. Peter Anvin | 5e47c47 | 2009-03-11 10:55:33 -0700 | [diff] [blame] | 96 | setup_sects: .byte 0 /* Filled in by build.c */ |
H. Peter Anvin | 6260731 | 2007-07-11 12:18:54 -0700 | [diff] [blame] | 97 | root_flags: .word ROOT_RDONLY |
H. Peter Anvin | 5e47c47 | 2009-03-11 10:55:33 -0700 | [diff] [blame] | 98 | syssize: .long 0 /* Filled in by build.c */ |
| 99 | ram_size: .word 0 /* Obsolete */ |
H. Peter Anvin | 6260731 | 2007-07-11 12:18:54 -0700 | [diff] [blame] | 100 | vid_mode: .word SVGA_MODE |
H. Peter Anvin | 5e47c47 | 2009-03-11 10:55:33 -0700 | [diff] [blame] | 101 | root_dev: .word 0 /* Filled in by build.c */ |
H. Peter Anvin | 6260731 | 2007-07-11 12:18:54 -0700 | [diff] [blame] | 102 | boot_flag: .word 0xAA55 |
| 103 | |
| 104 | # offset 512, entry point |
| 105 | |
| 106 | .globl _start |
| 107 | _start: |
| 108 | # Explicitly enter this as bytes, or the assembler |
| 109 | # tries to generate a 3-byte jump here, which causes |
| 110 | # everything else to push off to the wrong offset. |
| 111 | .byte 0xeb # short (2-byte) jump |
| 112 | .byte start_of_setup-1f |
| 113 | 1: |
| 114 | |
| 115 | # Part 2 of the header, from the old setup.S |
| 116 | |
| 117 | .ascii "HdrS" # header signature |
Huang, Ying | 8b664aa | 2008-03-28 10:49:44 +0800 | [diff] [blame] | 118 | .word 0x0209 # header version number (>= 0x0105) |
H. Peter Anvin | 6260731 | 2007-07-11 12:18:54 -0700 | [diff] [blame] | 119 | # or else old loadlin-1.5 will fail) |
| 120 | .globl realmode_swtch |
| 121 | realmode_swtch: .word 0, 0 # default_switch, SETUPSEG |
H. Peter Anvin | 5e47c47 | 2009-03-11 10:55:33 -0700 | [diff] [blame] | 122 | start_sys_seg: .word SYSSEG # obsolete and meaningless, but just |
| 123 | # in case something decided to "use" it |
H. Peter Anvin | 6260731 | 2007-07-11 12:18:54 -0700 | [diff] [blame] | 124 | .word kernel_version-512 # pointing to kernel version string |
| 125 | # above section of header is compatible |
| 126 | # with loadlin-1.5 (header v1.5). Don't |
| 127 | # change it. |
| 128 | |
H. Peter Anvin | 5e47c47 | 2009-03-11 10:55:33 -0700 | [diff] [blame] | 129 | type_of_loader: .byte 0 # 0 means ancient bootloader, newer |
| 130 | # bootloaders know to change this. |
H. Peter Anvin | 6260731 | 2007-07-11 12:18:54 -0700 | [diff] [blame] | 131 | # See Documentation/i386/boot.txt for |
| 132 | # assigned ids |
| 133 | |
| 134 | # flags, unused bits must be zero (RFU) bit within loadflags |
| 135 | loadflags: |
| 136 | LOADED_HIGH = 1 # If set, the kernel is loaded high |
| 137 | CAN_USE_HEAP = 0x80 # If set, the loader also has set |
| 138 | # heap_end_ptr to tell how much |
| 139 | # space behind setup.S can be used for |
| 140 | # heap purposes. |
| 141 | # Only the loader knows what is free |
H. Peter Anvin | 6260731 | 2007-07-11 12:18:54 -0700 | [diff] [blame] | 142 | .byte LOADED_HIGH |
H. Peter Anvin | 6260731 | 2007-07-11 12:18:54 -0700 | [diff] [blame] | 143 | |
| 144 | setup_move_size: .word 0x8000 # size to move, when setup is not |
| 145 | # loaded at 0x90000. We will move setup |
| 146 | # to 0x90000 then just before jumping |
| 147 | # into the kernel. However, only the |
| 148 | # loader knows how much data behind |
| 149 | # us also needs to be loaded. |
| 150 | |
| 151 | code32_start: # here loaders can put a different |
| 152 | # start address for 32-bit code. |
H. Peter Anvin | 6260731 | 2007-07-11 12:18:54 -0700 | [diff] [blame] | 153 | .long 0x100000 # 0x100000 = default for big kernel |
H. Peter Anvin | 6260731 | 2007-07-11 12:18:54 -0700 | [diff] [blame] | 154 | |
| 155 | ramdisk_image: .long 0 # address of loaded ramdisk image |
| 156 | # Here the loader puts the 32-bit |
| 157 | # address where it loaded the image. |
| 158 | # This only will be read by the kernel. |
| 159 | |
| 160 | ramdisk_size: .long 0 # its size in bytes |
| 161 | |
| 162 | bootsect_kludge: |
| 163 | .long 0 # obsolete |
| 164 | |
H. Peter Anvin | 6b6815c | 2007-10-25 16:11:33 -0700 | [diff] [blame] | 165 | heap_end_ptr: .word _end+STACK_SIZE-512 |
| 166 | # (Header version 0x0201 or later) |
H. Peter Anvin | 6260731 | 2007-07-11 12:18:54 -0700 | [diff] [blame] | 167 | # space from here (exclusive) down to |
| 168 | # end of setup code can be used by setup |
| 169 | # for local heap purposes. |
| 170 | |
| 171 | pad1: .word 0 |
| 172 | cmd_line_ptr: .long 0 # (Header version 0x0202 or later) |
| 173 | # If nonzero, a 32-bit pointer |
| 174 | # to the kernel command line. |
| 175 | # The command line should be |
| 176 | # located between the start of |
| 177 | # setup and the end of low |
| 178 | # memory (0xa0000), or it may |
| 179 | # get overwritten before it |
| 180 | # gets read. If this field is |
| 181 | # used, there is no longer |
| 182 | # anything magical about the |
| 183 | # 0x90000 segment; the setup |
| 184 | # can be located anywhere in |
| 185 | # low memory 0x10000 or higher. |
| 186 | |
H. Peter Anvin | cf8fa92 | 2008-01-30 13:32:51 +0100 | [diff] [blame] | 187 | ramdisk_max: .long 0x7fffffff |
H. Peter Anvin | 6260731 | 2007-07-11 12:18:54 -0700 | [diff] [blame] | 188 | # (Header version 0x0203 or later) |
| 189 | # The highest safe address for |
| 190 | # the contents of an initrd |
H. Peter Anvin | cf8fa92 | 2008-01-30 13:32:51 +0100 | [diff] [blame] | 191 | # The current kernel allows up to 4 GB, |
| 192 | # but leave it at 2 GB to avoid |
| 193 | # possible bootloader bugs. |
H. Peter Anvin | 6260731 | 2007-07-11 12:18:54 -0700 | [diff] [blame] | 194 | |
| 195 | kernel_alignment: .long CONFIG_PHYSICAL_ALIGN #physical addr alignment |
| 196 | #required for protected mode |
| 197 | #kernel |
| 198 | #ifdef CONFIG_RELOCATABLE |
| 199 | relocatable_kernel: .byte 1 |
| 200 | #else |
| 201 | relocatable_kernel: .byte 0 |
| 202 | #endif |
| 203 | pad2: .byte 0 |
| 204 | pad3: .word 0 |
| 205 | |
| 206 | cmdline_size: .long COMMAND_LINE_SIZE-1 #length of the command line, |
| 207 | #added with boot protocol |
| 208 | #version 2.06 |
| 209 | |
Rusty Russell | a24e785 | 2007-10-21 16:41:35 -0700 | [diff] [blame] | 210 | hardware_subarch: .long 0 # subarchitecture, added with 2.07 |
| 211 | # default to 0 for normal x86 PC |
| 212 | |
| 213 | hardware_subarch_data: .quad 0 |
| 214 | |
Ian Campbell | 87253d1 | 2008-02-19 11:12:30 +0000 | [diff] [blame] | 215 | payload_offset: .long input_data |
| 216 | payload_length: .long input_data_end-input_data |
Ian Campbell | 099e137 | 2008-02-13 20:54:58 +0000 | [diff] [blame] | 217 | |
Huang, Ying | 8b664aa | 2008-03-28 10:49:44 +0800 | [diff] [blame] | 218 | setup_data: .quad 0 # 64-bit physical pointer to |
| 219 | # single linked list of |
| 220 | # struct setup_data |
| 221 | |
H. Peter Anvin | 6260731 | 2007-07-11 12:18:54 -0700 | [diff] [blame] | 222 | # End of setup header ##################################################### |
| 223 | |
H. Peter Anvin | 7a734e7 | 2009-04-01 18:08:28 -0700 | [diff] [blame] | 224 | .section ".entrytext", "ax" |
H. Peter Anvin | 6260731 | 2007-07-11 12:18:54 -0700 | [diff] [blame] | 225 | start_of_setup: |
| 226 | #ifdef SAFE_RESET_DISK_CONTROLLER |
| 227 | # Reset the disk controller. |
| 228 | movw $0x0000, %ax # Reset disk controller |
| 229 | movb $0x80, %dl # All disks |
| 230 | int $0x13 |
| 231 | #endif |
| 232 | |
H. Peter Anvin | 6260731 | 2007-07-11 12:18:54 -0700 | [diff] [blame] | 233 | # Force %es = %ds |
| 234 | movw %ds, %ax |
| 235 | movw %ax, %es |
| 236 | cld |
| 237 | |
Jens Rottmann | 16252da | 2007-11-27 12:35:13 +0100 | [diff] [blame] | 238 | # Apparently some ancient versions of LILO invoked the kernel with %ss != %ds, |
| 239 | # which happened to work by accident for the old code. Recalculate the stack |
| 240 | # pointer if %ss is invalid. Otherwise leave it alone, LOADLIN sets up the |
| 241 | # stack behind its own code, so we can't blindly put it directly past the heap. |
H. Peter Anvin | 6b6815c | 2007-10-25 16:11:33 -0700 | [diff] [blame] | 242 | |
H. Peter Anvin | 6b6815c | 2007-10-25 16:11:33 -0700 | [diff] [blame] | 243 | movw %ss, %dx |
| 244 | cmpw %ax, %dx # %ds == %ss? |
| 245 | movw %sp, %dx |
Jens Rottmann | 16252da | 2007-11-27 12:35:13 +0100 | [diff] [blame] | 246 | je 2f # -> assume %sp is reasonably set |
H. Peter Anvin | 6b6815c | 2007-10-25 16:11:33 -0700 | [diff] [blame] | 247 | |
Jens Rottmann | 16252da | 2007-11-27 12:35:13 +0100 | [diff] [blame] | 248 | # Invalid %ss, make up a new stack |
| 249 | movw $_end, %dx |
| 250 | testb $CAN_USE_HEAP, loadflags |
| 251 | jz 1f |
| 252 | movw heap_end_ptr, %dx |
| 253 | 1: addw $STACK_SIZE, %dx |
| 254 | jnc 2f |
| 255 | xorw %dx, %dx # Prevent wraparound |
| 256 | |
| 257 | 2: # Now %dx should point to the end of our stack space |
H. Peter Anvin | 6b6815c | 2007-10-25 16:11:33 -0700 | [diff] [blame] | 258 | andw $~3, %dx # dword align (might as well...) |
| 259 | jnz 3f |
| 260 | movw $0xfffc, %dx # Make sure we're not zero |
Jens Rottmann | 16252da | 2007-11-27 12:35:13 +0100 | [diff] [blame] | 261 | 3: movw %ax, %ss |
H. Peter Anvin | 6b6815c | 2007-10-25 16:11:33 -0700 | [diff] [blame] | 262 | movzwl %dx, %esp # Clear upper half of %esp |
| 263 | sti # Now we should have a working stack |
| 264 | |
| 265 | # We will have entered with %cs = %ds+0x20, normalize %cs so |
| 266 | # it is on par with the other segments. |
| 267 | pushw %ds |
| 268 | pushw $6f |
| 269 | lretw |
| 270 | 6: |
H. Peter Anvin | 6260731 | 2007-07-11 12:18:54 -0700 | [diff] [blame] | 271 | |
| 272 | # Check signature at end of setup |
| 273 | cmpl $0x5a5aaa55, setup_sig |
| 274 | jne setup_bad |
| 275 | |
| 276 | # Zero the bss |
| 277 | movw $__bss_start, %di |
| 278 | movw $_end+3, %cx |
| 279 | xorl %eax, %eax |
| 280 | subw %di, %cx |
| 281 | shrw $2, %cx |
| 282 | rep; stosl |
| 283 | |
| 284 | # Jump to C code (should not return) |
| 285 | calll main |
| 286 | |
| 287 | # Setup corrupt somehow... |
| 288 | setup_bad: |
| 289 | movl $setup_corrupt, %eax |
| 290 | calll puts |
| 291 | # Fall through... |
| 292 | |
| 293 | .globl die |
| 294 | .type die, @function |
| 295 | die: |
| 296 | hlt |
| 297 | jmp die |
| 298 | |
Paul Bolle | bbc15f4 | 2007-09-10 23:39:02 +0200 | [diff] [blame] | 299 | .size die, .-die |
H. Peter Anvin | 6260731 | 2007-07-11 12:18:54 -0700 | [diff] [blame] | 300 | |
| 301 | .section ".initdata", "a" |
| 302 | setup_corrupt: |
| 303 | .byte 7 |
H. Peter Anvin | 8b608d2 | 2007-07-26 16:10:22 -0700 | [diff] [blame] | 304 | .string "No setup signature found...\n" |