cdboot.S revision 58713
1#
2# Copyright (c) 2000 John Baldwin
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms are freely
6# permitted provided that the above copyright notice and this
7# paragraph and the following disclaimer are duplicated in all
8# such forms.
9#
10# This software is provided "AS IS" and without any express or
11# implied warranties, including, without limitation, the implied
12# warranties of merchantability and fitness for a particular
13# purpose.
14#
15
16# $FreeBSD: head/sys/boot/i386/cdboot/cdboot.s 58713 2000-03-28 01:19:53Z jhb $
17
18#
19# This simple program is a preloader for the normal boot3 loader.  It is simply
20# prepended to the beginning of a fully built and btxld'd loader.  It then
21# copies the loader to the address boot2 normally loads it, emulates the
22# boot[12] environment (protected mode, a bootinfo struct, etc.), and then jumps
23# to the start of btxldr to start the boot process.  This method allows a stock
24# /boot/loader to be used w/o having to fully rewrite boot[12] to handle the
25# cd9660 file system.
26#
27
28#
29# Memory locations.
30#
31		.set MEM_PAGE_SIZE,0x1000	# memory page size, 4k
32		.set MEM_ARG,0x900		# Arguments at start
33		.set MEM_ARG_BTX,0xa100		# Where we move them to so the
34						#  BTX client can see them
35		.set MEM_ARG_SIZE,0x18		# Size of the arguments
36		.set MEM_BTX_ADDRESS,0x9000	# where BTX lives
37		.set MEM_BTX_ENTRY,0x9010	# where BTX starts to execute
38		.set MEM_BTX_OFFSET,MEM_PAGE_SIZE # offset of BTX in the loader
39		.set MEM_BTX_CLIENT,0xa000	# where BTX clients live
40#
41# a.out header fields
42#
43		.set AOUT_TEXT,0x04		# text segment size
44		.set AOUT_DATA,0x08		# data segment size
45		.set AOUT_BSS,0x0c		# zero'd BSS size
46		.set AOUT_SYMBOLS,0x10		# symbol table
47		.set AOUT_ENTRY,0x14		# entry point
48		.set AOUT_HEADER,MEM_PAGE_SIZE	# size of the a.out header
49#
50# Flags for kargs->bootflags
51#
52		.set KARGS_FLAGS_CD,0x1		# flag to indicate booting from
53						#  CD loader
54#
55# Segment selectors.
56#
57		.set SEL_SDATA,0x8		# Supervisor data
58		.set SEL_RDATA,0x10		# Real mode data
59		.set SEL_SCODE,0x18		# PM-32 code
60		.set SEL_SCODE16,0x20		# PM-16 code
61#
62# BTX constants
63#
64		.set INT_SYS,0x30		# BTX syscall interrupt
65#
66# We expect to be loaded by the BIOS at 0x7c00 (standard boot loader entry
67# point)
68#
69		.code16
70		.globl start
71		.org 0x0, 0x0
72#
73# BTX program loader for CD booting
74#
75start:		cld				# string ops inc
76		xorw %ax, %ax			# zero %ax
77		movw %ax, %ss			# setup the
78		movw $start, %sp		#  stack
79		pushw %dx			# save the BIOS boot device in
80						#  %dl for later
81		movw %ax, %ds			# setup the
82		movw %ax, %es			#  data segments
83		movw $welcome_msg, %si		# %ds:(%si) -> welcome message
84		call putstr			# display the welcome message
85#
86# Setup the arguments that the loader is expecting from boot[12]
87#
88		movw $bootinfo_msg, %si		# %ds:(%si) -> boot args message
89		call putstr			# display the message
90		movl $MEM_ARG, %ebx		# %ds:(%ebx) -> boot args
91		movw %bx, %di			# %es:(%di) -> boot args
92		xorl %eax, %eax			# zero %eax
93		movw $(MEM_ARG_SIZE/4), %cx	# Size of arguments in 32-bit
94						#  dwords
95		rep				# Clear the arguments
96		stosl				#  to zero
97		popw %dx			# restore BIOS boot device
98		movb %dl, 0x4(%ebx)		# set kargs->bootdev
99		orb $KARGS_FLAGS_CD, 0x8(%ebx)	# kargs->bootflags |=
100						#  KARGS_FLAGS_CD
101#
102# Turn on the A20 address line
103#
104		call seta20			# Turn A20 on
105#
106# Relocate the loader and BTX using a very lazy protected mode
107#
108		movw $relocate_msg, %si		# Display the
109		call putstr			#  relocation message
110		movl end+AOUT_ENTRY, %edi	# %edi is the destination
111		movl $(end+AOUT_HEADER), %esi	# %esi is
112						#  the start of the text
113						#  segment
114		movl end+AOUT_TEXT, %ecx	# %ecx = length of the text
115						#  segment
116		lgdt gdtdesc			# setup our own gdt
117		cli				# turn off interrupts
118		movl %cr0, %eax			# Turn on
119		orb $0x1, %al			#  protected
120		movl %eax, %cr0			#  mode
121		.byte 0xea			# long jump to
122		.word pm_start			#   clear the instruction
123		.word SEL_SCODE			#   pre-fetch queue
124		.code32
125pm_start:	movw $SEL_SDATA, %ax		# Initialize
126		movw %ax, %ds			#  %ds and
127		movw %ax, %es			#  %es to a flat selector
128		rep				# Relocate the
129		movsb				#  text segment
130		addl $(MEM_PAGE_SIZE - 1), %edi	# pad %edi out to a new page
131		andl $~(MEM_PAGE_SIZE - 1), %edi #  for the data segment
132		movl end+AOUT_DATA, %ecx	# size of the data segment
133		rep				# Relocate the
134		movsb				#  data segment
135		movl end+AOUT_BSS, %ecx		# size of the bss
136		xorl %eax, %eax			# zero %eax
137		addb $3, %cl			# round %ecx up to
138		shrl $2, %ecx			#  a multiple of 4
139		rep				# zero the
140		stosl				#  bss
141		movl end+AOUT_ENTRY, %esi	# %esi -> relocated loader
142		addl $MEM_BTX_OFFSET, %esi	# %esi -> BTX in the loader
143		movl $MEM_BTX_ADDRESS, %edi	# %edi -> where BTX needs to go
144		movzwl 0xa(%esi), %ecx		# %ecx -> length of BTX
145		rep				# Relocate
146		movsb				#  BTX
147		ljmp $SEL_SCODE16,$pm_16	# Jump to 16-bit PM
148		.code16
149pm_16:		movw $SEL_RDATA, %ax		# Initialize
150		movw %ax, %ds			#  %ds and
151		movw %ax, %es			#  %es to a real mode selector
152		movl %cr0, %eax			# Turn off
153		andb $~0x1, %al			#  protected
154		movl %eax, %cr0			#  mode
155		.byte 0xea			# Long jump to
156		.word pm_end			#   clear the instruction
157		.word 0x0			#   pre-fetch
158pm_end:		sti				# Turn interrupts back on now
159#
160# Copy the BTX client to MEM_BTX_CLIENT
161#
162		xorw %ax, %ax			# zero %ax and set
163		movw %ax, %ds			#  %ds and %es
164		movw %ax, %es			#  to segment 0
165		movw $MEM_BTX_CLIENT, %di	# Prepare to relocate
166		movw $btx_client, %si		#  the simple btx client
167		movw $(btx_client_end-btx_client), %cx # length of btx client
168		rep				# Relocate the
169		movsb				#  simple BTX client
170#
171# Copy the boot[12] args to where the BTX client can see them
172#
173		movw $MEM_ARG, %si		# where the args are at now
174		movw $MEM_ARG_BTX, %di		# where the args are moving to
175		movw $(MEM_ARG_SIZE/4), %cx	# size of the arguments in longs
176		rep				# Relocate
177		movsl				#  the words
178#
179# Save the entry point so the client can get to it later on
180#
181		movl end+AOUT_ENTRY, %eax	# load the entry point
182		stosl				# add it to the end of the
183						#  arguments
184#
185# Now we just start up BTX and let it do the rest
186#
187		movw $jump_message, %si		# Display the
188		call putstr			#  jump message
189		.byte 0xea			# Jump to
190		.word MEM_BTX_ENTRY 		# BTX entry
191		.word 0x0			#  point
192
193#
194# Display a null-terminated string
195#
196putstr:		lodsb				# load %al from %ds:(%si)
197		testb %al,%al			# stop at null
198		jnz putc			# if the char != null, output it
199		ret				# return when null is hit
200putc:		movw $0x7,%bx			# attribute for output
201		movb $0xe,%ah			# BIOS: put_char
202		int $0x10			# call BIOS, print char in %al
203		jmp putstr			# keep looping
204
205#
206# Enable A20
207#
208seta20: 	cli				# Disable interrupts
209seta20.1:	inb $0x64,%al			# Get status
210		testb $0x2,%al			# Busy?
211		jnz seta20.1			# Yes
212		movb $0xd1,%al			# Command: Write
213		outb %al,$0x64			#  output port
214seta20.2:	inb $0x64,%al			# Get status
215		testb $0x2,%al			# Busy?
216		jnz seta20.2			# Yes
217		movb $0xdf,%al			# Enable
218		outb %al,$0x60			#  A20
219		sti				# Enable interrupts
220		ret				# To caller
221
222#
223# BTX client to start btxldr
224#
225		.code32
226btx_client:	movl $(MEM_ARG_BTX-MEM_BTX_CLIENT+MEM_ARG_SIZE-4), %esi
227						# %ds:(%esi) -> end
228						#  of boot[12] args
229		movl $(MEM_ARG_SIZE/4), %ecx	# Number of words to push
230		std				# Go backwards
231push_arg:	lodsl				# Read argument
232		pushl %eax			# Push it onto the stack
233		loop push_arg			# Push all of the arguments
234		cld				# In case anyone depends on this
235		pushl MEM_ARG_BTX-MEM_BTX_CLIENT+MEM_ARG_SIZE # Entry point of
236						#  the loader
237		pushl %eax			# Emulate a near call
238		movl $0x1, %eax			# 'exec' system call
239		int $INT_SYS			# BTX system call
240btx_client_end:
241		.code16
242
243		.p2align 4
244#
245# Global descriptor table.
246#
247gdt:		.word 0x0,0x0,0x0,0x0		# Null entry
248		.word 0xffff,0x0,0x9200,0xcf	# SEL_SDATA
249		.word 0xffff,0x0,0x9200,0x0	# SEL_RDATA
250		.word 0xffff,0x0,0x9a00,0xcf	# SEL_SCODE (32-bit)
251		.word 0xffff,0x0,0x9a00,0x8f	# SEL_SCODE16 (16-bit)
252gdt.1:
253#
254# Pseudo-descriptors.
255#
256gdtdesc:	.word gdt.1-gdt-1		# Limit
257		.long gdt			# Base
258
259welcome_msg:	.asciz	"CD Loader 1.00\r\n\n"
260bootinfo_msg:	.asciz	"Building the boot loader arguments\r\n"
261relocate_msg:	.asciz	"Relocating the loader and the BTX\r\n"
262jump_message:	.asciz	"Starting the BTX loader\r\n"
263
264end:
265