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