Parcourir la source

2020.05.15 / TC Hsu

Actions: Implement Upgrade_CCS() function module for CCS EV board upgrade feature, Which using ftpput to transfer upgrade image to CCS EV board by ethernet at first, secondly it send the download finish message to CCS EV board by CAN bus, finally, if the CCS EV board acknowledge the download finish message, upgrade is completed.

Image version    : N/A
Image checksum   : N/A

Hardware PWB P/N : N/A
Hardware Version : N/A

Files:

	modified:   EVSE/Modularization/Module_Upgrade.c
	modified:   EVSE/Modularization/Module_Upgrade.h
TC_Hsu il y a 4 ans
Parent
commit
9a44d2c5d7
2 fichiers modifiés avec 1224 ajouts et 1099 suppressions
  1. 1109 986
      EVSE/Modularization/Module_Upgrade.c
  2. 115 113
      EVSE/Modularization/Module_Upgrade.h

+ 1109 - 986
EVSE/Modularization/Module_Upgrade.c

@@ -1,986 +1,1109 @@
-/*
- * 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 ARRAY_SIZE(A)		(sizeof(A) / sizeof(A[0]))
-#define PASS				1
-#define FAIL				-1
-
-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;
-	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);
-	sprintf(Buf,"echo \"[%04d.%02d.%02d %02d:%02d:%02d] - %s\" >> /Storage/SystemLog/[%04d.%02d]Upgrade_SystemLog",
-			tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,
-			buffer,
-			tm->tm_year+1900,tm->tm_mon+1);
-#ifdef SystemLogMessage
-	system(Buf);
-#endif
-
-	printf("[%04d.%02d.%02d %02d:%02d:%02d] - %s", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec, buffer);
-
-	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 Upgrade_Flash(unsigned int Type,char *SourcePath,char *ModelName)
-{
-	int result = FAIL;
-
-	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_BOOTLOADER:
-			MaxLen = 1*1024*1024;
-			//DEBUG_INFO("Image type: U-Boot\r\n");
-			break;
-		case CSU_KERNEL_CONFIGURATION:
-			MaxLen = 0.5*1024*1024;
-			DEBUG_INFO("Image type: DTB\r\n");
-			break;
-		case CSU_KERNEL_IMAGE:
-			MaxLen = 10*1024*1024;
-			DEBUG_INFO("Image type: Kernel\r\n");
-			break;
-		case CSU_ROOT_FILE_SYSTEM:
-			MaxLen = 48*1024*1024;
-			DEBUG_INFO("Image type: Root fs\r\n");
-			break;
-		case CSU_USER_CONFIGURATION:
-			MaxLen = 6*1024*1024+48;
-			DEBUG_INFO("Image type: Config\r\n");
-			break;
-		default:
-			break;
-	}
-
-	fd = open(SourcePath, O_RDONLY);
-	if(fd < 0)
-	{
-		DEBUG_ERROR("UpdateRootfs NG - can not open rootfs\n");
-		return result;
-	}
-
-	unsigned char *ptr = malloc(MaxLen);
-	memset(ptr,0xFF,MaxLen);
-
-	//get the image length
-	ImageLen = read(fd,ptr,MaxLen);
-	close(fd);
-	//read out the header
-	int i;
-	int isModelNameOK = PASS;
-	for(i=0;i<16;i++)
-	{
-		if(ModelName[i] != ptr[i])
-		{
-			isModelNameOK = FAIL;
-		}
-	}
-
-	if(isModelNameOK == FAIL)
-	{
-		DEBUG_ERROR("Model name mismatch.\r\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-48) == (((unsigned int)ptr[20])<<24 | ((unsigned int)ptr[21])<<16 | ((unsigned int)ptr[22])<<8 | ((unsigned int)ptr[23])))
-			{
-				DataLength = ImageLen-48;
-
-				// 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\r\n",ImageCRC);
-				DEBUG_INFO("CRC32 by calculation: 0x%08X\r\n",crc32(ptr,ImageLen));
-				if(crc32(ptr,ImageLen) == ImageCRC)
-				{
-					// Write image to target flash block
-					switch(Type)
-					{
-						case FLASH_IMAGE_TYPE_SPL:
-							fd = open("/dev/mtdblock0", O_RDWR);
-							if (fd < 0)
-							{
-								DEBUG_ERROR("Can not open mtdblock0\r\n");
-								result = FAIL;
-							}
-							else
-							{
-								// Write image to flash
-								DEBUG_INFO("Writing image to mtdblock0...\n");
-								wrd=write(fd, ptr+48, DataLength);
-								close(fd);
-								DEBUG_INFO(">> mtdblock0 Written length: 0x%x\r\n", wrd);
-								if(wrd != DataLength)
-								{
-									result = FAIL;
-								}
-								else
-								{
-									result = PASS;
-								}
-							}
-							break;
-						case CSU_BOOTLOADER:
-							fd = open("/dev/mtdblock1", O_RDWR);
-							if (fd < 0)
-							{
-								DEBUG_ERROR("Can not open mtdblock1\r\n");
-								result = FAIL;
-							}
-							else
-							{
-								// Write image to flash
-								DEBUG_INFO("Writing image to mtdblock1...\n");
-								wrd=write(fd, ptr+48, DataLength);
-								close(fd);
-								DEBUG_INFO(">> mtdblock1 written length: 0x%x\r\n", wrd);
-								if(wrd != DataLength)
-								{
-									result = FAIL;
-								}
-								else
-								{
-									// Open flash target mtdblock
-									fd = open("/dev/mtdblock3", O_RDWR);
-									if (fd < 0)
-									{
-										DEBUG_ERROR("Can not open mtdblock3\r\n");
-										result = FAIL;
-									}
-									else
-									{
-										// Write image to flash
-										DEBUG_INFO("Writing image to mtdblock3...\n");
-										wrd=write(fd, ptr+48, DataLength);
-										close(fd);
-										DEBUG_INFO(">> mtdblock3 written length: 0x%x\r\n", wrd);
-										if(wrd != DataLength)
-										{
-											result = FAIL;
-										}
-										else
-										{
-											result = PASS;
-										}
-									}
-								}
-							}
-							break;
-						case CSU_KERNEL_CONFIGURATION:
-							fd = open("/dev/mtdblock4", O_RDWR);
-							if (fd < 0)
-							{
-								DEBUG_ERROR("Can not open mtdblock4\r\n");
-								result = FAIL;
-							}
-							else
-							{
-								// Write image to flash
-								DEBUG_INFO("Writing image to mtdblock4...\n");
-								wrd=write(fd, ptr+48, DataLength);
-								close(fd);
-								DEBUG_INFO(">> mtdblock4 written length: 0x%x\r\n", wrd);
-								if(wrd != DataLength)
-								{
-									result = FAIL;
-								}
-								else
-								{
-									// Open flash target mtdblock
-									fd = open("/dev/mtdblock5", O_RDWR);
-									if (fd < 0)
-									{
-										DEBUG_ERROR("Can not open mtdblock5\r\n");
-										result = FAIL;
-									}
-									else
-									{
-										// Write image to flash
-										DEBUG_INFO("Writing image to mtdblock5...\n");
-										wrd=write(fd, ptr+48, DataLength);
-										close(fd);
-										DEBUG_INFO(">> mtdblock5 written length: 0x%x\r\n", wrd);
-										if(wrd != DataLength)
-										{
-											result = FAIL;
-										}
-										else
-										{
-											result = PASS;
-										}
-									}
-								}
-							}
-							break;
-						case CSU_KERNEL_IMAGE:
-							fd = open("/dev/mtdblock6", O_RDWR);
-							if (fd < 0)
-							{
-								DEBUG_ERROR("Can not open mtdblock6\r\n");
-								result = FAIL;
-							}
-							else
-							{
-								// Write image to flash
-								DEBUG_INFO("Writing image to mtdblock6...\n");
-								wrd=write(fd, ptr+48, DataLength);
-								close(fd);
-								DEBUG_INFO(">> mtdblock6 written length: 0x%x\r\n", wrd);
-								if(wrd != DataLength)
-								{
-									result = FAIL;
-								}
-								else
-								{
-									// Open flash target mtdblock
-									fd = open("/dev/mtdblock7", O_RDWR);
-									if (fd < 0)
-									{
-										DEBUG_ERROR("Can not open mtdblock7\r\n");
-										result = FAIL;
-									}
-									else
-									{
-										// Write image to flash
-										DEBUG_INFO("Writing image to mtdblock7...\n");
-										wrd=write(fd, ptr+48, DataLength);
-										close(fd);
-										DEBUG_INFO(">> mtdblock7 written length: 0x%x\r\n", wrd);
-										if(wrd != DataLength)
-										{
-											result = FAIL;
-										}
-										else
-										{
-											result = PASS;
-										}
-									}
-								}
-							}
-							break;
-						case CSU_ROOT_FILE_SYSTEM:
-							fd = open("/dev/mtdblock8", O_RDWR);
-							if(fd < 0)
-							{
-								DEBUG_ERROR("UpdateRootfs NG - can not open rootfs\n");
-								result = FAIL;
-							}
-							else
-							{
-								DEBUG_INFO("Writing image to mtdblock8...\n");
-								wrd=write(fd, ptr+48, DataLength);
-								close(fd);
-								DEBUG_INFO(">> mtdblock8 written length: 0x%x\r\n", wrd);
-								if(wrd!=DataLength)
-								{
-									result = FAIL;
-								}
-								else
-								{
-									fd = open("/dev/mtdblock9", O_RDWR);
-									if(fd < 0)
-									{
-										DEBUG_ERROR("UpdateRootfs NG - can not open rootfs\n");
-										result = FAIL;
-									}
-
-									DEBUG_INFO("Writing image to mtdblock9...\n");
-									wrd=write(fd, ptr+48, DataLength);
-									close(fd);
-									DEBUG_INFO(">> mtdblock9 written length: 0x%x\r\n", wrd);
-									if(wrd!=DataLength)
-									{
-										result = FAIL;
-									}
-									else
-									{
-										result = PASS;
-									}
-								}
-							}
-							break;
-						case CSU_USER_CONFIGURATION:
-							// Open flash target mtdblock
-							fd = open("/dev/mtdblock10", O_RDWR);
-							if (fd < 0)
-							{
-								DEBUG_ERROR("Can not open mtdblock10\r\n");
-								result = FAIL;
-							}
-							else
-							{
-								// Write image to flash
-								DEBUG_INFO("Writing image to mtdblock10...\n");
-								wrd=write(fd, ptr+48, DataLength);
-								close(fd);
-								DEBUG_INFO(">> mtdblock10 written length: 0x%x\r\n", wrd);
-								if(wrd != DataLength)
-								{
-									result = FAIL;
-								}
-								else
-								{
-									// Open flash target mtdblock
-									fd = open("/dev/mtdblock11", O_RDWR);
-									if (fd < 0)
-									{
-										DEBUG_ERROR("Can not open mtdblock11\r\n");
-										result = FAIL;
-									}
-									else
-									{
-										// Write image to flash
-										DEBUG_INFO("Writing image to mtdblock11...\n");
-										wrd=write(fd, ptr+48, DataLength);
-										close(fd);
-										DEBUG_INFO(">> mtdblock11 written length: 0x%x\r\n", wrd);
-										if(wrd != DataLength)
-										{
-											result = FAIL;
-										}
-										else
-										{
-											result = PASS;
-										}
-									}
-								}
-							}
-							break;
-						default:
-							break;
-					}
-				}
-				else
-					DEBUG_ERROR("Firmware image CRC32 mismatch.\r\n");
-			}
-			else
-				DEBUG_ERROR("Firmware image length mismatch.\r\n");
-		}
-		else
-			DEBUG_ERROR("Firmware image type mismatch.\r\n");
-	}
-	free(ptr);
-
-	if(result == PASS)
-		DEBUG_INFO("Update image success\r\n");
-	else
-		DEBUG_ERROR("Update image fail\r\n");
-
-	return result;
-}
-
-//================================================
-// UART update function
-//================================================
-int uart_tranceive(int fd, unsigned char* cmd, unsigned char* rx, int len, unsigned char needErase)
-{
-	tcflush(fd,TCIOFLUSH);
-	if(write(fd, cmd, len) >= len)
-	{
-		len = 0;
-		if (needErase == 0x01)
-			sleep(5);
-		else
-			usleep(500000);
-		len = read(fd, rx, 512);
-	}
-	else
-	{
-		DEBUG_ERROR("Serial command %s response fail.\n", cmd);
-	}
-
-	return len;
-}
-
-unsigned char uart_update_start(unsigned char 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[512];
-	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) >0)
-	{
-		chksum = 0x00;
-		for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
-		{
-			chksum ^= rx[6+idx];
-		}
-
-		if((chksum == rx[6+(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(unsigned char 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[512];
-	unsigned char chksum = 0x00;
-
-
-	if(uart_tranceive(fd, tx, rx, 7, 0x00) >0)
-	{
-		for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
-		{
-			chksum ^= rx[6+idx];
-		}
-
-		if((chksum == rx[6+(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(unsigned char 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[512];
-	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) >0)
-	{
-		chksum = 0;
-		for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
-		{
-			chksum ^= rx[6+idx];
-		}
-
-		if((chksum == rx[6+(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(unsigned char 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[512];
-	unsigned char chksum = 0x00;
-
-
-	if(uart_tranceive(fd, tx, rx, 7,0x00) >0)
-	{
-		for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
-		{
-			chksum ^= rx[6+idx];
-		}
-
-		if((chksum == rx[6+(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;
-}
-
-int Upgrade_UART(unsigned char uartfd,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 rootfs\n");
-		return result;
-	}
-
-	unsigned char *ptr = malloc(MaxLen);
-	memset(ptr,0xFF,MaxLen);
-
-	//get the image length
-	ImageLen = read(fd,ptr,MaxLen);
-	close(fd);
-	//read out the header
-	int i;
-	int isModelNameOK = PASS;
-	for(i=0;i<16;i++)
-	{
-		if(ModelName[i] != ptr[i])
-		{
-			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-48) == (((unsigned int)ptr[20])<<24 | ((unsigned int)ptr[21])<<16 | ((unsigned int)ptr[22])<<8 | ((unsigned int)ptr[23])))
-			{
-				DataLength = ImageLen-48;
-
-				// 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\r\n",ImageCRC);
-				DEBUG_INFO("CRC32 by calculation: 0x%08X\r\n",crc32(ptr,ImageLen));
-				if(crc32(ptr,ImageLen) == ImageCRC)
-				{
-					if(uart_update_start(uartfd, TargetAddr, crc32(ptr+48,DataLength))==PASS)
-					{
-						int CNT_Fail = 0;
-						int CNT_Trans = 0;
-						do
-						{
-							if(uart_update_transfer(uartfd, TargetAddr, CNT_Trans*1024, ptr+48+(CNT_Trans*1024), 1024)==PASS)
-							{
-								CNT_Fail = 0;
-								CNT_Trans++;
-								DEBUG_INFO("Upgrade progress:%.2f%%\r\n", ((float)(CNT_Trans*1024))/(DataLength)*100);
-							}
-							else
-							{
-								DEBUG_WARN("Data transfer fail, retry %d \r\n", ++CNT_Fail);
-							}
-						}while(DataLength-(CNT_Trans*1024)>0 && CNT_Fail<3);
-
-						if(CNT_Fail>=3)
-						{
-							uart_update_abord(uartfd, TargetAddr);
-							DEBUG_ERROR("UART upgrade retry > limits, aboard upgrade.\r\n");
-						}
-						else if(uart_update_finish(uartfd, TargetAddr)==PASS)
-						{
-							result = PASS;
-						}
-					}
-					else
-						DEBUG_ERROR("UART upgrade request failed.\n");
-				}
-				else
-					DEBUG_ERROR("Firmware image CRC32 mismatch.\r\n");
-			}
-			else
-				DEBUG_ERROR("Firmware image length mismatch.\r\n");
-		}
-		else
-			DEBUG_ERROR("Firmware image type mismatch.\r\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 rootfs\n");
-		return result;
-	}
-
-	unsigned char *ptr = malloc(MaxLen);
-	memset(ptr,0xFF,MaxLen);
-
-	//get the image length
-	ImageLen = read(fd,ptr,MaxLen);
-	close(fd);
-	//read out the header
-	int i;
-	int isModelNameOK = PASS;
-	for(i=0;i<16;i++) {
-		if(ModelName[i] != ptr[i]){
-			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-48) == (((unsigned int)ptr[20])<<24 | ((unsigned int)ptr[21])<<16 | ((unsigned int)ptr[22])<<8 | ((unsigned int)ptr[23])))
-			{
-				DataLength = ImageLen-48;
-
-				// 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\r\n",ImageCRC);
-				DEBUG_INFO("CRC32 by calculation: 0x%08X\r\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 + 48);
-					}
-
-					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 + 48);
-								}
-								DEBUG_INFO(" \r\n\r\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.\r\n");
-			}
-			else
-				DEBUG_ERROR("Firmware image length mismatch.\r\n");
-		}
-		else
-			DEBUG_ERROR("Firmware image type mismatch.\r\n");
-
-	}
-	free(ptr);
-	return result;
-}
-
-
+/*
+ * 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 ARRAY_SIZE(A)       (sizeof(A) / sizeof(A[0]))
+#define PASS                1
+#define FAIL                -1
+
+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;
+    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);
+    sprintf(Buf,"echo \"[%04d.%02d.%02d %02d:%02d:%02d] - %s\" >> /Storage/SystemLog/[%04d.%02d]Upgrade_SystemLog",
+            tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,
+            buffer,
+            tm->tm_year+1900,tm->tm_mon+1);
+#ifdef SystemLogMessage
+    system(Buf);
+#endif
+
+    printf("[%04d.%02d.%02d %02d:%02d:%02d] - %s", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec, buffer);
+
+    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 Upgrade_Flash(unsigned int Type,char *SourcePath,char *ModelName)
+{
+    int result = FAIL;
+
+    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_BOOTLOADER:
+            MaxLen = 1*1024*1024;
+            //DEBUG_INFO("Image type: U-Boot\r\n");
+            break;
+        case CSU_KERNEL_CONFIGURATION:
+            MaxLen = 0.5*1024*1024;
+            DEBUG_INFO("Image type: DTB\r\n");
+            break;
+        case CSU_KERNEL_IMAGE:
+            MaxLen = 10*1024*1024;
+            DEBUG_INFO("Image type: Kernel\r\n");
+            break;
+        case CSU_ROOT_FILE_SYSTEM:
+            MaxLen = 48*1024*1024;
+            DEBUG_INFO("Image type: Root fs\r\n");
+            break;
+        case CSU_USER_CONFIGURATION:
+            MaxLen = 6*1024*1024+48;
+            DEBUG_INFO("Image type: Config\r\n");
+            break;
+        default:
+            break;
+    }
+
+    fd = open(SourcePath, O_RDONLY);
+    if(fd < 0)
+    {
+        DEBUG_ERROR("UpdateRootfs NG - can not open rootfs\n");
+        return result;
+    }
+
+    unsigned char *ptr = malloc(MaxLen);
+    memset(ptr,0xFF,MaxLen);
+
+    //get the image length
+    ImageLen = read(fd,ptr,MaxLen);
+    close(fd);
+    //read out the header
+    int i;
+    int isModelNameOK = PASS;
+    for(i=0;i<16;i++)
+    {
+        if(ModelName[i] != ptr[i])
+        {
+            isModelNameOK = FAIL;
+        }
+    }
+
+    if(isModelNameOK == FAIL)
+    {
+        DEBUG_ERROR("Model name mismatch.\r\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-48) == (((unsigned int)ptr[20])<<24 | ((unsigned int)ptr[21])<<16 | ((unsigned int)ptr[22])<<8 | ((unsigned int)ptr[23])))
+            {
+                DataLength = ImageLen-48;
+
+                // 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\r\n",ImageCRC);
+                DEBUG_INFO("CRC32 by calculation: 0x%08X\r\n",crc32(ptr,ImageLen));
+                if(crc32(ptr,ImageLen) == ImageCRC)
+                {
+                    // Write image to target flash block
+                    switch(Type)
+                    {
+                        case FLASH_IMAGE_TYPE_SPL:
+                            fd = open("/dev/mtdblock0", O_RDWR);
+                            if (fd < 0)
+                            {
+                                DEBUG_ERROR("Can not open mtdblock0\r\n");
+                                result = FAIL;
+                            }
+                            else
+                            {
+                                // Write image to flash
+                                DEBUG_INFO("Writing image to mtdblock0...\n");
+                                wrd=write(fd, ptr+48, DataLength);
+                                close(fd);
+                                DEBUG_INFO(">> mtdblock0 Written length: 0x%x\r\n", wrd);
+                                if(wrd != DataLength)
+                                {
+                                    result = FAIL;
+                                }
+                                else
+                                {
+                                    result = PASS;
+                                }
+                            }
+                            break;
+                        case CSU_BOOTLOADER:
+                            fd = open("/dev/mtdblock1", O_RDWR);
+                            if (fd < 0)
+                            {
+                                DEBUG_ERROR("Can not open mtdblock1\r\n");
+                                result = FAIL;
+                            }
+                            else
+                            {
+                                // Write image to flash
+                                DEBUG_INFO("Writing image to mtdblock1...\n");
+                                wrd=write(fd, ptr+48, DataLength);
+                                close(fd);
+                                DEBUG_INFO(">> mtdblock1 written length: 0x%x\r\n", wrd);
+                                if(wrd != DataLength)
+                                {
+                                    result = FAIL;
+                                }
+                                else
+                                {
+                                    // Open flash target mtdblock
+                                    fd = open("/dev/mtdblock3", O_RDWR);
+                                    if (fd < 0)
+                                    {
+                                        DEBUG_ERROR("Can not open mtdblock3\r\n");
+                                        result = FAIL;
+                                    }
+                                    else
+                                    {
+                                        // Write image to flash
+                                        DEBUG_INFO("Writing image to mtdblock3...\n");
+                                        wrd=write(fd, ptr+48, DataLength);
+                                        close(fd);
+                                        DEBUG_INFO(">> mtdblock3 written length: 0x%x\r\n", wrd);
+                                        if(wrd != DataLength)
+                                        {
+                                            result = FAIL;
+                                        }
+                                        else
+                                        {
+                                            result = PASS;
+                                        }
+                                    }
+                                }
+                            }
+                            break;
+                        case CSU_KERNEL_CONFIGURATION:
+                            fd = open("/dev/mtdblock4", O_RDWR);
+                            if (fd < 0)
+                            {
+                                DEBUG_ERROR("Can not open mtdblock4\r\n");
+                                result = FAIL;
+                            }
+                            else
+                            {
+                                // Write image to flash
+                                DEBUG_INFO("Writing image to mtdblock4...\n");
+                                wrd=write(fd, ptr+48, DataLength);
+                                close(fd);
+                                DEBUG_INFO(">> mtdblock4 written length: 0x%x\r\n", wrd);
+                                if(wrd != DataLength)
+                                {
+                                    result = FAIL;
+                                }
+                                else
+                                {
+                                    // Open flash target mtdblock
+                                    fd = open("/dev/mtdblock5", O_RDWR);
+                                    if (fd < 0)
+                                    {
+                                        DEBUG_ERROR("Can not open mtdblock5\r\n");
+                                        result = FAIL;
+                                    }
+                                    else
+                                    {
+                                        // Write image to flash
+                                        DEBUG_INFO("Writing image to mtdblock5...\n");
+                                        wrd=write(fd, ptr+48, DataLength);
+                                        close(fd);
+                                        DEBUG_INFO(">> mtdblock5 written length: 0x%x\r\n", wrd);
+                                        if(wrd != DataLength)
+                                        {
+                                            result = FAIL;
+                                        }
+                                        else
+                                        {
+                                            result = PASS;
+                                        }
+                                    }
+                                }
+                            }
+                            break;
+                        case CSU_KERNEL_IMAGE:
+                            fd = open("/dev/mtdblock6", O_RDWR);
+                            if (fd < 0)
+                            {
+                                DEBUG_ERROR("Can not open mtdblock6\r\n");
+                                result = FAIL;
+                            }
+                            else
+                            {
+                                // Write image to flash
+                                DEBUG_INFO("Writing image to mtdblock6...\n");
+                                wrd=write(fd, ptr+48, DataLength);
+                                close(fd);
+                                DEBUG_INFO(">> mtdblock6 written length: 0x%x\r\n", wrd);
+                                if(wrd != DataLength)
+                                {
+                                    result = FAIL;
+                                }
+                                else
+                                {
+                                    // Open flash target mtdblock
+                                    fd = open("/dev/mtdblock7", O_RDWR);
+                                    if (fd < 0)
+                                    {
+                                        DEBUG_ERROR("Can not open mtdblock7\r\n");
+                                        result = FAIL;
+                                    }
+                                    else
+                                    {
+                                        // Write image to flash
+                                        DEBUG_INFO("Writing image to mtdblock7...\n");
+                                        wrd=write(fd, ptr+48, DataLength);
+                                        close(fd);
+                                        DEBUG_INFO(">> mtdblock7 written length: 0x%x\r\n", wrd);
+                                        if(wrd != DataLength)
+                                        {
+                                            result = FAIL;
+                                        }
+                                        else
+                                        {
+                                            result = PASS;
+                                        }
+                                    }
+                                }
+                            }
+                            break;
+                        case CSU_ROOT_FILE_SYSTEM:
+                            fd = open("/dev/mtdblock8", O_RDWR);
+                            if(fd < 0)
+                            {
+                                DEBUG_ERROR("UpdateRootfs NG - can not open rootfs\n");
+                                result = FAIL;
+                            }
+                            else
+                            {
+                                DEBUG_INFO("Writing image to mtdblock8...\n");
+                                wrd=write(fd, ptr+48, DataLength);
+                                close(fd);
+                                DEBUG_INFO(">> mtdblock8 written length: 0x%x\r\n", wrd);
+                                if(wrd!=DataLength)
+                                {
+                                    result = FAIL;
+                                }
+                                else
+                                {
+                                    fd = open("/dev/mtdblock9", O_RDWR);
+                                    if(fd < 0)
+                                    {
+                                        DEBUG_ERROR("UpdateRootfs NG - can not open rootfs\n");
+                                        result = FAIL;
+                                    }
+
+                                    DEBUG_INFO("Writing image to mtdblock9...\n");
+                                    wrd=write(fd, ptr+48, DataLength);
+                                    close(fd);
+                                    DEBUG_INFO(">> mtdblock9 written length: 0x%x\r\n", wrd);
+                                    if(wrd!=DataLength)
+                                    {
+                                        result = FAIL;
+                                    }
+                                    else
+                                    {
+                                        result = PASS;
+                                    }
+                                }
+                            }
+                            break;
+                        case CSU_USER_CONFIGURATION:
+                            // Open flash target mtdblock
+                            fd = open("/dev/mtdblock10", O_RDWR);
+                            if (fd < 0)
+                            {
+                                DEBUG_ERROR("Can not open mtdblock10\r\n");
+                                result = FAIL;
+                            }
+                            else
+                            {
+                                // Write image to flash
+                                DEBUG_INFO("Writing image to mtdblock10...\n");
+                                wrd=write(fd, ptr+48, DataLength);
+                                close(fd);
+                                DEBUG_INFO(">> mtdblock10 written length: 0x%x\r\n", wrd);
+                                if(wrd != DataLength)
+                                {
+                                    result = FAIL;
+                                }
+                                else
+                                {
+                                    // Open flash target mtdblock
+                                    fd = open("/dev/mtdblock11", O_RDWR);
+                                    if (fd < 0)
+                                    {
+                                        DEBUG_ERROR("Can not open mtdblock11\r\n");
+                                        result = FAIL;
+                                    }
+                                    else
+                                    {
+                                        // Write image to flash
+                                        DEBUG_INFO("Writing image to mtdblock11...\n");
+                                        wrd=write(fd, ptr+48, DataLength);
+                                        close(fd);
+                                        DEBUG_INFO(">> mtdblock11 written length: 0x%x\r\n", wrd);
+                                        if(wrd != DataLength)
+                                        {
+                                            result = FAIL;
+                                        }
+                                        else
+                                        {
+                                            result = PASS;
+                                        }
+                                    }
+                                }
+                            }
+                            break;
+                        default:
+                            break;
+                    }
+                }
+                else
+                    DEBUG_ERROR("Firmware image CRC32 mismatch.\r\n");
+            }
+            else
+                DEBUG_ERROR("Firmware image length mismatch.\r\n");
+        }
+        else
+            DEBUG_ERROR("Firmware image type mismatch.\r\n");
+    }
+    free(ptr);
+
+    if(result == PASS)
+        DEBUG_INFO("Update image success\r\n");
+    else
+        DEBUG_ERROR("Update image fail\r\n");
+
+    return result;
+}
+
+//================================================
+// UART update function
+//================================================
+int uart_tranceive(int fd, unsigned char* cmd, unsigned char* rx, int len, unsigned char needErase)
+{
+    tcflush(fd,TCIOFLUSH);
+    if(write(fd, cmd, len) >= len)
+    {
+        len = 0;
+        if (needErase == 0x01)
+            sleep(5);
+        else
+            usleep(500000);
+        len = read(fd, rx, 512);
+    }
+    else
+    {
+        DEBUG_ERROR("Serial command %s response fail.\n", cmd);
+    }
+
+    return len;
+}
+
+unsigned char uart_update_start(unsigned char 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[512];
+    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) >0)
+    {
+        chksum = 0x00;
+        for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
+        {
+            chksum ^= rx[6+idx];
+        }
+
+        if((chksum == rx[6+(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(unsigned char 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[512];
+    unsigned char chksum = 0x00;
+
+
+    if(uart_tranceive(fd, tx, rx, 7, 0x00) >0)
+    {
+        for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
+        {
+            chksum ^= rx[6+idx];
+        }
+
+        if((chksum == rx[6+(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(unsigned char 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[512];
+    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) >0)
+    {
+        chksum = 0;
+        for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
+        {
+            chksum ^= rx[6+idx];
+        }
+
+        if((chksum == rx[6+(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(unsigned char 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[512];
+    unsigned char chksum = 0x00;
+
+
+    if(uart_tranceive(fd, tx, rx, 7,0x00) >0)
+    {
+        for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
+        {
+            chksum ^= rx[6+idx];
+        }
+
+        if((chksum == rx[6+(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;
+}
+
+int Upgrade_UART(unsigned char uartfd,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 rootfs\n");
+        return result;
+    }
+
+    unsigned char *ptr = malloc(MaxLen);
+    memset(ptr,0xFF,MaxLen);
+
+    //get the image length
+    ImageLen = read(fd,ptr,MaxLen);
+    close(fd);
+    //read out the header
+    int i;
+    int isModelNameOK = PASS;
+    for(i=0;i<16;i++)
+    {
+        if(ModelName[i] != ptr[i])
+        {
+            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-48) == (((unsigned int)ptr[20])<<24 | ((unsigned int)ptr[21])<<16 | ((unsigned int)ptr[22])<<8 | ((unsigned int)ptr[23])))
+            {
+                DataLength = ImageLen-48;
+
+                // 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\r\n",ImageCRC);
+                DEBUG_INFO("CRC32 by calculation: 0x%08X\r\n",crc32(ptr,ImageLen));
+                if(crc32(ptr,ImageLen) == ImageCRC)
+                {
+                    if(uart_update_start(uartfd, TargetAddr, crc32(ptr+48,DataLength))==PASS)
+                    {
+                        int CNT_Fail = 0;
+                        int CNT_Trans = 0;
+                        do
+                        {
+                            if(uart_update_transfer(uartfd, TargetAddr, CNT_Trans*1024, ptr+48+(CNT_Trans*1024), 1024)==PASS)
+                            {
+                                CNT_Fail = 0;
+                                CNT_Trans++;
+                                DEBUG_INFO("Upgrade progress:%.2f%%\r\n", ((float)(CNT_Trans*1024))/(DataLength)*100);
+                            }
+                            else
+                            {
+                                DEBUG_WARN("Data transfer fail, retry %d \r\n", ++CNT_Fail);
+                            }
+                        }while(DataLength-(CNT_Trans*1024)>0 && CNT_Fail<3);
+
+                        if(CNT_Fail>=3)
+                        {
+                            uart_update_abord(uartfd, TargetAddr);
+                            DEBUG_ERROR("UART upgrade retry > limits, aboard upgrade.\r\n");
+                        }
+                        else if(uart_update_finish(uartfd, TargetAddr)==PASS)
+                        {
+                            result = PASS;
+                        }
+                    }
+                    else
+                        DEBUG_ERROR("UART upgrade request failed.\n");
+                }
+                else
+                    DEBUG_ERROR("Firmware image CRC32 mismatch.\r\n");
+            }
+            else
+                DEBUG_ERROR("Firmware image length mismatch.\r\n");
+        }
+        else
+            DEBUG_ERROR("Firmware image type mismatch.\r\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 rootfs\n");
+        return result;
+    }
+
+    unsigned char *ptr = malloc(MaxLen);
+    memset(ptr,0xFF,MaxLen);
+
+    //get the image length
+    ImageLen = read(fd,ptr,MaxLen);
+    close(fd);
+    //read out the header
+    int i;
+    int isModelNameOK = PASS;
+    for(i=0;i<16;i++) {
+        if(ModelName[i] != ptr[i]){
+            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-48) == (((unsigned int)ptr[20])<<24 | ((unsigned int)ptr[21])<<16 | ((unsigned int)ptr[22])<<8 | ((unsigned int)ptr[23])))
+            {
+                DataLength = ImageLen-48;
+
+                // 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\r\n",ImageCRC);
+                DEBUG_INFO("CRC32 by calculation: 0x%08X\r\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 + 48);
+                    }
+
+                    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 + 48);
+                                }
+                                DEBUG_INFO(" \r\n\r\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.\r\n");
+            }
+            else
+                DEBUG_ERROR("Firmware image length mismatch.\r\n");
+        }
+        else
+            DEBUG_ERROR("Firmware image type mismatch.\r\n");
+
+    }
+    free(ptr);
+    return result;
+}
+
+//================================================
+// CCS update function
+//================================================
+int Check_CCS_image_exist(char *SourcePath)
+{
+    int fd;
+
+    fd = open(SourcePath, O_RDONLY);
+
+    if(fd < 0)
+    {
+        DEBUG_ERROR("Update CCS NG - can not open upgrade image\n");
+        return FAIL;
+    }
+    else
+    {
+        return PASS;
+    }
+}
+
+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(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(ftpcmdbuf,"ftpput -u root -p y42j/4cj84 %s /root/ccs.image %s",
+    CCSIpAddress, SourcePath);
+
+    if(system(ftpcmdbuf) != 0)
+    {
+        DEBUG_ERROR("Update CCS NG - FTP put CCS upgrade image to CCS board %d fail\n", TargetAddr);
+        return FAIL;
+    }
+    else
+    {
+        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 = 30 * 60 * 1000 * 1000;  //30 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)
+{
+    if(Type == CCS_BOARD_BOOTLOADER)
+    {
+        DEBUG_INFO("Upgrade CCS BOOTLOADER to CCS board %d, file name: %s.\n", TargetAddr, SourcePath);
+    }
+    else if(Type == CCS_BOARD_KERNEL_CONFIGURATION)
+    {
+        DEBUG_INFO("Upgrade CCS KERNEL CONFIGURATION to CCS board %d, file name: %s.\n", TargetAddr, SourcePath);
+    }
+    else if(Type == CCS_BOARD_KERNEL_IMAGE)
+    {
+        DEBUG_INFO("Upgrade CCS KERNEL to CCS board %d, file name: %s.\n", TargetAddr, SourcePath);
+    }
+    else if(Type == CCS_BOARD_FILE_SYSTEM)
+    {
+        DEBUG_INFO("Upgrade CCS FILE SYSTEM to CCS board %d, file name: %s.\n", TargetAddr, SourcePath);
+    }
+
+    if(Check_CCS_image_exist(SourcePath) == 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;
+}

+ 115 - 113
EVSE/Modularization/Module_Upgrade.h

@@ -1,113 +1,115 @@
-/*
- * Module_Upgrade.h
- *
- *  Created on: 2019-12-24
- *      Author: Jerry Wang
- *     Version: D0.01
- */
-
-#ifndef MODULE_UPGRADE_H_
-#define MODULE_UPGRADE_H_
-
-#include 	<sys/time.h>
-#include 	<sys/timeb.h>
-#include    <sys/types.h>
-#include    <sys/stat.h>
-#include 	<sys/types.h>
-#include 	<sys/ioctl.h>
-#include 	<sys/socket.h>
-#include 	<sys/ipc.h>
-#include 	<sys/shm.h>
-#include 	<sys/shm.h>
-#include 	<sys/mman.h>
-#include 	<linux/wireless.h>
-#include 	<linux/can.h>
-#include 	<linux/can/raw.h>
-#include 	<arpa/inet.h>
-#include 	<netinet/in.h>
-
-#include 	<unistd.h>
-#include 	<stdarg.h>
-#include    <stdio.h>
-#include    <stdlib.h>
-#include    <unistd.h>
-#include    <fcntl.h>
-#include    <termios.h>
-#include    <errno.h>
-#include 	<errno.h>
-#include 	<string.h>
-#include	<time.h>
-#include	<ctype.h>
-#include 	<ifaddrs.h>
-
-enum Image_Type
-{
-	CSU_BOOTLOADER 		           = 0x10000001,
-	CSU_KERNEL_CONFIGURATION       = 0x10000002,
-	CSU_KERNEL_IMAGE 	           = 0x10000003,
-	CSU_ROOT_FILE_SYSTEM	       = 0x10000004,
-	CSU_USER_CONFIGURATION         = 0x10000005,
-	CSU_PRIMARY_CONTROLLER 		   = 0x10000006,
-	CCS_BOARD_BOOTLOADER 		   = 0x10000007,
-	CCS_BOARD_KERNEL_CONFIGURATION = 0x10000008,
-	CCS_BOARD_KERNEL_IMAGE 		   = 0x10000009,
-	CCS_BOARD_FILE_SYSTEM		   = 0x1000000A,
-	CHAdeMO_BOARD                  = 0x1000000B,
-	GB_BOARD                       = 0x1000000C,
-	RELAY_CONTROL_BOARD            = 0x1000000D,
-	FAN_CONTROL_BOARD              = 0x1000000E,
-	LCM                            = 0x1000000F,
-	F750_PSU_PRIMARY_CONTROLLER    = 0x10000010,
-	F750_PSU_SECONDARY_CONTROLLER  = 0x10000011,
-	F950_PSU_PRIMARY_CONTROLLER    = 0x10000012,
-	F950_PSU_SECONDARY_CONTROLLER  = 0x10000013,
-	LED_BOARD		       = 0x10000014,
-	AC_CORDSET_CONTROLLER          = 0x20000001,
-	AC_WALLMOUNT_CONTROLLER        = 0x20000002,
-	CMU_IN_BMS                     = 0x30000001,
-	BMU_IN_BMS                     = 0x30000002
-};
-
-enum Flash_ImageType
-{
-	FLASH_IMAGE_TYPE_SPL						= 0x01,
-	FLASH_IMAGE_TYPE_UBOOT						= 0x02,
-	FLASH_IMAGE_TYPE_DTB						= 0x03,
-	FLASH_IMAGE_TYPE_KERNEL						= 0x04,
-	FLASH_IMAGE_TYPE_ROOTFS						= 0x05,
-	FLASH_IMAGE_TYPE_CONFIG						= 0x06
-};
-
-enum Canbus_ImageType
-{
-	CANBUS_IMAGE_TYPE_SPL						= 0x01,
-	CANBUS_IMAGE_TYPE_UBOOT						= 0x02,
-	CANBUS_IMAGE_TYPE_DTB						= 0x03,
-	CANBUS_IMAGE_TYPE_KERNEL					= 0x04,
-	CANBUS_IMAGE_TYPE_ROOTFS					= 0x05,
-	CANBUS_IMAGE_TYPE_CONFIG					= 0x06,
-	CANBUS_IMAGE_TYPE_MCU						= 0x07
-};
-
-enum Canbus_MessageId
-{
-	CANBUS_MESSAGE_ID_UPGRADE_REQUEST			= 0x00000e00,
-	CANBUS_MESSAGE_ID_UPGRADE_TRANS_BLOCK		= 0x00000f00,
-	CANBUS_MESSAGE_ID_UPGRADE_TRANS_DATA		= 0x00001000,
-	CANBUS_MESSAGE_ID_UPGRADE_FINISH			= 0x00001100
-};
-
-enum Uart_Command
-{
-	UART_CMD_UPDATE_START 		= 0xe0,
-	UART_CMD_UPDATE_ABORD 		= 0xe1,
-	UART_CMD_UPDATE_TRANSFER 	= 0xe2,
-	UART_CMD_UPDATE_FINISH 		= 0xe3
-};
-
-
-int Upgrade_Flash(unsigned int Type,char *SourcePath,char *ModelName);
-int Upgrade_UART(unsigned char uartfd,unsigned int Type,unsigned char TargetAddr,char *SourcePath,char *ModelName);
-int Upgrade_CAN(int canfd,unsigned int Type,unsigned char TargetAddr,char *SourcePath,char *ModelName);
-
-#endif /* MODULE_UPGRADE_H_ */
+/*
+ * Module_Upgrade.h
+ *
+ *  Created on: 2019-12-24
+ *      Author: Jerry Wang
+ *     Version: D0.01
+ */
+
+#ifndef MODULE_UPGRADE_H_
+#define MODULE_UPGRADE_H_
+
+#include <sys/time.h>
+#include <sys/timeb.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <sys/ipc.h>
+#include <sys/shm.h>
+#include <sys/shm.h>
+#include <sys/mman.h>
+#include <linux/wireless.h>
+#include <linux/can.h>
+#include <linux/can/raw.h>
+#include <arpa/inet.h>
+#include <netinet/in.h>
+
+#include <unistd.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <termios.h>
+#include <errno.h>
+#include <errno.h>
+#include <string.h>
+#include <time.h>
+#include <ctype.h>
+#include <ifaddrs.h>
+
+
+enum Image_Type
+{
+    CSU_BOOTLOADER                              = 0x10000001,
+    CSU_KERNEL_CONFIGURATION                    = 0x10000002,
+    CSU_KERNEL_IMAGE                            = 0x10000003,
+    CSU_ROOT_FILE_SYSTEM                        = 0x10000004,
+    CSU_USER_CONFIGURATION                      = 0x10000005,
+    CSU_PRIMARY_CONTROLLER                      = 0x10000006,
+    CCS_BOARD_BOOTLOADER                        = 0x10000007,
+    CCS_BOARD_KERNEL_CONFIGURATION              = 0x10000008,
+    CCS_BOARD_KERNEL_IMAGE                      = 0x10000009,
+    CCS_BOARD_FILE_SYSTEM                       = 0x1000000A,
+    CHAdeMO_BOARD                               = 0x1000000B,
+    GB_BOARD                                    = 0x1000000C,
+    RELAY_CONTROL_BOARD                         = 0x1000000D,
+    FAN_CONTROL_BOARD                           = 0x1000000E,
+    LCM                                         = 0x1000000F,
+    F750_PSU_PRIMARY_CONTROLLER                 = 0x10000010,
+    F750_PSU_SECONDARY_CONTROLLER               = 0x10000011,
+    F950_PSU_PRIMARY_CONTROLLER                 = 0x10000012,
+    F950_PSU_SECONDARY_CONTROLLER               = 0x10000013,
+    LED_BOARD                                   = 0x10000014,
+    AC_CORDSET_CONTROLLER                       = 0x20000001,
+    AC_WALLMOUNT_CONTROLLER                     = 0x20000002,
+    CMU_IN_BMS                                  = 0x30000001,
+    BMU_IN_BMS                                  = 0x30000002,
+};
+
+enum Flash_ImageType
+{
+    FLASH_IMAGE_TYPE_SPL                        = 0x01,
+    FLASH_IMAGE_TYPE_UBOOT                      = 0x02,
+    FLASH_IMAGE_TYPE_DTB                        = 0x03,
+    FLASH_IMAGE_TYPE_KERNEL                     = 0x04,
+    FLASH_IMAGE_TYPE_ROOTFS                     = 0x05,
+    FLASH_IMAGE_TYPE_CONFIG                     = 0x06,
+};
+
+enum Canbus_ImageType
+{
+    CANBUS_IMAGE_TYPE_SPL                       = 0x01,
+    CANBUS_IMAGE_TYPE_UBOOT                     = 0x02,
+    CANBUS_IMAGE_TYPE_DTB                       = 0x03,
+    CANBUS_IMAGE_TYPE_KERNEL                    = 0x04,
+    CANBUS_IMAGE_TYPE_ROOTFS                    = 0x05,
+    CANBUS_IMAGE_TYPE_CONFIG                    = 0x06,
+    CANBUS_IMAGE_TYPE_MCU                       = 0x07,
+};
+
+enum Canbus_MessageId
+{
+    CANBUS_MESSAGE_ID_UPGRADE_REQUEST           = 0x00000e00,
+    CANBUS_MESSAGE_ID_UPGRADE_TRANS_BLOCK       = 0x00000f00,
+    CANBUS_MESSAGE_ID_UPGRADE_TRANS_DATA        = 0x00001000,
+    CANBUS_MESSAGE_ID_UPGRADE_FINISH            = 0x00001100,
+};
+
+enum Uart_Command
+{
+    UART_CMD_UPDATE_START                       = 0xe0,
+    UART_CMD_UPDATE_ABORD                       = 0xe1,
+    UART_CMD_UPDATE_TRANSFER                    = 0xe2,
+    UART_CMD_UPDATE_FINISH                      = 0xe3,
+};
+
+
+int Upgrade_Flash(unsigned int Type,char *SourcePath,char *ModelName);
+int Upgrade_UART(unsigned char uartfd,unsigned int Type,unsigned char TargetAddr,char *SourcePath,char *ModelName);
+int Upgrade_CAN(int canfd,unsigned int Type,unsigned char TargetAddr,char *SourcePath,char *ModelName);
+int Upgrade_CCS(int canfd,unsigned int Type,unsigned char TargetAddr,char *SourcePath);
+
+#endif /* MODULE_UPGRADE_H_ */