Denys Vlasenko | 2ab9403 | 2017-10-05 15:33:28 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Ported to busybox from mtd-utils. |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 3 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 4 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 5 | */ |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 6 | //config:config UBIATTACH |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 7 | //config: bool "ubiattach (4.2 kb)" |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 8 | //config: default y |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 9 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 10 | //config: Attach MTD device to an UBI device. |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 11 | //config: |
| 12 | //config:config UBIDETACH |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 13 | //config: bool "ubidetach (4.1 kb)" |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 14 | //config: default y |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 15 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 16 | //config: Detach MTD device from an UBI device. |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 17 | //config: |
| 18 | //config:config UBIMKVOL |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 19 | //config: bool "ubimkvol (5.3 kb)" |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 20 | //config: default y |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 21 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 22 | //config: Create a UBI volume. |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 23 | //config: |
| 24 | //config:config UBIRMVOL |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 25 | //config: bool "ubirmvol (4.9 kb)" |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 26 | //config: default y |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 27 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 28 | //config: Delete a UBI volume. |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 29 | //config: |
| 30 | //config:config UBIRSVOL |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 31 | //config: bool "ubirsvol (4.2 kb)" |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 32 | //config: default y |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 33 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 34 | //config: Resize a UBI volume. |
Reuben Dowle | a610842 | 2011-04-26 04:27:48 +0200 | [diff] [blame] | 35 | //config: |
| 36 | //config:config UBIUPDATEVOL |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 37 | //config: bool "ubiupdatevol (5.2 kb)" |
Reuben Dowle | a610842 | 2011-04-26 04:27:48 +0200 | [diff] [blame] | 38 | //config: default y |
Reuben Dowle | a610842 | 2011-04-26 04:27:48 +0200 | [diff] [blame] | 39 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 40 | //config: Update a UBI volume. |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 41 | |
Denys Vlasenko | 205d48e | 2017-01-29 14:57:33 +0100 | [diff] [blame] | 42 | // APPLET_ODDNAME:name main location suid_type help |
| 43 | //applet:IF_UBIATTACH( APPLET_ODDNAME(ubiattach, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubiattach)) |
| 44 | //applet:IF_UBIDETACH( APPLET_ODDNAME(ubidetach, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubidetach)) |
| 45 | //applet:IF_UBIMKVOL( APPLET_ODDNAME(ubimkvol, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubimkvol)) |
| 46 | //applet:IF_UBIRMVOL( APPLET_ODDNAME(ubirmvol, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubirmvol)) |
| 47 | //applet:IF_UBIRSVOL( APPLET_ODDNAME(ubirsvol, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubirsvol)) |
Reuben Dowle | a610842 | 2011-04-26 04:27:48 +0200 | [diff] [blame] | 48 | //applet:IF_UBIUPDATEVOL(APPLET_ODDNAME(ubiupdatevol, ubi_tools, BB_DIR_USR_SBIN, BB_SUID_DROP, ubiupdatevol)) |
Denys Vlasenko | 798b945 | 2017-08-07 16:00:25 +0200 | [diff] [blame] | 49 | /* not NOEXEC: if flash operation stalls, use less memory in "hung" process */ |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 50 | |
Denys Vlasenko | d1993f1 | 2011-04-26 04:32:05 +0200 | [diff] [blame] | 51 | //kbuild:lib-$(CONFIG_UBIATTACH) += ubi_tools.o |
| 52 | //kbuild:lib-$(CONFIG_UBIDETACH) += ubi_tools.o |
| 53 | //kbuild:lib-$(CONFIG_UBIMKVOL) += ubi_tools.o |
| 54 | //kbuild:lib-$(CONFIG_UBIRMVOL) += ubi_tools.o |
| 55 | //kbuild:lib-$(CONFIG_UBIRSVOL) += ubi_tools.o |
| 56 | //kbuild:lib-$(CONFIG_UBIUPDATEVOL) += ubi_tools.o |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 57 | |
| 58 | #include "libbb.h" |
Denys Vlasenko | 7fe1e3f | 2012-03-11 18:04:14 +0100 | [diff] [blame] | 59 | /* Some versions of kernel have broken headers, need this hack */ |
| 60 | #ifndef __packed |
| 61 | # define __packed __attribute__((packed)) |
| 62 | #endif |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 63 | #include <mtd/ubi-user.h> |
| 64 | |
Denys Vlasenko | 115e0a7 | 2017-08-07 02:55:33 +0200 | [diff] [blame] | 65 | #define UBI_APPLET_CNT (0 \ |
| 66 | + ENABLE_UBIATTACH \ |
| 67 | + ENABLE_UBIDETACH \ |
| 68 | + ENABLE_UBIMKVOL \ |
| 69 | + ENABLE_UBIRMVOL \ |
| 70 | + ENABLE_UBIRSVOL \ |
| 71 | + ENABLE_UBIUPDATEVOL \ |
| 72 | ) |
| 73 | |
| 74 | #define do_attach (ENABLE_UBIATTACH && (UBI_APPLET_CNT == 1 || applet_name[4] == 't')) |
| 75 | #define do_detach (ENABLE_UBIDETACH && (UBI_APPLET_CNT == 1 || applet_name[4] == 'e')) |
| 76 | #define do_mkvol (ENABLE_UBIMKVOL && (UBI_APPLET_CNT == 1 || applet_name[4] == 'k')) |
| 77 | #define do_rmvol (ENABLE_UBIRMVOL && (UBI_APPLET_CNT == 1 || applet_name[4] == 'm')) |
| 78 | #define do_rsvol (ENABLE_UBIRSVOL && (UBI_APPLET_CNT == 1 || applet_name[4] == 's')) |
| 79 | #define do_update (ENABLE_UBIUPDATEVOL && (UBI_APPLET_CNT == 1 || applet_name[4] == 'p')) |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 80 | |
Denys Vlasenko | 798b945 | 2017-08-07 16:00:25 +0200 | [diff] [blame] | 81 | static unsigned get_num_from_file(const char *path, unsigned max) |
Denys Vlasenko | 65bdf0b | 2013-06-30 02:38:18 +0200 | [diff] [blame] | 82 | { |
| 83 | char buf[sizeof(long long)*3]; |
| 84 | unsigned long long num; |
Denys Vlasenko | 0bfb9c2 | 2011-04-26 04:31:03 +0200 | [diff] [blame] | 85 | |
Denys Vlasenko | 65bdf0b | 2013-06-30 02:38:18 +0200 | [diff] [blame] | 86 | if (open_read_close(path, buf, sizeof(buf)) < 0) |
Denys Vlasenko | 798b945 | 2017-08-07 16:00:25 +0200 | [diff] [blame] | 87 | bb_perror_msg_and_die("can't open '%s'", path); |
Denys Vlasenko | 65bdf0b | 2013-06-30 02:38:18 +0200 | [diff] [blame] | 88 | /* It can be \n terminated, xatoull won't work well */ |
| 89 | if (sscanf(buf, "%llu", &num) != 1 || num > max) |
Denys Vlasenko | 798b945 | 2017-08-07 16:00:25 +0200 | [diff] [blame] | 90 | bb_error_msg_and_die("number in '%s' is malformed or too large", path); |
Denys Vlasenko | 65bdf0b | 2013-06-30 02:38:18 +0200 | [diff] [blame] | 91 | return num; |
| 92 | } |
| 93 | |
| 94 | /* To prevent malloc(1G) accidents */ |
| 95 | #define MAX_SANE_ERASEBLOCK (16*1024*1024) |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 96 | |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 97 | int ubi_tools_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 98 | int ubi_tools_main(int argc UNUSED_PARAM, char **argv) |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 99 | { |
Denys Vlasenko | 965b795 | 2020-11-30 13:03:03 +0100 | [diff] [blame] | 100 | static const struct suffix_mult size_suffixes[] ALIGN_SUFFIX = { |
Denys Vlasenko | e9b3fcc | 2013-06-30 02:47:45 +0200 | [diff] [blame] | 101 | { "KiB", 1024 }, |
| 102 | { "MiB", 1024*1024 }, |
| 103 | { "GiB", 1024*1024*1024 }, |
| 104 | { "", 0 } |
| 105 | }; |
| 106 | |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 107 | unsigned opts; |
| 108 | char *ubi_ctrl; |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 109 | int fd; |
| 110 | int mtd_num; |
| 111 | int dev_num = UBI_DEV_NUM_AUTO; |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 112 | int vol_id = UBI_VOL_NUM_AUTO; |
Micke Prag | 07fa09a | 2014-11-02 11:08:24 +0100 | [diff] [blame] | 113 | int vid_hdr_offset = 0; |
Denys Vlasenko | 65bdf0b | 2013-06-30 02:38:18 +0200 | [diff] [blame] | 114 | char *vol_name; |
| 115 | unsigned long long size_bytes = size_bytes; /* for compiler */ |
| 116 | char *size_bytes_str; |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 117 | int alignment = 1; |
Denys Vlasenko | 65bdf0b | 2013-06-30 02:38:18 +0200 | [diff] [blame] | 118 | char *type; |
| 119 | union { |
| 120 | struct ubi_attach_req attach_req; |
| 121 | struct ubi_mkvol_req mkvol_req; |
| 122 | struct ubi_rsvol_req rsvol_req; |
| 123 | } req_structs; |
| 124 | #define attach_req req_structs.attach_req |
| 125 | #define mkvol_req req_structs.mkvol_req |
| 126 | #define rsvol_req req_structs.rsvol_req |
| 127 | char path[sizeof("/sys/class/ubi/ubi%d_%d/usable_eb_size") |
| 128 | + 2 * sizeof(int)*3 + /*just in case:*/ 16]; |
| 129 | #define path_sys_class_ubi_ubi (path + sizeof("/sys/class/ubi/ubi")-1) |
| 130 | |
Paul B. Henson | 985345d | 2013-08-04 21:07:20 +0200 | [diff] [blame] | 131 | strcpy(path, "/sys/class/ubi/ubi"); |
Denys Vlasenko | 65bdf0b | 2013-06-30 02:38:18 +0200 | [diff] [blame] | 132 | memset(&req_structs, 0, sizeof(req_structs)); |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 133 | |
Denys Vlasenko | 65bdf0b | 2013-06-30 02:38:18 +0200 | [diff] [blame] | 134 | #define OPTION_m (1 << 0) |
| 135 | #define OPTION_d (1 << 1) |
| 136 | #define OPTION_n (1 << 2) |
| 137 | #define OPTION_N (1 << 3) |
| 138 | #define OPTION_s (1 << 4) |
| 139 | #define OPTION_a (1 << 5) |
| 140 | #define OPTION_t (1 << 6) |
Denys Vlasenko | 186b98a | 2014-09-25 22:10:32 +0200 | [diff] [blame] | 141 | if (do_mkvol) { |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 142 | opts = getopt32(argv, "^" "md:+n:+N:s:a:+t:O:+" "\0" "-1", |
Denys Vlasenko | 186b98a | 2014-09-25 22:10:32 +0200 | [diff] [blame] | 143 | &dev_num, &vol_id, |
Micke Prag | 07fa09a | 2014-11-02 11:08:24 +0100 | [diff] [blame] | 144 | &vol_name, &size_bytes_str, &alignment, &type, |
| 145 | &vid_hdr_offset |
Denys Vlasenko | 186b98a | 2014-09-25 22:10:32 +0200 | [diff] [blame] | 146 | ); |
| 147 | } else |
| 148 | if (do_update) { |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 149 | opts = getopt32(argv, "^" "s:at" "\0" "-1", &size_bytes_str); |
Denys Vlasenko | 186b98a | 2014-09-25 22:10:32 +0200 | [diff] [blame] | 150 | opts *= OPTION_s; |
| 151 | } else { |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 152 | opts = getopt32(argv, "^" "m:+d:+n:+N:s:a:+t:" "\0" "-1", |
Denys Vlasenko | 186b98a | 2014-09-25 22:10:32 +0200 | [diff] [blame] | 153 | &mtd_num, &dev_num, &vol_id, |
| 154 | &vol_name, &size_bytes_str, &alignment, &type |
| 155 | ); |
| 156 | } |
Denys Vlasenko | 65bdf0b | 2013-06-30 02:38:18 +0200 | [diff] [blame] | 157 | |
| 158 | if (opts & OPTION_s) |
Denys Vlasenko | e9b3fcc | 2013-06-30 02:47:45 +0200 | [diff] [blame] | 159 | size_bytes = xatoull_sfx(size_bytes_str, size_suffixes); |
Denys Vlasenko | 65bdf0b | 2013-06-30 02:38:18 +0200 | [diff] [blame] | 160 | argv += optind; |
Denys Vlasenko | f798ba9 | 2013-06-30 02:46:44 +0200 | [diff] [blame] | 161 | ubi_ctrl = *argv++; |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 162 | |
| 163 | fd = xopen(ubi_ctrl, O_RDWR); |
Denys Vlasenko | 8d3e225 | 2010-08-31 12:42:06 +0200 | [diff] [blame] | 164 | //xfstat(fd, &st, ubi_ctrl); |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 165 | //if (!S_ISCHR(st.st_mode)) |
Denys Vlasenko | 8d3e225 | 2010-08-31 12:42:06 +0200 | [diff] [blame] | 166 | // bb_error_msg_and_die("%s: not a char device", ubi_ctrl); |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 167 | |
Denys Vlasenko | 65bdf0b | 2013-06-30 02:38:18 +0200 | [diff] [blame] | 168 | //usage:#define ubiattach_trivial_usage |
Micke Prag | 07fa09a | 2014-11-02 11:08:24 +0100 | [diff] [blame] | 169 | //usage: "-m MTD_NUM [-d UBI_NUM] [-O VID_HDR_OFF] UBI_CTRL_DEV" |
Denys Vlasenko | 65bdf0b | 2013-06-30 02:38:18 +0200 | [diff] [blame] | 170 | //usage:#define ubiattach_full_usage "\n\n" |
| 171 | //usage: "Attach MTD device to UBI\n" |
| 172 | //usage: "\n -m MTD_NUM MTD device number to attach" |
| 173 | //usage: "\n -d UBI_NUM UBI device number to assign" |
Micke Prag | 07fa09a | 2014-11-02 11:08:24 +0100 | [diff] [blame] | 174 | //usage: "\n -O VID_HDR_OFF VID header offset" |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 175 | if (do_attach) { |
Denys Vlasenko | 65bdf0b | 2013-06-30 02:38:18 +0200 | [diff] [blame] | 176 | if (!(opts & OPTION_m)) |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 177 | bb_error_msg_and_die("%s device not specified", "MTD"); |
| 178 | |
Denys Vlasenko | 65bdf0b | 2013-06-30 02:38:18 +0200 | [diff] [blame] | 179 | attach_req.mtd_num = mtd_num; |
| 180 | attach_req.ubi_num = dev_num; |
Micke Prag | 07fa09a | 2014-11-02 11:08:24 +0100 | [diff] [blame] | 181 | attach_req.vid_hdr_offset = vid_hdr_offset; |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 182 | |
Denys Vlasenko | 65bdf0b | 2013-06-30 02:38:18 +0200 | [diff] [blame] | 183 | xioctl(fd, UBI_IOCATT, &attach_req); |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 184 | } else |
Denys Vlasenko | 65bdf0b | 2013-06-30 02:38:18 +0200 | [diff] [blame] | 185 | |
| 186 | //usage:#define ubidetach_trivial_usage |
| 187 | //usage: "-d UBI_NUM UBI_CTRL_DEV" |
| 188 | //usage:#define ubidetach_full_usage "\n\n" |
| 189 | //usage: "Detach MTD device from UBI\n" |
| 190 | //usage: "\n -d UBI_NUM UBI device number" |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 191 | if (do_detach) { |
Denys Vlasenko | 65bdf0b | 2013-06-30 02:38:18 +0200 | [diff] [blame] | 192 | if (!(opts & OPTION_d)) |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 193 | bb_error_msg_and_die("%s device not specified", "UBI"); |
| 194 | |
Denys Vlasenko | 65bdf0b | 2013-06-30 02:38:18 +0200 | [diff] [blame] | 195 | /* FIXME? kernel expects int32_t* here: */ |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 196 | xioctl(fd, UBI_IOCDET, &dev_num); |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 197 | } else |
Denys Vlasenko | 65bdf0b | 2013-06-30 02:38:18 +0200 | [diff] [blame] | 198 | |
| 199 | //usage:#define ubimkvol_trivial_usage |
Sven Eisenberg | b068cf2 | 2016-04-03 21:53:12 +0200 | [diff] [blame] | 200 | //usage: "-N NAME [-s SIZE | -m] UBI_DEVICE" |
Denys Vlasenko | 65bdf0b | 2013-06-30 02:38:18 +0200 | [diff] [blame] | 201 | //usage:#define ubimkvol_full_usage "\n\n" |
| 202 | //usage: "Create UBI volume\n" |
| 203 | //usage: "\n -a ALIGNMENT Volume alignment (default 1)" |
| 204 | //usage: "\n -m Set volume size to maximum available" |
| 205 | //usage: "\n -n VOLID Volume ID. If not specified," |
| 206 | //usage: "\n assigned automatically" |
| 207 | //usage: "\n -N NAME Volume name" |
| 208 | //usage: "\n -s SIZE Size in bytes" |
| 209 | //usage: "\n -t TYPE Volume type (static|dynamic)" |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 210 | if (do_mkvol) { |
Denys Vlasenko | 65bdf0b | 2013-06-30 02:38:18 +0200 | [diff] [blame] | 211 | if (opts & OPTION_m) { |
Paul B. Henson | f86404f | 2013-06-02 17:48:05 -0700 | [diff] [blame] | 212 | unsigned leb_avail; |
| 213 | unsigned leb_size; |
| 214 | unsigned num; |
Paul B. Henson | f86404f | 2013-06-02 17:48:05 -0700 | [diff] [blame] | 215 | char *p; |
Paul B. Henson | f86404f | 2013-06-02 17:48:05 -0700 | [diff] [blame] | 216 | |
Sven Eisenberg | b068cf2 | 2016-04-03 21:53:12 +0200 | [diff] [blame] | 217 | num = ubi_devnum_from_devname(ubi_ctrl); |
Denys Vlasenko | 65bdf0b | 2013-06-30 02:38:18 +0200 | [diff] [blame] | 218 | p = path_sys_class_ubi_ubi + sprintf(path_sys_class_ubi_ubi, "%u/", num); |
Paul B. Henson | f86404f | 2013-06-02 17:48:05 -0700 | [diff] [blame] | 219 | |
| 220 | strcpy(p, "avail_eraseblocks"); |
Denys Vlasenko | 798b945 | 2017-08-07 16:00:25 +0200 | [diff] [blame] | 221 | leb_avail = get_num_from_file(path, UINT_MAX); |
Paul B. Henson | f86404f | 2013-06-02 17:48:05 -0700 | [diff] [blame] | 222 | |
| 223 | strcpy(p, "eraseblock_size"); |
Denys Vlasenko | 798b945 | 2017-08-07 16:00:25 +0200 | [diff] [blame] | 224 | leb_size = get_num_from_file(path, MAX_SANE_ERASEBLOCK); |
Paul B. Henson | f86404f | 2013-06-02 17:48:05 -0700 | [diff] [blame] | 225 | |
Denys Vlasenko | 65bdf0b | 2013-06-30 02:38:18 +0200 | [diff] [blame] | 226 | size_bytes = leb_avail * (unsigned long long)leb_size; |
| 227 | //if (size_bytes <= 0) |
| 228 | // bb_error_msg_and_die("%s invalid maximum size calculated", "UBI"); |
Paul B. Henson | f86404f | 2013-06-02 17:48:05 -0700 | [diff] [blame] | 229 | } else |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 230 | if (!(opts & OPTION_s)) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 231 | bb_simple_error_msg_and_die("size not specified"); |
Denys Vlasenko | 65bdf0b | 2013-06-30 02:38:18 +0200 | [diff] [blame] | 232 | |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 233 | if (!(opts & OPTION_N)) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 234 | bb_simple_error_msg_and_die("name not specified"); |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 235 | |
Denys Vlasenko | 798b945 | 2017-08-07 16:00:25 +0200 | [diff] [blame] | 236 | /* the structure is memset(0) above */ |
Denys Vlasenko | 65bdf0b | 2013-06-30 02:38:18 +0200 | [diff] [blame] | 237 | mkvol_req.vol_id = vol_id; |
| 238 | mkvol_req.vol_type = UBI_DYNAMIC_VOLUME; |
| 239 | if ((opts & OPTION_t) && type[0] == 's') |
| 240 | mkvol_req.vol_type = UBI_STATIC_VOLUME; |
| 241 | mkvol_req.alignment = alignment; |
| 242 | mkvol_req.bytes = size_bytes; /* signed int64_t */ |
Denys Vlasenko | 798b945 | 2017-08-07 16:00:25 +0200 | [diff] [blame] | 243 | /* strnlen avoids overflow of 16-bit field (paranoia) */ |
| 244 | mkvol_req.name_len = strnlen(vol_name, UBI_MAX_VOLUME_NAME+1); |
Denys Vlasenko | 65bdf0b | 2013-06-30 02:38:18 +0200 | [diff] [blame] | 245 | if (mkvol_req.name_len > UBI_MAX_VOLUME_NAME) |
| 246 | bb_error_msg_and_die("volume name too long: '%s'", vol_name); |
Denys Vlasenko | 798b945 | 2017-08-07 16:00:25 +0200 | [diff] [blame] | 247 | /* this is safe: .name[] is UBI_MAX_VOLUME_NAME+1 bytes */ |
| 248 | strcpy(mkvol_req.name, vol_name); |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 249 | |
Denys Vlasenko | 65bdf0b | 2013-06-30 02:38:18 +0200 | [diff] [blame] | 250 | xioctl(fd, UBI_IOCMKVOL, &mkvol_req); |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 251 | } else |
Denys Vlasenko | 65bdf0b | 2013-06-30 02:38:18 +0200 | [diff] [blame] | 252 | |
| 253 | //usage:#define ubirmvol_trivial_usage |
Denys Vlasenko | 1f60d88 | 2021-06-15 10:00:18 +0200 | [diff] [blame] | 254 | //usage: "-n VOLID | -N VOLNAME UBI_DEVICE" |
Denys Vlasenko | 65bdf0b | 2013-06-30 02:38:18 +0200 | [diff] [blame] | 255 | //usage:#define ubirmvol_full_usage "\n\n" |
| 256 | //usage: "Remove UBI volume\n" |
| 257 | //usage: "\n -n VOLID Volume ID" |
Sven Eisenberg | b068cf2 | 2016-04-03 21:53:12 +0200 | [diff] [blame] | 258 | //usage: "\n -N VOLNAME Volume name" |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 259 | if (do_rmvol) { |
Sven Eisenberg | b068cf2 | 2016-04-03 21:53:12 +0200 | [diff] [blame] | 260 | if (!(opts & (OPTION_n|OPTION_N))) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 261 | bb_simple_error_msg_and_die("volume id not specified"); |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 262 | |
Sven Eisenberg | b068cf2 | 2016-04-03 21:53:12 +0200 | [diff] [blame] | 263 | if (opts & OPTION_N) { |
| 264 | unsigned num = ubi_devnum_from_devname(ubi_ctrl); |
Denys Vlasenko | 6aab992 | 2016-04-03 22:24:51 +0200 | [diff] [blame] | 265 | vol_id = ubi_get_volid_by_name(num, vol_name); |
Sven Eisenberg | b068cf2 | 2016-04-03 21:53:12 +0200 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | if (sizeof(vol_id) != 4) { |
| 269 | /* kernel expects int32_t* in this ioctl */ |
| 270 | int32_t t = vol_id; |
| 271 | xioctl(fd, UBI_IOCRMVOL, &t); |
| 272 | } else { |
| 273 | xioctl(fd, UBI_IOCRMVOL, &vol_id); |
| 274 | } |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 275 | } else |
Denys Vlasenko | 65bdf0b | 2013-06-30 02:38:18 +0200 | [diff] [blame] | 276 | |
| 277 | //usage:#define ubirsvol_trivial_usage |
Sven Eisenberg | b068cf2 | 2016-04-03 21:53:12 +0200 | [diff] [blame] | 278 | //usage: "-n VOLID -s SIZE UBI_DEVICE" |
Denys Vlasenko | 65bdf0b | 2013-06-30 02:38:18 +0200 | [diff] [blame] | 279 | //usage:#define ubirsvol_full_usage "\n\n" |
| 280 | //usage: "Resize UBI volume\n" |
| 281 | //usage: "\n -n VOLID Volume ID" |
| 282 | //usage: "\n -s SIZE Size in bytes" |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 283 | if (do_rsvol) { |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 284 | if (!(opts & OPTION_s)) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 285 | bb_simple_error_msg_and_die("size not specified"); |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 286 | if (!(opts & OPTION_n)) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 287 | bb_simple_error_msg_and_die("volume id not specified"); |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 288 | |
Denys Vlasenko | 65bdf0b | 2013-06-30 02:38:18 +0200 | [diff] [blame] | 289 | rsvol_req.bytes = size_bytes; /* signed int64_t */ |
| 290 | rsvol_req.vol_id = vol_id; |
Reuben Dowle | 3e2834f | 2011-02-05 03:18:08 +0100 | [diff] [blame] | 291 | |
Denys Vlasenko | 65bdf0b | 2013-06-30 02:38:18 +0200 | [diff] [blame] | 292 | xioctl(fd, UBI_IOCRSVOL, &rsvol_req); |
Reuben Dowle | a610842 | 2011-04-26 04:27:48 +0200 | [diff] [blame] | 293 | } else |
Denys Vlasenko | 65bdf0b | 2013-06-30 02:38:18 +0200 | [diff] [blame] | 294 | |
| 295 | //usage:#define ubiupdatevol_trivial_usage |
Denys Vlasenko | b34eb4a | 2017-08-07 03:24:29 +0200 | [diff] [blame] | 296 | //usage: "-t UBI_DEVICE | [-s SIZE] UBI_DEVICE IMG_FILE" |
Denys Vlasenko | 65bdf0b | 2013-06-30 02:38:18 +0200 | [diff] [blame] | 297 | //usage:#define ubiupdatevol_full_usage "\n\n" |
| 298 | //usage: "Update UBI volume\n" |
| 299 | //usage: "\n -t Truncate to zero size" |
| 300 | //usage: "\n -s SIZE Size in bytes to resize to" |
Reuben Dowle | a610842 | 2011-04-26 04:27:48 +0200 | [diff] [blame] | 301 | if (do_update) { |
Denys Vlasenko | 65bdf0b | 2013-06-30 02:38:18 +0200 | [diff] [blame] | 302 | int64_t bytes64; |
Reuben Dowle | a610842 | 2011-04-26 04:27:48 +0200 | [diff] [blame] | 303 | |
| 304 | if (opts & OPTION_t) { |
Denys Vlasenko | 65bdf0b | 2013-06-30 02:38:18 +0200 | [diff] [blame] | 305 | /* truncate the volume by starting an update for size 0 */ |
| 306 | bytes64 = 0; |
| 307 | /* this ioctl expects int64_t* parameter */ |
| 308 | xioctl(fd, UBI_IOCVOLUP, &bytes64); |
Reuben Dowle | a610842 | 2011-04-26 04:27:48 +0200 | [diff] [blame] | 309 | } |
| 310 | else { |
Reuben Dowle | a610842 | 2011-04-26 04:27:48 +0200 | [diff] [blame] | 311 | unsigned ubinum, volnum; |
| 312 | unsigned leb_size; |
Denys Vlasenko | 798b945 | 2017-08-07 16:00:25 +0200 | [diff] [blame] | 313 | char *buf; |
Reuben Dowle | a610842 | 2011-04-26 04:27:48 +0200 | [diff] [blame] | 314 | |
Denys Vlasenko | 65bdf0b | 2013-06-30 02:38:18 +0200 | [diff] [blame] | 315 | /* Assume that device is in normal format. */ |
| 316 | /* Removes need for scanning sysfs tree as full libubi does. */ |
Reuben Dowle | a610842 | 2011-04-26 04:27:48 +0200 | [diff] [blame] | 317 | if (sscanf(ubi_ctrl, "/dev/ubi%u_%u", &ubinum, &volnum) != 2) |
Denys Vlasenko | 798b945 | 2017-08-07 16:00:25 +0200 | [diff] [blame] | 318 | bb_error_msg_and_die("UBI device name '%s' is not /dev/ubiN_M", ubi_ctrl); |
Reuben Dowle | a610842 | 2011-04-26 04:27:48 +0200 | [diff] [blame] | 319 | |
Denys Vlasenko | 65bdf0b | 2013-06-30 02:38:18 +0200 | [diff] [blame] | 320 | sprintf(path_sys_class_ubi_ubi, "%u_%u/usable_eb_size", ubinum, volnum); |
Denys Vlasenko | 798b945 | 2017-08-07 16:00:25 +0200 | [diff] [blame] | 321 | leb_size = get_num_from_file(path, MAX_SANE_ERASEBLOCK); |
Reuben Dowle | a610842 | 2011-04-26 04:27:48 +0200 | [diff] [blame] | 322 | |
Denys Vlasenko | 798b945 | 2017-08-07 16:00:25 +0200 | [diff] [blame] | 323 | if (!*argv) |
| 324 | bb_show_usage(); |
| 325 | if (NOT_LONE_DASH(*argv)) /* mtd-utils supports "-" as stdin */ |
Denys Vlasenko | 65bdf0b | 2013-06-30 02:38:18 +0200 | [diff] [blame] | 326 | xmove_fd(xopen(*argv, O_RDONLY), STDIN_FILENO); |
Denys Vlasenko | 798b945 | 2017-08-07 16:00:25 +0200 | [diff] [blame] | 327 | |
| 328 | if (!(opts & OPTION_s)) { |
| 329 | struct stat st; |
| 330 | xfstat(STDIN_FILENO, &st, *argv); |
| 331 | size_bytes = st.st_size; |
Reuben Dowle | a610842 | 2011-04-26 04:27:48 +0200 | [diff] [blame] | 332 | } |
| 333 | |
Denys Vlasenko | 65bdf0b | 2013-06-30 02:38:18 +0200 | [diff] [blame] | 334 | bytes64 = size_bytes; |
| 335 | /* this ioctl expects signed int64_t* parameter */ |
| 336 | xioctl(fd, UBI_IOCVOLUP, &bytes64); |
Reuben Dowle | a610842 | 2011-04-26 04:27:48 +0200 | [diff] [blame] | 337 | |
Denys Vlasenko | 798b945 | 2017-08-07 16:00:25 +0200 | [diff] [blame] | 338 | /* can't use bb_copyfd_exact_size(): copy in blocks of exactly leb_size */ |
| 339 | buf = xmalloc(leb_size); |
| 340 | while (size_bytes != 0) { |
| 341 | int len = full_read(STDIN_FILENO, buf, leb_size); |
| 342 | if (len <= 0) { |
| 343 | if (len < 0) |
| 344 | bb_perror_msg_and_die("read error from '%s'", *argv); |
| 345 | break; |
| 346 | } |
| 347 | if ((unsigned)len > size_bytes) { |
| 348 | /* for this case: "ubiupdatevol -s 1024000 $UBIDEV /dev/urandom" */ |
| 349 | len = size_bytes; |
| 350 | } |
| 351 | xwrite(fd, buf, len); |
| 352 | size_bytes -= len; |
Reuben Dowle | a610842 | 2011-04-26 04:27:48 +0200 | [diff] [blame] | 353 | } |
Denys Vlasenko | 798b945 | 2017-08-07 16:00:25 +0200 | [diff] [blame] | 354 | if (ENABLE_FEATURE_CLEAN_UP) |
| 355 | free(buf); |
Reuben Dowle | a610842 | 2011-04-26 04:27:48 +0200 | [diff] [blame] | 356 | } |
Baruch Siach | 3324c96 | 2010-06-16 12:22:58 +0200 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | if (ENABLE_FEATURE_CLEAN_UP) |
| 360 | close(fd); |
| 361 | |
| 362 | return EXIT_SUCCESS; |
| 363 | } |