server side fixed

This commit is contained in:
2021-11-08 17:24:51 +01:00
parent 7b4f8c118b
commit 64a25a242b
22 changed files with 484 additions and 322 deletions

View File

@ -11,10 +11,19 @@
/* global variables */
#define CMD_GET_FLASH "awk \'$5== \"Application\" {print $1}\' /proc/mtd"
#define CMD_GET_FPGA_FLASH_DRIVE \
"awk \'$5== \"Application\" {print $1}\' /proc/mtd"
#define CMD_GET_KERNEL_FLASH_DRIVE "awk \'$5== \"Linux\" {print $1}\' /proc/mtd"
#define FLASH_DRIVE_NAME_SIZE 16
#ifdef VIRTUAL
char flashDriveName[FLASH_DRIVE_NAME_SIZE] = "/tmp/SLS_mtd3";
#else
char flashDriveName[FLASH_DRIVE_NAME_SIZE] = {0};
#endif
char messageType[SHORT_STR_LENGTH] = {0};
#define MICROCONTROLLER_FILE "/dev/ttyAL0"
extern int executeCommand(char *command, char *result, enum TLogLevel level);
@ -35,15 +44,19 @@ void rebootControllerAndFPGA() {
system(command);
}
int eraseAndWriteToFlash(char *mess, char *checksum, char *fpgasrc,
int eraseAndWriteToFlash(char *mess, enum PROGRAM_INDEX index,
char *functionType, char *checksum, char *fpgasrc,
uint64_t fsize) {
if (getDrive(mess) == FAIL) {
memset(messageType, 0, sizeof(messageType));
strcpy(messageType, functionType);
if (getDrive(mess, index) == FAIL) {
return FAIL;
}
FILE *flashfd = NULL;
if (openFileForFlash(&flashfd, mess) == FAIL) {
if (openFileForFlash(mess, &flashfd) == FAIL) {
return FAIL;
}
@ -52,21 +65,33 @@ int eraseAndWriteToFlash(char *mess, char *checksum, char *fpgasrc,
return FAIL;
}
if (writeToFlash(fsize, flashfd, fpgasrc, mess) == FAIL) {
if (writeToFlash(mess, fsize, flashfd, fpgasrc) == FAIL) {
return FAIL;
}
/* ignoring this until a consistent way to read from nios flash
if (verifyChecksumFromFlash(mess, checksum, flashDriveName, fsize) ==
FAIL) {
return FAIL;
/* remove condition when flash fpga fixed */
if (index == PROGRAM_KERNEL) {
if (verifyChecksumFromFlash(mess, messageType, checksum, flashDriveName,
fsize) == FAIL) {
return FAIL;
}
}
*/
if (index == PROGRAM_KERNEL) {
char retvals[MAX_STR_LENGTH] = {0};
if (executeCommand("sync", retvals, logDEBUG1) == FAIL) {
snprintf(mess, MAX_STR_LENGTH,
"Could not update %s. (could not sync)\n", messageType);
// LOG(logERROR, (mess)); already printed in executecommand
return FAIL;
}
}
return OK;
}
int getDrive(char *mess) {
int getDrive(char *mess, enum PROGRAM_INDEX index) {
#ifdef VIRTUAL
strcpy(flashDriveName, "/tmp/SLS_mtd3");
return OK;
#endif
LOG(logDEBUG1, ("Finding flash drive...\n"));
@ -82,10 +107,15 @@ int getDrive(char *mess) {
char cmd[MAX_STR_LENGTH] = {0};
char retvals[MAX_STR_LENGTH] = {0};
strcpy(cmd, CMD_GET_FLASH);
if (index == PROGRAM_FPGA) {
strcpy(cmd, CMD_GET_FPGA_FLASH_DRIVE);
} else {
strcpy(cmd, CMD_GET_KERNEL_FLASH_DRIVE);
}
if (executeCommand(cmd, retvals, logDEBUG1) == FAIL) {
snprintf(mess, MAX_STR_LENGTH,
"Could not program fpga. (could not get flash drive: %s)\n",
"Could not %s. (could not get flash drive: %s)\n", messageType,
retvals);
// LOG(logERROR, (mess)); already printed in executecommand
return FAIL;
@ -93,7 +123,10 @@ int getDrive(char *mess) {
char *pch = strtok(retvals, ":");
if (pch == NULL) {
strcpy(mess, "Could not get mtd drive to flash (strtok fail).\n");
sprintf(
mess,
"Could not %s. Could not get mtd drive to flash (strtok fail).\n",
messageType);
LOG(logERROR, (mess));
return FAIL;
}
@ -105,11 +138,12 @@ int getDrive(char *mess) {
return OK;
}
int openFileForFlash(FILE **flashfd, char *mess) {
int openFileForFlash(char *mess, FILE **flashfd) {
*flashfd = fopen(flashDriveName, "w");
if (*flashfd == NULL) {
sprintf(mess, "Unable to open flash drive %s in write mode\n",
flashDriveName);
sprintf(mess,
"Could not %s. Unable to open flash drive %s in write mode\n",
messageType, flashDriveName);
LOG(logERROR, (mess));
return FAIL;
}
@ -129,14 +163,14 @@ int eraseFlash(char *mess) {
char *format = "flash_erase %s 0 0";
if (snprintf(cmd, MAX_STR_LENGTH, format, flashDriveName) >=
MAX_STR_LENGTH) {
strcpy(mess,
"Could not program fpga. Command to erase flash is too long\n");
sprintf(mess, "Could not %s. Command to erase flash is too long\n",
messageType);
LOG(logERROR, (mess));
return FAIL;
}
if (executeCommand(cmd, retvals, logDEBUG1) == FAIL) {
snprintf(mess, MAX_STR_LENGTH,
"Could not program fpga. (could not erase flash: %s)\n",
"Could not %s. (could not erase flash: %s)\n", messageType,
retvals);
// LOG(logERROR, (mess)); already printed in executecommand
return FAIL;
@ -146,16 +180,16 @@ int eraseFlash(char *mess) {
return OK;
}
int writeToFlash(ssize_t fsize, FILE *flashfd, char *buffer, char *mess) {
int writeToFlash(char *mess, ssize_t fsize, FILE *flashfd, char *buffer) {
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 "
"Could not %s. Incorrect bytes written to flash %lu "
"[expected: %lu]\n",
(long int)bytesWritten, (long int)fsize);
messageType, (long int)bytesWritten, (long int)fsize);
LOG(logERROR, (mess));
return FAIL;
}