boot0.S revision 185562
1128723Sru/*
2185562Sluigi * Copyright (c) 2008 Luigi Rizzo (mostly documentation)
3128723Sru * Copyright (c) 2002 Bruce M. Simpson
4128723Sru * Copyright (c) 1998 Robert Nordier
5128723Sru * All rights reserved.
6128723Sru *
7128723Sru * Redistribution and use in source and binary forms are freely
8128723Sru * permitted provided that the above copyright notice and this
9128723Sru * paragraph and the following disclaimer are duplicated in all
10128723Sru * such forms.
11128723Sru *
12128723Sru * This software is provided "AS IS" and without any express or
13128723Sru * implied warranties, including, without limitation, the implied
14128723Sru * warranties of merchantability and fitness for a particular
15128723Sru * purpose.
16128723Sru *
17128723Sru * $FreeBSD: head/sys/boot/i386/boot0/boot0.S 185562 2008-12-02 14:57:48Z luigi $
18128723Sru */
19128722Sru
20185562Sluigi/* build options: */
21185562Sluigi#ifdef SIO		/* use serial console on COM1.	*/
22138048Sjhb#endif
23128722Sru
24185562Sluigi#ifdef PXE		/* enable PXE/INT18 booting with F6 */
25185562Sluigi#endif
26185562Sluigi
27185562Sluigi#ifdef CHECK_DRIVE	/* make sure we boot from a HD. */
28185562Sluigi#endif
29185562Sluigi
30185562Sluigi#ifdef ONLY_F_KEYS	/* Only F1..F6, no digits on console */
31185562Sluigi#endif
32185562Sluigi
33185562Sluigi#ifdef TEST		/* enable some test code */
34185562Sluigi#ifndef ONLY_F_KEYS
35185562Sluigi#define ONLY_F_KEYS	/* make room for the test code */
36185562Sluigi#endif
37185562Sluigi#endif
38185562Sluigi
39185562Sluigi/*
40185562Sluigi * Note - this code uses many tricks to save space and fit in one sector.
41185562Sluigi * This includes using side effects of certain instructions, reusing
42185562Sluigi * register values from previous operations, etc.
43185562Sluigi * Be extremely careful when changing the code, even for simple things.
44185562Sluigi */
45185562Sluigi
46185562Sluigi/*
47185562Sluigi *		BOOT BLOCK STRUCTURE
48185562Sluigi *
49185562Sluigi * This code implements a Master Boot Record (MBR) for an Intel/PC disk.
50185562Sluigi * It is 512 bytes long and it is normally loaded by the BIOS (or another
51185562Sluigi * bootloader) at 0:0x7c00. This code depends on %cs:%ip being 0:0x7c00
52185562Sluigi *
53185562Sluigi * The initial chunk of instructions is used as a signature by external
54185562Sluigi * tools (e.g. boot0cfg) which can manipulate the block itself.
55185562Sluigi *
56185562Sluigi * The area at offset 0x1b2 contains a magic string ('Drive '), also
57185562Sluigi * used as a signature to detect the block, and some variables that can
58185562Sluigi * be updated by boot0cfg (and optionally written back to the disk).
59185562Sluigi * These variables control the operation of the bootloader itself,
60185562Sluigi * e.g. which partitions to enable, the timeout, the use of LBA
61185562Sluigi * (called 'packet') or CHS mode, whether to force a drive number,
62185562Sluigi * and whether to write back the user's selection back to disk.
63185562Sluigi *
64185562Sluigi * As in every Master Boot Record, the partition table is at 0x1be,
65185562Sluigi * made of four 16-byte entries each containing:
66185562Sluigi *
67185562Sluigi *   OFF SIZE	DESCRIPTION
68185562Sluigi *    0	  1	status (0x80: bootable, 0: non bootable)
69185562Sluigi *    1	  3	start sector CHS
70185562Sluigi *		   8:head, 6:sector, 2:cyl bit 9..8, 8:cyl bit 7..0
71185562Sluigi *    4   1	partition type
72185562Sluigi *    5   3	end sector CHS
73185562Sluigi *    8   4	LBA of first sector
74185562Sluigi *   12   4	partition size in sectors
75185562Sluigi *
76185562Sluigi * and followed by the two bytes 0x55, 0xAA (MBR signature).
77185562Sluigi */
78185562Sluigi
79185562Sluigi
80185562Sluigi/*
81185562Sluigi *		BOOT BLOCK OPERATION
82185562Sluigi *
83185562Sluigi * On entry, the registers contain the following values:
84185562Sluigi *
85185562Sluigi *	%cs:%ip	0:0x7c00
86185562Sluigi *	%dl	drive number (0x80, 0x81, ... )
87185562Sluigi *	%si	pointer to the partition table from which we were loaded.
88185562Sluigi *		Some boot code (e.g. syslinux) use this info to relocate
89185562Sluigi *		themselves, so we want to pass a valid one to the next stage.
90185562Sluigi *		NOTE: the use of %is is not a standard.
91185562Sluigi *
92185562Sluigi * This boot block first relocates itself at a different address (0:0x600),
93185562Sluigi * to free the space at 0:0x7c00 for the next stage boot block.
94185562Sluigi *
95185562Sluigi * It then initializes some memory at 0:0x800 and above (pointed by %bp)
96185562Sluigi * to store the original drive number (%dl) passed to us, and to construct a
97185562Sluigi * fake partition entry. The latter is used by the disk I/O routine and,
98185562Sluigi * in some cases, passed in %si to the next stage boot code.
99185562Sluigi *
100185562Sluigi * The variables at 0x1b2 are accessed as negative offsets from %bp.
101185562Sluigi *
102185562Sluigi * After the relocation, the code scans the partition table printing
103185562Sluigi * out enabled partition or disks, and waits for user input.
104185562Sluigi *
105185562Sluigi * When a partition is selected, or a timeout expires, the currently
106185562Sluigi * selected partition is used to load the next stage boot code,
107185562Sluigi * %dl and %si are set appropriately as when we were called, and
108185562Sluigi * control is transferred to the newly loaded code at 0:0x7c00.
109185562Sluigi */
110185562Sluigi
111185562Sluigi/*
112185562Sluigi *	CONSTANTS
113185562Sluigi *
114185562Sluigi * NHRDRV is the address in segment 0 where the BIOS writes the
115185562Sluigi *	total number of hard disks in the system.
116185562Sluigi * LOAD is the original load address and cannot be changed.
117185562Sluigi * ORIGIN is the relocation address. If you change it, you also need
118185562Sluigi * 	to change the value passed to the linker in the Makefile
119185562Sluigi * PRT_OFF is the location of the partition table (from the MBR standard).
120185562Sluigi * B0_OFF is the location of the data area, known to boot0cfg so
121185562Sluigi *	it cannot be changed.
122185562Sluigi * MAGIC is the signature of a boot block.
123185562Sluigi */
124185562Sluigi
125129239Sru		.set NHRDRV,0x475		# Number of hard drives
126129239Sru		.set ORIGIN,0x600		# Execution address
127129239Sru		.set LOAD,0x7c00		# Load address
128128722Sru
129129239Sru		.set PRT_OFF,0x1be		# Partition table
130185562Sluigi		.set B0_OFF,0x1b2		# Offset of boot0 data
131128722Sru
132129239Sru		.set MAGIC,0xaa55		# Magic: bootable
133128722Sru
134129239Sru		.set KEY_ENTER,0x1c		# Enter key scan code
135129239Sru		.set KEY_F1,0x3b		# F1 key scan code
136129239Sru		.set KEY_1,0x02			# #1 key scan code
137128722Sru
138185562Sluigi		.set ASCII_BEL,'#'		# ASCII code for <BEL>
139129239Sru		.set ASCII_CR,0x0D		# ASCII code for <CR>
140128722Sru
141128723Sru/*
142185562Sluigi * Offsets of variables in the block at B0_OFF, and in the volatile
143185562Sluigi * data area, computed as displacement from %bp.
144185562Sluigi * We need to define them as constant as the assembler cannot
145185562Sluigi * compute them in its single pass.
146128723Sru */
147185562Sluigi		.set _NXTDRV,	-0x48		# Next drive
148185562Sluigi		.set _OPT,	-0x47 		# Default option
149185562Sluigi		.set _SETDRV,	-0x46		# Drive to force
150185562Sluigi		.set _FLAGS,	-0x45		# Flags
151185562Sluigi		.set SETDRV,	0x20		# the 'setdrv' flag
152185562Sluigi		.set NOUPDATE,	0x40		# the 'noupdate' flag
153185562Sluigi		.set USEPACKET,	0x80		# the 'packet' flag
154185562Sluigi		.set _TICKS,	-0x44		# Timeout ticks
155185562Sluigi		.set _FAKE,0x0			# Fake partition table
156185562Sluigi		.set _MNUOPT, 0x10		# Saved menu entries
157128722Sru
158185562Sluigi		.set TLEN, (desc_ofs - bootable_ids)	# size of bootable ids
159129239Sru		.globl start			# Entry point
160129239Sru		.code16				# This runs in real mode
161128722Sru
162128723Sru/*
163185562Sluigi * 	MAIN ENTRY POINT
164128723Sru * Initialise segments and registers to known values.
165128723Sru * segments start at 0.
166128723Sru * The stack is immediately below the address we were loaded to.
167185562Sluigi * NOTE: the initial section of the code (up to movw $LOAD,%sp)
168185562Sluigi * is used by boot0cfg, together with the 'Drive ' string and
169185562Sluigi * the 0x55, 0xaa at the end, as an identifier for version 1.0
170185562Sluigi * of the boot code. Do not change it.
171185562Sluigi * In version 1.0 the parameter table (_NEXTDRV etc) is at 0x1b9
172128723Sru */
173129239Srustart:		cld				# String ops inc
174129239Sru		xorw %ax,%ax			# Zero
175129239Sru		movw %ax,%es			# Address
176129239Sru		movw %ax,%ds			#  data
177129239Sru		movw %ax,%ss			# Set up
178129239Sru		movw $LOAD,%sp			#  stack
179128722Sru
180185562Sluigi	/*
181185562Sluigi	 * Copy this code to the address it was linked for, 0x600 by default.
182185562Sluigi	 */
183129239Sru		movw %sp,%si			# Source
184129239Sru		movw $start,%di			# Destination
185129239Sru		movw $0x100,%cx			# Word count
186129239Sru		rep				# Relocate
187129239Sru		movsw				#  code
188185562Sluigi	/*
189185562Sluigi	 * After the code, (i.e. at %di+0, 0x800) create a partition entry,
190185562Sluigi	 * initialized to LBA 0 / CHS 0:0:1.
191185562Sluigi	 * Set %bp to point to the partition and also, with negative offsets,
192185562Sluigi	 * to the variables embedded in the bootblock (nextdrv and so on).
193185562Sluigi	 */
194129239Sru		movw %di,%bp			# Address variables
195129239Sru		movb $0x8,%cl			# Words to clear
196129239Sru		rep				# Zero
197129239Sru		stosw				#  them
198185562Sluigi		incb -0xe(%di)			# Set the S field to 1
199128722Sru
200185562Sluigi		jmp main-LOAD+ORIGIN		# Jump to relocated code
201185562Sluigi
202128722Srumain:
203138048Sjhb#if defined(SIO) && COMSPEED != 0
204185562Sluigi	/*
205185562Sluigi	 * Init the serial port. bioscom preserves the driver number in DX.
206185562Sluigi	 */
207167914Sthomas		movw $COMSPEED,%ax		# defined by Makefile
208130632Sphk		callw bioscom
209138048Sjhb#endif
210185562Sluigi
211185562Sluigi	/*
212185562Sluigi	 * If the 'setdrv' flag is set in the boot sector, use the drive
213185562Sluigi	 * number from the boot sector at 'setdrv_num'.
214185562Sluigi	 * Optionally, do the same if the BIOS gives us an invalid number
215185562Sluigi	 * (note though that the override prevents booting from a floppy
216185562Sluigi	 * or a ZIP/flash drive in floppy emulation).
217185562Sluigi	 * The test costs 4 bytes of code so it is disabled by default.
218185562Sluigi	 */
219185562Sluigi		testb $SETDRV,_FLAGS(%bp)	# Set drive number?
220185562Sluigi#ifndef CHECK_DRIVE	/* disable drive checks */
221185562Sluigi		jz save_curdrive		# no, use the default
222185562Sluigi#else
223185562Sluigi		jnz disable_update		# Yes
224129239Sru		testb %dl,%dl			# Drive number valid?
225185562Sluigi		js save_curdrive		# Possibly (0x80 set)
226185562Sluigi#endif
227185562Sluigi	/*
228185562Sluigi	 * Disable updates if the drive number is forced.
229185562Sluigi	 */
230185562Sluigidisable_update:	orb $NOUPDATE,_FLAGS(%bp)	# Disable updates
231185562Sluigi		movb _SETDRV(%bp),%dl		# Use stored drive number
232185562Sluigi
233185562Sluigi	/*
234185562Sluigi	 * Whatever drive we decided to use, store it at (%bp). The byte
235185562Sluigi	 * is normally used for the state of the partition (0x80 or 0x00),
236185562Sluigi	 * but we abuse it as it is very convenient to access at offset 0.
237185562Sluigi	 * The value is read back after 'check_selection'
238185562Sluigi	 */
239185562Sluigisave_curdrive:	movb %dl, (%bp)			# Save drive number
240185562Sluigi		pushw %dx			# Also in the stack
241185562Sluigi#ifdef	TEST	/* test code, print internal bios drive */
242185562Sluigi		rolb $1, %dl
243185562Sluigi		movw $drive, %si
244185562Sluigi		call putkey
245185562Sluigi#endif
246185562Sluigi		callw putn			# Print a newline
247185562Sluigi	/*
248185562Sluigi	 * Start out with a pointer to the 4th byte of the first table entry
249185562Sluigi	 * so that after 4 iterations it's beyond the end of the sector
250185562Sluigi	 * and beyond a 256 byte boundary. We use the latter trick to check for
251185562Sluigi	 * end of the loop without using an extra register (see start.5).
252185562Sluigi	 */
253129239Sru		movw $(partbl+0x4),%bx		# Partition table (+4)
254129239Sru		xorw %dx,%dx			# Item number
255185562Sluigi
256185562Sluigi	/*
257185562Sluigi	 * Loop around on the partition table, printing values until we
258185562Sluigi	 * pass a 256 byte boundary.
259185562Sluigi	 */
260185562Sluigiread_entry:	movb %ch,-0x4(%bx)		# Zero active flag (ch == 0)
261129239Sru		btw %dx,_FLAGS(%bp)		# Entry enabled?
262185562Sluigi		jnc next_entry			# No
263185562Sluigi	/*
264185562Sluigi	 * Lookup type in the 'non_bootable_ids' table, skip matching entries.
265185562Sluigi	 * This is implemented is by setting %di to the start of the
266185562Sluigi	 * exclude table, and %cl to the length of the table itself. After the
267185562Sluigi	 * 'repne scasb' the zero flag is set if we found a match.
268185562Sluigi	 * If not, %di points to the beginning of the 'valid' types,
269185562Sluigi	 * which is what we need for the next check.
270185562Sluigi	 */
271129239Sru		movb (%bx),%al			# Load type
272185562Sluigi		movw $non_bootable_ids,%di	# Lookup tables
273185562Sluigi		movb $(bootable_ids-non_bootable_ids),%cl	# length
274129239Sru		repne				# Exclude
275129239Sru		scasb				#  partition?
276185562Sluigi		je next_entry			# Yes, ignore it
277185562Sluigi	/*
278185562Sluigi	 * Now scan the table of bootable ids, which starts at %di and has
279185562Sluigi	 * length TLEN. On a match, %di points to the element following the
280185562Sluigi	 * match; the corresponding offset to the description is $(TLEN-1)
281185562Sluigi	 * bytes ahead. If we don't find a match, we hit the 'unknown' entry.
282185562Sluigi	 */
283185562Sluigi		movb $(TLEN),%cl		# Number of entries
284139948Speadar		repne				# Locate
285139948Speadar		scasb				#  type
286185562Sluigi	/*
287185562Sluigi	 * Get the matching element in the next array.
288185562Sluigi	 * The byte at $(TLEN-1)(%di) contains the offset of the description
289185562Sluigi	 * string from %di, so we add the number and print the string.
290185562Sluigi	 */
291185562Sluigi		addw $(TLEN-1), %di		# Adjust
292139948Speadar		movb (%di),%cl			# Partition
293129239Sru		addw %cx,%di			#  description
294129239Sru		callw putx			# Display it
295185562Sluigi
296185562Sluiginext_entry:	incw %dx			# Next item
297129239Sru		addb $0x10,%bl			# Next entry
298185562Sluigi		jnc read_entry			# Till done
299185562Sluigi	/*
300185562Sluigi	 * We are past a 256 byte boundary: the partition table is finished.
301185562Sluigi	 * Add one to the drive number and check it is valid.
302185562Sluigi	 * Note that if we started from a floppy, %dl was 0 so we still
303185562Sluigi	 * get an entry for the next drive, which is the first Hard Disk.
304185562Sluigi	 */
305129239Sru		popw %ax			# Drive number
306129239Sru		subb $0x80-0x1,%al		# Does next
307129239Sru		cmpb NHRDRV,%al			#  drive exist? (from BIOS?)
308185562Sluigi		jb print_drive			# Yes
309185562Sluigi	/*
310185562Sluigi	 * If this is the only drive, don't display it as an option.
311185562Sluigi	 */
312129239Sru		decw %ax			# Already drive 0?
313185562Sluigi		jz print_prompt			# Yes
314185562Sluigi	/*
315185562Sluigi	 * If it was illegal or we cycled through them, go back to drive 0.
316185562Sluigi	 */
317129239Sru		xorb %al,%al			# Drive 0
318185562Sluigi	/*
319185562Sluigi	 * Whatever drive we selected, make it an ascii digit and save it
320185562Sluigi	 * back to the "nxtdrv" location in case we want to save it to disk.
321185562Sluigi	 * This digit is also part of the printed drive string, so add 0x80
322185562Sluigi	 * to indicate end of string.
323185562Sluigi	 */
324185562Sluigiprint_drive:	addb $'0'|0x80,%al		# Save next
325129239Sru		movb %al,_NXTDRV(%bp)		#  drive number
326129239Sru		movw $drive,%di			# Display
327129239Sru		callw putx			#  item
328185562Sluigi	/*
329185562Sluigi	 * Menu is complete, display a prompt followed by current selection.
330185562Sluigi	 * 'decw %si' makes the register point to the space after 'Default: '
331185562Sluigi	 * so we do not see an extra CRLF on the screen.
332185562Sluigi	 */
333185562Sluigiprint_prompt:	movw $prompt,%si		# Display
334129239Sru		callw putstr			#  prompt
335129239Sru		movb _OPT(%bp),%dl		# Display
336129239Sru		decw %si			#  default
337129239Sru		callw putkey			#  key
338185562Sluigi		jmp start_input			# Skip beep
339185562Sluigi
340130343Sphk/*
341185562Sluigi * Here we have the code waiting for user input or a timeout.
342130343Sphk */
343185562Sluigibeep:		movb $ASCII_BEL,%al		# Input error, print or beep
344185562Sluigi		callw putchr
345185562Sluigi
346185562Sluigistart_input:
347185562Sluigi	/*
348185562Sluigi	 * Actual Start of input loop.  Take note of time
349185562Sluigi	 */
350185562Sluigi		xorb %ah,%ah			# BIOS: Get
351129239Sru		int $0x1a			#  system time
352129239Sru		movw %dx,%di			# Ticks when
353130343Sphk		addw _TICKS(%bp),%di		#  timeout
354185562Sluigiread_key:
355185562Sluigi	/*
356185562Sluigi	 * Busy loop, looking for keystrokes but keeping one eye on the time.
357185562Sluigi	 */
358128722Sru#ifndef SIO
359129239Sru		movb $0x1,%ah			# BIOS: Check
360129239Sru		int $0x16			#  for keypress
361128722Sru#else /* SIO */
362129239Sru		movb $0x03,%ah			# BIOS: Read COM
363130343Sphk		call bioscom
364129239Sru		testb $0x01,%ah			# Check line status
365185562Sluigi						# (bit 1 indicates input)
366128722Sru#endif /* SIO */
367185562Sluigi		jnz got_key 			# Have input
368185562Sluigi		xorb %ah,%ah			# BIOS: int 0x1a, 00
369185562Sluigi		int $0x1a			#  get system time
370129239Sru		cmpw %di,%dx			# Timeout?
371185562Sluigi		jb read_key			# No
372185562Sluigi
373185562Sluigi	/*
374185562Sluigi	 * Timed out or default selection
375185562Sluigi	 */
376185562Sluigiuse_default:	movb _OPT(%bp),%al		# Load default
377185562Sluigi		jmp check_selection		# Join common code
378185562Sluigi
379185562Sluigi	/*
380185562Sluigi	 * Get the keystroke.
381185562Sluigi	 * ENTER or CR confirm the current selection (same as a timeout).
382185562Sluigi	 * Otherwise convert F1..F6 (or '1'..'6') to 0..5 and check if the
383185562Sluigi	 * selection is valid.
384185562Sluigi	 * The SIO code uses ascii chars, the console code uses scancodes.
385185562Sluigi	 */
386185562Sluigigot_key:
387128722Sru#ifndef SIO
388185562Sluigi		xorb %ah,%ah			# BIOS: int 0x16, 00
389185562Sluigi		int $0x16			#  get keypress
390185562Sluigi		movb %ah,%al			# move scan code to %al
391185562Sluigi		cmpb $KEY_ENTER,%al
392138048Sjhb#else
393129239Sru		movb $0x02,%ah			# BIOS: Receive
394130343Sphk		call bioscom
395185562Sluigi		cmpb $ASCII_CR,%al
396138048Sjhb#endif
397185562Sluigi		je use_default			# enter -> default
398185562Sluigi	/*
399185562Sluigi	 * Check if the key is acceptable, and loop back if not.
400185562Sluigi	 * The console (non-SIO) code looks at scancodes and accepts
401185562Sluigi	 * both F1..F6 and 1..6 (the latter costs 6 bytes of code),
402185562Sluigi	 * relying on the fact that F1..F6 have higher scancodes than 1..6
403185562Sluigi	 * The SIO code only takes 1..6
404185562Sluigi	 */
405185562Sluigi#ifdef SIO /* SIO mode, use ascii values */
406185562Sluigi		subb $'1',%al			# Subtract '1' ascii code
407185562Sluigi#else /*  console mode -- use scancodes */
408185562Sluigi		subb $KEY_F1,%al		/* Subtract F1 scan code */
409185562Sluigi#if !defined(ONLY_F_KEYS)
410185562Sluigi		cmpb $0x5,%al			# F1..F6
411185562Sluigi		jna 3f				# Yes
412129239Sru		subb $(KEY_1 - KEY_F1),%al	# Less #1 scan code
413185562Sluigi	3:
414185562Sluigi#endif /* ONLY_F_KEYS */
415185562Sluigi#endif /* SIO */
416185562Sluigi		cmpb $0x5,%al			# F1..F6 or 1..6 ?
417185562Sluigi#ifdef PXE /* enable PXE/INT18 using F6 */
418185562Sluigi		jne 1f;
419185562Sluigi		int $0x18			# found F6, try INT18
420185562Sluigi	1:
421185562Sluigi#endif /* PXE */
422185562Sluigi		jae beep			# Not in F1..F5, beep
423185562Sluigi
424185562Sluigicheck_selection:
425185562Sluigi	/*
426185562Sluigi	 * We have a selection.  If it's a bad selection go back to complain.
427185562Sluigi	 * The bits in MNUOPT were set when the options were printed.
428185562Sluigi	 * Anything not printed is not an option.
429185562Sluigi	 */
430185562Sluigi		cbtw				# Extend (%ah=0 used later)
431185562Sluigi		btw %ax,_MNUOPT(%bp)	 	# Option enabled?
432185562Sluigi		jnc beep			# No
433185562Sluigi	/*
434185562Sluigi	 * Save the info in the original tables
435185562Sluigi	 * for rewriting to the disk.
436185562Sluigi	 */
437129239Sru		movb %al,_OPT(%bp)		# Save option
438185562Sluigi
439185562Sluigi	/*
440185562Sluigi	 * Make %si and %bx point to the fake partition at LBA 0 (CHS 0:0:1).
441185562Sluigi	 * Because the correct address is already in %bp, just use it.
442185562Sluigi	 * Set %dl with the drive number saved in byte 0.
443185562Sluigi	 * If we have pressed F5 or 5, then this is a good, fake value
444185562Sluigi	 * to present to the next stage boot code.
445185562Sluigi	 */
446185562Sluigi		movw %bp,%si			# Partition for write
447185562Sluigi		movb (%si),%dl			# Drive number, saved above
448129239Sru		movw %si,%bx			# Partition for read
449129239Sru		cmpb $0x4,%al			# F5/#5 pressed?
450185562Sluigi		pushf				# Save results for later
451185562Sluigi		je 1f				# Yes, F5
452185562Sluigi
453185562Sluigi	/*
454185562Sluigi	 * F1..F4 was pressed, so make %bx point to the currently
455185562Sluigi	 * selected partition, and leave the drive number unchanged.
456185562Sluigi	 */
457129239Sru		shlb $0x4,%al			# Point to
458129239Sru		addw $partbl,%ax		#  selected
459129239Sru		xchgw %bx,%ax	 		#  partition
460129239Sru		movb $0x80,(%bx)		# Flag active
461185562Sluigi	/*
462185562Sluigi	 * If not asked to do a write-back (flags 0x40) don't do one.
463185562Sluigi	 * Around the call, save the partition pointer to %bx and
464185562Sluigi	 * restore to %si which is where the next stage expects it.
465185562Sluigi	 */
466185562Sluigi	1:	pushw %bx			# Save
467185562Sluigi		testb $NOUPDATE,_FLAGS(%bp)	# No updates?
468185562Sluigi		jnz 2f				# skip update
469129239Sru		movw $start,%bx			# Data to write
470129239Sru		movb $0x3,%ah			# Write sector
471129239Sru		callw intx13			#  to disk
472185562Sluigi	2:	popw %si			# Restore
473185562Sluigi
474185562Sluigi	/*
475185562Sluigi	 * If going to next drive, replace drive with selected one.
476185562Sluigi	 * Remember to un-ascii it. Hey 0x80 is already set, cool!
477185562Sluigi	 */
478185562Sluigi		popf				# Restore %al test results
479185562Sluigi		jne 3f				# If not F5/#5
480129239Sru		movb _NXTDRV(%bp),%dl		# Next drive
481129239Sru		subb $'0',%dl			#  number
482185562Sluigi	/*
483185562Sluigi	 * Load selected bootsector to the LOAD location in RAM. If read
484185562Sluigi	 * fails or there is no 0x55aa marker, treat it as a bad selection.
485185562Sluigi	 */
486185562Sluigi	3:	movw $LOAD,%bx			# Address for read
487129239Sru		movb $0x2,%ah			# Read sector
488129239Sru		callw intx13			#  from disk
489185562Sluigi		jc beep				# If error
490129239Sru		cmpw $MAGIC,0x1fe(%bx)		# Bootable?
491185562Sluigi		jne beep			# No
492185339Sluigi		pushw %si			# Save ptr to selected part.
493185346Sluigi		callw putn			# Leave some space
494185339Sluigi		popw %si			# Restore, next stage uses it
495129239Sru		jmp *%bx			# Invoke bootstrap
496128722Sru
497128723Sru/*
498128723Sru * Display routines
499185562Sluigi * putkey	prints the option selected in %dl (F1..F5 or 1..5) followed by
500185562Sluigi *		the string at %si
501185562Sluigi * putx:	print the option in %dl followed by the string at %di
502185562Sluigi *		also record the drive as valid.
503185562Sluigi * puts:	print the string at %si followed by a crlf
504185562Sluigi * putn:	print a crlf
505185562Sluigi * putstr:	print the string at %si
506185562Sluigi * putchr:	print the char in al
507128723Sru */
508128722Sruputkey:
509128722Sru#ifndef SIO
510129239Sru		movb $'F',%al			# Display
511129239Sru		callw putchr			#  'F'
512138048Sjhb#endif
513129239Sru		movb $'1',%al			# Prepare
514129239Sru		addb %dl,%al			#  digit
515129239Sru		jmp putstr.1			# Display the rest
516128722Sru
517128723Sru/*
518185562Sluigi * Display the option and record the drive as valid in the options.
519185562Sluigi * That last point is done using the btsw instruction which does
520185562Sluigi * a test and set. We don't care for the test part.
521128723Sru */
522129239Sruputx:		btsw %dx,_MNUOPT(%bp)		# Enable menu option
523129239Sru		movw $item,%si			# Display
524129239Sru		callw putkey			#  key
525129239Sru		movw %di,%si			# Display the rest
526128722Sru
527129239Sruputs:		callw putstr			# Display string
528128722Sru
529129239Sruputn:		movw $crlf,%si			# To next line
530128722Sru
531129239Sruputstr:		lodsb				# Get byte
532129239Sru		testb $0x80,%al 		# End of string?
533129239Sru		jnz putstr.2			# Yes
534129239Sruputstr.1:	callw putchr			# Display char
535129239Sru		jmp putstr			# Continue
536129239Sruputstr.2:	andb $~0x80,%al 		# Clear MSB
537128722Sru
538185562Sluigiputchr:
539130343Sphk#ifndef SIO
540129239Sru		pushw %bx			# Save
541129239Sru		movw $0x7,%bx	 		# Page:attribute
542129239Sru		movb $0xe,%ah			# BIOS: Display
543129239Sru		int $0x10			#  character
544129239Sru		popw %bx			# Restore
545128722Sru#else /* SIO */
546130343Sphk		movb $0x01,%ah			# BIOS: Send
547130343Sphkbioscom:
548129239Sru		pushw %dx			# Save
549129239Sru		xorw %dx,%dx 			# Use COM1
550129239Sru		int $0x14			#  Character
551129239Sru		popw %dx			# Restore
552185562Sluigi#endif /* SIO */
553130343Sphk		retw				# To caller
554128722Sru
555128723Sru/* One-sector disk I/O routine */
556128722Sru
557185562Sluigi/*
558185562Sluigi * %dl: drive, %si partition entry, %es:%bx transfer buffer.
559185562Sluigi * Load the CHS values and possibly the LBA address from the block
560185562Sluigi * at %si, and use the appropriate method to load the sector.
561185562Sluigi * Don't use packet mode for a floppy.
562185562Sluigi */
563185562Sluigiintx13:						# Prepare CHS parameters
564185562Sluigi		movb 0x1(%si),%dh		# Load head
565129239Sru		movw 0x2(%si),%cx		# Load cylinder:sector
566129239Sru		movb $0x1,%al			# Sector count
567129239Sru		pushw %si			# Save
568129239Sru		movw %sp,%di			# Save
569185562Sluigi#ifndef CHECK_DRIVE				/* floppy support */
570185562Sluigi		testb %dl, %dl			# is this a floppy ?
571185562Sluigi		jz 1f				# Yes, use CHS mode
572185562Sluigi#endif
573185562Sluigi		testb $USEPACKET,_FLAGS(%bp)	# Use packet interface?
574185562Sluigi		jz 1f				# No
575129239Sru		pushl $0x0			# Set the
576129239Sru		pushl 0x8(%si)			# LBA address
577129239Sru		pushw %es			# Set the transfer
578129239Sru		pushw %bx			#  buffer address
579129239Sru		push  $0x1			# Block count
580129239Sru		push  $0x10			# Packet size
581129239Sru		movw %sp,%si			# Packet pointer
582129239Sru		decw %ax			# Verify off
583129239Sru		orb $0x40,%ah			# Use disk packet
584185562Sluigi	1:	int $0x13			# BIOS: Disk I/O
585129239Sru		movw %di,%sp			# Restore
586129239Sru		popw %si			# Restore
587129239Sru		retw				# To caller
588128722Sru
589185562Sluigi/*
590185562Sluigi * Various menu strings. 'item' goes after 'prompt' to save space.
591185562Sluigi * Also use shorter versions to make room for the PXE/INT18 code.
592185562Sluigi */
593185562Sluigi#ifdef PXE
594185562Sluigiprompt:		.ascii "\nBoot:"
595185562Sluigiitem:		.ascii " ";	     .byte ' '|0x80
596185562Sluigi#else
597185562Sluigiprompt:		.ascii "\nDefault:"
598128722Sruitem:		.ascii "  ";	     .byte ' '|0x80
599185562Sluigi#endif
600128722Srucrlf:		.ascii "\r";	     .byte '\n'|0x80
601128722Sru
602128723Sru/* Partition type tables */
603128722Sru
604185562Sluiginon_bootable_ids:
605185562Sluigi	/*
606185562Sluigi	 * These entries identify invalid or NON BOOT types and partitions.
607185562Sluigi	 * 0: empty, 5: DOS3 ext. partition, 0xf: WIN95 ext partition
608185562Sluigi	 */
609128722Sru		.byte 0x0, 0x5, 0xf
610185562Sluigibootable_ids:
611185562Sluigi	/*
612185562Sluigi	 * These values indicate bootable types we know the names of.
613185562Sluigi	 * 1: FAT12, 4: FAT16 <32M, 6: FAT16 > 32M, 7: NTFS
614185562Sluigi	 * 0xb: FAT32, 0xc: FAT32-LBA, 0xe: FAT16-LBA,
615185562Sluigi	 * 0x83: linux, 0xa5: FreeBSD, 0xa6: netbsd, 0xa9:openbsd
616185562Sluigi	 */
617141540Sdes		.byte 0x1, 0x6, 0x7, 0xb, 0xc, 0xe, 0x83
618185562Sluigi		.byte 0xa5, 0xa6, 0xa9, 0x4
619185562Sluigidesc_ofs:
620185562Sluigi	/*
621185562Sluigi	 * Offsets that match the known types above, used to point to the
622185562Sluigi	 * actual partition name. The last entry must point to os_misc,
623185562Sluigi	 * which is used for non-matching names.
624185562Sluigi	 */
625185562Sluigi		.byte os_dos-.			# 1, DOS
626185562Sluigi		.byte os_dos-.			# 6, DOS/WIN
627185562Sluigi		.byte os_win-.			# 7, Windows
628185562Sluigi		.byte os_win-.			# 11, Windows
629185562Sluigi		.byte os_win-.			# 12, Windows
630185562Sluigi		.byte os_win-.			# 14, Windows
631185562Sluigi		.byte os_linux-.		# 131, Linux
632185562Sluigi		.byte os_freebsd-.		# 165, FreeBSD
633185562Sluigi		.byte os_bsd-.			# 166, OpenBSD
634185562Sluigi		.byte os_bsd-.			# 169, NetBSD
635185562Sluigi		.byte os_dos-.			# 4, DOS
636139948Speadar		.byte os_misc-. 		# Unknown
637185562Sluigi
638185562Sluigi	/*
639185562Sluigi	 * And here are the strings themselves. The last byte of
640185562Sluigi	 * the string has bit 7 set.
641185562Sluigi	 */
642185562Sluigios_misc:	.byte '?'|0x80
643185562Sluigios_dos:
644185562Sluigi#if !defined(TEST)	/* DOS string only if room */
645185562Sluigi		.ascii "DO";   .byte 'S'|0x80
646185562Sluigi#endif
647185562Sluigios_win:		.ascii "WI";   .byte 'N'|0x80
648128722Sruos_linux:	.ascii "Linu"; .byte 'x'|0x80
649128722Sruos_freebsd:	.ascii "Free"
650128722Sruos_bsd:		.ascii "BS";   .byte 'D'|0x80
651128722Sru
652185562Sluigi		.org B0_OFF,0x90
653128723Sru/*
654185562Sluigi * The boot0 version 1.0 parameter table.
655185562Sluigi * Do not move it nor change the "Drive " string, boot0cfg
656185562Sluigi * uses its offset and content to identify the boot sector.
657185562Sluigi * The other fields are sometimes changed before writing back to the drive
658128723Sru * Be especially careful that nxtdrv: must come after drive:, as it
659128723Sru * is part of the same string.
660128723Sru */
661128722Srudrive:		.ascii "Drive "
662129239Srunxtdrv:		.byte 0x0			# Next drive number
663129239Sruopt:		.byte 0x0			# Option
664185562Sluigisetdrv_num:	.byte 0x80			# Drive to force
665129239Sruflags:		.byte FLAGS			# Flags
666129239Sruticks:		.word TICKS			# Delay
667128722Sru
668185562Sluigi		.org PRT_OFF
669128723Sru/*
670137298Skeramida * Here is the 64 byte partition table that fdisk would fiddle with.
671128723Sru */
672129239Srupartbl:		.fill 0x40,0x1,0x0		# Partition table
673129239Sru		.word MAGIC			# Magic number
674185562Sluigi		.org 0x200			# again, safety check
675185562Sluigiendblock:
676