header.S 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. #include <asm/segment.h>
  18. #include <generated/utsrelease.h>
  19. #include <asm/boot.h>
  20. #include <asm/e820.h>
  21. #include <asm/page_types.h>
  22. #include <asm/setup.h>
  23. #include "boot.h"
  24. #include "voffset.h"
  25. #include "zoffset.h"
  26. BOOTSEG = 0x07C0 /* original address of boot-sector */
  27. SYSSEG = 0x1000 /* historical load address >> 4 */
  28. #ifndef SVGA_MODE
  29. #define SVGA_MODE ASK_VGA
  30. #endif
  31. #ifndef RAMDISK
  32. #define RAMDISK 0
  33. #endif
  34. #ifndef ROOT_RDONLY
  35. #define ROOT_RDONLY 1
  36. #endif
  37. .code16
  38. .section ".bstext", "ax"
  39. .global bootsect_start
  40. bootsect_start:
  41. #ifdef CONFIG_EFI_STUB
  42. # "MZ", MS-DOS header
  43. .byte 0x4d
  44. .byte 0x5a
  45. #endif
  46. # Normalize the start address
  47. ljmp $BOOTSEG, $start2
  48. start2:
  49. movw %cs, %ax
  50. movw %ax, %ds
  51. movw %ax, %es
  52. movw %ax, %ss
  53. xorw %sp, %sp
  54. sti
  55. cld
  56. movw $bugger_off_msg, %si
  57. msg_loop:
  58. lodsb
  59. andb %al, %al
  60. jz bs_die
  61. movb $0xe, %ah
  62. movw $7, %bx
  63. int $0x10
  64. jmp msg_loop
  65. bs_die:
  66. # Allow the user to press a key, then reboot
  67. xorw %ax, %ax
  68. int $0x16
  69. int $0x19
  70. # int 0x19 should never return. In case it does anyway,
  71. # invoke the BIOS reset code...
  72. ljmp $0xf000,$0xfff0
  73. #ifdef CONFIG_EFI_STUB
  74. .org 0x3c
  75. #
  76. # Offset to the PE header.
  77. #
  78. .long pe_header
  79. #endif /* CONFIG_EFI_STUB */
  80. .section ".bsdata", "a"
  81. bugger_off_msg:
  82. .ascii "Direct booting from floppy is no longer supported.\r\n"
  83. .ascii "Please use a boot loader program instead.\r\n"
  84. .ascii "\n"
  85. .ascii "Remove disk and press any key to reboot . . .\r\n"
  86. .byte 0
  87. #ifdef CONFIG_EFI_STUB
  88. pe_header:
  89. .ascii "PE"
  90. .word 0
  91. coff_header:
  92. #ifdef CONFIG_X86_32
  93. .word 0x14c # i386
  94. #else
  95. .word 0x8664 # x86-64
  96. #endif
  97. .word 2 # nr_sections
  98. .long 0 # TimeDateStamp
  99. .long 0 # PointerToSymbolTable
  100. .long 1 # NumberOfSymbols
  101. .word section_table - optional_header # SizeOfOptionalHeader
  102. #ifdef CONFIG_X86_32
  103. .word 0x306 # Characteristics.
  104. # IMAGE_FILE_32BIT_MACHINE |
  105. # IMAGE_FILE_DEBUG_STRIPPED |
  106. # IMAGE_FILE_EXECUTABLE_IMAGE |
  107. # IMAGE_FILE_LINE_NUMS_STRIPPED
  108. #else
  109. .word 0x206 # Characteristics
  110. # IMAGE_FILE_DEBUG_STRIPPED |
  111. # IMAGE_FILE_EXECUTABLE_IMAGE |
  112. # IMAGE_FILE_LINE_NUMS_STRIPPED
  113. #endif
  114. optional_header:
  115. #ifdef CONFIG_X86_32
  116. .word 0x10b # PE32 format
  117. #else
  118. .word 0x20b # PE32+ format
  119. #endif
  120. .byte 0x02 # MajorLinkerVersion
  121. .byte 0x14 # MinorLinkerVersion
  122. # Filled in by build.c
  123. .long 0 # SizeOfCode
  124. .long 0 # SizeOfInitializedData
  125. .long 0 # SizeOfUninitializedData
  126. # Filled in by build.c
  127. .long 0x0000 # AddressOfEntryPoint
  128. .long 0x0000 # BaseOfCode
  129. #ifdef CONFIG_X86_32
  130. .long 0 # data
  131. #endif
  132. extra_header_fields:
  133. #ifdef CONFIG_X86_32
  134. .long 0 # ImageBase
  135. #else
  136. .quad 0 # ImageBase
  137. #endif
  138. .long 0x1000 # SectionAlignment
  139. .long 0x200 # FileAlignment
  140. .word 0 # MajorOperatingSystemVersion
  141. .word 0 # MinorOperatingSystemVersion
  142. .word 0 # MajorImageVersion
  143. .word 0 # MinorImageVersion
  144. .word 0 # MajorSubsystemVersion
  145. .word 0 # MinorSubsystemVersion
  146. .long 0 # Win32VersionValue
  147. #
  148. # The size of the bzImage is written in tools/build.c
  149. #
  150. .long 0 # SizeOfImage
  151. .long 0x200 # SizeOfHeaders
  152. .long 0 # CheckSum
  153. .word 0xa # Subsystem (EFI application)
  154. .word 0 # DllCharacteristics
  155. #ifdef CONFIG_X86_32
  156. .long 0 # SizeOfStackReserve
  157. .long 0 # SizeOfStackCommit
  158. .long 0 # SizeOfHeapReserve
  159. .long 0 # SizeOfHeapCommit
  160. #else
  161. .quad 0 # SizeOfStackReserve
  162. .quad 0 # SizeOfStackCommit
  163. .quad 0 # SizeOfHeapReserve
  164. .quad 0 # SizeOfHeapCommit
  165. #endif
  166. .long 0 # LoaderFlags
  167. .long 0x1 # NumberOfRvaAndSizes
  168. .quad 0 # ExportTable
  169. .quad 0 # ImportTable
  170. .quad 0 # ResourceTable
  171. .quad 0 # ExceptionTable
  172. .quad 0 # CertificationTable
  173. .quad 0 # BaseRelocationTable
  174. # Section table
  175. section_table:
  176. .ascii ".text"
  177. .byte 0
  178. .byte 0
  179. .byte 0
  180. .long 0
  181. .long 0x0 # startup_{32,64}
  182. .long 0 # Size of initialized data
  183. # on disk
  184. .long 0x0 # startup_{32,64}
  185. .long 0 # PointerToRelocations
  186. .long 0 # PointerToLineNumbers
  187. .word 0 # NumberOfRelocations
  188. .word 0 # NumberOfLineNumbers
  189. .long 0x60500020 # Characteristics (section flags)
  190. #
  191. # The EFI application loader requires a relocation section
  192. # because EFI applications are relocatable and not having
  193. # this section seems to confuse it. But since we don't need
  194. # the loader to fixup any relocs for us just fill it with a
  195. # single dummy reloc.
  196. #
  197. .ascii ".reloc"
  198. .byte 0
  199. .byte 0
  200. .long reloc_end - reloc_start
  201. .long reloc_start
  202. .long reloc_end - reloc_start # SizeOfRawData
  203. .long reloc_start # PointerToRawData
  204. .long 0 # PointerToRelocations
  205. .long 0 # PointerToLineNumbers
  206. .word 0 # NumberOfRelocations
  207. .word 0 # NumberOfLineNumbers
  208. .long 0x42100040 # Characteristics (section flags)
  209. #endif /* CONFIG_EFI_STUB */
  210. # Kernel attributes; used by setup. This is part 1 of the
  211. # header, from the old boot sector.
  212. .section ".header", "a"
  213. .globl hdr
  214. hdr:
  215. setup_sects: .byte 0 /* Filled in by build.c */
  216. root_flags: .word ROOT_RDONLY
  217. syssize: .long 0 /* Filled in by build.c */
  218. ram_size: .word 0 /* Obsolete */
  219. vid_mode: .word SVGA_MODE
  220. root_dev: .word 0 /* Filled in by build.c */
  221. boot_flag: .word 0xAA55
  222. # offset 512, entry point
  223. .globl _start
  224. _start:
  225. # Explicitly enter this as bytes, or the assembler
  226. # tries to generate a 3-byte jump here, which causes
  227. # everything else to push off to the wrong offset.
  228. .byte 0xeb # short (2-byte) jump
  229. .byte start_of_setup-1f
  230. 1:
  231. # Part 2 of the header, from the old setup.S
  232. .ascii "HdrS" # header signature
  233. .word 0x020a # header version number (>= 0x0105)
  234. # or else old loadlin-1.5 will fail)
  235. .globl realmode_swtch
  236. realmode_swtch: .word 0, 0 # default_switch, SETUPSEG
  237. start_sys_seg: .word SYSSEG # obsolete and meaningless, but just
  238. # in case something decided to "use" it
  239. .word kernel_version-512 # pointing to kernel version string
  240. # above section of header is compatible
  241. # with loadlin-1.5 (header v1.5). Don't
  242. # change it.
  243. type_of_loader: .byte 0 # 0 means ancient bootloader, newer
  244. # bootloaders know to change this.
  245. # See Documentation/x86/boot.txt for
  246. # assigned ids
  247. # flags, unused bits must be zero (RFU) bit within loadflags
  248. loadflags:
  249. LOADED_HIGH = 1 # If set, the kernel is loaded high
  250. CAN_USE_HEAP = 0x80 # If set, the loader also has set
  251. # heap_end_ptr to tell how much
  252. # space behind setup.S can be used for
  253. # heap purposes.
  254. # Only the loader knows what is free
  255. .byte LOADED_HIGH
  256. setup_move_size: .word 0x8000 # size to move, when setup is not
  257. # loaded at 0x90000. We will move setup
  258. # to 0x90000 then just before jumping
  259. # into the kernel. However, only the
  260. # loader knows how much data behind
  261. # us also needs to be loaded.
  262. code32_start: # here loaders can put a different
  263. # start address for 32-bit code.
  264. .long 0x100000 # 0x100000 = default for big kernel
  265. ramdisk_image: .long 0 # address of loaded ramdisk image
  266. # Here the loader puts the 32-bit
  267. # address where it loaded the image.
  268. # This only will be read by the kernel.
  269. ramdisk_size: .long 0 # its size in bytes
  270. bootsect_kludge:
  271. .long 0 # obsolete
  272. heap_end_ptr: .word _end+STACK_SIZE-512
  273. # (Header version 0x0201 or later)
  274. # space from here (exclusive) down to
  275. # end of setup code can be used by setup
  276. # for local heap purposes.
  277. ext_loader_ver:
  278. .byte 0 # Extended boot loader version
  279. ext_loader_type:
  280. .byte 0 # Extended boot loader type
  281. cmd_line_ptr: .long 0 # (Header version 0x0202 or later)
  282. # If nonzero, a 32-bit pointer
  283. # to the kernel command line.
  284. # The command line should be
  285. # located between the start of
  286. # setup and the end of low
  287. # memory (0xa0000), or it may
  288. # get overwritten before it
  289. # gets read. If this field is
  290. # used, there is no longer
  291. # anything magical about the
  292. # 0x90000 segment; the setup
  293. # can be located anywhere in
  294. # low memory 0x10000 or higher.
  295. ramdisk_max: .long 0x7fffffff
  296. # (Header version 0x0203 or later)
  297. # The highest safe address for
  298. # the contents of an initrd
  299. # The current kernel allows up to 4 GB,
  300. # but leave it at 2 GB to avoid
  301. # possible bootloader bugs.
  302. kernel_alignment: .long CONFIG_PHYSICAL_ALIGN #physical addr alignment
  303. #required for protected mode
  304. #kernel
  305. #ifdef CONFIG_RELOCATABLE
  306. relocatable_kernel: .byte 1
  307. #else
  308. relocatable_kernel: .byte 0
  309. #endif
  310. min_alignment: .byte MIN_KERNEL_ALIGN_LG2 # minimum alignment
  311. pad3: .word 0
  312. cmdline_size: .long COMMAND_LINE_SIZE-1 #length of the command line,
  313. #added with boot protocol
  314. #version 2.06
  315. hardware_subarch: .long 0 # subarchitecture, added with 2.07
  316. # default to 0 for normal x86 PC
  317. hardware_subarch_data: .quad 0
  318. payload_offset: .long ZO_input_data
  319. payload_length: .long ZO_z_input_len
  320. setup_data: .quad 0 # 64-bit physical pointer to
  321. # single linked list of
  322. # struct setup_data
  323. pref_address: .quad LOAD_PHYSICAL_ADDR # preferred load addr
  324. #define ZO_INIT_SIZE (ZO__end - ZO_startup_32 + ZO_z_extract_offset)
  325. #define VO_INIT_SIZE (VO__end - VO__text)
  326. #if ZO_INIT_SIZE > VO_INIT_SIZE
  327. #define INIT_SIZE ZO_INIT_SIZE
  328. #else
  329. #define INIT_SIZE VO_INIT_SIZE
  330. #endif
  331. init_size: .long INIT_SIZE # kernel initialization size
  332. # End of setup header #####################################################
  333. .section ".entrytext", "ax"
  334. start_of_setup:
  335. #ifdef SAFE_RESET_DISK_CONTROLLER
  336. # Reset the disk controller.
  337. movw $0x0000, %ax # Reset disk controller
  338. movb $0x80, %dl # All disks
  339. int $0x13
  340. #endif
  341. # Force %es = %ds
  342. movw %ds, %ax
  343. movw %ax, %es
  344. cld
  345. # Apparently some ancient versions of LILO invoked the kernel with %ss != %ds,
  346. # which happened to work by accident for the old code. Recalculate the stack
  347. # pointer if %ss is invalid. Otherwise leave it alone, LOADLIN sets up the
  348. # stack behind its own code, so we can't blindly put it directly past the heap.
  349. movw %ss, %dx
  350. cmpw %ax, %dx # %ds == %ss?
  351. movw %sp, %dx
  352. je 2f # -> assume %sp is reasonably set
  353. # Invalid %ss, make up a new stack
  354. movw $_end, %dx
  355. testb $CAN_USE_HEAP, loadflags
  356. jz 1f
  357. movw heap_end_ptr, %dx
  358. 1: addw $STACK_SIZE, %dx
  359. jnc 2f
  360. xorw %dx, %dx # Prevent wraparound
  361. 2: # Now %dx should point to the end of our stack space
  362. andw $~3, %dx # dword align (might as well...)
  363. jnz 3f
  364. movw $0xfffc, %dx # Make sure we're not zero
  365. 3: movw %ax, %ss
  366. movzwl %dx, %esp # Clear upper half of %esp
  367. sti # Now we should have a working stack
  368. # We will have entered with %cs = %ds+0x20, normalize %cs so
  369. # it is on par with the other segments.
  370. pushw %ds
  371. pushw $6f
  372. lretw
  373. 6:
  374. # Check signature at end of setup
  375. cmpl $0x5a5aaa55, setup_sig
  376. jne setup_bad
  377. # Zero the bss
  378. movw $__bss_start, %di
  379. movw $_end+3, %cx
  380. xorl %eax, %eax
  381. subw %di, %cx
  382. shrw $2, %cx
  383. rep; stosl
  384. # Jump to C code (should not return)
  385. calll main
  386. # Setup corrupt somehow...
  387. setup_bad:
  388. movl $setup_corrupt, %eax
  389. calll puts
  390. # Fall through...
  391. .globl die
  392. .type die, @function
  393. die:
  394. hlt
  395. jmp die
  396. .size die, .-die
  397. .section ".initdata", "a"
  398. setup_corrupt:
  399. .byte 7
  400. .string "No setup signature found...\n"
  401. .data
  402. dummy: .long 0
  403. .section .reloc
  404. reloc_start:
  405. .long dummy - reloc_start
  406. .long 10
  407. .word 0
  408. reloc_end: