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 */ 18a3326a0dSArd Biesheuvel#include <linux/pe.h> 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" 41291f3632SMatt Fleming#ifdef CONFIG_EFI_STUB 42291f3632SMatt Fleming # "MZ", MS-DOS header 43a3326a0dSArd Biesheuvel .word MZ_MAGIC 4429636a5cSArd Biesheuvel .org 0x38 45291f3632SMatt Fleming # 46291f3632SMatt Fleming # Offset to the PE header. 47291f3632SMatt Fleming # 4829636a5cSArd Biesheuvel .long LINUX_PE_MAGIC 49291f3632SMatt Fleming .long pe_header 50291f3632SMatt Flemingpe_header: 51a3326a0dSArd Biesheuvel .long PE_MAGIC 52291f3632SMatt Fleming 53291f3632SMatt Flemingcoff_header: 54291f3632SMatt Fleming#ifdef CONFIG_X86_32 55a3326a0dSArd Biesheuvel .set image_file_add_flags, IMAGE_FILE_32BIT_MACHINE 56a3326a0dSArd Biesheuvel .set pe_opt_magic, PE_OPT_MAGIC_PE32 57a3326a0dSArd Biesheuvel .word IMAGE_FILE_MACHINE_I386 58291f3632SMatt Fleming#else 59a3326a0dSArd Biesheuvel .set image_file_add_flags, 0 60a3326a0dSArd Biesheuvel .set pe_opt_magic, PE_OPT_MAGIC_PE32PLUS 61a3326a0dSArd Biesheuvel .word IMAGE_FILE_MACHINE_AMD64 62291f3632SMatt Fleming#endif 6397aa2765SArd Biesheuvel .word section_count # nr_sections 64291f3632SMatt Fleming .long 0 # TimeDateStamp 65291f3632SMatt Fleming .long 0 # PointerToSymbolTable 66291f3632SMatt Fleming .long 1 # NumberOfSymbols 67291f3632SMatt Fleming .word section_table - optional_header # SizeOfOptionalHeader 68a3326a0dSArd Biesheuvel .word IMAGE_FILE_EXECUTABLE_IMAGE | \ 69a3326a0dSArd Biesheuvel image_file_add_flags | \ 70a3326a0dSArd Biesheuvel IMAGE_FILE_DEBUG_STRIPPED | \ 71a3326a0dSArd Biesheuvel IMAGE_FILE_LINE_NUMS_STRIPPED # Characteristics 72291f3632SMatt Fleming 73291f3632SMatt Flemingoptional_header: 74a3326a0dSArd Biesheuvel .word pe_opt_magic 75291f3632SMatt Fleming .byte 0x02 # MajorLinkerVersion 76291f3632SMatt Fleming .byte 0x14 # MinorLinkerVersion 77291f3632SMatt Fleming 78*efa089e6SArd Biesheuvel .long setup_size + ZO__end - 0x200 # SizeOfCode 79291f3632SMatt Fleming 80291f3632SMatt Fleming .long 0 # SizeOfInitializedData 81291f3632SMatt Fleming .long 0 # SizeOfUninitializedData 82291f3632SMatt Fleming 83*efa089e6SArd Biesheuvel .long setup_size + ZO_efi_pe_entry # AddressOfEntryPoint 84291f3632SMatt Fleming 85e31be363SMatt Fleming .long 0x0200 # BaseOfCode 86291f3632SMatt Fleming#ifdef CONFIG_X86_32 87291f3632SMatt Fleming .long 0 # data 88291f3632SMatt Fleming#endif 89291f3632SMatt Fleming 90291f3632SMatt Flemingextra_header_fields: 91291f3632SMatt Fleming#ifdef CONFIG_X86_32 928eace5b3SArd Biesheuvel .long 0 # ImageBase 93291f3632SMatt Fleming#else 948eace5b3SArd Biesheuvel .quad 0 # ImageBase 95291f3632SMatt Fleming#endif 96fa5c3501SMatt Fleming .long 0x20 # SectionAlignment 97743628e8SJordan Justen .long 0x20 # FileAlignment 98291f3632SMatt Fleming .word 0 # MajorOperatingSystemVersion 99291f3632SMatt Fleming .word 0 # MinorOperatingSystemVersion 100148d3f71SArd Biesheuvel .word LINUX_EFISTUB_MAJOR_VERSION # MajorImageVersion 101148d3f71SArd Biesheuvel .word LINUX_EFISTUB_MINOR_VERSION # MinorImageVersion 102291f3632SMatt Fleming .word 0 # MajorSubsystemVersion 103291f3632SMatt Fleming .word 0 # MinorSubsystemVersion 104291f3632SMatt Fleming .long 0 # Win32VersionValue 105291f3632SMatt Fleming 106*efa089e6SArd Biesheuvel .long setup_size + ZO__end # SizeOfImage 107291f3632SMatt Fleming 108291f3632SMatt Fleming .long 0x200 # SizeOfHeaders 109291f3632SMatt Fleming .long 0 # CheckSum 110a3326a0dSArd Biesheuvel .word IMAGE_SUBSYSTEM_EFI_APPLICATION # Subsystem (EFI application) 111ca209f8bSLukas Bulwahn#ifdef CONFIG_EFI_DXE_MEM_ATTRIBUTES 11224b72bb1SPeter Jones .word IMAGE_DLL_CHARACTERISTICS_NX_COMPAT # DllCharacteristics 11324b72bb1SPeter Jones#else 114291f3632SMatt Fleming .word 0 # DllCharacteristics 11524b72bb1SPeter Jones#endif 116291f3632SMatt Fleming#ifdef CONFIG_X86_32 117291f3632SMatt Fleming .long 0 # SizeOfStackReserve 118291f3632SMatt Fleming .long 0 # SizeOfStackCommit 119291f3632SMatt Fleming .long 0 # SizeOfHeapReserve 120291f3632SMatt Fleming .long 0 # SizeOfHeapCommit 121291f3632SMatt Fleming#else 122291f3632SMatt Fleming .quad 0 # SizeOfStackReserve 123291f3632SMatt Fleming .quad 0 # SizeOfStackCommit 124291f3632SMatt Fleming .quad 0 # SizeOfHeapReserve 125291f3632SMatt Fleming .quad 0 # SizeOfHeapCommit 126291f3632SMatt Fleming#endif 127291f3632SMatt Fleming .long 0 # LoaderFlags 128a3326a0dSArd Biesheuvel .long (section_table - .) / 8 # NumberOfRvaAndSizes 129291f3632SMatt Fleming 130291f3632SMatt Fleming .quad 0 # ExportTable 131291f3632SMatt Fleming .quad 0 # ImportTable 132291f3632SMatt Fleming .quad 0 # ResourceTable 133291f3632SMatt Fleming .quad 0 # ExceptionTable 134291f3632SMatt Fleming .quad 0 # CertificationTable 135291f3632SMatt Fleming .quad 0 # BaseRelocationTable 136291f3632SMatt Fleming 137291f3632SMatt Fleming # Section table 138291f3632SMatt Flemingsection_table: 139743628e8SJordan Justen # 140743628e8SJordan Justen # The offset & size fields are filled in by build.c. 141743628e8SJordan Justen # 142743628e8SJordan Justen .ascii ".setup" 143291f3632SMatt Fleming .byte 0 144291f3632SMatt Fleming .byte 0 145291f3632SMatt Fleming .long 0 146291f3632SMatt Fleming .long 0x0 # startup_{32,64} 147291f3632SMatt Fleming .long 0 # Size of initialized data 148291f3632SMatt Fleming # on disk 149291f3632SMatt Fleming .long 0x0 # startup_{32,64} 150291f3632SMatt Fleming .long 0 # PointerToRelocations 151291f3632SMatt Fleming .long 0 # PointerToLineNumbers 152291f3632SMatt Fleming .word 0 # NumberOfRelocations 153291f3632SMatt Fleming .word 0 # NumberOfLineNumbers 154a3326a0dSArd Biesheuvel .long IMAGE_SCN_CNT_CODE | \ 155a3326a0dSArd Biesheuvel IMAGE_SCN_MEM_READ | \ 156bfab35f5SArd Biesheuvel IMAGE_SCN_MEM_EXECUTE # Characteristics 157291f3632SMatt Fleming 158291f3632SMatt Fleming # 159291f3632SMatt Fleming # The EFI application loader requires a relocation section 160743628e8SJordan Justen # because EFI applications must be relocatable. The .reloc 161743628e8SJordan Justen # offset & size fields are filled in by build.c. 162291f3632SMatt Fleming # 163291f3632SMatt Fleming .ascii ".reloc" 164291f3632SMatt Fleming .byte 0 165291f3632SMatt Fleming .byte 0 1662e064b1eSJordan Justen .long 0 1672e064b1eSJordan Justen .long 0 1682e064b1eSJordan Justen .long 0 # SizeOfRawData 1692e064b1eSJordan Justen .long 0 # PointerToRawData 170291f3632SMatt Fleming .long 0 # PointerToRelocations 171291f3632SMatt Fleming .long 0 # PointerToLineNumbers 172291f3632SMatt Fleming .word 0 # NumberOfRelocations 173291f3632SMatt Fleming .word 0 # NumberOfLineNumbers 174a3326a0dSArd Biesheuvel .long IMAGE_SCN_CNT_INITIALIZED_DATA | \ 175a3326a0dSArd Biesheuvel IMAGE_SCN_MEM_READ | \ 176bfab35f5SArd Biesheuvel IMAGE_SCN_MEM_DISCARDABLE # Characteristics 177743628e8SJordan Justen 17897aa2765SArd Biesheuvel#ifdef CONFIG_EFI_MIXED 17997aa2765SArd Biesheuvel # 18097aa2765SArd Biesheuvel # The offset & size fields are filled in by build.c. 18197aa2765SArd Biesheuvel # 18297aa2765SArd Biesheuvel .asciz ".compat" 18397aa2765SArd Biesheuvel .long 0 18497aa2765SArd Biesheuvel .long 0x0 18597aa2765SArd Biesheuvel .long 0 # Size of initialized data 18697aa2765SArd Biesheuvel # on disk 18797aa2765SArd Biesheuvel .long 0x0 18897aa2765SArd Biesheuvel .long 0 # PointerToRelocations 18997aa2765SArd Biesheuvel .long 0 # PointerToLineNumbers 19097aa2765SArd Biesheuvel .word 0 # NumberOfRelocations 19197aa2765SArd Biesheuvel .word 0 # NumberOfLineNumbers 192a3326a0dSArd Biesheuvel .long IMAGE_SCN_CNT_INITIALIZED_DATA | \ 193a3326a0dSArd Biesheuvel IMAGE_SCN_MEM_READ | \ 194bfab35f5SArd Biesheuvel IMAGE_SCN_MEM_DISCARDABLE # Characteristics 19597aa2765SArd Biesheuvel#endif 19697aa2765SArd Biesheuvel 197743628e8SJordan Justen .ascii ".text" 198743628e8SJordan Justen .byte 0 199743628e8SJordan Justen .byte 0 200743628e8SJordan Justen .byte 0 201*efa089e6SArd Biesheuvel .long ZO__end 202*efa089e6SArd Biesheuvel .long setup_size 203*efa089e6SArd Biesheuvel .long ZO__edata # Size of initialized data 204743628e8SJordan Justen # on disk 205*efa089e6SArd Biesheuvel .long setup_size 206743628e8SJordan Justen .long 0 # PointerToRelocations 207743628e8SJordan Justen .long 0 # PointerToLineNumbers 208743628e8SJordan Justen .word 0 # NumberOfRelocations 209743628e8SJordan Justen .word 0 # NumberOfLineNumbers 210a3326a0dSArd Biesheuvel .long IMAGE_SCN_CNT_CODE | \ 211a3326a0dSArd Biesheuvel IMAGE_SCN_MEM_READ | \ 212bfab35f5SArd Biesheuvel IMAGE_SCN_MEM_EXECUTE # Characteristics 213743628e8SJordan Justen 21497aa2765SArd Biesheuvel .set section_count, (. - section_table) / 40 215291f3632SMatt Fleming#endif /* CONFIG_EFI_STUB */ 21696ae6ea0SThomas Gleixner 21796ae6ea0SThomas Gleixner # Kernel attributes; used by setup. This is part 1 of the 21896ae6ea0SThomas Gleixner # header, from the old boot sector. 21996ae6ea0SThomas Gleixner 22096ae6ea0SThomas Gleixner .section ".header", "a" 22109c205afSH. Peter Anvin .globl sentinel 22209c205afSH. Peter Anvinsentinel: .byte 0xff, 0xff /* Used to detect broken loaders */ 22309c205afSH. Peter Anvin 22496ae6ea0SThomas Gleixner .globl hdr 22596ae6ea0SThomas Gleixnerhdr: 226093ab258SArd Biesheuvel .byte setup_sects - 1 22796ae6ea0SThomas Gleixnerroot_flags: .word ROOT_RDONLY 228aeb92067SArd Biesheuvelsyssize: .long ZO__edata / 16 2295e47c478SH. Peter Anvinram_size: .word 0 /* Obsolete */ 23096ae6ea0SThomas Gleixnervid_mode: .word SVGA_MODE 2317448e8e5SArd Biesheuvelroot_dev: .word 0 /* Default to major/minor 0/0 */ 23296ae6ea0SThomas Gleixnerboot_flag: .word 0xAA55 23396ae6ea0SThomas Gleixner 23496ae6ea0SThomas Gleixner # offset 512, entry point 23596ae6ea0SThomas Gleixner 23696ae6ea0SThomas Gleixner .globl _start 23796ae6ea0SThomas Gleixner_start: 23896ae6ea0SThomas Gleixner # Explicitly enter this as bytes, or the assembler 23996ae6ea0SThomas Gleixner # tries to generate a 3-byte jump here, which causes 24096ae6ea0SThomas Gleixner # everything else to push off to the wrong offset. 24196ae6ea0SThomas Gleixner .byte 0xeb # short (2-byte) jump 24296ae6ea0SThomas Gleixner .byte start_of_setup-1f 24396ae6ea0SThomas Gleixner1: 24496ae6ea0SThomas Gleixner 24596ae6ea0SThomas Gleixner # Part 2 of the header, from the old setup.S 24696ae6ea0SThomas Gleixner 24796ae6ea0SThomas Gleixner .ascii "HdrS" # header signature 248b3c72fc9SDaniel Kiper .word 0x020f # header version number (>= 0x0105) 24996ae6ea0SThomas Gleixner # or else old loadlin-1.5 will fail) 25096ae6ea0SThomas Gleixner .globl realmode_swtch 25196ae6ea0SThomas Gleixnerrealmode_swtch: .word 0, 0 # default_switch, SETUPSEG 2525e47c478SH. Peter Anvinstart_sys_seg: .word SYSSEG # obsolete and meaningless, but just 2535e47c478SH. Peter Anvin # in case something decided to "use" it 25496ae6ea0SThomas Gleixner .word kernel_version-512 # pointing to kernel version string 25596ae6ea0SThomas Gleixner # above section of header is compatible 25696ae6ea0SThomas Gleixner # with loadlin-1.5 (header v1.5). Don't 25796ae6ea0SThomas Gleixner # change it. 25896ae6ea0SThomas Gleixner 2595e47c478SH. Peter Anvintype_of_loader: .byte 0 # 0 means ancient bootloader, newer 2605e47c478SH. Peter Anvin # bootloaders know to change this. 261ff61f079SJonathan Corbet # See Documentation/arch/x86/boot.rst for 26296ae6ea0SThomas Gleixner # assigned ids 26396ae6ea0SThomas Gleixner 26496ae6ea0SThomas Gleixner# flags, unused bits must be zero (RFU) bit within loadflags 26596ae6ea0SThomas Gleixnerloadflags: 26609c205afSH. Peter Anvin .byte LOADED_HIGH # The kernel is to be loaded high 26796ae6ea0SThomas Gleixner 26896ae6ea0SThomas Gleixnersetup_move_size: .word 0x8000 # size to move, when setup is not 26996ae6ea0SThomas Gleixner # loaded at 0x90000. We will move setup 27096ae6ea0SThomas Gleixner # to 0x90000 then just before jumping 27196ae6ea0SThomas Gleixner # into the kernel. However, only the 27296ae6ea0SThomas Gleixner # loader knows how much data behind 27396ae6ea0SThomas Gleixner # us also needs to be loaded. 27496ae6ea0SThomas Gleixner 27596ae6ea0SThomas Gleixnercode32_start: # here loaders can put a different 27696ae6ea0SThomas Gleixner # start address for 32-bit code. 27796ae6ea0SThomas Gleixner .long 0x100000 # 0x100000 = default for big kernel 27896ae6ea0SThomas Gleixner 27996ae6ea0SThomas Gleixnerramdisk_image: .long 0 # address of loaded ramdisk image 28096ae6ea0SThomas Gleixner # Here the loader puts the 32-bit 28196ae6ea0SThomas Gleixner # address where it loaded the image. 28296ae6ea0SThomas Gleixner # This only will be read by the kernel. 28396ae6ea0SThomas Gleixner 28496ae6ea0SThomas Gleixnerramdisk_size: .long 0 # its size in bytes 28596ae6ea0SThomas Gleixner 28696ae6ea0SThomas Gleixnerbootsect_kludge: 28796ae6ea0SThomas Gleixner .long 0 # obsolete 28896ae6ea0SThomas Gleixner 2896b6815c6SH. Peter Anvinheap_end_ptr: .word _end+STACK_SIZE-512 2906b6815c6SH. Peter Anvin # (Header version 0x0201 or later) 29196ae6ea0SThomas Gleixner # space from here (exclusive) down to 29296ae6ea0SThomas Gleixner # end of setup code can be used by setup 29396ae6ea0SThomas Gleixner # for local heap purposes. 29496ae6ea0SThomas Gleixner 2955031296cSH. Peter Anvinext_loader_ver: 2965031296cSH. Peter Anvin .byte 0 # Extended boot loader version 2975031296cSH. Peter Anvinext_loader_type: 2985031296cSH. Peter Anvin .byte 0 # Extended boot loader type 2995031296cSH. Peter Anvin 30096ae6ea0SThomas Gleixnercmd_line_ptr: .long 0 # (Header version 0x0202 or later) 30196ae6ea0SThomas Gleixner # If nonzero, a 32-bit pointer 30296ae6ea0SThomas Gleixner # to the kernel command line. 30396ae6ea0SThomas Gleixner # The command line should be 30496ae6ea0SThomas Gleixner # located between the start of 30596ae6ea0SThomas Gleixner # setup and the end of low 30696ae6ea0SThomas Gleixner # memory (0xa0000), or it may 30796ae6ea0SThomas Gleixner # get overwritten before it 30896ae6ea0SThomas Gleixner # gets read. If this field is 30996ae6ea0SThomas Gleixner # used, there is no longer 31096ae6ea0SThomas Gleixner # anything magical about the 31196ae6ea0SThomas Gleixner # 0x90000 segment; the setup 31296ae6ea0SThomas Gleixner # can be located anywhere in 31396ae6ea0SThomas Gleixner # low memory 0x10000 or higher. 31496ae6ea0SThomas Gleixner 3153e920b53SBorislav Petkovinitrd_addr_max: .long 0x7fffffff 31696ae6ea0SThomas Gleixner # (Header version 0x0203 or later) 31796ae6ea0SThomas Gleixner # The highest safe address for 31896ae6ea0SThomas Gleixner # the contents of an initrd 319cf8fa920SH. Peter Anvin # The current kernel allows up to 4 GB, 320cf8fa920SH. Peter Anvin # but leave it at 2 GB to avoid 321cf8fa920SH. Peter Anvin # possible bootloader bugs. 32296ae6ea0SThomas Gleixner 32396ae6ea0SThomas Gleixnerkernel_alignment: .long CONFIG_PHYSICAL_ALIGN #physical addr alignment 32496ae6ea0SThomas Gleixner #required for protected mode 32596ae6ea0SThomas Gleixner #kernel 32696ae6ea0SThomas Gleixner#ifdef CONFIG_RELOCATABLE 32796ae6ea0SThomas Gleixnerrelocatable_kernel: .byte 1 32896ae6ea0SThomas Gleixner#else 32996ae6ea0SThomas Gleixnerrelocatable_kernel: .byte 0 33096ae6ea0SThomas Gleixner#endif 33137ba7ab5SH. Peter Anvinmin_alignment: .byte MIN_KERNEL_ALIGN_LG2 # minimum alignment 33209c205afSH. Peter Anvin 33309c205afSH. Peter Anvinxloadflags: 33409c205afSH. Peter Anvin#ifdef CONFIG_X86_64 33509c205afSH. Peter Anvin# define XLF0 XLF_KERNEL_64 /* 64-bit kernel */ 33609c205afSH. Peter Anvin#else 33709c205afSH. Peter Anvin# define XLF0 0 33809c205afSH. Peter Anvin#endif 339ee92d815SYinghai Lu 340745c5167SMatt Fleming#if defined(CONFIG_RELOCATABLE) && defined(CONFIG_X86_64) 341ee92d815SYinghai Lu /* kernel/boot_param/ramdisk could be loaded above 4g */ 342ee92d815SYinghai Lu# define XLF1 XLF_CAN_BE_LOADED_ABOVE_4G 343ee92d815SYinghai Lu#else 344ee92d815SYinghai Lu# define XLF1 0 345ee92d815SYinghai Lu#endif 346ee92d815SYinghai Lu 347cc3fdda2SArd Biesheuvel#ifdef CONFIG_EFI_HANDOVER_PROTOCOL 3487d453eeeSMatt Fleming# ifdef CONFIG_EFI_MIXED 3497d453eeeSMatt Fleming# define XLF23 (XLF_EFI_HANDOVER_32|XLF_EFI_HANDOVER_64) 3507d453eeeSMatt Fleming# else 35109c205afSH. Peter Anvin# ifdef CONFIG_X86_64 35209c205afSH. Peter Anvin# define XLF23 XLF_EFI_HANDOVER_64 /* 64-bit EFI handover ok */ 35309c205afSH. Peter Anvin# else 35409c205afSH. Peter Anvin# define XLF23 XLF_EFI_HANDOVER_32 /* 32-bit EFI handover ok */ 35509c205afSH. Peter Anvin# endif 3567d453eeeSMatt Fleming# endif 35709c205afSH. Peter Anvin#else 35809c205afSH. Peter Anvin# define XLF23 0 35909c205afSH. Peter Anvin#endif 360456a29ddSDave Young 3612965faa5SDave Young#if defined(CONFIG_X86_64) && defined(CONFIG_EFI) && defined(CONFIG_KEXEC_CORE) 362456a29ddSDave Young# define XLF4 XLF_EFI_KEXEC 363456a29ddSDave Young#else 364456a29ddSDave Young# define XLF4 0 365456a29ddSDave Young#endif 366456a29ddSDave Young 367f2d08c5dSBaoquan He#ifdef CONFIG_X86_64 368f2d08c5dSBaoquan He#ifdef CONFIG_X86_5LEVEL 369f2d08c5dSBaoquan He#define XLF56 (XLF_5LEVEL|XLF_5LEVEL_ENABLED) 370f2d08c5dSBaoquan He#else 371f2d08c5dSBaoquan He#define XLF56 XLF_5LEVEL 372f2d08c5dSBaoquan He#endif 373f2d08c5dSBaoquan He#else 374f2d08c5dSBaoquan He#define XLF56 0 375f2d08c5dSBaoquan He#endif 376f2d08c5dSBaoquan He 377f2d08c5dSBaoquan He .word XLF0 | XLF1 | XLF23 | XLF4 | XLF56 37896ae6ea0SThomas Gleixner 37996ae6ea0SThomas Gleixnercmdline_size: .long COMMAND_LINE_SIZE-1 #length of the command line, 38096ae6ea0SThomas Gleixner #added with boot protocol 38196ae6ea0SThomas Gleixner #version 2.06 38296ae6ea0SThomas Gleixner 383a24e7851SRusty Russellhardware_subarch: .long 0 # subarchitecture, added with 2.07 384a24e7851SRusty Russell # default to 0 for normal x86 PC 385a24e7851SRusty Russell 386a24e7851SRusty Russellhardware_subarch_data: .quad 0 387a24e7851SRusty Russell 38877d1a499SH. Peter Anvinpayload_offset: .long ZO_input_data 38977d1a499SH. Peter Anvinpayload_length: .long ZO_z_input_len 390099e1377SIan Campbell 3918b664aa6SHuang, Yingsetup_data: .quad 0 # 64-bit physical pointer to 3928b664aa6SHuang, Ying # single linked list of 3938b664aa6SHuang, Ying # struct setup_data 3948b664aa6SHuang, Ying 39537ba7ab5SH. Peter Anvinpref_address: .quad LOAD_PHYSICAL_ADDR # preferred load addr 39637ba7ab5SH. Peter Anvin 3974252db10SBaoquan He# 3984252db10SBaoquan He# Getting to provably safe in-place decompression is hard. Worst case 3994252db10SBaoquan He# behaviours need to be analyzed. Here let's take the decompression of 4004252db10SBaoquan He# a gzip-compressed kernel as example, to illustrate it: 4014252db10SBaoquan He# 4024252db10SBaoquan He# The file layout of gzip compressed kernel is: 4034252db10SBaoquan He# 4044252db10SBaoquan He# magic[2] 4054252db10SBaoquan He# method[1] 4064252db10SBaoquan He# flags[1] 4074252db10SBaoquan He# timestamp[4] 4084252db10SBaoquan He# extraflags[1] 4094252db10SBaoquan He# os[1] 4104252db10SBaoquan He# compressed data blocks[N] 4114252db10SBaoquan He# crc[4] orig_len[4] 4124252db10SBaoquan He# 4134252db10SBaoquan He# ... resulting in +18 bytes overhead of uncompressed data. 4144252db10SBaoquan He# 4154252db10SBaoquan He# (For more information, please refer to RFC 1951 and RFC 1952.) 4164252db10SBaoquan He# 4174252db10SBaoquan He# Files divided into blocks 4184252db10SBaoquan He# 1 bit (last block flag) 4194252db10SBaoquan He# 2 bits (block type) 4204252db10SBaoquan He# 4214252db10SBaoquan He# 1 block occurs every 32K -1 bytes or when there 50% compression 4224252db10SBaoquan He# has been achieved. The smallest block type encoding is always used. 4234252db10SBaoquan He# 4244252db10SBaoquan He# stored: 4254252db10SBaoquan He# 32 bits length in bytes. 4264252db10SBaoquan He# 4274252db10SBaoquan He# fixed: 4284252db10SBaoquan He# magic fixed tree. 4294252db10SBaoquan He# symbols. 4304252db10SBaoquan He# 4314252db10SBaoquan He# dynamic: 4324252db10SBaoquan He# dynamic tree encoding. 4334252db10SBaoquan He# symbols. 4344252db10SBaoquan He# 4354252db10SBaoquan He# 4364252db10SBaoquan He# The buffer for decompression in place is the length of the uncompressed 4374252db10SBaoquan He# data, plus a small amount extra to keep the algorithm safe. The 4384252db10SBaoquan He# compressed data is placed at the end of the buffer. The output pointer 4394252db10SBaoquan He# is placed at the start of the buffer and the input pointer is placed 4404252db10SBaoquan He# where the compressed data starts. Problems will occur when the output 4414252db10SBaoquan He# pointer overruns the input pointer. 4424252db10SBaoquan He# 4434252db10SBaoquan He# The output pointer can only overrun the input pointer if the input 4444252db10SBaoquan He# pointer is moving faster than the output pointer. A condition only 4454252db10SBaoquan He# triggered by data whose compressed form is larger than the uncompressed 4464252db10SBaoquan He# form. 4474252db10SBaoquan He# 4484252db10SBaoquan He# The worst case at the block level is a growth of the compressed data 4494252db10SBaoquan He# of 5 bytes per 32767 bytes. 4504252db10SBaoquan He# 4514252db10SBaoquan He# The worst case internal to a compressed block is very hard to figure. 4524252db10SBaoquan He# The worst case can at least be bounded by having one bit that represents 4534252db10SBaoquan He# 32764 bytes and then all of the rest of the bytes representing the very 4544252db10SBaoquan He# very last byte. 4554252db10SBaoquan He# 4564252db10SBaoquan He# All of which is enough to compute an amount of extra data that is required 4574252db10SBaoquan He# to be safe. To avoid problems at the block level allocating 5 extra bytes 4584252db10SBaoquan He# per 32767 bytes of data is sufficient. To avoid problems internal to a 4594252db10SBaoquan He# block adding an extra 32767 bytes (the worst case uncompressed block size) 4604252db10SBaoquan He# is sufficient, to ensure that in the worst case the decompressed data for 4614252db10SBaoquan He# block will stop the byte before the compressed data for a block begins. 4624252db10SBaoquan He# To avoid problems with the compressed data's meta information an extra 18 4634252db10SBaoquan He# bytes are needed. Leading to the formula: 4644252db10SBaoquan He# 465d607251bSYinghai Lu# extra_bytes = (uncompressed_size >> 12) + 32768 + 18 4664252db10SBaoquan He# 4674252db10SBaoquan He# Adding 8 bytes per 32K is a bit excessive but much easier to calculate. 4684252db10SBaoquan He# Adding 32768 instead of 32767 just makes for round numbers. 4694252db10SBaoquan He# 4704252db10SBaoquan He# Above analysis is for decompressing gzip compressed kernel only. Up to 4714252db10SBaoquan He# now 6 different decompressor are supported all together. And among them 4724252db10SBaoquan He# xz stores data in chunks and has maximum chunk of 64K. Hence safety 4734252db10SBaoquan He# margin should be updated to cover all decompressors so that we don't 4744252db10SBaoquan He# need to deal with each of them separately. Please check 4754252db10SBaoquan He# the description in lib/decompressor_xxx.c for specific information. 4764252db10SBaoquan He# 4774252db10SBaoquan He# extra_bytes = (uncompressed_size >> 12) + 65536 + 128 4785746f055SJan H. Schönherr# 4795746f055SJan H. Schönherr# LZ4 is even worse: data that cannot be further compressed grows by 0.4%, 4805746f055SJan H. Schönherr# or one byte per 256 bytes. OTOH, we can safely get rid of the +128 as 4815746f055SJan H. Schönherr# the size-dependent part now grows so fast. 4825746f055SJan H. Schönherr# 4835746f055SJan H. Schönherr# extra_bytes = (uncompressed_size >> 8) + 65536 4840fe4f4efSNick Terrell# 4850fe4f4efSNick Terrell# ZSTD compressed data grows by at most 3 bytes per 128K, and only has a 22 4860fe4f4efSNick Terrell# byte fixed overhead but has a maximum block size of 128K, so it needs a 4870fe4f4efSNick Terrell# larger margin. 4880fe4f4efSNick Terrell# 4890fe4f4efSNick Terrell# extra_bytes = (uncompressed_size >> 8) + 131072 4904252db10SBaoquan He 4910fe4f4efSNick Terrell#define ZO_z_extra_bytes ((ZO_z_output_len >> 8) + 131072) 492d607251bSYinghai Lu#if ZO_z_output_len > ZO_z_input_len 493d607251bSYinghai Lu# define ZO_z_extract_offset (ZO_z_output_len + ZO_z_extra_bytes - \ 494d607251bSYinghai Lu ZO_z_input_len) 495d607251bSYinghai Lu#else 496d607251bSYinghai Lu# define ZO_z_extract_offset ZO_z_extra_bytes 497d607251bSYinghai Lu#endif 498d607251bSYinghai Lu 499d607251bSYinghai Lu/* 500d607251bSYinghai Lu * The extract_offset has to be bigger than ZO head section. Otherwise when 501d607251bSYinghai Lu * the head code is running to move ZO to the end of the buffer, it will 502d607251bSYinghai Lu * overwrite the head code itself. 503d607251bSYinghai Lu */ 504d607251bSYinghai Lu#if (ZO__ehead - ZO_startup_32) > ZO_z_extract_offset 505d607251bSYinghai Lu# define ZO_z_min_extract_offset ((ZO__ehead - ZO_startup_32 + 4095) & ~4095) 506d607251bSYinghai Lu#else 507d607251bSYinghai Lu# define ZO_z_min_extract_offset ((ZO_z_extract_offset + 4095) & ~4095) 508d607251bSYinghai Lu#endif 509d607251bSYinghai Lu 510d607251bSYinghai Lu#define ZO_INIT_SIZE (ZO__end - ZO_startup_32 + ZO_z_min_extract_offset) 511d607251bSYinghai Lu 51237ba7ab5SH. Peter Anvin#define VO_INIT_SIZE (VO__end - VO__text) 51337ba7ab5SH. Peter Anvin#if ZO_INIT_SIZE > VO_INIT_SIZE 51437ba7ab5SH. Peter Anvin# define INIT_SIZE ZO_INIT_SIZE 51537ba7ab5SH. Peter Anvin#else 51637ba7ab5SH. Peter Anvin# define INIT_SIZE VO_INIT_SIZE 51737ba7ab5SH. Peter Anvin#endif 518d607251bSYinghai Lu 519eac95634SArd Biesheuvel .macro __handover_offset 520eac95634SArd Biesheuvel#ifndef CONFIG_EFI_HANDOVER_PROTOCOL 521eac95634SArd Biesheuvel .long 0 522eac95634SArd Biesheuvel#elif !defined(CONFIG_X86_64) 523eac95634SArd Biesheuvel .long ZO_efi32_stub_entry 524eac95634SArd Biesheuvel#else 525eac95634SArd Biesheuvel /* Yes, this is really how we defined it :( */ 526eac95634SArd Biesheuvel .long ZO_efi64_stub_entry - 0x200 527eac95634SArd Biesheuvel#ifdef CONFIG_EFI_MIXED 528eac95634SArd Biesheuvel .if ZO_efi32_stub_entry != ZO_efi64_stub_entry - 0x200 529eac95634SArd Biesheuvel .error "32-bit and 64-bit EFI entry points do not match" 530eac95634SArd Biesheuvel .endif 531eac95634SArd Biesheuvel#endif 532eac95634SArd Biesheuvel#endif 533eac95634SArd Biesheuvel .endm 534eac95634SArd Biesheuvel 53537ba7ab5SH. Peter Anvininit_size: .long INIT_SIZE # kernel initialization size 536eac95634SArd Biesheuvelhandover_offset: __handover_offset 5372e765c02SArd Biesheuvelkernel_info_offset: .long ZO_kernel_info 53837ba7ab5SH. Peter Anvin 53996ae6ea0SThomas Gleixner# End of setup header ##################################################### 54096ae6ea0SThomas Gleixner 5417a734e7dSH. Peter Anvin .section ".entrytext", "ax" 54296ae6ea0SThomas Gleixnerstart_of_setup: 54396ae6ea0SThomas Gleixner# Force %es = %ds 54496ae6ea0SThomas Gleixner movw %ds, %ax 54596ae6ea0SThomas Gleixner movw %ax, %es 54696ae6ea0SThomas Gleixner cld 54796ae6ea0SThomas Gleixner 54816252da6SJens Rottmann# Apparently some ancient versions of LILO invoked the kernel with %ss != %ds, 54916252da6SJens Rottmann# which happened to work by accident for the old code. Recalculate the stack 55016252da6SJens Rottmann# pointer if %ss is invalid. Otherwise leave it alone, LOADLIN sets up the 55116252da6SJens Rottmann# stack behind its own code, so we can't blindly put it directly past the heap. 5526b6815c6SH. Peter Anvin 5536b6815c6SH. Peter Anvin movw %ss, %dx 5546b6815c6SH. Peter Anvin cmpw %ax, %dx # %ds == %ss? 5556b6815c6SH. Peter Anvin movw %sp, %dx 55616252da6SJens Rottmann je 2f # -> assume %sp is reasonably set 5576b6815c6SH. Peter Anvin 55816252da6SJens Rottmann # Invalid %ss, make up a new stack 55916252da6SJens Rottmann movw $_end, %dx 56016252da6SJens Rottmann testb $CAN_USE_HEAP, loadflags 56116252da6SJens Rottmann jz 1f 56216252da6SJens Rottmann movw heap_end_ptr, %dx 56316252da6SJens Rottmann1: addw $STACK_SIZE, %dx 56416252da6SJens Rottmann jnc 2f 56516252da6SJens Rottmann xorw %dx, %dx # Prevent wraparound 56616252da6SJens Rottmann 56716252da6SJens Rottmann2: # Now %dx should point to the end of our stack space 5686b6815c6SH. Peter Anvin andw $~3, %dx # dword align (might as well...) 5696b6815c6SH. Peter Anvin jnz 3f 5706b6815c6SH. Peter Anvin movw $0xfffc, %dx # Make sure we're not zero 57116252da6SJens Rottmann3: movw %ax, %ss 5726b6815c6SH. Peter Anvin movzwl %dx, %esp # Clear upper half of %esp 5736b6815c6SH. Peter Anvin sti # Now we should have a working stack 5746b6815c6SH. Peter Anvin 5756b6815c6SH. Peter Anvin# We will have entered with %cs = %ds+0x20, normalize %cs so 5766b6815c6SH. Peter Anvin# it is on par with the other segments. 5776b6815c6SH. Peter Anvin pushw %ds 5786b6815c6SH. Peter Anvin pushw $6f 5796b6815c6SH. Peter Anvin lretw 5806b6815c6SH. Peter Anvin6: 58196ae6ea0SThomas Gleixner 58296ae6ea0SThomas Gleixner# Check signature at end of setup 58396ae6ea0SThomas Gleixner cmpl $0x5a5aaa55, setup_sig 58496ae6ea0SThomas Gleixner jne setup_bad 58596ae6ea0SThomas Gleixner 58696ae6ea0SThomas Gleixner# Zero the bss 58796ae6ea0SThomas Gleixner movw $__bss_start, %di 58896ae6ea0SThomas Gleixner movw $_end+3, %cx 58996ae6ea0SThomas Gleixner xorl %eax, %eax 59096ae6ea0SThomas Gleixner subw %di, %cx 59196ae6ea0SThomas Gleixner shrw $2, %cx 59296ae6ea0SThomas Gleixner rep; stosl 59396ae6ea0SThomas Gleixner 59496ae6ea0SThomas Gleixner# Jump to C code (should not return) 59596ae6ea0SThomas Gleixner calll main 59696ae6ea0SThomas Gleixner 59796ae6ea0SThomas Gleixner# Setup corrupt somehow... 59896ae6ea0SThomas Gleixnersetup_bad: 59996ae6ea0SThomas Gleixner movl $setup_corrupt, %eax 60096ae6ea0SThomas Gleixner calll puts 60196ae6ea0SThomas Gleixner # Fall through... 60296ae6ea0SThomas Gleixner 60396ae6ea0SThomas Gleixner .globl die 60496ae6ea0SThomas Gleixner .type die, @function 60596ae6ea0SThomas Gleixnerdie: 60696ae6ea0SThomas Gleixner hlt 60796ae6ea0SThomas Gleixner jmp die 60896ae6ea0SThomas Gleixner 60996ae6ea0SThomas Gleixner .size die, .-die 61096ae6ea0SThomas Gleixner 61196ae6ea0SThomas Gleixner .section ".initdata", "a" 61296ae6ea0SThomas Gleixnersetup_corrupt: 61396ae6ea0SThomas Gleixner .byte 7 61496ae6ea0SThomas Gleixner .string "No setup signature found...\n" 615