programs, but flashes checksum wrong

This commit is contained in:
2021-09-09 17:52:36 +02:00
parent aea664f40c
commit a9f82985bc
10 changed files with 115 additions and 35 deletions

View File

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

View File

@ -2,6 +2,7 @@
#include <stdint.h>
#include <stdio.h>
#include <sys/types.h>
#define TEMP_PROG_FILE_NAME "/var/tmp/tmp.pof"
@ -17,13 +18,13 @@ void resetFPGA();
* open file to copy
*/
int preparetoCopyFPGAProgram(FILE **fd, uint64_t fsize, char *mess);
int copyToFlash(char *clientChecksum, char *mess);
int copyToFlash(ssize_t fsize, char *clientChecksum, char *mess);
int getDrive(char *mess);
/** Notify fpga not to touch flash, open src and flash drive to write */
int openFileForFlash(FILE **flashfd, FILE **srcfd, char *mess);
int eraseFlash(char *mess);
/* write from tmp file to flash */
int writeToFlash(FILE *flashfd, FILE *srcfd, char *mess);
int writeToFlash(ssize_t fsize, FILE *flashfd, FILE *srcfd, char *mess);
/** Notify fpga to pick up firmware from flash and wait for status confirmation
*/
int waitForFPGAtoTouchFlash(char *mess);

View File

@ -181,6 +181,7 @@ int setModuleIdInFile(char *mess, int arg, char *fileName) {
int verifyChecksumFromBuffer(char *mess, char *clientChecksum, char *buffer,
ssize_t bytes) {
LOG(logINFO, ("\tVerifying Checksum...\n"));
MD5_CTX c;
if (!MD5_Init(&c)) {
strcpy(mess, "Unable to calculate checksum (MD5_Init)\n");
@ -195,7 +196,9 @@ 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");
if (fp == NULL) {
sprintf(mess, "Unable to open %s in read mode to get checksum\n",
@ -211,8 +214,10 @@ int verifyChecksumFromFile(char *mess, char *clientChecksum, char *fname) {
LOG(logERROR, (mess));
return FAIL;
}
char buf[512];
ssize_t bytes = fread(buf, 1, 512, fp);
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);
@ -220,8 +225,26 @@ int verifyChecksumFromFile(char *mess, char *clientChecksum, char *fname) {
LOG(logERROR, (mess));
return FAIL;
}
bytes = fread(buf, 1, 512, fp);
// 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);
strcpy(mess, "Unable to calculate checksum (MD5_Update)\n");
LOG(logERROR, (mess));
return FAIL;
}
}*/
LOG(logINFO, ("\tRead %lu bytes to calculate checksum\n", totalBytesRead));
fclose(fp);
return verifyChecksum(mess, clientChecksum, &c);
}
@ -255,6 +278,6 @@ int verifyChecksum(char *mess, char *clientChecksum, MD5_CTX *c) {
LOG(logERROR, (mess));
return FAIL;
}
LOG(logINFO, ("\tChecksum verified from copied program\n"));
LOG(logINFO, ("\tChecksum verified\n"));
return OK;
}

View File

@ -9,10 +9,12 @@
#include <sys/sysinfo.h>
/* global variables */
// clang-format off
#define MAX_TIME_FPGA_TOUCH_FLASH_US (10 * 1000 * 1000) // 10s
#define CMD_GET_FLASH \
"awk \'$4== \"\\\"bitfile(spi)\\\"\" {print $1}\' /proc/mtd"
#define CMD_GET_FLASH "awk \'$4== \"\\\"bitfile(spi)\\\"\" {print $1}\' /proc/mtd"
#define CMD_FPGA_PICKED_STATUS "cat /sys/class/gpio/gpio7/value"
#define FLASH_BUFFER_MEMORY_SIZE (128 * 1024) // 500 KB
// clang-format on
#define FLASH_DRIVE_NAME_SIZE 16
char flashDriveName[FLASH_DRIVE_NAME_SIZE] = {0};
@ -107,7 +109,7 @@ int preparetoCopyFPGAProgram(FILE **fd, uint64_t fsize, char *mess) {
return OK;
}
int copyToFlash(char *clientChecksum, char *mess) {
int copyToFlash(ssize_t fsize, char *clientChecksum, char *mess) {
if (getDrive(mess) == FAIL) {
return FAIL;
@ -115,19 +117,21 @@ int copyToFlash(char *clientChecksum, char *mess) {
FILE *flashfd = NULL;
FILE *srcfd = NULL;
if (openFileForFlash(&flashfd, &srcfd, mess) == FAIL) {
if (openFileForFlash(&flashfd, &srcfd, mess) == FAIL) {
return FAIL;
}
if (eraseFlash(mess) == FAIL) {
fclose(flashfd);
fclose(srcfd);
return FAIL;
}
if (writeToFlash(flashfd, srcfd, mess) == FAIL) {
if (writeToFlash(fsize, flashfd, srcfd, mess) == FAIL) {
return FAIL;
}
if (verifyChecksumFromFile(mess, clientChecksum, flashDriveName) == FAIL) {
if (verifyChecksumFromFile(mess, clientChecksum, flashDriveName, fsize) == FAIL) {
return FAIL;
}
LOG(logINFO, ("Checksum in Flash verified\n"));
@ -155,7 +159,7 @@ int getDrive(char *mess) {
char cmd[MAX_STR_LENGTH] = {0};
char retvals[MAX_STR_LENGTH] = {0};
strcpy(cmd, CMD_GET_FLASH);
if (executeCommand(cmd, retvals, logINFO) == FAIL) {
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");
@ -205,7 +209,7 @@ int openFileForFlash(FILE **flashfd, FILE **srcfd, char *mess) {
}
int eraseFlash(char *mess) {
LOG(logDEBUG1, ("Erasing Flash\n"));
LOG(logINFO, ("\tErasing Flash...\n"));
#ifdef VIRTUAL
return OK;
@ -225,10 +229,11 @@ int eraseFlash(char *mess) {
return OK;
}
int writeToFlash(FILE *flashfd, FILE *srcfd, char *mess) {
int writeToFlash(ssize_t fsize, FILE *flashfd, FILE *srcfd, char *mess) {
LOG(logDEBUG1, ("writing to flash\n"));
char* buffer = malloc(MAX_FPGAPROGRAMSIZE);
char* buffer = malloc(FLASH_BUFFER_MEMORY_SIZE);
if (buffer == NULL) {
fclose(flashfd);
fclose(srcfd);
@ -237,13 +242,18 @@ int writeToFlash(FILE *flashfd, FILE *srcfd, char *mess) {
LOG(logERROR, (mess));
return FAIL;
}
LOG(logINFO, ("\tWriting to Flash...\n"));
int oldProgress = 0;
ssize_t totalBytes = 0;
ssize_t bytes = fread(buffer, sizeof(char), MAX_FPGAPROGRAMSIZE, srcfd);
ssize_t bytes = fread((void*)buffer, sizeof(char), FLASH_BUFFER_MEMORY_SIZE, srcfd);
while (bytes > 0) {
ssize_t bytesWritten =
fwrite((void *)buffer, sizeof(char), bytes, flashfd);
fwrite((void*)buffer, sizeof(char), bytes, flashfd);
totalBytes += bytesWritten;
if (bytesWritten != bytes) {
free(buffer);
fclose(flashfd);
@ -256,14 +266,32 @@ int writeToFlash(FILE *flashfd, FILE *srcfd, char *mess) {
LOG(logERROR, (mess));
return FAIL;
}
bytes = fread(buffer, sizeof(char), bytes, srcfd);
printf(".");
// print progress
if (fsize > 0) {
int progress = (int)(((double)(totalBytes) / fsize) * 100);
if (oldProgress != progress) {
printf("%d%%\r", progress);
fflush(stdout);
oldProgress = progress;
}
} else printf(".");
bytes = fread((void*)buffer, sizeof(char), FLASH_BUFFER_MEMORY_SIZE, srcfd);
}
if (fsize <= 0) {
printf("\n");
}
printf("\n");
free(buffer);
fclose(flashfd);
fclose(srcfd);
LOG(logINFO, ("\tWrote %ld bytes to flash\n", totalBytes));
if (totalBytes != fsize) {
sprintf(mess, "Could not program fpga. Incorrect bytes written to flash %lu [expected: %lu]\n", totalBytes, fsize);
LOG(logERROR, (mess));
return FAIL;
}
return OK;
}

View File

@ -14,6 +14,7 @@
#include <pthread.h>
#include <string.h>
#include <unistd.h>
#include <sys/sysinfo.h>
// defined in the detector specific Makefile
#ifdef GOTTHARDD
@ -3735,7 +3736,9 @@ int program_fpga(int file_des) {
src = malloc(MAX_FPGAPROGRAMSIZE);
if (src == NULL) {
fclose(fd);
strcpy(mess, "Could not allocate memory to get fpga program\n");
struct sysinfo info;
sysinfo(&info);
sprintf(mess, "Could not allocate memory to get fpga program. Free space: %d MB\n", (int)(info.freeram/ (1024 * 1024)));
LOG(logERROR, (mess));
ret = FAIL;
}
@ -3792,7 +3795,7 @@ 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) {
@ -3801,7 +3804,7 @@ int program_fpga(int file_des) {
}
// copy to flash
ret = copyToFlash(checksum, mess);
ret = copyToFlash(totalsize, checksum, mess);
Server_SendResult(file_des, INT32, NULL, 0);
if (ret == FAIL) {
LOG(logERROR, ("Program FPGA FAIL!\n"));