mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-04-24 07:20:01 +02:00
wip
This commit is contained in:
parent
5b3e5e8d16
commit
3785a314e6
@ -7,8 +7,6 @@
|
||||
#include <string.h>
|
||||
#include <unistd.h> // 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);
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user