boot0.S revision 185579
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 185579 2008-12-03 14:53:59Z 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
33185579Sluigi#ifdef VOLUME_SERIAL	/* support Volume serial number */
34185579Sluigi#define B0_BASE	0x1ae	/* move the internal data area */
35185579Sluigi#define SAVE_MEMORY
36185579Sluigi#else
37185579Sluigi#define B0_BASE	0x1b2
38185579Sluigi#endif
39185579Sluigi
40185562Sluigi#ifdef TEST		/* enable some test code */
41185579Sluigi#define SAVE_MEMORY
42185579Sluigi#define SAVE_MORE_MEMORY
43185562Sluigi#endif
44185562Sluigi
45185562Sluigi/*
46185562Sluigi * Note - this code uses many tricks to save space and fit in one sector.
47185562Sluigi * This includes using side effects of certain instructions, reusing
48185562Sluigi * register values from previous operations, etc.
49185562Sluigi * Be extremely careful when changing the code, even for simple things.
50185562Sluigi */
51185562Sluigi
52185562Sluigi/*
53185562Sluigi *		BOOT BLOCK STRUCTURE
54185562Sluigi *
55185562Sluigi * This code implements a Master Boot Record (MBR) for an Intel/PC disk.
56185562Sluigi * It is 512 bytes long and it is normally loaded by the BIOS (or another
57185562Sluigi * bootloader) at 0:0x7c00. This code depends on %cs:%ip being 0:0x7c00
58185562Sluigi *
59185562Sluigi * The initial chunk of instructions is used as a signature by external
60185562Sluigi * tools (e.g. boot0cfg) which can manipulate the block itself.
61185562Sluigi *
62185562Sluigi * The area at offset 0x1b2 contains a magic string ('Drive '), also
63185562Sluigi * used as a signature to detect the block, and some variables that can
64185562Sluigi * be updated by boot0cfg (and optionally written back to the disk).
65185562Sluigi * These variables control the operation of the bootloader itself,
66185562Sluigi * e.g. which partitions to enable, the timeout, the use of LBA
67185562Sluigi * (called 'packet') or CHS mode, whether to force a drive number,
68185562Sluigi * and whether to write back the user's selection back to disk.
69185562Sluigi *
70185562Sluigi * As in every Master Boot Record, the partition table is at 0x1be,
71185562Sluigi * made of four 16-byte entries each containing:
72185562Sluigi *
73185562Sluigi *   OFF SIZE	DESCRIPTION
74185562Sluigi *    0	  1	status (0x80: bootable, 0: non bootable)
75185562Sluigi *    1	  3	start sector CHS
76185562Sluigi *		   8:head, 6:sector, 2:cyl bit 9..8, 8:cyl bit 7..0
77185562Sluigi *    4   1	partition type
78185562Sluigi *    5   3	end sector CHS
79185562Sluigi *    8   4	LBA of first sector
80185562Sluigi *   12   4	partition size in sectors
81185562Sluigi *
82185562Sluigi * and followed by the two bytes 0x55, 0xAA (MBR signature).
83185562Sluigi */
84185562Sluigi
85185562Sluigi
86185562Sluigi/*
87185562Sluigi *		BOOT BLOCK OPERATION
88185562Sluigi *
89185562Sluigi * On entry, the registers contain the following values:
90185562Sluigi *
91185562Sluigi *	%cs:%ip	0:0x7c00
92185562Sluigi *	%dl	drive number (0x80, 0x81, ... )
93185562Sluigi *	%si	pointer to the partition table from which we were loaded.
94185562Sluigi *		Some boot code (e.g. syslinux) use this info to relocate
95185562Sluigi *		themselves, so we want to pass a valid one to the next stage.
96185562Sluigi *		NOTE: the use of %is is not a standard.
97185562Sluigi *
98185562Sluigi * This boot block first relocates itself at a different address (0:0x600),
99185562Sluigi * to free the space at 0:0x7c00 for the next stage boot block.
100185562Sluigi *
101185562Sluigi * It then initializes some memory at 0:0x800 and above (pointed by %bp)
102185562Sluigi * to store the original drive number (%dl) passed to us, and to construct a
103185562Sluigi * fake partition entry. The latter is used by the disk I/O routine and,
104185562Sluigi * in some cases, passed in %si to the next stage boot code.
105185562Sluigi *
106185562Sluigi * The variables at 0x1b2 are accessed as negative offsets from %bp.
107185562Sluigi *
108185562Sluigi * After the relocation, the code scans the partition table printing
109185562Sluigi * out enabled partition or disks, and waits for user input.
110185562Sluigi *
111185562Sluigi * When a partition is selected, or a timeout expires, the currently
112185562Sluigi * selected partition is used to load the next stage boot code,
113185562Sluigi * %dl and %si are set appropriately as when we were called, and
114185562Sluigi * control is transferred to the newly loaded code at 0:0x7c00.
115185562Sluigi */
116185562Sluigi
117185562Sluigi/*
118185562Sluigi *	CONSTANTS
119185562Sluigi *
120185562Sluigi * NHRDRV is the address in segment 0 where the BIOS writes the
121185562Sluigi *	total number of hard disks in the system.
122185562Sluigi * LOAD is the original load address and cannot be changed.
123185562Sluigi * ORIGIN is the relocation address. If you change it, you also need
124185562Sluigi * 	to change the value passed to the linker in the Makefile
125185562Sluigi * PRT_OFF is the location of the partition table (from the MBR standard).
126185562Sluigi * B0_OFF is the location of the data area, known to boot0cfg so
127185579Sluigi *	it cannot be changed. Computed as a negative offset from 0x200
128185562Sluigi * MAGIC is the signature of a boot block.
129185562Sluigi */
130185562Sluigi
131129239Sru		.set NHRDRV,0x475		# Number of hard drives
132129239Sru		.set ORIGIN,0x600		# Execution address
133129239Sru		.set LOAD,0x7c00		# Load address
134128722Sru
135129239Sru		.set PRT_OFF,0x1be		# Partition table
136185579Sluigi		.set B0_OFF,(B0_BASE-0x200)	# Offset of boot0 data
137128722Sru
138129239Sru		.set MAGIC,0xaa55		# Magic: bootable
139128722Sru
140129239Sru		.set KEY_ENTER,0x1c		# Enter key scan code
141129239Sru		.set KEY_F1,0x3b		# F1 key scan code
142129239Sru		.set KEY_1,0x02			# #1 key scan code
143128722Sru
144185562Sluigi		.set ASCII_BEL,'#'		# ASCII code for <BEL>
145129239Sru		.set ASCII_CR,0x0D		# ASCII code for <CR>
146128722Sru
147128723Sru/*
148185562Sluigi * Offsets of variables in the block at B0_OFF, and in the volatile
149185562Sluigi * data area, computed as displacement from %bp.
150185562Sluigi * We need to define them as constant as the assembler cannot
151185562Sluigi * compute them in its single pass.
152128723Sru */
153185579Sluigi		.set _NXTDRV,	B0_OFF+6	# Next drive
154185579Sluigi		.set _OPT,	B0_OFF+7	# Default option
155185579Sluigi		.set _SETDRV,	B0_OFF+8	# Drive to force
156185579Sluigi		.set _FLAGS,	B0_OFF+9	# Flags
157185562Sluigi		.set SETDRV,	0x20		# the 'setdrv' flag
158185562Sluigi		.set NOUPDATE,	0x40		# the 'noupdate' flag
159185562Sluigi		.set USEPACKET,	0x80		# the 'packet' flag
160185579Sluigi
161185579Sluigi	/* ticks is at a fixed position */
162185579Sluigi		.set _TICKS,	(PRT_OFF - 0x200 - 2)	# Timeout ticks
163185562Sluigi		.set _MNUOPT, 0x10		# Saved menu entries
164128722Sru
165185562Sluigi		.set TLEN, (desc_ofs - bootable_ids)	# size of bootable ids
166129239Sru		.globl start			# Entry point
167129239Sru		.code16				# This runs in real mode
168128722Sru
169128723Sru/*
170185562Sluigi * 	MAIN ENTRY POINT
171128723Sru * Initialise segments and registers to known values.
172128723Sru * segments start at 0.
173128723Sru * The stack is immediately below the address we were loaded to.
174185562Sluigi * NOTE: the initial section of the code (up to movw $LOAD,%sp)
175185562Sluigi * is used by boot0cfg, together with the 'Drive ' string and
176185562Sluigi * the 0x55, 0xaa at the end, as an identifier for version 1.0
177185562Sluigi * of the boot code. Do not change it.
178185562Sluigi * In version 1.0 the parameter table (_NEXTDRV etc) is at 0x1b9
179128723Sru */
180129239Srustart:		cld				# String ops inc
181129239Sru		xorw %ax,%ax			# Zero
182129239Sru		movw %ax,%es			# Address
183129239Sru		movw %ax,%ds			#  data
184129239Sru		movw %ax,%ss			# Set up
185129239Sru		movw $LOAD,%sp			#  stack
186128722Sru
187185562Sluigi	/*
188185562Sluigi	 * Copy this code to the address it was linked for, 0x600 by default.
189185562Sluigi	 */
190129239Sru		movw %sp,%si			# Source
191129239Sru		movw $start,%di			# Destination
192129239Sru		movw $0x100,%cx			# Word count
193129239Sru		rep				# Relocate
194129239Sru		movsw				#  code
195185562Sluigi	/*
196185562Sluigi	 * After the code, (i.e. at %di+0, 0x800) create a partition entry,
197185562Sluigi	 * initialized to LBA 0 / CHS 0:0:1.
198185562Sluigi	 * Set %bp to point to the partition and also, with negative offsets,
199185562Sluigi	 * to the variables embedded in the bootblock (nextdrv and so on).
200185562Sluigi	 */
201129239Sru		movw %di,%bp			# Address variables
202129239Sru		movb $0x8,%cl			# Words to clear
203129239Sru		rep				# Zero
204129239Sru		stosw				#  them
205185562Sluigi		incb -0xe(%di)			# Set the S field to 1
206128722Sru
207185562Sluigi		jmp main-LOAD+ORIGIN		# Jump to relocated code
208185562Sluigi
209128722Srumain:
210138048Sjhb#if defined(SIO) && COMSPEED != 0
211185562Sluigi	/*
212185562Sluigi	 * Init the serial port. bioscom preserves the driver number in DX.
213185562Sluigi	 */
214167914Sthomas		movw $COMSPEED,%ax		# defined by Makefile
215130632Sphk		callw bioscom
216138048Sjhb#endif
217185562Sluigi
218185562Sluigi	/*
219185562Sluigi	 * If the 'setdrv' flag is set in the boot sector, use the drive
220185562Sluigi	 * number from the boot sector at 'setdrv_num'.
221185562Sluigi	 * Optionally, do the same if the BIOS gives us an invalid number
222185562Sluigi	 * (note though that the override prevents booting from a floppy
223185562Sluigi	 * or a ZIP/flash drive in floppy emulation).
224185562Sluigi	 * The test costs 4 bytes of code so it is disabled by default.
225185562Sluigi	 */
226185562Sluigi		testb $SETDRV,_FLAGS(%bp)	# Set drive number?
227185562Sluigi#ifndef CHECK_DRIVE	/* disable drive checks */
228185562Sluigi		jz save_curdrive		# no, use the default
229185562Sluigi#else
230185562Sluigi		jnz disable_update		# Yes
231129239Sru		testb %dl,%dl			# Drive number valid?
232185562Sluigi		js save_curdrive		# Possibly (0x80 set)
233185562Sluigi#endif
234185562Sluigi	/*
235185562Sluigi	 * Disable updates if the drive number is forced.
236185562Sluigi	 */
237185562Sluigidisable_update:	orb $NOUPDATE,_FLAGS(%bp)	# Disable updates
238185562Sluigi		movb _SETDRV(%bp),%dl		# Use stored drive number
239185562Sluigi
240185562Sluigi	/*
241185562Sluigi	 * Whatever drive we decided to use, store it at (%bp). The byte
242185562Sluigi	 * is normally used for the state of the partition (0x80 or 0x00),
243185562Sluigi	 * but we abuse it as it is very convenient to access at offset 0.
244185562Sluigi	 * The value is read back after 'check_selection'
245185562Sluigi	 */
246185562Sluigisave_curdrive:	movb %dl, (%bp)			# Save drive number
247185562Sluigi		pushw %dx			# Also in the stack
248185562Sluigi#ifdef	TEST	/* test code, print internal bios drive */
249185562Sluigi		rolb $1, %dl
250185562Sluigi		movw $drive, %si
251185562Sluigi		call putkey
252185562Sluigi#endif
253185562Sluigi		callw putn			# Print a newline
254185562Sluigi	/*
255185562Sluigi	 * Start out with a pointer to the 4th byte of the first table entry
256185562Sluigi	 * so that after 4 iterations it's beyond the end of the sector
257185562Sluigi	 * and beyond a 256 byte boundary. We use the latter trick to check for
258185562Sluigi	 * end of the loop without using an extra register (see start.5).
259185562Sluigi	 */
260129239Sru		movw $(partbl+0x4),%bx		# Partition table (+4)
261129239Sru		xorw %dx,%dx			# Item number
262185562Sluigi
263185562Sluigi	/*
264185562Sluigi	 * Loop around on the partition table, printing values until we
265185562Sluigi	 * pass a 256 byte boundary.
266185562Sluigi	 */
267185562Sluigiread_entry:	movb %ch,-0x4(%bx)		# Zero active flag (ch == 0)
268129239Sru		btw %dx,_FLAGS(%bp)		# Entry enabled?
269185562Sluigi		jnc next_entry			# No
270129239Sru		movb (%bx),%al			# Load type
271185579Sluigi		test %al, %al			# skip empty partition
272185579Sluigi		jz next_entry
273185562Sluigi	/*
274185579Sluigi	 * Scan the table of bootable ids, which starts at %di and has
275185562Sluigi	 * length TLEN. On a match, %di points to the element following the
276185562Sluigi	 * match; the corresponding offset to the description is $(TLEN-1)
277185562Sluigi	 * bytes ahead. If we don't find a match, we hit the 'unknown' entry.
278185562Sluigi	 */
279185579Sluigi		movw $bootable_ids,%di		# Lookup tables
280185562Sluigi		movb $(TLEN),%cl		# Number of entries
281139948Speadar		repne				# Locate
282139948Speadar		scasb				#  type
283185562Sluigi	/*
284185562Sluigi	 * Get the matching element in the next array.
285185562Sluigi	 * The byte at $(TLEN-1)(%di) contains the offset of the description
286185562Sluigi	 * string from %di, so we add the number and print the string.
287185562Sluigi	 */
288185562Sluigi		addw $(TLEN-1), %di		# Adjust
289139948Speadar		movb (%di),%cl			# Partition
290129239Sru		addw %cx,%di			#  description
291129239Sru		callw putx			# Display it
292185562Sluigi
293185562Sluiginext_entry:	incw %dx			# Next item
294129239Sru		addb $0x10,%bl			# Next entry
295185562Sluigi		jnc read_entry			# Till done
296185562Sluigi	/*
297185562Sluigi	 * We are past a 256 byte boundary: the partition table is finished.
298185562Sluigi	 * Add one to the drive number and check it is valid.
299185562Sluigi	 * Note that if we started from a floppy, %dl was 0 so we still
300185562Sluigi	 * get an entry for the next drive, which is the first Hard Disk.
301185562Sluigi	 */
302129239Sru		popw %ax			# Drive number
303129239Sru		subb $0x80-0x1,%al		# Does next
304129239Sru		cmpb NHRDRV,%al			#  drive exist? (from BIOS?)
305185562Sluigi		jb print_drive			# Yes
306185562Sluigi	/*
307185562Sluigi	 * If this is the only drive, don't display it as an option.
308185562Sluigi	 */
309129239Sru		decw %ax			# Already drive 0?
310185562Sluigi		jz print_prompt			# Yes
311185562Sluigi	/*
312185562Sluigi	 * If it was illegal or we cycled through them, go back to drive 0.
313185562Sluigi	 */
314129239Sru		xorb %al,%al			# Drive 0
315185562Sluigi	/*
316185562Sluigi	 * Whatever drive we selected, make it an ascii digit and save it
317185562Sluigi	 * back to the "nxtdrv" location in case we want to save it to disk.
318185562Sluigi	 * This digit is also part of the printed drive string, so add 0x80
319185562Sluigi	 * to indicate end of string.
320185562Sluigi	 */
321185562Sluigiprint_drive:	addb $'0'|0x80,%al		# Save next
322129239Sru		movb %al,_NXTDRV(%bp)		#  drive number
323129239Sru		movw $drive,%di			# Display
324129239Sru		callw putx			#  item
325185562Sluigi	/*
326185562Sluigi	 * Menu is complete, display a prompt followed by current selection.
327185562Sluigi	 * 'decw %si' makes the register point to the space after 'Default: '
328185562Sluigi	 * so we do not see an extra CRLF on the screen.
329185562Sluigi	 */
330185562Sluigiprint_prompt:	movw $prompt,%si		# Display
331129239Sru		callw putstr			#  prompt
332129239Sru		movb _OPT(%bp),%dl		# Display
333129239Sru		decw %si			#  default
334129239Sru		callw putkey			#  key
335185562Sluigi		jmp start_input			# Skip beep
336185562Sluigi
337130343Sphk/*
338185562Sluigi * Here we have the code waiting for user input or a timeout.
339130343Sphk */
340185562Sluigibeep:		movb $ASCII_BEL,%al		# Input error, print or beep
341185562Sluigi		callw putchr
342185562Sluigi
343185562Sluigistart_input:
344185562Sluigi	/*
345185562Sluigi	 * Actual Start of input loop.  Take note of time
346185562Sluigi	 */
347185562Sluigi		xorb %ah,%ah			# BIOS: Get
348129239Sru		int $0x1a			#  system time
349129239Sru		movw %dx,%di			# Ticks when
350130343Sphk		addw _TICKS(%bp),%di		#  timeout
351185562Sluigiread_key:
352185562Sluigi	/*
353185562Sluigi	 * Busy loop, looking for keystrokes but keeping one eye on the time.
354185562Sluigi	 */
355128722Sru#ifndef SIO
356129239Sru		movb $0x1,%ah			# BIOS: Check
357129239Sru		int $0x16			#  for keypress
358128722Sru#else /* SIO */
359129239Sru		movb $0x03,%ah			# BIOS: Read COM
360130343Sphk		call bioscom
361129239Sru		testb $0x01,%ah			# Check line status
362185562Sluigi						# (bit 1 indicates input)
363128722Sru#endif /* SIO */
364185562Sluigi		jnz got_key 			# Have input
365185562Sluigi		xorb %ah,%ah			# BIOS: int 0x1a, 00
366185562Sluigi		int $0x1a			#  get system time
367129239Sru		cmpw %di,%dx			# Timeout?
368185562Sluigi		jb read_key			# No
369185562Sluigi
370185562Sluigi	/*
371185562Sluigi	 * Timed out or default selection
372185562Sluigi	 */
373185562Sluigiuse_default:	movb _OPT(%bp),%al		# Load default
374185562Sluigi		jmp check_selection		# Join common code
375185562Sluigi
376185562Sluigi	/*
377185562Sluigi	 * Get the keystroke.
378185562Sluigi	 * ENTER or CR confirm the current selection (same as a timeout).
379185562Sluigi	 * Otherwise convert F1..F6 (or '1'..'6') to 0..5 and check if the
380185562Sluigi	 * selection is valid.
381185562Sluigi	 * The SIO code uses ascii chars, the console code uses scancodes.
382185562Sluigi	 */
383185562Sluigigot_key:
384128722Sru#ifndef SIO
385185562Sluigi		xorb %ah,%ah			# BIOS: int 0x16, 00
386185562Sluigi		int $0x16			#  get keypress
387185562Sluigi		movb %ah,%al			# move scan code to %al
388185562Sluigi		cmpb $KEY_ENTER,%al
389138048Sjhb#else
390129239Sru		movb $0x02,%ah			# BIOS: Receive
391130343Sphk		call bioscom
392185562Sluigi		cmpb $ASCII_CR,%al
393138048Sjhb#endif
394185562Sluigi		je use_default			# enter -> default
395185562Sluigi	/*
396185562Sluigi	 * Check if the key is acceptable, and loop back if not.
397185562Sluigi	 * The console (non-SIO) code looks at scancodes and accepts
398185562Sluigi	 * both F1..F6 and 1..6 (the latter costs 6 bytes of code),
399185562Sluigi	 * relying on the fact that F1..F6 have higher scancodes than 1..6
400185562Sluigi	 * The SIO code only takes 1..6
401185562Sluigi	 */
402185562Sluigi#ifdef SIO /* SIO mode, use ascii values */
403185562Sluigi		subb $'1',%al			# Subtract '1' ascii code
404185562Sluigi#else /*  console mode -- use scancodes */
405185562Sluigi		subb $KEY_F1,%al		/* Subtract F1 scan code */
406185562Sluigi#if !defined(ONLY_F_KEYS)
407185562Sluigi		cmpb $0x5,%al			# F1..F6
408185562Sluigi		jna 3f				# Yes
409129239Sru		subb $(KEY_1 - KEY_F1),%al	# Less #1 scan code
410185562Sluigi	3:
411185562Sluigi#endif /* ONLY_F_KEYS */
412185562Sluigi#endif /* SIO */
413185562Sluigi		cmpb $0x5,%al			# F1..F6 or 1..6 ?
414185562Sluigi#ifdef PXE /* enable PXE/INT18 using F6 */
415185562Sluigi		jne 1f;
416185562Sluigi		int $0x18			# found F6, try INT18
417185562Sluigi	1:
418185562Sluigi#endif /* PXE */
419185562Sluigi		jae beep			# Not in F1..F5, beep
420185562Sluigi
421185562Sluigicheck_selection:
422185562Sluigi	/*
423185562Sluigi	 * We have a selection.  If it's a bad selection go back to complain.
424185562Sluigi	 * The bits in MNUOPT were set when the options were printed.
425185562Sluigi	 * Anything not printed is not an option.
426185562Sluigi	 */
427185562Sluigi		cbtw				# Extend (%ah=0 used later)
428185562Sluigi		btw %ax,_MNUOPT(%bp)	 	# Option enabled?
429185562Sluigi		jnc beep			# No
430185562Sluigi	/*
431185562Sluigi	 * Save the info in the original tables
432185562Sluigi	 * for rewriting to the disk.
433185562Sluigi	 */
434129239Sru		movb %al,_OPT(%bp)		# Save option
435185562Sluigi
436185562Sluigi	/*
437185562Sluigi	 * Make %si and %bx point to the fake partition at LBA 0 (CHS 0:0:1).
438185562Sluigi	 * Because the correct address is already in %bp, just use it.
439185562Sluigi	 * Set %dl with the drive number saved in byte 0.
440185562Sluigi	 * If we have pressed F5 or 5, then this is a good, fake value
441185562Sluigi	 * to present to the next stage boot code.
442185562Sluigi	 */
443185562Sluigi		movw %bp,%si			# Partition for write
444185562Sluigi		movb (%si),%dl			# Drive number, saved above
445129239Sru		movw %si,%bx			# Partition for read
446129239Sru		cmpb $0x4,%al			# F5/#5 pressed?
447185562Sluigi		pushf				# Save results for later
448185562Sluigi		je 1f				# Yes, F5
449185562Sluigi
450185562Sluigi	/*
451185562Sluigi	 * F1..F4 was pressed, so make %bx point to the currently
452185562Sluigi	 * selected partition, and leave the drive number unchanged.
453185562Sluigi	 */
454129239Sru		shlb $0x4,%al			# Point to
455129239Sru		addw $partbl,%ax		#  selected
456129239Sru		xchgw %bx,%ax	 		#  partition
457129239Sru		movb $0x80,(%bx)		# Flag active
458185562Sluigi	/*
459185562Sluigi	 * If not asked to do a write-back (flags 0x40) don't do one.
460185562Sluigi	 * Around the call, save the partition pointer to %bx and
461185562Sluigi	 * restore to %si which is where the next stage expects it.
462185562Sluigi	 */
463185562Sluigi	1:	pushw %bx			# Save
464185562Sluigi		testb $NOUPDATE,_FLAGS(%bp)	# No updates?
465185562Sluigi		jnz 2f				# skip update
466129239Sru		movw $start,%bx			# Data to write
467129239Sru		movb $0x3,%ah			# Write sector
468129239Sru		callw intx13			#  to disk
469185562Sluigi	2:	popw %si			# Restore
470185562Sluigi
471185562Sluigi	/*
472185562Sluigi	 * If going to next drive, replace drive with selected one.
473185562Sluigi	 * Remember to un-ascii it. Hey 0x80 is already set, cool!
474185562Sluigi	 */
475185562Sluigi		popf				# Restore %al test results
476185562Sluigi		jne 3f				# If not F5/#5
477129239Sru		movb _NXTDRV(%bp),%dl		# Next drive
478129239Sru		subb $'0',%dl			#  number
479185562Sluigi	/*
480185562Sluigi	 * Load selected bootsector to the LOAD location in RAM. If read
481185562Sluigi	 * fails or there is no 0x55aa marker, treat it as a bad selection.
482185562Sluigi	 */
483185562Sluigi	3:	movw $LOAD,%bx			# Address for read
484129239Sru		movb $0x2,%ah			# Read sector
485129239Sru		callw intx13			#  from disk
486185562Sluigi		jc beep				# If error
487129239Sru		cmpw $MAGIC,0x1fe(%bx)		# Bootable?
488185562Sluigi		jne beep			# No
489185339Sluigi		pushw %si			# Save ptr to selected part.
490185346Sluigi		callw putn			# Leave some space
491185339Sluigi		popw %si			# Restore, next stage uses it
492129239Sru		jmp *%bx			# Invoke bootstrap
493128722Sru
494128723Sru/*
495128723Sru * Display routines
496185562Sluigi * putkey	prints the option selected in %dl (F1..F5 or 1..5) followed by
497185562Sluigi *		the string at %si
498185562Sluigi * putx:	print the option in %dl followed by the string at %di
499185562Sluigi *		also record the drive as valid.
500185562Sluigi * putn:	print a crlf
501185562Sluigi * putstr:	print the string at %si
502185562Sluigi * putchr:	print the char in al
503128723Sru */
504128722Sru
505128723Sru/*
506185562Sluigi * Display the option and record the drive as valid in the options.
507185562Sluigi * That last point is done using the btsw instruction which does
508185562Sluigi * a test and set. We don't care for the test part.
509128723Sru */
510129239Sruputx:		btsw %dx,_MNUOPT(%bp)		# Enable menu option
511129239Sru		movw $item,%si			# Display
512129239Sru		callw putkey			#  key
513129239Sru		movw %di,%si			# Display the rest
514185579Sluigi		callw putstr			# Display string
515128722Sru
516129239Sruputn:		movw $crlf,%si			# To next line
517185579Sluigi		jmp putstr
518128722Sru
519185579Sluigiputkey:
520185579Sluigi#ifndef SIO
521185579Sluigi		movb $'F',%al			# Display
522185579Sluigi		callw putchr			#  'F'
523185579Sluigi#endif
524185579Sluigi		movb $'1',%al			# Prepare
525185579Sluigi		addb %dl,%al			#  digit
526185579Sluigi
527185579Sluigiputstr.1:	callw putchr			# Display char
528129239Sruputstr:		lodsb				# Get byte
529129239Sru		testb $0x80,%al 		# End of string?
530185579Sluigi		jz putstr.1			# No
531185579Sluigi		andb $~0x80,%al 		# Clear MSB then print last
532128722Sru
533185562Sluigiputchr:
534130343Sphk#ifndef SIO
535129239Sru		pushw %bx			# Save
536129239Sru		movw $0x7,%bx	 		# Page:attribute
537129239Sru		movb $0xe,%ah			# BIOS: Display
538129239Sru		int $0x10			#  character
539129239Sru		popw %bx			# Restore
540128722Sru#else /* SIO */
541130343Sphk		movb $0x01,%ah			# BIOS: Send
542130343Sphkbioscom:
543129239Sru		pushw %dx			# Save
544129239Sru		xorw %dx,%dx 			# Use COM1
545129239Sru		int $0x14			#  Character
546129239Sru		popw %dx			# Restore
547185562Sluigi#endif /* SIO */
548130343Sphk		retw				# To caller
549128722Sru
550128723Sru/* One-sector disk I/O routine */
551128722Sru
552185562Sluigi/*
553185562Sluigi * %dl: drive, %si partition entry, %es:%bx transfer buffer.
554185562Sluigi * Load the CHS values and possibly the LBA address from the block
555185562Sluigi * at %si, and use the appropriate method to load the sector.
556185562Sluigi * Don't use packet mode for a floppy.
557185562Sluigi */
558185562Sluigiintx13:						# Prepare CHS parameters
559185562Sluigi		movb 0x1(%si),%dh		# Load head
560129239Sru		movw 0x2(%si),%cx		# Load cylinder:sector
561129239Sru		movb $0x1,%al			# Sector count
562129239Sru		pushw %si			# Save
563129239Sru		movw %sp,%di			# Save
564185562Sluigi#ifndef CHECK_DRIVE				/* floppy support */
565185562Sluigi		testb %dl, %dl			# is this a floppy ?
566185562Sluigi		jz 1f				# Yes, use CHS mode
567185562Sluigi#endif
568185562Sluigi		testb $USEPACKET,_FLAGS(%bp)	# Use packet interface?
569185562Sluigi		jz 1f				# No
570129239Sru		pushl $0x0			# Set the
571129239Sru		pushl 0x8(%si)			# LBA address
572129239Sru		pushw %es			# Set the transfer
573129239Sru		pushw %bx			#  buffer address
574129239Sru		push  $0x1			# Block count
575129239Sru		push  $0x10			# Packet size
576129239Sru		movw %sp,%si			# Packet pointer
577129239Sru		decw %ax			# Verify off
578129239Sru		orb $0x40,%ah			# Use disk packet
579185562Sluigi	1:	int $0x13			# BIOS: Disk I/O
580129239Sru		movw %di,%sp			# Restore
581129239Sru		popw %si			# Restore
582129239Sru		retw				# To caller
583128722Sru
584185562Sluigi/*
585185562Sluigi * Various menu strings. 'item' goes after 'prompt' to save space.
586185562Sluigi * Also use shorter versions to make room for the PXE/INT18 code.
587185562Sluigi */
588185562Sluigi#ifdef PXE
589185562Sluigiprompt:		.ascii "\nBoot:"
590185562Sluigiitem:		.ascii " ";	     .byte ' '|0x80
591185562Sluigi#else
592185562Sluigiprompt:		.ascii "\nDefault:"
593128722Sruitem:		.ascii "  ";	     .byte ' '|0x80
594185562Sluigi#endif
595128722Srucrlf:		.ascii "\r";	     .byte '\n'|0x80
596128722Sru
597128723Sru/* Partition type tables */
598128722Sru
599185562Sluigibootable_ids:
600185562Sluigi	/*
601185579Sluigi	 * These values indicate bootable types we know about.
602185579Sluigi	 * Corresponding descriptions are at desc_ofs:
603185579Sluigi	 * Entries don't need to be sorted.
604185562Sluigi	 */
605185579Sluigi		.byte 0x1, 0x6, 0x7, 0xb, 0xc
606185579Sluigi#ifndef SAVE_MEMORY
607185579Sluigi		.byte 0xe
608185579Sluigi#endif
609185579Sluigi		.byte 0x83, 0xa5, 0xa6, 0xa9, 0x4
610185579Sluigi#ifndef SAVE_MORE_MEMORY
611185579Sluigi		.byte 0x5, 0xf
612185579Sluigi#endif
613185579Sluigi
614185562Sluigidesc_ofs:
615185562Sluigi	/*
616185562Sluigi	 * Offsets that match the known types above, used to point to the
617185562Sluigi	 * actual partition name. The last entry must point to os_misc,
618185562Sluigi	 * which is used for non-matching names.
619185562Sluigi	 */
620185579Sluigi		.byte os_dos-.			#   1, FAT12 DOS
621185579Sluigi		.byte os_dos-.			#   6, FAT16 <32M, DOS/WIN
622185579Sluigi		.byte os_win-.			#   7, FAT16 >=32M Windows
623185579Sluigi		.byte os_win-.			#  11, FAT32
624185579Sluigi		.byte os_win-.			#  12, FAT32-LBA
625185579Sluigi#ifndef SAVE_MEMORY
626185579Sluigi		.byte os_win-.			#  14, FAT16-LBA
627185579Sluigi#endif
628185562Sluigi		.byte os_linux-.		# 131, Linux
629185562Sluigi		.byte os_freebsd-.		# 165, FreeBSD
630185562Sluigi		.byte os_bsd-.			# 166, OpenBSD
631185562Sluigi		.byte os_bsd-.			# 169, NetBSD
632185579Sluigi		.byte os_dos-.			#   4, FAT16 < 32M
633185579Sluigi#ifndef SAVE_MORE_MEMORY
634185579Sluigi		.byte os_ext-.			#   5, DOS Ext
635185579Sluigi		.byte os_ext-.			#  15, DOS Ext-LBA
636185579Sluigi#endif
637185579Sluigi
638139948Speadar		.byte os_misc-. 		# Unknown
639185562Sluigi
640185562Sluigi	/*
641185562Sluigi	 * And here are the strings themselves. The last byte of
642185562Sluigi	 * the string has bit 7 set.
643185562Sluigi	 */
644185562Sluigios_misc:	.byte '?'|0x80
645185562Sluigios_dos:
646185579Sluigi#ifndef SAVE_MEMORY	/* DOS string only if room */
647185562Sluigi		.ascii "DO";   .byte 'S'|0x80
648185562Sluigi#endif
649185562Sluigios_win:		.ascii "WI";   .byte 'N'|0x80
650128722Sruos_linux:	.ascii "Linu"; .byte 'x'|0x80
651128722Sruos_freebsd:	.ascii "Free"
652128722Sruos_bsd:		.ascii "BS";   .byte 'D'|0x80
653185579Sluigi#ifndef SAVE_MORE_MEMORY
654185579Sluigios_ext:		.ascii "EX";   .byte 'T'|0x80
655185579Sluigi#endif
656128722Sru
657185579Sluigi		.org (0x200 + B0_OFF),0x90
658128723Sru/*
659185562Sluigi * The boot0 version 1.0 parameter table.
660185562Sluigi * Do not move it nor change the "Drive " string, boot0cfg
661185562Sluigi * uses its offset and content to identify the boot sector.
662185562Sluigi * The other fields are sometimes changed before writing back to the drive
663128723Sru * Be especially careful that nxtdrv: must come after drive:, as it
664128723Sru * is part of the same string.
665128723Sru */
666128722Srudrive:		.ascii "Drive "
667129239Srunxtdrv:		.byte 0x0			# Next drive number
668129239Sruopt:		.byte 0x0			# Option
669185562Sluigisetdrv_num:	.byte 0x80			# Drive to force
670129239Sruflags:		.byte FLAGS			# Flags
671185579Sluigi#ifdef VOLUME_SERIAL
672185579Sluigi		.byte 0xa8,0xa8,0xa8,0xa8       # Volume Serial Number
673185579Sluigi#endif
674129239Sruticks:		.word TICKS			# Delay
675128722Sru
676185562Sluigi		.org PRT_OFF
677128723Sru/*
678137298Skeramida * Here is the 64 byte partition table that fdisk would fiddle with.
679128723Sru */
680129239Srupartbl:		.fill 0x40,0x1,0x0		# Partition table
681129239Sru		.word MAGIC			# Magic number
682185562Sluigi		.org 0x200			# again, safety check
683185562Sluigiendblock:
684