boot0.S revision 139948
1/* 2 * Copyright (c) 2002 Bruce M. Simpson 3 * Copyright (c) 1998 Robert Nordier 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms are freely 7 * permitted provided that the above copyright notice and this 8 * paragraph and the following disclaimer are duplicated in all 9 * such forms. 10 * 11 * This software is provided "AS IS" and without any express or 12 * implied warranties, including, without limitation, the implied 13 * warranties of merchantability and fitness for a particular 14 * purpose. 15 * 16 * $FreeBSD: head/sys/boot/i386/boot0/boot0.S 139948 2005-01-09 23:30:35Z peadar $ 17 */ 18 19/* A 512-byte boot manager. */ 20#ifdef SIO 21/* ... using a serial console on COM1. */ 22#endif 23 24 .set NHRDRV,0x475 # Number of hard drives 25 .set ORIGIN,0x600 # Execution address 26 .set FAKE,0x800 # Partition entry 27 .set LOAD,0x7c00 # Load address 28 29 .set PRT_OFF,0x1be # Partition table 30 31 .set TBL0SZ,0x3 # Table 0 size 32 .set TBL1SZ,0xb # Table 1 size 33 34 .set MAGIC,0xaa55 # Magic: bootable 35 .set B0MAGIC,0xbb66 # Identification 36 37 .set KEY_ENTER,0x1c # Enter key scan code 38 .set KEY_F1,0x3b # F1 key scan code 39 .set KEY_1,0x02 # #1 key scan code 40 41 .set ASCII_BEL,0x07 # ASCII code for <BEL> 42 .set ASCII_CR,0x0D # ASCII code for <CR> 43 44/* 45 * Addresses in the sector of embedded data values. 46 * Accessed with negative offsets from the end of the relocated sector (%ebp). 47 */ 48 .set _NXTDRV,-0x48 # Next drive 49 .set _OPT,-0x47 # Default option 50 .set _SETDRV,-0x46 # Drive to force 51 .set _FLAGS,-0x45 # Flags 52 .set _TICKS,-0x44 # Timeout ticks 53 .set _FAKE,0x0 # Fake partition entry 54 .set _MNUOPT,0xc # Menu options 55 56 .globl start # Entry point 57 .code16 # This runs in real mode 58 59/* 60 * Initialise segments and registers to known values. 61 * segments start at 0. 62 * The stack is immediately below the address we were loaded to. 63 */ 64start: cld # String ops inc 65 xorw %ax,%ax # Zero 66 movw %ax,%es # Address 67 movw %ax,%ds # data 68 movw %ax,%ss # Set up 69 movw $LOAD,%sp # stack 70 71/* 72 * Copy this code to the address it was linked for 73 */ 74 movw %sp,%si # Source 75 movw $start,%di # Destination 76 movw $0x100,%cx # Word count 77 rep # Relocate 78 movsw # code 79/* 80 * Set address for variable space beyond code, and clear it. 81 * Notice that this is also used to point to the values embedded in the block, 82 * by using negative offsets. 83 */ 84 movw %di,%bp # Address variables 85 movb $0x8,%cl # Words to clear 86 rep # Zero 87 stosw # them 88/* 89 * Relocate to the new copy of the code. 90 */ 91 incb -0xe(%di) # Sector number 92 jmp main-LOAD+ORIGIN # To relocated code 93 94main: 95#if defined(SIO) && COMSPEED != 0 96/* 97 * Initialize the serial port. bioscom preserves the driver number in DX. 98 */ 99 movw COMSPEED,%ax # defined by Makefile 100 callw bioscom 101#endif 102/* 103 * Check what flags were loaded with us, specifically if a predefined drive 104 * number should be used. If what the bios gives us is bad, use the '0' in 105 * the block instead. 106 */ 107 testb $0x20,_FLAGS(%bp) # Set drive number? 108 jnz main.1 # Yes 109 testb %dl,%dl # Drive number valid? 110 js main.2 # Possibly (0x80 set) 111/* 112 * Only update the boot-sector when there is a valid drive number or 113 * the drive number is set manually. 114 */ 115 orb $0x40,_FLAGS(%bp) # Disable updates 116main.1: movb _SETDRV(%bp),%dl # Drive number to use 117/* 118 * Whatever we decided to use, now store it into the fake 119 * partition entry that lives in the data space above us. 120 */ 121main.2: movb %dl,_FAKE(%bp) # Save drive number 122 callw putn # To new line 123 pushw %dx # Save drive number 124/* 125 * Start out with a pointer to the 4th byte of the first table entry 126 * so that after 4 iterations it's beyond the end of the sector 127 * and beyond a 256 byte boundary and has overflowed 8 bits (see next comment). 128 * Remember that the table starts 2 bytes earlier than you would expect 129 * as the bootable flag is after it in the block. 130 */ 131 movw $(partbl+0x4),%bx # Partition table (+4) 132 xorw %dx,%dx # Item number 133/* 134 * Loop around on the partition table, printing values until we 135 * pass a 256 byte boundary. The end of loop test is at main.5. 136 */ 137main.3: movb %ch,-0x4(%bx) # Zero active flag (ch == 0) 138 btw %dx,_FLAGS(%bp) # Entry enabled? 139 jnc main.5 # No 140/* 141 * If any of the entries in the table are the same as the 'type' in the slice 142 * table entry, then this is an empty or non bootable partition. Skip it. 143 */ 144 movb (%bx),%al # Load type 145 movw $tables,%di # Lookup tables 146 movb $TBL0SZ,%cl # Number of entries 147 repne # Exclude 148 scasb # partition? 149 je main.5 # Yes 150/* 151 * Now scan the table of known types 152 */ 153 movb $TBL1SZ+1,%cl # Number of entries 154 repne # Locate 155 scasb # type 156/* 157 * Get the matching element in the next array. 158 */ 159 addw $TBL1SZ-1, %di # Adjust 160 movb (%di),%cl # Partition 161 addw %cx,%di # description 162 callw putx # Display it 163main.5: incw %dx # Next item 164 addb $0x10,%bl # Next entry 165 jnc main.3 # Till done 166/* 167 * Passed a 256 byte boundary; the table is finished. 168 * Add one to the drive number and check it is valid. 169 */ 170 popw %ax # Drive number 171 subb $0x80-0x1,%al # Does next 172 cmpb NHRDRV,%al # drive exist? (from BIOS?) 173 jb main.6 # Yes 174/* 175 * If this is the only drive, don't display it as an option. 176 */ 177 decw %ax # Already drive 0? 178 jz main.7 # Yes 179/* 180 * If it was illegal or we cycled through them, go back to drive 0. 181 */ 182 xorb %al,%al # Drive 0 183/* 184 * Whatever drive we selected, make it an ascii digit and save it back to the 185 * "next drive" location in the loaded block in case we want to save it later 186 * for next time. This also is part of the printed drive string so add 0x80 187 * to indicate end of string. 188 */ 189main.6: addb $'0'|0x80,%al # Save next 190 movb %al,_NXTDRV(%bp) # drive number 191 movw $drive,%di # Display 192 callw putx # item 193/* 194 * Now that we've printed the drive (if we needed to), display a prompt. 195 */ 196main.7: movw $prompt,%si # Display 197 callw putstr # prompt 198 movb _OPT(%bp),%dl # Display 199 decw %si # default 200 callw putkey # key 201/* 202 * Start of input loop. Beep and take note of time 203 */ 204main.10: movb $ASCII_BEL,%al # Signal 205 callw putchr # beep! 206 xorb %ah,%ah # BIOS: Get 207 int $0x1a # system time 208 movw %dx,%di # Ticks when 209 addw _TICKS(%bp),%di # timeout 210/* 211 * Busy loop, looking for keystrokes but keeping one eye on the time. 212 */ 213main.8: 214#ifndef SIO 215 movb $0x1,%ah # BIOS: Check 216 int $0x16 # for keypress 217 jnz main.11 # Have one 218#else /* SIO */ 219 movb $0x03,%ah # BIOS: Read COM 220 call bioscom 221 testb $0x01,%ah # Check line status 222 jnz main.11 # (bit 1 indicates input) 223#endif /* SIO */ 224 xorb %ah,%ah # BIOS: Get 225 int $0x1a # system time 226 cmpw %di,%dx # Timeout? 227 jb main.8 # No 228/* 229 * If timed out or defaulting, come here. 230 */ 231main.9: movb _OPT(%bp),%al # Load default 232 jmp main.12 # Join common code 233/* 234 * Get the keystroke. 235 */ 236main.11: 237#ifndef SIO 238 xorb %ah,%ah # BIOS: Get 239 int $0x16 # keypress 240 movb %ah,%al # Scan code 241#else 242 movb $0x02,%ah # BIOS: Receive 243 call bioscom 244#endif 245/* 246 * If it's CR act as if timed out. 247 */ 248#ifndef SIO 249 cmpb $KEY_ENTER,%al # Enter pressed? 250#else 251 cmpb $ASCII_CR,%al # Enter pressed? 252#endif 253 je main.9 # Yes 254/* 255 * Otherwise check if legal. If not ask again. 256 */ 257#ifndef SIO 258 subb $KEY_F1,%al # Less F1 scan code 259 cmpb $0x4,%al # F1..F5? 260 jna main.12 # Yes 261 subb $(KEY_1 - KEY_F1),%al # Less #1 scan code 262#else 263 subb $'1',%al # Less '1' ascii character 264#endif 265 cmpb $0x4,%al # #1..#5? 266 ja main.10 # No 267/* 268 * We have a selection. If it's a bad selection go back to complain. 269 * The bits in MNUOPT were set when the options were printed. 270 * Anything not printed is not an option. 271 */ 272main.12: cbtw # Option 273 btw %ax,_MNUOPT(%bp) # enabled? 274 jnc main.10 # No 275/* 276 * Save the info in the original tables 277 * for rewriting to the disk. 278 */ 279 movb %al,_OPT(%bp) # Save option 280 movw $FAKE,%si # Partition for write 281 movb (%si),%dl # Drive number 282 movw %si,%bx # Partition for read 283 cmpb $0x4,%al # F5/#5 pressed? 284 pushf # Save 285 je main.13 # Yes 286 shlb $0x4,%al # Point to 287 addw $partbl,%ax # selected 288 xchgw %bx,%ax # partition 289 movb $0x80,(%bx) # Flag active 290/* 291 * If not asked to do a write-back (flags 0x40) don't do one. 292 */ 293main.13: pushw %bx # Save 294 testb $0x40,_FLAGS(%bp) # No updates? 295 jnz main.14 # Yes 296 movw $start,%bx # Data to write 297 movb $0x3,%ah # Write sector 298 callw intx13 # to disk 299main.14: popw %si # Restore 300 popf # Restore 301/* 302 * If going to next drive, replace drive with selected one. 303 * Remember to un-ascii it. Hey 0x80 is already set, cool! 304 */ 305 jne main.15 # If not F5/#5 306 movb _NXTDRV(%bp),%dl # Next drive 307 subb $'0',%dl # number 308/* 309 * Load selected bootsector to the LOAD location in RAM. 310 * If it fails to read or isn't marked bootable, treat it as a bad selection. 311 */ 312main.15: movw $LOAD,%bx # Address for read 313 movb $0x2,%ah # Read sector 314 callw intx13 # from disk 315 jc main.10 # If error 316 cmpw $MAGIC,0x1fe(%bx) # Bootable? 317 jne main.10 # No 318 movw $crlf,%si # Leave some 319 callw puts # space 320 jmp *%bx # Invoke bootstrap 321 322/* 323 * Display routines 324 */ 325putkey: 326#ifndef SIO 327 movb $'F',%al # Display 328 callw putchr # 'F' 329#endif 330 movb $'1',%al # Prepare 331 addb %dl,%al # digit 332 jmp putstr.1 # Display the rest 333 334/* 335 * Display the option and note that it is a valid option. 336 * That last point is a bit tricky.. 337 */ 338putx: btsw %dx,_MNUOPT(%bp) # Enable menu option 339 movw $item,%si # Display 340 callw putkey # key 341 movw %di,%si # Display the rest 342 343puts: callw putstr # Display string 344 345putn: movw $crlf,%si # To next line 346 347putstr: lodsb # Get byte 348 testb $0x80,%al # End of string? 349 jnz putstr.2 # Yes 350putstr.1: callw putchr # Display char 351 jmp putstr # Continue 352putstr.2: andb $~0x80,%al # Clear MSB 353 354#ifndef SIO 355putchr: 356 pushw %bx # Save 357 movw $0x7,%bx # Page:attribute 358 movb $0xe,%ah # BIOS: Display 359 int $0x10 # character 360 popw %bx # Restore 361 retw # To caller 362#else /* SIO */ 363putchr: 364 movb $0x01,%ah # BIOS: Send 365bioscom: 366 pushw %dx # Save 367 xorw %dx,%dx # Use COM1 368 int $0x14 # Character 369 popw %dx # Restore 370 retw # To caller 371#endif /* SIO */ 372 373/* One-sector disk I/O routine */ 374 375intx13: movb 0x1(%si),%dh # Load head 376 movw 0x2(%si),%cx # Load cylinder:sector 377 movb $0x1,%al # Sector count 378 pushw %si # Save 379 movw %sp,%di # Save 380 testb $0x80,_FLAGS(%bp) # Use packet interface? 381 jz intx13.1 # No 382 pushl $0x0 # Set the 383 pushl 0x8(%si) # LBA address 384 pushw %es # Set the transfer 385 pushw %bx # buffer address 386 push $0x1 # Block count 387 push $0x10 # Packet size 388 movw %sp,%si # Packet pointer 389 decw %ax # Verify off 390 orb $0x40,%ah # Use disk packet 391intx13.1: int $0x13 # BIOS: Disk I/O 392 movw %di,%sp # Restore 393 popw %si # Restore 394 retw # To caller 395 396/* Menu strings */ 397 398item: .ascii " "; .byte ' '|0x80 399prompt: .ascii "\nDefault:"; .byte ' '|0x80 400crlf: .ascii "\r"; .byte '\n'|0x80 401 402/* Partition type tables */ 403 404tables: 405/* 406 * These entries identify invalid or NON BOOT types and partitions. 407 */ 408 .byte 0x0, 0x5, 0xf 409/* 410 * These values indicate bootable types we know the names of. 411 */ 412 .byte 0x1, 0x4, 0x6, 0xb, 0xc, 0xe, 0x83 413 .byte 0x9f, 0xa5, 0xa6, 0xa9 414/* 415 * These are offsets that match the known names above and point to the strings 416 * that will be printed. os_misc will be used if the search of the above table 417 * runs over. 418 */ 419 .byte os_dos-. # DOS 420 .byte os_dos-. # DOS 421 .byte os_dos-. # DOS 422 .byte os_dos-. # Windows 423 .byte os_dos-. # Windows 424 .byte os_dos-. # Windows 425 .byte os_linux-. # Linux 426 .byte os_bsd-. # BSD/OS 427 .byte os_freebsd-. # FreeBSD 428 .byte os_bsd-. # OpenBSD 429 .byte os_bsd-. # NetBSD 430 .byte os_misc-. # Unknown 431/* 432 * And here are the strings themselves. 0x80 or'd into a byte indicates 433 * the end of the string. (not so great for Russians but...) 434 */ 435os_misc: .ascii "?"; .byte '?'|0x80 436os_dos: .ascii "DO"; .byte 'S'|0x80 437os_linux: .ascii "Linu"; .byte 'x'|0x80 438os_freebsd: .ascii "Free" 439os_bsd: .ascii "BS"; .byte 'D'|0x80 440 441 .org PRT_OFF-0xe,0x90 442 443 .word B0MAGIC # Magic number 444 445/* 446 * These values are sometimes changed before writing back to the drive 447 * Be especially careful that nxtdrv: must come after drive:, as it 448 * is part of the same string. 449 */ 450drive: .ascii "Drive " 451nxtdrv: .byte 0x0 # Next drive number 452opt: .byte 0x0 # Option 453setdrv: .byte 0x80 # Drive to force 454flags: .byte FLAGS # Flags 455ticks: .word TICKS # Delay 456 457/* 458 * Here is the 64 byte partition table that fdisk would fiddle with. 459 */ 460partbl: .fill 0x40,0x1,0x0 # Partition table 461 .word MAGIC # Magic number 462