From 3785a314e693cc9797d8d11fae21f670210b961a Mon Sep 17 00:00:00 2001 From: Dhanya Thattil Date: Fri, 10 Sep 2021 14:07:36 +0200 Subject: [PATCH] wip --- .../slsDetectorServer/src/common.c | 84 ++++--------------- .../src/programFpgaBlackfin.c | 36 ++++---- .../src/slsDetectorServer_funcs.c | 3 +- 3 files changed, 34 insertions(+), 89 deletions(-) diff --git a/slsDetectorServers/slsDetectorServer/src/common.c b/slsDetectorServers/slsDetectorServer/src/common.c index a4794ba3f..34c676e7f 100644 --- a/slsDetectorServers/slsDetectorServer/src/common.c +++ b/slsDetectorServers/slsDetectorServer/src/common.c @@ -7,8 +7,6 @@ #include #include // readlink -#define FLASH_READ_FNAME "/var/tmp/flash.rawbin" - int ConvertToDifferentRange(int inputMin, int inputMax, int outputMin, int outputMax, int inputValue, int *outputValue) { LOG(logDEBUG1, (" Input Value: %d (Input:(%d - %d), Output:(%d - %d))\n", @@ -198,7 +196,7 @@ int verifyChecksumFromBuffer(char *mess, char *clientChecksum, char *buffer, return verifyChecksum(mess, clientChecksum, &c); } -int verifyChecksumFromFile(char *mess, char *clientChecksum, char *fname) { +int verifyChecksumFromFile(char *mess, char *clientChecksum, char *fname, ssize_t fsize) { LOG(logINFO, ("\tVerifying Checksum...\n")); FILE *fp = fopen(fname, "r"); @@ -218,7 +216,9 @@ int verifyChecksumFromFile(char *mess, char *clientChecksum, char *fname) { } const int readUnitSize = 128; char buf[readUnitSize]; - ssize_t bytes = fread(buf, 1, readUnitSize, fp); + buf[0]=0xf; + ssize_t bytes = 1; + ssize_t totalBytesRead = bytes; while (bytes > 0) { if (!MD5_Update(&c, buf, bytes)) { fclose(fp); @@ -226,75 +226,27 @@ int verifyChecksumFromFile(char *mess, char *clientChecksum, char *fname) { LOG(logERROR, (mess)); return FAIL; } - - bytes = fread(buf, 1, readUnitSize, fp); - } - fclose(fp); - return verifyChecksum(mess, clientChecksum, &c); -} - -int verifyChecksumFromFlash(char *mess, char *clientChecksum, char *fname, - ssize_t fsize) { - LOG(logINFO, ("\tVerifying Checksum...\n")); - - 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)) { - fclose(fp); - strcpy(mess, "Unable to calculate checksum (MD5_Init)\n"); - LOG(logERROR, (mess)); - return FAIL; - } - - FILE *flashfp = fopen(FLASH_READ_FNAME, "w"); - if (fp == NULL) { - sprintf(mess, "Unable to open %s in write mode to get from flash\n", - FLASH_READ_FNAME); - LOG(logERROR, (mess)); - return FAIL; - } - - LOG(logINFO, ("\tReading from flash\n")); - int oldProgress = 0; - ssize_t bytes = 0; - while (!feof(fp) && bytes < fsize) { - // print progress - int progress = (int)(((double)(bytes) / fsize) * 100); - if (oldProgress != progress) { - printf("%d%%\r", progress); - fflush(stdout); - oldProgress = progress; - } - // read source - int s = fgetc(fp); - if (s < 0) { + // read only until a particular size (drive) + if (fsize != 0 && totalBytesRead >= fsize) { + LOG(logINFOBLUE, ("\tReached %lu bytes. Not reading more\n", totalBytesRead)); break; } - ++bytes; - write(flashfp, &s, 1); - // swap bits - int d = 0; - for (int i = 0; i < 8; ++i) { - d = d | (((s & (1 << i)) >> i) << (7 - i)); - } - // update md5 - if (!MD5_Update(&c, &d, 1)) { + bytes = fread(buf, 1, readUnitSize, fp); + totalBytesRead += bytes; + } + // last null character (if size does not match) + /*if (fsize == totalBytesRead + 1) { + bytes = 1; + totalBytesRead += bytes; + if (!MD5_Update(&c, buf, bytes)) { fclose(fp); strcpy(mess, "Unable to calculate checksum (MD5_Update)\n"); LOG(logERROR, (mess)); return FAIL; - } - } - LOG(logINFO, ("\tRead %lu bytes to calculate checksum\n", (long int)bytes)); + } + }*/ + LOG(logINFO, ("\tRead %lu bytes to calculate checksum\n", totalBytesRead)); fclose(fp); - fclose(flashfp); return verifyChecksum(mess, clientChecksum, &c); } diff --git a/slsDetectorServers/slsDetectorServer/src/programFpgaBlackfin.c b/slsDetectorServers/slsDetectorServer/src/programFpgaBlackfin.c index c27bc8ba1..4a329beb4 100644 --- a/slsDetectorServers/slsDetectorServer/src/programFpgaBlackfin.c +++ b/slsDetectorServers/slsDetectorServer/src/programFpgaBlackfin.c @@ -66,25 +66,21 @@ void resetFPGA() { usleep(CTRL_SRVR_INIT_TIME_US); } -int deleteOldFile(char *mess) { - char cmd[MAX_STR_LENGTH] = {0}; - char retvals[MAX_STR_LENGTH] = {0}; - sprintf(cmd, "rm -fr %s", TEMP_PROG_FILE_NAME); - if (FAIL == executeCommand(cmd, retvals, logDEBUG1)) { - strcpy(mess, - "Could not program fpga. (could not delete old file: "); - strncat(mess, retvals, sizeof(mess) - strlen(mess) - 1); - strcat(mess, "\n"); - LOG(logERROR, (mess)); - return FAIL; - } - return OK; -} - int preparetoCopyFPGAProgram(FILE **fd, uint64_t fsize, char *mess) { - if (deleteOldFile(mess) == FAIL) { - return FAIL; + // delete old /var/tmp/file + { + char cmd[MAX_STR_LENGTH] = {0}; + char retvals[MAX_STR_LENGTH] = {0}; + sprintf(cmd, "rm -fr %s", TEMP_PROG_FILE_NAME); + if (FAIL == executeCommand(cmd, retvals, logDEBUG1)) { + strcpy(mess, + "Could not program fpga. (could not delete old file: "); + strncat(mess, retvals, sizeof(mess) - strlen(mess) - 1); + strcat(mess, "\n"); + LOG(logERROR, (mess)); + return FAIL; + } } // check available memory to copy program @@ -135,11 +131,7 @@ int copyToFlash(ssize_t fsize, char *clientChecksum, char *mess) { return FAIL; } - if (deleteOldFile(mess) == FAIL) { - return FAIL; - } - - if (verifyChecksumFromFlash(mess, clientChecksum, flashDriveName, fsize) == + if (verifyChecksumFromFile(mess, clientChecksum, flashDriveName, fsize) == FAIL) { return FAIL; } diff --git a/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c b/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c index 996a58dc3..991566747 100644 --- a/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c +++ b/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c @@ -3795,7 +3795,8 @@ int program_fpga(int file_des) { // checksum of copied program if (ret == OK) { - ret = verifyChecksumFromFile(mess, checksum, TEMP_PROG_FILE_NAME); + ret = + verifyChecksumFromFile(mess, checksum, TEMP_PROG_FILE_NAME, 0); } Server_SendResult(file_des, INT32, NULL, 0); if (ret == FAIL) {