blob: 0cbec9958e7631001138c56fb2f1c20df28aa4ce [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>
Simon Glass117e0502012-12-26 09:53:32 +000023#include <asm/io.h>
Stephen Warren045fa1e2012-10-22 06:43:51 +000024
Stephen Warrena1b231c2012-10-30 07:50:47 +000025DECLARE_GLOBAL_DATA_PTR;
26
Stephen Warren045fa1e2012-10-22 06:43:51 +000027static block_dev_desc_t *fs_dev_desc;
28static disk_partition_t fs_partition;
29static int fs_type = FS_TYPE_ANY;
30
Simon Glass2ded0d42012-12-26 09:53:31 +000031static inline int fs_probe_unsupported(block_dev_desc_t *fs_dev_desc,
32 disk_partition_t *fs_partition)
Simon Glass436e2b72012-12-26 09:53:29 +000033{
34 printf("** Unrecognized filesystem type **\n");
35 return -1;
36}
37
Stephen Warren045fa1e2012-10-22 06:43:51 +000038static inline int fs_ls_unsupported(const char *dirname)
39{
Stephen Warren045fa1e2012-10-22 06:43:51 +000040 return -1;
41}
42
Simon Glass117e0502012-12-26 09:53:32 +000043static inline int fs_read_unsupported(const char *filename, void *buf,
Stephen Warren045fa1e2012-10-22 06:43:51 +000044 int offset, int len)
45{
Stephen Warren045fa1e2012-10-22 06:43:51 +000046 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
Simon Glass2ded0d42012-12-26 09:53:31 +000054static int fs_probe_fat(block_dev_desc_t *fs_dev_desc,
55 disk_partition_t *fs_partition)
Stephen Warren045fa1e2012-10-22 06:43:51 +000056{
Simon Glass2ded0d42012-12-26 09:53:31 +000057 return fat_set_blk_dev(fs_dev_desc, fs_partition);
Stephen Warren045fa1e2012-10-22 06:43:51 +000058}
59
60static void fs_close_fat(void)
61{
62}
63
64#define fs_ls_fat file_fat_ls
65
Simon Glass117e0502012-12-26 09:53:32 +000066static int fs_read_fat(const char *filename, void *buf, int offset, int len)
Stephen Warren045fa1e2012-10-22 06:43:51 +000067{
68 int len_read;
69
Simon Glass117e0502012-12-26 09:53:32 +000070 len_read = file_fat_read_at(filename, offset, buf, len);
Stephen Warren045fa1e2012-10-22 06:43:51 +000071 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
Simon Glass2ded0d42012-12-26 09:53:31 +000093static int fs_probe_ext(block_dev_desc_t *fs_dev_desc,
94 disk_partition_t *fs_partition)
Stephen Warren045fa1e2012-10-22 06:43:51 +000095{
Simon Glass2ded0d42012-12-26 09:53:31 +000096 ext4fs_set_blk_dev(fs_dev_desc, fs_partition);
Stephen Warren045fa1e2012-10-22 06:43:51 +000097
Simon Glass2ded0d42012-12-26 09:53:31 +000098 if (!ext4fs_mount(fs_partition->size)) {
Stephen Warren045fa1e2012-10-22 06:43:51 +000099 ext4fs_close();
100 return -1;
101 }
102
103 return 0;
104}
105
106static void fs_close_ext(void)
107{
108 ext4fs_close();
109}
110
111#define fs_ls_ext ext4fs_ls
112
Simon Glass117e0502012-12-26 09:53:32 +0000113static int fs_read_ext(const char *filename, void *buf, int offset, int len)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000114{
115 int file_len;
116 int len_read;
117
118 if (offset != 0) {
119 printf("** Cannot support non-zero offset **\n");
120 return -1;
121 }
122
123 file_len = ext4fs_open(filename);
124 if (file_len < 0) {
125 printf("** File not found %s **\n", filename);
126 ext4fs_close();
127 return -1;
128 }
129
130 if (len == 0)
131 len = file_len;
132
Simon Glass117e0502012-12-26 09:53:32 +0000133 len_read = ext4fs_read(buf, len);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000134 ext4fs_close();
135
136 if (len_read != len) {
137 printf("** Unable to read file %s **\n", filename);
138 return -1;
139 }
140
141 return len_read;
142}
143#else
144static inline int fs_probe_ext(void)
145{
146 return -1;
147}
148
149static inline void fs_close_ext(void)
150{
151}
152
153#define fs_ls_ext fs_ls_unsupported
154#define fs_read_ext fs_read_unsupported
155#endif
156
Simon Glass436e2b72012-12-26 09:53:29 +0000157struct fstype_info {
Stephen Warren045fa1e2012-10-22 06:43:51 +0000158 int fstype;
Simon Glass2ded0d42012-12-26 09:53:31 +0000159 int (*probe)(block_dev_desc_t *fs_dev_desc,
160 disk_partition_t *fs_partition);
Simon Glass436e2b72012-12-26 09:53:29 +0000161 int (*ls)(const char *dirname);
Simon Glass117e0502012-12-26 09:53:32 +0000162 int (*read)(const char *filename, void *buf, int offset, int len);
Simon Glass436e2b72012-12-26 09:53:29 +0000163 void (*close)(void);
164};
165
166static struct fstype_info fstypes[] = {
167#ifdef CONFIG_FS_FAT
Stephen Warren045fa1e2012-10-22 06:43:51 +0000168 {
169 .fstype = FS_TYPE_FAT,
170 .probe = fs_probe_fat,
Simon Glass436e2b72012-12-26 09:53:29 +0000171 .close = fs_close_fat,
172 .ls = file_fat_ls,
173 .read = fs_read_fat,
Stephen Warren045fa1e2012-10-22 06:43:51 +0000174 },
Simon Glass436e2b72012-12-26 09:53:29 +0000175#endif
176#ifdef CONFIG_FS_EXT4
Stephen Warren045fa1e2012-10-22 06:43:51 +0000177 {
178 .fstype = FS_TYPE_EXT,
179 .probe = fs_probe_ext,
Simon Glass436e2b72012-12-26 09:53:29 +0000180 .close = fs_close_ext,
181 .ls = ext4fs_ls,
182 .read = fs_read_ext,
183 },
184#endif
185 {
186 .fstype = FS_TYPE_ANY,
187 .probe = fs_probe_unsupported,
188 .close = fs_close_unsupported,
189 .ls = fs_ls_unsupported,
190 .read = fs_read_unsupported,
Stephen Warren045fa1e2012-10-22 06:43:51 +0000191 },
192};
193
Simon Glassc6f548d2012-12-26 09:53:30 +0000194static struct fstype_info *fs_get_info(int fstype)
195{
196 struct fstype_info *info;
197 int i;
198
199 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
200 if (fstype == info->fstype)
201 return info;
202 }
203
204 /* Return the 'unsupported' sentinel */
205 return info;
206}
207
Stephen Warren045fa1e2012-10-22 06:43:51 +0000208int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
209{
Simon Glass436e2b72012-12-26 09:53:29 +0000210 struct fstype_info *info;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000211 int part, i;
Stephen Warrena1b231c2012-10-30 07:50:47 +0000212#ifdef CONFIG_NEEDS_MANUAL_RELOC
213 static int relocated;
214
215 if (!relocated) {
Simon Glass436e2b72012-12-26 09:53:29 +0000216 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
217 i++, info++) {
218 info->probe += gd->reloc_off;
219 info->close += gd->reloc_off;
220 info->ls += gd->reloc_off;
221 info->read += gd->reloc_off;
222 }
Stephen Warrena1b231c2012-10-30 07:50:47 +0000223 relocated = 1;
224 }
225#endif
Stephen Warren045fa1e2012-10-22 06:43:51 +0000226
227 part = get_device_and_partition(ifname, dev_part_str, &fs_dev_desc,
228 &fs_partition, 1);
229 if (part < 0)
230 return -1;
231
Simon Glass436e2b72012-12-26 09:53:29 +0000232 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
233 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
234 fstype != info->fstype)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000235 continue;
236
Simon Glass2ded0d42012-12-26 09:53:31 +0000237 if (!info->probe(fs_dev_desc, &fs_partition)) {
Simon Glass436e2b72012-12-26 09:53:29 +0000238 fs_type = info->fstype;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000239 return 0;
240 }
241 }
242
Stephen Warren045fa1e2012-10-22 06:43:51 +0000243 return -1;
244}
245
246static void fs_close(void)
247{
Simon Glassc6f548d2012-12-26 09:53:30 +0000248 struct fstype_info *info = fs_get_info(fs_type);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000249
Simon Glassc6f548d2012-12-26 09:53:30 +0000250 info->close();
Stephen Warren045fa1e2012-10-22 06:43:51 +0000251 fs_type = FS_TYPE_ANY;
252}
253
254int fs_ls(const char *dirname)
255{
256 int ret;
257
Simon Glassc6f548d2012-12-26 09:53:30 +0000258 struct fstype_info *info = fs_get_info(fs_type);
259
260 ret = info->ls(dirname);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000261
262 fs_close();
263
264 return ret;
265}
266
267int fs_read(const char *filename, ulong addr, int offset, int len)
268{
Simon Glassc6f548d2012-12-26 09:53:30 +0000269 struct fstype_info *info = fs_get_info(fs_type);
Simon Glass117e0502012-12-26 09:53:32 +0000270 void *buf;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000271 int ret;
272
Simon Glass117e0502012-12-26 09:53:32 +0000273 /*
274 * We don't actually know how many bytes are being read, since len==0
275 * means read the whole file.
276 */
277 buf = map_sysmem(addr, len);
278 ret = info->read(filename, buf, offset, len);
279 unmap_sysmem(buf);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000280
Simon Glassc6f548d2012-12-26 09:53:30 +0000281 /* If we requested a specific number of bytes, check we got it */
282 if (ret >= 0 && len && ret != len) {
283 printf("** Unable to read file %s **\n", filename);
284 ret = -1;
285 }
Stephen Warren045fa1e2012-10-22 06:43:51 +0000286 fs_close();
287
288 return ret;
289}
290
Stephen Warrenf9b55e22012-10-31 11:05:07 +0000291int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
Stephen Warren3f83c872012-10-30 12:04:19 +0000292 int fstype, int cmdline_base)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000293{
294 unsigned long addr;
295 const char *addr_str;
296 const char *filename;
297 unsigned long bytes;
298 unsigned long pos;
299 int len_read;
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100300 unsigned long time;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000301
Stephen Warrene9b0f992012-10-30 12:04:17 +0000302 if (argc < 2)
303 return CMD_RET_USAGE;
304 if (argc > 7)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000305 return CMD_RET_USAGE;
306
Stephen Warrene9b0f992012-10-30 12:04:17 +0000307 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
Stephen Warren045fa1e2012-10-22 06:43:51 +0000308 return 1;
309
310 if (argc >= 4) {
Stephen Warren3f83c872012-10-30 12:04:19 +0000311 addr = simple_strtoul(argv[3], NULL, cmdline_base);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000312 } else {
313 addr_str = getenv("loadaddr");
314 if (addr_str != NULL)
315 addr = simple_strtoul(addr_str, NULL, 16);
316 else
317 addr = CONFIG_SYS_LOAD_ADDR;
318 }
319 if (argc >= 5) {
320 filename = argv[4];
321 } else {
322 filename = getenv("bootfile");
323 if (!filename) {
324 puts("** No boot file defined **\n");
325 return 1;
326 }
327 }
328 if (argc >= 6)
Stephen Warren3f83c872012-10-30 12:04:19 +0000329 bytes = simple_strtoul(argv[5], NULL, cmdline_base);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000330 else
331 bytes = 0;
332 if (argc >= 7)
Stephen Warren3f83c872012-10-30 12:04:19 +0000333 pos = simple_strtoul(argv[6], NULL, cmdline_base);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000334 else
335 pos = 0;
336
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100337 time = get_timer(0);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000338 len_read = fs_read(filename, addr, pos, bytes);
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100339 time = get_timer(time);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000340 if (len_read <= 0)
341 return 1;
342
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100343 printf("%d bytes read in %lu ms", len_read, time);
344 if (time > 0) {
345 puts(" (");
346 print_size(len_read / time * 1000, "/s");
347 puts(")");
348 }
349 puts("\n");
Stephen Warren045fa1e2012-10-22 06:43:51 +0000350
Simon Glass49c4f032013-02-24 17:33:23 +0000351 setenv_hex("filesize", len_read);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000352
353 return 0;
354}
355
356int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
357 int fstype)
358{
359 if (argc < 2)
360 return CMD_RET_USAGE;
Stephen Warrene9b0f992012-10-30 12:04:17 +0000361 if (argc > 4)
362 return CMD_RET_USAGE;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000363
364 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
365 return 1;
366
Stephen Warrene9b0f992012-10-30 12:04:17 +0000367 if (fs_ls(argc >= 4 ? argv[3] : "/"))
Stephen Warren045fa1e2012-10-22 06:43:51 +0000368 return 1;
369
370 return 0;
371}