1 ; Various functions for the Interaction with the FAT16 Filesystem
3 ; Copyright (C) 2010 Frank Zoll
4 ; Copyright (C) 2010 Sprite_tm
5 ; Copyright (C) 2010 Leo C.
7 ; This file is part of avrcpm.
9 ; avrcpm is free software: you can redistribute it and/or modify it
10 ; under the terms of the GNU General Public License as published by
11 ; the Free Software Foundation, either version 3 of the License, or
12 ; (at your option) any later version.
14 ; avrcpm is distributed in the hope that it will be useful,
15 ; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ; GNU General Public License for more details.
19 ; You should have received a copy of the GNU General Public License
20 ; along with avrcpm. If not, see <http://www.gnu.org/licenses/>.
25 ; ============================================================================
28 ; Size of a Sector is fixed to 512 Bytes by Base - MMC Driver implementation
29 ; The Functions below therefore assume a fixed Size of 512 Bytes per Sector.
30 ; ============================================================================
35 ; ############################################################################
36 ; Defines for FAT16 Structures
37 ; ############################################################################
39 /*These are the Offsets to the Variables within the Bootsector of a FAT16
42 ;#define FAT16_BSO_SECSIZE 0x0b ; Offset to Sectorsize Word
43 #define FAT16_BSO_CLUSTSZ 0x0d ; Offset to Clustersize Byte
44 #define FAT16_BSO_RESSECT 0x0e ; Offset to Number of Reserved Sectors
45 #define FAT16_BSO_VOLPTR 0x1c ; Offset to First VolumeSector
46 #define FAT16_BSO_SECPERFAT 0x16 ; Offset to Number of Sectors per Fat
47 #define FAT16_BSO_NUMFATCP 0x10 ; Offset to Ammount of FAT Copys
48 #define FAT16_BSO_NUMDIRENT 0x11 ; Offset to Max. Root Dir. Entrys
49 #define FAT16_FIRST_IMAGENAME 'A' ; First letter of filename to search
50 #define FAT16_LAST_IMAGENAME 'A'+MAXDISKS-1 ; Last letter of filename to
53 ; ############################################################################
54 ; Start of Data Segment
55 ; ############################################################################
58 fat_partfound: .byte 1 ; (partition: 0= no found 1=found )
59 fat_parttbl: .byte 4 ; first fat16 partition entry
60 ; only startsector is needed
61 fat_clustersize: .byte 1 ; sectors per cluster
62 fat_numdirentrys:.byte 2 ; Max. num. of entrys within Rootdirektory
63 fat_ptr2fat: .byte 4 ; pointer to the first fat sector
64 fat_ptr2dat: .byte 4 ; pointer to the first data sector
66 /*These variables define a cache that holds the last Cluster and Sector
67 thats been searched vor. To save some of the valuabe SRAM- Space these
68 variables also are used as temporary variables by the function
71 fat_last_dsk: .byte 1 ; number of disk with entry in cache
72 fat_log_clust: .byte 2 ; last searched logical cluster
73 fat_clust_offset: .byte 1 ; offset within the cluster
74 fat_clust_ptr: .byte 4 ; sector of last real cluster
76 /* This Variable is only needed within the scanning of the directory
77 for tempoary variable storage.. todo: optimize away :-) */
78 fat_temp: .byte 3 ; for tempoary use
80 ; ############################################################################
81 ; Start of Code Segment
82 ; ############################################################################
85 ; ============================================================================
86 ; Function: Initialize internal FAT-Partition Variables
87 ; ============================================================================
89 ; ----------------------------------------------------------------------------
91 ; Variables : [out] fat_parttabl
92 ; ----------------------------------------------------------------------------
94 ; This Routine initializes the internal Variables, that point to the
95 ; first Found FAT16 Partition.
96 ; ============================================================================
97 fat_init_partitiontable:
108 ; ============================================================================
109 ; Function: Resets the Cache
110 ; ============================================================================
112 ; ----------------------------------------------------------------------------
114 ; Variables : [out] fat_log_clust
116 ; ----------------------------------------------------------------------------
118 ; This Routine resets the internal Cache- Variables. After reset, the
119 ; next read or write Command will initialize a scan of the FAT of
120 ; the FAT16-Partition for the given sector.
121 ; ============================================================================
125 sts fat_log_clust ,yl
126 sts fat_log_clust+1,yl
131 ; ============================================================================
132 ; Function: Saves FAT16 Partitiondata for later Scanning
133 ; ============================================================================
135 ; ----------------------------------------------------------------------------
136 ; Registers : [in] z Pointer to the Partitondata
137 ; Variables : [out] fat_partfound Boolean for "Partition found"
138 ; [out] fat_parttbl Pointer to Partitiontable
139 ; ----------------------------------------------------------------------------
141 ; This funktion sets the internal Variables to the Start and Size
142 ; of a given FAT16 Paritition. This Information will be used for a
143 ; later scanning of the Partition. See Function "fat_scan_partition"
144 ; for more information.
145 ; ============================================================================
149 printstring "fat16 part found"
154 ; save variables on stack
158 ; set fat16 partition found flag
162 ; save data from first fat16 partition
165 ldd temp,z+PART_START
167 ldd temp,z+PART_START+1
169 ldd temp,z+PART_START+2
171 ldd temp,z+PART_START+3
174 ; reload variables from stack
180 ; ============================================================================
181 ; Read and Scann a FAT16 Partition for Imagedatefiles
182 ; ============================================================================
185 ; ----------------------------------------------------------------------------
186 ; This Routine reads the Bootblock and scanns it for a Diskimage
187 ; ============================================================================
193 printstring "fat16 scanning"
197 ; Check if a FAT16 Partition was realy found
204 printstring "free entrys in ptable ?"
208 ; Check for free Entrys in Partition table
214 printstring "read fat bootblock."
218 ; Scan partition start
225 ; Load first sector from Partition
228 breq fat_bootblock_check
230 ; Read error: Block not found
238 printstring "fat16 bootblock check"
242 ; get sectors per cluster from bootblock
243 ldiw z,hostbuf+FAT16_BSO_CLUSTSZ
245 sts fat_clustersize,temp
248 printstring "Sectors per Cluster "
253 ; get num of FAT Tables from bootblock
254 ldiw z,hostbuf+FAT16_BSO_NUMFATCP
256 sts fat_last_dsk,temp ; low byte
259 printstring "Ammount of FAT copies: "
264 ; get max num of entrys in root direktory from bootblock
265 ldiw z,hostbuf+FAT16_BSO_NUMDIRENT
267 sts fat_numdirentrys,temp ; low byte
269 sts fat_numdirentrys+1,temp2 ; high byte
272 printstring "Max. entrys in Rootdir.: "
277 ; Print begin of Volume
286 printstring "Begin of Volume at: "
296 ; get num of sectors per FAT-Table from bootblock
297 ldiw z,hostbuf+FAT16_BSO_SECPERFAT
299 sts fat_log_clust,temp ; low byte
301 sts fat_log_clust+1,temp2 ; high byte
304 printstring "Sectors per FAT__: "
309 ; get num of reseved sectors from bootblock
310 ldiw z,hostbuf+FAT16_BSO_RESSECT
314 ; Calculate begin of FAT within the Volume
332 printstring "Begin of FAT at___: "
342 ; Calculate begin of Root- Directory within the Volume
349 lds temp ,fat_log_clust
350 lds temp2,fat_log_clust+1
351 lds temp3,fat_last_dsk
355 breq fat_calc_dp_lend
364 rjmp fat_calc_dp_loop
367 sts fat_clust_ptr ,xl
368 sts fat_clust_ptr+1,xh
369 sts fat_clust_ptr+2,yl
370 sts fat_clust_ptr+3,yh
374 printstring "Begin of DIR at___: "
384 ; Calculate begin of DATA Clusters within the Volume
385 ; Num. Dir.Sektors = (Num. of Dir. Entrys * 32) / Bytes per Sektor
387 ; Sectorsize is fixed at 512 Bytes, makes 16 Entrys per Sektor
389 lds zl,fat_numdirentrys ; low byte
390 lds zh,fat_numdirentrys+1 ; high byte
392 ; Num. Direntrys / 16
403 lds xh,fat_clust_ptr+1
404 lds yl,fat_clust_ptr+2
405 lds yh,fat_clust_ptr+3
418 printstring "Begin of Data at__: "
428 ; Here Starts the Scann of the Directory for valid image Files.
430 ; Init Image-Namecounter
431 ldi temp,FAT16_FIRST_IMAGENAME
432 sts fat_last_dsk,temp
434 fat_scan_for_next_image:
436 ; Init Offset into Directory-Sectors
438 sts fat_clust_offset,temp
440 ; Init counter for number of entry left to scan
441 lds temp,fat_numdirentrys
442 sts fat_log_clust ,temp
444 lds temp,fat_numdirentrys+1
445 sts fat_log_clust+1,temp
447 fat_next_sector_loop:
448 ; Get a Pointer to the first Directory sector
450 lds xh,fat_clust_ptr+1
451 lds yl,fat_clust_ptr+2
452 lds yh,fat_clust_ptr+3
455 lds temp,fat_clust_offset
461 ; Load sector from Directory
464 breq fat_look_for_images
466 ; Read error: Block not found
470 ; Looks at a read directory block for image entrys
505 lds temp3,fat_last_dsk ; Get actual Diskname (A to Z)
525 rjmp fat_store_new_entry
537 cpi temp2,16 ; max entrys/sector
538 breq fat_scan_next_sector
539 rjmp fat_look_for_loop
541 fat_scan_next_sector:
544 lds temp3, fat_log_clust
545 lds temp4, fat_log_clust+1
550 sts fat_log_clust,temp3
551 sts fat_log_clust+1,temp4
557 lds temp,fat_clust_offset
559 sts fat_clust_offset,temp
561 rjmp fat_next_sector_loop
565 lds temp,fat_last_dsk
567 sts fat_last_dsk,temp
569 ldi temp2,FAT16_LAST_IMAGENAME
571 brge fat_scaned_last_disk
573 rjmp fat_scan_for_next_image
575 fat_scaned_last_disk:
580 ; Create new Partition Entry
583 ; Found a valid image
585 printstring "Found a valid Image ! Z="
599 adiw y,PARTENTRY_SIZE
601 rjmp fat_look_store_loop
604 ; Set Type of Partition to FAT16- Fileimage
609 ; Offset to Startcluster + 2
620 ; Convert Filesize to ammount of sectors
621 ; (calc with 512byte/sector)
631 ; store ammount of sectors in partitiontable
633 tst zl ;file size larger than 65535 sectors?
643 ; Test finding of the first sector
650 printstring "Begin of Image at: "
660 ; Check for another free entry in partition table
668 rjmp fat_scan_for_more
675 ; ============================================================================
676 ; Function: Cluster to HostSector
677 ; ============================================================================
678 ; Parameters: [in] xh,xl Cluster Number
679 ; [out] yh,yl,xh,xl Sector Number on Disk
680 ; ----------------------------------------------------------------------------
682 ; Variables : [used] fat_clustersize Ammount of Sectors per Cluster
684 ; ----------------------------------------------------------------------------
686 ; ! Only works with Clustersizes 2,4,8,16,32,64,128 !
687 ; ============================================================================
690 ; Get Offset into Data area of Disk
694 ; add begin of data area to offset
695 lds temp,fat_ptr2dat+0
697 lds temp,fat_ptr2dat+1
699 lds temp,fat_ptr2dat+2
701 lds temp,fat_ptr2dat+3
705 ; ============================================================================
706 ; Function: Cluster to Sector
707 ; ============================================================================
708 ; Registers: [in] xl,xh Cluster Number
709 ; [out] xl,xh,yl,yh Sector Number
710 ; Variables: [in] fat_clustersize Ammount of Sectors per Cluster
712 ; ----------------------------------------------------------------------------
714 ; Calculates the logical Sectornumber given the physical ClusterNumber
715 ; and the size of a Cluster un sectors.
717 ; ! Only works with Clustersizes 2,4,8,16,32,64,128 !
718 ; ============================================================================
724 sub xl,temp ; Substract the 2 reserved clusters
727 lds temp,fat_clustersize
744 ; ====================================================================
745 ; Function: Searches a physical Cluster, given the logical Cluster
746 ; ====================================================================
747 ; Registers: [in] xh,xl logical- Cluster
748 ; [out] yh,yl physical- Cluster
750 ; --------------------------------------------------------------------
752 ; ====================================================================
757 rcall dsk_getpartentry ; get partition entry
760 ; Get First FAT- Cluster Number of Diskimage
765 .if FAT16_DBG_FAT > 0
766 printstring "Search log. Cluster "
772 printstring "Search phys. Cluster "
782 breq fat_found_phsy_clust
783 ; Get Next Cluster from Fat
785 ; Trick: 512 Bytes Per Sector equals to 256 FAT- Entrys per Sector
786 ; so given: yl is the Offset within the FAT Sector
787 ; yh is the number off se FAT sector to Read
789 ; in zh,zl: Pointer to Word within the Sector to read
790 ; in yh..xl: Start sector number (LBA)
791 ; out zh,zl : word thats been read
795 ; Create FAT Offset Value
806 ; Add Cluster offset within sector
819 ; Check next logical Cluster
823 rjmp fat_next_phsy_clust
825 ; Found the physical cluster
826 fat_found_phsy_clust:
828 .if FAT16_DBG_FAT > 0
829 printstring "Found phys. Cluster at:"
838 ; ============================================================================
839 ; Function: This Routine searches for the Sector within an Imagefile
840 ; ============================================================================
841 ; Registers: [out] xl,xh,yl,yh Pointer to the Sector on the SD-Card
842 ; [out] temp Error- Variable (0= No Error)
843 ; Variables: [in] hostdsk host disk #, (partition #)
844 ; [in] hostlba host block #, relative to part.start
845 ; [in] fat_last_dsk number of disk with entry in cache
846 ; [in] fat_log_clust last searched logical cluster
847 ; [in] fat_clust_offset offset within the cluster
848 ; [in] fat_clust_ptr sector of last real cluster
849 ; ----------------------------------------------------------------------------
851 ; This Routine uses the variables hostdsk and hostlba to find an Sector
853 ; The CP/M Sector given within "hostlba" are splited to a logical Cluster-
854 ; Number and the Subsector within this logical Cluster.
855 ; logical Cluster Number = hostlba / fat_clustersize
856 ; The logical Cluster Number will be compared to the logical Cluster- Number
857 ; within the Cache. When this Clusters are the same and the DiskID's are
858 ; also the same, then the cached physical Sector will be used.
859 ; When the Clusters or the Disks don't match, a seek for the physical
860 ; Cluster is performed. This seek is done thru an access over the FAT of
861 ; the FAT16 Partition. The Routine starts at the first Cluster of the
862 ; Imagefile and goes along the linked list of Clusternumber till it reaches
863 ; the searched cluster. The found Clusternumber will be used to calculate
864 ; the Sektor where this Cluster lies on the SD- Card. Both the found physical
865 ; Cluster and the logical Cluster together with the physical Sectornumber
866 ; are stored in the cache.
867 ; The last step done is to add the SubSectorOffset to the found physical
868 ; Sector. This gives the pointer to the Sector to be read and or written.
869 ; ============================================================================
875 rcall dsk_getpartentry ; get partition entry
883 ldd xl,z+5 ; get size of disk in sectors
887 cp temp,xl ; check given sector against disksize
896 ; ################# Get logical Number of Cluster within the imagefile
897 ; printstring "calc log sector"
898 ; Get logical Sectornumber from temp
904 ; Divide logical Sectornumber by size of Cluster in sectors
905 lds zl,fat_clustersize
918 rjmp fat_search_clst_lp
921 ; at this point xh and xl are carying the logical cluster number
922 ; printstring "find subsector"
923 ; ################# Get Subsector within the logical Cluster for later use
925 lds zl,fat_clustersize
929 breq fat_found_subsec
933 rjmp fat_search_clst_lp2
938 sts fat_clust_offset,zl
940 ; Check against last HOSTDISK searched
944 brne fat_wrong_cache_clst
946 ; Check against last Cluster searched
948 lds yh,fat_log_clust+1
951 brne fat_wrong_cache_clst
953 brne fat_wrong_cache_clst
955 ; Last Cluster = searched Cluster -> get Sectornumber from cache
957 lds xh,fat_clust_ptr+1
958 lds yl,fat_clust_ptr+2
959 lds yh,fat_clust_ptr+3
963 ; Cluster is not in cache, so we must search for it
964 fat_wrong_cache_clst:
968 sts fat_log_clust+1,xh
970 ; Map Logical Cluster-Number to "Physical" Cluster Number using the FAT
971 rcall fat_find_phsy_clust
973 ; Get StartSector of "physical" Cluster
978 ; Found the physical sector
979 .if FAT16_DBG_FAT > 0
980 printstring "Found phys. Sector at:"
990 ; Save the found Sector for later use into cache
991 sts fat_clust_ptr ,xl
992 sts fat_clust_ptr+1,xh
993 sts fat_clust_ptr+2,yl
994 sts fat_clust_ptr+3,yh
996 ; Add- Subsector to Startsector
998 lds zl,fat_clust_offset
1004 ; Found the physical sector
1005 .if FAT16_DBG_FAT > 0
1006 printstring "Sector with Offset at:"
1020 ; ============================================================================
1021 ; Function: Does a Disk write operation
1022 ; ============================================================================
1023 ; Registers: [out] temp Error-Variable ( 0= No Error)
1024 ; Variables: [in] hostdsk host disk #, (partition #)
1025 ; [in] hostlba host block #, relative to part.start
1026 ; [in] hostbuf Sector to be written
1027 ; ----------------------------------------------------------------------------
1029 ; This Routine writes a Sector to the Imagefile inside an FAT16 Partition.
1030 ; ============================================================================
1033 .if FAT16_RWDEBUG > 1
1035 printstring "host write "
1044 rjmp fat_rdwr_err ; skip disk change detection code
1046 ; After a second thought, the following code doesn't make sense, because
1047 ; disk change (change of one or more disk images) can not reliably detected.
1048 ; At least with the existing code.
1052 rcall mgr_init_partitions
1058 call mmcWriteSect ; disabled till read is functioning
1071 ; ============================================================================
1072 ; Function: Does a Disk read operation
1073 ; ============================================================================
1075 ; Variables: [in] hostdsk host disk #, (partition #)
1076 ; [in] hostlba host block #, relative to part.start
1077 ; [out] hostbuf Sector read by this routine
1078 ; ----------------------------------------------------------------------------
1080 ; This Routine reads a Sector from the Imagefile inside an FAT16 Partition.
1081 ; ============================================================================
1084 .if FAT16_RWDEBUG > 1
1086 printstring "host read "
1093 .if FAT16_RWDEBUG > 0
1094 printstring "Read Image Sector:"
1113 rjmp fat_rdwr_err ; skip disk change detection code
1115 rcall mgr_init_partitions