fdos.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. #include <malloc.h>
  27. #include "dos.h"
  28. #include "fdos.h"
  29. const char *month [] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
  30. Fs_t fs;
  31. File_t file;
  32. /*-----------------------------------------------------------------------------
  33. * dos_open --
  34. *-----------------------------------------------------------------------------
  35. */
  36. int dos_open(char *name)
  37. {
  38. int lg;
  39. int entry;
  40. char *fname;
  41. /* We need to suppress the " char around the name */
  42. if (name [0] == '"') {
  43. name ++;
  44. }
  45. lg = strlen (name);
  46. if (name [lg - 1] == '"') {
  47. name [lg - 1] = '\0';
  48. }
  49. /* Open file system */
  50. if (fs_init (&fs) < 0) {
  51. return -1;
  52. }
  53. /* Init the file descriptor */
  54. file.name = name;
  55. file.fs = &fs;
  56. /* find the subdirectory containing the file */
  57. if (open_subdir (&file) < 0) {
  58. return (-1);
  59. }
  60. fname = basename (name);
  61. /* if we try to open root directory */
  62. if (*fname == '\0') {
  63. file.file = file.subdir;
  64. return (0);
  65. }
  66. /* find the file in the subdir */
  67. entry = 0;
  68. if (vfat_lookup (&file.subdir,
  69. file.fs,
  70. &file.file.dir,
  71. &entry,
  72. 0,
  73. fname,
  74. ACCEPT_DIR | ACCEPT_PLAIN | SINGLE | DO_OPEN,
  75. 0,
  76. &file.file) != 0) {
  77. /* File not found */
  78. printf ("File not found\n");
  79. return (-1);
  80. }
  81. return 0;
  82. }
  83. /*-----------------------------------------------------------------------------
  84. * dos_read --
  85. *-----------------------------------------------------------------------------
  86. */
  87. int dos_read (ulong addr)
  88. {
  89. int read = 0, nb;
  90. /* Try to boot a directory ? */
  91. if (file.file.dir.attr & (ATTR_DIRECTORY | ATTR_VOLUME)) {
  92. printf ("Unable to boot %s !!\n", file.name);
  93. return (-1);
  94. }
  95. while (read < file.file.FileSize) {
  96. PRINTF ("read_file (%ld)\n", (file.file.FileSize - read));
  97. nb = read_file (&fs,
  98. &file.file,
  99. (char *)addr + read,
  100. read,
  101. (file.file.FileSize - read));
  102. PRINTF ("read_file -> %d\n", nb);
  103. if (nb < 0) {
  104. printf ("read error\n");
  105. return (-1);
  106. }
  107. read += nb;
  108. }
  109. return (read);
  110. }
  111. /*-----------------------------------------------------------------------------
  112. * dos_dir --
  113. *-----------------------------------------------------------------------------
  114. */
  115. int dos_dir (void)
  116. {
  117. int entry;
  118. Directory_t dir;
  119. char *name;
  120. if ((file.file.dir.attr & ATTR_DIRECTORY) == 0) {
  121. printf ("%s: not a directory !!\n", file.name);
  122. return (1);
  123. }
  124. entry = 0;
  125. if ((name = malloc (MAX_VNAMELEN + 1)) == NULL) {
  126. PRINTF ("Allcation error\n");
  127. return (1);
  128. }
  129. while (vfat_lookup (&file.file,
  130. file.fs,
  131. &dir,
  132. &entry,
  133. 0,
  134. NULL,
  135. ACCEPT_DIR | ACCEPT_PLAIN | MATCH_ANY,
  136. name,
  137. NULL) == 0) {
  138. /* Display file info */
  139. printf ("%3.3s %9d %s %02d %04d %02d:%02d:%02d %s\n",
  140. (dir.attr & ATTR_DIRECTORY) ? "dir" : " ",
  141. __le32_to_cpu (dir.size),
  142. month [DOS_MONTH (&dir) - 1],
  143. DOS_DAY (&dir),
  144. DOS_YEAR (&dir),
  145. DOS_HOUR (&dir),
  146. DOS_MINUTE (&dir),
  147. DOS_SEC (&dir),
  148. name);
  149. }
  150. free (name);
  151. return (0);
  152. }