file.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * file.c
  3. *
  4. * Mini "VFS" by Marcus Sundberg
  5. *
  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
  8. *
  9. * See file CREDITS for list of people who contributed to this
  10. * project.
  11. *
  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.
  16. *
  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.
  21. *
  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,
  25. * MA 02111-1307 USA
  26. */
  27. #include <common.h>
  28. #include <config.h>
  29. #include <malloc.h>
  30. #include <fat.h>
  31. #include <linux/stat.h>
  32. #include <linux/time.h>
  33. /* Supported filesystems */
  34. static const struct filesystem filesystems[] = {
  35. { file_fat_detectfs, file_fat_ls, file_fat_read, "FAT" },
  36. };
  37. #define NUM_FILESYS (sizeof(filesystems)/sizeof(struct filesystem))
  38. /* The filesystem which was last detected */
  39. static int current_filesystem = FSTYPE_NONE;
  40. /* The current working directory */
  41. #define CWD_LEN 511
  42. char file_cwd[CWD_LEN+1] = "/";
  43. const char *
  44. file_getfsname(int idx)
  45. {
  46. if (idx < 0 || idx >= NUM_FILESYS) return NULL;
  47. return filesystems[idx].name;
  48. }
  49. static void
  50. pathcpy(char *dest, const char *src)
  51. {
  52. char *origdest = dest;
  53. do {
  54. if (dest-file_cwd >= CWD_LEN) {
  55. *dest = '\0';
  56. return;
  57. }
  58. *(dest) = *(src);
  59. if (*src == '\0') {
  60. if (dest-- != origdest && ISDIRDELIM(*dest)) {
  61. *dest = '\0';
  62. }
  63. return;
  64. }
  65. ++dest;
  66. if (ISDIRDELIM(*src)) {
  67. while (ISDIRDELIM(*src)) src++;
  68. } else {
  69. src++;
  70. }
  71. } while (1);
  72. }
  73. int
  74. file_cd(const char *path)
  75. {
  76. if (ISDIRDELIM(*path)) {
  77. while (ISDIRDELIM(*path)) path++;
  78. strncpy(file_cwd+1, path, CWD_LEN-1);
  79. } else {
  80. const char *origpath = path;
  81. char *tmpstr = file_cwd;
  82. int back = 0;
  83. while (*tmpstr != '\0') tmpstr++;
  84. do {
  85. tmpstr--;
  86. } while (ISDIRDELIM(*tmpstr));
  87. while (*path == '.') {
  88. path++;
  89. while (*path == '.') {
  90. path++;
  91. back++;
  92. }
  93. if (*path != '\0' && !ISDIRDELIM(*path)) {
  94. path = origpath;
  95. back = 0;
  96. break;
  97. }
  98. while (ISDIRDELIM(*path)) path++;
  99. origpath = path;
  100. }
  101. while (back--) {
  102. /* Strip off path component */
  103. while (!ISDIRDELIM(*tmpstr)) {
  104. tmpstr--;
  105. }
  106. if (tmpstr == file_cwd) {
  107. /* Incremented again right after the loop. */
  108. tmpstr--;
  109. break;
  110. }
  111. /* Skip delimiters */
  112. while (ISDIRDELIM(*tmpstr)) tmpstr--;
  113. }
  114. tmpstr++;
  115. if (*path == '\0') {
  116. if (tmpstr == file_cwd) {
  117. *tmpstr = '/';
  118. tmpstr++;
  119. }
  120. *tmpstr = '\0';
  121. return 0;
  122. }
  123. *tmpstr = '/';
  124. pathcpy(tmpstr+1, path);
  125. }
  126. return 0;
  127. }
  128. int
  129. file_detectfs(void)
  130. {
  131. int i;
  132. current_filesystem = FSTYPE_NONE;
  133. for (i = 0; i < NUM_FILESYS; i++) {
  134. if (filesystems[i].detect() == 0) {
  135. strcpy(file_cwd, "/");
  136. current_filesystem = i;
  137. break;
  138. }
  139. }
  140. return current_filesystem;
  141. }
  142. int
  143. file_ls(const char *dir)
  144. {
  145. char fullpath[1024];
  146. const char *arg;
  147. if (current_filesystem == FSTYPE_NONE) {
  148. printf("Can't list files without a filesystem!\n");
  149. return -1;
  150. }
  151. if (ISDIRDELIM(*dir)) {
  152. arg = dir;
  153. } else {
  154. sprintf(fullpath, "%s/%s", file_cwd, dir);
  155. arg = fullpath;
  156. }
  157. return filesystems[current_filesystem].ls(arg);
  158. }
  159. long
  160. file_read(const char *filename, void *buffer, unsigned long maxsize)
  161. {
  162. char fullpath[1024];
  163. const char *arg;
  164. if (current_filesystem == FSTYPE_NONE) {
  165. printf("Can't load file without a filesystem!\n");
  166. return -1;
  167. }
  168. if (ISDIRDELIM(*filename)) {
  169. arg = filename;
  170. } else {
  171. sprintf(fullpath, "%s/%s", file_cwd, filename);
  172. arg = fullpath;
  173. }
  174. return filesystems[current_filesystem].read(arg, buffer, maxsize);
  175. }