]> Git Repo - u-boot.git/blame - fs/fs.c
fs: make it possible to read the filesystem UUID
[u-boot.git] / fs / fs.c
CommitLineData
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>
59e890ef 18#include <errno.h>
045fa1e1
SW
19#include <common.h>
20#include <part.h>
21#include <ext4fs.h>
22#include <fat.h>
23#include <fs.h>
92ccc96b 24#include <sandboxfs.h>
117e0507 25#include <asm/io.h>
045fa1e1 26
a1b231ce
SW
27DECLARE_GLOBAL_DATA_PTR;
28
045fa1e1
SW
29static block_dev_desc_t *fs_dev_desc;
30static disk_partition_t fs_partition;
31static int fs_type = FS_TYPE_ANY;
32
2ded0d47
SG
33static inline int fs_probe_unsupported(block_dev_desc_t *fs_dev_desc,
34 disk_partition_t *fs_partition)
436e2b73
SG
35{
36 printf("** Unrecognized filesystem type **\n");
37 return -1;
38}
39
045fa1e1
SW
40static inline int fs_ls_unsupported(const char *dirname)
41{
045fa1e1
SW
42 return -1;
43}
44
6152916a
SW
45static inline int fs_exists_unsupported(const char *filename)
46{
47 return 0;
48}
49
cf659819
SW
50static inline int fs_size_unsupported(const char *filename)
51{
52 return -1;
53}
54
117e0507 55static inline int fs_read_unsupported(const char *filename, void *buf,
045fa1e1
SW
56 int offset, int len)
57{
045fa1e1
SW
58 return -1;
59}
60
a8f6ab52
SG
61static inline int fs_write_unsupported(const char *filename, void *buf,
62 int offset, int len)
63{
64 return -1;
65}
66
436e2b73
SG
67static inline void fs_close_unsupported(void)
68{
69}
70
59e890ef
CG
71static inline int fs_uuid_unsupported(char *uuid_str)
72{
73 return -1;
74}
75
436e2b73 76struct fstype_info {
045fa1e1 77 int fstype;
377202b5
SW
78 /*
79 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
80 * should be false in most cases. For "virtual" filesystems which
81 * aren't based on a U-Boot block device (e.g. sandbox), this can be
82 * set to true. This should also be true for the dumm entry at the end
83 * of fstypes[], since that is essentially a "virtual" (non-existent)
84 * filesystem.
85 */
86 bool null_dev_desc_ok;
2ded0d47
SG
87 int (*probe)(block_dev_desc_t *fs_dev_desc,
88 disk_partition_t *fs_partition);
436e2b73 89 int (*ls)(const char *dirname);
6152916a 90 int (*exists)(const char *filename);
cf659819 91 int (*size)(const char *filename);
117e0507 92 int (*read)(const char *filename, void *buf, int offset, int len);
a8f6ab52 93 int (*write)(const char *filename, void *buf, int offset, int len);
436e2b73 94 void (*close)(void);
59e890ef 95 int (*uuid)(char *uuid_str);
436e2b73
SG
96};
97
98static struct fstype_info fstypes[] = {
99#ifdef CONFIG_FS_FAT
045fa1e1
SW
100 {
101 .fstype = FS_TYPE_FAT,
377202b5 102 .null_dev_desc_ok = false,
e6d52415
SG
103 .probe = fat_set_blk_dev,
104 .close = fat_close,
436e2b73 105 .ls = file_fat_ls,
b7b5f319 106 .exists = fat_exists,
cf659819 107 .size = fat_size,
e6d52415 108 .read = fat_read_file,
bd6fb31f 109 .write = fs_write_unsupported,
59e890ef 110 .uuid = fs_uuid_unsupported,
045fa1e1 111 },
436e2b73
SG
112#endif
113#ifdef CONFIG_FS_EXT4
045fa1e1
SW
114 {
115 .fstype = FS_TYPE_EXT,
377202b5 116 .null_dev_desc_ok = false,
e6d52415
SG
117 .probe = ext4fs_probe,
118 .close = ext4fs_close,
436e2b73 119 .ls = ext4fs_ls,
55af5c93 120 .exists = ext4fs_exists,
cf659819 121 .size = ext4fs_size,
e6d52415 122 .read = ext4_read_file,
bd6fb31f 123 .write = fs_write_unsupported,
59e890ef 124 .uuid = ext4fs_uuid,
436e2b73 125 },
92ccc96b
SG
126#endif
127#ifdef CONFIG_SANDBOX
128 {
129 .fstype = FS_TYPE_SANDBOX,
377202b5 130 .null_dev_desc_ok = true,
92ccc96b
SG
131 .probe = sandbox_fs_set_blk_dev,
132 .close = sandbox_fs_close,
133 .ls = sandbox_fs_ls,
0a30aa1e 134 .exists = sandbox_fs_exists,
cf659819 135 .size = sandbox_fs_size,
92ccc96b 136 .read = fs_read_sandbox,
7eb2c8d5 137 .write = fs_write_sandbox,
59e890ef 138 .uuid = fs_uuid_unsupported,
92ccc96b 139 },
436e2b73
SG
140#endif
141 {
142 .fstype = FS_TYPE_ANY,
377202b5 143 .null_dev_desc_ok = true,
436e2b73
SG
144 .probe = fs_probe_unsupported,
145 .close = fs_close_unsupported,
146 .ls = fs_ls_unsupported,
6152916a 147 .exists = fs_exists_unsupported,
cf659819 148 .size = fs_size_unsupported,
436e2b73 149 .read = fs_read_unsupported,
a8f6ab52 150 .write = fs_write_unsupported,
59e890ef 151 .uuid = fs_uuid_unsupported,
045fa1e1
SW
152 },
153};
154
c6f548d2
SG
155static struct fstype_info *fs_get_info(int fstype)
156{
157 struct fstype_info *info;
158 int i;
159
160 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
161 if (fstype == info->fstype)
162 return info;
163 }
164
165 /* Return the 'unsupported' sentinel */
166 return info;
167}
168
045fa1e1
SW
169int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
170{
436e2b73 171 struct fstype_info *info;
045fa1e1 172 int part, i;
a1b231ce
SW
173#ifdef CONFIG_NEEDS_MANUAL_RELOC
174 static int relocated;
175
176 if (!relocated) {
436e2b73
SG
177 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
178 i++, info++) {
179 info->probe += gd->reloc_off;
180 info->close += gd->reloc_off;
181 info->ls += gd->reloc_off;
182 info->read += gd->reloc_off;
a8f6ab52 183 info->write += gd->reloc_off;
436e2b73 184 }
a1b231ce
SW
185 relocated = 1;
186 }
187#endif
045fa1e1
SW
188
189 part = get_device_and_partition(ifname, dev_part_str, &fs_dev_desc,
190 &fs_partition, 1);
191 if (part < 0)
192 return -1;
193
436e2b73
SG
194 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
195 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
196 fstype != info->fstype)
045fa1e1
SW
197 continue;
198
377202b5
SW
199 if (!fs_dev_desc && !info->null_dev_desc_ok)
200 continue;
201
2ded0d47 202 if (!info->probe(fs_dev_desc, &fs_partition)) {
436e2b73 203 fs_type = info->fstype;
045fa1e1
SW
204 return 0;
205 }
206 }
207
045fa1e1
SW
208 return -1;
209}
210
211static void fs_close(void)
212{
c6f548d2 213 struct fstype_info *info = fs_get_info(fs_type);
045fa1e1 214
c6f548d2 215 info->close();
e6d52415 216
045fa1e1
SW
217 fs_type = FS_TYPE_ANY;
218}
219
59e890ef
CG
220int fs_uuid(char *uuid_str)
221{
222 struct fstype_info *info = fs_get_info(fs_type);
223
224 return info->uuid(uuid_str);
225}
226
045fa1e1
SW
227int fs_ls(const char *dirname)
228{
229 int ret;
230
c6f548d2
SG
231 struct fstype_info *info = fs_get_info(fs_type);
232
233 ret = info->ls(dirname);
045fa1e1 234
e6d52415 235 fs_type = FS_TYPE_ANY;
045fa1e1
SW
236 fs_close();
237
238 return ret;
239}
240
6152916a
SW
241int fs_exists(const char *filename)
242{
243 int ret;
244
245 struct fstype_info *info = fs_get_info(fs_type);
246
247 ret = info->exists(filename);
248
249 fs_close();
250
251 return ret;
252}
253
cf659819
SW
254int fs_size(const char *filename)
255{
256 int ret;
257
258 struct fstype_info *info = fs_get_info(fs_type);
259
260 ret = info->size(filename);
261
262 fs_close();
263
264 return ret;
265}
266
045fa1e1
SW
267int fs_read(const char *filename, ulong addr, int offset, int len)
268{
c6f548d2 269 struct fstype_info *info = fs_get_info(fs_type);
117e0507 270 void *buf;
045fa1e1
SW
271 int ret;
272
117e0507
SG
273 /*
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);
045fa1e1 280
c6f548d2
SG
281 /* 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 }
045fa1e1
SW
286 fs_close();
287
288 return ret;
289}
290
a8f6ab52
SG
291int fs_write(const char *filename, ulong addr, int offset, int len)
292{
293 struct fstype_info *info = fs_get_info(fs_type);
294 void *buf;
295 int ret;
296
a8f6ab52
SG
297 buf = map_sysmem(addr, len);
298 ret = info->write(filename, buf, offset, len);
299 unmap_sysmem(buf);
300
bd6fb31f 301 if (ret >= 0 && ret != len) {
a8f6ab52
SG
302 printf("** Unable to write file %s **\n", filename);
303 ret = -1;
304 }
305 fs_close();
306
307 return ret;
308}
309
cf659819
SW
310int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
311 int fstype)
312{
313 int size;
314
315 if (argc != 4)
316 return CMD_RET_USAGE;
317
318 if (fs_set_blk_dev(argv[1], argv[2], fstype))
319 return 1;
320
321 size = fs_size(argv[3]);
322 if (size < 0)
323 return CMD_RET_FAILURE;
324
325 setenv_hex("filesize", size);
326
327 return 0;
328}
329
f9b55e22 330int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
b770e88a 331 int fstype)
045fa1e1
SW
332{
333 unsigned long addr;
334 const char *addr_str;
335 const char *filename;
336 unsigned long bytes;
337 unsigned long pos;
338 int len_read;
da1fd96c 339 unsigned long time;
949bbd7c 340 char *ep;
045fa1e1 341
e9b0f99e
SW
342 if (argc < 2)
343 return CMD_RET_USAGE;
344 if (argc > 7)
045fa1e1
SW
345 return CMD_RET_USAGE;
346
e9b0f99e 347 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
045fa1e1
SW
348 return 1;
349
350 if (argc >= 4) {
949bbd7c
PM
351 addr = simple_strtoul(argv[3], &ep, 16);
352 if (ep == argv[3] || *ep != '\0')
353 return CMD_RET_USAGE;
045fa1e1
SW
354 } else {
355 addr_str = getenv("loadaddr");
356 if (addr_str != NULL)
357 addr = simple_strtoul(addr_str, NULL, 16);
358 else
359 addr = CONFIG_SYS_LOAD_ADDR;
360 }
361 if (argc >= 5) {
362 filename = argv[4];
363 } else {
364 filename = getenv("bootfile");
365 if (!filename) {
366 puts("** No boot file defined **\n");
367 return 1;
368 }
369 }
370 if (argc >= 6)
b770e88a 371 bytes = simple_strtoul(argv[5], NULL, 16);
045fa1e1
SW
372 else
373 bytes = 0;
374 if (argc >= 7)
b770e88a 375 pos = simple_strtoul(argv[6], NULL, 16);
045fa1e1
SW
376 else
377 pos = 0;
378
da1fd96c 379 time = get_timer(0);
045fa1e1 380 len_read = fs_read(filename, addr, pos, bytes);
da1fd96c 381 time = get_timer(time);
045fa1e1
SW
382 if (len_read <= 0)
383 return 1;
384
da1fd96c
AB
385 printf("%d bytes read in %lu ms", len_read, time);
386 if (time > 0) {
387 puts(" (");
388 print_size(len_read / time * 1000, "/s");
389 puts(")");
390 }
391 puts("\n");
045fa1e1 392
49c4f037 393 setenv_hex("filesize", len_read);
045fa1e1
SW
394
395 return 0;
396}
397
398int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
399 int fstype)
400{
401 if (argc < 2)
402 return CMD_RET_USAGE;
e9b0f99e
SW
403 if (argc > 4)
404 return CMD_RET_USAGE;
045fa1e1
SW
405
406 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
407 return 1;
408
e9b0f99e 409 if (fs_ls(argc >= 4 ? argv[3] : "/"))
045fa1e1
SW
410 return 1;
411
412 return 0;
413}
a8f6ab52 414
6152916a
SW
415int file_exists(const char *dev_type, const char *dev_part, const char *file,
416 int fstype)
417{
418 if (fs_set_blk_dev(dev_type, dev_part, fstype))
419 return 0;
420
421 return fs_exists(file);
422}
423
a8f6ab52 424int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
b770e88a 425 int fstype)
a8f6ab52
SG
426{
427 unsigned long addr;
428 const char *filename;
429 unsigned long bytes;
430 unsigned long pos;
431 int len;
432 unsigned long time;
433
434 if (argc < 6 || argc > 7)
435 return CMD_RET_USAGE;
436
437 if (fs_set_blk_dev(argv[1], argv[2], fstype))
438 return 1;
439
440 filename = argv[3];
b770e88a
WD
441 addr = simple_strtoul(argv[4], NULL, 16);
442 bytes = simple_strtoul(argv[5], NULL, 16);
a8f6ab52 443 if (argc >= 7)
b770e88a 444 pos = simple_strtoul(argv[6], NULL, 16);
a8f6ab52
SG
445 else
446 pos = 0;
447
448 time = get_timer(0);
449 len = fs_write(filename, addr, pos, bytes);
450 time = get_timer(time);
451 if (len <= 0)
452 return 1;
453
454 printf("%d bytes written in %lu ms", len, time);
455 if (time > 0) {
456 puts(" (");
457 print_size(len / time * 1000, "/s");
458 puts(")");
459 }
460 puts("\n");
461
462 return 0;
463}
59e890ef
CG
464
465int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
466 int fstype)
467{
468 int ret;
469 char uuid[37];
470 memset(uuid, 0, sizeof(uuid));
471
472 if (argc < 3 || argc > 4)
473 return CMD_RET_USAGE;
474
475 if (fs_set_blk_dev(argv[1], argv[2], fstype))
476 return 1;
477
478 ret = fs_uuid(uuid);
479 if (ret)
480 return CMD_RET_FAILURE;
481
482 if (argc == 4)
483 setenv(argv[3], uuid);
484 else
485 printf("%s\n", uuid);
486
487 return CMD_RET_SUCCESS;
488}
This page took 0.168066 seconds and 4 git commands to generate.