Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Stefan Roese | 5c72127 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2008 |
| 4 | * Stefan Roese, DENX Software Engineering, sr@denx.de. |
Stefan Roese | 5c72127 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | |
| 8 | /* |
| 9 | * UBIFS command support |
| 10 | */ |
| 11 | |
| 12 | #undef DEBUG |
| 13 | |
Stefan Roese | 5c72127 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 14 | #include <config.h> |
| 15 | #include <command.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 16 | #include <log.h> |
Hans de Goede | a164477 | 2015-09-17 18:46:56 -0400 | [diff] [blame] | 17 | #include <ubifs_uboot.h> |
Tom Rini | 5c1444f | 2024-04-27 08:10:59 -0600 | [diff] [blame] | 18 | #include <vsprintf.h> |
Stefan Roese | ff98053 | 2010-10-28 14:09:22 +0200 | [diff] [blame] | 19 | |
Stefan Roese | 5c72127 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 20 | static int ubifs_initialized; |
| 21 | static int ubifs_mounted; |
| 22 | |
Tien Fong Chee | bb7387f | 2018-07-06 16:26:01 +0800 | [diff] [blame] | 23 | int cmd_ubifs_mount(char *vol_name) |
Stefan Roese | 5c72127 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 24 | { |
Stefan Roese | 5c72127 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 25 | int ret; |
| 26 | |
Stefan Roese | 5c72127 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 27 | debug("Using volume %s\n", vol_name); |
| 28 | |
| 29 | if (ubifs_initialized == 0) { |
| 30 | ubifs_init(); |
| 31 | ubifs_initialized = 1; |
| 32 | } |
| 33 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 34 | ret = uboot_ubifs_mount(vol_name); |
Stefan Roese | 5c72127 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 35 | if (ret) |
Pali Rohár | 63d3d97 | 2022-08-07 21:58:56 +0200 | [diff] [blame] | 36 | return CMD_RET_FAILURE; |
Stefan Roese | 5c72127 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 37 | |
| 38 | ubifs_mounted = 1; |
| 39 | |
Tien Fong Chee | bb7387f | 2018-07-06 16:26:01 +0800 | [diff] [blame] | 40 | return ret; |
| 41 | } |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 42 | |
| 43 | static int do_ubifs_mount(struct cmd_tbl *cmdtp, int flag, int argc, |
| 44 | char *const argv[]) |
Tien Fong Chee | bb7387f | 2018-07-06 16:26:01 +0800 | [diff] [blame] | 45 | { |
| 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 Roese | 5c72127 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 54 | } |
| 55 | |
Stefan Roese | 408c6c4 | 2010-11-01 17:28:22 +0100 | [diff] [blame] | 56 | int ubifs_is_mounted(void) |
| 57 | { |
| 58 | return ubifs_mounted; |
| 59 | } |
| 60 | |
Tien Fong Chee | dbc9d85 | 2018-07-06 16:25:12 +0800 | [diff] [blame] | 61 | int cmd_ubifs_umount(void) |
Stefan Roese | 408c6c4 | 2010-11-01 17:28:22 +0100 | [diff] [blame] | 62 | { |
Tien Fong Chee | dbc9d85 | 2018-07-06 16:25:12 +0800 | [diff] [blame] | 63 | if (ubifs_initialized == 0) { |
| 64 | printf("No UBIFS volume mounted!\n"); |
Pali Rohár | 63d3d97 | 2022-08-07 21:58:56 +0200 | [diff] [blame] | 65 | return CMD_RET_FAILURE; |
Tien Fong Chee | dbc9d85 | 2018-07-06 16:25:12 +0800 | [diff] [blame] | 66 | } |
| 67 | |
Hans de Goede | a164477 | 2015-09-17 18:46:56 -0400 | [diff] [blame] | 68 | uboot_ubifs_umount(); |
Stefan Roese | 408c6c4 | 2010-11-01 17:28:22 +0100 | [diff] [blame] | 69 | ubifs_mounted = 0; |
| 70 | ubifs_initialized = 0; |
Tien Fong Chee | dbc9d85 | 2018-07-06 16:25:12 +0800 | [diff] [blame] | 71 | |
| 72 | return 0; |
Stefan Roese | 408c6c4 | 2010-11-01 17:28:22 +0100 | [diff] [blame] | 73 | } |
| 74 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 75 | static int do_ubifs_umount(struct cmd_tbl *cmdtp, int flag, int argc, |
| 76 | char *const argv[]) |
Stefan Roese | ff98053 | 2010-10-28 14:09:22 +0200 | [diff] [blame] | 77 | { |
| 78 | if (argc != 1) |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 79 | return CMD_RET_USAGE; |
Stefan Roese | ff98053 | 2010-10-28 14:09:22 +0200 | [diff] [blame] | 80 | |
Tien Fong Chee | dbc9d85 | 2018-07-06 16:25:12 +0800 | [diff] [blame] | 81 | return cmd_ubifs_umount(); |
Stefan Roese | ff98053 | 2010-10-28 14:09:22 +0200 | [diff] [blame] | 82 | } |
| 83 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 84 | static int do_ubifs_ls(struct cmd_tbl *cmdtp, int flag, int argc, |
| 85 | char *const argv[]) |
Stefan Roese | 5c72127 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 86 | { |
| 87 | char *filename = "/"; |
| 88 | int ret; |
| 89 | |
| 90 | if (!ubifs_mounted) { |
Stefan Roese | 67a0f52 | 2010-10-28 14:09:29 +0200 | [diff] [blame] | 91 | printf("UBIFS not mounted, use ubifsmount to mount volume first!\n"); |
Pali Rohár | 63d3d97 | 2022-08-07 21:58:56 +0200 | [diff] [blame] | 92 | return CMD_RET_FAILURE; |
Stefan Roese | 5c72127 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | if (argc == 2) |
| 96 | filename = argv[1]; |
| 97 | debug("Using filename %s\n", filename); |
| 98 | |
| 99 | ret = ubifs_ls(filename); |
Tim Harvey | 8d66edc | 2013-10-10 01:32:26 +0200 | [diff] [blame] | 100 | if (ret) { |
| 101 | printf("** File not found %s **\n", filename); |
| 102 | ret = CMD_RET_FAILURE; |
| 103 | } |
Stefan Roese | 5c72127 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 104 | |
| 105 | return ret; |
| 106 | } |
| 107 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 108 | static int do_ubifs_load(struct cmd_tbl *cmdtp, int flag, int argc, |
| 109 | char *const argv[]) |
Stefan Roese | 5c72127 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 110 | { |
| 111 | char *filename; |
Simon Kagstrom | 9dae38a | 2009-07-07 16:01:02 +0200 | [diff] [blame] | 112 | char *endp; |
Stefan Roese | 5c72127 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 113 | int ret; |
Ben Dooks | 68ebcf7 | 2023-06-06 09:23:28 +0100 | [diff] [blame] | 114 | unsigned long addr; |
Stefan Roese | 5c72127 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 115 | u32 size = 0; |
| 116 | |
| 117 | if (!ubifs_mounted) { |
| 118 | printf("UBIFS not mounted, use ubifs mount to mount volume first!\n"); |
Pali Rohár | 63d3d97 | 2022-08-07 21:58:56 +0200 | [diff] [blame] | 119 | return CMD_RET_FAILURE; |
Stefan Roese | 5c72127 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 120 | } |
| 121 | |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 122 | if (argc < 3) |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 123 | return CMD_RET_USAGE; |
Stefan Roese | 5c72127 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 124 | |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 125 | addr = hextoul(argv[1], &endp); |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 126 | if (endp == argv[1]) |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 127 | return CMD_RET_USAGE; |
Simon Kagstrom | 9dae38a | 2009-07-07 16:01:02 +0200 | [diff] [blame] | 128 | |
Stefan Roese | 5c72127 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 129 | filename = argv[2]; |
| 130 | |
Simon Kagstrom | 9dae38a | 2009-07-07 16:01:02 +0200 | [diff] [blame] | 131 | if (argc == 4) { |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 132 | size = hextoul(argv[3], &endp); |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 133 | if (endp == argv[3]) |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 134 | return CMD_RET_USAGE; |
Simon Kagstrom | 9dae38a | 2009-07-07 16:01:02 +0200 | [diff] [blame] | 135 | } |
Ben Dooks | 68ebcf7 | 2023-06-06 09:23:28 +0100 | [diff] [blame] | 136 | debug("Loading file '%s' to address 0x%08lx (size %d)\n", filename, addr, size); |
Stefan Roese | 5c72127 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 137 | |
| 138 | ret = ubifs_load(filename, addr, size); |
Tim Harvey | 8d66edc | 2013-10-10 01:32:26 +0200 | [diff] [blame] | 139 | if (ret) { |
| 140 | printf("** File not found %s **\n", filename); |
| 141 | ret = CMD_RET_FAILURE; |
| 142 | } |
Stefan Roese | 5c72127 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 143 | |
| 144 | return ret; |
| 145 | } |
| 146 | |
| 147 | U_BOOT_CMD( |
| 148 | ubifsmount, 2, 0, do_ubifs_mount, |
Mike Frysinger | 27e1e62 | 2009-03-23 22:27:34 -0400 | [diff] [blame] | 149 | "mount UBIFS volume", |
Simon Kagstrom | 9dae38a | 2009-07-07 16:01:02 +0200 | [diff] [blame] | 150 | "<volume-name>\n" |
| 151 | " - mount 'volume-name' volume" |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 152 | ); |
Stefan Roese | 5c72127 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 153 | |
Frans Meulenbroeks | 7675a09 | 2010-07-31 15:01:53 +0200 | [diff] [blame] | 154 | U_BOOT_CMD( |
Stefan Roese | ff98053 | 2010-10-28 14:09:22 +0200 | [diff] [blame] | 155 | ubifsumount, 1, 0, do_ubifs_umount, |
| 156 | "unmount UBIFS volume", |
| 157 | " - unmount current volume" |
| 158 | ); |
| 159 | |
| 160 | U_BOOT_CMD( |
Frans Meulenbroeks | 7675a09 | 2010-07-31 15:01:53 +0200 | [diff] [blame] | 161 | ubifsls, 2, 0, do_ubifs_ls, |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 162 | "list files in a directory", |
| 163 | "[directory]\n" |
| 164 | " - list files in a 'directory' (default '/')" |
| 165 | ); |
Stefan Roese | 5c72127 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 166 | |
Frans Meulenbroeks | 7675a09 | 2010-07-31 15:01:53 +0200 | [diff] [blame] | 167 | U_BOOT_CMD( |
| 168 | ubifsload, 4, 0, do_ubifs_load, |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 169 | "load file from an UBIFS filesystem", |
| 170 | "<addr> <filename> [bytes]\n" |
| 171 | " - load file 'filename' to address 'addr'" |
| 172 | ); |