programs, but flashes checksum wrong

This commit is contained in:
2021-09-09 17:52:36 +02:00
parent aea664f40c
commit a9f82985bc
10 changed files with 115 additions and 35 deletions

View File

@ -35,5 +35,6 @@ int getModuleIdInFile(int *ret, char *mess, char *fileName);
int setModuleIdInFile(char *mess, int arg, char *fileName); int setModuleIdInFile(char *mess, int arg, char *fileName);
int verifyChecksumFromBuffer(char *mess, char *clientChecksum, char *buffer, int verifyChecksumFromBuffer(char *mess, char *clientChecksum, char *buffer,
ssize_t bytes); ssize_t bytes);
int verifyChecksumFromFile(char *mess, char *clientChecksum, char *fname); // fsize is specified only if it is less than intended size (drive)
int verifyChecksumFromFile(char *mess, char *clientChecksum, char *fname, ssize_t fsize);
int verifyChecksum(char *mess, char *clientChecksum, MD5_CTX *c); int verifyChecksum(char *mess, char *clientChecksum, MD5_CTX *c);

View File

@ -2,6 +2,7 @@
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <sys/types.h>
#define TEMP_PROG_FILE_NAME "/var/tmp/tmp.pof" #define TEMP_PROG_FILE_NAME "/var/tmp/tmp.pof"
@ -17,13 +18,13 @@ void resetFPGA();
* open file to copy * open file to copy
*/ */
int preparetoCopyFPGAProgram(FILE **fd, uint64_t fsize, char *mess); int preparetoCopyFPGAProgram(FILE **fd, uint64_t fsize, char *mess);
int copyToFlash(char *clientChecksum, char *mess); int copyToFlash(ssize_t fsize, char *clientChecksum, char *mess);
int getDrive(char *mess); int getDrive(char *mess);
/** Notify fpga not to touch flash, open src and flash drive to write */ /** Notify fpga not to touch flash, open src and flash drive to write */
int openFileForFlash(FILE **flashfd, FILE **srcfd, char *mess); int openFileForFlash(FILE **flashfd, FILE **srcfd, char *mess);
int eraseFlash(char *mess); int eraseFlash(char *mess);
/* write from tmp file to flash */ /* write from tmp file to flash */
int writeToFlash(FILE *flashfd, FILE *srcfd, char *mess); int writeToFlash(ssize_t fsize, FILE *flashfd, FILE *srcfd, char *mess);
/** Notify fpga to pick up firmware from flash and wait for status confirmation /** Notify fpga to pick up firmware from flash and wait for status confirmation
*/ */
int waitForFPGAtoTouchFlash(char *mess); int waitForFPGAtoTouchFlash(char *mess);

View File

@ -181,6 +181,7 @@ int setModuleIdInFile(char *mess, int arg, char *fileName) {
int verifyChecksumFromBuffer(char *mess, char *clientChecksum, char *buffer, int verifyChecksumFromBuffer(char *mess, char *clientChecksum, char *buffer,
ssize_t bytes) { ssize_t bytes) {
LOG(logINFO, ("\tVerifying Checksum...\n"));
MD5_CTX c; MD5_CTX c;
if (!MD5_Init(&c)) { if (!MD5_Init(&c)) {
strcpy(mess, "Unable to calculate checksum (MD5_Init)\n"); strcpy(mess, "Unable to calculate checksum (MD5_Init)\n");
@ -195,7 +196,9 @@ int verifyChecksumFromBuffer(char *mess, char *clientChecksum, char *buffer,
return verifyChecksum(mess, clientChecksum, &c); 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"); FILE *fp = fopen(fname, "r");
if (fp == NULL) { if (fp == NULL) {
sprintf(mess, "Unable to open %s in read mode to get checksum\n", sprintf(mess, "Unable to open %s in read mode to get checksum\n",
@ -211,8 +214,10 @@ int verifyChecksumFromFile(char *mess, char *clientChecksum, char *fname) {
LOG(logERROR, (mess)); LOG(logERROR, (mess));
return FAIL; return FAIL;
} }
char buf[512]; const int readUnitSize = 128;
ssize_t bytes = fread(buf, 1, 512, fp); char buf[readUnitSize];
ssize_t bytes = fread(buf, 1, readUnitSize, fp);
ssize_t totalBytesRead = bytes;
while (bytes > 0) { while (bytes > 0) {
if (!MD5_Update(&c, buf, bytes)) { if (!MD5_Update(&c, buf, bytes)) {
fclose(fp); fclose(fp);
@ -220,8 +225,26 @@ int verifyChecksumFromFile(char *mess, char *clientChecksum, char *fname) {
LOG(logERROR, (mess)); LOG(logERROR, (mess));
return FAIL; return FAIL;
} }
bytes = fread(buf, 1, 512, fp); // read only until a particular size (drive)
/*if (fsize != 0 && totalBytesRead >= fsize) {
LOG(logINFOBLUE, ("\tReached %lu bytes. Not reading more\n", totalBytesRead));
break;
}*/
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", totalBytesRead));
fclose(fp); fclose(fp);
return verifyChecksum(mess, clientChecksum, &c); return verifyChecksum(mess, clientChecksum, &c);
} }
@ -255,6 +278,6 @@ int verifyChecksum(char *mess, char *clientChecksum, MD5_CTX *c) {
LOG(logERROR, (mess)); LOG(logERROR, (mess));
return FAIL; return FAIL;
} }
LOG(logINFO, ("\tChecksum verified from copied program\n")); LOG(logINFO, ("\tChecksum verified\n"));
return OK; return OK;
} }

View File

@ -9,10 +9,12 @@
#include <sys/sysinfo.h> #include <sys/sysinfo.h>
/* global variables */ /* global variables */
// clang-format off
#define MAX_TIME_FPGA_TOUCH_FLASH_US (10 * 1000 * 1000) // 10s #define MAX_TIME_FPGA_TOUCH_FLASH_US (10 * 1000 * 1000) // 10s
#define CMD_GET_FLASH \ #define CMD_GET_FLASH "awk \'$4== \"\\\"bitfile(spi)\\\"\" {print $1}\' /proc/mtd"
"awk \'$4== \"\\\"bitfile(spi)\\\"\" {print $1}\' /proc/mtd"
#define CMD_FPGA_PICKED_STATUS "cat /sys/class/gpio/gpio7/value" #define CMD_FPGA_PICKED_STATUS "cat /sys/class/gpio/gpio7/value"
#define FLASH_BUFFER_MEMORY_SIZE (128 * 1024) // 500 KB
// clang-format on
#define FLASH_DRIVE_NAME_SIZE 16 #define FLASH_DRIVE_NAME_SIZE 16
char flashDriveName[FLASH_DRIVE_NAME_SIZE] = {0}; char flashDriveName[FLASH_DRIVE_NAME_SIZE] = {0};
@ -107,7 +109,7 @@ int preparetoCopyFPGAProgram(FILE **fd, uint64_t fsize, char *mess) {
return OK; return OK;
} }
int copyToFlash(char *clientChecksum, char *mess) { int copyToFlash(ssize_t fsize, char *clientChecksum, char *mess) {
if (getDrive(mess) == FAIL) { if (getDrive(mess) == FAIL) {
return FAIL; return FAIL;
@ -115,19 +117,21 @@ int copyToFlash(char *clientChecksum, char *mess) {
FILE *flashfd = NULL; FILE *flashfd = NULL;
FILE *srcfd = NULL; FILE *srcfd = NULL;
if (openFileForFlash(&flashfd, &srcfd, mess) == FAIL) { if (openFileForFlash(&flashfd, &srcfd, mess) == FAIL) {
return FAIL; return FAIL;
} }
if (eraseFlash(mess) == FAIL) { if (eraseFlash(mess) == FAIL) {
fclose(flashfd);
fclose(srcfd);
return FAIL; return FAIL;
} }
if (writeToFlash(flashfd, srcfd, mess) == FAIL) { if (writeToFlash(fsize, flashfd, srcfd, mess) == FAIL) {
return FAIL; return FAIL;
} }
if (verifyChecksumFromFile(mess, clientChecksum, flashDriveName) == FAIL) { if (verifyChecksumFromFile(mess, clientChecksum, flashDriveName, fsize) == FAIL) {
return FAIL; return FAIL;
} }
LOG(logINFO, ("Checksum in Flash verified\n")); LOG(logINFO, ("Checksum in Flash verified\n"));
@ -155,7 +159,7 @@ int getDrive(char *mess) {
char cmd[MAX_STR_LENGTH] = {0}; char cmd[MAX_STR_LENGTH] = {0};
char retvals[MAX_STR_LENGTH] = {0}; char retvals[MAX_STR_LENGTH] = {0};
strcpy(cmd, CMD_GET_FLASH); strcpy(cmd, CMD_GET_FLASH);
if (executeCommand(cmd, retvals, logINFO) == FAIL) { if (executeCommand(cmd, retvals, logDEBUG1) == FAIL) {
strcpy(mess, "Could not program fpga. (could not get flash drive: "); strcpy(mess, "Could not program fpga. (could not get flash drive: ");
strncat(mess, retvals, sizeof(mess) - strlen(mess) - 1); strncat(mess, retvals, sizeof(mess) - strlen(mess) - 1);
strcat(mess, "\n"); strcat(mess, "\n");
@ -205,7 +209,7 @@ int openFileForFlash(FILE **flashfd, FILE **srcfd, char *mess) {
} }
int eraseFlash(char *mess) { int eraseFlash(char *mess) {
LOG(logDEBUG1, ("Erasing Flash\n")); LOG(logINFO, ("\tErasing Flash...\n"));
#ifdef VIRTUAL #ifdef VIRTUAL
return OK; return OK;
@ -225,10 +229,11 @@ int eraseFlash(char *mess) {
return OK; return OK;
} }
int writeToFlash(FILE *flashfd, FILE *srcfd, char *mess) { int writeToFlash(ssize_t fsize, FILE *flashfd, FILE *srcfd, char *mess) {
LOG(logDEBUG1, ("writing to flash\n")); LOG(logDEBUG1, ("writing to flash\n"));
char* buffer = malloc(MAX_FPGAPROGRAMSIZE);
char* buffer = malloc(FLASH_BUFFER_MEMORY_SIZE);
if (buffer == NULL) { if (buffer == NULL) {
fclose(flashfd); fclose(flashfd);
fclose(srcfd); fclose(srcfd);
@ -237,13 +242,18 @@ int writeToFlash(FILE *flashfd, FILE *srcfd, char *mess) {
LOG(logERROR, (mess)); LOG(logERROR, (mess));
return FAIL; return FAIL;
} }
LOG(logINFO, ("\tWriting to Flash...\n"));
int oldProgress = 0;
ssize_t totalBytes = 0; ssize_t totalBytes = 0;
ssize_t bytes = fread(buffer, sizeof(char), MAX_FPGAPROGRAMSIZE, srcfd); ssize_t bytes = fread((void*)buffer, sizeof(char), FLASH_BUFFER_MEMORY_SIZE, srcfd);
while (bytes > 0) { while (bytes > 0) {
ssize_t bytesWritten = ssize_t bytesWritten =
fwrite((void *)buffer, sizeof(char), bytes, flashfd); fwrite((void*)buffer, sizeof(char), bytes, flashfd);
totalBytes += bytesWritten; totalBytes += bytesWritten;
if (bytesWritten != bytes) { if (bytesWritten != bytes) {
free(buffer); free(buffer);
fclose(flashfd); fclose(flashfd);
@ -256,14 +266,32 @@ int writeToFlash(FILE *flashfd, FILE *srcfd, char *mess) {
LOG(logERROR, (mess)); LOG(logERROR, (mess));
return FAIL; return FAIL;
} }
bytes = fread(buffer, sizeof(char), bytes, srcfd);
printf("."); // print progress
if (fsize > 0) {
int progress = (int)(((double)(totalBytes) / fsize) * 100);
if (oldProgress != progress) {
printf("%d%%\r", progress);
fflush(stdout);
oldProgress = progress;
}
} else printf(".");
bytes = fread((void*)buffer, sizeof(char), FLASH_BUFFER_MEMORY_SIZE, srcfd);
}
if (fsize <= 0) {
printf("\n");
} }
printf("\n");
free(buffer); free(buffer);
fclose(flashfd); fclose(flashfd);
fclose(srcfd); fclose(srcfd);
LOG(logINFO, ("\tWrote %ld bytes to flash\n", totalBytes)); LOG(logINFO, ("\tWrote %ld bytes to flash\n", totalBytes));
if (totalBytes != fsize) {
sprintf(mess, "Could not program fpga. Incorrect bytes written to flash %lu [expected: %lu]\n", totalBytes, fsize);
LOG(logERROR, (mess));
return FAIL;
}
return OK; return OK;
} }

View File

@ -14,6 +14,7 @@
#include <pthread.h> #include <pthread.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <sys/sysinfo.h>
// defined in the detector specific Makefile // defined in the detector specific Makefile
#ifdef GOTTHARDD #ifdef GOTTHARDD
@ -3735,7 +3736,9 @@ int program_fpga(int file_des) {
src = malloc(MAX_FPGAPROGRAMSIZE); src = malloc(MAX_FPGAPROGRAMSIZE);
if (src == NULL) { if (src == NULL) {
fclose(fd); fclose(fd);
strcpy(mess, "Could not allocate memory to get fpga program\n"); struct sysinfo info;
sysinfo(&info);
sprintf(mess, "Could not allocate memory to get fpga program. Free space: %d MB\n", (int)(info.freeram/ (1024 * 1024)));
LOG(logERROR, (mess)); LOG(logERROR, (mess));
ret = FAIL; ret = FAIL;
} }
@ -3792,7 +3795,7 @@ int program_fpga(int file_des) {
// checksum of copied program // checksum of copied program
if (ret == OK) { 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); Server_SendResult(file_des, INT32, NULL, 0);
if (ret == FAIL) { if (ret == FAIL) {
@ -3801,7 +3804,7 @@ int program_fpga(int file_des) {
} }
// copy to flash // copy to flash
ret = copyToFlash(checksum, mess); ret = copyToFlash(totalsize, checksum, mess);
Server_SendResult(file_des, INT32, NULL, 0); Server_SendResult(file_des, INT32, NULL, 0);
if (ret == FAIL) { if (ret == FAIL) {
LOG(logERROR, ("Program FPGA FAIL!\n")); LOG(logERROR, ("Program FPGA FAIL!\n"));

View File

@ -1241,7 +1241,7 @@ int DetectorImpl::kbhit() {
} }
std::vector<char> DetectorImpl::readProgrammingFile(const std::string &fname) { std::vector<char> DetectorImpl::readProgrammingFile(const std::string &fname) {
// validate type of file /* // validate type of file
bool isPof = false; bool isPof = false;
switch (multi_shm()->multiDetectorType) { switch (multi_shm()->multiDetectorType) {
case JUNGFRAU: case JUNGFRAU:
@ -1363,10 +1363,10 @@ std::vector<char> DetectorImpl::readProgrammingFile(const std::string &fname) {
if (close(dst) != 0) { if (close(dst) != 0) {
throw RuntimeError("Program FPGA: Could not close destination file"); throw RuntimeError("Program FPGA: Could not close destination file");
} }
LOG(logDEBUG1) << "File has been converted to " << destfname; LOG(logINFOBLUE) << "File has been converted to " << destfname;
*/
// loading dst file to memory // loading dst file to memory
FILE *fp = fopen(destfname, "r"); FILE *fp = fopen("/tmp/SLS_DET_MCB.ZpfQln", "r");//
if (fp == nullptr) { if (fp == nullptr) {
throw RuntimeError("Program FPGA: Could not open rawbin file"); throw RuntimeError("Program FPGA: Could not open rawbin file");
} }
@ -1389,7 +1389,7 @@ std::vector<char> DetectorImpl::readProgrammingFile(const std::string &fname) {
"Program FPGA: Could not close destination file after converting"); "Program FPGA: Could not close destination file after converting");
} }
unlink(destfname); // delete temporary file //unlink(destfname); // delete temporary file
LOG(logDEBUG1) << "Successfully loaded the rawbin file to program memory"; LOG(logDEBUG1) << "Successfully loaded the rawbin file to program memory";
LOG(logDEBUG1) << "Read file into memory"; LOG(logDEBUG1) << "Read file into memory";
return buffer; return buffer;

View File

@ -3474,11 +3474,34 @@ void Module::programFPGAviaBlackfin(std::vector<char> buffer) {
<< " returned error: " << client.readErrorMessage(); << " returned error: " << client.readErrorMessage();
throw RuntimeError(os.str()); throw RuntimeError(os.str());
} }
if (moduleIndex == 0) { LOG(logINFO) << "Checksum verified for module " << moduleIndex << " ("
LOG(logINFO) << "Checksum verified"; << shm()->hostname << ")";
// erasing flash
{
LOG(logINFO) << "Erasing Flash for module " << moduleIndex << " ("
<< shm()->hostname << ")";
printf("%d%%\r", 0);
std::cout << std::flush;
// erasing takes 65 seconds, printing here (otherwise need threads
// in server-unnecessary)
const int ERASE_TIME = 65;
int count = ERASE_TIME + 1;
while (count > 0) {
std::this_thread::sleep_for(std::chrono::seconds(1));
--count;
printf(
"%d%%\r",
static_cast<int>(
(static_cast<double>(ERASE_TIME - count) / ERASE_TIME) * 100));
std::cout << std::flush;
}
printf("\n");
} }
// copied to flash // copied to flash
LOG(logINFO) << "Writing to Flash to module " << moduleIndex << " ("
<< shm()->hostname << ")";
if (client.Receive<int>() == FAIL) { if (client.Receive<int>() == FAIL) {
std::ostringstream os; std::ostringstream os;
os << "Detector " << moduleIndex << " (" << shm()->hostname << ")" os << "Detector " << moduleIndex << " (" << shm()->hostname << ")"

View File

@ -54,7 +54,8 @@
#define MAX_TRIMEN 100 #define MAX_TRIMEN 100
/** maximum unit size of program sent to detector */ /** maximum unit size of program sent to detector */
#define MAX_FPGAPROGRAMSIZE (2 * 1024 * 1024) //#define MAX_FPGAPROGRAMSIZE (2 * 1024 * 1024)
#define MAX_FPGAPROGRAMSIZE (128 * 1024)
#define GET_FLAG -1 #define GET_FLAG -1

View File

@ -7,7 +7,7 @@
#define APICTB 0x210909 #define APICTB 0x210909
#define APIGOTTHARD 0x210909 #define APIGOTTHARD 0x210909
#define APIGOTTHARD2 0x210909 #define APIGOTTHARD2 0x210909
#define APIJUNGFRAU 0x210909
#define APIMYTHEN3 0x210909 #define APIMYTHEN3 0x210909
#define APIMOENCH 0x210909 #define APIMOENCH 0x210909
#define APIEIGER 0x210909 #define APIEIGER 0x210909
#define APIJUNGFRAU 0x210909