boot1.S revision 134430
1116742Ssam/*
2116904Ssam * Copyright (c) 1998 Robert Nordier
3186904Ssam * All rights reserved.
4116742Ssam *
5116742Ssam * Redistribution and use in source and binary forms are freely
6116742Ssam * permitted provided that the above copyright notice and this
7116742Ssam * paragraph and the following disclaimer are duplicated in all
8116742Ssam * such forms.
9116742Ssam *
10116742Ssam * This software is provided "AS IS" and without any express or
11116742Ssam * implied warranties, including, without limitation, the implied
12116742Ssam * warranties of merchantability and fitness for a particular
13116742Ssam * purpose.
14116742Ssam *
15116904Ssam * $FreeBSD: head/sys/boot/i386/boot2/boot1.S 134430 2004-08-28 08:32:23Z yar $
16116904Ssam */
17116904Ssam
18116904Ssam/* Memory Locations */
19116904Ssam		.set MEM_REL,0x700		# Relocation address
20116904Ssam		.set MEM_ARG,0x900		# Arguments
21116904Ssam		.set MEM_ORG,0x7c00		# Origin
22116904Ssam		.set MEM_BUF,0x8cec		# Load area
23116904Ssam		.set MEM_BTX,0x9000		# BTX start
24116904Ssam		.set MEM_JMP,0x9010		# BTX entry point
25116904Ssam		.set MEM_USR,0xa000		# Client start
26116742Ssam		.set BDA_BOOT,0x472		# Boot howto flag
27116742Ssam
28116742Ssam/* Partition Constants */
29116742Ssam		.set PRT_OFF,0x1be		# Partition offset
30116742Ssam		.set PRT_NUM,0x4		# Partitions
31138568Ssam		.set PRT_BSD,0xa5		# Partition type
32170530Ssam
33116742Ssam/* Flag Bits */
34138568Ssam		.set FL_PACKET,0x80		# Packet mode
35178354Ssam
36178354Ssam/* Misc. Constants */
37178354Ssam		.set SIZ_PAG,0x1000		# Page size
38178354Ssam		.set SIZ_SEC,0x200		# Sector size
39178354Ssam
40178354Ssam		.set NSECT,0x10
41178354Ssam		.globl start
42178354Ssam		.globl xread
43178354Ssam		.code16
44178354Ssam
45138568Ssamstart:		jmp main			# Start recognizably
46138568Ssam
47138568Ssam/*
48138568Ssam * This is the start of a standard BIOS Parameter Block (BPB). Most bootable
49138568Ssam * FAT disks have this at the start of their MBR. While normal BIOS's will
50138568Ssam * work fine without this section, IBM's El Torito emulation "fixes" up the
51138568Ssam * BPB by writing into the memory copy of the MBR. Rather than have data
52138568Ssam * written into our xread routine, we'll define a BPB to work around it.
53138568Ssam * The data marked with (T) indicates a field required for a ThinkPad to
54170530Ssam * recognize the disk and (W) indicates fields written from IBM BIOS code.
55138568Ssam * The use of the BPB is based on what OpenBSD and NetBSD implemented in
56172211Ssam * their boot code but the required fields were determined by trial and error.
57172211Ssam *
58172211Ssam * Note: If additional space is needed in boot1, one solution would be to
59116742Ssam * move the "prompt" message data (below) to replace the OEM ID.
60116742Ssam */
61116742Ssam		.org 0x03, 0x00
62170530Ssamoemid:		.space 0x08, 0x00	# OEM ID
63138568Ssam
64116742Ssam		.org 0x0b, 0x00
65138568Ssambpb:		.word   512		# sector size (T)
66138568Ssam		.byte	0		# sectors/clustor
67178354Ssam		.word	0		# reserved sectors
68138568Ssam		.byte	0		# number of FATs
69116742Ssam		.word	0		# root entries
70178354Ssam		.word	0		# small sectors
71178354Ssam		.byte	0		# media type (W)
72190093Srpaulo		.word	0		# sectors/fat
73178354Ssam		.word	18		# sectors per track (T)
74178354Ssam		.word	2		# number of heads (T)
75178354Ssam		.long	0		# hidden sectors (W)
76178354Ssam		.long	0		# large sectors
77178354Ssam
78178354Ssam		.org 0x24, 0x00
79178354Ssamebpb:		.byte	0		# BIOS physical drive number (W)
80178354Ssam
81178354Ssam		.org 0x25,0x90
82178354Ssam/*
83186904Ssam * Trampoline used by boot2 to call read to read data from the disk via
84193239Ssam * the BIOS.  Call with:
85178354Ssam *
86178354Ssam * %cx:%ax	- long    - LBA to read in
87178354Ssam * %es:(%bx)	- caddr_t - buffer to read data into
88178354Ssam * %dl		- byte    - drive to read from
89178354Ssam * %dh		- byte    - num sectors to read
90178354Ssam */
91116742Ssam
92116742Ssamxread:		push %ss			# Address
93116742Ssam		pop %ds				#  data
94116742Ssam/*
95116742Ssam * Setup an EDD disk packet and pass it to read
96116742Ssam */
97178354Ssamxread.1:					# Starting
98178354Ssam		pushl $0x0			#  absolute
99178354Ssam		push %cx			#  block
100178354Ssam		push %ax			#  number
101178354Ssam		push %es			# Address of
102178354Ssam		push %bx			#  transfer buffer
103120483Ssam		xor %ax,%ax			# Number of
104183252Ssam		movb %dh,%al			#  blocks to
105183252Ssam		push %ax			#  transfer
106183252Ssam		push $0x10			# Size of packet
107183252Ssam		mov %sp,%bp			# Packet pointer
108183252Ssam		callw read			# Read from disk
109183252Ssam		lea 0x10(%bp),%sp		# Clear stack
110183252Ssam		lret				# To far caller
111183252Ssam/*
112183252Ssam * Load the rest of boot2 and BTX up, copy the parts to the right locations,
113183252Ssam * and start it all up.
114183252Ssam */
115183252Ssam
116183252Ssam/*
117183255Ssam * Setup the segment registers to flat addressing (segment 0) and setup the
118183255Ssam * stack to end just below the start of our code.
119183256Ssam */
120183257Ssammain:		cld				# String ops inc
121183257Ssam		xor %cx,%cx			# Zero
122186099Ssam		mov %cx,%es			# Address
123193549Ssam		mov %cx,%ds			#  data
124193549Ssam		mov %cx,%ss			# Set up
125183252Ssam		mov $start,%sp			#  stack
126183252Ssam/*
127183252Ssam * Relocate ourself to MEM_REL.  Since %cx == 0, the inc %ch sets
128170530Ssam * %cx == 0x100.
129170530Ssam */
130170530Ssam		mov %sp,%si			# Source
131170530Ssam		mov $MEM_REL,%di		# Destination
132170530Ssam		incb %ch			# Word count
133170530Ssam		rep				# Copy
134170530Ssam		movsw				#  code
135170530Ssam/*
136183252Ssam * If we are on a hard drive, then load the MBR and look for the first
137170530Ssam * FreeBSD slice.  We use the fake partition entry below that points to
138183251Ssam * the MBR when we call nread.  The first pass looks for the first active
139173273Ssam * FreeBSD slice.  The second pass looks for the first non-active FreeBSD
140170530Ssam * slice if the first one fails.
141178354Ssam */
142172225Ssam		mov $part4,%si			# Partition
143191756Ssam		cmpb $0x80,%dl			# Hard drive?
144172225Ssam		jb main.4			# No
145191756Ssam		movb $0x1,%dh			# Block count
146170530Ssam		callw nread			# Read MBR
147138568Ssam		mov $0x1,%cx	 		# Two passes
148138568Ssammain.1: 	mov $MEM_BUF+PRT_OFF,%si	# Partition table
149116742Ssam		movb $0x1,%dh			# Partition
150116742Ssammain.2: 	cmpb $PRT_BSD,0x4(%si)		# Our partition type?
151178354Ssam		jne main.3			# No
152170530Ssam		jcxz main.5			# If second pass
153116742Ssam		testb $0x80,(%si)		# Active?
154116742Ssam		jnz main.5			# Yes
155170530Ssammain.3: 	add $0x10,%si	 		# Next entry
156170530Ssam		incb %dh			# Partition
157116742Ssam		cmpb $0x1+PRT_NUM,%dh		# In table?
158116742Ssam		jb main.2			# Yes
159138568Ssam		dec %cx				# Do two
160170530Ssam		jcxz main.1			#  passes
161178354Ssam/*
162138568Ssam * If we get here, we didn't find any FreeBSD slices at all, so print an
163170530Ssam * error message and die.
164170530Ssam */
165170530Ssam		mov $msg_part,%si		# Message
166170530Ssam		jmp error			# Error
167116742Ssam/*
168170530Ssam * Floppies use partition 0 of drive 0.
169170530Ssam */
170170530Ssammain.4: 	xor %dx,%dx			# Partition:drive
171178354Ssam/*
172170530Ssam * Ok, we have a slice and drive in %dx now, so use that to locate and load
173170530Ssam * boot2.  %si references the start of the slice we are looking for, so go
174170530Ssam * ahead and load up the first 16 sectors (boot1 + boot2) from that.  When
175116742Ssam * we read it in, we conveniently use 0x8cec as our transfer buffer.  Thus,
176170530Ssam * boot1 ends up at 0x8cec, and boot2 starts at 0x8cec + 0x200 = 0x8eec.
177170530Ssam * The first part of boot2 is the disklabel, which is 0x114 bytes long.
178170530Ssam * The second part is BTX, which is thus loaded into 0x9000, which is where
179170530Ssam * it also runs from.  The boot2.bin binary starts right after the end of
180170530Ssam * BTX, so we have to figure out where the start of it is and then move the
181170530Ssam * binary to 0xc000.  Normally, BTX clients start at MEM_USR, or 0xa000, but
182170530Ssam * when we use btxld to create boot2, we use an entry point of 0x2000.  That
183170530Ssam * entry point is relative to MEM_USR; thus boot2.bin starts at 0xc000.
184170530Ssam */
185170530Ssammain.5: 	mov %dx,MEM_ARG			# Save args
186170530Ssam		movb $NSECT,%dh			# Sector count
187170530Ssam		callw nread			# Read disk
188116742Ssam		mov $MEM_BTX,%bx		# BTX
189138568Ssam		mov 0xa(%bx),%si		# Get BTX length and set
190138568Ssam		add %bx,%si			#  %si to start of boot2.bin
191178354Ssam		mov $MEM_USR+SIZ_PAG*2,%di	# Client page 2
192184288Ssam		mov $MEM_BTX+(NSECT-1)*SIZ_SEC,%cx # Byte
193138568Ssam		sub %si,%cx			#  count
194178354Ssam		rep				# Relocate
195178354Ssam		movsb				#  client
196178354Ssam
197178354Ssam/*
198193239Ssam * Enable A20 so we can access memory above 1 meg.
199116742Ssam * Use the zero-valued %cx as a timeout for embedded hardware which do not
200138568Ssam * have a keyboard controller.
201178354Ssam */
202116742Ssamseta20: 	cli				# Disable interrupts
203170530Ssamseta20.1:	dec %cx				# Timeout?
204173273Ssam		jz seta20.3			# Yes
205173273Ssam		inb $0x64,%al			# Get status
206193549Ssam		testb $0x2,%al			# Busy?
207193549Ssam		jnz seta20.1			# Yes
208182828Ssam		movb $0xd1,%al			# Command: Write
209182828Ssam		outb %al,$0x64			#  output port
210193549Ssamseta20.2:	inb $0x64,%al			# Get status
211193549Ssam		testb $0x2,%al			# Busy?
212193549Ssam		jnz seta20.2			# Yes
213170530Ssam		movb $0xdf,%al			# Enable
214187797Ssam		outb %al,$0x60			#  A20
215187797Ssamseta20.3:
216187797Ssam		sti				# Enable interrupts
217187797Ssam
218187797Ssam		jmp start+MEM_JMP-MEM_ORG	# Start BTX
219138568Ssam
220138568Ssam
221138568Ssam/*
222138568Ssam * Trampoline used to call read from within boot1.
223138568Ssam */
224138568Ssamnread:		mov $MEM_BUF,%bx		# Transfer buffer
225178354Ssam		mov 0x8(%si),%ax		# Get
226178354Ssam		mov 0xa(%si),%cx		#  LBA
227178354Ssam		push %cs			# Read from
228178354Ssam		callw xread.1	 		#  disk
229178354Ssam		jnc return			# If success, return
230178354Ssam		mov $msg_read,%si		# Otherwise, set the error
231178354Ssam						#  message and fall through to
232178354Ssam						#  the error routine
233178354Ssam/*
234178354Ssam * Print out the error message pointed to by %ds:(%si) followed
235178354Ssam * by a prompt, wait for a keypress, and then reboot the machine.
236178354Ssam */
237178354Ssamerror:		callw putstr			# Display message
238178354Ssam		mov $prompt,%si			# Display
239178354Ssam		callw putstr			#  prompt
240178354Ssam		xorb %ah,%ah			# BIOS: Get
241178354Ssam		int $0x16			#  keypress
242178354Ssam		movw $0x1234, BDA_BOOT		# Do a warm boot
243178354Ssam		ljmp $0xffff,$0x0		# reboot the machine
244178354Ssam/*
245178354Ssam * Display a null-terminated string using the BIOS output.
246178354Ssam */
247178354Ssamputstr.0:	mov $0x7,%bx	 		# Page:attribute
248178354Ssam		movb $0xe,%ah			# BIOS: Display
249178354Ssam		int $0x10			#  character
250178354Ssamputstr: 	lodsb				# Get char
251178354Ssam		testb %al,%al			# End of string?
252178354Ssam		jne putstr.0			# No
253178354Ssam
254178354Ssam/*
255178354Ssam * Overused return code.  ereturn is used to return an error from the
256178354Ssam * read function.  Since we assume putstr succeeds, we (ab)use the
257116742Ssam * same code when we return from putstr.
258116742Ssam */
259116742Ssamereturn:	movb $0x1,%ah			# Invalid
260138568Ssam		stc				#  argument
261116742Ssamreturn: 	retw				# To caller
262116742Ssam/*
263116742Ssam * Reads sectors from the disk.  If EDD is enabled, then check if it is
264116742Ssam * installed and use it if it is.  If it is not installed or not enabled, then
265116742Ssam * fall back to using CHS.  Since we use a LBA, if we are using CHS, we have to
266116742Ssam * fetch the drive parameters from the BIOS and divide it out ourselves.
267138568Ssam * Call with:
268116742Ssam *
269116742Ssam * %dl	- byte     - drive number
270116742Ssam * stack - 10 bytes - EDD Packet
271116742Ssam */
272116742Ssamread:		testb $FL_PACKET,%cs:MEM_REL+flags-start # LBA support enabled?
273144618Ssam		jz read.1			# No, use CHS
274144618Ssam		cmpb $0x80,%dl			# Hard drive?
275144618Ssam		jb read.1			# No, use CHS
276178354Ssam		mov $0x55aa,%bx			# Magic
277178354Ssam		push %dx			# Save
278178354Ssam		movb $0x41,%ah			# BIOS: Check
279127877Ssam		int $0x13			#  extensions present
280138568Ssam		pop %dx				# Restore
281138568Ssam		jc read.1			# If error, use CHS
282138568Ssam		cmp $0xaa55,%bx			# Magic?
283138568Ssam		jne read.1			# No, so use CHS
284138568Ssam		testb $0x1,%cl			# Packet interface?
285116742Ssam		jz read.1			# No, so use CHS
286148302Ssam		mov %bp,%si			# Disk packet
287148302Ssam		movb $0x42,%ah			# BIOS: Extended
288138568Ssam		int $0x13			#  read
289178354Ssam		retw				# To caller
290178354Ssamread.1:	 	push %dx			# Save
291178354Ssam		movb $0x8,%ah			# BIOS: Get drive
292178354Ssam		int $0x13			#  parameters
293178354Ssam		movb %dh,%ch			# Max head number
294191746Sthompsa		pop %dx				# Restore
295191746Sthompsa		jc return			# If error
296178354Ssam		andb $0x3f,%cl			# Sectors per track
297148306Ssam		jz ereturn			# If zero
298170530Ssam		cli				# Disable interrupts
299184274Ssam		mov 0x8(%bp),%eax		# Get LBA
300170530Ssam		push %dx			# Save
301178354Ssam		movzbl %cl,%ebx			# Divide by
302178354Ssam		xor %edx,%edx			#  sectors
303138568Ssam		div %ebx			#  per track
304178354Ssam		movb %ch,%bl			# Max head number
305178354Ssam		movb %dl,%ch			# Sector number
306178354Ssam		inc %bx				# Divide by
307178354Ssam		xorb %dl,%dl			#  number
308178354Ssam		div %ebx			#  of heads
309178354Ssam		movb %dl,%bh			# Head number
310178354Ssam		pop %dx				# Restore
311138568Ssam		cmpl $0x3ff,%eax		# Cylinder number supportable?
312138568Ssam		sti				# Enable interrupts
313178354Ssam		ja ereturn			# No, return an error
314178354Ssam		xchgb %al,%ah			# Set up cylinder
315178354Ssam		rorb $0x2,%al			#  number
316178354Ssam		orb %ch,%al			# Merge
317138568Ssam		inc %ax				#  sector
318138568Ssam		xchg %ax,%cx	 		#  number
319138568Ssam		movb %bh,%dh			# Head number
320138568Ssam		subb %ah,%al			# Sectors this track
321138568Ssam		mov 0x2(%bp),%ah		# Blocks to read
322138568Ssam		cmpb %ah,%al			# To read
323170530Ssam		jb read.2			#  this
324170530Ssam#ifdef	TRACK_AT_A_TIME
325178354Ssam		movb %ah,%al			#  track
326138568Ssam#else
327178354Ssam		movb $1,%al			#  one sector
328138568Ssam#endif
329138568Ssamread.2: 	mov $0x5,%di	 		# Try count
330138568Ssamread.3: 	les 0x4(%bp),%bx		# Transfer buffer
331178354Ssam		push %ax			# Save
332178354Ssam		movb $0x2,%ah			# BIOS: Read
333178354Ssam		int $0x13			#  from disk
334178354Ssam		pop %bx				# Restore
335178354Ssam		jnc read.4			# If success
336178354Ssam		dec %di				# Retry?
337178354Ssam		jz read.6			# No
338178354Ssam		xorb %ah,%ah			# BIOS: Reset
339178354Ssam		int $0x13			#  disk system
340178354Ssam		xchg %bx,%ax	 		# Block count
341138568Ssam		jmp read.3			# Continue
342144618Ssamread.4: 	movzbw %bl,%ax	 		# Sectors read
343138568Ssam		add %ax,0x8(%bp)		# Adjust
344178354Ssam		jnc read.5			#  LBA,
345178354Ssam		incw 0xa(%bp)	 		#  transfer
346178354Ssamread.5: 	shlb %bl			#  buffer
347178354Ssam		add %bl,0x5(%bp)		#  pointer,
348170530Ssam		sub %al,0x2(%bp)		#  block count
349178354Ssam		ja read.1			# If not done
350138568Ssamread.6: 	retw				# To caller
351178354Ssam
352178354Ssam/* Messages */
353178354Ssam
354178354Ssammsg_read:	.asciz "Read"
355178354Ssammsg_part:	.asciz "Boot"
356178354Ssam
357178354Ssamprompt: 	.asciz " error\r\n"
358178354Ssam
359178354Ssamflags:		.byte FLAGS			# Flags
360178354Ssam
361170530Ssam		.org PRT_OFF,0x90
362170530Ssam
363138568Ssam/* Partition table */
364148863Ssam
365148863Ssam		.fill 0x30,0x1,0x0
366170530Ssampart4:		.byte 0x80, 0x00, 0x01, 0x00
367148863Ssam		.byte 0xa5, 0xfe, 0xff, 0xff
368178354Ssam		.byte 0x00, 0x00, 0x00, 0x00
369170530Ssam		.byte 0x50, 0xc3, 0x00, 0x00	# 50000 sectors long, bleh
370170530Ssam
371138568Ssam		.word 0xaa55			# Magic number
372138568Ssam