boot0.S revision 128722
1128722Sru#
2128722Sru# Copyright (c) 2002 Bruce M. Simpson
3128722Sru# Copyright (c) 1998 Robert Nordier
4128722Sru# All rights reserved.
5128722Sru#
6128722Sru# Redistribution and use in source and binary forms are freely
7128722Sru# permitted provided that the above copyright notice and this
8128722Sru# paragraph and the following disclaimer are duplicated in all
9128722Sru# such forms.
10128722Sru#
11128722Sru# This software is provided "AS IS" and without any express or
12128722Sru# implied warranties, including, without limitation, the implied
13128722Sru# warranties of merchantability and fitness for a particular
14128722Sru# purpose.
15128722Sru#
16128722Sru# $FreeBSD: head/sys/boot/i386/boot0/boot0.S 128722 2004-04-28 20:49:17Z ru $
17128722Sru#
18128722Sru
19128722Sru# A 512-byte boot manager.
20128722Sru#ifdef SIO
21128722Sru# ... using a serial console on COM1.
22128722Sru#endif /* SIO */
23128722Sru
24128722Sru		.set NHRDRV,0x475		# Number of hard drives
25128722Sru		.set ORIGIN,0x600		# Execution address
26128722Sru		.set FAKE,0x800 		# Partition entry
27128722Sru		.set LOAD,0x7c00		# Load address
28128722Sru
29128722Sru		.set PRT_OFF,0x1be		# Partition table
30128722Sru
31128722Sru		.set TBL0SZ,0x3 		# Table 0 size
32128722Sru		.set TBL1SZ,0xb 		# Table 1 size
33128722Sru
34128722Sru		.set MAGIC,0xaa55		# Magic: bootable
35128722Sru		.set B0MAGIC,0xbb66		# Identification
36128722Sru
37128722Sru		.set KEY_ENTER,0x1c		# Enter key scan code
38128722Sru		.set KEY_F1,0x3b		# F1 key scan code
39128722Sru		.set KEY_1,0x02			# #1 key scan code
40128722Sru
41128722Sru		.set ASCII_BEL,0x07		# ASCII code for <BEL>
42128722Sru		.set ASCII_CR,0x0D		# ASCII code for <CR>
43128722Sru
44128722Sru#
45128722Sru# Addresses in the sector of embedded data values.
46128722Sru# Accessed with negative offsets from the end of the relocated sector (%ebp).
47128722Sru#
48128722Sru		.set _NXTDRV,-0x48		# Next drive
49128722Sru		.set _OPT,-0x47 		# Default option
50128722Sru		.set _SETDRV,-0x46		# Drive to force
51128722Sru		.set _FLAGS,-0x45		# Flags
52128722Sru		.set _TICKS,-0x44		# Timeout ticks
53128722Sru		.set _FAKE,0x0			# Fake partition entry
54128722Sru		.set _MNUOPT,0xc		# Menu options
55128722Sru
56128722Sru		.globl start			# Entry point
57128722Sru		.code16				# This runs in real mode
58128722Sru
59128722Sru#
60128722Sru# Initialise segments and registers to known values.
61128722Sru# segments start at 0.
62128722Sru# The stack is immediately below the address we were loaded to.
63128722Sru#
64128722Srustart:		cld				# String ops inc
65128722Sru		xorw %ax,%ax			# Zero
66128722Sru		movw %ax,%es			# Address
67128722Sru		movw %ax,%ds			#  data
68128722Sru		movw %ax,%ss			# Set up
69128722Sru		movw $LOAD,%sp			#  stack
70128722Sru
71128722Sru#
72128722Sru# Copy this code to the address it was linked for
73128722Sru#
74128722Sru		movw %sp,%si			# Source
75128722Sru		movw $start,%di			# Destination
76128722Sru		movw $0x100,%cx			# Word count
77128722Sru		rep				# Relocate
78128722Sru		movsw				#  code
79128722Sru#
80128722Sru# Set address for variable space beyond code, and clear it.
81128722Sru# Notice that this is also used to point to the values embedded in the block,
82128722Sru# by using negative offsets.
83128722Sru#
84128722Sru		movw %di,%bp			# Address variables
85128722Sru		movb $0x8,%cl			# Words to clear
86128722Sru		rep				# Zero
87128722Sru		stosw				#  them
88128722Sru#
89128722Sru# Relocate to the new copy of the code.
90128722Sru#
91128722Sru		incb -0xe(%di)			# Sector number
92128722Sru		jmp main-LOAD+ORIGIN		# To relocated code
93128722Sru
94128722Srumain:
95128722Sru#ifdef SIO
96128722Sru#
97128722Sru# Initialize the serial port.
98128722Sru# Must save DX (contains drive number)
99128722Sru#
100128722Sru		pushw %dx			# Save
101128722Sru		xorw %dx,%dx			# Port: COM1
102128722Sru		movb COMSPEED,%al		# defined by Makefile
103128722Sru		movb $0x00,%ah			# BIOS: Set COM Port
104128722Sru		int $0x14			#  Parameters
105128722Sru		popw %dx			# Restore
106128722Sru#endif /* SIO */
107128722Sru#
108128722Sru# Check what flags were loaded with us, specifically, Use a predefined Drive.
109128722Sru# If what the bios gives us is bad, use the '0' in the block instead, as well.
110128722Sru#
111128722Sru		testb $0x20,_FLAGS(%bp)		# Set number drive?
112128722Sru		jnz main.1			# Yes
113128722Sru		testb %dl,%dl			# Drive number valid?
114128722Sru		js main.2			# Possibly (0x80 set)
115128722Srumain.1:		movb _SETDRV(%bp),%dl		# Drive number to use
116128722Sru#
117128722Sru# Whatever we decided to use, now store it into the fake
118128722Sru# partition entry that lives in the data space above us.
119128722Sru#
120128722Srumain.2:		movb %dl,_FAKE(%bp)		# Save drive number
121128722Sru		callw putn			# To new line
122128722Sru		pushw %dx			# Save drive number
123128722Sru#
124128722Sru# Start out with a pointer to the 4th byte of the first table entry
125128722Sru# so that after 4 iterations it's beyond the end of the sector.
126128722Sru# and beyond a 256 byte boundary and has overflowed 8 bits (see next comment).
127128722Sru# (remember that the table starts 2 bytes earlier than you would expect
128128722Sru# as the bootable flag is after it in the block)
129128722Sru#
130128722Sru		movw $(partbl+0x4),%bx		# Partition table (+4)
131128722Sru		xorw %dx,%dx			# Item number
132128722Sru#
133128722Sru# Loop around on the partition table, printing values until we
134128722Sru# pass a 256 byte boundary. The end of loop test is at main.5.
135128722Sru#
136128722Srumain.3:		movb %ch,-0x4(%bx)		# Zero active flag (ch == 0)
137128722Sru		btw %dx,_FLAGS(%bp)		# Entry enabled?
138128722Sru		jnc main.5			# No
139128722Sru#
140128722Sru# If any of the entries in the table are
141128722Sru# the same as the 'type' in the slice table entry,
142128722Sru# then this is an empty or non bootable partition. Skip it.
143128722Sru#
144128722Sru		movb (%bx),%al			# Load type
145128722Sru		movw $tables,%di		# Lookup tables
146128722Sru		movb $TBL0SZ,%cl		# Number of entries
147128722Sru		repne				# Exclude
148128722Sru		scasb				#  partition?
149128722Sru		je main.5			# Yes
150128722Sru#
151128722Sru# Now scan the table of known types
152128722Sru#
153128722Sru		movb $TBL1SZ,%cl		# Number of entries
154128722Sru		repne				# Known
155128722Sru		scasb				#  type?
156128722Sru		jne main.4			# No
157128722Sru#
158128722Sru# If it matches get the matching element in the
159128722Sru# next array. if it doesn't, we are already
160128722Sru# pointing at its first element which points to a "?".
161128722Sru#
162128722Sru		addw $TBL1SZ,%di		# Adjust
163128722Srumain.4:		movb (%di),%cl			# Partition
164128722Sru		addw %cx,%di			#  description
165128722Sru		callw putx			# Display it
166128722Srumain.5:		incw %dx			# Next item
167128722Sru		addb $0x10,%bl			# Next entry
168128722Sru		jnc main.3			# Till done
169128722Sru#
170128722Sru# Passed a 256 byte boundary..
171128722Sru# table is finished.
172128722Sru# Add one to the drive number and check it is valid,
173128722Sru#
174128722Sru		popw %ax			# Drive number
175128722Sru		subb $0x80-0x1,%al		# Does next
176128722Sru		cmpb NHRDRV,%al			#  drive exist? (from BIOS?)
177128722Sru		jb main.6			# Yes
178128722Sru#
179128722Sru# If not then if there is only one drive,
180128722Sru# Don't display drive as an option.
181128722Sru#
182128722Sru		decw %ax			# Already drive 0?
183128722Sru		jz main.7			# Yes
184128722Sru#
185128722Sru# If it was illegal or we cycled through them,
186128722Sru# then go back to drive 0.
187128722Sru#
188128722Sru		xorb %al,%al			# Drive 0
189128722Sru#
190128722Sru# Whatever drive we selected, make it an ascii digit and save it back
191128722Sru# to the "next drive" location in the loaded block in case we
192128722Sru# want to save it for next time.
193128722Sru# This also is part of the printed drive string so add 0x80 to indicate
194128722Sru# end of string.
195128722Sru#
196128722Srumain.6:		addb $'0'|0x80,%al		# Save next
197128722Sru		movb %al,_NXTDRV(%bp)		#  drive number
198128722Sru		movw $drive,%di			# Display
199128722Sru		callw putx			#  item
200128722Sru#
201128722Sru# Now that we've printed the drive (if we needed to), display a prompt.
202128722Sru# Get ready for the input by noting the time.
203128722Sru#
204128722Srumain.7:		movw $prompt,%si		# Display
205128722Sru		callw putstr			#  prompt
206128722Sru		movb _OPT(%bp),%dl		# Display
207128722Sru		decw %si			#  default
208128722Sru		callw putkey			#  key
209128722Srumain.7_1:
210128722Sru		xorb %ah,%ah			# BIOS: Get
211128722Sru		int $0x1a			#  system time
212128722Sru#ifndef SIO
213128722Sru		movw %dx,%di			# Ticks when
214128722Sru		addw _TICKS(%bp),%di	 	#  timeout
215128722Sru#else /* SIO */
216128722Sru		movw %dx,%si			# Ticks when
217128722Sru		addw _TICKS(%bp),%si		#  timeout
218128722Sru#endif /* SIO */
219128722Sru#
220128722Sru# Busy loop, looking for keystrokes but
221128722Sru# keeping one eye on the time.
222128722Sru#
223128722Srumain.8:
224128722Sru#ifndef SIO
225128722Sru		movb $0x1,%ah			# BIOS: Check
226128722Sru		int $0x16			#  for keypress
227128722Sru		jnz main.11			# Have one
228128722Sru#else /* SIO */
229128722Sru		xorw %dx,%dx			# Use COM1
230128722Sru		movb $0x03,%ah			# BIOS: Read COM
231128722Sru		int $0x14			#  Status
232128722Sru		testb $0x01,%ah			# Check line status
233128722Sru		jnz main.11 			# (bit 1 indicates input)
234128722Sru#endif /* SIO */
235128722Sru		xorb %ah,%ah			# BIOS: Get
236128722Sru		int $0x1a			#  system time
237128722Sru#ifndef SIO
238128722Sru		cmpw %di,%dx			# Timeout?
239128722Sru#else /* SIO */
240128722Sru		cmpw %si,%dx			# Timeout?
241128722Sru#endif /* SIO */
242128722Sru		jb main.8			# No
243128722Sru#
244128722Sru# If timed out or defaulting, come here.
245128722Sru#
246128722Srumain.9:		movb _OPT(%bp),%al		# Load default
247128722Sru		jmp main.12			# Join common code
248128722Sru#
249128722Sru# User's last try was bad, beep in displeasure.
250128722Sru# Since nothing was printed, just continue on as if the user
251128722Sru# hadn't done anything. This gives the effect of the user getting a beep
252128722Sru# for all bad keystrokes but no action until either the timeout
253128722Sru# occurs or the user hits a good key.
254128722Sru#
255128722Srumain.10:	movb $ASCII_BEL,%al		# Signal
256128722Sru		callw putchr			#  error
257128722Sru#ifdef SIO
258128722Sru		jmp main.7_1			# Go back
259128722Sru#endif /* SIO */
260128722Sru#
261128722Sru# Get the keystroke.
262128722Sru#
263128722Srumain.11:
264128722Sru#ifndef SIO
265128722Sru		xorb %ah,%ah			# BIOS: Get
266128722Sru		int $0x16			#  keypress
267128722Sru		movb %ah,%al			# Scan code
268128722Sru#else /* SIO */
269128722Sru		movb $0x02,%ah			# BIOS: Receive
270128722Sru		int $0x14			#  COM Byte
271128722Sru#endif /* SIO */
272128722Sru#
273128722Sru# If it's CR act as if timed out.
274128722Sru#
275128722Sru#ifndef SIO
276128722Sru		cmpb $KEY_ENTER,%al		# Enter pressed?
277128722Sru#else /* SIO */
278128722Sru		cmpb $ASCII_CR,%al		# Enter pressed?
279128722Sru#endif /* SIO */
280128722Sru		je main.9			# Yes
281128722Sru#
282128722Sru# Otherwise check if legal
283128722Sru# If not ask again.
284128722Sru#
285128722Sru#ifndef SIO
286128722Sru		subb $KEY_F1,%al		# Less F1 scan code
287128722Sru		cmpb $0x4,%al			# F1..F5?
288128722Sru		jna main.12			# Yes
289128722Sru		subb $(KEY_1 - KEY_F1),%al	# Less #1 scan code
290128722Sru#else /* SIO */
291128722Sru		subb $'1',%al			# Less '1' ascii character
292128722Sru#endif /* SIO */
293128722Sru		cmpb $0x4,%al			# #1..#5?
294128722Sru		ja main.10			# No
295128722Sru#
296128722Sru# We have a selection.
297128722Sru# but if it's a bad selection go back to complain.
298128722Sru# The bits in MNUOPT were set when the options were printed.
299128722Sru# Anything not printed is not an option.
300128722Sru#
301128722Srumain.12:	cbtw				# Option
302128722Sru		btw %ax,_MNUOPT(%bp)	 	#  enabled?
303128722Sru		jnc main.10			# No
304128722Sru#
305128722Sru# Save the info in the original tables
306128722Sru# for rewriting to the disk.
307128722Sru#
308128722Sru		movb %al,_OPT(%bp)		# Save option
309128722Sru		movw $FAKE,%si			# Partition for write
310128722Sru		movb (%si),%dl			# Drive number
311128722Sru		movw %si,%bx			# Partition for read
312128722Sru		cmpb $0x4,%al			# F5/#5 pressed?
313128722Sru		pushf				# Save
314128722Sru		je main.13			# Yes
315128722Sru		shlb $0x4,%al			# Point to
316128722Sru		addw $partbl,%ax		#  selected
317128722Sru		xchgw %bx,%ax	 		#  partition
318128722Sru		movb $0x80,(%bx)		# Flag active
319128722Sru#
320128722Sru# If not asked to do a write-back (flags 0x40) don't do one.
321128722Sru#
322128722Srumain.13:	pushw %bx			# Save
323128722Sru		testb $0x40,_FLAGS(%bp)		# No updates?
324128722Sru		jnz main.14			# Yes
325128722Sru		movw $start,%bx			# Data to write
326128722Sru		movb $0x3,%ah			# Write sector
327128722Sru		callw intx13			#  to disk
328128722Srumain.14:	popw %si			# Restore
329128722Sru		popf				# Restore
330128722Sru#
331128722Sru# If going to next drive, replace drive with selected one.
332128722Sru# Remember to un-ascii it. Hey 0x80 is already set, cool!
333128722Sru#
334128722Sru		jne main.15			# If not F5/#5
335128722Sru		movb _NXTDRV(%bp),%dl		# Next drive
336128722Sru		subb $'0',%dl			#  number
337128722Sru#
338128722Sru# load  selected bootsector to the LOAD location in RAM.
339128722Sru# If it fails to read or isn't marked bootable, treat it
340128722Sru# as a bad selection.
341128722Sru# XXX what does %si carry?
342128722Sru#
343128722Srumain.15:	movw $LOAD,%bx			# Address for read
344128722Sru		movb $0x2,%ah			# Read sector
345128722Sru		callw intx13			#  from disk
346128722Sru		jc main.10			# If error
347128722Sru		cmpw $MAGIC,0x1fe(%bx)		# Bootable?
348128722Sru		jne main.10			# No
349128722Sru		pushw %si			# Save
350128722Sru		movw $crlf,%si			# Leave some
351128722Sru		callw puts			#  space
352128722Sru		popw %si			# Restore
353128722Sru		jmp *%bx			# Invoke bootstrap
354128722Sru
355128722Sru#
356128722Sru# Display routines
357128722Sru#
358128722Sruputkey:
359128722Sru#ifndef SIO
360128722Sru		movb $'F',%al			# Display
361128722Sru		callw putchr			#  'F'
362128722Sru#endif /* SIO */
363128722Sru		movb $'1',%al			# Prepare
364128722Sru		addb %dl,%al			#  digit
365128722Sru		jmp putstr.1			# Display the rest
366128722Sru
367128722Sru#
368128722Sru# Display the option and note that it is a valid option.
369128722Sru# That last point is a bit tricky..
370128722Sru#
371128722Sruputx:		btsw %dx,_MNUOPT(%bp)		# Enable menu option
372128722Sru		movw $item,%si			# Display
373128722Sru		callw putkey			#  key
374128722Sru		movw %di,%si			# Display the rest
375128722Sru
376128722Sruputs:		callw putstr			# Display string
377128722Sru
378128722Sruputn:		movw $crlf,%si			# To next line
379128722Sru
380128722Sruputstr:		lodsb				# Get byte
381128722Sru		testb $0x80,%al 		# End of string?
382128722Sru		jnz putstr.2			# Yes
383128722Sruputstr.1:	callw putchr			# Display char
384128722Sru		jmp putstr			# Continue
385128722Sruputstr.2:	andb $~0x80,%al 		# Clear MSB
386128722Sru
387128722Sruputchr:
388128722Sru#ifndef SIO
389128722Sru		pushw %bx			# Save
390128722Sru		movw $0x7,%bx	 		# Page:attribute
391128722Sru		movb $0xe,%ah			# BIOS: Display
392128722Sru		int $0x10			#  character
393128722Sru		popw %bx			# Restore
394128722Sru#else /* SIO */
395128722Sru		pushw %dx			# Save
396128722Sru		xorw %dx,%dx 			# Use COM1
397128722Sru		xorw %cx,%cx 			# No timeout
398128722Sru		movb $0x01,%ah			# BIOS: Send
399128722Sru		int $0x14			#  Character
400128722Sru		popw %dx			# Restore
401128722Sru#endif /* SIO */
402128722Sru		retw				# To caller
403128722Sru
404128722Sru# One-sector disk I/O routine
405128722Sru
406128722Sruintx13:		movb 0x1(%si),%dh		# Load head
407128722Sru		movw 0x2(%si),%cx		# Load cylinder:sector
408128722Sru		movb $0x1,%al			# Sector count
409128722Sru		pushw %si			# Save
410128722Sru		movw %sp,%di			# Save
411128722Sru		testb $0x80,_FLAGS(%bp)		# Use packet interface?
412128722Sru		jz intx13.1			# No
413128722Sru		pushl $0x0			# Set the
414128722Sru		pushl 0x8(%si)			# LBA address
415128722Sru		pushw %es			# Set the transfer
416128722Sru		pushw %bx			#  buffer address
417128722Sru		push  $0x1			# Block count
418128722Sru		push  $0x10			# Packet size
419128722Sru		movw %sp,%si			# Packet pointer
420128722Sru		decw %ax			# Verify off
421128722Sru		orb $0x40,%ah			# Use disk packet
422128722Sruintx13.1:	int $0x13			# BIOS: Disk I/O
423128722Sru		movw %di,%sp			# Restore
424128722Sru		popw %si			# Restore
425128722Sru		retw				# To caller
426128722Sru
427128722Sru# Menu strings
428128722Sru
429128722Sru#ifndef SIO
430128722Sruitem:		.ascii "  ";	     .byte ' '|0x80
431128722Sruprompt:		.ascii "\nDefault:"; .byte ' '|0x80
432128722Sru#else /* SIO */
433128722Sruitem:		.ascii " ";	     .byte ' '|0x80
434128722Sruprompt:		.ascii "\nDef:";     .byte ' '|0x80
435128722Sru#endif /* SIO */
436128722Srucrlf:		.ascii "\r";	     .byte '\n'|0x80
437128722Sru
438128722Sru# Partition type tables
439128722Sru
440128722Srutables:
441128722Sru#
442128722Sru# These entries identify invalid or NON BOOT types and partitions.
443128722Sru#
444128722Sru		.byte 0x0, 0x5, 0xf
445128722Sru#
446128722Sru# These values indicate bootable types we know the names of
447128722Sru#
448128722Sru		.byte 0x1, 0x4, 0x6, 0xb, 0xc, 0xe, 0x83
449128722Sru		.byte 0x9f, 0xa5, 0xa6, 0xa9
450128722Sru#
451128722Sru# These are offsets that match the known names above and point to the strings
452128722Sru# that will be printed.
453128722Sru#
454128722Sru		.byte os_misc-. 		# Unknown
455128722Sru		.byte os_dos-.			# DOS
456128722Sru		.byte os_dos-.			# DOS
457128722Sru		.byte os_dos-.			# DOS
458128722Sru		.byte os_dos-.			# Windows
459128722Sru		.byte os_dos-.			# Windows
460128722Sru		.byte os_dos-.			# Windows
461128722Sru		.byte os_linux-.		# Linux
462128722Sru		.byte os_bsd-.			# BSD/OS
463128722Sru		.byte os_freebsd-.		# FreeBSD
464128722Sru		.byte os_bsd-.			# OpenBSD
465128722Sru		.byte os_bsd-.			# NetBSD
466128722Sru#
467128722Sru# And here are the strings themselves. 0x80 or'd into a byte indicates
468128722Sru# the end of the string. (not so great for Russians but...)
469128722Sru#
470128722Sruos_misc:	.ascii "?";    .byte '?'|0x80
471128722Sruos_dos:		.ascii "DO";   .byte 'S'|0x80
472128722Sruos_linux:	.ascii "Linu"; .byte 'x'|0x80
473128722Sruos_freebsd:	.ascii "Free"
474128722Sruos_bsd:		.ascii "BS";   .byte 'D'|0x80
475128722Sru
476128722Sru		.org PRT_OFF-0xe,0x90
477128722Sru
478128722Sru		.word B0MAGIC			# Magic number
479128722Sru
480128722Sru#
481128722Sru# These values are sometimes changed before writing back to the drive
482128722Sru# Be especially careful that nxtdrv: must come after drive:, as it
483128722Sru# is part of the same string.
484128722Sru#
485128722Srudrive:		.ascii "Drive "
486128722Srunxtdrv:		.byte 0x0			# Next drive number
487128722Sruopt:		.byte 0x0			# Option
488128722Srusetdrv:		.byte 0x80			# Drive to force
489128722Sruflags:		.byte FLAGS			# Flags
490128722Sruticks:		.word TICKS			# Delay
491128722Sru
492128722Sru#
493128722Sru# here is the 64 byte partition table that fdisk would fiddle with.
494128722Sru#
495128722Srupartbl:		.fill 0x40,0x1,0x0		# Partition table
496128722Sru		.word MAGIC			# Magic number
497