mbr.s revision 60822
147471Srnordier# 247471Srnordier# Copyright (c) 1999 Robert Nordier 347471Srnordier# All rights reserved. 447471Srnordier# 547471Srnordier# Redistribution and use in source and binary forms are freely 647471Srnordier# permitted provided that the above copyright notice and this 747471Srnordier# paragraph and the following disclaimer are duplicated in all 847471Srnordier# such forms. 947471Srnordier# 1047471Srnordier# This software is provided "AS IS" and without any express or 1147471Srnordier# implied warranties, including, without limitation, the implied 1247471Srnordier# warranties of merchantability and fitness for a particular 1347471Srnordier# purpose. 1447471Srnordier# 1547471Srnordier 1650477Speter# $FreeBSD: head/sys/boot/i386/mbr/mbr.s 60822 2000-05-23 12:28:31Z jhb $ 1747471Srnordier 1847471Srnordier# Master boot record 1947471Srnordier 2047471Srnordier .set LOAD,0x7c00 # Load address 2147471Srnordier .set EXEC,0x600 # Execution address 2247471Srnordier .set PT_OFF,0x1be # Partition table 2347471Srnordier .set MAGIC,0xaa55 # Magic: bootable 2447471Srnordier 2547471Srnordier .set NDRIVE,0x8 # Drives to support 2647471Srnordier 2747471Srnordier .globl start # Entry point 2860822Sjhb .code16 2947471Srnordier 3047471Srnordierstart: cld # String ops inc 3160822Sjhb xorw %ax,%ax # Zero 3260822Sjhb movw %ax,%es # Address 3360822Sjhb movw %ax,%ds # data 3447471Srnordier cli # Disable interrupts 3560822Sjhb movw %ax,%ss # Set up 3660822Sjhb movw $LOAD,%sp # stack 3747471Srnordier sti # Enable interrupts 3860822Sjhb movw $main-EXEC+LOAD,%si # Source 3960822Sjhb movw $main,%di # Destination 4060822Sjhb movw $0x200-(main-start),%cx # Byte count 4147471Srnordier rep # Relocate 4247471Srnordier movsb # code 4360822Sjhb jmp main-LOAD+EXEC # To relocated code 4447471Srnordier 4560822Sjhbmain: xorw %si,%si # No active partition 4660822Sjhb movw $partbl,%bx # Partition table 4748033Srnordier movb $0x4,%cl # Number of entries 4860822Sjhbmain.1: cmpb %ch,(%bx) # Null entry? 4947471Srnordier je main.2 # Yes 5048033Srnordier jg err_pt # If 0x1..0x7f 5160822Sjhb testw %si,%si # Active already found? 5247471Srnordier jnz err_pt # Yes 5360822Sjhb movw %bx,%si # Point to active 5447471Srnordiermain.2: addb $0x10,%bl # Till 5547471Srnordier loop main.1 # done 5660822Sjhb testw %si,%si # Active found? 5747471Srnordier jnz main.3 # Yes 5847471Srnordier int $0x18 # BIOS: Diskless boot 5947471Srnordier 6047471Srnordiermain.3: cmpb $0x80,%dl # Drive valid? 6147471Srnordier jb main.4 # No 6247471Srnordier cmpb $0x80+NDRIVE,%dl # Within range? 6347471Srnordier jb main.5 # Yes 6460822Sjhbmain.4: movb (%si),%dl # Load drive 6560822Sjhbmain.5: movb 0x1(%si),%dh # Load head 6660822Sjhb movw 0x2(%si),%cx # Load cylinder:sector 6760822Sjhb movw $LOAD,%bx # Transfer buffer 6860822Sjhb movw $0x201,%ax # BIOS: Read from 6947471Srnordier int $0x13 # disk 7047471Srnordier jc err_rd # If error 7160822Sjhb cmpw $MAGIC,0x1fe(%bx) # Bootable? 7247471Srnordier jne err_os # No 7360822Sjhb jmp *%bx # Invoke bootstrap 7447471Srnordier 7560822Sjhberr_pt: movw $msg_pt,%si # "Invalid partition 7647471Srnordier jmp putstr # table" 7747471Srnordier 7860822Sjhberr_rd: movw $msg_rd,%si # "Error loading 7947471Srnordier jmp putstr # operating system" 8047471Srnordier 8160822Sjhberr_os: movw $msg_os,%si # "Missing operating 8247471Srnordier jmp putstr # system" 8347471Srnordier 8460822Sjhbputstr.0: movw $0x7,%bx # Page:attribute 8547471Srnordier movb $0xe,%ah # BIOS: Display 8647471Srnordier int $0x10 # character 8747471Srnordierputstr: lodsb # Get character 8847471Srnordier testb %al,%al # End of string? 8947471Srnordier jnz putstr.0 # No 9047471Srnordierputstr.1: jmp putstr.1 # Await reset 9147471Srnordier 9247471Srnordiermsg_pt: .asciz "Invalid partition table" 9347471Srnordiermsg_rd: .asciz "Error loading operating system" 9447471Srnordiermsg_os: .asciz "Missing operating system" 9547471Srnordier 9647471Srnordier .org PT_OFF 9747471Srnordier 9847471Srnordierpartbl: .fill 0x10,0x4,0x0 # Partition table 9947471Srnordier .word MAGIC # Magic number 100