blob: 22e95db8ca58c6a21e49aad388f91408b86a8ef1 [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
Stefan Roesece6d0c82009-03-19 15:35:50 +01007/*
8 * UBIFS command support
9 */
10
11#undef DEBUG
12
Stefan Roesece6d0c82009-03-19 15:35:50 +010013#include <config.h>
14#include <command.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060015#include <log.h>
Hans de Goedead157492015-09-17 18:46:56 -040016#include <ubifs_uboot.h>
Tom Rini03de3052024-05-20 13:35:03 -060017#include <vsprintf.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)
Pali Rohár18538112022-08-07 21:58:56 +020035 return CMD_RET_FAILURE;
Stefan Roesece6d0c82009-03-19 15:35:50 +010036
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");
Pali Rohár18538112022-08-07 21:58:56 +020064 return CMD_RET_FAILURE;
Tien Fong Chee10c20442018-07-06 16:25:12 +080065 }
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");
Pali Rohár18538112022-08-07 21:58:56 +020091 return CMD_RET_FAILURE;
Stefan Roesece6d0c82009-03-19 15:35:50 +010092 }
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;
Ben Dooksb46cec42023-06-06 09:23:28 +0100113 unsigned long addr;
Stefan Roesece6d0c82009-03-19 15:35:50 +0100114 u32 size = 0;
115
116 if (!ubifs_mounted) {
117 printf("UBIFS not mounted, use ubifs mount to mount volume first!\n");
Pali Rohár18538112022-08-07 21:58:56 +0200118 return CMD_RET_FAILURE;
Stefan Roesece6d0c82009-03-19 15:35:50 +0100119 }
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 Glass7e5f4602021-07-24 09:03:29 -0600124 addr = hextoul(argv[1], &endp);
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) {
Simon Glass7e5f4602021-07-24 09:03:29 -0600131 size = hextoul(argv[3], &endp);
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 }
Ben Dooksb46cec42023-06-06 09:23:28 +0100135 debug("Loading file '%s' to address 0x%08lx (size %d)\n", filename, addr, size);
Stefan Roesece6d0c82009-03-19 15:35:50 +0100136
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);