summaryrefslogtreecommitdiffstats
path: root/fs/fdos/fdos.c
blob: 4e2f479191551c7e8fc4b691ad5f5805b8f5881f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 * (C) Copyright 2002
 * Stäubli Faverges - <www.staubli.com>
 * Pierre AUBERT  p.aubert@staubli.com
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */

#include <common.h>
#include <config.h>
#include <malloc.h>

#include "dos.h"
#include "fdos.h"


const char *month [] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

Fs_t    fs;
File_t  file;

/*-----------------------------------------------------------------------------
 * dos_open --
 *-----------------------------------------------------------------------------
 */
int dos_open(char *name)
{
    int lg;
    int entry;
    char *fname;

    /* We need to suppress the " char around the name                        */
    if (name [0] == '"') {
	name ++;
    }
    lg = strlen (name);
    if (name [lg - 1] == '"') {
	name [lg - 1] = '\0';
    }

    /* Open file system                                                      */
    if (fs_init (&fs) < 0) {
	return -1;
    }

    /* Init the file descriptor                                              */
    file.name = name;
    file.fs = &fs;

    /* find the subdirectory containing the file                             */
    if (open_subdir (&file) < 0) {
	return (-1);
    }

    fname = basename (name);

    /* if we try to open root directory                                      */
    if (*fname == '\0') {
	file.file = file.subdir;
	return (0);
    }

    /* find the file in the subdir                                           */
    entry = 0;
    if (vfat_lookup (&file.subdir,
		     file.fs,
		     &file.file.dir,
		     &entry,
		     0,
		     fname,
		     ACCEPT_DIR | ACCEPT_PLAIN | SINGLE | DO_OPEN,
		     0,
		     &file.file) != 0) {
	/* File not found                                                    */
	printf ("File not found\n");
	return (-1);
    }

    return 0;
}

/*-----------------------------------------------------------------------------
 * dos_read --
 *-----------------------------------------------------------------------------
 */
int dos_read (ulong addr)
{
    int read = 0, nb;

    /* Try to boot a directory ?                                             */
    if (file.file.dir.attr & (ATTR_DIRECTORY | ATTR_VOLUME)) {
	printf ("Unable to boot %s !!\n", file.name);
	return (-1);
    }
    while (read < file.file.FileSize) {
	PRINTF ("read_file (%ld)\n", (file.file.FileSize - read));
	nb = read_file (&fs,
			&file.file,
			(char *)addr + read,
			read,
			(file.file.FileSize - read));
	PRINTF ("read_file -> %d\n", nb);
	if (nb < 0) {
	    printf ("read error\n");
	    return (-1);
	}
	read += nb;
    }
    return (read);
}
/*-----------------------------------------------------------------------------
 * dos_dir --
 *-----------------------------------------------------------------------------
 */
int dos_dir (void)
{
    int entry;
    Directory_t dir;
    char *name;


    if ((file.file.dir.attr & ATTR_DIRECTORY) == 0) {
	printf ("%s: not a directory !!\n", file.name);
	return (1);
    }
    entry = 0;
    if ((name = malloc (MAX_VNAMELEN + 1)) == NULL) {
	PRINTF ("Allcation error\n");
	return (1);
    }

    while (vfat_lookup (&file.file,
			file.fs,
			&dir,
			&entry,
			0,
			NULL,
			ACCEPT_DIR | ACCEPT_PLAIN | MATCH_ANY,
			name,
			NULL) == 0) {
	/* Display file info                                                 */
	printf ("%3.3s %9d %s %02d %04d %02d:%02d:%02d %s\n",
		(dir.attr & ATTR_DIRECTORY) ? "dir" : "   ",
		__le32_to_cpu (dir.size),
		month [DOS_MONTH (&dir) - 1],
		DOS_DAY (&dir),
		DOS_YEAR (&dir),
		DOS_HOUR (&dir),
		DOS_MINUTE (&dir),
		DOS_SEC (&dir),
		name);

    }
    free (name);
    return (0);
}