/* Bytes: */
#define PAGE_SIZE 256
-#define NUM_PAGES 16384
+static unsigned int NUM_PAGES = 0;
+
#define BLOCK_SIZE (64 * 1024)
-#define NUM_BLOCKS 64
+static unsigned int NUM_BLOCKS = 0;
#define PAGE_BUFFER_SIZE (PAGE_SIZE * NUM_ENDPOINTS)
-static const uint32_t manId[1] = {
- 0x0000471f
-};
-
static FILE *logFile = NULL;
-static char logFileName[256] = "trbflash.log";
+static const char logFileName[256] = "trbflash.log";
static const char bussy[4][5] = {
"|\b",
"\\\b"
};
-#define NUM_MAN_IDS 1
+static const unsigned int timeout = 10000;
-/* ------ MAIN ---------------------------------------------------------- */
+typedef enum {
+ FLASH_INVALID = 0,
+ FLASH_ADCM1 = 1,
+ FLASH_ADCM2 = 2,
+ FLASH_MDC = 3
+} FlashType;
+
+static const char FlashTypeStr[][16] = {"INVALID", "ADCM1", "ADCM2", "MDC"};
+
+static FlashType flashType = FLASH_INVALID;
+
+/* ------ Local Functions ----------------------------------------------- */
static void atexit0()
{
free(imageBuffer);
}
-static const unsigned int timeout = 10000;
-
static int readSetupRegister(uint16_t trb_address,
uint8_t value[NUM_ENDPOINTS])
{
return -1;
}
- return 1;
+ return 0;
}
static int readCtrlRegister(uint16_t trb_address,
ctr++;
} while (trb_term.status_channel != 0);
- return 1;
+ return 0;
}
static int sendCommand(uint16_t trb_address, uint32_t cmd, uint8_t max)
return ret;
}
+static int initTransfer(uint16_t trb_address)
+{
+ /* Find Endpoint(s) ManId and allocate needed memory */
+ uint32_t trbcmd;
+ int status;
+ unsigned int counter = 0;
+ int success = 0;
+ int i;
+
+ /* Read ManId */
+ trbcmd = 0x9f << 24;
+ if (sendCommand(trb_address, trbcmd, 2) == -1) {
+ return -1;
+ }
+
+ if ((status =
+ trb_register_read(trb_address, BlockRam, trbBuffer,
+ TRB_BUFFER_SIZE)) == -1) {
+ fprintf(logFile, "Error > initTransfer: TRBNet %s\n",
+ trb_strerror(trb_errno));
+ return -1;
+ }
+
+ if (status <= 0) {
+ return -1;
+ }
+
+ flashType = FLASH_INVALID;
+ success = 0;
+ for (i = 0; i < status; i += 2) {
+ switch ((trbBuffer[i + 1] & 0x00ffffff)) {
+ case 0x01461f:
+ /* ADCM */
+ if ((flashType != FLASH_INVALID) && (flashType != FLASH_ADCM1)) {
+ success = -2;
+ }
+ NUM_PAGES = 8192;
+ flashType = FLASH_ADCM1;
+ break;
+
+ case 0x00471f:
+ /* ADCM */
+ if ((flashType != FLASH_INVALID) && (flashType != FLASH_ADCM2)) {
+ success = -2;
+ }
+ NUM_PAGES = 16384;
+ flashType = FLASH_ADCM2;
+ break;
+
+ case 0x1520c2:
+ /* MDC */
+ if ((flashType != FLASH_INVALID) && (flashType != FLASH_MDC)) {
+ success = -2;
+ }
+ NUM_PAGES = 8192;
+ flashType = FLASH_MDC;
+ break;
+
+ default:
+ flashType = FLASH_INVALID;
+ success = -1;
+ break;
+ }
+
+ if (success == -2) {
+ fprintf(logFile, "Error > initTransfer: "
+ "Incompatible ManId(s) 0x%04x on EndPoint 0x%04x\n",
+ trbBuffer[i + 1], trbBuffer[i] & 0xffff);
+ return -1;
+ }
+
+ if (flashType == FLASH_INVALID) {
+ fprintf(logFile, "Error > initTransfer: "
+ "Unsupported ManId(s) 0x%04x on EndPoint 0x%04x\n",
+ trbBuffer[i + 1], trbBuffer[i] & 0xffff);
+ return -1;
+ }
+
+ counter++;
+ }
+
+ NUM_BLOCKS = (NUM_PAGES * PAGE_SIZE) / BLOCK_SIZE;
+
+ /* Buffer holding the entire rom-image */
+ imageBuffer =
+ (uint8_t*)malloc(sizeof(uint8_t) * (PAGE_SIZE * (NUM_PAGES + 2)));
+ if (imageBuffer == NULL) {
+ abort();
+ }
+
+ fprintf(stderr, "Found %d Endpoint(s) of type %s\n",
+ counter, FlashTypeStr[flashType]);
+
+ return 0;
+}
+
static int readPage(uint16_t trb_address, uint32_t pageNumber,
uint32_t numBytes)
{
return 0;
}
-static int checkManId(uint16_t trb_address)
-{
- uint32_t trbcmd;
- int status;
- int i;
-
-
- /* Read ManId */
- trbcmd = 0x9f << 24;
- if (sendCommand(trb_address, trbcmd, 2) == -1) {
- return -1;
- }
-
- if ((status =
- trb_register_read(trb_address, BlockRam, trbBuffer,
- TRB_BUFFER_SIZE)) == -1) {
- fprintf(logFile, "Error > checkManId: TRBNet %s\n",
- trb_strerror(trb_errno));
- return -1;
- }
-
- if (status <= 0) {
- return -1;
- }
-
- for (i = 0; i < status; i += 2) {
- unsigned int id;
- int accepted = -1;
- for (id = 0; id < NUM_MAN_IDS; id++) {
- if ((trbBuffer[i + 1] & 0x00ffffff) == manId[id]) {
- accepted = 0;
- break;
- }
- }
- if (accepted == -1) {
- fprintf(logFile, "Error > checkManId: "
- "Invalid ManId 0x%04x on EndPoint 0x%04x\n",
- trbBuffer[i + 1], trbBuffer[i] & 0xffff);
- return -1;
- }
- }
-
- return 0;
-}
-
static int programImageBuffer(uint16_t trb_address, unsigned int size,
int verifyOnly)
{
fprintf(stderr, "Block: 0 1 2 3 4 5 6 7 8 9 A B C D E F");
errorCtr = 0;
+
+ if ((verifyOnly == 0) && (flashType == FLASH_MDC)) {
+ /* Enable writing */
+ if (sendCommand(trb_address, 0x06 << 24, 0) == -1) {
+ fprintf(stderr, "Error > program: write enable, aborting\n");
+ return -1;
+ }
+
+ /* Unprotect all Sectors */
+ if (trb_register_write(trb_address, BlockRam, 0) == -1) {
+ fprintf(logFile, "Error > program: TRBNet %s\n",
+ trb_strerror(trb_errno));
+ return -1;
+ }
+ if (sendCommand(trb_address, 0x01 << 24, 0) == -1) {
+ fprintf(stderr, "Error > program: unprotect all sectors, aborting\n");
+ return -1;
+ }
+ }
+
for (block = 0; (block < NUM_BLOCKS) && (bytesWritten > 0); block++) {
int error = 0;
if (block % 16 == 0) {
fprintf(stderr, ".\b");
if (verifyOnly == 0) {
- /* Enable writing */
- if (sendCommand(trb_address, 0x06 << 24, 0) == -1) {
- fprintf(stderr, "Error > program: write enable, aborting\n");
- return -1;
- }
-
- /* Unprotect sector */
- if (sendCommand(trb_address, 0x39 << 24 | (BLOCK_SIZE * block), 3)
- == -1) {
- fprintf(stderr, "Error > program: unprotect sector #%d, aborting\n",
- block);
- return -1;
+ if ((flashType == FLASH_ADCM1) || (flashType == FLASH_ADCM2)) {
+ /* Enable writing */
+ if (sendCommand(trb_address, 0x06 << 24, 0) == -1) {
+ fprintf(stderr, "Error > program: write enable, aborting\n");
+ return -1;
+ }
+
+ /* Unprotect sector */
+ if (sendCommand(trb_address, 0x39 << 24 | (BLOCK_SIZE * block), 3)
+ == -1) {
+ fprintf(stderr, "Error > program: unprotect sector #%d, aborting\n",
+ block);
+ return -1;
+ }
}
-
+
/* Enable writing */
if (sendCommand(trb_address, 0x06 << 24, 0) == -1) {
fprintf(stderr, "Error > program: write enable, aborting\n");
}
}
- /* Enable writing */
- if (sendCommand(trb_address, 0x06 << 24, 0) == -1) {
- fprintf(stderr, "Error > program: write enable, aborting\n");
- return -1;
- }
-
- /* Protect sector */
- if (sendCommand(trb_address, 0x36 << 24 | (BLOCK_SIZE * block), 3)
- == -1) {
- fprintf(stderr, "Error > program: unprotect sector #%d, aborting\n",
- block);
- return -1;
+ if ((flashType == FLASH_ADCM1) || (flashType == FLASH_ADCM2)) {
+ /* Enable writing */
+ if (sendCommand(trb_address, 0x06 << 24, 0) == -1) {
+ fprintf(stderr, "Error > program: write enable, aborting\n");
+ return -1;
+ }
+
+ /* Protect sector */
+ if (sendCommand(trb_address, 0x36 << 24 | (BLOCK_SIZE * block), 3)
+ == -1) {
+ fprintf(stderr, "Error > program: unprotect sector #%d, aborting\n",
+ block);
+ return -1;
+ }
}
}
fprintf(stderr, "X ");
}
}
+
+ if ((verifyOnly == 0) && (flashType == FLASH_MDC)) {
+ /* Enable writing */
+ if (sendCommand(trb_address, 0x06 << 24, 0) == -1) {
+ fprintf(stderr, "Error > program: write enable, aborting\n");
+ return -1;
+ }
+
+ /* Protect all Sectors */
+ if (trb_register_write(trb_address, BlockRam, 0x3c) == -1) {
+ fprintf(logFile, "Error > program: TRBNet %s\n",
+ trb_strerror(trb_errno));
+ return -1;
+ }
+ if (sendCommand(trb_address, 0x01 << 24, 0) == -1) {
+ fprintf(stderr, "Error > program: unprotect all sectors, aborting\n");
+ return -1;
+ }
+ }
+
if (errorCtr == 0) {
fprintf(stderr, "\n\nSuccess\n\n");
} else {
{
FILE *imageFile = NULL;
int imageSize;
+ unsigned int i;
+
+ /* Cleanup Buffer */
+ for (i = 0; i < (PAGE_SIZE * (NUM_PAGES + 2)); i++) imageBuffer[i] = 0;
/* Read in image */
imageFile = fopen(imageFileName, "r");
static int prepareImageBuffer()
{
+ char strId[32] = "";
+ int found;
+ unsigned int end;
unsigned int i;
+
+ /* Verify imageFile Id */
+ switch (flashType) {
+ case FLASH_ADCM1:
+ strncpy(strId, "edif_adcmv1", 32);
+ break;
+
+ case FLASH_ADCM2:
+ strncpy(strId, "edif_adcmv2", 32);
+ break;
+
+ case FLASH_MDC:
+ strncpy(strId, "mdc_oepb", 32);
+ break;
+
+ default:
+ abort();
+ break;
+ }
/* Verify imageFile Id */
- if (strncmp(imageBuffer + 105, "edif_adcmv2", 11) != 0) {
+ found = 0;
+ for (i = 0; i < 2 * PAGE_SIZE; i++) {
+ if (memcmp(imageBuffer + i, strId, strlen(strId)) == 0) {
+ found = 1;
+ break;
+ }
+ }
+
+ if (found == 0) {
fprintf(logFile, "Error > prepareImageBuffer: invalid Firmware-Id\n");
return -1;
}
-
- /* Overwrite the first 301 byte with 0xff */
- for (i = 0; i < 0x12a; i++) {
+
+ /* Overwrite Header with 0xff */
+ end = 0;
+ for (i = 0; i < 2 * PAGE_SIZE; i++) {
+ if (strncmp(imageBuffer + i, "Bitstream CRC: 0x", 17) == 0) {
+ end = i + 22;
+ break;
+ }
+ }
+ if (end == 0) {
+ fprintf(logFile, "Error > prepareImageBuffer: invalid Firmware, "
+ "CRC not found\n");
+ }
+ for (i = 0; i < end; i++) {
imageBuffer[i] = 0xff;
}
return 0;
}
+/* ------ MAIN ---------------------------------------------------------- */
+
void usage(const char *progName)
{
- printf("Usage: %s [-h] [-d level] <COMMAND>\n",
+ printf("Usage: %s [-h] <COMMAND>\n",
progName);
printf("Options:\n");
printf(" -h give this help\n");
printf("\nCommands:\n");
- printf(" program <trbaddress> <bit-file> -> \n");
- printf(" verify <trbaddress> <bit-file> -> \n");
- printf(" backup <trbaddress> <raw-file> -> \n");
- printf(" restore <trbaddress> <raw-file> -> \n");
- printf(" dumppage <trbaddress> pagenumber -> \n");
+ printf(" program <trbaddress> <bit-file> -> "
+ "program bit-file to flash-memory\n");
+ printf(" verify <trbaddress> <bit-file> -> "
+ "compare bit-file to flash-memory\n");
+ printf(" backup <trbaddress> <raw-file> -> "
+ "write entire flash-memory to raw-file\n");
+ printf(" restore <trbaddress> <raw-file> -> "
+ "write backuped raw-file to flash-memory\n");
+ printf(" dumppage <trbaddress> pagenumber -> "
+ "dump one flash-memory page to stdout\n");
}
int main(int argc, char ** argv)
trb_debug = strtoul(optarg, NULL, 0);
break;
default:
+ usage(basename(argv[0]));
+ exit(EXIT_FAILURE);
break;
}
}
abort();
}
- /* Buffer holding the entire rom-image */
- imageBuffer =
- (uint8_t*)malloc(sizeof(uint8_t) * (PAGE_SIZE * NUM_PAGES + 2));
- if (imageBuffer == NULL) {
- abort();
- }
-
/* Execute command */
if (strcmp(argv[optind], "program") == 0) {
imageFileName = argv[optind + 2];
/* Check for correct ManId */
- if (checkManId(trb_address) == -1) {
+ if (initTransfer(trb_address) == -1) {
fprintf(stderr, "Invalid ManId(s), aborting\n");
exit(EXIT_FAILURE);
}
imageFileName = argv[optind + 2];
/* Check for correct ManId */
- if (checkManId(trb_address) == -1) {
+ if (initTransfer(trb_address) == -1) {
fprintf(stderr, "Invalid ManId(s), aborting\n");
exit(EXIT_FAILURE);
}
"Broadcast addresses are not supported by this command\n");
exit(EXIT_FAILURE);
}
-
-
- if (checkManId(trb_address) == -1) {
+
+ if (initTransfer(trb_address) == -1) {
exit(EXIT_FAILURE);
}
imageFileName = argv[optind + 2];
/* Check for correct ManId */
- if (checkManId(trb_address) == -1) {
+ if (initTransfer(trb_address) == -1) {
fprintf(stderr, "Invalid ManId(s), aborting\n");
exit(EXIT_FAILURE);
}
trb_address = strtoul(argv[optind + 1], NULL, 0);
pageNumber = strtoul(argv[optind + 2], NULL, 0);
+ /* Check for correct ManId */
+ if (initTransfer(trb_address) == -1) {
+ fprintf(stderr, "Invalid ManId(s), aborting\n");
+ exit(EXIT_FAILURE);
+ }
+
if ((status = readPage(trb_address, pageNumber, 256)) == -1) {
fprintf(stderr, "Error reading Page, aborting\n");
exit(EXIT_FAILURE);
char text[32] = "";
char cc[2] = " \0";
int c;
- fprintf(stdout, "EndPoint: 0x%04x Page #%d\n",
+ fprintf(stdout, "\nEndPoint: 0x%04x Page #%d\n",
pageBufferAddress[i],pageNumber);
for (c = 0; c < 256; c++) {
if ((c % 16) == 0) {