main.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. FILE *fp;
  24. size_t len, totalSize = 1, writeSize = 0;
  25. FILE *fprogress;
  26. uint8_t progressStr[10] = {0};
  27. uint8_t rate = 0;
  28. fp = fopen("/var/www/goahead/tmp/tmp.uImage", "rb");
  29. if(NULL == fp)
  30. {
  31. printf("Open image file fail!\n");
  32. return -1;
  33. }
  34. //get file total size
  35. fseek(fp, 0, SEEK_END);
  36. totalSize = ftell(fp);
  37. fseek(fp, 0, SEEK_SET);
  38. printf("Image size: %d\n", totalSize);
  39. do{
  40. len = fread(buf, 1, 4*1024, fp);
  41. if(len <= 0)
  42. {
  43. close(fp);
  44. printf("Read file fail!\n");
  45. break;
  46. }
  47. writeSize += len;
  48. rate = (writeSize*100)/totalSize;
  49. //printf("writeSize: %d, len: %d, rate: %d\n", writeSize, len, rate);
  50. //用来保存更新的进度
  51. fprogress = fopen("/var/www/goahead/tmp/UpdateProgress.log", "w");
  52. if(NULL == fp)
  53. {
  54. printf("Create UpdateProgress.log file fail!\n");
  55. return -1;
  56. }
  57. sprintf(progressStr, "%d%%", rate);
  58. fputs(progressStr, fprogress);
  59. fclose(fprogress);
  60. //TODO: update Flash
  61. usleep(10000);
  62. }while(writeSize < totalSize);
  63. printf("Update Flash finish.\n");
  64. //Verify
  65. printf("Verigy successful.\n");
  66. //用来保存更新的进度
  67. fprogress = fopen("/var/www/goahead/tmp/UpdateProgress.log", "w");
  68. if(NULL == fp)
  69. {
  70. printf("Create UpdateProgress.log file fail!\n");
  71. return -1;
  72. }
  73. sprintf(progressStr, "Verify OK");
  74. fputs(progressStr, fprogress);
  75. fclose(fprogress);
  76. fclose(fp);
  77. return 0;
  78. }