Fs.inc
[ Return to Browse Source Page ]
; Generic File System Functions for PizziOS
; Started 20 Aug 2000

read_sector_p2:
; ebx is sector number
; esi -> partition table
; ds:[edi] is memory address
 pushad
 mov cx,os_data
 push ds
 mov ds,cx
 mov edx,[esi+8]
 mov ecx,[esi+12]
 lea ebx,[ebx+edx]
 xor edx,edx
 mov dl,[esi+1]
 cmp ebx,ecx
  ja read_sector_err
 mov eax,edx
 jmp read_sector0

read_sector_p:
; eax is partition number
; ebx is sector number
; ds:[edi] is memory address
 pushad
 mov cx,os_data
 push ds
 mov ds,cx
 lea eax,[eax*8]
 lea eax,[eax*4 + offset partition_tbl]
 mov edx,[eax+8]
 mov ecx,[eax+12]
 lea ebx,[ebx+edx]
 xor edx,edx
 mov dl,[eax+1]
 cmp ebx,ecx
  ja read_sector_err
 mov eax,edx
 jmp read_sector0

tmp_dir:
 pushad
 mov eax,ds:[current_partition]
 lea ebx,[eax*8]
 lea ecx,[eax+1]
 lea edi,[ebx*4 + offset partition_tbl]
 test ecx,ecx
  jz fs_show_partitions
 mov esi,offset tmp_dir_str
 call print
 mov esi,offset current_directory_str
 call print
 call do_crlf
 call do_crlf
 mov esi,edi
 mov bl,[edi]
 cmp bl,1
  jz fat16_tmp_dir
; cmp bl,2
;  jz fat32_tmp_dir
 popad
ret

fs_show_partitions:
 xor ebx,ebx
 mov bl,ds:[num_partitions]
 mov esi,offset tmp_dir_root
 call print
 test ebx,ebx
  jz fs_show_partitions_none
 mov esi,offset tmp_dir_hd
 mov eax,00d3030h
 mov [esi+4],eax
 fs_show_partitions_loop:
  call print
  dec ebx
  jz fs_show_partitions_ret
  inc ah
  mov [esi+5],ah
  cmp ah,039h
  jna fs_show_partitions_loop
  mov ah,030h
  inc al
  mov [esi+4],eax
 jmp fs_show_partitions_loop
 fs_show_partitions_ret:
 call do_crlf
 popad
ret

fs_show_partitions_none:
 mov esi, offset tmp_dir_none
 call print
 jmp fs_show_partitions_ret

fs_cd_ll:
 ; takes:                returns:
 ; eax = "directory"     cf on error
 pushad
 mov ebx,ds:[current_partition]
 lea esi,[ebx*8]
 lea ecx,[ebx+1]
 lea esi,[esi*4 + offset partition_tbl]
 test ecx,ecx
  jz fs_select_partition
 mov cl,[esi]
 cmp cl,1
  jz fat16_cd
; cmp cl,2
;  jz fat32_cd
 stc
 popad
ret

 fs_select_partition_1d:
  mov ch,030h
  jmp fs_select_partition_1dret

fs_select_partition:
 mov ebx,[eax]
 and ebx,0ffffh
 cmp bx,06468h ; 'hd'
  jnz fs_select_partition_err
 mov cl,[eax+2]
 mov ch,[eax+3]
 test cl,cl
  jz fs_select_partition_err ; "hd"
 test ch,ch
  jz fs_select_partition_1d ; "hd#"
 mov bl,[eax+4]
 test bl,bl
  jnz fs_select_partition_err ; "hd???"
 fs_select_partition_2d:     ; "hd##"
 ror cx,8
 fs_select_partition_1dret:
 sub cl,30h
  jb fs_select_partition_err
 sub ch,30h
  jb fs_select_partition_err
 cmp cl,9
  ja fs_select_partition_err
 cmp ch,9
  ja fs_select_partition_err
 xor ebx,ebx
 mov bl,ch
 and ecx,0fh
 lea ebx,[ebx*4 + ebx] ; ebx *= 5
 lea ebx,[ebx*2 + ecx] ; ebx * 10 + ecx
 xor ecx,ecx
 mov cl,ds:[num_partitions]
 cmp ebx,ecx
  jnb fs_select_partition_err
; lea ecx,[ebx*8]
 mov ds:[current_partition],ebx
; mov ecx,[ecx*4 + offset partition_tbl + 28]
 xor ecx,ecx
 mov ds:[current_directory],ecx
 mov edi,offset current_directory_str
 mov eax,ebx
 call fs_partition_make_str
 clc
 popad
ret
fs_select_partition_err:
 stc
 popad
ret

fs_partition_make_str:
 ; uses ebx, eax
 mov ebx,64683030h
 cmp eax,10
  jb fs_partition_make_str_1d
 fs_partition_make_str_loop:
  lea eax,[eax-10]
  inc bh
  cmp eax,10
 jnb fs_partition_make_str_loop
 add bl,al
 ror ebx,16
 xor al,al
 mov [edi],ebx
 mov [edi+4],al
ret
fs_partition_make_str_1d:
 add bl,al
 xor bh,bh
 rol ebx,16
 mov [edi],ebx
ret 

fs_init:
 mov ecx,ds:[hd_units]
 xor eax,eax
 fs_init_loop:
  call scan_partitions
IFDEF debugfs
    pushad
    push eax
    mov al,' '
    call print_char
    call print_char
    pop eax
    call print_num
    mov al,':'
    call print_char
    xor eax,eax
    mov al,ds:[num_partitions]
    call print_num
    call do_crlf
    popad
ENDIF
  dec ecx
  lea eax,[eax+1]
 jnz fs_init_loop
 xor eax,eax
 mov al,ds:[num_partitions]
 test eax,eax
 lea eax,[eax-1]
  jz fs_init_no_partitions
 lea ebx,[eax*8]
 lea esi,[ebx*4 + offset partition_tbl]
 inc eax
 fs_init_setup_partitions:
  dec eax
  mov bl,[esi]
  cmp bl,1
   jz fat16_setup
;  cmp bl,2
;   jz fat32_setup
  fs_setup_invalid_partition:
   mov edx,offset num_partitions
   mov cl,[edx]
   lea edi,[esi+32]
   mov ebx,offset partition_tbl + 32*32
   dec cl
   sub ebx,edi
   mov [edx],cl
   shr ebx,2
   fs_setup_invalid_loop:
    mov ecx,[edi]
    dec ebx
    mov [edi-32],ecx
    lea edi,[edi+4]
    jnz fs_setup_invalid_loop
  fs_setup_partition_next:
  test eax,eax
  lea esi,[esi - 32]
 jnz fs_init_setup_partitions
 fs_init_no_partitions:
IFDEF debugfs
 mov al,ds:[num_partitions]
 call do_crlf
 call print_hex
 call do_crlf
ENDIF
 mov eax,-1
 mov ds:[current_partition],eax ; root = -1
ret

scan_partitions:
; eax = drive #
pushad
 mov edi,offset partition_tbl
 xor ebx,ebx
 mov bl,ds:[num_partitions]
 lea ebx,[ebx*8]
 lea edi,[edi+ebx*4]
 xor ebx,ebx
  scan_partitions_0:
IFDEF debugfs
   call pause
ENDIF
   mov esi,edi
   mov edi,offset hd_buffer
   call read_sector
    jc scan_partitions_q
IFDEF debugfs
    pushad
    mov eax,ebx
    call do_crlf
    call print_hex_32
    mov al,'!'
    call print_char
    call do_crlf
    mov esi,offset hd_buffer + 01BEh
    mov dl,8
    disp_partition_o:
     mov cl,8
     disp_partition_i:
      mov al,[esi]
      inc esi
      call print_hex
      mov al,' '
      call print_char
      dec cl
      jnz disp_partition_i
     call do_crlf
     dec dl
     jnz disp_partition_o
    popad
ENDIF
   mov cl,4
   mov edi,esi
   mov esi,offset hd_buffer + 01BEh
   xor ebp,ebp
   scan_partitions_loop:
;    mov dh,[esi]
    mov dl,[esi+4]
;    test dh,07fh             ; condition removed: According to Hale Landis,
;     jnz scan_partitions_loopend ; bits 6 thru 0 don't _have_ to be cleared

; PUT BACK:
;    test dl,dl                    ; for secondary drives.
;     jz scan_partitions_loopend ; if (empty partition entry) { skip_it(); }

    cmp dl,04h  ; Dos 16-bit FAT
     jz scan_partitions_fat16
    cmp dl,05h  ; Extended partition
     jz scan_partitions_ext
    cmp dl,06h  ; Dos 32+ Mb 16-bit FAT
     jz scan_partitions_fat16
    cmp dl,0bh  ; Windows 32-bit FAT
     jz scan_partitions_fat32
    cmp dl,0ch  ; Windows 32-bit FAT LBA
     jz scan_partitions_fat32
    cmp dl,0eh  ; Dos 16-bit FAT LBA
     jz scan_partitions_fat16
    cmp dl,0fh  ; Extended partition LBA
     jz scan_partitions_ext
    cmp dl,055h
     jz scan_partitions_ezdrive
    cmp dl,0f2h ; Dos 3.3+ secondary 16-bit FAT
     jz scan_partitions_fat16
IFDEF debugfs
    push eax
     mov al,'-'
     call print_char
    pop eax
ENDIF
    jmp scan_partitions_loopend

    scan_partitions_ext_nogood:
     xor ebp,ebp
     jmp scan_partitions_loopend

   scan_partitions_ezdrive:
    mov ebp,[esi+8]
    push ebx
    add ebx,ebp
     call scan_partitions_ezentrance
    pop ebx
    mov edi,offset hd_buffer
    call read_sector
     jc scan_partitions_q
    xor edx,edx
    mov dl,ds:[num_partitions]
    lea edi,[edx*8]
    lea edi,[edi*4 + offset partition_tbl]
    jmp scan_partitions_loopend

    scan_partitions_ezentrance:
     pushad
     jmp scan_partitions_0

   scan_partitions_ext:
    mov ebp,[esi+8]
IFDEF debugfs
      push eax
      mov al,'x'
      call print_char
      pop eax
ENDIF
    jmp scan_partitions_loopend

   scan_partitions_fat32:
IFDEF debugfs
    push eax
     mov al,'3'
     call print_char
    pop eax
ENDIF
    mov ch,2
    jmp scan_partitions_good
   scan_partitions_fat16:
IFDEF debugfs
    push eax
     mov al,'1'
     call print_char
    pop eax
ENDIF
    mov ch,1
    ;jmp scan_partitions_good

   scan_partitions_good:
    mov dl,ds:[num_partitions]
    test dl,020h
    mov edx,[esi + 8]
     jnz scan_partitions_q ; max # of partitions already
    mov [edi],ch
    mov [edi+1],al
    test edx,edx
    lea edx,[edx+ebx] ; LBA sector fields relative to current sector
     jz scan_partitions_loopend ; can't have partition at 0.
    mov [edi + 8],edx
    add edx,[esi + 12]
    lea edx,[edx-1]
    test edx,edx
    mov [edi + 12],edx
    mov dl,ds:[num_partitions]
     jz scan_partitions_loopend ; can't have length of 0.
    inc dl
    lea edi,[edi + 32]
    mov ds:[num_partitions],dl ; makes the partition officially added
    ;jmp scan_partitions_loopend
   scan_partitions_loopend:
    dec cl
    lea esi,[esi + 16]
    jnz scan_partitions_loop
   test ebp,ebp
    jnz scan_partitions_doext
   scan_partitions_q:
popad
ret

   scan_partitions_doext:
    add ebx,ebp
    jmp scan_partitions_0


Download this file.


[ Return to Browse Source Page ]
Copyright 2000, Ed Pizzi