4 * Mini "VFS" by Marcus Sundberg
6 * 2002-07-28 - rjones@nexus-tech.net - ported to ppcboot v1.1.6
7 * 2003-03-10 - kharris@nexus-tech.net - ported to uboot
9 * See file CREDITS for list of people who contributed to this
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
32 #include <linux/stat.h>
33 #include <linux/time.h>
35 #if (CONFIG_COMMANDS & CFG_CMD_FAT)
37 /* Supported filesystems */
38 static const struct filesystem filesystems[] = {
39 { file_fat_detectfs, file_fat_ls, file_fat_read, "FAT" },
41 #define NUM_FILESYS (sizeof(filesystems)/sizeof(struct filesystem))
43 /* The filesystem which was last detected */
44 static int current_filesystem = FSTYPE_NONE;
46 /* The current working directory */
48 char file_cwd[CWD_LEN+1] = "/";
51 file_getfsname(int idx)
53 if (idx < 0 || idx >= NUM_FILESYS) return NULL;
55 return filesystems[idx].name;
60 pathcpy(char *dest, const char *src)
62 char *origdest = dest;
65 if (dest-file_cwd >= CWD_LEN) {
71 if (dest-- != origdest && ISDIRDELIM(*dest)) {
77 if (ISDIRDELIM(*src)) {
78 while (ISDIRDELIM(*src)) src++;
87 file_cd(const char *path)
89 if (ISDIRDELIM(*path)) {
90 while (ISDIRDELIM(*path)) path++;
91 strncpy(file_cwd+1, path, CWD_LEN-1);
93 const char *origpath = path;
94 char *tmpstr = file_cwd;
97 while (*tmpstr != '\0') tmpstr++;
100 } while (ISDIRDELIM(*tmpstr));
102 while (*path == '.') {
104 while (*path == '.') {
108 if (*path != '\0' && !ISDIRDELIM(*path)) {
113 while (ISDIRDELIM(*path)) path++;
118 /* Strip off path component */
119 while (!ISDIRDELIM(*tmpstr)) {
122 if (tmpstr == file_cwd) {
123 /* Incremented again right after the loop. */
127 /* Skip delimiters */
128 while (ISDIRDELIM(*tmpstr)) tmpstr--;
132 if (tmpstr == file_cwd) {
140 pathcpy(tmpstr+1, path);
152 current_filesystem = FSTYPE_NONE;
154 for (i = 0; i < NUM_FILESYS; i++) {
155 if (filesystems[i].detect() == 0) {
156 strcpy(file_cwd, "/");
157 current_filesystem = i;
162 return current_filesystem;
167 file_ls(const char *dir)
172 if (current_filesystem == FSTYPE_NONE) {
173 printf("Can't list files without a filesystem!\n");
177 if (ISDIRDELIM(*dir)) {
180 sprintf(fullpath, "%s/%s", file_cwd, dir);
183 return filesystems[current_filesystem].ls(arg);
188 file_read(const char *filename, void *buffer, unsigned long maxsize)
193 if (current_filesystem == FSTYPE_NONE) {
194 printf("Can't load file without a filesystem!\n");
198 if (ISDIRDELIM(*filename)) {
201 sprintf(fullpath, "%s/%s", file_cwd, filename);
205 return filesystems[current_filesystem].read(arg, buffer, maxsize);
208 #endif /* #if (CONFIG_COMMANDS & CFG_CMD_FAT) */