3 * Copyright (C) 2007 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
4 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
5 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 #include <partition.h>
35 static unsigned char tolower (char c
)
37 return ((c
>= 'A' && c
<= 'Z') ? c
+ 32: c
);
40 static void dec2bin (long decimal
, char *binary
)
47 // take care of negative input
53 decimal
= decimal
/ 2; // whittle down decimal
54 temp
[k
++] = remain
+ 0x30; // makes characters 0 or 1
55 } while (decimal
> 0);
58 temp
[k
++] = '-'; // add back - sign
60 temp
[k
++] = ' '; // or space
63 binary
[n
++] = temp
[--k
]; // reverse spelling
65 binary
[n
-1] = 0; // end with NULL
70 static void parse_attr (char attr
,int y
)
75 0x04 000100 System file
78 0x20 100000 Binary which stands for Archive
81 // printf ("attr: %d '%c'\n", attr, attr);
103 // printf ("attr: r%u h%u s%u v%u d%u b%u\n", dir[y].read, dir[y].hidden, dir[y].system, dir[y].volume, dir[y].dir, dir[y].bin);
109 printf ("file: %s | binary: %s\n", dir[y].name, binary);
110 for(i = strlen(binary)-1; i>=0; i--)
116 case 0: dir[y].read=1; break;
117 case 1: dir[y].hidden=1; break;
118 case 2: dir[y].system=1; break;
119 case 3: dir[y].volume=1; break;
120 case 4: dir[y].dir=1; break;
121 case 5: dir[y].bin=1; break;
129 static void parse_attr (char* binary
,int y
)
132 0x01 000001 Read only
134 0x04 000100 System file
135 0x08 001000 Volume ID
136 0x10 010000 Directory
137 0x20 100000 Binary which stands for Archive
149 // printf ("file: %s | binary: %s\n", dir[y].name, binary);
151 for (i
= strlen (binary
)-1; i
>= 0; i
--) {
152 if(binary
[i
] == '1') {
154 case 0: dir
[y
].read
=1; break;
155 case 1: dir
[y
].hidden
=1; break;
156 case 2: dir
[y
].system
=1; break;
157 case 3: dir
[y
].volume
=1; break;
158 case 4: dir
[y
].dir
=1; break;
159 case 5: dir
[y
].bin
=1; break;
168 int fat16_read (unsigned int byte
, unsigned int byte2
)
171 unsigned char *buf
= text
;
174 unsigned int start
= -1;
175 unsigned int next
= 0;
176 unsigned int test
= 0;
177 unsigned int pom
= 0;
179 lba28_drive_read (curr_part
, 64, text
);//read_block(1,text,1);
183 for(x
= 0; x
< 512; x
++) {
184 //if (i>byte-3 && i<byte+3)
185 //kprintf("%x",*buf);
189 if (i
==byte2
&& start
!=-1) {
190 if (start
== 0xf0 && *buf
== 0xff)
193 next
= ((*buf
<< 8 | start
) >> 4);
194 test
= (*buf
<< 8 | start
);
197 next
= (*buf
<< 8 | start
)&0x0FFF;
201 //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);
214 int fat16_nextsector (int i
)
216 cele
= (i
* 150) % 100;
218 return (i
* 150) / 100;
221 void fat16_read_f (unsigned int cluster
){
222 static BYTE text1
[512] = "";
225 // kprintf("\n*** %d\n",cluster);
226 printf ("cluster: %d\n", cluster
);
227 lba28_drive_read (curr_part
, 322 + cluster
, text1
);//read_block (33 + cluster, text1, 1);
229 //text1[strlen(text1)-4]='\0';
232 memcpy (file_cache
+file_cache_id
, text1
, len
);
234 file_cache_id
+= len
;
236 kprintf("%s",file_cache
);
237 //kprintf("%s",text1);fat16_read_dir
239 //strcat(file_cache,*buf);
250 // 33 sector = open space
251 unsigned long fat16_read_file (partition_t
*p
, int aa
)
258 strcpy (file_cache
, "");
260 next
= fat16_nextsector(dir
[aa
].start
); // start: 4; fat: 6; sect: 16 .. cluster: 2
262 sect
= fat16_read(next
,next
+1);
263 // printf ("name=%s start=%d fat=%d %d\n",dir[aa].name,dir[aa].start,next, sect);
265 next
= fat16_nextsector(sect
);
266 // printf ("name=%s start=%d fat=%d %d\n",dir[aa].name,dir[aa].start,next, sect);
267 fat16_read_f (dir
[aa
].start
);
268 // file_cache[strlen(file_cache)-4]='\0';
269 for(i
= 0; i
< 100; i
++)
271 //DPRINT (DBG_DRIVERĀ | DBG_FS, "next: %d sect: %d", next, sect);
272 if (sect
< 0 || sect
== 4095)
274 //file_cache[strlen (file_cache)-4] = '\0';
276 sect
= fat16_read (next
, next
+1);
277 next
= fat16_nextsector (sect
);
280 return file_cache_id
;
283 void fat16_read_dir (partition_t
*p
, int aa
)
285 unsigned char *text
= (unsigned char *) kmalloc (512 * sizeof (unsigned char));
286 unsigned char *buf
= text
;
294 /* vymazeme nazvy vsech souboru */
295 for(x
= 0; x
< 223; x
++)
296 dir
[x
].name
[0] = '\0';
299 lba28_drive_read (p
, 286, text
);//read_block (19, text, 1);
300 else // 321+3-2 = 324-2 = 322
301 lba28_drive_read (p
, 321+dir
[aa
].start
-2, text
);//read_block (33+dir[aa].start-2, text, 1);
305 for (x
= 0; x
< 512; x
++) {//286, 63
312 //printf ("#i je: %d '%c'", i, *buf);
315 if (i
== 27 && label
== 0) {
316 dir
[y
].start
= (*buf
<< 8 | start
);
317 // printf ("start: %d\n", dir[y].start);
319 if (i
== 11 && (*buf
== 0x0f || *buf
== 0x00)) {
320 strcpy (dir
[y
].name
, "");
324 if (i
== 11 && *buf
!= 0x0f && *buf
!= 0x00) {
326 //dec2bin (*buf,bin);
327 parse_attr (*buf
, y
);
329 if (i
<= 10 && i
>= 0)
330 dir
[y
].name
[i
] = tolower (*buf
);
333 printf ("> '%c' '%d'#", (unsigned char) *buf, i);
345 bool fat16_handler (unsigned act
, char *block
, unsigned n
, unsigned long l
)
356 /*fat16_read_file (curr_part, n);
357 memcpy (block, file_cache, file_cache_id);
358 block[file_cache_id] = '\0';
366 /*fat16_read_dir (curr_part, n);
368 memcpy (block, file_cache, file_cache_id);
369 block[file_cache_id] = '\0';
370 l = file_cache_id; */