1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321 |
- /*
- * Module_Upgrade.c
- *
- * Created on: 2020-01-21
- * Author: Jerry Wang
- * Version: D0.03
- */
- #include "Module_Upgrade.h"
- //==================================
- // PRINT OUT LOG FORMAT
- //==================================
- #define DEBUG_INFO(format, args...) storeLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
- #define DEBUG_WARN(format, args...) storeLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
- #define DEBUG_ERROR(format, args...) storeLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
- #define SystemLogMessage
- //#define ConsloePrintLog
- #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
- #define PASS 1
- #define FAIL -1
- #define YES 1
- #define NO 0
- #define ON 1
- #define OFF 0
- #define HEADER_LENGTH 48
- struct SysConfigAndInfo *ShmSysConfigAndInfo;
- struct StatusCodeData *ShmStatusCodeData;
- struct FanModuleData *ShmFanModuleData;
- int storeLogMsg(const char *fmt, ...)
- {
- char Buf[4096+256];
- char buffer[4096];
- time_t CurrentTime;
- struct tm *tm;
- struct timeval tv;
- va_list args;
- va_start(args, fmt);
- int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
- va_end(args);
- memset(Buf,0,sizeof(Buf));
- CurrentTime = time(NULL);
- tm=localtime(&CurrentTime);
- gettimeofday(&tv, NULL); // get microseconds, 10^-6
- sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s\" >> /Storage/SystemLog/[%04d.%02d]Module_UpgradeLog",
- tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec,
- buffer,
- tm->tm_year+1900,tm->tm_mon+1);
- #ifdef SystemLogMessage
- system(Buf);
- #endif
- #ifdef ConsloePrintLog
- printf("[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec, buffer);
- #endif
- return rc;
- }
- int DiffTimebByUpgrade(struct timeb ST, struct timeb ET)
- {
- //return milli-second
- unsigned int StartTime,StopTime;
- StartTime=(unsigned int)ST.time;
- StopTime=(unsigned int)ET.time;
- return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
- }
- unsigned char *memcat(unsigned char *dest, unsigned int dest_len, unsigned char *src, unsigned int src_len)
- {
- memcpy(dest+dest_len, src, src_len);
- return dest;
- }
- uint32_t crc32(uint8_t *data, unsigned int length)
- {
- 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;
- }
- int runShellCmd(const char*cmd)
- {
- int result = FAIL;
- char buf[256];
- FILE *fp;
- fp = popen(cmd, "r");
- if(fp != NULL)
- {
- while(fgets(buf, sizeof(buf), fp) != NULL)
- {
- DEBUG_INFO("%s\n", buf);
- }
- result = PASS;
- }
- pclose(fp);
- return result;
- }
- int config_upgrade_flag(unsigned int upgradeFlag, unsigned char isSetFlag)
- {
- int result = FAIL;
- int fd;
- unsigned char envFlash[512*1024];
- system("rm -f /tmp/envFlash");
- DEBUG_INFO("Read /dev/mtd2.\n");
- runShellCmd("nanddump -s 0x0 -l 0x80000 -f /tmp/envFlash /dev/mtd2");
- fd = open("/tmp/envFlash", O_RDWR);
- if(fd < 0)
- {
- DEBUG_ERROR("Open envFlash NG.\n");
- }
- else
- {
- read(fd,envFlash,ARRAY_SIZE(envFlash));
- close(fd);
- system("rm -f /tmp/envFlash");
- envFlash[upgradeFlag] = (isSetFlag?0x00:0xff);
- fd = open("/tmp/envFlash", O_RDWR | O_CREAT | O_EXCL);
- if(fd < 0)
- {
- DEBUG_ERROR("Open envFlash NG.\n");
- }
- else
- {
- write(fd,envFlash,ARRAY_SIZE(envFlash));
- close(fd);
- DEBUG_INFO("Erase /dev/mtd2.\n");
- runShellCmd("flash_erase /dev/mtd2 0 0");
- DEBUG_INFO("Write /dev/mtd2.\n");
- runShellCmd("nandwrite -p /dev/mtd2 /tmp/envFlash");
- system("rm -f /tmp/envFlash");
- result = PASS;
- }
- }
- return result;
- }
- int Upgrade_Flash(unsigned int Type,char *SourcePath,char *ModelName)
- {
- int result = FAIL;
- char cmdBuf[128];
- long int MaxLen=48*1024*1024, ImageLen=0;
- unsigned int ImageCRC=0, DataLength=0;
- int wrd,fd;
- // space max size set
- switch(Type)
- {
- case CSU_MLO:
- MaxLen = 0.5*1024*1024;
- DEBUG_INFO("Image type: MLO\n");
- break;
- case CSU_BOOTLOADER:
- MaxLen = 1*1024*1024;
- DEBUG_INFO("Image type: U-Boot\n");
- break;
- case CSU_KERNEL_CONFIGURATION:
- MaxLen = 0.5*1024*1024;
- DEBUG_INFO("Image type: DTB\n");
- break;
- case CSU_KERNEL_IMAGE:
- MaxLen = 10*1024*1024;
- DEBUG_INFO("Image type: Kernel\n");
- break;
- case CSU_ROOT_FILE_SYSTEM:
- MaxLen = 48*1024*1024;
- DEBUG_INFO("Image type: Root fs\n");
- break;
- case CSU_USER_CONFIGURATION:
- MaxLen = 6*1024*1024;
- DEBUG_INFO("Image type: Config\n");
- break;
- default:
- break;
- }
- fd = open(SourcePath, O_RDONLY);
- if(fd < 0)
- {
- DEBUG_ERROR("UpdateRootfs NG - can not open image file %s\n", SourcePath);
- return result;
- }
- unsigned char *ptr = malloc(MaxLen+HEADER_LENGTH);
- memset(ptr,0xFF,MaxLen+HEADER_LENGTH);
- //get the image length
- ImageLen = read(fd,ptr,MaxLen+HEADER_LENGTH);
- close(fd);
- // Delete source file, in order to down size ram disk usage
- sprintf(cmdBuf, "rm -f %s", SourcePath);
- system(cmdBuf);
- DEBUG_INFO("Delete source file.\n");
- //read out the header
- int isModelNameOK = PASS;
- if((ModelName[0] != ptr[0]) ||
- (ModelName[1] != ptr[1]) ||
- (ModelName[7] != ptr[7]) ||
- (ModelName[8] != ptr[8]) ||
- (ModelName[9] != ptr[9]) ||
- (ModelName[11] != ptr[11]) ||
- (ModelName[12] != ptr[12]) ||
- (ModelName[13] != ptr[13]))
- {
- isModelNameOK = FAIL;
- }
- if(isModelNameOK == FAIL)
- {
- DEBUG_ERROR("Model name mismatch.\n");
- }
- else
- {
- // check if the firmware type is correct
- if(Type == (((unsigned int)ptr[16])<<24 | ((unsigned int)ptr[17])<<16 | ((unsigned int)ptr[18])<<8 | ((unsigned int)ptr[19])))
- {
- if((ImageLen-HEADER_LENGTH) == (((unsigned int)ptr[20])<<24 | ((unsigned int)ptr[21])<<16 | ((unsigned int)ptr[22])<<8 | ((unsigned int)ptr[23])))
- {
- DataLength = ImageLen-HEADER_LENGTH;
- // get CRC in the header
- ImageCRC = ((unsigned int)ptr[34])<<24 | ((unsigned int)ptr[35])<<16 | ((unsigned int)ptr[36])<<8 | ((unsigned int)ptr[37]);
- // calculate the image CRC
- DEBUG_INFO("CRC32 in image: 0x%08X\n",ImageCRC);
- DEBUG_INFO("CRC32 by calculation: 0x%08X\n",crc32(ptr,ImageLen));
- if(crc32(ptr,ImageLen) == ImageCRC)
- {
- // Write image to target flash block
- switch(Type)
- {
- case CSU_MLO:
- fd = open("/mnt/imgBuffer", O_RDWR | O_CREAT | O_EXCL);
- if (fd < 0)
- {
- DEBUG_ERROR("Can not create MLO image buffer file.\n");
- result = FAIL;
- }
- else
- {
- // Write image to flash
- DEBUG_INFO("Writing image to image buffer file...\n");
- wrd=write(fd, ptr+HEADER_LENGTH, DataLength);
- close(fd);
- DEBUG_INFO(">> imgBuffer Written length: 0x%x\n", wrd);
- if(wrd != DataLength)
- {
- result = FAIL;
- }
- else
- {
- DEBUG_INFO("Erase /dev/mtd0.\n");
- runShellCmd("flash_erase /dev/mtd0 0 0");
- DEBUG_INFO("Write /dev/mtd0.\n");
- runShellCmd("nandwrite -p /dev/mtd0 /mnt/imgBuffer");
- system("rm -f /mnt/imgBuffer");
- result = PASS;
- }
- }
- break;
- case CSU_BOOTLOADER:
- fd = open("/mnt/imgBuffer", O_RDWR | O_CREAT | O_EXCL);
- if (fd < 0)
- {
- DEBUG_ERROR("Can not create uboot image buffer file.\n");
- result = FAIL;
- }
- else
- {
- // Write image to flash
- DEBUG_INFO("Writing image to image buffer file...\n");
- wrd=write(fd, ptr+HEADER_LENGTH, DataLength);
- close(fd);
- DEBUG_INFO(">> imgBuffer written length: 0x%x\n", wrd);
- if(wrd != DataLength)
- {
- result = FAIL;
- }
- else
- {
- DEBUG_INFO("Erase /dev/mtd1.\n");
- runShellCmd("flash_erase /dev/mtd1 0 0");
- DEBUG_INFO("Write /dev/mtd1.\n");
- runShellCmd("nandwrite -p /dev/mtd1 /mnt/imgBuffer");
- DEBUG_INFO("Erase /dev/mtd3.\n");
- runShellCmd("flash_erase /dev/mtd3 0 0");
- DEBUG_INFO("Write /dev/mtd3.\n");
- runShellCmd("nandwrite -p /dev/mtd3 /mnt/imgBuffer");
- system("rm -f /mnt/imgBuffer");
- result = PASS;
- }
- }
- break;
- case CSU_KERNEL_CONFIGURATION:
- fd = open("/mnt/imgBuffer", O_RDWR | O_CREAT | O_EXCL);
- if (fd < 0)
- {
- DEBUG_ERROR("Can not create DTB image buffer file.\n");
- result = FAIL;
- }
- else
- {
- // Write image to flash
- DEBUG_INFO("Writing image to image buffer file...\n");
- wrd=write(fd, ptr+HEADER_LENGTH, DataLength);
- close(fd);
- DEBUG_INFO(">> imgBuffer written length: 0x%x\n", wrd);
- if(wrd != DataLength)
- {
- result = FAIL;
- }
- else
- {
- config_upgrade_flag(UPGRADE_FLAG_DTS, ON);
- DEBUG_INFO("Erase /dev/mtd4.\n");
- runShellCmd("flash_erase /dev/mtd4 0 0");
- DEBUG_INFO("Write /dev/mtd4.\n");
- runShellCmd("nandwrite -p /dev/mtd4 /mnt/imgBuffer");
- config_upgrade_flag(UPGRADE_FLAG_DTS, OFF);
- DEBUG_INFO("Erase /dev/mtd5.\n");
- runShellCmd("flash_erase /dev/mtd5 0 0");
- DEBUG_INFO("Write /dev/mtd5.\n");
- runShellCmd("nandwrite -p /dev/mtd5 /mnt/imgBuffer");
- system("rm -f /mnt/imgBuffer");
- result = PASS;
- }
- }
- break;
- case CSU_KERNEL_IMAGE:
- fd = open("/mnt/imgBuffer", O_RDWR | O_CREAT | O_EXCL);
- if (fd < 0)
- {
- DEBUG_ERROR("Can not create kernel image buffer file.\n");
- result = FAIL;
- }
- else
- {
- // Write image to flash
- DEBUG_INFO("Writing image to image buffer file...\n");
- wrd=write(fd, ptr+HEADER_LENGTH, DataLength);
- close(fd);
- DEBUG_INFO(">> imgBuffer written length: 0x%x\n", wrd);
- if(wrd != DataLength)
- {
- result = FAIL;
- }
- else
- {
- config_upgrade_flag(UPGRADE_FLAG_KERNEL, ON);
- DEBUG_INFO("Erase /dev/mtd6.\n");
- runShellCmd("flash_erase /dev/mtd6 0 0");
- DEBUG_INFO("Write /dev/mtd6.\n");
- runShellCmd("nandwrite -p /dev/mtd6 /mnt/imgBuffer");
- config_upgrade_flag(UPGRADE_FLAG_KERNEL, OFF);
- DEBUG_INFO("Erase /dev/mtd7.\n");
- runShellCmd("flash_erase /dev/mtd7 0 0");
- DEBUG_INFO("Write /dev/mtd7.\n");
- runShellCmd("nandwrite -p /dev/mtd7 /mnt/imgBuffer");
- system("rm -f /mnt/imgBuffer");
- result = PASS;
- }
- }
- break;
- case CSU_ROOT_FILE_SYSTEM:
- fd = open("/mnt/imgBuffer", O_RDWR | O_CREAT | O_EXCL);
- if(fd < 0)
- {
- DEBUG_ERROR("UpdateRootfs NG - can not create rootfs image buffer file\n");
- result = FAIL;
- }
- else
- {
- DEBUG_INFO("Writing image to image buffer file...\n");
- wrd=write(fd, ptr+HEADER_LENGTH, DataLength);
- close(fd);
- DEBUG_INFO(">> imgBuffer written length: 0x%x\n", wrd);
- if(wrd!=DataLength)
- {
- result = FAIL;
- }
- else
- {
- config_upgrade_flag(UPGRADE_FLAG_ROOTFS, ON);
- DEBUG_INFO("Erase /dev/mtd8.\n");
- runShellCmd("flash_erase /dev/mtd8 0 0");
- DEBUG_INFO("Write /dev/mtd8.\n");
- runShellCmd("nandwrite -p /dev/mtd8 /mnt/imgBuffer");
- config_upgrade_flag(UPGRADE_FLAG_ROOTFS, OFF);
- DEBUG_INFO("Erase /dev/mtd9.\n");
- runShellCmd("flash_erase /dev/mtd9 0 0");
- DEBUG_INFO("Write /dev/mtd9.\n");
- runShellCmd("nandwrite -p /dev/mtd9 /mnt/imgBuffer");
- system("rm -f /mnt/imgBuffer");
- result = PASS;
- }
- }
- break;
- case CSU_USER_CONFIGURATION:
- fd = open("/mnt/imgBuffer", O_RDWR | O_CREAT | O_EXCL);
- if (fd < 0)
- {
- DEBUG_ERROR("Can not create configuration image buffer file\n");
- result = FAIL;
- }
- else
- {
- // Write image to flash
- DEBUG_INFO("Writing image to image buffer file...\n");
- wrd=write(fd, ptr+HEADER_LENGTH, DataLength);
- close(fd);
- DEBUG_INFO(">> imgBuffer written length: 0x%x\n", wrd);
- if(wrd != DataLength)
- {
- result = FAIL;
- }
- else
- {
- DEBUG_INFO("Erase /dev/mtd10.\n");
- runShellCmd("flash_erase /dev/mtd10 0 0");
- DEBUG_INFO("Write /dev/mtd10.\n");
- runShellCmd("nandwrite -p /dev/mtd10 /mnt/imgBuffer");
- DEBUG_INFO("Erase /dev/mtd11.\n");
- runShellCmd("flash_erase /dev/mtd11 0 0");
- DEBUG_INFO("Write /dev/mtd11.\n");
- runShellCmd("nandwrite -p /dev/mtd11 /mnt/imgBuffer");
- system("rm -f /mnt/imgBuffer");
- result = PASS;
- }
- }
- break;
- default:
- break;
- }
- }
- else
- DEBUG_ERROR("Firmware image CRC32 mismatch.\n");
- }
- else
- DEBUG_ERROR("Firmware image length mismatch.\n");
- }
- else
- DEBUG_ERROR("Firmware image type mismatch.\n");
- }
- free(ptr);
- if(result == PASS)
- DEBUG_INFO("Update image success\n");
- else
- DEBUG_ERROR("Update image fail\n");
- return result;
- }
- //================================================
- // UART update function
- //================================================
- void displayMessage(uint8_t *data, uint16_t len, uint8_t isRX)
- {
- uint8_t output[8192];
- memset(output, 0x00, ARRAY_SIZE(output));
- sprintf((char*)output, "%s", (isRX?"RX: ":"TX: "));
- for(uint16_t idx = 0;idx<len;idx++)
- {
- sprintf((char*)output, "%s%02x ", output, data[idx]);
- }
- DEBUG_INFO("%s\n", output);
- }
- int uart_tranceive(int fd, unsigned char* cmd, unsigned char* rx, int len, unsigned char needErase)
- {
- uint16_t rxLen = 0;
- tcflush(fd,TCIOFLUSH);
- //displayMessage(cmd, 6, NO);
- if(write(fd, cmd, len) >= len)
- {
- rxLen = read(fd, rx, 8);
- /*
- if(rxLen > 0)
- displayMessage(rx, rxLen, YES);
- else
- DEBUG_INFO("RX: NULL\n");*/
- }
- else
- {
- DEBUG_ERROR("Serial command %s response fail.\n", cmd);
- }
- return rxLen;
- }
- unsigned char uart_update_start(int fd, unsigned char targetAddr, unsigned int crc32)
- {
- unsigned char result = FAIL;
- unsigned char tx[11] = {0xaa, 0x00, targetAddr, UART_CMD_UPDATE_START, 0x04, 0x00, (crc32>>0)&0xff, (crc32>>8)&0xff, (crc32>>16)&0xff, (crc32>>24)&0xff, 0x00};
- unsigned char rx[8] = {0};
- unsigned char chksum = 0x00;
- for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
- chksum ^= tx[6+idx];
- tx[10] = chksum;
- if(uart_tranceive(fd, tx, rx, 11, 0x01) >= 8)
- {
- chksum = 0x00;
- for(int idx=0;idx<((rx[4] | rx[5]<<8)>1?1:(rx[4] | rx[5]<<8));idx++)
- {
- chksum ^= rx[6+idx];
- }
- if((chksum == rx[6+((rx[4] | rx[5]<<8)>1?1:(rx[4] | rx[5]<<8))]) &&
- (rx[2] == tx[1]) &&
- (rx[1] == tx[2]) &&
- (rx[3] == tx[3]) &&
- (rx[6] == 0x01))
- {
- result = PASS;
- DEBUG_INFO("UART target is ready for upgrade.\n");
- }
- else
- {
- DEBUG_INFO("UART target is not ready...\n");
- }
- }
- else
- {
- DEBUG_ERROR("UART receiving update start ack failed...\n");
- }
- return result;
- }
- unsigned char uart_update_abord(int fd, unsigned char targetAddr)
- {
- unsigned char result = FAIL;
- unsigned char tx[7] = {0xaa, 0x00, targetAddr, UART_CMD_UPDATE_ABORD, 0x00, 0x00, 0x00};
- unsigned char rx[8] = {0};
- unsigned char chksum = 0x00;
- if(uart_tranceive(fd, tx, rx, 7, 0x00) >= 8)
- {
- for(int idx=0;idx<((rx[4] | rx[5]<<8)>1?1:(rx[4] | rx[5]<<8));idx++)
- {
- chksum ^= rx[6+idx];
- }
- if((chksum == rx[6+((rx[4] | rx[5]<<8)>1?1:(rx[4] | rx[5]<<8))]) &&
- (rx[2] == tx[1]) &&
- (rx[1] == tx[2]) &&
- (rx[3] == tx[3]) &&
- (rx[6] == 0x01))
- {
- result = PASS;
- DEBUG_INFO("UART target abord update OK.\n");
- }
- else
- {
- DEBUG_ERROR("UART target abord update failed.\n");
- }
- }
- else
- {
- DEBUG_ERROR("UART receiving update abord ack failed...\n");
- }
- return result;
- }
- unsigned char uart_update_transfer(int fd, unsigned char targetAddr, unsigned int startAddr, unsigned char *data, unsigned short int length)
- {
- unsigned char result = FAIL;
- unsigned char tx[11 + length];
- unsigned char rx[8] = {0};
- unsigned char chksum = 0x00;
- tx[0] = 0xaa;
- tx[1] = 0x00;
- tx[2] = targetAddr;
- tx[3] = UART_CMD_UPDATE_TRANSFER;
- tx[4] = (4 + length) & 0xff;
- tx[5] = ((4 + length)>>8) & 0xff;
- tx[6] = (startAddr>>0) & 0xff;
- tx[7] = (startAddr>>8) & 0xff;
- tx[8] = (startAddr>>16) & 0xff;
- tx[9] = (startAddr>>24) & 0xff;
- memcpy(tx+10, data, length);
- for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
- chksum ^= tx[6+idx];
- tx[sizeof(tx)-1] = chksum;
- if(uart_tranceive(fd, tx, rx, (11 + length), 0x00) >= 8)
- {
- chksum = 0;
- for(int idx=0;idx<((rx[4] | rx[5]<<8)>1?1:(rx[4] | rx[5]<<8));idx++)
- {
- chksum ^= rx[6+idx];
- }
- if((chksum == rx[6+((rx[4] | rx[5]<<8)>1?1:(rx[4] | rx[5]<<8))]) &&
- (rx[2] == tx[1]) &&
- (rx[1] == tx[2]) &&
- (rx[3] == tx[3]) &&
- (rx[6] == 0x01))
- {
- result = PASS;
- }
- }
- else
- {
- DEBUG_ERROR("UART receiving update transfer ack failed...\n");
- }
- return result;
- }
- unsigned char uart_update_finish(int fd, unsigned char targetAddr)
- {
- unsigned char result = FAIL;
- unsigned char tx[7] = {0xaa, 0x00, targetAddr, UART_CMD_UPDATE_FINISH, 0x00, 0x00, 0x00};
- unsigned char rx[8] = {0};
- unsigned char chksum = 0x00;
- if(uart_tranceive(fd, tx, rx, 7, 0x00) >= 8)
- {
- for(int idx=0;idx<((rx[4] | rx[5]<<8)>1?1:(rx[4] | rx[5]<<8));idx++)
- {
- chksum ^= rx[6+idx];
- }
- if((chksum == rx[6+((rx[4] | rx[5]<<8)>1?1:(rx[4] | rx[5]<<8))]) &&
- (rx[2] == tx[1]) &&
- (rx[1] == tx[2]) &&
- (rx[3] == tx[3]) &&
- (rx[6] == 0x01))
- {
- result = PASS;
- DEBUG_INFO("UART update finish check OK...\n");
- }
- else
- {
- DEBUG_ERROR("UART update finish check failed...\n");
- }
- }
- else
- {
- DEBUG_ERROR("UART receiving update finish ack failed...\n");
- }
- return result;
- }
- unsigned char uart_config_timeout(int uartfd)
- {
- struct termios tios;
- ioctl (uartfd, TCGETS, &tios);
- tios.c_cc[VTIME]=(unsigned char)50; // timeout 5 secod
- tcflush(uartfd, TCIFLUSH);
- ioctl (uartfd, TCSETS, &tios);
- return uartfd;
- }
- int Upgrade_UART(int uartfdOrd,unsigned int Type,unsigned char TargetAddr,char *SourcePath,char *ModelName)
- {
- int result = FAIL;
- long int MaxLen=48*1024*1024, ImageLen=0;
- unsigned int ImageCRC=0, DataLength=0;
- int fd;
- int uartfd = uart_config_timeout(uartfdOrd);
- fd = open(SourcePath, O_RDONLY);
- if(fd < 0)
- {
- DEBUG_ERROR("UpdateRootfs NG - can not open image file %s\n", SourcePath);
- return result;
- }
- unsigned char *ptr = malloc(MaxLen+HEADER_LENGTH);
- memset(ptr,0xFF,MaxLen+HEADER_LENGTH);
- //get the image length
- ImageLen = read(fd,ptr,MaxLen+HEADER_LENGTH);
- close(fd);
- //read out the header
- int isModelNameOK = PASS;
- if((ModelName[0] != ptr[0]) ||
- (ModelName[1] != ptr[1]) ||
- (ModelName[7] != ptr[7]) ||
- (ModelName[8] != ptr[8]) ||
- (ModelName[9] != ptr[9]) ||
- (ModelName[11] != ptr[11]) ||
- (ModelName[12] != ptr[12]) ||
- (ModelName[13] != ptr[13]))
- {
- isModelNameOK = FAIL;
- }
- if(isModelNameOK == FAIL)
- {
- DEBUG_ERROR("Model name mismatch...\n");
- }
- else
- {
- // check if the firmware type is correct
- if(Type == (((unsigned int)ptr[16])<<24 | ((unsigned int)ptr[17])<<16 | ((unsigned int)ptr[18])<<8 | ((unsigned int)ptr[19])))
- {
- if((ImageLen-HEADER_LENGTH) == (((unsigned int)ptr[20])<<24 | ((unsigned int)ptr[21])<<16 | ((unsigned int)ptr[22])<<8 | ((unsigned int)ptr[23])))
- {
- DataLength = ImageLen-HEADER_LENGTH;
- // get CRC in the header
- ImageCRC = ((unsigned int)ptr[34])<<24 | ((unsigned int)ptr[35])<<16 | ((unsigned int)ptr[36])<<8 | ((unsigned int)ptr[37]);
- // calculate the image CRC
- DEBUG_INFO("CRC32 in image: 0x%08X\n",ImageCRC);
- DEBUG_INFO("CRC32 by calculation: 0x%08X\n",crc32(ptr,ImageLen));
- if(crc32(ptr,ImageLen) == ImageCRC)
- {
- if(YES)
- {
- int CNT_Fail = 0;
- int CNT_Trans = 0;
- do
- {
- if(uart_update_start(uartfd, TargetAddr, crc32(ptr+HEADER_LENGTH,DataLength))==PASS)
- break;
- else
- DEBUG_WARN("Upgrade start fail, retry %d \n", ++CNT_Fail);
- }while(CNT_Fail<10);
- if(CNT_Fail>=10)
- {
- uart_update_abord(uartfd, TargetAddr);
- DEBUG_ERROR("UART upgrade start retry > limits, aboard upgrade.\n");
- }
- else
- {
- CNT_Fail = 0;
- do
- {
- if(uart_update_transfer(uartfd, TargetAddr, CNT_Trans*1024, ptr+HEADER_LENGTH+(CNT_Trans*1024), 1024)==PASS)
- {
- CNT_Fail = 0;
- CNT_Trans++;
- DEBUG_INFO("Upgrade progress:%.2f%%\n", ((float)(CNT_Trans*1024))/(DataLength)*100);
- }
- else
- {
- DEBUG_WARN("Data transfer fail, retry %d \n", ++CNT_Fail);
- sleep(1);
- }
- }while(DataLength-(CNT_Trans*1024)>0 && CNT_Fail<10);
- if(CNT_Fail>=10)
- {
- uart_update_abord(uartfd, TargetAddr);
- DEBUG_ERROR("UART upgrade transfer retry > limits, aboard upgrade.\n");
- }
- else
- {
- CNT_Fail = 0;
- do
- {
- if(uart_update_finish(uartfd, TargetAddr)==PASS)
- break;
- else
- DEBUG_WARN("Upgrade finish fail, retry %d \n", ++CNT_Fail);
- }while(CNT_Fail<10);
- if(CNT_Fail>=10)
- {
- uart_update_abord(uartfd, TargetAddr);
- DEBUG_ERROR("UART upgrade finish retry > limits, aboard upgrade.\n");
- }
- else
- {
- result = PASS;
- printf("UART upgrade success.\n");
- }
- }
- }
- }
- else
- DEBUG_ERROR("UART upgrade request failed.\n");
- }
- else
- DEBUG_ERROR("Firmware image CRC32 mismatch.\n");
- }
- else
- DEBUG_ERROR("Firmware image length mismatch.\n");
- }
- else
- DEBUG_ERROR("Firmware image type mismatch.\n");
- }
- free(ptr);
- return result;
- }
- //================================================
- // CANBUS update function
- //================================================
- unsigned long getTimeoutValue(struct timeval _sour_time)
- {
- struct timeval _end_time;
- gettimeofday(&_end_time, NULL);
- return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
- }
- int CAN_Download_REQ(int canfd,unsigned int Slave_Addr, unsigned int imageSize)
- {
- struct can_frame frame;
- frame.can_id = (0x00000E00 + Slave_Addr) | 0x80000000; //extended frame
- frame.can_dlc = 0x07;
- frame.data[0] = 0x04; //0x01:Configuration file, 0x02:Bootloader of primary side MCU, 0x03:Firmware (main code) of primary side MCU, 0x04:Bootloader of secondary side MCU, 0x05:Firmware (main code) of secondary side MCU
- frame.data[1] = (imageSize>>0)&0xff; //Total 384 KBytes
- frame.data[2] = (imageSize>>8)&0xff; //Total 384 KBytes
- frame.data[3] = (imageSize>>16)&0xff; //Total 384 KBytes
- frame.data[4] = (imageSize>>24)&0xff; //Total 384 KBytes
- frame.data[5] = 0x10; //16 blocks
- frame.data[6] = 0x18; //24 KBytes
- DEBUG_INFO( "File size = %x, %d \n", imageSize, imageSize);
- write(canfd, &frame, sizeof(struct can_frame));
- if (canfd > 0)
- {
- struct timeval timer;
- gettimeofday(&timer, NULL);
- while (getTimeoutValue(timer) < 5000000)
- {
- struct can_frame frame;
- int len;
- len = read(canfd, &frame, sizeof(struct can_frame));
- if (len >= 0)
- {
- DEBUG_INFO( "*****************************CAN_Download_REQ Get***************************** \n");
- DEBUG_INFO("data = %x \n", frame.can_id & CAN_EFF_MASK);
- if (((int)(frame.can_id & CAN_EFF_MASK & 0xFFFFFF00) == 0x08000E00) && frame.data[0] == 1)
- {
- DEBUG_INFO("PASS \n");
- return PASS;
- }
- }
- }
- }
- return FAIL;
- }
- int CAN_Start_BLK_Trans(int canfd,unsigned int Slave_Addr,unsigned int Block_No,unsigned int Block_Checksum)
- {
- struct can_frame frame;
- frame.can_id = (0x00000F00 + Slave_Addr) | 0x80000000; //extended frame
- frame.can_dlc = 0x02;
- frame.data[0] = Block_No;
- frame.data[1] = Block_Checksum;
- DEBUG_INFO("Block_No = %x, Block_Checksum = %x \n", Block_No, Block_Checksum);
- write(canfd, &frame, sizeof(struct can_frame));
- usleep(100000);
- if (canfd > 0)
- {
- struct timeval timer;
- gettimeofday(&timer, NULL);
- while (getTimeoutValue(timer) < 1000000)
- {
- struct can_frame frame;
- int len;
- len = read(canfd, &frame, sizeof(struct can_frame));
- if(len >= 0)
- {
- DEBUG_INFO("*****************************CAN_Start_BLK_Trans Get***************************** \n");
- DEBUG_INFO("data = %x \n", frame.can_id & CAN_EFF_MASK); // extended frame CAN_EFF_MASK
- if(((int)(frame.can_id & CAN_EFF_MASK & 0xFFFFFF00) == 0x08000F00) &&frame.data[0] == 1)
- {
- DEBUG_INFO("CAN_Start_BLK_Trans PASS \n");
- return PASS;
- }
- }
- }
- }
- return FAIL;
- }
- void CAN_Data_Trans(int canfd,unsigned int Slave_Addr,long Data_num,unsigned char Data[])
- {
- struct can_frame frame;
- frame.can_id = (0x00001000 + Slave_Addr) | 0x80000000; //extended frame
- frame.can_dlc = 0x08;
- frame.data[0] = Data[Data_num+0];
- frame.data[1] = Data[Data_num+1];
- frame.data[2] = Data[Data_num+2];
- frame.data[3] = Data[Data_num+3];
- frame.data[4] = Data[Data_num+4];
- frame.data[5] = Data[Data_num+5];
- frame.data[6] = Data[Data_num+6];
- frame.data[7] = Data[Data_num+7];
- // DEBUG_INFO("%02x %02x %02x %02x %02x %02x %02x %02x \n", frame.data[0], frame.data[1], frame.data[2], frame.data[3],
- // frame.data[4], frame.data[5], frame.data[6], frame.data[7]);
- write(canfd, &frame, sizeof(struct can_frame));
- usleep(2000);
- }
- int CAN_Download_FIN(int canfd,unsigned int Slave_Addr)
- {
- struct can_frame frame;
- frame.can_id = (0x00001100 + Slave_Addr) | 0x80000000; //extended frame
- frame.can_dlc = 0x00;
- write(canfd, &frame, sizeof(struct can_frame));
- usleep(10000);
- if (canfd > 0)
- {
- struct timeval timer;
- gettimeofday(&timer, NULL);
- while (getTimeoutValue(timer) < 1000000)
- {
- struct can_frame frame;
- int len;
- len = read(canfd, &frame, sizeof(struct can_frame));
- if(len >= 0)
- {
- DEBUG_INFO("data = %x \n", frame.can_id & CAN_EFF_MASK); // extended frame
- if(((int)(frame.can_id & CAN_EFF_MASK & 0xFFFFFF00) == 0x08001100) && frame.data[0] == 1)
- {
- DEBUG_INFO("CAN_Download_FIN PASS \n");
- return PASS;
- }
- }
- }
- }
- return FAIL;
- }
- int Checksum_Cal(unsigned int StartAdress,unsigned int length, unsigned char Data[])
- {
- unsigned char checksum = 0x00;
- for(unsigned int i = 0; i < length; i++)
- {
- //DEBUG_INFO("value = %x \n", Data[StartAdress + i]);
- checksum ^= Data[StartAdress + i];
- //DEBUG_INFO("checksum = %x \n", checksum);
- }
- return checksum;
- }
- int Upgrade_CAN(int canfd,unsigned int Type,unsigned char TargetAddr,char *SourcePath,char *ModelName)
- {
- int result = FAIL;
- long int MaxLen=48*1024*1024, ImageLen=0;
- unsigned int ImageCRC=0, DataLength=0;
- int fd;
- fd = open(SourcePath, O_RDONLY);
- if(fd < 0)
- {
- DEBUG_ERROR("UpdateRootfs NG - can not open image file %s\n", SourcePath);
- return result;
- }
- unsigned char *ptr = malloc(MaxLen+HEADER_LENGTH);
- memset(ptr,0xFF,MaxLen+HEADER_LENGTH);
- //get the image length
- ImageLen = read(fd,ptr,MaxLen+HEADER_LENGTH);
- close(fd);
- //read out the header
- int isModelNameOK = PASS;
- if((ModelName[0] != ptr[0]) ||
- (ModelName[1] != ptr[1]) ||
- (ModelName[7] != ptr[7]) ||
- (ModelName[8] != ptr[8]) ||
- (ModelName[9] != ptr[9]) ||
- (ModelName[11] != ptr[11]) ||
- (ModelName[12] != ptr[12]) ||
- (ModelName[13] != ptr[13]))
- {
- isModelNameOK = FAIL;
- }
- if(isModelNameOK == FAIL)
- {
- DEBUG_ERROR("Model name mismatch...\n");
- return result;
- }
- else
- {
- // check if the firmware type is correct
- if(Type == (((unsigned int)ptr[16])<<24 | ((unsigned int)ptr[17])<<16 | ((unsigned int)ptr[18])<<8 | ((unsigned int)ptr[19])))
- {
- if((ImageLen-HEADER_LENGTH) == (((unsigned int)ptr[20])<<24 | ((unsigned int)ptr[21])<<16 | ((unsigned int)ptr[22])<<8 | ((unsigned int)ptr[23])))
- {
- DataLength = ImageLen-HEADER_LENGTH;
- // get CRC in the header
- ImageCRC = ((unsigned int)ptr[34])<<24 | ((unsigned int)ptr[35])<<16 | ((unsigned int)ptr[36])<<8 | ((unsigned int)ptr[37]);
- // calculate the image CRC
- DEBUG_INFO("CRC32 in image: 0x%08X\n",ImageCRC);
- DEBUG_INFO("CRC32 by calculation: 0x%08X\n",crc32(ptr,ImageLen));
- if(crc32(ptr,ImageLen) == ImageCRC)
- {
- unsigned int Checksum[16];
- for(int i=0;i<16;i++)
- {
- Checksum[i] = Checksum_Cal(i * 24576, 24576, ptr + HEADER_LENGTH);
- }
- if(CAN_Download_REQ(canfd, TargetAddr, DataLength) == PASS)
- {
- for(int block = 1; block <= 16; block++)
- {
- if(CAN_Start_BLK_Trans(canfd, TargetAddr, block, Checksum[block - 1]) == PASS)
- {
- for(int times = 0; times < 3072; times++)
- {
- CAN_Data_Trans(canfd, TargetAddr, ((block - 1) * 24576 + times * 8), ptr + HEADER_LENGTH);
- }
- DEBUG_INFO(" \n\n");
- }
- else
- {
- free(ptr);
- return result;
- }
- }
- if (CAN_Download_FIN(canfd, TargetAddr) == PASS)
- result = PASS;
- }
- else
- DEBUG_ERROR("CANBUS upgrade request failed.\n");
- }
- else
- DEBUG_ERROR("Firmware image CRC32 mismatch.\n");
- }
- else
- DEBUG_ERROR("Firmware image length mismatch.\n");
- }
- else
- DEBUG_ERROR("Firmware image type mismatch.\n");
- }
- free(ptr);
- return result;
- }
- //================================================
- // CCS update function
- //================================================
- int Check_CCS_image_header(unsigned int Type,char *SourcePath,char *ModelName)
- {
- int result = FAIL;
- long int MaxLen=48*1024*1024, ImageLen=0;
- unsigned int ImageCRC=0;
- int fd;
- // space max size set
- fd = open(SourcePath, O_RDONLY);
- if(fd < 0)
- {
- DEBUG_ERROR("Update CCS NG - can not open upgrade image %s\n", SourcePath);
- return FAIL;
- }
- switch(Type)
- {
- case CCS_BOARD_MLO:
- MaxLen = 0.5*1024*1024;
- DEBUG_INFO("Prepare to upgrade CCS MLO\n");
- break;
- case CCS_BOARD_BOOTLOADER:
- MaxLen = 1*1024*1024;
- DEBUG_INFO("Prepare to upgrade CCS BOOTLOADER\n");
- break;
- case CCS_BOARD_KERNEL_CONFIGURATION:
- MaxLen = 0.5*1024*1024;
- DEBUG_INFO("Prepare to upgrade CCS KERNEL CONFIGURATION\n");
- break;
- case CCS_BOARD_KERNEL_IMAGE:
- MaxLen = 10*1024*1024;
- DEBUG_INFO("Prepare to upgrade CCS KERNEL\n");
- break;
- case CCS_BOARD_FILE_SYSTEM:
- MaxLen = 48*1024*1024;
- DEBUG_INFO("Prepare to upgrade CCS FILE SYSTEM\n");
- break;
- default:
- DEBUG_ERROR("Wrong image type for CCS upgrade\n");
- return FAIL;
- break;
- }
- unsigned char *ptr = malloc(MaxLen+HEADER_LENGTH);
- memset(ptr,0xFF,MaxLen+HEADER_LENGTH);
- //get the image length
- ImageLen = read(fd,ptr,MaxLen+HEADER_LENGTH);
- close(fd);
- //read out the header
- int isModelNameOK = PASS;
- if((ModelName[0] != ptr[0]) ||
- (ModelName[1] != ptr[1]) ||
- (ModelName[7] != ptr[7]) ||
- (ModelName[8] != ptr[8]) ||
- (ModelName[9] != ptr[9]) ||
- (ModelName[11] != ptr[11]) ||
- (ModelName[12] != ptr[12]) ||
- (ModelName[13] != ptr[13]))
- {
- isModelNameOK = FAIL;
- }
- if(isModelNameOK == FAIL)
- {
- DEBUG_ERROR("Model name mismatch.\n");
- }
- else
- {
- // check if the firmware type is correct
- if(Type == (((unsigned int)ptr[16])<<24 | ((unsigned int)ptr[17])<<16 | ((unsigned int)ptr[18])<<8 | ((unsigned int)ptr[19])))
- {
- if((ImageLen-HEADER_LENGTH) == (((unsigned int)ptr[20])<<24 | ((unsigned int)ptr[21])<<16 | ((unsigned int)ptr[22])<<8 | ((unsigned int)ptr[23])))
- {
- // get CRC in the header
- ImageCRC = ((unsigned int)ptr[34])<<24 | ((unsigned int)ptr[35])<<16 | ((unsigned int)ptr[36])<<8 | ((unsigned int)ptr[37]);
- // calculate the image CRC
- DEBUG_INFO("CRC32 in CCS image: 0x%08X\n",ImageCRC);
- DEBUG_INFO("CRC32 by calculation: 0x%08X\n",crc32(ptr,ImageLen));
- if(crc32(ptr,ImageLen) == ImageCRC)
- {
- result = PASS;
- }
- else
- {
- DEBUG_ERROR("Firmware image CRC32 mismatch.\n");
- }
- }
- else
- {
- DEBUG_ERROR("Firmware image length mismatch.\n");
- }
- }
- else
- {
- DEBUG_ERROR("Firmware image type mismatch.\n");
- }
- }
- free(ptr);
- return result;
- }
- int Put_CCS_image(char *SourcePath, unsigned char TargetAddr)
- {
- unsigned char ftpcmdbuf[256];
- unsigned char CCSIpAddress[16];
- //If ID of target EV board is 1, the IP address will be 192.168.0.21,
- //if ID of target EV board is 2, the IP address will be 192.168.0.22.
- sprintf((char*)CCSIpAddress,"192.168.0.2%d", TargetAddr);
- //Using ftpput command to transfer CCS upgrade image,
- //User name : root
- //User password : y42j/4cj84
- //Destination : /root/ccs.image
- sprintf((char*)ftpcmdbuf,"ftpput -u root -p y42j/4cj84 %s /root/ccs.image %s",
- CCSIpAddress, SourcePath);
- if(system((char*)ftpcmdbuf) != 0)
- {
- DEBUG_ERROR("Update CCS NG - FTP put CCS upgrade image to CCS board %d fail\n", TargetAddr);
- return FAIL;
- }
- else
- {
- DEBUG_INFO("FTP put %s to CCS board %d finish\n", SourcePath, TargetAddr);
- return PASS;
- }
- }
- int Send_CCS_download_finish(int canfd,unsigned int Slave_Addr)
- {
- if (canfd > 0)
- {
- struct can_frame frame;
- frame.can_id = (CANBUS_MESSAGE_ID_UPGRADE_FINISH + Slave_Addr) | 0x80000000; //extended frame
- frame.can_dlc = 0x00;
- write(canfd, &frame, sizeof(struct can_frame));
- usleep(10000);
- struct timeval timer;
- gettimeofday(&timer, NULL);
- unsigned long ack_timeout = 5 * 60 * 1000 * 1000; //5 minutes
- while (getTimeoutValue(timer) < ack_timeout)
- {
- struct can_frame frame;
- int len;
- len = read(canfd, &frame, sizeof(struct can_frame));
- if(len >= 0)
- {
- if(((int)(frame.can_id & CAN_EFF_MASK) == (CANBUS_MESSAGE_ID_UPGRADE_FINISH | Slave_Addr | 0x08000000)) && frame.data[0] == 1)
- {
- return PASS;
- }
- }
- }
- DEBUG_ERROR("Wait for download finish ack from CCS %d timeout\n", Slave_Addr);
- return FAIL;
- }
- else
- {
- DEBUG_ERROR("Send CCS download finish command fail, CAN fd is null\n");
- return FAIL;
- }
- }
- int Upgrade_CCS(int canfd,unsigned int Type,unsigned char TargetAddr,char *SourcePath,char *ModelName)
- {
- if(Check_CCS_image_header(Type, SourcePath, ModelName) == FAIL)
- {
- return FAIL;
- }
- if(Put_CCS_image(SourcePath, TargetAddr) == FAIL)
- {
- return FAIL;
- }
- if(Send_CCS_download_finish(canfd, TargetAddr) == FAIL)
- {
- return FAIL;
- }
- DEBUG_INFO("Upgrade CCS board %d complete.\n", TargetAddr);
- return PASS;
- }
|