3
0

flashcp.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. /* If 1, simulates "flashing" by writing to existing regular file */
  17. #define MTD_DEBUG 0
  18. #define OPT_v (1 << 0)
  19. #define BUFSIZE (4 * 1024)
  20. static void progress(int mode, uoff_t count, uoff_t total)
  21. {
  22. uoff_t percent;
  23. if (!option_mask32) //if (!(option_mask32 & OPT_v))
  24. return;
  25. percent = count * 100;
  26. if (total)
  27. percent = (unsigned) (percent / total);
  28. printf("\r%s: %"OFF_FMT"u/%"OFF_FMT"u (%u%%) ",
  29. (mode < 0) ? "Erasing block" : ((mode == 0) ? "Writing kb" : "Verifying kb"),
  30. count, total, (unsigned)percent);
  31. fflush_all();
  32. }
  33. static void progress_newline(void)
  34. {
  35. if (!option_mask32) //if (!(option_mask32 & OPT_v))
  36. return;
  37. bb_putchar('\n');
  38. }
  39. int flashcp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  40. int flashcp_main(int argc UNUSED_PARAM, char **argv)
  41. {
  42. int fd_f, fd_d; /* input file and mtd device file descriptors */
  43. int i;
  44. uoff_t erase_count;
  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(-1, i, erase_count);
  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 = 0; i <= 1; 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;
  112. progress(i, done / 1024, (uoff_t)statb.st_size / 1024);
  113. rem = statb.st_size - done;
  114. if (rem == 0)
  115. break;
  116. if (rem < BUFSIZE)
  117. count = rem;
  118. xread(fd_f, buf, count);
  119. if (i == 0) {
  120. int ret;
  121. if (count < BUFSIZE)
  122. memset((char*)buf + count, 0, BUFSIZE - count);
  123. errno = 0;
  124. ret = full_write(fd_d, buf, BUFSIZE);
  125. if (ret != BUFSIZE) {
  126. bb_perror_msg_and_die("write error at 0x%"OFF_FMT"x on %s, "
  127. "write returned %d",
  128. done, devicename, ret);
  129. }
  130. } else { /* i == 1 */
  131. xread(fd_d, buf2, count);
  132. if (memcmp(buf, buf2, count) != 0) {
  133. bb_error_msg_and_die("verification mismatch at 0x%"OFF_FMT"x", done);
  134. }
  135. }
  136. done += count;
  137. }
  138. progress_newline();
  139. }
  140. /* we won't come here if there was an error */
  141. return EXIT_SUCCESS;
  142. }