3
0

flashcp.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. struct mtd_info_user mtd;
  45. struct erase_info_user e;
  46. struct stat statb;
  47. // const char *filename, *devicename;
  48. RESERVE_CONFIG_UBUFFER(buf, BUFSIZE);
  49. RESERVE_CONFIG_UBUFFER(buf2, BUFSIZE);
  50. opt_complementary = "=2"; /* exactly 2 non-option args: file, dev */
  51. /*opts =*/ getopt32(argv, "v");
  52. argv += optind;
  53. // filename = *argv++;
  54. // devicename = *argv;
  55. #define filename argv[0]
  56. #define devicename argv[1]
  57. /* open input file and mtd device and do sanity checks */
  58. fd_f = xopen(filename, O_RDONLY);
  59. fstat(fd_f, &statb);
  60. fd_d = xopen(devicename, O_SYNC | O_RDWR);
  61. #if !MTD_DEBUG
  62. if (ioctl(fd_d, MEMGETINFO, &mtd) < 0) {
  63. bb_error_msg_and_die("%s is not a MTD flash device", devicename);
  64. }
  65. if (statb.st_size > mtd.size) {
  66. bb_error_msg_and_die("%s bigger than %s", filename, devicename);
  67. }
  68. #else
  69. mtd.erasesize = 64 * 1024;
  70. #endif
  71. /* always erase a complete block */
  72. erase_count = (uoff_t)(statb.st_size + mtd.erasesize - 1) / mtd.erasesize;
  73. /* erase 1 block at a time to be able to give verbose output */
  74. e.length = mtd.erasesize;
  75. #if 0
  76. /* (1) bloat
  77. * (2) will it work for multi-gigabyte devices?
  78. * (3) worse wrt error detection granularity
  79. */
  80. /* optimization: if not verbose, erase in one go */
  81. if (!opts) { // if (!(opts & OPT_v))
  82. e.length = mtd.erasesize * erase_count;
  83. erase_count = 1;
  84. }
  85. #endif
  86. e.start = 0;
  87. for (i = 1; i <= erase_count; i++) {
  88. progress(0, i, erase_count);
  89. errno = 0;
  90. #if !MTD_DEBUG
  91. if (ioctl(fd_d, MEMERASE, &e) < 0) {
  92. bb_perror_msg_and_die("erase error at 0x%llx on %s",
  93. (long long)e.start, devicename);
  94. }
  95. #else
  96. usleep(100*1000);
  97. #endif
  98. e.start += mtd.erasesize;
  99. }
  100. progress_newline();
  101. /* doing this outer loop gives significantly smaller code
  102. * than doing two separate loops for writing and verifying */
  103. for (i = 1; i <= 2; i++) {
  104. uoff_t done;
  105. unsigned count;
  106. xlseek(fd_f, 0, SEEK_SET);
  107. xlseek(fd_d, 0, SEEK_SET);
  108. done = 0;
  109. count = BUFSIZE;
  110. while (1) {
  111. uoff_t rem = statb.st_size - done;
  112. if (rem == 0)
  113. break;
  114. if (rem < BUFSIZE)
  115. count = rem;
  116. progress(i, done / 1024, (uoff_t)statb.st_size / 1024);
  117. xread(fd_f, buf, count);
  118. if (i == 1) {
  119. int ret;
  120. errno = 0;
  121. ret = full_write(fd_d, buf, count);
  122. if (ret != count) {
  123. bb_perror_msg_and_die("write error at 0x%"OFF_FMT"x on %s, "
  124. "write returned %d",
  125. done, devicename, ret);
  126. }
  127. } else { /* i == 2 */
  128. xread(fd_d, buf2, count);
  129. if (memcmp(buf, buf2, count)) {
  130. bb_error_msg_and_die("verification mismatch at 0x%"OFF_FMT"x", done);
  131. }
  132. }
  133. done += count;
  134. }
  135. progress_newline();
  136. }
  137. /* we won't come here if there was an error */
  138. return EXIT_SUCCESS;
  139. }