1128708Sru/*
2128708Sru * Copyright (c) 2000 John Baldwin
3128708Sru * All rights reserved.
4128708Sru *
5262192Sjhb * Redistribution and use in source and binary forms, with or without
6262192Sjhb * modification, are permitted provided that the following conditions
7262192Sjhb * are met:
8262192Sjhb * 1. Redistributions of source code must retain the above copyright
9262192Sjhb *    notice, this list of conditions and the following disclaimer.
10262192Sjhb * 2. Redistributions in binary form must reproduce the above copyright
11262192Sjhb *    notice, this list of conditions and the following disclaimer in the
12262192Sjhb *    documentation and/or other materials provided with the distribution.
13128708Sru *
14262192Sjhb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15262192Sjhb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16262192Sjhb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17262192Sjhb * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18262192Sjhb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19262192Sjhb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20262192Sjhb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21262192Sjhb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22262192Sjhb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23262192Sjhb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24262192Sjhb * SUCH DAMAGE.
25128708Sru *
26128708Sru * $FreeBSD$
27128708Sru */
2858713Sjhb
29128708Sru/*
30128708Sru * This simple program is a preloader for the normal boot3 loader.  It is simply
31128708Sru * prepended to the beginning of a fully built and btxld'd loader.  It then
32128708Sru * copies the loader to the address boot2 normally loads it, emulates the
33129240Sru * boot[12] environment (protected mode, a bootinfo struct, etc.), and then jumps
34128708Sru * to the start of btxldr to start the boot process.  This method allows a stock
35128708Sru * /boot/loader to be booted over the network via PXE w/o having to write a
36128708Sru * separate PXE-aware client just to load the loader.
37128708Sru */
3858713Sjhb
39162046Sru#include <sys/reboot.h>
40237761Savg#include <bootargs.h>
41162046Sru
42128708Sru/*
43128708Sru * Memory locations.
44128708Sru */
45129240Sru		.set MEM_PAGE_SIZE,0x1000	# memory page size, 4k
46129240Sru		.set MEM_ARG,0x900		# Arguments at start
47129240Sru		.set MEM_ARG_BTX,0xa100		# Where we move them to so the
48129240Sru						#  BTX client can see them
49129240Sru		.set MEM_ARG_SIZE,0x18		# Size of the arguments
50129240Sru		.set MEM_BTX_ADDRESS,0x9000	# where BTX lives
51129240Sru		.set MEM_BTX_ENTRY,0x9010	# where BTX starts to execute
52129240Sru		.set MEM_BTX_OFFSET,MEM_PAGE_SIZE # offset of BTX in the loader
53129240Sru		.set MEM_BTX_CLIENT,0xa000	# where BTX clients live
54129240Sru		.set MEM_BIOS_KEYBOARD,0x496	# BDA byte with keyboard bit
55128708Sru/*
56128708Sru * a.out header fields
57128708Sru */
58129240Sru		.set AOUT_TEXT,0x04		# text segment size
59129240Sru		.set AOUT_DATA,0x08		# data segment size
60129240Sru		.set AOUT_BSS,0x0c		# zero'd BSS size
61129240Sru		.set AOUT_SYMBOLS,0x10		# symbol table
62129240Sru		.set AOUT_ENTRY,0x14		# entry point
63129240Sru		.set AOUT_HEADER,MEM_PAGE_SIZE	# size of the a.out header
64128708Sru/*
65128708Sru * Segment selectors.
66128708Sru */
67129240Sru		.set SEL_SDATA,0x8		# Supervisor data
68129240Sru		.set SEL_RDATA,0x10		# Real mode data
69129240Sru		.set SEL_SCODE,0x18		# PM-32 code
70129240Sru		.set SEL_SCODE16,0x20		# PM-16 code
71128708Sru/*
72128708Sru * BTX constants
73128708Sru */
74129240Sru		.set INT_SYS,0x30		# BTX syscall interrupt
75128708Sru/*
76128708Sru * Bit in MEM_BIOS_KEYBOARD that is set if an enhanced keyboard is present
77128708Sru */
7869391Sps		.set KEYBOARD_BIT,0x10
79128708Sru/*
80128708Sru * We expect to be loaded by the BIOS at 0x7c00 (standard boot loader entry
81128708Sru * point)
82128708Sru */
8358713Sjhb		.code16
8458713Sjhb		.globl start
8558713Sjhb		.org 0x0, 0x0
86128708Sru/*
87128708Sru * BTX program loader for PXE network booting
88128708Sru */
89129240Srustart:		cld				# string ops inc
90129240Sru		xorw %ax, %ax			# zero %ax
91129240Sru		movw %ax, %ss			# setup the
92129240Sru		movw $start, %sp		#  stack
93129240Sru		movw %es, %cx			# save PXENV+ segment
94129240Sru		movw %ax, %ds			# setup the
95129240Sru		movw %ax, %es			#  data segments
96129240Sru		andl $0xffff, %ecx		# clear upper words
97129240Sru		andl $0xffff, %ebx		#  of %ebx and %ecx
98129240Sru		shll $4, %ecx			# calculate the offset of
99129240Sru		addl %ebx, %ecx			#  the PXENV+ struct and
100129240Sru		pushl %ecx			#  save it on the stack
101129240Sru		movw $welcome_msg, %si		# %ds:(%si) -> welcome message
102129240Sru		callw putstr			# display the welcome message
103128708Sru/*
104128708Sru * Setup the arguments that the loader is expecting from boot[12]
105128708Sru */
106129240Sru		movw $bootinfo_msg, %si		# %ds:(%si) -> boot args message
107129240Sru		callw putstr			# display the message
108129240Sru		movw $MEM_ARG, %bx		# %ds:(%bx) -> boot args
109129240Sru		movw %bx, %di			# %es:(%di) -> boot args
110129240Sru		xorl %eax, %eax			# zero %eax
111129240Sru		movw $(MEM_ARG_SIZE/4), %cx	# Size of arguments in 32-bit
112129240Sru						#  dwords
113129240Sru		rep				# Clear the arguments
114129240Sru		stosl				#  to zero
115129240Sru		orb $KARGS_FLAGS_PXE, 0x8(%bx)	# kargs->bootflags |=
116129240Sru						#  KARGS_FLAGS_PXE
117129240Sru		popl 0xc(%bx)			# kargs->pxeinfo = *PXENV+
118125693Sru#ifdef ALWAYS_SERIAL
119128708Sru/*
120128708Sru * set the RBX_SERIAL bit in the howto byte.
121128708Sru */
122129240Sru		orl $RB_SERIAL, (%bx)		# enable serial console
123125693Sru#endif
124125693Sru#ifdef PROBE_KEYBOARD
125128708Sru/*
126128708Sru * Look at the BIOS data area to see if we have an enhanced keyboard.  If not,
127178037Sjhb * set the RBX_DUAL and RBX_SERIAL bits in the howto byte.
128128708Sru */
129129240Sru		testb $KEYBOARD_BIT, MEM_BIOS_KEYBOARD # keyboard present?
130129240Sru		jnz keyb			# yes, so skip
131178037Sjhb		orl $(RB_MULTIPLE | RB_SERIAL), (%bx) # enable serial console
13269391Spskeyb:
133125693Sru#endif
134128708Sru/*
135128708Sru * Turn on the A20 address line
136128708Sru */
137129240Sru		callw seta20			# Turn A20 on
138128708Sru/*
139128708Sru * Relocate the loader and BTX using a very lazy protected mode
140128708Sru */
141129240Sru		movw $relocate_msg, %si		# Display the
142129240Sru		callw putstr			#  relocation message
143129240Sru		movl end+AOUT_ENTRY, %edi	# %edi is the destination
144129240Sru		movl $(end+AOUT_HEADER), %esi	# %esi is
145129240Sru						#  the start of the text
146129240Sru						#  segment
147129240Sru		movl end+AOUT_TEXT, %ecx	# %ecx = length of the text
148129240Sru						#  segment
149129240Sru		lgdt gdtdesc			# setup our own gdt
150129240Sru		cli				# turn off interrupts
151129240Sru		movl %cr0, %eax			# Turn on
152129240Sru		orb $0x1, %al			#  protected
153129240Sru		movl %eax, %cr0			#  mode
154129240Sru		ljmp $SEL_SCODE,$pm_start	# long jump to clear the
155129240Sru						#  instruction pre-fetch queue
15658713Sjhb		.code32
157129240Srupm_start:	movw $SEL_SDATA, %ax		# Initialize
158129240Sru		movw %ax, %ds			#  %ds and
159129240Sru		movw %ax, %es			#  %es to a flat selector
160129240Sru		rep				# Relocate the
161129240Sru		movsb				#  text segment
162129240Sru		addl $(MEM_PAGE_SIZE - 1), %edi	# pad %edi out to a new page
163129240Sru		andl $~(MEM_PAGE_SIZE - 1), %edi #  for the data segment
164129240Sru		movl end+AOUT_DATA, %ecx	# size of the data segment
165129240Sru		rep				# Relocate the
166129240Sru		movsb				#  data segment
167129240Sru		movl end+AOUT_BSS, %ecx		# size of the bss
168129240Sru		xorl %eax, %eax			# zero %eax
169129240Sru		addb $3, %cl			# round %ecx up to
170129240Sru		shrl $2, %ecx			#  a multiple of 4
171129240Sru		rep				# zero the
172129240Sru		stosl				#  bss
173129240Sru		movl end+AOUT_ENTRY, %esi	# %esi -> relocated loader
174129240Sru		addl $MEM_BTX_OFFSET, %esi	# %esi -> BTX in the loader
175129240Sru		movl $MEM_BTX_ADDRESS, %edi	# %edi -> where BTX needs to go
176129240Sru		movzwl 0xa(%esi), %ecx		# %ecx -> length of BTX
177129240Sru		rep				# Relocate
178129240Sru		movsb				#  BTX
179129240Sru		ljmp $SEL_SCODE16,$pm_16	# Jump to 16-bit PM
18058713Sjhb		.code16
181129240Srupm_16:		movw $SEL_RDATA, %ax		# Initialize
182129240Sru		movw %ax, %ds			#  %ds and
183129240Sru		movw %ax, %es			#  %es to a real mode selector
184129240Sru		movl %cr0, %eax			# Turn off
185129240Sru		andb $~0x1, %al			#  protected
186129240Sru		movl %eax, %cr0			#  mode
187129240Sru		ljmp $0,$pm_end			# Long jump to clear the
188129240Sru						#  instruction pre-fetch queue
189129240Srupm_end:		sti				# Turn interrupts back on now
190128708Sru/*
191128708Sru * Copy the BTX client to MEM_BTX_CLIENT
192128708Sru */
193129240Sru		xorw %ax, %ax			# zero %ax and set
194129240Sru		movw %ax, %ds			#  %ds and %es
195129240Sru		movw %ax, %es			#  to segment 0
196129240Sru		movw $MEM_BTX_CLIENT, %di	# Prepare to relocate
197129240Sru		movw $btx_client, %si		#  the simple btx client
198129240Sru		movw $(btx_client_end-btx_client), %cx # length of btx client
199129240Sru		rep				# Relocate the
200129240Sru		movsb				#  simple BTX client
201128708Sru/*
202128708Sru * Copy the boot[12] args to where the BTX client can see them
203128708Sru */
204129240Sru		movw $MEM_ARG, %si		# where the args are at now
205129240Sru		movw $MEM_ARG_BTX, %di		# where the args are moving to
206129240Sru		movw $(MEM_ARG_SIZE/4), %cx	# size of the arguments in longs
207129240Sru		rep				# Relocate
208129240Sru		movsl				#  the words
209128708Sru/*
210128708Sru * Save the entry point so the client can get to it later on
211128708Sru */
212129240Sru		movl end+AOUT_ENTRY, %eax	# load the entry point
213129240Sru		stosl				# add it to the end of the
214129240Sru						#  arguments
215128708Sru/*
216128708Sru * Now we just start up BTX and let it do the rest
217128708Sru */
218129240Sru		movw $jump_message, %si		# Display the
219129240Sru		callw putstr			#  jump message
220129240Sru		ljmp $0,$MEM_BTX_ENTRY		# Jump to the BTX entry point
22158713Sjhb
222128708Sru/*
223128708Sru * Display a null-terminated string
224128708Sru */
225129240Sruputstr:		lodsb				# load %al from %ds:(%si)
226129240Sru		testb %al,%al			# stop at null
227129240Sru		jnz putc			# if the char != null, output it
228129240Sru		retw				# return when null is hit
229129240Sruputc:		movw $0x7,%bx			# attribute for output
230129240Sru		movb $0xe,%ah			# BIOS: put_char
231129240Sru		int $0x10			# call BIOS, print char in %al
232129240Sru		jmp putstr			# keep looping
23358713Sjhb
234128708Sru/*
235157669Ssobomax * Enable A20. Put an upper limit on the amount of time we wait for the
236157669Ssobomax * keyboard controller to get ready (65K x ISA access time). If
237157669Ssobomax * we wait more than that amount, the hardware is probably
238157669Ssobomax * legacy-free and simply doesn't have a keyboard controller.
239157669Ssobomax * Thus, the A20 line is already enabled.
240128708Sru */
241129240Sruseta20: 	cli				# Disable interrupts
242157669Ssobomax		xor %cx,%cx			# Clear
243157669Ssobomaxseta20.1:	inc %cx				# Increment, overflow?
244157669Ssobomax		jz seta20.3			# Yes
245157669Ssobomax		inb $0x64,%al			# Get status
246129240Sru		testb $0x2,%al			# Busy?
247129240Sru		jnz seta20.1			# Yes
248129240Sru		movb $0xd1,%al			# Command: Write
249129240Sru		outb %al,$0x64			#  output port
250129240Sruseta20.2:	inb $0x64,%al			# Get status
251129240Sru		testb $0x2,%al			# Busy?
252129240Sru		jnz seta20.2			# Yes
253129240Sru		movb $0xdf,%al			# Enable
254129240Sru		outb %al,$0x60			#  A20
255157669Ssobomaxseta20.3:	sti				# Enable interrupts
256129240Sru		retw				# To caller
25758713Sjhb
258128708Sru/*
259128708Sru * BTX client to start btxldr
260128708Sru */
26158713Sjhb		.code32
26258713Sjhbbtx_client:	movl $(MEM_ARG_BTX-MEM_BTX_CLIENT+MEM_ARG_SIZE-4), %esi
263129240Sru						# %ds:(%esi) -> end
264129240Sru						#  of boot[12] args
265129240Sru		movl $(MEM_ARG_SIZE/4), %ecx	# Number of words to push
266129240Sru		std				# Go backwards
267129240Srupush_arg:	lodsl				# Read argument
268129240Sru		pushl %eax			# Push it onto the stack
269129240Sru		loop push_arg			# Push all of the arguments
270129240Sru		cld				# In case anyone depends on this
271129240Sru		pushl MEM_ARG_BTX-MEM_BTX_CLIENT+MEM_ARG_SIZE # Entry point of
272129240Sru						#  the loader
273129240Sru		pushl %eax			# Emulate a near call
274129240Sru		movl $0x1, %eax			# 'exec' system call
275129240Sru		int $INT_SYS			# BTX system call
27658713Sjhbbtx_client_end:
27758713Sjhb		.code16
27881363Sjhb
27958713Sjhb		.p2align 4
280128708Sru/*
281128708Sru * Global descriptor table.
282128708Sru */
283129240Srugdt:		.word 0x0,0x0,0x0,0x0		# Null entry
284129240Sru		.word 0xffff,0x0,0x9200,0xcf	# SEL_SDATA
285129240Sru		.word 0xffff,0x0,0x9200,0x0	# SEL_RDATA
286129240Sru		.word 0xffff,0x0,0x9a00,0xcf	# SEL_SCODE (32-bit)
287129240Sru		.word 0xffff,0x0,0x9a00,0x8f	# SEL_SCODE16 (16-bit)
28858713Sjhbgdt.1:
289128708Sru/*
290128708Sru * Pseudo-descriptors.
291128708Sru */
292129240Srugdtdesc:	.word gdt.1-gdt-1		# Limit
293129240Sru		.long gdt			# Base
29481363Sjhb
29558713Sjhbwelcome_msg:	.asciz	"PXE Loader 1.00\r\n\n"
29658713Sjhbbootinfo_msg:	.asciz	"Building the boot loader arguments\r\n"
29758713Sjhbrelocate_msg:	.asciz	"Relocating the loader and the BTX\r\n"
29858713Sjhbjump_message:	.asciz	"Starting the BTX loader\r\n"
29981363Sjhb
30081367Smp		.p2align 4
30158713Sjhbend:
302