flashcp.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. //usage:#define flashcp_trivial_usage
  10. //usage: "-v FILE MTD_DEVICE"
  11. //usage:#define flashcp_full_usage "\n\n"
  12. //usage: "Copy an image to MTD device\n"
  13. //usage: "\n -v Verbose"
  14. #include "libbb.h"
  15. #include <mtd/mtd-user.h>
  16. #define MTD_DEBUG 0
  17. #define OPT_v (1 << 0)
  18. #define BUFSIZE (8 * 1024)
  19. static void progress(int mode, uoff_t count, uoff_t total)
  20. {
  21. uoff_t percent;
  22. if (!option_mask32) //if (!(option_mask32 & OPT_v))
  23. return;
  24. percent = count * 100;
  25. if (total)
  26. percent = (unsigned) (percent / total);
  27. printf("\r%s: %"OFF_FMT"u/%"OFF_FMT"u (%u%%) ",
  28. (mode == 0) ? "Erasing block" : ((mode == 1) ? "Writing kb" : "Verifying kb"),
  29. count, total, (unsigned)percent);
  30. fflush_all();
  31. }
  32. static void progress_newline(void)
  33. {
  34. if (!option_mask32) //if (!(option_mask32 & OPT_v))
  35. return;
  36. bb_putchar('\n');
  37. }
  38. int flashcp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  39. int flashcp_main(int argc UNUSED_PARAM, char **argv)
  40. {
  41. int fd_f, fd_d; /* input file and mtd device file descriptors */
  42. int i;
  43. uoff_t erase_count;
  44. unsigned opts;
  45. struct mtd_info_user mtd;
  46. struct erase_info_user e;
  47. struct stat statb;
  48. // const char *filename, *devicename;
  49. RESERVE_CONFIG_UBUFFER(buf, BUFSIZE);
  50. RESERVE_CONFIG_UBUFFER(buf2, BUFSIZE);
  51. opt_complementary = "=2"; /* exactly 2 non-option args: file, dev */
  52. opts = getopt32(argv, "v");
  53. argv += optind;
  54. // filename = *argv++;
  55. // devicename = *argv;
  56. #define filename argv[0]
  57. #define devicename argv[1]
  58. /* open input file and mtd device and do sanity checks */
  59. fd_f = xopen(filename, O_RDONLY);
  60. fstat(fd_f, &statb);
  61. fd_d = xopen(devicename, O_SYNC | O_RDWR);
  62. #if !MTD_DEBUG
  63. if (ioctl(fd_d, MEMGETINFO, &mtd) < 0) {
  64. bb_error_msg_and_die("%s is not a MTD flash device", devicename);
  65. }
  66. if (statb.st_size > mtd.size) {
  67. bb_error_msg_and_die("%s bigger than %s", filename, devicename);
  68. }
  69. #else
  70. mtd.erasesize = 64 * 1024;
  71. #endif
  72. /* always erase a complete block */
  73. erase_count = (uoff_t)(statb.st_size + mtd.erasesize - 1) / mtd.erasesize;
  74. /* erase 1 block at a time to be able to give verbose output */
  75. e.length = mtd.erasesize;
  76. #if 0
  77. /* (1) bloat
  78. * (2) will it work for multi-gigabyte devices?
  79. * (3) worse wrt error detection granularity
  80. */
  81. /* optimization: if not verbose, erase in one go */
  82. if (!opts) { // if (!(opts & OPT_v))
  83. e.length = mtd.erasesize * erase_count;
  84. erase_count = 1;
  85. }
  86. #endif
  87. e.start = 0;
  88. for (i = 1; i <= erase_count; i++) {
  89. progress(0, i, erase_count);
  90. errno = 0;
  91. #if !MTD_DEBUG
  92. if (ioctl(fd_d, MEMERASE, &e) < 0) {
  93. bb_perror_msg_and_die("erase error at 0x%llx on %s",
  94. (long long)e.start, devicename);
  95. }
  96. #else
  97. usleep(100*1000);
  98. #endif
  99. e.start += mtd.erasesize;
  100. }
  101. progress_newline();
  102. /* doing this outer loop gives significantly smaller code
  103. * than doing two separate loops for writing and verifying */
  104. for (i = 1; i <= 2; i++) {
  105. uoff_t done;
  106. unsigned count;
  107. xlseek(fd_f, 0, SEEK_SET);
  108. xlseek(fd_d, 0, SEEK_SET);
  109. done = 0;
  110. count = BUFSIZE;
  111. while (1) {
  112. uoff_t rem = statb.st_size - done;
  113. if (rem == 0)
  114. break;
  115. if (rem < BUFSIZE)
  116. count = rem;
  117. progress(i, done / 1024, (uoff_t)statb.st_size / 1024);
  118. xread(fd_f, buf, count);
  119. if (i == 1) {
  120. int ret;
  121. errno = 0;
  122. ret = full_write(fd_d, buf, count);
  123. if (ret != count) {
  124. bb_perror_msg_and_die("write error at 0x%"OFF_FMT"x on %s, "
  125. "write returned %d",
  126. done, devicename, ret);
  127. }
  128. } else { /* i == 2 */
  129. xread(fd_d, buf2, count);
  130. if (memcmp(buf, buf2, count)) {
  131. bb_error_msg_and_die("verification mismatch at 0x%"OFF_FMT"x", done);
  132. }
  133. }
  134. done += count;
  135. }
  136. progress_newline();
  137. }
  138. /* we won't come here if there was an error */
  139. return EXIT_SUCCESS;
  140. }