header.S 18 KB

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