diff --git a/CanUds.c b/CanUds.c index 48c2962..a7442b4 100644 --- a/CanUds.c +++ b/CanUds.c @@ -14,6 +14,7 @@ #include "fc7xxx_driver_flash.h" #include "DownloadFile.h" #include "crc32_uds.h" +#include "EraseMemory.h" #define LOG_SIGN "CAN_UDS" #define LOGGER env->logger @@ -975,8 +976,7 @@ static uint16_t vUDS_routine_Compare_Checksum(tCanUds *env, eUdsRoutineControlTy setResponseErrorPending(env, UDS_RoutineControl, UDS_error_requestCorrectlyReceived_ResponsePending); - uint32_t crc32_calc = UdsCrc32Update(0xFFFFFFFF, (uint8_t *)env->BlockMainAdr1, env->BlockMainSize1); - crc32_calc = UdsCrc32Update(crc32_calc, (uint8_t *)env->BlockMainAdr2, env->BlockMainSize2); + uint32_t crc32_calc = UdsCrc32Update(0xFFFFFFFF, (uint8_t *) env->CurrentBlockAdr, env->CurrentBlockSize); crc32_calc = UdsCrc32Finalize(crc32_calc); uint32_t crc32_received = @@ -999,12 +999,21 @@ static uint16_t vUDS_routine_Erase_Memory(tCanUds *env, eUdsRoutineControlType u return 0xFF00 | UDS_error_sub_functionNotSupported; } + if (env->data->len < 13) { + return setResponseError(env, UDS_Communication_Control, + UDS_error_incorrectMessageLengthOrInvalidFormat); + } + + tEraseMemory *eraseMemory = (tEraseMemory *) env->data->data; + env->udsBlock = UDS_BlockInit; - if ((env->data->data[4] == 0) && ((env->data->data[5] == 0))) { + if (eraseMemory->memoryAddress == 0x01100000) { env->udsBlock = UDS_BlockMain; - } else if ((env->data->data[4] == 1) && ((env->data->data[5] == 0))) { + } else if (eraseMemory->memoryAddress == 0x011B0000) { env->udsBlock = UDS_BlockCalib; + } else if (eraseMemory->memoryAddress == 0x011DEC00) { + env->udsBlock = UDS_BlockMeta; } if (env->udsBlock == UDS_BlockInit) { @@ -1013,23 +1022,23 @@ static uint16_t vUDS_routine_Erase_Memory(tCanUds *env, eUdsRoutineControlType u setResponseErrorPending(env, UDS_RoutineControl, UDS_error_requestCorrectlyReceived_ResponsePending); - bool resultMain = false; - bool resultMeta = false; + bool result = false; if (env->udsBlock == UDS_BlockMain) { - resultMain = env->clear_flash_func(0x01100000, 704 * 1024); - uint32_t adrMeta = 0x01100000 + 704 * 1024 - 2048; - resultMeta = env->clear_flash_func(adrMeta, 2048); + result = env->clear_flash_func(0x01100000, 704 * 1024); } if (env->udsBlock == UDS_BlockCalib) { - resultMain = env->clear_flash_func(0x011B0000, 192 * 1024); - resultMeta = true; + result = env->clear_flash_func(0x011B0000, 192 * 1024 - 2048); + } + + if (env->udsBlock == UDS_BlockMeta) { + result = env->clear_flash_func(0x011DF800, 2048); } // bool result = FLASHDRIVER_FlashEraseBlock(FLASH_BLOCK_SELECT1); - if ((resultMain) && (resultMeta)) { + if (result) { env->dataResponse[4] = UDS_routine_RoutineStartStopSuccess; } else { @@ -1336,15 +1345,20 @@ static uint16_t RequestDownload_34(tCanUds *env) { return setResponseError(env, UDS_RequestDownload, UDS_error_incorrectMessageLengthOrInvalidFormat); } - if ((com->dataFormatIdentifier != 0) && (com->dataFormatIdentifier != 1)) { + if (com->dataFormatIdentifier != 0) { return setResponseError(env, UDS_RequestDownload, UDS_error_requestOutOfRange); } - if (com->dataFormatIdentifier == 0) { + + if (com->memoryAddress == 0x01020000) { env->udsBlock = UDS_BlockMain; } - if (com->dataFormatIdentifier == 1) { + if (com->memoryAddress == 0x010D0000) { + env->udsBlock = UDS_BlockCalib; + } + + if (com->memoryAddress == 0x010FF800) { env->udsBlock = UDS_BlockCalib; } @@ -1355,43 +1369,25 @@ static uint16_t RequestDownload_34(tCanUds *env) { if (env->udsBlock == UDS_BlockMain) { - if ((com->memoryAddress != 0x01020000) && (com->memoryAddress != 0x010FF800)) { + if ((com->memoryAddress != 0x01020000) && (com->memorySize > (704 * 1024))) { return setResponseError(env, UDS_RequestDownload, UDS_error_requestOutOfRange); } - - if ((com->memoryAddress == 0x01020000) && (com->memorySize > (704 * 1024))) { - return setResponseError(env, UDS_RequestDownload, UDS_error_requestOutOfRange); - } - - if ((com->memoryAddress == 0x010FF800) && (com->memorySize > (2 * 1024))) { - return setResponseError(env, UDS_RequestDownload, UDS_error_requestOutOfRange); - } - - if (com->memoryAddress == 0x01020000) { - env->BlockMainAdr1 = env->AdrFlash; - env->BlockMainSize1 = env->DownloadFirmwareSize; - } - - if (com->memoryAddress == 0x010FF800) { - env->BlockMainAdr2= env->AdrFlash; - env->BlockMainSize2 = env->DownloadFirmwareSize; - } - } if (env->udsBlock == UDS_BlockCalib) { - if (com->memoryAddress != 0x010D0000) { + if ((com->memoryAddress != 0x010D0000) || (com->memorySize > (192 * 1024 - 2048))){ return setResponseError(env, UDS_RequestDownload, UDS_error_requestOutOfRange); } - if (com->memorySize > (192 * 1024 - 2048)) { + } + + if (env->udsBlock == UDS_BlockMeta) { + + if ((com->memoryAddress != 0x010FF800) || (com->memorySize > (2 * 1024))) { return setResponseError(env, UDS_RequestDownload, UDS_error_requestOutOfRange); } - env->BlockCalibAdr1 = env->AdrFlash; - env->BlockCalibSize1 = env->DownloadFirmwareSize; - } if (env->currentSession == UDS_session_defaultSession) { @@ -1402,6 +1398,9 @@ static uint16_t RequestDownload_34(tCanUds *env) { return setResponseError(env, UDS_RoutineControl, UDS_error_securityAccessDenied); } + env->CurrentBlockAdr = env->AdrFlash; + env->CurrentBlockSize = env->DownloadFirmwareSize; + tRequestDownload_Response *requestDownload_Response = (tRequestDownload_Response *) env->dataResponse; requestDownload_Response->ServiceId = UDS_RequestDownload | 0b1000000; requestDownload_Response->lengthFormatIdentifier = 0x20; // 2 байта - длина поля maxNumberOfBlockLength @@ -1411,7 +1410,8 @@ static uint16_t RequestDownload_34(tCanUds *env) { #if (LOG_UDS == 1) - LoggerFormatInfo(LOGGER, LOG_SIGN, "RequestDownload: memoryAddress = %d memorySize = %d ", env->AdrFlash, env->DownloadFirmwareSize) + LoggerFormatInfo(LOGGER, LOG_SIGN, "RequestDownload: memoryAddress = %d memorySize = %d ", env->AdrFlash, + env->DownloadFirmwareSize) #endif env->stateDownload = UDS_RequestDownload; @@ -1481,7 +1481,7 @@ static uint16_t TransferData_36(tCanUds *env) { #if (LOG_UDS == 1) LoggerFormatInfo(LOGGER, LOG_SIGN, "Download: %d of %d kb", env->SizeWriteFlash / 1024, - env->DownloadFirmwareSize / 1024) + env->DownloadFirmwareSize / 1024) #endif @@ -1513,7 +1513,6 @@ static uint16_t RequestTransferExit_37(tCanUds *env) { } - tRequestTransferExit_Request *com = (tRequestTransferExit_Request *) env->data->data; tRequestTransferExit_Response *requestTransferExit_Response = (tRequestTransferExit_Response *) env->dataResponse; @@ -1830,7 +1829,7 @@ void CanUds(tCanUds *env) { #if (LOG_UDS == 1) LoggerFormatInfo(LOGGER, LOG_SIGN, "Resetting the session to default: %d", - UDS_session_defaultSession) + UDS_session_defaultSession) #endif setDefaultSecurityAccess(env); set_CCU_Mute(env->canSpamTransmitter, false); diff --git a/CanUds.h b/CanUds.h index a20aaa1..7b8cd2a 100644 --- a/CanUds.h +++ b/CanUds.h @@ -138,6 +138,7 @@ typedef enum { typedef enum { UDS_BlockMain = 0, UDS_BlockCalib = 1, + UDS_BlockMeta = 2, UDS_BlockInit = 0xff } eUdsBlock; @@ -197,13 +198,8 @@ typedef struct { uint32_t DownloadFirmwareSize; eUdsBlock udsBlock; - uint32_t BlockMainAdr1; - uint32_t BlockMainSize1; - uint32_t BlockMainAdr2; - uint32_t BlockMainSize2; - - uint32_t BlockCalibAdr1; - uint32_t BlockCalibSize1; + uint32_t CurrentBlockAdr; + uint32_t CurrentBlockSize; eUdsServices stateDownload; diff --git a/EraseMemory.h b/EraseMemory.h new file mode 100644 index 0000000..6e860eb --- /dev/null +++ b/EraseMemory.h @@ -0,0 +1,23 @@ +// +// Created by cfif on 26.05.2026. +// + +#ifndef HVAC_M7_ERASEMEMORY_H +#define HVAC_M7_ERASEMEMORY_H + +#include "stdint.h" + +#pragma scalar_storage_order big-endian + +typedef struct __attribute__ ((packed)) { + uint8_t ServiceId; + uint8_t RouteControlType; + uint16_t RoutineIdentifier; + uint8_t addressAndLengthFormatIdentifier; + uint32_t memoryAddress; + uint32_t memorySize; +} tEraseMemory; + +#pragma scalar_storage_order little-endian + +#endif //HVAC_M7_ERASEMEMORY_H