main.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. FILE *fpErase;
  28. FILE *fpVerify;
  29. uint8_t progressStr[10] = {0};
  30. uint8_t rate = 0;
  31. buf = malloc(32*1024 + 50);
  32. // chkBuf = malloc(64*1024);
  33. fp = fopen("/var/www/goahead/tmp/tmp.uImage", "rb");
  34. if(NULL == fp)
  35. {
  36. printf("Open image file fail!\n");
  37. return -1;
  38. }
  39. //get file total size
  40. fseek(fp, 0, SEEK_END);
  41. totalSize = ftell(fp);
  42. fseek(fp, 0, SEEK_SET);
  43. printf("Image size: %d\n", totalSize);
  44. //开始擦除
  45. fpErase = fopen("/var/www/goahead/tmp/Erase.log", "w");
  46. if(NULL == fpErase)
  47. {
  48. printf("Create Erase.log file fail!\n");
  49. }
  50. fputs("Erasing", fpErase);
  51. fclose(fpErase);
  52. printf("Chip erase ...\n");
  53. sf_chip_erase(5);
  54. printf("Chip erase finished!\n");
  55. fpErase = fopen("/var/www/goahead/tmp/Erase.log", "w");
  56. if(NULL == fpErase)
  57. {
  58. printf("Create Erase.log file fail!\n");
  59. }
  60. fputs("Erase OK", fpErase);
  61. fclose(fpErase);
  62. //用来保存更新的进度
  63. fprogress = fopen("/var/www/goahead/tmp/UpdateProgress.log", "w");
  64. if(NULL == fprogress)
  65. {
  66. printf("Create UpdateProgress.log file fail!\n");
  67. //return -1;
  68. }
  69. fputs("0", fprogress);
  70. fclose(fprogress);
  71. do{
  72. len = fread(buf, 1, 32*1024, fp);
  73. if(len <= 0)
  74. {
  75. close(fp);
  76. printf("Read file fail!\n");
  77. break;
  78. }
  79. //update Flash
  80. //sf_sector_erase(5, writeSize);
  81. sf_write(5, writeSize, buf, len);
  82. // sf_read(5, writeSize, chkBuf, len);
  83. // if(memcmp(buf, chkBuf, len) != 0)
  84. // {
  85. // printf("---> chk fail, offset %#x, len %d\n", writeSize, len);
  86. // break;
  87. // }
  88. writeSize += len;
  89. rate = (writeSize*100)/totalSize;
  90. printf("writeSize: %d, len: %d, rate: %d\n", writeSize, len, rate);
  91. //用来保存更新的进度
  92. fprogress = fopen("/var/www/goahead/tmp/UpdateProgress.log", "w");
  93. if(NULL == fprogress)
  94. {
  95. printf("Create UpdateProgress.log file fail!\n");
  96. //return -1;
  97. }
  98. sprintf(progressStr, "%d", rate);
  99. fputs(progressStr, fprogress);
  100. fclose(fprogress);
  101. }while(writeSize < totalSize);
  102. close(fp);
  103. printf("Update Flash finish.\n");
  104. //Verify
  105. printf("Verigy successful.\n");
  106. //用来保存校验结果
  107. fpVerify = fopen("/var/www/goahead/tmp/Verify.log", "w");
  108. if(NULL == fpVerify)
  109. {
  110. printf("Create UpdateProgress.log file fail!\n");
  111. }
  112. fputs("Verify OK", fpVerify);
  113. fclose(fpVerify);
  114. fclose(fp);
  115. free(buf);
  116. // free(chkBuf);
  117. return 0;
  118. }