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