3 * Copyright (C) 2007 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 unsigned long file_cache_id
= 0;
28 unsigned char tolower(char c
)
30 return ((c
>= 'A' && c
<= 'Z') ? c
+ 32: c
);
33 static void wait(void)
37 for(wait
= WAIT
; wait
!= 0; wait
--)
41 void dec2bin (long decimal
, char *binary
)
48 // take care of negative input
57 decimal
= decimal
/ 2; // whittle down decimal
58 temp
[k
++] = remain
+ 0x30; // makes characters 0 or 1
59 } while (decimal
> 0);
62 temp
[k
++] = '-'; // add back - sign
64 temp
[k
++] = ' '; // or space
67 binary
[n
++] = temp
[--k
]; // reverse spelling
69 binary
[n
-1] = 0; // end with NULL
72 void parse_attr (char* binary
,int y
)
77 0x04 000100 System file
80 0x20 100000 Binary which stands for Archive
91 for(i
= strlen(binary
)-1; i
>=0; i
--)
97 case 0: dir
[y
].read
=1; break;
98 case 1: dir
[y
].hidden
=1; break;
99 case 2: dir
[y
].system
=1; break;
100 case 3: dir
[y
].volume
=1; break;
101 case 4: dir
[y
].dir
=1; break;
102 case 5: dir
[y
].bin
=1; break;
109 int fat_read (unsigned int byte
, unsigned int byte2
)
112 unsigned char *buf
= text
;
115 unsigned int start
= -1;
116 unsigned int next
= 0;
117 unsigned int test
= 0;
118 unsigned int pom
= 0;
120 if (!read_block (1, text
, 1))
124 for(x
= 0; x
< 512; x
++) {
125 //if (i>byte-3 && i<byte+3)
126 //kprintf("%x",*buf);
130 if (i
== byte2
&& start
!= -1) {
131 if (start
== 0xf0 && *buf
== 0xff)
134 next
= ((*buf
<< 8 | start
) >> 4);
135 test
= (*buf
<< 8 | start
);
138 next
= (*buf
<< 8 | start
) & 0x0FFF;
142 //kprintf("\nnext=%x %d start=%x end=%x byte=%d byte1=%d cele=%d test=%x\n",next,next,start,*buf,byte,byte2,cele,test);
155 int fat_nextsector (int i
) {
160 void read_f (unsigned int cluster
){
161 static BYTE text1
[512] = "";
164 // kprintf("\n*** %d\n",cluster);
165 read_block (33 + cluster
, text1
, 1);
167 //text1[strlen(text1)-4]='\0';
171 /* FIXME: lot of work with elf */
172 //file_cache = (unsigned char *) krealloc ((void *) file_cache, file_cache_id+len);
173 //kprintf ("file_cache: 0x%x\n", file_cache);
174 memcpy (file_cache
+file_cache_id
, text1
, len
);
176 file_cache_id
+= len
;
179 // 33 sector = open space
180 unsigned long read_file (int aa
)
187 file_cache
[0] = '\0';
188 //kprintf("name=%s start=%d fat=%d\n",dir[aa].name,dir[aa].start,next);
189 next
= fat_nextsector (dir
[aa
].start
);
190 sect
= fat_read (next
,next
+1);
192 next
= fat_nextsector (sect
);
193 read_f (dir
[aa
].start
-2);
194 // file_cache[strlen(file_cache)-4]='\0';
196 for (i
= 0; i
< 100; i
++) {
197 DPRINT ("next: %d sect: %d\n", next
, sect
);
199 if (sect
<0 || sect
==4095)
201 //file_cache[strlen (file_cache)-4] = '\0';
203 sect
= fat_read (next
, next
+1);
204 next
= fat_nextsector (sect
);
207 return file_cache_id
;
210 unsigned read_dir (int aa
)
212 static BYTE text
[512];
213 unsigned char *buf
= text
;
220 /* vymazeme nazvy vsech souboru */
221 for(x
= 0; x
< 223; x
++)
222 dir
[x
].name
[0] = '\0';
225 if (!read_block (19, text
, 1))
227 //printf ("ret: %d\n", ret);
229 if (!read_block (33+dir
[aa
].start
-2, text
, 1))
235 for (x
= 0; x
< 512; x
++) {
245 if (i
== 27 && label
== 0)
246 dir
[y
].start
= (*buf
<< 8 | start
);
248 if (i
== 11 && (*buf
== 0x0f || *buf
== 0x00)) {
249 strcpy (dir
[y
].name
, "");
253 if (i
== 11 && *buf
!= 0x0f && *buf
!= 0x00) {
257 if (i
<= 10 && i
>= 0)
258 dir
[y
].name
[i
] = tolower (*buf
);
274 void write_f (unsigned int cluster
, char *text1
)
276 if (!write_block (33 + cluster
, text1
, 1)) {
277 printf ("error -> FAT12 -> !floppy write\n");
281 DPRINT ("FAT12 -> write_f (): OK");
284 // 33 sector = open space
285 void write_file (int aa
, char *buf
)
293 //kprintf("name=%s start=%d fat=%d\n",dir[aa].name,dir[aa].start,next);
295 next
= fat_nextsector (dir
[aa
].start
);
296 sect
= fat_read (next
, next
+1);
299 next
= fat_nextsector (sect
);
300 read_f (dir
[aa
].start
-2);
302 for(i
= 0; i
< 100; i
++) {
303 DPRINT ("next: %d sect: %d\n", next
, sect
);
305 if (sect
< 0 || sect
== 4095)
308 write_f (sect
-2, buf
);
309 sect
= fat_read (next
, next
+1);
310 next
= fat_nextsector (sect
);
314 /* TODO: in development */
315 void write_dir (int aa
)
317 static BYTE text
[512];
318 unsigned char *buf
= text
;
325 /* vymazeme nazvy vsech souboru */
326 for(x
= 0; x
< 223; x
++)
327 dir
[x
].name
[0] = '\0';
330 write_block (19, text
, 1);
332 write_block (33+dir
[aa
].start
-2, text
, 1);
336 for (x
= 0; x
< 512; x
++) {
346 if (i
== 27 && label
== 0)
347 dir
[y
].start
= (*buf
<< 8 | start
);
349 if (i
== 11 && (*buf
== 0x0f || *buf
== 0x00)) {
350 strcpy (dir
[y
].name
, "");
354 if (i
== 11 && *buf
!= 0x0f && *buf
!= 0x00) {
358 if (i
<= 10 && i
>= 0)
359 dir
[y
].name
[i
] = tolower (*buf
);
368 bool fat_handler (unsigned act
, unsigned arg
, char *block
, unsigned n
, unsigned long l
)
391 //memcpy (block, file_cache, file_cache_id);
392 block
[file_cache_id
] = '\0';
411 //printf ("alokovano: %ud\n", y);
412 char *buf
= (char *) kmalloc (sizeof (char) * (y
+1));
415 DPRINT ("ERROR -> FAT WRITE : out of memory");
420 memcpy (buf
, block
, l
);
423 for (i
= 0; i
< (y
/512); i
++);
424 write_file (n
, buf
+(i
*512));