]>
Commit | Line | Data |
---|---|---|
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> | |
59e890ef | 18 | #include <errno.h> |
045fa1e1 SW |
19 | #include <common.h> |
20 | #include <part.h> | |
21 | #include <ext4fs.h> | |
22 | #include <fat.h> | |
23 | #include <fs.h> | |
92ccc96b | 24 | #include <sandboxfs.h> |
117e0507 | 25 | #include <asm/io.h> |
9e374e7b TR |
26 | #include <div64.h> |
27 | #include <linux/math64.h> | |
045fa1e1 | 28 | |
a1b231ce SW |
29 | DECLARE_GLOBAL_DATA_PTR; |
30 | ||
045fa1e1 SW |
31 | static block_dev_desc_t *fs_dev_desc; |
32 | static disk_partition_t fs_partition; | |
33 | static int fs_type = FS_TYPE_ANY; | |
34 | ||
2ded0d47 SG |
35 | static inline int fs_probe_unsupported(block_dev_desc_t *fs_dev_desc, |
36 | disk_partition_t *fs_partition) | |
436e2b73 SG |
37 | { |
38 | printf("** Unrecognized filesystem type **\n"); | |
39 | return -1; | |
40 | } | |
41 | ||
045fa1e1 SW |
42 | static inline int fs_ls_unsupported(const char *dirname) |
43 | { | |
045fa1e1 SW |
44 | return -1; |
45 | } | |
46 | ||
6152916a SW |
47 | static inline int fs_exists_unsupported(const char *filename) |
48 | { | |
49 | return 0; | |
50 | } | |
51 | ||
d455d878 | 52 | static inline int fs_size_unsupported(const char *filename, loff_t *size) |
cf659819 SW |
53 | { |
54 | return -1; | |
55 | } | |
56 | ||
117e0507 | 57 | static inline int fs_read_unsupported(const char *filename, void *buf, |
d455d878 SR |
58 | loff_t offset, loff_t len, |
59 | loff_t *actread) | |
045fa1e1 | 60 | { |
045fa1e1 SW |
61 | return -1; |
62 | } | |
63 | ||
a8f6ab52 | 64 | static inline int fs_write_unsupported(const char *filename, void *buf, |
d455d878 SR |
65 | loff_t offset, loff_t len, |
66 | loff_t *actwrite) | |
a8f6ab52 SG |
67 | { |
68 | return -1; | |
69 | } | |
70 | ||
436e2b73 SG |
71 | static inline void fs_close_unsupported(void) |
72 | { | |
73 | } | |
74 | ||
59e890ef CG |
75 | static inline int fs_uuid_unsupported(char *uuid_str) |
76 | { | |
77 | return -1; | |
78 | } | |
79 | ||
436e2b73 | 80 | struct fstype_info { |
045fa1e1 | 81 | int fstype; |
377202b5 SW |
82 | /* |
83 | * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This | |
84 | * should be false in most cases. For "virtual" filesystems which | |
85 | * aren't based on a U-Boot block device (e.g. sandbox), this can be | |
86 | * set to true. This should also be true for the dumm entry at the end | |
87 | * of fstypes[], since that is essentially a "virtual" (non-existent) | |
88 | * filesystem. | |
89 | */ | |
90 | bool null_dev_desc_ok; | |
2ded0d47 SG |
91 | int (*probe)(block_dev_desc_t *fs_dev_desc, |
92 | disk_partition_t *fs_partition); | |
436e2b73 | 93 | int (*ls)(const char *dirname); |
6152916a | 94 | int (*exists)(const char *filename); |
d455d878 SR |
95 | int (*size)(const char *filename, loff_t *size); |
96 | int (*read)(const char *filename, void *buf, loff_t offset, | |
97 | loff_t len, loff_t *actread); | |
98 | int (*write)(const char *filename, void *buf, loff_t offset, | |
99 | loff_t len, loff_t *actwrite); | |
436e2b73 | 100 | void (*close)(void); |
59e890ef | 101 | int (*uuid)(char *uuid_str); |
436e2b73 SG |
102 | }; |
103 | ||
104 | static struct fstype_info fstypes[] = { | |
105 | #ifdef CONFIG_FS_FAT | |
045fa1e1 SW |
106 | { |
107 | .fstype = FS_TYPE_FAT, | |
377202b5 | 108 | .null_dev_desc_ok = false, |
e6d52415 SG |
109 | .probe = fat_set_blk_dev, |
110 | .close = fat_close, | |
436e2b73 | 111 | .ls = file_fat_ls, |
b7b5f319 | 112 | .exists = fat_exists, |
cf659819 | 113 | .size = fat_size, |
e6d52415 | 114 | .read = fat_read_file, |
d455d878 SR |
115 | #ifdef CONFIG_FAT_WRITE |
116 | .write = file_fat_write, | |
117 | #else | |
bd6fb31f | 118 | .write = fs_write_unsupported, |
d455d878 | 119 | #endif |
59e890ef | 120 | .uuid = fs_uuid_unsupported, |
045fa1e1 | 121 | }, |
436e2b73 SG |
122 | #endif |
123 | #ifdef CONFIG_FS_EXT4 | |
045fa1e1 SW |
124 | { |
125 | .fstype = FS_TYPE_EXT, | |
377202b5 | 126 | .null_dev_desc_ok = false, |
e6d52415 SG |
127 | .probe = ext4fs_probe, |
128 | .close = ext4fs_close, | |
436e2b73 | 129 | .ls = ext4fs_ls, |
55af5c93 | 130 | .exists = ext4fs_exists, |
cf659819 | 131 | .size = ext4fs_size, |
e6d52415 | 132 | .read = ext4_read_file, |
d455d878 SR |
133 | #ifdef CONFIG_CMD_EXT4_WRITE |
134 | .write = ext4_write_file, | |
135 | #else | |
bd6fb31f | 136 | .write = fs_write_unsupported, |
d455d878 | 137 | #endif |
59e890ef | 138 | .uuid = ext4fs_uuid, |
436e2b73 | 139 | }, |
92ccc96b SG |
140 | #endif |
141 | #ifdef CONFIG_SANDBOX | |
142 | { | |
143 | .fstype = FS_TYPE_SANDBOX, | |
377202b5 | 144 | .null_dev_desc_ok = true, |
92ccc96b SG |
145 | .probe = sandbox_fs_set_blk_dev, |
146 | .close = sandbox_fs_close, | |
147 | .ls = sandbox_fs_ls, | |
0a30aa1e | 148 | .exists = sandbox_fs_exists, |
cf659819 | 149 | .size = sandbox_fs_size, |
92ccc96b | 150 | .read = fs_read_sandbox, |
7eb2c8d5 | 151 | .write = fs_write_sandbox, |
59e890ef | 152 | .uuid = fs_uuid_unsupported, |
92ccc96b | 153 | }, |
436e2b73 SG |
154 | #endif |
155 | { | |
156 | .fstype = FS_TYPE_ANY, | |
377202b5 | 157 | .null_dev_desc_ok = true, |
436e2b73 SG |
158 | .probe = fs_probe_unsupported, |
159 | .close = fs_close_unsupported, | |
160 | .ls = fs_ls_unsupported, | |
6152916a | 161 | .exists = fs_exists_unsupported, |
cf659819 | 162 | .size = fs_size_unsupported, |
436e2b73 | 163 | .read = fs_read_unsupported, |
a8f6ab52 | 164 | .write = fs_write_unsupported, |
59e890ef | 165 | .uuid = fs_uuid_unsupported, |
045fa1e1 SW |
166 | }, |
167 | }; | |
168 | ||
c6f548d2 SG |
169 | static struct fstype_info *fs_get_info(int fstype) |
170 | { | |
171 | struct fstype_info *info; | |
172 | int i; | |
173 | ||
174 | for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) { | |
175 | if (fstype == info->fstype) | |
176 | return info; | |
177 | } | |
178 | ||
179 | /* Return the 'unsupported' sentinel */ | |
180 | return info; | |
181 | } | |
182 | ||
045fa1e1 SW |
183 | int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype) |
184 | { | |
436e2b73 | 185 | struct fstype_info *info; |
045fa1e1 | 186 | int part, i; |
a1b231ce SW |
187 | #ifdef CONFIG_NEEDS_MANUAL_RELOC |
188 | static int relocated; | |
189 | ||
190 | if (!relocated) { | |
436e2b73 SG |
191 | for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); |
192 | i++, info++) { | |
193 | info->probe += gd->reloc_off; | |
194 | info->close += gd->reloc_off; | |
195 | info->ls += gd->reloc_off; | |
196 | info->read += gd->reloc_off; | |
a8f6ab52 | 197 | info->write += gd->reloc_off; |
436e2b73 | 198 | } |
a1b231ce SW |
199 | relocated = 1; |
200 | } | |
201 | #endif | |
045fa1e1 SW |
202 | |
203 | part = get_device_and_partition(ifname, dev_part_str, &fs_dev_desc, | |
204 | &fs_partition, 1); | |
205 | if (part < 0) | |
206 | return -1; | |
207 | ||
436e2b73 SG |
208 | for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) { |
209 | if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY && | |
210 | fstype != info->fstype) | |
045fa1e1 SW |
211 | continue; |
212 | ||
377202b5 SW |
213 | if (!fs_dev_desc && !info->null_dev_desc_ok) |
214 | continue; | |
215 | ||
2ded0d47 | 216 | if (!info->probe(fs_dev_desc, &fs_partition)) { |
436e2b73 | 217 | fs_type = info->fstype; |
045fa1e1 SW |
218 | return 0; |
219 | } | |
220 | } | |
221 | ||
045fa1e1 SW |
222 | return -1; |
223 | } | |
224 | ||
225 | static void fs_close(void) | |
226 | { | |
c6f548d2 | 227 | struct fstype_info *info = fs_get_info(fs_type); |
045fa1e1 | 228 | |
c6f548d2 | 229 | info->close(); |
e6d52415 | 230 | |
045fa1e1 SW |
231 | fs_type = FS_TYPE_ANY; |
232 | } | |
233 | ||
59e890ef CG |
234 | int fs_uuid(char *uuid_str) |
235 | { | |
236 | struct fstype_info *info = fs_get_info(fs_type); | |
237 | ||
238 | return info->uuid(uuid_str); | |
239 | } | |
240 | ||
045fa1e1 SW |
241 | int fs_ls(const char *dirname) |
242 | { | |
243 | int ret; | |
244 | ||
c6f548d2 SG |
245 | struct fstype_info *info = fs_get_info(fs_type); |
246 | ||
247 | ret = info->ls(dirname); | |
045fa1e1 | 248 | |
e6d52415 | 249 | fs_type = FS_TYPE_ANY; |
045fa1e1 SW |
250 | fs_close(); |
251 | ||
252 | return ret; | |
253 | } | |
254 | ||
6152916a SW |
255 | int fs_exists(const char *filename) |
256 | { | |
257 | int ret; | |
258 | ||
259 | struct fstype_info *info = fs_get_info(fs_type); | |
260 | ||
261 | ret = info->exists(filename); | |
262 | ||
263 | fs_close(); | |
264 | ||
265 | return ret; | |
266 | } | |
267 | ||
d455d878 | 268 | int fs_size(const char *filename, loff_t *size) |
cf659819 SW |
269 | { |
270 | int ret; | |
271 | ||
272 | struct fstype_info *info = fs_get_info(fs_type); | |
273 | ||
d455d878 | 274 | ret = info->size(filename, size); |
cf659819 SW |
275 | |
276 | fs_close(); | |
277 | ||
278 | return ret; | |
279 | } | |
280 | ||
d455d878 SR |
281 | int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len, |
282 | loff_t *actread) | |
045fa1e1 | 283 | { |
c6f548d2 | 284 | struct fstype_info *info = fs_get_info(fs_type); |
117e0507 | 285 | void *buf; |
045fa1e1 SW |
286 | int ret; |
287 | ||
117e0507 SG |
288 | /* |
289 | * We don't actually know how many bytes are being read, since len==0 | |
290 | * means read the whole file. | |
291 | */ | |
292 | buf = map_sysmem(addr, len); | |
d455d878 | 293 | ret = info->read(filename, buf, offset, len, actread); |
117e0507 | 294 | unmap_sysmem(buf); |
045fa1e1 | 295 | |
c6f548d2 | 296 | /* If we requested a specific number of bytes, check we got it */ |
d455d878 | 297 | if (ret == 0 && len && *actread != len) { |
c6f548d2 SG |
298 | printf("** Unable to read file %s **\n", filename); |
299 | ret = -1; | |
300 | } | |
045fa1e1 SW |
301 | fs_close(); |
302 | ||
303 | return ret; | |
304 | } | |
305 | ||
d455d878 SR |
306 | int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len, |
307 | loff_t *actwrite) | |
a8f6ab52 SG |
308 | { |
309 | struct fstype_info *info = fs_get_info(fs_type); | |
310 | void *buf; | |
311 | int ret; | |
312 | ||
a8f6ab52 | 313 | buf = map_sysmem(addr, len); |
d455d878 | 314 | ret = info->write(filename, buf, offset, len, actwrite); |
a8f6ab52 SG |
315 | unmap_sysmem(buf); |
316 | ||
d455d878 | 317 | if (ret < 0 && len != *actwrite) { |
a8f6ab52 SG |
318 | printf("** Unable to write file %s **\n", filename); |
319 | ret = -1; | |
320 | } | |
321 | fs_close(); | |
322 | ||
323 | return ret; | |
324 | } | |
325 | ||
cf659819 SW |
326 | int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
327 | int fstype) | |
328 | { | |
d455d878 | 329 | loff_t size; |
cf659819 SW |
330 | |
331 | if (argc != 4) | |
332 | return CMD_RET_USAGE; | |
333 | ||
334 | if (fs_set_blk_dev(argv[1], argv[2], fstype)) | |
335 | return 1; | |
336 | ||
d455d878 | 337 | if (fs_size(argv[3], &size) < 0) |
cf659819 SW |
338 | return CMD_RET_FAILURE; |
339 | ||
340 | setenv_hex("filesize", size); | |
341 | ||
342 | return 0; | |
343 | } | |
344 | ||
f9b55e22 | 345 | int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
b770e88a | 346 | int fstype) |
045fa1e1 SW |
347 | { |
348 | unsigned long addr; | |
349 | const char *addr_str; | |
350 | const char *filename; | |
d455d878 SR |
351 | loff_t bytes; |
352 | loff_t pos; | |
353 | loff_t len_read; | |
354 | int ret; | |
da1fd96c | 355 | unsigned long time; |
949bbd7c | 356 | char *ep; |
045fa1e1 | 357 | |
e9b0f99e SW |
358 | if (argc < 2) |
359 | return CMD_RET_USAGE; | |
360 | if (argc > 7) | |
045fa1e1 SW |
361 | return CMD_RET_USAGE; |
362 | ||
e9b0f99e | 363 | if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype)) |
045fa1e1 SW |
364 | return 1; |
365 | ||
366 | if (argc >= 4) { | |
949bbd7c PM |
367 | addr = simple_strtoul(argv[3], &ep, 16); |
368 | if (ep == argv[3] || *ep != '\0') | |
369 | return CMD_RET_USAGE; | |
045fa1e1 SW |
370 | } else { |
371 | addr_str = getenv("loadaddr"); | |
372 | if (addr_str != NULL) | |
373 | addr = simple_strtoul(addr_str, NULL, 16); | |
374 | else | |
375 | addr = CONFIG_SYS_LOAD_ADDR; | |
376 | } | |
377 | if (argc >= 5) { | |
378 | filename = argv[4]; | |
379 | } else { | |
380 | filename = getenv("bootfile"); | |
381 | if (!filename) { | |
382 | puts("** No boot file defined **\n"); | |
383 | return 1; | |
384 | } | |
385 | } | |
386 | if (argc >= 6) | |
b770e88a | 387 | bytes = simple_strtoul(argv[5], NULL, 16); |
045fa1e1 SW |
388 | else |
389 | bytes = 0; | |
390 | if (argc >= 7) | |
b770e88a | 391 | pos = simple_strtoul(argv[6], NULL, 16); |
045fa1e1 SW |
392 | else |
393 | pos = 0; | |
394 | ||
da1fd96c | 395 | time = get_timer(0); |
d455d878 | 396 | ret = fs_read(filename, addr, pos, bytes, &len_read); |
da1fd96c | 397 | time = get_timer(time); |
d455d878 | 398 | if (ret < 0) |
045fa1e1 SW |
399 | return 1; |
400 | ||
d455d878 | 401 | printf("%llu bytes read in %lu ms", len_read, time); |
da1fd96c AB |
402 | if (time > 0) { |
403 | puts(" ("); | |
9e374e7b | 404 | print_size(div_u64(len_read, time) * 1000, "/s"); |
da1fd96c AB |
405 | puts(")"); |
406 | } | |
407 | puts("\n"); | |
045fa1e1 | 408 | |
49c4f037 | 409 | setenv_hex("filesize", len_read); |
045fa1e1 SW |
410 | |
411 | return 0; | |
412 | } | |
413 | ||
414 | int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], | |
415 | int fstype) | |
416 | { | |
417 | if (argc < 2) | |
418 | return CMD_RET_USAGE; | |
e9b0f99e SW |
419 | if (argc > 4) |
420 | return CMD_RET_USAGE; | |
045fa1e1 SW |
421 | |
422 | if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype)) | |
423 | return 1; | |
424 | ||
e9b0f99e | 425 | if (fs_ls(argc >= 4 ? argv[3] : "/")) |
045fa1e1 SW |
426 | return 1; |
427 | ||
428 | return 0; | |
429 | } | |
a8f6ab52 | 430 | |
6152916a SW |
431 | int file_exists(const char *dev_type, const char *dev_part, const char *file, |
432 | int fstype) | |
433 | { | |
434 | if (fs_set_blk_dev(dev_type, dev_part, fstype)) | |
435 | return 0; | |
436 | ||
437 | return fs_exists(file); | |
438 | } | |
439 | ||
a8f6ab52 | 440 | int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
b770e88a | 441 | int fstype) |
a8f6ab52 SG |
442 | { |
443 | unsigned long addr; | |
444 | const char *filename; | |
d455d878 SR |
445 | loff_t bytes; |
446 | loff_t pos; | |
447 | loff_t len; | |
448 | int ret; | |
a8f6ab52 SG |
449 | unsigned long time; |
450 | ||
451 | if (argc < 6 || argc > 7) | |
452 | return CMD_RET_USAGE; | |
453 | ||
454 | if (fs_set_blk_dev(argv[1], argv[2], fstype)) | |
455 | return 1; | |
456 | ||
d455d878 SR |
457 | addr = simple_strtoul(argv[3], NULL, 16); |
458 | filename = argv[4]; | |
b770e88a | 459 | bytes = simple_strtoul(argv[5], NULL, 16); |
a8f6ab52 | 460 | if (argc >= 7) |
b770e88a | 461 | pos = simple_strtoul(argv[6], NULL, 16); |
a8f6ab52 SG |
462 | else |
463 | pos = 0; | |
464 | ||
465 | time = get_timer(0); | |
d455d878 | 466 | ret = fs_write(filename, addr, pos, bytes, &len); |
a8f6ab52 | 467 | time = get_timer(time); |
d455d878 | 468 | if (ret < 0) |
a8f6ab52 SG |
469 | return 1; |
470 | ||
d455d878 | 471 | printf("%llu bytes written in %lu ms", len, time); |
a8f6ab52 SG |
472 | if (time > 0) { |
473 | puts(" ("); | |
9e374e7b | 474 | print_size(div_u64(len, time) * 1000, "/s"); |
a8f6ab52 SG |
475 | puts(")"); |
476 | } | |
477 | puts("\n"); | |
478 | ||
479 | return 0; | |
480 | } | |
59e890ef CG |
481 | |
482 | int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], | |
483 | int fstype) | |
484 | { | |
485 | int ret; | |
486 | char uuid[37]; | |
487 | memset(uuid, 0, sizeof(uuid)); | |
488 | ||
489 | if (argc < 3 || argc > 4) | |
490 | return CMD_RET_USAGE; | |
491 | ||
492 | if (fs_set_blk_dev(argv[1], argv[2], fstype)) | |
493 | return 1; | |
494 | ||
495 | ret = fs_uuid(uuid); | |
496 | if (ret) | |
497 | return CMD_RET_FAILURE; | |
498 | ||
499 | if (argc == 4) | |
500 | setenv(argv[3], uuid); | |
501 | else | |
502 | printf("%s\n", uuid); | |
503 | ||
504 | return CMD_RET_SUCCESS; | |
505 | } |