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