commit 4a98dfd0520f15ddcd5ac181991ef5c79b4943cb from: Aleksey Ryndin date: Thu Mar 07 10:35:31 2024 UTC Add checksum calculation commit - 047062a2f005cb2382e7ab336c79e084f4dc6567 commit + 4a98dfd0520f15ddcd5ac181991ef5c79b4943cb blob - 04e7a99f6764824e1494ae9bc5c2eefef0084bff blob + a9886f3fdc8a849b5c7fe45d3bbfd3408023fe38 --- tools/img_eGON.BT0/img_eGON.BT0.cc +++ tools/img_eGON.BT0/img_eGON.BT0.cc @@ -8,12 +8,13 @@ #include #include #include +#include + namespace { - /** Non-copiable objects */ struct NonCopiable { @@ -186,11 +187,48 @@ bool validate_and_dump(const char *image_file_path) std::cout << "+0x0C Checksum : 0x" << std::hex << std::setw(8) << std::setfill('0') << header.m_checksum << std::endl; + + const bool length_expected = + (header.m_length > sizeof(header)) && (header.m_length & 511) == 0; + if (ret_value && length_expected) + { + const auto saved_checksum{header.m_checksum}; + header.m_checksum = 0x5f0a6c39; + + uint32_t checksum = 0; + for (uint32_t i = 0; i < sizeof(header) / sizeof(uint32_t); ++i) + checksum += reinterpret_cast(&header)[i]; + + const uint32_t buff_size = header.m_length - sizeof(header); + std::vector buff; + buff.resize(buff_size); + if (read(image_file.get(), &buff[0], buff_size) == buff_size) + { + for (uint32_t i = 0; i < buff_size / sizeof(uint32_t); ++i) + checksum += reinterpret_cast(&buff[0])[i]; + if (checksum == saved_checksum) + { + std::cout << " (v) OK" << std::endl; + } + else + { + ret_value = false; + } + } + else + { + ret_value = false; + } + } + else + { + ret_value = false; + } std::cout << "+0x10 Length : 0x" << std::hex << std::setw(8) << std::setfill('0') << header.m_length << ", " << std::dec << header.m_length << std::endl; - if ((header.m_length & 511) == 0) + if (length_expected) { std::cout << " (v) OK" << std::endl; }