fix for flash

This commit is contained in:
maliakal_d 2021-09-10 13:25:17 +02:00
parent 9d30d51da7
commit b8086dcd70
5 changed files with 62 additions and 25 deletions

View File

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

View File

@ -196,7 +196,7 @@ int verifyChecksumFromBuffer(char *mess, char *clientChecksum, char *buffer,
return verifyChecksum(mess, clientChecksum, &c);
}
int verifyChecksumFromFile(char *mess, char *clientChecksum, char *fname, ssize_t fsize) {
int verifyChecksumFromFile(char *mess, char *clientChecksum, char *fname) {
LOG(logINFO, ("\tVerifying Checksum...\n"));
FILE *fp = fopen(fname, "r");
@ -217,7 +217,6 @@ int verifyChecksumFromFile(char *mess, char *clientChecksum, char *fname, ssize_
const int readUnitSize = 128;
char buf[readUnitSize];
ssize_t bytes = fread(buf, 1, readUnitSize, fp);
ssize_t totalBytesRead = bytes;
while (bytes > 0) {
if (!MD5_Update(&c, buf, bytes)) {
fclose(fp);
@ -225,26 +224,64 @@ int verifyChecksumFromFile(char *mess, char *clientChecksum, char *fname, ssize_
LOG(logERROR, (mess));
return FAIL;
}
// 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);
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;
}
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) {
break;
}
++bytes;
// 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)) {
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));
}
}
LOG(logINFO, ("\tRead %lu bytes to calculate checksum\n", (long int)bytes));
fclose(fp);
return verifyChecksum(mess, clientChecksum, &c);
}

View File

@ -131,7 +131,8 @@ int copyToFlash(ssize_t fsize, char *clientChecksum, char *mess) {
return FAIL;
}
if (verifyChecksumFromFile(mess, clientChecksum, flashDriveName, fsize) == FAIL) {
if (verifyChecksumFromFlash(mess, clientChecksum, flashDriveName, fsize) ==
FAIL) {
return FAIL;
}
LOG(logINFO, ("Checksum in Flash verified\n"));

View File

@ -3795,7 +3795,7 @@ int program_fpga(int file_des) {
// checksum of copied program
if (ret == OK) {
ret = verifyChecksumFromFile(mess, checksum, TEMP_PROG_FILE_NAME, 0);
ret = verifyChecksumFromFile(mess, checksum, TEMP_PROG_FILE_NAME);
}
Server_SendResult(file_des, INT32, NULL, 0);
if (ret == FAIL) {

View File

@ -1241,7 +1241,7 @@ int DetectorImpl::kbhit() {
}
std::vector<char> DetectorImpl::readProgrammingFile(const std::string &fname) {
/* // validate type of file
// validate type of file
bool isPof = false;
switch (multi_shm()->multiDetectorType) {
case JUNGFRAU:
@ -1353,9 +1353,6 @@ std::vector<char> DetectorImpl::readProgrammingFile(const std::string &fname) {
throw RuntimeError(
"Could not convert programming file. EOF before end of flash");
}
// write 0 to tthe end
char c = '\0';
write(dst, &c, 1);
}
if (fclose(src) != 0) {
throw RuntimeError("Program FPGA: Could not close source file");
@ -1364,9 +1361,9 @@ std::vector<char> DetectorImpl::readProgrammingFile(const std::string &fname) {
throw RuntimeError("Program FPGA: Could not close destination file");
}
LOG(logINFOBLUE) << "File has been converted to " << destfname;
*/
// loading dst file to memory
FILE *fp = fopen("/tmp/SLS_DET_MCB.ZpfQln", "r");//
FILE *fp = fopen(destfname, "r");
if (fp == nullptr) {
throw RuntimeError("Program FPGA: Could not open rawbin file");
}