blob: 66835e2c747bd9455324147036d098ee27fb5384 [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>
23
Stephen Warrena1b231c2012-10-30 07:50:47 +000024DECLARE_GLOBAL_DATA_PTR;
25
Stephen Warren045fa1e2012-10-22 06:43:51 +000026static block_dev_desc_t *fs_dev_desc;
27static disk_partition_t fs_partition;
28static int fs_type = FS_TYPE_ANY;
29
Simon Glass436e2b72012-12-26 09:53:29 +000030static inline int fs_probe_unsupported(void)
31{
32 printf("** Unrecognized filesystem type **\n");
33 return -1;
34}
35
Stephen Warren045fa1e2012-10-22 06:43:51 +000036static inline int fs_ls_unsupported(const char *dirname)
37{
38 printf("** Unrecognized filesystem type **\n");
39 return -1;
40}
41
42static inline int fs_read_unsupported(const char *filename, ulong addr,
43 int offset, int len)
44{
45 printf("** Unrecognized filesystem type **\n");
46 return -1;
47}
48
Simon Glass436e2b72012-12-26 09:53:29 +000049static inline void fs_close_unsupported(void)
50{
51}
52
Stephen Warren045fa1e2012-10-22 06:43:51 +000053#ifdef CONFIG_FS_FAT
54static int fs_probe_fat(void)
55{
56 return fat_set_blk_dev(fs_dev_desc, &fs_partition);
57}
58
59static void fs_close_fat(void)
60{
61}
62
63#define fs_ls_fat file_fat_ls
64
65static int fs_read_fat(const char *filename, ulong addr, int offset, int len)
66{
67 int len_read;
68
69 len_read = file_fat_read_at(filename, offset,
70 (unsigned char *)addr, len);
71 if (len_read == -1) {
72 printf("** Unable to read file %s **\n", filename);
73 return -1;
74 }
75
76 return len_read;
77}
78#else
79static inline int fs_probe_fat(void)
80{
81 return -1;
82}
83
84static inline void fs_close_fat(void)
85{
86}
87
88#define fs_ls_fat fs_ls_unsupported
89#define fs_read_fat fs_read_unsupported
90#endif
91
92#ifdef CONFIG_FS_EXT4
93static int fs_probe_ext(void)
94{
95 ext4fs_set_blk_dev(fs_dev_desc, &fs_partition);
96
97 if (!ext4fs_mount(fs_partition.size)) {
98 ext4fs_close();
99 return -1;
100 }
101
102 return 0;
103}
104
105static void fs_close_ext(void)
106{
107 ext4fs_close();
108}
109
110#define fs_ls_ext ext4fs_ls
111
112static int fs_read_ext(const char *filename, ulong addr, int offset, int len)
113{
114 int file_len;
115 int len_read;
116
117 if (offset != 0) {
118 printf("** Cannot support non-zero offset **\n");
119 return -1;
120 }
121
122 file_len = ext4fs_open(filename);
123 if (file_len < 0) {
124 printf("** File not found %s **\n", filename);
125 ext4fs_close();
126 return -1;
127 }
128
129 if (len == 0)
130 len = file_len;
131
132 len_read = ext4fs_read((char *)addr, len);
133 ext4fs_close();
134
135 if (len_read != len) {
136 printf("** Unable to read file %s **\n", filename);
137 return -1;
138 }
139
140 return len_read;
141}
142#else
143static inline int fs_probe_ext(void)
144{
145 return -1;
146}
147
148static inline void fs_close_ext(void)
149{
150}
151
152#define fs_ls_ext fs_ls_unsupported
153#define fs_read_ext fs_read_unsupported
154#endif
155
Simon Glass436e2b72012-12-26 09:53:29 +0000156struct fstype_info {
Stephen Warren045fa1e2012-10-22 06:43:51 +0000157 int fstype;
158 int (*probe)(void);
Simon Glass436e2b72012-12-26 09:53:29 +0000159 int (*ls)(const char *dirname);
160 int (*read)(const char *filename, ulong addr, int offset, int len);
161 void (*close)(void);
162};
163
164static struct fstype_info fstypes[] = {
165#ifdef CONFIG_FS_FAT
Stephen Warren045fa1e2012-10-22 06:43:51 +0000166 {
167 .fstype = FS_TYPE_FAT,
168 .probe = fs_probe_fat,
Simon Glass436e2b72012-12-26 09:53:29 +0000169 .close = fs_close_fat,
170 .ls = file_fat_ls,
171 .read = fs_read_fat,
Stephen Warren045fa1e2012-10-22 06:43:51 +0000172 },
Simon Glass436e2b72012-12-26 09:53:29 +0000173#endif
174#ifdef CONFIG_FS_EXT4
Stephen Warren045fa1e2012-10-22 06:43:51 +0000175 {
176 .fstype = FS_TYPE_EXT,
177 .probe = fs_probe_ext,
Simon Glass436e2b72012-12-26 09:53:29 +0000178 .close = fs_close_ext,
179 .ls = ext4fs_ls,
180 .read = fs_read_ext,
181 },
182#endif
183 {
184 .fstype = FS_TYPE_ANY,
185 .probe = fs_probe_unsupported,
186 .close = fs_close_unsupported,
187 .ls = fs_ls_unsupported,
188 .read = fs_read_unsupported,
Stephen Warren045fa1e2012-10-22 06:43:51 +0000189 },
190};
191
192int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
193{
Simon Glass436e2b72012-12-26 09:53:29 +0000194 struct fstype_info *info;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000195 int part, i;
Stephen Warrena1b231c2012-10-30 07:50:47 +0000196#ifdef CONFIG_NEEDS_MANUAL_RELOC
197 static int relocated;
198
199 if (!relocated) {
Simon Glass436e2b72012-12-26 09:53:29 +0000200 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
201 i++, info++) {
202 info->probe += gd->reloc_off;
203 info->close += gd->reloc_off;
204 info->ls += gd->reloc_off;
205 info->read += gd->reloc_off;
206 }
Stephen Warrena1b231c2012-10-30 07:50:47 +0000207 relocated = 1;
208 }
209#endif
Stephen Warren045fa1e2012-10-22 06:43:51 +0000210
211 part = get_device_and_partition(ifname, dev_part_str, &fs_dev_desc,
212 &fs_partition, 1);
213 if (part < 0)
214 return -1;
215
Simon Glass436e2b72012-12-26 09:53:29 +0000216 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
217 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
218 fstype != info->fstype)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000219 continue;
220
Simon Glass436e2b72012-12-26 09:53:29 +0000221 if (!info->probe()) {
222 fs_type = info->fstype;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000223 return 0;
224 }
225 }
226
Stephen Warren045fa1e2012-10-22 06:43:51 +0000227 return -1;
228}
229
230static void fs_close(void)
231{
232 switch (fs_type) {
233 case FS_TYPE_FAT:
234 fs_close_fat();
235 break;
236 case FS_TYPE_EXT:
237 fs_close_ext();
238 break;
239 default:
240 break;
241 }
242
243 fs_type = FS_TYPE_ANY;
244}
245
246int fs_ls(const char *dirname)
247{
248 int ret;
249
250 switch (fs_type) {
251 case FS_TYPE_FAT:
252 ret = fs_ls_fat(dirname);
253 break;
254 case FS_TYPE_EXT:
255 ret = fs_ls_ext(dirname);
256 break;
257 default:
258 ret = fs_ls_unsupported(dirname);
259 break;
260 }
261
262 fs_close();
263
264 return ret;
265}
266
267int fs_read(const char *filename, ulong addr, int offset, int len)
268{
269 int ret;
270
271 switch (fs_type) {
272 case FS_TYPE_FAT:
273 ret = fs_read_fat(filename, addr, offset, len);
274 break;
275 case FS_TYPE_EXT:
276 ret = fs_read_ext(filename, addr, offset, len);
277 break;
278 default:
279 ret = fs_read_unsupported(filename, addr, offset, len);
280 break;
281 }
282
283 fs_close();
284
285 return ret;
286}
287
Stephen Warrenf9b55e22012-10-31 11:05:07 +0000288int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
Stephen Warren3f83c872012-10-30 12:04:19 +0000289 int fstype, int cmdline_base)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000290{
291 unsigned long addr;
292 const char *addr_str;
293 const char *filename;
294 unsigned long bytes;
295 unsigned long pos;
296 int len_read;
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100297 unsigned long time;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000298
Stephen Warrene9b0f992012-10-30 12:04:17 +0000299 if (argc < 2)
300 return CMD_RET_USAGE;
301 if (argc > 7)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000302 return CMD_RET_USAGE;
303
Stephen Warrene9b0f992012-10-30 12:04:17 +0000304 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
Stephen Warren045fa1e2012-10-22 06:43:51 +0000305 return 1;
306
307 if (argc >= 4) {
Stephen Warren3f83c872012-10-30 12:04:19 +0000308 addr = simple_strtoul(argv[3], NULL, cmdline_base);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000309 } else {
310 addr_str = getenv("loadaddr");
311 if (addr_str != NULL)
312 addr = simple_strtoul(addr_str, NULL, 16);
313 else
314 addr = CONFIG_SYS_LOAD_ADDR;
315 }
316 if (argc >= 5) {
317 filename = argv[4];
318 } else {
319 filename = getenv("bootfile");
320 if (!filename) {
321 puts("** No boot file defined **\n");
322 return 1;
323 }
324 }
325 if (argc >= 6)
Stephen Warren3f83c872012-10-30 12:04:19 +0000326 bytes = simple_strtoul(argv[5], NULL, cmdline_base);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000327 else
328 bytes = 0;
329 if (argc >= 7)
Stephen Warren3f83c872012-10-30 12:04:19 +0000330 pos = simple_strtoul(argv[6], NULL, cmdline_base);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000331 else
332 pos = 0;
333
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100334 time = get_timer(0);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000335 len_read = fs_read(filename, addr, pos, bytes);
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100336 time = get_timer(time);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000337 if (len_read <= 0)
338 return 1;
339
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100340 printf("%d bytes read in %lu ms", len_read, time);
341 if (time > 0) {
342 puts(" (");
343 print_size(len_read / time * 1000, "/s");
344 puts(")");
345 }
346 puts("\n");
Stephen Warren045fa1e2012-10-22 06:43:51 +0000347
Simon Glass49c4f032013-02-24 17:33:23 +0000348 setenv_hex("filesize", len_read);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000349
350 return 0;
351}
352
353int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
354 int fstype)
355{
356 if (argc < 2)
357 return CMD_RET_USAGE;
Stephen Warrene9b0f992012-10-30 12:04:17 +0000358 if (argc > 4)
359 return CMD_RET_USAGE;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000360
361 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
362 return 1;
363
Stephen Warrene9b0f992012-10-30 12:04:17 +0000364 if (fs_ls(argc >= 4 ? argv[3] : "/"))
Stephen Warren045fa1e2012-10-22 06:43:51 +0000365 return 1;
366
367 return 0;
368}