blob: ea15c5f447bd3fb30ea4a5604a471b0b9a852110 [file] [log] [blame]
Stephen Warren045fa1e2012-10-22 06:43:51 +00001/*
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>
Simon Glass92ccc962012-12-26 09:53:35 +000023#include <sandboxfs.h>
Simon Glass117e0502012-12-26 09:53:32 +000024#include <asm/io.h>
Stephen Warren045fa1e2012-10-22 06:43:51 +000025
Stephen Warrena1b231c2012-10-30 07:50:47 +000026DECLARE_GLOBAL_DATA_PTR;
27
Stephen Warren045fa1e2012-10-22 06:43:51 +000028static block_dev_desc_t *fs_dev_desc;
29static disk_partition_t fs_partition;
30static int fs_type = FS_TYPE_ANY;
31
Simon Glass2ded0d42012-12-26 09:53:31 +000032static inline int fs_probe_unsupported(block_dev_desc_t *fs_dev_desc,
33 disk_partition_t *fs_partition)
Simon Glass436e2b72012-12-26 09:53:29 +000034{
35 printf("** Unrecognized filesystem type **\n");
36 return -1;
37}
38
Stephen Warren045fa1e2012-10-22 06:43:51 +000039static inline int fs_ls_unsupported(const char *dirname)
40{
Stephen Warren045fa1e2012-10-22 06:43:51 +000041 return -1;
42}
43
Stephen Warren61529162014-02-03 13:21:00 -070044static inline int fs_exists_unsupported(const char *filename)
45{
46 return 0;
47}
48
Simon Glass117e0502012-12-26 09:53:32 +000049static inline int fs_read_unsupported(const char *filename, void *buf,
Stephen Warren045fa1e2012-10-22 06:43:51 +000050 int offset, int len)
51{
Stephen Warren045fa1e2012-10-22 06:43:51 +000052 return -1;
53}
54
Simon Glassa8f6ab52013-04-20 08:42:50 +000055static inline int fs_write_unsupported(const char *filename, void *buf,
56 int offset, int len)
57{
58 return -1;
59}
60
Simon Glass436e2b72012-12-26 09:53:29 +000061static inline void fs_close_unsupported(void)
62{
63}
64
Simon Glass436e2b72012-12-26 09:53:29 +000065struct fstype_info {
Stephen Warren045fa1e2012-10-22 06:43:51 +000066 int fstype;
Stephen Warren377202b2014-02-03 13:21:01 -070067 /*
68 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
69 * should be false in most cases. For "virtual" filesystems which
70 * aren't based on a U-Boot block device (e.g. sandbox), this can be
71 * set to true. This should also be true for the dumm entry at the end
72 * of fstypes[], since that is essentially a "virtual" (non-existent)
73 * filesystem.
74 */
75 bool null_dev_desc_ok;
Simon Glass2ded0d42012-12-26 09:53:31 +000076 int (*probe)(block_dev_desc_t *fs_dev_desc,
77 disk_partition_t *fs_partition);
Simon Glass436e2b72012-12-26 09:53:29 +000078 int (*ls)(const char *dirname);
Stephen Warren61529162014-02-03 13:21:00 -070079 int (*exists)(const char *filename);
Simon Glass117e0502012-12-26 09:53:32 +000080 int (*read)(const char *filename, void *buf, int offset, int len);
Simon Glassa8f6ab52013-04-20 08:42:50 +000081 int (*write)(const char *filename, void *buf, int offset, int len);
Simon Glass436e2b72012-12-26 09:53:29 +000082 void (*close)(void);
83};
84
85static struct fstype_info fstypes[] = {
86#ifdef CONFIG_FS_FAT
Stephen Warren045fa1e2012-10-22 06:43:51 +000087 {
88 .fstype = FS_TYPE_FAT,
Stephen Warren377202b2014-02-03 13:21:01 -070089 .null_dev_desc_ok = false,
Simon Glasse6d52412012-12-26 09:53:33 +000090 .probe = fat_set_blk_dev,
91 .close = fat_close,
Simon Glass436e2b72012-12-26 09:53:29 +000092 .ls = file_fat_ls,
Stephen Warrenb7b5f312014-02-03 13:21:10 -070093 .exists = fat_exists,
Simon Glasse6d52412012-12-26 09:53:33 +000094 .read = fat_read_file,
Stephen Warrenbd6fb312014-02-03 13:20:59 -070095 .write = fs_write_unsupported,
Stephen Warren045fa1e2012-10-22 06:43:51 +000096 },
Simon Glass436e2b72012-12-26 09:53:29 +000097#endif
98#ifdef CONFIG_FS_EXT4
Stephen Warren045fa1e2012-10-22 06:43:51 +000099 {
100 .fstype = FS_TYPE_EXT,
Stephen Warren377202b2014-02-03 13:21:01 -0700101 .null_dev_desc_ok = false,
Simon Glasse6d52412012-12-26 09:53:33 +0000102 .probe = ext4fs_probe,
103 .close = ext4fs_close,
Simon Glass436e2b72012-12-26 09:53:29 +0000104 .ls = ext4fs_ls,
Stephen Warren55af5c92014-02-03 13:21:09 -0700105 .exists = ext4fs_exists,
Simon Glasse6d52412012-12-26 09:53:33 +0000106 .read = ext4_read_file,
Stephen Warrenbd6fb312014-02-03 13:20:59 -0700107 .write = fs_write_unsupported,
Simon Glass436e2b72012-12-26 09:53:29 +0000108 },
109#endif
Simon Glass92ccc962012-12-26 09:53:35 +0000110#ifdef CONFIG_SANDBOX
111 {
112 .fstype = FS_TYPE_SANDBOX,
Stephen Warren377202b2014-02-03 13:21:01 -0700113 .null_dev_desc_ok = true,
Simon Glass92ccc962012-12-26 09:53:35 +0000114 .probe = sandbox_fs_set_blk_dev,
115 .close = sandbox_fs_close,
116 .ls = sandbox_fs_ls,
Stephen Warren0a30aa12014-02-03 13:21:07 -0700117 .exists = sandbox_fs_exists,
Simon Glass92ccc962012-12-26 09:53:35 +0000118 .read = fs_read_sandbox,
Simon Glass7eb2c8d2013-04-20 08:42:51 +0000119 .write = fs_write_sandbox,
Simon Glass92ccc962012-12-26 09:53:35 +0000120 },
121#endif
Simon Glass436e2b72012-12-26 09:53:29 +0000122 {
123 .fstype = FS_TYPE_ANY,
Stephen Warren377202b2014-02-03 13:21:01 -0700124 .null_dev_desc_ok = true,
Simon Glass436e2b72012-12-26 09:53:29 +0000125 .probe = fs_probe_unsupported,
126 .close = fs_close_unsupported,
127 .ls = fs_ls_unsupported,
Stephen Warren61529162014-02-03 13:21:00 -0700128 .exists = fs_exists_unsupported,
Simon Glass436e2b72012-12-26 09:53:29 +0000129 .read = fs_read_unsupported,
Simon Glassa8f6ab52013-04-20 08:42:50 +0000130 .write = fs_write_unsupported,
Stephen Warren045fa1e2012-10-22 06:43:51 +0000131 },
132};
133
Simon Glassc6f548d2012-12-26 09:53:30 +0000134static struct fstype_info *fs_get_info(int fstype)
135{
136 struct fstype_info *info;
137 int i;
138
139 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
140 if (fstype == info->fstype)
141 return info;
142 }
143
144 /* Return the 'unsupported' sentinel */
145 return info;
146}
147
Stephen Warren045fa1e2012-10-22 06:43:51 +0000148int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
149{
Simon Glass436e2b72012-12-26 09:53:29 +0000150 struct fstype_info *info;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000151 int part, i;
Stephen Warrena1b231c2012-10-30 07:50:47 +0000152#ifdef CONFIG_NEEDS_MANUAL_RELOC
153 static int relocated;
154
155 if (!relocated) {
Simon Glass436e2b72012-12-26 09:53:29 +0000156 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
157 i++, info++) {
158 info->probe += gd->reloc_off;
159 info->close += gd->reloc_off;
160 info->ls += gd->reloc_off;
161 info->read += gd->reloc_off;
Simon Glassa8f6ab52013-04-20 08:42:50 +0000162 info->write += gd->reloc_off;
Simon Glass436e2b72012-12-26 09:53:29 +0000163 }
Stephen Warrena1b231c2012-10-30 07:50:47 +0000164 relocated = 1;
165 }
166#endif
Stephen Warren045fa1e2012-10-22 06:43:51 +0000167
168 part = get_device_and_partition(ifname, dev_part_str, &fs_dev_desc,
169 &fs_partition, 1);
170 if (part < 0)
171 return -1;
172
Simon Glass436e2b72012-12-26 09:53:29 +0000173 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
174 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
175 fstype != info->fstype)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000176 continue;
177
Stephen Warren377202b2014-02-03 13:21:01 -0700178 if (!fs_dev_desc && !info->null_dev_desc_ok)
179 continue;
180
Simon Glass2ded0d42012-12-26 09:53:31 +0000181 if (!info->probe(fs_dev_desc, &fs_partition)) {
Simon Glass436e2b72012-12-26 09:53:29 +0000182 fs_type = info->fstype;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000183 return 0;
184 }
185 }
186
Stephen Warren045fa1e2012-10-22 06:43:51 +0000187 return -1;
188}
189
190static void fs_close(void)
191{
Simon Glassc6f548d2012-12-26 09:53:30 +0000192 struct fstype_info *info = fs_get_info(fs_type);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000193
Simon Glassc6f548d2012-12-26 09:53:30 +0000194 info->close();
Simon Glasse6d52412012-12-26 09:53:33 +0000195
Stephen Warren045fa1e2012-10-22 06:43:51 +0000196 fs_type = FS_TYPE_ANY;
197}
198
199int fs_ls(const char *dirname)
200{
201 int ret;
202
Simon Glassc6f548d2012-12-26 09:53:30 +0000203 struct fstype_info *info = fs_get_info(fs_type);
204
205 ret = info->ls(dirname);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000206
Simon Glasse6d52412012-12-26 09:53:33 +0000207 fs_type = FS_TYPE_ANY;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000208 fs_close();
209
210 return ret;
211}
212
Stephen Warren61529162014-02-03 13:21:00 -0700213int fs_exists(const char *filename)
214{
215 int ret;
216
217 struct fstype_info *info = fs_get_info(fs_type);
218
219 ret = info->exists(filename);
220
221 fs_close();
222
223 return ret;
224}
225
Stephen Warren045fa1e2012-10-22 06:43:51 +0000226int fs_read(const char *filename, ulong addr, int offset, int len)
227{
Simon Glassc6f548d2012-12-26 09:53:30 +0000228 struct fstype_info *info = fs_get_info(fs_type);
Simon Glass117e0502012-12-26 09:53:32 +0000229 void *buf;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000230 int ret;
231
Simon Glass117e0502012-12-26 09:53:32 +0000232 /*
233 * We don't actually know how many bytes are being read, since len==0
234 * means read the whole file.
235 */
236 buf = map_sysmem(addr, len);
237 ret = info->read(filename, buf, offset, len);
238 unmap_sysmem(buf);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000239
Simon Glassc6f548d2012-12-26 09:53:30 +0000240 /* If we requested a specific number of bytes, check we got it */
241 if (ret >= 0 && len && ret != len) {
242 printf("** Unable to read file %s **\n", filename);
243 ret = -1;
244 }
Stephen Warren045fa1e2012-10-22 06:43:51 +0000245 fs_close();
246
247 return ret;
248}
249
Simon Glassa8f6ab52013-04-20 08:42:50 +0000250int fs_write(const char *filename, ulong addr, int offset, int len)
251{
252 struct fstype_info *info = fs_get_info(fs_type);
253 void *buf;
254 int ret;
255
Simon Glassa8f6ab52013-04-20 08:42:50 +0000256 buf = map_sysmem(addr, len);
257 ret = info->write(filename, buf, offset, len);
258 unmap_sysmem(buf);
259
Stephen Warrenbd6fb312014-02-03 13:20:59 -0700260 if (ret >= 0 && ret != len) {
Simon Glassa8f6ab52013-04-20 08:42:50 +0000261 printf("** Unable to write file %s **\n", filename);
262 ret = -1;
263 }
264 fs_close();
265
266 return ret;
267}
268
Stephen Warrenf9b55e22012-10-31 11:05:07 +0000269int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
Wolfgang Denkb770e882013-10-05 21:07:25 +0200270 int fstype)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000271{
272 unsigned long addr;
273 const char *addr_str;
274 const char *filename;
275 unsigned long bytes;
276 unsigned long pos;
277 int len_read;
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100278 unsigned long time;
Pavel Machek949bbd72014-07-09 22:42:57 +0200279 char *ep;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000280
Stephen Warrene9b0f992012-10-30 12:04:17 +0000281 if (argc < 2)
282 return CMD_RET_USAGE;
283 if (argc > 7)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000284 return CMD_RET_USAGE;
285
Stephen Warrene9b0f992012-10-30 12:04:17 +0000286 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
Stephen Warren045fa1e2012-10-22 06:43:51 +0000287 return 1;
288
289 if (argc >= 4) {
Pavel Machek949bbd72014-07-09 22:42:57 +0200290 addr = simple_strtoul(argv[3], &ep, 16);
291 if (ep == argv[3] || *ep != '\0')
292 return CMD_RET_USAGE;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000293 } else {
294 addr_str = getenv("loadaddr");
295 if (addr_str != NULL)
296 addr = simple_strtoul(addr_str, NULL, 16);
297 else
298 addr = CONFIG_SYS_LOAD_ADDR;
299 }
300 if (argc >= 5) {
301 filename = argv[4];
302 } else {
303 filename = getenv("bootfile");
304 if (!filename) {
305 puts("** No boot file defined **\n");
306 return 1;
307 }
308 }
309 if (argc >= 6)
Wolfgang Denkb770e882013-10-05 21:07:25 +0200310 bytes = simple_strtoul(argv[5], NULL, 16);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000311 else
312 bytes = 0;
313 if (argc >= 7)
Wolfgang Denkb770e882013-10-05 21:07:25 +0200314 pos = simple_strtoul(argv[6], NULL, 16);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000315 else
316 pos = 0;
317
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100318 time = get_timer(0);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000319 len_read = fs_read(filename, addr, pos, bytes);
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100320 time = get_timer(time);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000321 if (len_read <= 0)
322 return 1;
323
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100324 printf("%d bytes read in %lu ms", len_read, time);
325 if (time > 0) {
326 puts(" (");
327 print_size(len_read / time * 1000, "/s");
328 puts(")");
329 }
330 puts("\n");
Stephen Warren045fa1e2012-10-22 06:43:51 +0000331
Simon Glass49c4f032013-02-24 17:33:23 +0000332 setenv_hex("filesize", len_read);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000333
334 return 0;
335}
336
337int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
338 int fstype)
339{
340 if (argc < 2)
341 return CMD_RET_USAGE;
Stephen Warrene9b0f992012-10-30 12:04:17 +0000342 if (argc > 4)
343 return CMD_RET_USAGE;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000344
345 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
346 return 1;
347
Stephen Warrene9b0f992012-10-30 12:04:17 +0000348 if (fs_ls(argc >= 4 ? argv[3] : "/"))
Stephen Warren045fa1e2012-10-22 06:43:51 +0000349 return 1;
350
351 return 0;
352}
Simon Glassa8f6ab52013-04-20 08:42:50 +0000353
Stephen Warren61529162014-02-03 13:21:00 -0700354int file_exists(const char *dev_type, const char *dev_part, const char *file,
355 int fstype)
356{
357 if (fs_set_blk_dev(dev_type, dev_part, fstype))
358 return 0;
359
360 return fs_exists(file);
361}
362
Simon Glassa8f6ab52013-04-20 08:42:50 +0000363int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
Wolfgang Denkb770e882013-10-05 21:07:25 +0200364 int fstype)
Simon Glassa8f6ab52013-04-20 08:42:50 +0000365{
366 unsigned long addr;
367 const char *filename;
368 unsigned long bytes;
369 unsigned long pos;
370 int len;
371 unsigned long time;
372
373 if (argc < 6 || argc > 7)
374 return CMD_RET_USAGE;
375
376 if (fs_set_blk_dev(argv[1], argv[2], fstype))
377 return 1;
378
379 filename = argv[3];
Wolfgang Denkb770e882013-10-05 21:07:25 +0200380 addr = simple_strtoul(argv[4], NULL, 16);
381 bytes = simple_strtoul(argv[5], NULL, 16);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000382 if (argc >= 7)
Wolfgang Denkb770e882013-10-05 21:07:25 +0200383 pos = simple_strtoul(argv[6], NULL, 16);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000384 else
385 pos = 0;
386
387 time = get_timer(0);
388 len = fs_write(filename, addr, pos, bytes);
389 time = get_timer(time);
390 if (len <= 0)
391 return 1;
392
393 printf("%d bytes written in %lu ms", len, time);
394 if (time > 0) {
395 puts(" (");
396 print_size(len / time * 1000, "/s");
397 puts(")");
398 }
399 puts("\n");
400
401 return 0;
402}