From 18ebacce8739c1c1272f3379aee8b562ae80357a Mon Sep 17 00:00:00 2001 From: hadaq Date: Fri, 4 Sep 2009 21:36:24 +0000 Subject: [PATCH] Ask before flashing.. . --- trbrich/trbflash.c | 107 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 96 insertions(+), 11 deletions(-) diff --git a/trbrich/trbflash.c b/trbrich/trbflash.c index 0667e11..f8042dc 100644 --- a/trbrich/trbflash.c +++ b/trbrich/trbflash.c @@ -16,6 +16,8 @@ static const uint16_t CtrlReg = 0xd000; static const uint16_t SetupReg = 0xd001; static const uint16_t BlockRam = 0xd100; +static const uint16_t MDCFlashRomSelect = 0xd200; + static uint32_t* trbBuffer = NULL; static uint8_t** pageBuffer = NULL; static uint16_t* pageBufferAddress = NULL; @@ -61,7 +63,9 @@ static const char FlashTypeStr[][16] = {"INVALID", "ADCM1", "ADCM2", "MDC"}; static FlashType flashType = FLASH_INVALID; -static const char trbflash_version[] = "$Revision: 2.02 $"; +static const char trbflash_version[] = "$Revision: 2.03 $"; + +static uint32_t mdcFlashSelect = 1; /* ------ Local Functions ----------------------------------------------- */ @@ -541,6 +545,30 @@ static int programImageBuffer(uint16_t trb_address, unsigned int size, int bytesWritten = size; int tmp; + if (verifyOnly == 0) { + /* Be nice and ask before start flashing the roms */ + char c; + fprintf(stdout, "You decided to reprogram the FlashRom(s), " + "are you sure [N,y]: "); + c = getc(stdin); + if (!((c == 'Y') || (c == 'y'))) { + fprintf(stderr, "\nAborting on user request\n"); + return -1; + } + + if (flashType == FLASH_MDC) { + if (mdcFlashSelect == 0) { + fprintf(stdout, "\nFlashRom(s) #0 of MDC (i.e. the GoldenImage(s)), " + "do you know what you're about to do [N,y]: "); + c = getc(stdin); + if (!((c == 'Y') || (c == 'y'))) { + fprintf(stderr, "\nAborting on user request\n"); + return -1; + } + } + } + } + if (verifyOnly == 0) { fprintf(stderr, "Programming Endpoint(s) @ Address 0x%04x\n", trb_address); @@ -564,10 +592,11 @@ static int programImageBuffer(uint16_t trb_address, unsigned int size, errorCtr = 0; - if ((verifyOnly == 0) && (flashType == FLASH_MDC)) { + if ((flashType == FLASH_MDC) && (verifyOnly == 0)) { /* Unprotect all Sectors */ if (writeStatusRegister(trb_address, 0) == -1) { - fprintf(stderr, "\nError > program: unprotect all sectors, aborting\n"); + fprintf(stderr, + "\nError > program: unprotect all sectors, aborting\n"); return -1; } } @@ -590,7 +619,8 @@ static int programImageBuffer(uint16_t trb_address, unsigned int size, /* Unprotect sector */ if (sendCommand(trb_address, 0x39 << 24 | (BLOCK_SIZE * block), 3) == -1) { - fprintf(stderr, "\nError > program: unprotect sector #%d, aborting\n", + fprintf(stderr, + "\nError > program: unprotect sector #%d, aborting\n", block); return -1; } @@ -630,7 +660,8 @@ static int programImageBuffer(uint16_t trb_address, unsigned int size, status = writePage(trb_address, page, imageBuffer + page * PAGE_SIZE, bytes); if (status == -1) { - fprintf(stderr, "\nError > program: pageProgram page #%d, aborting\n", + fprintf(stderr, + "\nError > program: pageProgram page #%d, aborting\n", page); return -1; } @@ -646,7 +677,8 @@ static int programImageBuffer(uint16_t trb_address, unsigned int size, /* Protect sector */ if (sendCommand(trb_address, 0x36 << 24 | (BLOCK_SIZE * block), 3) == -1) { - fprintf(stderr, "\nError > program: unprotect sector #%d, aborting\n", + fprintf(stderr, + "\nError > program: unprotect sector #%d, aborting\n", block); return -1; } @@ -831,6 +863,16 @@ static int openLog() { return 0; } +static int selectMdcFlashRom(uint16_t trb_address, uint8_t number) { + /* Select MDC-FlashRom */ + if (trb_register_write(trb_address, MDCFlashRomSelect, + (uint32_t)number) == -1) { + fprintf(logFile, "\nError > selcetMdcFlashRom: TRBNet %s\n", + trb_strerror(trb_errno)); + return -1; + } +} + /* ------ MAIN ---------------------------------------------------------- */ void usage(const char *progName) @@ -838,6 +880,8 @@ void usage(const char *progName) printf("Usage: %s [-h] \n", progName); printf("Options:\n"); + printf(" -g select 'FlashRom #0 (the 'Golden Image') " + "(MDC only, default = no)\n"); printf(" -h give this help\n"); printf(" -V Version number\n"); printf("\nCommands:\n"); @@ -859,9 +903,10 @@ int main(int argc, char ** argv) trb_debug = 0; logFile = stderr; - + mdcFlashSelect = 1; + /* Parse Arguments */ - while ((i = getopt(argc, argv, "+hd:V")) != -1) { + while ((i = getopt(argc, argv, "+hd:gV")) != -1) { switch (i) { case '?': usage(basename(argv[0])); @@ -869,6 +914,9 @@ int main(int argc, char ** argv) case 'h': usage(basename(argv[0])); exit(EXIT_SUCCESS); + case 'g': + mdcFlashSelect = 0; + break; case 'd': trb_debug = strtoul(optarg, NULL, 0); break; @@ -954,6 +1002,13 @@ int main(int argc, char ** argv) if (prepareImageBuffer() == -1) { exit(EXIT_FAILURE); } + + /* In case of MDC: select FlashRom */ + if (flashType == FLASH_MDC) { + if (selectMdcFlashRom(trb_address, mdcFlashSelect) == -1) { + exit(EXIT_FAILURE); + } + } /* Open LogFile */ if (openLog() == -1) { @@ -1001,6 +1056,13 @@ int main(int argc, char ** argv) exit(EXIT_FAILURE); } + /* In case of MDC: select FlashRom */ + if (flashType == FLASH_MDC) { + if (selectMdcFlashRom(trb_address, mdcFlashSelect) == -1) { + exit(EXIT_FAILURE); + } + } + /* Open LogFile */ if (openLog() == -1) { exit(EXIT_FAILURE); @@ -1036,7 +1098,14 @@ int main(int argc, char ** argv) "Broadcast addresses are not supported by this command\n"); exit(EXIT_FAILURE); } - + + /* In case of MDC: select FlashRom */ + if (flashType == FLASH_MDC) { + if (selectMdcFlashRom(trb_address, mdcFlashSelect) == -1) { + exit(EXIT_FAILURE); + } + } + if (initTransfer(trb_address) == -1) { exit(EXIT_FAILURE); } @@ -1046,7 +1115,8 @@ int main(int argc, char ** argv) } /* Write Pages to file */ - fprintf(stderr, "Writing entire flashrom of EndPoint 0x%04x to file '%s' ", + fprintf(stderr, + "Dumping entire flashrom of EndPoint 0x%04x to file '%s' ", trb_address, imageFileName); imageFile = fopen(imageFileName, "w+"); @@ -1109,6 +1179,13 @@ int main(int argc, char ** argv) /* Check ImageBuffer ??? */ + /* In case of MDC: select FlashRom */ + if (flashType == FLASH_MDC) { + if (selectMdcFlashRom(trb_address, mdcFlashSelect) == -1) { + exit(EXIT_FAILURE); + } + } + /* Open LogFile */ if (openLog() == -1) { exit(EXIT_FAILURE); @@ -1144,7 +1221,14 @@ int main(int argc, char ** argv) fprintf(stderr, "Invalid ManId(s), aborting\n"); exit(EXIT_FAILURE); } - + + /* In case of MDC: select FlashRom */ + if (flashType == FLASH_MDC) { + if (selectMdcFlashRom(trb_address, mdcFlashSelect) == -1) { + exit(EXIT_FAILURE); + } + } + if ((status = readPage(trb_address, pageNumber, 256)) == -1) { fprintf(stderr, "Error reading Page, aborting\n"); exit(EXIT_FAILURE); @@ -1173,6 +1257,7 @@ int main(int argc, char ** argv) fprintf(stdout, "\n"); } else { + /* Invalid command */ usage(basename(argv[0])); exit(EXIT_FAILURE); -- 2.43.0