changes for nios similar to bfin

This commit is contained in:
maliakal_d 2021-09-14 14:41:49 +02:00
parent 0f8869153e
commit 78d8086e71
9 changed files with 174 additions and 77 deletions

View File

@ -10,8 +10,9 @@ void NotifyServerStartSuccess();
/** reset fpga and controller(only implemented for >= v1.1 boards) */ /** reset fpga and controller(only implemented for >= v1.1 boards) */
void rebootControllerAndFPGA(); void rebootControllerAndFPGA();
int findFlash(char *mess);
void eraseFlash(); int eraseAndWriteToFlash(char *mess, char *checksum, char *fpgasrc, uint64_t fsize);
int eraseAndWriteToFlash(char *mess, char *checksum, char *fpgasrc, int getDrive(char *mess);
uint64_t fsize); int openFileForFlash(FILE **flashfd, char *mess);
int writeFPGAProgram(char *mess, char *fpgasrc, uint64_t fsize, FILE *filefp); int eraseFlash(char *mess);
int writeToFlash(ssize_t fsize, FILE *flashfd, char *buffer, char *mess);

View File

@ -7,7 +7,9 @@
#include <string.h> #include <string.h>
#include <unistd.h> // readlink #include <unistd.h> // readlink
#ifdef JUNGFRAUD
#define FLASH_CONVERTED_NAME "/var/tmp/tests.rawbin" #define FLASH_CONVERTED_NAME "/var/tmp/tests.rawbin"
#endif
int ConvertToDifferentRange(int inputMin, int inputMax, int outputMin, int ConvertToDifferentRange(int inputMin, int inputMax, int outputMin,
int outputMax, int inputValue, int *outputValue) { int outputMax, int inputValue, int *outputValue) {
@ -337,6 +339,7 @@ int verifyChecksumFromFlash(char *mess, char *clientChecksum, char *fname,
return verifyChecksum(mess, clientChecksum, &c, "flash"); return verifyChecksum(mess, clientChecksum, &c, "flash");
} }
*/ */
#ifdef JUNGFRAUD
int verifyChecksumFromFlash(char *mess, char *clientChecksum, char *fname, int verifyChecksumFromFlash(char *mess, char *clientChecksum, char *fname,
ssize_t fsize) { ssize_t fsize) {
LOG(logINFO, ("\tVerifying FlashChecksum...\n")); LOG(logINFO, ("\tVerifying FlashChecksum...\n"));
@ -364,7 +367,6 @@ int verifyChecksumFromFlash(char *mess, char *clientChecksum, char *fname,
LOG(logERROR, (mess)); LOG(logERROR, (mess));
return FAIL; return FAIL;
} }
const int readUnitSize = 128;
int character = fgetc(fp); int character = fgetc(fp);
int oldProgress = 0; int oldProgress = 0;
ssize_t totalBytesRead = 1; 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)); LOG(logINFO, ("\tRead %lu bytes to calculate checksum\n", totalBytesRead));
fclose(fp); fclose(fp);
fclose(flashfp); 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) { int verifyChecksum(char *mess, char *clientChecksum, MD5_CTX *c, char *msg) {
unsigned char out[MD5_DIGEST_LENGTH]; unsigned char out[MD5_DIGEST_LENGTH];

View File

@ -143,7 +143,6 @@ int copyToFlash(ssize_t fsize, char *clientChecksum, char *mess) {
FAIL) { FAIL) {
return FAIL; return FAIL;
} }
LOG(logINFO, ("Checksum in Flash verified\n"));
if (waitForFPGAtoTouchFlash(mess) == FAIL) { if (waitForFPGAtoTouchFlash(mess) == FAIL) {
return FAIL; return FAIL;
@ -157,6 +156,7 @@ int getDrive(char *mess) {
strcpy(flashDriveName, "/tmp/SLS_mtd3"); strcpy(flashDriveName, "/tmp/SLS_mtd3");
return OK; return OK;
#endif #endif
LOG(logDEBUG1, ("Finding flash drive...\n"));
// getting the drive // getting the drive
// root:/> cat /proc/mtd // root:/> cat /proc/mtd
// dev: size erasesize name // dev: size erasesize name
@ -183,6 +183,7 @@ int getDrive(char *mess) {
return FAIL; return FAIL;
} }
memset(flashDriveName, 0, sizeof(flashDriveName));
strcpy(flashDriveName, "/dev/"); strcpy(flashDriveName, "/dev/");
strcat(flashDriveName, pch); strcat(flashDriveName, pch);
LOG(logINFO, ("\tFlash drive found: %s\n", flashDriveName)); LOG(logINFO, ("\tFlash drive found: %s\n", flashDriveName));

View File

@ -8,10 +8,15 @@
#include <unistd.h> // usleep #include <unistd.h> // usleep
/* global variables */ /* 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" #define MICROCONTROLLER_FILE "/dev/ttyAL0"
extern int executeCommand(char *command, char *result, enum TLogLevel level);
void NotifyServerStartSuccess() { void NotifyServerStartSuccess() {
LOG(logINFOBLUE, ("Server started successfully\n")); LOG(logINFOBLUE, ("Server started successfully\n"));
char command[255]; char command[255];
@ -28,7 +33,44 @@ void rebootControllerAndFPGA() {
system(command); 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")); LOG(logDEBUG1, ("Finding flash drive...\n"));
// getting the drive // getting the drive
// # cat /proc/mtd // # cat /proc/mtd
@ -39,88 +81,78 @@ int findFlash(char *mess) {
// mtd3: 00800000 00010000 "qspi Linux Kernel with initramfs Backup" // mtd3: 00800000 00010000 "qspi Linux Kernel with initramfs Backup"
// mtd4: 02500000 00010000 "qspi ubi filesystem" // mtd4: 02500000 00010000 "qspi ubi filesystem"
// mtd5: 04000000 00010000 "qspi Complete Flash" // mtd5: 04000000 00010000 "qspi Complete Flash"
char output[255];
memset(output, 0, 255); char cmd[MAX_STR_LENGTH] = {0};
FILE *fp = popen("awk \'$5== \"Application\" {print $1}\' /proc/mtd", "r"); char retvals[MAX_STR_LENGTH] = {0};
if (fp == NULL) { strcpy(cmd, CMD_GET_FLASH);
strcpy(mess, "popen returned NULL. Need that to get mtd drive.\n"); 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)); LOG(logERROR, (mess));
return FAIL; return FAIL;
} }
if (fgets(output, sizeof(output), fp) == NULL) {
strcpy(mess, "fgets returned NULL. Need that to get mtd drive.\n"); char *pch = strtok(retvals, ":");
LOG(logERROR, (mess));
return FAIL;
}
pclose(fp);
memset(mtdvalue, 0, MTDSIZE);
strcpy(mtdvalue, "/dev/");
char *pch = strtok(output, ":");
if (pch == NULL) { 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)); LOG(logERROR, (mess));
return FAIL; 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; return OK;
} }
void eraseFlash() { int openFileForFlash(FILE **flashfd, char *mess) {
LOG(logDEBUG1, ("Erasing Flash...\n")); *flashfd = fopen(flashDriveName, "w");
char command[255]; if (*flashfd == NULL) {
memset(command, 0, 255); sprintf(mess, "Unable to open flash drive %s in write mode\n",
sprintf(command, "flash_erase %s 0 0", mtdvalue); flashDriveName);
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);
LOG(logERROR, (mess)); LOG(logERROR, (mess));
return FAIL; return FAIL;
} }
LOG(logINFO, ("\tFlash ready for writing\n")); 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; return OK;
} }
int writeFPGAProgram(char *mess, char *fpgasrc, uint64_t fsize, FILE *filefp) { int eraseFlash(char *mess) {
LOG(logDEBUG1, ("Writing to flash...\n" LOG(logINFO, ("\tErasing Flash...\n"));
"\taddress of fpgasrc:%p\n"
"\tfsize:%lu\n\tpointer:%p\n",
(void *)fpgasrc, fsize, (void *)filefp));
uint64_t retval = fwrite((void *)fpgasrc, sizeof(char), fsize, filefp); #ifdef VIRTUAL
if (retval != fsize) { return OK;
sprintf( #endif
mess, char cmd[MAX_STR_LENGTH] = {0};
"Could not write FPGA source to flash (size:%llu), write %llu\n", char retvals[MAX_STR_LENGTH] = {0};
(long long unsigned int)fsize, (long long unsigned int)retval); 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)); LOG(logERROR, (mess));
return FAIL; 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; return OK;
} }

View File

@ -3715,9 +3715,7 @@ int program_fpga(int file_des) {
free(fpgasrc); free(fpgasrc);
return printSocketReadError(); return printSocketReadError();
} }
#ifndef VIRTUAL
ret = eraseAndWriteToFlash(mess, checksum, fpgasrc, filesize); ret = eraseAndWriteToFlash(mess, checksum, fpgasrc, filesize);
#endif
Server_SendResult(file_des, INT32, NULL, 0); Server_SendResult(file_des, INT32, NULL, 0);
free(fpgasrc); free(fpgasrc);
} }

View File

@ -6,8 +6,8 @@
#define APICTB 0x210909 #define APICTB 0x210909
#define APIGOTTHARD 0x210909 #define APIGOTTHARD 0x210909
#define APIMYTHEN3 0x210909
#define APIMOENCH 0x210909 #define APIMOENCH 0x210909
#define APIEIGER 0x210909 #define APIEIGER 0x210909
#define APIJUNGFRAU 0x210913 #define APIMYTHEN3 0x210914
#define APIGOTTHARD2 0x210914 #define APIGOTTHARD2 0x210914
#define APIJUNGFRAU 0x210914