blob: 5e9d357cd7d6f0a3d03d7e6634b7a1ad41060dfa [file] [log] [blame]
Stefan Roesece6d0c82009-03-19 15:35:50 +01001/*
2 * (C) Copyright 2008
3 * Stefan Roese, DENX Software Engineering, sr@denx.de.
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Stefan Roesece6d0c82009-03-19 15:35:50 +01006 */
7
8
9/*
10 * UBIFS command support
11 */
12
13#undef DEBUG
14
15#include <common.h>
16#include <config.h>
17#include <command.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
Jeroen Hofstee0e350f82014-06-23 00:22:08 +020023static int do_ubifs_mount(cmd_tbl_t *cmdtp, int flag, int argc,
24 char * const argv[])
Stefan Roesece6d0c82009-03-19 15:35:50 +010025{
26 char *vol_name;
27 int ret;
28
Wolfgang Denk47e26b12010-07-17 01:06:04 +020029 if (argc != 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +000030 return CMD_RET_USAGE;
Wolfgang Denk47e26b12010-07-17 01:06:04 +020031
Stefan Roesece6d0c82009-03-19 15:35:50 +010032 vol_name = argv[1];
33 debug("Using volume %s\n", vol_name);
34
35 if (ubifs_initialized == 0) {
36 ubifs_init();
37 ubifs_initialized = 1;
38 }
39
Heiko Schocherff94bc42014-06-24 10:10:04 +020040 ret = uboot_ubifs_mount(vol_name);
Stefan Roesece6d0c82009-03-19 15:35:50 +010041 if (ret)
42 return -1;
43
44 ubifs_mounted = 1;
45
46 return 0;
47}
48
Stefan Roese2f15cfd2010-11-01 17:28:22 +010049int ubifs_is_mounted(void)
50{
51 return ubifs_mounted;
52}
53
54void cmd_ubifs_umount(void)
55{
Hans de Goedead157492015-09-17 18:46:56 -040056 uboot_ubifs_umount();
Stefan Roese2f15cfd2010-11-01 17:28:22 +010057 ubifs_mounted = 0;
58 ubifs_initialized = 0;
59}
60
Jeroen Hofstee0e350f82014-06-23 00:22:08 +020061static int do_ubifs_umount(cmd_tbl_t *cmdtp, int flag, int argc,
62 char * const argv[])
Stefan Roesecb9c09d2010-10-28 14:09:22 +020063{
64 if (argc != 1)
Simon Glass4c12eeb2011-12-10 08:44:01 +000065 return CMD_RET_USAGE;
Stefan Roesecb9c09d2010-10-28 14:09:22 +020066
67 if (ubifs_initialized == 0) {
68 printf("No UBIFS volume mounted!\n");
69 return -1;
70 }
71
Stefan Roese2f15cfd2010-11-01 17:28:22 +010072 cmd_ubifs_umount();
Stefan Roesecb9c09d2010-10-28 14:09:22 +020073
74 return 0;
75}
76
Jeroen Hofstee0e350f82014-06-23 00:22:08 +020077static int do_ubifs_ls(cmd_tbl_t *cmdtp, int flag, int argc,
78 char * const argv[])
Stefan Roesece6d0c82009-03-19 15:35:50 +010079{
80 char *filename = "/";
81 int ret;
82
83 if (!ubifs_mounted) {
Stefan Roese9a2ea572010-10-28 14:09:29 +020084 printf("UBIFS not mounted, use ubifsmount to mount volume first!\n");
Stefan Roesece6d0c82009-03-19 15:35:50 +010085 return -1;
86 }
87
88 if (argc == 2)
89 filename = argv[1];
90 debug("Using filename %s\n", filename);
91
92 ret = ubifs_ls(filename);
Tim Harvey7cdebc32013-10-10 01:32:26 +020093 if (ret) {
94 printf("** File not found %s **\n", filename);
95 ret = CMD_RET_FAILURE;
96 }
Stefan Roesece6d0c82009-03-19 15:35:50 +010097
98 return ret;
99}
100
Jeroen Hofstee0e350f82014-06-23 00:22:08 +0200101static int do_ubifs_load(cmd_tbl_t *cmdtp, int flag, int argc,
102 char * const argv[])
Stefan Roesece6d0c82009-03-19 15:35:50 +0100103{
104 char *filename;
Simon Kagstrom2896b582009-07-07 16:01:02 +0200105 char *endp;
Stefan Roesece6d0c82009-03-19 15:35:50 +0100106 int ret;
107 u32 addr;
108 u32 size = 0;
109
110 if (!ubifs_mounted) {
111 printf("UBIFS not mounted, use ubifs mount to mount volume first!\n");
112 return -1;
113 }
114
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200115 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000116 return CMD_RET_USAGE;
Stefan Roesece6d0c82009-03-19 15:35:50 +0100117
Simon Kagstrom2896b582009-07-07 16:01:02 +0200118 addr = simple_strtoul(argv[1], &endp, 16);
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200119 if (endp == argv[1])
Simon Glass4c12eeb2011-12-10 08:44:01 +0000120 return CMD_RET_USAGE;
Simon Kagstrom2896b582009-07-07 16:01:02 +0200121
Stefan Roesece6d0c82009-03-19 15:35:50 +0100122 filename = argv[2];
123
Simon Kagstrom2896b582009-07-07 16:01:02 +0200124 if (argc == 4) {
125 size = simple_strtoul(argv[3], &endp, 16);
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200126 if (endp == argv[3])
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 debug("Loading file '%s' to address 0x%08x (size %d)\n", filename, addr, size);
130
131 ret = ubifs_load(filename, addr, size);
Tim Harvey7cdebc32013-10-10 01:32:26 +0200132 if (ret) {
133 printf("** File not found %s **\n", filename);
134 ret = CMD_RET_FAILURE;
135 }
Stefan Roesece6d0c82009-03-19 15:35:50 +0100136
137 return ret;
138}
139
140U_BOOT_CMD(
141 ubifsmount, 2, 0, do_ubifs_mount,
Mike Frysinger852dbfd2009-03-23 22:27:34 -0400142 "mount UBIFS volume",
Simon Kagstrom2896b582009-07-07 16:01:02 +0200143 "<volume-name>\n"
144 " - mount 'volume-name' volume"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200145);
Stefan Roesece6d0c82009-03-19 15:35:50 +0100146
Frans Meulenbroeks388a29d2010-07-31 15:01:53 +0200147U_BOOT_CMD(
Stefan Roesecb9c09d2010-10-28 14:09:22 +0200148 ubifsumount, 1, 0, do_ubifs_umount,
149 "unmount UBIFS volume",
150 " - unmount current volume"
151);
152
153U_BOOT_CMD(
Frans Meulenbroeks388a29d2010-07-31 15:01:53 +0200154 ubifsls, 2, 0, do_ubifs_ls,
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200155 "list files in a directory",
156 "[directory]\n"
157 " - list files in a 'directory' (default '/')"
158);
Stefan Roesece6d0c82009-03-19 15:35:50 +0100159
Frans Meulenbroeks388a29d2010-07-31 15:01:53 +0200160U_BOOT_CMD(
161 ubifsload, 4, 0, do_ubifs_load,
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200162 "load file from an UBIFS filesystem",
163 "<addr> <filename> [bytes]\n"
164 " - load file 'filename' to address 'addr'"
165);