1ca987d46SWarner Losh/* 2ca987d46SWarner Losh * Copyright (c) 2008 Luigi Rizzo (mostly documentation) 3ca987d46SWarner Losh * Copyright (c) 2002 Bruce M. Simpson 4ca987d46SWarner Losh * Copyright (c) 1998 Robert Nordier 5ca987d46SWarner Losh * All rights reserved. 6ca987d46SWarner Losh * 7ca987d46SWarner Losh * Redistribution and use in source and binary forms are freely 8ca987d46SWarner Losh * permitted provided that the above copyright notice and this 9ca987d46SWarner Losh * paragraph and the following disclaimer are duplicated in all 10ca987d46SWarner Losh * such forms. 11ca987d46SWarner Losh * 12ca987d46SWarner Losh * This software is provided "AS IS" and without any express or 13ca987d46SWarner Losh * implied warranties, including, without limitation, the implied 14ca987d46SWarner Losh * warranties of merchantability and fitness for a particular 15ca987d46SWarner Losh * purpose. 16ca987d46SWarner Losh */ 17ca987d46SWarner Losh 18ca987d46SWarner Losh/* build options: */ 19ca987d46SWarner Losh 20ca987d46SWarner Losh#ifdef PXE /* enable PXE/INT18 booting with F6 */ 21ca987d46SWarner Losh#define SAVE_MORE_MEMORY 22ca987d46SWarner Losh#endif 23ca987d46SWarner Losh 24ca987d46SWarner Losh 25ca987d46SWarner Losh#ifdef VOLUME_SERIAL /* support Volume serial number */ 26ca987d46SWarner Losh#define B0_BASE 0x1ae /* move the internal data area */ 27ca987d46SWarner Losh#define SAVE_MEMORY 28ca987d46SWarner Losh#else 29ca987d46SWarner Losh#define B0_BASE 0x1b2 30ca987d46SWarner Losh#endif 31ca987d46SWarner Losh 32ca987d46SWarner Losh#ifdef TEST /* enable some test code */ 33ca987d46SWarner Losh#define SAVE_MEMORY 34ca987d46SWarner Losh#define SAVE_MORE_MEMORY 35ca987d46SWarner Losh#endif 36ca987d46SWarner Losh 37ca987d46SWarner Losh/* 38ca987d46SWarner Losh * Note - this code uses many tricks to save space and fit in one sector. 39ca987d46SWarner Losh * This includes using side effects of certain instructions, reusing 40ca987d46SWarner Losh * register values from previous operations, etc. 41ca987d46SWarner Losh * Be extremely careful when changing the code, even for simple things. 42ca987d46SWarner Losh */ 43ca987d46SWarner Losh 44ca987d46SWarner Losh/* 45ca987d46SWarner Losh * BOOT BLOCK STRUCTURE 46ca987d46SWarner Losh * 475b990a94SBaptiste Daroussin * This code implements a Master Boot Record (MBR) for an Intel/PC disk. 48ca987d46SWarner Losh * It is 512 bytes long and it is normally loaded by the BIOS (or another 49ca987d46SWarner Losh * bootloader) at 0:0x7c00. This code depends on %cs:%ip being 0:0x7c00 50ca987d46SWarner Losh * 51ca987d46SWarner Losh * The initial chunk of instructions is used as a signature by external 52ca987d46SWarner Losh * tools (e.g. boot0cfg) which can manipulate the block itself. 53ca987d46SWarner Losh * 54ca987d46SWarner Losh * The area at offset 0x1b2 contains a magic string ('Drive '), also 55ca987d46SWarner Losh * used as a signature to detect the block, and some variables that can 56ca987d46SWarner Losh * be updated by boot0cfg (and optionally written back to the disk). 57ca987d46SWarner Losh * These variables control the operation of the bootloader itself, 58ca987d46SWarner Losh * e.g. which partitions to enable, the timeout, the use of LBA 59ca987d46SWarner Losh * (called 'packet') or CHS mode, whether to force a drive number, 60ca987d46SWarner Losh * and whether to write back the user's selection back to disk. 61ca987d46SWarner Losh * 625b990a94SBaptiste Daroussin * As in every Master Boot Record, the partition table is at 0x1be, 63ca987d46SWarner Losh * made of four 16-byte entries each containing: 64ca987d46SWarner Losh * 65ca987d46SWarner Losh * OFF SIZE DESCRIPTION 66ca987d46SWarner Losh * 0 1 status (0x80: bootable, 0: non bootable) 67ca987d46SWarner Losh * 1 3 start sector CHS 68ca987d46SWarner Losh * 8:head, 6:sector, 2:cyl bit 9..8, 8:cyl bit 7..0 69ca987d46SWarner Losh * 4 1 partition type 70ca987d46SWarner Losh * 5 3 end sector CHS 71ca987d46SWarner Losh * 8 4 LBA of first sector 72ca987d46SWarner Losh * 12 4 partition size in sectors 73ca987d46SWarner Losh * 74ca987d46SWarner Losh * and followed by the two bytes 0x55, 0xAA (MBR signature). 75ca987d46SWarner Losh */ 76ca987d46SWarner Losh 77ca987d46SWarner Losh 78ca987d46SWarner Losh/* 79ca987d46SWarner Losh * BOOT BLOCK OPERATION 80ca987d46SWarner Losh * 81ca987d46SWarner Losh * On entry, the registers contain the following values: 82ca987d46SWarner Losh * 83ca987d46SWarner Losh * %cs:%ip 0:0x7c00 84ca987d46SWarner Losh * %dl drive number (0x80, 0x81, ... ) 85ca987d46SWarner Losh * %si pointer to the partition table from which we were loaded. 86ca987d46SWarner Losh * Some boot code (e.g. syslinux) use this info to relocate 87ca987d46SWarner Losh * themselves, so we want to pass a valid one to the next stage. 88ca987d46SWarner Losh * NOTE: the use of %si is not a standard. 89ca987d46SWarner Losh * 90ca987d46SWarner Losh * This boot block first relocates itself at a different address (0:0x600), 91ca987d46SWarner Losh * to free the space at 0:0x7c00 for the next stage boot block. 92ca987d46SWarner Losh * 93ca987d46SWarner Losh * It then initializes some memory at 0:0x800 and above (pointed by %bp) 94ca987d46SWarner Losh * to store the original drive number (%dl) passed to us, and to construct a 95ca987d46SWarner Losh * fake partition entry. The latter is used by the disk I/O routine and, 96ca987d46SWarner Losh * in some cases, passed in %si to the next stage boot code. 97ca987d46SWarner Losh * 98ca987d46SWarner Losh * The variables at 0x1b2 are accessed as negative offsets from %bp. 99ca987d46SWarner Losh * 100ca987d46SWarner Losh * After the relocation, the code scans the partition table printing 101ca987d46SWarner Losh * out enabled partition or disks, and waits for user input. 102ca987d46SWarner Losh * 103ca987d46SWarner Losh * When a partition is selected, or a timeout expires, the currently 104ca987d46SWarner Losh * selected partition is used to load the next stage boot code, 105ca987d46SWarner Losh * %dl and %si are set appropriately as when we were called, and 106ca987d46SWarner Losh * control is transferred to the newly loaded code at 0:0x7c00. 107ca987d46SWarner Losh */ 108ca987d46SWarner Losh 109ca987d46SWarner Losh/* 110ca987d46SWarner Losh * CONSTANTS 111ca987d46SWarner Losh * 112ca987d46SWarner Losh * NHRDRV is the address in segment 0 where the BIOS writes the 113ca987d46SWarner Losh * total number of hard disks in the system. 114ca987d46SWarner Losh * LOAD is the original load address and cannot be changed. 115ca987d46SWarner Losh * ORIGIN is the relocation address. If you change it, you also need 116ca987d46SWarner Losh * to change the value passed to the linker in the Makefile 117ca987d46SWarner Losh * PRT_OFF is the location of the partition table (from the MBR standard). 118ca987d46SWarner Losh * B0_OFF is the location of the data area, known to boot0cfg so 119ca987d46SWarner Losh * it cannot be changed. Computed as a negative offset from 0x200 120ca987d46SWarner Losh * MAGIC is the signature of a boot block. 121ca987d46SWarner Losh */ 122ca987d46SWarner Losh 123ca987d46SWarner Losh .set NHRDRV,0x475 # Number of hard drives 124ca987d46SWarner Losh .set ORIGIN,0x600 # Execution address 125ca987d46SWarner Losh .set LOAD,0x7c00 # Load address 126ca987d46SWarner Losh 127ca987d46SWarner Losh .set PRT_OFF,0x1be # Partition table 128ca987d46SWarner Losh .set B0_OFF,(B0_BASE-0x200) # Offset of boot0 data 129ca987d46SWarner Losh 130ca987d46SWarner Losh .set MAGIC,0xaa55 # Magic: bootable 131ca987d46SWarner Losh 132ca987d46SWarner Losh .set KEY_ENTER,0x1c # Enter key scan code 133ca987d46SWarner Losh .set KEY_F1,0x3b # F1 key scan code 134ca987d46SWarner Losh .set KEY_1,0x02 # #1 key scan code 135ca987d46SWarner Losh 136ca987d46SWarner Losh .set ASCII_BEL,'#' # ASCII code for <BEL> 137ca987d46SWarner Losh .set ASCII_CR,0x0D # ASCII code for <CR> 138ca987d46SWarner Losh 139ca987d46SWarner Losh/* 140ca987d46SWarner Losh * Offsets of variables in the block at B0_OFF, and in the volatile 141ca987d46SWarner Losh * data area, computed as displacement from %bp. 142ca987d46SWarner Losh * We need to define them as constant as the assembler cannot 143ca987d46SWarner Losh * compute them in its single pass. 144ca987d46SWarner Losh */ 145ca987d46SWarner Losh .set _NXTDRV, B0_OFF+6 # Next drive 146ca987d46SWarner Losh .set _OPT, B0_OFF+7 # Default option 147ca987d46SWarner Losh .set _SETDRV, B0_OFF+8 # Drive to force 148ca987d46SWarner Losh .set _FLAGS, B0_OFF+9 # Flags 149ca987d46SWarner Losh .set SETDRV, 0x20 # the 'setdrv' flag 150ca987d46SWarner Losh .set NOUPDATE, 0x40 # the 'noupdate' flag 151ca987d46SWarner Losh .set USEPACKET, 0x80 # the 'packet' flag 152ca987d46SWarner Losh 153ca987d46SWarner Losh /* ticks is at a fixed position */ 154ca987d46SWarner Losh .set _TICKS, (PRT_OFF - 0x200 - 2) # Timeout ticks 155ca987d46SWarner Losh .set _MNUOPT, 0x10 # Saved menu entries 156ca987d46SWarner Losh 157ca987d46SWarner Losh .set TLEN, (desc_ofs - bootable_ids) # size of bootable ids 158ca987d46SWarner Losh .globl start # Entry point 159ca987d46SWarner Losh .code16 # This runs in real mode 160ca987d46SWarner Losh 161ca987d46SWarner Losh/* 162ca987d46SWarner Losh * MAIN ENTRY POINT 163ca987d46SWarner Losh * Initialise segments and registers to known values. 164ca987d46SWarner Losh * segments start at 0. 165ca987d46SWarner Losh * The stack is immediately below the address we were loaded to. 166ca987d46SWarner Losh * NOTE: the initial section of the code (up to movw $LOAD,%sp) 167ca987d46SWarner Losh * is used by boot0cfg, together with the 'Drive ' string and 168ca987d46SWarner Losh * the 0x55, 0xaa at the end, as an identifier for version 1.0 169ca987d46SWarner Losh * of the boot code. Do not change it. 170ca987d46SWarner Losh * In version 1.0 the parameter table (_NEXTDRV etc) is at 0x1b9 171ca987d46SWarner Losh */ 172ca987d46SWarner Loshstart: cld # String ops inc 173ca987d46SWarner Losh xorw %ax,%ax # Zero 174ca987d46SWarner Losh movw %ax,%es # Address 175ca987d46SWarner Losh movw %ax,%ds # data 176ca987d46SWarner Losh movw %ax,%ss # Set up 177ca987d46SWarner Losh movw $LOAD,%sp # stack 178ca987d46SWarner Losh 179ca987d46SWarner Losh /* 180ca987d46SWarner Losh * Copy this code to the address it was linked for, 0x600 by default. 181ca987d46SWarner Losh */ 182ca987d46SWarner Losh movw %sp,%si # Source 183ca987d46SWarner Losh movw $start,%di # Destination 184ca987d46SWarner Losh movw $0x100,%cx # Word count 185ca987d46SWarner Losh rep # Relocate 186ca987d46SWarner Losh movsw # code 187ca987d46SWarner Losh /* 188ca987d46SWarner Losh * After the code, (i.e. at %di+0, 0x800) create a partition entry, 189ca987d46SWarner Losh * initialized to LBA 0 / CHS 0:0:1. 190ca987d46SWarner Losh * Set %bp to point to the partition and also, with negative offsets, 191ca987d46SWarner Losh * to the variables embedded in the bootblock (nextdrv and so on). 192ca987d46SWarner Losh */ 193ca987d46SWarner Losh movw %di,%bp # Address variables 194ca987d46SWarner Losh movb $0x8,%cl # Words to clear 195ca987d46SWarner Losh rep # Zero 196ca987d46SWarner Losh stosw # them 197ca987d46SWarner Losh incb -0xe(%di) # Set the S field to 1 198ca987d46SWarner Losh 199ca987d46SWarner Losh jmp main-LOAD+ORIGIN # Jump to relocated code 200ca987d46SWarner Losh 201ca987d46SWarner Loshmain: 202ca987d46SWarner Losh#if defined(SIO) && COMSPEED != 0 203ca987d46SWarner Losh /* 204ca987d46SWarner Losh * Init the serial port. bioscom preserves the driver number in DX. 205ca987d46SWarner Losh */ 206ca987d46SWarner Losh movw $COMSPEED,%ax # defined by Makefile 207ca987d46SWarner Losh callw bioscom 208ca987d46SWarner Losh#endif 209ca987d46SWarner Losh 210ca987d46SWarner Losh /* 211ca987d46SWarner Losh * If the 'setdrv' flag is set in the boot sector, use the drive 212ca987d46SWarner Losh * number from the boot sector at 'setdrv_num'. 213ca987d46SWarner Losh * Optionally, do the same if the BIOS gives us an invalid number 214ca987d46SWarner Losh * (note though that the override prevents booting from a floppy 215ca987d46SWarner Losh * or a ZIP/flash drive in floppy emulation). 216ca987d46SWarner Losh * The test costs 4 bytes of code so it is disabled by default. 217ca987d46SWarner Losh */ 218ca987d46SWarner Losh testb $SETDRV,_FLAGS(%bp) # Set drive number? 219ca987d46SWarner Losh#ifndef CHECK_DRIVE /* disable drive checks */ 220ca987d46SWarner Losh jz save_curdrive # no, use the default 221ca987d46SWarner Losh#else 222ca987d46SWarner Losh jnz disable_update # Yes 223ca987d46SWarner Losh testb %dl,%dl # Drive number valid? 224ca987d46SWarner Losh js save_curdrive # Possibly (0x80 set) 225ca987d46SWarner Losh#endif 226ca987d46SWarner Losh /* 227ca987d46SWarner Losh * Disable updates if the drive number is forced. 228ca987d46SWarner Losh */ 229ca987d46SWarner Loshdisable_update: orb $NOUPDATE,_FLAGS(%bp) # Disable updates 230ca987d46SWarner Losh movb _SETDRV(%bp),%dl # Use stored drive number 231ca987d46SWarner Losh 232ca987d46SWarner Losh /* 233ca987d46SWarner Losh * Whatever drive we decided to use, store it at (%bp). The byte 234ca987d46SWarner Losh * is normally used for the state of the partition (0x80 or 0x00), 235ca987d46SWarner Losh * but we abuse it as it is very convenient to access at offset 0. 236ca987d46SWarner Losh * The value is read back after 'check_selection' 237ca987d46SWarner Losh */ 238ca987d46SWarner Loshsave_curdrive: movb %dl, (%bp) # Save drive number 239ca987d46SWarner Losh pushw %dx # Also in the stack 240ca987d46SWarner Losh#ifdef TEST /* test code, print internal bios drive */ 241ca987d46SWarner Losh rolb $1, %dl 242ca987d46SWarner Losh movw $drive, %si 243ca987d46SWarner Losh call putkey 244ca987d46SWarner Losh#endif 245ca987d46SWarner Losh callw putn # Print a newline 246ca987d46SWarner Losh /* 247ca987d46SWarner Losh * Start out with a pointer to the 4th byte of the first table entry 248ca987d46SWarner Losh * so that after 4 iterations it's beyond the end of the sector 249ca987d46SWarner Losh * and beyond a 256 byte boundary. We use the latter trick to check for 250ca987d46SWarner Losh * end of the loop without using an extra register (see start.5). 251ca987d46SWarner Losh */ 252ca987d46SWarner Losh movw $(partbl+0x4),%bx # Partition table (+4) 253ca987d46SWarner Losh xorw %dx,%dx # Item number 254ca987d46SWarner Losh 255ca987d46SWarner Losh /* 256ca987d46SWarner Losh * Loop around on the partition table, printing values until we 257ca987d46SWarner Losh * pass a 256 byte boundary. 258ca987d46SWarner Losh */ 259ca987d46SWarner Loshread_entry: movb %ch,-0x4(%bx) # Zero active flag (ch == 0) 260ca987d46SWarner Losh btw %dx,_FLAGS(%bp) # Entry enabled? 261ca987d46SWarner Losh jnc next_entry # No 262ca987d46SWarner Losh movb (%bx),%al # Load type 263ca987d46SWarner Losh test %al, %al # skip empty partition 264ca987d46SWarner Losh jz next_entry 265ca987d46SWarner Losh /* 266ca987d46SWarner Losh * Scan the table of bootable ids, which starts at %di and has 267ca987d46SWarner Losh * length TLEN. On a match, %di points to the element following the 268ca987d46SWarner Losh * match; the corresponding offset to the description is $(TLEN-1) 269ca987d46SWarner Losh * bytes ahead. We use a count of TLEN+1 so if we don't find a match 270ca987d46SWarner Losh * within the first TLEN entries, we hit the 'unknown' entry. 271ca987d46SWarner Losh */ 272ca987d46SWarner Losh movw $bootable_ids,%di # Lookup tables 273ca987d46SWarner Losh movb $(TLEN+1),%cl # Number of entries 274ca987d46SWarner Losh repne # Locate 275ca987d46SWarner Losh scasb # type 276ca987d46SWarner Losh /* 277ca987d46SWarner Losh * Get the matching element in the next array. 278ca987d46SWarner Losh * The byte at $(TLEN-1)(%di) contains the offset of the description 279ca987d46SWarner Losh * string from %di, so we add the number and print the string. 280ca987d46SWarner Losh */ 281ca987d46SWarner Losh addw $(TLEN-1), %di # Adjust 282ca987d46SWarner Losh movb (%di),%cl # Partition 283ca987d46SWarner Losh addw %cx,%di # description 284ca987d46SWarner Losh callw putx # Display it 285ca987d46SWarner Losh 286ca987d46SWarner Loshnext_entry: incw %dx # Next item 287ca987d46SWarner Losh addb $0x10,%bl # Next entry 288ca987d46SWarner Losh jnc read_entry # Till done 289ca987d46SWarner Losh /* 290ca987d46SWarner Losh * We are past a 256 byte boundary: the partition table is finished. 291ca987d46SWarner Losh * Add one to the drive number and check it is valid. 292ca987d46SWarner Losh * Note that if we started from a floppy, %dl was 0 so we still 293ca987d46SWarner Losh * get an entry for the next drive, which is the first Hard Disk. 294ca987d46SWarner Losh */ 295ca987d46SWarner Losh popw %ax # Drive number 296ca987d46SWarner Losh subb $0x80-0x1,%al # Does next 297ca987d46SWarner Losh cmpb NHRDRV,%al # drive exist? (from BIOS?) 298ca987d46SWarner Losh jb print_drive # Yes 299ca987d46SWarner Losh /* 300ca987d46SWarner Losh * If this is the only drive, don't display it as an option. 301ca987d46SWarner Losh */ 302ca987d46SWarner Losh decw %ax # Already drive 0? 303ca987d46SWarner Losh jz print_prompt # Yes 304ca987d46SWarner Losh /* 305ca987d46SWarner Losh * If it was illegal or we cycled through them, go back to drive 0. 306ca987d46SWarner Losh */ 307ca987d46SWarner Losh xorb %al,%al # Drive 0 308ca987d46SWarner Losh /* 309ca987d46SWarner Losh * Whatever drive we selected, make it an ascii digit and save it 310ca987d46SWarner Losh * back to the "nxtdrv" location in case we want to save it to disk. 311ca987d46SWarner Losh * This digit is also part of the printed drive string, so add 0x80 312ca987d46SWarner Losh * to indicate end of string. 313ca987d46SWarner Losh */ 314ca987d46SWarner Loshprint_drive: addb $'0'|0x80,%al # Save next 315ca987d46SWarner Losh movb %al,_NXTDRV(%bp) # drive number 316ca987d46SWarner Losh movw $drive,%di # Display 317ca987d46SWarner Losh callw putx # item 318ca987d46SWarner Losh /* 319ca987d46SWarner Losh * Menu is complete, display a prompt followed by current selection. 320ca987d46SWarner Losh * 'decw %si' makes the register point to the space after 'Boot: ' 321ca987d46SWarner Losh * so we do not see an extra CRLF on the screen. 322ca987d46SWarner Losh */ 323ca987d46SWarner Loshprint_prompt: movw $prompt,%si # Display 324ca987d46SWarner Losh callw putstr # prompt 325ca987d46SWarner Losh movb _OPT(%bp),%dl # Display 326ca987d46SWarner Losh decw %si # default 327ca987d46SWarner Losh callw putkey # key 328ca987d46SWarner Losh jmp start_input # Skip beep 329ca987d46SWarner Losh 330ca987d46SWarner Losh/* 331ca987d46SWarner Losh * Here we have the code waiting for user input or a timeout. 332ca987d46SWarner Losh */ 333ca987d46SWarner Loshbeep: movb $ASCII_BEL,%al # Input error, print or beep 334ca987d46SWarner Losh callw putchr 335ca987d46SWarner Losh 336ca987d46SWarner Loshstart_input: 337ca987d46SWarner Losh /* 338ca987d46SWarner Losh * Actual Start of input loop. Take note of time 339ca987d46SWarner Losh */ 340ca987d46SWarner Losh xorb %ah,%ah # BIOS: Get 341ca987d46SWarner Losh int $0x1a # system time 342ca987d46SWarner Losh movw %dx,%di # Ticks when 343ca987d46SWarner Losh addw _TICKS(%bp),%di # timeout 344ca987d46SWarner Loshread_key: 345ca987d46SWarner Losh /* 346ca987d46SWarner Losh * Busy loop, looking for keystrokes but keeping one eye on the time. 347ca987d46SWarner Losh */ 348ca987d46SWarner Losh#ifndef SIO 349ca987d46SWarner Losh movb $0x1,%ah # BIOS: Check 350ca987d46SWarner Losh int $0x16 # for keypress 351ca987d46SWarner Losh#else /* SIO */ 352ca987d46SWarner Losh movb $0x03,%ah # BIOS: Read COM 353ca987d46SWarner Losh call bioscom 354ca987d46SWarner Losh testb $0x01,%ah # Check line status 355ca987d46SWarner Losh # (bit 1 indicates input) 356ca987d46SWarner Losh#endif /* SIO */ 357ca987d46SWarner Losh jnz got_key # Have input 358ca987d46SWarner Losh xorb %ah,%ah # BIOS: int 0x1a, 00 359ca987d46SWarner Losh int $0x1a # get system time 360ca987d46SWarner Losh cmpw %di,%dx # Timeout? 361ca987d46SWarner Losh jb read_key # No 362ca987d46SWarner Losh 363ca987d46SWarner Losh /* 364ca987d46SWarner Losh * Timed out or default selection 365ca987d46SWarner Losh */ 366ca987d46SWarner Loshuse_default: movb _OPT(%bp),%al # Load default 367ca987d46SWarner Losh orb $NOUPDATE,_FLAGS(%bp) # Disable updates 368ca987d46SWarner Losh jmp check_selection # Join common code 369ca987d46SWarner Losh 370ca987d46SWarner Losh /* 371ca987d46SWarner Losh * Get the keystroke. 372ca987d46SWarner Losh * ENTER or CR confirm the current selection (same as a timeout). 373ca987d46SWarner Losh * Otherwise convert F1..F6 (or '1'..'6') to 0..5 and check if the 374ca987d46SWarner Losh * selection is valid. 375ca987d46SWarner Losh * The SIO code uses ascii chars, the console code uses scancodes. 376ca987d46SWarner Losh */ 377ca987d46SWarner Loshgot_key: 378ca987d46SWarner Losh#ifndef SIO 379ca987d46SWarner Losh xorb %ah,%ah # BIOS: int 0x16, 00 380ca987d46SWarner Losh int $0x16 # get keypress 381ca987d46SWarner Losh movb %ah,%al # move scan code to %al 382ca987d46SWarner Losh cmpb $KEY_ENTER,%al 383ca987d46SWarner Losh#else 384ca987d46SWarner Losh movb $0x02,%ah # BIOS: Receive 385ca987d46SWarner Losh call bioscom 386ca987d46SWarner Losh cmpb $ASCII_CR,%al 387ca987d46SWarner Losh#endif 388ca987d46SWarner Losh je use_default # enter -> default 389ca987d46SWarner Losh /* 390ca987d46SWarner Losh * Check if the key is acceptable, and loop back if not. 391ca987d46SWarner Losh * The console (non-SIO) code looks at scancodes and accepts 392ca987d46SWarner Losh * both F1..F6 and 1..6 (the latter costs 6 bytes of code), 393ca987d46SWarner Losh * relying on the fact that F1..F6 have higher scancodes than 1..6 394ca987d46SWarner Losh * The SIO code only takes 1..6 395ca987d46SWarner Losh */ 396ca987d46SWarner Losh#ifdef SIO /* SIO mode, use ascii values */ 397ca987d46SWarner Losh subb $'1',%al # Subtract '1' ascii code 398ca987d46SWarner Losh#else /* console mode -- use scancodes */ 399ca987d46SWarner Losh subb $KEY_F1,%al /* Subtract F1 scan code */ 400ca987d46SWarner Losh#if !defined(ONLY_F_KEYS) 401ca987d46SWarner Losh cmpb $0x5,%al # F1..F6 402ca987d46SWarner Losh jna 3f # Yes 403ca987d46SWarner Losh subb $(KEY_1 - KEY_F1),%al # Less #1 scan code 404ca987d46SWarner Losh 3: 405ca987d46SWarner Losh#endif /* ONLY_F_KEYS */ 406ca987d46SWarner Losh#endif /* SIO */ 407ca987d46SWarner Loshcheck_selection: 408ca987d46SWarner Losh cmpb $0x5,%al # F1..F6 or 1..6 ? 409ca987d46SWarner Losh#ifdef PXE /* enable PXE/INT18 using F6 */ 410ca987d46SWarner Losh jne 1f; 411ca987d46SWarner Losh int $0x18 # found F6, try INT18 412ca987d46SWarner Losh 1: 413ca987d46SWarner Losh#endif /* PXE */ 414ca987d46SWarner Losh jae beep # Not in F1..F5, beep 415ca987d46SWarner Losh 416ca987d46SWarner Losh /* 417ca987d46SWarner Losh * We have a selection. If it's a bad selection go back to complain. 418ca987d46SWarner Losh * The bits in MNUOPT were set when the options were printed. 419ca987d46SWarner Losh * Anything not printed is not an option. 420ca987d46SWarner Losh */ 421ca987d46SWarner Losh cbtw # Extend (%ah=0 used later) 422ca987d46SWarner Losh btw %ax,_MNUOPT(%bp) # Option enabled? 423ca987d46SWarner Losh jnc beep # No 424ca987d46SWarner Losh /* 425ca987d46SWarner Losh * Save the info in the original tables 426ca987d46SWarner Losh * for rewriting to the disk. 427ca987d46SWarner Losh */ 428ca987d46SWarner Losh movb %al,_OPT(%bp) # Save option 429ca987d46SWarner Losh 430ca987d46SWarner Losh /* 431ca987d46SWarner Losh * Make %si and %bx point to the fake partition at LBA 0 (CHS 0:0:1). 432ca987d46SWarner Losh * Because the correct address is already in %bp, just use it. 433ca987d46SWarner Losh * Set %dl with the drive number saved in byte 0. 434ca987d46SWarner Losh * If we have pressed F5 or 5, then this is a good, fake value 435ca987d46SWarner Losh * to present to the next stage boot code. 436ca987d46SWarner Losh */ 437ca987d46SWarner Losh movw %bp,%si # Partition for write 438ca987d46SWarner Losh movb (%si),%dl # Drive number, saved above 439ca987d46SWarner Losh movw %si,%bx # Partition for read 440ca987d46SWarner Losh cmpb $0x4,%al # F5/#5 pressed? 441ca987d46SWarner Losh pushf # Save results for later 442ca987d46SWarner Losh je 1f # Yes, F5 443ca987d46SWarner Losh 444ca987d46SWarner Losh /* 445ca987d46SWarner Losh * F1..F4 was pressed, so make %bx point to the currently 446ca987d46SWarner Losh * selected partition, and leave the drive number unchanged. 447ca987d46SWarner Losh */ 448ca987d46SWarner Losh shlb $0x4,%al # Point to 449ca987d46SWarner Losh addw $partbl,%ax # selected 450ca987d46SWarner Losh xchgw %bx,%ax # partition 451ca987d46SWarner Losh movb $0x80,(%bx) # Flag active 452ca987d46SWarner Losh /* 453ca987d46SWarner Losh * If not asked to do a write-back (flags 0x40) don't do one. 454ca987d46SWarner Losh * Around the call, save the partition pointer to %bx and 455ca987d46SWarner Losh * restore to %si which is where the next stage expects it. 456ca987d46SWarner Losh */ 457ca987d46SWarner Losh 1: pushw %bx # Save 458ca987d46SWarner Losh testb $NOUPDATE,_FLAGS(%bp) # No updates? 459ca987d46SWarner Losh jnz 2f # skip update 460ca987d46SWarner Losh movw $start,%bx # Data to write 461ca987d46SWarner Losh movb $0x3,%ah # Write sector 462ca987d46SWarner Losh callw intx13 # to disk 463ca987d46SWarner Losh 2: popw %si # Restore 464ca987d46SWarner Losh 465ca987d46SWarner Losh /* 466ca987d46SWarner Losh * If going to next drive, replace drive with selected one. 467ca987d46SWarner Losh * Remember to un-ascii it. Hey 0x80 is already set, cool! 468ca987d46SWarner Losh */ 469ca987d46SWarner Losh popf # Restore %al test results 470ca987d46SWarner Losh jne 3f # If not F5/#5 471ca987d46SWarner Losh movb _NXTDRV(%bp),%dl # Next drive 472ca987d46SWarner Losh subb $'0',%dl # number 473ca987d46SWarner Losh /* 474ca987d46SWarner Losh * Load selected bootsector to the LOAD location in RAM. If read 475ca987d46SWarner Losh * fails or there is no 0x55aa marker, treat it as a bad selection. 476ca987d46SWarner Losh */ 477ca987d46SWarner Losh 3: movw $LOAD,%bx # Address for read 478ca987d46SWarner Losh movb $0x2,%ah # Read sector 479ca987d46SWarner Losh callw intx13 # from disk 480ca987d46SWarner Losh jc beep # If error 481ca987d46SWarner Losh cmpw $MAGIC,0x1fe(%bx) # Bootable? 482ca987d46SWarner Losh jne beep # No 483ca987d46SWarner Losh pushw %si # Save ptr to selected part. 484ca987d46SWarner Losh callw putn # Leave some space 485ca987d46SWarner Losh popw %si # Restore, next stage uses it 486ca987d46SWarner Losh jmp *%bx # Invoke bootstrap 487ca987d46SWarner Losh 488ca987d46SWarner Losh/* 489ca987d46SWarner Losh * Display routines 490ca987d46SWarner Losh * putkey prints the option selected in %dl (F1..F5 or 1..5) followed by 491ca987d46SWarner Losh * the string at %si 492ca987d46SWarner Losh * putx: print the option in %dl followed by the string at %di 493ca987d46SWarner Losh * also record the drive as valid. 494ca987d46SWarner Losh * putn: print a crlf 495ca987d46SWarner Losh * putstr: print the string at %si 496ca987d46SWarner Losh * putchr: print the char in al 497ca987d46SWarner Losh */ 498ca987d46SWarner Losh 499ca987d46SWarner Losh/* 500ca987d46SWarner Losh * Display the option and record the drive as valid in the options. 501ca987d46SWarner Losh * That last point is done using the btsw instruction which does 502ca987d46SWarner Losh * a test and set. We don't care for the test part. 503ca987d46SWarner Losh */ 504ca987d46SWarner Loshputx: btsw %dx,_MNUOPT(%bp) # Enable menu option 505ca987d46SWarner Losh movw $item,%si # Display 506ca987d46SWarner Losh callw putkey # key 507ca987d46SWarner Losh movw %di,%si # Display the rest 508ca987d46SWarner Losh callw putstr # Display string 509ca987d46SWarner Losh 510ca987d46SWarner Loshputn: movw $crlf,%si # To next line 511ca987d46SWarner Losh jmp putstr 512ca987d46SWarner Losh 513ca987d46SWarner Loshputkey: 514ca987d46SWarner Losh#ifndef SIO 515ca987d46SWarner Losh movb $'F',%al # Display 516ca987d46SWarner Losh callw putchr # 'F' 517ca987d46SWarner Losh#endif 518ca987d46SWarner Losh movb $'1',%al # Prepare 519ca987d46SWarner Losh addb %dl,%al # digit 520ca987d46SWarner Losh 521ca987d46SWarner Loshputstr.1: callw putchr # Display char 522ca987d46SWarner Loshputstr: lodsb # Get byte 523ca987d46SWarner Losh testb $0x80,%al # End of string? 524ca987d46SWarner Losh jz putstr.1 # No 525ca987d46SWarner Losh andb $~0x80,%al # Clear MSB then print last 526ca987d46SWarner Losh 527ca987d46SWarner Loshputchr: 528ca987d46SWarner Losh#ifndef SIO 529ca987d46SWarner Losh pushw %bx # Save 530ca987d46SWarner Losh movw $0x7,%bx # Page:attribute 531ca987d46SWarner Losh movb $0xe,%ah # BIOS: Display 532ca987d46SWarner Losh int $0x10 # character 533ca987d46SWarner Losh popw %bx # Restore 534ca987d46SWarner Losh#else /* SIO */ 535ca987d46SWarner Losh movb $0x01,%ah # BIOS: Send character 536ca987d46SWarner Loshbioscom: 537ca987d46SWarner Losh pushw %dx # Save 538ca987d46SWarner Losh xorw %dx,%dx # Use COM1 539ca987d46SWarner Losh int $0x14 # BIOS: Serial I/O 540ca987d46SWarner Losh popw %dx # Restore 541ca987d46SWarner Losh#endif /* SIO */ 542ca987d46SWarner Losh retw # To caller 543ca987d46SWarner Losh 544ca987d46SWarner Losh/* One-sector disk I/O routine */ 545ca987d46SWarner Losh 546ca987d46SWarner Losh/* 547ca987d46SWarner Losh * %dl: drive, %si partition entry, %es:%bx transfer buffer. 548ca987d46SWarner Losh * Load the CHS values and possibly the LBA address from the block 549ca987d46SWarner Losh * at %si, and use the appropriate method to load the sector. 550ca987d46SWarner Losh * Don't use packet mode for a floppy. 551ca987d46SWarner Losh */ 552ca987d46SWarner Loshintx13: # Prepare CHS parameters 553ca987d46SWarner Losh movb 0x1(%si),%dh # Load head 554ca987d46SWarner Losh movw 0x2(%si),%cx # Load cylinder:sector 555ca987d46SWarner Losh movb $0x1,%al # Sector count 556ca987d46SWarner Losh pushw %si # Save 557ca987d46SWarner Losh movw %sp,%di # Save 558ca987d46SWarner Losh#ifndef CHECK_DRIVE /* floppy support */ 559ca987d46SWarner Losh testb %dl, %dl # is this a floppy ? 560ca987d46SWarner Losh jz 1f # Yes, use CHS mode 561ca987d46SWarner Losh#endif 562ca987d46SWarner Losh testb $USEPACKET,_FLAGS(%bp) # Use packet interface? 563ca987d46SWarner Losh jz 1f # No 564ca987d46SWarner Losh pushl $0x0 # Set the 565ca987d46SWarner Losh pushl 0x8(%si) # LBA address 566ca987d46SWarner Losh pushw %es # Set the transfer 567ca987d46SWarner Losh pushw %bx # buffer address 568ca987d46SWarner Losh push $0x1 # Block count 569ca987d46SWarner Losh push $0x10 # Packet size 570ca987d46SWarner Losh movw %sp,%si # Packet pointer 571ca987d46SWarner Losh decw %ax # Verify off 572ca987d46SWarner Losh orb $0x40,%ah # Use disk packet 573ca987d46SWarner Losh 1: int $0x13 # BIOS: Disk I/O 574ca987d46SWarner Losh movw %di,%sp # Restore 575ca987d46SWarner Losh popw %si # Restore 576ca987d46SWarner Losh retw # To caller 577ca987d46SWarner Losh 578ca987d46SWarner Losh/* 579ca987d46SWarner Losh * Various menu strings. 'item' goes after 'prompt' to save space. 580ca987d46SWarner Losh * Also use shorter versions to make room for the PXE/INT18 code. 581ca987d46SWarner Losh */ 582ca987d46SWarner Loshprompt: 583ca987d46SWarner Losh#ifdef PXE 584*4fa4693dSTatsuki Makino .ascii "F6 PXE\r" 585ca987d46SWarner Losh#endif 586ca987d46SWarner Losh .ascii "\nBoot:" 587ca987d46SWarner Loshitem: .ascii " "; .byte ' '|0x80 588ca987d46SWarner Loshcrlf: .ascii "\r"; .byte '\n'|0x80 589ca987d46SWarner Losh 590ca987d46SWarner Losh/* Partition type tables */ 591ca987d46SWarner Losh 592ca987d46SWarner Loshbootable_ids: 593ca987d46SWarner Losh /* 594ca987d46SWarner Losh * These values indicate bootable types we know about. 595ca987d46SWarner Losh * Corresponding descriptions are at desc_ofs: 596ca987d46SWarner Losh * Entries don't need to be sorted. 597ca987d46SWarner Losh */ 598ca987d46SWarner Losh .byte 0x83, 0xa5, 0xa6, 0xa9, 0x06, 0x07, 0x0b 599ca987d46SWarner Losh#ifndef SAVE_MORE_MEMORY 600ca987d46SWarner Losh .byte 0x05 # extended partition 601ca987d46SWarner Losh#endif 602ca987d46SWarner Losh#ifndef SAVE_MEMORY /* other DOS partitions */ 603ca987d46SWarner Losh .byte 0x01 # FAT12 604ca987d46SWarner Losh .byte 0x04 # FAT16 < 32M 605ca987d46SWarner Losh#endif 606ca987d46SWarner Losh 607ca987d46SWarner Loshdesc_ofs: 608ca987d46SWarner Losh /* 609ca987d46SWarner Losh * Offsets that match the known types above, used to point to the 610ca987d46SWarner Losh * actual partition name. The last entry must point to os_misc, 611ca987d46SWarner Losh * which is used for non-matching names. 612ca987d46SWarner Losh */ 613ca987d46SWarner Losh .byte os_linux-. # 131, Linux 614ca987d46SWarner Losh .byte os_freebsd-. # 165, FreeBSD 615ca987d46SWarner Losh .byte os_bsd-. # 166, OpenBSD 616ca987d46SWarner Losh .byte os_bsd-. # 169, NetBSD 617ca987d46SWarner Losh .byte os_dos-. # 6, FAT16 >= 32M 618ca987d46SWarner Losh .byte os_win-. # 7, NTFS 619ca987d46SWarner Losh .byte os_win-. # 11, FAT32 620ca987d46SWarner Losh 621ca987d46SWarner Losh#ifndef SAVE_MORE_MEMORY 622ca987d46SWarner Losh .byte os_ext-. # 5, DOS Ext 623ca987d46SWarner Losh#endif 624ca987d46SWarner Losh#ifndef SAVE_MEMORY 625ca987d46SWarner Losh .byte os_dos-. # 1, FAT12 DOS 626ca987d46SWarner Losh .byte os_dos-. # 4, FAT16 <32M 627ca987d46SWarner Losh#endif 628ca987d46SWarner Losh .byte os_misc-. # Unknown 629ca987d46SWarner Losh 630ca987d46SWarner Losh /* 631ca987d46SWarner Losh * And here are the strings themselves. The last byte of 632ca987d46SWarner Losh * the string has bit 7 set. 633ca987d46SWarner Losh */ 634ca987d46SWarner Loshos_misc: .byte '?'|0x80 635ca987d46SWarner Loshos_dos: 636ca987d46SWarner Losh#ifndef SAVE_MORE_MEMORY /* 'DOS' remapped to 'WIN' if no room */ 637ca987d46SWarner Losh .ascii "DO"; .byte 'S'|0x80 638ca987d46SWarner Losh#endif 639ca987d46SWarner Loshos_win: .ascii "Wi"; .byte 'n'|0x80 640ca987d46SWarner Loshos_linux: .ascii "Linu"; .byte 'x'|0x80 641ca987d46SWarner Loshos_freebsd: .ascii "Free" 642ca987d46SWarner Loshos_bsd: .ascii "BS"; .byte 'D'|0x80 643ca987d46SWarner Losh#ifndef SAVE_MORE_MEMORY 644ca987d46SWarner Loshos_ext: .ascii "EX"; .byte 'T'|0x80 645ca987d46SWarner Losh#endif 646ca987d46SWarner Losh 647ca987d46SWarner Losh .org (0x200 + B0_OFF),0x90 648ca987d46SWarner Losh/* 649ca987d46SWarner Losh * The boot0 version 1.0 parameter table. 650ca987d46SWarner Losh * Do not move it nor change the "Drive " string, boot0cfg 651ca987d46SWarner Losh * uses its offset and content to identify the boot sector. 652ca987d46SWarner Losh * The other fields are sometimes changed before writing back to the drive 653ca987d46SWarner Losh * Be especially careful that nxtdrv: must come after drive:, as it 654ca987d46SWarner Losh * is part of the same string. 655ca987d46SWarner Losh */ 656ca987d46SWarner Loshdrive: .ascii "Drive " 657ca987d46SWarner Loshnxtdrv: .byte 0x0 # Next drive number 658ca987d46SWarner Loshopt: .byte 0x0 # Option 659ca987d46SWarner Loshsetdrv_num: .byte 0x80 # Drive to force 660ca987d46SWarner Loshflags: .byte FLAGS # Flags 661ca987d46SWarner Losh#ifdef VOLUME_SERIAL 662ca987d46SWarner Losh .byte 0xa8,0xa8,0xa8,0xa8 # Volume Serial Number 663ca987d46SWarner Losh#endif 664ca987d46SWarner Loshticks: .word TICKS # Delay 665ca987d46SWarner Losh 666ca987d46SWarner Losh .org PRT_OFF 667ca987d46SWarner Losh/* 6683bd637afSEd Maste * Here is the 64 byte MBR partition table. 669ca987d46SWarner Losh */ 670ca987d46SWarner Loshpartbl: .fill 0x40,0x1,0x0 # Partition table 671ca987d46SWarner Losh .word MAGIC # Magic number 672ca987d46SWarner Losh .org 0x200 # again, safety check 673ca987d46SWarner Loshendblock: 674