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