]>
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 | ||
117e0507 | 44 | static inline int fs_read_unsupported(const char *filename, void *buf, |
045fa1e1 SW |
45 | int offset, int len) |
46 | { | |
045fa1e1 SW |
47 | return -1; |
48 | } | |
49 | ||
a8f6ab52 SG |
50 | static inline int fs_write_unsupported(const char *filename, void *buf, |
51 | int offset, int len) | |
52 | { | |
53 | return -1; | |
54 | } | |
55 | ||
436e2b73 SG |
56 | static inline void fs_close_unsupported(void) |
57 | { | |
58 | } | |
59 | ||
436e2b73 | 60 | struct fstype_info { |
045fa1e1 | 61 | int fstype; |
2ded0d47 SG |
62 | int (*probe)(block_dev_desc_t *fs_dev_desc, |
63 | disk_partition_t *fs_partition); | |
436e2b73 | 64 | int (*ls)(const char *dirname); |
117e0507 | 65 | int (*read)(const char *filename, void *buf, int offset, int len); |
a8f6ab52 | 66 | int (*write)(const char *filename, void *buf, int offset, int len); |
436e2b73 SG |
67 | void (*close)(void); |
68 | }; | |
69 | ||
70 | static struct fstype_info fstypes[] = { | |
71 | #ifdef CONFIG_FS_FAT | |
045fa1e1 SW |
72 | { |
73 | .fstype = FS_TYPE_FAT, | |
e6d52415 SG |
74 | .probe = fat_set_blk_dev, |
75 | .close = fat_close, | |
436e2b73 | 76 | .ls = file_fat_ls, |
e6d52415 | 77 | .read = fat_read_file, |
bd6fb31f | 78 | .write = fs_write_unsupported, |
045fa1e1 | 79 | }, |
436e2b73 SG |
80 | #endif |
81 | #ifdef CONFIG_FS_EXT4 | |
045fa1e1 SW |
82 | { |
83 | .fstype = FS_TYPE_EXT, | |
e6d52415 SG |
84 | .probe = ext4fs_probe, |
85 | .close = ext4fs_close, | |
436e2b73 | 86 | .ls = ext4fs_ls, |
e6d52415 | 87 | .read = ext4_read_file, |
bd6fb31f | 88 | .write = fs_write_unsupported, |
436e2b73 | 89 | }, |
92ccc96b SG |
90 | #endif |
91 | #ifdef CONFIG_SANDBOX | |
92 | { | |
93 | .fstype = FS_TYPE_SANDBOX, | |
94 | .probe = sandbox_fs_set_blk_dev, | |
95 | .close = sandbox_fs_close, | |
96 | .ls = sandbox_fs_ls, | |
97 | .read = fs_read_sandbox, | |
7eb2c8d5 | 98 | .write = fs_write_sandbox, |
92ccc96b | 99 | }, |
436e2b73 SG |
100 | #endif |
101 | { | |
102 | .fstype = FS_TYPE_ANY, | |
103 | .probe = fs_probe_unsupported, | |
104 | .close = fs_close_unsupported, | |
105 | .ls = fs_ls_unsupported, | |
106 | .read = fs_read_unsupported, | |
a8f6ab52 | 107 | .write = fs_write_unsupported, |
045fa1e1 SW |
108 | }, |
109 | }; | |
110 | ||
c6f548d2 SG |
111 | static struct fstype_info *fs_get_info(int fstype) |
112 | { | |
113 | struct fstype_info *info; | |
114 | int i; | |
115 | ||
116 | for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) { | |
117 | if (fstype == info->fstype) | |
118 | return info; | |
119 | } | |
120 | ||
121 | /* Return the 'unsupported' sentinel */ | |
122 | return info; | |
123 | } | |
124 | ||
045fa1e1 SW |
125 | int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype) |
126 | { | |
436e2b73 | 127 | struct fstype_info *info; |
045fa1e1 | 128 | int part, i; |
a1b231ce SW |
129 | #ifdef CONFIG_NEEDS_MANUAL_RELOC |
130 | static int relocated; | |
131 | ||
132 | if (!relocated) { | |
436e2b73 SG |
133 | for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); |
134 | i++, info++) { | |
135 | info->probe += gd->reloc_off; | |
136 | info->close += gd->reloc_off; | |
137 | info->ls += gd->reloc_off; | |
138 | info->read += gd->reloc_off; | |
a8f6ab52 | 139 | info->write += gd->reloc_off; |
436e2b73 | 140 | } |
a1b231ce SW |
141 | relocated = 1; |
142 | } | |
143 | #endif | |
045fa1e1 SW |
144 | |
145 | part = get_device_and_partition(ifname, dev_part_str, &fs_dev_desc, | |
146 | &fs_partition, 1); | |
147 | if (part < 0) | |
148 | return -1; | |
149 | ||
436e2b73 SG |
150 | for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) { |
151 | if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY && | |
152 | fstype != info->fstype) | |
045fa1e1 SW |
153 | continue; |
154 | ||
2ded0d47 | 155 | if (!info->probe(fs_dev_desc, &fs_partition)) { |
436e2b73 | 156 | fs_type = info->fstype; |
045fa1e1 SW |
157 | return 0; |
158 | } | |
159 | } | |
160 | ||
045fa1e1 SW |
161 | return -1; |
162 | } | |
163 | ||
164 | static void fs_close(void) | |
165 | { | |
c6f548d2 | 166 | struct fstype_info *info = fs_get_info(fs_type); |
045fa1e1 | 167 | |
c6f548d2 | 168 | info->close(); |
e6d52415 | 169 | |
045fa1e1 SW |
170 | fs_type = FS_TYPE_ANY; |
171 | } | |
172 | ||
173 | int fs_ls(const char *dirname) | |
174 | { | |
175 | int ret; | |
176 | ||
c6f548d2 SG |
177 | struct fstype_info *info = fs_get_info(fs_type); |
178 | ||
179 | ret = info->ls(dirname); | |
045fa1e1 | 180 | |
e6d52415 | 181 | fs_type = FS_TYPE_ANY; |
045fa1e1 SW |
182 | fs_close(); |
183 | ||
184 | return ret; | |
185 | } | |
186 | ||
187 | int fs_read(const char *filename, ulong addr, int offset, int len) | |
188 | { | |
c6f548d2 | 189 | struct fstype_info *info = fs_get_info(fs_type); |
117e0507 | 190 | void *buf; |
045fa1e1 SW |
191 | int ret; |
192 | ||
117e0507 SG |
193 | /* |
194 | * We don't actually know how many bytes are being read, since len==0 | |
195 | * means read the whole file. | |
196 | */ | |
197 | buf = map_sysmem(addr, len); | |
198 | ret = info->read(filename, buf, offset, len); | |
199 | unmap_sysmem(buf); | |
045fa1e1 | 200 | |
c6f548d2 SG |
201 | /* If we requested a specific number of bytes, check we got it */ |
202 | if (ret >= 0 && len && ret != len) { | |
203 | printf("** Unable to read file %s **\n", filename); | |
204 | ret = -1; | |
205 | } | |
045fa1e1 SW |
206 | fs_close(); |
207 | ||
208 | return ret; | |
209 | } | |
210 | ||
a8f6ab52 SG |
211 | int fs_write(const char *filename, ulong addr, int offset, int len) |
212 | { | |
213 | struct fstype_info *info = fs_get_info(fs_type); | |
214 | void *buf; | |
215 | int ret; | |
216 | ||
a8f6ab52 SG |
217 | buf = map_sysmem(addr, len); |
218 | ret = info->write(filename, buf, offset, len); | |
219 | unmap_sysmem(buf); | |
220 | ||
bd6fb31f | 221 | if (ret >= 0 && ret != len) { |
a8f6ab52 SG |
222 | printf("** Unable to write file %s **\n", filename); |
223 | ret = -1; | |
224 | } | |
225 | fs_close(); | |
226 | ||
227 | return ret; | |
228 | } | |
229 | ||
f9b55e22 | 230 | int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
b770e88a | 231 | int fstype) |
045fa1e1 SW |
232 | { |
233 | unsigned long addr; | |
234 | const char *addr_str; | |
235 | const char *filename; | |
236 | unsigned long bytes; | |
237 | unsigned long pos; | |
238 | int len_read; | |
da1fd96c | 239 | unsigned long time; |
045fa1e1 | 240 | |
e9b0f99e SW |
241 | if (argc < 2) |
242 | return CMD_RET_USAGE; | |
243 | if (argc > 7) | |
045fa1e1 SW |
244 | return CMD_RET_USAGE; |
245 | ||
e9b0f99e | 246 | if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype)) |
045fa1e1 SW |
247 | return 1; |
248 | ||
249 | if (argc >= 4) { | |
b770e88a | 250 | addr = simple_strtoul(argv[3], NULL, 16); |
045fa1e1 SW |
251 | } else { |
252 | addr_str = getenv("loadaddr"); | |
253 | if (addr_str != NULL) | |
254 | addr = simple_strtoul(addr_str, NULL, 16); | |
255 | else | |
256 | addr = CONFIG_SYS_LOAD_ADDR; | |
257 | } | |
258 | if (argc >= 5) { | |
259 | filename = argv[4]; | |
260 | } else { | |
261 | filename = getenv("bootfile"); | |
262 | if (!filename) { | |
263 | puts("** No boot file defined **\n"); | |
264 | return 1; | |
265 | } | |
266 | } | |
267 | if (argc >= 6) | |
b770e88a | 268 | bytes = simple_strtoul(argv[5], NULL, 16); |
045fa1e1 SW |
269 | else |
270 | bytes = 0; | |
271 | if (argc >= 7) | |
b770e88a | 272 | pos = simple_strtoul(argv[6], NULL, 16); |
045fa1e1 SW |
273 | else |
274 | pos = 0; | |
275 | ||
da1fd96c | 276 | time = get_timer(0); |
045fa1e1 | 277 | len_read = fs_read(filename, addr, pos, bytes); |
da1fd96c | 278 | time = get_timer(time); |
045fa1e1 SW |
279 | if (len_read <= 0) |
280 | return 1; | |
281 | ||
da1fd96c AB |
282 | printf("%d bytes read in %lu ms", len_read, time); |
283 | if (time > 0) { | |
284 | puts(" ("); | |
285 | print_size(len_read / time * 1000, "/s"); | |
286 | puts(")"); | |
287 | } | |
288 | puts("\n"); | |
045fa1e1 | 289 | |
49c4f037 | 290 | setenv_hex("filesize", len_read); |
045fa1e1 SW |
291 | |
292 | return 0; | |
293 | } | |
294 | ||
295 | int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], | |
296 | int fstype) | |
297 | { | |
298 | if (argc < 2) | |
299 | return CMD_RET_USAGE; | |
e9b0f99e SW |
300 | if (argc > 4) |
301 | return CMD_RET_USAGE; | |
045fa1e1 SW |
302 | |
303 | if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype)) | |
304 | return 1; | |
305 | ||
e9b0f99e | 306 | if (fs_ls(argc >= 4 ? argv[3] : "/")) |
045fa1e1 SW |
307 | return 1; |
308 | ||
309 | return 0; | |
310 | } | |
a8f6ab52 SG |
311 | |
312 | int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], | |
b770e88a | 313 | int fstype) |
a8f6ab52 SG |
314 | { |
315 | unsigned long addr; | |
316 | const char *filename; | |
317 | unsigned long bytes; | |
318 | unsigned long pos; | |
319 | int len; | |
320 | unsigned long time; | |
321 | ||
322 | if (argc < 6 || argc > 7) | |
323 | return CMD_RET_USAGE; | |
324 | ||
325 | if (fs_set_blk_dev(argv[1], argv[2], fstype)) | |
326 | return 1; | |
327 | ||
328 | filename = argv[3]; | |
b770e88a WD |
329 | addr = simple_strtoul(argv[4], NULL, 16); |
330 | bytes = simple_strtoul(argv[5], NULL, 16); | |
a8f6ab52 | 331 | if (argc >= 7) |
b770e88a | 332 | pos = simple_strtoul(argv[6], NULL, 16); |
a8f6ab52 SG |
333 | else |
334 | pos = 0; | |
335 | ||
336 | time = get_timer(0); | |
337 | len = fs_write(filename, addr, pos, bytes); | |
338 | time = get_timer(time); | |
339 | if (len <= 0) | |
340 | return 1; | |
341 | ||
342 | printf("%d bytes written in %lu ms", len, time); | |
343 | if (time > 0) { | |
344 | puts(" ("); | |
345 | print_size(len / time * 1000, "/s"); | |
346 | puts(")"); | |
347 | } | |
348 | puts("\n"); | |
349 | ||
350 | return 0; | |
351 | } |