header.S 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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 floppy boot is not supported. "
  83. .ascii "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 3 # 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 0x0200 # 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 0x20 # SectionAlignment
  139. .long 0x20 # 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 0x6 # 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. #
  177. # The offset & size fields are filled in by build.c.
  178. #
  179. .ascii ".setup"
  180. .byte 0
  181. .byte 0
  182. .long 0
  183. .long 0x0 # startup_{32,64}
  184. .long 0 # Size of initialized data
  185. # on disk
  186. .long 0x0 # startup_{32,64}
  187. .long 0 # PointerToRelocations
  188. .long 0 # PointerToLineNumbers
  189. .word 0 # NumberOfRelocations
  190. .word 0 # NumberOfLineNumbers
  191. .long 0x60500020 # Characteristics (section flags)
  192. #
  193. # The EFI application loader requires a relocation section
  194. # because EFI applications must be relocatable. The .reloc
  195. # offset & size fields are filled in by build.c.
  196. #
  197. .ascii ".reloc"
  198. .byte 0
  199. .byte 0
  200. .long 0
  201. .long 0
  202. .long 0 # SizeOfRawData
  203. .long 0 # PointerToRawData
  204. .long 0 # PointerToRelocations
  205. .long 0 # PointerToLineNumbers
  206. .word 0 # NumberOfRelocations
  207. .word 0 # NumberOfLineNumbers
  208. .long 0x42100040 # Characteristics (section flags)
  209. #
  210. # The offset & size fields are filled in by build.c.
  211. #
  212. .ascii ".text"
  213. .byte 0
  214. .byte 0
  215. .byte 0
  216. .long 0
  217. .long 0x0 # startup_{32,64}
  218. .long 0 # Size of initialized data
  219. # on disk
  220. .long 0x0 # startup_{32,64}
  221. .long 0 # PointerToRelocations
  222. .long 0 # PointerToLineNumbers
  223. .word 0 # NumberOfRelocations
  224. .word 0 # NumberOfLineNumbers
  225. .long 0x60500020 # Characteristics (section flags)
  226. #endif /* CONFIG_EFI_STUB */
  227. # Kernel attributes; used by setup. This is part 1 of the
  228. # header, from the old boot sector.
  229. .section ".header", "a"
  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 0x020b # 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. LOADED_HIGH = 1 # If set, the kernel is loaded high
  267. CAN_USE_HEAP = 0x80 # If set, the loader also has set
  268. # heap_end_ptr to tell how much
  269. # space behind setup.S can be used for
  270. # heap purposes.
  271. # Only the loader knows what is free
  272. .byte LOADED_HIGH
  273. setup_move_size: .word 0x8000 # size to move, when setup is not
  274. # loaded at 0x90000. We will move setup
  275. # to 0x90000 then just before jumping
  276. # into the kernel. However, only the
  277. # loader knows how much data behind
  278. # us also needs to be loaded.
  279. code32_start: # here loaders can put a different
  280. # start address for 32-bit code.
  281. .long 0x100000 # 0x100000 = default for big kernel
  282. ramdisk_image: .long 0 # address of loaded ramdisk image
  283. # Here the loader puts the 32-bit
  284. # address where it loaded the image.
  285. # This only will be read by the kernel.
  286. ramdisk_size: .long 0 # its size in bytes
  287. bootsect_kludge:
  288. .long 0 # obsolete
  289. heap_end_ptr: .word _end+STACK_SIZE-512
  290. # (Header version 0x0201 or later)
  291. # space from here (exclusive) down to
  292. # end of setup code can be used by setup
  293. # for local heap purposes.
  294. ext_loader_ver:
  295. .byte 0 # Extended boot loader version
  296. ext_loader_type:
  297. .byte 0 # Extended boot loader type
  298. cmd_line_ptr: .long 0 # (Header version 0x0202 or later)
  299. # If nonzero, a 32-bit pointer
  300. # to the kernel command line.
  301. # The command line should be
  302. # located between the start of
  303. # setup and the end of low
  304. # memory (0xa0000), or it may
  305. # get overwritten before it
  306. # gets read. If this field is
  307. # used, there is no longer
  308. # anything magical about the
  309. # 0x90000 segment; the setup
  310. # can be located anywhere in
  311. # low memory 0x10000 or higher.
  312. ramdisk_max: .long 0x7fffffff
  313. # (Header version 0x0203 or later)
  314. # The highest safe address for
  315. # the contents of an initrd
  316. # The current kernel allows up to 4 GB,
  317. # but leave it at 2 GB to avoid
  318. # possible bootloader bugs.
  319. kernel_alignment: .long CONFIG_PHYSICAL_ALIGN #physical addr alignment
  320. #required for protected mode
  321. #kernel
  322. #ifdef CONFIG_RELOCATABLE
  323. relocatable_kernel: .byte 1
  324. #else
  325. relocatable_kernel: .byte 0
  326. #endif
  327. min_alignment: .byte MIN_KERNEL_ALIGN_LG2 # minimum alignment
  328. pad3: .word 0
  329. cmdline_size: .long COMMAND_LINE_SIZE-1 #length of the command line,
  330. #added with boot protocol
  331. #version 2.06
  332. hardware_subarch: .long 0 # subarchitecture, added with 2.07
  333. # default to 0 for normal x86 PC
  334. hardware_subarch_data: .quad 0
  335. payload_offset: .long ZO_input_data
  336. payload_length: .long ZO_z_input_len
  337. setup_data: .quad 0 # 64-bit physical pointer to
  338. # single linked list of
  339. # struct setup_data
  340. pref_address: .quad LOAD_PHYSICAL_ADDR # preferred load addr
  341. #define ZO_INIT_SIZE (ZO__end - ZO_startup_32 + ZO_z_extract_offset)
  342. #define VO_INIT_SIZE (VO__end - VO__text)
  343. #if ZO_INIT_SIZE > VO_INIT_SIZE
  344. #define INIT_SIZE ZO_INIT_SIZE
  345. #else
  346. #define INIT_SIZE VO_INIT_SIZE
  347. #endif
  348. init_size: .long INIT_SIZE # kernel initialization size
  349. handover_offset: .long 0x30 # offset to the handover
  350. # protocol entry point
  351. # End of setup header #####################################################
  352. .section ".entrytext", "ax"
  353. start_of_setup:
  354. # Force %es = %ds
  355. movw %ds, %ax
  356. movw %ax, %es
  357. cld
  358. # Apparently some ancient versions of LILO invoked the kernel with %ss != %ds,
  359. # which happened to work by accident for the old code. Recalculate the stack
  360. # pointer if %ss is invalid. Otherwise leave it alone, LOADLIN sets up the
  361. # stack behind its own code, so we can't blindly put it directly past the heap.
  362. movw %ss, %dx
  363. cmpw %ax, %dx # %ds == %ss?
  364. movw %sp, %dx
  365. je 2f # -> assume %sp is reasonably set
  366. # Invalid %ss, make up a new stack
  367. movw $_end, %dx
  368. testb $CAN_USE_HEAP, loadflags
  369. jz 1f
  370. movw heap_end_ptr, %dx
  371. 1: addw $STACK_SIZE, %dx
  372. jnc 2f
  373. xorw %dx, %dx # Prevent wraparound
  374. 2: # Now %dx should point to the end of our stack space
  375. andw $~3, %dx # dword align (might as well...)
  376. jnz 3f
  377. movw $0xfffc, %dx # Make sure we're not zero
  378. 3: movw %ax, %ss
  379. movzwl %dx, %esp # Clear upper half of %esp
  380. sti # Now we should have a working stack
  381. # We will have entered with %cs = %ds+0x20, normalize %cs so
  382. # it is on par with the other segments.
  383. pushw %ds
  384. pushw $6f
  385. lretw
  386. 6:
  387. # Check signature at end of setup
  388. cmpl $0x5a5aaa55, setup_sig
  389. jne setup_bad
  390. # Zero the bss
  391. movw $__bss_start, %di
  392. movw $_end+3, %cx
  393. xorl %eax, %eax
  394. subw %di, %cx
  395. shrw $2, %cx
  396. rep; stosl
  397. # Jump to C code (should not return)
  398. calll main
  399. # Setup corrupt somehow...
  400. setup_bad:
  401. movl $setup_corrupt, %eax
  402. calll puts
  403. # Fall through...
  404. .globl die
  405. .type die, @function
  406. die:
  407. hlt
  408. jmp die
  409. .size die, .-die
  410. .section ".initdata", "a"
  411. setup_corrupt:
  412. .byte 7
  413. .string "No setup signature found...\n"
  414. .data
  415. dummy: .long 0