blob: e798b833dd40f158c9275145b9106b53433526ef [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stefan Roesece6d0c82009-03-19 15:35:50 +01002/*
3 * (C) Copyright 2008
4 * Stefan Roese, DENX Software Engineering, sr@denx.de.
Stefan Roesece6d0c82009-03-19 15:35:50 +01005 */
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>
Hans de Goedead157492015-09-17 18:46:56 -040017#include <ubifs_uboot.h>
Stefan Roesecb9c09d2010-10-28 14:09:22 +020018
Stefan Roesece6d0c82009-03-19 15:35:50 +010019static int ubifs_initialized;
20static int ubifs_mounted;
21
Tien Fong Chee14dfc642018-07-06 16:26:01 +080022int cmd_ubifs_mount(char *vol_name)
Stefan Roesece6d0c82009-03-19 15:35:50 +010023{
Stefan Roesece6d0c82009-03-19 15:35:50 +010024 int ret;
25
Stefan Roesece6d0c82009-03-19 15:35:50 +010026 debug("Using volume %s\n", vol_name);
27
28 if (ubifs_initialized == 0) {
29 ubifs_init();
30 ubifs_initialized = 1;
31 }
32
Heiko Schocherff94bc42014-06-24 10:10:04 +020033 ret = uboot_ubifs_mount(vol_name);
Stefan Roesece6d0c82009-03-19 15:35:50 +010034 if (ret)
35 return -1;
36
37 ubifs_mounted = 1;
38
Tien Fong Chee14dfc642018-07-06 16:26:01 +080039 return ret;
40}
Simon Glass09140112020-05-10 11:40:03 -060041
42static int do_ubifs_mount(struct cmd_tbl *cmdtp, int flag, int argc,
43 char *const argv[])
Tien Fong Chee14dfc642018-07-06 16:26:01 +080044{
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);
Stefan Roesece6d0c82009-03-19 15:35:50 +010053}
54
Stefan Roese2f15cfd2010-11-01 17:28:22 +010055int ubifs_is_mounted(void)
56{
57 return ubifs_mounted;
58}
59
Tien Fong Chee10c20442018-07-06 16:25:12 +080060int cmd_ubifs_umount(void)
Stefan Roese2f15cfd2010-11-01 17:28:22 +010061{
Tien Fong Chee10c20442018-07-06 16:25:12 +080062 if (ubifs_initialized == 0) {
63 printf("No UBIFS volume mounted!\n");
64 return -1;
65 }
66
Hans de Goedead157492015-09-17 18:46:56 -040067 uboot_ubifs_umount();
Stefan Roese2f15cfd2010-11-01 17:28:22 +010068 ubifs_mounted = 0;
69 ubifs_initialized = 0;
Tien Fong Chee10c20442018-07-06 16:25:12 +080070
71 return 0;
Stefan Roese2f15cfd2010-11-01 17:28:22 +010072}
73
Simon Glass09140112020-05-10 11:40:03 -060074static int do_ubifs_umount(struct cmd_tbl *cmdtp, int flag, int argc,
75 char *const argv[])
Stefan Roesecb9c09d2010-10-28 14:09:22 +020076{
77 if (argc != 1)
Simon Glass4c12eeb2011-12-10 08:44:01 +000078 return CMD_RET_USAGE;
Stefan Roesecb9c09d2010-10-28 14:09:22 +020079
Tien Fong Chee10c20442018-07-06 16:25:12 +080080 return cmd_ubifs_umount();
Stefan Roesecb9c09d2010-10-28 14:09:22 +020081}
82
Simon Glass09140112020-05-10 11:40:03 -060083static int do_ubifs_ls(struct cmd_tbl *cmdtp, int flag, int argc,
84 char *const argv[])
Stefan Roesece6d0c82009-03-19 15:35:50 +010085{
86 char *filename = "/";
87 int ret;
88
89 if (!ubifs_mounted) {
Stefan Roese9a2ea572010-10-28 14:09:29 +020090 printf("UBIFS not mounted, use ubifsmount to mount volume first!\n");
Stefan Roesece6d0c82009-03-19 15:35:50 +010091 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);
Tim Harvey7cdebc32013-10-10 01:32:26 +020099 if (ret) {
100 printf("** File not found %s **\n", filename);
101 ret = CMD_RET_FAILURE;
102 }
Stefan Roesece6d0c82009-03-19 15:35:50 +0100103
104 return ret;
105}
106
Simon Glass09140112020-05-10 11:40:03 -0600107static int do_ubifs_load(struct cmd_tbl *cmdtp, int flag, int argc,
108 char *const argv[])
Stefan Roesece6d0c82009-03-19 15:35:50 +0100109{
110 char *filename;
Simon Kagstrom2896b582009-07-07 16:01:02 +0200111 char *endp;
Stefan Roesece6d0c82009-03-19 15:35:50 +0100112 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
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200121 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000122 return CMD_RET_USAGE;
Stefan Roesece6d0c82009-03-19 15:35:50 +0100123
Simon Kagstrom2896b582009-07-07 16:01:02 +0200124 addr = simple_strtoul(argv[1], &endp, 16);
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200125 if (endp == argv[1])
Simon Glass4c12eeb2011-12-10 08:44:01 +0000126 return CMD_RET_USAGE;
Simon Kagstrom2896b582009-07-07 16:01:02 +0200127
Stefan Roesece6d0c82009-03-19 15:35:50 +0100128 filename = argv[2];
129
Simon Kagstrom2896b582009-07-07 16:01:02 +0200130 if (argc == 4) {
131 size = simple_strtoul(argv[3], &endp, 16);
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200132 if (endp == argv[3])
Simon Glass4c12eeb2011-12-10 08:44:01 +0000133 return CMD_RET_USAGE;
Simon Kagstrom2896b582009-07-07 16:01:02 +0200134 }
Stefan Roesece6d0c82009-03-19 15:35:50 +0100135 debug("Loading file '%s' to address 0x%08x (size %d)\n", filename, addr, size);
136
137 ret = ubifs_load(filename, addr, size);
Tim Harvey7cdebc32013-10-10 01:32:26 +0200138 if (ret) {
139 printf("** File not found %s **\n", filename);
140 ret = CMD_RET_FAILURE;
141 }
Stefan Roesece6d0c82009-03-19 15:35:50 +0100142
143 return ret;
144}
145
146U_BOOT_CMD(
147 ubifsmount, 2, 0, do_ubifs_mount,
Mike Frysinger852dbfd2009-03-23 22:27:34 -0400148 "mount UBIFS volume",
Simon Kagstrom2896b582009-07-07 16:01:02 +0200149 "<volume-name>\n"
150 " - mount 'volume-name' volume"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200151);
Stefan Roesece6d0c82009-03-19 15:35:50 +0100152
Frans Meulenbroeks388a29d2010-07-31 15:01:53 +0200153U_BOOT_CMD(
Stefan Roesecb9c09d2010-10-28 14:09:22 +0200154 ubifsumount, 1, 0, do_ubifs_umount,
155 "unmount UBIFS volume",
156 " - unmount current volume"
157);
158
159U_BOOT_CMD(
Frans Meulenbroeks388a29d2010-07-31 15:01:53 +0200160 ubifsls, 2, 0, do_ubifs_ls,
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200161 "list files in a directory",
162 "[directory]\n"
163 " - list files in a 'directory' (default '/')"
164);
Stefan Roesece6d0c82009-03-19 15:35:50 +0100165
Frans Meulenbroeks388a29d2010-07-31 15:01:53 +0200166U_BOOT_CMD(
167 ubifsload, 4, 0, do_ubifs_load,
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200168 "load file from an UBIFS filesystem",
169 "<addr> <filename> [bytes]\n"
170 " - load file 'filename' to address 'addr'"
171);