Cleanup in elf.c with .bss section clean; adm command mounts cdrom instead of floppy...
[ZeXOS.git] / kernel / drivers / fs / fat16.c
blob51967ad4c1082b949a366893548d5130ab7429f9
1 /*
2 * ZeX/OS
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/>.
22 #include <build.h>
24 #ifdef ARCH_i386
26 #include <system.h>
27 #include <string.h>
28 #include <dev.h>
29 #include <fs.h>
30 #include <partition.h>
31 #include <cache.h>
32 #ifdef TEST
33 static int cele;
35 static unsigned char tolower (char c)
37 return ((c >= 'A' && c <= 'Z') ? c + 32: c);
40 static void dec2bin (long decimal, char *binary)
42 int k = 0, n = 0;
43 int neg_flag = 0;
44 int remain;
45 char temp[80];
47 // take care of negative input
48 if (decimal < 0) {
49 decimal = -decimal;
50 neg_flag = 1;
51 } do {
52 remain = decimal % 2;
53 decimal = decimal / 2; // whittle down decimal
54 temp[k++] = remain + 0x30; // makes characters 0 or 1
55 } while (decimal > 0);
57 if (neg_flag)
58 temp[k++] = '-'; // add back - sign
59 else
60 temp[k++] = ' '; // or space
62 while (k >= 0)
63 binary[n++] = temp[--k]; // reverse spelling
65 binary[n-1] = 0; // end with NULL
70 static void parse_attr (char attr,int y)
73 0x01 000001 Read only
74 0x02 000010 Hidden
75 0x04 000100 System file
76 0x08 001000 Volume ID
77 0x10 010000 Directory
78 0x20 100000 Binary which stands for Archive
81 // printf ("attr: %d '%c'\n", attr, attr);
83 dir[y].read = 0;
84 dir[y].hidden = 0;
85 dir[y].system = 0;
86 dir[y].volume = 0;
87 dir[y].dir = 0;
88 dir[y].bin = 0;
90 if (attr & 0x01)
91 dir[y].read = 1;
92 if (attr & 0x02)
93 dir[y].hidden = 1;
94 if (attr & 0x04)
95 dir[y].system = 1;
96 if (attr & 0x08)
97 dir[y].volume = 1;
98 if (attr & 0x10)
99 dir[y].dir = 1;
100 if (attr & 0x20)
101 dir[y].bin = 1;
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);
106 /* int i;
107 int x=0;
109 printf ("file: %s | binary: %s\n", dir[y].name, binary);
110 for(i = strlen(binary)-1; i>=0; i--)
112 if(binary[i] == '1')
114 switch (x)
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;
124 x++;
128 #ifdef TEST
129 static void parse_attr (char* binary,int y)
132 0x01 000001 Read only
133 0x02 000010 Hidden
134 0x04 000100 System file
135 0x08 001000 Volume ID
136 0x10 010000 Directory
137 0x20 100000 Binary which stands for Archive
140 int i;
141 int x=0;
142 dir[y].read=0;
143 dir[y].hidden=0;
144 dir[y].system=0;
145 dir[y].volume=0;
146 dir[y].dir=0;
147 dir[y].bin=0;
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') {
153 switch (x) {
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;
162 x++;
165 #endif
168 int fat16_read (unsigned int byte, unsigned int byte2)
170 BYTE text[512];
171 unsigned char *buf = text;
172 unsigned int i=0;
173 unsigned int x;
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);
181 buf = text;
183 for(x = 0; x < 512; x ++) {
184 //if (i>byte-3 && i<byte+3)
185 //kprintf("%x",*buf);
186 if (i==byte)
187 start = *buf;
189 if (i==byte2 && start!=-1) {
190 if (start == 0xf0 && *buf == 0xff)
191 return -2;
193 next = ((*buf << 8 | start) >> 4);
194 test = (*buf << 8 | start);
196 if (cele == 0)
197 next = (*buf << 8 | start)&0x0FFF;
199 if (next < 0)
200 next = next*(-1);
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);
203 return next;
206 *buf ++;
207 i ++;
210 return -1;
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] = "";
223 unsigned char *buf;
224 unsigned int x;
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);
228 //strcpy(buf,text1);
229 //text1[strlen(text1)-4]='\0';
231 unsigned len = 512;
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
238 //+=*buf;
239 //strcat(file_cache,*buf);
241 for(x=0; x<512; x++)
243 wait();
244 kprintf("%c",*buf);
245 *buf++;
250 // 33 sector = open space
251 unsigned long fat16_read_file (partition_t *p, int aa)
253 int i;
254 int byte;
255 int next = 0;
256 int sect;
257 cele=0;
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);
264 file_cache_id = 0;
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)
273 break;
274 //file_cache[strlen (file_cache)-4] = '\0';
275 fat16_read_f (sect);
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;
288 int x;
289 int i = 0;
290 int y = 0;
291 char *bin;
292 int label = 0;
293 unsigned int start;
294 /* vymazeme nazvy vsech souboru */
295 for(x = 0; x < 223; x ++)
296 dir[x].name[0] = '\0';
298 if (aa == -1)
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);
303 buf = text;
305 for (x = 0; x < 512; x ++) {//286, 63
306 if (i == 31) {
307 i=-1;
308 y++;
309 label=0;
311 //if (*buf)
312 //printf ("#i je: %d '%c'", i, *buf);
313 if (i == 26)
314 start = *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, "");
321 label = 1;
322 y --;
324 if (i == 11 && *buf != 0x0f && *buf != 0x00) {
325 //putch (*buf);
326 //dec2bin (*buf,bin);
327 parse_attr (*buf, y);
329 if (i <= 10 && i >= 0)
330 dir[y].name[i] = tolower (*buf);
332 /*if (i < 32) {
333 printf ("> '%c' '%d'#", (unsigned char) *buf, i);
334 usleep (100);
337 i ++;
338 *buf ++;
342 //free (text);
344 #endif
345 bool fat16_handler (unsigned act, char *block, unsigned n, unsigned long l)
347 switch (act) {
348 case FS_ACT_INIT:
350 /* nothing */
351 return 1;
353 break;
354 case FS_ACT_READ:
356 /*fat16_read_file (curr_part, n);
357 memcpy (block, file_cache, file_cache_id);
358 block[file_cache_id] = '\0';
359 l = file_cache_id;*/
361 return 1;
363 break;
364 case FS_ACT_CHDIR:
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; */
372 return 1;
374 break;
377 return 0;
379 #endif