summaryrefslogtreecommitdiff
path: root/firmware/test/fat/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/test/fat/main.c')
-rw-r--r--firmware/test/fat/main.c219
1 files changed, 219 insertions, 0 deletions
diff --git a/firmware/test/fat/main.c b/firmware/test/fat/main.c
new file mode 100644
index 0000000000..ffe3dd8764
--- /dev/null
+++ b/firmware/test/fat/main.c
@@ -0,0 +1,219 @@
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include "fat.h"
5#include "ata.h"
6#include "debug.h"
7#include "disk.h"
8
9void dbg_dump_sector(int sec);
10void dbg_dump_buffer(unsigned char *buf);
11void dbg_print_bpb(struct bpb *bpb);
12void dbg_console(struct bpb *bpb);
13
14void dbg_dump_sector(int sec)
15{
16 unsigned char buf[512];
17
18 ata_read_sectors(sec,1,buf);
19 printf("---< Sector %d >-----------------------------------------\n", sec);
20 dbg_dump_buffer(buf);
21}
22
23void dbg_dump_buffer(unsigned char *buf)
24{
25 int i, j;
26 unsigned char c;
27 unsigned char ascii[33];
28
29 for(i = 0;i < 512/16;i++)
30 {
31 for(j = 0;j < 16;j++)
32 {
33 c = buf[i*16+j];
34
35 printf("%02x ", c);
36 if(c < 32 || c > 127)
37 {
38 ascii[j] = '.';
39 }
40 else
41 {
42 ascii[j] = c;
43 }
44 }
45
46 ascii[j] = 0;
47 printf("%s\n", ascii);
48 }
49}
50
51void dbg_print_bpb(struct bpb *bpb)
52{
53 printf("bpb_oemname = \"%s\"\n", bpb->bs_oemname);
54 printf("bpb_bytspersec = %d\n", bpb->bpb_bytspersec);
55 printf("bpb_secperclus = %d\n", bpb->bpb_secperclus);
56 printf("bpb_rsvdseccnt = %d\n", bpb->bpb_rsvdseccnt);
57 printf("bpb_numfats = %d\n", bpb->bpb_numfats);
58 printf("bpb_rootentcnt = %d\n", bpb->bpb_rootentcnt);
59 printf("bpb_totsec16 = %d\n", bpb->bpb_totsec16);
60 printf("bpb_media = %02x\n", bpb->bpb_media);
61 printf("bpb_fatsz16 = %d\n", bpb->bpb_fatsz16);
62 printf("bpb_secpertrk = %d\n", bpb->bpb_secpertrk);
63 printf("bpb_numheads = %d\n", bpb->bpb_numheads);
64 printf("bpb_hiddsec = %u\n", bpb->bpb_hiddsec);
65 printf("bpb_totsec32 = %u\n", bpb->bpb_totsec32);
66
67 printf("bs_drvnum = %d\n", bpb->bs_drvnum);
68 printf("bs_bootsig = %02x\n", bpb->bs_bootsig);
69 if(bpb->bs_bootsig == 0x29)
70 {
71 printf("bs_volid = %xl\n", bpb->bs_volid);
72 printf("bs_vollab = \"%s\"\n", bpb->bs_vollab);
73 printf("bs_filsystype = \"%s\"\n", bpb->bs_filsystype);
74 }
75
76 printf("bpb_fatsz32 = %u\n", bpb->bpb_fatsz32);
77 printf("last_word = %04x\n", bpb->last_word);
78
79 printf("fat_type = FAT32\n");
80}
81
82void dbg_dir(struct bpb *bpb, int currdir)
83{
84 struct fat_dirent dent;
85 struct fat_direntry de;
86
87 if(fat_opendir(bpb, &dent, currdir) >= 0)
88 {
89 while(fat_getnext(bpb, &dent, &de) >= 0)
90 {
91 printf("%s (%d)\n", de.name,de.firstcluster);
92 }
93 }
94 else
95 {
96 fprintf(stderr, "Could not read dir on cluster %d\n", currdir);
97 }
98}
99
100void dbg_type(struct bpb *bpb, int cluster)
101{
102 unsigned char buf[SECTOR_SIZE*5];
103 struct fat_fileent ent;
104 int i;
105
106 fat_open(bpb,cluster,&ent);
107
108 for (i=0;i<5;i++)
109 if(fat_read(bpb, &ent, 1, buf) >= 0)
110 {
111 buf[SECTOR_SIZE]=0;
112 printf("%s\n", buf);
113 }
114 else
115 {
116 fprintf(stderr, "Could not read file on cluster %d\n", cluster);
117 }
118}
119
120char current_directory[256] = "\\";
121int last_secnum = 0;
122
123void dbg_prompt(void)
124{
125 printf("C:%s> ", current_directory);
126}
127
128void dbg_console(struct bpb* bpb)
129{
130 char cmd[32] = "";
131 char last_cmd[32] = "";
132 int quit = 0;
133 char *s;
134
135 while(!quit)
136 {
137 dbg_prompt();
138 if(fgets(cmd, sizeof(cmd) - 1, stdin))
139 {
140 if(strlen(cmd) == 1) /* empty command? */
141 {
142 strcpy(cmd, last_cmd);
143 }
144
145 /* Get the first token */
146 s = strtok(cmd, " \n");
147 if(s)
148 {
149 if(!strcasecmp(s, "dir"))
150 {
151 int secnum = 0;
152 if((s = strtok(NULL, " \n")))
153 {
154 secnum = atoi(s);
155 }
156 dbg_dir(bpb, secnum);
157 continue;
158 }
159
160 if(!strcasecmp(s, "ds"))
161 {
162 /* Remember the command */
163 strcpy(last_cmd, s);
164
165 if((s = strtok(NULL, " \n")))
166 {
167 last_secnum = atoi(s);
168 }
169 else
170 {
171 last_secnum++;
172 }
173 printf("secnum: %d\n", last_secnum);
174 dbg_dump_sector(last_secnum);
175 continue;
176 }
177
178 if(!strcasecmp(s, "type"))
179 {
180 int cluster = 0;
181 if((s = strtok(NULL, " \n")))
182 {
183 cluster = atoi(s);
184 }
185 dbg_type(bpb,cluster);
186 continue;
187 }
188
189 if(!strcasecmp(s, "exit") ||
190 !strcasecmp(s, "x"))
191 {
192 quit = 1;
193 }
194 }
195 }
196 }
197}
198
199int main(int argc, char *argv[])
200{
201 struct bpb bpb;
202
203 if(ata_init()) {
204 DEBUGF("*** Warning! The disk is uninitialized\n");
205 return -1;
206 }
207 if (disk_init()) {
208 DEBUGF("*** Failed reading partitions\n");
209 return -1;
210 }
211
212 if(fat_mount(&bpb,part[0].start)) {
213 DEBUGF("*** Failed mounting fat\n");
214 }
215
216 dbg_console(&bpb);
217 return 0;
218}
219