From 124abf02137ff4588f65259c64d2c719b81ae2e7 Mon Sep 17 00:00:00 2001 From: "Jakob.Meier" <–meierj@irs.uni-stuttgart.de> Date: Wed, 18 Aug 2021 08:26:31 +0200 Subject: [PATCH] crc for ploc update --- README.md | 2 +- bsp_q7s/devices/PlocUpdater.cpp | 28 +++++++++++++++---- bsp_q7s/devices/PlocUpdater.h | 7 ++++- .../PlocSupervisorDefinitions.h | 2 +- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index d75c535b..cb2f9f53 100644 --- a/README.md +++ b/README.md @@ -926,7 +926,7 @@ candump can0 ## Dump content of file in hex ```` -cat file.bin | hexdump +cat file.bin | hexdump -C ```` ## Preparation of a fresh rootfs and SD card diff --git a/bsp_q7s/devices/PlocUpdater.cpp b/bsp_q7s/devices/PlocUpdater.cpp index 0e4ea77d..c37855a4 100644 --- a/bsp_q7s/devices/PlocUpdater.cpp +++ b/bsp_q7s/devices/PlocUpdater.cpp @@ -292,7 +292,7 @@ void PlocUpdater::commandUpdateAvailable() { remainingPackets = numOfUpdatePackets; packetsSent = 0; - uint32_t imageCrc = makeCrc(); + calcImageCrc(); PLOC_SPV::UpdateInfo packet(PLOC_SPV::APID_UPDATE_AVAILABLE, static_cast(updateMemory), static_cast(updatePartition), imageSize, imageCrc, numOfUpdatePackets); @@ -338,7 +338,7 @@ void PlocUpdater::commandUpdatePacket() { file.close(); // sequence count of first packet is 1 packet.setPacketSequenceCount((packetsSent + 1) & PLOC_SPV::SEQUENCE_COUNT_MASK); - if (numOfUpdatePackets > 0) { + if (numOfUpdatePackets > 1) { adjustSequenceFlags(packet); } packet.makeCrc(); @@ -383,9 +383,27 @@ void PlocUpdater::commandUpdateVerify() { return; } -ReturnValue_t PlocUpdater::makeCrc() { - //TODO: Waiting on input from TAS about the CRC to use - return 0; +void PlocUpdater::calcImageCrc() { + std::ifstream file(updateFile, std::ifstream::binary); + file.seekg(0, file.end); + uint32_t count; + uint32_t bit; + uint32_t remainder = INITIAL_REMAINDER_32; + char input; + for (count = 0; count < imageSize; count++) { + file.seekg(count, file.beg); + file.read(&input, 1); + remainder ^= (input << 16); + for (bit = 8; bit > 0; --bit) { + if (remainder & TOPBIT_32) { + remainder = (remainder << 1) ^ POLYNOMIAL_32; + } else { + remainder = (remainder << 1); + } + } + } + file.close(); + imageCrc = (remainder ^ FINAL_XOR_VALUE_32); } void PlocUpdater::adjustSequenceFlags(PLOC_SPV::UpdatePacket& packet) { diff --git a/bsp_q7s/devices/PlocUpdater.h b/bsp_q7s/devices/PlocUpdater.h index 8d7d9e30..c8a2b67b 100644 --- a/bsp_q7s/devices/PlocUpdater.h +++ b/bsp_q7s/devices/PlocUpdater.h @@ -92,6 +92,11 @@ private: // Maximum size of update payload data per space packet (max size of space packet is 1024 bytes) static const size_t MAX_SP_DATA = 1016; + static const uint32_t TOPBIT_32 = (1 << 31); + static const uint32_t POLYNOMIAL_32 = 0x04C11DB7; + static const uint32_t INITIAL_REMAINDER_32 = 0xFFFFFFFF; + static const uint32_t FINAL_XOR_VALUE_32 = 0xFFFFFFFF; + MessageQueueIF* commandQueue = nullptr; #if BOARD_TE0720 == 0 @@ -168,7 +173,7 @@ private: bool isSdCardMounted(sd::SdCard sdCard); #endif - ReturnValue_t makeCrc(); + void calcImageCrc(); void adjustSequenceFlags(PLOC_SPV::UpdatePacket& packet); }; diff --git a/bsp_q7s/devices/devicedefinitions/PlocSupervisorDefinitions.h b/bsp_q7s/devices/devicedefinitions/PlocSupervisorDefinitions.h index 909379df..26d44272 100644 --- a/bsp_q7s/devices/devicedefinitions/PlocSupervisorDefinitions.h +++ b/bsp_q7s/devices/devicedefinitions/PlocSupervisorDefinitions.h @@ -1453,7 +1453,7 @@ public: UpdateInfo(uint16_t apid, uint8_t memory, uint8_t partition, uint32_t imageSize, uint32_t imageCrc, uint32_t numPackets) : SupvTcSpacePacket(PAYLOAD_LENGTH, apid), memory(memory), partition(partition), imageSize( - imageSize), numPackets(numPackets) { + imageSize), imageCrc(imageCrc), numPackets(numPackets) { initPacket(); makeCrc(); }