xref: /freebsd/stand/i386/boot2/boot1.S (revision 031beb4e239bfce798af17f5fe8dba8bcaf13d99)
1/*
2 * Copyright (c) 1998 Robert Nordier
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/* Memory Locations */
17		.set MEM_REL,0x700		# Relocation address
18		.set MEM_ARG,0x900		# Arguments
19		.set MEM_ORG,0x7c00		# Origin
20		.set MEM_BUF,0x8c00		# Load area
21		.set MEM_BTX,0x9000		# BTX start
22		.set MEM_JMP,0x9010		# BTX entry point
23		.set MEM_USR,0xa000		# Client start
24		.set BDA_BOOT,0x472		# Boot howto flag
25
26/* Partition Constants */
27		.set PRT_OFF,0x1be		# Partition offset
28		.set PRT_NUM,0x4		# Partitions
29		.set PRT_BSD,0xa5		# Partition type
30
31/* Flag Bits */
32		.set FL_PACKET,0x80		# Packet mode
33
34/* Misc. Constants */
35		.set SIZ_PAG,0x1000		# Page size
36		.set SIZ_SEC,0x200		# Sector size
37
38		.set NSECT,0x10
39		.globl start
40		.globl xread
41		.code16
42
43start:		jmp main			# Start recognizably
44
45/*
46 * This is the start of a standard BIOS Parameter Block (BPB). Most bootable
47 * FAT disks have this at the start of their MBR. While normal BIOS's will
48 * work fine without this section, IBM's El Torito emulation "fixes" up the
49 * BPB by writing into the memory copy of the MBR. Rather than have data
50 * written into our xread routine, we'll define a BPB to work around it.
51 * The data marked with (T) indicates a field required for a ThinkPad to
52 * recognize the disk and (W) indicates fields written from IBM BIOS code.
53 * The use of the BPB is based on what OpenBSD and NetBSD implemented in
54 * their boot code but the required fields were determined by trial and error.
55 *
56 * Note: If additional space is needed in boot1, one solution would be to
57 * move the "prompt" message data (below) to replace the OEM ID.
58 */
59		.org 0x03, 0x00
60oemid:		.space 0x08, 0x00	# OEM ID
61
62		.org 0x0b, 0x00
63bpb:		.word   512		# sector size (T)
64		.byte	0		# sectors/clustor
65		.word	0		# reserved sectors
66		.byte	0		# number of FATs
67		.word	0		# root entries
68		.word	0		# small sectors
69		.byte	0		# media type (W)
70		.word	0		# sectors/fat
71		.word	18		# sectors per track (T)
72		.word	2		# number of heads (T)
73		.long	0		# hidden sectors (W)
74		.long	0		# large sectors
75
76		.org 0x24, 0x00
77ebpb:		.byte	0		# BIOS physical drive number (W)
78
79		.org 0x25,0x90
80/*
81 * Trampoline used by boot2 to call read to read data from the disk via
82 * the BIOS.  Call with:
83 *
84 * %cx:%ax	- long    - LBA to read in
85 * %es:(%bx)	- caddr_t - buffer to read data into
86 * %dl		- byte    - drive to read from
87 * %dh		- byte    - num sectors to read
88 */
89
90xread:		push %ss			# Address
91		pop %ds				#  data
92/*
93 * Setup an EDD disk packet and pass it to read
94 */
95xread.1:					# Starting
96		pushl $0x0			#  absolute
97		push %cx			#  block
98		push %ax			#  number
99		push %es			# Address of
100		push %bx			#  transfer buffer
101		xor %ax,%ax			# Number of
102		movb %dh,%al			#  blocks to
103		push %ax			#  transfer
104		push $0x10			# Size of packet
105		mov %sp,%bp			# Packet pointer
106		callw read			# Read from disk
107		lea 0x10(%bp),%sp		# Clear stack
108		lret				# To far caller
109/*
110 * Load the rest of boot2 and BTX up, copy the parts to the right locations,
111 * and start it all up.
112 */
113
114/*
115 * Setup the segment registers to flat addressing (segment 0) and setup the
116 * stack to end just below the start of our code.
117 */
118main:		cld				# String ops inc
119		xor %cx,%cx			# Zero
120		mov %cx,%es			# Address
121		mov %cx,%ds			#  data
122		mov %cx,%ss			# Set up
123		mov $start,%sp			#  stack
124/*
125 * Relocate ourself to MEM_REL.  Since %cx == 0, the inc %ch sets
126 * %cx == 0x100.  Note that boot1 does not use this relocated copy
127 * of itself while loading boot2; however, BTX reclaims the memory
128 * used by boot1 during its initialization.  As a result, boot2 uses
129 * xread from the relocated copy.
130 */
131		mov %sp,%si			# Source
132		mov $MEM_REL,%di		# Destination
133		incb %ch			# Word count
134		rep				# Copy
135		movsw				#  code
136/*
137 * If we are on a hard drive, then load the MBR and look for the first
138 * FreeBSD slice.  We use the fake partition entry below that points to
139 * the MBR when we call nread.  The first pass looks for the first active
140 * FreeBSD slice.  The second pass looks for the first non-active FreeBSD
141 * slice if the first one fails.
142 */
143		mov $part4,%si			# Partition
144		cmpb $0x80,%dl			# Hard drive?
145		jb main.4			# No
146		movb $0x1,%dh			# Block count
147		callw nread			# Read MBR
148		mov $0x1,%cx	 		# Two passes
149main.1: 	mov $MEM_BUF+PRT_OFF,%si	# Partition table
150		movb $0x1,%dh			# Partition
151main.2: 	cmpb $PRT_BSD,0x4(%si)		# Our partition type?
152		jne main.3			# No
153		jcxz main.5			# If second pass
154		testb $0x80,(%si)		# Active?
155		jnz main.5			# Yes
156main.3: 	add $0x10,%si	 		# Next entry
157		incb %dh			# Partition
158		cmpb $0x1+PRT_NUM,%dh		# In table?
159		jb main.2			# Yes
160		dec %cx				# Do two
161		jcxz main.1			#  passes
162/*
163 * If we get here, we didn't find any FreeBSD slices at all, so print an
164 * error message and die.
165 */
166		mov $msg_part,%si		# Message
167		jmp error			# Error
168/*
169 * Floppies use partition 0 of drive 0.
170 */
171main.4: 	xor %dx,%dx			# Partition:drive
172/*
173 * Ok, we have a slice and drive in %dx now, so use that to locate and load
174 * boot2.  %si references the start of the slice we are looking for, so go
175 * ahead and load up the first 16 sectors (boot1 + boot2) from that.  When
176 * we read it in, we conveniently use 0x8c00 as our transfer buffer.  Thus,
177 * boot1 ends up at 0x8c00, and boot2 starts at 0x8c00 + 0x200 = 0x8e00.
178 * The first part of boot2 is the disklabel, which is 0x200 bytes long.
179 * The second part is BTX, which is thus loaded into 0x9000, which is where
180 * it also runs from.  The boot2.bin binary starts right after the end of
181 * BTX, so we have to figure out where the start of it is and then move the
182 * binary to 0xc000.  Normally, BTX clients start at MEM_USR, or 0xa000, but
183 * when we use btxld to create boot2, we use an entry point of 0x2000.  That
184 * entry point is relative to MEM_USR; thus boot2.bin starts at 0xc000.
185 */
186main.5: 	mov %dx,MEM_ARG			# Save args
187		movb $NSECT,%dh			# Sector count
188		callw nread			# Read disk
189		mov $MEM_BTX,%bx		# BTX
190		mov 0xa(%bx),%si		# Get BTX length and set
191		add %bx,%si			#  %si to start of boot2.bin
192		mov $MEM_USR+SIZ_PAG*2,%di	# Client page 2
193		mov $MEM_BTX+(NSECT-1)*SIZ_SEC,%cx # Byte
194		sub %si,%cx			#  count
195		rep				# Relocate
196		movsb				#  client
197
198/*
199 * Enable A20 so we can access memory above 1 meg.
200 * Use the zero-valued %cx as a timeout for embedded hardware which do not
201 * have a keyboard controller.
202 */
203seta20: 	cli				# Disable interrupts
204seta20.1:	dec %cx				# Timeout?
205		jz seta20.3			# Yes
206		inb $0x64,%al			# Get status
207		testb $0x2,%al			# Busy?
208		jnz seta20.1			# Yes
209		movb $0xd1,%al			# Command: Write
210		outb %al,$0x64			#  output port
211seta20.2:	inb $0x64,%al			# Get status
212		testb $0x2,%al			# Busy?
213		jnz seta20.2			# Yes
214		movb $0xdf,%al			# Enable
215		outb %al,$0x60			#  A20
216seta20.3:	sti				# Enable interrupts
217
218		jmp start+MEM_JMP-MEM_ORG	# Start BTX
219
220
221/*
222 * Trampoline used to call read from within boot1.
223 */
224nread:		mov $MEM_BUF,%bx		# Transfer buffer
225		mov 0x8(%si),%ax		# Get
226		mov 0xa(%si),%cx		#  LBA
227		push %cs			# Read from
228		callw xread.1	 		#  disk
229		jnc return			# If success, return
230		mov $msg_read,%si		# Otherwise, set the error
231						#  message and fall through to
232						#  the error routine
233/*
234 * Print out the error message pointed to by %ds:(%si) followed
235 * by a prompt, wait for a keypress, and then reboot the machine.
236 */
237error:		callw putstr			# Display message
238		mov $prompt,%si			# Display
239		callw putstr			#  prompt
240		xorb %ah,%ah			# BIOS: Get
241		int $0x16			#  keypress
242		movw $0x1234, BDA_BOOT		# Do a warm boot
243		ljmp $0xf000,$0xfff0		# reboot the machine
244/*
245 * Display a null-terminated string using the BIOS output.
246 */
247putstr.0:	mov $0x7,%bx	 		# Page:attribute
248		movb $0xe,%ah			# BIOS: Display
249		int $0x10			#  character
250putstr: 	lodsb				# Get char
251		testb %al,%al			# End of string?
252		jne putstr.0			# No
253
254/*
255 * Overused return code.  ereturn is used to return an error from the
256 * read function.  Since we assume putstr succeeds, we (ab)use the
257 * same code when we return from putstr.
258 */
259ereturn:	movb $0x1,%ah			# Invalid
260		stc				#  argument
261return: 	retw				# To caller
262/*
263 * Reads sectors from the disk.  If EDD is enabled, then check if it is
264 * installed and use it if it is.  If it is not installed or not enabled, then
265 * fall back to using CHS.  Since we use a LBA, if we are using CHS, we have to
266 * fetch the drive parameters from the BIOS and divide it out ourselves.
267 * Call with:
268 *
269 * %dl	- byte     - drive number
270 * stack - 10 bytes - EDD Packet
271 */
272read:		testb $FL_PACKET,%cs:MEM_REL+flags-start # LBA support enabled?
273		jz read.1			# No, use CHS
274		cmpb $0x80,%dl			# Hard drive?
275		jb read.1			# No, use CHS
276		mov $0x55aa,%bx			# Magic
277		push %dx			# Save
278		movb $0x41,%ah			# BIOS: Check
279		int $0x13			#  extensions present
280		pop %dx				# Restore
281		jc read.1			# If error, use CHS
282		cmp $0xaa55,%bx			# Magic?
283		jne read.1			# No, so use CHS
284		testb $0x1,%cl			# Packet interface?
285		jz read.1			# No, so use CHS
286		mov %bp,%si			# Disk packet
287		movb $0x42,%ah			# BIOS: Extended
288		int $0x13			#  read
289		retw				# To caller
290read.1:	 	push %dx			# Save
291		movb $0x8,%ah			# BIOS: Get drive
292		int $0x13			#  parameters
293		movb %dh,%ch			# Max head number
294		pop %dx				# Restore
295		jc return			# If error
296		andb $0x3f,%cl			# Sectors per track
297		jz ereturn			# If zero
298		cli				# Disable interrupts
299		mov 0x8(%bp),%eax		# Get LBA
300		push %dx			# Save
301		movzbl %cl,%ebx			# Divide by
302		xor %edx,%edx			#  sectors
303		div %ebx			#  per track
304		movb %ch,%bl			# Max head number
305		movb %dl,%ch			# Sector number
306		inc %bx				# Divide by
307		xorb %dl,%dl			#  number
308		div %ebx			#  of heads
309		movb %dl,%bh			# Head number
310		pop %dx				# Restore
311		cmpl $0x3ff,%eax		# Cylinder number supportable?
312		sti				# Enable interrupts
313		ja ereturn			# No, return an error
314		xchgb %al,%ah			# Set up cylinder
315		rorb $0x2,%al			#  number
316		orb %ch,%al			# Merge
317		inc %ax				#  sector
318		xchg %ax,%cx	 		#  number
319		movb %bh,%dh			# Head number
320		subb %ah,%al			# Sectors this track
321		mov 0x2(%bp),%ah		# Blocks to read
322		cmpb %ah,%al			# To read
323		jb read.2			#  this
324#ifdef	TRACK_AT_A_TIME
325		movb %ah,%al			#  track
326#else
327		movb $1,%al			#  one sector
328#endif
329read.2: 	mov $0x5,%di	 		# Try count
330read.3: 	les 0x4(%bp),%bx		# Transfer buffer
331		push %ax			# Save
332		movb $0x2,%ah			# BIOS: Read
333		int $0x13			#  from disk
334		pop %bx				# Restore
335		jnc read.4			# If success
336		dec %di				# Retry?
337		jz read.6			# No
338		xorb %ah,%ah			# BIOS: Reset
339		int $0x13			#  disk system
340		xchg %bx,%ax	 		# Block count
341		jmp read.3			# Continue
342read.4: 	movzbw %bl,%ax	 		# Sectors read
343		add %ax,0x8(%bp)		# Adjust
344		jnc read.5			#  LBA,
345		incw 0xa(%bp)	 		#  transfer
346read.5: 	shlb %bl			#  buffer
347		add %bl,0x5(%bp)		#  pointer,
348		sub %al,0x2(%bp)		#  block count
349		ja read.1			# If not done
350read.6: 	retw				# To caller
351
352/* Messages */
353
354msg_read:	.asciz "Read"
355msg_part:	.asciz "Boot"
356
357prompt: 	.asciz " error\r\n"
358
359flags:		.byte FLAGS			# Flags
360
361		.org PRT_OFF,0x90
362
363/* Partition table */
364
365		.fill 0x30,0x1,0x0
366part4:		.byte 0x80, 0x00, 0x01, 0x00
367		.byte 0xa5, 0xfe, 0xff, 0xff
368		.byte 0x00, 0x00, 0x00, 0x00
369		.byte 0x50, 0xc3, 0x00, 0x00	# 50000 sectors long, bleh
370
371		.word 0xaa55			# Magic number
372