blob: 11bab7a1a1f67546bfc357faef8d315a4309795d [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
Jeroen Hofstee0e350f82014-06-23 00:22:08 +020022static int do_ubifs_mount(cmd_tbl_t *cmdtp, int flag, int argc,
23 char * const argv[])
Stefan Roesece6d0c82009-03-19 15:35:50 +010024{
25 char *vol_name;
26 int ret;
27
Wolfgang Denk47e26b12010-07-17 01:06:04 +020028 if (argc != 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +000029 return CMD_RET_USAGE;
Wolfgang Denk47e26b12010-07-17 01:06:04 +020030
Stefan Roesece6d0c82009-03-19 15:35:50 +010031 vol_name = argv[1];
32 debug("Using volume %s\n", vol_name);
33
34 if (ubifs_initialized == 0) {
35 ubifs_init();
36 ubifs_initialized = 1;
37 }
38
Heiko Schocherff94bc42014-06-24 10:10:04 +020039 ret = uboot_ubifs_mount(vol_name);
Stefan Roesece6d0c82009-03-19 15:35:50 +010040 if (ret)
41 return -1;
42
43 ubifs_mounted = 1;
44
45 return 0;
46}
47
Stefan Roese2f15cfd2010-11-01 17:28:22 +010048int ubifs_is_mounted(void)
49{
50 return ubifs_mounted;
51}
52
53void cmd_ubifs_umount(void)
54{
Hans de Goedead157492015-09-17 18:46:56 -040055 uboot_ubifs_umount();
Stefan Roese2f15cfd2010-11-01 17:28:22 +010056 ubifs_mounted = 0;
57 ubifs_initialized = 0;
58}
59
Jeroen Hofstee0e350f82014-06-23 00:22:08 +020060static int do_ubifs_umount(cmd_tbl_t *cmdtp, int flag, int argc,
61 char * const argv[])
Stefan Roesecb9c09d2010-10-28 14:09:22 +020062{
63 if (argc != 1)
Simon Glass4c12eeb2011-12-10 08:44:01 +000064 return CMD_RET_USAGE;
Stefan Roesecb9c09d2010-10-28 14:09:22 +020065
66 if (ubifs_initialized == 0) {
67 printf("No UBIFS volume mounted!\n");
68 return -1;
69 }
70
Stefan Roese2f15cfd2010-11-01 17:28:22 +010071 cmd_ubifs_umount();
Stefan Roesecb9c09d2010-10-28 14:09:22 +020072
73 return 0;
74}
75
Jeroen Hofstee0e350f82014-06-23 00:22:08 +020076static int do_ubifs_ls(cmd_tbl_t *cmdtp, int flag, int argc,
77 char * const argv[])
Stefan Roesece6d0c82009-03-19 15:35:50 +010078{
79 char *filename = "/";
80 int ret;
81
82 if (!ubifs_mounted) {
Stefan Roese9a2ea572010-10-28 14:09:29 +020083 printf("UBIFS not mounted, use ubifsmount to mount volume first!\n");
Stefan Roesece6d0c82009-03-19 15:35:50 +010084 return -1;
85 }
86
87 if (argc == 2)
88 filename = argv[1];
89 debug("Using filename %s\n", filename);
90
91 ret = ubifs_ls(filename);
Tim Harvey7cdebc32013-10-10 01:32:26 +020092 if (ret) {
93 printf("** File not found %s **\n", filename);
94 ret = CMD_RET_FAILURE;
95 }
Stefan Roesece6d0c82009-03-19 15:35:50 +010096
97 return ret;
98}
99
Jeroen Hofstee0e350f82014-06-23 00:22:08 +0200100static int do_ubifs_load(cmd_tbl_t *cmdtp, int flag, int argc,
101 char * const argv[])
Stefan Roesece6d0c82009-03-19 15:35:50 +0100102{
103 char *filename;
Simon Kagstrom2896b582009-07-07 16:01:02 +0200104 char *endp;
Stefan Roesece6d0c82009-03-19 15:35:50 +0100105 int ret;
106 u32 addr;
107 u32 size = 0;
108
109 if (!ubifs_mounted) {
110 printf("UBIFS not mounted, use ubifs mount to mount volume first!\n");
111 return -1;
112 }
113
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200114 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000115 return CMD_RET_USAGE;
Stefan Roesece6d0c82009-03-19 15:35:50 +0100116
Simon Kagstrom2896b582009-07-07 16:01:02 +0200117 addr = simple_strtoul(argv[1], &endp, 16);
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200118 if (endp == argv[1])
Simon Glass4c12eeb2011-12-10 08:44:01 +0000119 return CMD_RET_USAGE;
Simon Kagstrom2896b582009-07-07 16:01:02 +0200120
Stefan Roesece6d0c82009-03-19 15:35:50 +0100121 filename = argv[2];
122
Simon Kagstrom2896b582009-07-07 16:01:02 +0200123 if (argc == 4) {
124 size = simple_strtoul(argv[3], &endp, 16);
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200125 if (endp == argv[3])
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 debug("Loading file '%s' to address 0x%08x (size %d)\n", filename, addr, size);
129
130 ret = ubifs_load(filename, addr, size);
Tim Harvey7cdebc32013-10-10 01:32:26 +0200131 if (ret) {
132 printf("** File not found %s **\n", filename);
133 ret = CMD_RET_FAILURE;
134 }
Stefan Roesece6d0c82009-03-19 15:35:50 +0100135
136 return ret;
137}
138
139U_BOOT_CMD(
140 ubifsmount, 2, 0, do_ubifs_mount,
Mike Frysinger852dbfd2009-03-23 22:27:34 -0400141 "mount UBIFS volume",
Simon Kagstrom2896b582009-07-07 16:01:02 +0200142 "<volume-name>\n"
143 " - mount 'volume-name' volume"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200144);
Stefan Roesece6d0c82009-03-19 15:35:50 +0100145
Frans Meulenbroeks388a29d2010-07-31 15:01:53 +0200146U_BOOT_CMD(
Stefan Roesecb9c09d2010-10-28 14:09:22 +0200147 ubifsumount, 1, 0, do_ubifs_umount,
148 "unmount UBIFS volume",
149 " - unmount current volume"
150);
151
152U_BOOT_CMD(
Frans Meulenbroeks388a29d2010-07-31 15:01:53 +0200153 ubifsls, 2, 0, do_ubifs_ls,
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200154 "list files in a directory",
155 "[directory]\n"
156 " - list files in a 'directory' (default '/')"
157);
Stefan Roesece6d0c82009-03-19 15:35:50 +0100158
Frans Meulenbroeks388a29d2010-07-31 15:01:53 +0200159U_BOOT_CMD(
160 ubifsload, 4, 0, do_ubifs_load,
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200161 "load file from an UBIFS filesystem",
162 "<addr> <filename> [bytes]\n"
163 " - load file 'filename' to address 'addr'"
164);