1b2441318SGreg Kroah-Hartman/* SPDX-License-Identifier: GPL-2.0 */ 296ae6ea0SThomas Gleixner/* 396ae6ea0SThomas Gleixner * header.S 496ae6ea0SThomas Gleixner * 596ae6ea0SThomas Gleixner * Copyright (C) 1991, 1992 Linus Torvalds 696ae6ea0SThomas Gleixner * 796ae6ea0SThomas Gleixner * Based on bootsect.S and setup.S 896ae6ea0SThomas Gleixner * modified by more people than can be counted 996ae6ea0SThomas Gleixner * 1096ae6ea0SThomas Gleixner * Rewritten as a common file by H. Peter Anvin (Apr 2007) 1196ae6ea0SThomas Gleixner * 1296ae6ea0SThomas Gleixner * BIG FAT NOTE: We're in real mode using 64k segments. Therefore segment 1396ae6ea0SThomas Gleixner * addresses must be multiplied by 16 to obtain their respective linear 1496ae6ea0SThomas Gleixner * addresses. To avoid confusion, linear addresses are written using leading 1596ae6ea0SThomas Gleixner * hex while segment addresses are written as segment:offset. 1696ae6ea0SThomas Gleixner * 1796ae6ea0SThomas Gleixner */ 1896ae6ea0SThomas Gleixner 1996ae6ea0SThomas Gleixner#include <asm/segment.h> 2096ae6ea0SThomas Gleixner#include <asm/boot.h> 210341c14dSJeremy Fitzhardinge#include <asm/page_types.h> 2296ae6ea0SThomas Gleixner#include <asm/setup.h> 2309c205afSH. Peter Anvin#include <asm/bootparam.h> 2496ae6ea0SThomas Gleixner#include "boot.h" 2577d1a499SH. Peter Anvin#include "voffset.h" 2677d1a499SH. Peter Anvin#include "zoffset.h" 2796ae6ea0SThomas Gleixner 2896ae6ea0SThomas GleixnerBOOTSEG = 0x07C0 /* original address of boot-sector */ 295e47c478SH. Peter AnvinSYSSEG = 0x1000 /* historical load address >> 4 */ 3096ae6ea0SThomas Gleixner 3196ae6ea0SThomas Gleixner#ifndef SVGA_MODE 3296ae6ea0SThomas Gleixner#define SVGA_MODE ASK_VGA 3396ae6ea0SThomas Gleixner#endif 3496ae6ea0SThomas Gleixner 3596ae6ea0SThomas Gleixner#ifndef ROOT_RDONLY 3696ae6ea0SThomas Gleixner#define ROOT_RDONLY 1 3796ae6ea0SThomas Gleixner#endif 3896ae6ea0SThomas Gleixner 3996ae6ea0SThomas Gleixner .code16 4096ae6ea0SThomas Gleixner .section ".bstext", "ax" 4196ae6ea0SThomas Gleixner 4296ae6ea0SThomas Gleixner .global bootsect_start 4396ae6ea0SThomas Gleixnerbootsect_start: 44291f3632SMatt Fleming#ifdef CONFIG_EFI_STUB 45291f3632SMatt Fleming # "MZ", MS-DOS header 46291f3632SMatt Fleming .byte 0x4d 47291f3632SMatt Fleming .byte 0x5a 48291f3632SMatt Fleming#endif 4996ae6ea0SThomas Gleixner 5096ae6ea0SThomas Gleixner # Normalize the start address 5196ae6ea0SThomas Gleixner ljmp $BOOTSEG, $start2 5296ae6ea0SThomas Gleixner 5396ae6ea0SThomas Gleixnerstart2: 5496ae6ea0SThomas Gleixner movw %cs, %ax 5596ae6ea0SThomas Gleixner movw %ax, %ds 5696ae6ea0SThomas Gleixner movw %ax, %es 5796ae6ea0SThomas Gleixner movw %ax, %ss 5896ae6ea0SThomas Gleixner xorw %sp, %sp 5996ae6ea0SThomas Gleixner sti 6096ae6ea0SThomas Gleixner cld 6196ae6ea0SThomas Gleixner 6296ae6ea0SThomas Gleixner movw $bugger_off_msg, %si 6396ae6ea0SThomas Gleixner 6496ae6ea0SThomas Gleixnermsg_loop: 6596ae6ea0SThomas Gleixner lodsb 6696ae6ea0SThomas Gleixner andb %al, %al 6796ae6ea0SThomas Gleixner jz bs_die 6896ae6ea0SThomas Gleixner movb $0xe, %ah 6996ae6ea0SThomas Gleixner movw $7, %bx 7096ae6ea0SThomas Gleixner int $0x10 7196ae6ea0SThomas Gleixner jmp msg_loop 7296ae6ea0SThomas Gleixner 7396ae6ea0SThomas Gleixnerbs_die: 7496ae6ea0SThomas Gleixner # Allow the user to press a key, then reboot 7596ae6ea0SThomas Gleixner xorw %ax, %ax 7696ae6ea0SThomas Gleixner int $0x16 7796ae6ea0SThomas Gleixner int $0x19 7896ae6ea0SThomas Gleixner 7996ae6ea0SThomas Gleixner # int 0x19 should never return. In case it does anyway, 8096ae6ea0SThomas Gleixner # invoke the BIOS reset code... 8196ae6ea0SThomas Gleixner ljmp $0xf000,$0xfff0 8296ae6ea0SThomas Gleixner 83291f3632SMatt Fleming#ifdef CONFIG_EFI_STUB 84291f3632SMatt Fleming .org 0x3c 85291f3632SMatt Fleming # 86291f3632SMatt Fleming # Offset to the PE header. 87291f3632SMatt Fleming # 88291f3632SMatt Fleming .long pe_header 89291f3632SMatt Fleming#endif /* CONFIG_EFI_STUB */ 90291f3632SMatt Fleming 9196ae6ea0SThomas Gleixner .section ".bsdata", "a" 9296ae6ea0SThomas Gleixnerbugger_off_msg: 93c7fb93ecSMichael Brown .ascii "Use a boot loader.\r\n" 9496ae6ea0SThomas Gleixner .ascii "\n" 9596ae6ea0SThomas Gleixner .ascii "Remove disk and press any key to reboot...\r\n" 9696ae6ea0SThomas Gleixner .byte 0 9796ae6ea0SThomas Gleixner 98291f3632SMatt Fleming#ifdef CONFIG_EFI_STUB 99291f3632SMatt Flemingpe_header: 100291f3632SMatt Fleming .ascii "PE" 101291f3632SMatt Fleming .word 0 102291f3632SMatt Fleming 103291f3632SMatt Flemingcoff_header: 104291f3632SMatt Fleming#ifdef CONFIG_X86_32 105291f3632SMatt Fleming .word 0x14c # i386 106291f3632SMatt Fleming#else 107291f3632SMatt Fleming .word 0x8664 # x86-64 108291f3632SMatt Fleming#endif 109c7fb93ecSMichael Brown .word 4 # nr_sections 110291f3632SMatt Fleming .long 0 # TimeDateStamp 111291f3632SMatt Fleming .long 0 # PointerToSymbolTable 112291f3632SMatt Fleming .long 1 # NumberOfSymbols 113291f3632SMatt Fleming .word section_table - optional_header # SizeOfOptionalHeader 114291f3632SMatt Fleming#ifdef CONFIG_X86_32 115291f3632SMatt Fleming .word 0x306 # Characteristics. 116291f3632SMatt Fleming # IMAGE_FILE_32BIT_MACHINE | 117291f3632SMatt Fleming # IMAGE_FILE_DEBUG_STRIPPED | 118291f3632SMatt Fleming # IMAGE_FILE_EXECUTABLE_IMAGE | 119291f3632SMatt Fleming # IMAGE_FILE_LINE_NUMS_STRIPPED 120291f3632SMatt Fleming#else 121291f3632SMatt Fleming .word 0x206 # Characteristics 122291f3632SMatt Fleming # IMAGE_FILE_DEBUG_STRIPPED | 123291f3632SMatt Fleming # IMAGE_FILE_EXECUTABLE_IMAGE | 124291f3632SMatt Fleming # IMAGE_FILE_LINE_NUMS_STRIPPED 125291f3632SMatt Fleming#endif 126291f3632SMatt Fleming 127291f3632SMatt Flemingoptional_header: 128291f3632SMatt Fleming#ifdef CONFIG_X86_32 129291f3632SMatt Fleming .word 0x10b # PE32 format 130291f3632SMatt Fleming#else 131291f3632SMatt Fleming .word 0x20b # PE32+ format 132291f3632SMatt Fleming#endif 133291f3632SMatt Fleming .byte 0x02 # MajorLinkerVersion 134291f3632SMatt Fleming .byte 0x14 # MinorLinkerVersion 135291f3632SMatt Fleming 136291f3632SMatt Fleming # Filled in by build.c 137291f3632SMatt Fleming .long 0 # SizeOfCode 138291f3632SMatt Fleming 139291f3632SMatt Fleming .long 0 # SizeOfInitializedData 140291f3632SMatt Fleming .long 0 # SizeOfUninitializedData 141291f3632SMatt Fleming 142291f3632SMatt Fleming # Filled in by build.c 143291f3632SMatt Fleming .long 0x0000 # AddressOfEntryPoint 144291f3632SMatt Fleming 145e31be363SMatt Fleming .long 0x0200 # BaseOfCode 146291f3632SMatt Fleming#ifdef CONFIG_X86_32 147291f3632SMatt Fleming .long 0 # data 148291f3632SMatt Fleming#endif 149291f3632SMatt Fleming 150291f3632SMatt Flemingextra_header_fields: 151291f3632SMatt Fleming#ifdef CONFIG_X86_32 152291f3632SMatt Fleming .long 0 # ImageBase 153291f3632SMatt Fleming#else 154291f3632SMatt Fleming .quad 0 # ImageBase 155291f3632SMatt Fleming#endif 156fa5c3501SMatt Fleming .long 0x20 # SectionAlignment 157743628e8SJordan Justen .long 0x20 # FileAlignment 158291f3632SMatt Fleming .word 0 # MajorOperatingSystemVersion 159291f3632SMatt Fleming .word 0 # MinorOperatingSystemVersion 160291f3632SMatt Fleming .word 0 # MajorImageVersion 161291f3632SMatt Fleming .word 0 # MinorImageVersion 162291f3632SMatt Fleming .word 0 # MajorSubsystemVersion 163291f3632SMatt Fleming .word 0 # MinorSubsystemVersion 164291f3632SMatt Fleming .long 0 # Win32VersionValue 165291f3632SMatt Fleming 166291f3632SMatt Fleming # 167291f3632SMatt Fleming # The size of the bzImage is written in tools/build.c 168291f3632SMatt Fleming # 169291f3632SMatt Fleming .long 0 # SizeOfImage 170291f3632SMatt Fleming 171291f3632SMatt Fleming .long 0x200 # SizeOfHeaders 172291f3632SMatt Fleming .long 0 # CheckSum 173291f3632SMatt Fleming .word 0xa # Subsystem (EFI application) 174291f3632SMatt Fleming .word 0 # DllCharacteristics 175291f3632SMatt Fleming#ifdef CONFIG_X86_32 176291f3632SMatt Fleming .long 0 # SizeOfStackReserve 177291f3632SMatt Fleming .long 0 # SizeOfStackCommit 178291f3632SMatt Fleming .long 0 # SizeOfHeapReserve 179291f3632SMatt Fleming .long 0 # SizeOfHeapCommit 180291f3632SMatt Fleming#else 181291f3632SMatt Fleming .quad 0 # SizeOfStackReserve 182291f3632SMatt Fleming .quad 0 # SizeOfStackCommit 183291f3632SMatt Fleming .quad 0 # SizeOfHeapReserve 184291f3632SMatt Fleming .quad 0 # SizeOfHeapCommit 185291f3632SMatt Fleming#endif 186291f3632SMatt Fleming .long 0 # LoaderFlags 187e47bb0bdSMatt Fleming .long 0x6 # NumberOfRvaAndSizes 188291f3632SMatt Fleming 189291f3632SMatt Fleming .quad 0 # ExportTable 190291f3632SMatt Fleming .quad 0 # ImportTable 191291f3632SMatt Fleming .quad 0 # ResourceTable 192291f3632SMatt Fleming .quad 0 # ExceptionTable 193291f3632SMatt Fleming .quad 0 # CertificationTable 194291f3632SMatt Fleming .quad 0 # BaseRelocationTable 195291f3632SMatt Fleming 196291f3632SMatt Fleming # Section table 197291f3632SMatt Flemingsection_table: 198743628e8SJordan Justen # 199743628e8SJordan Justen # The offset & size fields are filled in by build.c. 200743628e8SJordan Justen # 201743628e8SJordan Justen .ascii ".setup" 202291f3632SMatt Fleming .byte 0 203291f3632SMatt Fleming .byte 0 204291f3632SMatt Fleming .long 0 205291f3632SMatt Fleming .long 0x0 # startup_{32,64} 206291f3632SMatt Fleming .long 0 # Size of initialized data 207291f3632SMatt Fleming # on disk 208291f3632SMatt Fleming .long 0x0 # startup_{32,64} 209291f3632SMatt Fleming .long 0 # PointerToRelocations 210291f3632SMatt Fleming .long 0 # PointerToLineNumbers 211291f3632SMatt Fleming .word 0 # NumberOfRelocations 212291f3632SMatt Fleming .word 0 # NumberOfLineNumbers 213291f3632SMatt Fleming .long 0x60500020 # Characteristics (section flags) 214291f3632SMatt Fleming 215291f3632SMatt Fleming # 216291f3632SMatt Fleming # The EFI application loader requires a relocation section 217743628e8SJordan Justen # because EFI applications must be relocatable. The .reloc 218743628e8SJordan Justen # offset & size fields are filled in by build.c. 219291f3632SMatt Fleming # 220291f3632SMatt Fleming .ascii ".reloc" 221291f3632SMatt Fleming .byte 0 222291f3632SMatt Fleming .byte 0 2232e064b1eSJordan Justen .long 0 2242e064b1eSJordan Justen .long 0 2252e064b1eSJordan Justen .long 0 # SizeOfRawData 2262e064b1eSJordan Justen .long 0 # PointerToRawData 227291f3632SMatt Fleming .long 0 # PointerToRelocations 228291f3632SMatt Fleming .long 0 # PointerToLineNumbers 229291f3632SMatt Fleming .word 0 # NumberOfRelocations 230291f3632SMatt Fleming .word 0 # NumberOfLineNumbers 231291f3632SMatt Fleming .long 0x42100040 # Characteristics (section flags) 232743628e8SJordan Justen 233743628e8SJordan Justen # 234743628e8SJordan Justen # The offset & size fields are filled in by build.c. 235743628e8SJordan Justen # 236743628e8SJordan Justen .ascii ".text" 237743628e8SJordan Justen .byte 0 238743628e8SJordan Justen .byte 0 239743628e8SJordan Justen .byte 0 240743628e8SJordan Justen .long 0 241743628e8SJordan Justen .long 0x0 # startup_{32,64} 242743628e8SJordan Justen .long 0 # Size of initialized data 243743628e8SJordan Justen # on disk 244743628e8SJordan Justen .long 0x0 # startup_{32,64} 245743628e8SJordan Justen .long 0 # PointerToRelocations 246743628e8SJordan Justen .long 0 # PointerToLineNumbers 247743628e8SJordan Justen .word 0 # NumberOfRelocations 248743628e8SJordan Justen .word 0 # NumberOfLineNumbers 249743628e8SJordan Justen .long 0x60500020 # Characteristics (section flags) 250743628e8SJordan Justen 251c7fb93ecSMichael Brown # 252c7fb93ecSMichael Brown # The offset & size fields are filled in by build.c. 253c7fb93ecSMichael Brown # 254c7fb93ecSMichael Brown .ascii ".bss" 255c7fb93ecSMichael Brown .byte 0 256c7fb93ecSMichael Brown .byte 0 257c7fb93ecSMichael Brown .byte 0 258c7fb93ecSMichael Brown .byte 0 259c7fb93ecSMichael Brown .long 0 260c7fb93ecSMichael Brown .long 0x0 261c7fb93ecSMichael Brown .long 0 # Size of initialized data 262c7fb93ecSMichael Brown # on disk 263c7fb93ecSMichael Brown .long 0x0 264c7fb93ecSMichael Brown .long 0 # PointerToRelocations 265c7fb93ecSMichael Brown .long 0 # PointerToLineNumbers 266c7fb93ecSMichael Brown .word 0 # NumberOfRelocations 267c7fb93ecSMichael Brown .word 0 # NumberOfLineNumbers 268c7fb93ecSMichael Brown .long 0xc8000080 # Characteristics (section flags) 269c7fb93ecSMichael Brown 270291f3632SMatt Fleming#endif /* CONFIG_EFI_STUB */ 27196ae6ea0SThomas Gleixner 27296ae6ea0SThomas Gleixner # Kernel attributes; used by setup. This is part 1 of the 27396ae6ea0SThomas Gleixner # header, from the old boot sector. 27496ae6ea0SThomas Gleixner 27596ae6ea0SThomas Gleixner .section ".header", "a" 27609c205afSH. Peter Anvin .globl sentinel 27709c205afSH. Peter Anvinsentinel: .byte 0xff, 0xff /* Used to detect broken loaders */ 27809c205afSH. Peter Anvin 27996ae6ea0SThomas Gleixner .globl hdr 28096ae6ea0SThomas Gleixnerhdr: 2815e47c478SH. Peter Anvinsetup_sects: .byte 0 /* Filled in by build.c */ 28296ae6ea0SThomas Gleixnerroot_flags: .word ROOT_RDONLY 2835e47c478SH. Peter Anvinsyssize: .long 0 /* Filled in by build.c */ 2845e47c478SH. Peter Anvinram_size: .word 0 /* Obsolete */ 28596ae6ea0SThomas Gleixnervid_mode: .word SVGA_MODE 2865e47c478SH. Peter Anvinroot_dev: .word 0 /* Filled in by build.c */ 28796ae6ea0SThomas Gleixnerboot_flag: .word 0xAA55 28896ae6ea0SThomas Gleixner 28996ae6ea0SThomas Gleixner # offset 512, entry point 29096ae6ea0SThomas Gleixner 29196ae6ea0SThomas Gleixner .globl _start 29296ae6ea0SThomas Gleixner_start: 29396ae6ea0SThomas Gleixner # Explicitly enter this as bytes, or the assembler 29496ae6ea0SThomas Gleixner # tries to generate a 3-byte jump here, which causes 29596ae6ea0SThomas Gleixner # everything else to push off to the wrong offset. 29696ae6ea0SThomas Gleixner .byte 0xeb # short (2-byte) jump 29796ae6ea0SThomas Gleixner .byte start_of_setup-1f 29896ae6ea0SThomas Gleixner1: 29996ae6ea0SThomas Gleixner 30096ae6ea0SThomas Gleixner # Part 2 of the header, from the old setup.S 30196ae6ea0SThomas Gleixner 30296ae6ea0SThomas Gleixner .ascii "HdrS" # header signature 30338418404SJuergen Gross .word 0x020d # header version number (>= 0x0105) 30496ae6ea0SThomas Gleixner # or else old loadlin-1.5 will fail) 30596ae6ea0SThomas Gleixner .globl realmode_swtch 30696ae6ea0SThomas Gleixnerrealmode_swtch: .word 0, 0 # default_switch, SETUPSEG 3075e47c478SH. Peter Anvinstart_sys_seg: .word SYSSEG # obsolete and meaningless, but just 3085e47c478SH. Peter Anvin # in case something decided to "use" it 30996ae6ea0SThomas Gleixner .word kernel_version-512 # pointing to kernel version string 31096ae6ea0SThomas Gleixner # above section of header is compatible 31196ae6ea0SThomas Gleixner # with loadlin-1.5 (header v1.5). Don't 31296ae6ea0SThomas Gleixner # change it. 31396ae6ea0SThomas Gleixner 3145e47c478SH. Peter Anvintype_of_loader: .byte 0 # 0 means ancient bootloader, newer 3155e47c478SH. Peter Anvin # bootloaders know to change this. 316cb1aaebeSMauro Carvalho Chehab # See Documentation/x86/boot.rst for 31796ae6ea0SThomas Gleixner # assigned ids 31896ae6ea0SThomas Gleixner 31996ae6ea0SThomas Gleixner# flags, unused bits must be zero (RFU) bit within loadflags 32096ae6ea0SThomas Gleixnerloadflags: 32109c205afSH. Peter Anvin .byte LOADED_HIGH # The kernel is to be loaded high 32296ae6ea0SThomas Gleixner 32396ae6ea0SThomas Gleixnersetup_move_size: .word 0x8000 # size to move, when setup is not 32496ae6ea0SThomas Gleixner # loaded at 0x90000. We will move setup 32596ae6ea0SThomas Gleixner # to 0x90000 then just before jumping 32696ae6ea0SThomas Gleixner # into the kernel. However, only the 32796ae6ea0SThomas Gleixner # loader knows how much data behind 32896ae6ea0SThomas Gleixner # us also needs to be loaded. 32996ae6ea0SThomas Gleixner 33096ae6ea0SThomas Gleixnercode32_start: # here loaders can put a different 33196ae6ea0SThomas Gleixner # start address for 32-bit code. 33296ae6ea0SThomas Gleixner .long 0x100000 # 0x100000 = default for big kernel 33396ae6ea0SThomas Gleixner 33496ae6ea0SThomas Gleixnerramdisk_image: .long 0 # address of loaded ramdisk image 33596ae6ea0SThomas Gleixner # Here the loader puts the 32-bit 33696ae6ea0SThomas Gleixner # address where it loaded the image. 33796ae6ea0SThomas Gleixner # This only will be read by the kernel. 33896ae6ea0SThomas Gleixner 33996ae6ea0SThomas Gleixnerramdisk_size: .long 0 # its size in bytes 34096ae6ea0SThomas Gleixner 34196ae6ea0SThomas Gleixnerbootsect_kludge: 34296ae6ea0SThomas Gleixner .long 0 # obsolete 34396ae6ea0SThomas Gleixner 3446b6815c6SH. Peter Anvinheap_end_ptr: .word _end+STACK_SIZE-512 3456b6815c6SH. Peter Anvin # (Header version 0x0201 or later) 34696ae6ea0SThomas Gleixner # space from here (exclusive) down to 34796ae6ea0SThomas Gleixner # end of setup code can be used by setup 34896ae6ea0SThomas Gleixner # for local heap purposes. 34996ae6ea0SThomas Gleixner 3505031296cSH. Peter Anvinext_loader_ver: 3515031296cSH. Peter Anvin .byte 0 # Extended boot loader version 3525031296cSH. Peter Anvinext_loader_type: 3535031296cSH. Peter Anvin .byte 0 # Extended boot loader type 3545031296cSH. Peter Anvin 35596ae6ea0SThomas Gleixnercmd_line_ptr: .long 0 # (Header version 0x0202 or later) 35696ae6ea0SThomas Gleixner # If nonzero, a 32-bit pointer 35796ae6ea0SThomas Gleixner # to the kernel command line. 35896ae6ea0SThomas Gleixner # The command line should be 35996ae6ea0SThomas Gleixner # located between the start of 36096ae6ea0SThomas Gleixner # setup and the end of low 36196ae6ea0SThomas Gleixner # memory (0xa0000), or it may 36296ae6ea0SThomas Gleixner # get overwritten before it 36396ae6ea0SThomas Gleixner # gets read. If this field is 36496ae6ea0SThomas Gleixner # used, there is no longer 36596ae6ea0SThomas Gleixner # anything magical about the 36696ae6ea0SThomas Gleixner # 0x90000 segment; the setup 36796ae6ea0SThomas Gleixner # can be located anywhere in 36896ae6ea0SThomas Gleixner # low memory 0x10000 or higher. 36996ae6ea0SThomas Gleixner 3703e920b53SBorislav Petkovinitrd_addr_max: .long 0x7fffffff 37196ae6ea0SThomas Gleixner # (Header version 0x0203 or later) 37296ae6ea0SThomas Gleixner # The highest safe address for 37396ae6ea0SThomas Gleixner # the contents of an initrd 374cf8fa920SH. Peter Anvin # The current kernel allows up to 4 GB, 375cf8fa920SH. Peter Anvin # but leave it at 2 GB to avoid 376cf8fa920SH. Peter Anvin # possible bootloader bugs. 37796ae6ea0SThomas Gleixner 37896ae6ea0SThomas Gleixnerkernel_alignment: .long CONFIG_PHYSICAL_ALIGN #physical addr alignment 37996ae6ea0SThomas Gleixner #required for protected mode 38096ae6ea0SThomas Gleixner #kernel 38196ae6ea0SThomas Gleixner#ifdef CONFIG_RELOCATABLE 38296ae6ea0SThomas Gleixnerrelocatable_kernel: .byte 1 38396ae6ea0SThomas Gleixner#else 38496ae6ea0SThomas Gleixnerrelocatable_kernel: .byte 0 38596ae6ea0SThomas Gleixner#endif 38637ba7ab5SH. Peter Anvinmin_alignment: .byte MIN_KERNEL_ALIGN_LG2 # minimum alignment 38709c205afSH. Peter Anvin 38809c205afSH. Peter Anvinxloadflags: 38909c205afSH. Peter Anvin#ifdef CONFIG_X86_64 39009c205afSH. Peter Anvin# define XLF0 XLF_KERNEL_64 /* 64-bit kernel */ 39109c205afSH. Peter Anvin#else 39209c205afSH. Peter Anvin# define XLF0 0 39309c205afSH. Peter Anvin#endif 394ee92d815SYinghai Lu 395745c5167SMatt Fleming#if defined(CONFIG_RELOCATABLE) && defined(CONFIG_X86_64) 396ee92d815SYinghai Lu /* kernel/boot_param/ramdisk could be loaded above 4g */ 397ee92d815SYinghai Lu# define XLF1 XLF_CAN_BE_LOADED_ABOVE_4G 398ee92d815SYinghai Lu#else 399ee92d815SYinghai Lu# define XLF1 0 400ee92d815SYinghai Lu#endif 401ee92d815SYinghai Lu 40209c205afSH. Peter Anvin#ifdef CONFIG_EFI_STUB 4037d453eeeSMatt Fleming# ifdef CONFIG_EFI_MIXED 4047d453eeeSMatt Fleming# define XLF23 (XLF_EFI_HANDOVER_32|XLF_EFI_HANDOVER_64) 4057d453eeeSMatt Fleming# else 40609c205afSH. Peter Anvin# ifdef CONFIG_X86_64 40709c205afSH. Peter Anvin# define XLF23 XLF_EFI_HANDOVER_64 /* 64-bit EFI handover ok */ 40809c205afSH. Peter Anvin# else 40909c205afSH. Peter Anvin# define XLF23 XLF_EFI_HANDOVER_32 /* 32-bit EFI handover ok */ 41009c205afSH. Peter Anvin# endif 4117d453eeeSMatt Fleming# endif 41209c205afSH. Peter Anvin#else 41309c205afSH. Peter Anvin# define XLF23 0 41409c205afSH. Peter Anvin#endif 415456a29ddSDave Young 4162965faa5SDave Young#if defined(CONFIG_X86_64) && defined(CONFIG_EFI) && defined(CONFIG_KEXEC_CORE) 417456a29ddSDave Young# define XLF4 XLF_EFI_KEXEC 418456a29ddSDave Young#else 419456a29ddSDave Young# define XLF4 0 420456a29ddSDave Young#endif 421456a29ddSDave Young 422f2d08c5dSBaoquan He#ifdef CONFIG_X86_64 423f2d08c5dSBaoquan He#ifdef CONFIG_X86_5LEVEL 424f2d08c5dSBaoquan He#define XLF56 (XLF_5LEVEL|XLF_5LEVEL_ENABLED) 425f2d08c5dSBaoquan He#else 426f2d08c5dSBaoquan He#define XLF56 XLF_5LEVEL 427f2d08c5dSBaoquan He#endif 428f2d08c5dSBaoquan He#else 429f2d08c5dSBaoquan He#define XLF56 0 430f2d08c5dSBaoquan He#endif 431f2d08c5dSBaoquan He 432f2d08c5dSBaoquan He .word XLF0 | XLF1 | XLF23 | XLF4 | XLF56 43396ae6ea0SThomas Gleixner 43496ae6ea0SThomas Gleixnercmdline_size: .long COMMAND_LINE_SIZE-1 #length of the command line, 43596ae6ea0SThomas Gleixner #added with boot protocol 43696ae6ea0SThomas Gleixner #version 2.06 43796ae6ea0SThomas Gleixner 438a24e7851SRusty Russellhardware_subarch: .long 0 # subarchitecture, added with 2.07 439a24e7851SRusty Russell # default to 0 for normal x86 PC 440a24e7851SRusty Russell 441a24e7851SRusty Russellhardware_subarch_data: .quad 0 442a24e7851SRusty Russell 44377d1a499SH. Peter Anvinpayload_offset: .long ZO_input_data 44477d1a499SH. Peter Anvinpayload_length: .long ZO_z_input_len 445099e1377SIan Campbell 4468b664aa6SHuang, Yingsetup_data: .quad 0 # 64-bit physical pointer to 4478b664aa6SHuang, Ying # single linked list of 4488b664aa6SHuang, Ying # struct setup_data 4498b664aa6SHuang, Ying 45037ba7ab5SH. Peter Anvinpref_address: .quad LOAD_PHYSICAL_ADDR # preferred load addr 45137ba7ab5SH. Peter Anvin 4524252db10SBaoquan He# 4534252db10SBaoquan He# Getting to provably safe in-place decompression is hard. Worst case 4544252db10SBaoquan He# behaviours need to be analyzed. Here let's take the decompression of 4554252db10SBaoquan He# a gzip-compressed kernel as example, to illustrate it: 4564252db10SBaoquan He# 4574252db10SBaoquan He# The file layout of gzip compressed kernel is: 4584252db10SBaoquan He# 4594252db10SBaoquan He# magic[2] 4604252db10SBaoquan He# method[1] 4614252db10SBaoquan He# flags[1] 4624252db10SBaoquan He# timestamp[4] 4634252db10SBaoquan He# extraflags[1] 4644252db10SBaoquan He# os[1] 4654252db10SBaoquan He# compressed data blocks[N] 4664252db10SBaoquan He# crc[4] orig_len[4] 4674252db10SBaoquan He# 4684252db10SBaoquan He# ... resulting in +18 bytes overhead of uncompressed data. 4694252db10SBaoquan He# 4704252db10SBaoquan He# (For more information, please refer to RFC 1951 and RFC 1952.) 4714252db10SBaoquan He# 4724252db10SBaoquan He# Files divided into blocks 4734252db10SBaoquan He# 1 bit (last block flag) 4744252db10SBaoquan He# 2 bits (block type) 4754252db10SBaoquan He# 4764252db10SBaoquan He# 1 block occurs every 32K -1 bytes or when there 50% compression 4774252db10SBaoquan He# has been achieved. The smallest block type encoding is always used. 4784252db10SBaoquan He# 4794252db10SBaoquan He# stored: 4804252db10SBaoquan He# 32 bits length in bytes. 4814252db10SBaoquan He# 4824252db10SBaoquan He# fixed: 4834252db10SBaoquan He# magic fixed tree. 4844252db10SBaoquan He# symbols. 4854252db10SBaoquan He# 4864252db10SBaoquan He# dynamic: 4874252db10SBaoquan He# dynamic tree encoding. 4884252db10SBaoquan He# symbols. 4894252db10SBaoquan He# 4904252db10SBaoquan He# 4914252db10SBaoquan He# The buffer for decompression in place is the length of the uncompressed 4924252db10SBaoquan He# data, plus a small amount extra to keep the algorithm safe. The 4934252db10SBaoquan He# compressed data is placed at the end of the buffer. The output pointer 4944252db10SBaoquan He# is placed at the start of the buffer and the input pointer is placed 4954252db10SBaoquan He# where the compressed data starts. Problems will occur when the output 4964252db10SBaoquan He# pointer overruns the input pointer. 4974252db10SBaoquan He# 4984252db10SBaoquan He# The output pointer can only overrun the input pointer if the input 4994252db10SBaoquan He# pointer is moving faster than the output pointer. A condition only 5004252db10SBaoquan He# triggered by data whose compressed form is larger than the uncompressed 5014252db10SBaoquan He# form. 5024252db10SBaoquan He# 5034252db10SBaoquan He# The worst case at the block level is a growth of the compressed data 5044252db10SBaoquan He# of 5 bytes per 32767 bytes. 5054252db10SBaoquan He# 5064252db10SBaoquan He# The worst case internal to a compressed block is very hard to figure. 5074252db10SBaoquan He# The worst case can at least be bounded by having one bit that represents 5084252db10SBaoquan He# 32764 bytes and then all of the rest of the bytes representing the very 5094252db10SBaoquan He# very last byte. 5104252db10SBaoquan He# 5114252db10SBaoquan He# All of which is enough to compute an amount of extra data that is required 5124252db10SBaoquan He# to be safe. To avoid problems at the block level allocating 5 extra bytes 5134252db10SBaoquan He# per 32767 bytes of data is sufficient. To avoid problems internal to a 5144252db10SBaoquan He# block adding an extra 32767 bytes (the worst case uncompressed block size) 5154252db10SBaoquan He# is sufficient, to ensure that in the worst case the decompressed data for 5164252db10SBaoquan He# block will stop the byte before the compressed data for a block begins. 5174252db10SBaoquan He# To avoid problems with the compressed data's meta information an extra 18 5184252db10SBaoquan He# bytes are needed. Leading to the formula: 5194252db10SBaoquan He# 520d607251bSYinghai Lu# extra_bytes = (uncompressed_size >> 12) + 32768 + 18 5214252db10SBaoquan He# 5224252db10SBaoquan He# Adding 8 bytes per 32K is a bit excessive but much easier to calculate. 5234252db10SBaoquan He# Adding 32768 instead of 32767 just makes for round numbers. 5244252db10SBaoquan He# 5254252db10SBaoquan He# Above analysis is for decompressing gzip compressed kernel only. Up to 5264252db10SBaoquan He# now 6 different decompressor are supported all together. And among them 5274252db10SBaoquan He# xz stores data in chunks and has maximum chunk of 64K. Hence safety 5284252db10SBaoquan He# margin should be updated to cover all decompressors so that we don't 5294252db10SBaoquan He# need to deal with each of them separately. Please check 5304252db10SBaoquan He# the description in lib/decompressor_xxx.c for specific information. 5314252db10SBaoquan He# 5324252db10SBaoquan He# extra_bytes = (uncompressed_size >> 12) + 65536 + 128 5335746f055SJan H. Schönherr# 5345746f055SJan H. Schönherr# LZ4 is even worse: data that cannot be further compressed grows by 0.4%, 5355746f055SJan H. Schönherr# or one byte per 256 bytes. OTOH, we can safely get rid of the +128 as 5365746f055SJan H. Schönherr# the size-dependent part now grows so fast. 5375746f055SJan H. Schönherr# 5385746f055SJan H. Schönherr# extra_bytes = (uncompressed_size >> 8) + 65536 5394252db10SBaoquan He 5405746f055SJan H. Schönherr#define ZO_z_extra_bytes ((ZO_z_output_len >> 8) + 65536) 541d607251bSYinghai Lu#if ZO_z_output_len > ZO_z_input_len 542d607251bSYinghai Lu# define ZO_z_extract_offset (ZO_z_output_len + ZO_z_extra_bytes - \ 543d607251bSYinghai Lu ZO_z_input_len) 544d607251bSYinghai Lu#else 545d607251bSYinghai Lu# define ZO_z_extract_offset ZO_z_extra_bytes 546d607251bSYinghai Lu#endif 547d607251bSYinghai Lu 548d607251bSYinghai Lu/* 549d607251bSYinghai Lu * The extract_offset has to be bigger than ZO head section. Otherwise when 550d607251bSYinghai Lu * the head code is running to move ZO to the end of the buffer, it will 551d607251bSYinghai Lu * overwrite the head code itself. 552d607251bSYinghai Lu */ 553d607251bSYinghai Lu#if (ZO__ehead - ZO_startup_32) > ZO_z_extract_offset 554d607251bSYinghai Lu# define ZO_z_min_extract_offset ((ZO__ehead - ZO_startup_32 + 4095) & ~4095) 555d607251bSYinghai Lu#else 556d607251bSYinghai Lu# define ZO_z_min_extract_offset ((ZO_z_extract_offset + 4095) & ~4095) 557d607251bSYinghai Lu#endif 558d607251bSYinghai Lu 559d607251bSYinghai Lu#define ZO_INIT_SIZE (ZO__end - ZO_startup_32 + ZO_z_min_extract_offset) 560d607251bSYinghai Lu 56137ba7ab5SH. Peter Anvin#define VO_INIT_SIZE (VO__end - VO__text) 56237ba7ab5SH. Peter Anvin#if ZO_INIT_SIZE > VO_INIT_SIZE 56337ba7ab5SH. Peter Anvin# define INIT_SIZE ZO_INIT_SIZE 56437ba7ab5SH. Peter Anvin#else 56537ba7ab5SH. Peter Anvin# define INIT_SIZE VO_INIT_SIZE 56637ba7ab5SH. Peter Anvin#endif 567d607251bSYinghai Lu 56837ba7ab5SH. Peter Anvininit_size: .long INIT_SIZE # kernel initialization size 56986134a1bSMatt Fleminghandover_offset: .long 0 # Filled in by build.c 570*2c33c27fSDaniel Kiperkernel_info_offset: .long 0 # Filled in by build.c 57137ba7ab5SH. Peter Anvin 57296ae6ea0SThomas Gleixner# End of setup header ##################################################### 57396ae6ea0SThomas Gleixner 5747a734e7dSH. Peter Anvin .section ".entrytext", "ax" 57596ae6ea0SThomas Gleixnerstart_of_setup: 57696ae6ea0SThomas Gleixner# Force %es = %ds 57796ae6ea0SThomas Gleixner movw %ds, %ax 57896ae6ea0SThomas Gleixner movw %ax, %es 57996ae6ea0SThomas Gleixner cld 58096ae6ea0SThomas Gleixner 58116252da6SJens Rottmann# Apparently some ancient versions of LILO invoked the kernel with %ss != %ds, 58216252da6SJens Rottmann# which happened to work by accident for the old code. Recalculate the stack 58316252da6SJens Rottmann# pointer if %ss is invalid. Otherwise leave it alone, LOADLIN sets up the 58416252da6SJens Rottmann# stack behind its own code, so we can't blindly put it directly past the heap. 5856b6815c6SH. Peter Anvin 5866b6815c6SH. Peter Anvin movw %ss, %dx 5876b6815c6SH. Peter Anvin cmpw %ax, %dx # %ds == %ss? 5886b6815c6SH. Peter Anvin movw %sp, %dx 58916252da6SJens Rottmann je 2f # -> assume %sp is reasonably set 5906b6815c6SH. Peter Anvin 59116252da6SJens Rottmann # Invalid %ss, make up a new stack 59216252da6SJens Rottmann movw $_end, %dx 59316252da6SJens Rottmann testb $CAN_USE_HEAP, loadflags 59416252da6SJens Rottmann jz 1f 59516252da6SJens Rottmann movw heap_end_ptr, %dx 59616252da6SJens Rottmann1: addw $STACK_SIZE, %dx 59716252da6SJens Rottmann jnc 2f 59816252da6SJens Rottmann xorw %dx, %dx # Prevent wraparound 59916252da6SJens Rottmann 60016252da6SJens Rottmann2: # Now %dx should point to the end of our stack space 6016b6815c6SH. Peter Anvin andw $~3, %dx # dword align (might as well...) 6026b6815c6SH. Peter Anvin jnz 3f 6036b6815c6SH. Peter Anvin movw $0xfffc, %dx # Make sure we're not zero 60416252da6SJens Rottmann3: movw %ax, %ss 6056b6815c6SH. Peter Anvin movzwl %dx, %esp # Clear upper half of %esp 6066b6815c6SH. Peter Anvin sti # Now we should have a working stack 6076b6815c6SH. Peter Anvin 6086b6815c6SH. Peter Anvin# We will have entered with %cs = %ds+0x20, normalize %cs so 6096b6815c6SH. Peter Anvin# it is on par with the other segments. 6106b6815c6SH. Peter Anvin pushw %ds 6116b6815c6SH. Peter Anvin pushw $6f 6126b6815c6SH. Peter Anvin lretw 6136b6815c6SH. Peter Anvin6: 61496ae6ea0SThomas Gleixner 61596ae6ea0SThomas Gleixner# Check signature at end of setup 61696ae6ea0SThomas Gleixner cmpl $0x5a5aaa55, setup_sig 61796ae6ea0SThomas Gleixner jne setup_bad 61896ae6ea0SThomas Gleixner 61996ae6ea0SThomas Gleixner# Zero the bss 62096ae6ea0SThomas Gleixner movw $__bss_start, %di 62196ae6ea0SThomas Gleixner movw $_end+3, %cx 62296ae6ea0SThomas Gleixner xorl %eax, %eax 62396ae6ea0SThomas Gleixner subw %di, %cx 62496ae6ea0SThomas Gleixner shrw $2, %cx 62596ae6ea0SThomas Gleixner rep; stosl 62696ae6ea0SThomas Gleixner 62796ae6ea0SThomas Gleixner# Jump to C code (should not return) 62896ae6ea0SThomas Gleixner calll main 62996ae6ea0SThomas Gleixner 63096ae6ea0SThomas Gleixner# Setup corrupt somehow... 63196ae6ea0SThomas Gleixnersetup_bad: 63296ae6ea0SThomas Gleixner movl $setup_corrupt, %eax 63396ae6ea0SThomas Gleixner calll puts 63496ae6ea0SThomas Gleixner # Fall through... 63596ae6ea0SThomas Gleixner 63696ae6ea0SThomas Gleixner .globl die 63796ae6ea0SThomas Gleixner .type die, @function 63896ae6ea0SThomas Gleixnerdie: 63996ae6ea0SThomas Gleixner hlt 64096ae6ea0SThomas Gleixner jmp die 64196ae6ea0SThomas Gleixner 64296ae6ea0SThomas Gleixner .size die, .-die 64396ae6ea0SThomas Gleixner 64496ae6ea0SThomas Gleixner .section ".initdata", "a" 64596ae6ea0SThomas Gleixnersetup_corrupt: 64696ae6ea0SThomas Gleixner .byte 7 64796ae6ea0SThomas Gleixner .string "No setup signature found...\n" 648