FWMaker.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*===========================================================================
  2. Combined Charging System (CCS): SECC
  3. FWMaker.c
  4. initiated by Vern, Joseph
  5. (since 2019/07/19)
  6. =============================================================================*/
  7. #include <stdio.h>
  8. #include <sys/types.h>
  9. #include <sys/socket.h>
  10. #include <netinet/in.h>
  11. #include <arpa/inet.h>
  12. #include <string.h>
  13. #include <net/if.h> /*struct ifreq*/
  14. #include <linux/sockios.h> /*SIOCGIFINDEX*/
  15. #include <linux/socket.h>
  16. #include <errno.h>
  17. #include <sys/time.h>
  18. #include <sys/timeb.h>
  19. #include <fcntl.h>
  20. #include <sys/mman.h>
  21. #include "FWMaker.h"
  22. unsigned char buf_log_fwmaker[SIZE_OF_LOG_BUFFER];
  23. /*===========================================================================
  24. FUNCTION: StoreLogMsg
  25. DESCRIPTION:
  26. PRE-CONDITION:
  27. INPUT:
  28. OUTPUT:
  29. GLOBAL VARIABLES:
  30. =============================================================================*/
  31. #if SAVE_SYS_LOG_MSG_FWMAKER_SWITCH == ENABLE
  32. int StoreLogMsg(unsigned char *DataString)
  33. {
  34. static unsigned char Buf[1024];
  35. static time_t CurrentTime;
  36. static struct tm *tm;
  37. static struct timeval tv;
  38. memset(Buf, 0, sizeof(Buf));
  39. CurrentTime = time(NULL);
  40. tm = localtime(&CurrentTime);
  41. gettimeofday(&tv, NULL); // get microseconds, 10^-6
  42. sprintf(Buf, "echo \"[%04d%02d%02d: %02d:%02d:%02d.%06d][FWMaker]%s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  43. tm->tm_year + 1900,
  44. tm->tm_mon + 1,
  45. tm->tm_mday,
  46. tm->tm_hour,
  47. tm->tm_min,
  48. tm->tm_sec,
  49. tv.tv_usec,
  50. DataString,
  51. tm->tm_year + 1900,
  52. tm->tm_mon + 1);
  53. system(Buf);
  54. DEBUG_PRINTF_FWMAKER_SYSTEM_LOG("[%02d:%02d:%02d.%06d][FWMaker]%s \n",
  55. tm->tm_hour,
  56. tm->tm_min,
  57. tm->tm_sec,
  58. tv.tv_usec,
  59. DataString);
  60. //Reset the buf_log_fwmaker Buffer, i.e. DataString
  61. memset(buf_log_fwmaker, 0, SIZE_OF_LOG_BUFFER);
  62. }
  63. #endif
  64. //./FWMaker 172.16.24.126
  65. int main(int argc, char *argv[])
  66. {
  67. unsigned char *MemBuf, *MemBuf2, buf[64];
  68. int fd, rd = 0, wrd = 0, tmp = 0, rd2 = 0;
  69. unsigned int CheckSum = 0;
  70. memset(buf, 0, 64);
  71. sprintf(buf, "tftp -gr uImage -l /mnt/uImage %s", argv[1]);
  72. system(buf);
  73. memset(buf, 0, 64);
  74. sprintf(buf, "tftp -gr rootfs_nor.img -l /mnt/rootfs_nor.img %s", argv[1]);
  75. system(buf);
  76. if((MemBuf = malloc(0x100000D)) == NULL)
  77. {
  78. DEBUG_PRINTF_FWMAKER_DETAIL("Allocate MemBuf memory error\n");
  79. return 0;
  80. }
  81. memset(MemBuf, 0xff, 0x100000D);
  82. fd = open("/mnt/uImage", O_RDWR);
  83. if(fd > 0)
  84. {
  85. if((rd = read(fd, MemBuf, 0x100000D)) <= 0)
  86. {
  87. DEBUG_PRINTF_FWMAKER_DETAIL("/mnt/uImage read Error\n");
  88. free(MemBuf);
  89. close(fd);
  90. return 0;
  91. }
  92. close(fd);
  93. for(tmp = 0; tmp < rd; tmp++)
  94. {
  95. CheckSum += MemBuf[tmp];
  96. }
  97. strncpy(MemBuf + rd, "DELTADCOK", 9);
  98. *(MemBuf + rd + 9) = CheckSum >> 24;
  99. *(MemBuf + rd + 10) = CheckSum >> 16;
  100. *(MemBuf + rd + 11) = CheckSum >> 8;
  101. *(MemBuf + rd + 12) = CheckSum;
  102. // memcpy(MemBuf+rd+9,&CheckSum,4);
  103. fd = open("/mnt/DcoKImage", O_CREAT | O_RDWR);
  104. wrd = write(fd, MemBuf, rd + 13);
  105. if(wrd != (rd + 13))
  106. {
  107. DEBUG_PRINTF_FWMAKER_DETAIL("write error wrd=0x%x, rd=0x%x\n", wrd, rd + 13);
  108. }
  109. else
  110. {
  111. DEBUG_PRINTF_FWMAKER_DETAIL("/mnt/DcoKImage OK\n");
  112. }
  113. }
  114. else
  115. {
  116. free(MemBuf);
  117. DEBUG_PRINTF_FWMAKER_DETAIL("/mnt/uImage open Error\n");
  118. return 0;
  119. }
  120. memset(MemBuf, 0xff, 0x100000D);
  121. CheckSum = rd = 0;
  122. fd = open("/mnt/rootfs_nor.img", O_RDWR);
  123. if(fd > 0)
  124. {
  125. if((rd = read(fd, MemBuf, 0x100000D)) <= 0)
  126. {
  127. DEBUG_PRINTF_FWMAKER_DETAIL("/mnt/rootfs_nor.img read Error\n");
  128. free(MemBuf);
  129. close(fd);
  130. return 0;
  131. }
  132. close(fd);
  133. for(tmp = 0; tmp < rd; tmp++)
  134. {
  135. CheckSum += MemBuf[tmp];
  136. }
  137. strncpy(MemBuf + rd, "DELTADCOF", 9);
  138. *(MemBuf + rd + 9) = CheckSum >> 24;
  139. *(MemBuf + rd + 10) = CheckSum >> 16;
  140. *(MemBuf + rd + 11) = CheckSum >> 8;
  141. *(MemBuf + rd + 12) = CheckSum;
  142. //memcpy(MemBuf+rd+9,&CheckSum,4);
  143. fd = open("/mnt/DcoFImage", O_CREAT | O_RDWR);
  144. wrd = write(fd, MemBuf, rd + 13);
  145. if(wrd != (rd + 13))
  146. {
  147. DEBUG_PRINTF_FWMAKER_DETAIL("write error wrd=0x%x, rd=0x%x\n", wrd, rd + 13);
  148. }
  149. else
  150. {
  151. DEBUG_PRINTF_FWMAKER_DETAIL("/mnt/DcoFImage OK\n");
  152. }
  153. }
  154. else
  155. {
  156. free(MemBuf);
  157. DEBUG_PRINTF_FWMAKER_DETAIL("/mnt/rootfs_nor.img open Error\n");
  158. return 0;
  159. }
  160. }