199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 2008 Luigi Rizzo (mostly documentation)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 2002 Bruce M. Simpson
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 1998 Robert Nordier
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Redistribution and use in source and binary forms are freely
199767f8919635c4928607450d9e0abb932109ceToomas Soome * permitted provided that the above copyright notice and this
199767f8919635c4928607450d9e0abb932109ceToomas Soome * paragraph and the following disclaimer are duplicated in all
199767f8919635c4928607450d9e0abb932109ceToomas Soome * such forms.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This software is provided "AS IS" and without any express or
199767f8919635c4928607450d9e0abb932109ceToomas Soome * implied warranties, including, without limitation, the implied
199767f8919635c4928607450d9e0abb932109ceToomas Soome * warranties of merchantability and fitness for a particular
199767f8919635c4928607450d9e0abb932109ceToomas Soome * purpose.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * $FreeBSD$
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* build options: */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef SIO /* use serial console on COM1. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef PXE /* enable PXE/INT18 booting with F6 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define SAVE_MORE_MEMORY
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef CHECK_DRIVE /* make sure we boot from a HD. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef ONLY_F_KEYS /* Only F1..F6, no digits on console */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef VOLUME_SERIAL /* support Volume serial number */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define B0_BASE 0x1ae /* move the internal data area */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define SAVE_MEMORY
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define B0_BASE 0x1b2
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef TEST /* enable some test code */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define SAVE_MEMORY
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define SAVE_MORE_MEMORY
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Note - this code uses many tricks to save space and fit in one sector.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This includes using side effects of certain instructions, reusing
199767f8919635c4928607450d9e0abb932109ceToomas Soome * register values from previous operations, etc.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Be extremely careful when changing the code, even for simple things.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * BOOT BLOCK STRUCTURE
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This code implements a Master Boot Record (MBR) for an Intel/PC disk.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * It is 512 bytes long and it is normally loaded by the BIOS (or another
199767f8919635c4928607450d9e0abb932109ceToomas Soome * bootloader) at 0:0x7c00. This code depends on %cs:%ip being 0:0x7c00
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The initial chunk of instructions is used as a signature by external
199767f8919635c4928607450d9e0abb932109ceToomas Soome * tools (e.g. boot0cfg) which can manipulate the block itself.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The area at offset 0x1b2 contains a magic string ('Drive '), also
199767f8919635c4928607450d9e0abb932109ceToomas Soome * used as a signature to detect the block, and some variables that can
199767f8919635c4928607450d9e0abb932109ceToomas Soome * be updated by boot0cfg (and optionally written back to the disk).
199767f8919635c4928607450d9e0abb932109ceToomas Soome * These variables control the operation of the bootloader itself,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * e.g. which partitions to enable, the timeout, the use of LBA
199767f8919635c4928607450d9e0abb932109ceToomas Soome * (called 'packet') or CHS mode, whether to force a drive number,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * and whether to write back the user's selection back to disk.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * As in every Master Boot Record, the partition table is at 0x1be,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * made of four 16-byte entries each containing:
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OFF SIZE DESCRIPTION
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 0 1 status (0x80: bootable, 0: non bootable)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1 3 start sector CHS
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 8:head, 6:sector, 2:cyl bit 9..8, 8:cyl bit 7..0
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 4 1 partition type
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 5 3 end sector CHS
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 8 4 LBA of first sector
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 12 4 partition size in sectors
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * and followed by the two bytes 0x55, 0xAA (MBR signature).
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * BOOT BLOCK OPERATION
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * On entry, the registers contain the following values:
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * %cs:%ip 0:0x7c00
199767f8919635c4928607450d9e0abb932109ceToomas Soome * %dl drive number (0x80, 0x81, ... )
199767f8919635c4928607450d9e0abb932109ceToomas Soome * %si pointer to the partition table from which we were loaded.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Some boot code (e.g. syslinux) use this info to relocate
199767f8919635c4928607450d9e0abb932109ceToomas Soome * themselves, so we want to pass a valid one to the next stage.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * NOTE: the use of %si is not a standard.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This boot block first relocates itself at a different address (0:0x600),
199767f8919635c4928607450d9e0abb932109ceToomas Soome * to free the space at 0:0x7c00 for the next stage boot block.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * It then initializes some memory at 0:0x800 and above (pointed by %bp)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * to store the original drive number (%dl) passed to us, and to construct a
199767f8919635c4928607450d9e0abb932109ceToomas Soome * fake partition entry. The latter is used by the disk I/O routine and,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * in some cases, passed in %si to the next stage boot code.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The variables at 0x1b2 are accessed as negative offsets from %bp.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * After the relocation, the code scans the partition table printing
199767f8919635c4928607450d9e0abb932109ceToomas Soome * out enabled partition or disks, and waits for user input.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * When a partition is selected, or a timeout expires, the currently
199767f8919635c4928607450d9e0abb932109ceToomas Soome * selected partition is used to load the next stage boot code,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * %dl and %si are set appropriately as when we were called, and
199767f8919635c4928607450d9e0abb932109ceToomas Soome * control is transferred to the newly loaded code at 0:0x7c00.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * CONSTANTS
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * NHRDRV is the address in segment 0 where the BIOS writes the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * total number of hard disks in the system.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * LOAD is the original load address and cannot be changed.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ORIGIN is the relocation address. If you change it, you also need
199767f8919635c4928607450d9e0abb932109ceToomas Soome * to change the value passed to the linker in the Makefile
199767f8919635c4928607450d9e0abb932109ceToomas Soome * PRT_OFF is the location of the partition table (from the MBR standard).
199767f8919635c4928607450d9e0abb932109ceToomas Soome * B0_OFF is the location of the data area, known to boot0cfg so
199767f8919635c4928607450d9e0abb932109ceToomas Soome * it cannot be changed. Computed as a negative offset from 0x200
199767f8919635c4928607450d9e0abb932109ceToomas Soome * MAGIC is the signature of a boot block.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome .set NHRDRV,0x475 # Number of hard drives
199767f8919635c4928607450d9e0abb932109ceToomas Soome .set ORIGIN,0x600 # Execution address
199767f8919635c4928607450d9e0abb932109ceToomas Soome .set LOAD,0x7c00 # Load address
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome .set PRT_OFF,0x1be # Partition table
199767f8919635c4928607450d9e0abb932109ceToomas Soome .set B0_OFF,(B0_BASE-0x200) # Offset of boot0 data
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome .set MAGIC,0xaa55 # Magic: bootable
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome .set KEY_ENTER,0x1c # Enter key scan code
199767f8919635c4928607450d9e0abb932109ceToomas Soome .set KEY_F1,0x3b # F1 key scan code
199767f8919635c4928607450d9e0abb932109ceToomas Soome .set KEY_1,0x02 # #1 key scan code
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome .set ASCII_BEL,'#' # ASCII code for <BEL>
199767f8919635c4928607450d9e0abb932109ceToomas Soome .set ASCII_CR,0x0D # ASCII code for <CR>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Offsets of variables in the block at B0_OFF, and in the volatile
199767f8919635c4928607450d9e0abb932109ceToomas Soome * data area, computed as displacement from %bp.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * We need to define them as constant as the assembler cannot
199767f8919635c4928607450d9e0abb932109ceToomas Soome * compute them in its single pass.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome .set _NXTDRV, B0_OFF+6 # Next drive
199767f8919635c4928607450d9e0abb932109ceToomas Soome .set _OPT, B0_OFF+7 # Default option
199767f8919635c4928607450d9e0abb932109ceToomas Soome .set _SETDRV, B0_OFF+8 # Drive to force
199767f8919635c4928607450d9e0abb932109ceToomas Soome .set _FLAGS, B0_OFF+9 # Flags
199767f8919635c4928607450d9e0abb932109ceToomas Soome .set SETDRV, 0x20 # the 'setdrv' flag
199767f8919635c4928607450d9e0abb932109ceToomas Soome .set NOUPDATE, 0x40 # the 'noupdate' flag
199767f8919635c4928607450d9e0abb932109ceToomas Soome .set USEPACKET, 0x80 # the 'packet' flag
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* ticks is at a fixed position */
199767f8919635c4928607450d9e0abb932109ceToomas Soome .set _TICKS, (PRT_OFF - 0x200 - 2) # Timeout ticks
199767f8919635c4928607450d9e0abb932109ceToomas Soome .set _MNUOPT, 0x10 # Saved menu entries
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome .set TLEN, (desc_ofs - bootable_ids) # size of bootable ids
199767f8919635c4928607450d9e0abb932109ceToomas Soome .globl start # Entry point
199767f8919635c4928607450d9e0abb932109ceToomas Soome .code16 # This runs in real mode
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * MAIN ENTRY POINT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Initialise segments and registers to known values.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * segments start at 0.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The stack is immediately below the address we were loaded to.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * NOTE: the initial section of the code (up to movw $LOAD,%sp)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * is used by boot0cfg, together with the 'Drive ' string and
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the 0x55, 0xaa at the end, as an identifier for version 1.0
199767f8919635c4928607450d9e0abb932109ceToomas Soome * of the boot code. Do not change it.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * In version 1.0 the parameter table (_NEXTDRV etc) is at 0x1b9
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestart: cld # String ops inc
199767f8919635c4928607450d9e0abb932109ceToomas Soome xorw %ax,%ax # Zero
199767f8919635c4928607450d9e0abb932109ceToomas Soome movw %ax,%es # Address
199767f8919635c4928607450d9e0abb932109ceToomas Soome movw %ax,%ds # data
199767f8919635c4928607450d9e0abb932109ceToomas Soome movw %ax,%ss # Set up
199767f8919635c4928607450d9e0abb932109ceToomas Soome movw $LOAD,%sp # stack
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copy this code to the address it was linked for, 0x600 by default.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome movw %sp,%si # Source
199767f8919635c4928607450d9e0abb932109ceToomas Soome movw $start,%di # Destination
199767f8919635c4928607450d9e0abb932109ceToomas Soome movw $0x100,%cx # Word count
199767f8919635c4928607450d9e0abb932109ceToomas Soome rep # Relocate
199767f8919635c4928607450d9e0abb932109ceToomas Soome movsw # code
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * After the code, (i.e. at %di+0, 0x800) create a partition entry,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * initialized to LBA 0 / CHS 0:0:1.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Set %bp to point to the partition and also, with negative offsets,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * to the variables embedded in the bootblock (nextdrv and so on).
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome movw %di,%bp # Address variables
199767f8919635c4928607450d9e0abb932109ceToomas Soome movb $0x8,%cl # Words to clear
199767f8919635c4928607450d9e0abb932109ceToomas Soome rep # Zero
199767f8919635c4928607450d9e0abb932109ceToomas Soome stosw # them
199767f8919635c4928607450d9e0abb932109ceToomas Soome incb -0xe(%di) # Set the S field to 1
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome jmp main-LOAD+ORIGIN # Jump to relocated code
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomemain:
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if defined(SIO) && COMSPEED != 0
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Init the serial port. bioscom preserves the driver number in DX.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome movw $COMSPEED,%ax # defined by Makefile
199767f8919635c4928607450d9e0abb932109ceToomas Soome callw bioscom
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * If the 'setdrv' flag is set in the boot sector, use the drive
199767f8919635c4928607450d9e0abb932109ceToomas Soome * number from the boot sector at 'setdrv_num'.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Optionally, do the same if the BIOS gives us an invalid number
199767f8919635c4928607450d9e0abb932109ceToomas Soome * (note though that the override prevents booting from a floppy
199767f8919635c4928607450d9e0abb932109ceToomas Soome * or a ZIP/flash drive in floppy emulation).
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The test costs 4 bytes of code so it is disabled by default.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome testb $SETDRV,_FLAGS(%bp) # Set drive number?
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifndef CHECK_DRIVE /* disable drive checks */
199767f8919635c4928607450d9e0abb932109ceToomas Soome jz save_curdrive # no, use the default
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else
199767f8919635c4928607450d9e0abb932109ceToomas Soome jnz disable_update # Yes
199767f8919635c4928607450d9e0abb932109ceToomas Soome testb %dl,%dl # Drive number valid?
199767f8919635c4928607450d9e0abb932109ceToomas Soome js save_curdrive # Possibly (0x80 set)
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Disable updates if the drive number is forced.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomedisable_update: orb $NOUPDATE,_FLAGS(%bp) # Disable updates
199767f8919635c4928607450d9e0abb932109ceToomas Soome movb _SETDRV(%bp),%dl # Use stored drive number
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Whatever drive we decided to use, store it at (%bp). The byte
199767f8919635c4928607450d9e0abb932109ceToomas Soome * is normally used for the state of the partition (0x80 or 0x00),
199767f8919635c4928607450d9e0abb932109ceToomas Soome * but we abuse it as it is very convenient to access at offset 0.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The value is read back after 'check_selection'
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomesave_curdrive: movb %dl, (%bp) # Save drive number
199767f8919635c4928607450d9e0abb932109ceToomas Soome pushw %dx # Also in the stack
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef TEST /* test code, print internal bios drive */
199767f8919635c4928607450d9e0abb932109ceToomas Soome rolb $1, %dl
199767f8919635c4928607450d9e0abb932109ceToomas Soome movw $drive, %si
199767f8919635c4928607450d9e0abb932109ceToomas Soome call putkey
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome callw putn # Print a newline
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Start out with a pointer to the 4th byte of the first table entry
199767f8919635c4928607450d9e0abb932109ceToomas Soome * so that after 4 iterations it's beyond the end of the sector
199767f8919635c4928607450d9e0abb932109ceToomas Soome * and beyond a 256 byte boundary. We use the latter trick to check for
199767f8919635c4928607450d9e0abb932109ceToomas Soome * end of the loop without using an extra register (see start.5).
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome movw $(partbl+0x4),%bx # Partition table (+4)
199767f8919635c4928607450d9e0abb932109ceToomas Soome xorw %dx,%dx # Item number
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Loop around on the partition table, printing values until we
199767f8919635c4928607450d9e0abb932109ceToomas Soome * pass a 256 byte boundary.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeread_entry: movb %ch,-0x4(%bx) # Zero active flag (ch == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome btw %dx,_FLAGS(%bp) # Entry enabled?
199767f8919635c4928607450d9e0abb932109ceToomas Soome jnc next_entry # No
199767f8919635c4928607450d9e0abb932109ceToomas Soome movb (%bx),%al # Load type
199767f8919635c4928607450d9e0abb932109ceToomas Soome test %al, %al # skip empty partition
199767f8919635c4928607450d9e0abb932109ceToomas Soome jz next_entry
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Scan the table of bootable ids, which starts at %di and has
199767f8919635c4928607450d9e0abb932109ceToomas Soome * length TLEN. On a match, %di points to the element following the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * match; the corresponding offset to the description is $(TLEN-1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * bytes ahead. We use a count of TLEN+1 so if we don't find a match
199767f8919635c4928607450d9e0abb932109ceToomas Soome * within the first TLEN entries, we hit the 'unknown' entry.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome movw $bootable_ids,%di # Lookup tables
199767f8919635c4928607450d9e0abb932109ceToomas Soome movb $(TLEN+1),%cl # Number of entries
199767f8919635c4928607450d9e0abb932109ceToomas Soome repne # Locate
199767f8919635c4928607450d9e0abb932109ceToomas Soome scasb # type
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Get the matching element in the next array.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The byte at $(TLEN-1)(%di) contains the offset of the description
199767f8919635c4928607450d9e0abb932109ceToomas Soome * string from %di, so we add the number and print the string.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome addw $(TLEN-1), %di # Adjust
199767f8919635c4928607450d9e0abb932109ceToomas Soome movb (%di),%cl # Partition
199767f8919635c4928607450d9e0abb932109ceToomas Soome addw %cx,%di # description
199767f8919635c4928607450d9e0abb932109ceToomas Soome callw putx # Display it
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomenext_entry: incw %dx # Next item
199767f8919635c4928607450d9e0abb932109ceToomas Soome addb $0x10,%bl # Next entry
199767f8919635c4928607450d9e0abb932109ceToomas Soome jnc read_entry # Till done
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * We are past a 256 byte boundary: the partition table is finished.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Add one to the drive number and check it is valid.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Note that if we started from a floppy, %dl was 0 so we still
199767f8919635c4928607450d9e0abb932109ceToomas Soome * get an entry for the next drive, which is the first Hard Disk.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome popw %ax # Drive number
199767f8919635c4928607450d9e0abb932109ceToomas Soome subb $0x80-0x1,%al # Does next
199767f8919635c4928607450d9e0abb932109ceToomas Soome cmpb NHRDRV,%al # drive exist? (from BIOS?)
199767f8919635c4928607450d9e0abb932109ceToomas Soome jb print_drive # Yes
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * If this is the only drive, don't display it as an option.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome decw %ax # Already drive 0?
199767f8919635c4928607450d9e0abb932109ceToomas Soome jz print_prompt # Yes
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * If it was illegal or we cycled through them, go back to drive 0.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome xorb %al,%al # Drive 0
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Whatever drive we selected, make it an ascii digit and save it
199767f8919635c4928607450d9e0abb932109ceToomas Soome * back to the "nxtdrv" location in case we want to save it to disk.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This digit is also part of the printed drive string, so add 0x80
199767f8919635c4928607450d9e0abb932109ceToomas Soome * to indicate end of string.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeprint_drive: addb $'0'|0x80,%al # Save next
199767f8919635c4928607450d9e0abb932109ceToomas Soome movb %al,_NXTDRV(%bp) # drive number
199767f8919635c4928607450d9e0abb932109ceToomas Soome movw $drive,%di # Display
199767f8919635c4928607450d9e0abb932109ceToomas Soome callw putx # item
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Menu is complete, display a prompt followed by current selection.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 'decw %si' makes the register point to the space after 'Boot: '
199767f8919635c4928607450d9e0abb932109ceToomas Soome * so we do not see an extra CRLF on the screen.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeprint_prompt: movw $prompt,%si # Display
199767f8919635c4928607450d9e0abb932109ceToomas Soome callw putstr # prompt
199767f8919635c4928607450d9e0abb932109ceToomas Soome movb _OPT(%bp),%dl # Display
199767f8919635c4928607450d9e0abb932109ceToomas Soome decw %si # default
199767f8919635c4928607450d9e0abb932109ceToomas Soome callw putkey # key
199767f8919635c4928607450d9e0abb932109ceToomas Soome jmp start_input # Skip beep
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Here we have the code waiting for user input or a timeout.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomebeep: movb $ASCII_BEL,%al # Input error, print or beep
199767f8919635c4928607450d9e0abb932109ceToomas Soome callw putchr
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestart_input:
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Actual Start of input loop. Take note of time
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome xorb %ah,%ah # BIOS: Get
199767f8919635c4928607450d9e0abb932109ceToomas Soome int $0x1a # system time
199767f8919635c4928607450d9e0abb932109ceToomas Soome movw %dx,%di # Ticks when
199767f8919635c4928607450d9e0abb932109ceToomas Soome addw _TICKS(%bp),%di # timeout
199767f8919635c4928607450d9e0abb932109ceToomas Soomeread_key:
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Busy loop, looking for keystrokes but keeping one eye on the time.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifndef SIO
199767f8919635c4928607450d9e0abb932109ceToomas Soome movb $0x1,%ah # BIOS: Check
199767f8919635c4928607450d9e0abb932109ceToomas Soome int $0x16 # for keypress
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else /* SIO */
199767f8919635c4928607450d9e0abb932109ceToomas Soome movb $0x03,%ah # BIOS: Read COM
199767f8919635c4928607450d9e0abb932109ceToomas Soome call bioscom
199767f8919635c4928607450d9e0abb932109ceToomas Soome testb $0x01,%ah # Check line status
199767f8919635c4928607450d9e0abb932109ceToomas Soome # (bit 1 indicates input)
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif /* SIO */
199767f8919635c4928607450d9e0abb932109ceToomas Soome jnz got_key # Have input
199767f8919635c4928607450d9e0abb932109ceToomas Soome xorb %ah,%ah # BIOS: int 0x1a, 00
199767f8919635c4928607450d9e0abb932109ceToomas Soome int $0x1a # get system time
199767f8919635c4928607450d9e0abb932109ceToomas Soome cmpw %di,%dx # Timeout?
199767f8919635c4928607450d9e0abb932109ceToomas Soome jb read_key # No
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Timed out or default selection
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeuse_default: movb _OPT(%bp),%al # Load default
199767f8919635c4928607450d9e0abb932109ceToomas Soome orb $NOUPDATE,_FLAGS(%bp) # Disable updates
199767f8919635c4928607450d9e0abb932109ceToomas Soome jmp check_selection # Join common code
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Get the keystroke.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ENTER or CR confirm the current selection (same as a timeout).
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Otherwise convert F1..F6 (or '1'..'6') to 0..5 and check if the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * selection is valid.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The SIO code uses ascii chars, the console code uses scancodes.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomegot_key:
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifndef SIO
199767f8919635c4928607450d9e0abb932109ceToomas Soome xorb %ah,%ah # BIOS: int 0x16, 00
199767f8919635c4928607450d9e0abb932109ceToomas Soome int $0x16 # get keypress
199767f8919635c4928607450d9e0abb932109ceToomas Soome movb %ah,%al # move scan code to %al
199767f8919635c4928607450d9e0abb932109ceToomas Soome cmpb $KEY_ENTER,%al
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else
199767f8919635c4928607450d9e0abb932109ceToomas Soome movb $0x02,%ah # BIOS: Receive
199767f8919635c4928607450d9e0abb932109ceToomas Soome call bioscom
199767f8919635c4928607450d9e0abb932109ceToomas Soome cmpb $ASCII_CR,%al
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome je use_default # enter -> default
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Check if the key is acceptable, and loop back if not.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The console (non-SIO) code looks at scancodes and accepts
199767f8919635c4928607450d9e0abb932109ceToomas Soome * both F1..F6 and 1..6 (the latter costs 6 bytes of code),
199767f8919635c4928607450d9e0abb932109ceToomas Soome * relying on the fact that F1..F6 have higher scancodes than 1..6
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The SIO code only takes 1..6
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef SIO /* SIO mode, use ascii values */
199767f8919635c4928607450d9e0abb932109ceToomas Soome subb $'1',%al # Subtract '1' ascii code
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else /* console mode -- use scancodes */
199767f8919635c4928607450d9e0abb932109ceToomas Soome subb $KEY_F1,%al /* Subtract F1 scan code */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if !defined(ONLY_F_KEYS)
199767f8919635c4928607450d9e0abb932109ceToomas Soome cmpb $0x5,%al # F1..F6
199767f8919635c4928607450d9e0abb932109ceToomas Soome jna 3f # Yes
199767f8919635c4928607450d9e0abb932109ceToomas Soome subb $(KEY_1 - KEY_F1),%al # Less #1 scan code
199767f8919635c4928607450d9e0abb932109ceToomas Soome 3:
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif /* ONLY_F_KEYS */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif /* SIO */
199767f8919635c4928607450d9e0abb932109ceToomas Soomecheck_selection:
199767f8919635c4928607450d9e0abb932109ceToomas Soome cmpb $0x5,%al # F1..F6 or 1..6 ?
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef PXE /* enable PXE/INT18 using F6 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome jne 1f;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int $0x18 # found F6, try INT18
199767f8919635c4928607450d9e0abb932109ceToomas Soome 1:
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif /* PXE */
199767f8919635c4928607450d9e0abb932109ceToomas Soome jae beep # Not in F1..F5, beep
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * We have a selection. If it's a bad selection go back to complain.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The bits in MNUOPT were set when the options were printed.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Anything not printed is not an option.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome cbtw # Extend (%ah=0 used later)
199767f8919635c4928607450d9e0abb932109ceToomas Soome btw %ax,_MNUOPT(%bp) # Option enabled?
199767f8919635c4928607450d9e0abb932109ceToomas Soome jnc beep # No
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Save the info in the original tables
199767f8919635c4928607450d9e0abb932109ceToomas Soome * for rewriting to the disk.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome movb %al,_OPT(%bp) # Save option
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Make %si and %bx point to the fake partition at LBA 0 (CHS 0:0:1).
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Because the correct address is already in %bp, just use it.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Set %dl with the drive number saved in byte 0.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * If we have pressed F5 or 5, then this is a good, fake value
199767f8919635c4928607450d9e0abb932109ceToomas Soome * to present to the next stage boot code.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome movw %bp,%si # Partition for write
199767f8919635c4928607450d9e0abb932109ceToomas Soome movb (%si),%dl # Drive number, saved above
199767f8919635c4928607450d9e0abb932109ceToomas Soome movw %si,%bx # Partition for read
199767f8919635c4928607450d9e0abb932109ceToomas Soome cmpb $0x4,%al # F5/#5 pressed?
199767f8919635c4928607450d9e0abb932109ceToomas Soome pushf # Save results for later
199767f8919635c4928607450d9e0abb932109ceToomas Soome je 1f # Yes, F5
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * F1..F4 was pressed, so make %bx point to the currently
199767f8919635c4928607450d9e0abb932109ceToomas Soome * selected partition, and leave the drive number unchanged.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome shlb $0x4,%al # Point to
199767f8919635c4928607450d9e0abb932109ceToomas Soome addw $partbl,%ax # selected
199767f8919635c4928607450d9e0abb932109ceToomas Soome xchgw %bx,%ax # partition
199767f8919635c4928607450d9e0abb932109ceToomas Soome movb $0x80,(%bx) # Flag active
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * If not asked to do a write-back (flags 0x40) don't do one.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Around the call, save the partition pointer to %bx and
199767f8919635c4928607450d9e0abb932109ceToomas Soome * restore to %si which is where the next stage expects it.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome 1: pushw %bx # Save
199767f8919635c4928607450d9e0abb932109ceToomas Soome testb $NOUPDATE,_FLAGS(%bp) # No updates?
199767f8919635c4928607450d9e0abb932109ceToomas Soome jnz 2f # skip update
199767f8919635c4928607450d9e0abb932109ceToomas Soome movw $start,%bx # Data to write
199767f8919635c4928607450d9e0abb932109ceToomas Soome movb $0x3,%ah # Write sector
199767f8919635c4928607450d9e0abb932109ceToomas Soome callw intx13 # to disk
199767f8919635c4928607450d9e0abb932109ceToomas Soome 2: popw %si # Restore
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * If going to next drive, replace drive with selected one.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Remember to un-ascii it. Hey 0x80 is already set, cool!
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome popf # Restore %al test results
199767f8919635c4928607450d9e0abb932109ceToomas Soome jne 3f # If not F5/#5
199767f8919635c4928607450d9e0abb932109ceToomas Soome movb _NXTDRV(%bp),%dl # Next drive
199767f8919635c4928607450d9e0abb932109ceToomas Soome subb $'0',%dl # number
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Load selected bootsector to the LOAD location in RAM. If read
199767f8919635c4928607450d9e0abb932109ceToomas Soome * fails or there is no 0x55aa marker, treat it as a bad selection.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome 3: movw $LOAD,%bx # Address for read
199767f8919635c4928607450d9e0abb932109ceToomas Soome movb $0x2,%ah # Read sector
199767f8919635c4928607450d9e0abb932109ceToomas Soome callw intx13 # from disk
199767f8919635c4928607450d9e0abb932109ceToomas Soome jc beep # If error
199767f8919635c4928607450d9e0abb932109ceToomas Soome cmpw $MAGIC,0x1fe(%bx) # Bootable?
199767f8919635c4928607450d9e0abb932109ceToomas Soome jne beep # No
199767f8919635c4928607450d9e0abb932109ceToomas Soome pushw %si # Save ptr to selected part.
199767f8919635c4928607450d9e0abb932109ceToomas Soome callw putn # Leave some space
199767f8919635c4928607450d9e0abb932109ceToomas Soome popw %si # Restore, next stage uses it
199767f8919635c4928607450d9e0abb932109ceToomas Soome jmp *%bx # Invoke bootstrap
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Display routines
199767f8919635c4928607450d9e0abb932109ceToomas Soome * putkey prints the option selected in %dl (F1..F5 or 1..5) followed by
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the string at %si
199767f8919635c4928607450d9e0abb932109ceToomas Soome * putx: print the option in %dl followed by the string at %di
199767f8919635c4928607450d9e0abb932109ceToomas Soome * also record the drive as valid.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * putn: print a crlf
199767f8919635c4928607450d9e0abb932109ceToomas Soome * putstr: print the string at %si
199767f8919635c4928607450d9e0abb932109ceToomas Soome * putchr: print the char in al
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Display the option and record the drive as valid in the options.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * That last point is done using the btsw instruction which does
199767f8919635c4928607450d9e0abb932109ceToomas Soome * a test and set. We don't care for the test part.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeputx: btsw %dx,_MNUOPT(%bp) # Enable menu option
199767f8919635c4928607450d9e0abb932109ceToomas Soome movw $item,%si # Display
199767f8919635c4928607450d9e0abb932109ceToomas Soome callw putkey # key
199767f8919635c4928607450d9e0abb932109ceToomas Soome movw %di,%si # Display the rest
199767f8919635c4928607450d9e0abb932109ceToomas Soome callw putstr # Display string
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeputn: movw $crlf,%si # To next line
199767f8919635c4928607450d9e0abb932109ceToomas Soome jmp putstr
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeputkey:
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifndef SIO
199767f8919635c4928607450d9e0abb932109ceToomas Soome movb $'F',%al # Display
199767f8919635c4928607450d9e0abb932109ceToomas Soome callw putchr # 'F'
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome movb $'1',%al # Prepare
199767f8919635c4928607450d9e0abb932109ceToomas Soome addb %dl,%al # digit
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeputstr.1: callw putchr # Display char
199767f8919635c4928607450d9e0abb932109ceToomas Soomeputstr: lodsb # Get byte
199767f8919635c4928607450d9e0abb932109ceToomas Soome testb $0x80,%al # End of string?
199767f8919635c4928607450d9e0abb932109ceToomas Soome jz putstr.1 # No
199767f8919635c4928607450d9e0abb932109ceToomas Soome andb $~0x80,%al # Clear MSB then print last
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeputchr:
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifndef SIO
199767f8919635c4928607450d9e0abb932109ceToomas Soome pushw %bx # Save
199767f8919635c4928607450d9e0abb932109ceToomas Soome movw $0x7,%bx # Page:attribute
199767f8919635c4928607450d9e0abb932109ceToomas Soome movb $0xe,%ah # BIOS: Display
199767f8919635c4928607450d9e0abb932109ceToomas Soome int $0x10 # character
199767f8919635c4928607450d9e0abb932109ceToomas Soome popw %bx # Restore
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else /* SIO */
199767f8919635c4928607450d9e0abb932109ceToomas Soome movb $0x01,%ah # BIOS: Send character
199767f8919635c4928607450d9e0abb932109ceToomas Soomebioscom:
199767f8919635c4928607450d9e0abb932109ceToomas Soome pushw %dx # Save
199767f8919635c4928607450d9e0abb932109ceToomas Soome xorw %dx,%dx # Use COM1
199767f8919635c4928607450d9e0abb932109ceToomas Soome int $0x14 # BIOS: Serial I/O
199767f8919635c4928607450d9e0abb932109ceToomas Soome popw %dx # Restore
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif /* SIO */
199767f8919635c4928607450d9e0abb932109ceToomas Soome retw # To caller
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* One-sector disk I/O routine */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * %dl: drive, %si partition entry, %es:%bx transfer buffer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Load the CHS values and possibly the LBA address from the block
199767f8919635c4928607450d9e0abb932109ceToomas Soome * at %si, and use the appropriate method to load the sector.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Don't use packet mode for a floppy.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeintx13: # Prepare CHS parameters
199767f8919635c4928607450d9e0abb932109ceToomas Soome movb 0x1(%si),%dh # Load head
199767f8919635c4928607450d9e0abb932109ceToomas Soome movw 0x2(%si),%cx # Load cylinder:sector
199767f8919635c4928607450d9e0abb932109ceToomas Soome movb $0x1,%al # Sector count
199767f8919635c4928607450d9e0abb932109ceToomas Soome pushw %si # Save
199767f8919635c4928607450d9e0abb932109ceToomas Soome movw %sp,%di # Save
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifndef CHECK_DRIVE /* floppy support */
199767f8919635c4928607450d9e0abb932109ceToomas Soome testb %dl, %dl # is this a floppy ?
199767f8919635c4928607450d9e0abb932109ceToomas Soome jz 1f # Yes, use CHS mode
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome testb $USEPACKET,_FLAGS(%bp) # Use packet interface?
199767f8919635c4928607450d9e0abb932109ceToomas Soome jz 1f # No
199767f8919635c4928607450d9e0abb932109ceToomas Soome pushl $0x0 # Set the
199767f8919635c4928607450d9e0abb932109ceToomas Soome pushl 0x8(%si) # LBA address
199767f8919635c4928607450d9e0abb932109ceToomas Soome pushw %es # Set the transfer
199767f8919635c4928607450d9e0abb932109ceToomas Soome pushw %bx # buffer address
199767f8919635c4928607450d9e0abb932109ceToomas Soome push $0x1 # Block count
199767f8919635c4928607450d9e0abb932109ceToomas Soome push $0x10 # Packet size
199767f8919635c4928607450d9e0abb932109ceToomas Soome movw %sp,%si # Packet pointer
199767f8919635c4928607450d9e0abb932109ceToomas Soome decw %ax # Verify off
199767f8919635c4928607450d9e0abb932109ceToomas Soome orb $0x40,%ah # Use disk packet
199767f8919635c4928607450d9e0abb932109ceToomas Soome 1: int $0x13 # BIOS: Disk I/O
199767f8919635c4928607450d9e0abb932109ceToomas Soome movw %di,%sp # Restore
199767f8919635c4928607450d9e0abb932109ceToomas Soome popw %si # Restore
199767f8919635c4928607450d9e0abb932109ceToomas Soome retw # To caller
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Various menu strings. 'item' goes after 'prompt' to save space.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Also use shorter versions to make room for the PXE/INT18 code.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeprompt:
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef PXE
199767f8919635c4928607450d9e0abb932109ceToomas Soome .ascii "\nF6 PXE\r"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome .ascii "\nBoot:"
199767f8919635c4928607450d9e0abb932109ceToomas Soomeitem: .ascii " "; .byte ' '|0x80
199767f8919635c4928607450d9e0abb932109ceToomas Soomecrlf: .ascii "\r"; .byte '\n'|0x80
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Partition type tables */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomebootable_ids:
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * These values indicate bootable types we know about.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Corresponding descriptions are at desc_ofs:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Entries don't need to be sorted.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome .byte 0x83, 0xa5, 0xa6, 0xa9, 0x06, 0x07, 0x0b
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifndef SAVE_MORE_MEMORY
199767f8919635c4928607450d9e0abb932109ceToomas Soome .byte 0x05 # extended partition
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifndef SAVE_MEMORY /* other DOS partitions */
199767f8919635c4928607450d9e0abb932109ceToomas Soome .byte 0x01 # FAT12
199767f8919635c4928607450d9e0abb932109ceToomas Soome .byte 0x04 # FAT16 < 32M
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomedesc_ofs:
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Offsets that match the known types above, used to point to the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * actual partition name. The last entry must point to os_misc,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * which is used for non-matching names.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome .byte os_linux-. # 131, Linux
199767f8919635c4928607450d9e0abb932109ceToomas Soome .byte os_freebsd-. # 165, FreeBSD
199767f8919635c4928607450d9e0abb932109ceToomas Soome .byte os_bsd-. # 166, OpenBSD
199767f8919635c4928607450d9e0abb932109ceToomas Soome .byte os_bsd-. # 169, NetBSD
199767f8919635c4928607450d9e0abb932109ceToomas Soome .byte os_dos-. # 6, FAT16 >= 32M
199767f8919635c4928607450d9e0abb932109ceToomas Soome .byte os_win-. # 7, NTFS
199767f8919635c4928607450d9e0abb932109ceToomas Soome .byte os_win-. # 11, FAT32
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifndef SAVE_MORE_MEMORY
199767f8919635c4928607450d9e0abb932109ceToomas Soome .byte os_ext-. # 5, DOS Ext
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifndef SAVE_MEMORY
199767f8919635c4928607450d9e0abb932109ceToomas Soome .byte os_dos-. # 1, FAT12 DOS
199767f8919635c4928607450d9e0abb932109ceToomas Soome .byte os_dos-. # 4, FAT16 <32M
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome .byte os_misc-. # Unknown
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * And here are the strings themselves. The last byte of
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the string has bit 7 set.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeos_misc: .byte '?'|0x80
199767f8919635c4928607450d9e0abb932109ceToomas Soomeos_dos:
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifndef SAVE_MORE_MEMORY /* 'DOS' remapped to 'WIN' if no room */
199767f8919635c4928607450d9e0abb932109ceToomas Soome .ascii "DO"; .byte 'S'|0x80
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soomeos_win: .ascii "Wi"; .byte 'n'|0x80
199767f8919635c4928607450d9e0abb932109ceToomas Soomeos_linux: .ascii "Linu"; .byte 'x'|0x80
199767f8919635c4928607450d9e0abb932109ceToomas Soomeos_freebsd: .ascii "Free"
199767f8919635c4928607450d9e0abb932109ceToomas Soomeos_bsd: .ascii "BS"; .byte 'D'|0x80
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifndef SAVE_MORE_MEMORY
199767f8919635c4928607450d9e0abb932109ceToomas Soomeos_ext: .ascii "EX"; .byte 'T'|0x80
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome .org (0x200 + B0_OFF),0x90
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The boot0 version 1.0 parameter table.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Do not move it nor change the "Drive " string, boot0cfg
199767f8919635c4928607450d9e0abb932109ceToomas Soome * uses its offset and content to identify the boot sector.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The other fields are sometimes changed before writing back to the drive
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Be especially careful that nxtdrv: must come after drive:, as it
199767f8919635c4928607450d9e0abb932109ceToomas Soome * is part of the same string.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomedrive: .ascii "Drive "
199767f8919635c4928607450d9e0abb932109ceToomas Soomenxtdrv: .byte 0x0 # Next drive number
199767f8919635c4928607450d9e0abb932109ceToomas Soomeopt: .byte 0x0 # Option
199767f8919635c4928607450d9e0abb932109ceToomas Soomesetdrv_num: .byte 0x80 # Drive to force
199767f8919635c4928607450d9e0abb932109ceToomas Soomeflags: .byte FLAGS # Flags
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef VOLUME_SERIAL
199767f8919635c4928607450d9e0abb932109ceToomas Soome .byte 0xa8,0xa8,0xa8,0xa8 # Volume Serial Number
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soometicks: .word TICKS # Delay
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome .org PRT_OFF
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Here is the 64 byte partition table that fdisk would fiddle with.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomepartbl: .fill 0x40,0x1,0x0 # Partition table
199767f8919635c4928607450d9e0abb932109ceToomas Soome .word MAGIC # Magic number
199767f8919635c4928607450d9e0abb932109ceToomas Soome .org 0x200 # again, safety check
199767f8919635c4928607450d9e0abb932109ceToomas Soomeendblock: