196ae6ea0SThomas Gleixner/* 296ae6ea0SThomas Gleixner * header.S 396ae6ea0SThomas Gleixner * 496ae6ea0SThomas Gleixner * Copyright (C) 1991, 1992 Linus Torvalds 596ae6ea0SThomas Gleixner * 696ae6ea0SThomas Gleixner * Based on bootsect.S and setup.S 796ae6ea0SThomas Gleixner * modified by more people than can be counted 896ae6ea0SThomas Gleixner * 996ae6ea0SThomas Gleixner * Rewritten as a common file by H. Peter Anvin (Apr 2007) 1096ae6ea0SThomas Gleixner * 1196ae6ea0SThomas Gleixner * BIG FAT NOTE: We're in real mode using 64k segments. Therefore segment 1296ae6ea0SThomas Gleixner * addresses must be multiplied by 16 to obtain their respective linear 1396ae6ea0SThomas Gleixner * addresses. To avoid confusion, linear addresses are written using leading 1496ae6ea0SThomas Gleixner * hex while segment addresses are written as segment:offset. 1596ae6ea0SThomas Gleixner * 1696ae6ea0SThomas Gleixner */ 1796ae6ea0SThomas Gleixner 1896ae6ea0SThomas Gleixner#include <asm/segment.h> 19273b281fSSam Ravnborg#include <generated/utsrelease.h> 2096ae6ea0SThomas Gleixner#include <asm/boot.h> 2196ae6ea0SThomas Gleixner#include <asm/e820.h> 220341c14dSJeremy Fitzhardinge#include <asm/page_types.h> 2396ae6ea0SThomas Gleixner#include <asm/setup.h> 2409c205afSH. Peter Anvin#include <asm/bootparam.h> 2596ae6ea0SThomas Gleixner#include "boot.h" 2677d1a499SH. Peter Anvin#include "voffset.h" 2777d1a499SH. Peter Anvin#include "zoffset.h" 2896ae6ea0SThomas Gleixner 2996ae6ea0SThomas GleixnerBOOTSEG = 0x07C0 /* original address of boot-sector */ 305e47c478SH. Peter AnvinSYSSEG = 0x1000 /* historical load address >> 4 */ 3196ae6ea0SThomas Gleixner 3296ae6ea0SThomas Gleixner#ifndef SVGA_MODE 3396ae6ea0SThomas Gleixner#define SVGA_MODE ASK_VGA 3496ae6ea0SThomas Gleixner#endif 3596ae6ea0SThomas Gleixner 3696ae6ea0SThomas Gleixner#ifndef ROOT_RDONLY 3796ae6ea0SThomas Gleixner#define ROOT_RDONLY 1 3896ae6ea0SThomas Gleixner#endif 3996ae6ea0SThomas Gleixner 4096ae6ea0SThomas Gleixner .code16 4196ae6ea0SThomas Gleixner .section ".bstext", "ax" 4296ae6ea0SThomas Gleixner 4396ae6ea0SThomas Gleixner .global bootsect_start 4496ae6ea0SThomas Gleixnerbootsect_start: 45291f3632SMatt Fleming#ifdef CONFIG_EFI_STUB 46291f3632SMatt Fleming # "MZ", MS-DOS header 47291f3632SMatt Fleming .byte 0x4d 48291f3632SMatt Fleming .byte 0x5a 49291f3632SMatt Fleming#endif 5096ae6ea0SThomas Gleixner 5196ae6ea0SThomas Gleixner # Normalize the start address 5296ae6ea0SThomas Gleixner ljmp $BOOTSEG, $start2 5396ae6ea0SThomas Gleixner 5496ae6ea0SThomas Gleixnerstart2: 5596ae6ea0SThomas Gleixner movw %cs, %ax 5696ae6ea0SThomas Gleixner movw %ax, %ds 5796ae6ea0SThomas Gleixner movw %ax, %es 5896ae6ea0SThomas Gleixner movw %ax, %ss 5996ae6ea0SThomas Gleixner xorw %sp, %sp 6096ae6ea0SThomas Gleixner sti 6196ae6ea0SThomas Gleixner cld 6296ae6ea0SThomas Gleixner 6396ae6ea0SThomas Gleixner movw $bugger_off_msg, %si 6496ae6ea0SThomas Gleixner 6596ae6ea0SThomas Gleixnermsg_loop: 6696ae6ea0SThomas Gleixner lodsb 6796ae6ea0SThomas Gleixner andb %al, %al 6896ae6ea0SThomas Gleixner jz bs_die 6996ae6ea0SThomas Gleixner movb $0xe, %ah 7096ae6ea0SThomas Gleixner movw $7, %bx 7196ae6ea0SThomas Gleixner int $0x10 7296ae6ea0SThomas Gleixner jmp msg_loop 7396ae6ea0SThomas Gleixner 7496ae6ea0SThomas Gleixnerbs_die: 7596ae6ea0SThomas Gleixner # Allow the user to press a key, then reboot 7696ae6ea0SThomas Gleixner xorw %ax, %ax 7796ae6ea0SThomas Gleixner int $0x16 7896ae6ea0SThomas Gleixner int $0x19 7996ae6ea0SThomas Gleixner 8096ae6ea0SThomas Gleixner # int 0x19 should never return. In case it does anyway, 8196ae6ea0SThomas Gleixner # invoke the BIOS reset code... 8296ae6ea0SThomas Gleixner ljmp $0xf000,$0xfff0 8396ae6ea0SThomas Gleixner 84291f3632SMatt Fleming#ifdef CONFIG_EFI_STUB 85291f3632SMatt Fleming .org 0x3c 86291f3632SMatt Fleming # 87291f3632SMatt Fleming # Offset to the PE header. 88291f3632SMatt Fleming # 89291f3632SMatt Fleming .long pe_header 90291f3632SMatt Fleming#endif /* CONFIG_EFI_STUB */ 91291f3632SMatt Fleming 9296ae6ea0SThomas Gleixner .section ".bsdata", "a" 9396ae6ea0SThomas Gleixnerbugger_off_msg: 94c7fb93ecSMichael Brown .ascii "Use a boot loader.\r\n" 9596ae6ea0SThomas Gleixner .ascii "\n" 9696ae6ea0SThomas Gleixner .ascii "Remove disk and press any key to reboot...\r\n" 9796ae6ea0SThomas Gleixner .byte 0 9896ae6ea0SThomas Gleixner 99291f3632SMatt Fleming#ifdef CONFIG_EFI_STUB 100291f3632SMatt Flemingpe_header: 101291f3632SMatt Fleming .ascii "PE" 102291f3632SMatt Fleming .word 0 103291f3632SMatt Fleming 104291f3632SMatt Flemingcoff_header: 105291f3632SMatt Fleming#ifdef CONFIG_X86_32 106291f3632SMatt Fleming .word 0x14c # i386 107291f3632SMatt Fleming#else 108291f3632SMatt Fleming .word 0x8664 # x86-64 109291f3632SMatt Fleming#endif 110c7fb93ecSMichael Brown .word 4 # nr_sections 111291f3632SMatt Fleming .long 0 # TimeDateStamp 112291f3632SMatt Fleming .long 0 # PointerToSymbolTable 113291f3632SMatt Fleming .long 1 # NumberOfSymbols 114291f3632SMatt Fleming .word section_table - optional_header # SizeOfOptionalHeader 115291f3632SMatt Fleming#ifdef CONFIG_X86_32 116291f3632SMatt Fleming .word 0x306 # Characteristics. 117291f3632SMatt Fleming # IMAGE_FILE_32BIT_MACHINE | 118291f3632SMatt Fleming # IMAGE_FILE_DEBUG_STRIPPED | 119291f3632SMatt Fleming # IMAGE_FILE_EXECUTABLE_IMAGE | 120291f3632SMatt Fleming # IMAGE_FILE_LINE_NUMS_STRIPPED 121291f3632SMatt Fleming#else 122291f3632SMatt Fleming .word 0x206 # Characteristics 123291f3632SMatt Fleming # IMAGE_FILE_DEBUG_STRIPPED | 124291f3632SMatt Fleming # IMAGE_FILE_EXECUTABLE_IMAGE | 125291f3632SMatt Fleming # IMAGE_FILE_LINE_NUMS_STRIPPED 126291f3632SMatt Fleming#endif 127291f3632SMatt Fleming 128291f3632SMatt Flemingoptional_header: 129291f3632SMatt Fleming#ifdef CONFIG_X86_32 130291f3632SMatt Fleming .word 0x10b # PE32 format 131291f3632SMatt Fleming#else 132291f3632SMatt Fleming .word 0x20b # PE32+ format 133291f3632SMatt Fleming#endif 134291f3632SMatt Fleming .byte 0x02 # MajorLinkerVersion 135291f3632SMatt Fleming .byte 0x14 # MinorLinkerVersion 136291f3632SMatt Fleming 137291f3632SMatt Fleming # Filled in by build.c 138291f3632SMatt Fleming .long 0 # SizeOfCode 139291f3632SMatt Fleming 140291f3632SMatt Fleming .long 0 # SizeOfInitializedData 141291f3632SMatt Fleming .long 0 # SizeOfUninitializedData 142291f3632SMatt Fleming 143291f3632SMatt Fleming # Filled in by build.c 144291f3632SMatt Fleming .long 0x0000 # AddressOfEntryPoint 145291f3632SMatt Fleming 146e31be363SMatt Fleming .long 0x0200 # BaseOfCode 147291f3632SMatt Fleming#ifdef CONFIG_X86_32 148291f3632SMatt Fleming .long 0 # data 149291f3632SMatt Fleming#endif 150291f3632SMatt Fleming 151291f3632SMatt Flemingextra_header_fields: 152291f3632SMatt Fleming#ifdef CONFIG_X86_32 153291f3632SMatt Fleming .long 0 # ImageBase 154291f3632SMatt Fleming#else 155291f3632SMatt Fleming .quad 0 # ImageBase 156291f3632SMatt Fleming#endif 157fa5c3501SMatt Fleming .long 0x20 # SectionAlignment 158743628e8SJordan Justen .long 0x20 # FileAlignment 159291f3632SMatt Fleming .word 0 # MajorOperatingSystemVersion 160291f3632SMatt Fleming .word 0 # MinorOperatingSystemVersion 161291f3632SMatt Fleming .word 0 # MajorImageVersion 162291f3632SMatt Fleming .word 0 # MinorImageVersion 163291f3632SMatt Fleming .word 0 # MajorSubsystemVersion 164291f3632SMatt Fleming .word 0 # MinorSubsystemVersion 165291f3632SMatt Fleming .long 0 # Win32VersionValue 166291f3632SMatt Fleming 167291f3632SMatt Fleming # 168291f3632SMatt Fleming # The size of the bzImage is written in tools/build.c 169291f3632SMatt Fleming # 170291f3632SMatt Fleming .long 0 # SizeOfImage 171291f3632SMatt Fleming 172291f3632SMatt Fleming .long 0x200 # SizeOfHeaders 173291f3632SMatt Fleming .long 0 # CheckSum 174291f3632SMatt Fleming .word 0xa # Subsystem (EFI application) 175291f3632SMatt Fleming .word 0 # DllCharacteristics 176291f3632SMatt Fleming#ifdef CONFIG_X86_32 177291f3632SMatt Fleming .long 0 # SizeOfStackReserve 178291f3632SMatt Fleming .long 0 # SizeOfStackCommit 179291f3632SMatt Fleming .long 0 # SizeOfHeapReserve 180291f3632SMatt Fleming .long 0 # SizeOfHeapCommit 181291f3632SMatt Fleming#else 182291f3632SMatt Fleming .quad 0 # SizeOfStackReserve 183291f3632SMatt Fleming .quad 0 # SizeOfStackCommit 184291f3632SMatt Fleming .quad 0 # SizeOfHeapReserve 185291f3632SMatt Fleming .quad 0 # SizeOfHeapCommit 186291f3632SMatt Fleming#endif 187291f3632SMatt Fleming .long 0 # LoaderFlags 188e47bb0bdSMatt Fleming .long 0x6 # NumberOfRvaAndSizes 189291f3632SMatt Fleming 190291f3632SMatt Fleming .quad 0 # ExportTable 191291f3632SMatt Fleming .quad 0 # ImportTable 192291f3632SMatt Fleming .quad 0 # ResourceTable 193291f3632SMatt Fleming .quad 0 # ExceptionTable 194291f3632SMatt Fleming .quad 0 # CertificationTable 195291f3632SMatt Fleming .quad 0 # BaseRelocationTable 196291f3632SMatt Fleming 197291f3632SMatt Fleming # Section table 198291f3632SMatt Flemingsection_table: 199743628e8SJordan Justen # 200743628e8SJordan Justen # The offset & size fields are filled in by build.c. 201743628e8SJordan Justen # 202743628e8SJordan Justen .ascii ".setup" 203291f3632SMatt Fleming .byte 0 204291f3632SMatt Fleming .byte 0 205291f3632SMatt Fleming .long 0 206291f3632SMatt Fleming .long 0x0 # startup_{32,64} 207291f3632SMatt Fleming .long 0 # Size of initialized data 208291f3632SMatt Fleming # on disk 209291f3632SMatt Fleming .long 0x0 # startup_{32,64} 210291f3632SMatt Fleming .long 0 # PointerToRelocations 211291f3632SMatt Fleming .long 0 # PointerToLineNumbers 212291f3632SMatt Fleming .word 0 # NumberOfRelocations 213291f3632SMatt Fleming .word 0 # NumberOfLineNumbers 214291f3632SMatt Fleming .long 0x60500020 # Characteristics (section flags) 215291f3632SMatt Fleming 216291f3632SMatt Fleming # 217291f3632SMatt Fleming # The EFI application loader requires a relocation section 218743628e8SJordan Justen # because EFI applications must be relocatable. The .reloc 219743628e8SJordan Justen # offset & size fields are filled in by build.c. 220291f3632SMatt Fleming # 221291f3632SMatt Fleming .ascii ".reloc" 222291f3632SMatt Fleming .byte 0 223291f3632SMatt Fleming .byte 0 2242e064b1eSJordan Justen .long 0 2252e064b1eSJordan Justen .long 0 2262e064b1eSJordan Justen .long 0 # SizeOfRawData 2272e064b1eSJordan Justen .long 0 # PointerToRawData 228291f3632SMatt Fleming .long 0 # PointerToRelocations 229291f3632SMatt Fleming .long 0 # PointerToLineNumbers 230291f3632SMatt Fleming .word 0 # NumberOfRelocations 231291f3632SMatt Fleming .word 0 # NumberOfLineNumbers 232291f3632SMatt Fleming .long 0x42100040 # Characteristics (section flags) 233743628e8SJordan Justen 234743628e8SJordan Justen # 235743628e8SJordan Justen # The offset & size fields are filled in by build.c. 236743628e8SJordan Justen # 237743628e8SJordan Justen .ascii ".text" 238743628e8SJordan Justen .byte 0 239743628e8SJordan Justen .byte 0 240743628e8SJordan Justen .byte 0 241743628e8SJordan Justen .long 0 242743628e8SJordan Justen .long 0x0 # startup_{32,64} 243743628e8SJordan Justen .long 0 # Size of initialized data 244743628e8SJordan Justen # on disk 245743628e8SJordan Justen .long 0x0 # startup_{32,64} 246743628e8SJordan Justen .long 0 # PointerToRelocations 247743628e8SJordan Justen .long 0 # PointerToLineNumbers 248743628e8SJordan Justen .word 0 # NumberOfRelocations 249743628e8SJordan Justen .word 0 # NumberOfLineNumbers 250743628e8SJordan Justen .long 0x60500020 # Characteristics (section flags) 251743628e8SJordan Justen 252c7fb93ecSMichael Brown # 253c7fb93ecSMichael Brown # The offset & size fields are filled in by build.c. 254c7fb93ecSMichael Brown # 255c7fb93ecSMichael Brown .ascii ".bss" 256c7fb93ecSMichael Brown .byte 0 257c7fb93ecSMichael Brown .byte 0 258c7fb93ecSMichael Brown .byte 0 259c7fb93ecSMichael Brown .byte 0 260c7fb93ecSMichael Brown .long 0 261c7fb93ecSMichael Brown .long 0x0 262c7fb93ecSMichael Brown .long 0 # Size of initialized data 263c7fb93ecSMichael Brown # on disk 264c7fb93ecSMichael Brown .long 0x0 265c7fb93ecSMichael Brown .long 0 # PointerToRelocations 266c7fb93ecSMichael Brown .long 0 # PointerToLineNumbers 267c7fb93ecSMichael Brown .word 0 # NumberOfRelocations 268c7fb93ecSMichael Brown .word 0 # NumberOfLineNumbers 269c7fb93ecSMichael Brown .long 0xc8000080 # Characteristics (section flags) 270c7fb93ecSMichael Brown 271291f3632SMatt Fleming#endif /* CONFIG_EFI_STUB */ 27296ae6ea0SThomas Gleixner 27396ae6ea0SThomas Gleixner # Kernel attributes; used by setup. This is part 1 of the 27496ae6ea0SThomas Gleixner # header, from the old boot sector. 27596ae6ea0SThomas Gleixner 27696ae6ea0SThomas Gleixner .section ".header", "a" 27709c205afSH. Peter Anvin .globl sentinel 27809c205afSH. Peter Anvinsentinel: .byte 0xff, 0xff /* Used to detect broken loaders */ 27909c205afSH. Peter Anvin 28096ae6ea0SThomas Gleixner .globl hdr 28196ae6ea0SThomas Gleixnerhdr: 2825e47c478SH. Peter Anvinsetup_sects: .byte 0 /* Filled in by build.c */ 28396ae6ea0SThomas Gleixnerroot_flags: .word ROOT_RDONLY 2845e47c478SH. Peter Anvinsyssize: .long 0 /* Filled in by build.c */ 2855e47c478SH. Peter Anvinram_size: .word 0 /* Obsolete */ 28696ae6ea0SThomas Gleixnervid_mode: .word SVGA_MODE 2875e47c478SH. Peter Anvinroot_dev: .word 0 /* Filled in by build.c */ 28896ae6ea0SThomas Gleixnerboot_flag: .word 0xAA55 28996ae6ea0SThomas Gleixner 29096ae6ea0SThomas Gleixner # offset 512, entry point 29196ae6ea0SThomas Gleixner 29296ae6ea0SThomas Gleixner .globl _start 29396ae6ea0SThomas Gleixner_start: 29496ae6ea0SThomas Gleixner # Explicitly enter this as bytes, or the assembler 29596ae6ea0SThomas Gleixner # tries to generate a 3-byte jump here, which causes 29696ae6ea0SThomas Gleixner # everything else to push off to the wrong offset. 29796ae6ea0SThomas Gleixner .byte 0xeb # short (2-byte) jump 29896ae6ea0SThomas Gleixner .byte start_of_setup-1f 29996ae6ea0SThomas Gleixner1: 30096ae6ea0SThomas Gleixner 30196ae6ea0SThomas Gleixner # Part 2 of the header, from the old setup.S 30296ae6ea0SThomas Gleixner 30396ae6ea0SThomas Gleixner .ascii "HdrS" # header signature 3047d453eeeSMatt Fleming .word 0x020d # header version number (>= 0x0105) 30596ae6ea0SThomas Gleixner # or else old loadlin-1.5 will fail) 30696ae6ea0SThomas Gleixner .globl realmode_swtch 30796ae6ea0SThomas Gleixnerrealmode_swtch: .word 0, 0 # default_switch, SETUPSEG 3085e47c478SH. Peter Anvinstart_sys_seg: .word SYSSEG # obsolete and meaningless, but just 3095e47c478SH. Peter Anvin # in case something decided to "use" it 31096ae6ea0SThomas Gleixner .word kernel_version-512 # pointing to kernel version string 31196ae6ea0SThomas Gleixner # above section of header is compatible 31296ae6ea0SThomas Gleixner # with loadlin-1.5 (header v1.5). Don't 31396ae6ea0SThomas Gleixner # change it. 31496ae6ea0SThomas Gleixner 3155e47c478SH. Peter Anvintype_of_loader: .byte 0 # 0 means ancient bootloader, newer 3165e47c478SH. Peter Anvin # bootloaders know to change this. 317395cf969SPaul Bolle # See Documentation/x86/boot.txt for 31896ae6ea0SThomas Gleixner # assigned ids 31996ae6ea0SThomas Gleixner 32096ae6ea0SThomas Gleixner# flags, unused bits must be zero (RFU) bit within loadflags 32196ae6ea0SThomas Gleixnerloadflags: 32209c205afSH. Peter Anvin .byte LOADED_HIGH # The kernel is to be loaded high 32396ae6ea0SThomas Gleixner 32496ae6ea0SThomas Gleixnersetup_move_size: .word 0x8000 # size to move, when setup is not 32596ae6ea0SThomas Gleixner # loaded at 0x90000. We will move setup 32696ae6ea0SThomas Gleixner # to 0x90000 then just before jumping 32796ae6ea0SThomas Gleixner # into the kernel. However, only the 32896ae6ea0SThomas Gleixner # loader knows how much data behind 32996ae6ea0SThomas Gleixner # us also needs to be loaded. 33096ae6ea0SThomas Gleixner 33196ae6ea0SThomas Gleixnercode32_start: # here loaders can put a different 33296ae6ea0SThomas Gleixner # start address for 32-bit code. 33396ae6ea0SThomas Gleixner .long 0x100000 # 0x100000 = default for big kernel 33496ae6ea0SThomas Gleixner 33596ae6ea0SThomas Gleixnerramdisk_image: .long 0 # address of loaded ramdisk image 33696ae6ea0SThomas Gleixner # Here the loader puts the 32-bit 33796ae6ea0SThomas Gleixner # address where it loaded the image. 33896ae6ea0SThomas Gleixner # This only will be read by the kernel. 33996ae6ea0SThomas Gleixner 34096ae6ea0SThomas Gleixnerramdisk_size: .long 0 # its size in bytes 34196ae6ea0SThomas Gleixner 34296ae6ea0SThomas Gleixnerbootsect_kludge: 34396ae6ea0SThomas Gleixner .long 0 # obsolete 34496ae6ea0SThomas Gleixner 3456b6815c6SH. Peter Anvinheap_end_ptr: .word _end+STACK_SIZE-512 3466b6815c6SH. Peter Anvin # (Header version 0x0201 or later) 34796ae6ea0SThomas Gleixner # space from here (exclusive) down to 34896ae6ea0SThomas Gleixner # end of setup code can be used by setup 34996ae6ea0SThomas Gleixner # for local heap purposes. 35096ae6ea0SThomas Gleixner 3515031296cSH. Peter Anvinext_loader_ver: 3525031296cSH. Peter Anvin .byte 0 # Extended boot loader version 3535031296cSH. Peter Anvinext_loader_type: 3545031296cSH. Peter Anvin .byte 0 # Extended boot loader type 3555031296cSH. Peter Anvin 35696ae6ea0SThomas Gleixnercmd_line_ptr: .long 0 # (Header version 0x0202 or later) 35796ae6ea0SThomas Gleixner # If nonzero, a 32-bit pointer 35896ae6ea0SThomas Gleixner # to the kernel command line. 35996ae6ea0SThomas Gleixner # The command line should be 36096ae6ea0SThomas Gleixner # located between the start of 36196ae6ea0SThomas Gleixner # setup and the end of low 36296ae6ea0SThomas Gleixner # memory (0xa0000), or it may 36396ae6ea0SThomas Gleixner # get overwritten before it 36496ae6ea0SThomas Gleixner # gets read. If this field is 36596ae6ea0SThomas Gleixner # used, there is no longer 36696ae6ea0SThomas Gleixner # anything magical about the 36796ae6ea0SThomas Gleixner # 0x90000 segment; the setup 36896ae6ea0SThomas Gleixner # can be located anywhere in 36996ae6ea0SThomas Gleixner # low memory 0x10000 or higher. 37096ae6ea0SThomas Gleixner 3713e920b53SBorislav Petkovinitrd_addr_max: .long 0x7fffffff 37296ae6ea0SThomas Gleixner # (Header version 0x0203 or later) 37396ae6ea0SThomas Gleixner # The highest safe address for 37496ae6ea0SThomas Gleixner # the contents of an initrd 375cf8fa920SH. Peter Anvin # The current kernel allows up to 4 GB, 376cf8fa920SH. Peter Anvin # but leave it at 2 GB to avoid 377cf8fa920SH. Peter Anvin # possible bootloader bugs. 37896ae6ea0SThomas Gleixner 37996ae6ea0SThomas Gleixnerkernel_alignment: .long CONFIG_PHYSICAL_ALIGN #physical addr alignment 38096ae6ea0SThomas Gleixner #required for protected mode 38196ae6ea0SThomas Gleixner #kernel 38296ae6ea0SThomas Gleixner#ifdef CONFIG_RELOCATABLE 38396ae6ea0SThomas Gleixnerrelocatable_kernel: .byte 1 38496ae6ea0SThomas Gleixner#else 38596ae6ea0SThomas Gleixnerrelocatable_kernel: .byte 0 38696ae6ea0SThomas Gleixner#endif 38737ba7ab5SH. Peter Anvinmin_alignment: .byte MIN_KERNEL_ALIGN_LG2 # minimum alignment 38809c205afSH. Peter Anvin 38909c205afSH. Peter Anvinxloadflags: 39009c205afSH. Peter Anvin#ifdef CONFIG_X86_64 39109c205afSH. Peter Anvin# define XLF0 XLF_KERNEL_64 /* 64-bit kernel */ 39209c205afSH. Peter Anvin#else 39309c205afSH. Peter Anvin# define XLF0 0 39409c205afSH. Peter Anvin#endif 395ee92d815SYinghai Lu 396745c5167SMatt Fleming#if defined(CONFIG_RELOCATABLE) && defined(CONFIG_X86_64) 397ee92d815SYinghai Lu /* kernel/boot_param/ramdisk could be loaded above 4g */ 398ee92d815SYinghai Lu# define XLF1 XLF_CAN_BE_LOADED_ABOVE_4G 399ee92d815SYinghai Lu#else 400ee92d815SYinghai Lu# define XLF1 0 401ee92d815SYinghai Lu#endif 402ee92d815SYinghai Lu 40309c205afSH. Peter Anvin#ifdef CONFIG_EFI_STUB 4047d453eeeSMatt Fleming# ifdef CONFIG_EFI_MIXED 4057d453eeeSMatt Fleming# define XLF23 (XLF_EFI_HANDOVER_32|XLF_EFI_HANDOVER_64) 4067d453eeeSMatt Fleming# else 40709c205afSH. Peter Anvin# ifdef CONFIG_X86_64 40809c205afSH. Peter Anvin# define XLF23 XLF_EFI_HANDOVER_64 /* 64-bit EFI handover ok */ 40909c205afSH. Peter Anvin# else 41009c205afSH. Peter Anvin# define XLF23 XLF_EFI_HANDOVER_32 /* 32-bit EFI handover ok */ 41109c205afSH. Peter Anvin# endif 4127d453eeeSMatt Fleming# endif 41309c205afSH. Peter Anvin#else 41409c205afSH. Peter Anvin# define XLF23 0 41509c205afSH. Peter Anvin#endif 416456a29ddSDave Young 4172965faa5SDave Young#if defined(CONFIG_X86_64) && defined(CONFIG_EFI) && defined(CONFIG_KEXEC_CORE) 418456a29ddSDave Young# define XLF4 XLF_EFI_KEXEC 419456a29ddSDave Young#else 420456a29ddSDave Young# define XLF4 0 421456a29ddSDave Young#endif 422456a29ddSDave Young 423456a29ddSDave Young .word XLF0 | XLF1 | XLF23 | XLF4 42496ae6ea0SThomas Gleixner 42596ae6ea0SThomas Gleixnercmdline_size: .long COMMAND_LINE_SIZE-1 #length of the command line, 42696ae6ea0SThomas Gleixner #added with boot protocol 42796ae6ea0SThomas Gleixner #version 2.06 42896ae6ea0SThomas Gleixner 429a24e7851SRusty Russellhardware_subarch: .long 0 # subarchitecture, added with 2.07 430a24e7851SRusty Russell # default to 0 for normal x86 PC 431a24e7851SRusty Russell 432a24e7851SRusty Russellhardware_subarch_data: .quad 0 433a24e7851SRusty Russell 43477d1a499SH. Peter Anvinpayload_offset: .long ZO_input_data 43577d1a499SH. Peter Anvinpayload_length: .long ZO_z_input_len 436099e1377SIan Campbell 4378b664aa6SHuang, Yingsetup_data: .quad 0 # 64-bit physical pointer to 4388b664aa6SHuang, Ying # single linked list of 4398b664aa6SHuang, Ying # struct setup_data 4408b664aa6SHuang, Ying 44137ba7ab5SH. Peter Anvinpref_address: .quad LOAD_PHYSICAL_ADDR # preferred load addr 44237ba7ab5SH. Peter Anvin 4434252db10SBaoquan He# 4444252db10SBaoquan He# Getting to provably safe in-place decompression is hard. Worst case 4454252db10SBaoquan He# behaviours need to be analyzed. Here let's take the decompression of 4464252db10SBaoquan He# a gzip-compressed kernel as example, to illustrate it: 4474252db10SBaoquan He# 4484252db10SBaoquan He# The file layout of gzip compressed kernel is: 4494252db10SBaoquan He# 4504252db10SBaoquan He# magic[2] 4514252db10SBaoquan He# method[1] 4524252db10SBaoquan He# flags[1] 4534252db10SBaoquan He# timestamp[4] 4544252db10SBaoquan He# extraflags[1] 4554252db10SBaoquan He# os[1] 4564252db10SBaoquan He# compressed data blocks[N] 4574252db10SBaoquan He# crc[4] orig_len[4] 4584252db10SBaoquan He# 4594252db10SBaoquan He# ... resulting in +18 bytes overhead of uncompressed data. 4604252db10SBaoquan He# 4614252db10SBaoquan He# (For more information, please refer to RFC 1951 and RFC 1952.) 4624252db10SBaoquan He# 4634252db10SBaoquan He# Files divided into blocks 4644252db10SBaoquan He# 1 bit (last block flag) 4654252db10SBaoquan He# 2 bits (block type) 4664252db10SBaoquan He# 4674252db10SBaoquan He# 1 block occurs every 32K -1 bytes or when there 50% compression 4684252db10SBaoquan He# has been achieved. The smallest block type encoding is always used. 4694252db10SBaoquan He# 4704252db10SBaoquan He# stored: 4714252db10SBaoquan He# 32 bits length in bytes. 4724252db10SBaoquan He# 4734252db10SBaoquan He# fixed: 4744252db10SBaoquan He# magic fixed tree. 4754252db10SBaoquan He# symbols. 4764252db10SBaoquan He# 4774252db10SBaoquan He# dynamic: 4784252db10SBaoquan He# dynamic tree encoding. 4794252db10SBaoquan He# symbols. 4804252db10SBaoquan He# 4814252db10SBaoquan He# 4824252db10SBaoquan He# The buffer for decompression in place is the length of the uncompressed 4834252db10SBaoquan He# data, plus a small amount extra to keep the algorithm safe. The 4844252db10SBaoquan He# compressed data is placed at the end of the buffer. The output pointer 4854252db10SBaoquan He# is placed at the start of the buffer and the input pointer is placed 4864252db10SBaoquan He# where the compressed data starts. Problems will occur when the output 4874252db10SBaoquan He# pointer overruns the input pointer. 4884252db10SBaoquan He# 4894252db10SBaoquan He# The output pointer can only overrun the input pointer if the input 4904252db10SBaoquan He# pointer is moving faster than the output pointer. A condition only 4914252db10SBaoquan He# triggered by data whose compressed form is larger than the uncompressed 4924252db10SBaoquan He# form. 4934252db10SBaoquan He# 4944252db10SBaoquan He# The worst case at the block level is a growth of the compressed data 4954252db10SBaoquan He# of 5 bytes per 32767 bytes. 4964252db10SBaoquan He# 4974252db10SBaoquan He# The worst case internal to a compressed block is very hard to figure. 4984252db10SBaoquan He# The worst case can at least be bounded by having one bit that represents 4994252db10SBaoquan He# 32764 bytes and then all of the rest of the bytes representing the very 5004252db10SBaoquan He# very last byte. 5014252db10SBaoquan He# 5024252db10SBaoquan He# All of which is enough to compute an amount of extra data that is required 5034252db10SBaoquan He# to be safe. To avoid problems at the block level allocating 5 extra bytes 5044252db10SBaoquan He# per 32767 bytes of data is sufficient. To avoid problems internal to a 5054252db10SBaoquan He# block adding an extra 32767 bytes (the worst case uncompressed block size) 5064252db10SBaoquan He# is sufficient, to ensure that in the worst case the decompressed data for 5074252db10SBaoquan He# block will stop the byte before the compressed data for a block begins. 5084252db10SBaoquan He# To avoid problems with the compressed data's meta information an extra 18 5094252db10SBaoquan He# bytes are needed. Leading to the formula: 5104252db10SBaoquan He# 5114252db10SBaoquan He# extra_bytes = (uncompressed_size >> 12) + 32768 + 18 + decompressor_size 5124252db10SBaoquan He# 5134252db10SBaoquan He# Adding 8 bytes per 32K is a bit excessive but much easier to calculate. 5144252db10SBaoquan He# Adding 32768 instead of 32767 just makes for round numbers. 5154252db10SBaoquan He# Adding the decompressor_size is necessary as it musht live after all 5164252db10SBaoquan He# of the data as well. Last I measured the decompressor is about 14K. 5174252db10SBaoquan He# 10K of actual data and 4K of bss. 5184252db10SBaoquan He# 5194252db10SBaoquan He# Above analysis is for decompressing gzip compressed kernel only. Up to 5204252db10SBaoquan He# now 6 different decompressor are supported all together. And among them 5214252db10SBaoquan He# xz stores data in chunks and has maximum chunk of 64K. Hence safety 5224252db10SBaoquan He# margin should be updated to cover all decompressors so that we don't 5234252db10SBaoquan He# need to deal with each of them separately. Please check 5244252db10SBaoquan He# the description in lib/decompressor_xxx.c for specific information. 5254252db10SBaoquan He# 5264252db10SBaoquan He# extra_bytes = (uncompressed_size >> 12) + 65536 + 128 5274252db10SBaoquan He# 5284252db10SBaoquan He# Note that this calculation, which results in z_extract_offset (below), 5294252db10SBaoquan He# is currently generated in compressed/mkpiggy.c 5304252db10SBaoquan He 531c6ac4c18SH. Peter Anvin#define ZO_INIT_SIZE (ZO__end - ZO_startup_32 + ZO_z_extract_offset) 53237ba7ab5SH. Peter Anvin#define VO_INIT_SIZE (VO__end - VO__text) 53337ba7ab5SH. Peter Anvin#if ZO_INIT_SIZE > VO_INIT_SIZE 53437ba7ab5SH. Peter Anvin#define INIT_SIZE ZO_INIT_SIZE 53537ba7ab5SH. Peter Anvin#else 53637ba7ab5SH. Peter Anvin#define INIT_SIZE VO_INIT_SIZE 53737ba7ab5SH. Peter Anvin#endif 53837ba7ab5SH. Peter Anvininit_size: .long INIT_SIZE # kernel initialization size 53986134a1bSMatt Fleminghandover_offset: .long 0 # Filled in by build.c 54037ba7ab5SH. Peter Anvin 54196ae6ea0SThomas Gleixner# End of setup header ##################################################### 54296ae6ea0SThomas Gleixner 5437a734e7dSH. Peter Anvin .section ".entrytext", "ax" 54496ae6ea0SThomas Gleixnerstart_of_setup: 54596ae6ea0SThomas Gleixner# Force %es = %ds 54696ae6ea0SThomas Gleixner movw %ds, %ax 54796ae6ea0SThomas Gleixner movw %ax, %es 54896ae6ea0SThomas Gleixner cld 54996ae6ea0SThomas Gleixner 55016252da6SJens Rottmann# Apparently some ancient versions of LILO invoked the kernel with %ss != %ds, 55116252da6SJens Rottmann# which happened to work by accident for the old code. Recalculate the stack 55216252da6SJens Rottmann# pointer if %ss is invalid. Otherwise leave it alone, LOADLIN sets up the 55316252da6SJens Rottmann# stack behind its own code, so we can't blindly put it directly past the heap. 5546b6815c6SH. Peter Anvin 5556b6815c6SH. Peter Anvin movw %ss, %dx 5566b6815c6SH. Peter Anvin cmpw %ax, %dx # %ds == %ss? 5576b6815c6SH. Peter Anvin movw %sp, %dx 55816252da6SJens Rottmann je 2f # -> assume %sp is reasonably set 5596b6815c6SH. Peter Anvin 56016252da6SJens Rottmann # Invalid %ss, make up a new stack 56116252da6SJens Rottmann movw $_end, %dx 56216252da6SJens Rottmann testb $CAN_USE_HEAP, loadflags 56316252da6SJens Rottmann jz 1f 56416252da6SJens Rottmann movw heap_end_ptr, %dx 56516252da6SJens Rottmann1: addw $STACK_SIZE, %dx 56616252da6SJens Rottmann jnc 2f 56716252da6SJens Rottmann xorw %dx, %dx # Prevent wraparound 56816252da6SJens Rottmann 56916252da6SJens Rottmann2: # Now %dx should point to the end of our stack space 5706b6815c6SH. Peter Anvin andw $~3, %dx # dword align (might as well...) 5716b6815c6SH. Peter Anvin jnz 3f 5726b6815c6SH. Peter Anvin movw $0xfffc, %dx # Make sure we're not zero 57316252da6SJens Rottmann3: movw %ax, %ss 5746b6815c6SH. Peter Anvin movzwl %dx, %esp # Clear upper half of %esp 5756b6815c6SH. Peter Anvin sti # Now we should have a working stack 5766b6815c6SH. Peter Anvin 5776b6815c6SH. Peter Anvin# We will have entered with %cs = %ds+0x20, normalize %cs so 5786b6815c6SH. Peter Anvin# it is on par with the other segments. 5796b6815c6SH. Peter Anvin pushw %ds 5806b6815c6SH. Peter Anvin pushw $6f 5816b6815c6SH. Peter Anvin lretw 5826b6815c6SH. Peter Anvin6: 58396ae6ea0SThomas Gleixner 58496ae6ea0SThomas Gleixner# Check signature at end of setup 58596ae6ea0SThomas Gleixner cmpl $0x5a5aaa55, setup_sig 58696ae6ea0SThomas Gleixner jne setup_bad 58796ae6ea0SThomas Gleixner 58896ae6ea0SThomas Gleixner# Zero the bss 58996ae6ea0SThomas Gleixner movw $__bss_start, %di 59096ae6ea0SThomas Gleixner movw $_end+3, %cx 59196ae6ea0SThomas Gleixner xorl %eax, %eax 59296ae6ea0SThomas Gleixner subw %di, %cx 59396ae6ea0SThomas Gleixner shrw $2, %cx 59496ae6ea0SThomas Gleixner rep; stosl 59596ae6ea0SThomas Gleixner 59696ae6ea0SThomas Gleixner# Jump to C code (should not return) 59796ae6ea0SThomas Gleixner calll main 59896ae6ea0SThomas Gleixner 59996ae6ea0SThomas Gleixner# Setup corrupt somehow... 60096ae6ea0SThomas Gleixnersetup_bad: 60196ae6ea0SThomas Gleixner movl $setup_corrupt, %eax 60296ae6ea0SThomas Gleixner calll puts 60396ae6ea0SThomas Gleixner # Fall through... 60496ae6ea0SThomas Gleixner 60596ae6ea0SThomas Gleixner .globl die 60696ae6ea0SThomas Gleixner .type die, @function 60796ae6ea0SThomas Gleixnerdie: 60896ae6ea0SThomas Gleixner hlt 60996ae6ea0SThomas Gleixner jmp die 61096ae6ea0SThomas Gleixner 61196ae6ea0SThomas Gleixner .size die, .-die 61296ae6ea0SThomas Gleixner 61396ae6ea0SThomas Gleixner .section ".initdata", "a" 61496ae6ea0SThomas Gleixnersetup_corrupt: 61596ae6ea0SThomas Gleixner .byte 7 61696ae6ea0SThomas Gleixner .string "No setup signature found...\n" 617