mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-04-21 11:20:04 +02:00
changes for nios similar to bfin
This commit is contained in:
parent
0f8869153e
commit
78d8086e71
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -10,8 +10,9 @@ void NotifyServerStartSuccess();
|
||||
|
||||
/** reset fpga and controller(only implemented for >= v1.1 boards) */
|
||||
void rebootControllerAndFPGA();
|
||||
int findFlash(char *mess);
|
||||
void eraseFlash();
|
||||
int eraseAndWriteToFlash(char *mess, char *checksum, char *fpgasrc,
|
||||
uint64_t fsize);
|
||||
int writeFPGAProgram(char *mess, char *fpgasrc, uint64_t fsize, FILE *filefp);
|
||||
|
||||
int eraseAndWriteToFlash(char *mess, char *checksum, char *fpgasrc, uint64_t fsize);
|
||||
int getDrive(char *mess);
|
||||
int openFileForFlash(FILE **flashfd, char *mess);
|
||||
int eraseFlash(char *mess);
|
||||
int writeToFlash(ssize_t fsize, FILE *flashfd, char *buffer, char *mess);
|
@ -7,7 +7,9 @@
|
||||
#include <string.h>
|
||||
#include <unistd.h> // readlink
|
||||
|
||||
#ifdef JUNGFRAUD
|
||||
#define FLASH_CONVERTED_NAME "/var/tmp/tests.rawbin"
|
||||
#endif
|
||||
|
||||
int ConvertToDifferentRange(int inputMin, int inputMax, int outputMin,
|
||||
int outputMax, int inputValue, int *outputValue) {
|
||||
@ -337,6 +339,7 @@ int verifyChecksumFromFlash(char *mess, char *clientChecksum, char *fname,
|
||||
return verifyChecksum(mess, clientChecksum, &c, "flash");
|
||||
}
|
||||
*/
|
||||
#ifdef JUNGFRAUD
|
||||
int verifyChecksumFromFlash(char *mess, char *clientChecksum, char *fname,
|
||||
ssize_t fsize) {
|
||||
LOG(logINFO, ("\tVerifying FlashChecksum...\n"));
|
||||
@ -364,7 +367,6 @@ int verifyChecksumFromFlash(char *mess, char *clientChecksum, char *fname,
|
||||
LOG(logERROR, (mess));
|
||||
return FAIL;
|
||||
}
|
||||
const int readUnitSize = 128;
|
||||
int character = fgetc(fp);
|
||||
int oldProgress = 0;
|
||||
ssize_t totalBytesRead = 1;
|
||||
@ -398,8 +400,71 @@ int verifyChecksumFromFlash(char *mess, char *clientChecksum, char *fname,
|
||||
LOG(logINFO, ("\tRead %lu bytes to calculate checksum\n", totalBytesRead));
|
||||
fclose(fp);
|
||||
fclose(flashfp);
|
||||
return verifyChecksum(mess, clientChecksum, &c, "flash");
|
||||
int ret = verifyChecksum(mess, clientChecksum, &c, "flash");
|
||||
if (ret == OK) {
|
||||
LOG(logINFO, ("Checksum in Flash verified\n"));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
#else
|
||||
int verifyChecksumFromFlash(char *mess, char *clientChecksum, char *fname,
|
||||
ssize_t fsize) {
|
||||
LOG(logINFO, ("\tVerifying FlashChecksum...\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;
|
||||
}
|
||||
const int readUnitSize = 128;
|
||||
char buf[readUnitSize];
|
||||
ssize_t bytes = fread(buf, 1, readUnitSize, fp);
|
||||
ssize_t totalBytesRead = bytes;
|
||||
int oldProgress = 0;
|
||||
|
||||
while (bytes > 0) {
|
||||
int progress = (int)(((double)(totalBytesRead) / fsize) * 100);
|
||||
if (oldProgress != progress) {
|
||||
printf("%d%%\r", progress);
|
||||
fflush(stdout);
|
||||
oldProgress = progress;
|
||||
}
|
||||
|
||||
if (!MD5_Update(&c, buf, bytes)) {
|
||||
fclose(fp);
|
||||
strcpy(mess, "Unable to calculate checksum (MD5_Update)\n");
|
||||
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;
|
||||
}
|
||||
LOG(logINFO, ("\tRead %lu bytes to calculate checksum\n", totalBytesRead));
|
||||
fclose(fp);
|
||||
int ret = verifyChecksum(mess, clientChecksum, &c, "flash");
|
||||
if (ret == OK) {
|
||||
LOG(logINFO, ("Checksum in Flash verified\n"));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
int verifyChecksum(char *mess, char *clientChecksum, MD5_CTX *c, char *msg) {
|
||||
unsigned char out[MD5_DIGEST_LENGTH];
|
||||
|
@ -143,7 +143,6 @@ int copyToFlash(ssize_t fsize, char *clientChecksum, char *mess) {
|
||||
FAIL) {
|
||||
return FAIL;
|
||||
}
|
||||
LOG(logINFO, ("Checksum in Flash verified\n"));
|
||||
|
||||
if (waitForFPGAtoTouchFlash(mess) == FAIL) {
|
||||
return FAIL;
|
||||
@ -157,6 +156,7 @@ int getDrive(char *mess) {
|
||||
strcpy(flashDriveName, "/tmp/SLS_mtd3");
|
||||
return OK;
|
||||
#endif
|
||||
LOG(logDEBUG1, ("Finding flash drive...\n"));
|
||||
// getting the drive
|
||||
// root:/> cat /proc/mtd
|
||||
// dev: size erasesize name
|
||||
@ -183,6 +183,7 @@ int getDrive(char *mess) {
|
||||
return FAIL;
|
||||
}
|
||||
|
||||
memset(flashDriveName, 0, sizeof(flashDriveName));
|
||||
strcpy(flashDriveName, "/dev/");
|
||||
strcat(flashDriveName, pch);
|
||||
LOG(logINFO, ("\tFlash drive found: %s\n", flashDriveName));
|
||||
|
@ -8,10 +8,15 @@
|
||||
#include <unistd.h> // usleep
|
||||
|
||||
/* global variables */
|
||||
#define MTDSIZE 10
|
||||
char mtdvalue[MTDSIZE] = {0};
|
||||
|
||||
#define CMD_GET_FLASH "awk \'$5== \"Application\" {print $1}\' /proc/mtd"
|
||||
|
||||
#define FLASH_DRIVE_NAME_SIZE 16
|
||||
char flashDriveName[FLASH_DRIVE_NAME_SIZE] = {0};
|
||||
#define MICROCONTROLLER_FILE "/dev/ttyAL0"
|
||||
|
||||
extern int executeCommand(char *command, char *result, enum TLogLevel level);
|
||||
|
||||
void NotifyServerStartSuccess() {
|
||||
LOG(logINFOBLUE, ("Server started successfully\n"));
|
||||
char command[255];
|
||||
@ -28,7 +33,44 @@ void rebootControllerAndFPGA() {
|
||||
system(command);
|
||||
}
|
||||
|
||||
int findFlash(char *mess) {
|
||||
int eraseAndWriteToFlash(char *mess, char *checksum, char *fpgasrc,
|
||||
uint64_t fsize) {
|
||||
|
||||
if (verifyChecksumFromBuffer(mess, checksum, fpgasrc, fsize) == FAIL) {
|
||||
return FAIL;
|
||||
}
|
||||
|
||||
if (getDrive(mess) == FAIL) {
|
||||
return FAIL;
|
||||
}
|
||||
|
||||
FILE *flashfd = NULL;
|
||||
if (openFileForFlash(&flashfd, mess) == FAIL) {
|
||||
return FAIL;
|
||||
}
|
||||
|
||||
if (eraseFlash(mess) == FAIL) {
|
||||
fclose(flashfd);
|
||||
return FAIL;
|
||||
}
|
||||
|
||||
if (writeToFlash(fsize, flashfd, fpgasrc, mess) == FAIL) {
|
||||
return FAIL;
|
||||
}
|
||||
|
||||
if (verifyChecksumFromFlash(mess, checksum, flashDriveName, fsize) ==
|
||||
FAIL) {
|
||||
return FAIL;
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
int getDrive(char *mess) {
|
||||
#ifdef VIRTUAL
|
||||
strcpy(flashDriveName, "/tmp/SLS_mtd3");
|
||||
return OK;
|
||||
#endif
|
||||
LOG(logDEBUG1, ("Finding flash drive...\n"));
|
||||
// getting the drive
|
||||
// # cat /proc/mtd
|
||||
@ -39,88 +81,78 @@ int findFlash(char *mess) {
|
||||
// mtd3: 00800000 00010000 "qspi Linux Kernel with initramfs Backup"
|
||||
// mtd4: 02500000 00010000 "qspi ubi filesystem"
|
||||
// mtd5: 04000000 00010000 "qspi Complete Flash"
|
||||
char output[255];
|
||||
memset(output, 0, 255);
|
||||
FILE *fp = popen("awk \'$5== \"Application\" {print $1}\' /proc/mtd", "r");
|
||||
if (fp == NULL) {
|
||||
strcpy(mess, "popen returned NULL. Need that to get mtd drive.\n");
|
||||
|
||||
char cmd[MAX_STR_LENGTH] = {0};
|
||||
char retvals[MAX_STR_LENGTH] = {0};
|
||||
strcpy(cmd, CMD_GET_FLASH);
|
||||
if (executeCommand(cmd, retvals, logDEBUG1) == FAIL) {
|
||||
strcpy(mess, "Could not program fpga. (could not get flash drive: ");
|
||||
strncat(mess, retvals, sizeof(mess) - strlen(mess) - 1);
|
||||
strcat(mess, "\n");
|
||||
LOG(logERROR, (mess));
|
||||
return FAIL;
|
||||
}
|
||||
if (fgets(output, sizeof(output), fp) == NULL) {
|
||||
strcpy(mess, "fgets returned NULL. Need that to get mtd drive.\n");
|
||||
LOG(logERROR, (mess));
|
||||
return FAIL;
|
||||
}
|
||||
pclose(fp);
|
||||
memset(mtdvalue, 0, MTDSIZE);
|
||||
strcpy(mtdvalue, "/dev/");
|
||||
char *pch = strtok(output, ":");
|
||||
|
||||
char *pch = strtok(retvals, ":");
|
||||
if (pch == NULL) {
|
||||
strcpy(mess, "Could not get mtd value\n");
|
||||
strcpy(mess, "Could not get mtd drive to flash (strtok fail).\n");
|
||||
LOG(logERROR, (mess));
|
||||
return FAIL;
|
||||
}
|
||||
strcat(mtdvalue, pch);
|
||||
LOG(logINFO, ("\tFlash drive found: %s\n", mtdvalue));
|
||||
|
||||
memset(flashDriveName, 0, sizeof(flashDriveName));
|
||||
strcpy(flashDriveName, "/dev/");
|
||||
strcat(flashDriveName, pch);
|
||||
LOG(logINFO, ("\tFlash drive found: %s\n", flashDriveName));
|
||||
return OK;
|
||||
}
|
||||
|
||||
void eraseFlash() {
|
||||
LOG(logDEBUG1, ("Erasing Flash...\n"));
|
||||
char command[255];
|
||||
memset(command, 0, 255);
|
||||
sprintf(command, "flash_erase %s 0 0", mtdvalue);
|
||||
system(command);
|
||||
LOG(logINFO, ("\tFlash erased\n"));
|
||||
}
|
||||
|
||||
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
|
||||
FILE *filefp = fopen(mtdvalue, "w");
|
||||
if (filefp == NULL) {
|
||||
sprintf(mess, "Unable to open %s in write mode\n", mtdvalue);
|
||||
int openFileForFlash(FILE **flashfd, char *mess) {
|
||||
*flashfd = fopen(flashDriveName, "w");
|
||||
if (*flashfd == NULL) {
|
||||
sprintf(mess, "Unable to open flash drive %s in write mode\n",
|
||||
flashDriveName);
|
||||
LOG(logERROR, (mess));
|
||||
return FAIL;
|
||||
}
|
||||
LOG(logINFO, ("\tFlash ready for writing\n"));
|
||||
|
||||
// write to flash
|
||||
if (writeFPGAProgram(mess, fpgasrc, fsize, filefp) == FAIL) {
|
||||
fclose(filefp);
|
||||
return FAIL;
|
||||
}
|
||||
|
||||
fclose(filefp);
|
||||
return OK;
|
||||
}
|
||||
|
||||
int writeFPGAProgram(char *mess, char *fpgasrc, uint64_t fsize, FILE *filefp) {
|
||||
LOG(logDEBUG1, ("Writing to flash...\n"
|
||||
"\taddress of fpgasrc:%p\n"
|
||||
"\tfsize:%lu\n\tpointer:%p\n",
|
||||
(void *)fpgasrc, fsize, (void *)filefp));
|
||||
int eraseFlash(char *mess) {
|
||||
LOG(logINFO, ("\tErasing Flash...\n"));
|
||||
|
||||
uint64_t retval = fwrite((void *)fpgasrc, sizeof(char), fsize, filefp);
|
||||
if (retval != fsize) {
|
||||
sprintf(
|
||||
mess,
|
||||
"Could not write FPGA source to flash (size:%llu), write %llu\n",
|
||||
(long long unsigned int)fsize, (long long unsigned int)retval);
|
||||
#ifdef VIRTUAL
|
||||
return OK;
|
||||
#endif
|
||||
char cmd[MAX_STR_LENGTH] = {0};
|
||||
char retvals[MAX_STR_LENGTH] = {0};
|
||||
sprintf(cmd, "flash_erase %s", flashDriveName);
|
||||
if (FAIL == executeCommand(cmd, retvals, logDEBUG1)) {
|
||||
strcpy(mess, "Could not program fpga. (could not erase flash: ");
|
||||
strncat(mess, retvals, sizeof(mess) - strlen(mess) - 1);
|
||||
strcat(mess, "\n");
|
||||
LOG(logERROR, (mess));
|
||||
return FAIL;
|
||||
}
|
||||
LOG(logINFO, ("\tProgram written to flash\n"));
|
||||
|
||||
LOG(logINFO, ("\tFlash erased\n"));
|
||||
return OK;
|
||||
}
|
||||
|
||||
int writeToFlash(ssize_t fsize, FILE *flashfd, char *buffer, char *mess) {
|
||||
LOG(logINFO, ("\tWriting to Flash...\n"));
|
||||
|
||||
ssize_t bytesWritten = fwrite((void *)buffer, sizeof(char), fsize, flashfd);
|
||||
if (bytesWritten != fsize) {
|
||||
fclose(flashfd);
|
||||
sprintf(
|
||||
mess,
|
||||
"Could not program fpga. Incorrect bytes written to flash %lu [expected: %lu]\n",
|
||||
(long int)bytesWritten, (long int)fsize);
|
||||
LOG(logERROR, (mess));
|
||||
return FAIL;
|
||||
}
|
||||
LOG(logINFO, ("\tWritten to Flash\n"));
|
||||
return OK;
|
||||
}
|
||||
|
@ -3715,9 +3715,7 @@ int program_fpga(int file_des) {
|
||||
free(fpgasrc);
|
||||
return printSocketReadError();
|
||||
}
|
||||
#ifndef VIRTUAL
|
||||
ret = eraseAndWriteToFlash(mess, checksum, fpgasrc, filesize);
|
||||
#endif
|
||||
Server_SendResult(file_des, INT32, NULL, 0);
|
||||
free(fpgasrc);
|
||||
}
|
||||
|
@ -6,8 +6,8 @@
|
||||
|
||||
#define APICTB 0x210909
|
||||
#define APIGOTTHARD 0x210909
|
||||
#define APIMYTHEN3 0x210909
|
||||
#define APIMOENCH 0x210909
|
||||
#define APIEIGER 0x210909
|
||||
#define APIJUNGFRAU 0x210913
|
||||
#define APIMYTHEN3 0x210914
|
||||
#define APIGOTTHARD2 0x210914
|
||||
#define APIJUNGFRAU 0x210914
|
||||
|
Loading…
x
Reference in New Issue
Block a user