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