main.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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/UpdateProgress.log", "w");
  44. if(NULL == fp)
  45. {
  46. printf("Create UpdateProgress.log file fail!\n");
  47. //return -1;
  48. }
  49. sprintf(progressStr, "%d", rate);
  50. fputs(progressStr, fprogress);
  51. fclose(fprogress);
  52. printf("Chip erase ...\n");
  53. sf_chip_erase(5);
  54. printf("Chip erase finished!\n");
  55. do{
  56. len = fread(buf, 1, 64*1024, fp);
  57. if(len <= 0)
  58. {
  59. close(fp);
  60. printf("Read file fail!\n");
  61. break;
  62. }
  63. //update Flash
  64. //sf_sector_erase(5, writeSize);
  65. sf_write(5, writeSize, buf, len);
  66. // sf_read(5, writeSize, chkBuf, len);
  67. // if(memcmp(buf, chkBuf, len) != 0)
  68. // {
  69. // printf("---> chk fail, offset %#x, len %d\n", writeSize, len);
  70. // break;
  71. // }
  72. writeSize += len;
  73. rate = (writeSize*100)/totalSize;
  74. printf("writeSize: %d, len: %d, rate: %d\n", writeSize, len, rate);
  75. //用来保存更新的进度
  76. fprogress = fopen("/var/www/goahead/tmp/UpdateProgress.log", "w");
  77. if(NULL == fp)
  78. {
  79. printf("Create UpdateProgress.log file fail!\n");
  80. //return -1;
  81. }
  82. sprintf(progressStr, "%d", rate);
  83. fputs(progressStr, fprogress);
  84. fclose(fprogress);
  85. }while(writeSize < totalSize);
  86. close(fp);
  87. printf("Update Flash finish.\n");
  88. //Verify
  89. printf("Verigy successful.\n");
  90. //用来保存校验结果
  91. fprogress = fopen("/var/www/goahead/tmp/Verify.log", "w");
  92. if(NULL == fp)
  93. {
  94. printf("Create UpdateProgress.log file fail!\n");
  95. }
  96. sprintf(progressStr, "Verify OK");
  97. fputs(progressStr, fprogress);
  98. fclose(fprogress);
  99. fclose(fp);
  100. free(buf);
  101. free(chkBuf);
  102. return 0;
  103. }