Rev 379 | Rev 383 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 379 | Rev 380 | ||
---|---|---|---|
1 | /*#######################################################################################*/ |
1 | /*#######################################################################################*/ |
2 | /* !!! THIS IS NOT FREE SOFTWARE !!! */ |
2 | /* !!! THIS IS NOT FREE SOFTWARE !!! */ |
3 | /*#######################################################################################*/ |
3 | /*#######################################################################################*/ |
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
5 | // + www.MikroKopter.com |
5 | // + www.MikroKopter.com |
6 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
6 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
7 | // + Software Nutzungsbedingungen (english version: see below) |
7 | // + Software Nutzungsbedingungen (english version: see below) |
8 | // + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt - |
8 | // + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt - |
9 | // + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den |
9 | // + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den |
10 | // + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool |
10 | // + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool |
11 | // + - nachfolgend Software genannt - nur für private Zwecke zu nutzen. |
11 | // + - nachfolgend Software genannt - nur für private Zwecke zu nutzen. |
12 | // + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig. |
12 | // + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig. |
13 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
13 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
14 | // + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im |
14 | // + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im |
15 | // + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu. |
15 | // + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu. |
16 | // + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie |
16 | // + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie |
17 | // + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden. |
17 | // + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden. |
18 | // + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren |
18 | // + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren |
19 | // + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt. |
19 | // + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt. |
20 | // + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren |
20 | // + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren |
21 | // + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand |
21 | // + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand |
22 | // + des Mitverschuldens offen. |
22 | // + des Mitverschuldens offen. |
23 | // + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet. |
23 | // + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet. |
24 | // + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt. |
24 | // + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt. |
25 | // + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern. |
25 | // + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern. |
26 | // + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang |
26 | // + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang |
27 | // + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt. |
27 | // + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt. |
28 | // + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software. |
28 | // + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software. |
29 | // + #### ENDE DER NUTZUNGSBEDINGUNGEN ####' |
29 | // + #### ENDE DER NUTZUNGSBEDINGUNGEN ####' |
30 | // + Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar. |
30 | // + Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar. |
31 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
31 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
32 | // + Software LICENSING TERMS |
32 | // + Software LICENSING TERMS |
33 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
33 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
34 | // + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor - |
34 | // + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor - |
35 | // + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware |
35 | // + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware |
36 | // + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*. |
36 | // + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*. |
37 | // + The Software may only be used with the Licensor's products. |
37 | // + The Software may only be used with the Licensor's products. |
38 | // + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this |
38 | // + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this |
39 | // + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this |
39 | // + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this |
40 | // + agreement shall be the property of the Licensor. |
40 | // + agreement shall be the property of the Licensor. |
41 | // + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other |
41 | // + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other |
42 | // + features that can be used to identify the program may not be altered or defaced by the customer. |
42 | // + features that can be used to identify the program may not be altered or defaced by the customer. |
43 | // + The customer shall be responsible for taking reasonable precautions |
43 | // + The customer shall be responsible for taking reasonable precautions |
44 | // + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the |
44 | // + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the |
45 | // + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and |
45 | // + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and |
46 | // + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product |
46 | // + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product |
47 | // + liability. However, the Licensor shall be entitled to the defense of contributory negligence. |
47 | // + liability. However, the Licensor shall be entitled to the defense of contributory negligence. |
48 | // + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test |
48 | // + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test |
49 | // + the software for his purpose before any operational usage. The customer will backup his data before using the software. |
49 | // + the software for his purpose before any operational usage. The customer will backup his data before using the software. |
50 | // + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data |
50 | // + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data |
51 | // + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations. |
51 | // + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations. |
52 | // + *) The territory aspect only refers to the place where the Software is used, not its programmed range. |
52 | // + *) The territory aspect only refers to the place where the Software is used, not its programmed range. |
53 | // + #### END OF LICENSING TERMS #### |
53 | // + #### END OF LICENSING TERMS #### |
54 | // + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de. |
54 | // + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de. |
55 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
55 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
56 | #include <stdio.h> |
56 | #include <stdio.h> |
57 | #include <string.h> |
57 | #include <string.h> |
58 | #include "91x_lib.h" |
58 | #include "91x_lib.h" |
59 | #include "timer1.h" |
59 | #include "timer1.h" |
60 | #include "fat16.h" |
60 | #include "fat16.h" |
61 | #include "sdc.h" |
61 | #include "sdc.h" |
62 | #include "uart1.h" |
62 | #include "uart1.h" |
63 | #include "main.h" |
63 | #include "main.h" |
64 | 64 | ||
65 | //________________________________________________________________________________________________________________________________________ |
65 | //________________________________________________________________________________________________________________________________________ |
66 | // Module name: fat16.c |
66 | // Module name: fat16.c |
67 | // Compiler used: avr-gcc 3.4.5 |
67 | // Compiler used: avr-gcc 3.4.5 |
68 | // Last Modifikation: 20.03.2010 |
68 | // Last Modifikation: 20.03.2010 |
69 | // Version: 2.10 |
69 | // Version: 2.10 |
70 | // Authors: Stephan Busker & Gregor Stobrawa |
70 | // Authors: Stephan Busker & Gregor Stobrawa |
71 | // Description: Source files for FAT16 implementation with read and write-access |
71 | // Description: Source files for FAT16 implementation with read and write-access |
72 | // Copyright (C) 2008 Stephan Busker & Gregor Stobrawa |
72 | // Copyright (C) 2008 Stephan Busker & Gregor Stobrawa |
73 | //........................................................................................................................................ |
73 | //........................................................................................................................................ |
74 | // Functions: extern s16 Fat16_Init(void); |
74 | // Functions: extern s16 Fat16_Init(void); |
75 | // extern s16 Fat16_Deinit(void); |
75 | // extern s16 Fat16_Deinit(void); |
76 | // extern s8* FAT16_GetVolumeLabel(void); |
76 | // extern s8* FAT16_GetVolumeLabel(void); |
77 | // extern File_t * fopen_(const u8 *filename, const s8 mode); |
77 | // extern File_t * fopen_(const u8 *filename, const s8 mode); |
78 | // extern s16 fclose_(File_t *File); |
78 | // extern s16 fclose_(File_t *File); |
79 | // extern u8 fexist_(s8 * const filename); |
79 | // extern u8 fexist_(s8 * const filename); |
80 | // extern s16 fflush_(File_t *File); |
80 | // extern s16 fflush_(File_t *File); |
81 | // extern s16 fseek_(File_t *File, s32 offset, s16 origin); |
81 | // extern s16 fseek_(File_t *File, s32 offset, s16 origin); |
82 | // extern s16 fgetc_(File_t *File); |
82 | // extern s16 fgetc_(File_t *File); |
83 | // extern s16 fputc_(u8 c, File_t *File); |
83 | // extern s16 fputc_(u8 c, File_t *File); |
84 | // extern u32 fread_(void *buffer, u32 size, u32 count, File_t *File); |
84 | // extern u32 fread_(void *buffer, u32 size, u32 count, File_t *File); |
85 | // extern u32 fwrite_(void *buffer, u32 size, u32 count, File_t *File); |
85 | // extern u32 fwrite_(void *buffer, u32 size, u32 count, File_t *File); |
86 | // extern s16 fputs_(const u8 *string, File_t *File); |
86 | // extern s16 fputs_(const u8 *string, File_t *File); |
87 | // extern u8 * fgets_(u8 *string, s16 length, File_t *File); |
87 | // extern u8 * fgets_(u8 *string, s16 length, File_t *File); |
88 | // extern u8 feof_(File_t * const file); |
88 | // extern u8 feof_(File_t * const file); |
89 | //........................................................................................................................................ |
89 | //........................................................................................................................................ |
90 | // ext. functions: extern SD_Result_t SDC_Init(void;) |
90 | // ext. functions: extern SD_Result_t SDC_Init(void;) |
91 | // extern SD_Result_t SDC_Deinit(void); |
91 | // extern SD_Result_t SDC_Deinit(void); |
92 | // extern SD_Result_t SDC_GetSector (u32,u8 *); |
92 | // extern SD_Result_t SDC_GetSector (u32,u8 *); |
93 | // extern SD_Result_t SDC_PutSector (u32,u8 *); |
93 | // extern SD_Result_t SDC_PutSector (u32,u8 *); |
94 | //........................................................................................................................................ |
94 | //........................................................................................................................................ |
95 | // |
95 | // |
96 | // URL: www.Mikro-Control.de |
96 | // URL: www.Mikro-Control.de |
97 | // mailto: stephan.busker@mikro-control.de |
97 | // mailto: stephan.busker@mikro-control.de |
98 | //________________________________________________________________________________________________________________________________________ |
98 | //________________________________________________________________________________________________________________________________________ |
99 | 99 | ||
100 | /* |
100 | /* |
101 | FAT16 Drive Layout: |
101 | FAT16 Drive Layout: |
102 | Description Offset |
102 | Description Offset |
103 | Volume Boot Sector Start of Partition |
103 | Volume Boot Sector Start of Partition |
104 | Fat Tables Start + # of Reserved Sectors |
104 | Fat Tables Start + # of Reserved Sectors |
105 | Root Directory Entry Start + # of Reserved + (# of Sectors Per FAT * 2) |
105 | Root Directory Entry Start + # of Reserved + (# of Sectors Per FAT * 2) |
106 | Data Area (Starts with Cluster #2) Start + # of Reserved + (# of Sectors Per FAT * 2) + ((Maximum Root Directory Entries * 32) / Bytes per Sector) |
106 | Data Area (Starts with Cluster #2) Start + # of Reserved + (# of Sectors Per FAT * 2) + ((Maximum Root Directory Entries * 32) / Bytes per Sector) |
107 | */ |
107 | */ |
108 | 108 | ||
109 | 109 | ||
110 | 110 | ||
111 | /* |
111 | /* |
112 | ________________________________________________________________________________________________________________________________________ |
112 | ________________________________________________________________________________________________________________________________________ |
113 | 113 | ||
114 | Structure of an partition entry |
114 | Structure of an partition entry |
115 | ________________________________________________________________________________________________________________________________________ |
115 | ________________________________________________________________________________________________________________________________________ |
116 | 116 | ||
117 | Partition Entry is 16 bytes long |
117 | Partition Entry is 16 bytes long |
118 | */ |
118 | */ |
119 | typedef struct |
119 | typedef struct |
120 | { |
120 | { |
121 | u8 PartitionState; // Current State of Partition (00h=Inactive, 80h=Active) |
121 | u8 PartitionState; // Current State of Partition (00h=Inactive, 80h=Active) |
122 | u8 BeginningHead; // Beginning of Partition - Head |
122 | u8 BeginningHead; // Beginning of Partition - Head |
123 | u16 BeginningCylSec; // Beginning of Partition - Cylinder/Sector (See Below) |
123 | u16 BeginningCylSec; // Beginning of Partition - Cylinder/Sector (See Below) |
124 | u8 Type; // Type of Partition (See List Below) |
124 | u8 Type; // Type of Partition (See List Below) |
125 | u8 EndHead; // End of Partition - Head |
125 | u8 EndHead; // End of Partition - Head |
126 | u16 EndCylSec; // End of Partition - Cylinder/Sector |
126 | u16 EndCylSec; // End of Partition - Cylinder/Sector |
127 | u32 NoSectorsBeforePartition; // Number of Sectors between the MBR and the First Sector in the Partition |
127 | u32 NoSectorsBeforePartition; // Number of Sectors between the MBR and the First Sector in the Partition |
128 | u32 NoSectorsPartition ; // Number of Sectors in the Partition |
128 | u32 NoSectorsPartition ; // Number of Sectors in the Partition |
129 | } __attribute__((packed)) PartitionEntry_t; |
129 | } __attribute__((packed)) PartitionEntry_t; |
130 | 130 | ||
131 | 131 | ||
132 | 132 | ||
133 | /* |
133 | /* |
134 | Coding of Cylinder/Sector words |
134 | Coding of Cylinder/Sector words |
135 | 135 | ||
136 | Cylinder is 10 bits: [7:0] at [15:8] and [9:8] at [7:6] |
136 | Cylinder is 10 bits: [7:0] at [15:8] and [9:8] at [7:6] |
137 | Sector is 5 bits: [5:0] at [5:0] |
137 | Sector is 5 bits: [5:0] at [5:0] |
138 | */ |
138 | */ |
139 | 139 | ||
140 | // Partition Types: |
140 | // Partition Types: |
141 | #define PART_TYPE_UNKNOWN 0x00 |
141 | #define PART_TYPE_UNKNOWN 0x00 |
142 | #define PART_TYPE_FAT12 0x01 |
142 | #define PART_TYPE_FAT12 0x01 |
143 | #define PART_TYPE_XENIX 0x02 |
143 | #define PART_TYPE_XENIX 0x02 |
144 | #define PART_TYPE_FAT16_ST_32_MB 0x04 |
144 | #define PART_TYPE_FAT16_ST_32_MB 0x04 |
145 | #define PART_TYPE_EXTDOS 0x05 |
145 | #define PART_TYPE_EXTDOS 0x05 |
146 | #define PART_TYPE_FAT16_LT_32_MB 0x06 |
146 | #define PART_TYPE_FAT16_LT_32_MB 0x06 |
147 | #define PART_TYPE_NTFS 0x07 |
147 | #define PART_TYPE_NTFS 0x07 |
148 | #define PART_TYPE_FAT32 0x0B |
148 | #define PART_TYPE_FAT32 0x0B |
149 | #define PART_TYPE_FAT32LBA 0x0C |
149 | #define PART_TYPE_FAT32LBA 0x0C |
150 | #define PART_TYPE_FAT16LBA 0x0E |
150 | #define PART_TYPE_FAT16LBA 0x0E |
151 | #define PART_TYPE_EXTDOSLBA 0x0F |
151 | #define PART_TYPE_EXTDOSLBA 0x0F |
152 | #define PART_TYPE_EISA 0x12 |
152 | #define PART_TYPE_EISA 0x12 |
153 | #define PART_TYPE_ONTRACK 0x33 |
153 | #define PART_TYPE_ONTRACK 0x33 |
154 | #define PART_TYPE_NOVELL 0x40 |
154 | #define PART_TYPE_NOVELL 0x40 |
155 | #define PART_TYPE_DYNAMIC 0x42 |
155 | #define PART_TYPE_DYNAMIC 0x42 |
156 | #define PART_TYPE_PCIX 0x4B |
156 | #define PART_TYPE_PCIX 0x4B |
157 | #define PART_TYPE_LINUX_SWAP 0x82 |
157 | #define PART_TYPE_LINUX_SWAP 0x82 |
158 | #define PART_TYPE_LINUX_NATIVE 0x83 |
158 | #define PART_TYPE_LINUX_NATIVE 0x83 |
159 | #define PART_TYPE_LINUX_LVM 0x8E |
159 | #define PART_TYPE_LINUX_LVM 0x8E |
160 | #define PART_TYPE_PHOENIXSAVE 0xA0 |
160 | #define PART_TYPE_PHOENIXSAVE 0xA0 |
161 | #define PART_TYPE_FREEBSD 0xA5 |
161 | #define PART_TYPE_FREEBSD 0xA5 |
162 | #define PART_TYPE_OPENBSD 0xA6 |
162 | #define PART_TYPE_OPENBSD 0xA6 |
163 | #define PART_TYPE_NETNBSD 0xA9 |
163 | #define PART_TYPE_NETNBSD 0xA9 |
164 | #define PART_TYPE_CPM 0xDB |
164 | #define PART_TYPE_CPM 0xDB |
165 | #define PART_TYPE_DBFS 0xE0 |
165 | #define PART_TYPE_DBFS 0xE0 |
166 | #define PART_TYPE_BBT 0xFF |
166 | #define PART_TYPE_BBT 0xFF |
167 | 167 | ||
168 | 168 | ||
169 | /* |
169 | /* |
170 | ________________________________________________________________________________________________________________________________________ |
170 | ________________________________________________________________________________________________________________________________________ |
171 | 171 | ||
172 | Structure of the MasterBootRecord |
172 | Structure of the MasterBootRecord |
173 | ________________________________________________________________________________________________________________________________________ |
173 | ________________________________________________________________________________________________________________________________________ |
174 | 174 | ||
175 | Master Boot Record is 512 bytes long |
175 | Master Boot Record is 512 bytes long |
176 | The Master Boot Record is the same for pretty much all Operating Systems. |
176 | The Master Boot Record is the same for pretty much all Operating Systems. |
177 | It is located on the first Sector of the Hard Drive, at Cylinder 0, Head 0, Sector 1 |
177 | It is located on the first Sector of the Hard Drive, at Cylinder 0, Head 0, Sector 1 |
178 | */ |
178 | */ |
179 | typedef struct |
179 | typedef struct |
180 | { |
180 | { |
181 | u8 ExecutableCode[446]; // 446 bytes for machine start code |
181 | u8 ExecutableCode[446]; // 446 bytes for machine start code |
182 | PartitionEntry_t PartitionEntry1; // 16 bytes for partition entry 1 |
182 | PartitionEntry_t PartitionEntry1; // 16 bytes for partition entry 1 |
183 | PartitionEntry_t PartitionEntry2; // 16 bytes for partition entry 2 |
183 | PartitionEntry_t PartitionEntry2; // 16 bytes for partition entry 2 |
184 | PartitionEntry_t PartitionEntry3; // 16 bytes for partition entry 3 |
184 | PartitionEntry_t PartitionEntry3; // 16 bytes for partition entry 3 |
185 | PartitionEntry_t PartitionEntry4; // 16 bytes for partition entry 4 |
185 | PartitionEntry_t PartitionEntry4; // 16 bytes for partition entry 4 |
186 | u16 ExecutableMarker; // BIOS-Signature (0x55 0xAA) |
186 | u16 ExecutableMarker; // BIOS-Signature (0x55 0xAA) |
187 | } __attribute__((packed)) MBR_Entry_t; |
187 | } __attribute__((packed)) MBR_Entry_t; |
188 | 188 | ||
189 | 189 | ||
190 | /* |
190 | /* |
191 | ________________________________________________________________________________________________________________________________________ |
191 | ________________________________________________________________________________________________________________________________________ |
192 | 192 | ||
193 | Structure of the VolumeBootRecord |
193 | Structure of the VolumeBootRecord |
194 | ________________________________________________________________________________________________________________________________________ |
194 | ________________________________________________________________________________________________________________________________________ |
195 | 195 | ||
196 | The Volume Boot Record is 512 bytes long |
196 | The Volume Boot Record is 512 bytes long |
197 | This information is located in the first sector of every partition. |
197 | This information is located in the first sector of every partition. |
198 | */ |
198 | */ |
199 | typedef struct |
199 | typedef struct |
200 | { |
200 | { |
201 | u8 JumpCode[3]; // Jump Code + NOP |
201 | u8 JumpCode[3]; // Jump Code + NOP |
202 | s8 OEMName[8]; // OEM Name |
202 | s8 OEMName[8]; // OEM Name |
203 | u16 BytesPerSector; // Bytes Per Sector |
203 | u16 BytesPerSector; // Bytes Per Sector |
204 | u8 SectorsPerCluster; // Sectors Per Cluster |
204 | u8 SectorsPerCluster; // Sectors Per Cluster |
205 | u16 ReservedSectors; // Reserved Sectors |
205 | u16 ReservedSectors; // Reserved Sectors |
206 | u8 NoFATCopies; // Number of Copies of FAT |
206 | u8 NoFATCopies; // Number of Copies of FAT |
207 | u16 MaxRootEntries; // Maximum Root Directory Entries |
207 | u16 MaxRootEntries; // Maximum Root Directory Entries |
208 | u16 NoSectorsInPartSml32MB; // Number of Sectors in Partition Smaller than 32 MB |
208 | u16 NoSectorsInPartSml32MB; // Number of Sectors in Partition Smaller than 32 MB |
209 | u8 MediaDescriptor; // Media Descriptor (0xF8 for Hard Disks) |
209 | u8 MediaDescriptor; // Media Descriptor (0xF8 for Hard Disks) |
210 | u16 SectorsPerFAT; // Sectors Per FAT |
210 | u16 SectorsPerFAT; // Sectors Per FAT |
211 | u16 SectorsPerTrack; // Sectors Per Track |
211 | u16 SectorsPerTrack; // Sectors Per Track |
212 | u16 NoHeads; // Number of Heads |
212 | u16 NoHeads; // Number of Heads |
213 | u32 NoHiddenSectors; // Number of Hidden Sectors in Partition |
213 | u32 NoHiddenSectors; // Number of Hidden Sectors in Partition |
214 | u32 NoSectors; // Number of Sectors in Partition |
214 | u32 NoSectors; // Number of Sectors in Partition |
215 | u16 DriveNo; // Logical Drive Number of Partition |
215 | u16 DriveNo; // Logical Drive Number of Partition |
216 | u8 ExtendedSig; // Extended Signature (0x29) |
216 | u8 ExtendedSig; // Extended Signature (0x29) |
217 | u32 SerialNo; // Serial Number of the Partition |
217 | u32 SerialNo; // Serial Number of the Partition |
218 | s8 VolumeName[11]; // Volume Name of the Partititon |
218 | s8 VolumeName[11]; // Volume Name of the Partititon |
219 | s8 FATName[8]; // FAT Name (FAT16) |
219 | s8 FATName[8]; // FAT Name (FAT16) |
220 | u8 ExecutableCode[446]; // 446 bytes for machine start code |
220 | u8 ExecutableCode[446]; // 446 bytes for machine start code |
221 | u16 ExecutableMarker; // Executable Marker (0x55 0xAA) |
221 | u16 ExecutableMarker; // Executable Marker (0x55 0xAA) |
222 | } __attribute__((packed)) VBR_Entry_t; |
222 | } __attribute__((packed)) VBR_Entry_t; |
223 | 223 | ||
224 | 224 | ||
225 | 225 | ||
226 | /* |
226 | /* |
227 | ________________________________________________________________________________________________________________________________________ |
227 | ________________________________________________________________________________________________________________________________________ |
228 | 228 | ||
229 | Structure of an directory entry |
229 | Structure of an directory entry |
230 | ________________________________________________________________________________________________________________________________________ |
230 | ________________________________________________________________________________________________________________________________________ |
231 | 231 | ||
232 | Directory entry is 32 bytes. |
232 | Directory entry is 32 bytes. |
233 | */ |
233 | */ |
234 | typedef struct |
234 | typedef struct |
235 | { |
235 | { |
236 | s8 Name[8]; // 8 bytes name, padded with spaces. |
236 | s8 Name[8]; // 8 bytes name, padded with spaces. |
237 | u8 Extension[3]; // 3 bytes extension, padded with spaces. |
237 | u8 Extension[3]; // 3 bytes extension, padded with spaces. |
238 | u8 Attribute; // attribute of the directory entry (unused,archive,read-only,system,directory,volume) |
238 | u8 Attribute; // attribute of the directory entry (unused,archive,read-only,system,directory,volume) |
239 | u8 Res1; // should be zero |
239 | u8 Res1; // should be zero |
240 | u8 CreationTime10ms; // subsecond resolution of creation time |
240 | u8 CreationTime10ms; // subsecond resolution of creation time |
241 | u16 CreationTime; // Time of creation h:m:s |
241 | u16 CreationTime; // Time of creation h:m:s |
242 | u16 CreationDate; // Date of creation Y.M.D |
242 | u16 CreationDate; // Date of creation Y.M.D |
243 | u16 LastAccessDate; // The date where the file was last accessed |
243 | u16 LastAccessDate; // The date where the file was last accessed |
244 | u8 Res2[2]; // should be zero |
244 | u8 Res2[2]; // should be zero |
245 | u16 ModTime; // date of last write access |
245 | u16 ModTime; // date of last write access |
246 | u16 ModDate; // date of last write access to the file or directory. |
246 | u16 ModDate; // date of last write access to the file or directory. |
247 | u16 StartCluster; // first cluster of the file or directory. |
247 | u16 StartCluster; // first cluster of the file or directory. |
248 | u32 Size; // size of the file or directory in bytes. |
248 | u32 Size; // size of the file or directory in bytes. |
249 | } __attribute__((packed)) DirEntry_t; |
249 | } __attribute__((packed)) DirEntry_t; |
250 | 250 | ||
251 | #define SLOT_EMPTY 0x00 // slot has never been used |
251 | #define SLOT_EMPTY 0x00 // slot has never been used |
252 | #define SLOT_E5 0x05 // the real value is 0xe5 |
252 | #define SLOT_E5 0x05 // the real value is 0xe5 |
253 | #define SLOT_DELETED 0xE5 // file in this slot deleted |
253 | #define SLOT_DELETED 0xE5 // file in this slot deleted |
254 | 254 | ||
255 | #define ATTR_NONE 0x00 // normal file |
255 | #define ATTR_NONE 0x00 // normal file |
256 | #define ATTR_READONLY 0x01 // file is readonly |
256 | #define ATTR_READONLY 0x01 // file is readonly |
257 | #define ATTR_HIDDEN 0x02 // file is hidden |
257 | #define ATTR_HIDDEN 0x02 // file is hidden |
258 | #define ATTR_SYSTEM 0x04 // file is a system file |
258 | #define ATTR_SYSTEM 0x04 // file is a system file |
259 | #define ATTR_VOLUMELABEL 0x08 // entry is a volume label |
259 | #define ATTR_VOLUMELABEL 0x08 // entry is a volume label |
260 | #define ATTR_LONG_FILENAME 0x0F // this is a long filename entry |
260 | #define ATTR_LONG_FILENAME 0x0F // this is a long filename entry |
261 | #define ATTR_SUBDIRECTORY 0x10 // entry is a directory name |
261 | #define ATTR_SUBDIRECTORY 0x10 // entry is a directory name |
262 | #define ATTR_ARCHIVE 0x20 // file is new or modified |
262 | #define ATTR_ARCHIVE 0x20 // file is new or modified |
263 | 263 | ||
264 | 264 | ||
265 | /* |
265 | /* |
266 | ________________________________________________________________________________________________________________________________________ |
266 | ________________________________________________________________________________________________________________________________________ |
267 | 267 | ||
268 | Structure of an entry within the fileallocationtable. |
268 | Structure of an entry within the fileallocationtable. |
269 | ________________________________________________________________________________________________________________________________________ |
269 | ________________________________________________________________________________________________________________________________________ |
270 | */ |
270 | */ |
271 | typedef struct |
271 | typedef struct |
272 | { |
272 | { |
273 | u16 NextCluster; // the next cluster of the file. |
273 | u16 NextCluster; // the next cluster of the file. |
274 | } __attribute__((packed)) Fat16Entry_t; |
274 | } __attribute__((packed)) Fat16Entry_t; |
275 | 275 | ||
276 | // secial fat entries |
276 | // secial fat entries |
277 | #define FAT16_CLUSTER_FREE 0x0000 |
277 | #define FAT16_CLUSTER_FREE 0x0000 |
278 | #define FAT16_CLUSTER_RESERVED 0x0001 |
278 | #define FAT16_CLUSTER_RESERVED 0x0001 |
279 | #define FAT16_CLUSTER_USED_MIN 0x0002 |
279 | #define FAT16_CLUSTER_USED_MIN 0x0002 |
280 | #define FAT16_CLUSTER_USED_MAX 0xFFEF |
280 | #define FAT16_CLUSTER_USED_MAX 0xFFEF |
281 | #define FAT16_CLUSTER_ROOTDIR_MIN 0xFFF0 |
281 | #define FAT16_CLUSTER_ROOTDIR_MIN 0xFFF0 |
282 | #define FAT16_CLUSTER_ROOTDIR_MAX 0xFFF6 |
282 | #define FAT16_CLUSTER_ROOTDIR_MAX 0xFFF6 |
283 | #define FAT16_CLUSTER_BAD 0xFFF7 |
283 | #define FAT16_CLUSTER_BAD 0xFFF7 |
284 | #define FAT16_CLUSTER_LAST_MIN 0xFFF8 |
284 | #define FAT16_CLUSTER_LAST_MIN 0xFFF8 |
285 | #define FAT16_CLUSTER_LAST_MAX 0xFFFF |
285 | #define FAT16_CLUSTER_LAST_MAX 0xFFFF |
286 | 286 | ||
287 | /*****************************************************************************************************************************************/ |
287 | /*****************************************************************************************************************************************/ |
288 | /* */ |
288 | /* */ |
289 | /* Global variables needed for read- or write-acces to the FAT16- filesystem. */ |
289 | /* Global variables needed for read- or write-acces to the FAT16- filesystem. */ |
290 | /* */ |
290 | /* */ |
291 | /*****************************************************************************************************************************************/ |
291 | /*****************************************************************************************************************************************/ |
292 | 292 | ||
293 | #define MBR_SECTOR 0x00 // the masterboot record is located in sector 0. |
293 | #define MBR_SECTOR 0x00 // the masterboot record is located in sector 0. |
294 | #define DIRENTRY_SIZE 32 //bytes |
294 | #define DIRENTRY_SIZE 32 //bytes |
295 | #define DIRENTRIES_PER_SECTOR BYTES_PER_SECTOR/DIRENTRY_SIZE |
295 | #define DIRENTRIES_PER_SECTOR BYTES_PER_SECTOR/DIRENTRY_SIZE |
296 | #define FAT16_BYTES 2 |
296 | #define FAT16_BYTES 2 |
297 | #define FAT16_ENTRIES_PER_SECTOR BYTES_PER_SECTOR/FAT16_BYTES |
297 | #define FAT16_ENTRIES_PER_SECTOR BYTES_PER_SECTOR/FAT16_BYTES |
298 | 298 | ||
299 | #define SECTOR_UNDEFINED 0x00000000L |
299 | #define SECTOR_UNDEFINED 0x00000000L |
300 | #define CLUSTER_UNDEFINED 0x0000 |
300 | #define CLUSTER_UNDEFINED 0x0000 |
301 | 301 | ||
302 | #define FSTATE_UNUSED 0 |
302 | #define FSTATE_UNUSED 0 |
303 | #define FSTATE_USED 1 |
303 | #define FSTATE_USED 1 |
304 | 304 | ||
305 | typedef struct |
305 | typedef struct |
306 | { |
306 | { |
307 | u8 IsValid; // 0 means invalid, else valid |
307 | u8 IsValid; // 0 means invalid, else valid |
308 | u8 SectorsPerCluster; // how many sectors does a cluster contain? |
308 | u8 SectorsPerCluster; // how many sectors does a cluster contain? |
309 | u8 FatCopies; // Numbers of copies of the FAT |
309 | u8 FatCopies; // Numbers of copies of the FAT |
310 | u16 MaxRootEntries; // Possible number of entries in the root directory. |
310 | u16 MaxRootEntries; // Possible number of entries in the root directory. |
311 | u16 SectorsPerFat; // how many sectors does a fat16 contain? |
311 | u16 SectorsPerFat; // how many sectors does a fat16 contain? |
312 | u32 FirstFatSector; // sector of the start of the fat |
312 | u32 FirstFatSector; // sector of the start of the fat |
313 | u32 FirstRootDirSector; // sector of the rootdirectory |
313 | u32 FirstRootDirSector; // sector of the rootdirectory |
314 | u32 FirstDataSector; // sector of the first cluster containing data (cluster2). |
314 | u32 FirstDataSector; // sector of the first cluster containing data (cluster2). |
315 | u32 LastDataSector; // the last data sector of the partition |
315 | u32 LastDataSector; // the last data sector of the partition |
316 | u8 VolumeLabel[12]; // the volume label |
316 | u8 VolumeLabel[12]; // the volume label |
317 | u32 CurrentWorkingDirectory;// A pointer to the directory we are actual using |
317 | u32 CurrentWorkingDirectory;// A pointer to the directory we are actual using |
318 | s8 PathToCwd[256]; // a string containing the complete path to the current working directory |
318 | s8 PathToCwd[256]; // a string containing the complete path to the current working directory |
319 | } __attribute__((packed)) Partition_t; |
319 | } __attribute__((packed)) Partition_t; |
320 | 320 | ||
321 | Partition_t Partition; // Structure holds partition information |
321 | Partition_t Partition; // Structure holds partition information |
322 | 322 | ||
323 | File_t FilePointer[FILE_MAX_OPEN]; // Allocate Memmoryspace for each filepointer used. |
323 | File_t FilePointer[FILE_MAX_OPEN]; // Allocate Memmoryspace for each filepointer used. |
324 | 324 | ||
325 | 325 | ||
326 | 326 | ||
327 | 327 | ||
328 | /****************************************************************************************************************************************/ |
328 | /****************************************************************************************************************************************/ |
329 | /* Function: FileDate(DateTime_t *); */ |
329 | /* Function: FileDate(DateTime_t *); */ |
330 | /* */ |
330 | /* */ |
331 | /* Description: This function calculates the DOS date from a pointer to a time structure. */ |
331 | /* Description: This function calculates the DOS date from a pointer to a time structure. */ |
332 | /* */ |
332 | /* */ |
333 | /* Returnvalue: Returns the DOS date. */ |
333 | /* Returnvalue: Returns the DOS date. */ |
334 | /****************************************************************************************************************************************/ |
334 | /****************************************************************************************************************************************/ |
335 | u16 FileDate(DateTime_t * pTimeStruct) |
335 | u16 FileDate(DateTime_t * pTimeStruct) |
336 | { |
336 | { |
337 | u16 date = 0; |
337 | u16 date = 0; |
338 | if(pTimeStruct == NULL) return date; |
338 | if(pTimeStruct == NULL) return date; |
339 | if(!(pTimeStruct->Valid)) return date; |
339 | if(!(pTimeStruct->Valid)) return date; |
340 | 340 | ||
341 | date |= (0x007F & (u16)(pTimeStruct->Year - 1980))<<9; // set year |
341 | date |= (0x007F & (u16)(pTimeStruct->Year - 1980))<<9; // set year |
342 | date |= (0x000F & (u16)(pTimeStruct->Month))<<5; // set month |
342 | date |= (0x000F & (u16)(pTimeStruct->Month))<<5; // set month |
343 | date |= (0x001F & (u16)(pTimeStruct->Day)); |
343 | date |= (0x001F & (u16)(pTimeStruct->Day)); |
344 | return date; |
344 | return date; |
345 | } |
345 | } |
346 | 346 | ||
347 | /****************************************************************************************************************************************/ |
347 | /****************************************************************************************************************************************/ |
348 | /* Function: FileTime(DateTime_t *); */ |
348 | /* Function: FileTime(DateTime_t *); */ |
349 | /* */ |
349 | /* */ |
350 | /* Description: This function calculates the DOS time from a pointer to a time structure. */ |
350 | /* Description: This function calculates the DOS time from a pointer to a time structure. */ |
351 | /* */ |
351 | /* */ |
352 | /* Returnvalue: Returns the DOS time. */ |
352 | /* Returnvalue: Returns the DOS time. */ |
353 | /****************************************************************************************************************************************/ |
353 | /****************************************************************************************************************************************/ |
354 | 354 | ||
355 | u16 FileTime(DateTime_t * pTimeStruct) |
355 | u16 FileTime(DateTime_t * pTimeStruct) |
356 | { |
356 | { |
357 | u16 time = 0; |
357 | u16 time = 0; |
358 | if(pTimeStruct == NULL) return time; |
358 | if(pTimeStruct == NULL) return time; |
359 | if(!(pTimeStruct->Valid)) return time; |
359 | if(!(pTimeStruct->Valid)) return time; |
360 | 360 | ||
361 | time |= (0x001F & (u16)(pTimeStruct->Hour))<<11; |
361 | time |= (0x001F & (u16)(pTimeStruct->Hour))<<11; |
362 | time |= (0x003F & (u16)(pTimeStruct->Min))<<5; |
362 | time |= (0x003F & (u16)(pTimeStruct->Min))<<5; |
363 | time |= (0x001F & (u16)(pTimeStruct->Sec/2)); |
363 | time |= (0x001F & (u16)(pTimeStruct->Sec/2)); |
364 | return time; |
364 | return time; |
365 | } |
365 | } |
366 | 366 | ||
367 | /****************************************************************************************************************************************/ |
367 | /****************************************************************************************************************************************/ |
368 | /* Function: LockFilePointer(); */ |
368 | /* Function: LockFilePointer(); */ |
369 | /* */ |
369 | /* */ |
370 | /* Description: This function trys to lock a free file pointer. */ |
370 | /* Description: This function trys to lock a free file pointer. */ |
371 | /* */ |
371 | /* */ |
372 | /* Returnvalue: Returns the Filepointer on success or 0. */ |
372 | /* Returnvalue: Returns the Filepointer on success or 0. */ |
373 | /****************************************************************************************************************************************/ |
373 | /****************************************************************************************************************************************/ |
374 | File_t * LockFilePointer(void) |
374 | File_t * LockFilePointer(void) |
375 | { |
375 | { |
376 | u8 i; |
376 | u8 i; |
377 | File_t * File = 0; |
377 | File_t * File = 0; |
378 | for(i = 0; i < FILE_MAX_OPEN; i++) |
378 | for(i = 0; i < FILE_MAX_OPEN; i++) |
379 | { |
379 | { |
380 | if(FilePointer[i].State == FSTATE_UNUSED) // found an unused one |
380 | if(FilePointer[i].State == FSTATE_UNUSED) // found an unused one |
381 | { |
381 | { |
382 | File = &FilePointer[i]; // set pointer to that entry |
382 | File = &FilePointer[i]; // set pointer to that entry |
383 | FilePointer[i].State = FSTATE_USED; // mark it as used |
383 | FilePointer[i].State = FSTATE_USED; // mark it as used |
384 | break; |
384 | break; |
385 | } |
385 | } |
386 | } |
386 | } |
387 | return(File); |
387 | return(File); |
388 | } |
388 | } |
389 | 389 | ||
390 | /****************************************************************************************************************************************/ |
390 | /****************************************************************************************************************************************/ |
391 | /* Function: UnlockFilePointer(file_t *); */ |
391 | /* Function: UnlockFilePointer(file_t *); */ |
392 | /* */ |
392 | /* */ |
393 | /* Description: This function trys to unlock a file pointer. */ |
393 | /* Description: This function trys to unlock a file pointer. */ |
394 | /* */ |
394 | /* */ |
395 | /* Returnvalue: Returns 1 if file pointer was freed else 0. */ |
395 | /* Returnvalue: Returns 1 if file pointer was freed else 0. */ |
396 | /****************************************************************************************************************************************/ |
396 | /****************************************************************************************************************************************/ |
397 | u8 UnlockFilePointer(File_t * file) |
397 | u8 UnlockFilePointer(File_t * file) |
398 | { |
398 | { |
399 | u8 cnt; |
399 | u8 cnt; |
400 | if(file == NULL) return(0); |
400 | if(file == NULL) return(0); |
401 | for(cnt = 0; cnt < FILE_MAX_OPEN; cnt++) |
401 | for(cnt = 0; cnt < FILE_MAX_OPEN; cnt++) |
402 | { |
402 | { |
403 | if(&FilePointer[cnt] == file) // filepointer to be freed found? |
403 | if(&FilePointer[cnt] == file) // filepointer to be freed found? |
404 | { |
404 | { |
405 | file->State = FSTATE_UNUSED; |
405 | file->State = FSTATE_UNUSED; |
406 | file->FirstSectorOfFirstCluster = SECTOR_UNDEFINED; // Sectorpointer to the first sector of the first datacluster of the file. |
406 | file->FirstSectorOfFirstCluster = SECTOR_UNDEFINED; // Sectorpointer to the first sector of the first datacluster of the file. |
407 | file->FirstSectorOfCurrCluster = SECTOR_UNDEFINED; // Pointer to the cluster which is edited at the moment. |
407 | file->FirstSectorOfCurrCluster = SECTOR_UNDEFINED; // Pointer to the cluster which is edited at the moment. |
408 | file->SectorOfCurrCluster = 0; // The sector which is edited at the moment (cluster_pointer + sector_index). |
408 | file->SectorOfCurrCluster = 0; // The sector which is edited at the moment (cluster_pointer + sector_index). |
409 | file->ByteOfCurrSector = 0; // The bytelocation within the current sector (cluster_pointer + sector_index + byte_index). |
409 | file->ByteOfCurrSector = 0; // The bytelocation within the current sector (cluster_pointer + sector_index + byte_index). |
410 | file->Mode = 0; // mode of fileoperation (read,write) |
410 | file->Mode = 0; // mode of fileoperation (read,write) |
411 | file->Size = 0; // the size of the opend file in bytes. |
411 | file->Size = 0; // the size of the opend file in bytes. |
412 | file->Position = 0; // pointer to a character within the file 0 < fileposition < filesize |
412 | file->Position = 0; // pointer to a character within the file 0 < fileposition < filesize |
413 | file->SectorInCache = SECTOR_UNDEFINED; // the last sector read, wich is still in the sectorbuffer. |
413 | file->SectorInCache = SECTOR_UNDEFINED; // the last sector read, wich is still in the sectorbuffer. |
414 | file->DirectorySector = SECTOR_UNDEFINED; // the sectorposition where the directoryentry has been made. |
414 | file->DirectorySector = SECTOR_UNDEFINED; // the sectorposition where the directoryentry has been made. |
415 | file->DirectoryIndex = 0; // the index to the directoryentry within the specified sector. |
415 | file->DirectoryIndex = 0; // the index to the directoryentry within the specified sector. |
416 | file->Attribute = 0; // the attribute of the file opened. |
416 | file->Attribute = 0; // the attribute of the file opened. |
417 | file = NULL; |
417 | file = NULL; |
418 | return(1); |
418 | return(1); |
419 | } |
419 | } |
420 | } |
420 | } |
421 | return(0); |
421 | return(0); |
422 | } |
422 | } |
423 | 423 | ||
424 | /****************************************************************************************************************************************/ |
424 | /****************************************************************************************************************************************/ |
425 | /* Function: SeperateDirName(s8*, s8*); */ |
425 | /* Function: SeperateDirName(s8*, s8*); */ |
426 | /* */ |
426 | /* */ |
427 | /* Description: This function seperates the first dirname from filepath and brings them */ |
427 | /* Description: This function seperates the first dirname from filepath and brings them */ |
428 | /* into the needed format ('test.txt' -> 'TEST TXT') */ |
428 | /* into the needed format ('test.txt' -> 'TEST TXT') */ |
429 | /* The subpath is the pointer to the remaining substring of the filepath */ |
429 | /* The subpath is the pointer to the remaining substring of the filepath */ |
430 | /* */ |
430 | /* */ |
431 | /* Returnvalue: Return NULL on error or pointer to subpath */ |
431 | /* Returnvalue: Return NULL on error or pointer to subpath */ |
432 | /****************************************************************************************************************************************/ |
432 | /****************************************************************************************************************************************/ |
433 | s8* SeperateDirName(const s8 *filepath, s8 *dirname) |
433 | s8* SeperateDirName(const s8 *filepath, s8 *dirname) |
434 | { |
434 | { |
435 | s8* subpath = NULL; |
435 | s8* subpath = NULL; |
436 | u8 readpointer = 0; |
436 | u8 readpointer = 0; |
437 | u8 writepointer = 0; |
437 | u8 writepointer = 0; |
438 | 438 | ||
439 | // search subpath from beginning of filepath |
439 | // search subpath from beginning of filepath |
440 | subpath = NULL; |
440 | subpath = NULL; |
441 | readpointer = 0; |
441 | readpointer = 0; |
442 | if(filepath[0] == '/') readpointer = 1; // ignore first '/' |
442 | if(filepath[0] == '/') readpointer = 1; // ignore first '/' |
443 | while(subpath == NULL) // search the filepath until a subpath was found. |
443 | while(subpath == NULL) // search the filepath until a subpath was found. |
444 | { |
444 | { |
445 | if(((filepath[readpointer] == 0) || (filepath[readpointer] == '/'))) // if '/' found or end of filepath reached |
445 | if(((filepath[readpointer] == 0) || (filepath[readpointer] == '/'))) // if '/' found or end of filepath reached |
446 | { |
446 | { |
447 | subpath = (s8*)&filepath[readpointer]; // store the position of the first "/" found after the beginning of the filenpath |
447 | subpath = (s8*)&filepath[readpointer]; // store the position of the first "/" found after the beginning of the filenpath |
448 | } |
448 | } |
449 | readpointer++; |
449 | readpointer++; |
450 | } |
450 | } |
451 | 451 | ||
452 | // clear dirname with spaces |
452 | // clear dirname with spaces |
453 | dirname[11] = 0; // terminate dirname |
453 | dirname[11] = 0; // terminate dirname |
454 | for(writepointer = 0; writepointer < 11; writepointer++) dirname[writepointer] = ' '; |
454 | for(writepointer = 0; writepointer < 11; writepointer++) dirname[writepointer] = ' '; |
455 | 455 | ||
456 | // handle the special dirnames "." and ".." seperately |
456 | // handle the special dirnames "." and ".." seperately |
457 | readpointer = 0; |
457 | readpointer = 0; |
458 | if(filepath[0] == '/') readpointer++; |
458 | if(filepath[0] == '/') readpointer++; |
459 | // if we are trying to enter directories "." or ".." |
459 | // if we are trying to enter directories "." or ".." |
460 | if(filepath[readpointer] == '.') |
460 | if(filepath[readpointer] == '.') |
461 | { |
461 | { |
462 | // directory '.' |
462 | // directory '.' |
463 | if(filepath[readpointer+1] == 0) |
463 | if(filepath[readpointer+1] == 0) |
464 | { |
464 | { |
465 | dirname[0] = '.'; |
465 | dirname[0] = '.'; |
466 | return((s8*)&filepath[readpointer]); |
466 | return((s8*)&filepath[readpointer]); |
467 | } |
467 | } |
468 | // directory '..' |
468 | // directory '..' |
469 | if((filepath[readpointer+1] == '.') && (filepath[readpointer+2] == 0)) |
469 | if((filepath[readpointer+1] == '.') && (filepath[readpointer+2] == 0)) |
470 | { |
470 | { |
471 | dirname[0] = '.'; |
471 | dirname[0] = '.'; |
472 | dirname[1] = '.'; |
472 | dirname[1] = '.'; |
473 | return((s8*)&filepath[readpointer]); |
473 | return((s8*)&filepath[readpointer]); |
474 | } |
474 | } |
475 | } |
475 | } |
476 | 476 | ||
477 | writepointer = 0; |
477 | writepointer = 0; |
478 | // start seperating the dirname from the filepath. |
478 | // start seperating the dirname from the filepath. |
479 | readpointer = 0; |
479 | readpointer = 0; |
480 | if(filepath[0] == '/') readpointer = 1; // ignore first '/' |
480 | if(filepath[0] == '/') readpointer = 1; // ignore first '/' |
481 | while( &filepath[readpointer] < subpath) |
481 | while( &filepath[readpointer] < subpath) |
482 | { |
482 | { |
483 | if(writepointer >= 11) return(NULL); // dirname to long |
483 | if(writepointer >= 11) return(NULL); // dirname to long |
484 | if(filepath[readpointer] == '.') // seperating dirname and extension. |
484 | if(filepath[readpointer] == '.') // seperating dirname and extension. |
485 | { |
485 | { |
486 | if(writepointer <= 8) |
486 | if(writepointer <= 8) |
487 | { |
487 | { |
488 | readpointer++; // next character in filename |
488 | readpointer++; // next character in filename |
489 | writepointer = 8; // jump to start of extension |
489 | writepointer = 8; // jump to start of extension |
490 | } |
490 | } |
491 | else return(NULL); // dirbasename to long |
491 | else return(NULL); // dirbasename to long |
492 | } |
492 | } |
493 | else |
493 | else |
494 | { |
494 | { |
495 | if((0x60 < filepath[readpointer]) && (filepath[readpointer] < 0x7B)) |
495 | if((0x60 < filepath[readpointer]) && (filepath[readpointer] < 0x7B)) |
496 | { |
496 | { |
497 | dirname[writepointer] = (filepath[readpointer] - 0x20); // all characters must be upper case. |
497 | dirname[writepointer] = (filepath[readpointer] - 0x20); // all characters must be upper case. |
498 | } |
498 | } |
499 | else |
499 | else |
500 | { |
500 | { |
501 | dirname[writepointer] = filepath[readpointer]; |
501 | dirname[writepointer] = filepath[readpointer]; |
502 | } |
502 | } |
503 | readpointer++; |
503 | readpointer++; |
504 | writepointer++; |
504 | writepointer++; |
505 | } |
505 | } |
506 | } |
506 | } |
507 | return(subpath); |
507 | return(subpath); |
508 | } |
508 | } |
509 | 509 | ||
510 | 510 | ||
511 | /**************************************************************************************************************************************+*/ |
511 | /**************************************************************************************************************************************+*/ |
512 | /* Function: Fat16ClusterToSector( u16 cluster); */ |
512 | /* Function: Fat16ClusterToSector( u16 cluster); */ |
513 | /* */ |
513 | /* */ |
514 | /* Description: This function converts a cluster number given by the fat to the corresponding */ |
514 | /* Description: This function converts a cluster number given by the fat to the corresponding */ |
515 | /* sector that points to the start of the data area that is represented by the cluster number. */ |
515 | /* sector that points to the start of the data area that is represented by the cluster number. */ |
516 | /* */ |
516 | /* */ |
517 | /* Returnvalue: The sector number with the data area of the given cluster */ |
517 | /* Returnvalue: The sector number with the data area of the given cluster */ |
518 | /****************************************************************************************************************************************/ |
518 | /****************************************************************************************************************************************/ |
519 | u32 Fat16ClusterToSector(u16 cluster) |
519 | u32 Fat16ClusterToSector(u16 cluster) |
520 | { |
520 | { |
521 | if(!Partition.IsValid) return SECTOR_UNDEFINED; |
521 | if(!Partition.IsValid) return SECTOR_UNDEFINED; |
522 | if ((cluster < 2) || (cluster == CLUSTER_UNDEFINED)) |
522 | if ((cluster < 2) || (cluster == CLUSTER_UNDEFINED)) |
523 | { |
523 | { |
524 | return SECTOR_UNDEFINED; |
524 | return SECTOR_UNDEFINED; |
525 | } |
525 | } |
526 | else |
526 | else |
527 | { |
527 | { |
528 | return ( (cluster - 2) * Partition.SectorsPerCluster) + Partition.FirstDataSector; // the first data sector is represented by the 2nd cluster |
528 | return ( (cluster - 2) * Partition.SectorsPerCluster) + Partition.FirstDataSector; // the first data sector is represented by the 2nd cluster |
529 | } |
529 | } |
530 | } |
530 | } |
531 | 531 | ||
532 | /****************************************************************************************************************************************/ |
532 | /****************************************************************************************************************************************/ |
533 | /* Function: SectorToFat16Cluster( u32 sector); */ |
533 | /* Function: SectorToFat16Cluster( u32 sector); */ |
534 | /* */ |
534 | /* */ |
535 | /* Description: This function converts a given sector number given to the corresponding */ |
535 | /* Description: This function converts a given sector number given to the corresponding */ |
536 | /* cluster number in the fat that represents this data area. */ |
536 | /* cluster number in the fat that represents this data area. */ |
537 | /* */ |
537 | /* */ |
538 | /* Returnvalue: The cluster number representing the data area of the sector. */ |
538 | /* Returnvalue: The cluster number representing the data area of the sector. */ |
539 | /****************************************************************************************************************************************/ |
539 | /****************************************************************************************************************************************/ |
540 | u16 SectorToFat16Cluster(u32 sector) |
540 | u16 SectorToFat16Cluster(u32 sector) |
541 | { |
541 | { |
542 | if(!Partition.IsValid) return CLUSTER_UNDEFINED; |
542 | if(!Partition.IsValid) return CLUSTER_UNDEFINED; |
543 | if((sector == SECTOR_UNDEFINED) || (sector < Partition.FirstDataSector)) return CLUSTER_UNDEFINED; |
543 | if((sector == SECTOR_UNDEFINED) || (sector < Partition.FirstDataSector)) return CLUSTER_UNDEFINED; |
544 | else return ((u16)((sector - Partition.FirstDataSector) / Partition.SectorsPerCluster) + 2); |
544 | else return ((u16)((sector - Partition.FirstDataSector) / Partition.SectorsPerCluster) + 2); |
545 | } |
545 | } |
546 | 546 | ||
547 | 547 | ||
548 | /****************************************************************************************************************************************/ |
548 | /****************************************************************************************************************************************/ |
549 | /* Function: Fat16_IsValid(void); */ |
549 | /* Function: Fat16_IsValid(void); */ |
550 | /* */ |
550 | /* */ |
551 | /* Description: This function return the Fat 16 filesystem state */ |
551 | /* Description: This function return the Fat 16 filesystem state */ |
552 | /* */ |
552 | /* */ |
553 | /* Returnvalue: The function returns "1" on success */ |
553 | /* Returnvalue: The function returns "1" on success */ |
554 | /****************************************************************************************************************************************/ |
554 | /****************************************************************************************************************************************/ |
555 | u8 Fat16_IsValid(void) |
555 | u8 Fat16_IsValid(void) |
556 | { |
556 | { |
557 | return(Partition.IsValid); |
557 | return(Partition.IsValid); |
558 | } |
558 | } |
559 | 559 | ||
560 | /****************************************************************************************************************************************/ |
560 | /****************************************************************************************************************************************/ |
561 | /* Function: Fat16_Deinit(void); */ |
561 | /* Function: Fat16_Deinit(void); */ |
562 | /* */ |
562 | /* */ |
563 | /* Description: This function uninitializes the fat 16 api */ |
563 | /* Description: This function uninitializes the fat 16 api */ |
564 | /* */ |
564 | /* */ |
565 | /* Returnvalue: The function returns "0" on success */ |
565 | /* Returnvalue: The function returns "0" on success */ |
566 | /****************************************************************************************************************************************/ |
566 | /****************************************************************************************************************************************/ |
567 | u8 Fat16_Deinit(void) |
567 | u8 Fat16_Deinit(void) |
568 | { |
568 | { |
569 | s16 returnvalue = 0; |
569 | s16 returnvalue = 0; |
570 | u8 cnt; |
570 | u8 cnt; |
571 | 571 | ||
572 | UART1_PutString("\r\n FAT16 deinit..."); |
572 | UART1_PutString("\r\n FAT16 deinit..."); |
573 | // declare the filepointers as unused. |
573 | // declare the filepointers as unused. |
574 | for(cnt = 0; cnt < FILE_MAX_OPEN; cnt++) |
574 | for(cnt = 0; cnt < FILE_MAX_OPEN; cnt++) |
575 | { |
575 | { |
576 | UnlockFilePointer(&FilePointer[cnt]); |
576 | UnlockFilePointer(&FilePointer[cnt]); |
577 | } |
577 | } |
578 | returnvalue = SDC_Deinit(); // uninitialize interface to sd-card |
578 | returnvalue = SDC_Deinit(); // uninitialize interface to sd-card |
579 | Partition.IsValid = 0; // mark data in partition structure as invalid |
579 | Partition.IsValid = 0; // mark data in partition structure as invalid |
580 | Partition.VolumeLabel[0]='\0'; |
580 | Partition.VolumeLabel[0]='\0'; |
581 | UART1_PutString("ok"); |
581 | UART1_PutString("ok"); |
582 | SD_WatchDog = 0; |
582 | SD_WatchDog = 0; |
583 | return(returnvalue); |
583 | return(returnvalue); |
584 | } |
584 | } |
585 | 585 | ||
586 | /****************************************************************************************************************************************/ |
586 | /****************************************************************************************************************************************/ |
587 | /* Function: Fat16_Init(void); */ |
587 | /* Function: Fat16_Init(void); */ |
588 | /* */ |
588 | /* */ |
589 | /* Description: This function reads the Masterbootrecord and finds the position of the Volumebootrecord, the FAT and the Rootdir */ |
589 | /* Description: This function reads the Masterbootrecord and finds the position of the Volumebootrecord, the FAT and the Rootdir */ |
590 | /* and stores the information in global variables. */ |
590 | /* and stores the information in global variables. */ |
591 | /* */ |
591 | /* */ |
592 | /* Returnvalue: The function returns "0" if the filesystem is initialized. */ |
592 | /* Returnvalue: The function returns "0" if the filesystem is initialized. */ |
593 | /****************************************************************************************************************************************/ |
593 | /****************************************************************************************************************************************/ |
594 | u8 Fat16_Init(void) |
594 | u8 Fat16_Init(void) |
595 | { |
595 | { |
596 | u8 cnt = 0; |
596 | u8 cnt = 0; |
597 | u32 partitionfirstsector; |
597 | u32 partitionfirstsector; |
598 | VBR_Entry_t *VBR; |
598 | VBR_Entry_t *VBR; |
599 | MBR_Entry_t *MBR; |
599 | MBR_Entry_t *MBR; |
600 | File_t *file; |
600 | File_t *file; |
601 | u8 result = 0; |
601 | u8 result = 0; |
602 | 602 | ||
603 | UART1_PutString("\r\n FAT16 init..."); |
603 | UART1_PutString("\r\n FAT16 init..."); |
604 | Partition.IsValid = 0; |
604 | Partition.IsValid = 0; |
605 | // declare the filepointers as unused. |
605 | // declare the filepointers as unused. |
606 | for(cnt = 0; cnt < FILE_MAX_OPEN; cnt++) |
606 | for(cnt = 0; cnt < FILE_MAX_OPEN; cnt++) |
607 | { |
607 | { |
608 | UnlockFilePointer(&FilePointer[cnt]); |
608 | UnlockFilePointer(&FilePointer[cnt]); |
609 | } |
609 | } |
610 | // set current file pinter to first position in list |
610 | // set current file pinter to first position in list |
611 | file = &FilePointer[0]; |
611 | file = &FilePointer[0]; |
612 | 612 | ||
613 | // try to initialize the sd-card. |
613 | // try to initialize the sd-card. |
614 | if(SD_SUCCESS != SDC_Init()) |
614 | if(SD_SUCCESS != SDC_Init()) |
615 | { |
615 | { |
616 | UART1_PutString("SD-Card could not be initialized."); |
616 | UART1_PutString("SD-Card could not be initialized."); |
617 | result = 1; |
617 | result = 1; |
618 | goto end; |
618 | goto end; |
619 | } |
619 | } |
620 | 620 | ||
621 | // SD-Card is initialized successfully |
621 | // SD-Card is initialized successfully |
622 | if(SD_SUCCESS != SDC_GetSector((u32)MBR_SECTOR,file->Cache)) // Read the MasterBootRecord |
622 | if(SD_SUCCESS != SDC_GetSector((u32)MBR_SECTOR,file->Cache)) // Read the MasterBootRecord |
623 | { |
623 | { |
624 | UART1_PutString("Error reading the MBR."); |
624 | UART1_PutString("Error reading the MBR."); |
625 | result = 2; |
625 | result = 2; |
626 | goto end; |
626 | goto end; |
627 | } |
627 | } |
628 | MBR = (MBR_Entry_t *)file->Cache; // Enter the MBR using the structure MBR_Entry_t. |
628 | MBR = (MBR_Entry_t *)file->Cache; // Enter the MBR using the structure MBR_Entry_t. |
629 | if((MBR->PartitionEntry1.Type == PART_TYPE_FAT16_ST_32_MB) || |
629 | if((MBR->PartitionEntry1.Type == PART_TYPE_FAT16_ST_32_MB) || |
630 | (MBR->PartitionEntry1.Type == PART_TYPE_FAT16_LT_32_MB) || |
630 | (MBR->PartitionEntry1.Type == PART_TYPE_FAT16_LT_32_MB) || |
631 | (MBR->PartitionEntry1.Type == PART_TYPE_FAT16LBA)) |
631 | (MBR->PartitionEntry1.Type == PART_TYPE_FAT16LBA)) |
632 | { |
632 | { |
633 | // get sector offset 1st partition |
633 | // get sector offset 1st partition |
634 | partitionfirstsector = MBR->PartitionEntry1.NoSectorsBeforePartition; |
634 | partitionfirstsector = MBR->PartitionEntry1.NoSectorsBeforePartition; |
635 | // Start of Partition is the Volume Boot Sector |
635 | // Start of Partition is the Volume Boot Sector |
636 | if(SD_SUCCESS != SDC_GetSector(partitionfirstsector,file->Cache)) // Read the volume boot record |
636 | if(SD_SUCCESS != SDC_GetSector(partitionfirstsector,file->Cache)) // Read the volume boot record |
637 | { |
637 | { |
638 | UART1_PutString("Error reading the VBR."); |
638 | UART1_PutString("Error reading the VBR."); |
639 | result = 3; |
639 | result = 3; |
640 | goto end; |
640 | goto end; |
641 | } |
641 | } |
642 | } |
642 | } |
643 | else // maybe the medium has no partition assuming sector 0 is the vbr |
643 | else // maybe the medium has no partition assuming sector 0 is the vbr |
644 | { |
644 | { |
645 | partitionfirstsector = 0; |
645 | partitionfirstsector = 0; |
646 | } |
646 | } |
647 | 647 | ||
648 | VBR = (VBR_Entry_t *) file->Cache; // Enter the VBR using the structure VBR_Entry_t. |
648 | VBR = (VBR_Entry_t *) file->Cache; // Enter the VBR using the structure VBR_Entry_t. |
649 | if(VBR->BytesPerSector != BYTES_PER_SECTOR) |
649 | if(VBR->BytesPerSector != BYTES_PER_SECTOR) |
650 | { |
650 | { |
651 | UART1_PutString("VBR: Sector size not supported."); |
651 | UART1_PutString("VBR: Sector size not supported."); |
652 | result = 4; |
652 | result = 4; |
653 | goto end; |
653 | goto end; |
654 | } |
654 | } |
655 | Partition.SectorsPerCluster = VBR->SectorsPerCluster; // Number of sectors per cluster. Depends on the memorysize of the sd-card. |
655 | Partition.SectorsPerCluster = VBR->SectorsPerCluster; // Number of sectors per cluster. Depends on the memorysize of the sd-card. |
656 | Partition.FatCopies = VBR->NoFATCopies; // Number of fatcopies. |
656 | Partition.FatCopies = VBR->NoFATCopies; // Number of fatcopies. |
657 | Partition.MaxRootEntries = VBR->MaxRootEntries; // How many Entries are possible in the rootdirectory (FAT16 allows max. 512 entries). |
657 | Partition.MaxRootEntries = VBR->MaxRootEntries; // How many Entries are possible in the rootdirectory (FAT16 allows max. 512 entries). |
658 | Partition.SectorsPerFat = VBR->SectorsPerFAT; // The number of sectors per FAT // copy volume label |
658 | Partition.SectorsPerFat = VBR->SectorsPerFAT; // The number of sectors per FAT // copy volume label |
659 | Partition.VolumeLabel[0] = '\0'; // set string terminator |
659 | Partition.VolumeLabel[0] = '\0'; // set string terminator |
660 | 660 | ||
661 | /* Calculate the sectorpositon of the FAT, the Rootdirectory and the first Datacluster. */ |
661 | /* Calculate the sectorpositon of the FAT, the Rootdirectory and the first Datacluster. */ |
662 | // Calculate the position of the FileAllocationTable: |
662 | // Calculate the position of the FileAllocationTable: |
663 | // Start + # of Reserved Sectors |
663 | // Start + # of Reserved Sectors |
664 | Partition.FirstFatSector = (u32)(partitionfirstsector + (u32)(VBR->ReservedSectors)); |
664 | Partition.FirstFatSector = (u32)(partitionfirstsector + (u32)(VBR->ReservedSectors)); |
665 | // Calculate the position of the Rootdirectory: |
665 | // Calculate the position of the Rootdirectory: |
666 | // Start + # of Reserved Sectors + (# of Sectors Per FAT * # of FAT Copies) |
666 | // Start + # of Reserved Sectors + (# of Sectors Per FAT * # of FAT Copies) |
667 | Partition.FirstRootDirSector = Partition.FirstFatSector + (u32)((u32)Partition.SectorsPerFat*(u32)Partition.FatCopies); |
667 | Partition.FirstRootDirSector = Partition.FirstFatSector + (u32)((u32)Partition.SectorsPerFat*(u32)Partition.FatCopies); |
668 | // Calculate the position of the first datacluster: |
668 | // Calculate the position of the first datacluster: |
669 | // Start + # of Reserved + (# of Sectors Per FAT * # of FAT Copies) + ((Maximum Root Directory Entries * 32) / Bytes per Sector) |
669 | // Start + # of Reserved + (# of Sectors Per FAT * # of FAT Copies) + ((Maximum Root Directory Entries * 32) / Bytes per Sector) |
670 | Partition.FirstDataSector = Partition.FirstRootDirSector + (u32)(Partition.MaxRootEntries>>4); // assuming 512 Byte Per Sector |
670 | Partition.FirstDataSector = Partition.FirstRootDirSector + (u32)(Partition.MaxRootEntries>>4); // assuming 512 Byte Per Sector |
671 | // Calculate the last data sector |
671 | // Calculate the last data sector |
672 | if(VBR->NoSectors == 0) |
672 | if(VBR->NoSectors == 0) |
673 | { |
673 | { |
674 | UART1_PutString("VBR: Bad number of sectors."); |
674 | UART1_PutString("VBR: Bad number of sectors."); |
675 | result = 5; |
675 | result = 5; |
676 | goto end; |
676 | goto end; |
677 | } |
677 | } |
678 | Partition.LastDataSector = Partition.FirstDataSector + VBR->NoSectors - 1; |
678 | Partition.LastDataSector = Partition.FirstDataSector + VBR->NoSectors - 1; |
679 | // check for FAT16 in VBR of first partition |
679 | // check for FAT16 in VBR of first partition |
680 | if(!((VBR->FATName[0]=='F') && (VBR->FATName[1]=='A') && (VBR->FATName[2]=='T') && (VBR->FATName[3]=='1')&&(VBR->FATName[4]=='6'))) |
680 | if(!((VBR->FATName[0]=='F') && (VBR->FATName[1]=='A') && (VBR->FATName[2]=='T') && (VBR->FATName[3]=='1')&&(VBR->FATName[4]=='6'))) |
681 | { |
681 | { |
682 | UART1_PutString("VBR: Partition ist not FAT16 type."); |
682 | UART1_PutString("VBR: Partition ist not FAT16 type."); |
683 | result = 6; |
683 | result = 6; |
684 | goto end; |
684 | goto end; |
685 | } |
685 | } |
686 | Partition.IsValid = 1; // mark data in partition structure as valid |
686 | Partition.IsValid = 1; // mark data in partition structure as valid |
687 | Partition.CurrentWorkingDirectory = Partition.FirstRootDirSector; |
687 | Partition.CurrentWorkingDirectory = Partition.FirstRootDirSector; |
688 | strcpy(Partition.PathToCwd,"/"); |
688 | strcpy(Partition.PathToCwd,"/"); |
689 | result = 0; |
689 | result = 0; |
690 | end: |
690 | end: |
691 | if(result != 0) Fat16_Deinit(); |
691 | if(result != 0) Fat16_Deinit(); |
692 | else UART1_PutString("ok"); |
692 | else UART1_PutString("ok"); |
693 | return(result); |
693 | return(result); |
694 | } |
694 | } |
695 | 695 | ||
696 | 696 | ||
697 | 697 | ||
698 | /****************************************************************************************************************************************/ |
698 | /****************************************************************************************************************************************/ |
699 | /* Function: ClearCurrCluster(File_t*); */ |
699 | /* Function: ClearCurrCluster(File_t*); */ |
700 | /* */ |
700 | /* */ |
701 | /* Description: This function fills the current cluster with 0. */ |
701 | /* Description: This function fills the current cluster with 0. */ |
702 | /* */ |
702 | /* */ |
703 | /* Returnvalue: The function returns 1 on success else 0. */ |
703 | /* Returnvalue: The function returns 1 on success else 0. */ |
704 | /****************************************************************************************************************************************/ |
704 | /****************************************************************************************************************************************/ |
705 | u8 ClearCurrCluster(File_t * file) |
705 | u8 ClearCurrCluster(File_t * file) |
706 | { |
706 | { |
707 | u8 retvalue = 1; |
707 | u8 retvalue = 1; |
708 | u32 i; |
708 | u32 i; |
709 | 709 | ||
710 | if((!Partition.IsValid) || (file == NULL)) return(0); |
710 | if((!Partition.IsValid) || (file == NULL)) return(0); |
711 | 711 | ||
712 | for(i = 0; i < BYTES_PER_SECTOR; i++) file->Cache[i] = 0; // clear file cache |
712 | for(i = 0; i < BYTES_PER_SECTOR; i++) file->Cache[i] = 0; // clear file cache |
713 | if(file->FirstSectorOfCurrCluster == SECTOR_UNDEFINED) return (0); // nothing to do |
713 | if(file->FirstSectorOfCurrCluster == SECTOR_UNDEFINED) return (0); // nothing to do |
714 | for(i = 0; i < Partition.SectorsPerCluster; i++) |
714 | for(i = 0; i < Partition.SectorsPerCluster; i++) |
715 | { |
715 | { |
716 | file->SectorInCache = file->FirstSectorOfCurrCluster + i; |
716 | file->SectorInCache = file->FirstSectorOfCurrCluster + i; |
717 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache)) |
717 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache)) |
718 | { |
718 | { |
719 | Fat16_Deinit(); |
719 | Fat16_Deinit(); |
720 | return(0); |
720 | return(0); |
721 | } |
721 | } |
722 | } |
722 | } |
723 | return(retvalue); |
723 | return(retvalue); |
724 | } |
724 | } |
725 | 725 | ||
726 | /*****************************************************************************************************************************************/ |
726 | /*****************************************************************************************************************************************/ |
727 | /* Function: GetNextCluster(File_t* ); */ |
727 | /* Function: GetNextCluster(File_t* ); */ |
728 | /* */ |
728 | /* */ |
729 | /* Description: This function finds the next datacluster of the file specified with File *File. */ |
729 | /* Description: This function finds the next datacluster of the file specified with File *File. */ |
730 | /* */ |
730 | /* */ |
731 | /* Returnvalue: The function returns the next cluster or 0 if the last cluster has already reached. */ |
731 | /* Returnvalue: The function returns the next cluster or 0 if the last cluster has already reached. */ |
732 | /*****************************************************************************************************************************************/ |
732 | /*****************************************************************************************************************************************/ |
733 | u16 GetNextCluster(File_t * file) |
733 | u16 GetNextCluster(File_t * file) |
734 | { |
734 | { |
735 | u16 cluster = CLUSTER_UNDEFINED; |
735 | u16 cluster = CLUSTER_UNDEFINED; |
736 | u32 fat_byte_offset, sector, byte; |
736 | u32 fat_byte_offset, sector, byte; |
737 | Fat16Entry_t * fat; |
737 | Fat16Entry_t * fat; |
738 | 738 | ||
739 | if((!Partition.IsValid) || (file == NULL)) return(cluster); |
739 | if((!Partition.IsValid) || (file == NULL)) return(cluster); |
740 | if(file->FirstSectorOfCurrCluster == SECTOR_UNDEFINED) return(cluster); |
740 | if(file->FirstSectorOfCurrCluster == SECTOR_UNDEFINED) return(cluster); |
741 | // if sector is within the data area |
741 | // if sector is within the data area |
742 | if((Partition.FirstDataSector <= file->FirstSectorOfCurrCluster)&& (file->FirstSectorOfCurrCluster <= Partition.LastDataSector)) |
742 | if((Partition.FirstDataSector <= file->FirstSectorOfCurrCluster)&& (file->FirstSectorOfCurrCluster <= Partition.LastDataSector)) |
743 | { |
743 | { |
744 | // determine current file cluster |
744 | // determine current file cluster |
745 | cluster = SectorToFat16Cluster(file->FirstSectorOfCurrCluster); |
745 | cluster = SectorToFat16Cluster(file->FirstSectorOfCurrCluster); |
746 | // calculate byte offset in the fat for corresponding entry |
746 | // calculate byte offset in the fat for corresponding entry |
747 | fat_byte_offset = ((u32)cluster)<<1; // two FAT bytes (16 bits) for every cluster |
747 | fat_byte_offset = ((u32)cluster)<<1; // two FAT bytes (16 bits) for every cluster |
748 | // calculate the sector that contains the current cluster within the fat |
748 | // calculate the sector that contains the current cluster within the fat |
749 | sector = Partition.FirstFatSector + ( fat_byte_offset / BYTES_PER_SECTOR); |
749 | sector = Partition.FirstFatSector + ( fat_byte_offset / BYTES_PER_SECTOR); |
750 | // calculate byte offset of the current cluster within that fat sector |
750 | // calculate byte offset of the current cluster within that fat sector |
751 | byte = fat_byte_offset % BYTES_PER_SECTOR; |
751 | byte = fat_byte_offset % BYTES_PER_SECTOR; |
752 | // read this sector to the file cache |
752 | // read this sector to the file cache |
753 | if(file->SectorInCache != sector) |
753 | if(file->SectorInCache != sector) |
754 | { |
754 | { |
755 | file->SectorInCache = sector; // update sector stored in buffer |
755 | file->SectorInCache = sector; // update sector stored in buffer |
756 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache)) // read sector from sd-card |
756 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache)) // read sector from sd-card |
757 | { |
757 | { |
758 | Fat16_Deinit(); |
758 | Fat16_Deinit(); |
759 | return (CLUSTER_UNDEFINED); |
759 | return (CLUSTER_UNDEFINED); |
760 | } |
760 | } |
761 | } |
761 | } |
762 | // read the next cluster from cache |
762 | // read the next cluster from cache |
763 | fat = (Fat16Entry_t *)(&(file->Cache[byte])); |
763 | fat = (Fat16Entry_t *)(&(file->Cache[byte])); |
764 | cluster = fat->NextCluster; |
764 | cluster = fat->NextCluster; |
765 | // if no next cluster exist |
765 | // if no next cluster exist |
766 | if(FAT16_CLUSTER_LAST_MIN <= cluster) |
766 | if(FAT16_CLUSTER_LAST_MIN <= cluster) |
767 | { |
767 | { |
768 | cluster = CLUSTER_UNDEFINED; // next cluster is undefined |
768 | cluster = CLUSTER_UNDEFINED; // next cluster is undefined |
769 | } |
769 | } |
770 | else |
770 | else |
771 | { |
771 | { |
772 | file->FirstSectorOfCurrCluster = Fat16ClusterToSector(cluster); |
772 | file->FirstSectorOfCurrCluster = Fat16ClusterToSector(cluster); |
773 | file->SectorOfCurrCluster = 0; |
773 | file->SectorOfCurrCluster = 0; |
774 | file->ByteOfCurrSector = 0; |
774 | file->ByteOfCurrSector = 0; |
775 | } |
775 | } |
776 | } |
776 | } |
777 | return(cluster); |
777 | return(cluster); |
778 | } |
778 | } |
779 | 779 | ||
780 | 780 | ||
781 | 781 | ||
782 | /****************************************************************************************************************************************/ |
782 | /****************************************************************************************************************************************/ |
783 | /* Function: FindNextFreeCluster(File_t *); */ |
783 | /* Function: FindNextFreeCluster(File_t *); */ |
784 | /* */ |
784 | /* */ |
785 | /* Description: This function looks in the fat to find the next free cluster */ |
785 | /* Description: This function looks in the fat to find the next free cluster */ |
786 | /* */ |
786 | /* */ |
787 | /* Returnvalue: The function returns the cluster number of the next free cluster found within the fat. */ |
787 | /* Returnvalue: The function returns the cluster number of the next free cluster found within the fat. */ |
788 | /****************************************************************************************************************************************/ |
788 | /****************************************************************************************************************************************/ |
789 | u16 FindNextFreeCluster(File_t *file) |
789 | u16 FindNextFreeCluster(File_t *file) |
790 | { |
790 | { |
791 | u32 fat_sector; // current sector within the fat relative to the first sector of the fat. |
791 | u32 fat_sector; // current sector within the fat relative to the first sector of the fat. |
792 | u32 curr_sector; // current sector |
792 | u32 curr_sector; // current sector |
793 | u16 fat_entry; // index to an fatentry within the actual sector (256 fatentries are possible within one sector). |
793 | u16 fat_entry; // index to an fatentry within the actual sector (256 fatentries are possible within one sector). |
794 | u16 free_cluster = CLUSTER_UNDEFINED; // next free cluster number. |
794 | u16 free_cluster = CLUSTER_UNDEFINED; // next free cluster number. |
795 | Fat16Entry_t * fat; |
795 | Fat16Entry_t * fat; |
796 | 796 | ||
797 | if((!Partition.IsValid) || (file == NULL)) return(0); |
797 | if((!Partition.IsValid) || (file == NULL)) return(0); |
798 | 798 | ||
799 | // start searching for an empty cluster at the beginning of the fat. |
799 | // start searching for an empty cluster at the beginning of the fat. |
800 | fat_sector = 0; |
800 | fat_sector = 0; |
801 | do |
801 | do |
802 | { |
802 | { |
803 | curr_sector = Partition.FirstFatSector + fat_sector; // calculate sector to read |
803 | curr_sector = Partition.FirstFatSector + fat_sector; // calculate sector to read |
804 | file->SectorInCache = curr_sector; // upate the sector number of file cache. |
804 | file->SectorInCache = curr_sector; // upate the sector number of file cache. |
805 | if( SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache)) // read sector of fat from sd-card. |
805 | if( SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache)) // read sector of fat from sd-card. |
806 | { |
806 | { |
807 | Fat16_Deinit(); |
807 | Fat16_Deinit(); |
808 | return(CLUSTER_UNDEFINED); |
808 | return(CLUSTER_UNDEFINED); |
809 | } |
809 | } |
810 | 810 | ||
811 | fat = (Fat16Entry_t *)file->Cache; // set fat pointer to file cache |
811 | fat = (Fat16Entry_t *)file->Cache; // set fat pointer to file cache |
812 | 812 | ||
813 | for(fat_entry = 0; fat_entry < FAT16_ENTRIES_PER_SECTOR; fat_entry++) // look for an free cluster at all entries in this sector of the fat. |
813 | for(fat_entry = 0; fat_entry < FAT16_ENTRIES_PER_SECTOR; fat_entry++) // look for an free cluster at all entries in this sector of the fat. |
814 | { |
814 | { |
815 | if(fat[fat_entry].NextCluster == FAT16_CLUSTER_FREE) // empty cluster found!! |
815 | if(fat[fat_entry].NextCluster == FAT16_CLUSTER_FREE) // empty cluster found!! |
816 | { |
816 | { |
817 | fat[fat_entry].NextCluster = FAT16_CLUSTER_LAST_MAX; // mark this fat-entry as used |
817 | fat[fat_entry].NextCluster = FAT16_CLUSTER_LAST_MAX; // mark this fat-entry as used |
818 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache)) // and save the sector at the sd-card. |
818 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache)) // and save the sector at the sd-card. |
819 | { |
819 | { |
820 | Fat16_Deinit(); |
820 | Fat16_Deinit(); |
821 | return(CLUSTER_UNDEFINED); |
821 | return(CLUSTER_UNDEFINED); |
822 | } |
822 | } |
823 | free_cluster = (u16)(fat_sector * FAT16_ENTRIES_PER_SECTOR + (u32)fat_entry); |
823 | free_cluster = (u16)(fat_sector * FAT16_ENTRIES_PER_SECTOR + (u32)fat_entry); |
824 | fat_entry = FAT16_ENTRIES_PER_SECTOR; // terminate the search for a free cluster in this sector. |
824 | fat_entry = FAT16_ENTRIES_PER_SECTOR; // terminate the search for a free cluster in this sector. |
825 | } |
825 | } |
826 | } |
826 | } |
827 | fat_sector++; // continue the search in next fat sector |
827 | fat_sector++; // continue the search in next fat sector |
828 | // repeat until the end of the fat is reached and no free cluster has been found so far |
828 | // repeat until the end of the fat is reached and no free cluster has been found so far |
829 | }while((fat_sector < Partition.SectorsPerFat) && (!free_cluster)); |
829 | }while((fat_sector < Partition.SectorsPerFat) && (!free_cluster)); |
830 | return(free_cluster); |
830 | return(free_cluster); |
831 | } |
831 | } |
832 | 832 | ||
833 | 833 | ||
834 | /****************************************************************************************************************************************************/ |
834 | /****************************************************************************************************************************************************/ |
835 | /* Function: s16 fseek_(File_t *, s32 *, u8) */ |
835 | /* Function: s16 fseek_(File_t *, s32 *, u8) */ |
836 | /* */ |
836 | /* */ |
837 | /* Description: This function sets the pointer of the stream relative to the position */ |
837 | /* Description: This function sets the pointer of the stream relative to the position */ |
838 | /* specified by origin (SEEK_SET, SEEK_CUR, SEEK_END) */ |
838 | /* specified by origin (SEEK_SET, SEEK_CUR, SEEK_END) */ |
839 | /* Returnvalue: Is 0 if seek was successful */ |
839 | /* Returnvalue: Is 0 if seek was successful */ |
840 | /****************************************************************************************************************************************************/ |
840 | /****************************************************************************************************************************************************/ |
841 | s16 fseek_(File_t *file, s32 offset, s16 origin) |
841 | s16 fseek_(File_t *file, s32 offset, s16 origin) |
842 | { |
842 | { |
843 | s32 fposition = 0; |
843 | s32 fposition = 0; |
844 | s16 retvalue = 1; |
844 | s16 retvalue = 1; |
- | 845 | ||
- | 846 | // the byteindex within a sector |
|
- | 847 | u32 byte_index = 0; |
|
- | 848 | // the sectorindex within a cluster |
|
- | 849 | u32 sector_index = 0; |
|
- | 850 | // the index of the cluster within the clusterchain inside the fat |
|
- | 851 | u32 cluster_index = 0; |
|
- | 852 | ||
845 | 853 | // check if the partition is valid |
|
846 | if((!Partition.IsValid) || (file == NULL)) return(retvalue); |
854 | if((!Partition.IsValid) || (file == NULL)) return(retvalue); |
- | 855 | //...................................................... |
|
847 | switch(origin) |
856 | if(origin == SEEK_SET) // Fileposition relative to the beginning of the file. |
848 | { |
- | |
849 | case SEEK_SET: // Fileposition relative to the beginning of the file. |
857 | { |
850 | fposition = 0; |
858 | fposition = 0; |
- | 859 | } |
|
851 | break; |
860 | //...................................................... |
- | 861 | else if(origin == SEEK_END) // Fileposition relative to the end of the file. |
|
852 | case SEEK_END: // Fileposition relative to the end of the file. |
862 | { |
853 | fposition = (s32)file->Size; |
863 | fposition = (s32) file->Size; |
- | 864 | } |
|
854 | break; |
865 | //...................................................... |
855 | case SEEK_CUR: // Fileposition relative to the current position of the file. |
866 | else if(origin == SEEK_CUR) // Fileposition relative to the current position of the file. |
856 | default: |
867 | { |
857 | fposition = file->Position; |
- | |
858 | break; |
868 | fposition = (s32)file->Position; |
859 | } |
- | |
860 | - | ||
- | 869 | } |
|
- | 870 | ||
- | 871 | // calculate the specified fileposition according to the selected mode |
|
861 | fposition += offset; |
872 | fposition += offset; // Die Absolute Dateiposition welche durch fseek angesprungen werden soll. |
862 | 873 | // is the fileposition within the file? |
|
863 | if((fposition >= 0) && (fposition <= (s32)file->Size)) // is the pointer still within the file? |
874 | if((fposition >= 0) && (fposition <= (s32)file->Size)) // Ist die berechnete Dateiposition innerhalb der geöffneten Datei? |
864 | { |
875 | { |
865 | // reset file position to start of the file |
876 | // initialize the filepointer |
866 | file->FirstSectorOfCurrCluster = file->FirstSectorOfFirstCluster; |
877 | file->FirstSectorOfCurrCluster = file->FirstSectorOfFirstCluster; |
867 | file->SectorOfCurrCluster = 0; |
878 | file->SectorOfCurrCluster = 0; |
868 | file->ByteOfCurrSector = 0; |
879 | file->ByteOfCurrSector = 0; |
869 | file->Position = 0; |
880 | file->Position = 0; |
- | 881 | // has the specified file at least one valid sector attached? |
|
870 | if(file->FirstSectorOfCurrCluster == SECTOR_UNDEFINED) return(retvalue); |
882 | if(file->FirstSectorOfCurrCluster == SECTOR_UNDEFINED) return(retvalue); |
871 | while(file->Position < fposition) // repeat until the current position is less than target |
883 | // calculate the absolute number of the sector wich contains the fileposition we are looking for |
872 | { |
- | |
873 | file->Position++; // increment file position |
884 | sector_index = (u32) ((u32)fposition >> 9); |
874 | file->ByteOfCurrSector++; // next byte in current sector |
885 | // calculate the index of the cluster containing the specified sector |
875 | if(file->ByteOfCurrSector >= BYTES_PER_SECTOR) |
886 | cluster_index = (u32) sector_index / Partition.SectorsPerCluster; |
876 | { |
- | |
877 | file->ByteOfCurrSector = 0; // reading at the beginning of new sector. |
887 | // the absolute sectornumber becomes relative to the beginning of the specified cluster |
878 | file->SectorOfCurrCluster++; // continue reading in next sector |
888 | sector_index = (sector_index % Partition.SectorsPerCluster); |
879 | if(file->SectorOfCurrCluster >= Partition.SectorsPerCluster) // if end of cluster is reached, the next datacluster has to be searched in the FAT. |
889 | // calculate the index of the byteposition the fileposition points to |
880 | { |
890 | byte_index = (u32) fposition % 512; |
881 | if(GetNextCluster(file)) // Sets the clusterpointer of the file to the next datacluster. |
891 | // parse the fat till the calculated cluster has been reached |
882 | { |
- | |
883 | file->SectorOfCurrCluster = 0; |
892 | while(cluster_index--) GetNextCluster(file); |
884 | } |
- | |
885 | else // the last cluster was allready reached |
893 | // set the filepointer to the specified sector and byteposition |
886 | { |
- | |
887 | file->SectorOfCurrCluster--; // jump back to the last sector in the last cluster |
894 | file->SectorOfCurrCluster = (u8) sector_index; |
888 | file->ByteOfCurrSector = BYTES_PER_SECTOR; // set ByteOfCurrSector one byte over sector end |
895 | file->ByteOfCurrSector = (u16) byte_index; |
- | 896 | // the fileposition now equals the filepointer |
|
889 | } |
897 | file->Position = (u32)fposition; |
- | 898 | // the specified fileposition has been reached |
|
890 | } |
899 | retvalue = 0; |
891 | } |
- | |
892 | } |
- | |
893 | } |
900 | } |
894 | if(file->Position == fposition) retvalue = 0; |
- | |
895 | return(retvalue); |
901 | return(retvalue); |
896 | } |
902 | } |
897 | 903 | ||
898 | 904 | ||
899 | /****************************************************************************************************************************************/ |
905 | /****************************************************************************************************************************************/ |
900 | /* Function: u16 DeleteClusterChain(File *file); */ |
906 | /* Function: u16 DeleteClusterChain(File *file); */ |
901 | /* */ |
907 | /* */ |
902 | /* Description: This function trances along a cluster chain in the fat and frees all clusters visited. */ |
908 | /* Description: This function trances along a cluster chain in the fat and frees all clusters visited. */ |
903 | /* */ |
909 | /* */ |
904 | /****************************************************************************************************************************************/ |
910 | /****************************************************************************************************************************************/ |
905 | u8 DeleteClusterChain(u16 StartCluster) |
911 | u8 DeleteClusterChain(u16 StartCluster) |
906 | { |
912 | { |
907 | u16 cluster; |
913 | u16 cluster; |
908 | u32 fat_byte_offset, sector, byte; |
914 | u32 fat_byte_offset, sector, byte; |
909 | Fat16Entry_t * fat; |
915 | Fat16Entry_t * fat; |
910 | u8 buffer[BYTES_PER_SECTOR]; |
916 | u8 buffer[BYTES_PER_SECTOR]; |
911 | u32 sector_in_buffer = 0; |
917 | u32 sector_in_buffer = 0; |
912 | u8 repeat = 0; |
918 | u8 repeat = 0; |
913 | 919 | ||
914 | if(!Partition.IsValid) return(0); |
920 | if(!Partition.IsValid) return(0); |
915 | if(StartCluster == CLUSTER_UNDEFINED) return(0); |
921 | if(StartCluster == CLUSTER_UNDEFINED) return(0); |
916 | cluster = StartCluster; // init chain trace |
922 | cluster = StartCluster; // init chain trace |
917 | // if start cluster is no real cluster |
923 | // if start cluster is no real cluster |
918 | if(FAT16_CLUSTER_LAST_MIN <= cluster) return 1; |
924 | if(FAT16_CLUSTER_LAST_MIN <= cluster) return 1; |
919 | 925 | ||
920 | // calculate byte offset in the fat for corresponding entry |
926 | // calculate byte offset in the fat for corresponding entry |
921 | fat_byte_offset = ((u32)cluster)<<1; // two FAT bytes (16 bits) for every cluster |
927 | fat_byte_offset = ((u32)cluster)<<1; // two FAT bytes (16 bits) for every cluster |
922 | // calculate the sector that contains the current cluster within the fat |
928 | // calculate the sector that contains the current cluster within the fat |
923 | sector = Partition.FirstFatSector + ( fat_byte_offset / BYTES_PER_SECTOR); |
929 | sector = Partition.FirstFatSector + ( fat_byte_offset / BYTES_PER_SECTOR); |
924 | // calculate byte offset of the current cluster within that fat sector |
930 | // calculate byte offset of the current cluster within that fat sector |
925 | byte = fat_byte_offset % BYTES_PER_SECTOR; |
931 | byte = fat_byte_offset % BYTES_PER_SECTOR; |
926 | do |
932 | do |
927 | { |
933 | { |
928 | if(sector != sector_in_buffer) |
934 | if(sector != sector_in_buffer) |
929 | { |
935 | { |
930 | // read this sector to buffer |
936 | // read this sector to buffer |
931 | sector_in_buffer = sector; |
937 | sector_in_buffer = sector; |
932 | if(SD_SUCCESS != SDC_GetSector(sector_in_buffer, buffer)) return 0; // read sector from sd-card |
938 | if(SD_SUCCESS != SDC_GetSector(sector_in_buffer, buffer)) return 0; // read sector from sd-card |
933 | } |
939 | } |
934 | // read the next cluster from cache |
940 | // read the next cluster from cache |
935 | fat = (Fat16Entry_t *)(&(buffer[byte])); |
941 | fat = (Fat16Entry_t *)(&(buffer[byte])); |
936 | cluster = fat->NextCluster; |
942 | cluster = fat->NextCluster; |
937 | fat->NextCluster = FAT16_CLUSTER_FREE; // mark current cluster as free |
943 | fat->NextCluster = FAT16_CLUSTER_FREE; // mark current cluster as free |
938 | 944 | ||
939 | if((FAT16_CLUSTER_USED_MIN <= cluster) && (cluster <= FAT16_CLUSTER_USED_MAX) ) |
945 | if((FAT16_CLUSTER_USED_MIN <= cluster) && (cluster <= FAT16_CLUSTER_USED_MAX) ) |
940 | { |
946 | { |
941 | repeat = 1; |
947 | repeat = 1; |
942 | // calculate sector byte and byte offset in the fat for the next cluster |
948 | // calculate sector byte and byte offset in the fat for the next cluster |
943 | fat_byte_offset = ((u32)cluster)<<1; // two FAT bytes (16 bits) for every cluster |
949 | fat_byte_offset = ((u32)cluster)<<1; // two FAT bytes (16 bits) for every cluster |
944 | // calculate the sector that contains the current cluster within the fat |
950 | // calculate the sector that contains the current cluster within the fat |
945 | sector = Partition.FirstFatSector + ( fat_byte_offset / BYTES_PER_SECTOR); |
951 | sector = Partition.FirstFatSector + ( fat_byte_offset / BYTES_PER_SECTOR); |
946 | // calculate byte offset of the current cluster within that fat sector |
952 | // calculate byte offset of the current cluster within that fat sector |
947 | byte = fat_byte_offset % BYTES_PER_SECTOR; |
953 | byte = fat_byte_offset % BYTES_PER_SECTOR; |
948 | } |
954 | } |
949 | else repeat = 0; |
955 | else repeat = 0; |
950 | 956 | ||
951 | // if new sector is not the sector in buffer or the last cluster in the chain was traced |
957 | // if new sector is not the sector in buffer or the last cluster in the chain was traced |
952 | if((sector != sector_in_buffer) || !repeat) |
958 | if((sector != sector_in_buffer) || !repeat) |
953 | { // write sector in buffer |
959 | { // write sector in buffer |
954 | if(SD_SUCCESS != SDC_PutSector(sector_in_buffer,buffer)) |
960 | if(SD_SUCCESS != SDC_PutSector(sector_in_buffer,buffer)) |
955 | { |
961 | { |
956 | Fat16_Deinit(); |
962 | Fat16_Deinit(); |
957 | return(0); |
963 | return(0); |
958 | } |
964 | } |
959 | } |
965 | } |
960 | } |
966 | } |
961 | while(repeat); |
967 | while(repeat); |
962 | 968 | ||
963 | return 1; |
969 | return 1; |
964 | } |
970 | } |
965 | 971 | ||
966 | 972 | ||
967 | /****************************************************************************************************************************************/ |
973 | /****************************************************************************************************************************************/ |
968 | /* Function: u16 AppendCluster(File *file); */ |
974 | /* Function: u16 AppendCluster(File *file); */ |
969 | /* */ |
975 | /* */ |
970 | /* Description: This function looks in the fat to find the next free cluster and appends it to the file. */ |
976 | /* Description: This function looks in the fat to find the next free cluster and appends it to the file. */ |
971 | /* */ |
977 | /* */ |
972 | /* Returnvalue: The function returns the appened cluster number or CLUSTER_UNDEFINED of no cluster was appended. */ |
978 | /* Returnvalue: The function returns the appened cluster number or CLUSTER_UNDEFINED of no cluster was appended. */ |
973 | /****************************************************************************************************************************************/ |
979 | /****************************************************************************************************************************************/ |
- | 980 | ||
974 | u16 AppendCluster(File_t *file) |
981 | u16 AppendCluster(File_t *file) |
975 | { |
982 | { |
976 | u16 last_cluster, new_cluster = CLUSTER_UNDEFINED; |
983 | u16 last_cluster, new_cluster = CLUSTER_UNDEFINED; |
977 | u32 fat_byte_offset, sector, byte; |
984 | u32 fat_byte_offset, sector, byte; |
978 | Fat16Entry_t * fat; |
985 | Fat16Entry_t * fat; |
- | 986 | // s8 text[64]; |
|
979 | 987 | ||
980 | if((!Partition.IsValid) || (file == NULL)) return(new_cluster); |
988 | if((!Partition.IsValid) || (file == NULL)) return(new_cluster); |
981 | 989 | ||
982 | new_cluster = FindNextFreeCluster(file); // the next free cluster found on the disk. |
990 | new_cluster = FindNextFreeCluster(file); // the next free cluster found on the disk. |
983 | if(new_cluster != CLUSTER_UNDEFINED) |
991 | if(new_cluster != CLUSTER_UNDEFINED) |
984 | { // A free cluster was found and can be added to the end of the file. |
992 | { // A free cluster was found and can be added to the end of the file. |
- | 993 | // is there at least one cluster appended to the file? |
|
- | 994 | if(file->FirstSectorOfLastCluster == CLUSTER_UNDEFINED) |
|
- | 995 | { |
|
985 | fseek_(file, 0, SEEK_END); // jump to the end of the file |
996 | fseek_(file, 0, SEEK_END); // jump to the end of the file |
- | 997 | // remember the first sector of the last cluster |
|
- | 998 | file->FirstSectorOfLastCluster = file->FirstSectorOfCurrCluster; |
|
986 | last_cluster = SectorToFat16Cluster(file->FirstSectorOfCurrCluster); // determine current file cluster |
999 | last_cluster = SectorToFat16Cluster(file->FirstSectorOfCurrCluster); // determine current file cluster |
- | 1000 | } |
|
- | 1001 | else |
|
- | 1002 | { |
|
- | 1003 | last_cluster = SectorToFat16Cluster(file->FirstSectorOfLastCluster); // determine current file cluster |
|
- | 1004 | } |
|
- | 1005 | ||
987 | if(last_cluster != CLUSTER_UNDEFINED) |
1006 | if(last_cluster != CLUSTER_UNDEFINED) |
988 | { |
1007 | { |
989 | // update FAT entry of last cluster |
1008 | // update FAT entry of last cluster |
990 | fat_byte_offset = ((u32)last_cluster)<<1; |
1009 | fat_byte_offset = ((u32)last_cluster)<<1; |
991 | sector = Partition.FirstFatSector + ( fat_byte_offset / BYTES_PER_SECTOR); |
1010 | sector = Partition.FirstFatSector + ( fat_byte_offset / BYTES_PER_SECTOR); |
992 | byte = fat_byte_offset % BYTES_PER_SECTOR; |
1011 | byte = fat_byte_offset % BYTES_PER_SECTOR; |
993 | // read the sector containing the last cluster of the file |
1012 | // read the sector containing the last cluster of the file |
994 | if(file->SectorInCache != sector) |
1013 | if(file->SectorInCache != sector) |
995 | { |
1014 | { |
996 | file->SectorInCache = sector; // update sector stored in buffer |
1015 | file->SectorInCache = sector; // update sector stored in buffer |
997 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache)) // read sector from sd-card |
1016 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache)) // read sector from sd-card |
998 | { |
1017 | { |
999 | Fat16_Deinit(); |
1018 | Fat16_Deinit(); |
1000 | return(0); |
1019 | return(0); |
1001 | } |
1020 | } |
1002 | } |
1021 | } |
1003 | fat = (Fat16Entry_t *)(&(file->Cache[byte])); |
1022 | fat = (Fat16Entry_t *)(&(file->Cache[byte])); |
1004 | fat->NextCluster = new_cluster; // append the free cluster to the end of the file in the FAT. |
1023 | fat->NextCluster = new_cluster; // append the free cluster to the end of the file in the FAT. |
1005 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache)) // save the modified sector to the FAT. |
1024 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache)) // save the modified sector to the FAT. |
1006 | { |
1025 | { |
1007 | Fat16_Deinit(); |
1026 | Fat16_Deinit(); |
1008 | return(0); |
1027 | return(0); |
1009 | } |
1028 | } |
- | 1029 | // now the new cluster appended to the fat is the last cluster |
|
- | 1030 | file->FirstSectorOfLastCluster = Fat16ClusterToSector(new_cluster); |
|
1010 | } |
1031 | } |
1011 | else // last cluster of the file is undefined |
1032 | else // last cluster of the file is undefined |
1012 | { // then the new cluster must be the first one of the file |
1033 | { // then the new cluster must be the first one of the file |
1013 | // and its cluster number must be set in the direntry |
1034 | // and its cluster number must be set in the direntry |
1014 | DirEntry_t * dir; |
1035 | DirEntry_t * dir; |
1015 | file->SectorInCache = file->DirectorySector; // update the sector number of file cache. |
1036 | file->SectorInCache = file->DirectorySector; // update the sector number of file cache. |
1016 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read in the sector. |
1037 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read in the sector. |
1017 | { |
1038 | { |
1018 | Fat16_Deinit(); |
1039 | Fat16_Deinit(); |
1019 | return(CLUSTER_UNDEFINED); |
1040 | return(CLUSTER_UNDEFINED); |
1020 | } |
1041 | } |
1021 | dir = (DirEntry_t *)file->Cache; // set pointer to directory |
1042 | dir = (DirEntry_t *)file->Cache; // set pointer to directory |
1022 | dir[file->DirectoryIndex].Res1 = 0; |
1043 | dir[file->DirectoryIndex].Res1 = 0; |
1023 | dir[file->DirectoryIndex].Res2[0] = 0; |
1044 | dir[file->DirectoryIndex].Res2[0] = 0; |
1024 | dir[file->DirectoryIndex].Res2[1] = 0; |
1045 | dir[file->DirectoryIndex].Res2[1] = 0; |
1025 | dir[file->DirectoryIndex].StartCluster = new_cluster; // update startcluster |
1046 | dir[file->DirectoryIndex].StartCluster = new_cluster; // update startcluster |
1026 | dir[file->DirectoryIndex].ModTime = FileTime(&SystemTime);// set time |
1047 | dir[file->DirectoryIndex].ModTime = FileTime(&SystemTime);// set time |
1027 | dir[file->DirectoryIndex].ModDate = FileDate(&SystemTime);// and date of modification |
1048 | dir[file->DirectoryIndex].ModDate = FileDate(&SystemTime);// and date of modification |
1028 | dir[file->DirectoryIndex].LastAccessDate = dir[file->DirectoryIndex].ModDate; |
1049 | dir[file->DirectoryIndex].LastAccessDate = dir[file->DirectoryIndex].ModDate; |
1029 | dir[file->DirectoryIndex].Size = 0; |
1050 | dir[file->DirectoryIndex].Size = 0; |
1030 | // write sector containing the direntry |
1051 | // write sector containing the direntry |
1031 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache)) |
1052 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache)) |
1032 | { |
1053 | { |
1033 | Fat16_Deinit(); |
1054 | Fat16_Deinit(); |
1034 | return(CLUSTER_UNDEFINED); |
1055 | return(CLUSTER_UNDEFINED); |
1035 | } |
1056 | } |
1036 | // update file info |
1057 | // update file info |
1037 | file->FirstSectorOfFirstCluster = Fat16ClusterToSector(new_cluster); |
1058 | file->FirstSectorOfFirstCluster = Fat16ClusterToSector(new_cluster); |
- | 1059 | file->FirstSectorOfLastCluster = file->FirstSectorOfFirstCluster; |
|
1038 | file->Size = 0; |
1060 | file->Size = 0; |
1039 | file->Position = 0; |
1061 | file->Position = 0; |
1040 | } |
1062 | } |
1041 | // update file pointes |
1063 | // update file pointes |
1042 | file->FirstSectorOfCurrCluster = Fat16ClusterToSector(new_cluster); |
1064 | file->FirstSectorOfCurrCluster = Fat16ClusterToSector(new_cluster); |
1043 | file->SectorOfCurrCluster = 0; |
1065 | file->SectorOfCurrCluster = 0; |
1044 | file->ByteOfCurrSector = 0; |
1066 | file->ByteOfCurrSector = 0; |
1045 | } |
1067 | } |
1046 | return(new_cluster); |
1068 | return(new_cluster); |
1047 | } |
1069 | } |
- | 1070 | ||
1048 | 1071 | ||
1049 | /****************************************************************************************************************************************************/ |
1072 | /****************************************************************************************************************************************************/ |
1050 | /* Function: DirectoryEntryExist(s8 *, u8, u8, File_t *) */ |
1073 | /* Function: DirectoryEntryExist(s8 *, u8, u8, File_t *) */ |
1051 | /* */ |
1074 | /* */ |
1052 | /* Description: This function searches all possible dir entries until the file or directory is found or the end of the directory is reached */ |
1075 | /* Description: This function searches all possible dir entries until the file or directory is found or the end of the directory is reached */ |
1053 | /* */ |
1076 | /* */ |
1054 | /* Returnvalue: This function returns 1 if the directory entry specified was found. */ |
1077 | /* Returnvalue: This function returns 1 if the directory entry specified was found. */ |
1055 | /****************************************************************************************************************************************************/ |
1078 | /****************************************************************************************************************************************************/ |
1056 | u8 DirectoryEntryExist(s8 *dirname, u8 attribfilter, u8 attribmask, File_t *file) |
1079 | u8 DirectoryEntryExist(s8 *dirname, u8 attribfilter, u8 attribmask, File_t *file) |
1057 | { |
1080 | { |
1058 | u32 dir_sector, max_dir_sector, curr_sector; |
1081 | u32 dir_sector, max_dir_sector, curr_sector; |
1059 | u16 dir_entry = 0; |
1082 | u16 dir_entry = 0; |
1060 | 1083 | ||
1061 | u16 end_of_directory_not_reached = 0; |
1084 | u16 end_of_directory_not_reached = 0; |
1062 | u8 i = 0; |
1085 | u8 i = 0; |
1063 | u8 direntry_exist = 0; |
1086 | u8 direntry_exist = 0; |
1064 | DirEntry_t * dir; |
1087 | DirEntry_t * dir; |
1065 | 1088 | ||
1066 | // if incomming pointers are useless return immediatly |
1089 | // if incomming pointers are useless return immediatly |
1067 | if((!Partition.IsValid) || (file == NULL) || (dirname == NULL)) return(direntry_exist); |
1090 | if((!Partition.IsValid) || (file == NULL) || (dirname == NULL)) return(direntry_exist); |
1068 | 1091 | ||
1069 | // dir entries can be searched only in filesclusters that have |
1092 | // dir entries can be searched only in filesclusters that have |
1070 | // a corresponding dir entry with adir-flag set in its attribute |
1093 | // a corresponding dir entry with adir-flag set in its attribute |
1071 | // or direct within the root directory area |
1094 | // or direct within the root directory area |
1072 | file->FirstSectorOfFirstCluster = SECTOR_UNDEFINED; |
1095 | file->FirstSectorOfFirstCluster = SECTOR_UNDEFINED; |
1073 | // no current directory exist therefore assume searching in the root |
1096 | // no current directory exist therefore assume searching in the root |
1074 | if(file->DirectorySector == SECTOR_UNDEFINED) |
1097 | if(file->DirectorySector == SECTOR_UNDEFINED) |
1075 | { |
1098 | { |
1076 | max_dir_sector = (Partition.MaxRootEntries * DIRENTRY_SIZE)/BYTES_PER_SECTOR; |
1099 | max_dir_sector = (Partition.MaxRootEntries * DIRENTRY_SIZE)/BYTES_PER_SECTOR; |
1077 | file->FirstSectorOfFirstCluster = Partition.FirstRootDirSector; |
1100 | file->FirstSectorOfFirstCluster = Partition.FirstRootDirSector; |
1078 | } |
1101 | } |
1079 | // within the root directory area we can read sectors sequentially until the end of this area |
1102 | // within the root directory area we can read sectors sequentially until the end of this area |
1080 | else if((Partition.FirstRootDirSector <= file->DirectorySector) && (file->DirectorySector < Partition.FirstDataSector)) |
1103 | else if((Partition.FirstRootDirSector <= file->DirectorySector) && (file->DirectorySector < Partition.FirstDataSector)) |
1081 | { |
1104 | { |
1082 | max_dir_sector = (Partition.MaxRootEntries * DIRENTRY_SIZE)/BYTES_PER_SECTOR; |
1105 | max_dir_sector = (Partition.MaxRootEntries * DIRENTRY_SIZE)/BYTES_PER_SECTOR; |
1083 | } |
1106 | } |
1084 | // within the data clusters we can read sectors sequentially only within the cluster |
1107 | // within the data clusters we can read sectors sequentially only within the cluster |
1085 | else if((Partition.FirstDataSector <= file->DirectorySector) && (file->DirectorySector <= Partition.LastDataSector)) |
1108 | else if((Partition.FirstDataSector <= file->DirectorySector) && (file->DirectorySector <= Partition.LastDataSector)) |
1086 | { |
1109 | { |
1087 | max_dir_sector = Partition.SectorsPerCluster; // limit max secters before next cluster |
1110 | max_dir_sector = Partition.SectorsPerCluster; // limit max secters before next cluster |
1088 | } |
1111 | } |
1089 | else return (direntry_exist); // bad sector range for directory sector of the file |
1112 | else return (direntry_exist); // bad sector range for directory sector of the file |
1090 | // if search area is not defined yet |
1113 | // if search area is not defined yet |
1091 | if(file->FirstSectorOfFirstCluster == SECTOR_UNDEFINED) |
1114 | if(file->FirstSectorOfFirstCluster == SECTOR_UNDEFINED) |
1092 | { |
1115 | { |
1093 | // check if the directory entry of current file is existent and has the dir-flag set |
1116 | // check if the directory entry of current file is existent and has the dir-flag set |
1094 | file->SectorInCache = file->DirectorySector; // update the sector number of file cache. |
1117 | file->SectorInCache = file->DirectorySector; // update the sector number of file cache. |
1095 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read in the sector. |
1118 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read in the sector. |
1096 | { |
1119 | { |
1097 | Fat16_Deinit(); |
1120 | Fat16_Deinit(); |
1098 | return(0); |
1121 | return(0); |
1099 | } |
1122 | } |
1100 | dir = (DirEntry_t *)file->Cache; // set pointer to directory |
1123 | dir = (DirEntry_t *)file->Cache; // set pointer to directory |
1101 | switch((u8)dir[file->DirectoryIndex].Name[0]) // check if current directory exist |
1124 | switch((u8)dir[file->DirectoryIndex].Name[0]) // check if current directory exist |
1102 | { |
1125 | { |
1103 | case SLOT_EMPTY: |
1126 | case SLOT_EMPTY: |
1104 | case SLOT_DELETED: |
1127 | case SLOT_DELETED: |
1105 | // the directrory pointer of this file points to a deleted or not existen directory |
1128 | // the directrory pointer of this file points to a deleted or not existen directory |
1106 | // therefore no file or subdirectory can be created |
1129 | // therefore no file or subdirectory can be created |
1107 | return (direntry_exist); |
1130 | return (direntry_exist); |
1108 | break; |
1131 | break; |
1109 | default: // and is a real directory |
1132 | default: // and is a real directory |
1110 | if((dir[file->DirectoryIndex].Attribute & ATTR_SUBDIRECTORY) != ATTR_SUBDIRECTORY) |
1133 | if((dir[file->DirectoryIndex].Attribute & ATTR_SUBDIRECTORY) != ATTR_SUBDIRECTORY) |
1111 | { // current file is not a directory therefore no file or subdirectory can be created here |
1134 | { // current file is not a directory therefore no file or subdirectory can be created here |
1112 | return (direntry_exist); |
1135 | return (direntry_exist); |
1113 | } |
1136 | } |
1114 | break; |
1137 | break; |
1115 | } |
1138 | } |
1116 | file->FirstSectorOfFirstCluster = Fat16ClusterToSector(dir[file->DirectoryIndex].StartCluster); |
1139 | file->FirstSectorOfFirstCluster = Fat16ClusterToSector(dir[file->DirectoryIndex].StartCluster); |
1117 | } |
1140 | } |
1118 | 1141 | ||
1119 | // update current file data area position to start of first cluster |
1142 | // update current file data area position to start of first cluster |
1120 | file->FirstSectorOfCurrCluster = file->FirstSectorOfFirstCluster; |
1143 | file->FirstSectorOfCurrCluster = file->FirstSectorOfFirstCluster; |
1121 | file->SectorOfCurrCluster = 0; |
1144 | file->SectorOfCurrCluster = 0; |
1122 | file->ByteOfCurrSector = 0; |
1145 | file->ByteOfCurrSector = 0; |
1123 | 1146 | ||
1124 | do // loop over all data clusters of the current directory entry |
1147 | do // loop over all data clusters of the current directory entry |
1125 | { |
1148 | { |
1126 | dir_sector = 0; |
1149 | dir_sector = 0; |
1127 | do // loop over all sectors of a cluster or all sectors of the root directory |
1150 | do // loop over all sectors of a cluster or all sectors of the root directory |
1128 | { |
1151 | { |
1129 | curr_sector = file->FirstSectorOfCurrCluster + dir_sector; // calculate sector number |
1152 | curr_sector = file->FirstSectorOfCurrCluster + dir_sector; // calculate sector number |
1130 | file->SectorInCache = curr_sector; // upate the sector number of file cache. |
1153 | file->SectorInCache = curr_sector; // upate the sector number of file cache. |
1131 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read the sector |
1154 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read the sector |
1132 | { |
1155 | { |
1133 | Fat16_Deinit(); |
1156 | Fat16_Deinit(); |
1134 | return(0); |
1157 | return(0); |
1135 | } |
1158 | } |
1136 | dir = (DirEntry_t *)file->Cache; // set pointer to directory |
1159 | dir = (DirEntry_t *)file->Cache; // set pointer to directory |
1137 | // search all directory entries within that sector |
1160 | // search all directory entries within that sector |
1138 | for(dir_entry = 0; dir_entry < DIRENTRIES_PER_SECTOR; dir_entry++) |
1161 | for(dir_entry = 0; dir_entry < DIRENTRIES_PER_SECTOR; dir_entry++) |
1139 | { // check for existing dir entry |
1162 | { // check for existing dir entry |
1140 | switch((u8)dir[dir_entry].Name[0]) |
1163 | switch((u8)dir[dir_entry].Name[0]) |
1141 | { |
1164 | { |
1142 | case SLOT_EMPTY: |
1165 | case SLOT_EMPTY: |
1143 | case SLOT_DELETED: |
1166 | case SLOT_DELETED: |
1144 | // ignore empty or deleted dir entries |
1167 | // ignore empty or deleted dir entries |
1145 | break; |
1168 | break; |
1146 | default: |
1169 | default: |
1147 | // if existing check attributes before names are compared will safe performance |
1170 | // if existing check attributes before names are compared will safe performance |
1148 | if ((dir[dir_entry].Attribute & attribmask) != attribfilter) break; // attribute must match |
1171 | if ((dir[dir_entry].Attribute & attribmask) != attribfilter) break; // attribute must match |
1149 | // then compare the name to the giveb dirname (first 11 characters include 8 chars of basename and 3 chars extension.) |
1172 | // then compare the name to the giveb dirname (first 11 characters include 8 chars of basename and 3 chars extension.) |
1150 | i = 0; |
1173 | i = 0; |
1151 | while((i < 11) && (dir[dir_entry].Name[i] == dirname[i])) i++; |
1174 | while((i < 11) && (dir[dir_entry].Name[i] == dirname[i])) i++; |
1152 | if (i < 10) break; // names does not match |
1175 | if (i < 10) break; // names does not match |
1153 | // if dirname and attribute have matched |
1176 | // if dirname and attribute have matched |
1154 | file->Attribute = dir[dir_entry].Attribute; // store attribute of found dir entry |
1177 | file->Attribute = dir[dir_entry].Attribute; // store attribute of found dir entry |
1155 | file->FirstSectorOfFirstCluster = Fat16ClusterToSector(dir[dir_entry].StartCluster); // set sector of first data cluster |
1178 | file->FirstSectorOfFirstCluster = Fat16ClusterToSector(dir[dir_entry].StartCluster); // set sector of first data cluster |
1156 | file->FirstSectorOfCurrCluster = file->FirstSectorOfFirstCluster; |
1179 | file->FirstSectorOfCurrCluster = file->FirstSectorOfFirstCluster; |
1157 | file->SectorOfCurrCluster = 0; |
1180 | file->SectorOfCurrCluster = 0; |
1158 | file->ByteOfCurrSector = 0; |
1181 | file->ByteOfCurrSector = 0; |
1159 | file->DirectorySector = curr_sector; // current sector |
1182 | file->DirectorySector = curr_sector; // current sector |
1160 | file->DirectoryIndex = dir_entry; // current direntry in current sector |
1183 | file->DirectoryIndex = dir_entry; // current direntry in current sector |
1161 | file->Size = dir[dir_entry].Size; |
1184 | file->Size = dir[dir_entry].Size; |
1162 | direntry_exist = 1; // mark as found |
1185 | direntry_exist = 1; // mark as found |
1163 | dir_entry = DIRENTRIES_PER_SECTOR; // stop for-loop |
1186 | dir_entry = DIRENTRIES_PER_SECTOR; // stop for-loop |
1164 | } // end of first byte of name check |
1187 | } // end of first byte of name check |
1165 | } |
1188 | } |
1166 | dir_sector++; // search next sector |
1189 | dir_sector++; // search next sector |
1167 | // stop if we reached the end of the cluster or the end of the root dir |
1190 | // stop if we reached the end of the cluster or the end of the root dir |
1168 | }while((dir_sector < max_dir_sector) && (!direntry_exist)); |
1191 | }while((dir_sector < max_dir_sector) && (!direntry_exist)); |
1169 | 1192 | ||
1170 | // if we are seaching in the data area and the file not found in this cluster so take next cluster. |
1193 | // if we are seaching in the data area and the file not found in this cluster so take next cluster. |
1171 | if(!direntry_exist && ( Partition.FirstDataSector <= file->FirstSectorOfCurrCluster)) |
1194 | if(!direntry_exist && ( Partition.FirstDataSector <= file->FirstSectorOfCurrCluster)) |
1172 | { |
1195 | { |
1173 | end_of_directory_not_reached = GetNextCluster(file); // updates File->FirstSectorOfCurrCluster |
1196 | end_of_directory_not_reached = GetNextCluster(file); // updates File->FirstSectorOfCurrCluster |
1174 | } |
1197 | } |
1175 | }while((end_of_directory_not_reached) && (!direntry_exist)); // repeat until a next cluster exist an no |
1198 | }while((end_of_directory_not_reached) && (!direntry_exist)); // repeat until a next cluster exist an no |
1176 | return(direntry_exist); |
1199 | return(direntry_exist); |
1177 | } |
1200 | } |
1178 | 1201 | ||
1179 | 1202 | ||
1180 | /****************************************************************************************************************************************/ |
1203 | /****************************************************************************************************************************************/ |
1181 | /* Function: CreateDirectoryEntry(s8 *, u16, File_t *) */ |
1204 | /* Function: CreateDirectoryEntry(s8 *, u16, File_t *) */ |
1182 | /* */ |
1205 | /* */ |
1183 | /* Description: This function looks for the next free position in the directory and creates an entry. */ |
1206 | /* Description: This function looks for the next free position in the directory and creates an entry. */ |
1184 | /* The type of an directory entry is specified by the file attribute. */ |
1207 | /* The type of an directory entry is specified by the file attribute. */ |
1185 | /* */ |
1208 | /* */ |
1186 | /* Returnvalue: Return 0 on error */ |
1209 | /* Returnvalue: Return 0 on error */ |
1187 | /****************************************************************************************************************************************/ |
1210 | /****************************************************************************************************************************************/ |
1188 | u8 CreateDirectoryEntry(s8 *dirname, u8 attrib, File_t *file) |
1211 | u8 CreateDirectoryEntry(s8 *dirname, u8 attrib, File_t *file) |
1189 | { |
1212 | { |
1190 | u32 dir_sector, max_dir_sector, curr_sector; |
1213 | u32 dir_sector, max_dir_sector, curr_sector; |
1191 | u16 dir_entry = 0; |
1214 | u16 dir_entry = 0; |
1192 | u16 subdircluster, dircluster = 0; |
1215 | u16 subdircluster, dircluster = 0; |
1193 | u16 end_of_directory_not_reached = 0; |
1216 | u16 end_of_directory_not_reached = 0; |
1194 | u8 i = 0; |
1217 | u8 i = 0; |
1195 | u8 retvalue = 0; |
1218 | u8 retvalue = 0; |
1196 | DirEntry_t *dir; |
1219 | DirEntry_t *dir; |
1197 | 1220 | ||
1198 | if((!Partition.IsValid) || (file == NULL) || (dirname == NULL)) return (retvalue); |
1221 | if((!Partition.IsValid) || (file == NULL) || (dirname == NULL)) return (retvalue); |
1199 | // It is not checked here that the dir entry that should be created is already existent! |
1222 | // It is not checked here that the dir entry that should be created is already existent! |
1200 | 1223 | ||
1201 | // Dir entries can be created only in file-clusters that have |
1224 | // Dir entries can be created only in file-clusters that have |
1202 | // the dir-flag set in its attribute or within the root directory |
1225 | // the dir-flag set in its attribute or within the root directory |
1203 | 1226 | ||
1204 | file->FirstSectorOfFirstCluster = SECTOR_UNDEFINED; |
1227 | file->FirstSectorOfFirstCluster = SECTOR_UNDEFINED; |
1205 | // no current directory exist therefore assume creating in the root |
1228 | // no current directory exist therefore assume creating in the root |
1206 | if(file->DirectorySector == SECTOR_UNDEFINED) |
1229 | if(file->DirectorySector == SECTOR_UNDEFINED) |
1207 | { |
1230 | { |
1208 | max_dir_sector = (Partition.MaxRootEntries * DIRENTRY_SIZE)/BYTES_PER_SECTOR; |
1231 | max_dir_sector = (Partition.MaxRootEntries * DIRENTRY_SIZE)/BYTES_PER_SECTOR; |
1209 | dircluster = 0; |
1232 | dircluster = 0; |
1210 | file->FirstSectorOfFirstCluster = Partition.FirstRootDirSector; |
1233 | file->FirstSectorOfFirstCluster = Partition.FirstRootDirSector; |
1211 | } |
1234 | } |
1212 | // within the root directory area we can read sectors sequentially until the end of this area |
1235 | // within the root directory area we can read sectors sequentially until the end of this area |
1213 | else if((Partition.FirstRootDirSector <= file->DirectorySector) && (file->DirectorySector < Partition.FirstDataSector)) |
1236 | else if((Partition.FirstRootDirSector <= file->DirectorySector) && (file->DirectorySector < Partition.FirstDataSector)) |
1214 | { |
1237 | { |
1215 | max_dir_sector = (Partition.MaxRootEntries * DIRENTRY_SIZE)/BYTES_PER_SECTOR; |
1238 | max_dir_sector = (Partition.MaxRootEntries * DIRENTRY_SIZE)/BYTES_PER_SECTOR; |
1216 | } |
1239 | } |
1217 | // within the data clusters we can read sectors sequentially only within the cluster |
1240 | // within the data clusters we can read sectors sequentially only within the cluster |
1218 | else if((Partition.FirstDataSector <= file->DirectorySector) && (file->DirectorySector <= Partition.LastDataSector)) |
1241 | else if((Partition.FirstDataSector <= file->DirectorySector) && (file->DirectorySector <= Partition.LastDataSector)) |
1219 | { |
1242 | { |
1220 | max_dir_sector = Partition.SectorsPerCluster; |
1243 | max_dir_sector = Partition.SectorsPerCluster; |
1221 | } |
1244 | } |
1222 | else return (retvalue); // bad sector range for directory sector of the file |
1245 | else return (retvalue); // bad sector range for directory sector of the file |
1223 | // if search area is not defined yet |
1246 | // if search area is not defined yet |
1224 | if(file->FirstSectorOfFirstCluster == SECTOR_UNDEFINED) |
1247 | if(file->FirstSectorOfFirstCluster == SECTOR_UNDEFINED) |
1225 | { |
1248 | { |
1226 | // check if the directory entry of current file is existent and has the dir-flag set |
1249 | // check if the directory entry of current file is existent and has the dir-flag set |
1227 | file->SectorInCache = file->DirectorySector; // update the sector number of file cache. |
1250 | file->SectorInCache = file->DirectorySector; // update the sector number of file cache. |
1228 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read in the sector. |
1251 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read in the sector. |
1229 | { |
1252 | { |
1230 | Fat16_Deinit(); |
1253 | Fat16_Deinit(); |
1231 | return(0); |
1254 | return(0); |
1232 | } |
1255 | } |
1233 | dir = (DirEntry_t *)file->Cache; // set pointer to directory |
1256 | dir = (DirEntry_t *)file->Cache; // set pointer to directory |
1234 | switch((u8)dir[file->DirectoryIndex].Name[0]) // check if current directory exist |
1257 | switch((u8)dir[file->DirectoryIndex].Name[0]) // check if current directory exist |
1235 | { |
1258 | { |
1236 | case SLOT_EMPTY: |
1259 | case SLOT_EMPTY: |
1237 | case SLOT_DELETED: |
1260 | case SLOT_DELETED: |
1238 | return (retvalue); |
1261 | return (retvalue); |
1239 | break; |
1262 | break; |
1240 | default: // and is a real directory |
1263 | default: // and is a real directory |
1241 | if((dir[file->DirectoryIndex].Attribute & ATTR_SUBDIRECTORY) != ATTR_SUBDIRECTORY) |
1264 | if((dir[file->DirectoryIndex].Attribute & ATTR_SUBDIRECTORY) != ATTR_SUBDIRECTORY) |
1242 | { // current file is not a directory therefore no file or subdirectory can be created here |
1265 | { // current file is not a directory therefore no file or subdirectory can be created here |
1243 | return (retvalue); |
1266 | return (retvalue); |
1244 | } |
1267 | } |
1245 | break; |
1268 | break; |
1246 | } |
1269 | } |
1247 | dircluster = dir[file->DirectoryIndex].StartCluster; |
1270 | dircluster = dir[file->DirectoryIndex].StartCluster; |
1248 | file->FirstSectorOfFirstCluster = Fat16ClusterToSector(dircluster); |
1271 | file->FirstSectorOfFirstCluster = Fat16ClusterToSector(dircluster); |
1249 | } |
1272 | } |
1250 | 1273 | ||
1251 | // if the new direntry is a subdirectory |
1274 | // if the new direntry is a subdirectory |
1252 | if((attrib & ATTR_SUBDIRECTORY) == ATTR_SUBDIRECTORY) |
1275 | if((attrib & ATTR_SUBDIRECTORY) == ATTR_SUBDIRECTORY) |
1253 | { // get a free clutser for its content |
1276 | { // get a free clutser for its content |
1254 | subdircluster = FindNextFreeCluster(file); // get the next free cluster on the disk and mark it as used. |
1277 | subdircluster = FindNextFreeCluster(file); // get the next free cluster on the disk and mark it as used. |
1255 | } |
1278 | } |
1256 | else // a normal file |
1279 | else // a normal file |
1257 | { // has no data cluster after creation |
1280 | { // has no data cluster after creation |
1258 | subdircluster = CLUSTER_UNDEFINED; |
1281 | subdircluster = CLUSTER_UNDEFINED; |
1259 | } |
1282 | } |
1260 | 1283 | ||
1261 | file->FirstSectorOfCurrCluster = file->FirstSectorOfFirstCluster; |
1284 | file->FirstSectorOfCurrCluster = file->FirstSectorOfFirstCluster; |
1262 | file->SectorOfCurrCluster = 0; |
1285 | file->SectorOfCurrCluster = 0; |
1263 | do // loop over all clusters of current directory |
1286 | do // loop over all clusters of current directory |
1264 | { |
1287 | { |
1265 | dir_sector = 0; // reset sector counter within a new cluster |
1288 | dir_sector = 0; // reset sector counter within a new cluster |
1266 | do // loop over all sectors of a cluster or all sectors of the root directory |
1289 | do // loop over all sectors of a cluster or all sectors of the root directory |
1267 | { |
1290 | { |
1268 | curr_sector = file->FirstSectorOfCurrCluster + dir_sector; // calculate sector number |
1291 | curr_sector = file->FirstSectorOfCurrCluster + dir_sector; // calculate sector number |
1269 | file->SectorInCache = curr_sector; // upate the sector number of file cache. |
1292 | file->SectorInCache = curr_sector; // upate the sector number of file cache. |
1270 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read in the sector. |
1293 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read in the sector. |
1271 | { |
1294 | { |
1272 | Fat16_Deinit(); |
1295 | Fat16_Deinit(); |
1273 | return(0); |
1296 | return(0); |
1274 | } |
1297 | } |
1275 | 1298 | ||
1276 | dir = (DirEntry_t *)file->Cache; // set pointer to directory |
1299 | dir = (DirEntry_t *)file->Cache; // set pointer to directory |
1277 | // search all directory entries of a sector |
1300 | // search all directory entries of a sector |
1278 | for(dir_entry = 0; dir_entry < DIRENTRIES_PER_SECTOR; dir_entry++) |
1301 | for(dir_entry = 0; dir_entry < DIRENTRIES_PER_SECTOR; dir_entry++) |
1279 | { // check if current direntry is available |
1302 | { // check if current direntry is available |
1280 | if(((u8)dir[dir_entry].Name[0] == SLOT_EMPTY) || ((u8)dir[dir_entry].Name[0] == SLOT_DELETED)) |
1303 | if(((u8)dir[dir_entry].Name[0] == SLOT_EMPTY) || ((u8)dir[dir_entry].Name[0] == SLOT_DELETED)) |
1281 | { // a free direntry was found |
1304 | { // a free direntry was found |
1282 | for(i = 0; i < 11; i++) dir[dir_entry].Name[i] = dirname[i]; // Set dir name |
1305 | for(i = 0; i < 11; i++) dir[dir_entry].Name[i] = dirname[i]; // Set dir name |
1283 | dir[dir_entry].Attribute = attrib; |
1306 | dir[dir_entry].Attribute = attrib; |
1284 | dir[dir_entry].Res1 = 0; |
1307 | dir[dir_entry].Res1 = 0; |
1285 | dir[dir_entry].CreationTime10ms = (u8)(SystemTime.mSec/10); |
1308 | dir[dir_entry].CreationTime10ms = (u8)(SystemTime.mSec/10); |
1286 | dir[dir_entry].CreationTime = FileTime(&SystemTime); |
1309 | dir[dir_entry].CreationTime = FileTime(&SystemTime); |
1287 | dir[dir_entry].CreationDate = FileDate(&SystemTime); |
1310 | dir[dir_entry].CreationDate = FileDate(&SystemTime); |
1288 | dir[dir_entry].LastAccessDate = dir[dir_entry].CreationDate; |
1311 | dir[dir_entry].LastAccessDate = dir[dir_entry].CreationDate; |
1289 | dir[dir_entry].Res2[0] = 0; |
1312 | dir[dir_entry].Res2[0] = 0; |
1290 | dir[dir_entry].Res2[1] = 0; |
1313 | dir[dir_entry].Res2[1] = 0; |
1291 | dir[dir_entry].ModTime = dir[dir_entry].CreationTime; |
1314 | dir[dir_entry].ModTime = dir[dir_entry].CreationTime; |
1292 | dir[dir_entry].ModDate = dir[dir_entry].CreationDate; |
1315 | dir[dir_entry].ModDate = dir[dir_entry].CreationDate; |
1293 | // Set the attribute of the new directoryentry. |
1316 | // Set the attribute of the new directoryentry. |
1294 | dir[dir_entry].StartCluster = subdircluster; // copy the location of the first datacluster to the directoryentry. |
1317 | dir[dir_entry].StartCluster = subdircluster; // copy the location of the first datacluster to the directoryentry. |
1295 | dir[dir_entry].Size = 0; // the new createted file has no content yet. |
1318 | dir[dir_entry].Size = 0; // the new createted file has no content yet. |
1296 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache)) // write back to card |
1319 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache)) // write back to card |
1297 | { |
1320 | { |
1298 | Fat16_Deinit(); |
1321 | Fat16_Deinit(); |
1299 | return(0); |
1322 | return(0); |
1300 | } |
1323 | } |
1301 | file->FirstSectorOfFirstCluster = Fat16ClusterToSector(subdircluster); // Calculate absolute sectorposition of first datacluster. |
1324 | file->FirstSectorOfFirstCluster = Fat16ClusterToSector(subdircluster); // Calculate absolute sectorposition of first datacluster. |
1302 | file->FirstSectorOfCurrCluster = file->FirstSectorOfFirstCluster; // Start reading the file with the first sector of the first datacluster. |
1325 | file->FirstSectorOfCurrCluster = file->FirstSectorOfFirstCluster; // Start reading the file with the first sector of the first datacluster. |
1303 | file->SectorOfCurrCluster = 0; // reset sector of cureen cluster |
1326 | file->SectorOfCurrCluster = 0; // reset sector of cureen cluster |
1304 | file->ByteOfCurrSector = 0; // reset the byte location within the current sector |
1327 | file->ByteOfCurrSector = 0; // reset the byte location within the current sector |
1305 | file->Attribute = attrib; // set file attribute to dir attribute |
1328 | file->Attribute = attrib; // set file attribute to dir attribute |
1306 | file->Size = 0; // new file has no size |
1329 | file->Size = 0; // new file has no size |
1307 | file->DirectorySector = curr_sector; |
1330 | file->DirectorySector = curr_sector; |
1308 | file->DirectoryIndex = dir_entry; |
1331 | file->DirectoryIndex = dir_entry; |
1309 | // prepare subdirectory data cluster |
1332 | // prepare subdirectory data cluster |
1310 | if((attrib & ATTR_SUBDIRECTORY) == ATTR_SUBDIRECTORY) // if a new directory was created then initilize the data area |
1333 | if((attrib & ATTR_SUBDIRECTORY) == ATTR_SUBDIRECTORY) // if a new directory was created then initilize the data area |
1311 | { |
1334 | { |
1312 | ClearCurrCluster(file); // fill cluster with zeros |
1335 | ClearCurrCluster(file); // fill cluster with zeros |
1313 | file->SectorInCache = file->FirstSectorOfFirstCluster; |
1336 | file->SectorInCache = file->FirstSectorOfFirstCluster; |
1314 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read in the sector. |
1337 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read in the sector. |
1315 | { |
1338 | { |
1316 | Fat16_Deinit(); |
1339 | Fat16_Deinit(); |
1317 | return(0); |
1340 | return(0); |
1318 | } |
1341 | } |
1319 | dir = (DirEntry_t *)file->Cache; |
1342 | dir = (DirEntry_t *)file->Cache; |
1320 | // create direntry "." to current dir |
1343 | // create direntry "." to current dir |
1321 | dir[0].Name[0] = 0x2E; |
1344 | dir[0].Name[0] = 0x2E; |
1322 | for(i = 1; i < 11; i++) dir[0].Name[i] = ' '; |
1345 | for(i = 1; i < 11; i++) dir[0].Name[i] = ' '; |
1323 | dir[0].Attribute = ATTR_SUBDIRECTORY; |
1346 | dir[0].Attribute = ATTR_SUBDIRECTORY; |
1324 | dir[0].StartCluster = subdircluster; |
1347 | dir[0].StartCluster = subdircluster; |
1325 | dir[0].Size = 0; |
1348 | dir[0].Size = 0; |
1326 | // create direntry ".." to the upper dir |
1349 | // create direntry ".." to the upper dir |
1327 | dir[1].Name[0] = 0x2E; |
1350 | dir[1].Name[0] = 0x2E; |
1328 | dir[1].Name[1] = 0x2E; |
1351 | dir[1].Name[1] = 0x2E; |
1329 | for(i = 2; i < 11; i++) dir[1].Name[i] = ' '; |
1352 | for(i = 2; i < 11; i++) dir[1].Name[i] = ' '; |
1330 | dir[1].Attribute = ATTR_SUBDIRECTORY; |
1353 | dir[1].Attribute = ATTR_SUBDIRECTORY; |
1331 | dir[1].StartCluster = dircluster; |
1354 | dir[1].StartCluster = dircluster; |
1332 | dir[1].Size = 0; |
1355 | dir[1].Size = 0; |
1333 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache))// read in the sector. |
1356 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache))// read in the sector. |
1334 | { |
1357 | { |
1335 | Fat16_Deinit(); |
1358 | Fat16_Deinit(); |
1336 | return(0); |
1359 | return(0); |
1337 | } |
1360 | } |
1338 | } |
1361 | } |
1339 | retvalue = 1; |
1362 | retvalue = 1; |
1340 | dir_entry = DIRENTRIES_PER_SECTOR; // stop for-loop |
1363 | dir_entry = DIRENTRIES_PER_SECTOR; // stop for-loop |
1341 | } |
1364 | } |
1342 | } |
1365 | } |
1343 | dir_sector++; // search next sector |
1366 | dir_sector++; // search next sector |
1344 | // stop if we reached the end of the cluster or the end of the root dir |
1367 | // stop if we reached the end of the cluster or the end of the root dir |
1345 | }while((dir_sector < max_dir_sector) && (!retvalue)); |
1368 | }while((dir_sector < max_dir_sector) && (!retvalue)); |
1346 | 1369 | ||
1347 | // if we are seaching in the data area and the file not found in this cluster so take next cluster. |
1370 | // if we are seaching in the data area and the file not found in this cluster so take next cluster. |
1348 | if(!retvalue && ( Partition.FirstDataSector <= file->FirstSectorOfCurrCluster)) |
1371 | if(!retvalue && ( Partition.FirstDataSector <= file->FirstSectorOfCurrCluster)) |
1349 | { |
1372 | { |
1350 | end_of_directory_not_reached = GetNextCluster(file); // updates File->FirstSectorOfCurrCluster |
1373 | end_of_directory_not_reached = GetNextCluster(file); // updates File->FirstSectorOfCurrCluster |
1351 | } |
1374 | } |
1352 | }while((end_of_directory_not_reached) && (!retvalue)); |
1375 | }while((end_of_directory_not_reached) && (!retvalue)); |
1353 | // Perhaps we are at the end of the last cluster of a directory file and have no free direntry found. |
1376 | // Perhaps we are at the end of the last cluster of a directory file and have no free direntry found. |
1354 | // Then we would need to add a cluster to that file and create the new direntry there. |
1377 | // Then we would need to add a cluster to that file and create the new direntry there. |
1355 | // This code is not implemented yet, because its occurs only if more that 32*32=1024 direntries are |
1378 | // This code is not implemented yet, because its occurs only if more that 32*32=1024 direntries are |
1356 | // within a subdirectory of root. |
1379 | // within a subdirectory of root. |
1357 | 1380 | ||
1358 | return(retvalue); // return 1 if file has been created otherwise return 0. |
1381 | return(retvalue); // return 1 if file has been created otherwise return 0. |
1359 | } |
1382 | } |
1360 | 1383 | ||
1361 | /********************************************************************************************************************************************/ |
1384 | /********************************************************************************************************************************************/ |
1362 | /* Function: FileExist(const s8* filename, u8 attribfilter, u8 attribmask, File_t *file); */ |
1385 | /* Function: FileExist(const s8* filename, u8 attribfilter, u8 attribmask, File_t *file); */ |
1363 | /* */ |
1386 | /* */ |
1364 | /* Description: This function looks for the specified file including its subdirectories beginning */ |
1387 | /* Description: This function looks for the specified file including its subdirectories beginning */ |
1365 | /* in the rootdirectory of the drive. If the file is found the Filepointer properties are */ |
1388 | /* in the rootdirectory of the drive. If the file is found the Filepointer properties are */ |
1366 | /* updated. */ |
1389 | /* updated. */ |
1367 | /* */ |
1390 | /* */ |
1368 | /* Returnvalue: 1 if file is found else 0. */ |
1391 | /* Returnvalue: 1 if file is found else 0. */ |
1369 | /********************************************************************************************************************************************/ |
1392 | /********************************************************************************************************************************************/ |
1370 | u8 FileExist(const s8* filename, const u8 attribfilter, const u8 attribmask, File_t *file) |
1393 | u8 FileExist(const s8* filename, const u8 attribfilter, const u8 attribmask, File_t *file) |
1371 | { |
1394 | { |
1372 | s8* path = 0; |
1395 | s8* path = 0; |
1373 | s8* subpath = 0; |
1396 | s8* subpath = 0; |
1374 | u8 af, am, file_exist = 0; |
1397 | u8 af, am, file_exist = 0; |
1375 | s8 dirname[12]; // 8+3 + temination character |
1398 | s8 dirname[12]; // 8+3 + temination character |
1376 | 1399 | ||
1377 | // if incomming pointers are useless return immediatly |
1400 | // if incomming pointers are useless return immediatly |
1378 | if ((filename == NULL) || (file == NULL) || (!Partition.IsValid)) return 0; |
1401 | if ((filename == NULL) || (file == NULL) || (!Partition.IsValid)) return 0; |
1379 | 1402 | ||
1380 | // trace along the filepath |
1403 | // trace along the filepath |
1381 | path = (s8*)filename; // start a the beginning of the filename string |
1404 | path = (s8*)filename; // start a the beginning of the filename string |
1382 | file->DirectorySector = 0; // start at RootDirectory with file search |
1405 | file->DirectorySector = 0; // start at RootDirectory with file search |
1383 | file->DirectoryIndex = 0; |
1406 | file->DirectoryIndex = 0; |
1384 | /* if a path begins with a '/' at index 0 the search starts at the rootdirectory otherwise we will start relative to the cwd */ |
1407 | /* if a path begins with a '/' at index 0 the search starts at the rootdirectory otherwise we will start relative to the cwd */ |
1385 | if(path[0] != '/') |
1408 | if(path[0] != '/') |
1386 | { |
1409 | { |
1387 | /* is the current working directory the rootdirectory? */ |
1410 | /* is the current working directory the rootdirectory? */ |
1388 | if(Partition.CurrentWorkingDirectory == Partition.FirstRootDirSector) file->DirectorySector = 0; |
1411 | if(Partition.CurrentWorkingDirectory == Partition.FirstRootDirSector) file->DirectorySector = 0; |
1389 | /* otherwise we are working in an subdirectory */ |
1412 | /* otherwise we are working in an subdirectory */ |
1390 | else file->DirectorySector = Partition.CurrentWorkingDirectory; |
1413 | else file->DirectorySector = Partition.CurrentWorkingDirectory; |
1391 | } |
1414 | } |
1392 | // as long as the file was not found and the remaining path is not empty |
1415 | // as long as the file was not found and the remaining path is not empty |
1393 | while((*path != 0) && !file_exist) |
1416 | while((*path != 0) && !file_exist) |
1394 | { // separate dirname and subpath from filepath string |
1417 | { // separate dirname and subpath from filepath string |
1395 | subpath = SeperateDirName(path, dirname); |
1418 | subpath = SeperateDirName(path, dirname); |
1396 | if(subpath != NULL) |
1419 | if(subpath != NULL) |
1397 | { |
1420 | { |
1398 | if(*subpath == 0) |
1421 | if(*subpath == 0) |
1399 | { // empty subpath indicates last element of dir chain |
1422 | { // empty subpath indicates last element of dir chain |
1400 | af = attribfilter; |
1423 | af = attribfilter; |
1401 | am = attribmask; |
1424 | am = attribmask; |
1402 | } |
1425 | } |
1403 | else // it must be a subdirectory and no volume label |
1426 | else // it must be a subdirectory and no volume label |
1404 | { |
1427 | { |
1405 | af = ATTR_SUBDIRECTORY; |
1428 | af = ATTR_SUBDIRECTORY; |
1406 | am = ATTR_SUBDIRECTORY|ATTR_VOLUMELABEL; |
1429 | am = ATTR_SUBDIRECTORY|ATTR_VOLUMELABEL; |
1407 | } |
1430 | } |
1408 | if(!DirectoryEntryExist(dirname, af, am, file)) |
1431 | if(!DirectoryEntryExist(dirname, af, am, file)) |
1409 | { |
1432 | { |
1410 | return (file_exist); // subdirectory does not exist |
1433 | return (file_exist); // subdirectory does not exist |
1411 | } |
1434 | } |
1412 | else |
1435 | else |
1413 | { |
1436 | { |
1414 | if (*subpath == 0) |
1437 | if (*subpath == 0) |
1415 | { |
1438 | { |
1416 | file_exist = 1; // last element of path chain was found with the given attribute filter |
1439 | file_exist = 1; // last element of path chain was found with the given attribute filter |
1417 | } |
1440 | } |
1418 | } |
1441 | } |
1419 | } |
1442 | } |
1420 | else // error seperating the subpath |
1443 | else // error seperating the subpath |
1421 | { |
1444 | { |
1422 | return file_exist; // bad subdir format |
1445 | return file_exist; // bad subdir format |
1423 | } |
1446 | } |
1424 | path = subpath; |
1447 | path = subpath; |
1425 | subpath = 0; |
1448 | subpath = 0; |
1426 | } |
1449 | } |
1427 | return (file_exist); |
1450 | return (file_exist); |
1428 | } |
1451 | } |
1429 | 1452 | ||
1430 | 1453 | ||
1431 | /********************************************************************************************************************************************/ |
1454 | /********************************************************************************************************************************************/ |
1432 | /* Function: FileCreate(const s8* filename, u8 attrib, File_t *file); */ |
1455 | /* Function: FileCreate(const s8* filename, u8 attrib, File_t *file); */ |
1433 | /* */ |
1456 | /* */ |
1434 | /* Description: This function looks for the specified file including its subdirectories beginning */ |
1457 | /* Description: This function looks for the specified file including its subdirectories beginning */ |
1435 | /* in the rootdirectory of the partition. If the file is found the Filepointer properties are */ |
1458 | /* in the rootdirectory of the partition. If the file is found the Filepointer properties are */ |
1436 | /* updated. If file or its subdirectories are not found they will be created */ |
1459 | /* updated. If file or its subdirectories are not found they will be created */ |
1437 | /* */ |
1460 | /* */ |
1438 | /* Returnvalue: 1 if file was created else 0. */ |
1461 | /* Returnvalue: 1 if file was created else 0. */ |
1439 | /********************************************************************************************************************************************/ |
1462 | /********************************************************************************************************************************************/ |
1440 | u8 FileCreate(const s8* filename, const u8 attrib, File_t *file) |
1463 | u8 FileCreate(const s8* filename, const u8 attrib, File_t *file) |
1441 | { |
1464 | { |
1442 | s8 *path = 0; |
1465 | s8 *path = 0; |
1443 | s8 *subpath = 0; |
1466 | s8 *subpath = 0; |
1444 | u8 af, am, file_created = 0; |
1467 | u8 af, am, file_created = 0; |
1445 | s8 dirname[12]; |
1468 | s8 dirname[12]; |
1446 | 1469 | ||
1447 | // if incomming pointers are useless return immediatly |
1470 | // if incomming pointers are useless return immediatly |
1448 | if ((filename == NULL) || (file == NULL) || (!Partition.IsValid)) return 0; |
1471 | if ((filename == NULL) || (file == NULL) || (!Partition.IsValid)) return 0; |
1449 | 1472 | ||
1450 | // trace along the filepath |
1473 | // trace along the filepath |
1451 | path = (s8*)filename; // start a the beginning of the filename string |
1474 | path = (s8*)filename; // start a the beginning of the filename string |
1452 | file->DirectorySector = 0; // start at RootDirectory with file search |
1475 | file->DirectorySector = 0; // start at RootDirectory with file search |
1453 | file->DirectoryIndex = 0; |
1476 | file->DirectoryIndex = 0; |
1454 | // as long as the file was not created and the remaining file path is not empty |
1477 | // as long as the file was not created and the remaining file path is not empty |
1455 | while((*path != 0) && !file_created) |
1478 | while((*path != 0) && !file_created) |
1456 | { // separate dirname and subpath from filepath string |
1479 | { // separate dirname and subpath from filepath string |
1457 | subpath = SeperateDirName(path, dirname); |
1480 | subpath = SeperateDirName(path, dirname); |
1458 | if(subpath != NULL) |
1481 | if(subpath != NULL) |
1459 | { |
1482 | { |
1460 | if(*subpath == 0) |
1483 | if(*subpath == 0) |
1461 | { // empty subpath indicates last element of dir chain |
1484 | { // empty subpath indicates last element of dir chain |
1462 | af = ATTR_NONE; |
1485 | af = ATTR_NONE; |
1463 | am = ATTR_SUBDIRECTORY|ATTR_VOLUMELABEL; // any file that is no subdir or volume label |
1486 | am = ATTR_SUBDIRECTORY|ATTR_VOLUMELABEL; // any file that is no subdir or volume label |
1464 | } |
1487 | } |
1465 | else // it must be a subdirectory and no volume label |
1488 | else // it must be a subdirectory and no volume label |
1466 | { |
1489 | { |
1467 | af = ATTR_SUBDIRECTORY; |
1490 | af = ATTR_SUBDIRECTORY; |
1468 | am = ATTR_SUBDIRECTORY|ATTR_VOLUMELABEL; |
1491 | am = ATTR_SUBDIRECTORY|ATTR_VOLUMELABEL; |
1469 | } |
1492 | } |
1470 | if(!DirectoryEntryExist(dirname, af, am, file)) // if subdir or file is not existent |
1493 | if(!DirectoryEntryExist(dirname, af, am, file)) // if subdir or file is not existent |
1471 | { // try to create subdir or file |
1494 | { // try to create subdir or file |
1472 | if(*subpath == 0) af = attrib; // if last element in dir chain take the given attribute |
1495 | if(*subpath == 0) af = attrib; // if last element in dir chain take the given attribute |
1473 | if(!CreateDirectoryEntry(dirname, af, file)) |
1496 | if(!CreateDirectoryEntry(dirname, af, file)) |
1474 | { // could not be created |
1497 | { // could not be created |
1475 | return(file_created); |
1498 | return(file_created); |
1476 | } |
1499 | } |
1477 | else if (*subpath == 0) file_created = 1; // last element of path chain was created |
1500 | else if (*subpath == 0) file_created = 1; // last element of path chain was created |
1478 | } |
1501 | } |
1479 | } |
1502 | } |
1480 | else // error seperating the subpath |
1503 | else // error seperating the subpath |
1481 | { |
1504 | { |
1482 | return file_created; // bad subdir format |
1505 | return file_created; // bad subdir format |
1483 | } |
1506 | } |
1484 | path = subpath; |
1507 | path = subpath; |
1485 | subpath = 0; |
1508 | subpath = 0; |
1486 | } |
1509 | } |
1487 | return (file_created); |
1510 | return (file_created); |
1488 | } |
1511 | } |
1489 | 1512 | ||
1490 | 1513 | ||
1491 | /********************************************************************************************************************************************/ |
1514 | /********************************************************************************************************************************************/ |
1492 | /* Function: File_t * fopen_(s8* filename, s8 mode); */ |
1515 | /* Function: File_t * fopen_(s8* filename, s8 mode); */ |
1493 | /* */ |
1516 | /* */ |
1494 | /* Description: This function looks for the specified file in the rootdirectory of the drive. If the file is found the number of the */ |
1517 | /* Description: This function looks for the specified file in the rootdirectory of the drive. If the file is found the number of the */ |
1495 | /* corrosponding filepointer is returned. Only modes 'r' (reading) and 'a' append are implemented yet. */ |
1518 | /* corrosponding filepointer is returned. Only modes 'r' (reading) and 'a' append are implemented yet. */ |
1496 | /* */ |
1519 | /* */ |
1497 | /* Returnvalue: The filepointer to the file or 0 if faild. */ |
1520 | /* Returnvalue: The filepointer to the file or 0 if faild. */ |
1498 | /********************************************************************************************************************************************/ |
1521 | /********************************************************************************************************************************************/ |
1499 | File_t * fopen_(s8 * const filename, const s8 mode) |
1522 | File_t * fopen_(s8 * const filename, const s8 mode) |
1500 | { |
1523 | { |
1501 | File_t *file = 0; |
1524 | File_t *file = 0; |
1502 | s8 *cptr; |
1525 | s8 *cptr; |
1503 | 1526 | ||
1504 | if((!Partition.IsValid) || (filename == 0)) return(file); |
1527 | if((!Partition.IsValid) || (filename == 0)) return(file); |
1505 | 1528 | ||
1506 | // Look for an unused filepointer in the file pointer list? |
1529 | // Look for an unused filepointer in the file pointer list? |
1507 | file = LockFilePointer(); |
1530 | file = LockFilePointer(); |
1508 | // if no unused file pointer was found return 0 |
1531 | // if no unused file pointer was found return 0 |
1509 | if(file == NULL) return(file); |
1532 | if(file == NULL) return(file); |
1510 | 1533 | ||
1511 | // now we have found a free filepointer and claimed it |
1534 | // now we have found a free filepointer and claimed it |
1512 | // so let initiate its property values |
1535 | // so let initiate its property values |
1513 | file->FirstSectorOfFirstCluster = SECTOR_UNDEFINED; // Sectorpointer to the first sector of the first datacluster of the file. |
1536 | file->FirstSectorOfFirstCluster = SECTOR_UNDEFINED; // Sectorpointer to the first sector of the first datacluster of the file. |
1514 | file->FirstSectorOfCurrCluster = SECTOR_UNDEFINED; // Pointer to the cluster which is edited at the moment. |
1537 | file->FirstSectorOfCurrCluster = SECTOR_UNDEFINED; // Pointer to the cluster which is edited at the moment. |
1515 | file->SectorOfCurrCluster = 0; // The sector which is edited at the moment (cluster_pointer + sector_index). |
1538 | file->SectorOfCurrCluster = 0; // The sector which is edited at the moment (cluster_pointer + sector_index). |
1516 | file->ByteOfCurrSector = 0; // The bytelocation within the current sector (cluster_pointer + sector_index + byte_index). |
1539 | file->ByteOfCurrSector = 0; // The bytelocation within the current sector (cluster_pointer + sector_index + byte_index). |
1517 | file->Mode = mode; // mode of fileoperation (read,write) |
1540 | file->Mode = mode; // mode of fileoperation (read,write) |
1518 | file->Size = 0; // the size of the opened file in bytes. |
1541 | file->Size = 0; // the size of the opened file in bytes. |
1519 | file->Position = 0; // pointer to a byte within the file 0 < fileposition < filesize |
1542 | file->Position = 0; // pointer to a byte within the file 0 < fileposition < filesize |
1520 | file->SectorInCache = SECTOR_UNDEFINED; // the last sector read, wich is still in the sectorbuffer. |
1543 | file->SectorInCache = SECTOR_UNDEFINED; // the last sector read, wich is still in the sectorbuffer. |
1521 | file->DirectorySector = SECTOR_UNDEFINED; // the sectorposition where the directoryentry has been made. |
1544 | file->DirectorySector = SECTOR_UNDEFINED; // the sectorposition where the directoryentry has been made. |
1522 | file->DirectoryIndex = 0; // the index to the directoryentry within the specified sector. |
1545 | file->DirectoryIndex = 0; // the index to the directoryentry within the specified sector. |
1523 | file->Attribute = 0; // the attribute of the file opened. |
1546 | file->Attribute = 0; // the attribute of the file opened. |
1524 | 1547 | ||
1525 | // bring the path into the correct syntax |
1548 | // bring the path into the correct syntax |
1526 | cptr = filename; |
1549 | cptr = filename; |
1527 | // search the whole string |
1550 | // search the whole string |
1528 | while(*cptr != 0) |
1551 | while(*cptr != 0) |
1529 | { |
1552 | { |
1530 | // replace all '\' by '/' |
1553 | // replace all '\' by '/' |
1531 | if(*cptr == '\\') *cptr = '/'; |
1554 | if(*cptr == '\\') *cptr = '/'; |
1532 | cptr++; |
1555 | cptr++; |
1533 | } |
1556 | } |
1534 | // check if a real file (no directory) to the given filename exist |
1557 | // check if a real file (no directory) to the given filename exist |
1535 | if(FileExist(filename, ATTR_NONE, ATTR_SUBDIRECTORY|ATTR_VOLUMELABEL, file)) |
1558 | if(FileExist(filename, ATTR_NONE, ATTR_SUBDIRECTORY|ATTR_VOLUMELABEL, file)) |
1536 | { // file exist |
1559 | { // file exist |
1537 | switch(mode) // check mode |
1560 | switch(mode) // check mode |
1538 | { |
1561 | { |
1539 | case 'a': // if mode is: append to file |
1562 | case 'a': // if mode is: append to file |
1540 | if((file->Attribute & ATTR_READONLY) == ATTR_READONLY) |
1563 | if((file->Attribute & ATTR_READONLY) == ATTR_READONLY) |
1541 | { // file is marked as readonly --> do not open this file |
1564 | { // file is marked as readonly --> do not open this file |
1542 | fclose_(file); |
1565 | fclose_(file); |
1543 | file = NULL; |
1566 | file = NULL; |
1544 | } |
1567 | } |
1545 | else |
1568 | else |
1546 | { // file is not marked as read only --> goto end of file |
1569 | { // file is not marked as read only --> goto end of file |
1547 | fseek_(file, 0, SEEK_END); // point to the end of the file |
1570 | fseek_(file, 0, SEEK_END); // point to the end of the file |
1548 | } |
1571 | } |
1549 | break; |
1572 | break; |
1550 | case 'w': // if mode is: write to file |
1573 | case 'w': // if mode is: write to file |
1551 | if((file->Attribute & ATTR_READONLY) == ATTR_READONLY) |
1574 | if((file->Attribute & ATTR_READONLY) == ATTR_READONLY) |
1552 | { // file is marked as readonly --> do not open this file |
1575 | { // file is marked as readonly --> do not open this file |
1553 | fclose_(file); |
1576 | fclose_(file); |
1554 | file = NULL; |
1577 | file = NULL; |
1555 | } |
1578 | } |
1556 | else |
1579 | else |
1557 | { // file is not marked as read only |
1580 | { // file is not marked as read only |
1558 | DirEntry_t * dir; |
1581 | DirEntry_t * dir; |
1559 | // free all clusters of that file |
1582 | // free all clusters of that file |
1560 | DeleteClusterChain(SectorToFat16Cluster(file->FirstSectorOfFirstCluster)); |
1583 | DeleteClusterChain(SectorToFat16Cluster(file->FirstSectorOfFirstCluster)); |
1561 | // update directory entry of that file |
1584 | // update directory entry of that file |
1562 | file->SectorInCache = file->DirectorySector; // update the sector number of file cache. |
1585 | file->SectorInCache = file->DirectorySector; // update the sector number of file cache. |
1563 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read in the sector. |
1586 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read in the sector. |
1564 | { |
1587 | { |
1565 | Fat16_Deinit(); |
1588 | Fat16_Deinit(); |
1566 | return(NULL); |
1589 | return(NULL); |
1567 | } |
1590 | } |
1568 | dir = (DirEntry_t *)file->Cache; // set pointer to directory |
1591 | dir = (DirEntry_t *)file->Cache; // set pointer to directory |
1569 | dir[file->DirectoryIndex].ModTime = FileTime(&SystemTime); // set modification time |
1592 | dir[file->DirectoryIndex].ModTime = FileTime(&SystemTime); // set modification time |
1570 | dir[file->DirectoryIndex].ModDate = FileDate(&SystemTime); // set modification date |
1593 | dir[file->DirectoryIndex].ModDate = FileDate(&SystemTime); // set modification date |
1571 | dir[file->DirectoryIndex].LastAccessDate = dir[file->DirectoryIndex].ModDate; |
1594 | dir[file->DirectoryIndex].LastAccessDate = dir[file->DirectoryIndex].ModDate; |
1572 | dir[file->DirectoryIndex].StartCluster = CLUSTER_UNDEFINED; // update startcluster |
1595 | dir[file->DirectoryIndex].StartCluster = CLUSTER_UNDEFINED; // update startcluster |
1573 | dir[file->DirectoryIndex].Size = 0; |
1596 | dir[file->DirectoryIndex].Size = 0; |
1574 | // write sector containing the direntry |
1597 | // write sector containing the direntry |
1575 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache)) |
1598 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache)) |
1576 | { |
1599 | { |
1577 | Fat16_Deinit(); |
1600 | Fat16_Deinit(); |
1578 | return(NULL); |
1601 | return(NULL); |
1579 | } |
1602 | } |
1580 | file->FirstSectorOfFirstCluster = SECTOR_UNDEFINED; |
1603 | file->FirstSectorOfFirstCluster = SECTOR_UNDEFINED; |
1581 | file->FirstSectorOfCurrCluster = file->FirstSectorOfFirstCluster; |
1604 | file->FirstSectorOfCurrCluster = file->FirstSectorOfFirstCluster; |
1582 | file->SectorOfCurrCluster = 0; |
1605 | file->SectorOfCurrCluster = 0; |
1583 | file->ByteOfCurrSector = 0; |
1606 | file->ByteOfCurrSector = 0; |
1584 | file->Size = 0; |
1607 | file->Size = 0; |
1585 | file->Position = 0; |
1608 | file->Position = 0; |
1586 | fseek_(file, 0, SEEK_SET); |
1609 | fseek_(file, 0, SEEK_SET); |
1587 | } |
1610 | } |
1588 | break; |
1611 | break; |
1589 | case 'r': // if mode is: read from file |
1612 | case 'r': // if mode is: read from file |
1590 | // goto end of file |
1613 | // goto end of file |
1591 | fseek_(file, 0, SEEK_SET); |
1614 | fseek_(file, 0, SEEK_SET); |
1592 | break; |
1615 | break; |
1593 | default: // other modes are not supported |
1616 | default: // other modes are not supported |
1594 | fclose_(file); |
1617 | fclose_(file); |
1595 | file = NULL; |
1618 | file = NULL; |
1596 | break; |
1619 | break; |
1597 | } |
1620 | } |
1598 | return(file); |
1621 | return(file); |
1599 | } |
1622 | } |
1600 | else // file does not exist |
1623 | else // file does not exist |
1601 | { |
1624 | { |
1602 | switch(mode) // check mode |
1625 | switch(mode) // check mode |
1603 | { |
1626 | { |
1604 | case 'a': |
1627 | case 'a': |
1605 | case 'w': // if mode is write or append |
1628 | case 'w': // if mode is write or append |
1606 | // try to create the file |
1629 | // try to create the file |
1607 | if(!FileCreate(filename, ATTR_ARCHIVE, file)) |
1630 | if(!FileCreate(filename, ATTR_ARCHIVE, file)) |
1608 | { // if it could not be created |
1631 | { // if it could not be created |
1609 | fclose_(file); |
1632 | fclose_(file); |
1610 | file = NULL; |
1633 | file = NULL; |
1611 | } |
1634 | } |
1612 | break; |
1635 | break; |
1613 | case 'r': // else opened for 'r' |
1636 | case 'r': // else opened for 'r' |
1614 | default: // if unsupported mode |
1637 | default: // if unsupported mode |
1615 | fclose_(file); |
1638 | fclose_(file); |
1616 | file = NULL; |
1639 | file = NULL; |
1617 | break; |
1640 | break; |
1618 | } |
1641 | } |
1619 | return(file); |
1642 | return(file); |
1620 | } |
1643 | } |
1621 | // we should never come to this point |
1644 | // we should never come to this point |
1622 | fclose_(file); |
1645 | fclose_(file); |
1623 | file = NULL; |
1646 | file = NULL; |
1624 | return(file); |
1647 | return(file); |
1625 | } |
1648 | } |
1626 | 1649 | ||
1627 | /****************************************************************************************************************************************************/ |
1650 | /****************************************************************************************************************************************************/ |
1628 | /* Function: fflush_(File *); */ |
1651 | /* Function: fflush_(File *); */ |
1629 | /* */ |
1652 | /* */ |
1630 | /* Description: This function writes the data already in the buffer but not yet written to the file. */ |
1653 | /* Description: This function writes the data already in the buffer but not yet written to the file. */ |
1631 | /* */ |
1654 | /* */ |
1632 | /* Returnvalue: 0 on success EOF on error */ |
1655 | /* Returnvalue: 0 on success EOF on error */ |
1633 | /****************************************************************************************************************************************************/ |
1656 | /****************************************************************************************************************************************************/ |
1634 | s16 fflush_(File_t *file) |
1657 | s16 fflush_(File_t *file) |
1635 | { |
1658 | { |
1636 | DirEntry_t *dir; |
1659 | DirEntry_t *dir; |
1637 | 1660 | ||
1638 | if((file == NULL) || (!Partition.IsValid)) return (EOF); |
1661 | if((file == NULL) || (!Partition.IsValid)) return (EOF); |
1639 | 1662 | ||
1640 | switch(file->Mode) |
1663 | switch(file->Mode) |
1641 | { |
1664 | { |
1642 | case 'a': |
1665 | case 'a': |
1643 | case 'w': |
1666 | case 'w': |
1644 | if(file->ByteOfCurrSector > 0) // has data been added to the file? |
1667 | if(file->ByteOfCurrSector > 0) // has data been added to the file? |
1645 | { |
1668 | { |
1646 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache))// save the data still in the buffer |
1669 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache))// save the data still in the buffer |
1647 | { |
1670 | { |
1648 | Fat16_Deinit(); |
1671 | Fat16_Deinit(); |
1649 | return(EOF); |
1672 | return(EOF); |
1650 | } |
1673 | } |
1651 | } |
1674 | } |
1652 | file->SectorInCache = file->DirectorySector; |
1675 | file->SectorInCache = file->DirectorySector; |
1653 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache)) // read the directory entry for this file. |
1676 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache)) // read the directory entry for this file. |
1654 | { |
1677 | { |
1655 | Fat16_Deinit(); |
1678 | Fat16_Deinit(); |
1656 | return(EOF); |
1679 | return(EOF); |
1657 | } |
1680 | } |
1658 | 1681 | ||
1659 | dir = (DirEntry_t *)file->Cache; |
1682 | dir = (DirEntry_t *)file->Cache; |
1660 | // update file size and modification time & date |
1683 | // update file size and modification time & date |
1661 | dir[file->DirectoryIndex].ModTime = FileTime(&SystemTime); |
1684 | dir[file->DirectoryIndex].ModTime = FileTime(&SystemTime); |
1662 | dir[file->DirectoryIndex].ModDate = FileDate(&SystemTime); |
1685 | dir[file->DirectoryIndex].ModDate = FileDate(&SystemTime); |
1663 | dir[file->DirectoryIndex].LastAccessDate = dir[file->DirectoryIndex].ModDate; |
1686 | dir[file->DirectoryIndex].LastAccessDate = dir[file->DirectoryIndex].ModDate; |
1664 | dir[file->DirectoryIndex].Size = file->Size; // update file size |
1687 | dir[file->DirectoryIndex].Size = file->Size; // update file size |
1665 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache)) // write back to sd-card |
1688 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache)) // write back to sd-card |
1666 | { |
1689 | { |
1667 | Fat16_Deinit(); |
1690 | Fat16_Deinit(); |
1668 | return(EOF); |
1691 | return(EOF); |
1669 | } |
1692 | } |
1670 | break; |
1693 | break; |
1671 | case 'r': |
1694 | case 'r': |
1672 | default: |
1695 | default: |
1673 | // do nothing! |
1696 | // do nothing! |
1674 | return(EOF); |
1697 | return(EOF); |
1675 | break; |
1698 | break; |
1676 | 1699 | ||
1677 | } |
1700 | } |
1678 | return(0); |
1701 | return(0); |
1679 | } |
1702 | } |
1680 | 1703 | ||
1681 | /****************************************************************************************************************************************/ |
1704 | /****************************************************************************************************************************************/ |
1682 | /* Function: fclose_(File *file); */ |
1705 | /* Function: fclose_(File *file); */ |
1683 | /* */ |
1706 | /* */ |
1684 | /* Description: This function closes the open file by writing the remaining data */ |
1707 | /* Description: This function closes the open file by writing the remaining data */ |
1685 | /* from the buffer to the device and entering the filesize in the directory entry. */ |
1708 | /* from the buffer to the device and entering the filesize in the directory entry. */ |
1686 | /* */ |
1709 | /* */ |
1687 | /* Returnvalue: 0 on success EOF on error */ |
1710 | /* Returnvalue: 0 on success EOF on error */ |
1688 | /****************************************************************************************************************************************/ |
1711 | /****************************************************************************************************************************************/ |
1689 | s16 fclose_(File_t *file) |
1712 | s16 fclose_(File_t *file) |
1690 | { |
1713 | { |
1691 | s16 returnvalue = EOF; |
1714 | s16 returnvalue = EOF; |
1692 | 1715 | ||
1693 | if(file == NULL) return(returnvalue); |
1716 | if(file == NULL) return(returnvalue); |
1694 | returnvalue = fflush_(file); |
1717 | returnvalue = fflush_(file); |
1695 | UnlockFilePointer(file); |
1718 | UnlockFilePointer(file); |
1696 | return(returnvalue); |
1719 | return(returnvalue); |
1697 | } |
1720 | } |
1698 | 1721 | ||
1699 | /********************************************************************************************************************************************/ |
1722 | /********************************************************************************************************************************************/ |
1700 | /* Function: fgetc_(File *file); */ |
1723 | /* Function: fgetc_(File *file); */ |
1701 | /* */ |
1724 | /* */ |
1702 | /* Description: This function reads and returns one character from the specified file. Is the end of the actual sector reached the */ |
1725 | /* Description: This function reads and returns one character from the specified file. Is the end of the actual sector reached the */ |
1703 | /* next sector of the cluster is read. If the last sector of the cluster read the next cluster will be searched in FAT. */ |
1726 | /* next sector of the cluster is read. If the last sector of the cluster read the next cluster will be searched in FAT. */ |
1704 | /* */ |
1727 | /* */ |
1705 | /* Returnvalue: The function returns the character read from the specified memorylocation as u8 casted to s16 or EOF. */ |
1728 | /* Returnvalue: The function returns the character read from the specified memorylocation as u8 casted to s16 or EOF. */ |
1706 | /********************************************************************************************************************************************/ |
1729 | /********************************************************************************************************************************************/ |
1707 | s16 fgetc_(File_t *file) |
1730 | s16 fgetc_(File_t *file) |
1708 | { |
1731 | { |
1709 | s16 c = EOF; |
1732 | s16 c = EOF; |
1710 | u32 curr_sector; |
1733 | u32 curr_sector; |
1711 | 1734 | ||
1712 | if( (!Partition.IsValid) || (file == NULL)) return(c); |
1735 | if( (!Partition.IsValid) || (file == NULL)) return(c); |
1713 | // if the end of the file is not reached, get the next character. |
1736 | // if the end of the file is not reached, get the next character. |
1714 | if((0 < file->Size) && ((file->Position) < file->Size) ) |
1737 | if((0 < file->Size) && ((file->Position) < file->Size) ) |
1715 | { |
1738 | { |
1716 | curr_sector = file->FirstSectorOfCurrCluster; // calculate the sector of the next character to be read. |
1739 | curr_sector = file->FirstSectorOfCurrCluster; // calculate the sector of the next character to be read. |
1717 | curr_sector += file->SectorOfCurrCluster; |
1740 | curr_sector += file->SectorOfCurrCluster; |
1718 | 1741 | ||
1719 | if(file->SectorInCache != curr_sector) |
1742 | if(file->SectorInCache != curr_sector) |
1720 | { |
1743 | { |
1721 | file->SectorInCache = curr_sector; |
1744 | file->SectorInCache = curr_sector; |
1722 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache,file->Cache)) |
1745 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache,file->Cache)) |
1723 | { |
1746 | { |
1724 | Fat16_Deinit(); |
1747 | Fat16_Deinit(); |
1725 | return(EOF); |
1748 | return(EOF); |
1726 | } |
1749 | } |
1727 | } |
1750 | } |
1728 | c = (s16) file->Cache[file->ByteOfCurrSector]; |
1751 | c = (s16) file->Cache[file->ByteOfCurrSector]; |
1729 | file->Position++; // increment file position |
1752 | file->Position++; // increment file position |
1730 | file->ByteOfCurrSector++; // goto next byte in sector |
1753 | file->ByteOfCurrSector++; // goto next byte in sector |
1731 | if(file->ByteOfCurrSector >= BYTES_PER_SECTOR) // if end of sector |
1754 | if(file->ByteOfCurrSector >= BYTES_PER_SECTOR) // if end of sector |
1732 | { |
1755 | { |
1733 | file->ByteOfCurrSector = 0; // reset byte location |
1756 | file->ByteOfCurrSector = 0; // reset byte location |
1734 | file->SectorOfCurrCluster++; // next sector |
1757 | file->SectorOfCurrCluster++; // next sector |
1735 | if(file->SectorOfCurrCluster >= Partition.SectorsPerCluster) // if end of cluster is reached, the next datacluster has to be searched in the FAT. |
1758 | if(file->SectorOfCurrCluster >= Partition.SectorsPerCluster) // if end of cluster is reached, the next datacluster has to be searched in the FAT. |
1736 | { |
1759 | { |
1737 | 1760 | ||
1738 | if(GetNextCluster(file)) // Sets the clusterpointer of the file to the next datacluster. |
1761 | if(GetNextCluster(file)) // Sets the clusterpointer of the file to the next datacluster. |
1739 | { |
1762 | { |
1740 | file->SectorOfCurrCluster = 0; // start reading new cluster at first sector of the cluster. |
1763 | file->SectorOfCurrCluster = 0; // start reading new cluster at first sector of the cluster. |
1741 | } |
1764 | } |
1742 | else // the last cluster was allready reached |
1765 | else // the last cluster was allready reached |
1743 | { |
1766 | { |
1744 | file->SectorOfCurrCluster--; // jump back to the last sector in the last cluster |
1767 | file->SectorOfCurrCluster--; // jump back to the last sector in the last cluster |
1745 | file->ByteOfCurrSector = BYTES_PER_SECTOR; // set ByteOfCurrSector one byte over sector end |
1768 | file->ByteOfCurrSector = BYTES_PER_SECTOR; // set ByteOfCurrSector one byte over sector end |
1746 | } |
1769 | } |
1747 | } |
1770 | } |
1748 | } |
1771 | } |
1749 | } |
1772 | } |
1750 | return(c); |
1773 | return(c); |
1751 | } |
1774 | } |
1752 | 1775 | ||
1753 | /********************************************************************************************************************************************/ |
1776 | /********************************************************************************************************************************************/ |
1754 | /* Function: fputc_( const s8 c, File *file); */ |
1777 | /* Function: fputc_( const s8 c, File *file); */ |
1755 | /* */ |
1778 | /* */ |
1756 | /* Description: This function writes a byte to the specified file and takes care of writing the necessary FAT- Entries. */ |
1779 | /* Description: This function writes a byte to the specified file and takes care of writing the necessary FAT- Entries. */ |
1757 | /* next sector of the cluster is read. If the last sector of the cluster read the next cluster will be searched in FAT. */ |
1780 | /* next sector of the cluster is read. If the last sector of the cluster read the next cluster will be searched in FAT. */ |
1758 | /* */ |
1781 | /* */ |
1759 | /* Returnvalue: The function returns the character written to the stream or EOF on error. */ |
1782 | /* Returnvalue: The function returns the character written to the stream or EOF on error. */ |
1760 | /********************************************************************************************************************************************/ |
1783 | /********************************************************************************************************************************************/ |
1761 | s16 fputc_(const s8 c, File_t *file) |
1784 | s16 fputc_(const s8 c, File_t *file) |
1762 | { |
1785 | { |
1763 | u32 curr_sector = 0; |
1786 | u32 curr_sector = 0; |
1764 | 1787 | ||
1765 | if((!Partition.IsValid) || (file == NULL)) return(EOF); |
1788 | if((!Partition.IsValid) || (file == NULL)) return(EOF); |
1766 | switch(file->Mode) |
1789 | switch(file->Mode) |
1767 | { |
1790 | { |
1768 | case 'w': |
1791 | case 'w': |
1769 | case 'a': |
1792 | case 'a': |
1770 | // If file position equals to file size, then the end of file has been reached. |
1793 | // If file position equals to file size, then the end of file has been reached. |
1771 | // In this case it has to be checked that the ByteOfCurrSector is BYTES_PER_SECTOR |
1794 | // In this case it has to be checked that the ByteOfCurrSector is BYTES_PER_SECTOR |
1772 | // and a new cluster should be appended. |
1795 | // and a new cluster should be appended. |
1773 | // If the first sector of first cluster is unvalid, then the file claims no data clusters |
1796 | // If the first sector of first cluster is unvalid, then the file claims no data clusters |
1774 | // and size should be zero, therefore append a new Cluster too. |
1797 | // and size should be zero, therefore append a new Cluster too. |
1775 | if(((file->Position >= file->Size) && (file->ByteOfCurrSector >= BYTES_PER_SECTOR)) || (file->FirstSectorOfFirstCluster == SECTOR_UNDEFINED)) |
1798 | if(((file->Position >= file->Size) && (file->ByteOfCurrSector >= BYTES_PER_SECTOR)) || (file->FirstSectorOfFirstCluster == SECTOR_UNDEFINED)) |
1776 | { |
1799 | { |
1777 | if(CLUSTER_UNDEFINED == AppendCluster(file)) return(EOF); |
1800 | if(CLUSTER_UNDEFINED == AppendCluster(file)) return(EOF); |
1778 | } |
1801 | } |
1779 | 1802 | ||
1780 | curr_sector = file->FirstSectorOfCurrCluster; |
1803 | curr_sector = file->FirstSectorOfCurrCluster; |
1781 | curr_sector += file->SectorOfCurrCluster; |
1804 | curr_sector += file->SectorOfCurrCluster; |
1782 | if(file->SectorInCache != curr_sector) |
1805 | if(file->SectorInCache != curr_sector) |
1783 | { |
1806 | { |
1784 | file->SectorInCache = curr_sector; |
1807 | file->SectorInCache = curr_sector; |
1785 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache)) |
1808 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache)) |
1786 | { |
1809 | { |
1787 | Fat16_Deinit(); |
1810 | Fat16_Deinit(); |
1788 | return(EOF); |
1811 | return(EOF); |
1789 | } |
1812 | } |
1790 | } |
1813 | } |
1791 | 1814 | ||
1792 | file->Cache[file->ByteOfCurrSector] = (u8)c; // write databyte into the buffer. The byte will be written to the device at once |
1815 | file->Cache[file->ByteOfCurrSector] = (u8)c; // write databyte into the buffer. The byte will be written to the device at once |
1793 | if(file->Size == file->Position) file->Size++; // a character has been written to the file so the size is incremented only when the character has been added at the end of the file. |
1816 | if(file->Size == file->Position) file->Size++; // a character has been written to the file so the size is incremented only when the character has been added at the end of the file. |
1794 | file->Position++; // the actual positon within the file. |
1817 | file->Position++; // the actual positon within the file. |
1795 | file->ByteOfCurrSector++; // goto next byte in sector |
1818 | file->ByteOfCurrSector++; // goto next byte in sector |
1796 | if(file->ByteOfCurrSector >= BYTES_PER_SECTOR) // if the end of this sector is reached yet |
1819 | if(file->ByteOfCurrSector >= BYTES_PER_SECTOR) // if the end of this sector is reached yet |
1797 | { // save the sector to the sd-card |
1820 | { // save the sector to the sd-card |
1798 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache)) |
1821 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache)) |
1799 | { |
1822 | { |
1800 | Fat16_Deinit(); |
1823 | Fat16_Deinit(); |
1801 | return(EOF); |
1824 | return(EOF); |
1802 | } |
1825 | } |
1803 | file->ByteOfCurrSector = 0; // reset byte location |
1826 | file->ByteOfCurrSector = 0; // reset byte location |
1804 | file->SectorOfCurrCluster++; // next sector |
1827 | file->SectorOfCurrCluster++; // next sector |
1805 | if(file->SectorOfCurrCluster >= Partition.SectorsPerCluster)// if end of cluster is reached, the next datacluster has to be searched in the FAT. |
1828 | if(file->SectorOfCurrCluster >= Partition.SectorsPerCluster)// if end of cluster is reached, the next datacluster has to be searched in the FAT. |
1806 | { |
1829 | { |
1807 | if(!GetNextCluster(file)) // Sets the clusterpointer of the file to the next datacluster. |
1830 | if(!GetNextCluster(file)) // Sets the clusterpointer of the file to the next datacluster. |
1808 | { // if current cluster was the last cluster of the file |
1831 | { // if current cluster was the last cluster of the file |
1809 | if(!AppendCluster(file)) // append a new and free cluster at the end of the file. |
1832 | if(!AppendCluster(file)) // append a new and free cluster at the end of the file. |
1810 | { |
1833 | { |
1811 | file->SectorOfCurrCluster--; // jump back to last sector of last cluster |
1834 | file->SectorOfCurrCluster--; // jump back to last sector of last cluster |
1812 | file->ByteOfCurrSector = BYTES_PER_SECTOR; // set byte location to 1 byte over sector len |
1835 | file->ByteOfCurrSector = BYTES_PER_SECTOR; // set byte location to 1 byte over sector len |
1813 | return(EOF); |
1836 | return(EOF); |
1814 | } |
1837 | } |
1815 | } |
1838 | } |
1816 | else // next cluster |
1839 | else // next cluster |
1817 | { |
1840 | { |
1818 | file->SectorOfCurrCluster = 0; // start reading new cluster at first sector of the cluster. |
1841 | file->SectorOfCurrCluster = 0; // start reading new cluster at first sector of the cluster. |
1819 | } |
1842 | } |
1820 | } |
1843 | } |
1821 | } |
1844 | } |
1822 | break; |
1845 | break; |
1823 | case 'r': |
1846 | case 'r': |
1824 | default: |
1847 | default: |
1825 | return(EOF); |
1848 | return(EOF); |
1826 | break; |
1849 | break; |
1827 | } // EOF switch(file->Mode) |
1850 | } // EOF switch(file->Mode) |
1828 | return(0); |
1851 | return(0); |
1829 | } |
1852 | } |
1830 | 1853 | ||
1831 | 1854 | ||
1832 | /****************************************************************************************************************************************/ |
1855 | /****************************************************************************************************************************************/ |
1833 | /* Function: fread_(void *buffer, s32 size, s32 count, File *File); */ |
1856 | /* Function: fread_(void *buffer, s32 size, s32 count, File *File); */ |
1834 | /* */ |
1857 | /* */ |
1835 | /* Description: This function reads count objects of the specified size */ |
1858 | /* Description: This function reads count objects of the specified size */ |
1836 | /* from the actual position of the file to the specified buffer. */ |
1859 | /* from the actual position of the file to the specified buffer. */ |
1837 | /* */ |
1860 | /* */ |
1838 | /* Returnvalue: The function returns the number of objects (not bytes) read from the file. */ |
1861 | /* Returnvalue: The function returns the number of objects (not bytes) read from the file. */ |
1839 | /****************************************************************************************************************************************/ |
1862 | /****************************************************************************************************************************************/ |
1840 | u32 fread_(void *buffer, u32 size, u32 count, File_t *file) |
1863 | u32 fread_(void *buffer, u32 size, u32 count, File_t *file) |
1841 | { |
1864 | { |
1842 | u32 object_cnt = 0; // count the number of objects read from the file. |
1865 | u32 object_cnt = 0; // count the number of objects read from the file. |
1843 | u32 object_size = 0; // count the number of bytes read from the actual object. |
1866 | u32 object_size = 0; // count the number of bytes read from the actual object. |
1844 | u8 *pbuff = 0; // a pointer to the actual bufferposition. |
1867 | u8 *pbuff = 0; // a pointer to the actual bufferposition. |
1845 | u8 success = 1; // no error occured during read operation to the file. |
1868 | u8 success = 1; // no error occured during read operation to the file. |
1846 | s16 c; |
1869 | s16 c; |
1847 | 1870 | ||
1848 | if((!Partition.IsValid) || (file == NULL) || (buffer == NULL)) return(0); |
1871 | if((!Partition.IsValid) || (file == NULL) || (buffer == NULL)) return(0); |
1849 | 1872 | ||
1850 | pbuff = (u8 *) buffer; // cast the void pointer to an u8 * |
1873 | pbuff = (u8 *) buffer; // cast the void pointer to an u8 * |
1851 | 1874 | ||
1852 | while((object_cnt < count) && success) |
1875 | while((object_cnt < count) && success) |
1853 | { |
1876 | { |
1854 | object_size = size; |
1877 | object_size = size; |
1855 | while((size > 0) && success) |
1878 | while((size > 0) && success) |
1856 | { |
1879 | { |
1857 | c = fgetc_(file); |
1880 | c = fgetc_(file); |
1858 | if(c != EOF) |
1881 | if(c != EOF) |
1859 | { |
1882 | { |
1860 | *pbuff = (u8)c; // read a byte from the buffer to the opened file. |
1883 | *pbuff = (u8)c; // read a byte from the buffer to the opened file. |
1861 | pbuff++; |
1884 | pbuff++; |
1862 | size--; |
1885 | size--; |
1863 | } |
1886 | } |
1864 | else // error or end of file reached |
1887 | else // error or end of file reached |
1865 | { |
1888 | { |
1866 | success = 0; |
1889 | success = 0; |
1867 | } |
1890 | } |
1868 | } |
1891 | } |
1869 | if(success) object_cnt++; |
1892 | if(success) object_cnt++; |
1870 | } |
1893 | } |
1871 | return(object_cnt); // return the number of objects succesfully read from the file |
1894 | return(object_cnt); // return the number of objects succesfully read from the file |
1872 | } |
1895 | } |
1873 | 1896 | ||
1874 | 1897 | ||
1875 | /****************************************************************************************************************************************/ |
1898 | /****************************************************************************************************************************************/ |
1876 | /* Function: fwrite_(void *buffer, s32 size, s32 count, File *file); */ |
1899 | /* Function: fwrite_(void *buffer, s32 size, s32 count, File *file); */ |
1877 | /* */ |
1900 | /* */ |
1878 | /* Description: This function writes count objects of the specified size */ |
1901 | /* Description: This function writes count objects of the specified size */ |
1879 | /* from the buffer pointer to the actual position in the file. */ |
1902 | /* from the buffer pointer to the actual position in the file. */ |
1880 | /* */ |
1903 | /* */ |
1881 | /* Returnvalue: The function returns the number of objects (not bytes) read from the file. */ |
1904 | /* Returnvalue: The function returns the number of objects (not bytes) read from the file. */ |
1882 | /****************************************************************************************************************************************/ |
1905 | /****************************************************************************************************************************************/ |
1883 | u32 fwrite_(void *buffer, u32 size, u32 count, File_t *file) |
1906 | u32 fwrite_(void *buffer, u32 size, u32 count, File_t *file) |
1884 | { |
1907 | { |
1885 | u32 object_cnt = 0; // count the number of objects written to the file. |
1908 | u32 object_cnt = 0; // count the number of objects written to the file. |
1886 | u32 object_size = 0; // count the number of bytes written from the actual object. |
1909 | u32 object_size = 0; // count the number of bytes written from the actual object. |
1887 | u8 *pbuff = 0; // a pointer to the actual bufferposition. |
1910 | u8 *pbuff = 0; // a pointer to the actual bufferposition. |
1888 | u8 success = 1; // no error occured during write operation to the file. |
1911 | u8 success = 1; // no error occured during write operation to the file. |
1889 | s16 c; |
1912 | s16 c; |
1890 | 1913 | ||
1891 | if((!Partition.IsValid) || (file == NULL) || (buffer == NULL)) return(0); |
1914 | if((!Partition.IsValid) || (file == NULL) || (buffer == NULL)) return(0); |
1892 | if(file->Mode == 'r') return (0); // opened read only |
1915 | if(file->Mode == 'r') return (0); // opened read only |
1893 | pbuff = (u8 *) buffer; // cast the void pointer to an u8 * |
1916 | pbuff = (u8 *) buffer; // cast the void pointer to an u8 * |
1894 | 1917 | ||
1895 | while((object_cnt < count) && success) |
1918 | while((object_cnt < count) && success) |
1896 | { |
1919 | { |
1897 | object_size = size; |
1920 | object_size = size; |
1898 | while((size > 0) && success) |
1921 | while((size > 0) && success) |
1899 | { |
1922 | { |
1900 | c = fputc_(*pbuff, file); // write a byte from the buffer to the opened file. |
1923 | c = fputc_(*pbuff, file); // write a byte from the buffer to the opened file. |
1901 | if(c != EOF) |
1924 | if(c != EOF) |
1902 | { |
1925 | { |
1903 | pbuff++; |
1926 | pbuff++; |
1904 | size--; |
1927 | size--; |
1905 | } |
1928 | } |
1906 | else |
1929 | else |
1907 | { |
1930 | { |
1908 | success = 0; |
1931 | success = 0; |
1909 | } |
1932 | } |
1910 | } |
1933 | } |
1911 | if(success) object_cnt++; |
1934 | if(success) object_cnt++; |
1912 | } |
1935 | } |
1913 | 1936 | ||
1914 | return(object_cnt); // return the number of objects succesfully written to the file |
1937 | return(object_cnt); // return the number of objects succesfully written to the file |
1915 | } |
1938 | } |
1916 | 1939 | ||
1917 | 1940 | ||
1918 | /****************************************************************************************************************************************/ |
1941 | /****************************************************************************************************************************************/ |
1919 | /* Function: fputs_(const s8 *string, File_t *File); */ |
1942 | /* Function: fputs_(const s8 *string, File_t *File); */ |
1920 | /* */ |
1943 | /* */ |
1921 | /* Description: This function writes a string to the specified file. */ |
1944 | /* Description: This function writes a string to the specified file. */ |
1922 | /* */ |
1945 | /* */ |
1923 | /* Returnvalue: The function returns a no negative value or EOF on error. */ |
1946 | /* Returnvalue: The function returns a no negative value or EOF on error. */ |
1924 | /****************************************************************************************************************************************/ |
1947 | /****************************************************************************************************************************************/ |
1925 | s16 fputs_(s8 * const string, File_t * const file) |
1948 | s16 fputs_(s8 * const string, File_t * const file) |
1926 | { |
1949 | { |
1927 | u8 i=0; |
1950 | u8 i=0; |
1928 | s16 c = 0; |
1951 | s16 c = 0; |
1929 | 1952 | ||
1930 | if((!Partition.IsValid) || (file == NULL) || (string == NULL)) return(EOF); |
1953 | if((!Partition.IsValid) || (file == NULL) || (string == NULL)) return(EOF); |
1931 | if(file->Mode == 'r') return(EOF); |
1954 | if(file->Mode == 'r') return(EOF); |
1932 | while((string[i] != 0)&& (c != EOF)) |
1955 | while((string[i] != 0)&& (c != EOF)) |
1933 | { |
1956 | { |
1934 | c = fputc_(string[i], file); |
1957 | c = fputc_(string[i], file); |
1935 | i++; |
1958 | i++; |
1936 | } |
1959 | } |
1937 | return(c); |
1960 | return(c); |
1938 | } |
1961 | } |
1939 | 1962 | ||
1940 | /****************************************************************************************************************************************/ |
1963 | /****************************************************************************************************************************************/ |
1941 | /* Function: fgets_(s8 *, s16 , File_t *); */ |
1964 | /* Function: fgets_(s8 *, s16 , File_t *); */ |
1942 | /* */ |
1965 | /* */ |
1943 | /* Description: This function reads a string from the file to the specifies string. */ |
1966 | /* Description: This function reads a string from the file to the specifies string. */ |
1944 | /* */ |
1967 | /* */ |
1945 | /* Returnvalue: A pointer to the string read from the file or 0 on error. */ |
1968 | /* Returnvalue: A pointer to the string read from the file or 0 on error. */ |
1946 | /****************************************************************************************************************************************/ |
1969 | /****************************************************************************************************************************************/ |
1947 | s8 * fgets_(s8 * const string, s16 const length, File_t * const file) |
1970 | s8 * fgets_(s8 * const string, s16 const length, File_t * const file) |
1948 | { |
1971 | { |
1949 | s8 *pbuff; |
1972 | s8 *pbuff; |
1950 | s16 c = 0, bytecount; |
1973 | s16 c = 0, bytecount; |
1951 | 1974 | ||
1952 | if((!Partition.IsValid) || (file == NULL) || (string == NULL) || (length < 1)) return (0); |
1975 | if((!Partition.IsValid) || (file == NULL) || (string == NULL) || (length < 1)) return (0); |
1953 | bytecount = length; |
1976 | bytecount = length; |
1954 | pbuff = string; // set write pointer to start of string |
1977 | pbuff = string; // set write pointer to start of string |
1955 | while(bytecount > 1) // read the length-1 characters from the file to the string. |
1978 | while(bytecount > 1) // read the length-1 characters from the file to the string. |
1956 | { |
1979 | { |
1957 | c = fgetc_(file); // read a character from the opened file. |
1980 | c = fgetc_(file); // read a character from the opened file. |
1958 | switch(c) |
1981 | switch(c) |
1959 | { |
1982 | { |
1960 | case 0x0A: // new line |
1983 | case 0x0A: // new line |
1961 | *pbuff = 0; // set string terminator |
1984 | *pbuff = 0; // set string terminator |
1962 | return(string); // return normal |
1985 | return(string); // return normal |
1963 | 1986 | ||
1964 | case EOF: |
1987 | case EOF: |
1965 | *pbuff = 0; // set string terminator |
1988 | *pbuff = 0; // set string terminator |
1966 | return(0); |
1989 | return(0); |
1967 | 1990 | ||
1968 | default: |
1991 | default: |
1969 | *pbuff++ = (s8)c; // copy byte to string |
1992 | *pbuff++ = (s8)c; // copy byte to string |
1970 | bytecount--; |
1993 | bytecount--; |
1971 | break; |
1994 | break; |
1972 | } |
1995 | } |
1973 | } |
1996 | } |
1974 | *pbuff = 0; // set string terminator |
1997 | *pbuff = 0; // set string terminator |
1975 | return(string); |
1998 | return(string); |
1976 | } |
1999 | } |
1977 | 2000 | ||
1978 | /****************************************************************************************************************************************/ |
2001 | /****************************************************************************************************************************************/ |
1979 | /* Function: fexist_(const u8*); */ |
2002 | /* Function: fexist_(const u8*); */ |
1980 | /* */ |
2003 | /* */ |
1981 | /* Description: This function checks if a file already exist. */ |
2004 | /* Description: This function checks if a file already exist. */ |
1982 | /* */ |
2005 | /* */ |
1983 | /* Returnvalue: 1 if the file exist else 0. */ |
2006 | /* Returnvalue: 1 if the file exist else 0. */ |
1984 | /****************************************************************************************************************************************/ |
2007 | /****************************************************************************************************************************************/ |
1985 | u8 fexist_(s8 * const filename) |
2008 | u8 fexist_(s8 * const filename) |
1986 | { |
2009 | { |
1987 | u8 exist = 0; |
2010 | u8 exist = 0; |
1988 | File_t *file = 0; |
2011 | File_t *file = 0; |
1989 | file = LockFilePointer(); |
2012 | file = LockFilePointer(); |
1990 | exist = FileExist(filename, ATTR_NONE, ATTR_SUBDIRECTORY|ATTR_VOLUMELABEL, file); |
2013 | exist = FileExist(filename, ATTR_NONE, ATTR_SUBDIRECTORY|ATTR_VOLUMELABEL, file); |
1991 | UnlockFilePointer(file); |
2014 | UnlockFilePointer(file); |
1992 | return(exist); |
2015 | return(exist); |
1993 | } |
2016 | } |
1994 | 2017 | ||
1995 | /****************************************************************************************************************************************/ |
2018 | /****************************************************************************************************************************************/ |
1996 | /* Function: feof_(File_t *File); */ |
2019 | /* Function: feof_(File_t *File); */ |
1997 | /* */ |
2020 | /* */ |
1998 | /* Description: This function checks wether the end of the file has been reached. */ |
2021 | /* Description: This function checks wether the end of the file has been reached. */ |
1999 | /* */ |
2022 | /* */ |
2000 | /* Returnvalue: 0 if the end of the file was not reached otherwise 1. */ |
2023 | /* Returnvalue: 0 if the end of the file was not reached otherwise 1. */ |
2001 | /****************************************************************************************************************************************/ |
2024 | /****************************************************************************************************************************************/ |
2002 | u8 feof_(File_t *file) |
2025 | u8 feof_(File_t *file) |
2003 | { |
2026 | { |
2004 | if(((file->Position)+1) < (file->Size)) |
2027 | if(((file->Position)+1) < (file->Size)) |
2005 | { |
2028 | { |
2006 | return(0); |
2029 | return(0); |
2007 | } |
2030 | } |
2008 | else |
2031 | else |
2009 | { |
2032 | { |
2010 | return(1); |
2033 | return(1); |
2011 | } |
2034 | } |
2012 | } |
2035 | } |
2013 | 2036 | ||
2014 | /****************************************************************************************************************************************************/ |
2037 | /****************************************************************************************************************************************************/ |
2015 | /* Function: s8* FAT16_GetVolumeLabel(void) */ |
2038 | /* Function: s8* FAT16_GetVolumeLabel(void) */ |
2016 | /* */ |
2039 | /* */ |
2017 | /* Description: This function returns the volume label */ |
2040 | /* Description: This function returns the volume label */ |
2018 | /* */ |
2041 | /* */ |
2019 | /* Returnvalue: This function returns the pointer to the volume label or NULL if not found. */ |
2042 | /* Returnvalue: This function returns the pointer to the volume label or NULL if not found. */ |
2020 | /****************************************************************************************************************************************************/ |
2043 | /****************************************************************************************************************************************************/ |
2021 | s8* FAT16_GetVolumeLabel(void) |
2044 | s8* FAT16_GetVolumeLabel(void) |
2022 | { |
2045 | { |
2023 | s8 *pVolumeLabel = NULL; |
2046 | s8 *pVolumeLabel = NULL; |
2024 | u32 dir_sector, max_dir_sector, curr_sector; |
2047 | u32 dir_sector, max_dir_sector, curr_sector; |
2025 | u16 dir_entry = 0; |
2048 | u16 dir_entry = 0; |
2026 | u8 i = 0; |
2049 | u8 i = 0; |
2027 | 2050 | ||
2028 | DirEntry_t * dir; |
2051 | DirEntry_t * dir; |
2029 | File_t *file = NULL; |
2052 | File_t *file = NULL; |
2030 | 2053 | ||
2031 | // if Partition is not valud return NULL |
2054 | // if Partition is not valud return NULL |
2032 | if(!Partition.IsValid) return(pVolumeLabel); |
2055 | if(!Partition.IsValid) return(pVolumeLabel); |
2033 | // if Volume label was read before return it |
2056 | // if Volume label was read before return it |
2034 | if(Partition.VolumeLabel[0]!= '\0') return (Partition.VolumeLabel); |
2057 | if(Partition.VolumeLabel[0]!= '\0') return (Partition.VolumeLabel); |
2035 | // try to catch a file pointer |
2058 | // try to catch a file pointer |
2036 | file = LockFilePointer(); |
2059 | file = LockFilePointer(); |
2037 | if(file == NULL) return(pVolumeLabel); |
2060 | if(file == NULL) return(pVolumeLabel); |
2038 | // search dir entries direct within the root directory area |
2061 | // search dir entries direct within the root directory area |
2039 | file->DirectorySector = 0; |
2062 | file->DirectorySector = 0; |
2040 | max_dir_sector = (Partition.MaxRootEntries * DIRENTRY_SIZE)/BYTES_PER_SECTOR; |
2063 | max_dir_sector = (Partition.MaxRootEntries * DIRENTRY_SIZE)/BYTES_PER_SECTOR; |
2041 | file->FirstSectorOfFirstCluster = Partition.FirstRootDirSector; |
2064 | file->FirstSectorOfFirstCluster = Partition.FirstRootDirSector; |
2042 | 2065 | ||
2043 | // update current file data area position to start of first cluster |
2066 | // update current file data area position to start of first cluster |
2044 | file->FirstSectorOfCurrCluster = file->FirstSectorOfFirstCluster; |
2067 | file->FirstSectorOfCurrCluster = file->FirstSectorOfFirstCluster; |
2045 | file->SectorOfCurrCluster = 0; |
2068 | file->SectorOfCurrCluster = 0; |
2046 | file->ByteOfCurrSector = 0; |
2069 | file->ByteOfCurrSector = 0; |
2047 | 2070 | ||
2048 | dir_sector = 0; // reset sector counter within a new cluster |
2071 | dir_sector = 0; // reset sector counter within a new cluster |
2049 | do // loop over all sectors of the root directory |
2072 | do // loop over all sectors of the root directory |
2050 | { |
2073 | { |
2051 | curr_sector = file->FirstSectorOfCurrCluster + dir_sector; // calculate sector number |
2074 | curr_sector = file->FirstSectorOfCurrCluster + dir_sector; // calculate sector number |
2052 | file->SectorInCache = curr_sector; // upate the sector number of file cache. |
2075 | file->SectorInCache = curr_sector; // upate the sector number of file cache. |
2053 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read the sector |
2076 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read the sector |
2054 | { |
2077 | { |
2055 | Fat16_Deinit(); |
2078 | Fat16_Deinit(); |
2056 | return(NULL); |
2079 | return(NULL); |
2057 | } |
2080 | } |
2058 | dir = (DirEntry_t *)file->Cache; // set pointer to directory |
2081 | dir = (DirEntry_t *)file->Cache; // set pointer to directory |
2059 | // search all directory entries within that sector |
2082 | // search all directory entries within that sector |
2060 | for(dir_entry = 0; dir_entry < DIRENTRIES_PER_SECTOR; dir_entry++) |
2083 | for(dir_entry = 0; dir_entry < DIRENTRIES_PER_SECTOR; dir_entry++) |
2061 | { // check for existing dir entry |
2084 | { // check for existing dir entry |
2062 | switch((u8)dir[dir_entry].Name[0]) |
2085 | switch((u8)dir[dir_entry].Name[0]) |
2063 | { |
2086 | { |
2064 | case SLOT_EMPTY: |
2087 | case SLOT_EMPTY: |
2065 | case SLOT_DELETED: |
2088 | case SLOT_DELETED: |
2066 | // ignore empty or deleted dir entries |
2089 | // ignore empty or deleted dir entries |
2067 | break; |
2090 | break; |
2068 | default: |
2091 | default: |
2069 | // check attributes for volume label |
2092 | // check attributes for volume label |
2070 | if ((dir[dir_entry].Attribute & ATTR_VOLUMELABEL) != ATTR_VOLUMELABEL) break; // attribute must match |
2093 | if ((dir[dir_entry].Attribute & ATTR_VOLUMELABEL) != ATTR_VOLUMELABEL) break; // attribute must match |
2071 | // (first 11 characters include 8 chars of basename and 3 chars extension.) |
2094 | // (first 11 characters include 8 chars of basename and 3 chars extension.) |
2072 | for(i = 0; i<11;i++) Partition.VolumeLabel[i] = dir[dir_entry].Name[i]; |
2095 | for(i = 0; i<11;i++) Partition.VolumeLabel[i] = dir[dir_entry].Name[i]; |
2073 | Partition.VolumeLabel[11] = '\0'; // terminate string |
2096 | Partition.VolumeLabel[11] = '\0'; // terminate string |
2074 | file->Attribute = dir[dir_entry].Attribute; // store attribute of found dir entry |
2097 | file->Attribute = dir[dir_entry].Attribute; // store attribute of found dir entry |
2075 | file->FirstSectorOfFirstCluster = Fat16ClusterToSector(dir[dir_entry].StartCluster); // set sector of first data cluster |
2098 | file->FirstSectorOfFirstCluster = Fat16ClusterToSector(dir[dir_entry].StartCluster); // set sector of first data cluster |
2076 | file->FirstSectorOfCurrCluster = file->FirstSectorOfFirstCluster; |
2099 | file->FirstSectorOfCurrCluster = file->FirstSectorOfFirstCluster; |
2077 | file->SectorOfCurrCluster = 0; |
2100 | file->SectorOfCurrCluster = 0; |
2078 | file->ByteOfCurrSector = 0; |
2101 | file->ByteOfCurrSector = 0; |
2079 | file->DirectorySector = curr_sector; // current sector |
2102 | file->DirectorySector = curr_sector; // current sector |
2080 | file->DirectoryIndex = dir_entry; // current direntry in current sector |
2103 | file->DirectoryIndex = dir_entry; // current direntry in current sector |
2081 | file->Size = dir[dir_entry].Size; |
2104 | file->Size = dir[dir_entry].Size; |
2082 | dir_entry = DIRENTRIES_PER_SECTOR; // stop for-loop |
2105 | dir_entry = DIRENTRIES_PER_SECTOR; // stop for-loop |
2083 | pVolumeLabel = Partition.VolumeLabel; |
2106 | pVolumeLabel = Partition.VolumeLabel; |
2084 | } // end of first byte of name check |
2107 | } // end of first byte of name check |
2085 | } |
2108 | } |
2086 | dir_sector++; // search next sector |
2109 | dir_sector++; // search next sector |
2087 | // stop if we reached the end of the cluster or the end of the root dir |
2110 | // stop if we reached the end of the cluster or the end of the root dir |
2088 | }while((dir_sector < max_dir_sector) && (!pVolumeLabel)); |
2111 | }while((dir_sector < max_dir_sector) && (!pVolumeLabel)); |
2089 | 2112 | ||
2090 | UnlockFilePointer(file); |
2113 | UnlockFilePointer(file); |
2091 | return(pVolumeLabel); |
2114 | return(pVolumeLabel); |
2092 | } |
2115 | } |
2093 | 2116 | ||
2094 | 2117 | ||
2095 | 2118 | ||
2096 | #define ATTR_NONE 0x00 // normal file |
2119 | #define ATTR_NONE 0x00 // normal file |
2097 | #define ATTR_READONLY 0x01 // file is readonly |
2120 | #define ATTR_READONLY 0x01 // file is readonly |
2098 | #define ATTR_HIDDEN 0x02 // file is hidden |
2121 | #define ATTR_HIDDEN 0x02 // file is hidden |
2099 | #define ATTR_SYSTEM 0x04 // file is a system file |
2122 | #define ATTR_SYSTEM 0x04 // file is a system file |
2100 | #define ATTR_VOLUMELABEL 0x08 // entry is a volume label |
2123 | #define ATTR_VOLUMELABEL 0x08 // entry is a volume label |
2101 | #define ATTR_LONG_FILENAME 0x0F // this is a long filename entry |
2124 | #define ATTR_LONG_FILENAME 0x0F // this is a long filename entry |
2102 | #define ATTR_SUBDIRECTORY 0x10 // entry is a directory name |
2125 | #define ATTR_SUBDIRECTORY 0x10 // entry is a directory name |
2103 | #define ATTR_ARCHIVE 0x20 // file is new or modified |
2126 | #define ATTR_ARCHIVE 0x20 // file is new or modified |
2104 | 2127 | ||
2105 | 2128 | ||
2106 | /********************************************************************************************************************************************/ |
2129 | /********************************************************************************************************************************************/ |
2107 | /* Function: u8 FindItem(Find_t); */ |
2130 | /* Function: u8 FindItem(Find_t); */ |
2108 | /* */ |
2131 | /* */ |
2109 | /* Description: This function looks for the item specified by global structure FindElement in the actual directory */ |
2132 | /* Description: This function looks for the item specified by global structure FindElement in the actual directory */ |
2110 | /* */ |
2133 | /* */ |
2111 | /* */ |
2134 | /* */ |
2112 | /* Returnvalue: TRUE if an matching element was found */ |
2135 | /* Returnvalue: TRUE if an matching element was found */ |
2113 | /********************************************************************************************************************************************/ |
2136 | /********************************************************************************************************************************************/ |
2114 | 2137 | ||
2115 | u8 FindItem(Find_t *findelement) |
2138 | u8 FindItem(Find_t *findelement) |
2116 | { |
2139 | { |
2117 | u16 index = 0; |
2140 | u16 index = 0; |
2118 | u16 max_dir_sector = 0; |
2141 | u16 max_dir_sector = 0; |
2119 | u16 end_of_directory_not_reached = 1; // the directory has been read completely without a result. |
2142 | u16 end_of_directory_not_reached = 1; // the directory has been read completely without a result. |
2120 | u8 i = 0; |
2143 | u8 i = 0; |
2121 | u8 readpointer = 0; |
2144 | u8 readpointer = 0; |
2122 | u8 writepointer = 0; |
2145 | u8 writepointer = 0; |
2123 | u8 retvalue = 0; |
2146 | u8 retvalue = 0; |
2124 | DirEntry_t *DirectoryEntry; |
2147 | DirEntry_t *DirectoryEntry; |
2125 | File_t file; |
2148 | File_t file; |
2126 | 2149 | ||
2127 | file.FirstSectorOfCurrCluster = findelement->fp.FirstSectorOfCurrCluster; |
2150 | file.FirstSectorOfCurrCluster = findelement->fp.FirstSectorOfCurrCluster; |
2128 | file.SectorOfCurrCluster = findelement->fp.SectorOfCurrCluster; |
2151 | file.SectorOfCurrCluster = findelement->fp.SectorOfCurrCluster; |
2129 | index = findelement->fp.DirectoryIndex; |
2152 | index = findelement->fp.DirectoryIndex; |
2130 | 2153 | ||
2131 | // within the root directory area we can read sectors sequentially until the end of this area |
2154 | // within the root directory area we can read sectors sequentially until the end of this area |
2132 | if((Partition.FirstRootDirSector <= file.FirstSectorOfCurrCluster) && (file.FirstSectorOfCurrCluster < Partition.FirstDataSector)) |
2155 | if((Partition.FirstRootDirSector <= file.FirstSectorOfCurrCluster) && (file.FirstSectorOfCurrCluster < Partition.FirstDataSector)) |
2133 | { |
2156 | { |
2134 | max_dir_sector = (Partition.MaxRootEntries * DIRENTRY_SIZE)/BYTES_PER_SECTOR; |
2157 | max_dir_sector = (Partition.MaxRootEntries * DIRENTRY_SIZE)/BYTES_PER_SECTOR; |
2135 | } |
2158 | } |
2136 | // within the data clusters we can read sectors sequentially only within the cluster |
2159 | // within the data clusters we can read sectors sequentially only within the cluster |
2137 | else if((Partition.FirstDataSector <= file.FirstSectorOfCurrCluster) && (file.FirstSectorOfCurrCluster <= Partition.LastDataSector)) |
2160 | else if((Partition.FirstDataSector <= file.FirstSectorOfCurrCluster) && (file.FirstSectorOfCurrCluster <= Partition.LastDataSector)) |
2138 | { |
2161 | { |
2139 | max_dir_sector = Partition.SectorsPerCluster; // limit max secters before next cluster |
2162 | max_dir_sector = Partition.SectorsPerCluster; // limit max secters before next cluster |
2140 | } |
2163 | } |
2141 | 2164 | ||
2142 | do |
2165 | do |
2143 | { // search the next 16 rootentries in this sector of the roordirectory. |
2166 | { // search the next 16 rootentries in this sector of the roordirectory. |
2144 | if(SD_SUCCESS != SDC_GetSector(((u32) file.FirstSectorOfCurrCluster + (u32)file.SectorOfCurrCluster), file.Cache)); // Read the Rootdirectory. |
2167 | if(SD_SUCCESS != SDC_GetSector(((u32) file.FirstSectorOfCurrCluster + (u32)file.SectorOfCurrCluster), file.Cache)); // Read the Rootdirectory. |
2145 | { |
2168 | { |
2146 | Fat16_Deinit(); |
2169 | Fat16_Deinit(); |
2147 | return(0); |
2170 | return(0); |
2148 | } |
2171 | } |
2149 | 2172 | ||
2150 | DirectoryEntry = (DirEntry_t *)file.Cache; |
2173 | DirectoryEntry = (DirEntry_t *)file.Cache; |
2151 | 2174 | ||
2152 | while((!retvalue)&&(index<16)) |
2175 | while((!retvalue)&&(index<16)) |
2153 | { |
2176 | { |
2154 | i=0; |
2177 | i=0; |
2155 | if((u8) DirectoryEntry[index].Name[0] != 0xe5) // ignore deleted items. |
2178 | if((u8) DirectoryEntry[index].Name[0] != 0xe5) // ignore deleted items. |
2156 | { |
2179 | { |
2157 | while((i<=10)&&((DirectoryEntry[index].Name[i] == findelement->searchstring[i]) || (findelement->searchstring[i]=='*') || findelement->searchstring[i]=='?')) |
2180 | while((i<=10)&&((DirectoryEntry[index].Name[i] == findelement->searchstring[i]) || (findelement->searchstring[i]=='*') || findelement->searchstring[i]=='?')) |
2158 | { |
2181 | { |
2159 | i++; |
2182 | i++; |
2160 | } |
2183 | } |
2161 | } |
2184 | } |
2162 | if((DirectoryEntry[index].Attribute <= 0x30) && (DirectoryEntry[index].Attribute & findelement->attribmask) && (i==11)) |
2185 | if((DirectoryEntry[index].Attribute <= 0x30) && (DirectoryEntry[index].Attribute & findelement->attribmask) && (i==11)) |
2163 | { |
2186 | { |
2164 | for(readpointer=0;readpointer<=10;readpointer++) |
2187 | for(readpointer=0;readpointer<=10;readpointer++) |
2165 | { |
2188 | { |
2166 | if((DirectoryEntry[index].Name[readpointer] != ' ') && (readpointer!=8)) |
2189 | if((DirectoryEntry[index].Name[readpointer] != ' ') && (readpointer!=8)) |
2167 | { |
2190 | { |
2168 | findelement->name[writepointer] = DirectoryEntry[index].Name[readpointer]; // copy the name of the item found to the find_structure. |
2191 | findelement->name[writepointer] = DirectoryEntry[index].Name[readpointer]; // copy the name of the item found to the find_structure. |
2169 | writepointer++; |
2192 | writepointer++; |
2170 | } |
2193 | } |
2171 | else |
2194 | else |
2172 | { |
2195 | { |
2173 | if(DirectoryEntry[index].Attribute == ATTR_ARCHIVE) |
2196 | if(DirectoryEntry[index].Attribute == ATTR_ARCHIVE) |
2174 | { |
2197 | { |
2175 | if(readpointer < 8) readpointer=8; |
2198 | if(readpointer < 8) readpointer=8; |
2176 | if(DirectoryEntry[index].Name[readpointer] != ' ') |
2199 | if(DirectoryEntry[index].Name[readpointer] != ' ') |
2177 | { |
2200 | { |
2178 | findelement->name[writepointer] = '.'; // then seperate the name and the extension by a '.' at index 8. |
2201 | findelement->name[writepointer] = '.'; // then seperate the name and the extension by a '.' at index 8. |
2179 | writepointer++; |
2202 | writepointer++; |
2180 | findelement->name[writepointer] = DirectoryEntry[index].Name[readpointer]; // copy the name of the item found to the find_structure. |
2203 | findelement->name[writepointer] = DirectoryEntry[index].Name[readpointer]; // copy the name of the item found to the find_structure. |
2181 | writepointer++; |
2204 | writepointer++; |
2182 | } |
2205 | } |
2183 | else break; |
2206 | else break; |
2184 | } |
2207 | } |
2185 | else break; |
2208 | else break; |
2186 | } |
2209 | } |
2187 | /* terminate the namestring with 0 for debugpurposes*/ |
2210 | /* terminate the namestring with 0 for debugpurposes*/ |
2188 | findelement->name[12] = 0; |
2211 | findelement->name[12] = 0; |
2189 | } |
2212 | } |
2190 | findelement->fp.FirstSectorOfFirstCluster = (u32) DirectoryEntry[index].StartCluster; |
2213 | findelement->fp.FirstSectorOfFirstCluster = (u32) DirectoryEntry[index].StartCluster; |
2191 | findelement->fp.DirectoryIndex = index; |
2214 | findelement->fp.DirectoryIndex = index; |
2192 | findelement->fp.FirstSectorOfCurrCluster = file.FirstSectorOfCurrCluster; |
2215 | findelement->fp.FirstSectorOfCurrCluster = file.FirstSectorOfCurrCluster; |
2193 | findelement->fp.DirectorySector = (file.FirstSectorOfCurrCluster + file.SectorOfCurrCluster); |
2216 | findelement->fp.DirectorySector = (file.FirstSectorOfCurrCluster + file.SectorOfCurrCluster); |
2194 | findelement->fp.SectorOfCurrCluster = file.SectorOfCurrCluster; |
2217 | findelement->fp.SectorOfCurrCluster = file.SectorOfCurrCluster; |
2195 | findelement->fp.Size = DirectoryEntry[index].Size; |
2218 | findelement->fp.Size = DirectoryEntry[index].Size; |
2196 | findelement->fp.Attribute = DirectoryEntry[index].Attribute; |
2219 | findelement->fp.Attribute = DirectoryEntry[index].Attribute; |
2197 | retvalue = 1; |
2220 | retvalue = 1; |
2198 | } |
2221 | } |
2199 | /* search the next sector */ |
2222 | /* search the next sector */ |
2200 | index++; |
2223 | index++; |
2201 | } |
2224 | } |
2202 | /* this sector has been searched but we havn't found what we are looking for. Therefore we have to find the next sector */ |
2225 | /* this sector has been searched but we havn't found what we are looking for. Therefore we have to find the next sector */ |
2203 | if(!retvalue) // file not found in this sector so take next sector. |
2226 | if(!retvalue) // file not found in this sector so take next sector. |
2204 | { |
2227 | { |
2205 | /* in the next sector we start looking for the specified entry beginning at index 0 */ |
2228 | /* in the next sector we start looking for the specified entry beginning at index 0 */ |
2206 | index = 0; |
2229 | index = 0; |
2207 | /* there are still sectors to be read within the cluster or within the linear addresspace of the rootdirectory */ |
2230 | /* there are still sectors to be read within the cluster or within the linear addresspace of the rootdirectory */ |
2208 | if(file.SectorOfCurrCluster < max_dir_sector-1) file.SectorOfCurrCluster++; else end_of_directory_not_reached = 0; |
2231 | if(file.SectorOfCurrCluster < max_dir_sector-1) file.SectorOfCurrCluster++; else end_of_directory_not_reached = 0; |
2209 | /* if we are looking for an directoryentry outside the rootdirectory and have reached the end of the cluster we have to get the next one */ |
2232 | /* if we are looking for an directoryentry outside the rootdirectory and have reached the end of the cluster we have to get the next one */ |
2210 | if(Partition.FirstDataSector <= file.FirstSectorOfCurrCluster) |
2233 | if(Partition.FirstDataSector <= file.FirstSectorOfCurrCluster) |
2211 | { |
2234 | { |
2212 | end_of_directory_not_reached = GetNextCluster(&file); |
2235 | end_of_directory_not_reached = GetNextCluster(&file); |
2213 | } |
2236 | } |
2214 | } |
2237 | } |
2215 | } |
2238 | } |
2216 | while((end_of_directory_not_reached) && (!retvalue) ); |
2239 | while((end_of_directory_not_reached) && (!retvalue) ); |
2217 | 2240 | ||
2218 | return(retvalue); |
2241 | return(retvalue); |
2219 | } |
2242 | } |
2220 | 2243 | ||
2221 | 2244 | ||
2222 | 2245 | ||
2223 | 2246 | ||
2224 | /********************************************************************************************************************************************/ |
2247 | /********************************************************************************************************************************************/ |
2225 | /* Function: findnext_(Find_t *); */ |
2248 | /* Function: findnext_(Find_t *); */ |
2226 | /* */ |
2249 | /* */ |
2227 | /* Description: This function looks for the next item in the specified directory with a matching filename and fileattributes specified */ |
2250 | /* Description: This function looks for the next item in the specified directory with a matching filename and fileattributes specified */ |
2228 | /* by function findfirst() */ |
2251 | /* by function findfirst() */ |
2229 | /* */ |
2252 | /* */ |
2230 | /* Returnvalue: */ |
2253 | /* Returnvalue: */ |
2231 | /********************************************************************************************************************************************/ |
2254 | /********************************************************************************************************************************************/ |
2232 | u8 findnext_(Find_t * findelement) |
2255 | u8 findnext_(Find_t * findelement) |
2233 | { |
2256 | { |
2234 | u8 itemfound = 0; |
2257 | u8 itemfound = 0; |
2235 | u8 index = 0; |
2258 | u8 index = 0; |
2236 | 2259 | ||
2237 | findelement->fp.DirectoryIndex++; |
2260 | findelement->fp.DirectoryIndex++; |
2238 | 2261 | ||
2239 | /* before we start searching an element we clear the complete namestring within the structure FindElement */ |
2262 | /* before we start searching an element we clear the complete namestring within the structure FindElement */ |
2240 | for(index=0;index<11;index++) findelement->name[index] = 0; |
2263 | for(index=0;index<11;index++) findelement->name[index] = 0; |
2241 | 2264 | ||
2242 | if(FindItem(findelement)) |
2265 | if(FindItem(findelement)) |
2243 | { |
2266 | { |
2244 | itemfound = 1; |
2267 | itemfound = 1; |
2245 | } |
2268 | } |
2246 | 2269 | ||
2247 | return(itemfound); |
2270 | return(itemfound); |
2248 | } |
2271 | } |
2249 | 2272 | ||
2250 | 2273 | ||
2251 | 2274 | ||
2252 | /********************************************************************************************************************************************/ |
2275 | /********************************************************************************************************************************************/ |
2253 | /* Function: findfirst_(s8* filename, u8 attribmask, Find_t *); */ |
2276 | /* Function: findfirst_(s8* filename, u8 attribmask, Find_t *); */ |
2254 | /* */ |
2277 | /* */ |
2255 | /* Description: This function looks for the first item in the specified directory with a matching filename and fileattributes */ |
2278 | /* Description: This function looks for the first item in the specified directory with a matching filename and fileattributes */ |
2256 | /* The filename of the element found is transformed from 8.3 to a string */ |
2279 | /* The filename of the element found is transformed from 8.3 to a string */ |
2257 | /* */ |
2280 | /* */ |
2258 | /* */ |
2281 | /* */ |
2259 | /* Returnvalue: (1) if Element was found. (0) if no valid element was found */ |
2282 | /* Returnvalue: (1) if Element was found. (0) if no valid element was found */ |
2260 | /********************************************************************************************************************************************/ |
2283 | /********************************************************************************************************************************************/ |
2261 | u8 findfirst_(const s8* name, u8 attribmask, Find_t *findelement) |
2284 | u8 findfirst_(const s8* name, u8 attribmask, Find_t *findelement) |
2262 | { |
2285 | { |
2263 | u8 itemfound = 0; |
2286 | u8 itemfound = 0; |
2264 | u8 index = 0; |
2287 | u8 index = 0; |
2265 | 2288 | ||
2266 | /* initialize the FindElement structure */ |
2289 | /* initialize the FindElement structure */ |
2267 | findelement->fp.FirstSectorOfFirstCluster = 0; // First sector of the first cluster of the file. |
2290 | findelement->fp.FirstSectorOfFirstCluster = 0; // First sector of the first cluster of the file. |
2268 | findelement->fp.FirstSectorOfCurrCluster = Partition.CurrentWorkingDirectory; // First sector of the cluster which is edited at the moment. |
2291 | findelement->fp.FirstSectorOfCurrCluster = Partition.CurrentWorkingDirectory; // First sector of the cluster which is edited at the moment. |
2269 | findelement->fp.SectorOfCurrCluster = 0; // The sector within the current cluster. |
2292 | findelement->fp.SectorOfCurrCluster = 0; // The sector within the current cluster. |
2270 | findelement->fp.ByteOfCurrSector = 0; // The byte location within the current sector. |
2293 | findelement->fp.ByteOfCurrSector = 0; // The byte location within the current sector. |
2271 | findelement->fp.Size = 0; // The size of the opend file in bytes. |
2294 | findelement->fp.Size = 0; // The size of the opend file in bytes. |
2272 | findelement->fp.Position = 0; // Pointer to a character within the file 0 < fileposition < filesize |
2295 | findelement->fp.Position = 0; // Pointer to a character within the file 0 < fileposition < filesize |
2273 | findelement->fp.DirectorySector = 0; // the sectorposition where the directoryentry has been made. |
2296 | findelement->fp.DirectorySector = 0; // the sectorposition where the directoryentry has been made. |
2274 | findelement->fp.DirectoryIndex = 0; // The index to the directoryentry within the specified sector. |
2297 | findelement->fp.DirectoryIndex = 0; // The index to the directoryentry within the specified sector. |
2275 | findelement->attribfilter = 0; |
2298 | findelement->attribfilter = 0; |
2276 | findelement->attribmask = attribmask; |
2299 | findelement->attribmask = attribmask; |
2277 | findelement->searchstring[0]=0; |
2300 | findelement->searchstring[0]=0; |
2278 | 2301 | ||
2279 | /* seperate the name of the element to be found from the filepath and bring it to the 8.3*/ |
2302 | /* seperate the name of the element to be found from the filepath and bring it to the 8.3*/ |
2280 | SeperateDirName(name, findelement->searchstring); |
2303 | SeperateDirName(name, findelement->searchstring); |
2281 | /* after the name of the element is in 8.3 we process the wildcards (*). After an * all following character are wildcards to */ |
2304 | /* after the name of the element is in 8.3 we process the wildcards (*). After an * all following character are wildcards to */ |
2282 | for(index=0;index<8;index++) |
2305 | for(index=0;index<8;index++) |
2283 | { |
2306 | { |
2284 | /* if we find an wildcard within the name of the searchstring all remaining character after the wildcard shall be wildcards also */ |
2307 | /* if we find an wildcard within the name of the searchstring all remaining character after the wildcard shall be wildcards also */ |
2285 | if(findelement->searchstring[index] == '*') |
2308 | if(findelement->searchstring[index] == '*') |
2286 | { |
2309 | { |
2287 | /* */ |
2310 | /* */ |
2288 | while(++index <8) findelement->searchstring[index] = '*'; |
2311 | while(++index <8) findelement->searchstring[index] = '*'; |
2289 | } |
2312 | } |
2290 | } |
2313 | } |
2291 | for(index=8;index<11;index++) |
2314 | for(index=8;index<11;index++) |
2292 | { |
2315 | { |
2293 | /* if we find an wildcard within the name of the searchstring all remaining character after the wildcard shall be wildcards also */ |
2316 | /* if we find an wildcard within the name of the searchstring all remaining character after the wildcard shall be wildcards also */ |
2294 | if(findelement->searchstring[index] == '*') |
2317 | if(findelement->searchstring[index] == '*') |
2295 | { |
2318 | { |
2296 | /* */ |
2319 | /* */ |
2297 | while(++index <11) findelement->searchstring[index] = '*'; |
2320 | while(++index <11) findelement->searchstring[index] = '*'; |
2298 | } |
2321 | } |
2299 | } |
2322 | } |
2300 | 2323 | ||
2301 | /* the value of ...DirectoryIndex will be incremented in findnext_() thererfore it has to be decremented in findfirst_() */ |
2324 | /* the value of ...DirectoryIndex will be incremented in findnext_() thererfore it has to be decremented in findfirst_() */ |
2302 | findelement->fp.DirectoryIndex--; |
2325 | findelement->fp.DirectoryIndex--; |
2303 | /* now lets search for the item within the direcory */ |
2326 | /* now lets search for the item within the direcory */ |
2304 | itemfound = findnext_(findelement); |
2327 | itemfound = findnext_(findelement); |
2305 | 2328 | ||
2306 | return(itemfound); |
2329 | return(itemfound); |
2307 | } |
2330 | } |
2308 | 2331 | ||
2309 | 2332 | ||
2310 | /********************************************************************************************************************************************/ |
2333 | /********************************************************************************************************************************************/ |
2311 | /* Function: u8 GetDirCount(s8* filepath); */ |
2334 | /* Function: u8 GetDirCount(s8* filepath); */ |
2312 | /* */ |
2335 | /* */ |
2313 | /* Description: This function counts the number of subdirectories the dirpath contains */ |
2336 | /* Description: This function counts the number of subdirectories the dirpath contains */ |
2314 | /* */ |
2337 | /* */ |
2315 | /* */ |
2338 | /* */ |
2316 | /* Returnvalue: then number of subdirectories within the specified path */ |
2339 | /* Returnvalue: then number of subdirectories within the specified path */ |
2317 | /********************************************************************************************************************************************/ |
2340 | /********************************************************************************************************************************************/ |
2318 | u8 GetDirCount(u8 *dirpath) |
2341 | u8 GetDirCount(u8 *dirpath) |
2319 | { |
2342 | { |
2320 | u8 i=0; |
2343 | u8 i=0; |
2321 | u8 cnt=0; |
2344 | u8 cnt=0; |
2322 | 2345 | ||
2323 | while(dirpath[i] != 0 ) |
2346 | while(dirpath[i] != 0 ) |
2324 | { |
2347 | { |
2325 | if(dirpath[i]=='/') |
2348 | if(dirpath[i]=='/') |
2326 | { |
2349 | { |
2327 | if(dirpath[i+1]!=0) cnt++; // ignore last'/' |
2350 | if(dirpath[i+1]!=0) cnt++; // ignore last'/' |
2328 | } |
2351 | } |
2329 | i++; |
2352 | i++; |
2330 | } |
2353 | } |
2331 | i=0; |
2354 | i=0; |
2332 | return(cnt); |
2355 | return(cnt); |
2333 | } |
2356 | } |
2334 | 2357 | ||
2335 | 2358 | ||
2336 | /********************************************************************************************************************************************/ |
2359 | /********************************************************************************************************************************************/ |
2337 | /* Funtion: char *GetSubDirectory (char *dirpath, char *directory) */ |
2360 | /* Funtion: char *GetSubDirectory (char *dirpath, char *directory) */ |
2338 | /* */ |
2361 | /* */ |
2339 | /* Description: this function returns a pointer to the beginning of the next subdirectory or NULL */ |
2362 | /* Description: this function returns a pointer to the beginning of the next subdirectory or NULL */ |
2340 | /* */ |
2363 | /* */ |
2341 | /* */ |
2364 | /* */ |
2342 | /* returnvalue: number of subdirectories in the filepath */ |
2365 | /* returnvalue: number of subdirectories in the filepath */ |
2343 | /********************************************************************************************************************************************/ |
2366 | /********************************************************************************************************************************************/ |
2344 | u8 * GetSubDirectory(u8 *dirpath, u8 *directory) |
2367 | u8 * GetSubDirectory(u8 *dirpath, u8 *directory) |
2345 | { |
2368 | { |
2346 | u8 *cptr = dirpath; |
2369 | u8 *cptr = dirpath; |
2347 | u8 *dptr = directory; |
2370 | u8 *dptr = directory; |
2348 | u8 *retvalue = NULL; |
2371 | u8 *retvalue = NULL; |
2349 | 2372 | ||
2350 | /* if the first character of the path is an '/' we go to the next character */ |
2373 | /* if the first character of the path is an '/' we go to the next character */ |
2351 | if(*cptr == '/') cptr++; |
2374 | if(*cptr == '/') cptr++; |
2352 | /* search end of path or subdirectory*/ |
2375 | /* search end of path or subdirectory*/ |
2353 | while((*cptr != 0) && (*cptr != '/')) |
2376 | while((*cptr != 0) && (*cptr != '/')) |
2354 | { |
2377 | { |
2355 | *dptr = *cptr; |
2378 | *dptr = *cptr; |
2356 | dptr++; |
2379 | dptr++; |
2357 | cptr++; |
2380 | cptr++; |
2358 | } |
2381 | } |
2359 | if(*cptr!=0) retvalue = ++cptr; |
2382 | if(*cptr!=0) retvalue = ++cptr; |
2360 | *dptr = 0; |
2383 | *dptr = 0; |
2361 | 2384 | ||
2362 | return(retvalue); |
2385 | return(retvalue); |
2363 | } |
2386 | } |
2364 | 2387 | ||
2365 | /********************************************************************************************************************************************/ |
2388 | /********************************************************************************************************************************************/ |
2366 | /* Function: s8 *GetPath(void); */ |
2389 | /* Function: s8 *GetPath(void); */ |
2367 | /* */ |
2390 | /* */ |
2368 | /* Description: This function function returns a pointer to the absolute path of the active partition */ |
2391 | /* Description: This function function returns a pointer to the absolute path of the active partition */ |
2369 | /* */ |
2392 | /* */ |
2370 | /* */ |
2393 | /* */ |
2371 | /* Returnvalue: */ |
2394 | /* Returnvalue: */ |
2372 | /********************************************************************************************************************************************/ |
2395 | /********************************************************************************************************************************************/ |
2373 | 2396 | ||
2374 | s8 *GetPath(void) |
2397 | s8 *GetPath(void) |
2375 | { |
2398 | { |
2376 | return(Partition.PathToCwd); |
2399 | return(Partition.PathToCwd); |
2377 | } |
2400 | } |
2378 | 2401 | ||
2379 | /********************************************************************************************************************************************/ |
2402 | /********************************************************************************************************************************************/ |
2380 | /* Function: void SetPathToRoot(void); */ |
2403 | /* Function: void SetPathToRoot(void); */ |
2381 | /* */ |
2404 | /* */ |
2382 | /* Description: This function sets the path to the rootdirectory */ |
2405 | /* Description: This function sets the path to the rootdirectory */ |
2383 | /* */ |
2406 | /* */ |
2384 | /* */ |
2407 | /* */ |
2385 | /* Returnvalue: */ |
2408 | /* Returnvalue: */ |
2386 | /********************************************************************************************************************************************/ |
2409 | /********************************************************************************************************************************************/ |
2387 | 2410 | ||
2388 | void SetPathToRoot(void) |
2411 | void SetPathToRoot(void) |
2389 | { |
2412 | { |
2390 | /* lets point to the rootdirectory */ |
2413 | /* lets point to the rootdirectory */ |
2391 | strcpy(Partition.PathToCwd, "/"); |
2414 | strcpy(Partition.PathToCwd, "/"); |
2392 | } |
2415 | } |
2393 | 2416 | ||
2394 | /********************************************************************************************************************************************/ |
2417 | /********************************************************************************************************************************************/ |
2395 | /* Function: void AppendDirToPath(s8* directory); */ |
2418 | /* Function: void AppendDirToPath(s8* directory); */ |
2396 | /* */ |
2419 | /* */ |
2397 | /* Description: This function function appends the name of an directory to the Path to the CWD */ |
2420 | /* Description: This function function appends the name of an directory to the Path to the CWD */ |
2398 | /* */ |
2421 | /* */ |
2399 | /* */ |
2422 | /* */ |
2400 | /* Returnvalue: */ |
2423 | /* Returnvalue: */ |
2401 | /********************************************************************************************************************************************/ |
2424 | /********************************************************************************************************************************************/ |
2402 | 2425 | ||
2403 | void AppendDirToPath(s8* directory) |
2426 | void AppendDirToPath(s8* directory) |
2404 | { |
2427 | { |
2405 | /* append the name of the directory to the path */ |
2428 | /* append the name of the directory to the path */ |
2406 | strcat(Partition.PathToCwd, directory); |
2429 | strcat(Partition.PathToCwd, directory); |
2407 | /* append a '/' after the directoryname */ |
2430 | /* append a '/' after the directoryname */ |
2408 | strcat(Partition.PathToCwd, "/"); |
2431 | strcat(Partition.PathToCwd, "/"); |
2409 | } |
2432 | } |
2410 | 2433 | ||
2411 | /********************************************************************************************************************************************/ |
2434 | /********************************************************************************************************************************************/ |
2412 | /* Function: RemoveLastDirFromPath(void); */ |
2435 | /* Function: RemoveLastDirFromPath(void); */ |
2413 | /* */ |
2436 | /* */ |
2414 | /* Description: This function removes the last directory from the path to the cwd */ |
2437 | /* Description: This function removes the last directory from the path to the cwd */ |
2415 | /* */ |
2438 | /* */ |
2416 | /* */ |
2439 | /* */ |
2417 | /* Returnvalue: */ |
2440 | /* Returnvalue: */ |
2418 | /********************************************************************************************************************************************/ |
2441 | /********************************************************************************************************************************************/ |
2419 | 2442 | ||
2420 | void RemoveLastDirFromPath(void) |
2443 | void RemoveLastDirFromPath(void) |
2421 | { |
2444 | { |
2422 | /* a pointer to the beginning of the absolute path to the cwd */ |
2445 | /* a pointer to the beginning of the absolute path to the cwd */ |
2423 | s8 * cptr = Partition.PathToCwd; |
2446 | s8 * cptr = Partition.PathToCwd; |
2424 | /* lets find the end of the path to the cwd */ |
2447 | /* lets find the end of the path to the cwd */ |
2425 | while(*cptr != 0 ) cptr++; |
2448 | while(*cptr != 0 ) cptr++; |
2426 | /* if the path is terminated with an '/' */ |
2449 | /* if the path is terminated with an '/' */ |
2427 | if((*(cptr-1)) == '/') *(cptr-1)=0; |
2450 | if((*(cptr-1)) == '/') *(cptr-1)=0; |
2428 | /* now lets find the beginning of the last directorientry */ |
2451 | /* now lets find the beginning of the last directorientry */ |
2429 | while((*cptr != '/' ) && cptr > Partition.PathToCwd) cptr--; |
2452 | while((*cptr != '/' ) && cptr > Partition.PathToCwd) cptr--; |
2430 | /* is there one subdirectory left within the path? */ |
2453 | /* is there one subdirectory left within the path? */ |
2431 | if(cptr > Partition.PathToCwd) |
2454 | if(cptr > Partition.PathToCwd) |
2432 | { |
2455 | { |
2433 | /* we delete the direntry by terminating the path with 0 */ |
2456 | /* we delete the direntry by terminating the path with 0 */ |
2434 | *cptr = 0; |
2457 | *cptr = 0; |
2435 | } |
2458 | } |
2436 | /* there is no subdirectory left within the path. Therefore we create the root instead. */ |
2459 | /* there is no subdirectory left within the path. Therefore we create the root instead. */ |
2437 | else |
2460 | else |
2438 | { |
2461 | { |
2439 | *cptr = '/'; |
2462 | *cptr = '/'; |
2440 | *(cptr+1) = 0; |
2463 | *(cptr+1) = 0; |
2441 | } |
2464 | } |
2442 | } |
2465 | } |
2443 | 2466 | ||
2444 | /********************************************************************************************************************************************/ |
2467 | /********************************************************************************************************************************************/ |
2445 | /* Function: chdir_(s8* filepath); */ |
2468 | /* Function: chdir_(s8* filepath); */ |
2446 | /* */ |
2469 | /* */ |
2447 | /* Description: This function changed the current working directory to the directory specified by the filepath */ |
2470 | /* Description: This function changed the current working directory to the directory specified by the filepath */ |
2448 | /* by function findfirst() */ |
2471 | /* by function findfirst() */ |
2449 | /* */ |
2472 | /* */ |
2450 | /* Returnvalue: */ |
2473 | /* Returnvalue: */ |
2451 | /********************************************************************************************************************************************/ |
2474 | /********************************************************************************************************************************************/ |
2452 | /* |
2475 | /* |
2453 | #define ATTR_NONE 0x00 // normal file |
2476 | #define ATTR_NONE 0x00 // normal file |
2454 | #define ATTR_READONLY 0x01 // file is readonly |
2477 | #define ATTR_READONLY 0x01 // file is readonly |
2455 | #define ATTR_HIDDEN 0x02 // file is hidden |
2478 | #define ATTR_HIDDEN 0x02 // file is hidden |
2456 | #define ATTR_SYSTEM 0x04 // file is a system file |
2479 | #define ATTR_SYSTEM 0x04 // file is a system file |
2457 | #define ATTR_VOLUMELABEL 0x08 // entry is a volume label |
2480 | #define ATTR_VOLUMELABEL 0x08 // entry is a volume label |
2458 | #define ATTR_LONG_FILENAME 0x0F // this is a long filename entry |
2481 | #define ATTR_LONG_FILENAME 0x0F // this is a long filename entry |
2459 | #define ATTR_SUBDIRECTORY 0x10 // entry is a directory name |
2482 | #define ATTR_SUBDIRECTORY 0x10 // entry is a directory name |
2460 | #define ATTR_ARCHIVE 0x20 // file is new or modified |
2483 | #define ATTR_ARCHIVE 0x20 // file is new or modified |
2461 | */ |
2484 | */ |
2462 | 2485 | ||
2463 | u8 chdir_(s8 *path) |
2486 | u8 chdir_(s8 *path) |
2464 | { |
2487 | { |
2465 | u8 retvalue = 0; // the value returned by this function |
2488 | u8 retvalue = 0; // the value returned by this function |
2466 | u32 ultemp = 0; // temp. variable |
2489 | u32 ultemp = 0; // temp. variable |
2467 | u8 *directory = path; // pointer to a directoryname within the path |
2490 | u8 *directory = path; // pointer to a directoryname within the path |
2468 | u8 dircount = 0; // the number of subdirectoryentries within the path |
2491 | u8 dircount = 0; // the number of subdirectoryentries within the path |
2469 | u8 cache[64]; // a buffer containing the name of the subdirectory we are actually looking for |
2492 | u8 cache[64]; // a buffer containing the name of the subdirectory we are actually looking for |
2470 | Find_t fe; // The findelement needed for function findfirst to find the subdirectoryentry |
2493 | Find_t fe; // The findelement needed for function findfirst to find the subdirectoryentry |
2471 | s8 tp[256]; // temporarily we remember the actual path until the operation has finished successfully |
2494 | s8 tp[256]; // temporarily we remember the actual path until the operation has finished successfully |
2472 | u32 cwdt = 0; |
2495 | u32 cwdt = 0; |
2473 | s8 *cptr; |
2496 | s8 *cptr; |
2474 | 2497 | ||
2475 | /* bring the path into the correct syntax */ |
2498 | /* bring the path into the correct syntax */ |
2476 | cptr = path; |
2499 | cptr = path; |
2477 | /* search the whole string */ |
2500 | /* search the whole string */ |
2478 | while(*cptr != 0 ) |
2501 | while(*cptr != 0 ) |
2479 | { |
2502 | { |
2480 | if(*cptr == '\\') *cptr = '/'; |
2503 | if(*cptr == '\\') *cptr = '/'; |
2481 | cptr++; |
2504 | cptr++; |
2482 | } |
2505 | } |
2483 | /* lets remember the actual path */ |
2506 | /* lets remember the actual path */ |
2484 | strcpy(tp, Partition.PathToCwd); |
2507 | strcpy(tp, Partition.PathToCwd); |
2485 | cwdt = Partition.CurrentWorkingDirectory; |
2508 | cwdt = Partition.CurrentWorkingDirectory; |
2486 | /* how many subdirectories are there within the path? */ |
2509 | /* how many subdirectories are there within the path? */ |
2487 | dircount = GetDirCount(path); |
2510 | dircount = GetDirCount(path); |
2488 | /* if the path is absolute we begin at the rootdirectory */ |
2511 | /* if the path is absolute we begin at the rootdirectory */ |
2489 | if(path[0] == '/') |
2512 | if(path[0] == '/') |
2490 | { |
2513 | { |
2491 | strcpy(Partition.PathToCwd, "/"); |
2514 | strcpy(Partition.PathToCwd, "/"); |
2492 | Partition.CurrentWorkingDirectory = Partition.FirstRootDirSector; |
2515 | Partition.CurrentWorkingDirectory = Partition.FirstRootDirSector; |
2493 | /* if there is no other pathinformation we only switch to the rootdirectory. So theres nothing left todo.*/ |
2516 | /* if there is no other pathinformation we only switch to the rootdirectory. So theres nothing left todo.*/ |
2494 | if(!dircount) return(1); |
2517 | if(!dircount) return(1); |
2495 | } |
2518 | } |
2496 | /* now we parse through all the subdirectories within the path */ |
2519 | /* now we parse through all the subdirectories within the path */ |
2497 | do |
2520 | do |
2498 | { |
2521 | { |
2499 | /* until all the subdirectories within the path have been processed */ |
2522 | /* until all the subdirectories within the path have been processed */ |
2500 | if(dircount) dircount--; |
2523 | if(dircount) dircount--; |
2501 | /* this is the name of the next subdirectory we are looking for */ |
2524 | /* this is the name of the next subdirectory we are looking for */ |
2502 | directory = GetSubDirectory(directory, cache); |
2525 | directory = GetSubDirectory(directory, cache); |
2503 | /* search for the next subdirectory within the path */ |
2526 | /* search for the next subdirectory within the path */ |
2504 | if(findfirst_(cache, ATTR_SUBDIRECTORY, &fe)) |
2527 | if(findfirst_(cache, ATTR_SUBDIRECTORY, &fe)) |
2505 | { |
2528 | { |
2506 | /* we try to change into the directory "..". Now we have to delete the last direntry from the path */ |
2529 | /* we try to change into the directory "..". Now we have to delete the last direntry from the path */ |
2507 | if(strcmp(cache,"..") == 0) RemoveLastDirFromPath(); |
2530 | if(strcmp(cache,"..") == 0) RemoveLastDirFromPath(); |
2508 | /* we try to change into the actual directory so there's nothing todo */ |
2531 | /* we try to change into the actual directory so there's nothing todo */ |
2509 | else if(cache[0] == '.') return(1); |
2532 | else if(cache[0] == '.') return(1); |
2510 | /* otherwise we append the name of the directory we are changing in to the path */ |
2533 | /* otherwise we append the name of the directory we are changing in to the path */ |
2511 | else AppendDirToPath(cache); |
2534 | else AppendDirToPath(cache); |
2512 | /* The startcluster within an directoryentry specifies the position within the fat where the file or directory starts */ |
2535 | /* The startcluster within an directoryentry specifies the position within the fat where the file or directory starts */ |
2513 | ultemp = (u32) fe.fp.FirstSectorOfFirstCluster; |
2536 | ultemp = (u32) fe.fp.FirstSectorOfFirstCluster; |
2514 | /* the first 2 entries are reserved for '.' and '..' */ |
2537 | /* the first 2 entries are reserved for '.' and '..' */ |
2515 | ultemp -= 2; |
2538 | ultemp -= 2; |
2516 | /* now we have to transform the position within the fat into the corrosponding sectoraddress relative to the beginning of the datasection of the active partition*/ |
2539 | /* now we have to transform the position within the fat into the corrosponding sectoraddress relative to the beginning of the datasection of the active partition*/ |
2517 | ultemp *= Partition.SectorsPerCluster; |
2540 | ultemp *= Partition.SectorsPerCluster; |
2518 | /* at least we make the sectoraddress absolute by adding the relative address to the beginning of the datasection of the active partition */ |
2541 | /* at least we make the sectoraddress absolute by adding the relative address to the beginning of the datasection of the active partition */ |
2519 | ultemp += Partition.FirstDataSector; |
2542 | ultemp += Partition.FirstDataSector; |
2520 | /* the cwd now points to the specified directory */ |
2543 | /* the cwd now points to the specified directory */ |
2521 | Partition.CurrentWorkingDirectory = ultemp; |
2544 | Partition.CurrentWorkingDirectory = ultemp; |
2522 | /* we found the folder specified by the foldername */ |
2545 | /* we found the folder specified by the foldername */ |
2523 | retvalue = 1; |
2546 | retvalue = 1; |
2524 | } |
2547 | } |
2525 | } |
2548 | } |
2526 | /* do this until all subdirectories have been found or a subdirectory is missing */ |
2549 | /* do this until all subdirectories have been found or a subdirectory is missing */ |
2527 | while(dircount && retvalue ); |
2550 | while(dircount && retvalue ); |
2528 | 2551 | ||
2529 | /* if we could not change to the specified directory we restore the actual path */ |
2552 | /* if we could not change to the specified directory we restore the actual path */ |
2530 | if(!retvalue) |
2553 | if(!retvalue) |
2531 | { |
2554 | { |
2532 | Partition.CurrentWorkingDirectory = cwdt; |
2555 | Partition.CurrentWorkingDirectory = cwdt; |
2533 | strcpy(Partition.PathToCwd, tp); |
2556 | strcpy(Partition.PathToCwd, tp); |
2534 | } |
2557 | } |
2535 | return(retvalue); |
2558 | return(retvalue); |
2536 | } |
2559 | } |
2537 | 2560 |