boot0.S revision 137298
1/*
2 * Copyright (c) 2002 Bruce M. Simpson
3 * Copyright (c) 1998 Robert Nordier
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms are freely
7 * permitted provided that the above copyright notice and this
8 * paragraph and the following disclaimer are duplicated in all
9 * such forms.
10 *
11 * This software is provided "AS IS" and without any express or
12 * implied warranties, including, without limitation, the implied
13 * warranties of merchantability and fitness for a particular
14 * purpose.
15 *
16 * $FreeBSD: head/sys/boot/i386/boot0/boot0.S 137298 2004-11-06 06:13:13Z keramida $
17 */
18
19/* A 512-byte boot manager. */
20#ifdef SIO
21/* ... using a serial console on COM1. */
22#endif /* SIO */
23
24		.set NHRDRV,0x475		# Number of hard drives
25		.set ORIGIN,0x600		# Execution address
26		.set FAKE,0x800 		# Partition entry
27		.set LOAD,0x7c00		# Load address
28
29		.set PRT_OFF,0x1be		# Partition table
30
31		.set TBL0SZ,0x3 		# Table 0 size
32		.set TBL1SZ,0xb 		# Table 1 size
33
34		.set MAGIC,0xaa55		# Magic: bootable
35		.set B0MAGIC,0xbb66		# Identification
36
37		.set KEY_ENTER,0x1c		# Enter key scan code
38		.set KEY_F1,0x3b		# F1 key scan code
39		.set KEY_1,0x02			# #1 key scan code
40
41		.set ASCII_BEL,0x07		# ASCII code for <BEL>
42		.set ASCII_CR,0x0D		# ASCII code for <CR>
43
44/*
45 * Addresses in the sector of embedded data values.
46 * Accessed with negative offsets from the end of the relocated sector (%ebp).
47 */
48		.set _NXTDRV,-0x48		# Next drive
49		.set _OPT,-0x47 		# Default option
50		.set _SETDRV,-0x46		# Drive to force
51		.set _FLAGS,-0x45		# Flags
52		.set _TICKS,-0x44		# Timeout ticks
53		.set _FAKE,0x0			# Fake partition entry
54		.set _MNUOPT,0xc		# Menu options
55
56		.globl start			# Entry point
57		.code16				# This runs in real mode
58
59/*
60 * Initialise segments and registers to known values.
61 * segments start at 0.
62 * The stack is immediately below the address we were loaded to.
63 */
64start:		cld				# String ops inc
65		xorw %ax,%ax			# Zero
66		movw %ax,%es			# Address
67		movw %ax,%ds			#  data
68		movw %ax,%ss			# Set up
69		movw $LOAD,%sp			#  stack
70
71/*
72 * Copy this code to the address it was linked for
73 */
74		movw %sp,%si			# Source
75		movw $start,%di			# Destination
76		movw $0x100,%cx			# Word count
77		rep				# Relocate
78		movsw				#  code
79/*
80 * Set address for variable space beyond code, and clear it.
81 * Notice that this is also used to point to the values embedded in the block,
82 * by using negative offsets.
83 */
84		movw %di,%bp			# Address variables
85		movb $0x8,%cl			# Words to clear
86		rep				# Zero
87		stosw				#  them
88/*
89 * Relocate to the new copy of the code.
90 */
91		incb -0xe(%di)			# Sector number
92		jmp main-LOAD+ORIGIN		# To relocated code
93
94main:
95#ifdef SIO
96/*
97 * Initialize the serial port.  bioscom preserves the driver number in DX.
98 */
99		movw COMSPEED,%ax		# defined by Makefile
100		callw bioscom
101#endif /* SIO */
102/*
103 * Check what flags were loaded with us, specifically if a predefined drive
104 * number should be used.  If what the bios gives us is bad, use the '0' in
105 * the block instead.
106 */
107		testb $0x20,_FLAGS(%bp)		# Set number drive?
108		jnz main.1			# Yes
109		testb %dl,%dl			# Drive number valid?
110		js main.2			# Possibly (0x80 set)
111main.1:		movb _SETDRV(%bp),%dl		# Drive number to use
112/*
113 * Whatever we decided to use, now store it into the fake
114 * partition entry that lives in the data space above us.
115 */
116main.2:		movb %dl,_FAKE(%bp)		# Save drive number
117		callw putn			# To new line
118		pushw %dx			# Save drive number
119/*
120 * Start out with a pointer to the 4th byte of the first table entry
121 * so that after 4 iterations it's beyond the end of the sector
122 * and beyond a 256 byte boundary and has overflowed 8 bits (see next comment).
123 * Remember that the table starts 2 bytes earlier than you would expect
124 * as the bootable flag is after it in the block.
125 */
126		movw $(partbl+0x4),%bx		# Partition table (+4)
127		xorw %dx,%dx			# Item number
128/*
129 * Loop around on the partition table, printing values until we
130 * pass a 256 byte boundary. The end of loop test is at main.5.
131 */
132main.3:		movb %ch,-0x4(%bx)		# Zero active flag (ch == 0)
133		btw %dx,_FLAGS(%bp)		# Entry enabled?
134		jnc main.5			# No
135/*
136 * If any of the entries in the table are the same as the 'type' in the slice
137 * table entry, then this is an empty or non bootable partition. Skip it.
138 */
139		movb (%bx),%al			# Load type
140		movw $tables,%di		# Lookup tables
141		movb $TBL0SZ,%cl		# Number of entries
142		repne				# Exclude
143		scasb				#  partition?
144		je main.5			# Yes
145/*
146 * Now scan the table of known types
147 */
148		movb $TBL1SZ,%cl		# Number of entries
149		repne				# Known
150		scasb				#  type?
151		jne main.4			# No
152/*
153 * If it matches get the matching element in the next array.  If it doesn't,
154 * we are already pointing at its first element which points to a "?".
155 */
156		addw $TBL1SZ,%di		# Adjust
157main.4:		movb (%di),%cl			# Partition
158		addw %cx,%di			#  description
159		callw putx			# Display it
160main.5:		incw %dx			# Next item
161		addb $0x10,%bl			# Next entry
162		jnc main.3			# Till done
163/*
164 * Passed a 256 byte boundary; the table is finished.
165 * Add one to the drive number and check it is valid.
166 */
167		popw %ax			# Drive number
168		subb $0x80-0x1,%al		# Does next
169		cmpb NHRDRV,%al			#  drive exist? (from BIOS?)
170		jb main.6			# Yes
171/*
172 * If this is the only drive, don't display it as an option.
173 */
174		decw %ax			# Already drive 0?
175		jz main.7			# Yes
176/*
177 * If it was illegal or we cycled through them, go back to drive 0.
178 */
179		xorb %al,%al			# Drive 0
180/*
181 * Whatever drive we selected, make it an ascii digit and save it back to the
182 * "next drive" location in the loaded block in case we want to save it later
183 * for next time.  This also is part of the printed drive string so add 0x80
184 * to indicate end of string.
185 */
186main.6:		addb $'0'|0x80,%al		# Save next
187		movb %al,_NXTDRV(%bp)		#  drive number
188		movw $drive,%di			# Display
189		callw putx			#  item
190/*
191 * Now that we've printed the drive (if we needed to), display a prompt.
192 */
193main.7:		movw $prompt,%si		# Display
194		callw putstr			#  prompt
195		movb _OPT(%bp),%dl		# Display
196		decw %si			#  default
197		callw putkey			#  key
198/*
199 * Start of input loop.  Beep and take note of time
200 */
201main.10:	movb $ASCII_BEL,%al		# Signal
202		callw putchr			#  beep!
203		xorb %ah,%ah			# BIOS: Get
204		int $0x1a			#  system time
205		movw %dx,%di			# Ticks when
206		addw _TICKS(%bp),%di		#  timeout
207/*
208 * Busy loop, looking for keystrokes but keeping one eye on the time.
209 */
210main.8:
211#ifndef SIO
212		movb $0x1,%ah			# BIOS: Check
213		int $0x16			#  for keypress
214		jnz main.11			# Have one
215#else /* SIO */
216		movb $0x03,%ah			# BIOS: Read COM
217		call bioscom
218		testb $0x01,%ah			# Check line status
219		jnz main.11 			# (bit 1 indicates input)
220#endif /* SIO */
221		xorb %ah,%ah			# BIOS: Get
222		int $0x1a			#  system time
223		cmpw %di,%dx			# Timeout?
224		jb main.8			# No
225/*
226 * If timed out or defaulting, come here.
227 */
228main.9:		movb _OPT(%bp),%al		# Load default
229		jmp main.12			# Join common code
230/*
231 * Get the keystroke.
232 */
233main.11:
234#ifndef SIO
235		xorb %ah,%ah			# BIOS: Get
236		int $0x16			#  keypress
237		movb %ah,%al			# Scan code
238#else /* SIO */
239		movb $0x02,%ah			# BIOS: Receive
240		call bioscom
241#endif /* SIO */
242/*
243 * If it's CR act as if timed out.
244 */
245#ifndef SIO
246		cmpb $KEY_ENTER,%al		# Enter pressed?
247#else /* SIO */
248		cmpb $ASCII_CR,%al		# Enter pressed?
249#endif /* SIO */
250		je main.9			# Yes
251/*
252 * Otherwise check if legal. If not ask again.
253 */
254#ifndef SIO
255		subb $KEY_F1,%al		# Less F1 scan code
256		cmpb $0x4,%al			# F1..F5?
257		jna main.12			# Yes
258		subb $(KEY_1 - KEY_F1),%al	# Less #1 scan code
259#else /* SIO */
260		subb $'1',%al			# Less '1' ascii character
261#endif /* SIO */
262		cmpb $0x4,%al			# #1..#5?
263		ja main.10			# No
264/*
265 * We have a selection.  If it's a bad selection go back to complain.
266 * The bits in MNUOPT were set when the options were printed.
267 * Anything not printed is not an option.
268 */
269main.12:	cbtw				# Option
270		btw %ax,_MNUOPT(%bp)	 	#  enabled?
271		jnc main.10			# No
272/*
273 * Save the info in the original tables
274 * for rewriting to the disk.
275 */
276		movb %al,_OPT(%bp)		# Save option
277		movw $FAKE,%si			# Partition for write
278		movb (%si),%dl			# Drive number
279		movw %si,%bx			# Partition for read
280		cmpb $0x4,%al			# F5/#5 pressed?
281		pushf				# Save
282		je main.13			# Yes
283		shlb $0x4,%al			# Point to
284		addw $partbl,%ax		#  selected
285		xchgw %bx,%ax	 		#  partition
286		movb $0x80,(%bx)		# Flag active
287/*
288 * If not asked to do a write-back (flags 0x40) don't do one.
289 */
290main.13:	pushw %bx			# Save
291		testb $0x40,_FLAGS(%bp)		# No updates?
292		jnz main.14			# Yes
293		movw $start,%bx			# Data to write
294		movb $0x3,%ah			# Write sector
295		callw intx13			#  to disk
296main.14:	popw %si			# Restore
297		popf				# Restore
298/*
299 * If going to next drive, replace drive with selected one.
300 * Remember to un-ascii it. Hey 0x80 is already set, cool!
301 */
302		jne main.15			# If not F5/#5
303		movb _NXTDRV(%bp),%dl		# Next drive
304		subb $'0',%dl			#  number
305/*
306 * Load selected bootsector to the LOAD location in RAM.
307 * If it fails to read or isn't marked bootable, treat it as a bad selection.
308 * XXX: What does %si carry?
309 */
310main.15:	movw $LOAD,%bx			# Address for read
311		movb $0x2,%ah			# Read sector
312		callw intx13			#  from disk
313		jc main.10			# If error
314		cmpw $MAGIC,0x1fe(%bx)		# Bootable?
315		jne main.10			# No
316		pushw %si			# Save
317		movw $crlf,%si			# Leave some
318		callw puts			#  space
319		popw %si			# Restore
320		jmp *%bx			# Invoke bootstrap
321
322/*
323 * Display routines
324 */
325putkey:
326#ifndef SIO
327		movb $'F',%al			# Display
328		callw putchr			#  'F'
329#endif /* SIO */
330		movb $'1',%al			# Prepare
331		addb %dl,%al			#  digit
332		jmp putstr.1			# Display the rest
333
334/*
335 * Display the option and note that it is a valid option.
336 * That last point is a bit tricky..
337 */
338putx:		btsw %dx,_MNUOPT(%bp)		# Enable menu option
339		movw $item,%si			# Display
340		callw putkey			#  key
341		movw %di,%si			# Display the rest
342
343puts:		callw putstr			# Display string
344
345putn:		movw $crlf,%si			# To next line
346
347putstr:		lodsb				# Get byte
348		testb $0x80,%al 		# End of string?
349		jnz putstr.2			# Yes
350putstr.1:	callw putchr			# Display char
351		jmp putstr			# Continue
352putstr.2:	andb $~0x80,%al 		# Clear MSB
353
354#ifndef SIO
355putchr:
356		pushw %bx			# Save
357		movw $0x7,%bx	 		# Page:attribute
358		movb $0xe,%ah			# BIOS: Display
359		int $0x10			#  character
360		popw %bx			# Restore
361		retw				# To caller
362#else /* SIO */
363putchr:
364		movb $0x01,%ah			# BIOS: Send
365bioscom:
366		pushw %dx			# Save
367		xorw %dx,%dx 			# Use COM1
368		int $0x14			#  Character
369		popw %dx			# Restore
370		retw				# To caller
371#endif /* SIO */
372
373/* One-sector disk I/O routine */
374
375intx13:		movb 0x1(%si),%dh		# Load head
376		movw 0x2(%si),%cx		# Load cylinder:sector
377		movb $0x1,%al			# Sector count
378		pushw %si			# Save
379		movw %sp,%di			# Save
380		testb $0x80,_FLAGS(%bp)		# Use packet interface?
381		jz intx13.1			# No
382		pushl $0x0			# Set the
383		pushl 0x8(%si)			# LBA address
384		pushw %es			# Set the transfer
385		pushw %bx			#  buffer address
386		push  $0x1			# Block count
387		push  $0x10			# Packet size
388		movw %sp,%si			# Packet pointer
389		decw %ax			# Verify off
390		orb $0x40,%ah			# Use disk packet
391intx13.1:	int $0x13			# BIOS: Disk I/O
392		movw %di,%sp			# Restore
393		popw %si			# Restore
394		retw				# To caller
395
396/* Menu strings */
397
398item:		.ascii "  ";	     .byte ' '|0x80
399prompt:		.ascii "\nDefault:"; .byte ' '|0x80
400crlf:		.ascii "\r";	     .byte '\n'|0x80
401
402/* Partition type tables */
403
404tables:
405/*
406 * These entries identify invalid or NON BOOT types and partitions.
407 */
408		.byte 0x0, 0x5, 0xf
409/*
410 * These values indicate bootable types we know the names of.
411 */
412		.byte 0x1, 0x4, 0x6, 0xb, 0xc, 0xe, 0x83
413		.byte 0x9f, 0xa5, 0xa6, 0xa9
414/*
415 * These are offsets that match the known names above and point to the strings
416 * that will be printed.
417 */
418		.byte os_misc-. 		# Unknown
419		.byte os_dos-.			# DOS
420		.byte os_dos-.			# DOS
421		.byte os_dos-.			# DOS
422		.byte os_dos-.			# Windows
423		.byte os_dos-.			# Windows
424		.byte os_dos-.			# Windows
425		.byte os_linux-.		# Linux
426		.byte os_bsd-.			# BSD/OS
427		.byte os_freebsd-.		# FreeBSD
428		.byte os_bsd-.			# OpenBSD
429		.byte os_bsd-.			# NetBSD
430/*
431 * And here are the strings themselves. 0x80 or'd into a byte indicates
432 * the end of the string. (not so great for Russians but...)
433 */
434os_misc:	.ascii "?";    .byte '?'|0x80
435os_dos:		.ascii "DO";   .byte 'S'|0x80
436os_linux:	.ascii "Linu"; .byte 'x'|0x80
437os_freebsd:	.ascii "Free"
438os_bsd:		.ascii "BS";   .byte 'D'|0x80
439
440		.org PRT_OFF-0xe,0x90
441
442		.word B0MAGIC			# Magic number
443
444/*
445 * These values are sometimes changed before writing back to the drive
446 * Be especially careful that nxtdrv: must come after drive:, as it
447 * is part of the same string.
448 */
449drive:		.ascii "Drive "
450nxtdrv:		.byte 0x0			# Next drive number
451opt:		.byte 0x0			# Option
452setdrv:		.byte 0x80			# Drive to force
453flags:		.byte FLAGS			# Flags
454ticks:		.word TICKS			# Delay
455
456/*
457 * Here is the 64 byte partition table that fdisk would fiddle with.
458 */
459partbl:		.fill 0x40,0x1,0x0		# Partition table
460		.word MAGIC			# Magic number
461