From 664c2ca80f1f7f43c64616832f1d73a2f948abd2 Mon Sep 17 00:00:00 2001 From: Dhanya Thattil Date: Tue, 7 Sep 2021 16:33:52 +0200 Subject: [PATCH] wip done with md5 --- .../ctbDetectorServer/CMakeLists.txt | 2 + .../eigerDetectorServer/CMakeLists.txt | 2 + .../gotthard2DetectorServer/CMakeLists.txt | 2 + .../gotthardDetectorServer/CMakeLists.txt | 2 + .../jungfrauDetectorServer/CMakeLists.txt | 2 + .../moenchDetectorServer/CMakeLists.txt | 2 + .../mythen3DetectorServer/CMakeLists.txt | 2 + .../slsDetectorServer/include/common.h | 7 +- .../include/programFpgaNios.h | 26 +------ .../slsDetectorServer/src/common.c | 78 +++++++++++++++++++ .../src/programFpgaBlackfin.c | 30 +------ .../slsDetectorServer/src/programFpgaNios.c | 9 ++- .../src/slsDetectorServer_funcs.c | 29 +++---- slsDetectorSoftware/src/Module.cpp | 18 ++++- 14 files changed, 135 insertions(+), 76 deletions(-) diff --git a/slsDetectorServers/ctbDetectorServer/CMakeLists.txt b/slsDetectorServers/ctbDetectorServer/CMakeLists.txt index b909f6ecb..9f57c697c 100644 --- a/slsDetectorServers/ctbDetectorServer/CMakeLists.txt +++ b/slsDetectorServers/ctbDetectorServer/CMakeLists.txt @@ -18,12 +18,14 @@ add_executable(ctbDetectorServer_virtual ../slsDetectorServer/src/programFpgaBlackfin.c ../slsDetectorServer/src/sharedMemory.c ../slsDetectorServer/src/loadPattern.c + ../../slsSupportLib/opensslMd5/md5.c ) include_directories( ../slsDetectorServer/include ../../slsSupportLib/include ../../slsDetectorSoftware/include/sls/ + ../../slsSupportLib/opensslMd5 ) target_include_directories(ctbDetectorServer_virtual diff --git a/slsDetectorServers/eigerDetectorServer/CMakeLists.txt b/slsDetectorServers/eigerDetectorServer/CMakeLists.txt index a0f660f2e..5a3b43d7f 100644 --- a/slsDetectorServers/eigerDetectorServer/CMakeLists.txt +++ b/slsDetectorServers/eigerDetectorServer/CMakeLists.txt @@ -6,11 +6,13 @@ set(src ../slsDetectorServer/src/communication_funcs_UDP.c ../slsDetectorServer/src/common.c ../slsDetectorServer/src/sharedMemory.c + ../../slsSupportLib/opensslMd5/md5.c ) include_directories( ../slsDetectorServer/include ../../slsSupportLib/include + ../../slsSupportLib/opensslMd5 ) add_executable(eigerDetectorServerMaster_virtual diff --git a/slsDetectorServers/gotthard2DetectorServer/CMakeLists.txt b/slsDetectorServers/gotthard2DetectorServer/CMakeLists.txt index 9cebb9e9f..05f4275b2 100644 --- a/slsDetectorServers/gotthard2DetectorServer/CMakeLists.txt +++ b/slsDetectorServers/gotthard2DetectorServer/CMakeLists.txt @@ -12,11 +12,13 @@ add_executable(gotthard2DetectorServer_virtual ../slsDetectorServer/src/ASIC_Driver.c ../slsDetectorServer/src/programFpgaNios.c ../slsDetectorServer/src/sharedMemory.c + ../../slsSupportLib/opensslMd5/md5.c ) include_directories( ../slsDetectorServer/include ../../slsSupportLib/include + ../../slsSupportLib/opensslMd5 ) target_include_directories(gotthard2DetectorServer_virtual diff --git a/slsDetectorServers/gotthardDetectorServer/CMakeLists.txt b/slsDetectorServers/gotthardDetectorServer/CMakeLists.txt index d3a14b689..6bef90893 100644 --- a/slsDetectorServers/gotthardDetectorServer/CMakeLists.txt +++ b/slsDetectorServers/gotthardDetectorServer/CMakeLists.txt @@ -11,11 +11,13 @@ add_executable(gotthardDetectorServer_virtual ../slsDetectorServer/src/commonServerFunctions.c ../slsDetectorServer/src/communication_funcs_UDP.c ../slsDetectorServer/src/sharedMemory.c + ../../slsSupportLib/opensslMd5/md5.c ) include_directories( ../slsDetectorServer/include ../../slsSupportLib/include + ../../slsSupportLib/opensslMd5 ) target_include_directories(gotthardDetectorServer_virtual diff --git a/slsDetectorServers/jungfrauDetectorServer/CMakeLists.txt b/slsDetectorServers/jungfrauDetectorServer/CMakeLists.txt index 2d1a6fd12..e2257dd4f 100644 --- a/slsDetectorServers/jungfrauDetectorServer/CMakeLists.txt +++ b/slsDetectorServers/jungfrauDetectorServer/CMakeLists.txt @@ -13,12 +13,14 @@ add_executable(jungfrauDetectorServer_virtual ../slsDetectorServer/src/programFpgaBlackfin.c ../slsDetectorServer/src/communication_funcs_UDP.c ../slsDetectorServer/src/sharedMemory.c + ../../slsSupportLib/opensslMd5/md5.c ) target_include_directories(jungfrauDetectorServer_virtual PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ../slsDetectorServer/include ../../slsSupportLib/include + ../../slsSupportLib/opensslMd5 ) target_compile_definitions(jungfrauDetectorServer_virtual diff --git a/slsDetectorServers/moenchDetectorServer/CMakeLists.txt b/slsDetectorServers/moenchDetectorServer/CMakeLists.txt index 9c36d155b..a28528ef2 100644 --- a/slsDetectorServers/moenchDetectorServer/CMakeLists.txt +++ b/slsDetectorServers/moenchDetectorServer/CMakeLists.txt @@ -15,12 +15,14 @@ add_executable(moenchDetectorServer_virtual ../slsDetectorServer/src/programFpgaBlackfin.c ../slsDetectorServer/src/loadPattern.c ../slsDetectorServer/src/sharedMemory.c + ../../slsSupportLib/opensslMd5/md5.c ) include_directories( ../slsDetectorServer/include ../../slsSupportLib/include ../../slsDetectorSoftware/include/sls/ + ../../slsSupportLib/opensslMd5 ) target_include_directories(moenchDetectorServer_virtual diff --git a/slsDetectorServers/mythen3DetectorServer/CMakeLists.txt b/slsDetectorServers/mythen3DetectorServer/CMakeLists.txt index ea43df079..e93aff5c2 100644 --- a/slsDetectorServers/mythen3DetectorServer/CMakeLists.txt +++ b/slsDetectorServers/mythen3DetectorServer/CMakeLists.txt @@ -13,12 +13,14 @@ add_executable(mythen3DetectorServer_virtual ../slsDetectorServer/src/programFpgaNios.c ../slsDetectorServer/src/loadPattern.c ../slsDetectorServer/src/sharedMemory.c + ../../slsSupportLib/opensslMd5/md5.c ) include_directories( ../slsDetectorServer/include ../../slsSupportLib/include ../../slsDetectorSoftware/include/sls/ + ../../slsSupportLib/opensslMd5 ) target_include_directories(mythen3DetectorServer_virtual diff --git a/slsDetectorServers/slsDetectorServer/include/common.h b/slsDetectorServers/slsDetectorServer/include/common.h index d0beaca28..d83d83f8d 100644 --- a/slsDetectorServers/slsDetectorServer/include/common.h +++ b/slsDetectorServers/slsDetectorServer/include/common.h @@ -1,5 +1,6 @@ #pragma once +#include "md5.h" #include // int64_t #include #include @@ -30,4 +31,8 @@ void validate64(int *ret, char *mess, int64_t arg, int64_t retval, char *modename, enum numberMode nummode); int getModuleIdInFile(int *ret, char *mess, char *fileName); -int setModuleIdInFile(char *mess, int arg, char *fileName); \ No newline at end of file +int setModuleIdInFile(char *mess, int arg, char *fileName); +int verifyChecksumFromBuffer(char *mess, char *clientChecksum, char *buffer, + ssize_t bytes); +int verifyChecksumFromFile(char *mess, char *clientChecksum, char *fname); +int verifyChecksum(char *mess, char *clientChecksum, MD5_CTX *c); \ No newline at end of file diff --git a/slsDetectorServers/slsDetectorServer/include/programFpgaNios.h b/slsDetectorServers/slsDetectorServer/include/programFpgaNios.h index 87633f0c7..889a35b54 100644 --- a/slsDetectorServers/slsDetectorServer/include/programFpgaNios.h +++ b/slsDetectorServers/slsDetectorServer/include/programFpgaNios.h @@ -10,30 +10,8 @@ void NotifyServerStartSuccess(); /** reset fpga and controller(only implemented for >= v1.1 boards) */ void rebootControllerAndFPGA(); - -/** finds the right mtd drive - * @param mess error message - * @returns ok or fail - */ int findFlash(char *mess); - -/** erase flash */ void eraseFlash(); - -/** erase and write flash - * @param mess error message - * @param fpgasrc program source - * @param fsize file size - * @returns ok or fail - */ -int eraseAndWriteToFlash(char *mess, char *fpgasrc, uint64_t fsize); - -/** - * Write FPGA Program to flash - * @param mess error message - * @param fpgasrc source program - * @param fsize size of program - * @param filefp pointer to flash - * @return ok or fail - */ +int eraseAndWriteToFlash(char *mess, char *checksum, char *fpgasrc, + uint64_t fsize); int writeFPGAProgram(char *mess, char *fpgasrc, uint64_t fsize, FILE *filefp); diff --git a/slsDetectorServers/slsDetectorServer/src/common.c b/slsDetectorServers/slsDetectorServer/src/common.c index 08a172d06..553781901 100644 --- a/slsDetectorServers/slsDetectorServer/src/common.c +++ b/slsDetectorServers/slsDetectorServer/src/common.c @@ -178,3 +178,81 @@ int setModuleIdInFile(char *mess, int arg, char *fileName) { } return OK; } + +int verifyChecksumFromBuffer(char *mess, char *clientChecksum, char *buffer, + ssize_t bytes) { + MD5_CTX c; + if (!MD5_Init(&c)) { + strcpy(mess, "Unable to calculate checksum (MD5_Init)\n"); + LOG(logERROR, (mess)); + return FAIL; + } + if (!MD5_Update(&c, buffer, bytes)) { + strcpy(mess, "Unable to calculate checksum (MD5_Update)\n"); + LOG(logERROR, (mess)); + return FAIL; + } + return verifyChecksum(mess, clientChecksum, &c); +} + +int verifyChecksumFromFile(char *mess, char *clientChecksum, char *fname) { + FILE *fp = fopen(fname, "r"); + if (fp == NULL) { + sprintf(mess, "Unable to open %s in read mode to get checksum\n", + fname); + LOG(logERROR, (mess)); + return FAIL; + } + + MD5_CTX c; + if (!MD5_Init(&c)) { + strcpy(mess, "Unable to calculate checksum (MD5_Init)\n"); + LOG(logERROR, (mess)); + return FAIL; + } + char buf[512]; + ssize_t bytes = fread(buf, 1, 512, fp); + while (bytes > 0) { + if (!MD5_Update(&c, buf, bytes)) { + strcpy(mess, "Unable to calculate checksum (MD5_Update)\n"); + LOG(logERROR, (mess)); + return FAIL; + } + bytes = fread(buf, 1, 512, fp); + } + fclose(fp); + return verifyChecksum(mess, clientChecksum, &c); +} + +int verifyChecksum(char *mess, char *clientChecksum, MD5_CTX *c) { + unsigned char out[MD5_DIGEST_LENGTH]; + if (!MD5_Final(out, c)) { + strcpy(mess, "Unable to calculate checksum (MD5_Final)\n"); + LOG(logERROR, (mess)); + return FAIL; + } + + char checksum[512]; + memset(checksum, 0, 512); + for (int i = 0; i != MD5_DIGEST_LENGTH; ++i) { + char part[16]; + memset(byteparts, 0, 16); + sprintf(part, "%02x", out[i]); + strcat(checksum, part); + } + + LOG(logDEBUG1, + ("\nC checksum: %s\nS checksum: %s\n", clientChecksum, checksum)); + + // compare checksum + if (strcmp(clientChecksum, checksum)) { + sprintf(mess, + "Checksum of copied fpga program does not match. Client " + "checksum:%s, copied checksum:%s\n", + clientChecksum, checksum); + LOG(logERROR, (mess)); + return FAIL; + } + LOG(logINFO, ("\tChecksum verified from copied program\n")); + return OK; +} \ No newline at end of file diff --git a/slsDetectorServers/slsDetectorServer/src/programFpgaBlackfin.c b/slsDetectorServers/slsDetectorServer/src/programFpgaBlackfin.c index f0ae694d1..f0f96bc02 100644 --- a/slsDetectorServers/slsDetectorServer/src/programFpgaBlackfin.c +++ b/slsDetectorServers/slsDetectorServer/src/programFpgaBlackfin.c @@ -1,5 +1,6 @@ #include "programFpgaBlackfin.h" #include "clogger.h" +#include "common.h" #include "sls/ansi.h" #include "slsDetectorServer_defs.h" @@ -193,32 +194,5 @@ int writeFPGAProgram(FILE *fd, char *src, uint64_t fsize, char* msg, char* mess) } int verifyCheckSumofProgram(char* clientChecksum, char* mess) { - LOG(logINFOBLUE, ("\tVerifying Checksum\n")); - - // get checksum from copied file - char cmd[MAX_STR_LENGTH] = {0}; - memset(cmd, 0, MAX_STR_LENGTH); - sprintf(cmd, "md5sum %s", TEMP_PROG_FILE_NAME); - char retvals[MAX_STR_LENGTH] = {0}; - memset(retvals, 0, MAX_STR_LENGTH); - if (FAIL == executeCommand(cmd, retvals, logDEBUG1)) { - strcpy(mess, retvals); - // LOG(logERROR, (mess)); already printed in executecommand - return FAIL; - } - char checksum[MAX_STR_LENGTH]; - memset(checksum, 0, sizeof(checksum)); - if (sscanf(retvals, "%s", checksum) != 1) { - sprintf(mess, "Could not get checksum of fpga program copied over"); - LOG(logERROR, (mess)); - return FAIL; - } - // compare checksum - if (strcmp(clientChecksum, checksum)) { - sprintf(mess, "Checksum of copied fpga program does not match. Client checksum:%s, copied checksum:%s\n", clientChecksum, checksum); - LOG(logERROR, (mess)); - return FAIL; - } - LOG(logINFO, ("\tChecksum verified from copied program\n")); - return OK; + return verifyChecksumFromFile(mess, clientChecksum, TEMP_PROG_FILE_NAME); } \ No newline at end of file diff --git a/slsDetectorServers/slsDetectorServer/src/programFpgaNios.c b/slsDetectorServers/slsDetectorServer/src/programFpgaNios.c index bfd5957c3..be7dc5db1 100644 --- a/slsDetectorServers/slsDetectorServer/src/programFpgaNios.c +++ b/slsDetectorServers/slsDetectorServer/src/programFpgaNios.c @@ -1,5 +1,6 @@ #include "programFpgaNios.h" #include "clogger.h" +#include "common.h" #include "sls/ansi.h" #include "slsDetectorServer_defs.h" @@ -74,10 +75,16 @@ void eraseFlash() { LOG(logINFO, ("\tFlash erased\n")); } -int eraseAndWriteToFlash(char *mess, char *fpgasrc, uint64_t fsize) { +int eraseAndWriteToFlash(char *mess, char *checksum, char *fpgasrc, + uint64_t fsize) { if (findFlash(mess) == FAIL) { return FAIL; } + + if (verifyChecksumFromBuffer(mess, checksum, fpgasrc, fsize) == FAIL) { + return FAIL; + } + eraseFlash(); // open file pointer to flash diff --git a/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c b/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c index 1c918ae38..5b3c9061f 100644 --- a/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c +++ b/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c @@ -3680,13 +3680,20 @@ int program_fpga(int file_des) { LOG(logINFOBLUE, ("Programming FPGA...\n")); -#if defined(MYTHEN3D) || defined(GOTTHARD2D) - uint64_t filesize = 0; // filesize + uint64_t filesize = 0; if (receiveData(file_des, &filesize, sizeof(filesize), INT64) < 0) return printSocketReadError(); - LOG(logDEBUG1, ("Total program size is: %llx\n", - (long long unsigned int)filesize)); + LOG(logDEBUG1, ("Program size is: %lld\n", (long long int)filesize)); + + // checksum + char checksum[MAX_STR_LENGTH]; + memset(checksum, 0, MAX_STR_LENGTH); + if (receiveData(file_des, checksum, MAX_STR_LENGTH, OTHER) < 0) + return printSocketReadError(); + LOG(logDEBUG1, ("checksum is: %s\n\n", checksum)); + +#if defined(MYTHEN3D) || defined(GOTTHARD2D) if (filesize > NIOS_MAX_APP_IMAGE_SIZE) { ret = FAIL; sprintf(mess, @@ -3718,20 +3725,6 @@ int program_fpga(int file_des) { #else // jungfrau, ctb, moench - // filesize - uint64_t filesize = 0; - if (receiveData(file_des, &filesize, sizeof(filesize), INT64) < 0) - return printSocketReadError(); - LOG(logDEBUG1, - ("Program size is: %lld\n", (long long unsigned int)filesize)); - - // checksum - char checksum[MAX_STR_LENGTH]; - memset(checksum, 0, MAX_STR_LENGTH); - if (receiveData(file_des, checksum, MAX_STR_LENGTH, OTHER) < 0) - return printSocketReadError(); - LOG(logDEBUG1, ("checksum is: %s\n\n", checksum)); - // open file and allocate memory for part program FILE *fd = NULL; ret = startCopyingFPGAProgram(&fd, filesize, mess); diff --git a/slsDetectorSoftware/src/Module.cpp b/slsDetectorSoftware/src/Module.cpp index 390ece438..ab1a409dd 100644 --- a/slsDetectorSoftware/src/Module.cpp +++ b/slsDetectorSoftware/src/Module.cpp @@ -3424,16 +3424,17 @@ std::string Module::calculateChecksum(char *buffer, ssize_t bytes) { } void Module::programFPGAviaBlackfin(std::vector buffer) { - uint64_t filesize = buffer.size(); - std::string checksum = calculateChecksum(buffer.data(), filesize); - LOG(logDEBUG1) << "Checksum:" << checksum; - // send program from memory to detector LOG(logINFO) << "Sending programming binary (from pof) to module " << moduleId << " (" << shm()->hostname << ")"; auto client = DetectorSocket(shm()->hostname, shm()->controlPort); client.Send(F_PROGRAM_FPGA); + uint64_t filesize = buffer.size(); client.Send(filesize); + + // checksum + std::string checksum = calculateChecksum(buffer.data(), filesize); + LOG(logDEBUG1) << "Checksum:" << checksum; char cChecksum[MAX_STR_LENGTH]; memset(cChecksum, 0, MAX_STR_LENGTH); strcpy(cChecksum, checksum.c_str()); @@ -3537,6 +3538,15 @@ void Module::programFPGAviaNios(std::vector buffer) { client.Send(F_PROGRAM_FPGA); uint64_t filesize = buffer.size(); client.Send(filesize); + + // checksum + std::string checksum = calculateChecksum(buffer.data(), filesize); + LOG(logDEBUG1) << "Checksum:" << checksum; + char cChecksum[MAX_STR_LENGTH]; + memset(cChecksum, 0, MAX_STR_LENGTH); + strcpy(cChecksum, checksum.c_str()); + client.Send(cChecksum); + if (client.Receive() == FAIL) { std::ostringstream os; os << "Detector " << moduleId << " (" << shm()->hostname << ")"