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