]>
wirehaze git hosting - BOS.git/blob - boot/BOS_boot.asm
432465c5248129042e6d1574e0b73969575c8a99
1 ;------------------------------------------------------------;
2 ; BOS - FAT12 bootsector ;
3 ;------------------------------------------------------------;
4 ; - FAT12 compatible. ;
5 ; - Loads a binary file from the floppy. ;
7 ; 16-bit mem map (seg:off) ;
8 ;------------------------------------------------------------;
9 ; 0x0000:0x0000 -> 0x0000:0x0500 BIOS stuff ;
10 ; 0x0000:0x0500 -> 0x0000:0x2100 FAT12 rootdir ;
11 ; 0x0000:0x2100 -> 0x0000:0x3300 FAT for FAT12 ;
12 ; 0x0000:0x3300 -> 0x0000:0x6c00 14,25kb free space ;
13 ; 0x0000:0x6c00 -> 0x0000:0x7400 IDT, 256 descriptors ;
14 ; 0x0000:0x7400 -> 0x0000:0x7c00 GDT, 256 descriptors ;
15 ; 0x0000:0x7c00 -> 0x0000:0x7e00 bootsector ;
16 ; 0x0000:0x7e00 <- 0x0000:0x8000 512b stack for boot ;
17 ; 0x0000:0x8000 -> 0x9000:0xffff 608kb kernel/free space ;
18 ; 0xA000:0x0000 -> ............. VGA mem etc. ;
19 ;------------------------------------------------------------;
28 ;------------------------------------------;
29 ; Standard BIOS Parameter Block, "BPB". ;
30 ;------------------------------------------;
44 ;---------------------------------;
45 ; extended BPB for FAT12/FAT16 ;
46 ;---------------------------------;
49 bpbSignature db 41 ; 0 = end. 41 = three more.
51 bpbVolumeLabel db 'BOOT FLOPPY'
52 bpbFileSystem db 'FAT12 '
55 ;----------------------------------------;
56 ; starting point of bootsector code ;
57 ;----------------------------------------;
61 xor ax, ax ; initialize all the necessary
62 mov ds, ax ; registers.
65 mov sp, 0x8000 ; Stack..
72 ;----------------------------------;
73 ; clear screen and print some ;
74 ;----------------------------------;
75 mov ax, 3 ; Set mode 0x03
78 mov bp, loading
; Print loading message.
85 mov bl, 2 ; Set cursor.
90 mov ah, 9 ; Print 14 green dots.
96 ;---------------------------;
98 ;---------------------------;
99 mov di, 0x0050 ; Load the root to
100 mov ax, 19 ; 0x0000:0x0500 (0x500/0x10)
104 mov di, 0x0210 ; Load the fat to
105 mov ax, 1 ; 0x0000:0x2100
110 ;------------------------;
111 ; search for the file ;
112 ;------------------------;
113 mov dx, [bpbRootSize
]
128 ;-----------------------------------;
129 ; variables & functions ;
130 ;-----------------------------------;
131 loading db 'Starting BOS'
132 filename db 'KERNEL SYS'
133 failure db 'Read error!'
136 ;-----------------------------------------------;
137 ; read a number of sectors (one at a time) ;
138 ;-----------------------------------------------;
140 ; di = segment to save at ;
141 ; ax = sector to read ;
142 ; cx = number of sectors ;
144 ; di = updated (added for next read) ;
145 ; ax = updated (added for next read) ;
146 ;-----------------------------------------------;
149 mov bl, byte [bpbTrackSect
] ; bl = number of sectors per track
150 div bl ; al = ax / bl
151 mov cl, ah ; cl = real sector number
154 xor ah, ah ; del the rest of the div before
155 mov bl, byte [bpbHeads
] ; bl = number of heads
156 div bl ; ah = rest of ( ax / bx ), al = ax / bx
157 mov ch, al ; ch = number of track
158 mov dh, ah ; dh = the head number
160 mov ax, cx ; save cx in ax
161 mov cx, 6 ; try it 6 times
165 mov cx, ax ; restore cx
169 mov dl, [bpbDriveNo
] ; reset drive
178 mov ax, 0x0201 ; function 2, 1 sector
180 jnc .ok
; if it was ok, check next..
188 loop .next_try
; else try once again if there is an error
189 jmp error
; if cx = 0 and the read operation failed, halt
191 pop cx ; from the next_try loop
194 add di, 32 ; add 32 (512/16) to segment
195 inc ax ; add sector counter
200 ;----------------------------------------------------;
201 ; show a message and wait for a key before reboot ;
202 ;----------------------------------------------------;
216 ;-----------------------------------;
217 ; the file is found, load it. ;
218 ;-----------------------------------;
220 mov bp, [bx+26] ; bp = cluster from dir entry
221 mov di, 0x0800 ; 800 (segment)
225 mov cl, [bpbClustSize
] ; reset sector count to 1 cluster
226 mov si, bp ; si = next should-be cluster for
230 mul si ; multiply cluster number by 3
231 shr ax, 1 ; divide by two
233 mov ax, word [(0x2100+bx)] ; ax = FAT element with junk
234 jc .odd_cluster
; jump if the value was odd
235 and ax, 0x0FFF ; leave only lower 12 bits
236 jmp .got_cluster
; got it
238 shr ax, 4 ; (leave only bits 4-15)
241 inc si ; si = current cluster+1
242 cmp ax, si ; next cluster = current cluster+1?
243 jne .force_read
; is it still contiguous?
245 add cl, [bpbClustSize
] ; increase sector count by 1 cluster
246 adc ch, 0 ; add cf + 0 to ch
250 xchg bp, ax ; ax = bp (base cluster), bp = new cluster
251 dec ax ; decrease by 2 to get the actual...
252 dec ax ; ...cluster number
254 movzx dx, byte [bpbClustSize
]
255 mul dx ; multiply by sectors per cluster
256 add ax, 33 ; add data-area location (33)
259 cmp bp, 0x0FF8 ; is the new cluster EOF (FF8-FFF)?
260 jb .next_block
; if not, read next block
262 ;-----------------------;
263 ; the file is loaded ;
264 ;-----------------------;
266 jmp 0x0000:0x8000 ; jump to loaded file (64kb in mem)
269 ;-------------------------------------;
270 ; set the BOOT-signature at byte 510. ;
271 ;-------------------------------------;