Code: Select all

```
;**********************************************************************
; _fat12_LoadFile: load a specified file from the current disk
; Parameters: DS:SI - filename, ES:BX - buffer to file
; Returns: On error, CF set, AX = error code
;**********************************************************************
_fat12_LoadFile:
push es ; save es
push bx ; and bx
; first, try to load the root directory
call _LoadRootDir
jc .LoadRootError
; now, try to load the FAT tables
call _LoadFileTable
jc .LoadFATError
; try to locate the file
call _LocateFile
jc .LocateError
.LoadFileImage:
pop bx ; here's where we are
pop es ; storing our file
push ax ; save the current cluster number
call _clusterLBA ; translate to LBA
xor cx,cx ; zero out cx
mov cl,byte [bpbSectorsPerCluster] ; and store the number of sectors to read in cl
call _readSectors ; read the sectors
jc .ClusterReadError ; if the carry flag is set, we have a cluster read error
pop ax ; restore the cluster number
; save es and bx again
push es
push bx
; compute the next cluster
mov cx,ax ; store a copy of the cluster
mov dx,ax ; in cx and dx
shr dx,0x0001 ; divide dx by two
add cx,dx ; adjust for 3/2
push word FAT_SEGMENT
pop es
xor bx,bx ; the offset of the FAT
add bx,cx ; add the offset of the cluster
mov dx,word [es:bx] ; read the next cluster from the FAT
test ax,0x0001 ; see if the cluster is even or odd
jnz .OddCluster ; and handle if odd
.EvenCluster:
and dx,0000111111111111b ; get low twelve bits
jmp .ClusterDone ; and finish this cluster
.OddCluster:
shr dx,0x04 ; get high twelve bits
.ClusterDone:
mov ax,dx ; put the cluster into ax
cmp dx,0x0ff0 ; check for end-of-file cluster
jb .LoadFileImage ; and if it isn't EOF, keep reading
.Success:
pop bx
pop es
xor ax,ax
ret
.LoadRootError:
stc
mov ax,1
jmp .ErrorReturn
.LoadFATError:
stc
mov ax,2
jmp .ErrorReturn
.LocateError:
stc
mov ax,3
jmp .ErrorReturn
.ClusterReadError:
stc
mov ax,4
jmp .ErrorReturn
.ErrorReturn:
pop bx
pop es
ret
```

Code: Select all

```
; the minidriver started. Load the kernel image
xor ax,ax
mov es,ax
mov bx,IMAGE_RMODE_BASE
mov si,str_imgName
call _fat12_LoadFile
jc KERNEL_LOAD_EXCEPTION
```

Code: Select all

```
%define IMAGE_RMODE_BASE 0x3000
%define ROOT_SEGMENT 0x2e0
%define FAT_SEGMENT 0x2c0
%define ROOT_OFFSET 0x2e00
```