Skip to content

Commit a2248fe

Browse files
add more fatdrvce errors to diagnose drive issues easier
1 parent 0f58de9 commit a2248fe

File tree

2 files changed

+58
-43
lines changed

2 files changed

+58
-43
lines changed

src/fatdrvce/fatdrvce.asm

Lines changed: 53 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ include 'macros.inc'
3838
virtual 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
5557
end virtual
5658
virtual 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,yfat.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,.goodfat
186+
ld hl,FAT_ERROR_INVALID_FILESYSTEM
187+
pop ix
188+
ret
189+
.goodfat:
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 (yfat.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
;-------------------------------------------------------------------------------
262277
fat_Close:
@@ -540,10 +555,14 @@ fat_GetVolumeLabel:
540555
ld (yfat.working_block),a,hl
541556
ld b,(yfat.blocks_per_cluster)
542557
.findblock:
543-
push bc
544558
ld (yfat.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,yfat.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.fat),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
;-------------------------------------------------------------------------------
652667
fat_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

src/fatdrvce/fatdrvce.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ extern "C" {
3636
typedef enum {
3737
FAT_SUCCESS = 0, /**< Operation was successful */
3838
FAT_ERROR_INVALID_PARAM, /**< An invalid argument was provided */
39-
FAT_ERROR_NOT_SUPPORTED, /**< The operation is not supported */
4039
FAT_ERROR_INVALID_CLUSTER, /**< An invalid FAT cluster was accessed */
4140
FAT_ERROR_INVALID_POSITION, /**< An invalid position in the file */
4241
FAT_ERROR_NOT_FOUND, /**< The partition, file, or entry does not exist */
@@ -47,8 +46,11 @@ typedef enum {
4746
FAT_ERROR_DIRECTORY_NOT_EMPTY, /**< The directory is not empty */
4847
FAT_ERROR_NO_VOLUME_LABEL, /**< No volume label found for partition */
4948
FAT_ERROR_RDONLY, /**< The file or entry is read-only */
50-
FAT_ERROR_RW_FAILED, /**< The callback read failed to read/write */
49+
FAT_ERROR_RW_FAILED, /**< The read or write callback failed (count != return) */
5150
FAT_ERROR_INVALID_FILESYSTEM, /**< A non-FAT filesystem detected */
51+
FAT_ERROR_INVALID_SIZE, /**< An invalid size was detected */
52+
FAT_ERROR_INVALID_MAGIC, /**< Some invalid magic bytes were detected */
53+
FAT_ERROR_INVALID_SIGNATURE, /**< Some invalid signature was detected */
5254
FAT_ERROR_NO_MORE_ENTRIES, /**< No more entries in the directory */
5355
} fat_error_t;
5456

@@ -71,7 +73,7 @@ typedef uint24_t (*fat_read_callback_t)(fat_callback_usr_t *usr,
7173
* @param[in] lba Local block address (LBA) to write.
7274
* @param[in] count Number of logical blocks to write.
7375
* @param[in] buffer Buffer to fetch write data from.
74-
* @returns Number of logical blocks writen.
76+
* @returns Number of logical blocks written.
7577
*/
7678
typedef uint24_t (*fat_write_callback_t)(fat_callback_usr_t *usr,
7779
uint32_t lba,

0 commit comments

Comments
 (0)