header.S 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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 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 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 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. .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 must be relocatable. But since
  193. # we don't need the loader to fixup any relocs for us, we
  194. # just create an empty (zero-length) .reloc section header.
  195. #
  196. .ascii ".reloc"
  197. .byte 0
  198. .byte 0
  199. .long 0
  200. .long 0
  201. .long 0 # SizeOfRawData
  202. .long 0 # PointerToRawData
  203. .long 0 # PointerToRelocations
  204. .long 0 # PointerToLineNumbers
  205. .word 0 # NumberOfRelocations
  206. .word 0 # NumberOfLineNumbers
  207. .long 0x42100040 # Characteristics (section flags)
  208. #endif /* CONFIG_EFI_STUB */
  209. # Kernel attributes; used by setup. This is part 1 of the
  210. # header, from the old boot sector.
  211. .section ".header", "a"
  212. .globl hdr
  213. hdr:
  214. setup_sects: .byte 0 /* Filled in by build.c */
  215. root_flags: .word ROOT_RDONLY
  216. syssize: .long 0 /* Filled in by build.c */
  217. ram_size: .word 0 /* Obsolete */
  218. vid_mode: .word SVGA_MODE
  219. root_dev: .word 0 /* Filled in by build.c */
  220. boot_flag: .word 0xAA55
  221. # offset 512, entry point
  222. .globl _start
  223. _start:
  224. # Explicitly enter this as bytes, or the assembler
  225. # tries to generate a 3-byte jump here, which causes
  226. # everything else to push off to the wrong offset.
  227. .byte 0xeb # short (2-byte) jump
  228. .byte start_of_setup-1f
  229. 1:
  230. # Part 2 of the header, from the old setup.S
  231. .ascii "HdrS" # header signature
  232. .word 0x020a # header version number (>= 0x0105)
  233. # or else old loadlin-1.5 will fail)
  234. .globl realmode_swtch
  235. realmode_swtch: .word 0, 0 # default_switch, SETUPSEG
  236. start_sys_seg: .word SYSSEG # obsolete and meaningless, but just
  237. # in case something decided to "use" it
  238. .word kernel_version-512 # pointing to kernel version string
  239. # above section of header is compatible
  240. # with loadlin-1.5 (header v1.5). Don't
  241. # change it.
  242. type_of_loader: .byte 0 # 0 means ancient bootloader, newer
  243. # bootloaders know to change this.
  244. # See Documentation/x86/boot.txt for
  245. # assigned ids
  246. # flags, unused bits must be zero (RFU) bit within loadflags
  247. loadflags:
  248. LOADED_HIGH = 1 # If set, the kernel is loaded high
  249. CAN_USE_HEAP = 0x80 # If set, the loader also has set
  250. # heap_end_ptr to tell how much
  251. # space behind setup.S can be used for
  252. # heap purposes.
  253. # Only the loader knows what is free
  254. .byte LOADED_HIGH
  255. setup_move_size: .word 0x8000 # size to move, when setup is not
  256. # loaded at 0x90000. We will move setup
  257. # to 0x90000 then just before jumping
  258. # into the kernel. However, only the
  259. # loader knows how much data behind
  260. # us also needs to be loaded.
  261. code32_start: # here loaders can put a different
  262. # start address for 32-bit code.
  263. .long 0x100000 # 0x100000 = default for big kernel
  264. ramdisk_image: .long 0 # address of loaded ramdisk image
  265. # Here the loader puts the 32-bit
  266. # address where it loaded the image.
  267. # This only will be read by the kernel.
  268. ramdisk_size: .long 0 # its size in bytes
  269. bootsect_kludge:
  270. .long 0 # obsolete
  271. heap_end_ptr: .word _end+STACK_SIZE-512
  272. # (Header version 0x0201 or later)
  273. # space from here (exclusive) down to
  274. # end of setup code can be used by setup
  275. # for local heap purposes.
  276. ext_loader_ver:
  277. .byte 0 # Extended boot loader version
  278. ext_loader_type:
  279. .byte 0 # Extended boot loader type
  280. cmd_line_ptr: .long 0 # (Header version 0x0202 or later)
  281. # If nonzero, a 32-bit pointer
  282. # to the kernel command line.
  283. # The command line should be
  284. # located between the start of
  285. # setup and the end of low
  286. # memory (0xa0000), or it may
  287. # get overwritten before it
  288. # gets read. If this field is
  289. # used, there is no longer
  290. # anything magical about the
  291. # 0x90000 segment; the setup
  292. # can be located anywhere in
  293. # low memory 0x10000 or higher.
  294. ramdisk_max: .long 0x7fffffff
  295. # (Header version 0x0203 or later)
  296. # The highest safe address for
  297. # the contents of an initrd
  298. # The current kernel allows up to 4 GB,
  299. # but leave it at 2 GB to avoid
  300. # possible bootloader bugs.
  301. kernel_alignment: .long CONFIG_PHYSICAL_ALIGN #physical addr alignment
  302. #required for protected mode
  303. #kernel
  304. #ifdef CONFIG_RELOCATABLE
  305. relocatable_kernel: .byte 1
  306. #else
  307. relocatable_kernel: .byte 0
  308. #endif
  309. min_alignment: .byte MIN_KERNEL_ALIGN_LG2 # minimum alignment
  310. pad3: .word 0
  311. cmdline_size: .long COMMAND_LINE_SIZE-1 #length of the command line,
  312. #added with boot protocol
  313. #version 2.06
  314. hardware_subarch: .long 0 # subarchitecture, added with 2.07
  315. # default to 0 for normal x86 PC
  316. hardware_subarch_data: .quad 0
  317. payload_offset: .long ZO_input_data
  318. payload_length: .long ZO_z_input_len
  319. setup_data: .quad 0 # 64-bit physical pointer to
  320. # single linked list of
  321. # struct setup_data
  322. pref_address: .quad LOAD_PHYSICAL_ADDR # preferred load addr
  323. #define ZO_INIT_SIZE (ZO__end - ZO_startup_32 + ZO_z_extract_offset)
  324. #define VO_INIT_SIZE (VO__end - VO__text)
  325. #if ZO_INIT_SIZE > VO_INIT_SIZE
  326. #define INIT_SIZE ZO_INIT_SIZE
  327. #else
  328. #define INIT_SIZE VO_INIT_SIZE
  329. #endif
  330. init_size: .long INIT_SIZE # kernel initialization size
  331. # End of setup header #####################################################
  332. .section ".entrytext", "ax"
  333. start_of_setup:
  334. # Force %es = %ds
  335. movw %ds, %ax
  336. movw %ax, %es
  337. cld
  338. # Apparently some ancient versions of LILO invoked the kernel with %ss != %ds,
  339. # which happened to work by accident for the old code. Recalculate the stack
  340. # pointer if %ss is invalid. Otherwise leave it alone, LOADLIN sets up the
  341. # stack behind its own code, so we can't blindly put it directly past the heap.
  342. movw %ss, %dx
  343. cmpw %ax, %dx # %ds == %ss?
  344. movw %sp, %dx
  345. je 2f # -> assume %sp is reasonably set
  346. # Invalid %ss, make up a new stack
  347. movw $_end, %dx
  348. testb $CAN_USE_HEAP, loadflags
  349. jz 1f
  350. movw heap_end_ptr, %dx
  351. 1: addw $STACK_SIZE, %dx
  352. jnc 2f
  353. xorw %dx, %dx # Prevent wraparound
  354. 2: # Now %dx should point to the end of our stack space
  355. andw $~3, %dx # dword align (might as well...)
  356. jnz 3f
  357. movw $0xfffc, %dx # Make sure we're not zero
  358. 3: movw %ax, %ss
  359. movzwl %dx, %esp # Clear upper half of %esp
  360. sti # Now we should have a working stack
  361. # We will have entered with %cs = %ds+0x20, normalize %cs so
  362. # it is on par with the other segments.
  363. pushw %ds
  364. pushw $6f
  365. lretw
  366. 6:
  367. # Check signature at end of setup
  368. cmpl $0x5a5aaa55, setup_sig
  369. jne setup_bad
  370. # Zero the bss
  371. movw $__bss_start, %di
  372. movw $_end+3, %cx
  373. xorl %eax, %eax
  374. subw %di, %cx
  375. shrw $2, %cx
  376. rep; stosl
  377. # Jump to C code (should not return)
  378. calll main
  379. # Setup corrupt somehow...
  380. setup_bad:
  381. movl $setup_corrupt, %eax
  382. calll puts
  383. # Fall through...
  384. .globl die
  385. .type die, @function
  386. die:
  387. hlt
  388. jmp die
  389. .size die, .-die
  390. .section ".initdata", "a"
  391. setup_corrupt:
  392. .byte 7
  393. .string "No setup signature found...\n"
  394. .data
  395. dummy: .long 0