header.S 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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 "Use a boot loader.\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 4 # 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. #
  224. # The offset & size fields are filled in by build.c.
  225. #
  226. .ascii ".bss"
  227. .byte 0
  228. .byte 0
  229. .byte 0
  230. .byte 0
  231. .long 0
  232. .long 0x0
  233. .long 0 # Size of initialized data
  234. # on disk
  235. .long 0x0
  236. .long 0 # PointerToRelocations
  237. .long 0 # PointerToLineNumbers
  238. .word 0 # NumberOfRelocations
  239. .word 0 # NumberOfLineNumbers
  240. .long 0xc8000080 # Characteristics (section flags)
  241. #endif /* CONFIG_EFI_STUB */
  242. # Kernel attributes; used by setup. This is part 1 of the
  243. # header, from the old boot sector.
  244. .section ".header", "a"
  245. .globl sentinel
  246. sentinel: .byte 0xff, 0xff /* Used to detect broken loaders */
  247. .globl hdr
  248. hdr:
  249. setup_sects: .byte 0 /* Filled in by build.c */
  250. root_flags: .word ROOT_RDONLY
  251. syssize: .long 0 /* Filled in by build.c */
  252. ram_size: .word 0 /* Obsolete */
  253. vid_mode: .word SVGA_MODE
  254. root_dev: .word 0 /* Filled in by build.c */
  255. boot_flag: .word 0xAA55
  256. # offset 512, entry point
  257. .globl _start
  258. _start:
  259. # Explicitly enter this as bytes, or the assembler
  260. # tries to generate a 3-byte jump here, which causes
  261. # everything else to push off to the wrong offset.
  262. .byte 0xeb # short (2-byte) jump
  263. .byte start_of_setup-1f
  264. 1:
  265. # Part 2 of the header, from the old setup.S
  266. .ascii "HdrS" # header signature
  267. .word 0x020d # header version number (>= 0x0105)
  268. # or else old loadlin-1.5 will fail)
  269. .globl realmode_swtch
  270. realmode_swtch: .word 0, 0 # default_switch, SETUPSEG
  271. start_sys_seg: .word SYSSEG # obsolete and meaningless, but just
  272. # in case something decided to "use" it
  273. .word kernel_version-512 # pointing to kernel version string
  274. # above section of header is compatible
  275. # with loadlin-1.5 (header v1.5). Don't
  276. # change it.
  277. type_of_loader: .byte 0 # 0 means ancient bootloader, newer
  278. # bootloaders know to change this.
  279. # See Documentation/x86/boot.txt for
  280. # assigned ids
  281. # flags, unused bits must be zero (RFU) bit within loadflags
  282. loadflags:
  283. .byte LOADED_HIGH # The kernel is to be loaded high
  284. setup_move_size: .word 0x8000 # size to move, when setup is not
  285. # loaded at 0x90000. We will move setup
  286. # to 0x90000 then just before jumping
  287. # into the kernel. However, only the
  288. # loader knows how much data behind
  289. # us also needs to be loaded.
  290. code32_start: # here loaders can put a different
  291. # start address for 32-bit code.
  292. .long 0x100000 # 0x100000 = default for big kernel
  293. ramdisk_image: .long 0 # address of loaded ramdisk image
  294. # Here the loader puts the 32-bit
  295. # address where it loaded the image.
  296. # This only will be read by the kernel.
  297. ramdisk_size: .long 0 # its size in bytes
  298. bootsect_kludge:
  299. .long 0 # obsolete
  300. heap_end_ptr: .word _end+STACK_SIZE-512
  301. # (Header version 0x0201 or later)
  302. # space from here (exclusive) down to
  303. # end of setup code can be used by setup
  304. # for local heap purposes.
  305. ext_loader_ver:
  306. .byte 0 # Extended boot loader version
  307. ext_loader_type:
  308. .byte 0 # Extended boot loader type
  309. cmd_line_ptr: .long 0 # (Header version 0x0202 or later)
  310. # If nonzero, a 32-bit pointer
  311. # to the kernel command line.
  312. # The command line should be
  313. # located between the start of
  314. # setup and the end of low
  315. # memory (0xa0000), or it may
  316. # get overwritten before it
  317. # gets read. If this field is
  318. # used, there is no longer
  319. # anything magical about the
  320. # 0x90000 segment; the setup
  321. # can be located anywhere in
  322. # low memory 0x10000 or higher.
  323. initrd_addr_max: .long 0x7fffffff
  324. # (Header version 0x0203 or later)
  325. # The highest safe address for
  326. # the contents of an initrd
  327. # The current kernel allows up to 4 GB,
  328. # but leave it at 2 GB to avoid
  329. # possible bootloader bugs.
  330. kernel_alignment: .long CONFIG_PHYSICAL_ALIGN #physical addr alignment
  331. #required for protected mode
  332. #kernel
  333. #ifdef CONFIG_RELOCATABLE
  334. relocatable_kernel: .byte 1
  335. #else
  336. relocatable_kernel: .byte 0
  337. #endif
  338. min_alignment: .byte MIN_KERNEL_ALIGN_LG2 # minimum alignment
  339. xloadflags:
  340. #ifdef CONFIG_X86_64
  341. # define XLF0 XLF_KERNEL_64 /* 64-bit kernel */
  342. #else
  343. # define XLF0 0
  344. #endif
  345. #if defined(CONFIG_RELOCATABLE) && defined(CONFIG_X86_64)
  346. /* kernel/boot_param/ramdisk could be loaded above 4g */
  347. # define XLF1 XLF_CAN_BE_LOADED_ABOVE_4G
  348. #else
  349. # define XLF1 0
  350. #endif
  351. #ifdef CONFIG_EFI_STUB
  352. # ifdef CONFIG_EFI_MIXED
  353. # define XLF23 (XLF_EFI_HANDOVER_32|XLF_EFI_HANDOVER_64)
  354. # else
  355. # ifdef CONFIG_X86_64
  356. # define XLF23 XLF_EFI_HANDOVER_64 /* 64-bit EFI handover ok */
  357. # else
  358. # define XLF23 XLF_EFI_HANDOVER_32 /* 32-bit EFI handover ok */
  359. # endif
  360. # endif
  361. #else
  362. # define XLF23 0
  363. #endif
  364. #if defined(CONFIG_X86_64) && defined(CONFIG_EFI) && defined(CONFIG_KEXEC_CORE)
  365. # define XLF4 XLF_EFI_KEXEC
  366. #else
  367. # define XLF4 0
  368. #endif
  369. .word XLF0 | XLF1 | XLF23 | XLF4
  370. cmdline_size: .long COMMAND_LINE_SIZE-1 #length of the command line,
  371. #added with boot protocol
  372. #version 2.06
  373. hardware_subarch: .long 0 # subarchitecture, added with 2.07
  374. # default to 0 for normal x86 PC
  375. hardware_subarch_data: .quad 0
  376. payload_offset: .long ZO_input_data
  377. payload_length: .long ZO_z_input_len
  378. setup_data: .quad 0 # 64-bit physical pointer to
  379. # single linked list of
  380. # struct setup_data
  381. pref_address: .quad LOAD_PHYSICAL_ADDR # preferred load addr
  382. #
  383. # Getting to provably safe in-place decompression is hard. Worst case
  384. # behaviours need to be analyzed. Here let's take the decompression of
  385. # a gzip-compressed kernel as example, to illustrate it:
  386. #
  387. # The file layout of gzip compressed kernel is:
  388. #
  389. # magic[2]
  390. # method[1]
  391. # flags[1]
  392. # timestamp[4]
  393. # extraflags[1]
  394. # os[1]
  395. # compressed data blocks[N]
  396. # crc[4] orig_len[4]
  397. #
  398. # ... resulting in +18 bytes overhead of uncompressed data.
  399. #
  400. # (For more information, please refer to RFC 1951 and RFC 1952.)
  401. #
  402. # Files divided into blocks
  403. # 1 bit (last block flag)
  404. # 2 bits (block type)
  405. #
  406. # 1 block occurs every 32K -1 bytes or when there 50% compression
  407. # has been achieved. The smallest block type encoding is always used.
  408. #
  409. # stored:
  410. # 32 bits length in bytes.
  411. #
  412. # fixed:
  413. # magic fixed tree.
  414. # symbols.
  415. #
  416. # dynamic:
  417. # dynamic tree encoding.
  418. # symbols.
  419. #
  420. #
  421. # The buffer for decompression in place is the length of the uncompressed
  422. # data, plus a small amount extra to keep the algorithm safe. The
  423. # compressed data is placed at the end of the buffer. The output pointer
  424. # is placed at the start of the buffer and the input pointer is placed
  425. # where the compressed data starts. Problems will occur when the output
  426. # pointer overruns the input pointer.
  427. #
  428. # The output pointer can only overrun the input pointer if the input
  429. # pointer is moving faster than the output pointer. A condition only
  430. # triggered by data whose compressed form is larger than the uncompressed
  431. # form.
  432. #
  433. # The worst case at the block level is a growth of the compressed data
  434. # of 5 bytes per 32767 bytes.
  435. #
  436. # The worst case internal to a compressed block is very hard to figure.
  437. # The worst case can at least be bounded by having one bit that represents
  438. # 32764 bytes and then all of the rest of the bytes representing the very
  439. # very last byte.
  440. #
  441. # All of which is enough to compute an amount of extra data that is required
  442. # to be safe. To avoid problems at the block level allocating 5 extra bytes
  443. # per 32767 bytes of data is sufficient. To avoid problems internal to a
  444. # block adding an extra 32767 bytes (the worst case uncompressed block size)
  445. # is sufficient, to ensure that in the worst case the decompressed data for
  446. # block will stop the byte before the compressed data for a block begins.
  447. # To avoid problems with the compressed data's meta information an extra 18
  448. # bytes are needed. Leading to the formula:
  449. #
  450. # extra_bytes = (uncompressed_size >> 12) + 32768 + 18
  451. #
  452. # Adding 8 bytes per 32K is a bit excessive but much easier to calculate.
  453. # Adding 32768 instead of 32767 just makes for round numbers.
  454. #
  455. # Above analysis is for decompressing gzip compressed kernel only. Up to
  456. # now 6 different decompressor are supported all together. And among them
  457. # xz stores data in chunks and has maximum chunk of 64K. Hence safety
  458. # margin should be updated to cover all decompressors so that we don't
  459. # need to deal with each of them separately. Please check
  460. # the description in lib/decompressor_xxx.c for specific information.
  461. #
  462. # extra_bytes = (uncompressed_size >> 12) + 65536 + 128
  463. #define ZO_z_extra_bytes ((ZO_z_output_len >> 12) + 65536 + 128)
  464. #if ZO_z_output_len > ZO_z_input_len
  465. # define ZO_z_extract_offset (ZO_z_output_len + ZO_z_extra_bytes - \
  466. ZO_z_input_len)
  467. #else
  468. # define ZO_z_extract_offset ZO_z_extra_bytes
  469. #endif
  470. /*
  471. * The extract_offset has to be bigger than ZO head section. Otherwise when
  472. * the head code is running to move ZO to the end of the buffer, it will
  473. * overwrite the head code itself.
  474. */
  475. #if (ZO__ehead - ZO_startup_32) > ZO_z_extract_offset
  476. # define ZO_z_min_extract_offset ((ZO__ehead - ZO_startup_32 + 4095) & ~4095)
  477. #else
  478. # define ZO_z_min_extract_offset ((ZO_z_extract_offset + 4095) & ~4095)
  479. #endif
  480. #define ZO_INIT_SIZE (ZO__end - ZO_startup_32 + ZO_z_min_extract_offset)
  481. #define VO_INIT_SIZE (VO__end - VO__text)
  482. #if ZO_INIT_SIZE > VO_INIT_SIZE
  483. # define INIT_SIZE ZO_INIT_SIZE
  484. #else
  485. # define INIT_SIZE VO_INIT_SIZE
  486. #endif
  487. init_size: .long INIT_SIZE # kernel initialization size
  488. handover_offset: .long 0 # Filled in by build.c
  489. # End of setup header #####################################################
  490. .section ".entrytext", "ax"
  491. start_of_setup:
  492. # Force %es = %ds
  493. movw %ds, %ax
  494. movw %ax, %es
  495. cld
  496. # Apparently some ancient versions of LILO invoked the kernel with %ss != %ds,
  497. # which happened to work by accident for the old code. Recalculate the stack
  498. # pointer if %ss is invalid. Otherwise leave it alone, LOADLIN sets up the
  499. # stack behind its own code, so we can't blindly put it directly past the heap.
  500. movw %ss, %dx
  501. cmpw %ax, %dx # %ds == %ss?
  502. movw %sp, %dx
  503. je 2f # -> assume %sp is reasonably set
  504. # Invalid %ss, make up a new stack
  505. movw $_end, %dx
  506. testb $CAN_USE_HEAP, loadflags
  507. jz 1f
  508. movw heap_end_ptr, %dx
  509. 1: addw $STACK_SIZE, %dx
  510. jnc 2f
  511. xorw %dx, %dx # Prevent wraparound
  512. 2: # Now %dx should point to the end of our stack space
  513. andw $~3, %dx # dword align (might as well...)
  514. jnz 3f
  515. movw $0xfffc, %dx # Make sure we're not zero
  516. 3: movw %ax, %ss
  517. movzwl %dx, %esp # Clear upper half of %esp
  518. sti # Now we should have a working stack
  519. # We will have entered with %cs = %ds+0x20, normalize %cs so
  520. # it is on par with the other segments.
  521. pushw %ds
  522. pushw $6f
  523. lretw
  524. 6:
  525. # Check signature at end of setup
  526. cmpl $0x5a5aaa55, setup_sig
  527. jne setup_bad
  528. # Zero the bss
  529. movw $__bss_start, %di
  530. movw $_end+3, %cx
  531. xorl %eax, %eax
  532. subw %di, %cx
  533. shrw $2, %cx
  534. rep; stosl
  535. # Jump to C code (should not return)
  536. calll main
  537. # Setup corrupt somehow...
  538. setup_bad:
  539. movl $setup_corrupt, %eax
  540. calll puts
  541. # Fall through...
  542. .globl die
  543. .type die, @function
  544. die:
  545. hlt
  546. jmp die
  547. .size die, .-die
  548. .section ".initdata", "a"
  549. setup_corrupt:
  550. .byte 7
  551. .string "No setup signature found...\n"