xref: /openbmc/linux/arch/x86/boot/header.S (revision 8e102324)
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
78291f3632SMatt Fleming	# Filled in by build.c
79291f3632SMatt Fleming	.long	0				# SizeOfCode
80291f3632SMatt Fleming
81291f3632SMatt Fleming	.long	0				# SizeOfInitializedData
82291f3632SMatt Fleming	.long	0				# SizeOfUninitializedData
83291f3632SMatt Fleming
84291f3632SMatt Fleming	# Filled in by build.c
85291f3632SMatt Fleming	.long	0x0000				# AddressOfEntryPoint
86291f3632SMatt Fleming
87e31be363SMatt Fleming	.long	0x0200				# BaseOfCode
88291f3632SMatt Fleming#ifdef CONFIG_X86_32
89291f3632SMatt Fleming	.long	0				# data
90291f3632SMatt Fleming#endif
91291f3632SMatt Fleming
92291f3632SMatt Flemingextra_header_fields:
93291f3632SMatt Fleming#ifdef CONFIG_X86_32
944bac079dSArd Biesheuvel	.long	0				# ImageBase
95291f3632SMatt Fleming#else
964bac079dSArd Biesheuvel	.quad	0				# ImageBase
97291f3632SMatt Fleming#endif
98fa5c3501SMatt Fleming	.long	0x20				# SectionAlignment
99743628e8SJordan Justen	.long	0x20				# FileAlignment
100291f3632SMatt Fleming	.word	0				# MajorOperatingSystemVersion
101291f3632SMatt Fleming	.word	0				# MinorOperatingSystemVersion
102148d3f71SArd Biesheuvel	.word	LINUX_EFISTUB_MAJOR_VERSION	# MajorImageVersion
103148d3f71SArd Biesheuvel	.word	LINUX_EFISTUB_MINOR_VERSION	# MinorImageVersion
104291f3632SMatt Fleming	.word	0				# MajorSubsystemVersion
105291f3632SMatt Fleming	.word	0				# MinorSubsystemVersion
106291f3632SMatt Fleming	.long	0				# Win32VersionValue
107291f3632SMatt Fleming
108291f3632SMatt Fleming	#
109291f3632SMatt Fleming	# The size of the bzImage is written in tools/build.c
110291f3632SMatt Fleming	#
111291f3632SMatt Fleming	.long	0				# SizeOfImage
112291f3632SMatt Fleming
113291f3632SMatt Fleming	.long	0x200				# SizeOfHeaders
114291f3632SMatt Fleming	.long	0				# CheckSum
115a3326a0dSArd Biesheuvel	.word	IMAGE_SUBSYSTEM_EFI_APPLICATION	# Subsystem (EFI application)
116ca209f8bSLukas Bulwahn#ifdef CONFIG_EFI_DXE_MEM_ATTRIBUTES
11724b72bb1SPeter Jones	.word	IMAGE_DLL_CHARACTERISTICS_NX_COMPAT	# DllCharacteristics
11824b72bb1SPeter Jones#else
119291f3632SMatt Fleming	.word	0				# DllCharacteristics
12024b72bb1SPeter Jones#endif
121291f3632SMatt Fleming#ifdef CONFIG_X86_32
122291f3632SMatt Fleming	.long	0				# SizeOfStackReserve
123291f3632SMatt Fleming	.long	0				# SizeOfStackCommit
124291f3632SMatt Fleming	.long	0				# SizeOfHeapReserve
125291f3632SMatt Fleming	.long	0				# SizeOfHeapCommit
126291f3632SMatt Fleming#else
127291f3632SMatt Fleming	.quad	0				# SizeOfStackReserve
128291f3632SMatt Fleming	.quad	0				# SizeOfStackCommit
129291f3632SMatt Fleming	.quad	0				# SizeOfHeapReserve
130291f3632SMatt Fleming	.quad	0				# SizeOfHeapCommit
131291f3632SMatt Fleming#endif
132291f3632SMatt Fleming	.long	0				# LoaderFlags
133a3326a0dSArd Biesheuvel	.long	(section_table - .) / 8		# NumberOfRvaAndSizes
134291f3632SMatt Fleming
135291f3632SMatt Fleming	.quad	0				# ExportTable
136291f3632SMatt Fleming	.quad	0				# ImportTable
137291f3632SMatt Fleming	.quad	0				# ResourceTable
138291f3632SMatt Fleming	.quad	0				# ExceptionTable
139291f3632SMatt Fleming	.quad	0				# CertificationTable
140291f3632SMatt Fleming	.quad	0				# BaseRelocationTable
141291f3632SMatt Fleming
142291f3632SMatt Fleming	# Section table
143291f3632SMatt Flemingsection_table:
144743628e8SJordan Justen	#
145743628e8SJordan Justen	# The offset & size fields are filled in by build.c.
146743628e8SJordan Justen	#
147743628e8SJordan Justen	.ascii	".setup"
148291f3632SMatt Fleming	.byte	0
149291f3632SMatt Fleming	.byte	0
150291f3632SMatt Fleming	.long	0
151291f3632SMatt Fleming	.long	0x0				# startup_{32,64}
152291f3632SMatt Fleming	.long	0				# Size of initialized data
153291f3632SMatt Fleming						# on disk
154291f3632SMatt Fleming	.long	0x0				# startup_{32,64}
155291f3632SMatt Fleming	.long	0				# PointerToRelocations
156291f3632SMatt Fleming	.long	0				# PointerToLineNumbers
157291f3632SMatt Fleming	.word	0				# NumberOfRelocations
158291f3632SMatt Fleming	.word	0				# NumberOfLineNumbers
159a3326a0dSArd Biesheuvel	.long	IMAGE_SCN_CNT_CODE		| \
160a3326a0dSArd Biesheuvel		IMAGE_SCN_MEM_READ		| \
161e17a8bf1SArd Biesheuvel		IMAGE_SCN_MEM_EXECUTE		# Characteristics
162291f3632SMatt Fleming
163291f3632SMatt Fleming	#
164291f3632SMatt Fleming	# The EFI application loader requires a relocation section
165743628e8SJordan Justen	# because EFI applications must be relocatable. The .reloc
166743628e8SJordan Justen	# offset & size fields are filled in by build.c.
167291f3632SMatt Fleming	#
168291f3632SMatt Fleming	.ascii	".reloc"
169291f3632SMatt Fleming	.byte	0
170291f3632SMatt Fleming	.byte	0
1712e064b1eSJordan Justen	.long	0
1722e064b1eSJordan Justen	.long	0
1732e064b1eSJordan Justen	.long	0				# SizeOfRawData
1742e064b1eSJordan Justen	.long	0				# PointerToRawData
175291f3632SMatt Fleming	.long	0				# PointerToRelocations
176291f3632SMatt Fleming	.long	0				# PointerToLineNumbers
177291f3632SMatt Fleming	.word	0				# NumberOfRelocations
178291f3632SMatt Fleming	.word	0				# NumberOfLineNumbers
179a3326a0dSArd Biesheuvel	.long	IMAGE_SCN_CNT_INITIALIZED_DATA	| \
180a3326a0dSArd Biesheuvel		IMAGE_SCN_MEM_READ		| \
181e17a8bf1SArd Biesheuvel		IMAGE_SCN_MEM_DISCARDABLE	# Characteristics
182743628e8SJordan Justen
18397aa2765SArd Biesheuvel#ifdef CONFIG_EFI_MIXED
18497aa2765SArd Biesheuvel	#
18597aa2765SArd Biesheuvel	# The offset & size fields are filled in by build.c.
18697aa2765SArd Biesheuvel	#
18797aa2765SArd Biesheuvel	.asciz	".compat"
18897aa2765SArd Biesheuvel	.long	0
18997aa2765SArd Biesheuvel	.long	0x0
19097aa2765SArd Biesheuvel	.long	0				# Size of initialized data
19197aa2765SArd Biesheuvel						# on disk
19297aa2765SArd Biesheuvel	.long	0x0
19397aa2765SArd Biesheuvel	.long	0				# PointerToRelocations
19497aa2765SArd Biesheuvel	.long	0				# PointerToLineNumbers
19597aa2765SArd Biesheuvel	.word	0				# NumberOfRelocations
19697aa2765SArd Biesheuvel	.word	0				# NumberOfLineNumbers
197a3326a0dSArd Biesheuvel	.long	IMAGE_SCN_CNT_INITIALIZED_DATA	| \
198a3326a0dSArd Biesheuvel		IMAGE_SCN_MEM_READ		| \
199e17a8bf1SArd Biesheuvel		IMAGE_SCN_MEM_DISCARDABLE	# Characteristics
20097aa2765SArd Biesheuvel#endif
20197aa2765SArd Biesheuvel
202743628e8SJordan Justen	#
203743628e8SJordan Justen	# The offset & size fields are filled in by build.c.
204743628e8SJordan Justen	#
205743628e8SJordan Justen	.ascii	".text"
206743628e8SJordan Justen	.byte	0
207743628e8SJordan Justen	.byte	0
208743628e8SJordan Justen	.byte	0
209743628e8SJordan Justen	.long	0
210743628e8SJordan Justen	.long	0x0				# startup_{32,64}
211743628e8SJordan Justen	.long	0				# Size of initialized data
212743628e8SJordan Justen						# on disk
213743628e8SJordan Justen	.long	0x0				# startup_{32,64}
214743628e8SJordan Justen	.long	0				# PointerToRelocations
215743628e8SJordan Justen	.long	0				# PointerToLineNumbers
216743628e8SJordan Justen	.word	0				# NumberOfRelocations
217743628e8SJordan Justen	.word	0				# NumberOfLineNumbers
218a3326a0dSArd Biesheuvel	.long	IMAGE_SCN_CNT_CODE		| \
219a3326a0dSArd Biesheuvel		IMAGE_SCN_MEM_READ		| \
220e17a8bf1SArd Biesheuvel		IMAGE_SCN_MEM_EXECUTE		# Characteristics
221743628e8SJordan Justen
22297aa2765SArd Biesheuvel	.set	section_count, (. - section_table) / 40
223291f3632SMatt Fleming#endif /* CONFIG_EFI_STUB */
22496ae6ea0SThomas Gleixner
22596ae6ea0SThomas Gleixner	# Kernel attributes; used by setup.  This is part 1 of the
22696ae6ea0SThomas Gleixner	# header, from the old boot sector.
22796ae6ea0SThomas Gleixner
22896ae6ea0SThomas Gleixner	.section ".header", "a"
22909c205afSH. Peter Anvin	.globl	sentinel
23009c205afSH. Peter Anvinsentinel:	.byte 0xff, 0xff        /* Used to detect broken loaders */
23109c205afSH. Peter Anvin
23296ae6ea0SThomas Gleixner	.globl	hdr
23396ae6ea0SThomas Gleixnerhdr:
2345e47c478SH. Peter Anvinsetup_sects:	.byte 0			/* Filled in by build.c */
23596ae6ea0SThomas Gleixnerroot_flags:	.word ROOT_RDONLY
2365e47c478SH. Peter Anvinsyssize:	.long 0			/* Filled in by build.c */
2375e47c478SH. Peter Anvinram_size:	.word 0			/* Obsolete */
23896ae6ea0SThomas Gleixnervid_mode:	.word SVGA_MODE
23908796fc9SArd Biesheuvelroot_dev:	.word 0			/* Default to major/minor 0/0 */
24096ae6ea0SThomas Gleixnerboot_flag:	.word 0xAA55
24196ae6ea0SThomas Gleixner
24296ae6ea0SThomas Gleixner	# offset 512, entry point
24396ae6ea0SThomas Gleixner
24496ae6ea0SThomas Gleixner	.globl	_start
24596ae6ea0SThomas Gleixner_start:
24696ae6ea0SThomas Gleixner		# Explicitly enter this as bytes, or the assembler
24796ae6ea0SThomas Gleixner		# tries to generate a 3-byte jump here, which causes
24896ae6ea0SThomas Gleixner		# everything else to push off to the wrong offset.
24996ae6ea0SThomas Gleixner		.byte	0xeb		# short (2-byte) jump
25096ae6ea0SThomas Gleixner		.byte	start_of_setup-1f
25196ae6ea0SThomas Gleixner1:
25296ae6ea0SThomas Gleixner
25396ae6ea0SThomas Gleixner	# Part 2 of the header, from the old setup.S
25496ae6ea0SThomas Gleixner
25596ae6ea0SThomas Gleixner		.ascii	"HdrS"		# header signature
256b3c72fc9SDaniel Kiper		.word	0x020f		# header version number (>= 0x0105)
25796ae6ea0SThomas Gleixner					# or else old loadlin-1.5 will fail)
25896ae6ea0SThomas Gleixner		.globl realmode_swtch
25996ae6ea0SThomas Gleixnerrealmode_swtch:	.word	0, 0		# default_switch, SETUPSEG
2605e47c478SH. Peter Anvinstart_sys_seg:	.word	SYSSEG		# obsolete and meaningless, but just
2615e47c478SH. Peter Anvin					# in case something decided to "use" it
26296ae6ea0SThomas Gleixner		.word	kernel_version-512 # pointing to kernel version string
26396ae6ea0SThomas Gleixner					# above section of header is compatible
26496ae6ea0SThomas Gleixner					# with loadlin-1.5 (header v1.5). Don't
26596ae6ea0SThomas Gleixner					# change it.
26696ae6ea0SThomas Gleixner
2675e47c478SH. Peter Anvintype_of_loader:	.byte	0		# 0 means ancient bootloader, newer
2685e47c478SH. Peter Anvin					# bootloaders know to change this.
269ff61f079SJonathan Corbet					# See Documentation/arch/x86/boot.rst for
27096ae6ea0SThomas Gleixner					# assigned ids
27196ae6ea0SThomas Gleixner
27296ae6ea0SThomas Gleixner# flags, unused bits must be zero (RFU) bit within loadflags
27396ae6ea0SThomas Gleixnerloadflags:
27409c205afSH. Peter Anvin		.byte	LOADED_HIGH	# The kernel is to be loaded high
27596ae6ea0SThomas Gleixner
27696ae6ea0SThomas Gleixnersetup_move_size: .word  0x8000		# size to move, when setup is not
27796ae6ea0SThomas Gleixner					# loaded at 0x90000. We will move setup
27896ae6ea0SThomas Gleixner					# to 0x90000 then just before jumping
27996ae6ea0SThomas Gleixner					# into the kernel. However, only the
28096ae6ea0SThomas Gleixner					# loader knows how much data behind
28196ae6ea0SThomas Gleixner					# us also needs to be loaded.
28296ae6ea0SThomas Gleixner
28396ae6ea0SThomas Gleixnercode32_start:				# here loaders can put a different
28496ae6ea0SThomas Gleixner					# start address for 32-bit code.
28596ae6ea0SThomas Gleixner		.long	0x100000	# 0x100000 = default for big kernel
28696ae6ea0SThomas Gleixner
28796ae6ea0SThomas Gleixnerramdisk_image:	.long	0		# address of loaded ramdisk image
28896ae6ea0SThomas Gleixner					# Here the loader puts the 32-bit
28996ae6ea0SThomas Gleixner					# address where it loaded the image.
29096ae6ea0SThomas Gleixner					# This only will be read by the kernel.
29196ae6ea0SThomas Gleixner
29296ae6ea0SThomas Gleixnerramdisk_size:	.long	0		# its size in bytes
29396ae6ea0SThomas Gleixner
29496ae6ea0SThomas Gleixnerbootsect_kludge:
29596ae6ea0SThomas Gleixner		.long	0		# obsolete
29696ae6ea0SThomas Gleixner
2976b6815c6SH. Peter Anvinheap_end_ptr:	.word	_end+STACK_SIZE-512
2986b6815c6SH. Peter Anvin					# (Header version 0x0201 or later)
29996ae6ea0SThomas Gleixner					# space from here (exclusive) down to
30096ae6ea0SThomas Gleixner					# end of setup code can be used by setup
30196ae6ea0SThomas Gleixner					# for local heap purposes.
30296ae6ea0SThomas Gleixner
3035031296cSH. Peter Anvinext_loader_ver:
3045031296cSH. Peter Anvin		.byte	0		# Extended boot loader version
3055031296cSH. Peter Anvinext_loader_type:
3065031296cSH. Peter Anvin		.byte	0		# Extended boot loader type
3075031296cSH. Peter Anvin
30896ae6ea0SThomas Gleixnercmd_line_ptr:	.long	0		# (Header version 0x0202 or later)
30996ae6ea0SThomas Gleixner					# If nonzero, a 32-bit pointer
31096ae6ea0SThomas Gleixner					# to the kernel command line.
31196ae6ea0SThomas Gleixner					# The command line should be
31296ae6ea0SThomas Gleixner					# located between the start of
31396ae6ea0SThomas Gleixner					# setup and the end of low
31496ae6ea0SThomas Gleixner					# memory (0xa0000), or it may
31596ae6ea0SThomas Gleixner					# get overwritten before it
31696ae6ea0SThomas Gleixner					# gets read.  If this field is
31796ae6ea0SThomas Gleixner					# used, there is no longer
31896ae6ea0SThomas Gleixner					# anything magical about the
31996ae6ea0SThomas Gleixner					# 0x90000 segment; the setup
32096ae6ea0SThomas Gleixner					# can be located anywhere in
32196ae6ea0SThomas Gleixner					# low memory 0x10000 or higher.
32296ae6ea0SThomas Gleixner
3233e920b53SBorislav Petkovinitrd_addr_max: .long 0x7fffffff
32496ae6ea0SThomas Gleixner					# (Header version 0x0203 or later)
32596ae6ea0SThomas Gleixner					# The highest safe address for
32696ae6ea0SThomas Gleixner					# the contents of an initrd
327cf8fa920SH. Peter Anvin					# The current kernel allows up to 4 GB,
328cf8fa920SH. Peter Anvin					# but leave it at 2 GB to avoid
329cf8fa920SH. Peter Anvin					# possible bootloader bugs.
33096ae6ea0SThomas Gleixner
33196ae6ea0SThomas Gleixnerkernel_alignment:  .long CONFIG_PHYSICAL_ALIGN	#physical addr alignment
33296ae6ea0SThomas Gleixner						#required for protected mode
33396ae6ea0SThomas Gleixner						#kernel
33496ae6ea0SThomas Gleixner#ifdef CONFIG_RELOCATABLE
33596ae6ea0SThomas Gleixnerrelocatable_kernel:    .byte 1
33696ae6ea0SThomas Gleixner#else
33796ae6ea0SThomas Gleixnerrelocatable_kernel:    .byte 0
33896ae6ea0SThomas Gleixner#endif
33937ba7ab5SH. Peter Anvinmin_alignment:		.byte MIN_KERNEL_ALIGN_LG2	# minimum alignment
34009c205afSH. Peter Anvin
34109c205afSH. Peter Anvinxloadflags:
34209c205afSH. Peter Anvin#ifdef CONFIG_X86_64
34309c205afSH. Peter Anvin# define XLF0 XLF_KERNEL_64			/* 64-bit kernel */
34409c205afSH. Peter Anvin#else
34509c205afSH. Peter Anvin# define XLF0 0
34609c205afSH. Peter Anvin#endif
347ee92d815SYinghai Lu
348745c5167SMatt Fleming#if defined(CONFIG_RELOCATABLE) && defined(CONFIG_X86_64)
349ee92d815SYinghai Lu   /* kernel/boot_param/ramdisk could be loaded above 4g */
350ee92d815SYinghai Lu# define XLF1 XLF_CAN_BE_LOADED_ABOVE_4G
351ee92d815SYinghai Lu#else
352ee92d815SYinghai Lu# define XLF1 0
353ee92d815SYinghai Lu#endif
354ee92d815SYinghai Lu
355cc3fdda2SArd Biesheuvel#ifdef CONFIG_EFI_HANDOVER_PROTOCOL
3567d453eeeSMatt Fleming# ifdef CONFIG_EFI_MIXED
3577d453eeeSMatt Fleming#  define XLF23 (XLF_EFI_HANDOVER_32|XLF_EFI_HANDOVER_64)
3587d453eeeSMatt Fleming# else
35909c205afSH. Peter Anvin#  ifdef CONFIG_X86_64
36009c205afSH. Peter Anvin#   define XLF23 XLF_EFI_HANDOVER_64		/* 64-bit EFI handover ok */
36109c205afSH. Peter Anvin#  else
36209c205afSH. Peter Anvin#   define XLF23 XLF_EFI_HANDOVER_32		/* 32-bit EFI handover ok */
36309c205afSH. Peter Anvin#  endif
3647d453eeeSMatt Fleming# endif
36509c205afSH. Peter Anvin#else
36609c205afSH. Peter Anvin# define XLF23 0
36709c205afSH. Peter Anvin#endif
368456a29ddSDave Young
3692965faa5SDave Young#if defined(CONFIG_X86_64) && defined(CONFIG_EFI) && defined(CONFIG_KEXEC_CORE)
370456a29ddSDave Young# define XLF4 XLF_EFI_KEXEC
371456a29ddSDave Young#else
372456a29ddSDave Young# define XLF4 0
373456a29ddSDave Young#endif
374456a29ddSDave Young
375f2d08c5dSBaoquan He#ifdef CONFIG_X86_64
376f2d08c5dSBaoquan He#ifdef CONFIG_X86_5LEVEL
377f2d08c5dSBaoquan He#define XLF56 (XLF_5LEVEL|XLF_5LEVEL_ENABLED)
378f2d08c5dSBaoquan He#else
379f2d08c5dSBaoquan He#define XLF56 XLF_5LEVEL
380f2d08c5dSBaoquan He#endif
381f2d08c5dSBaoquan He#else
382f2d08c5dSBaoquan He#define XLF56 0
383f2d08c5dSBaoquan He#endif
384f2d08c5dSBaoquan He
385f2d08c5dSBaoquan He			.word XLF0 | XLF1 | XLF23 | XLF4 | XLF56
38696ae6ea0SThomas Gleixner
38796ae6ea0SThomas Gleixnercmdline_size:   .long   COMMAND_LINE_SIZE-1     #length of the command line,
38896ae6ea0SThomas Gleixner                                                #added with boot protocol
38996ae6ea0SThomas Gleixner                                                #version 2.06
39096ae6ea0SThomas Gleixner
391a24e7851SRusty Russellhardware_subarch:	.long 0			# subarchitecture, added with 2.07
392a24e7851SRusty Russell						# default to 0 for normal x86 PC
393a24e7851SRusty Russell
394a24e7851SRusty Russellhardware_subarch_data:	.quad 0
395a24e7851SRusty Russell
39677d1a499SH. Peter Anvinpayload_offset:		.long ZO_input_data
39777d1a499SH. Peter Anvinpayload_length:		.long ZO_z_input_len
398099e1377SIan Campbell
3998b664aa6SHuang, Yingsetup_data:		.quad 0			# 64-bit physical pointer to
4008b664aa6SHuang, Ying						# single linked list of
4018b664aa6SHuang, Ying						# struct setup_data
4028b664aa6SHuang, Ying
40337ba7ab5SH. Peter Anvinpref_address:		.quad LOAD_PHYSICAL_ADDR	# preferred load addr
40437ba7ab5SH. Peter Anvin
4054252db10SBaoquan He#
4064252db10SBaoquan He# Getting to provably safe in-place decompression is hard. Worst case
4074252db10SBaoquan He# behaviours need to be analyzed. Here let's take the decompression of
4084252db10SBaoquan He# a gzip-compressed kernel as example, to illustrate it:
4094252db10SBaoquan He#
4104252db10SBaoquan He# The file layout of gzip compressed kernel is:
4114252db10SBaoquan He#
4124252db10SBaoquan He#    magic[2]
4134252db10SBaoquan He#    method[1]
4144252db10SBaoquan He#    flags[1]
4154252db10SBaoquan He#    timestamp[4]
4164252db10SBaoquan He#    extraflags[1]
4174252db10SBaoquan He#    os[1]
4184252db10SBaoquan He#    compressed data blocks[N]
4194252db10SBaoquan He#    crc[4] orig_len[4]
4204252db10SBaoquan He#
4214252db10SBaoquan He# ... resulting in +18 bytes overhead of uncompressed data.
4224252db10SBaoquan He#
4234252db10SBaoquan He# (For more information, please refer to RFC 1951 and RFC 1952.)
4244252db10SBaoquan He#
4254252db10SBaoquan He# Files divided into blocks
4264252db10SBaoquan He# 1 bit (last block flag)
4274252db10SBaoquan He# 2 bits (block type)
4284252db10SBaoquan He#
4294252db10SBaoquan He# 1 block occurs every 32K -1 bytes or when there 50% compression
4304252db10SBaoquan He# has been achieved. The smallest block type encoding is always used.
4314252db10SBaoquan He#
4324252db10SBaoquan He# stored:
4334252db10SBaoquan He#    32 bits length in bytes.
4344252db10SBaoquan He#
4354252db10SBaoquan He# fixed:
4364252db10SBaoquan He#    magic fixed tree.
4374252db10SBaoquan He#    symbols.
4384252db10SBaoquan He#
4394252db10SBaoquan He# dynamic:
4404252db10SBaoquan He#    dynamic tree encoding.
4414252db10SBaoquan He#    symbols.
4424252db10SBaoquan He#
4434252db10SBaoquan He#
4444252db10SBaoquan He# The buffer for decompression in place is the length of the uncompressed
4454252db10SBaoquan He# data, plus a small amount extra to keep the algorithm safe. The
4464252db10SBaoquan He# compressed data is placed at the end of the buffer.  The output pointer
4474252db10SBaoquan He# is placed at the start of the buffer and the input pointer is placed
4484252db10SBaoquan He# where the compressed data starts. Problems will occur when the output
4494252db10SBaoquan He# pointer overruns the input pointer.
4504252db10SBaoquan He#
4514252db10SBaoquan He# The output pointer can only overrun the input pointer if the input
4524252db10SBaoquan He# pointer is moving faster than the output pointer.  A condition only
4534252db10SBaoquan He# triggered by data whose compressed form is larger than the uncompressed
4544252db10SBaoquan He# form.
4554252db10SBaoquan He#
4564252db10SBaoquan He# The worst case at the block level is a growth of the compressed data
4574252db10SBaoquan He# of 5 bytes per 32767 bytes.
4584252db10SBaoquan He#
4594252db10SBaoquan He# The worst case internal to a compressed block is very hard to figure.
4604252db10SBaoquan He# The worst case can at least be bounded by having one bit that represents
4614252db10SBaoquan He# 32764 bytes and then all of the rest of the bytes representing the very
4624252db10SBaoquan He# very last byte.
4634252db10SBaoquan He#
4644252db10SBaoquan He# All of which is enough to compute an amount of extra data that is required
4654252db10SBaoquan He# to be safe.  To avoid problems at the block level allocating 5 extra bytes
4664252db10SBaoquan He# per 32767 bytes of data is sufficient.  To avoid problems internal to a
4674252db10SBaoquan He# block adding an extra 32767 bytes (the worst case uncompressed block size)
4684252db10SBaoquan He# is sufficient, to ensure that in the worst case the decompressed data for
4694252db10SBaoquan He# block will stop the byte before the compressed data for a block begins.
4704252db10SBaoquan He# To avoid problems with the compressed data's meta information an extra 18
4714252db10SBaoquan He# bytes are needed.  Leading to the formula:
4724252db10SBaoquan He#
473d607251bSYinghai Lu# extra_bytes = (uncompressed_size >> 12) + 32768 + 18
4744252db10SBaoquan He#
4754252db10SBaoquan He# Adding 8 bytes per 32K is a bit excessive but much easier to calculate.
4764252db10SBaoquan He# Adding 32768 instead of 32767 just makes for round numbers.
4774252db10SBaoquan He#
4784252db10SBaoquan He# Above analysis is for decompressing gzip compressed kernel only. Up to
4794252db10SBaoquan He# now 6 different decompressor are supported all together. And among them
4804252db10SBaoquan He# xz stores data in chunks and has maximum chunk of 64K. Hence safety
4814252db10SBaoquan He# margin should be updated to cover all decompressors so that we don't
4824252db10SBaoquan He# need to deal with each of them separately. Please check
4834252db10SBaoquan He# the description in lib/decompressor_xxx.c for specific information.
4844252db10SBaoquan He#
4854252db10SBaoquan He# extra_bytes = (uncompressed_size >> 12) + 65536 + 128
4865746f055SJan H. Schönherr#
4875746f055SJan H. Schönherr# LZ4 is even worse: data that cannot be further compressed grows by 0.4%,
4885746f055SJan H. Schönherr# or one byte per 256 bytes. OTOH, we can safely get rid of the +128 as
4895746f055SJan H. Schönherr# the size-dependent part now grows so fast.
4905746f055SJan H. Schönherr#
4915746f055SJan H. Schönherr# extra_bytes = (uncompressed_size >> 8) + 65536
4920fe4f4efSNick Terrell#
4930fe4f4efSNick Terrell# ZSTD compressed data grows by at most 3 bytes per 128K, and only has a 22
4940fe4f4efSNick Terrell# byte fixed overhead but has a maximum block size of 128K, so it needs a
4950fe4f4efSNick Terrell# larger margin.
4960fe4f4efSNick Terrell#
4970fe4f4efSNick Terrell# extra_bytes = (uncompressed_size >> 8) + 131072
4984252db10SBaoquan He
4990fe4f4efSNick Terrell#define ZO_z_extra_bytes	((ZO_z_output_len >> 8) + 131072)
500d607251bSYinghai Lu#if ZO_z_output_len > ZO_z_input_len
501d607251bSYinghai Lu# define ZO_z_extract_offset	(ZO_z_output_len + ZO_z_extra_bytes - \
502d607251bSYinghai Lu				 ZO_z_input_len)
503d607251bSYinghai Lu#else
504d607251bSYinghai Lu# define ZO_z_extract_offset	ZO_z_extra_bytes
505d607251bSYinghai Lu#endif
506d607251bSYinghai Lu
507d607251bSYinghai Lu/*
508d607251bSYinghai Lu * The extract_offset has to be bigger than ZO head section. Otherwise when
509d607251bSYinghai Lu * the head code is running to move ZO to the end of the buffer, it will
510d607251bSYinghai Lu * overwrite the head code itself.
511d607251bSYinghai Lu */
512d607251bSYinghai Lu#if (ZO__ehead - ZO_startup_32) > ZO_z_extract_offset
513d607251bSYinghai Lu# define ZO_z_min_extract_offset ((ZO__ehead - ZO_startup_32 + 4095) & ~4095)
514d607251bSYinghai Lu#else
515d607251bSYinghai Lu# define ZO_z_min_extract_offset ((ZO_z_extract_offset + 4095) & ~4095)
516d607251bSYinghai Lu#endif
517d607251bSYinghai Lu
518d607251bSYinghai Lu#define ZO_INIT_SIZE	(ZO__end - ZO_startup_32 + ZO_z_min_extract_offset)
519d607251bSYinghai Lu
52037ba7ab5SH. Peter Anvin#define VO_INIT_SIZE	(VO__end - VO__text)
52137ba7ab5SH. Peter Anvin#if ZO_INIT_SIZE > VO_INIT_SIZE
52237ba7ab5SH. Peter Anvin# define INIT_SIZE ZO_INIT_SIZE
52337ba7ab5SH. Peter Anvin#else
52437ba7ab5SH. Peter Anvin# define INIT_SIZE VO_INIT_SIZE
52537ba7ab5SH. Peter Anvin#endif
526d607251bSYinghai Lu
52737ba7ab5SH. Peter Anvininit_size:		.long INIT_SIZE		# kernel initialization size
52886134a1bSMatt Fleminghandover_offset:	.long 0			# Filled in by build.c
529*8e102324SArd Biesheuvelkernel_info_offset:	.long ZO_kernel_info
53037ba7ab5SH. Peter Anvin
53196ae6ea0SThomas Gleixner# End of setup header #####################################################
53296ae6ea0SThomas Gleixner
5337a734e7dSH. Peter Anvin	.section ".entrytext", "ax"
53496ae6ea0SThomas Gleixnerstart_of_setup:
53596ae6ea0SThomas Gleixner# Force %es = %ds
53696ae6ea0SThomas Gleixner	movw	%ds, %ax
53796ae6ea0SThomas Gleixner	movw	%ax, %es
53896ae6ea0SThomas Gleixner	cld
53996ae6ea0SThomas Gleixner
54016252da6SJens Rottmann# Apparently some ancient versions of LILO invoked the kernel with %ss != %ds,
54116252da6SJens Rottmann# which happened to work by accident for the old code.  Recalculate the stack
54216252da6SJens Rottmann# pointer if %ss is invalid.  Otherwise leave it alone, LOADLIN sets up the
54316252da6SJens Rottmann# stack behind its own code, so we can't blindly put it directly past the heap.
5446b6815c6SH. Peter Anvin
5456b6815c6SH. Peter Anvin	movw	%ss, %dx
5466b6815c6SH. Peter Anvin	cmpw	%ax, %dx	# %ds == %ss?
5476b6815c6SH. Peter Anvin	movw	%sp, %dx
54816252da6SJens Rottmann	je	2f		# -> assume %sp is reasonably set
5496b6815c6SH. Peter Anvin
55016252da6SJens Rottmann	# Invalid %ss, make up a new stack
55116252da6SJens Rottmann	movw	$_end, %dx
55216252da6SJens Rottmann	testb	$CAN_USE_HEAP, loadflags
55316252da6SJens Rottmann	jz	1f
55416252da6SJens Rottmann	movw	heap_end_ptr, %dx
55516252da6SJens Rottmann1:	addw	$STACK_SIZE, %dx
55616252da6SJens Rottmann	jnc	2f
55716252da6SJens Rottmann	xorw	%dx, %dx	# Prevent wraparound
55816252da6SJens Rottmann
55916252da6SJens Rottmann2:	# Now %dx should point to the end of our stack space
5606b6815c6SH. Peter Anvin	andw	$~3, %dx	# dword align (might as well...)
5616b6815c6SH. Peter Anvin	jnz	3f
5626b6815c6SH. Peter Anvin	movw	$0xfffc, %dx	# Make sure we're not zero
56316252da6SJens Rottmann3:	movw	%ax, %ss
5646b6815c6SH. Peter Anvin	movzwl	%dx, %esp	# Clear upper half of %esp
5656b6815c6SH. Peter Anvin	sti			# Now we should have a working stack
5666b6815c6SH. Peter Anvin
5676b6815c6SH. Peter Anvin# We will have entered with %cs = %ds+0x20, normalize %cs so
5686b6815c6SH. Peter Anvin# it is on par with the other segments.
5696b6815c6SH. Peter Anvin	pushw	%ds
5706b6815c6SH. Peter Anvin	pushw	$6f
5716b6815c6SH. Peter Anvin	lretw
5726b6815c6SH. Peter Anvin6:
57396ae6ea0SThomas Gleixner
57496ae6ea0SThomas Gleixner# Check signature at end of setup
57596ae6ea0SThomas Gleixner	cmpl	$0x5a5aaa55, setup_sig
57696ae6ea0SThomas Gleixner	jne	setup_bad
57796ae6ea0SThomas Gleixner
57896ae6ea0SThomas Gleixner# Zero the bss
57996ae6ea0SThomas Gleixner	movw	$__bss_start, %di
58096ae6ea0SThomas Gleixner	movw	$_end+3, %cx
58196ae6ea0SThomas Gleixner	xorl	%eax, %eax
58296ae6ea0SThomas Gleixner	subw	%di, %cx
58396ae6ea0SThomas Gleixner	shrw	$2, %cx
58496ae6ea0SThomas Gleixner	rep; stosl
58596ae6ea0SThomas Gleixner
58696ae6ea0SThomas Gleixner# Jump to C code (should not return)
58796ae6ea0SThomas Gleixner	calll	main
58896ae6ea0SThomas Gleixner
58996ae6ea0SThomas Gleixner# Setup corrupt somehow...
59096ae6ea0SThomas Gleixnersetup_bad:
59196ae6ea0SThomas Gleixner	movl	$setup_corrupt, %eax
59296ae6ea0SThomas Gleixner	calll	puts
59396ae6ea0SThomas Gleixner	# Fall through...
59496ae6ea0SThomas Gleixner
59596ae6ea0SThomas Gleixner	.globl	die
59696ae6ea0SThomas Gleixner	.type	die, @function
59796ae6ea0SThomas Gleixnerdie:
59896ae6ea0SThomas Gleixner	hlt
59996ae6ea0SThomas Gleixner	jmp	die
60096ae6ea0SThomas Gleixner
60196ae6ea0SThomas Gleixner	.size	die, .-die
60296ae6ea0SThomas Gleixner
60396ae6ea0SThomas Gleixner	.section ".initdata", "a"
60496ae6ea0SThomas Gleixnersetup_corrupt:
60596ae6ea0SThomas Gleixner	.byte	7
60696ae6ea0SThomas Gleixner	.string	"No setup signature found...\n"
607