xref: /illumos-gate/usr/src/boot/i386/pxeldr/pxeldr.S (revision 22028508fd28d36ff74dc02c5774a8ba1f0db045)
1/*
2 * Copyright (c) 2000 John Baldwin
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD$
27 */
28
29/*
30 * This simple program is a preloader for the normal boot3 loader.  It is simply
31 * prepended to the beginning of a fully built and btxld'd loader.  It then
32 * copies the loader to the address boot2 normally loads it, emulates the
33 * boot[12] environment (protected mode, a bootinfo struct, etc.), and then jumps
34 * to the start of btxldr to start the boot process.  This method allows a stock
35 * /boot/loader to be booted over the network via PXE w/o having to write a
36 * separate PXE-aware client just to load the loader.
37 */
38
39#include <sys/reboot.h>
40#include <bootargs.h>
41
42/*
43 * Memory locations.
44 */
45		.set MEM_PAGE_SIZE,0x1000	# memory page size, 4k
46		.set MEM_ARG,0x900		# Arguments at start
47		.set MEM_ARG_BTX,0xa100		# Where we move them to so the
48						#  BTX client can see them
49		.set MEM_ARG_SIZE,0x18		# Size of the arguments
50		.set MEM_BTX_ADDRESS,0x9000	# where BTX lives
51		.set MEM_BTX_ENTRY,0x9010	# where BTX starts to execute
52		.set MEM_BTX_OFFSET,MEM_PAGE_SIZE # offset of BTX in the loader
53		.set MEM_BTX_CLIENT,0xa000	# where BTX clients live
54		.set MEM_BIOS_KEYBOARD,0x496	# BDA byte with keyboard bit
55/*
56 * a.out header fields
57 */
58		.set AOUT_TEXT,0x04		# text segment size
59		.set AOUT_DATA,0x08		# data segment size
60		.set AOUT_BSS,0x0c		# zero'd BSS size
61		.set AOUT_SYMBOLS,0x10		# symbol table
62		.set AOUT_ENTRY,0x14		# entry point
63		.set AOUT_HEADER,MEM_PAGE_SIZE	# size of the a.out header
64/*
65 * Segment selectors.
66 */
67		.set SEL_SDATA,0x8		# Supervisor data
68		.set SEL_RDATA,0x10		# Real mode data
69		.set SEL_SCODE,0x18		# PM-32 code
70		.set SEL_SCODE16,0x20		# PM-16 code
71/*
72 * BTX constants
73 */
74		.set INT_SYS,0x30		# BTX syscall interrupt
75/*
76 * Bit in MEM_BIOS_KEYBOARD that is set if an enhanced keyboard is present
77 */
78		.set KEYBOARD_BIT,0x10
79/*
80 * We expect to be loaded by the BIOS at 0x7c00 (standard boot loader entry
81 * point)
82 */
83		.code16
84		.globl start
85		.org 0x0, 0x0
86/*
87 * BTX program loader for PXE network booting
88 */
89start:		cld				# string ops inc
90		xorw %ax, %ax			# zero %ax
91		movw %ax, %ss			# setup the
92		movw $start, %sp		#  stack
93		movw %es, %cx			# save PXENV+ segment
94		movw %ax, %ds			# setup the
95		movw %ax, %es			#  data segments
96		andl $0xffff, %ecx		# clear upper words
97		andl $0xffff, %ebx		#  of %ebx and %ecx
98		shll $4, %ecx			# calculate the offset of
99		addl %ebx, %ecx			#  the PXENV+ struct and
100		pushl %ecx			#  save it on the stack
101		movw $welcome_msg, %si		# %ds:(%si) -> welcome message
102		callw putstr			# display the welcome message
103/*
104 * Setup the arguments that the loader is expecting from boot[12]
105 */
106		movw $bootinfo_msg, %si		# %ds:(%si) -> boot args message
107		callw putstr			# display the message
108		movw $MEM_ARG, %bx		# %ds:(%bx) -> boot args
109		movw %bx, %di			# %es:(%di) -> boot args
110		xorl %eax, %eax			# zero %eax
111		movw $(MEM_ARG_SIZE/4), %cx	# Size of arguments in 32-bit
112						#  dwords
113		rep				# Clear the arguments
114		stosl				#  to zero
115		orb $KARGS_FLAGS_PXE, 0x8(%bx)	# kargs->bootflags |=
116						#  KARGS_FLAGS_PXE
117		popl 0xc(%bx)			# kargs->pxeinfo = *PXENV+
118#ifdef ALWAYS_SERIAL
119/*
120 * set the RBX_SERIAL bit in the howto byte.
121 */
122		orl $RB_SERIAL, (%bx)		# enable serial console
123#endif
124#ifdef PROBE_KEYBOARD
125/*
126 * Look at the BIOS data area to see if we have an enhanced keyboard.  If not,
127 * set the RBX_DUAL and RBX_SERIAL bits in the howto byte.
128 */
129		testb $KEYBOARD_BIT, MEM_BIOS_KEYBOARD # keyboard present?
130		jnz keyb			# yes, so skip
131		orl $(RB_MULTIPLE | RB_SERIAL), (%bx) # enable serial console
132keyb:
133#endif
134/*
135 * Turn on the A20 address line
136 */
137		callw seta20			# Turn A20 on
138/*
139 * Relocate the loader and BTX using a very lazy protected mode
140 */
141		movw $relocate_msg, %si		# Display the
142		callw putstr			#  relocation message
143		movl end+AOUT_ENTRY, %edi	# %edi is the destination
144		movl $(end+AOUT_HEADER), %esi	# %esi is
145						#  the start of the text
146						#  segment
147		movl end+AOUT_TEXT, %ecx	# %ecx = length of the text
148						#  segment
149		lgdt gdtdesc			# setup our own gdt
150		cli				# turn off interrupts
151		movl %cr0, %eax			# Turn on
152		orb $0x1, %al			#  protected
153		movl %eax, %cr0			#  mode
154		ljmp $SEL_SCODE,$pm_start	# long jump to clear the
155						#  instruction pre-fetch queue
156		.code32
157pm_start:	movw $SEL_SDATA, %ax		# Initialize
158		movw %ax, %ds			#  %ds and
159		movw %ax, %es			#  %es to a flat selector
160		rep				# Relocate the
161		movsb				#  text segment
162		addl $(MEM_PAGE_SIZE - 1), %edi	# pad %edi out to a new page
163		andl $~(MEM_PAGE_SIZE - 1), %edi #  for the data segment
164		movl end+AOUT_DATA, %ecx	# size of the data segment
165		rep				# Relocate the
166		movsb				#  data segment
167		movl end+AOUT_BSS, %ecx		# size of the bss
168		xorl %eax, %eax			# zero %eax
169		addb $3, %cl			# round %ecx up to
170		shrl $2, %ecx			#  a multiple of 4
171		rep				# zero the
172		stosl				#  bss
173		movl end+AOUT_ENTRY, %esi	# %esi -> relocated loader
174		addl $MEM_BTX_OFFSET, %esi	# %esi -> BTX in the loader
175		movl $MEM_BTX_ADDRESS, %edi	# %edi -> where BTX needs to go
176		movzwl 0xa(%esi), %ecx		# %ecx -> length of BTX
177		rep				# Relocate
178		movsb				#  BTX
179		ljmp $SEL_SCODE16,$pm_16	# Jump to 16-bit PM
180		.code16
181pm_16:		movw $SEL_RDATA, %ax		# Initialize
182		movw %ax, %ds			#  %ds and
183		movw %ax, %es			#  %es to a real mode selector
184		movl %cr0, %eax			# Turn off
185		andb $~0x1, %al			#  protected
186		movl %eax, %cr0			#  mode
187		ljmp $0,$pm_end			# Long jump to clear the
188						#  instruction pre-fetch queue
189pm_end:		sti				# Turn interrupts back on now
190/*
191 * Copy the BTX client to MEM_BTX_CLIENT
192 */
193		xorw %ax, %ax			# zero %ax and set
194		movw %ax, %ds			#  %ds and %es
195		movw %ax, %es			#  to segment 0
196		movw $MEM_BTX_CLIENT, %di	# Prepare to relocate
197		movw $btx_client, %si		#  the simple btx client
198		movw $(btx_client_end-btx_client), %cx # length of btx client
199		rep				# Relocate the
200		movsb				#  simple BTX client
201/*
202 * Copy the boot[12] args to where the BTX client can see them
203 */
204		movw $MEM_ARG, %si		# where the args are at now
205		movw $MEM_ARG_BTX, %di		# where the args are moving to
206		movw $(MEM_ARG_SIZE/4), %cx	# size of the arguments in longs
207		rep				# Relocate
208		movsl				#  the words
209/*
210 * Save the entry point so the client can get to it later on
211 */
212		movl end+AOUT_ENTRY, %eax	# load the entry point
213		stosl				# add it to the end of the
214						#  arguments
215/*
216 * Now we just start up BTX and let it do the rest
217 */
218		movw $jump_message, %si		# Display the
219		callw putstr			#  jump message
220		ljmp $0,$MEM_BTX_ENTRY		# Jump to the BTX entry point
221
222/*
223 * Display a null-terminated string
224 */
225putstr:		lodsb				# load %al from %ds:(%si)
226		testb %al,%al			# stop at null
227		jnz putc			# if the char != null, output it
228		retw				# return when null is hit
229putc:		movw $0x7,%bx			# attribute for output
230		movb $0xe,%ah			# BIOS: put_char
231		int $0x10			# call BIOS, print char in %al
232		jmp putstr			# keep looping
233
234/*
235 * Enable A20. Put an upper limit on the amount of time we wait for the
236 * keyboard controller to get ready (65K x ISA access time). If
237 * we wait more than that amount, the hardware is probably
238 * legacy-free and simply doesn't have a keyboard controller.
239 * Thus, the A20 line is already enabled.
240 */
241seta20: 	cli				# Disable interrupts
242		xor %cx,%cx			# Clear
243seta20.1:	inc %cx				# Increment, overflow?
244		jz seta20.3			# Yes
245		inb $0x64,%al			# Get status
246		testb $0x2,%al			# Busy?
247		jnz seta20.1			# Yes
248		movb $0xd1,%al			# Command: Write
249		outb %al,$0x64			#  output port
250seta20.2:	inb $0x64,%al			# Get status
251		testb $0x2,%al			# Busy?
252		jnz seta20.2			# Yes
253		movb $0xdf,%al			# Enable
254		outb %al,$0x60			#  A20
255seta20.3:	sti				# Enable interrupts
256		retw				# To caller
257
258/*
259 * BTX client to start btxldr
260 */
261		.code32
262btx_client:	movl $(MEM_ARG_BTX-MEM_BTX_CLIENT+MEM_ARG_SIZE-4), %esi
263						# %ds:(%esi) -> end
264						#  of boot[12] args
265		movl $(MEM_ARG_SIZE/4), %ecx	# Number of words to push
266		std				# Go backwards
267push_arg:	lodsl				# Read argument
268		pushl %eax			# Push it onto the stack
269		loop push_arg			# Push all of the arguments
270		cld				# In case anyone depends on this
271		pushl MEM_ARG_BTX-MEM_BTX_CLIENT+MEM_ARG_SIZE # Entry point of
272						#  the loader
273		pushl %eax			# Emulate a near call
274		movl $0x1, %eax			# 'exec' system call
275		int $INT_SYS			# BTX system call
276btx_client_end:
277		.code16
278
279		.p2align 4
280/*
281 * Global descriptor table.
282 */
283gdt:		.word 0x0,0x0,0x0,0x0		# Null entry
284		.word 0xffff,0x0,0x9200,0xcf	# SEL_SDATA
285		.word 0xffff,0x0,0x9200,0x0	# SEL_RDATA
286		.word 0xffff,0x0,0x9a00,0xcf	# SEL_SCODE (32-bit)
287		.word 0xffff,0x0,0x9a00,0x8f	# SEL_SCODE16 (16-bit)
288gdt.1:
289/*
290 * Pseudo-descriptors.
291 */
292gdtdesc:	.word gdt.1-gdt-1		# Limit
293		.long gdt			# Base
294
295welcome_msg:	.asciz	"PXE Loader 1.00\r\n\n"
296bootinfo_msg:	.asciz	"Building the boot loader arguments\r\n"
297relocate_msg:	.asciz	"Relocating the loader and the BTX\r\n"
298jump_message:	.asciz	"Starting the BTX loader\r\n"
299
300		.p2align 4
301end:
302