blob: 8e9a4e5038d87c19cfda9850dab0dbfa3d7446b0 [file] [log] [blame]
Stefan Roesece6d0c82009-03-19 15:35:50 +01001/*
2 * (C) Copyright 2008
3 * Stefan Roese, DENX Software Engineering, sr@denx.de.
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Stefan Roesece6d0c82009-03-19 15:35:50 +01006 */
7
8
9/*
10 * UBIFS command support
11 */
12
13#undef DEBUG
14
15#include <common.h>
16#include <config.h>
17#include <command.h>
18
Stefan Roesecb9c09d2010-10-28 14:09:22 +020019#include "../fs/ubifs/ubifs.h"
20
Stefan Roesece6d0c82009-03-19 15:35:50 +010021static int ubifs_initialized;
22static int ubifs_mounted;
23
Jeroen Hofstee0e350f82014-06-23 00:22:08 +020024static int do_ubifs_mount(cmd_tbl_t *cmdtp, int flag, int argc,
25 char * const argv[])
Stefan Roesece6d0c82009-03-19 15:35:50 +010026{
27 char *vol_name;
28 int ret;
29
Wolfgang Denk47e26b12010-07-17 01:06:04 +020030 if (argc != 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +000031 return CMD_RET_USAGE;
Wolfgang Denk47e26b12010-07-17 01:06:04 +020032
Stefan Roesece6d0c82009-03-19 15:35:50 +010033 vol_name = argv[1];
34 debug("Using volume %s\n", vol_name);
35
36 if (ubifs_initialized == 0) {
37 ubifs_init();
38 ubifs_initialized = 1;
39 }
40
Heiko Schocherff94bc42014-06-24 10:10:04 +020041 ret = uboot_ubifs_mount(vol_name);
Stefan Roesece6d0c82009-03-19 15:35:50 +010042 if (ret)
43 return -1;
44
45 ubifs_mounted = 1;
46
47 return 0;
48}
49
Stefan Roese2f15cfd2010-11-01 17:28:22 +010050int ubifs_is_mounted(void)
51{
52 return ubifs_mounted;
53}
54
55void cmd_ubifs_umount(void)
56{
57
58 if (ubifs_sb) {
59 printf("Unmounting UBIFS volume %s!\n",
60 ((struct ubifs_info *)(ubifs_sb->s_fs_info))->vi.name);
61 ubifs_umount(ubifs_sb->s_fs_info);
62 }
63
64 ubifs_sb = NULL;
65 ubifs_mounted = 0;
66 ubifs_initialized = 0;
67}
68
Jeroen Hofstee0e350f82014-06-23 00:22:08 +020069static int do_ubifs_umount(cmd_tbl_t *cmdtp, int flag, int argc,
70 char * const argv[])
Stefan Roesecb9c09d2010-10-28 14:09:22 +020071{
72 if (argc != 1)
Simon Glass4c12eeb2011-12-10 08:44:01 +000073 return CMD_RET_USAGE;
Stefan Roesecb9c09d2010-10-28 14:09:22 +020074
75 if (ubifs_initialized == 0) {
76 printf("No UBIFS volume mounted!\n");
77 return -1;
78 }
79
Stefan Roese2f15cfd2010-11-01 17:28:22 +010080 cmd_ubifs_umount();
Stefan Roesecb9c09d2010-10-28 14:09:22 +020081
82 return 0;
83}
84
Jeroen Hofstee0e350f82014-06-23 00:22:08 +020085static int do_ubifs_ls(cmd_tbl_t *cmdtp, int flag, int argc,
86 char * const argv[])
Stefan Roesece6d0c82009-03-19 15:35:50 +010087{
88 char *filename = "/";
89 int ret;
90
91 if (!ubifs_mounted) {
Stefan Roese9a2ea572010-10-28 14:09:29 +020092 printf("UBIFS not mounted, use ubifsmount to mount volume first!\n");
Stefan Roesece6d0c82009-03-19 15:35:50 +010093 return -1;
94 }
95
96 if (argc == 2)
97 filename = argv[1];
98 debug("Using filename %s\n", filename);
99
100 ret = ubifs_ls(filename);
Tim Harvey7cdebc32013-10-10 01:32:26 +0200101 if (ret) {
102 printf("** File not found %s **\n", filename);
103 ret = CMD_RET_FAILURE;
104 }
Stefan Roesece6d0c82009-03-19 15:35:50 +0100105
106 return ret;
107}
108
Jeroen Hofstee0e350f82014-06-23 00:22:08 +0200109static int do_ubifs_load(cmd_tbl_t *cmdtp, int flag, int argc,
110 char * const argv[])
Stefan Roesece6d0c82009-03-19 15:35:50 +0100111{
112 char *filename;
Simon Kagstrom2896b582009-07-07 16:01:02 +0200113 char *endp;
Stefan Roesece6d0c82009-03-19 15:35:50 +0100114 int ret;
115 u32 addr;
116 u32 size = 0;
117
118 if (!ubifs_mounted) {
119 printf("UBIFS not mounted, use ubifs mount to mount volume first!\n");
120 return -1;
121 }
122
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200123 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000124 return CMD_RET_USAGE;
Stefan Roesece6d0c82009-03-19 15:35:50 +0100125
Simon Kagstrom2896b582009-07-07 16:01:02 +0200126 addr = simple_strtoul(argv[1], &endp, 16);
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200127 if (endp == argv[1])
Simon Glass4c12eeb2011-12-10 08:44:01 +0000128 return CMD_RET_USAGE;
Simon Kagstrom2896b582009-07-07 16:01:02 +0200129
Stefan Roesece6d0c82009-03-19 15:35:50 +0100130 filename = argv[2];
131
Simon Kagstrom2896b582009-07-07 16:01:02 +0200132 if (argc == 4) {
133 size = simple_strtoul(argv[3], &endp, 16);
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200134 if (endp == argv[3])
Simon Glass4c12eeb2011-12-10 08:44:01 +0000135 return CMD_RET_USAGE;
Simon Kagstrom2896b582009-07-07 16:01:02 +0200136 }
Stefan Roesece6d0c82009-03-19 15:35:50 +0100137 debug("Loading file '%s' to address 0x%08x (size %d)\n", filename, addr, size);
138
139 ret = ubifs_load(filename, addr, size);
Tim Harvey7cdebc32013-10-10 01:32:26 +0200140 if (ret) {
141 printf("** File not found %s **\n", filename);
142 ret = CMD_RET_FAILURE;
143 }
Stefan Roesece6d0c82009-03-19 15:35:50 +0100144
145 return ret;
146}
147
148U_BOOT_CMD(
149 ubifsmount, 2, 0, do_ubifs_mount,
Mike Frysinger852dbfd2009-03-23 22:27:34 -0400150 "mount UBIFS volume",
Simon Kagstrom2896b582009-07-07 16:01:02 +0200151 "<volume-name>\n"
152 " - mount 'volume-name' volume"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200153);
Stefan Roesece6d0c82009-03-19 15:35:50 +0100154
Frans Meulenbroeks388a29d2010-07-31 15:01:53 +0200155U_BOOT_CMD(
Stefan Roesecb9c09d2010-10-28 14:09:22 +0200156 ubifsumount, 1, 0, do_ubifs_umount,
157 "unmount UBIFS volume",
158 " - unmount current volume"
159);
160
161U_BOOT_CMD(
Frans Meulenbroeks388a29d2010-07-31 15:01:53 +0200162 ubifsls, 2, 0, do_ubifs_ls,
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200163 "list files in a directory",
164 "[directory]\n"
165 " - list files in a 'directory' (default '/')"
166);
Stefan Roesece6d0c82009-03-19 15:35:50 +0100167
Frans Meulenbroeks388a29d2010-07-31 15:01:53 +0200168U_BOOT_CMD(
169 ubifsload, 4, 0, do_ubifs_load,
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200170 "load file from an UBIFS filesystem",
171 "<addr> <filename> [bytes]\n"
172 " - load file 'filename' to address 'addr'"
173);