; 1. Setup basic processing conditions ; 2. Check for Hammer Class processor ; 3. Find system boot strap on boot drive and load it ; 4. Start the system boot strap ; CHS positions and disk numbers are usually stored in cx, dx, and bx: ; ch = cylinder ; cl = sector and high cylinder ; dh = head ; dl = disk ; bl = boot partition bits 16 org 7C00h jmp Start AQUOS_SIG db 13, 10, 'AquOS', 0 Msg_Loading db 13, 10, 'Loading', 0 ;Error messages BadCPU db 13, 10, 'You need a Hammer processor.',0 HIIK db 13, 10, 'Heck if I know what went wrong.',0 PNF db 13, 10, 'Active AquOS Partition not found.',0 SysHalt db 13, 10, 'System Halted.',0 ;Common Functions (for boot only) ;--------------------------------------------------------------------------------- ; Code to display a simple teletype message. ; give ds:si as string ending in zero. DispMsg: mov ah, 0Eh ;BIOS Subroutine for displaying teletype xor bx, bx ;Zero out BX jmp short Loadorret ;Start displaying stuff DispLet: int 10h ;Call BIOS to do teletype Loadorret: lodsb ;Loadup next letter or al, al ;Set the zero flag jnz DispLet ;Check that zero flag, disp next letter if not zero ret ;Return if zero ; Code to find next CHS location ; ch = cyl, cl = hcyl & sec, dh = head CHSInc: mov al, cl and cl, 00111111b and al, 11000000b inc cl or cl, al ret ; Code to read sector from a disk ; CH, CL, & DH = CHS | DL = Drive | ES:DI = Data Buffer Base ; SI = # of Sectors | ReadSectors: mov bp, 3 jmp short ReadLoop RetryRead: dec bp ; decrement the retry number jz Error ; No retries left? -> Error mov ah, 0 ; Reset drive int 13h jc Error ; Couln't reset drive? -> Error ReadLoop: mov bx, di ; restore bx to pointer mov ax, 0201h ; Read sectors int 13h jc RetryRead ; If failed start retrying mov ax, 0e2eh ; We want a dot xor bx, bx ; This corrupts bx int 10h ; Print that dot call CHSInc ; Find the next sector mov bp, 3 ; Reset the retry number add di, 512 ; Point to next sector in memory dec si ; Decrement the number of sectors to read jnz ReadLoop ; Keep reading until we have all the sectors ret ;--------------------------------------------------------------------------------- ; Fatal error handlers Error: mov si, [HIIK] call DispMsg jmp short HaltSys NoPart: mov si, [PNF] call DispMsg jmp short HaltSys BadCPUErr: mov si, [BadCPU] call DispMsg HaltSys: mov si, [SysHalt] call DispMsg hlt ;This stops the system. ;--------------------------------------------------------------------------------- ; And now for the good stuff Start: cli ;We don't want interupts right now xor eax, eax mov ds, ax mov ss, ax ;install exception handler, execute cpuid, and gather info. mov word [18h], BadCPUErr mov [1ah], cs pushad ;save all the general purpose registers for later mov eax, 80000000h cpuid ;This serves as a check for a pentium class or later processor. ;This also checks if the processor supports extended functions cmp eax, 80000000h jbe BadCPUErr mov eax, 80000001h cpuid bt edx, 29 ;Check for feature 29, Long Mode (64-bit) Support jnc BadCPUErr ;We only want a Hammer processor! mov si, [AQUOS_SIG] call DispMsg popad ;get those original registers back, for the next step ;Alright, so we've got a good processor. Now we need to find the system's boot ;strap loader, which will load the rest system itself. We'd do that here except ;the boot sector must be 512 bytes or less, and that doesn't leave us much space. int 13h ;Reset the boot drive jc Error ;If error -> Error mov si, Msg_Loading ;Load the loading message call DispMsg ;Display the loading message mov ax, 60h ;Can't put 60h into es, so gotta do it this way mov es, ax ;Data Segment 60h for sector read mov cx, 1 ;Cylinder 0, Sector 1 mov dh, 0 ;The Boot Drive or dl, dl ;set the signed bit jns LoadSystem ;The high 2 bits will be set on a HD, so ;if they are not set it's a floppy and ;we can begin loading immediately. ;We need to lookup the partition table in the master boot record. xor di, di ; Zero out di mov si, 1 ; We want to read 1 sector call ReadSectors ; read the sector into 60:00h mov si, 446 ; Point to the partitiontable (512 = Sector size, ; 64 bytes for 4 partition tables, and 2 bytes for ; a partition table signature. mov cx, 4 ; The loop counter, for below FindBootPartition: test byte [es:si], 80h ; Check if this partition is active (8th bit) jz FindNext cmp byte [es:si + 4], 21h ; Check if it has the correct ID je FoundSystemPartition FindNext: add si,16 ; Point to the next partition entry loop FindBootPartition ; and try again call NoPart ;cx is 4 and so it will try all four partitions ;if it is still not found then it gives an error FoundSystemPartition: mov cx, [es:si + 2] ;Get CHS from the partition table mov dh, [es:si + 1] LoadSystem: call CHSInc xor di, di ; Start Loading at 0600h (es is still 60h) mov si, 1 call ReadSectors ; Load the System Disk Header to Memory 600h. movzx si, byte [es:34h] ;Get the number of sectors in the Boot Strap call ReadSectors ; Load the Boot Strap jmp 0060h:0200h ; The BootStrap starts at 800h b/c the FS header is at 600h. times 446-($-$$) db 90h ;Fill the rest of this file with junk. times 64 db 90h ;This reserves space for the partition table ;that will eventualy be written here by the OS. dw 0AA55h ;This is a boot signature