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