3
0

flashcp.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * busybox reimplementation of flashcp
  4. *
  5. * (C) 2009 Stefan Seyfried <seife@sphairon.com>
  6. *
  7. * Licensed under GPLv2, see file LICENSE in this source tree.
  8. */
  9. //config:config FLASHCP
  10. //config: bool "flashcp (5.3 kb)"
  11. //config: default n # doesn't build on Ubuntu 8.04
  12. //config: help
  13. //config: The flashcp binary, inspired by mtd-utils as of git head 5eceb74f7.
  14. //config: This utility is used to copy images into a MTD device.
  15. //applet:IF_FLASHCP(APPLET(flashcp, BB_DIR_USR_SBIN, BB_SUID_DROP))
  16. /* not NOEXEC: if flash operation stalls, use less memory in "hung" process */
  17. //kbuild:lib-$(CONFIG_FLASHCP) += flashcp.o
  18. //usage:#define flashcp_trivial_usage
  19. //usage: "-v FILE MTD_DEVICE"
  20. //usage:#define flashcp_full_usage "\n\n"
  21. //usage: "Copy an image to MTD device\n"
  22. //usage: "\n -v Verbose"
  23. #include "libbb.h"
  24. #include <mtd/mtd-user.h>
  25. /* If 1, simulates "flashing" by writing to existing regular file */
  26. #define MTD_DEBUG 0
  27. #define OPT_v (1 << 0)
  28. #define BUFSIZE (4 * 1024)
  29. static void progress(int mode, uoff_t count, uoff_t total)
  30. {
  31. uoff_t percent;
  32. if (!option_mask32) //if (!(option_mask32 & OPT_v))
  33. return;
  34. percent = count * 100;
  35. if (total)
  36. percent = (unsigned) (percent / total);
  37. printf("\r%s: %"OFF_FMT"u/%"OFF_FMT"u (%u%%) ",
  38. (mode < 0) ? "Erasing block" : ((mode == 0) ? "Writing kb" : "Verifying kb"),
  39. count, total, (unsigned)percent);
  40. fflush_all();
  41. }
  42. static void progress_newline(void)
  43. {
  44. if (!option_mask32) //if (!(option_mask32 & OPT_v))
  45. return;
  46. bb_putchar('\n');
  47. }
  48. int flashcp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  49. int flashcp_main(int argc UNUSED_PARAM, char **argv)
  50. {
  51. int fd_f, fd_d; /* input file and mtd device file descriptors */
  52. int i;
  53. uoff_t erase_count;
  54. struct mtd_info_user mtd;
  55. struct erase_info_user e;
  56. struct stat statb;
  57. // const char *filename, *devicename;
  58. RESERVE_CONFIG_UBUFFER(buf, BUFSIZE);
  59. RESERVE_CONFIG_UBUFFER(buf2, BUFSIZE);
  60. /*opts =*/ getopt32(argv, "^" "v" "\0" "=2"/*exactly 2 non-option args: file,dev*/);
  61. argv += optind;
  62. // filename = *argv++;
  63. // devicename = *argv;
  64. #define filename argv[0]
  65. #define devicename argv[1]
  66. /* open input file and mtd device and do sanity checks */
  67. fd_f = xopen(filename, O_RDONLY);
  68. fstat(fd_f, &statb);
  69. fd_d = xopen(devicename, O_SYNC | O_RDWR);
  70. #if !MTD_DEBUG
  71. if (ioctl(fd_d, MEMGETINFO, &mtd) < 0) {
  72. bb_error_msg_and_die("%s is not a MTD flash device", devicename);
  73. }
  74. if (statb.st_size > mtd.size) {
  75. bb_error_msg_and_die("%s bigger than %s", filename, devicename);
  76. }
  77. #else
  78. mtd.erasesize = 64 * 1024;
  79. #endif
  80. /* always erase a complete block */
  81. erase_count = (uoff_t)(statb.st_size + mtd.erasesize - 1) / mtd.erasesize;
  82. /* erase 1 block at a time to be able to give verbose output */
  83. e.length = mtd.erasesize;
  84. #if 0
  85. /* (1) bloat
  86. * (2) will it work for multi-gigabyte devices?
  87. * (3) worse wrt error detection granularity
  88. */
  89. /* optimization: if not verbose, erase in one go */
  90. if (!opts) { // if (!(opts & OPT_v))
  91. e.length = mtd.erasesize * erase_count;
  92. erase_count = 1;
  93. }
  94. #endif
  95. e.start = 0;
  96. for (i = 1; i <= erase_count; i++) {
  97. progress(-1, i, erase_count);
  98. #if !MTD_DEBUG
  99. if (ioctl(fd_d, MEMERASE, &e) < 0) {
  100. bb_perror_msg_and_die("erase error at 0x%llx on %s",
  101. (long long)e.start, devicename);
  102. }
  103. #else
  104. usleep(100*1000);
  105. #endif
  106. e.start += mtd.erasesize;
  107. }
  108. progress_newline();
  109. /* doing this outer loop gives significantly smaller code
  110. * than doing two separate loops for writing and verifying */
  111. for (i = 0; i <= 1; i++) {
  112. uoff_t done;
  113. unsigned count;
  114. xlseek(fd_f, 0, SEEK_SET);
  115. xlseek(fd_d, 0, SEEK_SET);
  116. done = 0;
  117. count = BUFSIZE;
  118. while (1) {
  119. uoff_t rem;
  120. progress(i, done / 1024, (uoff_t)statb.st_size / 1024);
  121. rem = statb.st_size - done;
  122. if (rem == 0)
  123. break;
  124. if (rem < BUFSIZE)
  125. count = rem;
  126. xread(fd_f, buf, count);
  127. if (i == 0) {
  128. int ret;
  129. if (count < BUFSIZE)
  130. memset((char*)buf + count, 0, BUFSIZE - count);
  131. errno = 0;
  132. ret = full_write(fd_d, buf, BUFSIZE);
  133. if (ret != BUFSIZE) {
  134. bb_perror_msg_and_die("write error at 0x%"OFF_FMT"x on %s, "
  135. "write returned %d",
  136. done, devicename, ret);
  137. }
  138. } else { /* i == 1 */
  139. xread(fd_d, buf2, count);
  140. if (memcmp(buf, buf2, count) != 0) {
  141. bb_error_msg_and_die("verification mismatch at 0x%"OFF_FMT"x", done);
  142. }
  143. }
  144. done += count;
  145. }
  146. progress_newline();
  147. }
  148. /* we won't come here if there was an error */
  149. return EXIT_SUCCESS;
  150. }