mbr.s revision 47471
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 1647471Srnordier# $Id: $ 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 2847471Srnordier 2947471Srnordierstart: cld # String ops inc 3047471Srnordier xorl %eax,%eax # Zero 3147471Srnordier movl %eax,%es # Address 3247471Srnordier movl %eax,%ds # data 3347471Srnordier cli # Disable interrupts 3447471Srnordier movl %eax,%ss # Set up 3547471Srnordier movwir(LOAD,_sp) # stack 3647471Srnordier sti # Enable interrupts 3747471Srnordier movwir(main-EXEC+LOAD,_si) # Source 3847471Srnordier movwir(main,_di) # Destination 3947471Srnordier movwir(0x200-(main-start),_cx) # Word count 4047471Srnordier rep # Relocate 4147471Srnordier movsb # code 4247471Srnordier jmpnwi(main-LOAD+EXEC) # To relocated code 4347471Srnordier 4447471Srnordiermain: xorl %esi,%esi # No active found 4547471Srnordier movwir(partbl,_bx) # Partition table 4647471Srnordier movb $0x4,%cl # Entries 4747471Srnordiermain.1: cmpbr0(_ch,_bx_) # Null entry? 4847471Srnordier je main.2 # Yes 4947471Srnordier jg err_pt # If bit 7 unset 5047471Srnordier testl %esi,%esi # Active found? 5147471Srnordier jnz err_pt # Yes 5247471Srnordier movl %ebx,%esi # Keep place 5347471Srnordiermain.2: addb $0x10,%bl # Till 5447471Srnordier loop main.1 # done 5547471Srnordier testl %esi,%esi # Active found? 5647471Srnordier jnz main.3 # Yes 5747471Srnordier int $0x18 # BIOS: Diskless boot 5847471Srnordier 5947471Srnordiermain.3: cmpb $0x80,%dl # Drive valid? 6047471Srnordier jb main.4 # No 6147471Srnordier cmpb $0x80+NDRIVE,%dl # Within range? 6247471Srnordier jb main.5 # Yes 6347471Srnordiermain.4: movb0r(_si_,_dl) # Load drive 6447471Srnordiermain.5: movb1r(0x1,_si_,_dh) # Load head 6547471Srnordier movw1r(0x2,_si_,_cx) # Load cylinder:sector 6647471Srnordier movwir(LOAD,_bx) # Transfer buffer 6747471Srnordier movwir(0x201,_ax) # BIOS: Read from 6847471Srnordier int $0x13 # disk 6947471Srnordier jc err_rd # If error 7047471Srnordier cmpwi2(MAGIC,0x1fe,_bx_) # Bootable? 7147471Srnordier jne err_os # No 7247471Srnordier jmp *%ebx # Invoke bootstrap 7347471Srnordier 7447471Srnordiererr_pt: movwir(msg_pt,_si) # "Invalid partition 7547471Srnordier jmp putstr # table" 7647471Srnordier 7747471Srnordiererr_rd: movwir(msg_rd,_si) # "Error loading 7847471Srnordier jmp putstr # operating system" 7947471Srnordier 8047471Srnordiererr_os: movwir(msg_os,_si) # "Missing operating 8147471Srnordier jmp putstr # system" 8247471Srnordier 8347471Srnordierputstr.0: movwir(0x7,_bx) # Page:attribute 8447471Srnordier movb $0xe,%ah # BIOS: Display 8547471Srnordier int $0x10 # character 8647471Srnordierputstr: lodsb # Get character 8747471Srnordier testb %al,%al # End of string? 8847471Srnordier jnz putstr.0 # No 8947471Srnordierputstr.1: jmp putstr.1 # Await reset 9047471Srnordier 9147471Srnordiermsg_pt: .asciz "Invalid partition table" 9247471Srnordiermsg_rd: .asciz "Error loading operating system" 9347471Srnordiermsg_os: .asciz "Missing operating system" 9447471Srnordier 9547471Srnordier .org PT_OFF 9647471Srnordier 9747471Srnordierpartbl: .fill 0x10,0x4,0x0 # Partition table 9847471Srnordier .word MAGIC # Magic number 99