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