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