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