ubiupdatevol.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * Copyright (c) International Business Machines Corp., 2006
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  12. * the GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. /*
  19. * An utility to update UBI volumes.
  20. *
  21. * Authors: Frank Haverkamp
  22. * Joshua W. Boyer
  23. * Artem Bityutskiy
  24. */
  25. #define PROGRAM_NAME "ubiupdatevol"
  26. #include <fcntl.h>
  27. #include <stdio.h>
  28. #include <stdint.h>
  29. #include <getopt.h>
  30. #include <stdarg.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <unistd.h>
  34. #include <sys/stat.h>
  35. #include <libubi.h>
  36. #include "common.h"
  37. struct args {
  38. int truncate;
  39. const char *node;
  40. const char *img;
  41. /* For deprecated -d and -B options handling */
  42. char dev_name[256];
  43. long long size;
  44. long long skip;
  45. int use_stdin;
  46. };
  47. static struct args args;
  48. static const char doc[] = PROGRAM_NAME " version " VERSION
  49. " - a tool to write data to UBI volumes.";
  50. static const char optionsstr[] =
  51. "-t, --truncate truncate volume (wipe it out)\n"
  52. "-s, --size=<bytes> bytes to read from input\n"
  53. " --skip=<bytes> leading bytes to skip from input\n"
  54. "-h, --help print help message\n"
  55. "-V, --version print program version";
  56. static const char usage[] =
  57. "Usage: " PROGRAM_NAME " <UBI volume node file name> [-t] [-s <size>] [-h] [-V] [--truncate]\n"
  58. "\t\t\t[--size=<size>] [--help] [--version] <image file>\n\n"
  59. "Example 1: " PROGRAM_NAME " /dev/ubi0_1 fs.img - write file \"fs.img\" to UBI volume /dev/ubi0_1\n"
  60. "Example 2: " PROGRAM_NAME " /dev/ubi0_1 -t - wipe out UBI volume /dev/ubi0_1";
  61. static const struct option long_options[] = {
  62. /* Order matters for opts w/val=0; see option_index below. */
  63. { .name = "skip", .has_arg = 1, .flag = NULL, .val = 0 },
  64. { .name = "truncate", .has_arg = 0, .flag = NULL, .val = 't' },
  65. { .name = "help", .has_arg = 0, .flag = NULL, .val = 'h' },
  66. { .name = "version", .has_arg = 0, .flag = NULL, .val = 'V' },
  67. { .name = "size", .has_arg = 1, .flag = NULL, .val = 's' },
  68. { NULL, 0, NULL, 0}
  69. };
  70. static int parse_opt(int argc, char * const argv[])
  71. {
  72. while (1) {
  73. int option_index, key, error = 0;
  74. key = getopt_long(argc, argv, "ts:h?V", long_options, &option_index);
  75. if (key == -1)
  76. break;
  77. switch (key) {
  78. case 0:
  79. switch (option_index) {
  80. case 0: /* --skip */
  81. args.skip = simple_strtoull(optarg, &error);
  82. if (error || args.skip < 0)
  83. return errmsg("bad skip: " "\"%s\"", optarg);
  84. break;
  85. }
  86. break;
  87. case 't':
  88. args.truncate = 1;
  89. break;
  90. case 's':
  91. args.size = simple_strtoull(optarg, &error);
  92. if (error || args.size < 0)
  93. return errmsg("bad size: " "\"%s\"", optarg);
  94. break;
  95. case 'h':
  96. printf("%s\n\n", doc);
  97. printf("%s\n\n", usage);
  98. printf("%s\n", optionsstr);
  99. exit(EXIT_SUCCESS);
  100. case '?':
  101. printf("%s\n\n", doc);
  102. printf("%s\n\n", usage);
  103. printf("%s\n", optionsstr);
  104. return -1;
  105. case 'V':
  106. common_print_version();
  107. exit(EXIT_SUCCESS);
  108. case ':':
  109. return errmsg("parameter is missing");
  110. default:
  111. fprintf(stderr, "Use -h for help\n");
  112. return -1;
  113. }
  114. }
  115. if (optind == argc)
  116. return errmsg("UBI device name was not specified (use -h for help)");
  117. else if (optind != argc - 2 && !args.truncate)
  118. return errmsg("specify UBI device name and image file name as first 2 "
  119. "parameters (use -h for help)");
  120. args.node = argv[optind];
  121. args.img = argv[optind + 1];
  122. if (args.img && args.truncate)
  123. return errmsg("You can't truncate and specify an image (use -h for help)");
  124. if (args.img && !args.truncate) {
  125. if (strcmp(args.img, "-") == 0)
  126. args.use_stdin = 1;
  127. if (args.use_stdin && !args.size)
  128. return errmsg("file size must be specified if input is stdin");
  129. }
  130. return 0;
  131. }
  132. static int truncate_volume(libubi_t libubi)
  133. {
  134. int err, fd;
  135. fd = open(args.node, O_RDWR);
  136. if (fd == -1)
  137. return sys_errmsg("cannot open \"%s\"", args.node);
  138. err = ubi_update_start(libubi, fd, 0);
  139. if (err) {
  140. sys_errmsg("cannot truncate volume \"%s\"", args.node);
  141. close(fd);
  142. return -1;
  143. }
  144. close(fd);
  145. return 0;
  146. }
  147. static int ubi_write(int fd, const void *buf, int len)
  148. {
  149. int ret;
  150. while (len) {
  151. ret = write(fd, buf, len);
  152. if (ret < 0) {
  153. if (errno == EINTR) {
  154. warnmsg("do not interrupt me!");
  155. continue;
  156. }
  157. return sys_errmsg("cannot write %d bytes to volume \"%s\"",
  158. len, args.node);
  159. }
  160. if (ret == 0)
  161. return errmsg("cannot write %d bytes to volume \"%s\"", len, args.node);
  162. len -= ret;
  163. buf += ret;
  164. }
  165. return 0;
  166. }
  167. static int update_volume(libubi_t libubi, struct ubi_vol_info *vol_info)
  168. {
  169. int err, fd, ifd;
  170. long long bytes;
  171. char *buf;
  172. buf = malloc(vol_info->leb_size);
  173. if (!buf)
  174. return errmsg("cannot allocate %d bytes of memory", vol_info->leb_size);
  175. if (!args.size) {
  176. struct stat st;
  177. err = stat(args.img, &st);
  178. if (err < 0) {
  179. errmsg("stat failed on \"%s\"", args.img);
  180. goto out_free;
  181. }
  182. bytes = st.st_size - args.skip;
  183. } else
  184. bytes = args.size;
  185. if (bytes > vol_info->rsvd_bytes) {
  186. errmsg("\"%s\" (size %lld) will not fit volume \"%s\" (size %lld)",
  187. args.img, bytes, args.node, vol_info->rsvd_bytes);
  188. goto out_free;
  189. }
  190. fd = open(args.node, O_RDWR);
  191. if (fd == -1) {
  192. sys_errmsg("cannot open UBI volume \"%s\"", args.node);
  193. goto out_free;
  194. }
  195. if (args.use_stdin) {
  196. ifd = STDIN_FILENO;
  197. if (args.skip) {
  198. errmsg("seeking stdin not supported");
  199. goto out_close1;
  200. }
  201. } else {
  202. ifd = open(args.img, O_RDONLY);
  203. if (ifd == -1) {
  204. sys_errmsg("cannot open \"%s\"", args.img);
  205. goto out_close1;
  206. }
  207. if (args.skip && lseek(ifd, args.skip, SEEK_CUR) == -1) {
  208. sys_errmsg("lseek input by %lld failed", args.skip);
  209. goto out_close;
  210. }
  211. }
  212. err = ubi_update_start(libubi, fd, bytes);
  213. if (err) {
  214. sys_errmsg("cannot start volume \"%s\" update", args.node);
  215. goto out_close;
  216. }
  217. while (bytes) {
  218. ssize_t ret;
  219. int to_copy = min(vol_info->leb_size, bytes);
  220. ret = read(ifd, buf, to_copy);
  221. if (ret <= 0) {
  222. if (errno == EINTR) {
  223. warnmsg("do not interrupt me!");
  224. continue;
  225. } else {
  226. sys_errmsg("cannot read %d bytes from \"%s\"",
  227. to_copy, args.img);
  228. goto out_close;
  229. }
  230. }
  231. err = ubi_write(fd, buf, ret);
  232. if (err)
  233. goto out_close;
  234. bytes -= ret;
  235. }
  236. close(ifd);
  237. close(fd);
  238. free(buf);
  239. return 0;
  240. out_close:
  241. close(ifd);
  242. out_close1:
  243. close(fd);
  244. out_free:
  245. free(buf);
  246. return -1;
  247. }
  248. int main(int argc, char * const argv[])
  249. {
  250. int err;
  251. libubi_t libubi;
  252. struct ubi_vol_info vol_info;
  253. err = parse_opt(argc, argv);
  254. if (err)
  255. return -1;
  256. libubi = libubi_open();
  257. if (!libubi) {
  258. if (errno == 0)
  259. errmsg("UBI is not present in the system");
  260. return sys_errmsg("cannot open libubi");
  261. }
  262. err = ubi_probe_node(libubi, args.node);
  263. if (err == 1) {
  264. errmsg("\"%s\" is an UBI device node, not an UBI volume node",
  265. args.node);
  266. goto out_libubi;
  267. } else if (err < 0) {
  268. if (errno == ENODEV)
  269. errmsg("\"%s\" is not an UBI volume node", args.node);
  270. else
  271. sys_errmsg("error while probing \"%s\"", args.node);
  272. goto out_libubi;
  273. }
  274. err = ubi_get_vol_info(libubi, args.node, &vol_info);
  275. if (err) {
  276. sys_errmsg("cannot get information about UBI volume \"%s\"",
  277. args.node);
  278. goto out_libubi;
  279. }
  280. if (args.truncate)
  281. err = truncate_volume(libubi);
  282. else
  283. err = update_volume(libubi, &vol_info);
  284. if (err)
  285. goto out_libubi;
  286. libubi_close(libubi);
  287. return 0;
  288. out_libubi:
  289. libubi_close(libubi);
  290. return -1;
  291. }