header.S 12.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 *	header.S
 *
 *	Copyright (C) 1991, 1992 Linus Torvalds
 *
 *	Based on bootsect.S and setup.S
 *	modified by more people than can be counted
 *
 *	Rewritten as a common file by H. Peter Anvin (Apr 2007)
 *
 * BIG FAT NOTE: We're in real mode using 64k segments.  Therefore segment
 * addresses must be multiplied by 16 to obtain their respective linear
 * addresses. To avoid confusion, linear addresses are written using leading
 * hex while segment addresses are written as segment:offset.
 *
 */

#include <asm/segment.h>
19
#include <generated/utsrelease.h>
20 21
#include <asm/boot.h>
#include <asm/e820.h>
22
#include <asm/page_types.h>
23
#include <asm/setup.h>
24
#include <asm/bootparam.h>
25
#include "boot.h"
26 27
#include "voffset.h"
#include "zoffset.h"
28 29

BOOTSEG		= 0x07C0		/* original address of boot-sector */
H
H. Peter Anvin 已提交
30
SYSSEG		= 0x1000		/* historical load address >> 4 */
31 32 33 34 35 36 37 38 39 40 41 42 43 44

#ifndef SVGA_MODE
#define SVGA_MODE ASK_VGA
#endif

#ifndef ROOT_RDONLY
#define ROOT_RDONLY 1
#endif

	.code16
	.section ".bstext", "ax"

	.global bootsect_start
bootsect_start:
M
Matt Fleming 已提交
45 46 47 48 49
#ifdef CONFIG_EFI_STUB
	# "MZ", MS-DOS header
	.byte 0x4d
	.byte 0x5a
#endif
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

	# Normalize the start address
	ljmp	$BOOTSEG, $start2

start2:
	movw	%cs, %ax
	movw	%ax, %ds
	movw	%ax, %es
	movw	%ax, %ss
	xorw	%sp, %sp
	sti
	cld

	movw	$bugger_off_msg, %si

msg_loop:
	lodsb
	andb	%al, %al
	jz	bs_die
	movb	$0xe, %ah
	movw	$7, %bx
	int	$0x10
	jmp	msg_loop

bs_die:
	# Allow the user to press a key, then reboot
	xorw	%ax, %ax
	int	$0x16
	int	$0x19

	# int 0x19 should never return.  In case it does anyway,
	# invoke the BIOS reset code...
	ljmp	$0xf000,$0xfff0

M
Matt Fleming 已提交
84 85 86 87 88 89 90 91
#ifdef CONFIG_EFI_STUB
	.org	0x3c
	#
	# Offset to the PE header.
	#
	.long	pe_header
#endif /* CONFIG_EFI_STUB */

92 93
	.section ".bsdata", "a"
bugger_off_msg:
94 95
	.ascii	"Direct floppy boot is not supported. "
	.ascii	"Use a boot loader program instead.\r\n"
96
	.ascii	"\n"
97
	.ascii	"Remove disk and press any key to reboot ...\r\n"
98 99
	.byte	0

M
Matt Fleming 已提交
100 101 102 103 104 105 106 107 108 109 110
#ifdef CONFIG_EFI_STUB
pe_header:
	.ascii	"PE"
	.word 	0

coff_header:
#ifdef CONFIG_X86_32
	.word	0x14c				# i386
#else
	.word	0x8664				# x86-64
#endif
111
	.word	3				# nr_sections
M
Matt Fleming 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
	.long	0 				# TimeDateStamp
	.long	0				# PointerToSymbolTable
	.long	1				# NumberOfSymbols
	.word	section_table - optional_header	# SizeOfOptionalHeader
#ifdef CONFIG_X86_32
	.word	0x306				# Characteristics.
						# IMAGE_FILE_32BIT_MACHINE |
						# IMAGE_FILE_DEBUG_STRIPPED |
						# IMAGE_FILE_EXECUTABLE_IMAGE |
						# IMAGE_FILE_LINE_NUMS_STRIPPED
#else
	.word	0x206				# Characteristics
						# IMAGE_FILE_DEBUG_STRIPPED |
						# IMAGE_FILE_EXECUTABLE_IMAGE |
						# IMAGE_FILE_LINE_NUMS_STRIPPED
#endif

optional_header:
#ifdef CONFIG_X86_32
	.word	0x10b				# PE32 format
#else
	.word	0x20b 				# PE32+ format
#endif
	.byte	0x02				# MajorLinkerVersion
	.byte	0x14				# MinorLinkerVersion

	# Filled in by build.c
	.long	0				# SizeOfCode

	.long	0				# SizeOfInitializedData
	.long	0				# SizeOfUninitializedData

	# Filled in by build.c
	.long	0x0000				# AddressOfEntryPoint

147
	.long	0x0200				# BaseOfCode
M
Matt Fleming 已提交
148 149 150 151 152 153 154 155 156 157
#ifdef CONFIG_X86_32
	.long	0				# data
#endif

extra_header_fields:
#ifdef CONFIG_X86_32
	.long	0				# ImageBase
#else
	.quad	0				# ImageBase
#endif
158 159
	.long	0x20				# SectionAlignment
	.long	0x20				# FileAlignment
M
Matt Fleming 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
	.word	0				# MajorOperatingSystemVersion
	.word	0				# MinorOperatingSystemVersion
	.word	0				# MajorImageVersion
	.word	0				# MinorImageVersion
	.word	0				# MajorSubsystemVersion
	.word	0				# MinorSubsystemVersion
	.long	0				# Win32VersionValue

	#
	# The size of the bzImage is written in tools/build.c
	#
	.long	0				# SizeOfImage

	.long	0x200				# SizeOfHeaders
	.long	0				# CheckSum
	.word	0xa				# Subsystem (EFI application)
	.word	0				# DllCharacteristics
#ifdef CONFIG_X86_32
	.long	0				# SizeOfStackReserve
	.long	0				# SizeOfStackCommit
	.long	0				# SizeOfHeapReserve
	.long	0				# SizeOfHeapCommit
#else
	.quad	0				# SizeOfStackReserve
	.quad	0				# SizeOfStackCommit
	.quad	0				# SizeOfHeapReserve
	.quad	0				# SizeOfHeapCommit
#endif
	.long	0				# LoaderFlags
189
	.long	0x6				# NumberOfRvaAndSizes
M
Matt Fleming 已提交
190 191 192 193 194 195 196 197 198 199

	.quad	0				# ExportTable
	.quad	0				# ImportTable
	.quad	0				# ResourceTable
	.quad	0				# ExceptionTable
	.quad	0				# CertificationTable
	.quad	0				# BaseRelocationTable

	# Section table
section_table:
200 201 202 203
	#
	# The offset & size fields are filled in by build.c.
	#
	.ascii	".setup"
M
Matt Fleming 已提交
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
	.byte	0
	.byte	0
	.long	0
	.long	0x0				# startup_{32,64}
	.long	0				# Size of initialized data
						# on disk
	.long	0x0				# startup_{32,64}
	.long	0				# PointerToRelocations
	.long	0				# PointerToLineNumbers
	.word	0				# NumberOfRelocations
	.word	0				# NumberOfLineNumbers
	.long	0x60500020			# Characteristics (section flags)

	#
	# The EFI application loader requires a relocation section
219 220
	# because EFI applications must be relocatable. The .reloc
	# offset & size fields are filled in by build.c.
M
Matt Fleming 已提交
221 222 223 224
	#
	.ascii	".reloc"
	.byte	0
	.byte	0
225 226 227 228
	.long	0
	.long	0
	.long	0				# SizeOfRawData
	.long	0				# PointerToRawData
M
Matt Fleming 已提交
229 230 231 232 233
	.long	0				# PointerToRelocations
	.long	0				# PointerToLineNumbers
	.word	0				# NumberOfRelocations
	.word	0				# NumberOfLineNumbers
	.long	0x42100040			# Characteristics (section flags)
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252

	#
	# The offset & size fields are filled in by build.c.
	#
	.ascii	".text"
	.byte	0
	.byte	0
	.byte	0
	.long	0
	.long	0x0				# startup_{32,64}
	.long	0				# Size of initialized data
						# on disk
	.long	0x0				# startup_{32,64}
	.long	0				# PointerToRelocations
	.long	0				# PointerToLineNumbers
	.word	0				# NumberOfRelocations
	.word	0				# NumberOfLineNumbers
	.long	0x60500020			# Characteristics (section flags)

M
Matt Fleming 已提交
253
#endif /* CONFIG_EFI_STUB */
254 255 256 257 258

	# Kernel attributes; used by setup.  This is part 1 of the
	# header, from the old boot sector.

	.section ".header", "a"
259 260 261
	.globl	sentinel
sentinel:	.byte 0xff, 0xff        /* Used to detect broken loaders */

262 263
	.globl	hdr
hdr:
H
H. Peter Anvin 已提交
264
setup_sects:	.byte 0			/* Filled in by build.c */
265
root_flags:	.word ROOT_RDONLY
H
H. Peter Anvin 已提交
266 267
syssize:	.long 0			/* Filled in by build.c */
ram_size:	.word 0			/* Obsolete */
268
vid_mode:	.word SVGA_MODE
H
H. Peter Anvin 已提交
269
root_dev:	.word 0			/* Filled in by build.c */
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
boot_flag:	.word 0xAA55

	# offset 512, entry point

	.globl	_start
_start:
		# Explicitly enter this as bytes, or the assembler
		# tries to generate a 3-byte jump here, which causes
		# everything else to push off to the wrong offset.
		.byte	0xeb		# short (2-byte) jump
		.byte	start_of_setup-1f
1:

	# Part 2 of the header, from the old setup.S

		.ascii	"HdrS"		# header signature
286
		.word	0x020c		# header version number (>= 0x0105)
287 288 289
					# or else old loadlin-1.5 will fail)
		.globl realmode_swtch
realmode_swtch:	.word	0, 0		# default_switch, SETUPSEG
H
H. Peter Anvin 已提交
290 291
start_sys_seg:	.word	SYSSEG		# obsolete and meaningless, but just
					# in case something decided to "use" it
292 293 294 295 296
		.word	kernel_version-512 # pointing to kernel version string
					# above section of header is compatible
					# with loadlin-1.5 (header v1.5). Don't
					# change it.

H
H. Peter Anvin 已提交
297 298
type_of_loader:	.byte	0		# 0 means ancient bootloader, newer
					# bootloaders know to change this.
P
Paul Bolle 已提交
299
					# See Documentation/x86/boot.txt for
300 301 302 303
					# assigned ids

# flags, unused bits must be zero (RFU) bit within loadflags
loadflags:
304
		.byte	LOADED_HIGH	# The kernel is to be loaded high
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326

setup_move_size: .word  0x8000		# size to move, when setup is not
					# loaded at 0x90000. We will move setup
					# to 0x90000 then just before jumping
					# into the kernel. However, only the
					# loader knows how much data behind
					# us also needs to be loaded.

code32_start:				# here loaders can put a different
					# start address for 32-bit code.
		.long	0x100000	# 0x100000 = default for big kernel

ramdisk_image:	.long	0		# address of loaded ramdisk image
					# Here the loader puts the 32-bit
					# address where it loaded the image.
					# This only will be read by the kernel.

ramdisk_size:	.long	0		# its size in bytes

bootsect_kludge:
		.long	0		# obsolete

327 328
heap_end_ptr:	.word	_end+STACK_SIZE-512
					# (Header version 0x0201 or later)
329 330 331 332
					# space from here (exclusive) down to
					# end of setup code can be used by setup
					# for local heap purposes.

333 334 335 336 337
ext_loader_ver:
		.byte	0		# Extended boot loader version
ext_loader_type:
		.byte	0		# Extended boot loader type

338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
cmd_line_ptr:	.long	0		# (Header version 0x0202 or later)
					# If nonzero, a 32-bit pointer
					# to the kernel command line.
					# The command line should be
					# located between the start of
					# setup and the end of low
					# memory (0xa0000), or it may
					# get overwritten before it
					# gets read.  If this field is
					# used, there is no longer
					# anything magical about the
					# 0x90000 segment; the setup
					# can be located anywhere in
					# low memory 0x10000 or higher.

353
ramdisk_max:	.long 0x7fffffff
354 355 356
					# (Header version 0x0203 or later)
					# The highest safe address for
					# the contents of an initrd
357 358 359
					# The current kernel allows up to 4 GB,
					# but leave it at 2 GB to avoid
					# possible bootloader bugs.
360 361 362 363 364 365 366 367 368

kernel_alignment:  .long CONFIG_PHYSICAL_ALIGN	#physical addr alignment
						#required for protected mode
						#kernel
#ifdef CONFIG_RELOCATABLE
relocatable_kernel:    .byte 1
#else
relocatable_kernel:    .byte 0
#endif
369
min_alignment:		.byte MIN_KERNEL_ALIGN_LG2	# minimum alignment
370 371 372 373 374 375 376

xloadflags:
#ifdef CONFIG_X86_64
# define XLF0 XLF_KERNEL_64			/* 64-bit kernel */
#else
# define XLF0 0
#endif
377 378 379 380 381 382 383 384

#if defined(CONFIG_RELOCATABLE) && defined(CONFIG_X86_64)
   /* kernel/boot_param/ramdisk could be loaded above 4g */
# define XLF1 XLF_CAN_BE_LOADED_ABOVE_4G
#else
# define XLF1 0
#endif

385 386 387 388 389 390 391 392 393
#ifdef CONFIG_EFI_STUB
# ifdef CONFIG_X86_64
#  define XLF23 XLF_EFI_HANDOVER_64		/* 64-bit EFI handover ok */
# else
#  define XLF23 XLF_EFI_HANDOVER_32		/* 32-bit EFI handover ok */
# endif
#else
# define XLF23 0
#endif
394 395 396 397 398 399 400 401

#if defined(CONFIG_X86_64) && defined(CONFIG_EFI) && defined(CONFIG_KEXEC)
# define XLF4 XLF_EFI_KEXEC
#else
# define XLF4 0
#endif

			.word XLF0 | XLF1 | XLF23 | XLF4
402 403 404 405 406

cmdline_size:   .long   COMMAND_LINE_SIZE-1     #length of the command line,
                                                #added with boot protocol
                                                #version 2.06

R
Rusty Russell 已提交
407 408 409 410 411
hardware_subarch:	.long 0			# subarchitecture, added with 2.07
						# default to 0 for normal x86 PC

hardware_subarch_data:	.quad 0

412 413
payload_offset:		.long ZO_input_data
payload_length:		.long ZO_z_input_len
414

415 416 417 418
setup_data:		.quad 0			# 64-bit physical pointer to
						# single linked list of
						# struct setup_data

419 420
pref_address:		.quad LOAD_PHYSICAL_ADDR	# preferred load addr

421
#define ZO_INIT_SIZE	(ZO__end - ZO_startup_32 + ZO_z_extract_offset)
422 423 424 425 426 427 428
#define VO_INIT_SIZE	(VO__end - VO__text)
#if ZO_INIT_SIZE > VO_INIT_SIZE
#define INIT_SIZE ZO_INIT_SIZE
#else
#define INIT_SIZE VO_INIT_SIZE
#endif
init_size:		.long INIT_SIZE		# kernel initialization size
429 430 431
handover_offset:
#ifdef CONFIG_EFI_STUB
  			.long 0x30		# offset to the handover
M
Matt Fleming 已提交
432
						# protocol entry point
433 434 435
#else
			.long 0
#endif
436

437 438
# End of setup header #####################################################

439
	.section ".entrytext", "ax"
440 441 442 443 444 445
start_of_setup:
# Force %es = %ds
	movw	%ds, %ax
	movw	%ax, %es
	cld

446 447 448 449
# Apparently some ancient versions of LILO invoked the kernel with %ss != %ds,
# which happened to work by accident for the old code.  Recalculate the stack
# pointer if %ss is invalid.  Otherwise leave it alone, LOADLIN sets up the
# stack behind its own code, so we can't blindly put it directly past the heap.
450 451 452 453

	movw	%ss, %dx
	cmpw	%ax, %dx	# %ds == %ss?
	movw	%sp, %dx
454 455 456 457 458 459 460 461 462 463
	je	2f		# -> assume %sp is reasonably set

	# Invalid %ss, make up a new stack
	movw	$_end, %dx
	testb	$CAN_USE_HEAP, loadflags
	jz	1f
	movw	heap_end_ptr, %dx
1:	addw	$STACK_SIZE, %dx
	jnc	2f
	xorw	%dx, %dx	# Prevent wraparound
464

465
2:	# Now %dx should point to the end of our stack space
466 467 468
	andw	$~3, %dx	# dword align (might as well...)
	jnz	3f
	movw	$0xfffc, %dx	# Make sure we're not zero
469
3:	movw	%ax, %ss
470 471 472 473 474 475 476 477 478
	movzwl	%dx, %esp	# Clear upper half of %esp
	sti			# Now we should have a working stack

# We will have entered with %cs = %ds+0x20, normalize %cs so
# it is on par with the other segments.
	pushw	%ds
	pushw	$6f
	lretw
6:
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506

# Check signature at end of setup
	cmpl	$0x5a5aaa55, setup_sig
	jne	setup_bad

# Zero the bss
	movw	$__bss_start, %di
	movw	$_end+3, %cx
	xorl	%eax, %eax
	subw	%di, %cx
	shrw	$2, %cx
	rep; stosl

# Jump to C code (should not return)
	calll	main

# Setup corrupt somehow...
setup_bad:
	movl	$setup_corrupt, %eax
	calll	puts
	# Fall through...

	.globl	die
	.type	die, @function
die:
	hlt
	jmp	die

507
	.size	die, .-die
508 509 510 511

	.section ".initdata", "a"
setup_corrupt:
	.byte	7
512
	.string	"No setup signature found...\n"