header.S 12 KB

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