boot0.S revision 185776
131921Sbrian/*
231921Sbrian * Copyright (c) 2008 Luigi Rizzo (mostly documentation)
331921Sbrian * Copyright (c) 2002 Bruce M. Simpson
431921Sbrian * Copyright (c) 1998 Robert Nordier
531921Sbrian * All rights reserved.
631921Sbrian *
730715Sbrian * Redistribution and use in source and binary forms are freely
831921Sbrian * permitted provided that the above copyright notice and this
931921Sbrian * paragraph and the following disclaimer are duplicated in all
1031921Sbrian * such forms.
1131921Sbrian *
1231921Sbrian * This software is provided "AS IS" and without any express or
1331921Sbrian * implied warranties, including, without limitation, the implied
1431921Sbrian * warranties of merchantability and fitness for a particular
1531921Sbrian * purpose.
1630715Sbrian *
1731921Sbrian * $FreeBSD: head/sys/boot/i386/boot0/boot0.S 185776 2008-12-08 20:53:27Z sobomax $
1831921Sbrian */
1931921Sbrian
2031921Sbrian/* build options: */
2131921Sbrian#ifdef SIO		/* use serial console on COM1.	*/
2231921Sbrian#endif
2331921Sbrian
2431921Sbrian#ifdef PXE		/* enable PXE/INT18 booting with F6 */
2531921Sbrian#endif
2631921Sbrian
2731921Sbrian#ifdef CHECK_DRIVE	/* make sure we boot from a HD. */
2831921Sbrian#endif
2950479Speter
306059Samurai#ifdef ONLY_F_KEYS	/* Only F1..F6, no digits on console */
316059Samurai#endif
3236285Sbrian
3330715Sbrian#ifdef VOLUME_SERIAL	/* support Volume serial number */
3431514Sbrian#define B0_BASE	0x1ae	/* move the internal data area */
3530715Sbrian#define SAVE_MEMORY
3646686Sbrian#else
3730715Sbrian#define B0_BASE	0x1b2
3838174Sbrian#endif
3946686Sbrian
4030715Sbrian#ifdef TEST		/* enable some test code */
4130715Sbrian#define SAVE_MEMORY
4230715Sbrian#define SAVE_MORE_MEMORY
4330715Sbrian#endif
4436285Sbrian
4530715Sbrian/*
4631514Sbrian * Note - this code uses many tricks to save space and fit in one sector.
4730715Sbrian * This includes using side effects of certain instructions, reusing
4844650Sbrian * register values from previous operations, etc.
4944650Sbrian * Be extremely careful when changing the code, even for simple things.
5030715Sbrian */
5130715Sbrian
526059Samurai/*
536059Samurai *		BOOT BLOCK STRUCTURE
546059Samurai *
556059Samurai * This code implements a Master Boot Record (MBR) for an Intel/PC disk.
566059Samurai * It is 512 bytes long and it is normally loaded by the BIOS (or another
576059Samurai * bootloader) at 0:0x7c00. This code depends on %cs:%ip being 0:0x7c00
5836285Sbrian *
5931514Sbrian * The initial chunk of instructions is used as a signature by external
606059Samurai * tools (e.g. boot0cfg) which can manipulate the block itself.
6136285Sbrian *
6236285Sbrian * The area at offset 0x1b2 contains a magic string ('Drive '), also
6336285Sbrian * used as a signature to detect the block, and some variables that can
6436285Sbrian * be updated by boot0cfg (and optionally written back to the disk).
656059Samurai * These variables control the operation of the bootloader itself,
666059Samurai * e.g. which partitions to enable, the timeout, the use of LBA
6736285Sbrian * (called 'packet') or CHS mode, whether to force a drive number,
686059Samurai * and whether to write back the user's selection back to disk.
6928679Sbrian *
7028679Sbrian * As in every Master Boot Record, the partition table is at 0x1be,
716059Samurai * made of four 16-byte entries each containing:
7228679Sbrian *
7328679Sbrian *   OFF SIZE	DESCRIPTION
7428679Sbrian *    0	  1	status (0x80: bootable, 0: non bootable)
7528679Sbrian *    1	  3	start sector CHS
7628679Sbrian *		   8:head, 6:sector, 2:cyl bit 9..8, 8:cyl bit 7..0
7736285Sbrian *    4   1	partition type
7828679Sbrian *    5   3	end sector CHS
7928679Sbrian *    8   4	LBA of first sector
8036285Sbrian *   12   4	partition size in sectors
8128679Sbrian *
8228679Sbrian * and followed by the two bytes 0x55, 0xAA (MBR signature).
8336285Sbrian */
8428679Sbrian
856059Samurai
8628679Sbrian/*
8728679Sbrian *		BOOT BLOCK OPERATION
8828679Sbrian *
896059Samurai * On entry, the registers contain the following values:
906059Samurai *
916059Samurai *	%cs:%ip	0:0x7c00
9244792Sbrian *	%dl	drive number (0x80, 0x81, ... )
936059Samurai *	%si	pointer to the partition table from which we were loaded.
9428679Sbrian *		Some boot code (e.g. syslinux) use this info to relocate
9544792Sbrian *		themselves, so we want to pass a valid one to the next stage.
9644792Sbrian *		NOTE: the use of %si is not a standard.
9728679Sbrian *
986059Samurai * This boot block first relocates itself at a different address (0:0x600),
996059Samurai * to free the space at 0:0x7c00 for the next stage boot block.
1006059Samurai *
10144792Sbrian * It then initializes some memory at 0:0x800 and above (pointed by %bp)
1026059Samurai * to store the original drive number (%dl) passed to us, and to construct a
10328679Sbrian * fake partition entry. The latter is used by the disk I/O routine and,
10428679Sbrian * in some cases, passed in %si to the next stage boot code.
1056059Samurai *
10628679Sbrian * The variables at 0x1b2 are accessed as negative offsets from %bp.
10728679Sbrian *
10828679Sbrian * After the relocation, the code scans the partition table printing
10928679Sbrian * out enabled partition or disks, and waits for user input.
11028679Sbrian *
11128679Sbrian * When a partition is selected, or a timeout expires, the currently
11236285Sbrian * selected partition is used to load the next stage boot code,
11328679Sbrian * %dl and %si are set appropriately as when we were called, and
11428679Sbrian * control is transferred to the newly loaded code at 0:0x7c00.
11528679Sbrian */
11636285Sbrian
11728679Sbrian/*
11828679Sbrian *	CONSTANTS
11928679Sbrian *
12036285Sbrian * NHRDRV is the address in segment 0 where the BIOS writes the
1216059Samurai *	total number of hard disks in the system.
12228679Sbrian * LOAD is the original load address and cannot be changed.
12328679Sbrian * ORIGIN is the relocation address. If you change it, you also need
1246059Samurai * 	to change the value passed to the linker in the Makefile
1256059Samurai * PRT_OFF is the location of the partition table (from the MBR standard).
12631514Sbrian * B0_OFF is the location of the data area, known to boot0cfg so
12736285Sbrian *	it cannot be changed. Computed as a negative offset from 0x200
1286059Samurai * MAGIC is the signature of a boot block.
12936285Sbrian */
13036285Sbrian
13131514Sbrian		.set NHRDRV,0x475		# Number of hard drives
13231514Sbrian		.set ORIGIN,0x600		# Execution address
13331514Sbrian		.set LOAD,0x7c00		# Load address
13436285Sbrian
13531514Sbrian		.set PRT_OFF,0x1be		# Partition table
13636285Sbrian		.set B0_OFF,(B0_BASE-0x200)	# Offset of boot0 data
13736285Sbrian
13836285Sbrian		.set MAGIC,0xaa55		# Magic: bootable
13936285Sbrian
1406059Samurai		.set KEY_ENTER,0x1c		# Enter key scan code
1416059Samurai		.set KEY_F1,0x3b		# F1 key scan code
14278411Sbrian		.set KEY_1,0x02			# #1 key scan code
14336285Sbrian
14431514Sbrian		.set ASCII_BEL,'#'		# ASCII code for <BEL>
14536285Sbrian		.set ASCII_CR,0x0D		# ASCII code for <CR>
14636285Sbrian
14736285Sbrian/*
14836285Sbrian * Offsets of variables in the block at B0_OFF, and in the volatile
14978411Sbrian * data area, computed as displacement from %bp.
15078411Sbrian * We need to define them as constant as the assembler cannot
15131514Sbrian * compute them in its single pass.
15231514Sbrian */
15336285Sbrian		.set _NXTDRV,	B0_OFF+6	# Next drive
154134789Sbrian		.set _OPT,	B0_OFF+7	# Default option
15531514Sbrian		.set _SETDRV,	B0_OFF+8	# Drive to force
15636285Sbrian		.set _FLAGS,	B0_OFF+9	# Flags
15736285Sbrian		.set SETDRV,	0x20		# the 'setdrv' flag
15836285Sbrian		.set NOUPDATE,	0x40		# the 'noupdate' flag
15936285Sbrian		.set USEPACKET,	0x80		# the 'packet' flag
16036285Sbrian
16131514Sbrian	/* ticks is at a fixed position */
16231514Sbrian		.set _TICKS,	(PRT_OFF - 0x200 - 2)	# Timeout ticks
16336285Sbrian		.set _MNUOPT, 0x10		# Saved menu entries
164134789Sbrian
16531514Sbrian		.set TLEN, (desc_ofs - bootable_ids)	# size of bootable ids
16636285Sbrian		.globl start			# Entry point
16736285Sbrian		.code16				# This runs in real mode
16836285Sbrian
16936285Sbrian/*
17036285Sbrian * 	MAIN ENTRY POINT
17131514Sbrian * Initialise segments and registers to known values.
17231514Sbrian * segments start at 0.
17346686Sbrian * The stack is immediately below the address we were loaded to.
174134789Sbrian * NOTE: the initial section of the code (up to movw $LOAD,%sp)
175134789Sbrian * is used by boot0cfg, together with the 'Drive ' string and
17631514Sbrian * the 0x55, 0xaa at the end, as an identifier for version 1.0
17736285Sbrian * of the boot code. Do not change it.
1786059Samurai * In version 1.0 the parameter table (_NEXTDRV etc) is at 0x1b9
1796059Samurai */
1806059Samuraistart:		cld				# String ops inc
18128679Sbrian		xorw %ax,%ax			# Zero
1826059Samurai		movw %ax,%es			# Address
1836059Samurai		movw %ax,%ds			#  data
18454912Sbrian		movw %ax,%ss			# Set up
18554912Sbrian		movw $LOAD,%sp			#  stack
1866059Samurai
1876059Samurai	/*
1886059Samurai	 * Copy this code to the address it was linked for, 0x600 by default.
1896059Samurai	 */
19046686Sbrian		movw %sp,%si			# Source
19146686Sbrian		movw $start,%di			# Destination
19236285Sbrian		movw $0x100,%cx			# Word count
19346686Sbrian		rep				# Relocate
1946059Samurai		movsw				#  code
1956059Samurai	/*
19636285Sbrian	 * After the code, (i.e. at %di+0, 0x800) create a partition entry,
19736285Sbrian	 * initialized to LBA 0 / CHS 0:0:1.
19836285Sbrian	 * Set %bp to point to the partition and also, with negative offsets,
1996059Samurai	 * to the variables embedded in the bootblock (nextdrv and so on).
2006059Samurai	 */
2016059Samurai		movw %di,%bp			# Address variables
20236285Sbrian		movb $0x8,%cl			# Words to clear
2036059Samurai		rep				# Zero
20430715Sbrian		stosw				#  them
2056059Samurai		incb -0xe(%di)			# Set the S field to 1
20636285Sbrian
2076059Samurai		jmp main-LOAD+ORIGIN		# Jump to relocated code
2086059Samurai
2096059Samuraimain:
2106059Samurai#if defined(SIO) && COMSPEED != 0
21154912Sbrian	/*
21246686Sbrian	 * Init the serial port. bioscom preserves the driver number in DX.
21346686Sbrian	 */
2146059Samurai		movw $COMSPEED,%ax		# defined by Makefile
2156059Samurai		callw bioscom
21631514Sbrian#endif
21736285Sbrian
2186059Samurai	/*
21936285Sbrian	 * If the 'setdrv' flag is set in the boot sector, use the drive
2206059Samurai	 * number from the boot sector at 'setdrv_num'.
2216059Samurai	 * Optionally, do the same if the BIOS gives us an invalid number
2226059Samurai	 * (note though that the override prevents booting from a floppy
2236059Samurai	 * or a ZIP/flash drive in floppy emulation).
22431514Sbrian	 * The test costs 4 bytes of code so it is disabled by default.
2256059Samurai	 */
22654912Sbrian		testb $SETDRV,_FLAGS(%bp)	# Set drive number?
2276059Samurai#ifndef CHECK_DRIVE	/* disable drive checks */
22854912Sbrian		jz save_curdrive		# no, use the default
2296059Samurai#else
2306059Samurai		jnz disable_update		# Yes
2316059Samurai		testb %dl,%dl			# Drive number valid?
2326059Samurai		js save_curdrive		# Possibly (0x80 set)
2336059Samurai#endif
23436285Sbrian	/*
2356059Samurai	 * Disable updates if the drive number is forced.
23636285Sbrian	 */
23736285Sbriandisable_update:	orb $NOUPDATE,_FLAGS(%bp)	# Disable updates
2386059Samurai		movb _SETDRV(%bp),%dl		# Use stored drive number
23928679Sbrian
24044650Sbrian	/*
24144650Sbrian	 * Whatever drive we decided to use, store it at (%bp). The byte
24254912Sbrian	 * is normally used for the state of the partition (0x80 or 0x00),
24354912Sbrian	 * but we abuse it as it is very convenient to access at offset 0.
24431514Sbrian	 * The value is read back after 'check_selection'
2456059Samurai	 */
2466059Samuraisave_curdrive:	movb %dl, (%bp)			# Save drive number
2476059Samurai		pushw %dx			# Also in the stack
24844792Sbrian#ifdef	TEST	/* test code, print internal bios drive */
24944792Sbrian		rolb $1, %dl
25044792Sbrian		movw $drive, %si
25154912Sbrian		call putkey
25254912Sbrian#endif
25344792Sbrian		callw putn			# Print a newline
2546059Samurai	/*
25536285Sbrian	 * Start out with a pointer to the 4th byte of the first table entry
25636285Sbrian	 * so that after 4 iterations it's beyond the end of the sector
2576059Samurai	 * and beyond a 256 byte boundary. We use the latter trick to check for
2586059Samurai	 * end of the loop without using an extra register (see start.5).
2596059Samurai	 */
26028679Sbrian		movw $(partbl+0x4),%bx		# Partition table (+4)
2616059Samurai		xorw %dx,%dx			# Item number
26254912Sbrian
2636059Samurai	/*
26454912Sbrian	 * Loop around on the partition table, printing values until we
26554912Sbrian	 * pass a 256 byte boundary.
2666059Samurai	 */
26731514Sbrianread_entry:	movb %ch,-0x4(%bx)		# Zero active flag (ch == 0)
26831514Sbrian		btw %dx,_FLAGS(%bp)		# Entry enabled?
26954912Sbrian		jnc next_entry			# No
27054912Sbrian		movb (%bx),%al			# Load type
2716059Samurai		test %al, %al			# skip empty partition
27254912Sbrian		jz next_entry
27354912Sbrian	/*
27431514Sbrian	 * Scan the table of bootable ids, which starts at %di and has
2756059Samurai	 * length TLEN. On a match, %di points to the element following the
27654912Sbrian	 * match; the corresponding offset to the description is $(TLEN-1)
27731514Sbrian	 * bytes ahead. If we don't find a match, we hit the 'unknown' entry.
27828679Sbrian	 */
27944792Sbrian		movw $bootable_ids,%di		# Lookup tables
28044792Sbrian		movb $(TLEN),%cl		# Number of entries
28144792Sbrian		repne				# Locate
28244792Sbrian		scasb				#  type
28344792Sbrian	/*
28444650Sbrian	 * Get the matching element in the next array.
28554912Sbrian	 * The byte at $(TLEN-1)(%di) contains the offset of the description
2866059Samurai	 * string from %di, so we add the number and print the string.
28754912Sbrian	 */
28831514Sbrian		addw $(TLEN-1), %di		# Adjust
2896059Samurai		movb (%di),%cl			# Partition
29031514Sbrian		addw %cx,%di			#  description
29131514Sbrian		callw putx			# Display it
292134789Sbrian
293134789Sbriannext_entry:	incw %dx			# Next item
29431514Sbrian		addb $0x10,%bl			# Next entry
295134789Sbrian		jnc read_entry			# Till done
29631514Sbrian	/*
29731514Sbrian	 * We are past a 256 byte boundary: the partition table is finished.
29831514Sbrian	 * Add one to the drive number and check it is valid.
299134789Sbrian	 * Note that if we started from a floppy, %dl was 0 so we still
30031514Sbrian	 * get an entry for the next drive, which is the first Hard Disk.
30131514Sbrian	 */
30231514Sbrian		popw %ax			# Drive number
30331514Sbrian		subb $0x80-0x1,%al		# Does next
30431514Sbrian		cmpb NHRDRV,%al			#  drive exist? (from BIOS?)
305134789Sbrian		jb print_drive			# Yes
306134789Sbrian	/*
30731514Sbrian	 * If this is the only drive, don't display it as an option.
30894894Sbrian	 */
30931514Sbrian		decw %ax			# Already drive 0?
31031514Sbrian		jz print_prompt			# Yes
31131514Sbrian	/*
312134789Sbrian	 * If it was illegal or we cycled through them, go back to drive 0.
313134789Sbrian	 */
31431514Sbrian		xorb %al,%al			# Drive 0
31594894Sbrian	/*
31694894Sbrian	 * Whatever drive we selected, make it an ascii digit and save it
31731514Sbrian	 * back to the "nxtdrv" location in case we want to save it to disk.
31831514Sbrian	 * This digit is also part of the printed drive string, so add 0x80
31931514Sbrian	 * to indicate end of string.
32031514Sbrian	 */
32131514Sbrianprint_drive:	addb $'0'|0x80,%al		# Save next
32231514Sbrian		movb %al,_NXTDRV(%bp)		#  drive number
32331514Sbrian		movw $drive,%di			# Display
32436285Sbrian		callw putx			#  item
32531514Sbrian	/*
32678411Sbrian	 * Menu is complete, display a prompt followed by current selection.
32778411Sbrian	 * 'decw %si' makes the register point to the space after 'Default: '
32831514Sbrian	 * so we do not see an extra CRLF on the screen.
32994894Sbrian	 */
33031514Sbrianprint_prompt:	movw $prompt,%si		# Display
33136285Sbrian		callw putstr			#  prompt
33231514Sbrian		movb _OPT(%bp),%dl		# Display
33331514Sbrian		decw %si			#  default
33431514Sbrian		callw putkey			#  key
33531514Sbrian		jmp start_input			# Skip beep
33631514Sbrian
33779165Sbrian/*
33836285Sbrian * Here we have the code waiting for user input or a timeout.
33994894Sbrian */
34031514Sbrianbeep:		movb $ASCII_BEL,%al		# Input error, print or beep
34136285Sbrian		callw putchr
34231514Sbrian
34331514Sbrianstart_input:
34431514Sbrian	/*
34531514Sbrian	 * Actual Start of input loop.  Take note of time
346	 */
347		xorb %ah,%ah			# BIOS: Get
348		int $0x1a			#  system time
349		movw %dx,%di			# Ticks when
350		addw _TICKS(%bp),%di		#  timeout
351read_key:
352	/*
353	 * Busy loop, looking for keystrokes but keeping one eye on the time.
354	 */
355#ifndef SIO
356		movb $0x1,%ah			# BIOS: Check
357		int $0x16			#  for keypress
358#else /* SIO */
359		movb $0x03,%ah			# BIOS: Read COM
360		call bioscom
361		testb $0x01,%ah			# Check line status
362						# (bit 1 indicates input)
363#endif /* SIO */
364		jnz got_key 			# Have input
365		xorb %ah,%ah			# BIOS: int 0x1a, 00
366		int $0x1a			#  get system time
367		cmpw %di,%dx			# Timeout?
368		jb read_key			# No
369
370	/*
371	 * Timed out or default selection
372	 */
373use_default:	movb _OPT(%bp),%al		# Load default
374		jmp check_selection		# Join common code
375
376	/*
377	 * Get the keystroke.
378	 * ENTER or CR confirm the current selection (same as a timeout).
379	 * Otherwise convert F1..F6 (or '1'..'6') to 0..5 and check if the
380	 * selection is valid.
381	 * The SIO code uses ascii chars, the console code uses scancodes.
382	 */
383got_key:
384#ifndef SIO
385		xorb %ah,%ah			# BIOS: int 0x16, 00
386		int $0x16			#  get keypress
387		movb %ah,%al			# move scan code to %al
388		cmpb $KEY_ENTER,%al
389#else
390		movb $0x02,%ah			# BIOS: Receive
391		call bioscom
392		cmpb $ASCII_CR,%al
393#endif
394		je use_default			# enter -> default
395	/*
396	 * Check if the key is acceptable, and loop back if not.
397	 * The console (non-SIO) code looks at scancodes and accepts
398	 * both F1..F6 and 1..6 (the latter costs 6 bytes of code),
399	 * relying on the fact that F1..F6 have higher scancodes than 1..6
400	 * The SIO code only takes 1..6
401	 */
402#ifdef SIO /* SIO mode, use ascii values */
403		subb $'1',%al			# Subtract '1' ascii code
404#else /*  console mode -- use scancodes */
405		subb $KEY_F1,%al		/* Subtract F1 scan code */
406#if !defined(ONLY_F_KEYS)
407		cmpb $0x5,%al			# F1..F6
408		jna 3f				# Yes
409		subb $(KEY_1 - KEY_F1),%al	# Less #1 scan code
410	3:
411#endif /* ONLY_F_KEYS */
412#endif /* SIO */
413		cmpb $0x5,%al			# F1..F6 or 1..6 ?
414#ifdef PXE /* enable PXE/INT18 using F6 */
415		jne 1f;
416		int $0x18			# found F6, try INT18
417	1:
418#endif /* PXE */
419		jae beep			# Not in F1..F5, beep
420
421check_selection:
422	/*
423	 * We have a selection.  If it's a bad selection go back to complain.
424	 * The bits in MNUOPT were set when the options were printed.
425	 * Anything not printed is not an option.
426	 */
427		cbtw				# Extend (%ah=0 used later)
428		btw %ax,_MNUOPT(%bp)	 	# Option enabled?
429		jnc beep			# No
430	/*
431	 * Save the info in the original tables
432	 * for rewriting to the disk.
433	 */
434		movb %al,_OPT(%bp)		# Save option
435
436	/*
437	 * Make %si and %bx point to the fake partition at LBA 0 (CHS 0:0:1).
438	 * Because the correct address is already in %bp, just use it.
439	 * Set %dl with the drive number saved in byte 0.
440	 * If we have pressed F5 or 5, then this is a good, fake value
441	 * to present to the next stage boot code.
442	 */
443		movw %bp,%si			# Partition for write
444		movb (%si),%dl			# Drive number, saved above
445		movw %si,%bx			# Partition for read
446		cmpb $0x4,%al			# F5/#5 pressed?
447		pushf				# Save results for later
448		je 1f				# Yes, F5
449
450	/*
451	 * F1..F4 was pressed, so make %bx point to the currently
452	 * selected partition, and leave the drive number unchanged.
453	 */
454		shlb $0x4,%al			# Point to
455		addw $partbl,%ax		#  selected
456		xchgw %bx,%ax	 		#  partition
457		movb $0x80,(%bx)		# Flag active
458	/*
459	 * If not asked to do a write-back (flags 0x40) don't do one.
460	 * Around the call, save the partition pointer to %bx and
461	 * restore to %si which is where the next stage expects it.
462	 */
463	1:	pushw %bx			# Save
464		testb $NOUPDATE,_FLAGS(%bp)	# No updates?
465		jnz 2f				# skip update
466		movw $start,%bx			# Data to write
467		movb $0x3,%ah			# Write sector
468		callw intx13			#  to disk
469	2:	popw %si			# Restore
470
471	/*
472	 * If going to next drive, replace drive with selected one.
473	 * Remember to un-ascii it. Hey 0x80 is already set, cool!
474	 */
475		popf				# Restore %al test results
476		jne 3f				# If not F5/#5
477		movb _NXTDRV(%bp),%dl		# Next drive
478		subb $'0',%dl			#  number
479	/*
480	 * Load selected bootsector to the LOAD location in RAM. If read
481	 * fails or there is no 0x55aa marker, treat it as a bad selection.
482	 */
483	3:	movw $LOAD,%bx			# Address for read
484		movb $0x2,%ah			# Read sector
485		callw intx13			#  from disk
486		jc beep				# If error
487		cmpw $MAGIC,0x1fe(%bx)		# Bootable?
488		jne beep			# No
489		pushw %si			# Save ptr to selected part.
490		callw putn			# Leave some space
491		popw %si			# Restore, next stage uses it
492		jmp *%bx			# Invoke bootstrap
493
494/*
495 * Display routines
496 * putkey	prints the option selected in %dl (F1..F5 or 1..5) followed by
497 *		the string at %si
498 * putx:	print the option in %dl followed by the string at %di
499 *		also record the drive as valid.
500 * putn:	print a crlf
501 * putstr:	print the string at %si
502 * putchr:	print the char in al
503 */
504
505/*
506 * Display the option and record the drive as valid in the options.
507 * That last point is done using the btsw instruction which does
508 * a test and set. We don't care for the test part.
509 */
510putx:		btsw %dx,_MNUOPT(%bp)		# Enable menu option
511		movw $item,%si			# Display
512		callw putkey			#  key
513		movw %di,%si			# Display the rest
514		callw putstr			# Display string
515
516putn:		movw $crlf,%si			# To next line
517		jmp putstr
518
519putkey:
520#ifndef SIO
521		movb $'F',%al			# Display
522		callw putchr			#  'F'
523#endif
524		movb $'1',%al			# Prepare
525		addb %dl,%al			#  digit
526
527putstr.1:	callw putchr			# Display char
528putstr:		lodsb				# Get byte
529		testb $0x80,%al 		# End of string?
530		jz putstr.1			# No
531		andb $~0x80,%al 		# Clear MSB then print last
532
533putchr:
534#ifndef SIO
535		pushw %bx			# Save
536		movw $0x7,%bx	 		# Page:attribute
537		movb $0xe,%ah			# BIOS: Display
538		int $0x10			#  character
539		popw %bx			# Restore
540#else /* SIO */
541		movb $0x01,%ah			# BIOS: Send
542bioscom:
543		pushw %dx			# Save
544		xorw %dx,%dx 			# Use COM1
545		int $0x14			#  Character
546		popw %dx			# Restore
547#endif /* SIO */
548		retw				# To caller
549
550/* One-sector disk I/O routine */
551
552/*
553 * %dl: drive, %si partition entry, %es:%bx transfer buffer.
554 * Load the CHS values and possibly the LBA address from the block
555 * at %si, and use the appropriate method to load the sector.
556 * Don't use packet mode for a floppy.
557 */
558intx13:						# Prepare CHS parameters
559		movb 0x1(%si),%dh		# Load head
560		movw 0x2(%si),%cx		# Load cylinder:sector
561		movb $0x1,%al			# Sector count
562		pushw %si			# Save
563		movw %sp,%di			# Save
564#ifndef CHECK_DRIVE				/* floppy support */
565		testb %dl, %dl			# is this a floppy ?
566		jz 1f				# Yes, use CHS mode
567#endif
568		testb $USEPACKET,_FLAGS(%bp)	# Use packet interface?
569		jz 1f				# No
570		pushl $0x0			# Set the
571		pushl 0x8(%si)			# LBA address
572		pushw %es			# Set the transfer
573		pushw %bx			#  buffer address
574		push  $0x1			# Block count
575		push  $0x10			# Packet size
576		movw %sp,%si			# Packet pointer
577		decw %ax			# Verify off
578		orb $0x40,%ah			# Use disk packet
579	1:	int $0x13			# BIOS: Disk I/O
580		movw %di,%sp			# Restore
581		popw %si			# Restore
582		retw				# To caller
583
584/*
585 * Various menu strings. 'item' goes after 'prompt' to save space.
586 * Also use shorter versions to make room for the PXE/INT18 code.
587 */
588#ifdef PXE
589prompt:		.ascii "\nBoot:"
590item:		.ascii " ";	     .byte ' '|0x80
591#else
592prompt:		.ascii "\nDefault:"
593item:		.ascii "  ";	     .byte ' '|0x80
594#endif
595crlf:		.ascii "\r";	     .byte '\n'|0x80
596
597/* Partition type tables */
598
599bootable_ids:
600	/*
601	 * These values indicate bootable types we know about.
602	 * Corresponding descriptions are at desc_ofs:
603	 * Entries don't need to be sorted.
604	 */
605		.byte 0x1, 0x6, 0x7, 0xb, 0xc
606#ifndef SAVE_MEMORY
607		.byte 0xe
608#endif
609		.byte 0x83, 0xa5, 0xa6, 0xa9, 0x4
610#ifndef SAVE_MORE_MEMORY
611		.byte 0x5, 0xf
612#endif
613
614desc_ofs:
615	/*
616	 * Offsets that match the known types above, used to point to the
617	 * actual partition name. The last entry must point to os_misc,
618	 * which is used for non-matching names.
619	 */
620		.byte os_dos-.			#   1, FAT12 DOS
621		.byte os_dos-.			#   6, FAT16 <32M, DOS/WIN
622		.byte os_win-.			#   7, FAT16 >=32M Windows
623		.byte os_win-.			#  11, FAT32
624		.byte os_win-.			#  12, FAT32-LBA
625#ifndef SAVE_MEMORY
626		.byte os_win-.			#  14, FAT16-LBA
627#endif
628		.byte os_linux-.		# 131, Linux
629		.byte os_freebsd-.		# 165, FreeBSD
630		.byte os_bsd-.			# 166, OpenBSD
631		.byte os_bsd-.			# 169, NetBSD
632		.byte os_dos-.			#   4, FAT16 < 32M
633#ifndef SAVE_MORE_MEMORY
634		.byte os_ext-.			#   5, DOS Ext
635		.byte os_ext-.			#  15, DOS Ext-LBA
636#endif
637
638		.byte os_misc-. 		# Unknown
639
640	/*
641	 * And here are the strings themselves. The last byte of
642	 * the string has bit 7 set.
643	 */
644os_misc:	.byte '?'|0x80
645os_dos:
646#ifndef SAVE_MEMORY	/* DOS string only if room */
647		.ascii "DO";   .byte 'S'|0x80
648#endif
649os_win:		.ascii "WI";   .byte 'N'|0x80
650os_linux:	.ascii "Linu"; .byte 'x'|0x80
651os_freebsd:	.ascii "Free"
652os_bsd:		.ascii "BS";   .byte 'D'|0x80
653#ifndef SAVE_MORE_MEMORY
654os_ext:		.ascii "EX";   .byte 'T'|0x80
655#endif
656
657		.org (0x200 + B0_OFF),0x90
658/*
659 * The boot0 version 1.0 parameter table.
660 * Do not move it nor change the "Drive " string, boot0cfg
661 * uses its offset and content to identify the boot sector.
662 * The other fields are sometimes changed before writing back to the drive
663 * Be especially careful that nxtdrv: must come after drive:, as it
664 * is part of the same string.
665 */
666drive:		.ascii "Drive "
667nxtdrv:		.byte 0x0			# Next drive number
668opt:		.byte 0x0			# Option
669setdrv_num:	.byte 0x80			# Drive to force
670flags:		.byte FLAGS			# Flags
671#ifdef VOLUME_SERIAL
672		.byte 0xa8,0xa8,0xa8,0xa8       # Volume Serial Number
673#endif
674ticks:		.word TICKS			# Delay
675
676		.org PRT_OFF
677/*
678 * Here is the 64 byte partition table that fdisk would fiddle with.
679 */
680partbl:		.fill 0x40,0x1,0x0		# Partition table
681		.word MAGIC			# Magic number
682		.org 0x200			# again, safety check
683endblock:
684