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