123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344 |
- /*===========================================================================
- Combined Charging System (CCS): SECC
- FWUpdate.c
- initiated by Vern, Joseph
- (since 2019/07/19)
- =============================================================================*/
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <linux/termios.h>
- #include <stdio.h>
- #include <string.h>
- #include <time.h>
- #include <stdlib.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #include <sys/mman.h>
- #include <linux/sockios.h>
- #include <linux/socket.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <sys/time.h>
- #include <sys/timeb.h>
- #include <math.h>//for pow
- #include "define.h"
- #include "FWUpdate.h"
- #include <unistd.h>
- struct SysConfigAndInfo *ShmSysConfigAndInfo;
- struct StatusCodeData *ShmStatusCodeData;
- struct CcsData *ShmCcsData;
- struct InternalComm *ShmInternalComm;
- struct InternalCommAC *ShmInternalCommAC;
- unsigned char buf_log_fwupdate[SIZE_OF_LOG_BUFFER];
- /*===========================================================================
- FUNCTION: Check_V2G_Flow_Status
- DESCRIPTION:
- PRE-CONDITION:
- INPUT:
- OUTPUT:
- GLOBAL VARIABLES:
- =============================================================================*/
- unsigned char Check_V2G_Flow_Status()
- {
- unsigned char result = 0;
- switch (ShmCcsData->CommProtocol)
- {
- case V2GT_MSG_PROTOCOL_DIN70121: //0
- {
- result = ShmCcsData->V2GMessage_DIN70121.PresentMsgFlowStatus;
- break;
- }
- case V2GT_MSG_PROTOCOL_ISO15118_2014: //1
- {
- result = ShmCcsData->V2GMessage_ISO15118_2014.PresentMsgFlowStatus;
- break;
- }
- case V2GT_MSG_PROTOCOL_ISO15118_2018: //2
- {
- result = ShmCcsData->V2GMessage_ISO15118_2018.PresentMsgFlowStatus;
- break;
- }
- default:
- break;
- }
- return result;
- }
- /*===========================================================================
- FUNCTION: StoreLogMsg
- DESCRIPTION:
- PRE-CONDITION:
- INPUT:
- OUTPUT:
- GLOBAL VARIABLES:
- =============================================================================*/
- #if SAVE_SYS_LOG_MSG_FWUPDATE_SWITCH == ENABLE
- int StoreLogMsg(unsigned char *DataString)
- {
- static unsigned char Buf[1024];
- static time_t CurrentTime;
- static struct tm *tm;
- static struct timeval tv;
- memset(Buf, 0, sizeof(Buf));
- CurrentTime = time(NULL);
- tm = localtime(&CurrentTime);
- gettimeofday(&tv, NULL); // get microseconds, 10^-6
- sprintf(Buf, "echo \"[%04d%02d%02d: %02d:%02d:%02d.%06d][FWUpdate][%d][%02d]%s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
- tm->tm_year + 1900,
- tm->tm_mon + 1,
- tm->tm_mday,
- tm->tm_hour,
- tm->tm_min,
- tm->tm_sec,
- tv.tv_usec,
- EVCOMM_SYS_INFO.CpState,
- Check_V2G_Flow_Status(),
- DataString,
- tm->tm_year + 1900,
- tm->tm_mon + 1);
- system(Buf);
- DEBUG_PRINTF_FWUPDATE_SYSTEM_LOG("[%02d:%02d:%02d.%06d][FWUpdate][%d][%02d]%s \n",
- tm->tm_hour,
- tm->tm_min,
- tm->tm_sec,
- tv.tv_usec,
- EVCOMM_SYS_INFO.CpState,
- Check_V2G_Flow_Status(),
- DataString);
- //Reset the buf_log_fwupdate Buffer, i.e. DataString
- memset(buf_log_fwupdate, 0, SIZE_OF_LOG_BUFFER);
- }
- #endif
- /*===========================================================================
- FUNCTION: fsize
- DESCRIPTION:
- PRE-CONDITION:
- INPUT:
- OUTPUT:
- GLOBAL VARIABLES:
- =============================================================================*/
- unsigned long fsize(char* filename)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("[fsize]image size: caculating...");
- unsigned long length = 0;
- FILE *file;
- file = fopen(filename, "rb");
- if (file == NULL)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("[fsize]fopen: fail");
- return 0;
- }
- else
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("[fsize]fopen: OK");
- fseek(file, 0, SEEK_END);
- length = (unsigned long) ftell(file);
- }
- fclose(file);
- sprintf(buf_log_fwupdate, "[fsize]image size: %ld bytes", length);
- SAVE_SYS_LOG_MSG_FWUPDATE(buf_log_fwupdate);
- return length;
- }
- /*===========================================================================
- FUNCTION: crc32
- DESCRIPTION:
- PRE-CONDITION:
- INPUT:
- OUTPUT:
- GLOBAL VARIABLES:
- =============================================================================*/
- uint32_t crc32(uint8_t *data, unsigned int length)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("[crc32]ongoing...");
- uint8_t i;
- uint32_t cnt = 0;
- uint32_t crc = 0xffffffff; // Initial value
- while(length--)
- {
- if(cnt > 33 && cnt < 48)
- {
- data++;
- }
- else
- {
- crc ^= *data++; // crc ^= *data; data++;
- for (i = 0; i < 8; ++i)
- {
- if (crc & 1)
- crc = (crc >> 1) ^ 0xEDB88320;// 0xEDB88320= reverse 0x04C11DB7
- else
- crc = (crc >> 1);
- }
- }
- cnt++;
- }
- return ~crc;
- }
- /*===========================================================================
- FUNCTION: ShareMemory_Init
- DESCRIPTION
- Initialize all share memories.
- PRE-CONDITION:
- INPUT:
- OUTPUT:
- GLOBAL VARIABLES:
- =============================================================================*/
- int ShareMemory_Init()
- {
- int MeterSMId;
- //[1/5] create ShmSysConfigAndInfo
- if((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("ShareMemory_Init:shmget ShmSysConfigAndInfo NG");
- return 0;
- }
- else if((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *)-1)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("ShareMemory_Init:shmat ShmSysConfigAndInfo NG");
- return 0;
- }
- //[2/5] create ShmStatusCodeData
- if((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("ShareMemory_Init:shmget ShmStatusCodeData NG");
- return 0;
- }
- else if((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *)-1)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("ShareMemory_Init:shmat ShmStatusCodeData NG");
- return 0;
- }
- //[3/5] create ShmCcsData
- if((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), 0777)) < 0)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("ShareMemory_Init:shmget ShmCcsData NG");
- return 0;
- }
- else if((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *)-1)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("ShareMemory_Init:shmat ShmCcsData NG");
- return 0;
- }
- //[4/5] create ShmInternalComm
- if((MeterSMId = shmget(ShmInternalCommKey, sizeof(struct InternalComm), IPC_CREAT | 0777)) < 0)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("ShareMemory_Init:shmget ShmInternalComm NG");
- return 0;
- }
- else if((ShmInternalComm = shmat(MeterSMId, NULL, 0)) == (void *)-1)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("ShareMemory_Init:shmat ShmInternalComm NG");
- return 0;
- }
- //[5/5] create ShmInternalCommAC
- if((MeterSMId = shmget(ShmInternalCommACKey, sizeof(struct InternalCommAC), IPC_CREAT | 0777)) < 0)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("ShareMemory_Init:shmget ShmInternalCommAC NG");
- return 0;
- }
- else if((ShmInternalCommAC = shmat(MeterSMId, NULL, 0)) == (void *)-1)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("ShareMemory_Init:shmat ShmInternalCommAC NG");
- return 0;
- }
- return 1;
- }
- /*===========================================================================
- FUNCTION: Update_MLO
- DESCRIPTION:
- PRE-CONDITION:
- INPUT:
- OUTPUT:
- GLOBAL VARIABLES:
- =============================================================================*/
- unsigned char Update_MLO()
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("-------- [MLO]start --------");
- unsigned char result = PASS;
- long int MaxLen = 48*1024*1024;
- unsigned long file_length = 0;
- unsigned char *ptr = malloc(MaxLen);
- if(ptr == NULL)
- {
- return FAIL;
- }
- memset(ptr, 0, MaxLen);
- file_length = fsize("/root/MLO");
- int fd = 0;
- int wrd = 0;
- FILE *file;
- fd = open("/dev/mtdblock0", O_RDWR);
- file = fopen("/root/MLO", "rb");
- if (fd < 0)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("[MLO]Can not open mtdblock0\n");
- free(ptr);
- return FAIL;
- }
- else if (file == NULL)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("[MLO]Can not open or find the downloaded image file\n");
- free(ptr);
- return FAIL;
- }
- else
- {
- //[VITAL] Closing unneccesary task to supoort rapid CAN data receving..
- SAVE_SYS_LOG_MSG_FWUPDATE("[MLO] closing SeccComm...");
- system("killall SeccComm");
- // Write image to flash
- SAVE_SYS_LOG_MSG_FWUPDATE("[MLO]Writing image to mtdblock0...");
- fread(ptr, sizeof(unsigned char), file_length, file);
- wrd = write(fd, ptr, file_length);
- close(fd);
- fclose(file);
- sprintf(buf_log_fwupdate, "[MLO]mtdblock0 - written length: %d bytes\n", wrd);
- SAVE_SYS_LOG_MSG_FWUPDATE(buf_log_fwupdate);
- if(wrd != file_length)
- {
- free(ptr);
- return FAIL;
- }
- }
- free(ptr);
- return result;
- }
- /*===========================================================================
- FUNCTION: IMAGE_TYPE_BOOTLOADER_UBOOT
- DESCRIPTION:
- PRE-CONDITION:
- INPUT:
- OUTPUT:
- GLOBAL VARIABLES:
- =============================================================================*/
- unsigned char Update_uboot()
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("-------- [uboot]start --------");
- unsigned char result = PASS;
- long int MaxLen = 48*1024*1024;
- unsigned long file_length = 0;
- unsigned char *ptr = malloc(MaxLen);
- if(ptr == NULL)
- {
- return FAIL;
- }
- memset(ptr, 0, MaxLen);
- file_length = fsize("/root/u-boot.img");
- int fd = 0;
- int wrd = 0;
- FILE *file;
- fd = open("/dev/mtdblock1", O_RDWR);
- file = fopen("/root/u-boot.img", "rb");
- if (fd < 0)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("[uboot]Can not open mtdblock1\n");
- free(ptr);
- return FAIL;
- }
- else if (file == NULL)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("[uboot]Can not open the downloaded image file\n");
- free(ptr);
- return FAIL;
- }
- else
- {
- //[VITAL] Closing unneccesary task to supoort rapid CAN data receving..
- SAVE_SYS_LOG_MSG_FWUPDATE("[uboot] closing SeccComm...");
- system("killall SeccComm");
- // Write image to flash
- SAVE_SYS_LOG_MSG_FWUPDATE("[uboot]Writing image to mtdblock1...");
- fread(ptr, sizeof(unsigned char), file_length, file);
- wrd = write(fd, ptr, file_length);
- close(fd);
- fclose(file);
- sprintf(buf_log_fwupdate, "[uboot]mtdblock1 - written length: %d bytes\n", wrd);
- SAVE_SYS_LOG_MSG_FWUPDATE(buf_log_fwupdate);
- if(wrd != file_length)
- {
- free(ptr);
- return FAIL;
- }
- else
- {
- // Open flash target mtdblock
- fd = open("/dev/mtdblock3", O_RDWR);
- if (fd < 0)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("[uboot]Can not open mtdblock3\n");
- free(ptr);
- return FAIL;
- }
- else if (file == NULL)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("[uboot]Can not open the downloaded image file\n");
- free(ptr);
- return FAIL;
- }
- else
- {
- // Write image to flash
- SAVE_SYS_LOG_MSG_FWUPDATE("[uboot]Writing image to mtdblock3...");
- wrd = write(fd, ptr, file_length);
- close(fd);
- sprintf(buf_log_fwupdate, "[uboot]mtdblock3 - written length: %d bytes\n", wrd);
- SAVE_SYS_LOG_MSG_FWUPDATE(buf_log_fwupdate);
- if(wrd != file_length)
- {
- free(ptr);
- return FAIL;
- }
- }
- }
- }
- return result;
- }
- /*===========================================================================
- FUNCTION: Update_DTB
- DESCRIPTION:
- PRE-CONDITION:
- INPUT:
- OUTPUT:
- GLOBAL VARIABLES:
- =============================================================================*/
- unsigned char Update_DTB()
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("-------- [DTB]start --------");
- unsigned char result = PASS;
- long int MaxLen = 48*1024*1024;
- unsigned long file_length = 0;
- unsigned char *ptr = malloc(MaxLen);
- if(ptr == NULL)
- {
- return FAIL;
- }
- memset(ptr, 0, MaxLen);
- file_length = fsize("/root/am335x-evm.dtb");
- int fd = 0;
- int wrd = 0;
- FILE *file;
- fd = open("/dev/mtdblock4", O_RDWR);
- file = fopen("/root/am335x-evm.dtb", "rb");
- if (fd < 0)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("[DTB]Can not open mtdblock4\n");
- free(ptr);
- return FAIL;
- }
- else if (file == NULL)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("[DTB]Can not open the downloaded image file\n");
- free(ptr);
- return FAIL;
- }
- else
- {
- //[VITAL] Closing unneccesary task to supoort rapid CAN data receving..
- SAVE_SYS_LOG_MSG_FWUPDATE("[DTB] closing SeccComm...");
- system("killall SeccComm");
- // Write image to flash
- SAVE_SYS_LOG_MSG_FWUPDATE("[DTB]Writing image to mtdblock4...");
- fread(ptr, sizeof(unsigned char), file_length, file);
- wrd = write(fd, ptr, file_length);
- close(fd);
- fclose(file);
- sprintf(buf_log_fwupdate, "[DTB]mtdblock4 - written length: %d bytes\n", wrd);
- SAVE_SYS_LOG_MSG_FWUPDATE(buf_log_fwupdate);
- if(wrd != file_length)
- {
- free(ptr);
- return FAIL;
- }
- else
- {
- // Open flash target mtdblock
- fd = open("/dev/mtdblock5", O_RDWR);
- if (fd < 0)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("[DTB]Can not open mtdblock5");
- free(ptr);
- return FAIL;
- }
- else
- {
- // Write image to flash
- SAVE_SYS_LOG_MSG_FWUPDATE("[DTB]Writing image to mtdblock5...");
- wrd=write(fd, ptr, file_length);
- close(fd);
- sprintf(buf_log_fwupdate, "[DTB]mtdblock5 - written length: %d bytes\n", wrd);
- SAVE_SYS_LOG_MSG_FWUPDATE(buf_log_fwupdate);
- if(wrd != file_length)
- {
- free(ptr);
- return FAIL;
- }
- }
- }
- }
- return result;
- }
- /*===========================================================================
- FUNCTION: Update_zImage
- DESCRIPTION:
- PRE-CONDITION:
- INPUT:
- OUTPUT:
- GLOBAL VARIABLES:
- =============================================================================*/
- unsigned char Update_zImage()
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("-------- [zImage]start --------");
- unsigned char result = PASS;
- long int MaxLen = 48*1024*1024;
- unsigned long file_length = 0;
- unsigned char *ptr = malloc(MaxLen);
- if(ptr == NULL)
- {
- return FAIL;
- }
- memset(ptr, 0, MaxLen);
- file_length = fsize("/root/zImage");
- int fd = 0;
- int wrd = 0;
- FILE *file;
- fd = open("/dev/mtdblock6", O_RDWR);
- file = fopen("/root/zImage", "rb");
- if (fd < 0)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("[zImage]Can not open mtdblock6\n");
- free(ptr);
- return FAIL;
- }
- else if (file == NULL)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("[zImage]Can not open the downloaded image file\n");
- free(ptr);
- return FAIL;
- }
- else
- {
- //[VITAL] Closing unneccesary task to supoort rapid CAN data receving..
- SAVE_SYS_LOG_MSG_FWUPDATE("[zImage] closing SeccComm...");
- system("killall SeccComm");
- // Write image to flash
- SAVE_SYS_LOG_MSG_FWUPDATE("[zImage]Writing image to mtdblock6...");
- fread(ptr, sizeof(unsigned char), file_length, file);
- wrd = write(fd, ptr, file_length);
- close(fd);
- fclose(file);
- sprintf(buf_log_fwupdate, "[zImage]mtdblock6 - written length: %d bytes\n", wrd);
- SAVE_SYS_LOG_MSG_FWUPDATE(buf_log_fwupdate);
- if(wrd != file_length)
- {
- free(ptr);
- return FAIL;
- }
- else
- {
- // Open flash target mtdblock
- fd = open("/dev/mtdblock7", O_RDWR);
- if (fd < 0)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("[zImage]Can not open mtdblock7");
- free(ptr);
- return FAIL;
- }
- else
- {
- // Write image to flash
- SAVE_SYS_LOG_MSG_FWUPDATE("[zImage]Writing image to mtdblock7...");
- wrd = write(fd, ptr, file_length);
- close(fd);
- sprintf(buf_log_fwupdate, "[zImage]mtdblock7 - written length: %d bytes\n", wrd);
- SAVE_SYS_LOG_MSG_FWUPDATE(buf_log_fwupdate);
- if(wrd != file_length)
- {
- free(ptr);
- return FAIL;
- }
- }
- }
- }
- return result;
- }
- /*===========================================================================
- FUNCTION: Update_ramdisk
- DESCRIPTION:
- PRE-CONDITION:
- INPUT:
- OUTPUT:
- GLOBAL VARIABLES:
- =============================================================================*/
- unsigned char Update_ramdisk()
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("-------- [ramdisk]start --------");
- unsigned char result = PASS;
- long int MaxLen = 48*1024*1024;
- unsigned long file_length = 0;
- unsigned char *ptr = malloc(MaxLen);
- if(ptr == NULL)
- {
- return FAIL;
- }
- memset(ptr, 0, MaxLen);
- file_length = fsize("/root/ramdisk.gz");
- int fd = 0;
- int wrd = 0;
- FILE *file;
- fd = open("/dev/mtdblock8", O_RDWR);
- file = fopen("/root/ramdisk.gz", "rb");
- if(fd < 0)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("[ramdisk]UpdateRootfs NG - can not open rootfs\n");
- return FAIL;
- }
- else if (file == NULL)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("[ramdisk]Can not open the downloaded image file\n");
- return FAIL;
- }
- else
- {
- //[VITAL] Closing unneccesary task to supoort rapid CAN data receving..
- SAVE_SYS_LOG_MSG_FWUPDATE("[ramdisk] closing SeccComm...");
- system("killall SeccComm");
- SAVE_SYS_LOG_MSG_FWUPDATE("[ramdisk]Writing image to mtdblock8...");
- fread(ptr, sizeof(unsigned char), file_length, file);
- wrd = write(fd, ptr, file_length);
- close(fd);
- fclose(file);
- sprintf(buf_log_fwupdate, "[ramdisk]mtdblock8 - written length: %d bytes\n", wrd);
- SAVE_SYS_LOG_MSG_FWUPDATE(buf_log_fwupdate);
- if(wrd != file_length)
- {
- free(ptr);
- return FAIL;
- }
- else
- {
- fd = open("/dev/mtdblock9", O_RDWR);
- if(fd < 0)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("[ramdisk]UpdateRootfs NG - can not open rootfs");
- free(ptr);
- return FAIL;
- }
- SAVE_SYS_LOG_MSG_FWUPDATE("[ramdisk]Writing image to mtdblock9...");
- wrd = write(fd, ptr, file_length);
- close(fd);
- sprintf(buf_log_fwupdate, "[ramdisk]mtdblock9 - written length: %d bytes\n", wrd);
- SAVE_SYS_LOG_MSG_FWUPDATE(buf_log_fwupdate);
- if(wrd != file_length)
- {
- free(ptr);
- return FAIL;
- }
- else
- {
- //result = PASS;
- SAVE_SYS_LOG_MSG_FWUPDATE("[ramdisk]deleting Storage-root files.\n");
- system("cd /Storage;rm -rf root/*");
- #if (FIRMWARE_VERSION_COMPILE_SETTING_RELEASE_MODE == DISABLE)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("[ramdisk]sync...");
- system("sync");
- }
- #endif
- }
- }
- }
- return result;
- }
- /*===========================================================================
- FUNCTION: Update_user_config
- DESCRIPTION:
- PRE-CONDITION:
- INPUT:
- OUTPUT:
- GLOBAL VARIABLES:
- =============================================================================*/
- unsigned char Update_user_config()
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("-------- [user_config]start --------");
- unsigned char result = PASS;
- long int MaxLen = 48*1024*1024;
- unsigned long file_length = 0;
- unsigned char *ptr = malloc(MaxLen);
- if(ptr == NULL)
- {
- return FAIL;
- }
- memset(ptr, 0, MaxLen);
- file_length = fsize("/root/FactoryConfig.bin");
- // Open flash target mtdblock
- int fd = 0;
- int wrd = 0;
- FILE *file;
- fd = open("/dev/mtdblock10", O_RDWR);
- file = fopen("/root/FactoryConfig.bin", "rb");
- if (fd < 0)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("[user_config]Can not open mtdblock10\n");
- free(ptr);
- return FAIL;
- }
- else if (file == NULL)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("[user_config]Can not open the downloaded image file\n");
- free(ptr);
- return FAIL;
- }
- else
- {
- //[VITAL] Closing unneccesary task to supoort rapid CAN data receving..
- SAVE_SYS_LOG_MSG_FWUPDATE("[user_config] closing SeccComm...");
- system("killall SeccComm");
- // Write image to flash
- SAVE_SYS_LOG_MSG_FWUPDATE("[user_config]Writing image to mtdblock10...");
- fread(ptr, sizeof(unsigned char), file_length, file);
- wrd = write(fd, ptr, file_length);
- close(fd);
- fclose(file);
- sprintf(buf_log_fwupdate,
- "[user_config]mtdblock10 - written length: %d bytes\n",
- wrd);
- SAVE_SYS_LOG_MSG_FWUPDATE(buf_log_fwupdate);
- if(wrd != file_length)
- {
- free(ptr);
- return FAIL;
- }
- else
- {
- // Open flash target mtdblock
- fd = open("/dev/mtdblock11", O_RDWR);
- if (fd < 0)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("[user_config]Can not open mtdblock11");
- free(ptr);
- return FAIL;
- }
- else
- {
- // Write image to flash
- SAVE_SYS_LOG_MSG_FWUPDATE("[user_config]Writing image to mtdblock11...");
- wrd = write(fd, ptr, file_length);
- close(fd);
- sprintf(buf_log_fwupdate,
- "[user_config]mtdblock11 - written length: %d bytes\n",
- wrd);
- SAVE_SYS_LOG_MSG_FWUPDATE(buf_log_fwupdate);
- if(wrd != file_length)
- {
- free(ptr);
- return FAIL;
- }
- }
- }
- }
- return result;
- }
- /*===========================================================================
- FUNCTION: Update_root_app
- DESCRIPTION:
- PRE-CONDITION:
- INPUT:
- OUTPUT:
- GLOBAL VARIABLES:
- =============================================================================*/
- unsigned char Update_root_app()
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("-------- [root_app]start --------");
- unsigned char result = 0;
- //STEP 1: Unzip tmp (.tar.gz) (also checking the file completion)
- system("tar -C /Storage/ -zxvf /Storage/tmp"); //.tar.gz
- //[zip command] tar -zcvf tmp image/
- //STEP 2: Checking File Existance (to-be enhanced)
- FILE *file;
- file = fopen("/root/ramdisk_app_type_5.tar.gz", "rb");
- if (file == NULL)
- {
- result = 0; //Failed on update
- SAVE_SYS_LOG_MSG_FWUPDATE("[root_app]checking downloaded file: FAIL");
- system("rm -rf /Storage/image/");
- return 0;
- }
- else
- {
- fclose(file);
- SAVE_SYS_LOG_MSG_FWUPDATE("[root_app]checking downloaded file: OK");
- SAVE_SYS_LOG_MSG_FWUPDATE("[root_app]updtating...");
- system("cp -rfv /Storage/image/* /Storage/root/");
- system("rm -rf /Storage/image/");
- #if (FIRMWARE_VERSION_COMPILE_SETTING_RELEASE_MODE == DISABLE)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("[root_app]sync...");
- system("sync");
- }
- #endif
- SAVE_SYS_LOG_MSG_FWUPDATE("[root_app]update: done");
- result = 1; //Successful on update
- }
- return result;
- }
- /*===========================================================================
- FUNCTION: Update_ALL
- DESCRIPTION:
- PRE-CONDITION:
- INPUT:
- OUTPUT:
- GLOBAL VARIABLES:
- =============================================================================*/
- int Update_ALL()
- {
- char result;
- result = FAIL;
- //[VITAL] Closing unneccesary task to supoort rapid CAN data receving..
- SAVE_SYS_LOG_MSG_FWUPDATE("[Update_ALL] closing SeccComm...");
- system("killall SeccComm");
- if (Update_MLO() == FAIL)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("[Update]MLO: fail");
- }
- else if (Update_uboot() == FAIL)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("[Update]MLO: fail");
- }
- else if (Update_DTB() == FAIL)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("[Update]DTB: fail");
- }
- else if (Update_zImage() == FAIL)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("[Update]zImage: fail");
- }
- else if (Update_ramdisk() == FAIL)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("[Update]ramdisk: fail");
- }
- /*
- else if (Update_user_config() == FAIL)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("[Update]configuration: fail");
- }
- */
- //else if (Update_root_app() == FAIL)
- //{
- //}
- else
- {
- result = PASS;
- }
- SAVE_SYS_LOG_MSG_FWUPDATE("-------------[FW Update Summary]-----------");
- if (result == FAIL)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("[ALL]Fail");
- CSUCOMMDC_TASK_FLAG.FW_Update_result = FAIL;
- }
- else if (result == PASS)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("[ALL]Pass");
- CSUCOMMDC_TASK_FLAG.FW_Update_result = PASS;
- }
- else
- {
- CSUCOMMDC_TASK_FLAG.FW_Update_result = FAIL;
- sprintf(buf_log_fwupdate, "[ALL]unexpected response content (0x%X)", result);
- SAVE_SYS_LOG_MSG_FWUPDATE(buf_log_fwupdate);
- }
- CSUCOMMDC_TASK_FLAG.FW_Update_Done = TRUE; //sending CAN Res msg to CSU and reboot
- return result;
- }
- /*===========================================================================
- FUNCTION: FWImage_HeaderParser
- DESCRIPTION:
- PRE-CONDITION:
- INPUT:
- OUTPUT:
- GLOBAL VARIABLES:
- =============================================================================*/
- int FWImage_HeaderParser(struct FwImageDataType *image)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("[HeaderParser]starting...");
- //Step 0: Initialize
- unsigned char result = 0;
- long int MaxLen = 48*1024*1024;
- unsigned char *ptr = malloc(MaxLen);
- if(ptr == NULL)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("[HeaderParser]malloc: fail");
- free(ptr);
- return FAIL;
- }
- else
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("[HeaderParser]malloc: OK");
- }
- memset(ptr, 0, MaxLen);
- FILE *file;
- file = fopen(image->path, "rb");
- if (file == NULL)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("[HeaderParser]fopen: fail");
- free(ptr);
- return FAIL;
- }
- else
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("[HeaderParser]fopen: OK");
- }
- fread(ptr, sizeof(unsigned char), image->size, file);
- fclose(file);
- //Step 1: Check FW Image Type
- image->type = (((unsigned int)ptr[16])<<24 | ((unsigned int)ptr[17])<<16 | ((unsigned int)ptr[18])<<8 | ((unsigned int)ptr[19]));
- if (image->type != FW_IMAGE_TYPE_CCS_DTB &&
- image->type != FW_IMAGE_TYPE_CCS_UBOOT &&
- image->type != FW_IMAGE_TYPE_CCS_ZIMAGE &&
- image->type != FW_IMAGE_TYPE_CCS_RAMDISK)
- {
- sprintf(buf_log_fwupdate,
- "[HeaderParser]incorrect type (0x%X)",
- image->type);
- SAVE_SYS_LOG_MSG_FWUPDATE(buf_log_fwupdate);
- free(ptr);
- return FAIL;
- }
- //Step 2: Check FW Image Length
- unsigned long size_header = 0;
- size_header = (((unsigned int)ptr[20])<<24 | ((unsigned int)ptr[21])<<16 | ((unsigned int)ptr[22])<<8 | ((unsigned int)ptr[23]));
- if(image->size != (size_header + 48))
- {
- sprintf(buf_log_fwupdate,
- "[HeaderParser]image length is not matched (header = %d, calculated = %d, bytes)",
- size_header,
- image->size);
- SAVE_SYS_LOG_MSG_FWUPDATE(buf_log_fwupdate);
- free(ptr);
- return FAIL;
- }
- //Step 3: Check CRC
- unsigned int crc_cal = 0;
- image->crc = ((unsigned int)ptr[34])<<24 | ((unsigned int)ptr[35])<<16 | ((unsigned int)ptr[36])<<8 | ((unsigned int)ptr[37]);
- crc_cal = crc32(ptr, image->size);
- if(image->crc != crc_cal)
- {
- sprintf(buf_log_fwupdate,
- "[HeaderParser]CRC error (header = 0x%X, calculated = 0x%X)",
- image->crc,
- crc_cal);
- SAVE_SYS_LOG_MSG_FWUPDATE(buf_log_fwupdate);
- free(ptr);
- return FAIL;
- }
- else
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("[HeaderParser]CRC: OK");
- }
- free(ptr);
- return PASS;
- }
- /*===========================================================================
- FUNCTION: FWImage_Normalize
- DESCRIPTION:
- PRE-CONDITION:
- INPUT:
- OUTPUT:
- GLOBAL VARIABLES:
- =============================================================================*/
- int FWImage_Normalize(struct FwImageDataType *image)
- {
- //Step 0: Initialize
- unsigned char result = 0;
- long int MaxLen = 48*1024*1024;
- unsigned char *ptr = malloc(MaxLen);
- if(ptr == NULL)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("[Normalize] malloc fail");
- free(ptr);
- return FAIL;
- }
- memset(ptr, 0, MaxLen);
- FILE *file;
- file = fopen(image->path, "rb");
- if (file == NULL)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("[Normalize] fopen fail");
- free(ptr);
- return FAIL;
- }
- fread(ptr, sizeof(unsigned char), image->size, file);
- fclose(file);
- //Step 1: Delete the original image
- char buf[64];
- memset(buf, 0, 64);
- sprintf(buf, "rm -rf %s", image->path);
- system(buf);
- //Step 2: Remove header and create a renamed image accoring to its firmware type
- struct FwImageDataType image_new;
- switch (image->type)
- {
- case FW_IMAGE_TYPE_CCS_UBOOT: //0x10000007
- {
- sprintf(image_new.path, "/root/u-boot.img");
- break;
- }
- case FW_IMAGE_TYPE_CCS_DTB: //0x10000008
- {
- sprintf(image_new.path, "/root/am335x-evm.dtb");
- break;
- }
- case FW_IMAGE_TYPE_CCS_ZIMAGE: //0x10000009
- {
- sprintf(image_new.path, "/root/zImage");
- break;
- }
- case FW_IMAGE_TYPE_CCS_RAMDISK: //0x1000000A
- {
- sprintf(image_new.path, "/root/ramdisk.gz");
- break;
- }
- default:
- {
- break;
- }
- }
- memset(buf, 0, 64);
- sprintf(buf, "rm -rf %s", image_new.path);
- system(buf);
- SAVE_SYS_LOG_MSG_FWUPDATE("[Normalize]creating new image: ongoing...");
- image_new.size = image->size - 48;
- file = fopen(image_new.path, "wb+");
- fwrite(&ptr[48], image_new.size, 1, file); //48: header size
- fclose(file);
- free(ptr);
- SAVE_SYS_LOG_MSG_FWUPDATE("[Normalize]creating new image: done");
- return PASS;
- }
- /*===========================================================================
- FUNCTION: FWImage_Init
- DESCRIPTION:
- PRE-CONDITION:
- INPUT:
- 1. image file name: "ccs.image"
- OUTPUT:
- 1. type: FW image type
- - 0: file is not exist or error occours
- - 0x10000007: FW_IMAGE_TYPE_CCS_UBOOT
- - 0x10000008: FW_IMAGE_TYPE_CCS_DTB
- - 0x10000009: FW_IMAGE_TYPE_CCS_ZIMAGE
- - 0x1000000A: FW_IMAGE_TYPE_CCS_RAMDISK
- GLOBAL VARIABLES:
- =============================================================================*/
- unsigned int FWImage_Init()
- {
- struct FwImageDataType image;
- sprintf(image.path, "/root/ccs.image");
- //STEP 1: Check if image exist?
- image.size = fsize(image.path);
- if (image.size == 0)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("[Init][Error]image does not exist.");
- return FAIL; //FAIL: 0
- }
- //STEP 2: Check FW image type = ?
- if (FWImage_HeaderParser(&image) != PASS)
- {
- return FAIL; //FAIL: 0
- }
- //STEP 4: Remove header and rename the image file accoring to its firmware type
- if (FWImage_Normalize(&image) != PASS)
- {
- return FAIL; //FAIL: 0
- }
- //Step 5: return result
- return image.type;
- }
- /*===========================================================================
- FUNCTION: Update_viaWebService
- DESCRIPTION:
- PRE-CONDITION:
- INPUT:
- OUTPUT:
- GLOBAL VARIABLES:
- =============================================================================*/
- int Update_viaWebService()
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("-------- [viaWebService]start --------");
- unsigned char result = FAIL;
- unsigned int type = 0;
- type = FWImage_Init();
- switch (type)
- {
- case FW_IMAGE_TYPE_CCS_UBOOT:
- {
- result = Update_uboot();
- break;
- }
- case FW_IMAGE_TYPE_CCS_DTB:
- {
- result = Update_DTB();
- break;
- }
- case FW_IMAGE_TYPE_CCS_ZIMAGE:
- {
- result = Update_zImage();
- break;
- }
- case FW_IMAGE_TYPE_CCS_RAMDISK:
- {
- result = Update_ramdisk();
- break;
- }
- default:
- {
- result = FAIL;
- sprintf(buf_log_fwupdate,
- "[Error]undefined FW image type or file not exist.(0x%X)",
- type
- );
- SAVE_SYS_LOG_MSG_FWUPDATE(buf_log_fwupdate);
- break;
- }
- }
- if (result == PASS)
- {
- CSUCOMMDC_TASK_FLAG.FW_Update_result = PASS;
- }
- else
- {
- CSUCOMMDC_TASK_FLAG.FW_Update_result = FAIL;
- }
- CSUCOMMDC_TASK_FLAG.FW_Update_Done = TRUE; //sending CAN Res msg to CSU and reboot
- }
- /*===========================================================================
- FUNCTION: main
- DESCRIPTION:
- PRE-CONDITION:
- INPUT:
- OUTPUT:
- GLOBAL VARIABLES:
- =============================================================================*/
- int main(int argc, char *argv[])
- {
- // ======== [STEP 1/5] Initialize Share Memory ========
- if(ShareMemory_Init() == 0)
- {
- SAVE_SYS_LOG_MSG_FWUPDATE("[main]ShareMemory_Init NG");
- if(ShmStatusCodeData != NULL)
- {
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
- }
- sleep(5);
- return 0;
- }
- //Arguments Parsing for Commands
- if (argv[1][0] == '-' )
- {
- switch (argv[1][1])
- {
- case 'h': //help
- {
- sprintf(buf_log_fwupdate,
- " ================= [HELP] CCS FW Update ==============");
- SAVE_SYS_LOG_MSG_FWUPDATE(buf_log_fwupdate);
- sprintf(buf_log_fwupdate,
- " -h \t :help");
- SAVE_SYS_LOG_MSG_FWUPDATE(buf_log_fwupdate);
- sprintf(buf_log_fwupdate,
- " -w \t :image update from WebService (with header, named as ccs.image)");
- SAVE_SYS_LOG_MSG_FWUPDATE(buf_log_fwupdate);
- sprintf(buf_log_fwupdate,
- " -a \t :full images update \t(no header, named as original names)");
- SAVE_SYS_LOG_MSG_FWUPDATE(buf_log_fwupdate);
- sprintf(buf_log_fwupdate,
- " -m \t :MLO update \t\t(no header, named as MLO)");
- SAVE_SYS_LOG_MSG_FWUPDATE(buf_log_fwupdate);
- sprintf(buf_log_fwupdate,
- " -u \t :uboot update \t\t(no header, named as u-boot.img)");
- SAVE_SYS_LOG_MSG_FWUPDATE(buf_log_fwupdate);
- sprintf(buf_log_fwupdate,
- " -d \t :dtb update \t\t(no header, named as am335x-evm.dtb)");
- SAVE_SYS_LOG_MSG_FWUPDATE(buf_log_fwupdate);
- sprintf(buf_log_fwupdate,
- " -z \t :zImage update \t(no header, named as zImage)");
- SAVE_SYS_LOG_MSG_FWUPDATE(buf_log_fwupdate);
- sprintf(buf_log_fwupdate,
- " -r \t :ramdisk update \t(no header, named as ramdisk.gz)");
- SAVE_SYS_LOG_MSG_FWUPDATE(buf_log_fwupdate);
- break;
- }
- case 'w': //image update from WebService
- {
- Update_viaWebService();
- break;
- }
- case 'a': //all image update
- {
- Update_ALL();
- break;
- }
- case 'm': //MLO update
- {
- Update_MLO();
- break;
- }
- case 'u': //uboot update
- {
- Update_uboot();
- break;
- }
- case 'd': //dtb update
- {
- Update_DTB();
- break;
- }
- case 'z': //zImage update
- {
- Update_zImage();
- break;
- }
- case 'r': //ramdisk update
- {
- Update_ramdisk();
- break;
- }
- default:
- {
- sprintf(buf_log_fwupdate, "Unknown option -%c\n\n", argv[1][1]);
- SAVE_SYS_LOG_MSG_FWUPDATE(buf_log_fwupdate);
- break;
- }
- }
- }
- }
|