altos: let FAT tracing work in ao_fat_test as needed
[fw/altos] / src / drivers / ao_fat.c
index ea8cdf961f589e58828f066396eaa750e40ffb55..6aae1410b8676821fec41d7d5f06bd8e6928c947 100644 (file)
 #include "ao_bufio.h"
 
 /* Include FAT commands */
+#ifndef AO_FAT_TEST
 #define FAT_COMMANDS   1
+#endif
 
 /* Spew FAT tracing */
 #define FAT_TRACE      0
  
+#ifdef DBG
+#undef DBG
+#endif
+
 #if FAT_TRACE
 #define DBG(...) printf(__VA_ARGS__)
 #else
@@ -573,7 +579,6 @@ static uint32_t             ao_file_offset;
 static uint32_t                        ao_file_cluster_offset;
 static cluster_t               ao_file_cluster;
 static uint8_t                 ao_file_opened;
-static uint8_t                 ao_filesystem_available;
 static uint8_t                 ao_filesystem_setup;
 static uint8_t                 ao_filesystem_status;
 
@@ -607,6 +612,12 @@ ao_fat_setup(void)
        return ao_filesystem_status;
 }
 
+void
+ao_fat_unmount(void)
+{
+       ao_filesystem_setup = 0;
+}
+
 /*
  * Basic file operations
  */
@@ -1019,7 +1030,6 @@ int
 ao_fat_write(void *src, int len)
 {
        uint8_t         *src_b = src;
-       uint32_t        sector;
        uint16_t        this_time;
        uint16_t        offset;
        uint8_t         *buf;
@@ -1147,23 +1157,25 @@ ao_fat_readdir(uint16_t *entry, struct ao_fat_dirent *dirent)
        for (;;) {
                dent = ao_fat_root_get(*entry);
                if (!dent)
-                       return 0;
+                       return -AO_FAT_EDIREOF;
 
                if (dent[0] == AO_FAT_DENT_END) {
                        ao_fat_root_put(dent, *entry, 0);
-                       return 0;
+                       return -AO_FAT_EDIREOF;
                }
                if (dent[0] != AO_FAT_DENT_EMPTY && (dent[0xb] & 0xf) != 0xf) {
                        ao_fat_dirent_init(dent, *entry, dirent);
                        ao_fat_root_put(dent, *entry, 0);
                        (*entry)++;
-                       return 1;
+                       return AO_FAT_SUCCESS;
                }
                ao_fat_root_put(dent, *entry, 0);
                (*entry)++;
        }
 }
 
+#if FAT_COMMANDS
+
 static const char *filesystem_errors[] = {
        [AO_FAT_FILESYSTEM_SUCCESS] = "FAT file system operating normally",
        [AO_FAT_FILESYSTEM_MBR_READ_FAILURE] = "MBR media read error",
@@ -1224,8 +1236,9 @@ ao_fat_list_cmd(void)
        uint16_t                entry = 0;
        struct ao_fat_dirent    dirent;
        int                     i;
+       int8_t                  status;
 
-       while (ao_fat_readdir(&entry, &dirent)) {
+       while ((status = ao_fat_readdir(&entry, &dirent)) == AO_FAT_SUCCESS) {
                for (i = 0; i < 8; i++)
                        putchar(dirent.name[i]);
                putchar('.');
@@ -1235,6 +1248,8 @@ ao_fat_list_cmd(void)
                        putchar (dirent.attr & ao_fat_attr[i].bit ? ao_fat_attr[i].label : ' ');
                printf (" @%08x %d\n", dirent.cluster, dirent.size);
        }
+       if (status != -AO_FAT_EDIREOF)
+               printf ("readdir failed: %d\n", status);
 }
 
 static uint8_t
@@ -1255,6 +1270,8 @@ ao_fat_parse_name(char name[11])
                }
                ao_cmd_lex();
        }
+       while (c < 11)
+               name[c++] = ' ';
 }
 
 static void
@@ -1331,6 +1348,8 @@ static const struct ao_cmds ao_fat_cmds[] = {
        { 0, NULL },
 };
 
+#endif
+
 void
 ao_fat_init(void)
 {