xref: /openbmc/linux/arch/x86/boot/header.S (revision 0a962f2f)
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
39686b58ceSArd Biesheuvel	.set	salign, 0x1000
40686b58ceSArd Biesheuvel	.set	falign, 0x200
41686b58ceSArd 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
81f7eedad7SArd Biesheuvel	.long	ZO__data			# SizeOfCode
82291f3632SMatt Fleming
83f7eedad7SArd Biesheuvel	.long	ZO__end - ZO__data		# SizeOfInitializedData
84291f3632SMatt Fleming	.long	0				# SizeOfUninitializedData
85291f3632SMatt Fleming
860db81e8eSArd Biesheuvel	.long	setup_size + ZO_efi_pe_entry	# AddressOfEntryPoint
87291f3632SMatt Fleming
88686b58ceSArd 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
954bac079dSArd Biesheuvel	.long	0				# ImageBase
96291f3632SMatt Fleming#else
974bac079dSArd Biesheuvel	.quad	0				# ImageBase
98291f3632SMatt Fleming#endif
99686b58ceSArd Biesheuvel	.long	salign				# SectionAlignment
100686b58ceSArd 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*0a962f2fSArd Biesheuvel	.long	setup_size + ZO__end		# SizeOfImage
110291f3632SMatt Fleming
111686b58ceSArd Biesheuvel	.long	salign				# SizeOfHeaders
112291f3632SMatt Fleming	.long	0				# CheckSum
113a3326a0dSArd Biesheuvel	.word	IMAGE_SUBSYSTEM_EFI_APPLICATION	# Subsystem (EFI application)
114ca209f8bSLukas Bulwahn#ifdef CONFIG_EFI_DXE_MEM_ATTRIBUTES
11524b72bb1SPeter Jones	.word	IMAGE_DLL_CHARACTERISTICS_NX_COMPAT	# DllCharacteristics
11624b72bb1SPeter Jones#else
117291f3632SMatt Fleming	.word	0				# DllCharacteristics
11824b72bb1SPeter Jones#endif
119291f3632SMatt Fleming#ifdef CONFIG_X86_32
120291f3632SMatt Fleming	.long	0				# SizeOfStackReserve
121291f3632SMatt Fleming	.long	0				# SizeOfStackCommit
122291f3632SMatt Fleming	.long	0				# SizeOfHeapReserve
123291f3632SMatt Fleming	.long	0				# SizeOfHeapCommit
124291f3632SMatt Fleming#else
125291f3632SMatt Fleming	.quad	0				# SizeOfStackReserve
126291f3632SMatt Fleming	.quad	0				# SizeOfStackCommit
127291f3632SMatt Fleming	.quad	0				# SizeOfHeapReserve
128291f3632SMatt Fleming	.quad	0				# SizeOfHeapCommit
129291f3632SMatt Fleming#endif
130291f3632SMatt Fleming	.long	0				# LoaderFlags
131a3326a0dSArd Biesheuvel	.long	(section_table - .) / 8		# NumberOfRvaAndSizes
132291f3632SMatt Fleming
133291f3632SMatt Fleming	.quad	0				# ExportTable
134291f3632SMatt Fleming	.quad	0				# ImportTable
135291f3632SMatt Fleming	.quad	0				# ResourceTable
136291f3632SMatt Fleming	.quad	0				# ExceptionTable
137291f3632SMatt Fleming	.quad	0				# CertificationTable
138291f3632SMatt Fleming	.quad	0				# BaseRelocationTable
139291f3632SMatt Fleming
140291f3632SMatt Fleming	# Section table
141291f3632SMatt Flemingsection_table:
142743628e8SJordan Justen	.ascii	".setup"
143291f3632SMatt Fleming	.byte	0
144291f3632SMatt Fleming	.byte	0
145*0a962f2fSArd Biesheuvel	.long	pecompat_fstart - salign 	# VirtualSize
146686b58ceSArd Biesheuvel	.long	salign				# VirtualAddress
147686b58ceSArd Biesheuvel	.long	pecompat_fstart - salign	# SizeOfRawData
148686b58ceSArd Biesheuvel	.long	salign				# PointerToRawData
149291f3632SMatt Fleming
150686b58ceSArd Biesheuvel	.long	0, 0, 0
151a3326a0dSArd Biesheuvel	.long	IMAGE_SCN_CNT_INITIALIZED_DATA	| \
152a3326a0dSArd Biesheuvel		IMAGE_SCN_MEM_READ		| \
153e17a8bf1SArd Biesheuvel		IMAGE_SCN_MEM_DISCARDABLE	# Characteristics
15497aa2765SArd Biesheuvel
155686b58ceSArd Biesheuvel#ifdef CONFIG_EFI_MIXED
156686b58ceSArd Biesheuvel	.asciz	".compat"
157686b58ceSArd Biesheuvel
158*0a962f2fSArd Biesheuvel	.long	pecompat_fsize			# VirtualSize
159*0a962f2fSArd Biesheuvel	.long	pecompat_fstart			# VirtualAddress
160686b58ceSArd Biesheuvel	.long	pecompat_fsize			# SizeOfRawData
161686b58ceSArd Biesheuvel	.long	pecompat_fstart			# PointerToRawData
162686b58ceSArd Biesheuvel
163686b58ceSArd Biesheuvel	.long	0, 0, 0
164686b58ceSArd Biesheuvel	.long	IMAGE_SCN_CNT_INITIALIZED_DATA	| \
165686b58ceSArd Biesheuvel		IMAGE_SCN_MEM_READ		| \
166686b58ceSArd Biesheuvel		IMAGE_SCN_MEM_DISCARDABLE	# Characteristics
167686b58ceSArd Biesheuvel
168686b58ceSArd Biesheuvel	/*
169686b58ceSArd Biesheuvel	 * Put the IA-32 machine type and the associated entry point address in
170686b58ceSArd Biesheuvel	 * the .compat section, so loaders can figure out which other execution
171686b58ceSArd Biesheuvel	 * modes this image supports.
172686b58ceSArd Biesheuvel	 */
173686b58ceSArd Biesheuvel	.pushsection ".pecompat", "a", @progbits
174*0a962f2fSArd Biesheuvel	.balign	salign
175686b58ceSArd Biesheuvel	.globl	pecompat_fstart
176686b58ceSArd Biesheuvelpecompat_fstart:
177686b58ceSArd Biesheuvel	.byte	0x1				# Version
178686b58ceSArd Biesheuvel	.byte	8				# Size
179686b58ceSArd Biesheuvel	.word	IMAGE_FILE_MACHINE_I386		# PE machine type
180686b58ceSArd Biesheuvel	.long	setup_size + ZO_efi32_pe_entry	# Entrypoint
181*0a962f2fSArd Biesheuvel	.byte	0x0				# Sentinel
182686b58ceSArd Biesheuvel	.popsection
183686b58ceSArd Biesheuvel#else
184686b58ceSArd Biesheuvel	.set	pecompat_fstart, setup_size
185686b58ceSArd Biesheuvel#endif
186743628e8SJordan Justen	.ascii	".text"
187743628e8SJordan Justen	.byte	0
188743628e8SJordan Justen	.byte	0
189743628e8SJordan Justen	.byte	0
190f7eedad7SArd Biesheuvel	.long	ZO__data
1910db81e8eSArd Biesheuvel	.long	setup_size
192f7eedad7SArd Biesheuvel	.long	ZO__data			# Size of initialized data
193743628e8SJordan Justen						# on disk
1940db81e8eSArd Biesheuvel	.long	setup_size
195743628e8SJordan Justen	.long	0				# PointerToRelocations
196743628e8SJordan Justen	.long	0				# PointerToLineNumbers
197743628e8SJordan Justen	.word	0				# NumberOfRelocations
198743628e8SJordan Justen	.word	0				# NumberOfLineNumbers
199a3326a0dSArd Biesheuvel	.long	IMAGE_SCN_CNT_CODE		| \
200a3326a0dSArd Biesheuvel		IMAGE_SCN_MEM_READ		| \
201e17a8bf1SArd Biesheuvel		IMAGE_SCN_MEM_EXECUTE		# Characteristics
202743628e8SJordan Justen
203f7eedad7SArd Biesheuvel	.ascii	".data\0\0\0"
204f7eedad7SArd Biesheuvel	.long	ZO__end - ZO__data		# VirtualSize
205f7eedad7SArd Biesheuvel	.long	setup_size + ZO__data		# VirtualAddress
206f7eedad7SArd Biesheuvel	.long	ZO__edata - ZO__data		# SizeOfRawData
207f7eedad7SArd Biesheuvel	.long	setup_size + ZO__data		# PointerToRawData
208f7eedad7SArd Biesheuvel
209f7eedad7SArd Biesheuvel	.long	0, 0, 0
210f7eedad7SArd Biesheuvel	.long	IMAGE_SCN_CNT_INITIALIZED_DATA	| \
211f7eedad7SArd Biesheuvel		IMAGE_SCN_MEM_READ		| \
212f7eedad7SArd Biesheuvel		IMAGE_SCN_MEM_WRITE		# Characteristics
213f7eedad7SArd Biesheuvel
21497aa2765SArd Biesheuvel	.set	section_count, (. - section_table) / 40
215291f3632SMatt Fleming#endif /* CONFIG_EFI_STUB */
21696ae6ea0SThomas Gleixner
21796ae6ea0SThomas Gleixner	# Kernel attributes; used by setup.  This is part 1 of the
21896ae6ea0SThomas Gleixner	# header, from the old boot sector.
21996ae6ea0SThomas Gleixner
22096ae6ea0SThomas Gleixner	.section ".header", "a"
22109c205afSH. Peter Anvin	.globl	sentinel
22209c205afSH. Peter Anvinsentinel:	.byte 0xff, 0xff        /* Used to detect broken loaders */
22309c205afSH. Peter Anvin
22496ae6ea0SThomas Gleixner	.globl	hdr
22596ae6ea0SThomas Gleixnerhdr:
226c731fbcfSArd Biesheuvel		.byte setup_sects - 1
22796ae6ea0SThomas Gleixnerroot_flags:	.word ROOT_RDONLY
2280cf3d613SArd Biesheuvelsyssize:	.long ZO__edata / 16
2295e47c478SH. Peter Anvinram_size:	.word 0			/* Obsolete */
23096ae6ea0SThomas Gleixnervid_mode:	.word SVGA_MODE
23108796fc9SArd Biesheuvelroot_dev:	.word 0			/* Default to major/minor 0/0 */
23296ae6ea0SThomas Gleixnerboot_flag:	.word 0xAA55
23396ae6ea0SThomas Gleixner
23496ae6ea0SThomas Gleixner	# offset 512, entry point
23596ae6ea0SThomas Gleixner
23696ae6ea0SThomas Gleixner	.globl	_start
23796ae6ea0SThomas Gleixner_start:
23896ae6ea0SThomas Gleixner		# Explicitly enter this as bytes, or the assembler
23996ae6ea0SThomas Gleixner		# tries to generate a 3-byte jump here, which causes
24096ae6ea0SThomas Gleixner		# everything else to push off to the wrong offset.
24196ae6ea0SThomas Gleixner		.byte	0xeb		# short (2-byte) jump
24296ae6ea0SThomas Gleixner		.byte	start_of_setup-1f
24396ae6ea0SThomas Gleixner1:
24496ae6ea0SThomas Gleixner
24596ae6ea0SThomas Gleixner	# Part 2 of the header, from the old setup.S
24696ae6ea0SThomas Gleixner
24796ae6ea0SThomas Gleixner		.ascii	"HdrS"		# header signature
248b3c72fc9SDaniel Kiper		.word	0x020f		# header version number (>= 0x0105)
24996ae6ea0SThomas Gleixner					# or else old loadlin-1.5 will fail)
25096ae6ea0SThomas Gleixner		.globl realmode_swtch
25196ae6ea0SThomas Gleixnerrealmode_swtch:	.word	0, 0		# default_switch, SETUPSEG
2525e47c478SH. Peter Anvinstart_sys_seg:	.word	SYSSEG		# obsolete and meaningless, but just
2535e47c478SH. Peter Anvin					# in case something decided to "use" it
25496ae6ea0SThomas Gleixner		.word	kernel_version-512 # pointing to kernel version string
25596ae6ea0SThomas Gleixner					# above section of header is compatible
25696ae6ea0SThomas Gleixner					# with loadlin-1.5 (header v1.5). Don't
25796ae6ea0SThomas Gleixner					# change it.
25896ae6ea0SThomas Gleixner
2595e47c478SH. Peter Anvintype_of_loader:	.byte	0		# 0 means ancient bootloader, newer
2605e47c478SH. Peter Anvin					# bootloaders know to change this.
261ff61f079SJonathan Corbet					# See Documentation/arch/x86/boot.rst for
26296ae6ea0SThomas Gleixner					# assigned ids
26396ae6ea0SThomas Gleixner
26496ae6ea0SThomas Gleixner# flags, unused bits must be zero (RFU) bit within loadflags
26596ae6ea0SThomas Gleixnerloadflags:
26609c205afSH. Peter Anvin		.byte	LOADED_HIGH	# The kernel is to be loaded high
26796ae6ea0SThomas Gleixner
26896ae6ea0SThomas Gleixnersetup_move_size: .word  0x8000		# size to move, when setup is not
26996ae6ea0SThomas Gleixner					# loaded at 0x90000. We will move setup
27096ae6ea0SThomas Gleixner					# to 0x90000 then just before jumping
27196ae6ea0SThomas Gleixner					# into the kernel. However, only the
27296ae6ea0SThomas Gleixner					# loader knows how much data behind
27396ae6ea0SThomas Gleixner					# us also needs to be loaded.
27496ae6ea0SThomas Gleixner
27596ae6ea0SThomas Gleixnercode32_start:				# here loaders can put a different
27696ae6ea0SThomas Gleixner					# start address for 32-bit code.
27796ae6ea0SThomas Gleixner		.long	0x100000	# 0x100000 = default for big kernel
27896ae6ea0SThomas Gleixner
27996ae6ea0SThomas Gleixnerramdisk_image:	.long	0		# address of loaded ramdisk image
28096ae6ea0SThomas Gleixner					# Here the loader puts the 32-bit
28196ae6ea0SThomas Gleixner					# address where it loaded the image.
28296ae6ea0SThomas Gleixner					# This only will be read by the kernel.
28396ae6ea0SThomas Gleixner
28496ae6ea0SThomas Gleixnerramdisk_size:	.long	0		# its size in bytes
28596ae6ea0SThomas Gleixner
28696ae6ea0SThomas Gleixnerbootsect_kludge:
28796ae6ea0SThomas Gleixner		.long	0		# obsolete
28896ae6ea0SThomas Gleixner
2896b6815c6SH. Peter Anvinheap_end_ptr:	.word	_end+STACK_SIZE-512
2906b6815c6SH. Peter Anvin					# (Header version 0x0201 or later)
29196ae6ea0SThomas Gleixner					# space from here (exclusive) down to
29296ae6ea0SThomas Gleixner					# end of setup code can be used by setup
29396ae6ea0SThomas Gleixner					# for local heap purposes.
29496ae6ea0SThomas Gleixner
2955031296cSH. Peter Anvinext_loader_ver:
2965031296cSH. Peter Anvin		.byte	0		# Extended boot loader version
2975031296cSH. Peter Anvinext_loader_type:
2985031296cSH. Peter Anvin		.byte	0		# Extended boot loader type
2995031296cSH. Peter Anvin
30096ae6ea0SThomas Gleixnercmd_line_ptr:	.long	0		# (Header version 0x0202 or later)
30196ae6ea0SThomas Gleixner					# If nonzero, a 32-bit pointer
30296ae6ea0SThomas Gleixner					# to the kernel command line.
30396ae6ea0SThomas Gleixner					# The command line should be
30496ae6ea0SThomas Gleixner					# located between the start of
30596ae6ea0SThomas Gleixner					# setup and the end of low
30696ae6ea0SThomas Gleixner					# memory (0xa0000), or it may
30796ae6ea0SThomas Gleixner					# get overwritten before it
30896ae6ea0SThomas Gleixner					# gets read.  If this field is
30996ae6ea0SThomas Gleixner					# used, there is no longer
31096ae6ea0SThomas Gleixner					# anything magical about the
31196ae6ea0SThomas Gleixner					# 0x90000 segment; the setup
31296ae6ea0SThomas Gleixner					# can be located anywhere in
31396ae6ea0SThomas Gleixner					# low memory 0x10000 or higher.
31496ae6ea0SThomas Gleixner
3153e920b53SBorislav Petkovinitrd_addr_max: .long 0x7fffffff
31696ae6ea0SThomas Gleixner					# (Header version 0x0203 or later)
31796ae6ea0SThomas Gleixner					# The highest safe address for
31896ae6ea0SThomas Gleixner					# the contents of an initrd
319cf8fa920SH. Peter Anvin					# The current kernel allows up to 4 GB,
320cf8fa920SH. Peter Anvin					# but leave it at 2 GB to avoid
321cf8fa920SH. Peter Anvin					# possible bootloader bugs.
32296ae6ea0SThomas Gleixner
32396ae6ea0SThomas Gleixnerkernel_alignment:  .long CONFIG_PHYSICAL_ALIGN	#physical addr alignment
32496ae6ea0SThomas Gleixner						#required for protected mode
32596ae6ea0SThomas Gleixner						#kernel
32696ae6ea0SThomas Gleixner#ifdef CONFIG_RELOCATABLE
32796ae6ea0SThomas Gleixnerrelocatable_kernel:    .byte 1
32896ae6ea0SThomas Gleixner#else
32996ae6ea0SThomas Gleixnerrelocatable_kernel:    .byte 0
33096ae6ea0SThomas Gleixner#endif
33137ba7ab5SH. Peter Anvinmin_alignment:		.byte MIN_KERNEL_ALIGN_LG2	# minimum alignment
33209c205afSH. Peter Anvin
33309c205afSH. Peter Anvinxloadflags:
33409c205afSH. Peter Anvin#ifdef CONFIG_X86_64
33509c205afSH. Peter Anvin# define XLF0 XLF_KERNEL_64			/* 64-bit kernel */
33609c205afSH. Peter Anvin#else
33709c205afSH. Peter Anvin# define XLF0 0
33809c205afSH. Peter Anvin#endif
339ee92d815SYinghai Lu
340745c5167SMatt Fleming#if defined(CONFIG_RELOCATABLE) && defined(CONFIG_X86_64)
341ee92d815SYinghai Lu   /* kernel/boot_param/ramdisk could be loaded above 4g */
342ee92d815SYinghai Lu# define XLF1 XLF_CAN_BE_LOADED_ABOVE_4G
343ee92d815SYinghai Lu#else
344ee92d815SYinghai Lu# define XLF1 0
345ee92d815SYinghai Lu#endif
346ee92d815SYinghai Lu
347cc3fdda2SArd Biesheuvel#ifdef CONFIG_EFI_HANDOVER_PROTOCOL
3487d453eeeSMatt Fleming# ifdef CONFIG_EFI_MIXED
3497d453eeeSMatt Fleming#  define XLF23 (XLF_EFI_HANDOVER_32|XLF_EFI_HANDOVER_64)
3507d453eeeSMatt Fleming# else
35109c205afSH. Peter Anvin#  ifdef CONFIG_X86_64
35209c205afSH. Peter Anvin#   define XLF23 XLF_EFI_HANDOVER_64		/* 64-bit EFI handover ok */
35309c205afSH. Peter Anvin#  else
35409c205afSH. Peter Anvin#   define XLF23 XLF_EFI_HANDOVER_32		/* 32-bit EFI handover ok */
35509c205afSH. Peter Anvin#  endif
3567d453eeeSMatt Fleming# endif
35709c205afSH. Peter Anvin#else
35809c205afSH. Peter Anvin# define XLF23 0
35909c205afSH. Peter Anvin#endif
360456a29ddSDave Young
3612965faa5SDave Young#if defined(CONFIG_X86_64) && defined(CONFIG_EFI) && defined(CONFIG_KEXEC_CORE)
362456a29ddSDave Young# define XLF4 XLF_EFI_KEXEC
363456a29ddSDave Young#else
364456a29ddSDave Young# define XLF4 0
365456a29ddSDave Young#endif
366456a29ddSDave Young
367f2d08c5dSBaoquan He#ifdef CONFIG_X86_64
368f2d08c5dSBaoquan He#ifdef CONFIG_X86_5LEVEL
369f2d08c5dSBaoquan He#define XLF56 (XLF_5LEVEL|XLF_5LEVEL_ENABLED)
370f2d08c5dSBaoquan He#else
371f2d08c5dSBaoquan He#define XLF56 XLF_5LEVEL
372f2d08c5dSBaoquan He#endif
373f2d08c5dSBaoquan He#else
374f2d08c5dSBaoquan He#define XLF56 0
375f2d08c5dSBaoquan He#endif
376f2d08c5dSBaoquan He
377f2d08c5dSBaoquan He			.word XLF0 | XLF1 | XLF23 | XLF4 | XLF56
37896ae6ea0SThomas Gleixner
37996ae6ea0SThomas Gleixnercmdline_size:   .long   COMMAND_LINE_SIZE-1     #length of the command line,
38096ae6ea0SThomas Gleixner                                                #added with boot protocol
38196ae6ea0SThomas Gleixner                                                #version 2.06
38296ae6ea0SThomas Gleixner
383a24e7851SRusty Russellhardware_subarch:	.long 0			# subarchitecture, added with 2.07
384a24e7851SRusty Russell						# default to 0 for normal x86 PC
385a24e7851SRusty Russell
386a24e7851SRusty Russellhardware_subarch_data:	.quad 0
387a24e7851SRusty Russell
38877d1a499SH. Peter Anvinpayload_offset:		.long ZO_input_data
38977d1a499SH. Peter Anvinpayload_length:		.long ZO_z_input_len
390099e1377SIan Campbell
3918b664aa6SHuang, Yingsetup_data:		.quad 0			# 64-bit physical pointer to
3928b664aa6SHuang, Ying						# single linked list of
3938b664aa6SHuang, Ying						# struct setup_data
3948b664aa6SHuang, Ying
39537ba7ab5SH. Peter Anvinpref_address:		.quad LOAD_PHYSICAL_ADDR	# preferred load addr
39637ba7ab5SH. Peter Anvin
3974252db10SBaoquan He#
3984252db10SBaoquan He# Getting to provably safe in-place decompression is hard. Worst case
3994252db10SBaoquan He# behaviours need to be analyzed. Here let's take the decompression of
4004252db10SBaoquan He# a gzip-compressed kernel as example, to illustrate it:
4014252db10SBaoquan He#
4024252db10SBaoquan He# The file layout of gzip compressed kernel is:
4034252db10SBaoquan He#
4044252db10SBaoquan He#    magic[2]
4054252db10SBaoquan He#    method[1]
4064252db10SBaoquan He#    flags[1]
4074252db10SBaoquan He#    timestamp[4]
4084252db10SBaoquan He#    extraflags[1]
4094252db10SBaoquan He#    os[1]
4104252db10SBaoquan He#    compressed data blocks[N]
4114252db10SBaoquan He#    crc[4] orig_len[4]
4124252db10SBaoquan He#
4134252db10SBaoquan He# ... resulting in +18 bytes overhead of uncompressed data.
4144252db10SBaoquan He#
4154252db10SBaoquan He# (For more information, please refer to RFC 1951 and RFC 1952.)
4164252db10SBaoquan He#
4174252db10SBaoquan He# Files divided into blocks
4184252db10SBaoquan He# 1 bit (last block flag)
4194252db10SBaoquan He# 2 bits (block type)
4204252db10SBaoquan He#
4214252db10SBaoquan He# 1 block occurs every 32K -1 bytes or when there 50% compression
4224252db10SBaoquan He# has been achieved. The smallest block type encoding is always used.
4234252db10SBaoquan He#
4244252db10SBaoquan He# stored:
4254252db10SBaoquan He#    32 bits length in bytes.
4264252db10SBaoquan He#
4274252db10SBaoquan He# fixed:
4284252db10SBaoquan He#    magic fixed tree.
4294252db10SBaoquan He#    symbols.
4304252db10SBaoquan He#
4314252db10SBaoquan He# dynamic:
4324252db10SBaoquan He#    dynamic tree encoding.
4334252db10SBaoquan He#    symbols.
4344252db10SBaoquan He#
4354252db10SBaoquan He#
4364252db10SBaoquan He# The buffer for decompression in place is the length of the uncompressed
4374252db10SBaoquan He# data, plus a small amount extra to keep the algorithm safe. The
4384252db10SBaoquan He# compressed data is placed at the end of the buffer.  The output pointer
4394252db10SBaoquan He# is placed at the start of the buffer and the input pointer is placed
4404252db10SBaoquan He# where the compressed data starts. Problems will occur when the output
4414252db10SBaoquan He# pointer overruns the input pointer.
4424252db10SBaoquan He#
4434252db10SBaoquan He# The output pointer can only overrun the input pointer if the input
4444252db10SBaoquan He# pointer is moving faster than the output pointer.  A condition only
4454252db10SBaoquan He# triggered by data whose compressed form is larger than the uncompressed
4464252db10SBaoquan He# form.
4474252db10SBaoquan He#
4484252db10SBaoquan He# The worst case at the block level is a growth of the compressed data
4494252db10SBaoquan He# of 5 bytes per 32767 bytes.
4504252db10SBaoquan He#
4514252db10SBaoquan He# The worst case internal to a compressed block is very hard to figure.
4524252db10SBaoquan He# The worst case can at least be bounded by having one bit that represents
4534252db10SBaoquan He# 32764 bytes and then all of the rest of the bytes representing the very
4544252db10SBaoquan He# very last byte.
4554252db10SBaoquan He#
4564252db10SBaoquan He# All of which is enough to compute an amount of extra data that is required
4574252db10SBaoquan He# to be safe.  To avoid problems at the block level allocating 5 extra bytes
4584252db10SBaoquan He# per 32767 bytes of data is sufficient.  To avoid problems internal to a
4594252db10SBaoquan He# block adding an extra 32767 bytes (the worst case uncompressed block size)
4604252db10SBaoquan He# is sufficient, to ensure that in the worst case the decompressed data for
4614252db10SBaoquan He# block will stop the byte before the compressed data for a block begins.
4624252db10SBaoquan He# To avoid problems with the compressed data's meta information an extra 18
4634252db10SBaoquan He# bytes are needed.  Leading to the formula:
4644252db10SBaoquan He#
465d607251bSYinghai Lu# extra_bytes = (uncompressed_size >> 12) + 32768 + 18
4664252db10SBaoquan He#
4674252db10SBaoquan He# Adding 8 bytes per 32K is a bit excessive but much easier to calculate.
4684252db10SBaoquan He# Adding 32768 instead of 32767 just makes for round numbers.
4694252db10SBaoquan He#
4704252db10SBaoquan He# Above analysis is for decompressing gzip compressed kernel only. Up to
4714252db10SBaoquan He# now 6 different decompressor are supported all together. And among them
4724252db10SBaoquan He# xz stores data in chunks and has maximum chunk of 64K. Hence safety
4734252db10SBaoquan He# margin should be updated to cover all decompressors so that we don't
4744252db10SBaoquan He# need to deal with each of them separately. Please check
4754252db10SBaoquan He# the description in lib/decompressor_xxx.c for specific information.
4764252db10SBaoquan He#
4774252db10SBaoquan He# extra_bytes = (uncompressed_size >> 12) + 65536 + 128
4785746f055SJan H. Schönherr#
4795746f055SJan H. Schönherr# LZ4 is even worse: data that cannot be further compressed grows by 0.4%,
4805746f055SJan H. Schönherr# or one byte per 256 bytes. OTOH, we can safely get rid of the +128 as
4815746f055SJan H. Schönherr# the size-dependent part now grows so fast.
4825746f055SJan H. Schönherr#
4835746f055SJan H. Schönherr# extra_bytes = (uncompressed_size >> 8) + 65536
4840fe4f4efSNick Terrell#
4850fe4f4efSNick Terrell# ZSTD compressed data grows by at most 3 bytes per 128K, and only has a 22
4860fe4f4efSNick Terrell# byte fixed overhead but has a maximum block size of 128K, so it needs a
4870fe4f4efSNick Terrell# larger margin.
4880fe4f4efSNick Terrell#
4890fe4f4efSNick Terrell# extra_bytes = (uncompressed_size >> 8) + 131072
4904252db10SBaoquan He
4910fe4f4efSNick Terrell#define ZO_z_extra_bytes	((ZO_z_output_len >> 8) + 131072)
492d607251bSYinghai Lu#if ZO_z_output_len > ZO_z_input_len
493d607251bSYinghai Lu# define ZO_z_extract_offset	(ZO_z_output_len + ZO_z_extra_bytes - \
494d607251bSYinghai Lu				 ZO_z_input_len)
495d607251bSYinghai Lu#else
496d607251bSYinghai Lu# define ZO_z_extract_offset	ZO_z_extra_bytes
497d607251bSYinghai Lu#endif
498d607251bSYinghai Lu
499d607251bSYinghai Lu/*
500d607251bSYinghai Lu * The extract_offset has to be bigger than ZO head section. Otherwise when
501d607251bSYinghai Lu * the head code is running to move ZO to the end of the buffer, it will
502d607251bSYinghai Lu * overwrite the head code itself.
503d607251bSYinghai Lu */
504d607251bSYinghai Lu#if (ZO__ehead - ZO_startup_32) > ZO_z_extract_offset
505d607251bSYinghai Lu# define ZO_z_min_extract_offset ((ZO__ehead - ZO_startup_32 + 4095) & ~4095)
506d607251bSYinghai Lu#else
507d607251bSYinghai Lu# define ZO_z_min_extract_offset ((ZO_z_extract_offset + 4095) & ~4095)
508d607251bSYinghai Lu#endif
509d607251bSYinghai Lu
510d607251bSYinghai Lu#define ZO_INIT_SIZE	(ZO__end - ZO_startup_32 + ZO_z_min_extract_offset)
511d607251bSYinghai Lu
51237ba7ab5SH. Peter Anvin#define VO_INIT_SIZE	(VO__end - VO__text)
51337ba7ab5SH. Peter Anvin#if ZO_INIT_SIZE > VO_INIT_SIZE
51437ba7ab5SH. Peter Anvin# define INIT_SIZE ZO_INIT_SIZE
51537ba7ab5SH. Peter Anvin#else
51637ba7ab5SH. Peter Anvin# define INIT_SIZE VO_INIT_SIZE
51737ba7ab5SH. Peter Anvin#endif
518d607251bSYinghai Lu
519431b39e6SArd Biesheuvel	.macro		__handover_offset
520431b39e6SArd Biesheuvel#ifndef CONFIG_EFI_HANDOVER_PROTOCOL
521431b39e6SArd Biesheuvel	.long		0
522431b39e6SArd Biesheuvel#elif !defined(CONFIG_X86_64)
523431b39e6SArd Biesheuvel	.long		ZO_efi32_stub_entry
524431b39e6SArd Biesheuvel#else
525431b39e6SArd Biesheuvel	/* Yes, this is really how we defined it :( */
526431b39e6SArd Biesheuvel	.long		ZO_efi64_stub_entry - 0x200
527431b39e6SArd Biesheuvel#ifdef CONFIG_EFI_MIXED
528431b39e6SArd Biesheuvel	.if		ZO_efi32_stub_entry != ZO_efi64_stub_entry - 0x200
529431b39e6SArd Biesheuvel	.error		"32-bit and 64-bit EFI entry points do not match"
530431b39e6SArd Biesheuvel	.endif
531431b39e6SArd Biesheuvel#endif
532431b39e6SArd Biesheuvel#endif
533431b39e6SArd Biesheuvel	.endm
534431b39e6SArd Biesheuvel
53537ba7ab5SH. Peter Anvininit_size:		.long INIT_SIZE		# kernel initialization size
536431b39e6SArd Biesheuvelhandover_offset:	__handover_offset
5378e102324SArd Biesheuvelkernel_info_offset:	.long ZO_kernel_info
53837ba7ab5SH. Peter Anvin
53996ae6ea0SThomas Gleixner# End of setup header #####################################################
54096ae6ea0SThomas Gleixner
5417a734e7dSH. Peter Anvin	.section ".entrytext", "ax"
54296ae6ea0SThomas Gleixnerstart_of_setup:
54396ae6ea0SThomas Gleixner# Force %es = %ds
54496ae6ea0SThomas Gleixner	movw	%ds, %ax
54596ae6ea0SThomas Gleixner	movw	%ax, %es
54696ae6ea0SThomas Gleixner	cld
54796ae6ea0SThomas Gleixner
54816252da6SJens Rottmann# Apparently some ancient versions of LILO invoked the kernel with %ss != %ds,
54916252da6SJens Rottmann# which happened to work by accident for the old code.  Recalculate the stack
55016252da6SJens Rottmann# pointer if %ss is invalid.  Otherwise leave it alone, LOADLIN sets up the
55116252da6SJens Rottmann# stack behind its own code, so we can't blindly put it directly past the heap.
5526b6815c6SH. Peter Anvin
5536b6815c6SH. Peter Anvin	movw	%ss, %dx
5546b6815c6SH. Peter Anvin	cmpw	%ax, %dx	# %ds == %ss?
5556b6815c6SH. Peter Anvin	movw	%sp, %dx
55616252da6SJens Rottmann	je	2f		# -> assume %sp is reasonably set
5576b6815c6SH. Peter Anvin
55816252da6SJens Rottmann	# Invalid %ss, make up a new stack
55916252da6SJens Rottmann	movw	$_end, %dx
56016252da6SJens Rottmann	testb	$CAN_USE_HEAP, loadflags
56116252da6SJens Rottmann	jz	1f
56216252da6SJens Rottmann	movw	heap_end_ptr, %dx
56316252da6SJens Rottmann1:	addw	$STACK_SIZE, %dx
56416252da6SJens Rottmann	jnc	2f
56516252da6SJens Rottmann	xorw	%dx, %dx	# Prevent wraparound
56616252da6SJens Rottmann
56716252da6SJens Rottmann2:	# Now %dx should point to the end of our stack space
5686b6815c6SH. Peter Anvin	andw	$~3, %dx	# dword align (might as well...)
5696b6815c6SH. Peter Anvin	jnz	3f
5706b6815c6SH. Peter Anvin	movw	$0xfffc, %dx	# Make sure we're not zero
57116252da6SJens Rottmann3:	movw	%ax, %ss
5726b6815c6SH. Peter Anvin	movzwl	%dx, %esp	# Clear upper half of %esp
5736b6815c6SH. Peter Anvin	sti			# Now we should have a working stack
5746b6815c6SH. Peter Anvin
5756b6815c6SH. Peter Anvin# We will have entered with %cs = %ds+0x20, normalize %cs so
5766b6815c6SH. Peter Anvin# it is on par with the other segments.
5776b6815c6SH. Peter Anvin	pushw	%ds
5786b6815c6SH. Peter Anvin	pushw	$6f
5796b6815c6SH. Peter Anvin	lretw
5806b6815c6SH. Peter Anvin6:
58196ae6ea0SThomas Gleixner
58296ae6ea0SThomas Gleixner# Check signature at end of setup
58396ae6ea0SThomas Gleixner	cmpl	$0x5a5aaa55, setup_sig
58496ae6ea0SThomas Gleixner	jne	setup_bad
58596ae6ea0SThomas Gleixner
58696ae6ea0SThomas Gleixner# Zero the bss
58796ae6ea0SThomas Gleixner	movw	$__bss_start, %di
58896ae6ea0SThomas Gleixner	movw	$_end+3, %cx
58996ae6ea0SThomas Gleixner	xorl	%eax, %eax
59096ae6ea0SThomas Gleixner	subw	%di, %cx
59196ae6ea0SThomas Gleixner	shrw	$2, %cx
59296ae6ea0SThomas Gleixner	rep; stosl
59396ae6ea0SThomas Gleixner
59496ae6ea0SThomas Gleixner# Jump to C code (should not return)
59596ae6ea0SThomas Gleixner	calll	main
59696ae6ea0SThomas Gleixner
59796ae6ea0SThomas Gleixner# Setup corrupt somehow...
59896ae6ea0SThomas Gleixnersetup_bad:
59996ae6ea0SThomas Gleixner	movl	$setup_corrupt, %eax
60096ae6ea0SThomas Gleixner	calll	puts
60196ae6ea0SThomas Gleixner	# Fall through...
60296ae6ea0SThomas Gleixner
60396ae6ea0SThomas Gleixner	.globl	die
60496ae6ea0SThomas Gleixner	.type	die, @function
60596ae6ea0SThomas Gleixnerdie:
60696ae6ea0SThomas Gleixner	hlt
60796ae6ea0SThomas Gleixner	jmp	die
60896ae6ea0SThomas Gleixner
60996ae6ea0SThomas Gleixner	.size	die, .-die
61096ae6ea0SThomas Gleixner
61196ae6ea0SThomas Gleixner	.section ".initdata", "a"
61296ae6ea0SThomas Gleixnersetup_corrupt:
61396ae6ea0SThomas Gleixner	.byte	7
61496ae6ea0SThomas Gleixner	.string	"No setup signature found...\n"
615