summaryrefslogtreecommitdiffstats
path: root/firmware/test/fat/main.c
blob: 5490fa97777e2ca254ca4043c26fe5a1e82d3a6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "fat.h"
#include "ata.h"
#include "debug.h"
#include "disk.h"
#include "dir.h"
#include "file.h"

void dbg_dump_sector(int sec);
void dbg_dump_buffer(unsigned char *buf);
void dbg_print_bpb(struct bpb *bpb);
void dbg_console(void);

void dbg_dump_sector(int sec)
{
    unsigned char buf[512];

    ata_read_sectors(sec,1,buf);
    DEBUGF("---< Sector %d >-----------------------------------------\n", sec);
    dbg_dump_buffer(buf);
}

void dbg_dump_buffer(unsigned char *buf)
{
    int i, j;
    unsigned char c;
    unsigned char ascii[33];

    for(i = 0;i < 512/16;i++)
    {
        for(j = 0;j < 16;j++)
        {
            c = buf[i*16+j];

            DEBUGF("%02x ", c);
            if(c < 32 || c > 127)
            {
                ascii[j] = '.';
            }
            else
            {
                ascii[j] = c;
            }
        }

        ascii[j] = 0;
        DEBUGF("%s\n", ascii);
    }
}

void dbg_print_bpb(struct bpb *bpb)
{
    DEBUGF("bpb_oemname = \"%s\"\n", bpb->bs_oemname);
    DEBUGF("bpb_bytspersec = %d\n", bpb->bpb_bytspersec);
    DEBUGF("bpb_secperclus = %d\n", bpb->bpb_secperclus);
    DEBUGF("bpb_rsvdseccnt = %d\n", bpb->bpb_rsvdseccnt);
    DEBUGF("bpb_numfats = %d\n", bpb->bpb_numfats);
    DEBUGF("bpb_rootentcnt = %d\n", bpb->bpb_rootentcnt);
    DEBUGF("bpb_totsec16 = %d\n", bpb->bpb_totsec16);
    DEBUGF("bpb_media = %02x\n", bpb->bpb_media);
    DEBUGF("bpb_fatsz16 = %d\n", bpb->bpb_fatsz16);
    DEBUGF("bpb_secpertrk = %d\n", bpb->bpb_secpertrk);
    DEBUGF("bpb_numheads = %d\n", bpb->bpb_numheads);
    DEBUGF("bpb_hiddsec = %u\n", bpb->bpb_hiddsec);
    DEBUGF("bpb_totsec32 = %u\n", bpb->bpb_totsec32);

    DEBUGF("bs_drvnum = %d\n", bpb->bs_drvnum);
    DEBUGF("bs_bootsig = %02x\n", bpb->bs_bootsig);
    if(bpb->bs_bootsig == 0x29)
    {
       DEBUGF("bs_volid = %xl\n", bpb->bs_volid);
       DEBUGF("bs_vollab = \"%s\"\n", bpb->bs_vollab);
       DEBUGF("bs_filsystype = \"%s\"\n", bpb->bs_filsystype);
    }

    DEBUGF("bpb_fatsz32 = %u\n", bpb->bpb_fatsz32);
    DEBUGF("last_word = %04x\n", bpb->last_word);

    DEBUGF("fat_type = FAT32\n");
}

void dbg_dir(char* currdir)
{
    DIR* dir;
    struct dirent* entry;

    dir = opendir(currdir);
    if (dir)
    {
        while ( (entry = readdir(dir)) ) {
            DEBUGF("%15s (%d bytes)\n", entry->d_name, entry->size);
        }
    }
    else
    {
        DEBUGF( "Could not open dir %s\n", currdir);
    }
    closedir(dir);
}

void dbg_type(char* name)
{
    unsigned char buf[SECTOR_SIZE*5];
    int i,fd,rc;

    fd = open(name,O_RDONLY);
    if (fd<0)
        return;
    DEBUGF("Got file descriptor %d\n",fd);
    
    for (i=0;i<5;i++) {
        rc = read(fd, buf, SECTOR_SIZE*2/3);
        if( rc > 0 )
        {
            buf[rc]=0;
            printf("%d: %s\n", i, buf);
        }
        else if ( rc == 0 ) {
            DEBUGF("EOF\n");
            break;
        }
        else
        {
            DEBUGF("Failed reading file: %d\n",rc);
        }
    }
    close(fd);
}

char current_directory[256] = "\\";
int last_secnum = 0;

void dbg_prompt(void)
{
    DEBUGF("C:%s> ", current_directory);
}

void dbg_console(void)
{
    char cmd[32] = "";
    char last_cmd[32] = "";
    int quit = 0;
    char *s;

    while(!quit)
    {
        dbg_prompt();
        if(fgets(cmd, sizeof(cmd) - 1, stdin))
        {
            if(strlen(cmd) == 1) /* empty command? */
            {
                strcpy(cmd, last_cmd);
            }

            /* Get the first token */
	    s = strtok(cmd, " \n");
            if(s)
            {
                if(!strcasecmp(s, "dir"))
                {
                    s = strtok(NULL, " \n");
                    if (!s)
                        s = "/";
                    dbg_dir(s);
                    continue;
                }

                if(!strcasecmp(s, "ds"))
                {
                    /* Remember the command */
                    strcpy(last_cmd, s);
                    
                    if((s = strtok(NULL, " \n")))
                    {
                        last_secnum = atoi(s);
                    }
                    else
                    {
                        last_secnum++;
                    }
                    DEBUGF("secnum: %d\n", last_secnum);
                    dbg_dump_sector(last_secnum);
                    continue;
                }

                if(!strcasecmp(s, "type"))
                {
                    s = strtok(NULL, " \n");
                    if (!s)
                        continue;
                    dbg_type(s);
                    continue;
                }
            
                if(!strcasecmp(s, "exit") ||
                   !strcasecmp(s, "x"))
                {
                    quit = 1;
                }
            }
        }
        else
            quit = 1;
    }
}

int main(int argc, char *argv[])
{
    if(ata_init()) {
        DEBUGF("*** Warning! The disk is uninitialized\n");
        return -1;
    }
    if (disk_init()) {
        DEBUGF("*** Failed reading partitions\n");
        return -1;
    }

    if(fat_mount(part[0].start)) {
        DEBUGF("*** Failed mounting fat\n");
    }

    dbg_console();

    return 0;
}