]>
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 | ||
117e0507 | 49 | static inline int fs_read_unsupported(const char *filename, void *buf, |
045fa1e1 SW |
50 | int offset, int len) |
51 | { | |
045fa1e1 SW |
52 | return -1; |
53 | } | |
54 | ||
a8f6ab52 SG |
55 | static inline int fs_write_unsupported(const char *filename, void *buf, |
56 | int offset, int len) | |
57 | { | |
58 | return -1; | |
59 | } | |
60 | ||
436e2b73 SG |
61 | static inline void fs_close_unsupported(void) |
62 | { | |
63 | } | |
64 | ||
436e2b73 | 65 | struct fstype_info { |
045fa1e1 | 66 | int fstype; |
377202b5 SW |
67 | /* |
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; | |
2ded0d47 SG |
76 | int (*probe)(block_dev_desc_t *fs_dev_desc, |
77 | disk_partition_t *fs_partition); | |
436e2b73 | 78 | int (*ls)(const char *dirname); |
6152916a | 79 | int (*exists)(const char *filename); |
117e0507 | 80 | int (*read)(const char *filename, void *buf, int offset, int len); |
a8f6ab52 | 81 | int (*write)(const char *filename, void *buf, int offset, int len); |
436e2b73 SG |
82 | void (*close)(void); |
83 | }; | |
84 | ||
85 | static struct fstype_info fstypes[] = { | |
86 | #ifdef CONFIG_FS_FAT | |
045fa1e1 SW |
87 | { |
88 | .fstype = FS_TYPE_FAT, | |
377202b5 | 89 | .null_dev_desc_ok = false, |
e6d52415 SG |
90 | .probe = fat_set_blk_dev, |
91 | .close = fat_close, | |
436e2b73 | 92 | .ls = file_fat_ls, |
b7b5f319 | 93 | .exists = fat_exists, |
e6d52415 | 94 | .read = fat_read_file, |
bd6fb31f | 95 | .write = fs_write_unsupported, |
045fa1e1 | 96 | }, |
436e2b73 SG |
97 | #endif |
98 | #ifdef CONFIG_FS_EXT4 | |
045fa1e1 SW |
99 | { |
100 | .fstype = FS_TYPE_EXT, | |
377202b5 | 101 | .null_dev_desc_ok = false, |
e6d52415 SG |
102 | .probe = ext4fs_probe, |
103 | .close = ext4fs_close, | |
436e2b73 | 104 | .ls = ext4fs_ls, |
55af5c93 | 105 | .exists = ext4fs_exists, |
e6d52415 | 106 | .read = ext4_read_file, |
bd6fb31f | 107 | .write = fs_write_unsupported, |
436e2b73 | 108 | }, |
92ccc96b SG |
109 | #endif |
110 | #ifdef CONFIG_SANDBOX | |
111 | { | |
112 | .fstype = FS_TYPE_SANDBOX, | |
377202b5 | 113 | .null_dev_desc_ok = true, |
92ccc96b SG |
114 | .probe = sandbox_fs_set_blk_dev, |
115 | .close = sandbox_fs_close, | |
116 | .ls = sandbox_fs_ls, | |
0a30aa1e | 117 | .exists = sandbox_fs_exists, |
92ccc96b | 118 | .read = fs_read_sandbox, |
7eb2c8d5 | 119 | .write = fs_write_sandbox, |
92ccc96b | 120 | }, |
436e2b73 SG |
121 | #endif |
122 | { | |
123 | .fstype = FS_TYPE_ANY, | |
377202b5 | 124 | .null_dev_desc_ok = true, |
436e2b73 SG |
125 | .probe = fs_probe_unsupported, |
126 | .close = fs_close_unsupported, | |
127 | .ls = fs_ls_unsupported, | |
6152916a | 128 | .exists = fs_exists_unsupported, |
436e2b73 | 129 | .read = fs_read_unsupported, |
a8f6ab52 | 130 | .write = fs_write_unsupported, |
045fa1e1 SW |
131 | }, |
132 | }; | |
133 | ||
c6f548d2 SG |
134 | static 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 | ||
045fa1e1 SW |
148 | int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype) |
149 | { | |
436e2b73 | 150 | struct fstype_info *info; |
045fa1e1 | 151 | int part, i; |
a1b231ce SW |
152 | #ifdef CONFIG_NEEDS_MANUAL_RELOC |
153 | static int relocated; | |
154 | ||
155 | if (!relocated) { | |
436e2b73 SG |
156 | 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; | |
a8f6ab52 | 162 | info->write += gd->reloc_off; |
436e2b73 | 163 | } |
a1b231ce SW |
164 | relocated = 1; |
165 | } | |
166 | #endif | |
045fa1e1 SW |
167 | |
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 | ||
436e2b73 SG |
173 | 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) | |
045fa1e1 SW |
176 | continue; |
177 | ||
377202b5 SW |
178 | if (!fs_dev_desc && !info->null_dev_desc_ok) |
179 | continue; | |
180 | ||
2ded0d47 | 181 | if (!info->probe(fs_dev_desc, &fs_partition)) { |
436e2b73 | 182 | fs_type = info->fstype; |
045fa1e1 SW |
183 | return 0; |
184 | } | |
185 | } | |
186 | ||
045fa1e1 SW |
187 | return -1; |
188 | } | |
189 | ||
190 | static void fs_close(void) | |
191 | { | |
c6f548d2 | 192 | struct fstype_info *info = fs_get_info(fs_type); |
045fa1e1 | 193 | |
c6f548d2 | 194 | info->close(); |
e6d52415 | 195 | |
045fa1e1 SW |
196 | fs_type = FS_TYPE_ANY; |
197 | } | |
198 | ||
199 | int fs_ls(const char *dirname) | |
200 | { | |
201 | int ret; | |
202 | ||
c6f548d2 SG |
203 | struct fstype_info *info = fs_get_info(fs_type); |
204 | ||
205 | ret = info->ls(dirname); | |
045fa1e1 | 206 | |
e6d52415 | 207 | fs_type = FS_TYPE_ANY; |
045fa1e1 SW |
208 | fs_close(); |
209 | ||
210 | return ret; | |
211 | } | |
212 | ||
6152916a SW |
213 | int 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 | ||
045fa1e1 SW |
226 | int fs_read(const char *filename, ulong addr, int offset, int len) |
227 | { | |
c6f548d2 | 228 | struct fstype_info *info = fs_get_info(fs_type); |
117e0507 | 229 | void *buf; |
045fa1e1 SW |
230 | int ret; |
231 | ||
117e0507 SG |
232 | /* |
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); | |
045fa1e1 | 239 | |
c6f548d2 SG |
240 | /* 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 | } | |
045fa1e1 SW |
245 | fs_close(); |
246 | ||
247 | return ret; | |
248 | } | |
249 | ||
a8f6ab52 SG |
250 | int 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 | ||
a8f6ab52 SG |
256 | buf = map_sysmem(addr, len); |
257 | ret = info->write(filename, buf, offset, len); | |
258 | unmap_sysmem(buf); | |
259 | ||
bd6fb31f | 260 | if (ret >= 0 && ret != len) { |
a8f6ab52 SG |
261 | printf("** Unable to write file %s **\n", filename); |
262 | ret = -1; | |
263 | } | |
264 | fs_close(); | |
265 | ||
266 | return ret; | |
267 | } | |
268 | ||
f9b55e22 | 269 | int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
b770e88a | 270 | int fstype) |
045fa1e1 SW |
271 | { |
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; | |
da1fd96c | 278 | unsigned long time; |
949bbd7c | 279 | char *ep; |
045fa1e1 | 280 | |
e9b0f99e SW |
281 | if (argc < 2) |
282 | return CMD_RET_USAGE; | |
283 | if (argc > 7) | |
045fa1e1 SW |
284 | return CMD_RET_USAGE; |
285 | ||
e9b0f99e | 286 | if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype)) |
045fa1e1 SW |
287 | return 1; |
288 | ||
289 | if (argc >= 4) { | |
949bbd7c PM |
290 | addr = simple_strtoul(argv[3], &ep, 16); |
291 | if (ep == argv[3] || *ep != '\0') | |
292 | return CMD_RET_USAGE; | |
045fa1e1 SW |
293 | } 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) | |
b770e88a | 310 | bytes = simple_strtoul(argv[5], NULL, 16); |
045fa1e1 SW |
311 | else |
312 | bytes = 0; | |
313 | if (argc >= 7) | |
b770e88a | 314 | pos = simple_strtoul(argv[6], NULL, 16); |
045fa1e1 SW |
315 | else |
316 | pos = 0; | |
317 | ||
da1fd96c | 318 | time = get_timer(0); |
045fa1e1 | 319 | len_read = fs_read(filename, addr, pos, bytes); |
da1fd96c | 320 | time = get_timer(time); |
045fa1e1 SW |
321 | if (len_read <= 0) |
322 | return 1; | |
323 | ||
da1fd96c AB |
324 | 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"); | |
045fa1e1 | 331 | |
49c4f037 | 332 | setenv_hex("filesize", len_read); |
045fa1e1 SW |
333 | |
334 | return 0; | |
335 | } | |
336 | ||
337 | int 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; | |
e9b0f99e SW |
342 | if (argc > 4) |
343 | return CMD_RET_USAGE; | |
045fa1e1 SW |
344 | |
345 | if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype)) | |
346 | return 1; | |
347 | ||
e9b0f99e | 348 | if (fs_ls(argc >= 4 ? argv[3] : "/")) |
045fa1e1 SW |
349 | return 1; |
350 | ||
351 | return 0; | |
352 | } | |
a8f6ab52 | 353 | |
6152916a SW |
354 | int 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 | ||
a8f6ab52 | 363 | int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
b770e88a | 364 | int fstype) |
a8f6ab52 SG |
365 | { |
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]; | |
b770e88a WD |
380 | addr = simple_strtoul(argv[4], NULL, 16); |
381 | bytes = simple_strtoul(argv[5], NULL, 16); | |
a8f6ab52 | 382 | if (argc >= 7) |
b770e88a | 383 | pos = simple_strtoul(argv[6], NULL, 16); |
a8f6ab52 SG |
384 | 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 | } |