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 | } |
| 41 | static int do_ubifs_mount(cmd_tbl_t *cmdtp, int flag, int argc, |
| 42 | char * const argv[]) |
| 43 | { |
| 44 | char *vol_name; |
| 45 | |
| 46 | if (argc != 2) |
| 47 | return CMD_RET_USAGE; |
| 48 | |
| 49 | vol_name = argv[1]; |
| 50 | |
| 51 | return cmd_ubifs_mount(vol_name); |
Stefan Roese | ce6d0c8 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 52 | } |
| 53 | |
Stefan Roese | 2f15cfd | 2010-11-01 17:28:22 +0100 | [diff] [blame] | 54 | int ubifs_is_mounted(void) |
| 55 | { |
| 56 | return ubifs_mounted; |
| 57 | } |
| 58 | |
Tien Fong Chee | 10c2044 | 2018-07-06 16:25:12 +0800 | [diff] [blame] | 59 | int cmd_ubifs_umount(void) |
Stefan Roese | 2f15cfd | 2010-11-01 17:28:22 +0100 | [diff] [blame] | 60 | { |
Tien Fong Chee | 10c2044 | 2018-07-06 16:25:12 +0800 | [diff] [blame] | 61 | if (ubifs_initialized == 0) { |
| 62 | printf("No UBIFS volume mounted!\n"); |
| 63 | return -1; |
| 64 | } |
| 65 | |
Hans de Goede | ad15749 | 2015-09-17 18:46:56 -0400 | [diff] [blame] | 66 | uboot_ubifs_umount(); |
Stefan Roese | 2f15cfd | 2010-11-01 17:28:22 +0100 | [diff] [blame] | 67 | ubifs_mounted = 0; |
| 68 | ubifs_initialized = 0; |
Tien Fong Chee | 10c2044 | 2018-07-06 16:25:12 +0800 | [diff] [blame] | 69 | |
| 70 | return 0; |
Stefan Roese | 2f15cfd | 2010-11-01 17:28:22 +0100 | [diff] [blame] | 71 | } |
| 72 | |
Jeroen Hofstee | 0e350f8 | 2014-06-23 00:22:08 +0200 | [diff] [blame] | 73 | static int do_ubifs_umount(cmd_tbl_t *cmdtp, int flag, int argc, |
| 74 | char * const argv[]) |
Stefan Roese | cb9c09d | 2010-10-28 14:09:22 +0200 | [diff] [blame] | 75 | { |
| 76 | if (argc != 1) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 77 | return CMD_RET_USAGE; |
Stefan Roese | cb9c09d | 2010-10-28 14:09:22 +0200 | [diff] [blame] | 78 | |
Tien Fong Chee | 10c2044 | 2018-07-06 16:25:12 +0800 | [diff] [blame] | 79 | return cmd_ubifs_umount(); |
Stefan Roese | cb9c09d | 2010-10-28 14:09:22 +0200 | [diff] [blame] | 80 | } |
| 81 | |
Jeroen Hofstee | 0e350f8 | 2014-06-23 00:22:08 +0200 | [diff] [blame] | 82 | static int do_ubifs_ls(cmd_tbl_t *cmdtp, int flag, int argc, |
| 83 | char * const argv[]) |
Stefan Roese | ce6d0c8 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 84 | { |
| 85 | char *filename = "/"; |
| 86 | int ret; |
| 87 | |
| 88 | if (!ubifs_mounted) { |
Stefan Roese | 9a2ea57 | 2010-10-28 14:09:29 +0200 | [diff] [blame] | 89 | printf("UBIFS not mounted, use ubifsmount to mount volume first!\n"); |
Stefan Roese | ce6d0c8 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 90 | return -1; |
| 91 | } |
| 92 | |
| 93 | if (argc == 2) |
| 94 | filename = argv[1]; |
| 95 | debug("Using filename %s\n", filename); |
| 96 | |
| 97 | ret = ubifs_ls(filename); |
Tim Harvey | 7cdebc3 | 2013-10-10 01:32:26 +0200 | [diff] [blame] | 98 | if (ret) { |
| 99 | printf("** File not found %s **\n", filename); |
| 100 | ret = CMD_RET_FAILURE; |
| 101 | } |
Stefan Roese | ce6d0c8 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 102 | |
| 103 | return ret; |
| 104 | } |
| 105 | |
Jeroen Hofstee | 0e350f8 | 2014-06-23 00:22:08 +0200 | [diff] [blame] | 106 | static int do_ubifs_load(cmd_tbl_t *cmdtp, int flag, int argc, |
| 107 | char * const argv[]) |
Stefan Roese | ce6d0c8 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 108 | { |
| 109 | char *filename; |
Simon Kagstrom | 2896b58 | 2009-07-07 16:01:02 +0200 | [diff] [blame] | 110 | char *endp; |
Stefan Roese | ce6d0c8 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 111 | int ret; |
| 112 | u32 addr; |
| 113 | u32 size = 0; |
| 114 | |
| 115 | if (!ubifs_mounted) { |
| 116 | printf("UBIFS not mounted, use ubifs mount to mount volume first!\n"); |
| 117 | return -1; |
| 118 | } |
| 119 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 120 | if (argc < 3) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 121 | return CMD_RET_USAGE; |
Stefan Roese | ce6d0c8 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 122 | |
Simon Kagstrom | 2896b58 | 2009-07-07 16:01:02 +0200 | [diff] [blame] | 123 | addr = simple_strtoul(argv[1], &endp, 16); |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 124 | if (endp == argv[1]) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 125 | return CMD_RET_USAGE; |
Simon Kagstrom | 2896b58 | 2009-07-07 16:01:02 +0200 | [diff] [blame] | 126 | |
Stefan Roese | ce6d0c8 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 127 | filename = argv[2]; |
| 128 | |
Simon Kagstrom | 2896b58 | 2009-07-07 16:01:02 +0200 | [diff] [blame] | 129 | if (argc == 4) { |
| 130 | size = simple_strtoul(argv[3], &endp, 16); |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 131 | if (endp == argv[3]) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 132 | return CMD_RET_USAGE; |
Simon Kagstrom | 2896b58 | 2009-07-07 16:01:02 +0200 | [diff] [blame] | 133 | } |
Stefan Roese | ce6d0c8 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 134 | debug("Loading file '%s' to address 0x%08x (size %d)\n", filename, addr, size); |
| 135 | |
| 136 | ret = ubifs_load(filename, addr, size); |
Tim Harvey | 7cdebc3 | 2013-10-10 01:32:26 +0200 | [diff] [blame] | 137 | if (ret) { |
| 138 | printf("** File not found %s **\n", filename); |
| 139 | ret = CMD_RET_FAILURE; |
| 140 | } |
Stefan Roese | ce6d0c8 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 141 | |
| 142 | return ret; |
| 143 | } |
| 144 | |
| 145 | U_BOOT_CMD( |
| 146 | ubifsmount, 2, 0, do_ubifs_mount, |
Mike Frysinger | 852dbfd | 2009-03-23 22:27:34 -0400 | [diff] [blame] | 147 | "mount UBIFS volume", |
Simon Kagstrom | 2896b58 | 2009-07-07 16:01:02 +0200 | [diff] [blame] | 148 | "<volume-name>\n" |
| 149 | " - mount 'volume-name' volume" |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 150 | ); |
Stefan Roese | ce6d0c8 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 151 | |
Frans Meulenbroeks | 388a29d | 2010-07-31 15:01:53 +0200 | [diff] [blame] | 152 | U_BOOT_CMD( |
Stefan Roese | cb9c09d | 2010-10-28 14:09:22 +0200 | [diff] [blame] | 153 | ubifsumount, 1, 0, do_ubifs_umount, |
| 154 | "unmount UBIFS volume", |
| 155 | " - unmount current volume" |
| 156 | ); |
| 157 | |
| 158 | U_BOOT_CMD( |
Frans Meulenbroeks | 388a29d | 2010-07-31 15:01:53 +0200 | [diff] [blame] | 159 | ubifsls, 2, 0, do_ubifs_ls, |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 160 | "list files in a directory", |
| 161 | "[directory]\n" |
| 162 | " - list files in a 'directory' (default '/')" |
| 163 | ); |
Stefan Roese | ce6d0c8 | 2009-03-19 15:35:50 +0100 | [diff] [blame] | 164 | |
Frans Meulenbroeks | 388a29d | 2010-07-31 15:01:53 +0200 | [diff] [blame] | 165 | U_BOOT_CMD( |
| 166 | ubifsload, 4, 0, do_ubifs_load, |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 167 | "load file from an UBIFS filesystem", |
| 168 | "<addr> <filename> [bytes]\n" |
| 169 | " - load file 'filename' to address 'addr'" |
| 170 | ); |