@@ -38,7 +38,6 @@ include 'macros.inc'
3838virtual at 0
3939 FAT_SUCCESS rb 1
4040 FAT_ERROR_INVALID_PARAM rb 1
41- FAT_ERROR_NOT_SUPPORTED rb 1
4241 FAT_ERROR_INVALID_CLUSTER rb 1
4342 FAT_ERROR_INVALID_POSITION rb 1
4443 FAT_ERROR_NOT_FOUND rb 1
@@ -51,6 +50,9 @@ virtual at 0
5150 FAT_ERROR_RDONLY rb 1
5251 FAT_ERROR_RW_FAILED rb 1
5352 FAT_ERROR_INVALID_FILESYSTEM rb 1
53+ FAT_ERROR_INVALID_SIZE rb 1
54+ FAT_ERROR_INVALID_MAGIC rb 1
55+ FAT_ERROR_INVALID_SIGNATURE rb 1
5456 FAT_ERROR_NO_MORE_ENTRIES rb 1
5557end virtual
5658virtual at 0
@@ -172,21 +174,26 @@ fat_Open:
172174 xor a , a
173175 sbc hl , hl
174176 call util_read_fat_block ; read fat zero block
177+ jr z , .read_zero_block
175178 ld hl , FAT_ERROR_RW_FAILED
176- jq nz , .error
179+ pop ix
180+ ret
181+ .read_zero_block:
177182 lea ix , yf at .buffer
178183 ld a , (ix + 12 )
179184 cp a , (ix + 16 ) ; ensure 512 byte blocks and 2 FATs
180- jq nz , .error
185+ jr z , .goodf at
186+ ld hl , FAT_ERROR_INVALID_FILESYSTEM
187+ pop ix
188+ ret
189+ .goodf at :
181190 ld a , (ix + 39 )
182191 or a , a ; can't support reallllly big drives (BPB_FAT32_FATSz32 high)
183- jq nz , .error
184- ld a , (ix + 66 )
185- cp a , $ 28 ; check fat32 signature
186- jr z , .goodsig
187- cp a , $ 29
188- jq nz , .error
189- .goodsig:
192+ jr z , .goodsize
193+ ld hl , FAT_ERROR_INVALID_SIZE
194+ pop ix
195+ ret
196+ .goodsize:
190197 xor a , a
191198 ld b , a
192199 ld hl , (ix + 14 )
@@ -226,14 +233,26 @@ fat_Open:
226233 ld (yf at .fs_info) , hl
227234 xor a , a
228235 call util_read_fat_block
229- jr nz , .error
236+ jr z , .check_magic
237+ ld hl , FAT_ERROR_RW_FAILED
238+ pop ix
239+ ret
240+ .check_magic:
230241 call util_checkmagic
231- jr nz , .error ; uh oh!
242+ jr z , .goodmagic
243+ ld hl , FAT_ERROR_INVALID_MAGIC
244+ pop ix
245+ ret
246+ .goodmagic:
232247 ld hl , (ix + 0 ) ; ix should still point to the temp block...
233248 ld bc , $ 615252 ; don't bother comparing $41 byte...
234249 xor a , a
235250 sbc hl , bc
236- jr nz , .error
251+ jr z , .goodsig
252+ ld hl , FAT_ERROR_INVALID_SIGNATURE
253+ pop ix
254+ ret
255+ .goodsig:
237256 scf
238257 sbc hl , hl
239258 ex de , hl
@@ -253,10 +272,6 @@ fat_Open:
253272 xor a , a
254273 sbc hl , hl ; return success
255274 ret
256- .error:
257- ld hl , FAT_ERROR_INVALID_FILESYSTEM
258- pop ix
259- ret
260275
261276;-------------------------------------------------------------------------------
262277fat_Close:
@@ -540,10 +555,14 @@ fat_GetVolumeLabel:
540555 ld (yf at .working_block) , a , hl
541556 ld b , (yf at .blocks_per_cluster)
542557.findblock:
543- push bc
544558 ld (yf at .working_block) , a , hl
559+ push bc
545560 call util_read_fat_block
546- jq nz , .rw_error
561+ jr z , .rd_success
562+ pop bc
563+ ld hl , FAT_ERROR_RW_FAILED
564+ ret
565+ .rd_success:
547566 lea hl , yf at .buffer + 11
548567 ld b , 16
549568 ld de , 32
@@ -584,11 +603,7 @@ fat_GetVolumeLabel:
584603 jq .removetrailingspaces
585604.done:
586605 xor a , a
587- sbc hl , hl
588- ret
589- .rw_error:
590- pop bc
591- ld hl , FAT_ERROR_RW_FAILED
606+ sbc hl , hl ; return success
592607 ret
593608
594609;-------------------------------------------------------------------------------
@@ -608,7 +623,10 @@ fat_OpenFile:
608623 ld iy , (iy + 3 )
609624 call util_locate_entry
610625 pop iy
611- jq z , .error
626+ jr nz , .found
627+ ld hl , FAT_ERROR_NOT_FOUND
628+ ret
629+ .found:
612630 ld bc , (iy + 3 )
613631 ld iy , (iy + 12 )
614632 ld (yfatFile.f at ) , bc
@@ -644,9 +662,6 @@ fat_OpenFile:
644662 ld (yfatFile.cluster_block) , a
645663 ld (yfatFile.block_pos) , hl ; return success
646664 ret
647- .error:
648- ld hl , FAT_ERROR_NOT_FOUND
649- ret
650665
651666;-------------------------------------------------------------------------------
652667fat_SetFileSize:
@@ -718,7 +733,10 @@ fat_SetFileSize:
718733 call util_next_cluster
719734 compare_auhl_zero
720735 pop bc
721- jq z , .failedchain ; the filesystem is screwed up
736+ jr nz , .goodchain ; the filesystem is screwed up
737+ ld hl , FAT_ERROR_CLUSTER_CHAIN
738+ ret
739+ .goodchain:
722740 dec bc
723741.entertraverseclusters:
724742 compare_bc_zero
@@ -748,11 +766,14 @@ fat_SetFileSize:
748766 compare_auhl_zero
749767 pop iy
750768 pop hl
751- jq z , .failedalloc
769+ jr nz , .goodalloc
770+ ld hl , FAT_ERROR_FAILED_ALLOC
771+ ret
772+ .goodalloc:
752773 dec hl
753774 compare_hl_zero
754775 jq nz , .allocateclusters
755- jq .success
776+ ; jq .success
756777.success:
757778 ld iy , (iy + 3 )
758779 ld a , hl , (yfatFile.entry_block) ; read the entry again for open
@@ -790,15 +811,7 @@ fat_SetFileSize:
790811 call util_ceil_byte_size_to_blocks_per_cluster
791812 pop iy
792813 ret
793- .failedchain:
794- ld hl , FAT_ERROR_CLUSTER_CHAIN
795- ret
796- .invalidpath:
797- ld hl , FAT_ERROR_INVALID_PATH
798- ret
799- .failedalloc:
800- ld hl , FAT_ERROR_FAILED_ALLOC
801- ret
814+
802815.currentcluster:
803816 dd 0
804817
0 commit comments