main.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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[4*1024] = {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. fp = fopen("/var/www/goahead/tmp/tmp.uImage", "rb");
  30. if(NULL == fp)
  31. {
  32. printf("Open image file fail!\n");
  33. return -1;
  34. }
  35. //get file total size
  36. fseek(fp, 0, SEEK_END);
  37. totalSize = ftell(fp);
  38. fseek(fp, 0, SEEK_SET);
  39. printf("Image size: %d\n", totalSize);
  40. do{
  41. len = fread(buf, 1, 4*1024, fp);
  42. if(len <= 0)
  43. {
  44. close(fp);
  45. printf("Read file fail!\n");
  46. break;
  47. }
  48. //update Flash
  49. sf_sector_erase(5, writeSize);
  50. sf_write(5, writeSize, buf, len);
  51. // sf_read(5, writeSize, chkBuf, len);
  52. // if(memcmp(buf, chkBuf, len) != 0)
  53. // {
  54. // printf("---> chk fail, offset %#x, len %d\n", writeSize, len);
  55. // break;
  56. // }
  57. writeSize += len;
  58. rate = (writeSize*100)/totalSize;
  59. printf("writeSize: %d, len: %d, rate: %d\n", writeSize, len, rate);
  60. //用来保存更新的进度
  61. fprogress = fopen("/var/www/goahead/tmp/UpdateProgress.log", "w");
  62. if(NULL == fp)
  63. {
  64. printf("Create UpdateProgress.log file fail!\n");
  65. //return -1;
  66. }
  67. sprintf(progressStr, "%d", rate);
  68. fputs(progressStr, fprogress);
  69. fclose(fprogress);
  70. }while(writeSize < totalSize);
  71. close(fp);
  72. printf("Update Flash finish.\n");
  73. //Verify
  74. printf("Verigy successful.\n");
  75. //用来保存校验结果
  76. fprogress = fopen("/var/www/goahead/tmp/Verify.log", "w");
  77. if(NULL == fp)
  78. {
  79. printf("Create UpdateProgress.log file fail!\n");
  80. }
  81. sprintf(progressStr, "Verify OK");
  82. fputs(progressStr, fprogress);
  83. fclose(fprogress);
  84. fclose(fp);
  85. return 0;
  86. }