blob: a26b653d61e95036c58e0e080e2bae7587bc41c8 [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>
Simon Glassf7ae49f2020-05-10 11:40:05 -060017#include <log.h>
Hans de Goedead157492015-09-17 18:46:56 -040018#include <ubifs_uboot.h>
Stefan Roesecb9c09d2010-10-28 14:09:22 +020019
Stefan Roesece6d0c82009-03-19 15:35:50 +010020static int ubifs_initialized;
21static int ubifs_mounted;
22
Tien Fong Chee14dfc642018-07-06 16:26:01 +080023int cmd_ubifs_mount(char *vol_name)
Stefan Roesece6d0c82009-03-19 15:35:50 +010024{
Stefan Roesece6d0c82009-03-19 15:35:50 +010025 int ret;
26
Stefan Roesece6d0c82009-03-19 15:35:50 +010027 debug("Using volume %s\n", vol_name);
28
29 if (ubifs_initialized == 0) {
30 ubifs_init();
31 ubifs_initialized = 1;
32 }
33
Heiko Schocherff94bc42014-06-24 10:10:04 +020034 ret = uboot_ubifs_mount(vol_name);
Stefan Roesece6d0c82009-03-19 15:35:50 +010035 if (ret)
36 return -1;
37
38 ubifs_mounted = 1;
39
Tien Fong Chee14dfc642018-07-06 16:26:01 +080040 return ret;
41}
Simon Glass09140112020-05-10 11:40:03 -060042
43static int do_ubifs_mount(struct cmd_tbl *cmdtp, int flag, int argc,
44 char *const argv[])
Tien Fong Chee14dfc642018-07-06 16:26:01 +080045{
46 char *vol_name;
47
48 if (argc != 2)
49 return CMD_RET_USAGE;
50
51 vol_name = argv[1];
52
53 return cmd_ubifs_mount(vol_name);
Stefan Roesece6d0c82009-03-19 15:35:50 +010054}
55
Stefan Roese2f15cfd2010-11-01 17:28:22 +010056int ubifs_is_mounted(void)
57{
58 return ubifs_mounted;
59}
60
Tien Fong Chee10c20442018-07-06 16:25:12 +080061int cmd_ubifs_umount(void)
Stefan Roese2f15cfd2010-11-01 17:28:22 +010062{
Tien Fong Chee10c20442018-07-06 16:25:12 +080063 if (ubifs_initialized == 0) {
64 printf("No UBIFS volume mounted!\n");
65 return -1;
66 }
67
Hans de Goedead157492015-09-17 18:46:56 -040068 uboot_ubifs_umount();
Stefan Roese2f15cfd2010-11-01 17:28:22 +010069 ubifs_mounted = 0;
70 ubifs_initialized = 0;
Tien Fong Chee10c20442018-07-06 16:25:12 +080071
72 return 0;
Stefan Roese2f15cfd2010-11-01 17:28:22 +010073}
74
Simon Glass09140112020-05-10 11:40:03 -060075static int do_ubifs_umount(struct cmd_tbl *cmdtp, int flag, int argc,
76 char *const argv[])
Stefan Roesecb9c09d2010-10-28 14:09:22 +020077{
78 if (argc != 1)
Simon Glass4c12eeb2011-12-10 08:44:01 +000079 return CMD_RET_USAGE;
Stefan Roesecb9c09d2010-10-28 14:09:22 +020080
Tien Fong Chee10c20442018-07-06 16:25:12 +080081 return cmd_ubifs_umount();
Stefan Roesecb9c09d2010-10-28 14:09:22 +020082}
83
Simon Glass09140112020-05-10 11:40:03 -060084static int do_ubifs_ls(struct cmd_tbl *cmdtp, int flag, int argc,
85 char *const argv[])
Stefan Roesece6d0c82009-03-19 15:35:50 +010086{
87 char *filename = "/";
88 int ret;
89
90 if (!ubifs_mounted) {
Stefan Roese9a2ea572010-10-28 14:09:29 +020091 printf("UBIFS not mounted, use ubifsmount to mount volume first!\n");
Stefan Roesece6d0c82009-03-19 15:35:50 +010092 return -1;
93 }
94
95 if (argc == 2)
96 filename = argv[1];
97 debug("Using filename %s\n", filename);
98
99 ret = ubifs_ls(filename);
Tim Harvey7cdebc32013-10-10 01:32:26 +0200100 if (ret) {
101 printf("** File not found %s **\n", filename);
102 ret = CMD_RET_FAILURE;
103 }
Stefan Roesece6d0c82009-03-19 15:35:50 +0100104
105 return ret;
106}
107
Simon Glass09140112020-05-10 11:40:03 -0600108static int do_ubifs_load(struct cmd_tbl *cmdtp, int flag, int argc,
109 char *const argv[])
Stefan Roesece6d0c82009-03-19 15:35:50 +0100110{
111 char *filename;
Simon Kagstrom2896b582009-07-07 16:01:02 +0200112 char *endp;
Stefan Roesece6d0c82009-03-19 15:35:50 +0100113 int ret;
114 u32 addr;
115 u32 size = 0;
116
117 if (!ubifs_mounted) {
118 printf("UBIFS not mounted, use ubifs mount to mount volume first!\n");
119 return -1;
120 }
121
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200122 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000123 return CMD_RET_USAGE;
Stefan Roesece6d0c82009-03-19 15:35:50 +0100124
Simon Kagstrom2896b582009-07-07 16:01:02 +0200125 addr = simple_strtoul(argv[1], &endp, 16);
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200126 if (endp == argv[1])
Simon Glass4c12eeb2011-12-10 08:44:01 +0000127 return CMD_RET_USAGE;
Simon Kagstrom2896b582009-07-07 16:01:02 +0200128
Stefan Roesece6d0c82009-03-19 15:35:50 +0100129 filename = argv[2];
130
Simon Kagstrom2896b582009-07-07 16:01:02 +0200131 if (argc == 4) {
132 size = simple_strtoul(argv[3], &endp, 16);
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200133 if (endp == argv[3])
Simon Glass4c12eeb2011-12-10 08:44:01 +0000134 return CMD_RET_USAGE;
Simon Kagstrom2896b582009-07-07 16:01:02 +0200135 }
Stefan Roesece6d0c82009-03-19 15:35:50 +0100136 debug("Loading file '%s' to address 0x%08x (size %d)\n", filename, addr, size);
137
138 ret = ubifs_load(filename, addr, size);
Tim Harvey7cdebc32013-10-10 01:32:26 +0200139 if (ret) {
140 printf("** File not found %s **\n", filename);
141 ret = CMD_RET_FAILURE;
142 }
Stefan Roesece6d0c82009-03-19 15:35:50 +0100143
144 return ret;
145}
146
147U_BOOT_CMD(
148 ubifsmount, 2, 0, do_ubifs_mount,
Mike Frysinger852dbfd2009-03-23 22:27:34 -0400149 "mount UBIFS volume",
Simon Kagstrom2896b582009-07-07 16:01:02 +0200150 "<volume-name>\n"
151 " - mount 'volume-name' volume"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200152);
Stefan Roesece6d0c82009-03-19 15:35:50 +0100153
Frans Meulenbroeks388a29d2010-07-31 15:01:53 +0200154U_BOOT_CMD(
Stefan Roesecb9c09d2010-10-28 14:09:22 +0200155 ubifsumount, 1, 0, do_ubifs_umount,
156 "unmount UBIFS volume",
157 " - unmount current volume"
158);
159
160U_BOOT_CMD(
Frans Meulenbroeks388a29d2010-07-31 15:01:53 +0200161 ubifsls, 2, 0, do_ubifs_ls,
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200162 "list files in a directory",
163 "[directory]\n"
164 " - list files in a 'directory' (default '/')"
165);
Stefan Roesece6d0c82009-03-19 15:35:50 +0100166
Frans Meulenbroeks388a29d2010-07-31 15:01:53 +0200167U_BOOT_CMD(
168 ubifsload, 4, 0, do_ubifs_load,
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200169 "load file from an UBIFS filesystem",
170 "<addr> <filename> [bytes]\n"
171 " - load file 'filename' to address 'addr'"
172);