blob: 856d8baf9287d023de03b764b0b9e1d716ba7ae1 [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{
Stephen Warren045fa1e2012-10-22 06:43:51 +000038 return -1;
39}
40
41static inline int fs_read_unsupported(const char *filename, ulong addr,
42 int offset, int len)
43{
Stephen Warren045fa1e2012-10-22 06:43:51 +000044 return -1;
45}
46
Simon Glass436e2b72012-12-26 09:53:29 +000047static inline void fs_close_unsupported(void)
48{
49}
50
Stephen Warren045fa1e2012-10-22 06:43:51 +000051#ifdef CONFIG_FS_FAT
52static int fs_probe_fat(void)
53{
54 return fat_set_blk_dev(fs_dev_desc, &fs_partition);
55}
56
57static void fs_close_fat(void)
58{
59}
60
61#define fs_ls_fat file_fat_ls
62
63static int fs_read_fat(const char *filename, ulong addr, int offset, int len)
64{
65 int len_read;
66
67 len_read = file_fat_read_at(filename, offset,
68 (unsigned char *)addr, len);
69 if (len_read == -1) {
70 printf("** Unable to read file %s **\n", filename);
71 return -1;
72 }
73
74 return len_read;
75}
76#else
77static inline int fs_probe_fat(void)
78{
79 return -1;
80}
81
82static inline void fs_close_fat(void)
83{
84}
85
86#define fs_ls_fat fs_ls_unsupported
87#define fs_read_fat fs_read_unsupported
88#endif
89
90#ifdef CONFIG_FS_EXT4
91static int fs_probe_ext(void)
92{
93 ext4fs_set_blk_dev(fs_dev_desc, &fs_partition);
94
95 if (!ext4fs_mount(fs_partition.size)) {
96 ext4fs_close();
97 return -1;
98 }
99
100 return 0;
101}
102
103static void fs_close_ext(void)
104{
105 ext4fs_close();
106}
107
108#define fs_ls_ext ext4fs_ls
109
110static int fs_read_ext(const char *filename, ulong addr, int offset, int len)
111{
112 int file_len;
113 int len_read;
114
115 if (offset != 0) {
116 printf("** Cannot support non-zero offset **\n");
117 return -1;
118 }
119
120 file_len = ext4fs_open(filename);
121 if (file_len < 0) {
122 printf("** File not found %s **\n", filename);
123 ext4fs_close();
124 return -1;
125 }
126
127 if (len == 0)
128 len = file_len;
129
130 len_read = ext4fs_read((char *)addr, len);
131 ext4fs_close();
132
133 if (len_read != len) {
134 printf("** Unable to read file %s **\n", filename);
135 return -1;
136 }
137
138 return len_read;
139}
140#else
141static inline int fs_probe_ext(void)
142{
143 return -1;
144}
145
146static inline void fs_close_ext(void)
147{
148}
149
150#define fs_ls_ext fs_ls_unsupported
151#define fs_read_ext fs_read_unsupported
152#endif
153
Simon Glass436e2b72012-12-26 09:53:29 +0000154struct fstype_info {
Stephen Warren045fa1e2012-10-22 06:43:51 +0000155 int fstype;
156 int (*probe)(void);
Simon Glass436e2b72012-12-26 09:53:29 +0000157 int (*ls)(const char *dirname);
158 int (*read)(const char *filename, ulong addr, int offset, int len);
159 void (*close)(void);
160};
161
162static struct fstype_info fstypes[] = {
163#ifdef CONFIG_FS_FAT
Stephen Warren045fa1e2012-10-22 06:43:51 +0000164 {
165 .fstype = FS_TYPE_FAT,
166 .probe = fs_probe_fat,
Simon Glass436e2b72012-12-26 09:53:29 +0000167 .close = fs_close_fat,
168 .ls = file_fat_ls,
169 .read = fs_read_fat,
Stephen Warren045fa1e2012-10-22 06:43:51 +0000170 },
Simon Glass436e2b72012-12-26 09:53:29 +0000171#endif
172#ifdef CONFIG_FS_EXT4
Stephen Warren045fa1e2012-10-22 06:43:51 +0000173 {
174 .fstype = FS_TYPE_EXT,
175 .probe = fs_probe_ext,
Simon Glass436e2b72012-12-26 09:53:29 +0000176 .close = fs_close_ext,
177 .ls = ext4fs_ls,
178 .read = fs_read_ext,
179 },
180#endif
181 {
182 .fstype = FS_TYPE_ANY,
183 .probe = fs_probe_unsupported,
184 .close = fs_close_unsupported,
185 .ls = fs_ls_unsupported,
186 .read = fs_read_unsupported,
Stephen Warren045fa1e2012-10-22 06:43:51 +0000187 },
188};
189
Simon Glassc6f548d2012-12-26 09:53:30 +0000190static struct fstype_info *fs_get_info(int fstype)
191{
192 struct fstype_info *info;
193 int i;
194
195 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
196 if (fstype == info->fstype)
197 return info;
198 }
199
200 /* Return the 'unsupported' sentinel */
201 return info;
202}
203
Stephen Warren045fa1e2012-10-22 06:43:51 +0000204int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
205{
Simon Glass436e2b72012-12-26 09:53:29 +0000206 struct fstype_info *info;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000207 int part, i;
Stephen Warrena1b231c2012-10-30 07:50:47 +0000208#ifdef CONFIG_NEEDS_MANUAL_RELOC
209 static int relocated;
210
211 if (!relocated) {
Simon Glass436e2b72012-12-26 09:53:29 +0000212 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
213 i++, info++) {
214 info->probe += gd->reloc_off;
215 info->close += gd->reloc_off;
216 info->ls += gd->reloc_off;
217 info->read += gd->reloc_off;
218 }
Stephen Warrena1b231c2012-10-30 07:50:47 +0000219 relocated = 1;
220 }
221#endif
Stephen Warren045fa1e2012-10-22 06:43:51 +0000222
223 part = get_device_and_partition(ifname, dev_part_str, &fs_dev_desc,
224 &fs_partition, 1);
225 if (part < 0)
226 return -1;
227
Simon Glass436e2b72012-12-26 09:53:29 +0000228 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
229 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
230 fstype != info->fstype)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000231 continue;
232
Simon Glass436e2b72012-12-26 09:53:29 +0000233 if (!info->probe()) {
234 fs_type = info->fstype;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000235 return 0;
236 }
237 }
238
Stephen Warren045fa1e2012-10-22 06:43:51 +0000239 return -1;
240}
241
242static void fs_close(void)
243{
Simon Glassc6f548d2012-12-26 09:53:30 +0000244 struct fstype_info *info = fs_get_info(fs_type);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000245
Simon Glassc6f548d2012-12-26 09:53:30 +0000246 info->close();
Stephen Warren045fa1e2012-10-22 06:43:51 +0000247 fs_type = FS_TYPE_ANY;
248}
249
250int fs_ls(const char *dirname)
251{
252 int ret;
253
Simon Glassc6f548d2012-12-26 09:53:30 +0000254 struct fstype_info *info = fs_get_info(fs_type);
255
256 ret = info->ls(dirname);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000257
258 fs_close();
259
260 return ret;
261}
262
263int fs_read(const char *filename, ulong addr, int offset, int len)
264{
Simon Glassc6f548d2012-12-26 09:53:30 +0000265 struct fstype_info *info = fs_get_info(fs_type);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000266 int ret;
267
Simon Glassc6f548d2012-12-26 09:53:30 +0000268 ret = info->read(filename, addr, offset, len);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000269
Simon Glassc6f548d2012-12-26 09:53:30 +0000270 /* If we requested a specific number of bytes, check we got it */
271 if (ret >= 0 && len && ret != len) {
272 printf("** Unable to read file %s **\n", filename);
273 ret = -1;
274 }
Stephen Warren045fa1e2012-10-22 06:43:51 +0000275 fs_close();
276
277 return ret;
278}
279
Stephen Warrenf9b55e22012-10-31 11:05:07 +0000280int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
Stephen Warren3f83c872012-10-30 12:04:19 +0000281 int fstype, int cmdline_base)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000282{
283 unsigned long addr;
284 const char *addr_str;
285 const char *filename;
286 unsigned long bytes;
287 unsigned long pos;
288 int len_read;
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100289 unsigned long time;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000290
Stephen Warrene9b0f992012-10-30 12:04:17 +0000291 if (argc < 2)
292 return CMD_RET_USAGE;
293 if (argc > 7)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000294 return CMD_RET_USAGE;
295
Stephen Warrene9b0f992012-10-30 12:04:17 +0000296 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
Stephen Warren045fa1e2012-10-22 06:43:51 +0000297 return 1;
298
299 if (argc >= 4) {
Stephen Warren3f83c872012-10-30 12:04:19 +0000300 addr = simple_strtoul(argv[3], NULL, cmdline_base);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000301 } else {
302 addr_str = getenv("loadaddr");
303 if (addr_str != NULL)
304 addr = simple_strtoul(addr_str, NULL, 16);
305 else
306 addr = CONFIG_SYS_LOAD_ADDR;
307 }
308 if (argc >= 5) {
309 filename = argv[4];
310 } else {
311 filename = getenv("bootfile");
312 if (!filename) {
313 puts("** No boot file defined **\n");
314 return 1;
315 }
316 }
317 if (argc >= 6)
Stephen Warren3f83c872012-10-30 12:04:19 +0000318 bytes = simple_strtoul(argv[5], NULL, cmdline_base);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000319 else
320 bytes = 0;
321 if (argc >= 7)
Stephen Warren3f83c872012-10-30 12:04:19 +0000322 pos = simple_strtoul(argv[6], NULL, cmdline_base);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000323 else
324 pos = 0;
325
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100326 time = get_timer(0);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000327 len_read = fs_read(filename, addr, pos, bytes);
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100328 time = get_timer(time);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000329 if (len_read <= 0)
330 return 1;
331
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100332 printf("%d bytes read in %lu ms", len_read, time);
333 if (time > 0) {
334 puts(" (");
335 print_size(len_read / time * 1000, "/s");
336 puts(")");
337 }
338 puts("\n");
Stephen Warren045fa1e2012-10-22 06:43:51 +0000339
Simon Glass49c4f032013-02-24 17:33:23 +0000340 setenv_hex("filesize", len_read);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000341
342 return 0;
343}
344
345int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
346 int fstype)
347{
348 if (argc < 2)
349 return CMD_RET_USAGE;
Stephen Warrene9b0f992012-10-30 12:04:17 +0000350 if (argc > 4)
351 return CMD_RET_USAGE;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000352
353 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
354 return 1;
355
Stephen Warrene9b0f992012-10-30 12:04:17 +0000356 if (fs_ls(argc >= 4 ? argv[3] : "/"))
Stephen Warren045fa1e2012-10-22 06:43:51 +0000357 return 1;
358
359 return 0;
360}