Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Stefan Roese | ce6d0c8 | 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 | ce6d0c8 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 5 | */ |
| 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 Goede | ad15749 | 2015-09-17 18:46:56 -0400 | [diff] [blame] | 17 | #include <ubifs_uboot.h> |
Stefan Roese | cb9c09d | 2010-10-28 14:09:22 +0200 | [diff] [blame] | 18 | |
Stefan Roese | ce6d0c8 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 19 | static int ubifs_initialized; |
| 20 | static int ubifs_mounted; |
| 21 | |
Tien Fong Chee | 14dfc64 | 2018-07-06 16:26:01 +0800 | [diff] [blame] | 22 | int cmd_ubifs_mount(char *vol_name) |
Stefan Roese | ce6d0c8 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 23 | { |
Stefan Roese | ce6d0c8 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 24 | int ret; |
| 25 | |
Stefan Roese | ce6d0c8 | 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 | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 33 | ret = uboot_ubifs_mount(vol_name); |
Stefan Roese | ce6d0c8 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 34 | if (ret) |
| 35 | return -1; |
| 36 | |
| 37 | ubifs_mounted = 1; |
| 38 | |
Tien Fong Chee | 14dfc64 | 2018-07-06 16:26:01 +0800 | [diff] [blame] | 39 | return ret; |
| 40 | } |
Simon Glass | 0914011 | 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 | 14dfc64 | 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 | ce6d0c8 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 53 | } |
| 54 | |
Stefan Roese | 2f15cfd | 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 | 10c2044 | 2018-07-06 16:25:12 +0800 | [diff] [blame] | 60 | int cmd_ubifs_umount(void) |
Stefan Roese | 2f15cfd | 2010-11-01 17:28:22 +0100 | [diff] [blame] | 61 | { |
Tien Fong Chee | 10c2044 | 2018-07-06 16:25:12 +0800 | [diff] [blame] | 62 | if (ubifs_initialized == 0) { |
| 63 | printf("No UBIFS volume mounted!\n"); |
| 64 | return -1; |
| 65 | } |
| 66 | |
Hans de Goede | ad15749 | 2015-09-17 18:46:56 -0400 | [diff] [blame] | 67 | uboot_ubifs_umount(); |
Stefan Roese | 2f15cfd | 2010-11-01 17:28:22 +0100 | [diff] [blame] | 68 | ubifs_mounted = 0; |
| 69 | ubifs_initialized = 0; |
Tien Fong Chee | 10c2044 | 2018-07-06 16:25:12 +0800 | [diff] [blame] | 70 | |
| 71 | return 0; |
Stefan Roese | 2f15cfd | 2010-11-01 17:28:22 +0100 | [diff] [blame] | 72 | } |
| 73 | |
Simon Glass | 0914011 | 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 | cb9c09d | 2010-10-28 14:09:22 +0200 | [diff] [blame] | 76 | { |
| 77 | if (argc != 1) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 78 | return CMD_RET_USAGE; |
Stefan Roese | cb9c09d | 2010-10-28 14:09:22 +0200 | [diff] [blame] | 79 | |
Tien Fong Chee | 10c2044 | 2018-07-06 16:25:12 +0800 | [diff] [blame] | 80 | return cmd_ubifs_umount(); |
Stefan Roese | cb9c09d | 2010-10-28 14:09:22 +0200 | [diff] [blame] | 81 | } |
| 82 | |
Simon Glass | 0914011 | 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 | ce6d0c8 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 85 | { |
| 86 | char *filename = "/"; |
| 87 | int ret; |
| 88 | |
| 89 | if (!ubifs_mounted) { |
Stefan Roese | 9a2ea57 | 2010-10-28 14:09:29 +0200 | [diff] [blame] | 90 | printf("UBIFS not mounted, use ubifsmount to mount volume first!\n"); |
Stefan Roese | ce6d0c8 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 91 | return -1; |
| 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 | 7cdebc3 | 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 | ce6d0c8 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 103 | |
| 104 | return ret; |
| 105 | } |
| 106 | |
Simon Glass | 0914011 | 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 | ce6d0c8 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 109 | { |
| 110 | char *filename; |
Simon Kagstrom | 2896b58 | 2009-07-07 16:01:02 +0200 | [diff] [blame] | 111 | char *endp; |
Stefan Roese | ce6d0c8 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 112 | int ret; |
| 113 | u32 addr; |
| 114 | u32 size = 0; |
| 115 | |
| 116 | if (!ubifs_mounted) { |
| 117 | printf("UBIFS not mounted, use ubifs mount to mount volume first!\n"); |
| 118 | return -1; |
| 119 | } |
| 120 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 121 | if (argc < 3) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 122 | return CMD_RET_USAGE; |
Stefan Roese | ce6d0c8 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 123 | |
Simon Kagstrom | 2896b58 | 2009-07-07 16:01:02 +0200 | [diff] [blame] | 124 | addr = simple_strtoul(argv[1], &endp, 16); |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 125 | if (endp == argv[1]) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 126 | return CMD_RET_USAGE; |
Simon Kagstrom | 2896b58 | 2009-07-07 16:01:02 +0200 | [diff] [blame] | 127 | |
Stefan Roese | ce6d0c8 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 128 | filename = argv[2]; |
| 129 | |
Simon Kagstrom | 2896b58 | 2009-07-07 16:01:02 +0200 | [diff] [blame] | 130 | if (argc == 4) { |
| 131 | size = simple_strtoul(argv[3], &endp, 16); |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 132 | if (endp == argv[3]) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 133 | return CMD_RET_USAGE; |
Simon Kagstrom | 2896b58 | 2009-07-07 16:01:02 +0200 | [diff] [blame] | 134 | } |
Stefan Roese | ce6d0c8 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 135 | debug("Loading file '%s' to address 0x%08x (size %d)\n", filename, addr, size); |
| 136 | |
| 137 | ret = ubifs_load(filename, addr, size); |
Tim Harvey | 7cdebc3 | 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 | ce6d0c8 | 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 | 852dbfd | 2009-03-23 22:27:34 -0400 | [diff] [blame] | 148 | "mount UBIFS volume", |
Simon Kagstrom | 2896b58 | 2009-07-07 16:01:02 +0200 | [diff] [blame] | 149 | "<volume-name>\n" |
| 150 | " - mount 'volume-name' volume" |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 151 | ); |
Stefan Roese | ce6d0c8 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 152 | |
Frans Meulenbroeks | 388a29d | 2010-07-31 15:01:53 +0200 | [diff] [blame] | 153 | U_BOOT_CMD( |
Stefan Roese | cb9c09d | 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 | 388a29d | 2010-07-31 15:01:53 +0200 | [diff] [blame] | 160 | ubifsls, 2, 0, do_ubifs_ls, |
Wolfgang Denk | a89c33d | 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 | ce6d0c8 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 165 | |
Frans Meulenbroeks | 388a29d | 2010-07-31 15:01:53 +0200 | [diff] [blame] | 166 | U_BOOT_CMD( |
| 167 | ubifsload, 4, 0, do_ubifs_load, |
Wolfgang Denk | a89c33d | 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 | ); |