]>
wirehaze git hosting - BOS.git/blob - boot/BOS_boot.asm
1 ;------------------------------------------------------------;
2 ; BOS - FAT12 bootsector ;
3 ;------------------------------------------------------------;
4 ; - FAT12 compatible. ;
5 ; - Loads a binary file from the floppy. ;
7 ;------------------------------------------------------------;
10 ; as i moved some 16-bit stuff to the kernel, i now got
11 ; space to do proper testing (for data-area etc),
15 ; find out why this crap takes ages to load the kernel.. :S
19 ; 16-bit mem map (seg:off)
20 ; 0x0000:0x0000 -> 0x0000:0x0500 BIOS stuff
21 ; 0x0000:0x0500 -> 0x0000:0x2100 FAT12 rootdir
22 ; 0x0000:0x2100 -> 0x0000:0x3300 FAT for FAT12
23 ; 0x0000:0x3300 -> 0x0000:0x6c00 14,25kb free space
24 ; 0x0000:0x6c00 -> 0x0000:0x7400 IDT, 256 descriptors
25 ; 0x0000:0x7400 -> 0x0000:0x7c00 GDT, 256 descriptors
26 ; 0x0000:0x7c00 -> 0x0000:0x7e00 bootsector
27 ; 0x0000:0x7e00 <- 0x0000:0xffff ~32,5kb stack for boot
28 ; 0x1000:0x0000 -> 0x9000:0xffff 576kb kernel/free space
29 ; 0xa000:0x0000 -> ............. VGA mem etc.
37 ;------------------------------------------;
38 ; Standard BIOS Parameter Block, "BPB". ;
39 ;------------------------------------------;
53 ;---------------------------------;
54 ; extended BPB for FAT12/FAT16 ;
55 ;---------------------------------;
58 bpbSignature db 41 ; 0 = nothing more. 41 = three more (below)..
60 bpbVolumeLabel db 'BOOT FLOPPY'
61 bpbFileSystem db 'FAT12 '
64 ;----------------------------------------;
65 ; starting point of bootsector code ;
66 ;----------------------------------------;
70 xor ax, ax ; initialize all the necessary
71 mov ds, ax ; registers.
74 mov sp, 0xFFFF ; Stack..
81 ;----------------------------------;
82 ; clear screen and print some ;
83 ;----------------------------------;
84 mov ax, 3 ; Set mode 0x03
87 mov bp, loading
; Print loading message.
94 mov bl, 2 ; Set cursor.
99 mov ah, 9 ; Print 14 green dots.
105 ;---------------------------;
106 ; load FAT and root ;
107 ;---------------------------;
108 mov di, 0x0050 ; Load the root to
109 mov ax, 19 ; 0x0000:0x0500 (0x500/0x10)
113 mov di, 0x0210 ; Load the fat to
114 mov ax, 1 ; 0x0000:0x2100
119 ;------------------------;
120 ; search for the file ;
121 ;------------------------;
122 mov dx, [bpbRootSize
]
137 ;-----------------------------------;
138 ; variables & functions ;
139 ;-----------------------------------;
140 loading db 'Starting BOS'
141 filename db 'KERNEL SYS'
142 failure db 'Read error!'
145 ;-----------------------------------------------;
146 ; read a number of sectors (one at a time) ;
147 ;-----------------------------------------------;
149 ; di = segment to save at ;
150 ; ax = sector to read ;
151 ; cx = number of sectors ;
153 ; di = updated (added for next read) ;
154 ; ax = updated (added for next read) ;
155 ;-----------------------------------------------;
158 mov bl, byte [bpbTrackSect
] ; bl = number of sectors per track
159 div bl ; al = ax / bl
160 mov cl, ah ; cl = real sector number
163 xor ah, ah ; del the rest of the div before
164 mov bl, byte [bpbHeads
] ; bl = number of heads
165 div bl ; ah = rest of ( ax / bx ), al = ax / bx
166 mov ch, al ; ch = number of track
167 mov dh, ah ; dh = the head number
169 mov ax, cx ; save cx in ax
170 mov cx, 6 ; try it 6 times
174 mov cx, ax ; restore cx
178 mov dl, [bpbDriveNo
] ; reset drive
187 mov ax, 0x0201 ; function 2, 1 sector
189 jnc .ok
; if it was ok, check next..
197 loop .next_try
; else try once again if there is an error
198 jmp error
; if cx = 0 and the read operation failed, halt
200 pop cx ; from the next_try loop
203 add di, 32 ; add 32 (512/16) to segment
204 inc ax ; add sector counter
209 ;----------------------------------------------------;
210 ; show a message and wait for a key before reboot ;
211 ;----------------------------------------------------;
225 ;-----------------------------------;
226 ; the file is found, load it. ;
227 ;-----------------------------------;
229 mov bp, [bx+26] ; bp=cluster number from directory entry
230 mov di, 0x1000 ; 1000 (segment)
234 mov cl, [bpbClustSize
] ; reset sector count to 1 cluster
235 mov si, bp ; si=next should-be cluster for
239 mul si ; multiply cluster number by 3
240 shr ax, 1 ; divide by two
242 mov ax, word [(0x2100+bx)] ; ax=FAT element with junk
243 jc .odd_cluster
; jump if the value was odd
244 and ax, 0x0FFF ; leave only lower 12 bits
245 jmp .got_cluster
; got it
247 shr ax, 4 ; (leave only bits 4-15)
250 inc si ; si=current cluster+1
251 cmp ax, si ; next cluster=current cluster+1?
252 jne .force_read
; is it still contiguous?
254 add cl, [bpbClustSize
] ; increase sector count by 1 cluster
255 adc ch, 0 ; add cf+0 to ch
259 xchg bp, ax ; ax=bp (base cluster), bp=new cluster
260 dec ax ; decrease by 2 to get the actual...
261 dec ax ; ...cluster number
263 movzx dx, byte [bpbClustSize
]
264 mul dx ; multiply by sectors per cluster
265 add ax, 33 ; add data-area location (33)
268 cmp bp, 0x0FF8 ; is the new cluster EOF (FF8-FFF)?
269 jb .next_block
; if not, read next block
271 ;-----------------------;
272 ; the file is loaded ;
273 ;-----------------------;
275 jmp 0x1000:0x0000 ; jump to loaded file (64kb in mem)
278 ;-------------------------------------;
279 ; set the BOOT-signature at byte 510. ;
280 ;-------------------------------------;