]> git.ipfire.org Git - thirdparty/u-boot.git/blame - fs/fs.c
fs: Fully populate the filesystem method struct
[thirdparty/u-boot.git] / fs / fs.c
CommitLineData
045fa1e1
SW
1/*
2 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#include <config.h>
18#include <common.h>
19#include <part.h>
20#include <ext4fs.h>
21#include <fat.h>
22#include <fs.h>
23
a1b231ce
SW
24DECLARE_GLOBAL_DATA_PTR;
25
045fa1e1
SW
26static block_dev_desc_t *fs_dev_desc;
27static disk_partition_t fs_partition;
28static int fs_type = FS_TYPE_ANY;
29
436e2b73
SG
30static inline int fs_probe_unsupported(void)
31{
32 printf("** Unrecognized filesystem type **\n");
33 return -1;
34}
35
045fa1e1
SW
36static inline int fs_ls_unsupported(const char *dirname)
37{
38 printf("** Unrecognized filesystem type **\n");
39 return -1;
40}
41
42static inline int fs_read_unsupported(const char *filename, ulong addr,
43 int offset, int len)
44{
45 printf("** Unrecognized filesystem type **\n");
46 return -1;
47}
48
436e2b73
SG
49static inline void fs_close_unsupported(void)
50{
51}
52
045fa1e1
SW
53#ifdef CONFIG_FS_FAT
54static int fs_probe_fat(void)
55{
56 return fat_set_blk_dev(fs_dev_desc, &fs_partition);
57}
58
59static void fs_close_fat(void)
60{
61}
62
63#define fs_ls_fat file_fat_ls
64
65static int fs_read_fat(const char *filename, ulong addr, int offset, int len)
66{
67 int len_read;
68
69 len_read = file_fat_read_at(filename, offset,
70 (unsigned char *)addr, len);
71 if (len_read == -1) {
72 printf("** Unable to read file %s **\n", filename);
73 return -1;
74 }
75
76 return len_read;
77}
78#else
79static inline int fs_probe_fat(void)
80{
81 return -1;
82}
83
84static inline void fs_close_fat(void)
85{
86}
87
88#define fs_ls_fat fs_ls_unsupported
89#define fs_read_fat fs_read_unsupported
90#endif
91
92#ifdef CONFIG_FS_EXT4
93static int fs_probe_ext(void)
94{
95 ext4fs_set_blk_dev(fs_dev_desc, &fs_partition);
96
97 if (!ext4fs_mount(fs_partition.size)) {
98 ext4fs_close();
99 return -1;
100 }
101
102 return 0;
103}
104
105static void fs_close_ext(void)
106{
107 ext4fs_close();
108}
109
110#define fs_ls_ext ext4fs_ls
111
112static int fs_read_ext(const char *filename, ulong addr, int offset, int len)
113{
114 int file_len;
115 int len_read;
116
117 if (offset != 0) {
118 printf("** Cannot support non-zero offset **\n");
119 return -1;
120 }
121
122 file_len = ext4fs_open(filename);
123 if (file_len < 0) {
124 printf("** File not found %s **\n", filename);
125 ext4fs_close();
126 return -1;
127 }
128
129 if (len == 0)
130 len = file_len;
131
132 len_read = ext4fs_read((char *)addr, len);
133 ext4fs_close();
134
135 if (len_read != len) {
136 printf("** Unable to read file %s **\n", filename);
137 return -1;
138 }
139
140 return len_read;
141}
142#else
143static inline int fs_probe_ext(void)
144{
145 return -1;
146}
147
148static inline void fs_close_ext(void)
149{
150}
151
152#define fs_ls_ext fs_ls_unsupported
153#define fs_read_ext fs_read_unsupported
154#endif
155
436e2b73 156struct fstype_info {
045fa1e1
SW
157 int fstype;
158 int (*probe)(void);
436e2b73
SG
159 int (*ls)(const char *dirname);
160 int (*read)(const char *filename, ulong addr, int offset, int len);
161 void (*close)(void);
162};
163
164static struct fstype_info fstypes[] = {
165#ifdef CONFIG_FS_FAT
045fa1e1
SW
166 {
167 .fstype = FS_TYPE_FAT,
168 .probe = fs_probe_fat,
436e2b73
SG
169 .close = fs_close_fat,
170 .ls = file_fat_ls,
171 .read = fs_read_fat,
045fa1e1 172 },
436e2b73
SG
173#endif
174#ifdef CONFIG_FS_EXT4
045fa1e1
SW
175 {
176 .fstype = FS_TYPE_EXT,
177 .probe = fs_probe_ext,
436e2b73
SG
178 .close = fs_close_ext,
179 .ls = ext4fs_ls,
180 .read = fs_read_ext,
181 },
182#endif
183 {
184 .fstype = FS_TYPE_ANY,
185 .probe = fs_probe_unsupported,
186 .close = fs_close_unsupported,
187 .ls = fs_ls_unsupported,
188 .read = fs_read_unsupported,
045fa1e1
SW
189 },
190};
191
192int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
193{
436e2b73 194 struct fstype_info *info;
045fa1e1 195 int part, i;
a1b231ce
SW
196#ifdef CONFIG_NEEDS_MANUAL_RELOC
197 static int relocated;
198
199 if (!relocated) {
436e2b73
SG
200 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
201 i++, info++) {
202 info->probe += gd->reloc_off;
203 info->close += gd->reloc_off;
204 info->ls += gd->reloc_off;
205 info->read += gd->reloc_off;
206 }
a1b231ce
SW
207 relocated = 1;
208 }
209#endif
045fa1e1
SW
210
211 part = get_device_and_partition(ifname, dev_part_str, &fs_dev_desc,
212 &fs_partition, 1);
213 if (part < 0)
214 return -1;
215
436e2b73
SG
216 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
217 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
218 fstype != info->fstype)
045fa1e1
SW
219 continue;
220
436e2b73
SG
221 if (!info->probe()) {
222 fs_type = info->fstype;
045fa1e1
SW
223 return 0;
224 }
225 }
226
045fa1e1
SW
227 return -1;
228}
229
230static void fs_close(void)
231{
232 switch (fs_type) {
233 case FS_TYPE_FAT:
234 fs_close_fat();
235 break;
236 case FS_TYPE_EXT:
237 fs_close_ext();
238 break;
239 default:
240 break;
241 }
242
243 fs_type = FS_TYPE_ANY;
244}
245
246int fs_ls(const char *dirname)
247{
248 int ret;
249
250 switch (fs_type) {
251 case FS_TYPE_FAT:
252 ret = fs_ls_fat(dirname);
253 break;
254 case FS_TYPE_EXT:
255 ret = fs_ls_ext(dirname);
256 break;
257 default:
258 ret = fs_ls_unsupported(dirname);
259 break;
260 }
261
262 fs_close();
263
264 return ret;
265}
266
267int fs_read(const char *filename, ulong addr, int offset, int len)
268{
269 int ret;
270
271 switch (fs_type) {
272 case FS_TYPE_FAT:
273 ret = fs_read_fat(filename, addr, offset, len);
274 break;
275 case FS_TYPE_EXT:
276 ret = fs_read_ext(filename, addr, offset, len);
277 break;
278 default:
279 ret = fs_read_unsupported(filename, addr, offset, len);
280 break;
281 }
282
283 fs_close();
284
285 return ret;
286}
287
f9b55e22 288int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
3f83c87e 289 int fstype, int cmdline_base)
045fa1e1
SW
290{
291 unsigned long addr;
292 const char *addr_str;
293 const char *filename;
294 unsigned long bytes;
295 unsigned long pos;
296 int len_read;
da1fd96c 297 unsigned long time;
045fa1e1 298
e9b0f99e
SW
299 if (argc < 2)
300 return CMD_RET_USAGE;
301 if (argc > 7)
045fa1e1
SW
302 return CMD_RET_USAGE;
303
e9b0f99e 304 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
045fa1e1
SW
305 return 1;
306
307 if (argc >= 4) {
3f83c87e 308 addr = simple_strtoul(argv[3], NULL, cmdline_base);
045fa1e1
SW
309 } else {
310 addr_str = getenv("loadaddr");
311 if (addr_str != NULL)
312 addr = simple_strtoul(addr_str, NULL, 16);
313 else
314 addr = CONFIG_SYS_LOAD_ADDR;
315 }
316 if (argc >= 5) {
317 filename = argv[4];
318 } else {
319 filename = getenv("bootfile");
320 if (!filename) {
321 puts("** No boot file defined **\n");
322 return 1;
323 }
324 }
325 if (argc >= 6)
3f83c87e 326 bytes = simple_strtoul(argv[5], NULL, cmdline_base);
045fa1e1
SW
327 else
328 bytes = 0;
329 if (argc >= 7)
3f83c87e 330 pos = simple_strtoul(argv[6], NULL, cmdline_base);
045fa1e1
SW
331 else
332 pos = 0;
333
da1fd96c 334 time = get_timer(0);
045fa1e1 335 len_read = fs_read(filename, addr, pos, bytes);
da1fd96c 336 time = get_timer(time);
045fa1e1
SW
337 if (len_read <= 0)
338 return 1;
339
da1fd96c
AB
340 printf("%d bytes read in %lu ms", len_read, time);
341 if (time > 0) {
342 puts(" (");
343 print_size(len_read / time * 1000, "/s");
344 puts(")");
345 }
346 puts("\n");
045fa1e1 347
49c4f037 348 setenv_hex("filesize", len_read);
045fa1e1
SW
349
350 return 0;
351}
352
353int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
354 int fstype)
355{
356 if (argc < 2)
357 return CMD_RET_USAGE;
e9b0f99e
SW
358 if (argc > 4)
359 return CMD_RET_USAGE;
045fa1e1
SW
360
361 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
362 return 1;
363
e9b0f99e 364 if (fs_ls(argc >= 4 ? argv[3] : "/"))
045fa1e1
SW
365 return 1;
366
367 return 0;
368}