blob: 22e95db8ca58c6a21e49aad388f91408b86a8ef1 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stefan Roese5c721272009-03-19 15:35:50 +01002/*
3 * (C) Copyright 2008
4 * Stefan Roese, DENX Software Engineering, sr@denx.de.
Stefan Roese5c721272009-03-19 15:35:50 +01005 */
6
Stefan Roese5c721272009-03-19 15:35:50 +01007/*
8 * UBIFS command support
9 */
10
11#undef DEBUG
12
Stefan Roese5c721272009-03-19 15:35:50 +010013#include <config.h>
14#include <command.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Hans de Goedea1644772015-09-17 18:46:56 -040016#include <ubifs_uboot.h>
Tom Rinidec7ea02024-05-20 13:35:03 -060017#include <vsprintf.h>
Stefan Roeseff980532010-10-28 14:09:22 +020018
Stefan Roese5c721272009-03-19 15:35:50 +010019static int ubifs_initialized;
20static int ubifs_mounted;
21
Tien Fong Cheebb7387f2018-07-06 16:26:01 +080022int cmd_ubifs_mount(char *vol_name)
Stefan Roese5c721272009-03-19 15:35:50 +010023{
Stefan Roese5c721272009-03-19 15:35:50 +010024 int ret;
25
Stefan Roese5c721272009-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 Schocherf5895d12014-06-24 10:10:04 +020033 ret = uboot_ubifs_mount(vol_name);
Stefan Roese5c721272009-03-19 15:35:50 +010034 if (ret)
Pali Rohár63d3d972022-08-07 21:58:56 +020035 return CMD_RET_FAILURE;
Stefan Roese5c721272009-03-19 15:35:50 +010036
37 ubifs_mounted = 1;
38
Tien Fong Cheebb7387f2018-07-06 16:26:01 +080039 return ret;
40}
Simon Glassed38aef2020-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 Cheebb7387f2018-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 Roese5c721272009-03-19 15:35:50 +010053}
54
Stefan Roese408c6c42010-11-01 17:28:22 +010055int ubifs_is_mounted(void)
56{
57 return ubifs_mounted;
58}
59
Tien Fong Cheedbc9d852018-07-06 16:25:12 +080060int cmd_ubifs_umount(void)
Stefan Roese408c6c42010-11-01 17:28:22 +010061{
Tien Fong Cheedbc9d852018-07-06 16:25:12 +080062 if (ubifs_initialized == 0) {
63 printf("No UBIFS volume mounted!\n");
Pali Rohár63d3d972022-08-07 21:58:56 +020064 return CMD_RET_FAILURE;
Tien Fong Cheedbc9d852018-07-06 16:25:12 +080065 }
66
Hans de Goedea1644772015-09-17 18:46:56 -040067 uboot_ubifs_umount();
Stefan Roese408c6c42010-11-01 17:28:22 +010068 ubifs_mounted = 0;
69 ubifs_initialized = 0;
Tien Fong Cheedbc9d852018-07-06 16:25:12 +080070
71 return 0;
Stefan Roese408c6c42010-11-01 17:28:22 +010072}
73
Simon Glassed38aef2020-05-10 11:40:03 -060074static int do_ubifs_umount(struct cmd_tbl *cmdtp, int flag, int argc,
75 char *const argv[])
Stefan Roeseff980532010-10-28 14:09:22 +020076{
77 if (argc != 1)
Simon Glassa06dfc72011-12-10 08:44:01 +000078 return CMD_RET_USAGE;
Stefan Roeseff980532010-10-28 14:09:22 +020079
Tien Fong Cheedbc9d852018-07-06 16:25:12 +080080 return cmd_ubifs_umount();
Stefan Roeseff980532010-10-28 14:09:22 +020081}
82
Simon Glassed38aef2020-05-10 11:40:03 -060083static int do_ubifs_ls(struct cmd_tbl *cmdtp, int flag, int argc,
84 char *const argv[])
Stefan Roese5c721272009-03-19 15:35:50 +010085{
86 char *filename = "/";
87 int ret;
88
89 if (!ubifs_mounted) {
Stefan Roese67a0f522010-10-28 14:09:29 +020090 printf("UBIFS not mounted, use ubifsmount to mount volume first!\n");
Pali Rohár63d3d972022-08-07 21:58:56 +020091 return CMD_RET_FAILURE;
Stefan Roese5c721272009-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 Harvey8d66edc2013-10-10 01:32:26 +020099 if (ret) {
100 printf("** File not found %s **\n", filename);
101 ret = CMD_RET_FAILURE;
102 }
Stefan Roese5c721272009-03-19 15:35:50 +0100103
104 return ret;
105}
106
Simon Glassed38aef2020-05-10 11:40:03 -0600107static int do_ubifs_load(struct cmd_tbl *cmdtp, int flag, int argc,
108 char *const argv[])
Stefan Roese5c721272009-03-19 15:35:50 +0100109{
110 char *filename;
Simon Kagstrom9dae38a2009-07-07 16:01:02 +0200111 char *endp;
Stefan Roese5c721272009-03-19 15:35:50 +0100112 int ret;
Ben Dooks68ebcf72023-06-06 09:23:28 +0100113 unsigned long addr;
Stefan Roese5c721272009-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ár63d3d972022-08-07 21:58:56 +0200118 return CMD_RET_FAILURE;
Stefan Roese5c721272009-03-19 15:35:50 +0100119 }
120
Wolfgang Denk3b683112010-07-17 01:06:04 +0200121 if (argc < 3)
Simon Glassa06dfc72011-12-10 08:44:01 +0000122 return CMD_RET_USAGE;
Stefan Roese5c721272009-03-19 15:35:50 +0100123
Simon Glass3ff49ec2021-07-24 09:03:29 -0600124 addr = hextoul(argv[1], &endp);
Wolfgang Denk3b683112010-07-17 01:06:04 +0200125 if (endp == argv[1])
Simon Glassa06dfc72011-12-10 08:44:01 +0000126 return CMD_RET_USAGE;
Simon Kagstrom9dae38a2009-07-07 16:01:02 +0200127
Stefan Roese5c721272009-03-19 15:35:50 +0100128 filename = argv[2];
129
Simon Kagstrom9dae38a2009-07-07 16:01:02 +0200130 if (argc == 4) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600131 size = hextoul(argv[3], &endp);
Wolfgang Denk3b683112010-07-17 01:06:04 +0200132 if (endp == argv[3])
Simon Glassa06dfc72011-12-10 08:44:01 +0000133 return CMD_RET_USAGE;
Simon Kagstrom9dae38a2009-07-07 16:01:02 +0200134 }
Ben Dooks68ebcf72023-06-06 09:23:28 +0100135 debug("Loading file '%s' to address 0x%08lx (size %d)\n", filename, addr, size);
Stefan Roese5c721272009-03-19 15:35:50 +0100136
137 ret = ubifs_load(filename, addr, size);
Tim Harvey8d66edc2013-10-10 01:32:26 +0200138 if (ret) {
139 printf("** File not found %s **\n", filename);
140 ret = CMD_RET_FAILURE;
141 }
Stefan Roese5c721272009-03-19 15:35:50 +0100142
143 return ret;
144}
145
146U_BOOT_CMD(
147 ubifsmount, 2, 0, do_ubifs_mount,
Mike Frysinger27e1e622009-03-23 22:27:34 -0400148 "mount UBIFS volume",
Simon Kagstrom9dae38a2009-07-07 16:01:02 +0200149 "<volume-name>\n"
150 " - mount 'volume-name' volume"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200151);
Stefan Roese5c721272009-03-19 15:35:50 +0100152
Frans Meulenbroeks7675a092010-07-31 15:01:53 +0200153U_BOOT_CMD(
Stefan Roeseff980532010-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 Meulenbroeks7675a092010-07-31 15:01:53 +0200160 ubifsls, 2, 0, do_ubifs_ls,
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200161 "list files in a directory",
162 "[directory]\n"
163 " - list files in a 'directory' (default '/')"
164);
Stefan Roese5c721272009-03-19 15:35:50 +0100165
Frans Meulenbroeks7675a092010-07-31 15:01:53 +0200166U_BOOT_CMD(
167 ubifsload, 4, 0, do_ubifs_load,
Wolfgang Denkc54781c2009-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);