blob: 4e2f479191551c7e8fc4b691ad5f5805b8f5881f [file] [log] [blame]
wdenk2262cfe2002-11-18 00:14:45 +00001/*
2 * (C) Copyright 2002
Albert ARIBAUDfa82f872011-08-04 18:45:45 +02003 * Stäubli Faverges - <www.staubli.com>
wdenk2262cfe2002-11-18 00:14:45 +00004 * Pierre AUBERT p.aubert@staubli.com
5 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
wdenk2262cfe2002-11-18 00:14:45 +00007 */
8
9#include <common.h>
10#include <config.h>
wdenk2262cfe2002-11-18 00:14:45 +000011#include <malloc.h>
Jean-Christophe PLAGNIOL-VILLARD08ab4e12008-08-31 04:24:56 +020012
wdenk2262cfe2002-11-18 00:14:45 +000013#include "dos.h"
14#include "fdos.h"
15
16
17const char *month [] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
18
19Fs_t fs;
20File_t file;
21
22/*-----------------------------------------------------------------------------
wdenk8bde7f72003-06-27 21:31:46 +000023 * dos_open --
wdenk2262cfe2002-11-18 00:14:45 +000024 *-----------------------------------------------------------------------------
25 */
26int dos_open(char *name)
27{
28 int lg;
29 int entry;
30 char *fname;
wdenk8bde7f72003-06-27 21:31:46 +000031
wdenk2262cfe2002-11-18 00:14:45 +000032 /* We need to suppress the " char around the name */
33 if (name [0] == '"') {
wdenk8bde7f72003-06-27 21:31:46 +000034 name ++;
wdenk2262cfe2002-11-18 00:14:45 +000035 }
36 lg = strlen (name);
37 if (name [lg - 1] == '"') {
wdenk8bde7f72003-06-27 21:31:46 +000038 name [lg - 1] = '\0';
wdenk2262cfe2002-11-18 00:14:45 +000039 }
40
41 /* Open file system */
42 if (fs_init (&fs) < 0) {
wdenk8bde7f72003-06-27 21:31:46 +000043 return -1;
wdenk2262cfe2002-11-18 00:14:45 +000044 }
45
46 /* Init the file descriptor */
47 file.name = name;
48 file.fs = &fs;
wdenk8bde7f72003-06-27 21:31:46 +000049
wdenk2262cfe2002-11-18 00:14:45 +000050 /* find the subdirectory containing the file */
51 if (open_subdir (&file) < 0) {
wdenk8bde7f72003-06-27 21:31:46 +000052 return (-1);
wdenk2262cfe2002-11-18 00:14:45 +000053 }
54
55 fname = basename (name);
56
57 /* if we try to open root directory */
58 if (*fname == '\0') {
wdenk8bde7f72003-06-27 21:31:46 +000059 file.file = file.subdir;
60 return (0);
wdenk2262cfe2002-11-18 00:14:45 +000061 }
wdenk8bde7f72003-06-27 21:31:46 +000062
wdenk2262cfe2002-11-18 00:14:45 +000063 /* find the file in the subdir */
64 entry = 0;
65 if (vfat_lookup (&file.subdir,
wdenk8bde7f72003-06-27 21:31:46 +000066 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);
wdenk2262cfe2002-11-18 00:14:45 +000077 }
78
79 return 0;
80}
81
82/*-----------------------------------------------------------------------------
wdenk8bde7f72003-06-27 21:31:46 +000083 * dos_read --
wdenk2262cfe2002-11-18 00:14:45 +000084 *-----------------------------------------------------------------------------
85 */
86int 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)) {
wdenk8bde7f72003-06-27 21:31:46 +000092 printf ("Unable to boot %s !!\n", file.name);
93 return (-1);
wdenk2262cfe2002-11-18 00:14:45 +000094 }
95 while (read < file.file.FileSize) {
wdenk8bde7f72003-06-27 21:31:46 +000096 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;
wdenk2262cfe2002-11-18 00:14:45 +0000108 }
109 return (read);
110}
111/*-----------------------------------------------------------------------------
wdenk8bde7f72003-06-27 21:31:46 +0000112 * dos_dir --
wdenk2262cfe2002-11-18 00:14:45 +0000113 *-----------------------------------------------------------------------------
114 */
115int dos_dir (void)
116{
117 int entry;
118 Directory_t dir;
119 char *name;
wdenk8bde7f72003-06-27 21:31:46 +0000120
121
wdenk2262cfe2002-11-18 00:14:45 +0000122 if ((file.file.dir.attr & ATTR_DIRECTORY) == 0) {
wdenk8bde7f72003-06-27 21:31:46 +0000123 printf ("%s: not a directory !!\n", file.name);
124 return (1);
wdenk2262cfe2002-11-18 00:14:45 +0000125 }
126 entry = 0;
127 if ((name = malloc (MAX_VNAMELEN + 1)) == NULL) {
wdenk8bde7f72003-06-27 21:31:46 +0000128 PRINTF ("Allcation error\n");
129 return (1);
wdenk2262cfe2002-11-18 00:14:45 +0000130 }
wdenk8bde7f72003-06-27 21:31:46 +0000131
wdenk2262cfe2002-11-18 00:14:45 +0000132 while (vfat_lookup (&file.file,
wdenk8bde7f72003-06-27 21:31:46 +0000133 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
wdenk2262cfe2002-11-18 00:14:45 +0000153 }
154 free (name);
155 return (0);
156}