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