Blame view

fs/fs.c 10.6 KB
045fa1e11   Stephen Warren   fs: add filesyste...
1
2
3
  /*
   * Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.
   *
5b8031ccb   Tom Rini   Add more SPDX-Lic...
4
   * SPDX-License-Identifier:	GPL-2.0
045fa1e11   Stephen Warren   fs: add filesyste...
5
6
7
   */
  
  #include <config.h>
59e890ef7   Christian Gmeiner   fs: make it possi...
8
  #include <errno.h>
045fa1e11   Stephen Warren   fs: add filesyste...
9
  #include <common.h>
0eb25b619   Joe Hershberger   common: Make sure...
10
  #include <mapmem.h>
045fa1e11   Stephen Warren   fs: add filesyste...
11
12
13
14
  #include <part.h>
  #include <ext4fs.h>
  #include <fat.h>
  #include <fs.h>
92ccc96bf   Simon Glass   sandbox: Add host...
15
  #include <sandboxfs.h>
251cee0db   Hans de Goede   ubifs: Add generi...
16
  #include <ubifs_uboot.h>
117e05072   Simon Glass   fs: Use map_sysme...
17
  #include <asm/io.h>
9e374e7b7   Tom Rini   fs/ext4/ext4fs.c,...
18
19
  #include <div64.h>
  #include <linux/math64.h>
045fa1e11   Stephen Warren   fs: add filesyste...
20

a1b231cef   Stephen Warren   fs: handle CONFIG...
21
  DECLARE_GLOBAL_DATA_PTR;
4101f6879   Simon Glass   dm: Drop the bloc...
22
  static struct blk_desc *fs_dev_desc;
045fa1e11   Stephen Warren   fs: add filesyste...
23
24
  static disk_partition_t fs_partition;
  static int fs_type = FS_TYPE_ANY;
4101f6879   Simon Glass   dm: Drop the bloc...
25
  static inline int fs_probe_unsupported(struct blk_desc *fs_dev_desc,
2ded0d471   Simon Glass   fs: Tell probe fu...
26
  				      disk_partition_t *fs_partition)
436e2b731   Simon Glass   fs: Fully populat...
27
28
29
30
31
  {
  	printf("** Unrecognized filesystem type **
  ");
  	return -1;
  }
045fa1e11   Stephen Warren   fs: add filesyste...
32
33
  static inline int fs_ls_unsupported(const char *dirname)
  {
045fa1e11   Stephen Warren   fs: add filesyste...
34
35
  	return -1;
  }
6152916a9   Stephen Warren   fs: implement inf...
36
37
38
39
  static inline int fs_exists_unsupported(const char *filename)
  {
  	return 0;
  }
d455d8789   Suriyan Ramasami   fs: API changes e...
40
  static inline int fs_size_unsupported(const char *filename, loff_t *size)
cf6598193   Stephen Warren   fs: implement siz...
41
42
43
  {
  	return -1;
  }
117e05072   Simon Glass   fs: Use map_sysme...
44
  static inline int fs_read_unsupported(const char *filename, void *buf,
d455d8789   Suriyan Ramasami   fs: API changes e...
45
46
  				      loff_t offset, loff_t len,
  				      loff_t *actread)
045fa1e11   Stephen Warren   fs: add filesyste...
47
  {
045fa1e11   Stephen Warren   fs: add filesyste...
48
49
  	return -1;
  }
a8f6ab522   Simon Glass   fs: Add support f...
50
  static inline int fs_write_unsupported(const char *filename, void *buf,
d455d8789   Suriyan Ramasami   fs: API changes e...
51
52
  				      loff_t offset, loff_t len,
  				      loff_t *actwrite)
a8f6ab522   Simon Glass   fs: Add support f...
53
54
55
  {
  	return -1;
  }
436e2b731   Simon Glass   fs: Fully populat...
56
57
58
  static inline void fs_close_unsupported(void)
  {
  }
59e890ef7   Christian Gmeiner   fs: make it possi...
59
60
61
62
  static inline int fs_uuid_unsupported(char *uuid_str)
  {
  	return -1;
  }
436e2b731   Simon Glass   fs: Fully populat...
63
  struct fstype_info {
045fa1e11   Stephen Warren   fs: add filesyste...
64
  	int fstype;
1a1ad8e09   Sjoerd Simons   fs: Add command t...
65
  	char *name;
377202b56   Stephen Warren   fs: don't pass NU...
66
67
68
69
70
71
72
73
74
  	/*
  	 * Is it legal to pass NULL as .probe()'s  fs_dev_desc parameter? This
  	 * should be false in most cases. For "virtual" filesystems which
  	 * aren't based on a U-Boot block device (e.g. sandbox), this can be
  	 * set to true. This should also be true for the dumm entry at the end
  	 * of fstypes[], since that is essentially a "virtual" (non-existent)
  	 * filesystem.
  	 */
  	bool null_dev_desc_ok;
4101f6879   Simon Glass   dm: Drop the bloc...
75
  	int (*probe)(struct blk_desc *fs_dev_desc,
2ded0d471   Simon Glass   fs: Tell probe fu...
76
  		     disk_partition_t *fs_partition);
436e2b731   Simon Glass   fs: Fully populat...
77
  	int (*ls)(const char *dirname);
6152916a9   Stephen Warren   fs: implement inf...
78
  	int (*exists)(const char *filename);
d455d8789   Suriyan Ramasami   fs: API changes e...
79
80
81
82
83
  	int (*size)(const char *filename, loff_t *size);
  	int (*read)(const char *filename, void *buf, loff_t offset,
  		    loff_t len, loff_t *actread);
  	int (*write)(const char *filename, void *buf, loff_t offset,
  		     loff_t len, loff_t *actwrite);
436e2b731   Simon Glass   fs: Fully populat...
84
  	void (*close)(void);
59e890ef7   Christian Gmeiner   fs: make it possi...
85
  	int (*uuid)(char *uuid_str);
436e2b731   Simon Glass   fs: Fully populat...
86
87
88
89
  };
  
  static struct fstype_info fstypes[] = {
  #ifdef CONFIG_FS_FAT
045fa1e11   Stephen Warren   fs: add filesyste...
90
91
  	{
  		.fstype = FS_TYPE_FAT,
1a1ad8e09   Sjoerd Simons   fs: Add command t...
92
  		.name = "fat",
377202b56   Stephen Warren   fs: don't pass NU...
93
  		.null_dev_desc_ok = false,
e6d524153   Simon Glass   fs: Move ls and r...
94
95
  		.probe = fat_set_blk_dev,
  		.close = fat_close,
436e2b731   Simon Glass   fs: Fully populat...
96
  		.ls = file_fat_ls,
b7b5f3195   Stephen Warren   fat: implement ex...
97
  		.exists = fat_exists,
cf6598193   Stephen Warren   fs: implement siz...
98
  		.size = fat_size,
e6d524153   Simon Glass   fs: Move ls and r...
99
  		.read = fat_read_file,
d455d8789   Suriyan Ramasami   fs: API changes e...
100
101
102
  #ifdef CONFIG_FAT_WRITE
  		.write = file_fat_write,
  #else
bd6fb31fa   Stephen Warren   fs: fix generic s...
103
  		.write = fs_write_unsupported,
d455d8789   Suriyan Ramasami   fs: API changes e...
104
  #endif
59e890ef7   Christian Gmeiner   fs: make it possi...
105
  		.uuid = fs_uuid_unsupported,
045fa1e11   Stephen Warren   fs: add filesyste...
106
  	},
436e2b731   Simon Glass   fs: Fully populat...
107
108
  #endif
  #ifdef CONFIG_FS_EXT4
045fa1e11   Stephen Warren   fs: add filesyste...
109
110
  	{
  		.fstype = FS_TYPE_EXT,
1a1ad8e09   Sjoerd Simons   fs: Add command t...
111
  		.name = "ext4",
377202b56   Stephen Warren   fs: don't pass NU...
112
  		.null_dev_desc_ok = false,
e6d524153   Simon Glass   fs: Move ls and r...
113
114
  		.probe = ext4fs_probe,
  		.close = ext4fs_close,
436e2b731   Simon Glass   fs: Fully populat...
115
  		.ls = ext4fs_ls,
55af5c931   Stephen Warren   ext4: implement e...
116
  		.exists = ext4fs_exists,
cf6598193   Stephen Warren   fs: implement siz...
117
  		.size = ext4fs_size,
e6d524153   Simon Glass   fs: Move ls and r...
118
  		.read = ext4_read_file,
d455d8789   Suriyan Ramasami   fs: API changes e...
119
120
121
  #ifdef CONFIG_CMD_EXT4_WRITE
  		.write = ext4_write_file,
  #else
bd6fb31fa   Stephen Warren   fs: fix generic s...
122
  		.write = fs_write_unsupported,
d455d8789   Suriyan Ramasami   fs: API changes e...
123
  #endif
59e890ef7   Christian Gmeiner   fs: make it possi...
124
  		.uuid = ext4fs_uuid,
436e2b731   Simon Glass   fs: Fully populat...
125
126
  	},
  #endif
92ccc96bf   Simon Glass   sandbox: Add host...
127
128
129
  #ifdef CONFIG_SANDBOX
  	{
  		.fstype = FS_TYPE_SANDBOX,
1a1ad8e09   Sjoerd Simons   fs: Add command t...
130
  		.name = "sandbox",
377202b56   Stephen Warren   fs: don't pass NU...
131
  		.null_dev_desc_ok = true,
92ccc96bf   Simon Glass   sandbox: Add host...
132
133
134
  		.probe = sandbox_fs_set_blk_dev,
  		.close = sandbox_fs_close,
  		.ls = sandbox_fs_ls,
0a30aa1e7   Stephen Warren   sandbox: implemen...
135
  		.exists = sandbox_fs_exists,
cf6598193   Stephen Warren   fs: implement siz...
136
  		.size = sandbox_fs_size,
92ccc96bf   Simon Glass   sandbox: Add host...
137
  		.read = fs_read_sandbox,
7eb2c8d57   Simon Glass   sandbox: fs: Add ...
138
  		.write = fs_write_sandbox,
59e890ef7   Christian Gmeiner   fs: make it possi...
139
  		.uuid = fs_uuid_unsupported,
92ccc96bf   Simon Glass   sandbox: Add host...
140
141
  	},
  #endif
251cee0db   Hans de Goede   ubifs: Add generi...
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
  #ifdef CONFIG_CMD_UBIFS
  	{
  		.fstype = FS_TYPE_UBIFS,
  		.name = "ubifs",
  		.null_dev_desc_ok = true,
  		.probe = ubifs_set_blk_dev,
  		.close = ubifs_close,
  		.ls = ubifs_ls,
  		.exists = ubifs_exists,
  		.size = ubifs_size,
  		.read = ubifs_read,
  		.write = fs_write_unsupported,
  		.uuid = fs_uuid_unsupported,
  	},
  #endif
436e2b731   Simon Glass   fs: Fully populat...
157
158
  	{
  		.fstype = FS_TYPE_ANY,
1a1ad8e09   Sjoerd Simons   fs: Add command t...
159
  		.name = "unsupported",
377202b56   Stephen Warren   fs: don't pass NU...
160
  		.null_dev_desc_ok = true,
436e2b731   Simon Glass   fs: Fully populat...
161
162
163
  		.probe = fs_probe_unsupported,
  		.close = fs_close_unsupported,
  		.ls = fs_ls_unsupported,
6152916a9   Stephen Warren   fs: implement inf...
164
  		.exists = fs_exists_unsupported,
cf6598193   Stephen Warren   fs: implement siz...
165
  		.size = fs_size_unsupported,
436e2b731   Simon Glass   fs: Fully populat...
166
  		.read = fs_read_unsupported,
a8f6ab522   Simon Glass   fs: Add support f...
167
  		.write = fs_write_unsupported,
59e890ef7   Christian Gmeiner   fs: make it possi...
168
  		.uuid = fs_uuid_unsupported,
045fa1e11   Stephen Warren   fs: add filesyste...
169
170
  	},
  };
c6f548d23   Simon Glass   fs: Use filesyste...
171
172
173
174
175
176
177
178
179
180
181
182
183
  static struct fstype_info *fs_get_info(int fstype)
  {
  	struct fstype_info *info;
  	int i;
  
  	for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
  		if (fstype == info->fstype)
  			return info;
  	}
  
  	/* Return the 'unsupported' sentinel */
  	return info;
  }
045fa1e11   Stephen Warren   fs: add filesyste...
184
185
  int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
  {
436e2b731   Simon Glass   fs: Fully populat...
186
  	struct fstype_info *info;
045fa1e11   Stephen Warren   fs: add filesyste...
187
  	int part, i;
a1b231cef   Stephen Warren   fs: handle CONFIG...
188
189
190
191
  #ifdef CONFIG_NEEDS_MANUAL_RELOC
  	static int relocated;
  
  	if (!relocated) {
436e2b731   Simon Glass   fs: Fully populat...
192
193
  		for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
  				i++, info++) {
1a1ad8e09   Sjoerd Simons   fs: Add command t...
194
  			info->name += gd->reloc_off;
436e2b731   Simon Glass   fs: Fully populat...
195
196
197
198
  			info->probe += gd->reloc_off;
  			info->close += gd->reloc_off;
  			info->ls += gd->reloc_off;
  			info->read += gd->reloc_off;
a8f6ab522   Simon Glass   fs: Add support f...
199
  			info->write += gd->reloc_off;
436e2b731   Simon Glass   fs: Fully populat...
200
  		}
a1b231cef   Stephen Warren   fs: handle CONFIG...
201
202
203
  		relocated = 1;
  	}
  #endif
045fa1e11   Stephen Warren   fs: add filesyste...
204

e35929e4a   Simon Glass   dm: blk: Rename g...
205
  	part = blk_get_device_part_str(ifname, dev_part_str, &fs_dev_desc,
045fa1e11   Stephen Warren   fs: add filesyste...
206
207
208
  					&fs_partition, 1);
  	if (part < 0)
  		return -1;
436e2b731   Simon Glass   fs: Fully populat...
209
210
211
  	for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
  		if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
  				fstype != info->fstype)
045fa1e11   Stephen Warren   fs: add filesyste...
212
  			continue;
377202b56   Stephen Warren   fs: don't pass NU...
213
214
  		if (!fs_dev_desc && !info->null_dev_desc_ok)
  			continue;
2ded0d471   Simon Glass   fs: Tell probe fu...
215
  		if (!info->probe(fs_dev_desc, &fs_partition)) {
436e2b731   Simon Glass   fs: Fully populat...
216
  			fs_type = info->fstype;
045fa1e11   Stephen Warren   fs: add filesyste...
217
218
219
  			return 0;
  		}
  	}
045fa1e11   Stephen Warren   fs: add filesyste...
220
221
222
223
224
  	return -1;
  }
  
  static void fs_close(void)
  {
c6f548d23   Simon Glass   fs: Use filesyste...
225
  	struct fstype_info *info = fs_get_info(fs_type);
045fa1e11   Stephen Warren   fs: add filesyste...
226

c6f548d23   Simon Glass   fs: Use filesyste...
227
  	info->close();
e6d524153   Simon Glass   fs: Move ls and r...
228

045fa1e11   Stephen Warren   fs: add filesyste...
229
230
  	fs_type = FS_TYPE_ANY;
  }
59e890ef7   Christian Gmeiner   fs: make it possi...
231
232
233
234
235
236
  int fs_uuid(char *uuid_str)
  {
  	struct fstype_info *info = fs_get_info(fs_type);
  
  	return info->uuid(uuid_str);
  }
045fa1e11   Stephen Warren   fs: add filesyste...
237
238
239
  int fs_ls(const char *dirname)
  {
  	int ret;
c6f548d23   Simon Glass   fs: Use filesyste...
240
241
242
  	struct fstype_info *info = fs_get_info(fs_type);
  
  	ret = info->ls(dirname);
045fa1e11   Stephen Warren   fs: add filesyste...
243

e6d524153   Simon Glass   fs: Move ls and r...
244
  	fs_type = FS_TYPE_ANY;
045fa1e11   Stephen Warren   fs: add filesyste...
245
246
247
248
  	fs_close();
  
  	return ret;
  }
6152916a9   Stephen Warren   fs: implement inf...
249
250
251
252
253
254
255
256
257
258
259
260
  int fs_exists(const char *filename)
  {
  	int ret;
  
  	struct fstype_info *info = fs_get_info(fs_type);
  
  	ret = info->exists(filename);
  
  	fs_close();
  
  	return ret;
  }
d455d8789   Suriyan Ramasami   fs: API changes e...
261
  int fs_size(const char *filename, loff_t *size)
cf6598193   Stephen Warren   fs: implement siz...
262
263
264
265
  {
  	int ret;
  
  	struct fstype_info *info = fs_get_info(fs_type);
d455d8789   Suriyan Ramasami   fs: API changes e...
266
  	ret = info->size(filename, size);
cf6598193   Stephen Warren   fs: implement siz...
267
268
269
270
271
  
  	fs_close();
  
  	return ret;
  }
d455d8789   Suriyan Ramasami   fs: API changes e...
272
273
  int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
  	    loff_t *actread)
045fa1e11   Stephen Warren   fs: add filesyste...
274
  {
c6f548d23   Simon Glass   fs: Use filesyste...
275
  	struct fstype_info *info = fs_get_info(fs_type);
117e05072   Simon Glass   fs: Use map_sysme...
276
  	void *buf;
045fa1e11   Stephen Warren   fs: add filesyste...
277
  	int ret;
117e05072   Simon Glass   fs: Use map_sysme...
278
279
280
281
282
  	/*
  	 * We don't actually know how many bytes are being read, since len==0
  	 * means read the whole file.
  	 */
  	buf = map_sysmem(addr, len);
d455d8789   Suriyan Ramasami   fs: API changes e...
283
  	ret = info->read(filename, buf, offset, len, actread);
117e05072   Simon Glass   fs: Use map_sysme...
284
  	unmap_sysmem(buf);
045fa1e11   Stephen Warren   fs: add filesyste...
285

c6f548d23   Simon Glass   fs: Use filesyste...
286
  	/* If we requested a specific number of bytes, check we got it */
7a3e70cfd   Max Krummenacher   fs/fs.c: read up ...
287
288
289
  	if (ret == 0 && len && *actread != len)
  		printf("** %s shorter than offset + len **
  ", filename);
045fa1e11   Stephen Warren   fs: add filesyste...
290
291
292
293
  	fs_close();
  
  	return ret;
  }
d455d8789   Suriyan Ramasami   fs: API changes e...
294
295
  int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
  	     loff_t *actwrite)
a8f6ab522   Simon Glass   fs: Add support f...
296
297
298
299
  {
  	struct fstype_info *info = fs_get_info(fs_type);
  	void *buf;
  	int ret;
a8f6ab522   Simon Glass   fs: Add support f...
300
  	buf = map_sysmem(addr, len);
d455d8789   Suriyan Ramasami   fs: API changes e...
301
  	ret = info->write(filename, buf, offset, len, actwrite);
a8f6ab522   Simon Glass   fs: Add support f...
302
  	unmap_sysmem(buf);
d455d8789   Suriyan Ramasami   fs: API changes e...
303
  	if (ret < 0 && len != *actwrite) {
a8f6ab522   Simon Glass   fs: Add support f...
304
305
306
307
308
309
310
311
  		printf("** Unable to write file %s **
  ", filename);
  		ret = -1;
  	}
  	fs_close();
  
  	return ret;
  }
cf6598193   Stephen Warren   fs: implement siz...
312
313
314
  int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
  		int fstype)
  {
d455d8789   Suriyan Ramasami   fs: API changes e...
315
  	loff_t size;
cf6598193   Stephen Warren   fs: implement siz...
316
317
318
319
320
321
  
  	if (argc != 4)
  		return CMD_RET_USAGE;
  
  	if (fs_set_blk_dev(argv[1], argv[2], fstype))
  		return 1;
d455d8789   Suriyan Ramasami   fs: API changes e...
322
  	if (fs_size(argv[3], &size) < 0)
cf6598193   Stephen Warren   fs: implement siz...
323
324
325
326
327
328
  		return CMD_RET_FAILURE;
  
  	setenv_hex("filesize", size);
  
  	return 0;
  }
f9b55e228   Stephen Warren   fs: rename fsload...
329
  int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
b770e88a6   Wolfgang Denk   Fix number base h...
330
  		int fstype)
045fa1e11   Stephen Warren   fs: add filesyste...
331
332
333
334
  {
  	unsigned long addr;
  	const char *addr_str;
  	const char *filename;
d455d8789   Suriyan Ramasami   fs: API changes e...
335
336
337
338
  	loff_t bytes;
  	loff_t pos;
  	loff_t len_read;
  	int ret;
da1fd96ce   Andreas Bießmann   fs/fs.c: do_fsloa...
339
  	unsigned long time;
949bbd7c8   Pavel Machek   catch wrong load ...
340
  	char *ep;
045fa1e11   Stephen Warren   fs: add filesyste...
341

e9b0f99e8   Stephen Warren   fs: fix do_fsload...
342
343
344
  	if (argc < 2)
  		return CMD_RET_USAGE;
  	if (argc > 7)
045fa1e11   Stephen Warren   fs: add filesyste...
345
  		return CMD_RET_USAGE;
e9b0f99e8   Stephen Warren   fs: fix do_fsload...
346
  	if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
045fa1e11   Stephen Warren   fs: add filesyste...
347
348
349
  		return 1;
  
  	if (argc >= 4) {
949bbd7c8   Pavel Machek   catch wrong load ...
350
351
352
  		addr = simple_strtoul(argv[3], &ep, 16);
  		if (ep == argv[3] || *ep != '\0')
  			return CMD_RET_USAGE;
045fa1e11   Stephen Warren   fs: add filesyste...
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
  	} else {
  		addr_str = getenv("loadaddr");
  		if (addr_str != NULL)
  			addr = simple_strtoul(addr_str, NULL, 16);
  		else
  			addr = CONFIG_SYS_LOAD_ADDR;
  	}
  	if (argc >= 5) {
  		filename = argv[4];
  	} else {
  		filename = getenv("bootfile");
  		if (!filename) {
  			puts("** No boot file defined **
  ");
  			return 1;
  		}
  	}
  	if (argc >= 6)
b770e88a6   Wolfgang Denk   Fix number base h...
371
  		bytes = simple_strtoul(argv[5], NULL, 16);
045fa1e11   Stephen Warren   fs: add filesyste...
372
373
374
  	else
  		bytes = 0;
  	if (argc >= 7)
b770e88a6   Wolfgang Denk   Fix number base h...
375
  		pos = simple_strtoul(argv[6], NULL, 16);
045fa1e11   Stephen Warren   fs: add filesyste...
376
377
  	else
  		pos = 0;
da1fd96ce   Andreas Bießmann   fs/fs.c: do_fsloa...
378
  	time = get_timer(0);
d455d8789   Suriyan Ramasami   fs: API changes e...
379
  	ret = fs_read(filename, addr, pos, bytes, &len_read);
da1fd96ce   Andreas Bießmann   fs/fs.c: do_fsloa...
380
  	time = get_timer(time);
d455d8789   Suriyan Ramasami   fs: API changes e...
381
  	if (ret < 0)
045fa1e11   Stephen Warren   fs: add filesyste...
382
  		return 1;
d455d8789   Suriyan Ramasami   fs: API changes e...
383
  	printf("%llu bytes read in %lu ms", len_read, time);
da1fd96ce   Andreas Bießmann   fs/fs.c: do_fsloa...
384
385
  	if (time > 0) {
  		puts(" (");
9e374e7b7   Tom Rini   fs/ext4/ext4fs.c,...
386
  		print_size(div_u64(len_read, time) * 1000, "/s");
da1fd96ce   Andreas Bießmann   fs/fs.c: do_fsloa...
387
388
389
390
  		puts(")");
  	}
  	puts("
  ");
045fa1e11   Stephen Warren   fs: add filesyste...
391

e9cdf3b85   David Müller (ELSOFT AG)   fs: handle the fi...
392
  	setenv_hex("fileaddr", addr);
49c4f0370   Simon Glass   fs: Use new numer...
393
  	setenv_hex("filesize", len_read);
045fa1e11   Stephen Warren   fs: add filesyste...
394
395
396
397
398
399
400
401
402
  
  	return 0;
  }
  
  int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
  	int fstype)
  {
  	if (argc < 2)
  		return CMD_RET_USAGE;
e9b0f99e8   Stephen Warren   fs: fix do_fsload...
403
404
  	if (argc > 4)
  		return CMD_RET_USAGE;
045fa1e11   Stephen Warren   fs: add filesyste...
405
406
407
  
  	if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
  		return 1;
e9b0f99e8   Stephen Warren   fs: fix do_fsload...
408
  	if (fs_ls(argc >= 4 ? argv[3] : "/"))
045fa1e11   Stephen Warren   fs: add filesyste...
409
410
411
412
  		return 1;
  
  	return 0;
  }
a8f6ab522   Simon Glass   fs: Add support f...
413

6152916a9   Stephen Warren   fs: implement inf...
414
415
416
417
418
419
420
421
  int file_exists(const char *dev_type, const char *dev_part, const char *file,
  		int fstype)
  {
  	if (fs_set_blk_dev(dev_type, dev_part, fstype))
  		return 0;
  
  	return fs_exists(file);
  }
a8f6ab522   Simon Glass   fs: Add support f...
422
  int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
b770e88a6   Wolfgang Denk   Fix number base h...
423
  		int fstype)
a8f6ab522   Simon Glass   fs: Add support f...
424
425
426
  {
  	unsigned long addr;
  	const char *filename;
d455d8789   Suriyan Ramasami   fs: API changes e...
427
428
429
430
  	loff_t bytes;
  	loff_t pos;
  	loff_t len;
  	int ret;
a8f6ab522   Simon Glass   fs: Add support f...
431
432
433
434
435
436
437
  	unsigned long time;
  
  	if (argc < 6 || argc > 7)
  		return CMD_RET_USAGE;
  
  	if (fs_set_blk_dev(argv[1], argv[2], fstype))
  		return 1;
d455d8789   Suriyan Ramasami   fs: API changes e...
438
439
  	addr = simple_strtoul(argv[3], NULL, 16);
  	filename = argv[4];
b770e88a6   Wolfgang Denk   Fix number base h...
440
  	bytes = simple_strtoul(argv[5], NULL, 16);
a8f6ab522   Simon Glass   fs: Add support f...
441
  	if (argc >= 7)
b770e88a6   Wolfgang Denk   Fix number base h...
442
  		pos = simple_strtoul(argv[6], NULL, 16);
a8f6ab522   Simon Glass   fs: Add support f...
443
444
445
446
  	else
  		pos = 0;
  
  	time = get_timer(0);
d455d8789   Suriyan Ramasami   fs: API changes e...
447
  	ret = fs_write(filename, addr, pos, bytes, &len);
a8f6ab522   Simon Glass   fs: Add support f...
448
  	time = get_timer(time);
d455d8789   Suriyan Ramasami   fs: API changes e...
449
  	if (ret < 0)
a8f6ab522   Simon Glass   fs: Add support f...
450
  		return 1;
d455d8789   Suriyan Ramasami   fs: API changes e...
451
  	printf("%llu bytes written in %lu ms", len, time);
a8f6ab522   Simon Glass   fs: Add support f...
452
453
  	if (time > 0) {
  		puts(" (");
9e374e7b7   Tom Rini   fs/ext4/ext4fs.c,...
454
  		print_size(div_u64(len, time) * 1000, "/s");
a8f6ab522   Simon Glass   fs: Add support f...
455
456
457
458
459
460
461
  		puts(")");
  	}
  	puts("
  ");
  
  	return 0;
  }
59e890ef7   Christian Gmeiner   fs: make it possi...
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
  
  int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
  		int fstype)
  {
  	int ret;
  	char uuid[37];
  	memset(uuid, 0, sizeof(uuid));
  
  	if (argc < 3 || argc > 4)
  		return CMD_RET_USAGE;
  
  	if (fs_set_blk_dev(argv[1], argv[2], fstype))
  		return 1;
  
  	ret = fs_uuid(uuid);
  	if (ret)
  		return CMD_RET_FAILURE;
  
  	if (argc == 4)
  		setenv(argv[3], uuid);
  	else
  		printf("%s
  ", uuid);
  
  	return CMD_RET_SUCCESS;
  }
1a1ad8e09   Sjoerd Simons   fs: Add command t...
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
  
  int do_fs_type(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  {
  	struct fstype_info *info;
  
  	if (argc < 3 || argc > 4)
  		return CMD_RET_USAGE;
  
  	if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
  		return 1;
  
  	info = fs_get_info(fs_type);
  
  	if (argc == 4)
  		setenv(argv[3], info->name);
  	else
  		printf("%s
  ", info->name);
  
  	return CMD_RET_SUCCESS;
  }