xref: /linux/arch/x86/boot/header.S (revision ca209f8b5f61b74782dd4275ebc7173d92cb4905)
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"
4196ae6ea0SThomas Gleixner
4296ae6ea0SThomas Gleixner	.global bootsect_start
4396ae6ea0SThomas Gleixnerbootsect_start:
44291f3632SMatt Fleming#ifdef CONFIG_EFI_STUB
45291f3632SMatt Fleming	# "MZ", MS-DOS header
46a3326a0dSArd Biesheuvel	.word	MZ_MAGIC
47291f3632SMatt Fleming#endif
4896ae6ea0SThomas Gleixner
4996ae6ea0SThomas Gleixner	# Normalize the start address
5096ae6ea0SThomas Gleixner	ljmp	$BOOTSEG, $start2
5196ae6ea0SThomas Gleixner
5296ae6ea0SThomas Gleixnerstart2:
5396ae6ea0SThomas Gleixner	movw	%cs, %ax
5496ae6ea0SThomas Gleixner	movw	%ax, %ds
5596ae6ea0SThomas Gleixner	movw	%ax, %es
5696ae6ea0SThomas Gleixner	movw	%ax, %ss
5796ae6ea0SThomas Gleixner	xorw	%sp, %sp
5896ae6ea0SThomas Gleixner	sti
5996ae6ea0SThomas Gleixner	cld
6096ae6ea0SThomas Gleixner
6196ae6ea0SThomas Gleixner	movw	$bugger_off_msg, %si
6296ae6ea0SThomas Gleixner
6396ae6ea0SThomas Gleixnermsg_loop:
6496ae6ea0SThomas Gleixner	lodsb
6596ae6ea0SThomas Gleixner	andb	%al, %al
6696ae6ea0SThomas Gleixner	jz	bs_die
6796ae6ea0SThomas Gleixner	movb	$0xe, %ah
6896ae6ea0SThomas Gleixner	movw	$7, %bx
6996ae6ea0SThomas Gleixner	int	$0x10
7096ae6ea0SThomas Gleixner	jmp	msg_loop
7196ae6ea0SThomas Gleixner
7296ae6ea0SThomas Gleixnerbs_die:
7396ae6ea0SThomas Gleixner	# Allow the user to press a key, then reboot
7496ae6ea0SThomas Gleixner	xorw	%ax, %ax
7596ae6ea0SThomas Gleixner	int	$0x16
7696ae6ea0SThomas Gleixner	int	$0x19
7796ae6ea0SThomas Gleixner
7896ae6ea0SThomas Gleixner	# int 0x19 should never return.  In case it does anyway,
7996ae6ea0SThomas Gleixner	# invoke the BIOS reset code...
8096ae6ea0SThomas Gleixner	ljmp	$0xf000,$0xfff0
8196ae6ea0SThomas Gleixner
82291f3632SMatt Fleming#ifdef CONFIG_EFI_STUB
83291f3632SMatt Fleming	.org	0x3c
84291f3632SMatt Fleming	#
85291f3632SMatt Fleming	# Offset to the PE header.
86291f3632SMatt Fleming	#
87291f3632SMatt Fleming	.long	pe_header
88291f3632SMatt Fleming#endif /* CONFIG_EFI_STUB */
89291f3632SMatt Fleming
9096ae6ea0SThomas Gleixner	.section ".bsdata", "a"
9196ae6ea0SThomas Gleixnerbugger_off_msg:
92c7fb93ecSMichael Brown	.ascii	"Use a boot loader.\r\n"
9396ae6ea0SThomas Gleixner	.ascii	"\n"
9496ae6ea0SThomas Gleixner	.ascii	"Remove disk and press any key to reboot...\r\n"
9596ae6ea0SThomas Gleixner	.byte	0
9696ae6ea0SThomas Gleixner
97291f3632SMatt Fleming#ifdef CONFIG_EFI_STUB
98291f3632SMatt Flemingpe_header:
99a3326a0dSArd Biesheuvel	.long	PE_MAGIC
100291f3632SMatt Fleming
101291f3632SMatt Flemingcoff_header:
102291f3632SMatt Fleming#ifdef CONFIG_X86_32
103a3326a0dSArd Biesheuvel	.set	image_file_add_flags, IMAGE_FILE_32BIT_MACHINE
104a3326a0dSArd Biesheuvel	.set	pe_opt_magic, PE_OPT_MAGIC_PE32
105a3326a0dSArd Biesheuvel	.word	IMAGE_FILE_MACHINE_I386
106291f3632SMatt Fleming#else
107a3326a0dSArd Biesheuvel	.set	image_file_add_flags, 0
108a3326a0dSArd Biesheuvel	.set	pe_opt_magic, PE_OPT_MAGIC_PE32PLUS
109a3326a0dSArd Biesheuvel	.word	IMAGE_FILE_MACHINE_AMD64
110291f3632SMatt Fleming#endif
11197aa2765SArd Biesheuvel	.word	section_count			# nr_sections
112291f3632SMatt Fleming	.long	0 				# TimeDateStamp
113291f3632SMatt Fleming	.long	0				# PointerToSymbolTable
114291f3632SMatt Fleming	.long	1				# NumberOfSymbols
115291f3632SMatt Fleming	.word	section_table - optional_header	# SizeOfOptionalHeader
116a3326a0dSArd Biesheuvel	.word	IMAGE_FILE_EXECUTABLE_IMAGE	| \
117a3326a0dSArd Biesheuvel		image_file_add_flags		| \
118a3326a0dSArd Biesheuvel		IMAGE_FILE_DEBUG_STRIPPED	| \
119a3326a0dSArd Biesheuvel		IMAGE_FILE_LINE_NUMS_STRIPPED	# Characteristics
120291f3632SMatt Fleming
121291f3632SMatt Flemingoptional_header:
122a3326a0dSArd Biesheuvel	.word	pe_opt_magic
123291f3632SMatt Fleming	.byte	0x02				# MajorLinkerVersion
124291f3632SMatt Fleming	.byte	0x14				# MinorLinkerVersion
125291f3632SMatt Fleming
126291f3632SMatt Fleming	# Filled in by build.c
127291f3632SMatt Fleming	.long	0				# SizeOfCode
128291f3632SMatt Fleming
129291f3632SMatt Fleming	.long	0				# SizeOfInitializedData
130291f3632SMatt Fleming	.long	0				# SizeOfUninitializedData
131291f3632SMatt Fleming
132291f3632SMatt Fleming	# Filled in by build.c
133291f3632SMatt Fleming	.long	0x0000				# AddressOfEntryPoint
134291f3632SMatt Fleming
135e31be363SMatt Fleming	.long	0x0200				# BaseOfCode
136291f3632SMatt Fleming#ifdef CONFIG_X86_32
137291f3632SMatt Fleming	.long	0				# data
138291f3632SMatt Fleming#endif
139291f3632SMatt Fleming
140291f3632SMatt Flemingextra_header_fields:
14126725192SArvind Sankar	# PE specification requires ImageBase to be 64k aligned
14226725192SArvind Sankar	.set	image_base, (LOAD_PHYSICAL_ADDR + 0xffff) & ~0xffff
143291f3632SMatt Fleming#ifdef CONFIG_X86_32
14426725192SArvind Sankar	.long	image_base			# ImageBase
145291f3632SMatt Fleming#else
14626725192SArvind Sankar	.quad	image_base			# ImageBase
147291f3632SMatt Fleming#endif
148fa5c3501SMatt Fleming	.long	0x20				# SectionAlignment
149743628e8SJordan Justen	.long	0x20				# FileAlignment
150291f3632SMatt Fleming	.word	0				# MajorOperatingSystemVersion
151291f3632SMatt Fleming	.word	0				# MinorOperatingSystemVersion
152148d3f71SArd Biesheuvel	.word	LINUX_EFISTUB_MAJOR_VERSION	# MajorImageVersion
153148d3f71SArd Biesheuvel	.word	LINUX_EFISTUB_MINOR_VERSION	# MinorImageVersion
154291f3632SMatt Fleming	.word	0				# MajorSubsystemVersion
155291f3632SMatt Fleming	.word	0				# MinorSubsystemVersion
156291f3632SMatt Fleming	.long	0				# Win32VersionValue
157291f3632SMatt Fleming
158291f3632SMatt Fleming	#
159291f3632SMatt Fleming	# The size of the bzImage is written in tools/build.c
160291f3632SMatt Fleming	#
161291f3632SMatt Fleming	.long	0				# SizeOfImage
162291f3632SMatt Fleming
163291f3632SMatt Fleming	.long	0x200				# SizeOfHeaders
164291f3632SMatt Fleming	.long	0				# CheckSum
165a3326a0dSArd Biesheuvel	.word	IMAGE_SUBSYSTEM_EFI_APPLICATION	# Subsystem (EFI application)
166*ca209f8bSLukas Bulwahn#ifdef CONFIG_EFI_DXE_MEM_ATTRIBUTES
16724b72bb1SPeter Jones	.word	IMAGE_DLL_CHARACTERISTICS_NX_COMPAT	# DllCharacteristics
16824b72bb1SPeter Jones#else
169291f3632SMatt Fleming	.word	0				# DllCharacteristics
17024b72bb1SPeter Jones#endif
171291f3632SMatt Fleming#ifdef CONFIG_X86_32
172291f3632SMatt Fleming	.long	0				# SizeOfStackReserve
173291f3632SMatt Fleming	.long	0				# SizeOfStackCommit
174291f3632SMatt Fleming	.long	0				# SizeOfHeapReserve
175291f3632SMatt Fleming	.long	0				# SizeOfHeapCommit
176291f3632SMatt Fleming#else
177291f3632SMatt Fleming	.quad	0				# SizeOfStackReserve
178291f3632SMatt Fleming	.quad	0				# SizeOfStackCommit
179291f3632SMatt Fleming	.quad	0				# SizeOfHeapReserve
180291f3632SMatt Fleming	.quad	0				# SizeOfHeapCommit
181291f3632SMatt Fleming#endif
182291f3632SMatt Fleming	.long	0				# LoaderFlags
183a3326a0dSArd Biesheuvel	.long	(section_table - .) / 8		# NumberOfRvaAndSizes
184291f3632SMatt Fleming
185291f3632SMatt Fleming	.quad	0				# ExportTable
186291f3632SMatt Fleming	.quad	0				# ImportTable
187291f3632SMatt Fleming	.quad	0				# ResourceTable
188291f3632SMatt Fleming	.quad	0				# ExceptionTable
189291f3632SMatt Fleming	.quad	0				# CertificationTable
190291f3632SMatt Fleming	.quad	0				# BaseRelocationTable
191291f3632SMatt Fleming
192291f3632SMatt Fleming	# Section table
193291f3632SMatt Flemingsection_table:
194743628e8SJordan Justen	#
195743628e8SJordan Justen	# The offset & size fields are filled in by build.c.
196743628e8SJordan Justen	#
197743628e8SJordan Justen	.ascii	".setup"
198291f3632SMatt Fleming	.byte	0
199291f3632SMatt Fleming	.byte	0
200291f3632SMatt Fleming	.long	0
201291f3632SMatt Fleming	.long	0x0				# startup_{32,64}
202291f3632SMatt Fleming	.long	0				# Size of initialized data
203291f3632SMatt Fleming						# on disk
204291f3632SMatt Fleming	.long	0x0				# startup_{32,64}
205291f3632SMatt Fleming	.long	0				# PointerToRelocations
206291f3632SMatt Fleming	.long	0				# PointerToLineNumbers
207291f3632SMatt Fleming	.word	0				# NumberOfRelocations
208291f3632SMatt Fleming	.word	0				# NumberOfLineNumbers
209a3326a0dSArd Biesheuvel	.long	IMAGE_SCN_CNT_CODE		| \
210a3326a0dSArd Biesheuvel		IMAGE_SCN_MEM_READ		| \
211a3326a0dSArd Biesheuvel		IMAGE_SCN_MEM_EXECUTE		| \
212a3326a0dSArd Biesheuvel		IMAGE_SCN_ALIGN_16BYTES		# Characteristics
213291f3632SMatt Fleming
214291f3632SMatt Fleming	#
215291f3632SMatt Fleming	# The EFI application loader requires a relocation section
216743628e8SJordan Justen	# because EFI applications must be relocatable. The .reloc
217743628e8SJordan Justen	# offset & size fields are filled in by build.c.
218291f3632SMatt Fleming	#
219291f3632SMatt Fleming	.ascii	".reloc"
220291f3632SMatt Fleming	.byte	0
221291f3632SMatt Fleming	.byte	0
2222e064b1eSJordan Justen	.long	0
2232e064b1eSJordan Justen	.long	0
2242e064b1eSJordan Justen	.long	0				# SizeOfRawData
2252e064b1eSJordan Justen	.long	0				# PointerToRawData
226291f3632SMatt Fleming	.long	0				# PointerToRelocations
227291f3632SMatt Fleming	.long	0				# PointerToLineNumbers
228291f3632SMatt Fleming	.word	0				# NumberOfRelocations
229291f3632SMatt Fleming	.word	0				# NumberOfLineNumbers
230a3326a0dSArd Biesheuvel	.long	IMAGE_SCN_CNT_INITIALIZED_DATA	| \
231a3326a0dSArd Biesheuvel		IMAGE_SCN_MEM_READ		| \
232a3326a0dSArd Biesheuvel		IMAGE_SCN_MEM_DISCARDABLE	| \
233a3326a0dSArd Biesheuvel		IMAGE_SCN_ALIGN_1BYTES		# Characteristics
234743628e8SJordan Justen
23597aa2765SArd Biesheuvel#ifdef CONFIG_EFI_MIXED
23697aa2765SArd Biesheuvel	#
23797aa2765SArd Biesheuvel	# The offset & size fields are filled in by build.c.
23897aa2765SArd Biesheuvel	#
23997aa2765SArd Biesheuvel	.asciz	".compat"
24097aa2765SArd Biesheuvel	.long	0
24197aa2765SArd Biesheuvel	.long	0x0
24297aa2765SArd Biesheuvel	.long	0				# Size of initialized data
24397aa2765SArd Biesheuvel						# on disk
24497aa2765SArd Biesheuvel	.long	0x0
24597aa2765SArd Biesheuvel	.long	0				# PointerToRelocations
24697aa2765SArd Biesheuvel	.long	0				# PointerToLineNumbers
24797aa2765SArd Biesheuvel	.word	0				# NumberOfRelocations
24897aa2765SArd Biesheuvel	.word	0				# NumberOfLineNumbers
249a3326a0dSArd Biesheuvel	.long	IMAGE_SCN_CNT_INITIALIZED_DATA	| \
250a3326a0dSArd Biesheuvel		IMAGE_SCN_MEM_READ		| \
251a3326a0dSArd Biesheuvel		IMAGE_SCN_MEM_DISCARDABLE	| \
252a3326a0dSArd Biesheuvel		IMAGE_SCN_ALIGN_1BYTES		# Characteristics
25397aa2765SArd Biesheuvel#endif
25497aa2765SArd Biesheuvel
255743628e8SJordan Justen	#
256743628e8SJordan Justen	# The offset & size fields are filled in by build.c.
257743628e8SJordan Justen	#
258743628e8SJordan Justen	.ascii	".text"
259743628e8SJordan Justen	.byte	0
260743628e8SJordan Justen	.byte	0
261743628e8SJordan Justen	.byte	0
262743628e8SJordan Justen	.long	0
263743628e8SJordan Justen	.long	0x0				# startup_{32,64}
264743628e8SJordan Justen	.long	0				# Size of initialized data
265743628e8SJordan Justen						# on disk
266743628e8SJordan Justen	.long	0x0				# startup_{32,64}
267743628e8SJordan Justen	.long	0				# PointerToRelocations
268743628e8SJordan Justen	.long	0				# PointerToLineNumbers
269743628e8SJordan Justen	.word	0				# NumberOfRelocations
270743628e8SJordan Justen	.word	0				# NumberOfLineNumbers
271a3326a0dSArd Biesheuvel	.long	IMAGE_SCN_CNT_CODE		| \
272a3326a0dSArd Biesheuvel		IMAGE_SCN_MEM_READ		| \
273a3326a0dSArd Biesheuvel		IMAGE_SCN_MEM_EXECUTE		| \
274a3326a0dSArd Biesheuvel		IMAGE_SCN_ALIGN_16BYTES		# Characteristics
275743628e8SJordan Justen
27697aa2765SArd Biesheuvel	.set	section_count, (. - section_table) / 40
277291f3632SMatt Fleming#endif /* CONFIG_EFI_STUB */
27896ae6ea0SThomas Gleixner
27996ae6ea0SThomas Gleixner	# Kernel attributes; used by setup.  This is part 1 of the
28096ae6ea0SThomas Gleixner	# header, from the old boot sector.
28196ae6ea0SThomas Gleixner
28296ae6ea0SThomas Gleixner	.section ".header", "a"
28309c205afSH. Peter Anvin	.globl	sentinel
28409c205afSH. Peter Anvinsentinel:	.byte 0xff, 0xff        /* Used to detect broken loaders */
28509c205afSH. Peter Anvin
28696ae6ea0SThomas Gleixner	.globl	hdr
28796ae6ea0SThomas Gleixnerhdr:
2885e47c478SH. Peter Anvinsetup_sects:	.byte 0			/* Filled in by build.c */
28996ae6ea0SThomas Gleixnerroot_flags:	.word ROOT_RDONLY
2905e47c478SH. Peter Anvinsyssize:	.long 0			/* Filled in by build.c */
2915e47c478SH. Peter Anvinram_size:	.word 0			/* Obsolete */
29296ae6ea0SThomas Gleixnervid_mode:	.word SVGA_MODE
2935e47c478SH. Peter Anvinroot_dev:	.word 0			/* Filled in by build.c */
29496ae6ea0SThomas Gleixnerboot_flag:	.word 0xAA55
29596ae6ea0SThomas Gleixner
29696ae6ea0SThomas Gleixner	# offset 512, entry point
29796ae6ea0SThomas Gleixner
29896ae6ea0SThomas Gleixner	.globl	_start
29996ae6ea0SThomas Gleixner_start:
30096ae6ea0SThomas Gleixner		# Explicitly enter this as bytes, or the assembler
30196ae6ea0SThomas Gleixner		# tries to generate a 3-byte jump here, which causes
30296ae6ea0SThomas Gleixner		# everything else to push off to the wrong offset.
30396ae6ea0SThomas Gleixner		.byte	0xeb		# short (2-byte) jump
30496ae6ea0SThomas Gleixner		.byte	start_of_setup-1f
30596ae6ea0SThomas Gleixner1:
30696ae6ea0SThomas Gleixner
30796ae6ea0SThomas Gleixner	# Part 2 of the header, from the old setup.S
30896ae6ea0SThomas Gleixner
30996ae6ea0SThomas Gleixner		.ascii	"HdrS"		# header signature
310b3c72fc9SDaniel Kiper		.word	0x020f		# header version number (>= 0x0105)
31196ae6ea0SThomas Gleixner					# or else old loadlin-1.5 will fail)
31296ae6ea0SThomas Gleixner		.globl realmode_swtch
31396ae6ea0SThomas Gleixnerrealmode_swtch:	.word	0, 0		# default_switch, SETUPSEG
3145e47c478SH. Peter Anvinstart_sys_seg:	.word	SYSSEG		# obsolete and meaningless, but just
3155e47c478SH. Peter Anvin					# in case something decided to "use" it
31696ae6ea0SThomas Gleixner		.word	kernel_version-512 # pointing to kernel version string
31796ae6ea0SThomas Gleixner					# above section of header is compatible
31896ae6ea0SThomas Gleixner					# with loadlin-1.5 (header v1.5). Don't
31996ae6ea0SThomas Gleixner					# change it.
32096ae6ea0SThomas Gleixner
3215e47c478SH. Peter Anvintype_of_loader:	.byte	0		# 0 means ancient bootloader, newer
3225e47c478SH. Peter Anvin					# bootloaders know to change this.
323cb1aaebeSMauro Carvalho Chehab					# See Documentation/x86/boot.rst for
32496ae6ea0SThomas Gleixner					# assigned ids
32596ae6ea0SThomas Gleixner
32696ae6ea0SThomas Gleixner# flags, unused bits must be zero (RFU) bit within loadflags
32796ae6ea0SThomas Gleixnerloadflags:
32809c205afSH. Peter Anvin		.byte	LOADED_HIGH	# The kernel is to be loaded high
32996ae6ea0SThomas Gleixner
33096ae6ea0SThomas Gleixnersetup_move_size: .word  0x8000		# size to move, when setup is not
33196ae6ea0SThomas Gleixner					# loaded at 0x90000. We will move setup
33296ae6ea0SThomas Gleixner					# to 0x90000 then just before jumping
33396ae6ea0SThomas Gleixner					# into the kernel. However, only the
33496ae6ea0SThomas Gleixner					# loader knows how much data behind
33596ae6ea0SThomas Gleixner					# us also needs to be loaded.
33696ae6ea0SThomas Gleixner
33796ae6ea0SThomas Gleixnercode32_start:				# here loaders can put a different
33896ae6ea0SThomas Gleixner					# start address for 32-bit code.
33996ae6ea0SThomas Gleixner		.long	0x100000	# 0x100000 = default for big kernel
34096ae6ea0SThomas Gleixner
34196ae6ea0SThomas Gleixnerramdisk_image:	.long	0		# address of loaded ramdisk image
34296ae6ea0SThomas Gleixner					# Here the loader puts the 32-bit
34396ae6ea0SThomas Gleixner					# address where it loaded the image.
34496ae6ea0SThomas Gleixner					# This only will be read by the kernel.
34596ae6ea0SThomas Gleixner
34696ae6ea0SThomas Gleixnerramdisk_size:	.long	0		# its size in bytes
34796ae6ea0SThomas Gleixner
34896ae6ea0SThomas Gleixnerbootsect_kludge:
34996ae6ea0SThomas Gleixner		.long	0		# obsolete
35096ae6ea0SThomas Gleixner
3516b6815c6SH. Peter Anvinheap_end_ptr:	.word	_end+STACK_SIZE-512
3526b6815c6SH. Peter Anvin					# (Header version 0x0201 or later)
35396ae6ea0SThomas Gleixner					# space from here (exclusive) down to
35496ae6ea0SThomas Gleixner					# end of setup code can be used by setup
35596ae6ea0SThomas Gleixner					# for local heap purposes.
35696ae6ea0SThomas Gleixner
3575031296cSH. Peter Anvinext_loader_ver:
3585031296cSH. Peter Anvin		.byte	0		# Extended boot loader version
3595031296cSH. Peter Anvinext_loader_type:
3605031296cSH. Peter Anvin		.byte	0		# Extended boot loader type
3615031296cSH. Peter Anvin
36296ae6ea0SThomas Gleixnercmd_line_ptr:	.long	0		# (Header version 0x0202 or later)
36396ae6ea0SThomas Gleixner					# If nonzero, a 32-bit pointer
36496ae6ea0SThomas Gleixner					# to the kernel command line.
36596ae6ea0SThomas Gleixner					# The command line should be
36696ae6ea0SThomas Gleixner					# located between the start of
36796ae6ea0SThomas Gleixner					# setup and the end of low
36896ae6ea0SThomas Gleixner					# memory (0xa0000), or it may
36996ae6ea0SThomas Gleixner					# get overwritten before it
37096ae6ea0SThomas Gleixner					# gets read.  If this field is
37196ae6ea0SThomas Gleixner					# used, there is no longer
37296ae6ea0SThomas Gleixner					# anything magical about the
37396ae6ea0SThomas Gleixner					# 0x90000 segment; the setup
37496ae6ea0SThomas Gleixner					# can be located anywhere in
37596ae6ea0SThomas Gleixner					# low memory 0x10000 or higher.
37696ae6ea0SThomas Gleixner
3773e920b53SBorislav Petkovinitrd_addr_max: .long 0x7fffffff
37896ae6ea0SThomas Gleixner					# (Header version 0x0203 or later)
37996ae6ea0SThomas Gleixner					# The highest safe address for
38096ae6ea0SThomas Gleixner					# the contents of an initrd
381cf8fa920SH. Peter Anvin					# The current kernel allows up to 4 GB,
382cf8fa920SH. Peter Anvin					# but leave it at 2 GB to avoid
383cf8fa920SH. Peter Anvin					# possible bootloader bugs.
38496ae6ea0SThomas Gleixner
38596ae6ea0SThomas Gleixnerkernel_alignment:  .long CONFIG_PHYSICAL_ALIGN	#physical addr alignment
38696ae6ea0SThomas Gleixner						#required for protected mode
38796ae6ea0SThomas Gleixner						#kernel
38896ae6ea0SThomas Gleixner#ifdef CONFIG_RELOCATABLE
38996ae6ea0SThomas Gleixnerrelocatable_kernel:    .byte 1
39096ae6ea0SThomas Gleixner#else
39196ae6ea0SThomas Gleixnerrelocatable_kernel:    .byte 0
39296ae6ea0SThomas Gleixner#endif
39337ba7ab5SH. Peter Anvinmin_alignment:		.byte MIN_KERNEL_ALIGN_LG2	# minimum alignment
39409c205afSH. Peter Anvin
39509c205afSH. Peter Anvinxloadflags:
39609c205afSH. Peter Anvin#ifdef CONFIG_X86_64
39709c205afSH. Peter Anvin# define XLF0 XLF_KERNEL_64			/* 64-bit kernel */
39809c205afSH. Peter Anvin#else
39909c205afSH. Peter Anvin# define XLF0 0
40009c205afSH. Peter Anvin#endif
401ee92d815SYinghai Lu
402745c5167SMatt Fleming#if defined(CONFIG_RELOCATABLE) && defined(CONFIG_X86_64)
403ee92d815SYinghai Lu   /* kernel/boot_param/ramdisk could be loaded above 4g */
404ee92d815SYinghai Lu# define XLF1 XLF_CAN_BE_LOADED_ABOVE_4G
405ee92d815SYinghai Lu#else
406ee92d815SYinghai Lu# define XLF1 0
407ee92d815SYinghai Lu#endif
408ee92d815SYinghai Lu
40909c205afSH. Peter Anvin#ifdef CONFIG_EFI_STUB
4107d453eeeSMatt Fleming# ifdef CONFIG_EFI_MIXED
4117d453eeeSMatt Fleming#  define XLF23 (XLF_EFI_HANDOVER_32|XLF_EFI_HANDOVER_64)
4127d453eeeSMatt Fleming# else
41309c205afSH. Peter Anvin#  ifdef CONFIG_X86_64
41409c205afSH. Peter Anvin#   define XLF23 XLF_EFI_HANDOVER_64		/* 64-bit EFI handover ok */
41509c205afSH. Peter Anvin#  else
41609c205afSH. Peter Anvin#   define XLF23 XLF_EFI_HANDOVER_32		/* 32-bit EFI handover ok */
41709c205afSH. Peter Anvin#  endif
4187d453eeeSMatt Fleming# endif
41909c205afSH. Peter Anvin#else
42009c205afSH. Peter Anvin# define XLF23 0
42109c205afSH. Peter Anvin#endif
422456a29ddSDave Young
4232965faa5SDave Young#if defined(CONFIG_X86_64) && defined(CONFIG_EFI) && defined(CONFIG_KEXEC_CORE)
424456a29ddSDave Young# define XLF4 XLF_EFI_KEXEC
425456a29ddSDave Young#else
426456a29ddSDave Young# define XLF4 0
427456a29ddSDave Young#endif
428456a29ddSDave Young
429f2d08c5dSBaoquan He#ifdef CONFIG_X86_64
430f2d08c5dSBaoquan He#ifdef CONFIG_X86_5LEVEL
431f2d08c5dSBaoquan He#define XLF56 (XLF_5LEVEL|XLF_5LEVEL_ENABLED)
432f2d08c5dSBaoquan He#else
433f2d08c5dSBaoquan He#define XLF56 XLF_5LEVEL
434f2d08c5dSBaoquan He#endif
435f2d08c5dSBaoquan He#else
436f2d08c5dSBaoquan He#define XLF56 0
437f2d08c5dSBaoquan He#endif
438f2d08c5dSBaoquan He
439f2d08c5dSBaoquan He			.word XLF0 | XLF1 | XLF23 | XLF4 | XLF56
44096ae6ea0SThomas Gleixner
44196ae6ea0SThomas Gleixnercmdline_size:   .long   COMMAND_LINE_SIZE-1     #length of the command line,
44296ae6ea0SThomas Gleixner                                                #added with boot protocol
44396ae6ea0SThomas Gleixner                                                #version 2.06
44496ae6ea0SThomas Gleixner
445a24e7851SRusty Russellhardware_subarch:	.long 0			# subarchitecture, added with 2.07
446a24e7851SRusty Russell						# default to 0 for normal x86 PC
447a24e7851SRusty Russell
448a24e7851SRusty Russellhardware_subarch_data:	.quad 0
449a24e7851SRusty Russell
45077d1a499SH. Peter Anvinpayload_offset:		.long ZO_input_data
45177d1a499SH. Peter Anvinpayload_length:		.long ZO_z_input_len
452099e1377SIan Campbell
4538b664aa6SHuang, Yingsetup_data:		.quad 0			# 64-bit physical pointer to
4548b664aa6SHuang, Ying						# single linked list of
4558b664aa6SHuang, Ying						# struct setup_data
4568b664aa6SHuang, Ying
45737ba7ab5SH. Peter Anvinpref_address:		.quad LOAD_PHYSICAL_ADDR	# preferred load addr
45837ba7ab5SH. Peter Anvin
4594252db10SBaoquan He#
4604252db10SBaoquan He# Getting to provably safe in-place decompression is hard. Worst case
4614252db10SBaoquan He# behaviours need to be analyzed. Here let's take the decompression of
4624252db10SBaoquan He# a gzip-compressed kernel as example, to illustrate it:
4634252db10SBaoquan He#
4644252db10SBaoquan He# The file layout of gzip compressed kernel is:
4654252db10SBaoquan He#
4664252db10SBaoquan He#    magic[2]
4674252db10SBaoquan He#    method[1]
4684252db10SBaoquan He#    flags[1]
4694252db10SBaoquan He#    timestamp[4]
4704252db10SBaoquan He#    extraflags[1]
4714252db10SBaoquan He#    os[1]
4724252db10SBaoquan He#    compressed data blocks[N]
4734252db10SBaoquan He#    crc[4] orig_len[4]
4744252db10SBaoquan He#
4754252db10SBaoquan He# ... resulting in +18 bytes overhead of uncompressed data.
4764252db10SBaoquan He#
4774252db10SBaoquan He# (For more information, please refer to RFC 1951 and RFC 1952.)
4784252db10SBaoquan He#
4794252db10SBaoquan He# Files divided into blocks
4804252db10SBaoquan He# 1 bit (last block flag)
4814252db10SBaoquan He# 2 bits (block type)
4824252db10SBaoquan He#
4834252db10SBaoquan He# 1 block occurs every 32K -1 bytes or when there 50% compression
4844252db10SBaoquan He# has been achieved. The smallest block type encoding is always used.
4854252db10SBaoquan He#
4864252db10SBaoquan He# stored:
4874252db10SBaoquan He#    32 bits length in bytes.
4884252db10SBaoquan He#
4894252db10SBaoquan He# fixed:
4904252db10SBaoquan He#    magic fixed tree.
4914252db10SBaoquan He#    symbols.
4924252db10SBaoquan He#
4934252db10SBaoquan He# dynamic:
4944252db10SBaoquan He#    dynamic tree encoding.
4954252db10SBaoquan He#    symbols.
4964252db10SBaoquan He#
4974252db10SBaoquan He#
4984252db10SBaoquan He# The buffer for decompression in place is the length of the uncompressed
4994252db10SBaoquan He# data, plus a small amount extra to keep the algorithm safe. The
5004252db10SBaoquan He# compressed data is placed at the end of the buffer.  The output pointer
5014252db10SBaoquan He# is placed at the start of the buffer and the input pointer is placed
5024252db10SBaoquan He# where the compressed data starts. Problems will occur when the output
5034252db10SBaoquan He# pointer overruns the input pointer.
5044252db10SBaoquan He#
5054252db10SBaoquan He# The output pointer can only overrun the input pointer if the input
5064252db10SBaoquan He# pointer is moving faster than the output pointer.  A condition only
5074252db10SBaoquan He# triggered by data whose compressed form is larger than the uncompressed
5084252db10SBaoquan He# form.
5094252db10SBaoquan He#
5104252db10SBaoquan He# The worst case at the block level is a growth of the compressed data
5114252db10SBaoquan He# of 5 bytes per 32767 bytes.
5124252db10SBaoquan He#
5134252db10SBaoquan He# The worst case internal to a compressed block is very hard to figure.
5144252db10SBaoquan He# The worst case can at least be bounded by having one bit that represents
5154252db10SBaoquan He# 32764 bytes and then all of the rest of the bytes representing the very
5164252db10SBaoquan He# very last byte.
5174252db10SBaoquan He#
5184252db10SBaoquan He# All of which is enough to compute an amount of extra data that is required
5194252db10SBaoquan He# to be safe.  To avoid problems at the block level allocating 5 extra bytes
5204252db10SBaoquan He# per 32767 bytes of data is sufficient.  To avoid problems internal to a
5214252db10SBaoquan He# block adding an extra 32767 bytes (the worst case uncompressed block size)
5224252db10SBaoquan He# is sufficient, to ensure that in the worst case the decompressed data for
5234252db10SBaoquan He# block will stop the byte before the compressed data for a block begins.
5244252db10SBaoquan He# To avoid problems with the compressed data's meta information an extra 18
5254252db10SBaoquan He# bytes are needed.  Leading to the formula:
5264252db10SBaoquan He#
527d607251bSYinghai Lu# extra_bytes = (uncompressed_size >> 12) + 32768 + 18
5284252db10SBaoquan He#
5294252db10SBaoquan He# Adding 8 bytes per 32K is a bit excessive but much easier to calculate.
5304252db10SBaoquan He# Adding 32768 instead of 32767 just makes for round numbers.
5314252db10SBaoquan He#
5324252db10SBaoquan He# Above analysis is for decompressing gzip compressed kernel only. Up to
5334252db10SBaoquan He# now 6 different decompressor are supported all together. And among them
5344252db10SBaoquan He# xz stores data in chunks and has maximum chunk of 64K. Hence safety
5354252db10SBaoquan He# margin should be updated to cover all decompressors so that we don't
5364252db10SBaoquan He# need to deal with each of them separately. Please check
5374252db10SBaoquan He# the description in lib/decompressor_xxx.c for specific information.
5384252db10SBaoquan He#
5394252db10SBaoquan He# extra_bytes = (uncompressed_size >> 12) + 65536 + 128
5405746f055SJan H. Schönherr#
5415746f055SJan H. Schönherr# LZ4 is even worse: data that cannot be further compressed grows by 0.4%,
5425746f055SJan H. Schönherr# or one byte per 256 bytes. OTOH, we can safely get rid of the +128 as
5435746f055SJan H. Schönherr# the size-dependent part now grows so fast.
5445746f055SJan H. Schönherr#
5455746f055SJan H. Schönherr# extra_bytes = (uncompressed_size >> 8) + 65536
5460fe4f4efSNick Terrell#
5470fe4f4efSNick Terrell# ZSTD compressed data grows by at most 3 bytes per 128K, and only has a 22
5480fe4f4efSNick Terrell# byte fixed overhead but has a maximum block size of 128K, so it needs a
5490fe4f4efSNick Terrell# larger margin.
5500fe4f4efSNick Terrell#
5510fe4f4efSNick Terrell# extra_bytes = (uncompressed_size >> 8) + 131072
5524252db10SBaoquan He
5530fe4f4efSNick Terrell#define ZO_z_extra_bytes	((ZO_z_output_len >> 8) + 131072)
554d607251bSYinghai Lu#if ZO_z_output_len > ZO_z_input_len
555d607251bSYinghai Lu# define ZO_z_extract_offset	(ZO_z_output_len + ZO_z_extra_bytes - \
556d607251bSYinghai Lu				 ZO_z_input_len)
557d607251bSYinghai Lu#else
558d607251bSYinghai Lu# define ZO_z_extract_offset	ZO_z_extra_bytes
559d607251bSYinghai Lu#endif
560d607251bSYinghai Lu
561d607251bSYinghai Lu/*
562d607251bSYinghai Lu * The extract_offset has to be bigger than ZO head section. Otherwise when
563d607251bSYinghai Lu * the head code is running to move ZO to the end of the buffer, it will
564d607251bSYinghai Lu * overwrite the head code itself.
565d607251bSYinghai Lu */
566d607251bSYinghai Lu#if (ZO__ehead - ZO_startup_32) > ZO_z_extract_offset
567d607251bSYinghai Lu# define ZO_z_min_extract_offset ((ZO__ehead - ZO_startup_32 + 4095) & ~4095)
568d607251bSYinghai Lu#else
569d607251bSYinghai Lu# define ZO_z_min_extract_offset ((ZO_z_extract_offset + 4095) & ~4095)
570d607251bSYinghai Lu#endif
571d607251bSYinghai Lu
572d607251bSYinghai Lu#define ZO_INIT_SIZE	(ZO__end - ZO_startup_32 + ZO_z_min_extract_offset)
573d607251bSYinghai Lu
57437ba7ab5SH. Peter Anvin#define VO_INIT_SIZE	(VO__end - VO__text)
57537ba7ab5SH. Peter Anvin#if ZO_INIT_SIZE > VO_INIT_SIZE
57637ba7ab5SH. Peter Anvin# define INIT_SIZE ZO_INIT_SIZE
57737ba7ab5SH. Peter Anvin#else
57837ba7ab5SH. Peter Anvin# define INIT_SIZE VO_INIT_SIZE
57937ba7ab5SH. Peter Anvin#endif
580d607251bSYinghai Lu
58137ba7ab5SH. Peter Anvininit_size:		.long INIT_SIZE		# kernel initialization size
58286134a1bSMatt Fleminghandover_offset:	.long 0			# Filled in by build.c
5832c33c27fSDaniel Kiperkernel_info_offset:	.long 0			# Filled in by build.c
58437ba7ab5SH. Peter Anvin
58596ae6ea0SThomas Gleixner# End of setup header #####################################################
58696ae6ea0SThomas Gleixner
5877a734e7dSH. Peter Anvin	.section ".entrytext", "ax"
58896ae6ea0SThomas Gleixnerstart_of_setup:
58996ae6ea0SThomas Gleixner# Force %es = %ds
59096ae6ea0SThomas Gleixner	movw	%ds, %ax
59196ae6ea0SThomas Gleixner	movw	%ax, %es
59296ae6ea0SThomas Gleixner	cld
59396ae6ea0SThomas Gleixner
59416252da6SJens Rottmann# Apparently some ancient versions of LILO invoked the kernel with %ss != %ds,
59516252da6SJens Rottmann# which happened to work by accident for the old code.  Recalculate the stack
59616252da6SJens Rottmann# pointer if %ss is invalid.  Otherwise leave it alone, LOADLIN sets up the
59716252da6SJens Rottmann# stack behind its own code, so we can't blindly put it directly past the heap.
5986b6815c6SH. Peter Anvin
5996b6815c6SH. Peter Anvin	movw	%ss, %dx
6006b6815c6SH. Peter Anvin	cmpw	%ax, %dx	# %ds == %ss?
6016b6815c6SH. Peter Anvin	movw	%sp, %dx
60216252da6SJens Rottmann	je	2f		# -> assume %sp is reasonably set
6036b6815c6SH. Peter Anvin
60416252da6SJens Rottmann	# Invalid %ss, make up a new stack
60516252da6SJens Rottmann	movw	$_end, %dx
60616252da6SJens Rottmann	testb	$CAN_USE_HEAP, loadflags
60716252da6SJens Rottmann	jz	1f
60816252da6SJens Rottmann	movw	heap_end_ptr, %dx
60916252da6SJens Rottmann1:	addw	$STACK_SIZE, %dx
61016252da6SJens Rottmann	jnc	2f
61116252da6SJens Rottmann	xorw	%dx, %dx	# Prevent wraparound
61216252da6SJens Rottmann
61316252da6SJens Rottmann2:	# Now %dx should point to the end of our stack space
6146b6815c6SH. Peter Anvin	andw	$~3, %dx	# dword align (might as well...)
6156b6815c6SH. Peter Anvin	jnz	3f
6166b6815c6SH. Peter Anvin	movw	$0xfffc, %dx	# Make sure we're not zero
61716252da6SJens Rottmann3:	movw	%ax, %ss
6186b6815c6SH. Peter Anvin	movzwl	%dx, %esp	# Clear upper half of %esp
6196b6815c6SH. Peter Anvin	sti			# Now we should have a working stack
6206b6815c6SH. Peter Anvin
6216b6815c6SH. Peter Anvin# We will have entered with %cs = %ds+0x20, normalize %cs so
6226b6815c6SH. Peter Anvin# it is on par with the other segments.
6236b6815c6SH. Peter Anvin	pushw	%ds
6246b6815c6SH. Peter Anvin	pushw	$6f
6256b6815c6SH. Peter Anvin	lretw
6266b6815c6SH. Peter Anvin6:
62796ae6ea0SThomas Gleixner
62896ae6ea0SThomas Gleixner# Check signature at end of setup
62996ae6ea0SThomas Gleixner	cmpl	$0x5a5aaa55, setup_sig
63096ae6ea0SThomas Gleixner	jne	setup_bad
63196ae6ea0SThomas Gleixner
63296ae6ea0SThomas Gleixner# Zero the bss
63396ae6ea0SThomas Gleixner	movw	$__bss_start, %di
63496ae6ea0SThomas Gleixner	movw	$_end+3, %cx
63596ae6ea0SThomas Gleixner	xorl	%eax, %eax
63696ae6ea0SThomas Gleixner	subw	%di, %cx
63796ae6ea0SThomas Gleixner	shrw	$2, %cx
63896ae6ea0SThomas Gleixner	rep; stosl
63996ae6ea0SThomas Gleixner
64096ae6ea0SThomas Gleixner# Jump to C code (should not return)
64196ae6ea0SThomas Gleixner	calll	main
64296ae6ea0SThomas Gleixner
64396ae6ea0SThomas Gleixner# Setup corrupt somehow...
64496ae6ea0SThomas Gleixnersetup_bad:
64596ae6ea0SThomas Gleixner	movl	$setup_corrupt, %eax
64696ae6ea0SThomas Gleixner	calll	puts
64796ae6ea0SThomas Gleixner	# Fall through...
64896ae6ea0SThomas Gleixner
64996ae6ea0SThomas Gleixner	.globl	die
65096ae6ea0SThomas Gleixner	.type	die, @function
65196ae6ea0SThomas Gleixnerdie:
65296ae6ea0SThomas Gleixner	hlt
65396ae6ea0SThomas Gleixner	jmp	die
65496ae6ea0SThomas Gleixner
65596ae6ea0SThomas Gleixner	.size	die, .-die
65696ae6ea0SThomas Gleixner
65796ae6ea0SThomas Gleixner	.section ".initdata", "a"
65896ae6ea0SThomas Gleixnersetup_corrupt:
65996ae6ea0SThomas Gleixner	.byte	7
66096ae6ea0SThomas Gleixner	.string	"No setup signature found...\n"
661