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