xref: /openbmc/linux/arch/x86/boot/header.S (revision 0fe4f4ef)
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)
166291f3632SMatt Fleming	.word	0				# DllCharacteristics
167291f3632SMatt Fleming#ifdef CONFIG_X86_32
168291f3632SMatt Fleming	.long	0				# SizeOfStackReserve
169291f3632SMatt Fleming	.long	0				# SizeOfStackCommit
170291f3632SMatt Fleming	.long	0				# SizeOfHeapReserve
171291f3632SMatt Fleming	.long	0				# SizeOfHeapCommit
172291f3632SMatt Fleming#else
173291f3632SMatt Fleming	.quad	0				# SizeOfStackReserve
174291f3632SMatt Fleming	.quad	0				# SizeOfStackCommit
175291f3632SMatt Fleming	.quad	0				# SizeOfHeapReserve
176291f3632SMatt Fleming	.quad	0				# SizeOfHeapCommit
177291f3632SMatt Fleming#endif
178291f3632SMatt Fleming	.long	0				# LoaderFlags
179a3326a0dSArd Biesheuvel	.long	(section_table - .) / 8		# NumberOfRvaAndSizes
180291f3632SMatt Fleming
181291f3632SMatt Fleming	.quad	0				# ExportTable
182291f3632SMatt Fleming	.quad	0				# ImportTable
183291f3632SMatt Fleming	.quad	0				# ResourceTable
184291f3632SMatt Fleming	.quad	0				# ExceptionTable
185291f3632SMatt Fleming	.quad	0				# CertificationTable
186291f3632SMatt Fleming	.quad	0				# BaseRelocationTable
187291f3632SMatt Fleming
188291f3632SMatt Fleming	# Section table
189291f3632SMatt Flemingsection_table:
190743628e8SJordan Justen	#
191743628e8SJordan Justen	# The offset & size fields are filled in by build.c.
192743628e8SJordan Justen	#
193743628e8SJordan Justen	.ascii	".setup"
194291f3632SMatt Fleming	.byte	0
195291f3632SMatt Fleming	.byte	0
196291f3632SMatt Fleming	.long	0
197291f3632SMatt Fleming	.long	0x0				# startup_{32,64}
198291f3632SMatt Fleming	.long	0				# Size of initialized data
199291f3632SMatt Fleming						# on disk
200291f3632SMatt Fleming	.long	0x0				# startup_{32,64}
201291f3632SMatt Fleming	.long	0				# PointerToRelocations
202291f3632SMatt Fleming	.long	0				# PointerToLineNumbers
203291f3632SMatt Fleming	.word	0				# NumberOfRelocations
204291f3632SMatt Fleming	.word	0				# NumberOfLineNumbers
205a3326a0dSArd Biesheuvel	.long	IMAGE_SCN_CNT_CODE		| \
206a3326a0dSArd Biesheuvel		IMAGE_SCN_MEM_READ		| \
207a3326a0dSArd Biesheuvel		IMAGE_SCN_MEM_EXECUTE		| \
208a3326a0dSArd Biesheuvel		IMAGE_SCN_ALIGN_16BYTES		# Characteristics
209291f3632SMatt Fleming
210291f3632SMatt Fleming	#
211291f3632SMatt Fleming	# The EFI application loader requires a relocation section
212743628e8SJordan Justen	# because EFI applications must be relocatable. The .reloc
213743628e8SJordan Justen	# offset & size fields are filled in by build.c.
214291f3632SMatt Fleming	#
215291f3632SMatt Fleming	.ascii	".reloc"
216291f3632SMatt Fleming	.byte	0
217291f3632SMatt Fleming	.byte	0
2182e064b1eSJordan Justen	.long	0
2192e064b1eSJordan Justen	.long	0
2202e064b1eSJordan Justen	.long	0				# SizeOfRawData
2212e064b1eSJordan Justen	.long	0				# PointerToRawData
222291f3632SMatt Fleming	.long	0				# PointerToRelocations
223291f3632SMatt Fleming	.long	0				# PointerToLineNumbers
224291f3632SMatt Fleming	.word	0				# NumberOfRelocations
225291f3632SMatt Fleming	.word	0				# NumberOfLineNumbers
226a3326a0dSArd Biesheuvel	.long	IMAGE_SCN_CNT_INITIALIZED_DATA	| \
227a3326a0dSArd Biesheuvel		IMAGE_SCN_MEM_READ		| \
228a3326a0dSArd Biesheuvel		IMAGE_SCN_MEM_DISCARDABLE	| \
229a3326a0dSArd Biesheuvel		IMAGE_SCN_ALIGN_1BYTES		# Characteristics
230743628e8SJordan Justen
23197aa2765SArd Biesheuvel#ifdef CONFIG_EFI_MIXED
23297aa2765SArd Biesheuvel	#
23397aa2765SArd Biesheuvel	# The offset & size fields are filled in by build.c.
23497aa2765SArd Biesheuvel	#
23597aa2765SArd Biesheuvel	.asciz	".compat"
23697aa2765SArd Biesheuvel	.long	0
23797aa2765SArd Biesheuvel	.long	0x0
23897aa2765SArd Biesheuvel	.long	0				# Size of initialized data
23997aa2765SArd Biesheuvel						# on disk
24097aa2765SArd Biesheuvel	.long	0x0
24197aa2765SArd Biesheuvel	.long	0				# PointerToRelocations
24297aa2765SArd Biesheuvel	.long	0				# PointerToLineNumbers
24397aa2765SArd Biesheuvel	.word	0				# NumberOfRelocations
24497aa2765SArd Biesheuvel	.word	0				# NumberOfLineNumbers
245a3326a0dSArd Biesheuvel	.long	IMAGE_SCN_CNT_INITIALIZED_DATA	| \
246a3326a0dSArd Biesheuvel		IMAGE_SCN_MEM_READ		| \
247a3326a0dSArd Biesheuvel		IMAGE_SCN_MEM_DISCARDABLE	| \
248a3326a0dSArd Biesheuvel		IMAGE_SCN_ALIGN_1BYTES		# Characteristics
24997aa2765SArd Biesheuvel#endif
25097aa2765SArd Biesheuvel
251743628e8SJordan Justen	#
252743628e8SJordan Justen	# The offset & size fields are filled in by build.c.
253743628e8SJordan Justen	#
254743628e8SJordan Justen	.ascii	".text"
255743628e8SJordan Justen	.byte	0
256743628e8SJordan Justen	.byte	0
257743628e8SJordan Justen	.byte	0
258743628e8SJordan Justen	.long	0
259743628e8SJordan Justen	.long	0x0				# startup_{32,64}
260743628e8SJordan Justen	.long	0				# Size of initialized data
261743628e8SJordan Justen						# on disk
262743628e8SJordan Justen	.long	0x0				# startup_{32,64}
263743628e8SJordan Justen	.long	0				# PointerToRelocations
264743628e8SJordan Justen	.long	0				# PointerToLineNumbers
265743628e8SJordan Justen	.word	0				# NumberOfRelocations
266743628e8SJordan Justen	.word	0				# NumberOfLineNumbers
267a3326a0dSArd Biesheuvel	.long	IMAGE_SCN_CNT_CODE		| \
268a3326a0dSArd Biesheuvel		IMAGE_SCN_MEM_READ		| \
269a3326a0dSArd Biesheuvel		IMAGE_SCN_MEM_EXECUTE		| \
270a3326a0dSArd Biesheuvel		IMAGE_SCN_ALIGN_16BYTES		# Characteristics
271743628e8SJordan Justen
27297aa2765SArd Biesheuvel	.set	section_count, (. - section_table) / 40
273291f3632SMatt Fleming#endif /* CONFIG_EFI_STUB */
27496ae6ea0SThomas Gleixner
27596ae6ea0SThomas Gleixner	# Kernel attributes; used by setup.  This is part 1 of the
27696ae6ea0SThomas Gleixner	# header, from the old boot sector.
27796ae6ea0SThomas Gleixner
27896ae6ea0SThomas Gleixner	.section ".header", "a"
27909c205afSH. Peter Anvin	.globl	sentinel
28009c205afSH. Peter Anvinsentinel:	.byte 0xff, 0xff        /* Used to detect broken loaders */
28109c205afSH. Peter Anvin
28296ae6ea0SThomas Gleixner	.globl	hdr
28396ae6ea0SThomas Gleixnerhdr:
2845e47c478SH. Peter Anvinsetup_sects:	.byte 0			/* Filled in by build.c */
28596ae6ea0SThomas Gleixnerroot_flags:	.word ROOT_RDONLY
2865e47c478SH. Peter Anvinsyssize:	.long 0			/* Filled in by build.c */
2875e47c478SH. Peter Anvinram_size:	.word 0			/* Obsolete */
28896ae6ea0SThomas Gleixnervid_mode:	.word SVGA_MODE
2895e47c478SH. Peter Anvinroot_dev:	.word 0			/* Filled in by build.c */
29096ae6ea0SThomas Gleixnerboot_flag:	.word 0xAA55
29196ae6ea0SThomas Gleixner
29296ae6ea0SThomas Gleixner	# offset 512, entry point
29396ae6ea0SThomas Gleixner
29496ae6ea0SThomas Gleixner	.globl	_start
29596ae6ea0SThomas Gleixner_start:
29696ae6ea0SThomas Gleixner		# Explicitly enter this as bytes, or the assembler
29796ae6ea0SThomas Gleixner		# tries to generate a 3-byte jump here, which causes
29896ae6ea0SThomas Gleixner		# everything else to push off to the wrong offset.
29996ae6ea0SThomas Gleixner		.byte	0xeb		# short (2-byte) jump
30096ae6ea0SThomas Gleixner		.byte	start_of_setup-1f
30196ae6ea0SThomas Gleixner1:
30296ae6ea0SThomas Gleixner
30396ae6ea0SThomas Gleixner	# Part 2 of the header, from the old setup.S
30496ae6ea0SThomas Gleixner
30596ae6ea0SThomas Gleixner		.ascii	"HdrS"		# header signature
306b3c72fc9SDaniel Kiper		.word	0x020f		# header version number (>= 0x0105)
30796ae6ea0SThomas Gleixner					# or else old loadlin-1.5 will fail)
30896ae6ea0SThomas Gleixner		.globl realmode_swtch
30996ae6ea0SThomas Gleixnerrealmode_swtch:	.word	0, 0		# default_switch, SETUPSEG
3105e47c478SH. Peter Anvinstart_sys_seg:	.word	SYSSEG		# obsolete and meaningless, but just
3115e47c478SH. Peter Anvin					# in case something decided to "use" it
31296ae6ea0SThomas Gleixner		.word	kernel_version-512 # pointing to kernel version string
31396ae6ea0SThomas Gleixner					# above section of header is compatible
31496ae6ea0SThomas Gleixner					# with loadlin-1.5 (header v1.5). Don't
31596ae6ea0SThomas Gleixner					# change it.
31696ae6ea0SThomas Gleixner
3175e47c478SH. Peter Anvintype_of_loader:	.byte	0		# 0 means ancient bootloader, newer
3185e47c478SH. Peter Anvin					# bootloaders know to change this.
319cb1aaebeSMauro Carvalho Chehab					# See Documentation/x86/boot.rst for
32096ae6ea0SThomas Gleixner					# assigned ids
32196ae6ea0SThomas Gleixner
32296ae6ea0SThomas Gleixner# flags, unused bits must be zero (RFU) bit within loadflags
32396ae6ea0SThomas Gleixnerloadflags:
32409c205afSH. Peter Anvin		.byte	LOADED_HIGH	# The kernel is to be loaded high
32596ae6ea0SThomas Gleixner
32696ae6ea0SThomas Gleixnersetup_move_size: .word  0x8000		# size to move, when setup is not
32796ae6ea0SThomas Gleixner					# loaded at 0x90000. We will move setup
32896ae6ea0SThomas Gleixner					# to 0x90000 then just before jumping
32996ae6ea0SThomas Gleixner					# into the kernel. However, only the
33096ae6ea0SThomas Gleixner					# loader knows how much data behind
33196ae6ea0SThomas Gleixner					# us also needs to be loaded.
33296ae6ea0SThomas Gleixner
33396ae6ea0SThomas Gleixnercode32_start:				# here loaders can put a different
33496ae6ea0SThomas Gleixner					# start address for 32-bit code.
33596ae6ea0SThomas Gleixner		.long	0x100000	# 0x100000 = default for big kernel
33696ae6ea0SThomas Gleixner
33796ae6ea0SThomas Gleixnerramdisk_image:	.long	0		# address of loaded ramdisk image
33896ae6ea0SThomas Gleixner					# Here the loader puts the 32-bit
33996ae6ea0SThomas Gleixner					# address where it loaded the image.
34096ae6ea0SThomas Gleixner					# This only will be read by the kernel.
34196ae6ea0SThomas Gleixner
34296ae6ea0SThomas Gleixnerramdisk_size:	.long	0		# its size in bytes
34396ae6ea0SThomas Gleixner
34496ae6ea0SThomas Gleixnerbootsect_kludge:
34596ae6ea0SThomas Gleixner		.long	0		# obsolete
34696ae6ea0SThomas Gleixner
3476b6815c6SH. Peter Anvinheap_end_ptr:	.word	_end+STACK_SIZE-512
3486b6815c6SH. Peter Anvin					# (Header version 0x0201 or later)
34996ae6ea0SThomas Gleixner					# space from here (exclusive) down to
35096ae6ea0SThomas Gleixner					# end of setup code can be used by setup
35196ae6ea0SThomas Gleixner					# for local heap purposes.
35296ae6ea0SThomas Gleixner
3535031296cSH. Peter Anvinext_loader_ver:
3545031296cSH. Peter Anvin		.byte	0		# Extended boot loader version
3555031296cSH. Peter Anvinext_loader_type:
3565031296cSH. Peter Anvin		.byte	0		# Extended boot loader type
3575031296cSH. Peter Anvin
35896ae6ea0SThomas Gleixnercmd_line_ptr:	.long	0		# (Header version 0x0202 or later)
35996ae6ea0SThomas Gleixner					# If nonzero, a 32-bit pointer
36096ae6ea0SThomas Gleixner					# to the kernel command line.
36196ae6ea0SThomas Gleixner					# The command line should be
36296ae6ea0SThomas Gleixner					# located between the start of
36396ae6ea0SThomas Gleixner					# setup and the end of low
36496ae6ea0SThomas Gleixner					# memory (0xa0000), or it may
36596ae6ea0SThomas Gleixner					# get overwritten before it
36696ae6ea0SThomas Gleixner					# gets read.  If this field is
36796ae6ea0SThomas Gleixner					# used, there is no longer
36896ae6ea0SThomas Gleixner					# anything magical about the
36996ae6ea0SThomas Gleixner					# 0x90000 segment; the setup
37096ae6ea0SThomas Gleixner					# can be located anywhere in
37196ae6ea0SThomas Gleixner					# low memory 0x10000 or higher.
37296ae6ea0SThomas Gleixner
3733e920b53SBorislav Petkovinitrd_addr_max: .long 0x7fffffff
37496ae6ea0SThomas Gleixner					# (Header version 0x0203 or later)
37596ae6ea0SThomas Gleixner					# The highest safe address for
37696ae6ea0SThomas Gleixner					# the contents of an initrd
377cf8fa920SH. Peter Anvin					# The current kernel allows up to 4 GB,
378cf8fa920SH. Peter Anvin					# but leave it at 2 GB to avoid
379cf8fa920SH. Peter Anvin					# possible bootloader bugs.
38096ae6ea0SThomas Gleixner
38196ae6ea0SThomas Gleixnerkernel_alignment:  .long CONFIG_PHYSICAL_ALIGN	#physical addr alignment
38296ae6ea0SThomas Gleixner						#required for protected mode
38396ae6ea0SThomas Gleixner						#kernel
38496ae6ea0SThomas Gleixner#ifdef CONFIG_RELOCATABLE
38596ae6ea0SThomas Gleixnerrelocatable_kernel:    .byte 1
38696ae6ea0SThomas Gleixner#else
38796ae6ea0SThomas Gleixnerrelocatable_kernel:    .byte 0
38896ae6ea0SThomas Gleixner#endif
38937ba7ab5SH. Peter Anvinmin_alignment:		.byte MIN_KERNEL_ALIGN_LG2	# minimum alignment
39009c205afSH. Peter Anvin
39109c205afSH. Peter Anvinxloadflags:
39209c205afSH. Peter Anvin#ifdef CONFIG_X86_64
39309c205afSH. Peter Anvin# define XLF0 XLF_KERNEL_64			/* 64-bit kernel */
39409c205afSH. Peter Anvin#else
39509c205afSH. Peter Anvin# define XLF0 0
39609c205afSH. Peter Anvin#endif
397ee92d815SYinghai Lu
398745c5167SMatt Fleming#if defined(CONFIG_RELOCATABLE) && defined(CONFIG_X86_64)
399ee92d815SYinghai Lu   /* kernel/boot_param/ramdisk could be loaded above 4g */
400ee92d815SYinghai Lu# define XLF1 XLF_CAN_BE_LOADED_ABOVE_4G
401ee92d815SYinghai Lu#else
402ee92d815SYinghai Lu# define XLF1 0
403ee92d815SYinghai Lu#endif
404ee92d815SYinghai Lu
40509c205afSH. Peter Anvin#ifdef CONFIG_EFI_STUB
4067d453eeeSMatt Fleming# ifdef CONFIG_EFI_MIXED
4077d453eeeSMatt Fleming#  define XLF23 (XLF_EFI_HANDOVER_32|XLF_EFI_HANDOVER_64)
4087d453eeeSMatt Fleming# else
40909c205afSH. Peter Anvin#  ifdef CONFIG_X86_64
41009c205afSH. Peter Anvin#   define XLF23 XLF_EFI_HANDOVER_64		/* 64-bit EFI handover ok */
41109c205afSH. Peter Anvin#  else
41209c205afSH. Peter Anvin#   define XLF23 XLF_EFI_HANDOVER_32		/* 32-bit EFI handover ok */
41309c205afSH. Peter Anvin#  endif
4147d453eeeSMatt Fleming# endif
41509c205afSH. Peter Anvin#else
41609c205afSH. Peter Anvin# define XLF23 0
41709c205afSH. Peter Anvin#endif
418456a29ddSDave Young
4192965faa5SDave Young#if defined(CONFIG_X86_64) && defined(CONFIG_EFI) && defined(CONFIG_KEXEC_CORE)
420456a29ddSDave Young# define XLF4 XLF_EFI_KEXEC
421456a29ddSDave Young#else
422456a29ddSDave Young# define XLF4 0
423456a29ddSDave Young#endif
424456a29ddSDave Young
425f2d08c5dSBaoquan He#ifdef CONFIG_X86_64
426f2d08c5dSBaoquan He#ifdef CONFIG_X86_5LEVEL
427f2d08c5dSBaoquan He#define XLF56 (XLF_5LEVEL|XLF_5LEVEL_ENABLED)
428f2d08c5dSBaoquan He#else
429f2d08c5dSBaoquan He#define XLF56 XLF_5LEVEL
430f2d08c5dSBaoquan He#endif
431f2d08c5dSBaoquan He#else
432f2d08c5dSBaoquan He#define XLF56 0
433f2d08c5dSBaoquan He#endif
434f2d08c5dSBaoquan He
435f2d08c5dSBaoquan He			.word XLF0 | XLF1 | XLF23 | XLF4 | XLF56
43696ae6ea0SThomas Gleixner
43796ae6ea0SThomas Gleixnercmdline_size:   .long   COMMAND_LINE_SIZE-1     #length of the command line,
43896ae6ea0SThomas Gleixner                                                #added with boot protocol
43996ae6ea0SThomas Gleixner                                                #version 2.06
44096ae6ea0SThomas Gleixner
441a24e7851SRusty Russellhardware_subarch:	.long 0			# subarchitecture, added with 2.07
442a24e7851SRusty Russell						# default to 0 for normal x86 PC
443a24e7851SRusty Russell
444a24e7851SRusty Russellhardware_subarch_data:	.quad 0
445a24e7851SRusty Russell
44677d1a499SH. Peter Anvinpayload_offset:		.long ZO_input_data
44777d1a499SH. Peter Anvinpayload_length:		.long ZO_z_input_len
448099e1377SIan Campbell
4498b664aa6SHuang, Yingsetup_data:		.quad 0			# 64-bit physical pointer to
4508b664aa6SHuang, Ying						# single linked list of
4518b664aa6SHuang, Ying						# struct setup_data
4528b664aa6SHuang, Ying
45337ba7ab5SH. Peter Anvinpref_address:		.quad LOAD_PHYSICAL_ADDR	# preferred load addr
45437ba7ab5SH. Peter Anvin
4554252db10SBaoquan He#
4564252db10SBaoquan He# Getting to provably safe in-place decompression is hard. Worst case
4574252db10SBaoquan He# behaviours need to be analyzed. Here let's take the decompression of
4584252db10SBaoquan He# a gzip-compressed kernel as example, to illustrate it:
4594252db10SBaoquan He#
4604252db10SBaoquan He# The file layout of gzip compressed kernel is:
4614252db10SBaoquan He#
4624252db10SBaoquan He#    magic[2]
4634252db10SBaoquan He#    method[1]
4644252db10SBaoquan He#    flags[1]
4654252db10SBaoquan He#    timestamp[4]
4664252db10SBaoquan He#    extraflags[1]
4674252db10SBaoquan He#    os[1]
4684252db10SBaoquan He#    compressed data blocks[N]
4694252db10SBaoquan He#    crc[4] orig_len[4]
4704252db10SBaoquan He#
4714252db10SBaoquan He# ... resulting in +18 bytes overhead of uncompressed data.
4724252db10SBaoquan He#
4734252db10SBaoquan He# (For more information, please refer to RFC 1951 and RFC 1952.)
4744252db10SBaoquan He#
4754252db10SBaoquan He# Files divided into blocks
4764252db10SBaoquan He# 1 bit (last block flag)
4774252db10SBaoquan He# 2 bits (block type)
4784252db10SBaoquan He#
4794252db10SBaoquan He# 1 block occurs every 32K -1 bytes or when there 50% compression
4804252db10SBaoquan He# has been achieved. The smallest block type encoding is always used.
4814252db10SBaoquan He#
4824252db10SBaoquan He# stored:
4834252db10SBaoquan He#    32 bits length in bytes.
4844252db10SBaoquan He#
4854252db10SBaoquan He# fixed:
4864252db10SBaoquan He#    magic fixed tree.
4874252db10SBaoquan He#    symbols.
4884252db10SBaoquan He#
4894252db10SBaoquan He# dynamic:
4904252db10SBaoquan He#    dynamic tree encoding.
4914252db10SBaoquan He#    symbols.
4924252db10SBaoquan He#
4934252db10SBaoquan He#
4944252db10SBaoquan He# The buffer for decompression in place is the length of the uncompressed
4954252db10SBaoquan He# data, plus a small amount extra to keep the algorithm safe. The
4964252db10SBaoquan He# compressed data is placed at the end of the buffer.  The output pointer
4974252db10SBaoquan He# is placed at the start of the buffer and the input pointer is placed
4984252db10SBaoquan He# where the compressed data starts. Problems will occur when the output
4994252db10SBaoquan He# pointer overruns the input pointer.
5004252db10SBaoquan He#
5014252db10SBaoquan He# The output pointer can only overrun the input pointer if the input
5024252db10SBaoquan He# pointer is moving faster than the output pointer.  A condition only
5034252db10SBaoquan He# triggered by data whose compressed form is larger than the uncompressed
5044252db10SBaoquan He# form.
5054252db10SBaoquan He#
5064252db10SBaoquan He# The worst case at the block level is a growth of the compressed data
5074252db10SBaoquan He# of 5 bytes per 32767 bytes.
5084252db10SBaoquan He#
5094252db10SBaoquan He# The worst case internal to a compressed block is very hard to figure.
5104252db10SBaoquan He# The worst case can at least be bounded by having one bit that represents
5114252db10SBaoquan He# 32764 bytes and then all of the rest of the bytes representing the very
5124252db10SBaoquan He# very last byte.
5134252db10SBaoquan He#
5144252db10SBaoquan He# All of which is enough to compute an amount of extra data that is required
5154252db10SBaoquan He# to be safe.  To avoid problems at the block level allocating 5 extra bytes
5164252db10SBaoquan He# per 32767 bytes of data is sufficient.  To avoid problems internal to a
5174252db10SBaoquan He# block adding an extra 32767 bytes (the worst case uncompressed block size)
5184252db10SBaoquan He# is sufficient, to ensure that in the worst case the decompressed data for
5194252db10SBaoquan He# block will stop the byte before the compressed data for a block begins.
5204252db10SBaoquan He# To avoid problems with the compressed data's meta information an extra 18
5214252db10SBaoquan He# bytes are needed.  Leading to the formula:
5224252db10SBaoquan He#
523d607251bSYinghai Lu# extra_bytes = (uncompressed_size >> 12) + 32768 + 18
5244252db10SBaoquan He#
5254252db10SBaoquan He# Adding 8 bytes per 32K is a bit excessive but much easier to calculate.
5264252db10SBaoquan He# Adding 32768 instead of 32767 just makes for round numbers.
5274252db10SBaoquan He#
5284252db10SBaoquan He# Above analysis is for decompressing gzip compressed kernel only. Up to
5294252db10SBaoquan He# now 6 different decompressor are supported all together. And among them
5304252db10SBaoquan He# xz stores data in chunks and has maximum chunk of 64K. Hence safety
5314252db10SBaoquan He# margin should be updated to cover all decompressors so that we don't
5324252db10SBaoquan He# need to deal with each of them separately. Please check
5334252db10SBaoquan He# the description in lib/decompressor_xxx.c for specific information.
5344252db10SBaoquan He#
5354252db10SBaoquan He# extra_bytes = (uncompressed_size >> 12) + 65536 + 128
5365746f055SJan H. Schönherr#
5375746f055SJan H. Schönherr# LZ4 is even worse: data that cannot be further compressed grows by 0.4%,
5385746f055SJan H. Schönherr# or one byte per 256 bytes. OTOH, we can safely get rid of the +128 as
5395746f055SJan H. Schönherr# the size-dependent part now grows so fast.
5405746f055SJan H. Schönherr#
5415746f055SJan H. Schönherr# extra_bytes = (uncompressed_size >> 8) + 65536
5420fe4f4efSNick Terrell#
5430fe4f4efSNick Terrell# ZSTD compressed data grows by at most 3 bytes per 128K, and only has a 22
5440fe4f4efSNick Terrell# byte fixed overhead but has a maximum block size of 128K, so it needs a
5450fe4f4efSNick Terrell# larger margin.
5460fe4f4efSNick Terrell#
5470fe4f4efSNick Terrell# extra_bytes = (uncompressed_size >> 8) + 131072
5484252db10SBaoquan He
5490fe4f4efSNick Terrell#define ZO_z_extra_bytes	((ZO_z_output_len >> 8) + 131072)
550d607251bSYinghai Lu#if ZO_z_output_len > ZO_z_input_len
551d607251bSYinghai Lu# define ZO_z_extract_offset	(ZO_z_output_len + ZO_z_extra_bytes - \
552d607251bSYinghai Lu				 ZO_z_input_len)
553d607251bSYinghai Lu#else
554d607251bSYinghai Lu# define ZO_z_extract_offset	ZO_z_extra_bytes
555d607251bSYinghai Lu#endif
556d607251bSYinghai Lu
557d607251bSYinghai Lu/*
558d607251bSYinghai Lu * The extract_offset has to be bigger than ZO head section. Otherwise when
559d607251bSYinghai Lu * the head code is running to move ZO to the end of the buffer, it will
560d607251bSYinghai Lu * overwrite the head code itself.
561d607251bSYinghai Lu */
562d607251bSYinghai Lu#if (ZO__ehead - ZO_startup_32) > ZO_z_extract_offset
563d607251bSYinghai Lu# define ZO_z_min_extract_offset ((ZO__ehead - ZO_startup_32 + 4095) & ~4095)
564d607251bSYinghai Lu#else
565d607251bSYinghai Lu# define ZO_z_min_extract_offset ((ZO_z_extract_offset + 4095) & ~4095)
566d607251bSYinghai Lu#endif
567d607251bSYinghai Lu
568d607251bSYinghai Lu#define ZO_INIT_SIZE	(ZO__end - ZO_startup_32 + ZO_z_min_extract_offset)
569d607251bSYinghai Lu
57037ba7ab5SH. Peter Anvin#define VO_INIT_SIZE	(VO__end - VO__text)
57137ba7ab5SH. Peter Anvin#if ZO_INIT_SIZE > VO_INIT_SIZE
57237ba7ab5SH. Peter Anvin# define INIT_SIZE ZO_INIT_SIZE
57337ba7ab5SH. Peter Anvin#else
57437ba7ab5SH. Peter Anvin# define INIT_SIZE VO_INIT_SIZE
57537ba7ab5SH. Peter Anvin#endif
576d607251bSYinghai Lu
57737ba7ab5SH. Peter Anvininit_size:		.long INIT_SIZE		# kernel initialization size
57886134a1bSMatt Fleminghandover_offset:	.long 0			# Filled in by build.c
5792c33c27fSDaniel Kiperkernel_info_offset:	.long 0			# Filled in by build.c
58037ba7ab5SH. Peter Anvin
58196ae6ea0SThomas Gleixner# End of setup header #####################################################
58296ae6ea0SThomas Gleixner
5837a734e7dSH. Peter Anvin	.section ".entrytext", "ax"
58496ae6ea0SThomas Gleixnerstart_of_setup:
58596ae6ea0SThomas Gleixner# Force %es = %ds
58696ae6ea0SThomas Gleixner	movw	%ds, %ax
58796ae6ea0SThomas Gleixner	movw	%ax, %es
58896ae6ea0SThomas Gleixner	cld
58996ae6ea0SThomas Gleixner
59016252da6SJens Rottmann# Apparently some ancient versions of LILO invoked the kernel with %ss != %ds,
59116252da6SJens Rottmann# which happened to work by accident for the old code.  Recalculate the stack
59216252da6SJens Rottmann# pointer if %ss is invalid.  Otherwise leave it alone, LOADLIN sets up the
59316252da6SJens Rottmann# stack behind its own code, so we can't blindly put it directly past the heap.
5946b6815c6SH. Peter Anvin
5956b6815c6SH. Peter Anvin	movw	%ss, %dx
5966b6815c6SH. Peter Anvin	cmpw	%ax, %dx	# %ds == %ss?
5976b6815c6SH. Peter Anvin	movw	%sp, %dx
59816252da6SJens Rottmann	je	2f		# -> assume %sp is reasonably set
5996b6815c6SH. Peter Anvin
60016252da6SJens Rottmann	# Invalid %ss, make up a new stack
60116252da6SJens Rottmann	movw	$_end, %dx
60216252da6SJens Rottmann	testb	$CAN_USE_HEAP, loadflags
60316252da6SJens Rottmann	jz	1f
60416252da6SJens Rottmann	movw	heap_end_ptr, %dx
60516252da6SJens Rottmann1:	addw	$STACK_SIZE, %dx
60616252da6SJens Rottmann	jnc	2f
60716252da6SJens Rottmann	xorw	%dx, %dx	# Prevent wraparound
60816252da6SJens Rottmann
60916252da6SJens Rottmann2:	# Now %dx should point to the end of our stack space
6106b6815c6SH. Peter Anvin	andw	$~3, %dx	# dword align (might as well...)
6116b6815c6SH. Peter Anvin	jnz	3f
6126b6815c6SH. Peter Anvin	movw	$0xfffc, %dx	# Make sure we're not zero
61316252da6SJens Rottmann3:	movw	%ax, %ss
6146b6815c6SH. Peter Anvin	movzwl	%dx, %esp	# Clear upper half of %esp
6156b6815c6SH. Peter Anvin	sti			# Now we should have a working stack
6166b6815c6SH. Peter Anvin
6176b6815c6SH. Peter Anvin# We will have entered with %cs = %ds+0x20, normalize %cs so
6186b6815c6SH. Peter Anvin# it is on par with the other segments.
6196b6815c6SH. Peter Anvin	pushw	%ds
6206b6815c6SH. Peter Anvin	pushw	$6f
6216b6815c6SH. Peter Anvin	lretw
6226b6815c6SH. Peter Anvin6:
62396ae6ea0SThomas Gleixner
62496ae6ea0SThomas Gleixner# Check signature at end of setup
62596ae6ea0SThomas Gleixner	cmpl	$0x5a5aaa55, setup_sig
62696ae6ea0SThomas Gleixner	jne	setup_bad
62796ae6ea0SThomas Gleixner
62896ae6ea0SThomas Gleixner# Zero the bss
62996ae6ea0SThomas Gleixner	movw	$__bss_start, %di
63096ae6ea0SThomas Gleixner	movw	$_end+3, %cx
63196ae6ea0SThomas Gleixner	xorl	%eax, %eax
63296ae6ea0SThomas Gleixner	subw	%di, %cx
63396ae6ea0SThomas Gleixner	shrw	$2, %cx
63496ae6ea0SThomas Gleixner	rep; stosl
63596ae6ea0SThomas Gleixner
63696ae6ea0SThomas Gleixner# Jump to C code (should not return)
63796ae6ea0SThomas Gleixner	calll	main
63896ae6ea0SThomas Gleixner
63996ae6ea0SThomas Gleixner# Setup corrupt somehow...
64096ae6ea0SThomas Gleixnersetup_bad:
64196ae6ea0SThomas Gleixner	movl	$setup_corrupt, %eax
64296ae6ea0SThomas Gleixner	calll	puts
64396ae6ea0SThomas Gleixner	# Fall through...
64496ae6ea0SThomas Gleixner
64596ae6ea0SThomas Gleixner	.globl	die
64696ae6ea0SThomas Gleixner	.type	die, @function
64796ae6ea0SThomas Gleixnerdie:
64896ae6ea0SThomas Gleixner	hlt
64996ae6ea0SThomas Gleixner	jmp	die
65096ae6ea0SThomas Gleixner
65196ae6ea0SThomas Gleixner	.size	die, .-die
65296ae6ea0SThomas Gleixner
65396ae6ea0SThomas Gleixner	.section ".initdata", "a"
65496ae6ea0SThomas Gleixnersetup_corrupt:
65596ae6ea0SThomas Gleixner	.byte	7
65696ae6ea0SThomas Gleixner	.string	"No setup signature found...\n"
657