/* Includes ------------------------------------------------------------------*/ #include "ymodem.h" #include "string.h" #include "main.h" #include "stdlib.h" #include "W25QXX/w25qxx.h" /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ /** * @} */ /* Exported constants --------------------------------------------------------*/ /* Packet structure defines */ #define PACKET_HEADER_SIZE ((uint32_t)3) #define PACKET_DATA_INDEX ((uint32_t)4) #define PACKET_START_INDEX ((uint32_t)1) #define PACKET_NUMBER_INDEX ((uint32_t)2) #define PACKET_CNUMBER_INDEX ((uint32_t)3) #define PACKET_TRAILER_SIZE ((uint32_t)2) #define PACKET_OVERHEAD_SIZE (PACKET_HEADER_SIZE + PACKET_TRAILER_SIZE - 1) #define PACKET_SIZE ((uint32_t)128) #define PACKET_1K_SIZE ((uint32_t)1024) /* /-------- Packet in IAP memory ------------------------------------------\ * | 0 | 1 | 2 | 3 | 4 | ... | n+4 | n+5 | n+6 | * |------------------------------------------------------------------------| * | unused | start | number | !num | data[0] | ... | data[n] | crc0 | crc1 | * \------------------------------------------------------------------------/ * the first byte is left unused for memory alignment reasons */ #define FILE_NAME_LENGTH ((uint32_t)64) #define FILE_SIZE_LENGTH ((uint32_t)16) #define SOH ((uint8_t)0x01) /* start of 128-byte data packet */ #define STX ((uint8_t)0x02) /* start of 1024-byte data packet */ #define EOT ((uint8_t)0x04) /* end of transmission */ #define ACK ((uint8_t)0x06) /* acknowledge */ #define NAK ((uint8_t)0x15) /* negative acknowledge */ #define CA ((uint32_t)0x18) /* two of these in succession aborts transfer */ #define CRC16 ((uint8_t)0x43) /* 'C' == 0x43, request 16-bit CRC */ #define NEGATIVE_BYTE ((uint8_t)0xFF) #define ABORT1 ((uint8_t)0x41) /* 'A' == 0x41, abort by user */ #define ABORT2 ((uint8_t)0x61) /* 'a' == 0x61, abort by user */ #define NAK_TIMEOUT ((uint32_t)0x100000) #define DOWNLOAD_TIMEOUT ((uint32_t)1000) /* One second retry delay */ #define MAX_ERRORS ((uint32_t)5) /* Private macro -------------------------------------------------------------*/ #define APPLICATION_ADDRESS 0x30000000 #define USER_FLASH_SIZE 0x40000 ///< 256KB /* Private variables ---------------------------------------------------------*/ /* @note ATTENTION - please keep this variable 32bit alligned */ static uint8_t aPacketData[PACKET_1K_SIZE + PACKET_DATA_INDEX + PACKET_TRAILER_SIZE]; #define UDP_RECV_BUF_LEN 10240 static uint8_t aucRecvBuf[10240]; static int udpRxLen = 0; static int udpRxPos = 0; /* Private function prototypes -----------------------------------------------*/ static HAL_StatusTypeDef ReceivePacket(uint8_t *p_data, uint32_t *p_length, uint32_t timeout); static uint16_t UpdateCRC16(uint16_t crc_in, uint8_t byte); static uint16_t Cal_CRC16(const uint8_t* p_data, uint32_t size); /* Private functions ---------------------------------------------------------*/ extern int udpRecv(uint8_t *buf, int len); extern int udpSend(uint8_t *buf, int len); HAL_StatusTypeDef ymodemTx(uint8_t ucByte) { static uint8_t ucBuf = 0; ucBuf = ucByte; udpSend(&ucBuf, 1); return HAL_OK; } HAL_StatusTypeDef ymodemTx2(uint8_t ucByte1, uint8_t ucByte2) { static uint8_t aucBuf[2]; aucBuf[0] = ucByte1; aucBuf[1] = ucByte2; udpSend(aucBuf, 2); return HAL_OK; } HAL_StatusTypeDef ymodemRx(uint8_t *pucBytes, uint32_t ulLen, int iTimeout) { int iRet; if (iTimeout < 100) { iTimeout = 100; } debug("ymodem to recv %d bytes", ulLen); if (udpRxLen - udpRxPos >= ulLen) { memcpy(pucBytes, &aucRecvBuf[udpRxPos], ulLen); udpRxPos += ulLen; debug("modem rx %d byte", ulLen); // dump(pucBytes, ulLen); return HAL_OK; } while (1) { if (iTimeout <= 0) { break; } iRet = udpRecv(aucRecvBuf, UDP_RECV_BUF_LEN); iTimeout -= 100; if (iRet > 0) { udpRxPos = 0; udpRxLen = iRet; break; } } if (udpRxLen - udpRxPos >= ulLen) { memcpy(pucBytes, &aucRecvBuf[udpRxPos], ulLen); udpRxPos += ulLen; debug("modem rx %d byte", ulLen); // dump(pucBytes, ulLen); return HAL_OK; } return HAL_TIMEOUT; } int FLASH_If_dump(uint32_t addr, uint32_t ulLen) { uint8_t aucbuf[1024]; int start_sector = 16; addr = addr + start_sector * 4096; while (ulLen >= 16) { W25QXX_Read(aucbuf, addr, 16); dump(aucbuf, 16); addr += 16; ulLen -= 16; } if (ulLen > 0) { W25QXX_Read(aucbuf, addr, ulLen); dump(aucbuf, ulLen); } return 0; } int ram_dump(uint8_t *ram, uint32_t ulLen) { while (ulLen >= 16) { dump(ram, 16); ram += 16; ulLen -= 16; } if (ulLen > 0) { dump(ram, ulLen); } return 0; } int FLASH_CehckEmpty(uint32_t addr, uint32_t ulLen) { uint8_t aucbuf[1024]; while (ulLen >= 256) { W25QXX_Read(aucbuf, addr, 256); for (int i= 0; i < 256; i++) { if (aucbuf[i] != 0xFF) { return i; } } addr += 256; ulLen -= 256; } if (ulLen > 0) { W25QXX_Read(aucbuf, addr, ulLen); for (int i= 0; i < ulLen; i++) { if (aucbuf[i] != 0xFF) { return i; } } } return -1; } int FLASH_If_Erase(uint32_t size) { int ret; int start_sector = 16; int num = size / 4096; if (size % 4096) { num++; } for (int i = 0; i < num; ++i) { W25QXX_Erase_Sector((start_sector + i) * 4096); debug("erase sector %d", start_sector + i); ret = FLASH_CehckEmpty((start_sector + i) * 4096, 4096); if (ret >= 0) { debug("erase failed at sector %d", i); } } return 0; } int FLASH_If_verify(uint32_t addr, uint8_t *pucSrc, uint32_t ulLen) { static uint8_t aucbuf[1024]; int start_sector = 16; addr = addr + start_sector * 4096; while (ulLen >= 256) { int iCnt = 0; AGAIN: if (W25QXX_Read(aucbuf, addr, 256) != 0) { debug("read flash failed at %d", addr); } if (memcmp(aucbuf, pucSrc, 256) != 0) { debug("verify fialed at %d", addr); debug("flash:"); ram_dump(aucbuf, 256); debug("ram:"); ram_dump(pucSrc, 256); if (iCnt < 3) { iCnt++; goto AGAIN; } return -1; } if (iCnt != 0) { debug("OK"); ram_dump(pucSrc, 256); } pucSrc += 256; addr += 256; ulLen -= 256; } if (ulLen > 0) { W25QXX_Read(aucbuf, addr, ulLen); if (memcmp(aucbuf, pucSrc, ulLen) != 0) { debug("verify fialed at %d", addr); return -1; } } return 0; } int FLASH_If_Write(uint32_t addr, uint8_t *pucSrc, uint32_t ulLen) { int start_sector = 16; addr = addr + start_sector * 4096; while (ulLen >= 256) { W25QXX_Page_Program(pucSrc, addr, 256); pucSrc += 256; addr += 256; ulLen -= 256; } if (ulLen > 0) { W25QXX_Page_Program(pucSrc, addr, ulLen); } return 0; } /** * @brief Receive a packet from sender * @param data * @param length * 0: end of transmission * 2: abort by sender * >0: packet length * @param timeout * @retval HAL_OK: normally return * HAL_BUSY: abort by user */ static HAL_StatusTypeDef ReceivePacket(uint8_t *p_data, uint32_t *p_length, uint32_t timeout) { uint32_t crc; uint32_t packet_size = 0; HAL_StatusTypeDef status; uint8_t char1; *p_length = 0; status = ymodemRx(&char1, 1, timeout); if (status == HAL_OK) { switch (char1) { case SOH: packet_size = PACKET_SIZE; debug("SOH"); break; case STX: packet_size = PACKET_1K_SIZE; break; case EOT: break; case CA: if ((ymodemRx(&char1, 1, timeout) == HAL_OK) && (char1 == CA)) { packet_size = 2; } else { status = HAL_ERROR; } break; case ABORT1: case ABORT2: status = HAL_BUSY; break; default: status = HAL_ERROR; break; } *p_data = char1; if (packet_size >= PACKET_SIZE ) { status = ymodemRx(&p_data[PACKET_NUMBER_INDEX], packet_size + PACKET_OVERHEAD_SIZE, timeout); /* Simple packet sanity check */ if (status == HAL_OK ) { if (p_data[PACKET_NUMBER_INDEX] != ((p_data[PACKET_CNUMBER_INDEX]) ^ NEGATIVE_BYTE)) { packet_size = 0; status = HAL_ERROR; debug("PACKET_NUMBER != PACKET_CNUMBER"); } else { /* Check packet CRC */ crc = p_data[ packet_size + PACKET_DATA_INDEX ] << 8; crc += p_data[ packet_size + PACKET_DATA_INDEX + 1 ]; if (Cal_CRC16(&p_data[PACKET_DATA_INDEX], packet_size) != crc ) { packet_size = 0; status = HAL_ERROR; debug("crc error"); } } } else { packet_size = 0; } } } *p_length = packet_size; return status; } /** * @brief Update CRC16 for input byte * @param crc_in input value * @param input byte * @retval None */ uint16_t UpdateCRC16(uint16_t crc_in, uint8_t byte) { uint32_t crc = crc_in; uint32_t in = byte | 0x100; do { crc <<= 1; in <<= 1; if(in & 0x100) ++crc; if(crc & 0x10000) crc ^= 0x1021; } while(!(in & 0x10000)); return crc & 0xffffu; } /** * @brief Cal CRC16 for YModem Packet * @param data * @param length * @retval None */ uint16_t Cal_CRC16(const uint8_t* p_data, uint32_t size) { uint32_t crc = 0; const uint8_t* dataEnd = p_data+size; while(p_data < dataEnd) crc = UpdateCRC16(crc, *p_data++); crc = UpdateCRC16(crc, 0); crc = UpdateCRC16(crc, 0); return crc&0xffffu; } /* Public functions ---------------------------------------------------------*/ /** * @brief Receive a file using the ymodem protocol with CRC16. * @param p_size The size of the file. * @retval COM_StatusTypeDef result of reception/programming */ COM_StatusTypeDef Ymodem_Receive ( uint32_t *p_size, char *aFileName ) { uint32_t i, packet_length, session_done = 0, file_done, errors = 0, session_begin = 0; uint32_t flashdestination, ramsource, filesize; uint8_t *file_ptr; uint8_t file_size[FILE_SIZE_LENGTH], tmp, packets_received; COM_StatusTypeDef result = COM_OK; /* Initialize flashdestination variable */ flashdestination = 0; while ((session_done == 0) && (result == COM_OK)) { packets_received = 0; file_done = 0; while ((file_done == 0) && (result == COM_OK)) { switch (ReceivePacket(aPacketData, &packet_length, DOWNLOAD_TIMEOUT)) { case HAL_OK: errors = 0; switch (packet_length) { case 2: /* Abort by sender */ ymodemTx(ACK); result = COM_ABORT; break; case 0: /* End of transmission */ ymodemTx(ACK); file_done = 1; break; default: /* Normal packet */ if (aPacketData[PACKET_NUMBER_INDEX] != packets_received) { ymodemTx(NAK); } else { if (packets_received == 0) { /* File name packet */ if (aPacketData[PACKET_DATA_INDEX] != 0) { /* File name extraction */ i = 0; file_ptr = aPacketData + PACKET_DATA_INDEX; while ( (*file_ptr != 0) && (i < FILE_NAME_LENGTH)) { aFileName[i++] = *file_ptr++; } /* File size extraction */ aFileName[i++] = '\0'; i = 0; file_ptr ++; while ( (*file_ptr != ' ') && (i < FILE_SIZE_LENGTH)) { file_size[i++] = *file_ptr++; } file_size[i++] = '\0'; filesize = atoi((char *)file_size); /* Test the size of the image to be sent */ /* Image size is greater than Flash size */ printf("[AAA]*p_size : %u , USER_FLASH_SIZE : %u \r\n",*p_size, (USER_FLASH_SIZE + 1)); if (*p_size > (USER_FLASH_SIZE + 1)) { /* End session */ ymodemTx2(CA, CA); result = COM_LIMIT; printf("[AAA] 222 \r\n"); } /* erase user application area */ printf("filesize : %u \r\n",filesize); FLASH_If_Erase(filesize); *p_size = filesize; ymodemTx2(ACK, CRC16); printf("[AAA] 1111 \r\n"); } /* File header packet is empty, end session */ else { ymodemTx(ACK); file_done = 1; session_done = 1; break; } } else /* Data packet */ { // ramsource = (uint32_t) & aPacketData[PACKET_DATA_INDEX]; static uint8_t aucBuf[sizeof(aPacketData)]; memcpy(aucBuf, aPacketData, sizeof(aPacketData)); /* Write received data in Flash */ if (FLASH_If_Write(flashdestination, &aucBuf[PACKET_DATA_INDEX], packet_length) == 0) { if (FLASH_If_verify(flashdestination, &aucBuf[PACKET_DATA_INDEX], packet_length) != 0) { debug("verify failed at %d", flashdestination); } flashdestination += packet_length; ymodemTx(ACK); } else /* An error occurred while writing to Flash memory */ { /* End session */ ymodemTx2(CA, CA); result = COM_DATA; } } packets_received ++; session_begin = 1; } break; } break; case HAL_BUSY: /* Abort actually */ ymodemTx2(CA, CA); result = COM_ABORT; break; default: if (session_begin > 0) { errors ++; } if (errors > MAX_ERRORS) { /* Abort communication */ ymodemTx2(CA, CA); } else { ymodemTx(CRC16); /* Ask for a packet */ } break; } } } return result; } /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ typedef void (*pFunction)(void); static pFunction JumpToApplication; static uint32_t JumpAddress; void copyFirmware(void) { uint32_t ulDstAddr = 0x30000000; uint32_t ulSrcAddr = 16 * 4096; uint32_t ullen = 256 * 1024; while (ullen >= 1024) { W25QXX_Read((uint8_t *)ulDstAddr, ulSrcAddr, 1024); ulDstAddr += 1024; ulSrcAddr += 1024; ullen -= 1024; } while (ullen > 0) { W25QXX_Read((uint8_t *)ulDstAddr, ulSrcAddr, ullen); } } void appJump(void) { __disable_irq(); W25QXX_Read((uint8_t *)(0x20000000 + 127 * 1024), 0, 1024); /* execute the new program */ JumpAddress = *(__IO uint32_t*) (APPLICATION_ADDRESS + 4); /* Jump to user application */ JumpToApplication = (pFunction) JumpAddress; /* Initialize user application's Stack Pointer */ __set_MSP(*(__IO uint32_t*) APPLICATION_ADDRESS); JumpToApplication(); } int checkApp(void) { uint32_t addr; copyFirmware(); dump(APPLICATION_ADDRESS, 16); addr = *(__IO uint32_t*) (APPLICATION_ADDRESS); if ((addr > 0x24000000) && (addr < (0x24000000 + 0x80000))) { } else { return 0; } addr = *(__IO uint32_t*) (APPLICATION_ADDRESS + 4); if ((addr > 0x20000000) && (addr < (0x20000000 + 0x20000))) { } else { return 0; } return 1; }