main.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <fcntl.h>
  4. #include <linux/types.h>
  5. #include <unistd.h>
  6. #include <stdint.h>
  7. #include <string.h>
  8. #include <errno.h>
  9. #include <pthread.h>
  10. #include <fcntl.h>
  11. #include <sys/socket.h>
  12. #include <netdb.h> /* getaddrinfo(3) et al. */
  13. #include <netinet/in.h> /* sockaddr_in & sockaddr_in6 definition. */
  14. #include <net/if.h>
  15. #include <sys/prctl.h>
  16. #include "driver.h"
  17. #include "hal_interface_api.h"
  18. #include "main.h"
  19. int main()
  20. {
  21. int i;
  22. uint8_t *buf;//[64*1024+1] = {0}; //4KB, one sector
  23. uint8_t *chkBuf;//[4*1024] = {0};
  24. FILE *fp;
  25. size_t len, totalSize = 1, writeSize = 0;
  26. FILE *fprogress;
  27. uint8_t progressStr[10] = {0};
  28. uint8_t rate = 0;
  29. buf = malloc(64*1024);
  30. // chkBuf = malloc(64*1024);
  31. fp = fopen("/var/www/goahead/tmp/tmp.uImage", "rb");
  32. if(NULL == fp)
  33. {
  34. printf("Open image file fail!\n");
  35. return -1;
  36. }
  37. //get file total size
  38. fseek(fp, 0, SEEK_END);
  39. totalSize = ftell(fp);
  40. fseek(fp, 0, SEEK_SET);
  41. printf("Image size: %d\n", totalSize);
  42. //开始擦除
  43. fprogress = fopen("/var/www/goahead/tmp/Erase.log", "w");
  44. if(NULL == fprogress)
  45. {
  46. printf("Create Erase.log file fail!\n");
  47. }
  48. fputs(progressStr, "Erasing");
  49. fclose(fprogress);
  50. printf("Chip erase ...\n");
  51. sf_chip_erase(5);
  52. printf("Chip erase finished!\n");
  53. fprogress = fopen("/var/www/goahead/tmp/Erase.log", "w");
  54. if(NULL == fprogress)
  55. {
  56. printf("Create Erase.log file fail!\n");
  57. }
  58. fputs(progressStr, "Erase OK");
  59. fclose(fprogress);
  60. //用来保存更新的进度
  61. fprogress = fopen("/var/www/goahead/tmp/UpdateProgress.log", "w");
  62. if(NULL == fprogress)
  63. {
  64. printf("Create UpdateProgress.log file fail!\n");
  65. //return -1;
  66. }
  67. fputs(progressStr, "0");
  68. fclose(fprogress);
  69. do{
  70. len = fread(buf, 1, 64*1024, fp);
  71. if(len <= 0)
  72. {
  73. close(fp);
  74. printf("Read file fail!\n");
  75. break;
  76. }
  77. //update Flash
  78. //sf_sector_erase(5, writeSize);
  79. sf_write(5, writeSize, buf, len);
  80. // sf_read(5, writeSize, chkBuf, len);
  81. // if(memcmp(buf, chkBuf, len) != 0)
  82. // {
  83. // printf("---> chk fail, offset %#x, len %d\n", writeSize, len);
  84. // break;
  85. // }
  86. writeSize += len;
  87. rate = (writeSize*100)/totalSize;
  88. printf("writeSize: %d, len: %d, rate: %d\n", writeSize, len, rate);
  89. //用来保存更新的进度
  90. fprogress = fopen("/var/www/goahead/tmp/UpdateProgress.log", "w");
  91. if(NULL == fprogress)
  92. {
  93. printf("Create UpdateProgress.log file fail!\n");
  94. //return -1;
  95. }
  96. sprintf(progressStr, "%d", rate);
  97. fputs(progressStr, fprogress);
  98. fclose(fprogress);
  99. }while(writeSize < totalSize);
  100. close(fp);
  101. printf("Update Flash finish.\n");
  102. //Verify
  103. printf("Verigy successful.\n");
  104. //用来保存校验结果
  105. fprogress = fopen("/var/www/goahead/tmp/Verify.log", "w");
  106. if(NULL == fprogress)
  107. {
  108. printf("Create UpdateProgress.log file fail!\n");
  109. }
  110. fputs(progressStr, "Verify OK");
  111. fclose(fprogress);
  112. fclose(fp);
  113. free(buf);
  114. // free(chkBuf);
  115. return 0;
  116. }