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 * SPDX-License-Identifier: GPL-2.0+
16 #include <linux/stat.h>
17 #include <linux/time.h>
19 /* Supported filesystems */
20 static const struct filesystem filesystems[] = {
21 { file_fat_detectfs, file_fat_ls, file_fat_read, "FAT" },
23 #define NUM_FILESYS (sizeof(filesystems)/sizeof(struct filesystem))
25 /* The filesystem which was last detected */
26 static int current_filesystem = FSTYPE_NONE;
28 /* The current working directory */
30 char file_cwd[CWD_LEN+1] = "/";
33 file_getfsname(int idx)
35 if (idx < 0 || idx >= NUM_FILESYS)
38 return filesystems[idx].name;
42 pathcpy(char *dest, const char *src)
44 char *origdest = dest;
47 if (dest-file_cwd >= CWD_LEN) {
53 if (dest-- != origdest && ISDIRDELIM(*dest)) {
61 while (ISDIRDELIM(*src)) src++;
68 file_cd(const char *path)
70 if (ISDIRDELIM(*path)) {
71 while (ISDIRDELIM(*path)) path++;
72 strncpy(file_cwd+1, path, CWD_LEN-1);
74 const char *origpath = path;
75 char *tmpstr = file_cwd;
78 while (*tmpstr != '\0') tmpstr++;
81 } while (ISDIRDELIM(*tmpstr));
83 while (*path == '.') {
85 while (*path == '.') {
89 if (*path != '\0' && !ISDIRDELIM(*path)) {
94 while (ISDIRDELIM(*path)) path++;
99 /* Strip off path component */
100 while (!ISDIRDELIM(*tmpstr)) {
103 if (tmpstr == file_cwd) {
104 /* Incremented again right after the loop. */
108 /* Skip delimiters */
109 while (ISDIRDELIM(*tmpstr)) tmpstr--;
113 if (tmpstr == file_cwd) {
121 pathcpy(tmpstr+1, path);
132 current_filesystem = FSTYPE_NONE;
134 for (i = 0; i < NUM_FILESYS; i++) {
135 if (filesystems[i].detect() == 0) {
136 strcpy(file_cwd, "/");
137 current_filesystem = i;
142 return current_filesystem;
146 file_ls(const char *dir)
151 if (current_filesystem == FSTYPE_NONE) {
152 printf("Can't list files without a filesystem!\n");
156 if (ISDIRDELIM(*dir)) {
159 sprintf(fullpath, "%s/%s", file_cwd, dir);
162 return filesystems[current_filesystem].ls(arg);
165 int file_read(const char *filename, void *buffer, int maxsize)
170 if (current_filesystem == FSTYPE_NONE) {
171 printf("Can't load file without a filesystem!\n");
175 if (ISDIRDELIM(*filename)) {
178 sprintf(fullpath, "%s/%s", file_cwd, filename);
182 return filesystems[current_filesystem].read(arg, buffer, maxsize);