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