header.S 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * header.S
  4. *
  5. * Copyright (C) 1991, 1992 Linus Torvalds
  6. *
  7. * Based on bootsect.S and setup.S
  8. * modified by more people than can be counted
  9. *
  10. * Rewritten as a common file by H. Peter Anvin (Apr 2007)
  11. *
  12. * BIG FAT NOTE: We're in real mode using 64k segments. Therefore segment
  13. * addresses must be multiplied by 16 to obtain their respective linear
  14. * addresses. To avoid confusion, linear addresses are written using leading
  15. * hex while segment addresses are written as segment:offset.
  16. *
  17. */
  18. #include <linux/pe.h>
  19. #include <asm/segment.h>
  20. #include <asm/boot.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. .set salign, 0x1000
  36. .set falign, 0x200
  37. .code16
  38. .section ".bstext", "ax"
  39. #ifdef CONFIG_EFI_STUB
  40. # "MZ", MS-DOS header
  41. .word MZ_MAGIC
  42. .org 0x38
  43. #
  44. # Offset to the PE header.
  45. #
  46. .long LINUX_PE_MAGIC
  47. .long pe_header
  48. pe_header:
  49. .long PE_MAGIC
  50. coff_header:
  51. #ifdef CONFIG_X86_32
  52. .set image_file_add_flags, IMAGE_FILE_32BIT_MACHINE
  53. .set pe_opt_magic, PE_OPT_MAGIC_PE32
  54. .word IMAGE_FILE_MACHINE_I386
  55. #else
  56. .set image_file_add_flags, 0
  57. .set pe_opt_magic, PE_OPT_MAGIC_PE32PLUS
  58. .word IMAGE_FILE_MACHINE_AMD64
  59. #endif
  60. .word section_count # nr_sections
  61. .long 0 # TimeDateStamp
  62. .long 0 # PointerToSymbolTable
  63. .long 1 # NumberOfSymbols
  64. .word section_table - optional_header # SizeOfOptionalHeader
  65. .word IMAGE_FILE_EXECUTABLE_IMAGE | \
  66. image_file_add_flags | \
  67. IMAGE_FILE_DEBUG_STRIPPED | \
  68. IMAGE_FILE_LINE_NUMS_STRIPPED # Characteristics
  69. optional_header:
  70. .word pe_opt_magic
  71. .byte 0x02 # MajorLinkerVersion
  72. .byte 0x14 # MinorLinkerVersion
  73. .long ZO__data # SizeOfCode
  74. .long ZO__end - ZO__data # SizeOfInitializedData
  75. .long 0 # SizeOfUninitializedData
  76. .long setup_size + ZO_efi_pe_entry # AddressOfEntryPoint
  77. .long setup_size # BaseOfCode
  78. #ifdef CONFIG_X86_32
  79. .long 0 # data
  80. #endif
  81. extra_header_fields:
  82. #ifdef CONFIG_X86_32
  83. .long 0 # ImageBase
  84. #else
  85. .quad 0 # ImageBase
  86. #endif
  87. .long salign # SectionAlignment
  88. .long falign # FileAlignment
  89. .word 0 # MajorOperatingSystemVersion
  90. .word 0 # MinorOperatingSystemVersion
  91. .word LINUX_EFISTUB_MAJOR_VERSION # MajorImageVersion
  92. .word LINUX_EFISTUB_MINOR_VERSION # MinorImageVersion
  93. .word 0 # MajorSubsystemVersion
  94. .word 0 # MinorSubsystemVersion
  95. .long 0 # Win32VersionValue
  96. .long setup_size + ZO__end # SizeOfImage
  97. .long salign # SizeOfHeaders
  98. .long 0 # CheckSum
  99. .word IMAGE_SUBSYSTEM_EFI_APPLICATION # Subsystem (EFI application)
  100. .word IMAGE_DLL_CHARACTERISTICS_NX_COMPAT # DllCharacteristics
  101. #ifdef CONFIG_X86_32
  102. .long 0 # SizeOfStackReserve
  103. .long 0 # SizeOfStackCommit
  104. .long 0 # SizeOfHeapReserve
  105. .long 0 # SizeOfHeapCommit
  106. #else
  107. .quad 0 # SizeOfStackReserve
  108. .quad 0 # SizeOfStackCommit
  109. .quad 0 # SizeOfHeapReserve
  110. .quad 0 # SizeOfHeapCommit
  111. #endif
  112. .long 0 # LoaderFlags
  113. .long (section_table - .) / 8 # NumberOfRvaAndSizes
  114. .quad 0 # ExportTable
  115. .quad 0 # ImportTable
  116. .quad 0 # ResourceTable
  117. .quad 0 # ExceptionTable
  118. .quad 0 # CertificationTable
  119. .quad 0 # BaseRelocationTable
  120. # Section table
  121. section_table:
  122. .ascii ".setup"
  123. .byte 0
  124. .byte 0
  125. .long pecompat_fstart - salign # VirtualSize
  126. .long salign # VirtualAddress
  127. .long pecompat_fstart - salign # SizeOfRawData
  128. .long salign # PointerToRawData
  129. .long 0, 0, 0
  130. .long IMAGE_SCN_CNT_INITIALIZED_DATA | \
  131. IMAGE_SCN_MEM_READ | \
  132. IMAGE_SCN_MEM_DISCARDABLE # Characteristics
  133. #ifdef CONFIG_EFI_MIXED
  134. .asciz ".compat"
  135. .long pecompat_fsize # VirtualSize
  136. .long pecompat_fstart # VirtualAddress
  137. .long pecompat_fsize # SizeOfRawData
  138. .long pecompat_fstart # PointerToRawData
  139. .long 0, 0, 0
  140. .long IMAGE_SCN_CNT_INITIALIZED_DATA | \
  141. IMAGE_SCN_MEM_READ | \
  142. IMAGE_SCN_MEM_DISCARDABLE # Characteristics
  143. /*
  144. * Put the IA-32 machine type and the associated entry point address in
  145. * the .compat section, so loaders can figure out which other execution
  146. * modes this image supports.
  147. */
  148. .pushsection ".pecompat", "a", @progbits
  149. .balign salign
  150. .globl pecompat_fstart
  151. pecompat_fstart:
  152. .byte 0x1 # Version
  153. .byte 8 # Size
  154. .word IMAGE_FILE_MACHINE_I386 # PE machine type
  155. .long setup_size + ZO_efi32_pe_entry # Entrypoint
  156. .byte 0x0 # Sentinel
  157. .popsection
  158. #else
  159. .set pecompat_fstart, setup_size
  160. #endif
  161. .ascii ".text"
  162. .byte 0
  163. .byte 0
  164. .byte 0
  165. .long ZO__data
  166. .long setup_size
  167. .long ZO__data # Size of initialized data
  168. # on disk
  169. .long setup_size
  170. .long 0 # PointerToRelocations
  171. .long 0 # PointerToLineNumbers
  172. .word 0 # NumberOfRelocations
  173. .word 0 # NumberOfLineNumbers
  174. .long IMAGE_SCN_CNT_CODE | \
  175. IMAGE_SCN_MEM_READ | \
  176. IMAGE_SCN_MEM_EXECUTE # Characteristics
  177. .ascii ".data\0\0\0"
  178. .long ZO__end - ZO__data # VirtualSize
  179. .long setup_size + ZO__data # VirtualAddress
  180. .long ZO__edata - ZO__data # SizeOfRawData
  181. .long setup_size + ZO__data # PointerToRawData
  182. .long 0, 0, 0
  183. .long IMAGE_SCN_CNT_INITIALIZED_DATA | \
  184. IMAGE_SCN_MEM_READ | \
  185. IMAGE_SCN_MEM_WRITE # Characteristics
  186. .set section_count, (. - section_table) / 40
  187. #endif /* CONFIG_EFI_STUB */
  188. # Kernel attributes; used by setup. This is part 1 of the
  189. # header, from the old boot sector.
  190. .section ".header", "a"
  191. .globl sentinel
  192. sentinel: .byte 0xff, 0xff /* Used to detect broken loaders */
  193. .globl hdr
  194. hdr:
  195. .byte setup_sects - 1
  196. root_flags: .word ROOT_RDONLY
  197. syssize: .long ZO__edata / 16
  198. ram_size: .word 0 /* Obsolete */
  199. vid_mode: .word SVGA_MODE
  200. root_dev: .word 0 /* Default to major/minor 0/0 */
  201. boot_flag: .word 0xAA55
  202. # offset 512, entry point
  203. .globl _start
  204. _start:
  205. # Explicitly enter this as bytes, or the assembler
  206. # tries to generate a 3-byte jump here, which causes
  207. # everything else to push off to the wrong offset.
  208. .byte 0xeb # short (2-byte) jump
  209. .byte start_of_setup-1f
  210. 1:
  211. # Part 2 of the header, from the old setup.S
  212. .ascii "HdrS" # header signature
  213. .word 0x020f # header version number (>= 0x0105)
  214. # or else old loadlin-1.5 will fail)
  215. .globl realmode_swtch
  216. realmode_swtch: .word 0, 0 # default_switch, SETUPSEG
  217. start_sys_seg: .word SYSSEG # obsolete and meaningless, but just
  218. # in case something decided to "use" it
  219. .word kernel_version-512 # pointing to kernel version string
  220. # above section of header is compatible
  221. # with loadlin-1.5 (header v1.5). Don't
  222. # change it.
  223. type_of_loader: .byte 0 # 0 means ancient bootloader, newer
  224. # bootloaders know to change this.
  225. # See Documentation/arch/x86/boot.rst for
  226. # assigned ids
  227. # flags, unused bits must be zero (RFU) bit within loadflags
  228. loadflags:
  229. .byte LOADED_HIGH # The kernel is to be loaded high
  230. setup_move_size: .word 0x8000 # size to move, when setup is not
  231. # loaded at 0x90000. We will move setup
  232. # to 0x90000 then just before jumping
  233. # into the kernel. However, only the
  234. # loader knows how much data behind
  235. # us also needs to be loaded.
  236. code32_start: # here loaders can put a different
  237. # start address for 32-bit code.
  238. .long 0x100000 # 0x100000 = default for big kernel
  239. ramdisk_image: .long 0 # address of loaded ramdisk image
  240. # Here the loader puts the 32-bit
  241. # address where it loaded the image.
  242. # This only will be read by the kernel.
  243. ramdisk_size: .long 0 # its size in bytes
  244. bootsect_kludge:
  245. .long 0 # obsolete
  246. heap_end_ptr: .word _end+STACK_SIZE-512
  247. # (Header version 0x0201 or later)
  248. # space from here (exclusive) down to
  249. # end of setup code can be used by setup
  250. # for local heap purposes.
  251. ext_loader_ver:
  252. .byte 0 # Extended boot loader version
  253. ext_loader_type:
  254. .byte 0 # Extended boot loader type
  255. cmd_line_ptr: .long 0 # (Header version 0x0202 or later)
  256. # If nonzero, a 32-bit pointer
  257. # to the kernel command line.
  258. # The command line should be
  259. # located between the start of
  260. # setup and the end of low
  261. # memory (0xa0000), or it may
  262. # get overwritten before it
  263. # gets read. If this field is
  264. # used, there is no longer
  265. # anything magical about the
  266. # 0x90000 segment; the setup
  267. # can be located anywhere in
  268. # low memory 0x10000 or higher.
  269. initrd_addr_max: .long 0x7fffffff
  270. # (Header version 0x0203 or later)
  271. # The highest safe address for
  272. # the contents of an initrd
  273. # The current kernel allows up to 4 GB,
  274. # but leave it at 2 GB to avoid
  275. # possible bootloader bugs.
  276. kernel_alignment: .long CONFIG_PHYSICAL_ALIGN #physical addr alignment
  277. #required for protected mode
  278. #kernel
  279. #ifdef CONFIG_RELOCATABLE
  280. relocatable_kernel: .byte 1
  281. #else
  282. relocatable_kernel: .byte 0
  283. #endif
  284. min_alignment: .byte MIN_KERNEL_ALIGN_LG2 # minimum alignment
  285. xloadflags:
  286. #ifdef CONFIG_X86_64
  287. # define XLF0 XLF_KERNEL_64 /* 64-bit kernel */
  288. #else
  289. # define XLF0 0
  290. #endif
  291. #if defined(CONFIG_RELOCATABLE) && defined(CONFIG_X86_64)
  292. /* kernel/boot_param/ramdisk could be loaded above 4g */
  293. # define XLF1 XLF_CAN_BE_LOADED_ABOVE_4G
  294. #else
  295. # define XLF1 0
  296. #endif
  297. #ifdef CONFIG_EFI_HANDOVER_PROTOCOL
  298. # ifdef CONFIG_EFI_MIXED
  299. # define XLF23 (XLF_EFI_HANDOVER_32|XLF_EFI_HANDOVER_64)
  300. # else
  301. # ifdef CONFIG_X86_64
  302. # define XLF23 XLF_EFI_HANDOVER_64 /* 64-bit EFI handover ok */
  303. # else
  304. # define XLF23 XLF_EFI_HANDOVER_32 /* 32-bit EFI handover ok */
  305. # endif
  306. # endif
  307. #else
  308. # define XLF23 0
  309. #endif
  310. #if defined(CONFIG_X86_64) && defined(CONFIG_EFI) && defined(CONFIG_KEXEC_CORE)
  311. # define XLF4 XLF_EFI_KEXEC
  312. #else
  313. # define XLF4 0
  314. #endif
  315. #ifdef CONFIG_X86_64
  316. #ifdef CONFIG_X86_5LEVEL
  317. #define XLF56 (XLF_5LEVEL|XLF_5LEVEL_ENABLED)
  318. #else
  319. #define XLF56 XLF_5LEVEL
  320. #endif
  321. #else
  322. #define XLF56 0
  323. #endif
  324. .word XLF0 | XLF1 | XLF23 | XLF4 | XLF56
  325. cmdline_size: .long COMMAND_LINE_SIZE-1 #length of the command line,
  326. #added with boot protocol
  327. #version 2.06
  328. hardware_subarch: .long 0 # subarchitecture, added with 2.07
  329. # default to 0 for normal x86 PC
  330. hardware_subarch_data: .quad 0
  331. payload_offset: .long ZO_input_data
  332. payload_length: .long ZO_z_input_len
  333. setup_data: .quad 0 # 64-bit physical pointer to
  334. # single linked list of
  335. # struct setup_data
  336. pref_address: .quad LOAD_PHYSICAL_ADDR # preferred load addr
  337. #
  338. # Getting to provably safe in-place decompression is hard. Worst case
  339. # behaviours need to be analyzed. Here let's take the decompression of
  340. # a gzip-compressed kernel as example, to illustrate it:
  341. #
  342. # The file layout of gzip compressed kernel is:
  343. #
  344. # magic[2]
  345. # method[1]
  346. # flags[1]
  347. # timestamp[4]
  348. # extraflags[1]
  349. # os[1]
  350. # compressed data blocks[N]
  351. # crc[4] orig_len[4]
  352. #
  353. # ... resulting in +18 bytes overhead of uncompressed data.
  354. #
  355. # (For more information, please refer to RFC 1951 and RFC 1952.)
  356. #
  357. # Files divided into blocks
  358. # 1 bit (last block flag)
  359. # 2 bits (block type)
  360. #
  361. # 1 block occurs every 32K -1 bytes or when there 50% compression
  362. # has been achieved. The smallest block type encoding is always used.
  363. #
  364. # stored:
  365. # 32 bits length in bytes.
  366. #
  367. # fixed:
  368. # magic fixed tree.
  369. # symbols.
  370. #
  371. # dynamic:
  372. # dynamic tree encoding.
  373. # symbols.
  374. #
  375. #
  376. # The buffer for decompression in place is the length of the uncompressed
  377. # data, plus a small amount extra to keep the algorithm safe. The
  378. # compressed data is placed at the end of the buffer. The output pointer
  379. # is placed at the start of the buffer and the input pointer is placed
  380. # where the compressed data starts. Problems will occur when the output
  381. # pointer overruns the input pointer.
  382. #
  383. # The output pointer can only overrun the input pointer if the input
  384. # pointer is moving faster than the output pointer. A condition only
  385. # triggered by data whose compressed form is larger than the uncompressed
  386. # form.
  387. #
  388. # The worst case at the block level is a growth of the compressed data
  389. # of 5 bytes per 32767 bytes.
  390. #
  391. # The worst case internal to a compressed block is very hard to figure.
  392. # The worst case can at least be bounded by having one bit that represents
  393. # 32764 bytes and then all of the rest of the bytes representing the very
  394. # very last byte.
  395. #
  396. # All of which is enough to compute an amount of extra data that is required
  397. # to be safe. To avoid problems at the block level allocating 5 extra bytes
  398. # per 32767 bytes of data is sufficient. To avoid problems internal to a
  399. # block adding an extra 32767 bytes (the worst case uncompressed block size)
  400. # is sufficient, to ensure that in the worst case the decompressed data for
  401. # block will stop the byte before the compressed data for a block begins.
  402. # To avoid problems with the compressed data's meta information an extra 18
  403. # bytes are needed. Leading to the formula:
  404. #
  405. # extra_bytes = (uncompressed_size >> 12) + 32768 + 18
  406. #
  407. # Adding 8 bytes per 32K is a bit excessive but much easier to calculate.
  408. # Adding 32768 instead of 32767 just makes for round numbers.
  409. #
  410. # Above analysis is for decompressing gzip compressed kernel only. Up to
  411. # now 6 different decompressor are supported all together. And among them
  412. # xz stores data in chunks and has maximum chunk of 64K. Hence safety
  413. # margin should be updated to cover all decompressors so that we don't
  414. # need to deal with each of them separately. Please check
  415. # the description in lib/decompressor_xxx.c for specific information.
  416. #
  417. # extra_bytes = (uncompressed_size >> 12) + 65536 + 128
  418. #
  419. # LZ4 is even worse: data that cannot be further compressed grows by 0.4%,
  420. # or one byte per 256 bytes. OTOH, we can safely get rid of the +128 as
  421. # the size-dependent part now grows so fast.
  422. #
  423. # extra_bytes = (uncompressed_size >> 8) + 65536
  424. #
  425. # ZSTD compressed data grows by at most 3 bytes per 128K, and only has a 22
  426. # byte fixed overhead but has a maximum block size of 128K, so it needs a
  427. # larger margin.
  428. #
  429. # extra_bytes = (uncompressed_size >> 8) + 131072
  430. #define ZO_z_extra_bytes ((ZO_z_output_len >> 8) + 131072)
  431. #if ZO_z_output_len > ZO_z_input_len
  432. # define ZO_z_extract_offset (ZO_z_output_len + ZO_z_extra_bytes - \
  433. ZO_z_input_len)
  434. #else
  435. # define ZO_z_extract_offset ZO_z_extra_bytes
  436. #endif
  437. /*
  438. * The extract_offset has to be bigger than ZO head section. Otherwise when
  439. * the head code is running to move ZO to the end of the buffer, it will
  440. * overwrite the head code itself.
  441. */
  442. #if (ZO__ehead - ZO_startup_32) > ZO_z_extract_offset
  443. # define ZO_z_min_extract_offset ((ZO__ehead - ZO_startup_32 + 4095) & ~4095)
  444. #else
  445. # define ZO_z_min_extract_offset ((ZO_z_extract_offset + 4095) & ~4095)
  446. #endif
  447. #define ZO_INIT_SIZE (ZO__end - ZO_startup_32 + ZO_z_min_extract_offset)
  448. #define VO_INIT_SIZE (VO__end - VO__text)
  449. #if ZO_INIT_SIZE > VO_INIT_SIZE
  450. # define INIT_SIZE ZO_INIT_SIZE
  451. #else
  452. # define INIT_SIZE VO_INIT_SIZE
  453. #endif
  454. .macro __handover_offset
  455. #ifndef CONFIG_EFI_HANDOVER_PROTOCOL
  456. .long 0
  457. #elif !defined(CONFIG_X86_64)
  458. .long ZO_efi32_stub_entry
  459. #else
  460. /* Yes, this is really how we defined it :( */
  461. .long ZO_efi64_stub_entry - 0x200
  462. #ifdef CONFIG_EFI_MIXED
  463. .if ZO_efi32_stub_entry != ZO_efi64_stub_entry - 0x200
  464. .error "32-bit and 64-bit EFI entry points do not match"
  465. .endif
  466. #endif
  467. #endif
  468. .endm
  469. init_size: .long INIT_SIZE # kernel initialization size
  470. handover_offset: __handover_offset
  471. kernel_info_offset: .long ZO_kernel_info
  472. # End of setup header #####################################################
  473. .section ".entrytext", "ax"
  474. start_of_setup:
  475. # Force %es = %ds
  476. movw %ds, %ax
  477. movw %ax, %es
  478. cld
  479. # Apparently some ancient versions of LILO invoked the kernel with %ss != %ds,
  480. # which happened to work by accident for the old code. Recalculate the stack
  481. # pointer if %ss is invalid. Otherwise leave it alone, LOADLIN sets up the
  482. # stack behind its own code, so we can't blindly put it directly past the heap.
  483. movw %ss, %dx
  484. cmpw %ax, %dx # %ds == %ss?
  485. movw %sp, %dx
  486. je 2f # -> assume %sp is reasonably set
  487. # Invalid %ss, make up a new stack
  488. movw $_end, %dx
  489. testb $CAN_USE_HEAP, loadflags
  490. jz 1f
  491. movw heap_end_ptr, %dx
  492. 1: addw $STACK_SIZE, %dx
  493. jnc 2f
  494. xorw %dx, %dx # Prevent wraparound
  495. 2: # Now %dx should point to the end of our stack space
  496. andw $~3, %dx # dword align (might as well...)
  497. jnz 3f
  498. movw $0xfffc, %dx # Make sure we're not zero
  499. 3: movw %ax, %ss
  500. movzwl %dx, %esp # Clear upper half of %esp
  501. sti # Now we should have a working stack
  502. # We will have entered with %cs = %ds+0x20, normalize %cs so
  503. # it is on par with the other segments.
  504. pushw %ds
  505. pushw $6f
  506. lretw
  507. 6:
  508. # Check signature at end of setup
  509. cmpl $0x5a5aaa55, setup_sig
  510. jne setup_bad
  511. # Zero the bss
  512. movw $__bss_start, %di
  513. movw $_end+3, %cx
  514. xorl %eax, %eax
  515. subw %di, %cx
  516. shrw $2, %cx
  517. rep; stosl
  518. # Jump to C code (should not return)
  519. calll main
  520. # Setup corrupt somehow...
  521. setup_bad:
  522. movl $setup_corrupt, %eax
  523. calll puts
  524. # Fall through...
  525. .globl die
  526. .type die, @function
  527. die:
  528. hlt
  529. jmp die
  530. .size die, .-die
  531. .section ".initdata", "a"
  532. setup_corrupt:
  533. .byte 7
  534. .string "No setup signature found...\n"