blob: 8fd39032eccf88ebe4da48be19ef44037fe81cd2 [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
7
8/*
9 * UBIFS command support
10 */
11
12#undef DEBUG
13
Stefan Roese5c721272009-03-19 15:35:50 +010014#include <config.h>
15#include <command.h>
Simon Glass0f2af882020-05-10 11:40:05 -060016#include <log.h>
Hans de Goedea1644772015-09-17 18:46:56 -040017#include <ubifs_uboot.h>
Tom Rinidec7ea02024-05-20 13:35:03 -060018#include <vsprintf.h>
Stefan Roeseff980532010-10-28 14:09:22 +020019
Stefan Roese5c721272009-03-19 15:35:50 +010020static int ubifs_initialized;
21static int ubifs_mounted;
22
Tien Fong Cheebb7387f2018-07-06 16:26:01 +080023int cmd_ubifs_mount(char *vol_name)
Stefan Roese5c721272009-03-19 15:35:50 +010024{
Stefan Roese5c721272009-03-19 15:35:50 +010025 int ret;
26
Stefan Roese5c721272009-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 Schocherf5895d12014-06-24 10:10:04 +020034 ret = uboot_ubifs_mount(vol_name);
Stefan Roese5c721272009-03-19 15:35:50 +010035 if (ret)
Pali Rohár63d3d972022-08-07 21:58:56 +020036 return CMD_RET_FAILURE;
Stefan Roese5c721272009-03-19 15:35:50 +010037
38 ubifs_mounted = 1;
39
Tien Fong Cheebb7387f2018-07-06 16:26:01 +080040 return ret;
41}
Simon Glassed38aef2020-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 Cheebb7387f2018-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 Roese5c721272009-03-19 15:35:50 +010054}
55
Stefan Roese408c6c42010-11-01 17:28:22 +010056int ubifs_is_mounted(void)
57{
58 return ubifs_mounted;
59}
60
Tien Fong Cheedbc9d852018-07-06 16:25:12 +080061int cmd_ubifs_umount(void)
Stefan Roese408c6c42010-11-01 17:28:22 +010062{
Tien Fong Cheedbc9d852018-07-06 16:25:12 +080063 if (ubifs_initialized == 0) {
64 printf("No UBIFS volume mounted!\n");
Pali Rohár63d3d972022-08-07 21:58:56 +020065 return CMD_RET_FAILURE;
Tien Fong Cheedbc9d852018-07-06 16:25:12 +080066 }
67
Hans de Goedea1644772015-09-17 18:46:56 -040068 uboot_ubifs_umount();
Stefan Roese408c6c42010-11-01 17:28:22 +010069 ubifs_mounted = 0;
70 ubifs_initialized = 0;
Tien Fong Cheedbc9d852018-07-06 16:25:12 +080071
72 return 0;
Stefan Roese408c6c42010-11-01 17:28:22 +010073}
74
Simon Glassed38aef2020-05-10 11:40:03 -060075static int do_ubifs_umount(struct cmd_tbl *cmdtp, int flag, int argc,
76 char *const argv[])
Stefan Roeseff980532010-10-28 14:09:22 +020077{
78 if (argc != 1)
Simon Glassa06dfc72011-12-10 08:44:01 +000079 return CMD_RET_USAGE;
Stefan Roeseff980532010-10-28 14:09:22 +020080
Tien Fong Cheedbc9d852018-07-06 16:25:12 +080081 return cmd_ubifs_umount();
Stefan Roeseff980532010-10-28 14:09:22 +020082}
83
Simon Glassed38aef2020-05-10 11:40:03 -060084static int do_ubifs_ls(struct cmd_tbl *cmdtp, int flag, int argc,
85 char *const argv[])
Stefan Roese5c721272009-03-19 15:35:50 +010086{
87 char *filename = "/";
88 int ret;
89
90 if (!ubifs_mounted) {
Stefan Roese67a0f522010-10-28 14:09:29 +020091 printf("UBIFS not mounted, use ubifsmount to mount volume first!\n");
Pali Rohár63d3d972022-08-07 21:58:56 +020092 return CMD_RET_FAILURE;
Stefan Roese5c721272009-03-19 15:35:50 +010093 }
94
95 if (argc == 2)
96 filename = argv[1];
97 debug("Using filename %s\n", filename);
98
99 ret = ubifs_ls(filename);
Tim Harvey8d66edc2013-10-10 01:32:26 +0200100 if (ret) {
101 printf("** File not found %s **\n", filename);
102 ret = CMD_RET_FAILURE;
103 }
Stefan Roese5c721272009-03-19 15:35:50 +0100104
105 return ret;
106}
107
Simon Glassed38aef2020-05-10 11:40:03 -0600108static int do_ubifs_load(struct cmd_tbl *cmdtp, int flag, int argc,
109 char *const argv[])
Stefan Roese5c721272009-03-19 15:35:50 +0100110{
111 char *filename;
Simon Kagstrom9dae38a2009-07-07 16:01:02 +0200112 char *endp;
Stefan Roese5c721272009-03-19 15:35:50 +0100113 int ret;
Ben Dooks68ebcf72023-06-06 09:23:28 +0100114 unsigned long addr;
Stefan Roese5c721272009-03-19 15:35:50 +0100115 u32 size = 0;
116
117 if (!ubifs_mounted) {
118 printf("UBIFS not mounted, use ubifs mount to mount volume first!\n");
Pali Rohár63d3d972022-08-07 21:58:56 +0200119 return CMD_RET_FAILURE;
Stefan Roese5c721272009-03-19 15:35:50 +0100120 }
121
Wolfgang Denk3b683112010-07-17 01:06:04 +0200122 if (argc < 3)
Simon Glassa06dfc72011-12-10 08:44:01 +0000123 return CMD_RET_USAGE;
Stefan Roese5c721272009-03-19 15:35:50 +0100124
Simon Glass3ff49ec2021-07-24 09:03:29 -0600125 addr = hextoul(argv[1], &endp);
Wolfgang Denk3b683112010-07-17 01:06:04 +0200126 if (endp == argv[1])
Simon Glassa06dfc72011-12-10 08:44:01 +0000127 return CMD_RET_USAGE;
Simon Kagstrom9dae38a2009-07-07 16:01:02 +0200128
Stefan Roese5c721272009-03-19 15:35:50 +0100129 filename = argv[2];
130
Simon Kagstrom9dae38a2009-07-07 16:01:02 +0200131 if (argc == 4) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600132 size = hextoul(argv[3], &endp);
Wolfgang Denk3b683112010-07-17 01:06:04 +0200133 if (endp == argv[3])
Simon Glassa06dfc72011-12-10 08:44:01 +0000134 return CMD_RET_USAGE;
Simon Kagstrom9dae38a2009-07-07 16:01:02 +0200135 }
Ben Dooks68ebcf72023-06-06 09:23:28 +0100136 debug("Loading file '%s' to address 0x%08lx (size %d)\n", filename, addr, size);
Stefan Roese5c721272009-03-19 15:35:50 +0100137
138 ret = ubifs_load(filename, addr, size);
Tim Harvey8d66edc2013-10-10 01:32:26 +0200139 if (ret) {
140 printf("** File not found %s **\n", filename);
141 ret = CMD_RET_FAILURE;
142 }
Stefan Roese5c721272009-03-19 15:35:50 +0100143
144 return ret;
145}
146
147U_BOOT_CMD(
148 ubifsmount, 2, 0, do_ubifs_mount,
Mike Frysinger27e1e622009-03-23 22:27:34 -0400149 "mount UBIFS volume",
Simon Kagstrom9dae38a2009-07-07 16:01:02 +0200150 "<volume-name>\n"
151 " - mount 'volume-name' volume"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200152);
Stefan Roese5c721272009-03-19 15:35:50 +0100153
Frans Meulenbroeks7675a092010-07-31 15:01:53 +0200154U_BOOT_CMD(
Stefan Roeseff980532010-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 Meulenbroeks7675a092010-07-31 15:01:53 +0200161 ubifsls, 2, 0, do_ubifs_ls,
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200162 "list files in a directory",
163 "[directory]\n"
164 " - list files in a 'directory' (default '/')"
165);
Stefan Roese5c721272009-03-19 15:35:50 +0100166
Frans Meulenbroeks7675a092010-07-31 15:01:53 +0200167U_BOOT_CMD(
168 ubifsload, 4, 0, do_ubifs_load,
Wolfgang Denkc54781c2009-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);