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 | #include <malloc.h> |
| 28 | |
Jon Loeliger | 80eb47c | 2007-07-09 17:56:50 -0500 | [diff] [blame] | 29 | #if defined(CONFIG_CMD_FDOS) |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 30 | |
| 31 | #include "dos.h" |
| 32 | #include "fdos.h" |
| 33 | |
| 34 | static int cache_sect; |
| 35 | static unsigned char cache [SZ_STD_SECTOR]; |
| 36 | |
| 37 | |
| 38 | #define min(x,y) ((x)<(y)?(x):(y)) |
| 39 | |
| 40 | static int descend (Slot_t *parent, |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 41 | Fs_t *fs, |
| 42 | char *path); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 43 | |
| 44 | /*----------------------------------------------------------------------------- |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 45 | * init_subdir -- |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 46 | *----------------------------------------------------------------------------- |
| 47 | */ |
| 48 | void init_subdir (void) |
| 49 | { |
| 50 | cache_sect = -1; |
| 51 | } |
| 52 | /*----------------------------------------------------------------------------- |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 53 | * basename -- |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 54 | *----------------------------------------------------------------------------- |
| 55 | */ |
| 56 | char *basename (char *name) |
| 57 | { |
| 58 | register char *cptr; |
| 59 | |
| 60 | if (!name || !*name) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 61 | return (""); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 62 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 63 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 64 | for (cptr= name; *cptr++; ); |
| 65 | while (--cptr >= name) { |
| 66 | if (*cptr == '/') { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 67 | return (cptr + 1); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | return(name); |
| 71 | } |
| 72 | /*----------------------------------------------------------------------------- |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 73 | * root_map -- |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 74 | *----------------------------------------------------------------------------- |
| 75 | */ |
| 76 | static int root_map (Fs_t *fs, Slot_t *file, int where, int *len) |
| 77 | { |
| 78 | *len = min (*len, fs -> dir_len * SZ_STD_SECTOR - where); |
| 79 | if (*len < 0 ) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 80 | *len = 0; |
| 81 | return (-1); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 82 | } |
| 83 | return fs -> dir_start * SZ_STD_SECTOR + where; |
| 84 | } |
| 85 | /*----------------------------------------------------------------------------- |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 86 | * normal_map -- |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 87 | *----------------------------------------------------------------------------- |
| 88 | */ |
| 89 | static int normal_map (Fs_t *fs, Slot_t *file, int where, int *len) |
| 90 | { |
| 91 | int offset; |
| 92 | int NrClu; |
| 93 | unsigned short RelCluNr; |
| 94 | unsigned short CurCluNr; |
| 95 | unsigned short NewCluNr; |
| 96 | unsigned short AbsCluNr; |
| 97 | int clus_size; |
| 98 | |
| 99 | clus_size = fs -> cluster_size * SZ_STD_SECTOR; |
| 100 | offset = where % clus_size; |
| 101 | |
| 102 | *len = min (*len, file -> FileSize - where); |
| 103 | |
| 104 | if (*len < 0 ) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 105 | *len = 0; |
| 106 | return (0); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | if (file -> FirstAbsCluNr < 2){ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 110 | *len = 0; |
| 111 | return (0); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | RelCluNr = where / clus_size; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 115 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 116 | if (RelCluNr >= file -> PreviousRelCluNr){ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 117 | CurCluNr = file -> PreviousRelCluNr; |
| 118 | AbsCluNr = file -> PreviousAbsCluNr; |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 119 | } else { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 120 | CurCluNr = 0; |
| 121 | AbsCluNr = file -> FirstAbsCluNr; |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | |
| 125 | NrClu = (offset + *len - 1) / clus_size; |
| 126 | while (CurCluNr <= RelCluNr + NrClu) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 127 | if (CurCluNr == RelCluNr){ |
| 128 | /* we have reached the beginning of our zone. Save |
| 129 | * coordinates */ |
| 130 | file -> PreviousRelCluNr = RelCluNr; |
| 131 | file -> PreviousAbsCluNr = AbsCluNr; |
| 132 | } |
| 133 | NewCluNr = fat_decode (fs, AbsCluNr); |
| 134 | if (NewCluNr == 1 || NewCluNr == 0) { |
| 135 | PRINTF("Fat problem while decoding %d %x\n", |
| 136 | AbsCluNr, NewCluNr); |
| 137 | return (-1); |
| 138 | } |
| 139 | if (CurCluNr == RelCluNr + NrClu) { |
| 140 | break; |
| 141 | } |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 142 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 143 | if (CurCluNr < RelCluNr && NewCluNr == FAT12_END) { |
| 144 | *len = 0; |
| 145 | return 0; |
| 146 | } |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 147 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 148 | if (CurCluNr >= RelCluNr && NewCluNr != AbsCluNr + 1) |
| 149 | break; |
| 150 | CurCluNr++; |
| 151 | AbsCluNr = NewCluNr; |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | *len = min (*len, (1 + CurCluNr - RelCluNr) * clus_size - offset); |
| 155 | |
| 156 | return (((file -> PreviousAbsCluNr - 2) * fs -> cluster_size + |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 157 | fs -> dir_start + fs -> dir_len) * |
| 158 | SZ_STD_SECTOR + offset); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 159 | } |
| 160 | /*----------------------------------------------------------------------------- |
| 161 | * open_subdir -- open the subdir containing the file |
| 162 | *----------------------------------------------------------------------------- |
| 163 | */ |
| 164 | int open_subdir (File_t *desc) |
| 165 | { |
| 166 | char *pathname; |
| 167 | char *tmp, *s, *path; |
| 168 | char terminator; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 169 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 170 | if ((pathname = (char *)malloc (MAX_PATH)) == NULL) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 171 | return (-1); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 172 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 173 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 174 | strcpy (pathname, desc -> name); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 175 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 176 | /* Suppress file name */ |
| 177 | tmp = basename (pathname); |
| 178 | *tmp = '\0'; |
| 179 | |
| 180 | /* root directory init */ |
| 181 | desc -> subdir.FirstAbsCluNr = 0; |
| 182 | desc -> subdir.FileSize = -1; |
| 183 | desc -> subdir.map = root_map; |
| 184 | desc -> subdir.dir.attr = ATTR_DIRECTORY; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 185 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 186 | tmp = pathname; |
| 187 | for (s = tmp; ; ++s) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 188 | if (*s == '/' || *s == '\0') { |
| 189 | path = tmp; |
| 190 | terminator = *s; |
| 191 | *s = '\0'; |
| 192 | if (s != tmp && strcmp (path,".")) { |
| 193 | if (descend (&desc -> subdir, desc -> fs, path) < 0) { |
| 194 | free (pathname); |
| 195 | return (-1); |
| 196 | } |
| 197 | } |
| 198 | if (terminator == 0) { |
| 199 | break; |
| 200 | } |
| 201 | tmp = s + 1; |
| 202 | } |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 203 | } |
| 204 | free (pathname); |
| 205 | return (0); |
| 206 | } |
| 207 | /*----------------------------------------------------------------------------- |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 208 | * descend -- |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 209 | *----------------------------------------------------------------------------- |
| 210 | */ |
| 211 | static int descend (Slot_t *parent, |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 212 | Fs_t *fs, |
| 213 | char *path) |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 214 | { |
| 215 | int entry; |
| 216 | Slot_t SubDir; |
| 217 | |
| 218 | if(path[0] == '\0' || strcmp (path, ".") == 0) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 219 | return (0); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 220 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 221 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 222 | |
| 223 | entry = 0; |
| 224 | if (vfat_lookup (parent, |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 225 | fs, |
| 226 | &(SubDir.dir), |
| 227 | &entry, |
| 228 | 0, |
| 229 | path, |
| 230 | ACCEPT_DIR | SINGLE | DO_OPEN, |
| 231 | 0, |
| 232 | &SubDir) == 0) { |
| 233 | *parent = SubDir; |
| 234 | return (0); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | if (strcmp(path, "..") == 0) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 238 | parent -> FileSize = -1; |
| 239 | parent -> FirstAbsCluNr = 0; |
| 240 | parent -> map = root_map; |
| 241 | return (0); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 242 | } |
| 243 | return (-1); |
| 244 | } |
| 245 | /*----------------------------------------------------------------------------- |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 246 | * open_file -- |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 247 | *----------------------------------------------------------------------------- |
| 248 | */ |
| 249 | int open_file (Slot_t *file, Directory_t *dir) |
| 250 | { |
| 251 | int first; |
| 252 | unsigned long size; |
| 253 | |
| 254 | first = __le16_to_cpu (dir -> start); |
| 255 | |
| 256 | if(first == 0 && |
| 257 | (dir -> attr & ATTR_DIRECTORY) != 0) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 258 | file -> FirstAbsCluNr = 0; |
| 259 | file -> FileSize = -1; |
| 260 | file -> map = root_map; |
| 261 | return (0); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 262 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 263 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 264 | if ((dir -> attr & ATTR_DIRECTORY) != 0) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 265 | size = (1UL << 31) - 1; |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 266 | } |
| 267 | else { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 268 | size = __le32_to_cpu (dir -> size); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | file -> map = normal_map; |
| 272 | file -> FirstAbsCluNr = first; |
| 273 | file -> PreviousRelCluNr = 0xffff; |
| 274 | file -> FileSize = size; |
| 275 | return (0); |
| 276 | } |
| 277 | /*----------------------------------------------------------------------------- |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 278 | * read_file -- |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 279 | *----------------------------------------------------------------------------- |
| 280 | */ |
| 281 | int read_file (Fs_t *fs, |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 282 | Slot_t *file, |
| 283 | char *buf, |
| 284 | int where, |
| 285 | int len) |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 286 | { |
| 287 | int pos; |
| 288 | int read, nb, sect, offset; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 289 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 290 | pos = file -> map (fs, file, where, &len); |
| 291 | if (pos < 0) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 292 | return -1; |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 293 | } |
| 294 | if (len == 0) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 295 | return (0); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | /* Compute sector number */ |
| 299 | sect = pos / SZ_STD_SECTOR; |
| 300 | offset = pos % SZ_STD_SECTOR; |
| 301 | read = 0; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 302 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 303 | if (offset) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 304 | /* Read doesn't start at the sector beginning. We need to use our */ |
| 305 | /* cache */ |
| 306 | if (sect != cache_sect) { |
| 307 | if (dev_read (cache, sect, 1) < 0) { |
| 308 | return (-1); |
| 309 | } |
| 310 | cache_sect = sect; |
| 311 | } |
| 312 | nb = min (len, SZ_STD_SECTOR - offset); |
| 313 | |
| 314 | memcpy (buf, cache + offset, nb); |
| 315 | read += nb; |
| 316 | len -= nb; |
| 317 | sect += 1; |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | if (len > SZ_STD_SECTOR) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 321 | nb = (len - 1) / SZ_STD_SECTOR; |
| 322 | if (dev_read (buf + read, sect, nb) < 0) { |
| 323 | return ((read) ? read : -1); |
| 324 | } |
| 325 | /* update sector position */ |
| 326 | sect += nb; |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 327 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 328 | /* Update byte position */ |
| 329 | nb *= SZ_STD_SECTOR; |
| 330 | read += nb; |
| 331 | len -= nb; |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | if (len) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 335 | if (sect != cache_sect) { |
| 336 | if (dev_read (cache, sect, 1) < 0) { |
| 337 | return ((read) ? read : -1); |
| 338 | cache_sect = -1; |
| 339 | } |
| 340 | cache_sect = sect; |
| 341 | } |
| 342 | |
| 343 | memcpy (buf + read, cache, len); |
| 344 | read += len; |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 345 | } |
| 346 | return (read); |
| 347 | } |
| 348 | #endif |