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: stable/11/stand/i386/boot0/boot0.S 283006 2015-05-16 10:31:15Z thomas $
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 */
25186598Sluigi#define SAVE_MORE_MEMORY
26185562Sluigi#endif
27185562Sluigi
28185562Sluigi#ifdef CHECK_DRIVE	/* make sure we boot from a HD. */
29185562Sluigi#endif
30185562Sluigi
31185562Sluigi#ifdef ONLY_F_KEYS	/* Only F1..F6, no digits on console */
32185562Sluigi#endif
33185562Sluigi
34185579Sluigi#ifdef VOLUME_SERIAL	/* support Volume serial number */
35185579Sluigi#define B0_BASE	0x1ae	/* move the internal data area */
36185579Sluigi#define SAVE_MEMORY
37185579Sluigi#else
38185579Sluigi#define B0_BASE	0x1b2
39185579Sluigi#endif
40185579Sluigi
41185562Sluigi#ifdef TEST		/* enable some test code */
42185579Sluigi#define SAVE_MEMORY
43185579Sluigi#define SAVE_MORE_MEMORY
44185562Sluigi#endif
45185562Sluigi
46185562Sluigi/*
47185562Sluigi * Note - this code uses many tricks to save space and fit in one sector.
48185562Sluigi * This includes using side effects of certain instructions, reusing
49185562Sluigi * register values from previous operations, etc.
50185562Sluigi * Be extremely careful when changing the code, even for simple things.
51185562Sluigi */
52185562Sluigi
53185562Sluigi/*
54185562Sluigi *		BOOT BLOCK STRUCTURE
55185562Sluigi *
56185562Sluigi * This code implements a Master Boot Record (MBR) for an Intel/PC disk.
57185562Sluigi * It is 512 bytes long and it is normally loaded by the BIOS (or another
58185562Sluigi * bootloader) at 0:0x7c00. This code depends on %cs:%ip being 0:0x7c00
59185562Sluigi *
60185562Sluigi * The initial chunk of instructions is used as a signature by external
61185562Sluigi * tools (e.g. boot0cfg) which can manipulate the block itself.
62185562Sluigi *
63185562Sluigi * The area at offset 0x1b2 contains a magic string ('Drive '), also
64185562Sluigi * used as a signature to detect the block, and some variables that can
65185562Sluigi * be updated by boot0cfg (and optionally written back to the disk).
66185562Sluigi * These variables control the operation of the bootloader itself,
67185562Sluigi * e.g. which partitions to enable, the timeout, the use of LBA
68185562Sluigi * (called 'packet') or CHS mode, whether to force a drive number,
69185562Sluigi * and whether to write back the user's selection back to disk.
70185562Sluigi *
71185562Sluigi * As in every Master Boot Record, the partition table is at 0x1be,
72185562Sluigi * made of four 16-byte entries each containing:
73185562Sluigi *
74185562Sluigi *   OFF SIZE	DESCRIPTION
75185562Sluigi *    0	  1	status (0x80: bootable, 0: non bootable)
76185562Sluigi *    1	  3	start sector CHS
77185562Sluigi *		   8:head, 6:sector, 2:cyl bit 9..8, 8:cyl bit 7..0
78185562Sluigi *    4   1	partition type
79185562Sluigi *    5   3	end sector CHS
80185562Sluigi *    8   4	LBA of first sector
81185562Sluigi *   12   4	partition size in sectors
82185562Sluigi *
83185562Sluigi * and followed by the two bytes 0x55, 0xAA (MBR signature).
84185562Sluigi */
85185562Sluigi
86185562Sluigi
87185562Sluigi/*
88185562Sluigi *		BOOT BLOCK OPERATION
89185562Sluigi *
90185562Sluigi * On entry, the registers contain the following values:
91185562Sluigi *
92185562Sluigi *	%cs:%ip	0:0x7c00
93185562Sluigi *	%dl	drive number (0x80, 0x81, ... )
94185562Sluigi *	%si	pointer to the partition table from which we were loaded.
95185562Sluigi *		Some boot code (e.g. syslinux) use this info to relocate
96185562Sluigi *		themselves, so we want to pass a valid one to the next stage.
97185776Ssobomax *		NOTE: the use of %si is not a standard.
98185562Sluigi *
99185562Sluigi * This boot block first relocates itself at a different address (0:0x600),
100185562Sluigi * to free the space at 0:0x7c00 for the next stage boot block.
101185562Sluigi *
102185562Sluigi * It then initializes some memory at 0:0x800 and above (pointed by %bp)
103185562Sluigi * to store the original drive number (%dl) passed to us, and to construct a
104185562Sluigi * fake partition entry. The latter is used by the disk I/O routine and,
105185562Sluigi * in some cases, passed in %si to the next stage boot code.
106185562Sluigi *
107185562Sluigi * The variables at 0x1b2 are accessed as negative offsets from %bp.
108185562Sluigi *
109185562Sluigi * After the relocation, the code scans the partition table printing
110185562Sluigi * out enabled partition or disks, and waits for user input.
111185562Sluigi *
112185562Sluigi * When a partition is selected, or a timeout expires, the currently
113185562Sluigi * selected partition is used to load the next stage boot code,
114185562Sluigi * %dl and %si are set appropriately as when we were called, and
115185562Sluigi * control is transferred to the newly loaded code at 0:0x7c00.
116185562Sluigi */
117185562Sluigi
118185562Sluigi/*
119185562Sluigi *	CONSTANTS
120185562Sluigi *
121185562Sluigi * NHRDRV is the address in segment 0 where the BIOS writes the
122185562Sluigi *	total number of hard disks in the system.
123185562Sluigi * LOAD is the original load address and cannot be changed.
124185562Sluigi * ORIGIN is the relocation address. If you change it, you also need
125185562Sluigi * 	to change the value passed to the linker in the Makefile
126185562Sluigi * PRT_OFF is the location of the partition table (from the MBR standard).
127185562Sluigi * B0_OFF is the location of the data area, known to boot0cfg so
128185579Sluigi *	it cannot be changed. Computed as a negative offset from 0x200
129185562Sluigi * MAGIC is the signature of a boot block.
130185562Sluigi */
131185562Sluigi
132129239Sru		.set NHRDRV,0x475		# Number of hard drives
133129239Sru		.set ORIGIN,0x600		# Execution address
134129239Sru		.set LOAD,0x7c00		# Load address
135128722Sru
136129239Sru		.set PRT_OFF,0x1be		# Partition table
137185579Sluigi		.set B0_OFF,(B0_BASE-0x200)	# Offset of boot0 data
138128722Sru
139129239Sru		.set MAGIC,0xaa55		# Magic: bootable
140128722Sru
141129239Sru		.set KEY_ENTER,0x1c		# Enter key scan code
142129239Sru		.set KEY_F1,0x3b		# F1 key scan code
143129239Sru		.set KEY_1,0x02			# #1 key scan code
144128722Sru
145185562Sluigi		.set ASCII_BEL,'#'		# ASCII code for <BEL>
146129239Sru		.set ASCII_CR,0x0D		# ASCII code for <CR>
147128722Sru
148128723Sru/*
149185562Sluigi * Offsets of variables in the block at B0_OFF, and in the volatile
150185562Sluigi * data area, computed as displacement from %bp.
151185562Sluigi * We need to define them as constant as the assembler cannot
152185562Sluigi * compute them in its single pass.
153128723Sru */
154185579Sluigi		.set _NXTDRV,	B0_OFF+6	# Next drive
155185579Sluigi		.set _OPT,	B0_OFF+7	# Default option
156185579Sluigi		.set _SETDRV,	B0_OFF+8	# Drive to force
157185579Sluigi		.set _FLAGS,	B0_OFF+9	# Flags
158185562Sluigi		.set SETDRV,	0x20		# the 'setdrv' flag
159185562Sluigi		.set NOUPDATE,	0x40		# the 'noupdate' flag
160185562Sluigi		.set USEPACKET,	0x80		# the 'packet' flag
161185579Sluigi
162185579Sluigi	/* ticks is at a fixed position */
163185579Sluigi		.set _TICKS,	(PRT_OFF - 0x200 - 2)	# Timeout ticks
164185562Sluigi		.set _MNUOPT, 0x10		# Saved menu entries
165128722Sru
166185562Sluigi		.set TLEN, (desc_ofs - bootable_ids)	# size of bootable ids
167129239Sru		.globl start			# Entry point
168129239Sru		.code16				# This runs in real mode
169128722Sru
170128723Sru/*
171185562Sluigi * 	MAIN ENTRY POINT
172128723Sru * Initialise segments and registers to known values.
173128723Sru * segments start at 0.
174128723Sru * The stack is immediately below the address we were loaded to.
175185562Sluigi * NOTE: the initial section of the code (up to movw $LOAD,%sp)
176185562Sluigi * is used by boot0cfg, together with the 'Drive ' string and
177185562Sluigi * the 0x55, 0xaa at the end, as an identifier for version 1.0
178185562Sluigi * of the boot code. Do not change it.
179185562Sluigi * In version 1.0 the parameter table (_NEXTDRV etc) is at 0x1b9
180128723Sru */
181129239Srustart:		cld				# String ops inc
182129239Sru		xorw %ax,%ax			# Zero
183129239Sru		movw %ax,%es			# Address
184129239Sru		movw %ax,%ds			#  data
185129239Sru		movw %ax,%ss			# Set up
186129239Sru		movw $LOAD,%sp			#  stack
187128722Sru
188185562Sluigi	/*
189185562Sluigi	 * Copy this code to the address it was linked for, 0x600 by default.
190185562Sluigi	 */
191129239Sru		movw %sp,%si			# Source
192129239Sru		movw $start,%di			# Destination
193129239Sru		movw $0x100,%cx			# Word count
194129239Sru		rep				# Relocate
195129239Sru		movsw				#  code
196185562Sluigi	/*
197185562Sluigi	 * After the code, (i.e. at %di+0, 0x800) create a partition entry,
198185562Sluigi	 * initialized to LBA 0 / CHS 0:0:1.
199185562Sluigi	 * Set %bp to point to the partition and also, with negative offsets,
200185562Sluigi	 * to the variables embedded in the bootblock (nextdrv and so on).
201185562Sluigi	 */
202129239Sru		movw %di,%bp			# Address variables
203129239Sru		movb $0x8,%cl			# Words to clear
204129239Sru		rep				# Zero
205129239Sru		stosw				#  them
206185562Sluigi		incb -0xe(%di)			# Set the S field to 1
207128722Sru
208185562Sluigi		jmp main-LOAD+ORIGIN		# Jump to relocated code
209185562Sluigi
210128722Srumain:
211138048Sjhb#if defined(SIO) && COMSPEED != 0
212185562Sluigi	/*
213185562Sluigi	 * Init the serial port. bioscom preserves the driver number in DX.
214185562Sluigi	 */
215167914Sthomas		movw $COMSPEED,%ax		# defined by Makefile
216130632Sphk		callw bioscom
217138048Sjhb#endif
218185562Sluigi
219185562Sluigi	/*
220185562Sluigi	 * If the 'setdrv' flag is set in the boot sector, use the drive
221185562Sluigi	 * number from the boot sector at 'setdrv_num'.
222185562Sluigi	 * Optionally, do the same if the BIOS gives us an invalid number
223185562Sluigi	 * (note though that the override prevents booting from a floppy
224185562Sluigi	 * or a ZIP/flash drive in floppy emulation).
225185562Sluigi	 * The test costs 4 bytes of code so it is disabled by default.
226185562Sluigi	 */
227185562Sluigi		testb $SETDRV,_FLAGS(%bp)	# Set drive number?
228185562Sluigi#ifndef CHECK_DRIVE	/* disable drive checks */
229185562Sluigi		jz save_curdrive		# no, use the default
230185562Sluigi#else
231185562Sluigi		jnz disable_update		# Yes
232129239Sru		testb %dl,%dl			# Drive number valid?
233185562Sluigi		js save_curdrive		# Possibly (0x80 set)
234185562Sluigi#endif
235185562Sluigi	/*
236185562Sluigi	 * Disable updates if the drive number is forced.
237185562Sluigi	 */
238185562Sluigidisable_update:	orb $NOUPDATE,_FLAGS(%bp)	# Disable updates
239185562Sluigi		movb _SETDRV(%bp),%dl		# Use stored drive number
240185562Sluigi
241185562Sluigi	/*
242185562Sluigi	 * Whatever drive we decided to use, store it at (%bp). The byte
243185562Sluigi	 * is normally used for the state of the partition (0x80 or 0x00),
244185562Sluigi	 * but we abuse it as it is very convenient to access at offset 0.
245185562Sluigi	 * The value is read back after 'check_selection'
246185562Sluigi	 */
247185562Sluigisave_curdrive:	movb %dl, (%bp)			# Save drive number
248185562Sluigi		pushw %dx			# Also in the stack
249185562Sluigi#ifdef	TEST	/* test code, print internal bios drive */
250185562Sluigi		rolb $1, %dl
251185562Sluigi		movw $drive, %si
252185562Sluigi		call putkey
253185562Sluigi#endif
254185562Sluigi		callw putn			# Print a newline
255185562Sluigi	/*
256185562Sluigi	 * Start out with a pointer to the 4th byte of the first table entry
257185562Sluigi	 * so that after 4 iterations it's beyond the end of the sector
258185562Sluigi	 * and beyond a 256 byte boundary. We use the latter trick to check for
259185562Sluigi	 * end of the loop without using an extra register (see start.5).
260185562Sluigi	 */
261129239Sru		movw $(partbl+0x4),%bx		# Partition table (+4)
262129239Sru		xorw %dx,%dx			# Item number
263185562Sluigi
264185562Sluigi	/*
265185562Sluigi	 * Loop around on the partition table, printing values until we
266185562Sluigi	 * pass a 256 byte boundary.
267185562Sluigi	 */
268185562Sluigiread_entry:	movb %ch,-0x4(%bx)		# Zero active flag (ch == 0)
269129239Sru		btw %dx,_FLAGS(%bp)		# Entry enabled?
270185562Sluigi		jnc next_entry			# No
271129239Sru		movb (%bx),%al			# Load type
272185579Sluigi		test %al, %al			# skip empty partition
273185579Sluigi		jz next_entry
274185562Sluigi	/*
275185579Sluigi	 * Scan the table of bootable ids, which starts at %di and has
276185562Sluigi	 * length TLEN. On a match, %di points to the element following the
277185562Sluigi	 * match; the corresponding offset to the description is $(TLEN-1)
278186598Sluigi	 * bytes ahead. We use a count of TLEN+1 so if we don't find a match
279186598Sluigi	 * within the first TLEN entries, we hit the 'unknown' entry.
280185562Sluigi	 */
281185579Sluigi		movw $bootable_ids,%di		# Lookup tables
282186598Sluigi		movb $(TLEN+1),%cl		# Number of entries
283139948Speadar		repne				# Locate
284139948Speadar		scasb				#  type
285185562Sluigi	/*
286185562Sluigi	 * Get the matching element in the next array.
287185562Sluigi	 * The byte at $(TLEN-1)(%di) contains the offset of the description
288185562Sluigi	 * string from %di, so we add the number and print the string.
289185562Sluigi	 */
290185562Sluigi		addw $(TLEN-1), %di		# Adjust
291139948Speadar		movb (%di),%cl			# Partition
292129239Sru		addw %cx,%di			#  description
293129239Sru		callw putx			# Display it
294185562Sluigi
295185562Sluiginext_entry:	incw %dx			# Next item
296129239Sru		addb $0x10,%bl			# Next entry
297185562Sluigi		jnc read_entry			# Till done
298185562Sluigi	/*
299185562Sluigi	 * We are past a 256 byte boundary: the partition table is finished.
300185562Sluigi	 * Add one to the drive number and check it is valid.
301185562Sluigi	 * Note that if we started from a floppy, %dl was 0 so we still
302185562Sluigi	 * get an entry for the next drive, which is the first Hard Disk.
303185562Sluigi	 */
304129239Sru		popw %ax			# Drive number
305129239Sru		subb $0x80-0x1,%al		# Does next
306129239Sru		cmpb NHRDRV,%al			#  drive exist? (from BIOS?)
307185562Sluigi		jb print_drive			# Yes
308185562Sluigi	/*
309185562Sluigi	 * If this is the only drive, don't display it as an option.
310185562Sluigi	 */
311129239Sru		decw %ax			# Already drive 0?
312185562Sluigi		jz print_prompt			# Yes
313185562Sluigi	/*
314185562Sluigi	 * If it was illegal or we cycled through them, go back to drive 0.
315185562Sluigi	 */
316129239Sru		xorb %al,%al			# Drive 0
317185562Sluigi	/*
318185562Sluigi	 * Whatever drive we selected, make it an ascii digit and save it
319185562Sluigi	 * back to the "nxtdrv" location in case we want to save it to disk.
320185562Sluigi	 * This digit is also part of the printed drive string, so add 0x80
321185562Sluigi	 * to indicate end of string.
322185562Sluigi	 */
323185562Sluigiprint_drive:	addb $'0'|0x80,%al		# Save next
324129239Sru		movb %al,_NXTDRV(%bp)		#  drive number
325129239Sru		movw $drive,%di			# Display
326129239Sru		callw putx			#  item
327185562Sluigi	/*
328185562Sluigi	 * Menu is complete, display a prompt followed by current selection.
329186598Sluigi	 * 'decw %si' makes the register point to the space after 'Boot: '
330185562Sluigi	 * so we do not see an extra CRLF on the screen.
331185562Sluigi	 */
332185562Sluigiprint_prompt:	movw $prompt,%si		# Display
333129239Sru		callw putstr			#  prompt
334129239Sru		movb _OPT(%bp),%dl		# Display
335129239Sru		decw %si			#  default
336129239Sru		callw putkey			#  key
337185562Sluigi		jmp start_input			# Skip beep
338185562Sluigi
339130343Sphk/*
340185562Sluigi * Here we have the code waiting for user input or a timeout.
341130343Sphk */
342185562Sluigibeep:		movb $ASCII_BEL,%al		# Input error, print or beep
343185562Sluigi		callw putchr
344185562Sluigi
345185562Sluigistart_input:
346185562Sluigi	/*
347185562Sluigi	 * Actual Start of input loop.  Take note of time
348185562Sluigi	 */
349185562Sluigi		xorb %ah,%ah			# BIOS: Get
350129239Sru		int $0x1a			#  system time
351129239Sru		movw %dx,%di			# Ticks when
352130343Sphk		addw _TICKS(%bp),%di		#  timeout
353185562Sluigiread_key:
354185562Sluigi	/*
355185562Sluigi	 * Busy loop, looking for keystrokes but keeping one eye on the time.
356185562Sluigi	 */
357128722Sru#ifndef SIO
358129239Sru		movb $0x1,%ah			# BIOS: Check
359129239Sru		int $0x16			#  for keypress
360128722Sru#else /* SIO */
361129239Sru		movb $0x03,%ah			# BIOS: Read COM
362130343Sphk		call bioscom
363129239Sru		testb $0x01,%ah			# Check line status
364185562Sluigi						# (bit 1 indicates input)
365128722Sru#endif /* SIO */
366185562Sluigi		jnz got_key 			# Have input
367185562Sluigi		xorb %ah,%ah			# BIOS: int 0x1a, 00
368185562Sluigi		int $0x1a			#  get system time
369129239Sru		cmpw %di,%dx			# Timeout?
370185562Sluigi		jb read_key			# No
371185562Sluigi
372185562Sluigi	/*
373185562Sluigi	 * Timed out or default selection
374185562Sluigi	 */
375185562Sluigiuse_default:	movb _OPT(%bp),%al		# Load default
376186598Sluigi		orb $NOUPDATE,_FLAGS(%bp) 	# Disable updates
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 */
416228738Sjhbcheck_selection:
417185562Sluigi		cmpb $0x5,%al			# F1..F6 or 1..6 ?
418185562Sluigi#ifdef PXE /* enable PXE/INT18 using F6 */
419185562Sluigi		jne 1f;
420185562Sluigi		int $0x18			# found F6, try INT18
421185562Sluigi	1:
422185562Sluigi#endif /* PXE */
423185562Sluigi		jae beep			# Not in F1..F5, beep
424185562Sluigi
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 * putn:	print a crlf
504185562Sluigi * putstr:	print the string at %si
505185562Sluigi * putchr:	print the char in al
506128723Sru */
507128722Sru
508128723Sru/*
509185562Sluigi * Display the option and record the drive as valid in the options.
510185562Sluigi * That last point is done using the btsw instruction which does
511185562Sluigi * a test and set. We don't care for the test part.
512128723Sru */
513129239Sruputx:		btsw %dx,_MNUOPT(%bp)		# Enable menu option
514129239Sru		movw $item,%si			# Display
515129239Sru		callw putkey			#  key
516129239Sru		movw %di,%si			# Display the rest
517185579Sluigi		callw putstr			# Display string
518128722Sru
519129239Sruputn:		movw $crlf,%si			# To next line
520185579Sluigi		jmp putstr
521128722Sru
522185579Sluigiputkey:
523185579Sluigi#ifndef SIO
524185579Sluigi		movb $'F',%al			# Display
525185579Sluigi		callw putchr			#  'F'
526185579Sluigi#endif
527185579Sluigi		movb $'1',%al			# Prepare
528185579Sluigi		addb %dl,%al			#  digit
529185579Sluigi
530185579Sluigiputstr.1:	callw putchr			# Display char
531129239Sruputstr:		lodsb				# Get byte
532129239Sru		testb $0x80,%al 		# End of string?
533185579Sluigi		jz putstr.1			# No
534185579Sluigi		andb $~0x80,%al 		# Clear MSB then print last
535128722Sru
536185562Sluigiputchr:
537130343Sphk#ifndef SIO
538129239Sru		pushw %bx			# Save
539129239Sru		movw $0x7,%bx	 		# Page:attribute
540129239Sru		movb $0xe,%ah			# BIOS: Display
541129239Sru		int $0x10			#  character
542129239Sru		popw %bx			# Restore
543128722Sru#else /* SIO */
544283006Sthomas		movb $0x01,%ah			# BIOS: Send character
545130343Sphkbioscom:
546129239Sru		pushw %dx			# Save
547129239Sru		xorw %dx,%dx 			# Use COM1
548283006Sthomas		int $0x14			# BIOS: Serial I/O
549129239Sru		popw %dx			# Restore
550185562Sluigi#endif /* SIO */
551130343Sphk		retw				# To caller
552128722Sru
553128723Sru/* One-sector disk I/O routine */
554128722Sru
555185562Sluigi/*
556185562Sluigi * %dl: drive, %si partition entry, %es:%bx transfer buffer.
557185562Sluigi * Load the CHS values and possibly the LBA address from the block
558185562Sluigi * at %si, and use the appropriate method to load the sector.
559185562Sluigi * Don't use packet mode for a floppy.
560185562Sluigi */
561185562Sluigiintx13:						# Prepare CHS parameters
562185562Sluigi		movb 0x1(%si),%dh		# Load head
563129239Sru		movw 0x2(%si),%cx		# Load cylinder:sector
564129239Sru		movb $0x1,%al			# Sector count
565129239Sru		pushw %si			# Save
566129239Sru		movw %sp,%di			# Save
567185562Sluigi#ifndef CHECK_DRIVE				/* floppy support */
568185562Sluigi		testb %dl, %dl			# is this a floppy ?
569185562Sluigi		jz 1f				# Yes, use CHS mode
570185562Sluigi#endif
571185562Sluigi		testb $USEPACKET,_FLAGS(%bp)	# Use packet interface?
572185562Sluigi		jz 1f				# No
573129239Sru		pushl $0x0			# Set the
574129239Sru		pushl 0x8(%si)			# LBA address
575129239Sru		pushw %es			# Set the transfer
576129239Sru		pushw %bx			#  buffer address
577129239Sru		push  $0x1			# Block count
578129239Sru		push  $0x10			# Packet size
579129239Sru		movw %sp,%si			# Packet pointer
580129239Sru		decw %ax			# Verify off
581129239Sru		orb $0x40,%ah			# Use disk packet
582185562Sluigi	1:	int $0x13			# BIOS: Disk I/O
583129239Sru		movw %di,%sp			# Restore
584129239Sru		popw %si			# Restore
585129239Sru		retw				# To caller
586128722Sru
587185562Sluigi/*
588185562Sluigi * Various menu strings. 'item' goes after 'prompt' to save space.
589185562Sluigi * Also use shorter versions to make room for the PXE/INT18 code.
590185562Sluigi */
591186598Sluigiprompt:
592185562Sluigi#ifdef PXE
593186598Sluigi		.ascii "\nF6 PXE\r"
594186598Sluigi#endif
595186598Sluigi		.ascii "\nBoot:"
596185562Sluigiitem:		.ascii " ";	     .byte ' '|0x80
597128722Srucrlf:		.ascii "\r";	     .byte '\n'|0x80
598128722Sru
599128723Sru/* Partition type tables */
600128722Sru
601185562Sluigibootable_ids:
602185562Sluigi	/*
603185579Sluigi	 * These values indicate bootable types we know about.
604185579Sluigi	 * Corresponding descriptions are at desc_ofs:
605185579Sluigi	 * Entries don't need to be sorted.
606185562Sluigi	 */
607186598Sluigi		.byte 0x83, 0xa5, 0xa6, 0xa9, 0x06, 0x07, 0x0b
608185579Sluigi#ifndef SAVE_MORE_MEMORY
609186598Sluigi		.byte 0x05	# extended partition
610185579Sluigi#endif
611186598Sluigi#ifndef SAVE_MEMORY	/* other DOS partitions */
612186598Sluigi		.byte 0x01	# FAT12
613186598Sluigi		.byte 0x04	# FAT16 < 32M
614186598Sluigi#endif
615185579Sluigi
616185562Sluigidesc_ofs:
617185562Sluigi	/*
618185562Sluigi	 * Offsets that match the known types above, used to point to the
619185562Sluigi	 * actual partition name. The last entry must point to os_misc,
620185562Sluigi	 * which is used for non-matching names.
621185562Sluigi	 */
622185562Sluigi		.byte os_linux-.		# 131, Linux
623185562Sluigi		.byte os_freebsd-.		# 165, FreeBSD
624185562Sluigi		.byte os_bsd-.			# 166, OpenBSD
625185562Sluigi		.byte os_bsd-.			# 169, NetBSD
626186598Sluigi		.byte os_dos-.			#   6, FAT16 >= 32M
627186598Sluigi		.byte os_win-.			#   7, NTFS
628186598Sluigi		.byte os_win-.			#  11, FAT32
629186598Sluigi
630185579Sluigi#ifndef SAVE_MORE_MEMORY
631185579Sluigi		.byte os_ext-.			#   5, DOS Ext
632185579Sluigi#endif
633186598Sluigi#ifndef SAVE_MEMORY
634186598Sluigi		.byte os_dos-.			#   1, FAT12 DOS
635186598Sluigi		.byte os_dos-.			#   4, FAT16 <32M
636186598Sluigi#endif
637139948Speadar		.byte os_misc-. 		# Unknown
638185562Sluigi
639185562Sluigi	/*
640185562Sluigi	 * And here are the strings themselves. The last byte of
641185562Sluigi	 * the string has bit 7 set.
642185562Sluigi	 */
643185562Sluigios_misc:	.byte '?'|0x80
644185562Sluigios_dos:
645186598Sluigi#ifndef SAVE_MORE_MEMORY	/* 'DOS' remapped to 'WIN' if no room */
646185562Sluigi		.ascii "DO";   .byte 'S'|0x80
647185562Sluigi#endif
648186598Sluigios_win:		.ascii "Wi";   .byte 'n'|0x80
649128722Sruos_linux:	.ascii "Linu"; .byte 'x'|0x80
650128722Sruos_freebsd:	.ascii "Free"
651128722Sruos_bsd:		.ascii "BS";   .byte 'D'|0x80
652185579Sluigi#ifndef SAVE_MORE_MEMORY
653185579Sluigios_ext:		.ascii "EX";   .byte 'T'|0x80
654185579Sluigi#endif
655128722Sru
656185579Sluigi		.org (0x200 + B0_OFF),0x90
657128723Sru/*
658185562Sluigi * The boot0 version 1.0 parameter table.
659185562Sluigi * Do not move it nor change the "Drive " string, boot0cfg
660185562Sluigi * uses its offset and content to identify the boot sector.
661185562Sluigi * The other fields are sometimes changed before writing back to the drive
662128723Sru * Be especially careful that nxtdrv: must come after drive:, as it
663128723Sru * is part of the same string.
664128723Sru */
665128722Srudrive:		.ascii "Drive "
666129239Srunxtdrv:		.byte 0x0			# Next drive number
667129239Sruopt:		.byte 0x0			# Option
668185562Sluigisetdrv_num:	.byte 0x80			# Drive to force
669129239Sruflags:		.byte FLAGS			# Flags
670185579Sluigi#ifdef VOLUME_SERIAL
671185579Sluigi		.byte 0xa8,0xa8,0xa8,0xa8       # Volume Serial Number
672185579Sluigi#endif
673129239Sruticks:		.word TICKS			# Delay
674128722Sru
675185562Sluigi		.org PRT_OFF
676128723Sru/*
677137298Skeramida * Here is the 64 byte partition table that fdisk would fiddle with.
678128723Sru */
679129239Srupartbl:		.fill 0x40,0x1,0x0		# Partition table
680129239Sru		.word MAGIC			# Magic number
681185562Sluigi		.org 0x200			# again, safety check
682185562Sluigiendblock:
683