1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2008
4  * Stefan Roese, DENX Software Engineering, sr@denx.de.
5  */
6 
7 
8 /*
9  * UBIFS command support
10  */
11 
12 #undef DEBUG
13 
14 #include <common.h>
15 #include <config.h>
16 #include <command.h>
17 #include <log.h>
18 #include <ubifs_uboot.h>
19 
20 static int ubifs_initialized;
21 static int ubifs_mounted;
22 
cmd_ubifs_mount(char * vol_name)23 int cmd_ubifs_mount(char *vol_name)
24 {
25 	int ret;
26 
27 	debug("Using volume %s\n", vol_name);
28 
29 	if (ubifs_initialized == 0) {
30 		ubifs_init();
31 		ubifs_initialized = 1;
32 	}
33 
34 	ret = uboot_ubifs_mount(vol_name);
35 	if (ret)
36 		return -1;
37 
38 	ubifs_mounted = 1;
39 
40 	return ret;
41 }
42 
do_ubifs_mount(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])43 static int do_ubifs_mount(struct cmd_tbl *cmdtp, int flag, int argc,
44 			  char *const argv[])
45 {
46 	char *vol_name;
47 
48 	if (argc != 2)
49 		return CMD_RET_USAGE;
50 
51 	vol_name = argv[1];
52 
53 	return cmd_ubifs_mount(vol_name);
54 }
55 
ubifs_is_mounted(void)56 int ubifs_is_mounted(void)
57 {
58 	return ubifs_mounted;
59 }
60 
cmd_ubifs_umount(void)61 int cmd_ubifs_umount(void)
62 {
63 	if (ubifs_initialized == 0) {
64 		printf("No UBIFS volume mounted!\n");
65 		return -1;
66 	}
67 
68 	uboot_ubifs_umount();
69 	ubifs_mounted = 0;
70 	ubifs_initialized = 0;
71 
72 	return 0;
73 }
74 
do_ubifs_umount(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])75 static int do_ubifs_umount(struct cmd_tbl *cmdtp, int flag, int argc,
76 			   char *const argv[])
77 {
78 	if (argc != 1)
79 		return CMD_RET_USAGE;
80 
81 	return cmd_ubifs_umount();
82 }
83 
do_ubifs_ls(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])84 static int do_ubifs_ls(struct cmd_tbl *cmdtp, int flag, int argc,
85 		       char *const argv[])
86 {
87 	char *filename = "/";
88 	int ret;
89 
90 	if (!ubifs_mounted) {
91 		printf("UBIFS not mounted, use ubifsmount to mount volume first!\n");
92 		return -1;
93 	}
94 
95 	if (argc == 2)
96 		filename = argv[1];
97 	debug("Using filename %s\n", filename);
98 
99 	ret = ubifs_ls(filename);
100 	if (ret) {
101 		printf("** File not found %s **\n", filename);
102 		ret = CMD_RET_FAILURE;
103 	}
104 
105 	return ret;
106 }
107 
do_ubifs_load(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])108 static int do_ubifs_load(struct cmd_tbl *cmdtp, int flag, int argc,
109 			 char *const argv[])
110 {
111 	char *filename;
112 	char *endp;
113 	int ret;
114 	u32 addr;
115 	u32 size = 0;
116 
117 	if (!ubifs_mounted) {
118 		printf("UBIFS not mounted, use ubifs mount to mount volume first!\n");
119 		return -1;
120 	}
121 
122 	if (argc < 3)
123 		return CMD_RET_USAGE;
124 
125 	addr = simple_strtoul(argv[1], &endp, 16);
126 	if (endp == argv[1])
127 		return CMD_RET_USAGE;
128 
129 	filename = argv[2];
130 
131 	if (argc == 4) {
132 		size = simple_strtoul(argv[3], &endp, 16);
133 		if (endp == argv[3])
134 			return CMD_RET_USAGE;
135 	}
136 	debug("Loading file '%s' to address 0x%08x (size %d)\n", filename, addr, size);
137 
138 	ret = ubifs_load(filename, addr, size);
139 	if (ret) {
140 		printf("** File not found %s **\n", filename);
141 		ret = CMD_RET_FAILURE;
142 	}
143 
144 	return ret;
145 }
146 
147 U_BOOT_CMD(
148 	ubifsmount, 2, 0, do_ubifs_mount,
149 	"mount UBIFS volume",
150 	"<volume-name>\n"
151 	"    - mount 'volume-name' volume"
152 );
153 
154 U_BOOT_CMD(
155 	ubifsumount, 1, 0, do_ubifs_umount,
156 	"unmount UBIFS volume",
157 	"    - unmount current volume"
158 );
159 
160 U_BOOT_CMD(
161 	ubifsls, 2, 0, do_ubifs_ls,
162 	"list files in a directory",
163 	"[directory]\n"
164 	"    - list files in a 'directory' (default '/')"
165 );
166 
167 U_BOOT_CMD(
168 	ubifsload, 4, 0, do_ubifs_load,
169 	"load file from an UBIFS filesystem",
170 	"<addr> <filename> [bytes]\n"
171 	"    - load file 'filename' to address 'addr'"
172 );
173