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 393e3eabe2SArd Biesheuvel .set salign, 0x1000 403e3eabe2SArd Biesheuvel .set falign, 0x200 413e3eabe2SArd Biesheuvel 4296ae6ea0SThomas Gleixner .code16 4396ae6ea0SThomas Gleixner .section ".bstext", "ax" 44291f3632SMatt Fleming#ifdef CONFIG_EFI_STUB 45291f3632SMatt Fleming # "MZ", MS-DOS header 46a3326a0dSArd Biesheuvel .word MZ_MAGIC 4729636a5cSArd Biesheuvel .org 0x38 48291f3632SMatt Fleming # 49291f3632SMatt Fleming # Offset to the PE header. 50291f3632SMatt Fleming # 5129636a5cSArd Biesheuvel .long LINUX_PE_MAGIC 52291f3632SMatt Fleming .long pe_header 53291f3632SMatt Flemingpe_header: 54a3326a0dSArd Biesheuvel .long PE_MAGIC 55291f3632SMatt Fleming 56291f3632SMatt Flemingcoff_header: 57291f3632SMatt Fleming#ifdef CONFIG_X86_32 58a3326a0dSArd Biesheuvel .set image_file_add_flags, IMAGE_FILE_32BIT_MACHINE 59a3326a0dSArd Biesheuvel .set pe_opt_magic, PE_OPT_MAGIC_PE32 60a3326a0dSArd Biesheuvel .word IMAGE_FILE_MACHINE_I386 61291f3632SMatt Fleming#else 62a3326a0dSArd Biesheuvel .set image_file_add_flags, 0 63a3326a0dSArd Biesheuvel .set pe_opt_magic, PE_OPT_MAGIC_PE32PLUS 64a3326a0dSArd Biesheuvel .word IMAGE_FILE_MACHINE_AMD64 65291f3632SMatt Fleming#endif 6697aa2765SArd Biesheuvel .word section_count # nr_sections 67291f3632SMatt Fleming .long 0 # TimeDateStamp 68291f3632SMatt Fleming .long 0 # PointerToSymbolTable 69291f3632SMatt Fleming .long 1 # NumberOfSymbols 70291f3632SMatt Fleming .word section_table - optional_header # SizeOfOptionalHeader 71a3326a0dSArd Biesheuvel .word IMAGE_FILE_EXECUTABLE_IMAGE | \ 72a3326a0dSArd Biesheuvel image_file_add_flags | \ 73a3326a0dSArd Biesheuvel IMAGE_FILE_DEBUG_STRIPPED | \ 74a3326a0dSArd Biesheuvel IMAGE_FILE_LINE_NUMS_STRIPPED # Characteristics 75291f3632SMatt Fleming 76291f3632SMatt Flemingoptional_header: 77a3326a0dSArd Biesheuvel .word pe_opt_magic 78291f3632SMatt Fleming .byte 0x02 # MajorLinkerVersion 79291f3632SMatt Fleming .byte 0x14 # MinorLinkerVersion 80291f3632SMatt Fleming 8134951f3cSArd Biesheuvel .long ZO__data # SizeOfCode 82291f3632SMatt Fleming 8334951f3cSArd Biesheuvel .long ZO__end - ZO__data # SizeOfInitializedData 84291f3632SMatt Fleming .long 0 # SizeOfUninitializedData 85291f3632SMatt Fleming 86efa089e6SArd Biesheuvel .long setup_size + ZO_efi_pe_entry # AddressOfEntryPoint 87291f3632SMatt Fleming 883e3eabe2SArd Biesheuvel .long setup_size # BaseOfCode 89291f3632SMatt Fleming#ifdef CONFIG_X86_32 90291f3632SMatt Fleming .long 0 # data 91291f3632SMatt Fleming#endif 92291f3632SMatt Fleming 93291f3632SMatt Flemingextra_header_fields: 94291f3632SMatt Fleming#ifdef CONFIG_X86_32 958eace5b3SArd Biesheuvel .long 0 # ImageBase 96291f3632SMatt Fleming#else 978eace5b3SArd Biesheuvel .quad 0 # ImageBase 98291f3632SMatt Fleming#endif 993e3eabe2SArd Biesheuvel .long salign # SectionAlignment 1003e3eabe2SArd Biesheuvel .long falign # FileAlignment 101291f3632SMatt Fleming .word 0 # MajorOperatingSystemVersion 102291f3632SMatt Fleming .word 0 # MinorOperatingSystemVersion 103148d3f71SArd Biesheuvel .word LINUX_EFISTUB_MAJOR_VERSION # MajorImageVersion 104148d3f71SArd Biesheuvel .word LINUX_EFISTUB_MINOR_VERSION # MinorImageVersion 105291f3632SMatt Fleming .word 0 # MajorSubsystemVersion 106291f3632SMatt Fleming .word 0 # MinorSubsystemVersion 107291f3632SMatt Fleming .long 0 # Win32VersionValue 108291f3632SMatt Fleming 109*1ad55cecSArd Biesheuvel .long setup_size + ZO__end # SizeOfImage 110291f3632SMatt Fleming 1113e3eabe2SArd Biesheuvel .long salign # SizeOfHeaders 112291f3632SMatt Fleming .long 0 # CheckSum 113a3326a0dSArd Biesheuvel .word IMAGE_SUBSYSTEM_EFI_APPLICATION # Subsystem (EFI application) 11424b72bb1SPeter Jones .word IMAGE_DLL_CHARACTERISTICS_NX_COMPAT # DllCharacteristics 115291f3632SMatt Fleming#ifdef CONFIG_X86_32 116291f3632SMatt Fleming .long 0 # SizeOfStackReserve 117291f3632SMatt Fleming .long 0 # SizeOfStackCommit 118291f3632SMatt Fleming .long 0 # SizeOfHeapReserve 119291f3632SMatt Fleming .long 0 # SizeOfHeapCommit 120291f3632SMatt Fleming#else 121291f3632SMatt Fleming .quad 0 # SizeOfStackReserve 122291f3632SMatt Fleming .quad 0 # SizeOfStackCommit 123291f3632SMatt Fleming .quad 0 # SizeOfHeapReserve 124291f3632SMatt Fleming .quad 0 # SizeOfHeapCommit 125291f3632SMatt Fleming#endif 126291f3632SMatt Fleming .long 0 # LoaderFlags 127a3326a0dSArd Biesheuvel .long (section_table - .) / 8 # NumberOfRvaAndSizes 128291f3632SMatt Fleming 129291f3632SMatt Fleming .quad 0 # ExportTable 130291f3632SMatt Fleming .quad 0 # ImportTable 131291f3632SMatt Fleming .quad 0 # ResourceTable 132291f3632SMatt Fleming .quad 0 # ExceptionTable 133291f3632SMatt Fleming .quad 0 # CertificationTable 134291f3632SMatt Fleming .quad 0 # BaseRelocationTable 135291f3632SMatt Fleming 136291f3632SMatt Fleming # Section table 137291f3632SMatt Flemingsection_table: 138743628e8SJordan Justen .ascii ".setup" 139291f3632SMatt Fleming .byte 0 140291f3632SMatt Fleming .byte 0 141*1ad55cecSArd Biesheuvel .long pecompat_fstart - salign # VirtualSize 1423e3eabe2SArd Biesheuvel .long salign # VirtualAddress 1433e3eabe2SArd Biesheuvel .long pecompat_fstart - salign # SizeOfRawData 1443e3eabe2SArd Biesheuvel .long salign # PointerToRawData 145291f3632SMatt Fleming 1463e3eabe2SArd Biesheuvel .long 0, 0, 0 147a3326a0dSArd Biesheuvel .long IMAGE_SCN_CNT_INITIALIZED_DATA | \ 148a3326a0dSArd Biesheuvel IMAGE_SCN_MEM_READ | \ 149bfab35f5SArd Biesheuvel IMAGE_SCN_MEM_DISCARDABLE # Characteristics 15097aa2765SArd Biesheuvel 1513e3eabe2SArd Biesheuvel#ifdef CONFIG_EFI_MIXED 1523e3eabe2SArd Biesheuvel .asciz ".compat" 1533e3eabe2SArd Biesheuvel 154*1ad55cecSArd Biesheuvel .long pecompat_fsize # VirtualSize 155*1ad55cecSArd Biesheuvel .long pecompat_fstart # VirtualAddress 1563e3eabe2SArd Biesheuvel .long pecompat_fsize # SizeOfRawData 1573e3eabe2SArd Biesheuvel .long pecompat_fstart # PointerToRawData 1583e3eabe2SArd Biesheuvel 1593e3eabe2SArd Biesheuvel .long 0, 0, 0 1603e3eabe2SArd Biesheuvel .long IMAGE_SCN_CNT_INITIALIZED_DATA | \ 1613e3eabe2SArd Biesheuvel IMAGE_SCN_MEM_READ | \ 1623e3eabe2SArd Biesheuvel IMAGE_SCN_MEM_DISCARDABLE # Characteristics 1633e3eabe2SArd Biesheuvel 1643e3eabe2SArd Biesheuvel /* 1653e3eabe2SArd Biesheuvel * Put the IA-32 machine type and the associated entry point address in 1663e3eabe2SArd Biesheuvel * the .compat section, so loaders can figure out which other execution 1673e3eabe2SArd Biesheuvel * modes this image supports. 1683e3eabe2SArd Biesheuvel */ 1693e3eabe2SArd Biesheuvel .pushsection ".pecompat", "a", @progbits 170*1ad55cecSArd Biesheuvel .balign salign 1713e3eabe2SArd Biesheuvel .globl pecompat_fstart 1723e3eabe2SArd Biesheuvelpecompat_fstart: 1733e3eabe2SArd Biesheuvel .byte 0x1 # Version 1743e3eabe2SArd Biesheuvel .byte 8 # Size 1753e3eabe2SArd Biesheuvel .word IMAGE_FILE_MACHINE_I386 # PE machine type 1763e3eabe2SArd Biesheuvel .long setup_size + ZO_efi32_pe_entry # Entrypoint 177*1ad55cecSArd Biesheuvel .byte 0x0 # Sentinel 1783e3eabe2SArd Biesheuvel .popsection 1793e3eabe2SArd Biesheuvel#else 1803e3eabe2SArd Biesheuvel .set pecompat_fstart, setup_size 1813e3eabe2SArd Biesheuvel#endif 182743628e8SJordan Justen .ascii ".text" 183743628e8SJordan Justen .byte 0 184743628e8SJordan Justen .byte 0 185743628e8SJordan Justen .byte 0 18634951f3cSArd Biesheuvel .long ZO__data 187efa089e6SArd Biesheuvel .long setup_size 18834951f3cSArd Biesheuvel .long ZO__data # Size of initialized data 189743628e8SJordan Justen # on disk 190efa089e6SArd Biesheuvel .long setup_size 191743628e8SJordan Justen .long 0 # PointerToRelocations 192743628e8SJordan Justen .long 0 # PointerToLineNumbers 193743628e8SJordan Justen .word 0 # NumberOfRelocations 194743628e8SJordan Justen .word 0 # NumberOfLineNumbers 195a3326a0dSArd Biesheuvel .long IMAGE_SCN_CNT_CODE | \ 196a3326a0dSArd Biesheuvel IMAGE_SCN_MEM_READ | \ 197bfab35f5SArd Biesheuvel IMAGE_SCN_MEM_EXECUTE # Characteristics 198743628e8SJordan Justen 19934951f3cSArd Biesheuvel .ascii ".data\0\0\0" 20034951f3cSArd Biesheuvel .long ZO__end - ZO__data # VirtualSize 20134951f3cSArd Biesheuvel .long setup_size + ZO__data # VirtualAddress 20234951f3cSArd Biesheuvel .long ZO__edata - ZO__data # SizeOfRawData 20334951f3cSArd Biesheuvel .long setup_size + ZO__data # PointerToRawData 20434951f3cSArd Biesheuvel 20534951f3cSArd Biesheuvel .long 0, 0, 0 20634951f3cSArd Biesheuvel .long IMAGE_SCN_CNT_INITIALIZED_DATA | \ 20734951f3cSArd Biesheuvel IMAGE_SCN_MEM_READ | \ 20834951f3cSArd Biesheuvel IMAGE_SCN_MEM_WRITE # Characteristics 20934951f3cSArd Biesheuvel 21097aa2765SArd Biesheuvel .set section_count, (. - section_table) / 40 211291f3632SMatt Fleming#endif /* CONFIG_EFI_STUB */ 21296ae6ea0SThomas Gleixner 21396ae6ea0SThomas Gleixner # Kernel attributes; used by setup. This is part 1 of the 21496ae6ea0SThomas Gleixner # header, from the old boot sector. 21596ae6ea0SThomas Gleixner 21696ae6ea0SThomas Gleixner .section ".header", "a" 21709c205afSH. Peter Anvin .globl sentinel 21809c205afSH. Peter Anvinsentinel: .byte 0xff, 0xff /* Used to detect broken loaders */ 21909c205afSH. Peter Anvin 22096ae6ea0SThomas Gleixner .globl hdr 22196ae6ea0SThomas Gleixnerhdr: 222093ab258SArd Biesheuvel .byte setup_sects - 1 22396ae6ea0SThomas Gleixnerroot_flags: .word ROOT_RDONLY 224aeb92067SArd Biesheuvelsyssize: .long ZO__edata / 16 2255e47c478SH. Peter Anvinram_size: .word 0 /* Obsolete */ 22696ae6ea0SThomas Gleixnervid_mode: .word SVGA_MODE 2277448e8e5SArd Biesheuvelroot_dev: .word 0 /* Default to major/minor 0/0 */ 22896ae6ea0SThomas Gleixnerboot_flag: .word 0xAA55 22996ae6ea0SThomas Gleixner 23096ae6ea0SThomas Gleixner # offset 512, entry point 23196ae6ea0SThomas Gleixner 23296ae6ea0SThomas Gleixner .globl _start 23396ae6ea0SThomas Gleixner_start: 23496ae6ea0SThomas Gleixner # Explicitly enter this as bytes, or the assembler 23596ae6ea0SThomas Gleixner # tries to generate a 3-byte jump here, which causes 23696ae6ea0SThomas Gleixner # everything else to push off to the wrong offset. 23796ae6ea0SThomas Gleixner .byte 0xeb # short (2-byte) jump 23896ae6ea0SThomas Gleixner .byte start_of_setup-1f 23996ae6ea0SThomas Gleixner1: 24096ae6ea0SThomas Gleixner 24196ae6ea0SThomas Gleixner # Part 2 of the header, from the old setup.S 24296ae6ea0SThomas Gleixner 24396ae6ea0SThomas Gleixner .ascii "HdrS" # header signature 244b3c72fc9SDaniel Kiper .word 0x020f # header version number (>= 0x0105) 24596ae6ea0SThomas Gleixner # or else old loadlin-1.5 will fail) 24696ae6ea0SThomas Gleixner .globl realmode_swtch 24796ae6ea0SThomas Gleixnerrealmode_swtch: .word 0, 0 # default_switch, SETUPSEG 2485e47c478SH. Peter Anvinstart_sys_seg: .word SYSSEG # obsolete and meaningless, but just 2495e47c478SH. Peter Anvin # in case something decided to "use" it 25096ae6ea0SThomas Gleixner .word kernel_version-512 # pointing to kernel version string 25196ae6ea0SThomas Gleixner # above section of header is compatible 25296ae6ea0SThomas Gleixner # with loadlin-1.5 (header v1.5). Don't 25396ae6ea0SThomas Gleixner # change it. 25496ae6ea0SThomas Gleixner 2555e47c478SH. Peter Anvintype_of_loader: .byte 0 # 0 means ancient bootloader, newer 2565e47c478SH. Peter Anvin # bootloaders know to change this. 257ff61f079SJonathan Corbet # See Documentation/arch/x86/boot.rst for 25896ae6ea0SThomas Gleixner # assigned ids 25996ae6ea0SThomas Gleixner 26096ae6ea0SThomas Gleixner# flags, unused bits must be zero (RFU) bit within loadflags 26196ae6ea0SThomas Gleixnerloadflags: 26209c205afSH. Peter Anvin .byte LOADED_HIGH # The kernel is to be loaded high 26396ae6ea0SThomas Gleixner 26496ae6ea0SThomas Gleixnersetup_move_size: .word 0x8000 # size to move, when setup is not 26596ae6ea0SThomas Gleixner # loaded at 0x90000. We will move setup 26696ae6ea0SThomas Gleixner # to 0x90000 then just before jumping 26796ae6ea0SThomas Gleixner # into the kernel. However, only the 26896ae6ea0SThomas Gleixner # loader knows how much data behind 26996ae6ea0SThomas Gleixner # us also needs to be loaded. 27096ae6ea0SThomas Gleixner 27196ae6ea0SThomas Gleixnercode32_start: # here loaders can put a different 27296ae6ea0SThomas Gleixner # start address for 32-bit code. 27396ae6ea0SThomas Gleixner .long 0x100000 # 0x100000 = default for big kernel 27496ae6ea0SThomas Gleixner 27596ae6ea0SThomas Gleixnerramdisk_image: .long 0 # address of loaded ramdisk image 27696ae6ea0SThomas Gleixner # Here the loader puts the 32-bit 27796ae6ea0SThomas Gleixner # address where it loaded the image. 27896ae6ea0SThomas Gleixner # This only will be read by the kernel. 27996ae6ea0SThomas Gleixner 28096ae6ea0SThomas Gleixnerramdisk_size: .long 0 # its size in bytes 28196ae6ea0SThomas Gleixner 28296ae6ea0SThomas Gleixnerbootsect_kludge: 28396ae6ea0SThomas Gleixner .long 0 # obsolete 28496ae6ea0SThomas Gleixner 2856b6815c6SH. Peter Anvinheap_end_ptr: .word _end+STACK_SIZE-512 2866b6815c6SH. Peter Anvin # (Header version 0x0201 or later) 28796ae6ea0SThomas Gleixner # space from here (exclusive) down to 28896ae6ea0SThomas Gleixner # end of setup code can be used by setup 28996ae6ea0SThomas Gleixner # for local heap purposes. 29096ae6ea0SThomas Gleixner 2915031296cSH. Peter Anvinext_loader_ver: 2925031296cSH. Peter Anvin .byte 0 # Extended boot loader version 2935031296cSH. Peter Anvinext_loader_type: 2945031296cSH. Peter Anvin .byte 0 # Extended boot loader type 2955031296cSH. Peter Anvin 29696ae6ea0SThomas Gleixnercmd_line_ptr: .long 0 # (Header version 0x0202 or later) 29796ae6ea0SThomas Gleixner # If nonzero, a 32-bit pointer 29896ae6ea0SThomas Gleixner # to the kernel command line. 29996ae6ea0SThomas Gleixner # The command line should be 30096ae6ea0SThomas Gleixner # located between the start of 30196ae6ea0SThomas Gleixner # setup and the end of low 30296ae6ea0SThomas Gleixner # memory (0xa0000), or it may 30396ae6ea0SThomas Gleixner # get overwritten before it 30496ae6ea0SThomas Gleixner # gets read. If this field is 30596ae6ea0SThomas Gleixner # used, there is no longer 30696ae6ea0SThomas Gleixner # anything magical about the 30796ae6ea0SThomas Gleixner # 0x90000 segment; the setup 30896ae6ea0SThomas Gleixner # can be located anywhere in 30996ae6ea0SThomas Gleixner # low memory 0x10000 or higher. 31096ae6ea0SThomas Gleixner 3113e920b53SBorislav Petkovinitrd_addr_max: .long 0x7fffffff 31296ae6ea0SThomas Gleixner # (Header version 0x0203 or later) 31396ae6ea0SThomas Gleixner # The highest safe address for 31496ae6ea0SThomas Gleixner # the contents of an initrd 315cf8fa920SH. Peter Anvin # The current kernel allows up to 4 GB, 316cf8fa920SH. Peter Anvin # but leave it at 2 GB to avoid 317cf8fa920SH. Peter Anvin # possible bootloader bugs. 31896ae6ea0SThomas Gleixner 31996ae6ea0SThomas Gleixnerkernel_alignment: .long CONFIG_PHYSICAL_ALIGN #physical addr alignment 32096ae6ea0SThomas Gleixner #required for protected mode 32196ae6ea0SThomas Gleixner #kernel 32296ae6ea0SThomas Gleixner#ifdef CONFIG_RELOCATABLE 32396ae6ea0SThomas Gleixnerrelocatable_kernel: .byte 1 32496ae6ea0SThomas Gleixner#else 32596ae6ea0SThomas Gleixnerrelocatable_kernel: .byte 0 32696ae6ea0SThomas Gleixner#endif 32737ba7ab5SH. Peter Anvinmin_alignment: .byte MIN_KERNEL_ALIGN_LG2 # minimum alignment 32809c205afSH. Peter Anvin 32909c205afSH. Peter Anvinxloadflags: 33009c205afSH. Peter Anvin#ifdef CONFIG_X86_64 33109c205afSH. Peter Anvin# define XLF0 XLF_KERNEL_64 /* 64-bit kernel */ 33209c205afSH. Peter Anvin#else 33309c205afSH. Peter Anvin# define XLF0 0 33409c205afSH. Peter Anvin#endif 335ee92d815SYinghai Lu 336745c5167SMatt Fleming#if defined(CONFIG_RELOCATABLE) && defined(CONFIG_X86_64) 337ee92d815SYinghai Lu /* kernel/boot_param/ramdisk could be loaded above 4g */ 338ee92d815SYinghai Lu# define XLF1 XLF_CAN_BE_LOADED_ABOVE_4G 339ee92d815SYinghai Lu#else 340ee92d815SYinghai Lu# define XLF1 0 341ee92d815SYinghai Lu#endif 342ee92d815SYinghai Lu 343cc3fdda2SArd Biesheuvel#ifdef CONFIG_EFI_HANDOVER_PROTOCOL 3447d453eeeSMatt Fleming# ifdef CONFIG_EFI_MIXED 3457d453eeeSMatt Fleming# define XLF23 (XLF_EFI_HANDOVER_32|XLF_EFI_HANDOVER_64) 3467d453eeeSMatt Fleming# else 34709c205afSH. Peter Anvin# ifdef CONFIG_X86_64 34809c205afSH. Peter Anvin# define XLF23 XLF_EFI_HANDOVER_64 /* 64-bit EFI handover ok */ 34909c205afSH. Peter Anvin# else 35009c205afSH. Peter Anvin# define XLF23 XLF_EFI_HANDOVER_32 /* 32-bit EFI handover ok */ 35109c205afSH. Peter Anvin# endif 3527d453eeeSMatt Fleming# endif 35309c205afSH. Peter Anvin#else 35409c205afSH. Peter Anvin# define XLF23 0 35509c205afSH. Peter Anvin#endif 356456a29ddSDave Young 3572965faa5SDave Young#if defined(CONFIG_X86_64) && defined(CONFIG_EFI) && defined(CONFIG_KEXEC_CORE) 358456a29ddSDave Young# define XLF4 XLF_EFI_KEXEC 359456a29ddSDave Young#else 360456a29ddSDave Young# define XLF4 0 361456a29ddSDave Young#endif 362456a29ddSDave Young 363f2d08c5dSBaoquan He#ifdef CONFIG_X86_64 364f2d08c5dSBaoquan He#ifdef CONFIG_X86_5LEVEL 365f2d08c5dSBaoquan He#define XLF56 (XLF_5LEVEL|XLF_5LEVEL_ENABLED) 366f2d08c5dSBaoquan He#else 367f2d08c5dSBaoquan He#define XLF56 XLF_5LEVEL 368f2d08c5dSBaoquan He#endif 369f2d08c5dSBaoquan He#else 370f2d08c5dSBaoquan He#define XLF56 0 371f2d08c5dSBaoquan He#endif 372f2d08c5dSBaoquan He 373f2d08c5dSBaoquan He .word XLF0 | XLF1 | XLF23 | XLF4 | XLF56 37496ae6ea0SThomas Gleixner 37596ae6ea0SThomas Gleixnercmdline_size: .long COMMAND_LINE_SIZE-1 #length of the command line, 37696ae6ea0SThomas Gleixner #added with boot protocol 37796ae6ea0SThomas Gleixner #version 2.06 37896ae6ea0SThomas Gleixner 379a24e7851SRusty Russellhardware_subarch: .long 0 # subarchitecture, added with 2.07 380a24e7851SRusty Russell # default to 0 for normal x86 PC 381a24e7851SRusty Russell 382a24e7851SRusty Russellhardware_subarch_data: .quad 0 383a24e7851SRusty Russell 38477d1a499SH. Peter Anvinpayload_offset: .long ZO_input_data 38577d1a499SH. Peter Anvinpayload_length: .long ZO_z_input_len 386099e1377SIan Campbell 3878b664aa6SHuang, Yingsetup_data: .quad 0 # 64-bit physical pointer to 3888b664aa6SHuang, Ying # single linked list of 3898b664aa6SHuang, Ying # struct setup_data 3908b664aa6SHuang, Ying 39137ba7ab5SH. Peter Anvinpref_address: .quad LOAD_PHYSICAL_ADDR # preferred load addr 39237ba7ab5SH. Peter Anvin 3934252db10SBaoquan He# 3944252db10SBaoquan He# Getting to provably safe in-place decompression is hard. Worst case 3954252db10SBaoquan He# behaviours need to be analyzed. Here let's take the decompression of 3964252db10SBaoquan He# a gzip-compressed kernel as example, to illustrate it: 3974252db10SBaoquan He# 3984252db10SBaoquan He# The file layout of gzip compressed kernel is: 3994252db10SBaoquan He# 4004252db10SBaoquan He# magic[2] 4014252db10SBaoquan He# method[1] 4024252db10SBaoquan He# flags[1] 4034252db10SBaoquan He# timestamp[4] 4044252db10SBaoquan He# extraflags[1] 4054252db10SBaoquan He# os[1] 4064252db10SBaoquan He# compressed data blocks[N] 4074252db10SBaoquan He# crc[4] orig_len[4] 4084252db10SBaoquan He# 4094252db10SBaoquan He# ... resulting in +18 bytes overhead of uncompressed data. 4104252db10SBaoquan He# 4114252db10SBaoquan He# (For more information, please refer to RFC 1951 and RFC 1952.) 4124252db10SBaoquan He# 4134252db10SBaoquan He# Files divided into blocks 4144252db10SBaoquan He# 1 bit (last block flag) 4154252db10SBaoquan He# 2 bits (block type) 4164252db10SBaoquan He# 4174252db10SBaoquan He# 1 block occurs every 32K -1 bytes or when there 50% compression 4184252db10SBaoquan He# has been achieved. The smallest block type encoding is always used. 4194252db10SBaoquan He# 4204252db10SBaoquan He# stored: 4214252db10SBaoquan He# 32 bits length in bytes. 4224252db10SBaoquan He# 4234252db10SBaoquan He# fixed: 4244252db10SBaoquan He# magic fixed tree. 4254252db10SBaoquan He# symbols. 4264252db10SBaoquan He# 4274252db10SBaoquan He# dynamic: 4284252db10SBaoquan He# dynamic tree encoding. 4294252db10SBaoquan He# symbols. 4304252db10SBaoquan He# 4314252db10SBaoquan He# 4324252db10SBaoquan He# The buffer for decompression in place is the length of the uncompressed 4334252db10SBaoquan He# data, plus a small amount extra to keep the algorithm safe. The 4344252db10SBaoquan He# compressed data is placed at the end of the buffer. The output pointer 4354252db10SBaoquan He# is placed at the start of the buffer and the input pointer is placed 4364252db10SBaoquan He# where the compressed data starts. Problems will occur when the output 4374252db10SBaoquan He# pointer overruns the input pointer. 4384252db10SBaoquan He# 4394252db10SBaoquan He# The output pointer can only overrun the input pointer if the input 4404252db10SBaoquan He# pointer is moving faster than the output pointer. A condition only 4414252db10SBaoquan He# triggered by data whose compressed form is larger than the uncompressed 4424252db10SBaoquan He# form. 4434252db10SBaoquan He# 4444252db10SBaoquan He# The worst case at the block level is a growth of the compressed data 4454252db10SBaoquan He# of 5 bytes per 32767 bytes. 4464252db10SBaoquan He# 4474252db10SBaoquan He# The worst case internal to a compressed block is very hard to figure. 4484252db10SBaoquan He# The worst case can at least be bounded by having one bit that represents 4494252db10SBaoquan He# 32764 bytes and then all of the rest of the bytes representing the very 4504252db10SBaoquan He# very last byte. 4514252db10SBaoquan He# 4524252db10SBaoquan He# All of which is enough to compute an amount of extra data that is required 4534252db10SBaoquan He# to be safe. To avoid problems at the block level allocating 5 extra bytes 4544252db10SBaoquan He# per 32767 bytes of data is sufficient. To avoid problems internal to a 4554252db10SBaoquan He# block adding an extra 32767 bytes (the worst case uncompressed block size) 4564252db10SBaoquan He# is sufficient, to ensure that in the worst case the decompressed data for 4574252db10SBaoquan He# block will stop the byte before the compressed data for a block begins. 4584252db10SBaoquan He# To avoid problems with the compressed data's meta information an extra 18 4594252db10SBaoquan He# bytes are needed. Leading to the formula: 4604252db10SBaoquan He# 461d607251bSYinghai Lu# extra_bytes = (uncompressed_size >> 12) + 32768 + 18 4624252db10SBaoquan He# 4634252db10SBaoquan He# Adding 8 bytes per 32K is a bit excessive but much easier to calculate. 4644252db10SBaoquan He# Adding 32768 instead of 32767 just makes for round numbers. 4654252db10SBaoquan He# 4664252db10SBaoquan He# Above analysis is for decompressing gzip compressed kernel only. Up to 4674252db10SBaoquan He# now 6 different decompressor are supported all together. And among them 4684252db10SBaoquan He# xz stores data in chunks and has maximum chunk of 64K. Hence safety 4694252db10SBaoquan He# margin should be updated to cover all decompressors so that we don't 4704252db10SBaoquan He# need to deal with each of them separately. Please check 4714252db10SBaoquan He# the description in lib/decompressor_xxx.c for specific information. 4724252db10SBaoquan He# 4734252db10SBaoquan He# extra_bytes = (uncompressed_size >> 12) + 65536 + 128 4745746f055SJan H. Schönherr# 4755746f055SJan H. Schönherr# LZ4 is even worse: data that cannot be further compressed grows by 0.4%, 4765746f055SJan H. Schönherr# or one byte per 256 bytes. OTOH, we can safely get rid of the +128 as 4775746f055SJan H. Schönherr# the size-dependent part now grows so fast. 4785746f055SJan H. Schönherr# 4795746f055SJan H. Schönherr# extra_bytes = (uncompressed_size >> 8) + 65536 4800fe4f4efSNick Terrell# 4810fe4f4efSNick Terrell# ZSTD compressed data grows by at most 3 bytes per 128K, and only has a 22 4820fe4f4efSNick Terrell# byte fixed overhead but has a maximum block size of 128K, so it needs a 4830fe4f4efSNick Terrell# larger margin. 4840fe4f4efSNick Terrell# 4850fe4f4efSNick Terrell# extra_bytes = (uncompressed_size >> 8) + 131072 4864252db10SBaoquan He 4870fe4f4efSNick Terrell#define ZO_z_extra_bytes ((ZO_z_output_len >> 8) + 131072) 488d607251bSYinghai Lu#if ZO_z_output_len > ZO_z_input_len 489d607251bSYinghai Lu# define ZO_z_extract_offset (ZO_z_output_len + ZO_z_extra_bytes - \ 490d607251bSYinghai Lu ZO_z_input_len) 491d607251bSYinghai Lu#else 492d607251bSYinghai Lu# define ZO_z_extract_offset ZO_z_extra_bytes 493d607251bSYinghai Lu#endif 494d607251bSYinghai Lu 495d607251bSYinghai Lu/* 496d607251bSYinghai Lu * The extract_offset has to be bigger than ZO head section. Otherwise when 497d607251bSYinghai Lu * the head code is running to move ZO to the end of the buffer, it will 498d607251bSYinghai Lu * overwrite the head code itself. 499d607251bSYinghai Lu */ 500d607251bSYinghai Lu#if (ZO__ehead - ZO_startup_32) > ZO_z_extract_offset 501d607251bSYinghai Lu# define ZO_z_min_extract_offset ((ZO__ehead - ZO_startup_32 + 4095) & ~4095) 502d607251bSYinghai Lu#else 503d607251bSYinghai Lu# define ZO_z_min_extract_offset ((ZO_z_extract_offset + 4095) & ~4095) 504d607251bSYinghai Lu#endif 505d607251bSYinghai Lu 506d607251bSYinghai Lu#define ZO_INIT_SIZE (ZO__end - ZO_startup_32 + ZO_z_min_extract_offset) 507d607251bSYinghai Lu 50837ba7ab5SH. Peter Anvin#define VO_INIT_SIZE (VO__end - VO__text) 50937ba7ab5SH. Peter Anvin#if ZO_INIT_SIZE > VO_INIT_SIZE 51037ba7ab5SH. Peter Anvin# define INIT_SIZE ZO_INIT_SIZE 51137ba7ab5SH. Peter Anvin#else 51237ba7ab5SH. Peter Anvin# define INIT_SIZE VO_INIT_SIZE 51337ba7ab5SH. Peter Anvin#endif 514d607251bSYinghai Lu 515eac95634SArd Biesheuvel .macro __handover_offset 516eac95634SArd Biesheuvel#ifndef CONFIG_EFI_HANDOVER_PROTOCOL 517eac95634SArd Biesheuvel .long 0 518eac95634SArd Biesheuvel#elif !defined(CONFIG_X86_64) 519eac95634SArd Biesheuvel .long ZO_efi32_stub_entry 520eac95634SArd Biesheuvel#else 521eac95634SArd Biesheuvel /* Yes, this is really how we defined it :( */ 522eac95634SArd Biesheuvel .long ZO_efi64_stub_entry - 0x200 523eac95634SArd Biesheuvel#ifdef CONFIG_EFI_MIXED 524eac95634SArd Biesheuvel .if ZO_efi32_stub_entry != ZO_efi64_stub_entry - 0x200 525eac95634SArd Biesheuvel .error "32-bit and 64-bit EFI entry points do not match" 526eac95634SArd Biesheuvel .endif 527eac95634SArd Biesheuvel#endif 528eac95634SArd Biesheuvel#endif 529eac95634SArd Biesheuvel .endm 530eac95634SArd Biesheuvel 53137ba7ab5SH. Peter Anvininit_size: .long INIT_SIZE # kernel initialization size 532eac95634SArd Biesheuvelhandover_offset: __handover_offset 5332e765c02SArd Biesheuvelkernel_info_offset: .long ZO_kernel_info 53437ba7ab5SH. Peter Anvin 53596ae6ea0SThomas Gleixner# End of setup header ##################################################### 53696ae6ea0SThomas Gleixner 5377a734e7dSH. Peter Anvin .section ".entrytext", "ax" 53896ae6ea0SThomas Gleixnerstart_of_setup: 53996ae6ea0SThomas Gleixner# Force %es = %ds 54096ae6ea0SThomas Gleixner movw %ds, %ax 54196ae6ea0SThomas Gleixner movw %ax, %es 54296ae6ea0SThomas Gleixner cld 54396ae6ea0SThomas Gleixner 54416252da6SJens Rottmann# Apparently some ancient versions of LILO invoked the kernel with %ss != %ds, 54516252da6SJens Rottmann# which happened to work by accident for the old code. Recalculate the stack 54616252da6SJens Rottmann# pointer if %ss is invalid. Otherwise leave it alone, LOADLIN sets up the 54716252da6SJens Rottmann# stack behind its own code, so we can't blindly put it directly past the heap. 5486b6815c6SH. Peter Anvin 5496b6815c6SH. Peter Anvin movw %ss, %dx 5506b6815c6SH. Peter Anvin cmpw %ax, %dx # %ds == %ss? 5516b6815c6SH. Peter Anvin movw %sp, %dx 55216252da6SJens Rottmann je 2f # -> assume %sp is reasonably set 5536b6815c6SH. Peter Anvin 55416252da6SJens Rottmann # Invalid %ss, make up a new stack 55516252da6SJens Rottmann movw $_end, %dx 55616252da6SJens Rottmann testb $CAN_USE_HEAP, loadflags 55716252da6SJens Rottmann jz 1f 55816252da6SJens Rottmann movw heap_end_ptr, %dx 55916252da6SJens Rottmann1: addw $STACK_SIZE, %dx 56016252da6SJens Rottmann jnc 2f 56116252da6SJens Rottmann xorw %dx, %dx # Prevent wraparound 56216252da6SJens Rottmann 56316252da6SJens Rottmann2: # Now %dx should point to the end of our stack space 5646b6815c6SH. Peter Anvin andw $~3, %dx # dword align (might as well...) 5656b6815c6SH. Peter Anvin jnz 3f 5666b6815c6SH. Peter Anvin movw $0xfffc, %dx # Make sure we're not zero 56716252da6SJens Rottmann3: movw %ax, %ss 5686b6815c6SH. Peter Anvin movzwl %dx, %esp # Clear upper half of %esp 5696b6815c6SH. Peter Anvin sti # Now we should have a working stack 5706b6815c6SH. Peter Anvin 5716b6815c6SH. Peter Anvin# We will have entered with %cs = %ds+0x20, normalize %cs so 5726b6815c6SH. Peter Anvin# it is on par with the other segments. 5736b6815c6SH. Peter Anvin pushw %ds 5746b6815c6SH. Peter Anvin pushw $6f 5756b6815c6SH. Peter Anvin lretw 5766b6815c6SH. Peter Anvin6: 57796ae6ea0SThomas Gleixner 57896ae6ea0SThomas Gleixner# Check signature at end of setup 57996ae6ea0SThomas Gleixner cmpl $0x5a5aaa55, setup_sig 58096ae6ea0SThomas Gleixner jne setup_bad 58196ae6ea0SThomas Gleixner 58296ae6ea0SThomas Gleixner# Zero the bss 58396ae6ea0SThomas Gleixner movw $__bss_start, %di 58496ae6ea0SThomas Gleixner movw $_end+3, %cx 58596ae6ea0SThomas Gleixner xorl %eax, %eax 58696ae6ea0SThomas Gleixner subw %di, %cx 58796ae6ea0SThomas Gleixner shrw $2, %cx 58896ae6ea0SThomas Gleixner rep; stosl 58996ae6ea0SThomas Gleixner 59096ae6ea0SThomas Gleixner# Jump to C code (should not return) 59196ae6ea0SThomas Gleixner calll main 59296ae6ea0SThomas Gleixner 59396ae6ea0SThomas Gleixner# Setup corrupt somehow... 59496ae6ea0SThomas Gleixnersetup_bad: 59596ae6ea0SThomas Gleixner movl $setup_corrupt, %eax 59696ae6ea0SThomas Gleixner calll puts 59796ae6ea0SThomas Gleixner # Fall through... 59896ae6ea0SThomas Gleixner 59996ae6ea0SThomas Gleixner .globl die 60096ae6ea0SThomas Gleixner .type die, @function 60196ae6ea0SThomas Gleixnerdie: 60296ae6ea0SThomas Gleixner hlt 60396ae6ea0SThomas Gleixner jmp die 60496ae6ea0SThomas Gleixner 60596ae6ea0SThomas Gleixner .size die, .-die 60696ae6ea0SThomas Gleixner 60796ae6ea0SThomas Gleixner .section ".initdata", "a" 60896ae6ea0SThomas Gleixnersetup_corrupt: 60996ae6ea0SThomas Gleixner .byte 7 61096ae6ea0SThomas Gleixner .string "No setup signature found...\n" 611