fdos.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * (C) Copyright 2002
  3. * Stäubli Faverges - <www.staubli.com>
  4. * Pierre AUBERT p.aubert@staubli.com
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. */
  24. #include <common.h>
  25. #include <config.h>
  26. #if (CONFIG_COMMANDS & CFG_CMD_FDOS)
  27. #include <malloc.h>
  28. #include "dos.h"
  29. #include "fdos.h"
  30. const char *month [] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
  31. Fs_t fs;
  32. File_t file;
  33. /*-----------------------------------------------------------------------------
  34. * dos_open --
  35. *-----------------------------------------------------------------------------
  36. */
  37. int dos_open(char *name)
  38. {
  39. int lg;
  40. int entry;
  41. char *fname;
  42. /* We need to suppress the " char around the name */
  43. if (name [0] == '"') {
  44. name ++;
  45. }
  46. lg = strlen (name);
  47. if (name [lg - 1] == '"') {
  48. name [lg - 1] = '\0';
  49. }
  50. /* Open file system */
  51. if (fs_init (&fs) < 0) {
  52. return -1;
  53. }
  54. /* Init the file descriptor */
  55. file.name = name;
  56. file.fs = &fs;
  57. /* find the subdirectory containing the file */
  58. if (open_subdir (&file) < 0) {
  59. return (-1);
  60. }
  61. fname = basename (name);
  62. /* if we try to open root directory */
  63. if (*fname == '\0') {
  64. file.file = file.subdir;
  65. return (0);
  66. }
  67. /* find the file in the subdir */
  68. entry = 0;
  69. if (vfat_lookup (&file.subdir,
  70. file.fs,
  71. &file.file.dir,
  72. &entry,
  73. 0,
  74. fname,
  75. ACCEPT_DIR | ACCEPT_PLAIN | SINGLE | DO_OPEN,
  76. 0,
  77. &file.file) != 0) {
  78. /* File not found */
  79. printf ("File not found\n");
  80. return (-1);
  81. }
  82. return 0;
  83. }
  84. /*-----------------------------------------------------------------------------
  85. * dos_read --
  86. *-----------------------------------------------------------------------------
  87. */
  88. int dos_read (ulong addr)
  89. {
  90. int read = 0, nb;
  91. /* Try to boot a directory ? */
  92. if (file.file.dir.attr & (ATTR_DIRECTORY | ATTR_VOLUME)) {
  93. printf ("Unable to boot %s !!\n", file.name);
  94. return (-1);
  95. }
  96. while (read < file.file.FileSize) {
  97. PRINTF ("read_file (%ld)\n", (file.file.FileSize - read));
  98. nb = read_file (&fs,
  99. &file.file,
  100. (char *)addr + read,
  101. read,
  102. (file.file.FileSize - read));
  103. PRINTF ("read_file -> %d\n", nb);
  104. if (nb < 0) {
  105. printf ("read error\n");
  106. return (-1);
  107. }
  108. read += nb;
  109. }
  110. return (read);
  111. }
  112. /*-----------------------------------------------------------------------------
  113. * dos_dir --
  114. *-----------------------------------------------------------------------------
  115. */
  116. int dos_dir (void)
  117. {
  118. int entry;
  119. Directory_t dir;
  120. char *name;
  121. if ((file.file.dir.attr & ATTR_DIRECTORY) == 0) {
  122. printf ("%s: not a directory !!\n", file.name);
  123. return (1);
  124. }
  125. entry = 0;
  126. if ((name = malloc (MAX_VNAMELEN + 1)) == NULL) {
  127. PRINTF ("Allcation error\n");
  128. return (1);
  129. }
  130. while (vfat_lookup (&file.file,
  131. file.fs,
  132. &dir,
  133. &entry,
  134. 0,
  135. NULL,
  136. ACCEPT_DIR | ACCEPT_PLAIN | MATCH_ANY,
  137. name,
  138. NULL) == 0) {
  139. /* Display file info */
  140. printf ("%3.3s %9d %s %02d %04d %02d:%02d:%02d %s\n",
  141. (dir.attr & ATTR_DIRECTORY) ? "dir" : " ",
  142. __le32_to_cpu (dir.size),
  143. month [DOS_MONTH (&dir) - 1],
  144. DOS_DAY (&dir),
  145. DOS_YEAR (&dir),
  146. DOS_HOUR (&dir),
  147. DOS_MINUTE (&dir),
  148. DOS_SEC (&dir),
  149. name);
  150. }
  151. free (name);
  152. return (0);
  153. }
  154. #endif