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