* add p cc
[mascara-docs.git] / amd64 / bareMetalOS-0.5.2 / pure64.boot0 / bootsector / fat16mbr.asm
blobecc99084d57f4845c46f53df15ec952674022d86
1 USE16
2 org 0x7C00
4 entry:
5 cli
6 mov [DriveNumber], dl ; BIOS passes drive number in DL
7 xor ax, ax
8 mov ss, ax
9 mov sp, 0x7C00
10 mov si, sp
11 push ax
12 pop es
13 push ax
14 pop ds
15 sti
17 ; Copy MBR sector to 0x0600 and jump there
18 cld
19 mov di, 0x0600
20 mov cx, 0x0100
21 repne movsw
22 jmp 0x0000:0x0621
23 ; 0x0621
25 ; Print message
26 mov si, msg_Load
27 call print_string_16
29 mov si, 0x07BE
30 cmp byte [si], 0x80
31 jne NoActivePartition
33 mov ecx, [0x07C6]
34 mov bx, 0x7C00
35 xor esi, esi
36 xor edi, edi
37 mov si, bx
38 mov di, bx
39 add eax, ecx
40 call readsector
41 jmp 0x0000:0x7C00
43 NoActivePartition:
44 mov si, msg_NoPartition
45 call print_string_16
46 jmp $
49 ;------------------------------------------------------------------------------
50 ; Read a sector from a disk, using LBA
51 ; input: EAX - 32-bit DOS sector number
52 ; ES:BX - destination buffer
53 ; output: ES:BX points one byte after the last byte read
54 ; EAX - next sector
55 readsector:
56 push dx
57 push si
58 push di
60 read_it:
61 push eax ; Save the sector number
62 mov di, sp ; remember parameter block end
64 push byte 0 ; other half of the 32 bits at [C]
65 push byte 0 ; [C] sector number high 32bit
66 push eax ; [8] sector number low 32bit
67 push es ; [6] buffer segment
68 push bx ; [4] buffer offset
69 push byte 1 ; [2] 1 sector (word)
70 push byte 16 ; [0] size of parameter block (word)
72 mov si, sp
73 mov dl, [DriveNumber]
74 mov ah, 42h ; EXTENDED READ
75 int 0x13 ; http://hdebruijn.soo.dto.tudelft.nl/newpage/interupt/out-0700.htm#0651
77 mov sp, di ; remove parameter block from stack
78 pop eax ; Restore the sector number
80 jnc read_ok ; jump if no error
82 push ax
83 xor ah, ah ; else, reset and retry
84 int 0x13
85 pop ax
86 jmp read_it
88 read_ok:
89 inc eax ; next sector
90 add bx, 512 ; Add bytes per sector
91 jnc no_incr_es ; if overflow...
93 incr_es:
94 mov dx, es
95 add dh, 0x10 ; ...add 1000h to ES
96 mov es, dx
98 no_incr_es:
99 pop di
100 pop si
101 pop dx
103 ;------------------------------------------------------------------------------
106 ;------------------------------------------------------------------------------
107 ; 16-bit Function to print a sting to the screen
108 ; input: SI - Address of start of string
109 print_string_16: ; Output string in SI to screen
110 pusha
111 mov ah, 0x0E ; int 0x10 teletype function
112 .repeat:
113 lodsb ; Get char from string
114 cmp al, 0
115 je .done ; If char is zero, end of string
116 int 0x10 ; Otherwise, print it
117 jmp short .repeat
118 .done:
119 popa
121 ;------------------------------------------------------------------------------
123 msg_Load db "Loading... ", 0
124 msg_NoPartition db "No active partition found"
125 DriveNumber db 0x00
127 times 446-$+$$ db 0
129 tables db "XXXXXXXXXXXXXXXX DO NOT OVERWRITE THIS AREA!!! XXXXXXXXXXXXXXXX" ; 64 bytes in length
131 sign dw 0xAA55