header.S 12 KB

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