blob: d9af023d703d08319bc196cf76b3585ea5b54196 [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>
18
Stefan Roesecb9c09d2010-10-28 14:09:22 +020019#include "../fs/ubifs/ubifs.h"
20
Stefan Roesece6d0c82009-03-19 15:35:50 +010021static int ubifs_initialized;
22static int ubifs_mounted;
23
Stefan Roesecb9c09d2010-10-28 14:09:22 +020024extern struct super_block *ubifs_sb;
25
Stefan Roesece6d0c82009-03-19 15:35:50 +010026/* Prototypes */
27int ubifs_init(void);
28int ubifs_mount(char *vol_name);
Stefan Roesecb9c09d2010-10-28 14:09:22 +020029void ubifs_umount(struct ubifs_info *c);
Stefan Roesece6d0c82009-03-19 15:35:50 +010030int ubifs_ls(char *dir_name);
31int ubifs_load(char *filename, u32 addr, u32 size);
32
Wolfgang Denk54841ab2010-06-28 22:00:46 +020033int do_ubifs_mount(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Stefan Roesece6d0c82009-03-19 15:35:50 +010034{
35 char *vol_name;
36 int ret;
37
Wolfgang Denk47e26b12010-07-17 01:06:04 +020038 if (argc != 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +000039 return CMD_RET_USAGE;
Wolfgang Denk47e26b12010-07-17 01:06:04 +020040
Stefan Roesece6d0c82009-03-19 15:35:50 +010041 vol_name = argv[1];
42 debug("Using volume %s\n", vol_name);
43
44 if (ubifs_initialized == 0) {
45 ubifs_init();
46 ubifs_initialized = 1;
47 }
48
49 ret = ubifs_mount(vol_name);
50 if (ret)
51 return -1;
52
53 ubifs_mounted = 1;
54
55 return 0;
56}
57
Stefan Roese2f15cfd2010-11-01 17:28:22 +010058int ubifs_is_mounted(void)
59{
60 return ubifs_mounted;
61}
62
63void cmd_ubifs_umount(void)
64{
65
66 if (ubifs_sb) {
67 printf("Unmounting UBIFS volume %s!\n",
68 ((struct ubifs_info *)(ubifs_sb->s_fs_info))->vi.name);
69 ubifs_umount(ubifs_sb->s_fs_info);
70 }
71
72 ubifs_sb = NULL;
73 ubifs_mounted = 0;
74 ubifs_initialized = 0;
75}
76
Stefan Roesecb9c09d2010-10-28 14:09:22 +020077int do_ubifs_umount(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
78{
79 if (argc != 1)
Simon Glass4c12eeb2011-12-10 08:44:01 +000080 return CMD_RET_USAGE;
Stefan Roesecb9c09d2010-10-28 14:09:22 +020081
82 if (ubifs_initialized == 0) {
83 printf("No UBIFS volume mounted!\n");
84 return -1;
85 }
86
Stefan Roese2f15cfd2010-11-01 17:28:22 +010087 cmd_ubifs_umount();
Stefan Roesecb9c09d2010-10-28 14:09:22 +020088
89 return 0;
90}
91
Wolfgang Denk54841ab2010-06-28 22:00:46 +020092int do_ubifs_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Stefan Roesece6d0c82009-03-19 15:35:50 +010093{
94 char *filename = "/";
95 int ret;
96
97 if (!ubifs_mounted) {
Stefan Roese9a2ea572010-10-28 14:09:29 +020098 printf("UBIFS not mounted, use ubifsmount to mount volume first!\n");
Stefan Roesece6d0c82009-03-19 15:35:50 +010099 return -1;
100 }
101
102 if (argc == 2)
103 filename = argv[1];
104 debug("Using filename %s\n", filename);
105
106 ret = ubifs_ls(filename);
Tim Harvey7cdebc32013-10-10 01:32:26 +0200107 if (ret) {
108 printf("** File not found %s **\n", filename);
109 ret = CMD_RET_FAILURE;
110 }
Stefan Roesece6d0c82009-03-19 15:35:50 +0100111
112 return ret;
113}
114
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200115int do_ubifs_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Stefan Roesece6d0c82009-03-19 15:35:50 +0100116{
117 char *filename;
Simon Kagstrom2896b582009-07-07 16:01:02 +0200118 char *endp;
Stefan Roesece6d0c82009-03-19 15:35:50 +0100119 int ret;
120 u32 addr;
121 u32 size = 0;
122
123 if (!ubifs_mounted) {
124 printf("UBIFS not mounted, use ubifs mount to mount volume first!\n");
125 return -1;
126 }
127
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200128 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000129 return CMD_RET_USAGE;
Stefan Roesece6d0c82009-03-19 15:35:50 +0100130
Simon Kagstrom2896b582009-07-07 16:01:02 +0200131 addr = simple_strtoul(argv[1], &endp, 16);
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200132 if (endp == argv[1])
Simon Glass4c12eeb2011-12-10 08:44:01 +0000133 return CMD_RET_USAGE;
Simon Kagstrom2896b582009-07-07 16:01:02 +0200134
Stefan Roesece6d0c82009-03-19 15:35:50 +0100135 filename = argv[2];
136
Simon Kagstrom2896b582009-07-07 16:01:02 +0200137 if (argc == 4) {
138 size = simple_strtoul(argv[3], &endp, 16);
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200139 if (endp == argv[3])
Simon Glass4c12eeb2011-12-10 08:44:01 +0000140 return CMD_RET_USAGE;
Simon Kagstrom2896b582009-07-07 16:01:02 +0200141 }
Stefan Roesece6d0c82009-03-19 15:35:50 +0100142 debug("Loading file '%s' to address 0x%08x (size %d)\n", filename, addr, size);
143
144 ret = ubifs_load(filename, addr, size);
Tim Harvey7cdebc32013-10-10 01:32:26 +0200145 if (ret) {
146 printf("** File not found %s **\n", filename);
147 ret = CMD_RET_FAILURE;
148 }
Stefan Roesece6d0c82009-03-19 15:35:50 +0100149
150 return ret;
151}
152
153U_BOOT_CMD(
154 ubifsmount, 2, 0, do_ubifs_mount,
Mike Frysinger852dbfd2009-03-23 22:27:34 -0400155 "mount UBIFS volume",
Simon Kagstrom2896b582009-07-07 16:01:02 +0200156 "<volume-name>\n"
157 " - mount 'volume-name' volume"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200158);
Stefan Roesece6d0c82009-03-19 15:35:50 +0100159
Frans Meulenbroeks388a29d2010-07-31 15:01:53 +0200160U_BOOT_CMD(
Stefan Roesecb9c09d2010-10-28 14:09:22 +0200161 ubifsumount, 1, 0, do_ubifs_umount,
162 "unmount UBIFS volume",
163 " - unmount current volume"
164);
165
166U_BOOT_CMD(
Frans Meulenbroeks388a29d2010-07-31 15:01:53 +0200167 ubifsls, 2, 0, do_ubifs_ls,
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200168 "list files in a directory",
169 "[directory]\n"
170 " - list files in a 'directory' (default '/')"
171);
Stefan Roesece6d0c82009-03-19 15:35:50 +0100172
Frans Meulenbroeks388a29d2010-07-31 15:01:53 +0200173U_BOOT_CMD(
174 ubifsload, 4, 0, do_ubifs_load,
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200175 "load file from an UBIFS filesystem",
176 "<addr> <filename> [bytes]\n"
177 " - load file 'filename' to address 'addr'"
178);