]> jspc29.x-matter.uni-frankfurt.de Git - trbnettools.git/commitdiff
added infopage, i.e. the lastPage
authorhadaq <hadaq>
Tue, 29 Sep 2009 14:00:45 +0000 (14:00 +0000)
committerhadaq <hadaq>
Tue, 29 Sep 2009 14:00:45 +0000 (14:00 +0000)
trbrich/trbflash.c

index 3866044d370f776c56c33a37c68867d74d4a6041..ac3a656c59c159a926ccde381fe3584f5ea10542 100644 (file)
@@ -9,8 +9,11 @@
 #include <errno.h>
 #include <time.h>
 #include <ctype.h>
-
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
 #include <trbnet.h>
+
 #include <trberror.h>
 
 static const uint16_t CtrlReg = 0xd000; 
@@ -63,10 +66,12 @@ static const char FlashTypeStr[][16] = {"INVALID", "ADCM1", "ADCM2", "MDC"};
 
 static FlashType flashType = FLASH_INVALID; 
 
-static const char trbflash_version[] = "$Revision: 2.07 $"; 
+static const char trbflash_version[] = "$Revision: 2.08 $"; 
 
 static uint32_t mdcFlashSelect = 1;
 
+static int force = 0;
+
 /* ------ Local Functions ----------------------------------------------- */
 
 static void atexit0()
@@ -534,8 +539,14 @@ static int writePage(uint16_t trb_address, uint32_t pageNumber,
   return 0;
 }
 
+typedef enum {
+  PMODE_PROGRAMM,
+  PMODE_PROGRAMM_FULL,
+  PMODE_VERIFY
+} PMode;
+
 static int programImageBuffer(uint16_t trb_address, unsigned int size, 
-                              int verifyOnly)
+                              PMode mode)
 {
   unsigned int block;
   int status;
@@ -545,7 +556,7 @@ static int programImageBuffer(uint16_t trb_address, unsigned int size,
   int bytesWritten = size;
   int tmp;
   
-  if (verifyOnly == 0) {
+  if ((mode != PMODE_VERIFY) && (force == 0)) {
     /* Be nice and ask before start flashing the roms */
     char c;
     if (flashType == FLASH_MDC) {
@@ -569,30 +580,30 @@ static int programImageBuffer(uint16_t trb_address, unsigned int size,
     }
   }
   
-  if (verifyOnly == 0) { 
+  if (mode != PMODE_VERIFY) { 
     fprintf(stderr, "Programming Endpoint(s) @ Address 0x%04x\n",
             trb_address);
     fprintf(stderr, "Symbols:\n"
-            "  .: Starting\n"
             "  E: Erasing\n"
             "  P: Programming\n"
             "  V: Verifying\n"
             "  X: Failed (see logfile 'trbflash.log' for details)\n"
-            "  @: Success\n\n");
+            "  @: Success\n"
+            "  .: Skipped\n\n");
   } else {
     fprintf(stderr, "Verifying Endpoint(s) @ Address 0x%04x\n",
             trb_address);
     fprintf(stderr, "Symbols:\n"
-            "  .: Starting\n"
             "  V: Verifying\n"
             "  X: Failed (see logfile 'trbflash.log' for details)\n"
-            "  @: Success\n\n");
+            "  @: Success\n"
+            "  .: Skipped\n\n");
   }
   fprintf(stderr, "Block: 0 1 2 3 4 5 6 7 8 9 A B C D E F");
   
   errorCtr = 0;
   
-  if ((flashType == FLASH_MDC) && (verifyOnly == 0)) {
+  if ((flashType == FLASH_MDC) && (mode != PMODE_VERIFY)) {
     /* Unprotect all Sectors */
     if (writeStatusRegister(trb_address, 0) == -1) {
       fprintf(stderr,
@@ -601,14 +612,24 @@ static int programImageBuffer(uint16_t trb_address, unsigned int size,
     }
   } 
   
-  for (block = 0; (block < NUM_BLOCKS) && (bytesWritten > 0); block++) {
+  
+  for (block = 0; (block < NUM_BLOCKS); block++) {
     int error = 0;
+    int writeInfoPage = (block == NUM_BLOCKS - 1) && (mode == PMODE_PROGRAMM)
+      ? 1 : 0;
+    
     if (block % 16 == 0) {
       fprintf(stderr, "\n%x      ", block / 16); 
     }
     fprintf(stderr, ".\b");
     
-    if (verifyOnly == 0) {
+    if (!(bytesWritten > 0) && (writeInfoPage == 0)) {
+      fprintf(stderr, ". ");
+      continue;
+    }
+    
+    if ((mode != PMODE_VERIFY) && 
+        ((bytesWritten > 0) || (writeInfoPage == 1))) {
       if ((flashType == FLASH_ADCM1) || (flashType == FLASH_ADCM2)) {
         /* Enable writing */
         if (sendCommand(trb_address, 0x06 << 24, 0) == -1) {
@@ -666,6 +687,19 @@ static int programImageBuffer(uint16_t trb_address, unsigned int size,
           return -1;
         }
       }
+
+      if (writeInfoPage == 1) {
+        /* Write Info-Page */
+        status = writePage(trb_address, (NUM_PAGES - 1), 
+                           imageBuffer + (NUM_PAGES - 1) * 
+                           PAGE_SIZE, PAGE_SIZE);
+        if (status == -1) {
+          fprintf(stderr,
+                  "\nError > program: pageProgram page #%d, aborting\n",
+                  page);
+          return -1;
+        }
+      }
       
       if ((flashType == FLASH_ADCM1) || (flashType == FLASH_ADCM2)) {
         /* Enable writing */
@@ -720,6 +754,36 @@ static int programImageBuffer(uint16_t trb_address, unsigned int size,
       
       bytesWritten = tmp;
     }
+
+    if (writeInfoPage == 1) {
+      /* Verify Info-Page */
+      unsigned int endPoint;
+      unsigned int c;
+      int bytes = tmp < PAGE_SIZE ? tmp : PAGE_SIZE;        
+      tmp -= bytes;
+      
+      if ((status = readPage(trb_address, NUM_PAGES - 1, PAGE_SIZE)) == -1) {
+        fprintf(stderr, "\nError > program: reading InfoPage, aborting\n");
+        return -1;
+      }
+      
+      for (endPoint = 0; endPoint < status; endPoint++) {
+        for (c = 0; c < PAGE_SIZE; c++) {
+          if (pageBuffer[endPoint][c] != 
+              imageBuffer[(NUM_PAGES - 1) * PAGE_SIZE + c]) {
+            error = -1;
+            errorCtr++;
+            fprintf(logFile, 
+                    "verify failed InfoPage, byte #%d "
+                    "(exp: 0x%02x rec: 0x%02x), EndPoint: 0x%04x\n",
+                    c, 
+                    imageBuffer[(NUM_PAGES - 1) * PAGE_SIZE + c], 
+                    pageBuffer[endPoint][c],
+                    pageBufferAddress[endPoint]);
+          }
+        }
+      }
+    }
     
     if (error == 0) {
       fprintf(stderr, "@ ");
@@ -728,7 +792,7 @@ static int programImageBuffer(uint16_t trb_address, unsigned int size,
     }
   }
   
-  if ((verifyOnly == 0) && (flashType == FLASH_MDC)) {
+  if ((mode != PMODE_VERIFY) && (flashType == FLASH_MDC)) {
     /* Protect all Sectors  i.e. write 0x3c to statusRegister */
     if (writeStatusRegister(trb_address, 0x00) == -1) {
       fprintf(stderr, "\nError > program: protect all sectors, aborting\n");
@@ -842,6 +906,32 @@ static int prepareImageBuffer()
   return 0;
 }
 
+static int createInfoPage(const char* fileName, const char* userString)
+{
+  char* buffer = imageBuffer + (NUM_PAGES - 1) * PAGE_SIZE;
+  struct stat statBuf;
+
+  unsigned int i;
+  for (i = 0; i < PAGE_SIZE; i++) {
+    buffer[i] = '\0';
+  }
+  if (stat(fileName, &statBuf) == -1) {
+    fprintf(logFile, "Error > prepareInfoPage: statCall failed: %s\n",
+            strerror(errno));  
+    return -1;
+  }
+  snprintf(buffer + 0, 63, "NAME: %s", basename((char*)fileName)); 
+  snprintf(buffer + 64, 31, "DATE: %s", ctime(&statBuf.st_mtime));
+  snprintf(buffer + 96, 159, "USER: %s", userString);
+  fprintf(stderr, "%s\n", buffer); 
+  fprintf(stderr, "%s\n", buffer + 64); 
+  fprintf(stderr, "%s\n", buffer + 96);
+  
+  return 0;
+}
+
 static int openLog() {
   time_t datet;
   char date[128];
@@ -879,11 +969,13 @@ static int selectMdcFlashRom(uint16_t trb_address, uint8_t number) {
 
 void usage(const char *progName)
 {
-  printf("Usage: %s [-h] [-V] <COMMAND>\n",
+  printf("Usage: %s [-s str] [-g] [-y] [-h] [-V] <COMMAND>\n",
          progName);
   printf("Options:\n");
+  printf("  -s    set user-string of info page (default: empty string)\n");
   printf("  -g    select 'FlashRom #0 (the 'Golden Image') "
          "(MDC only, default = no)\n");
+  printf("  -y    assume yes to all questions which would be asked\n");
   printf("  -h    give this help\n");
   printf("  -V    Version number\n");
   printf("\nCommands:\n");
@@ -895,6 +987,8 @@ void usage(const char *progName)
          "write entire flash-memory to raw-file\n");
   printf("   restore <trbaddress> <raw-file>  -> "
          "write backuped raw-file to flash-memory\n");
+  printf("   info <trbaddress>                -> "
+         "dump content of info-page to stdout\n");
   printf("   dumppage <trbaddress> pagenumber -> "
          "dump one flash-memory page to stdout\n");
 }
@@ -902,13 +996,15 @@ void usage(const char *progName)
 int main(int argc, char ** argv)
 {
   unsigned int i;
+  char userInfoStr[256] = "";
   
   trb_debug = 0;
   logFile = stderr;
   mdcFlashSelect = 1;
+  force = 0;
   
   /* Parse Arguments */
-  while ((i = getopt(argc, argv, "+hd:gV")) != -1) {
+  while ((i = getopt(argc, argv, "+hd:s:gyV")) != -1) {
     switch (i) {
     case '?':
       usage(basename(argv[0]));
@@ -916,12 +1012,18 @@ int main(int argc, char ** argv)
     case 'h':
       usage(basename(argv[0]));
       exit(EXIT_SUCCESS);
+    case 's':
+      strncpy(userInfoStr, optarg, 256);
+      break;
     case 'g':
       mdcFlashSelect = 0;
       break;  
     case 'd':
       trb_debug = strtoul(optarg, NULL, 0);
       break;
+    case 'y':
+      force = 1;
+      break;
     case 'V':
       printf("%s %s, using libtrbnet %s\n",
              basename(argv[0]), trbflash_version, trbnet_version);
@@ -943,9 +1045,9 @@ int main(int argc, char ** argv)
     trb_error("Init_Ports");
     exit(EXIT_FAILURE);
   }
-
-  atexit(atexit0);
   
+  atexit(atexit0);
+
   /* Allocate memory for buffers */
   
   /* Buffer holding the TRBNet packages, e.g. use by trb_read_register */
@@ -999,12 +1101,20 @@ int main(int argc, char ** argv)
     if ((size = readImageFile(imageFileName)) == -1) {
       exit(EXIT_FAILURE);
     }
+    if (size > (PAGE_SIZE * (NUM_PAGES - 1))) {
+      fprintf(stderr, "ImageFile to large (%d bytes, maximum is %d bytes)\n",
+              size, PAGE_SIZE * (NUM_PAGES - 1));
+      exit(EXIT_FAILURE);
+    }
     
     /* Validate Id and prepare imageBuffer */
     if (prepareImageBuffer() == -1) {
       exit(EXIT_FAILURE); 
     }
-
+    
+    /* Create InfoPage */
+    createInfoPage(imageFileName, userInfoStr);
+    
     /* In case of MDC: select FlashRom */
     if (flashType == FLASH_MDC) {
       if (selectMdcFlashRom(trb_address, mdcFlashSelect) == -1) {
@@ -1020,7 +1130,7 @@ int main(int argc, char ** argv)
     fprintf(stderr, "Start programming ImageFile '%s'\n",
             imageFileName);
     
-    if (programImageBuffer(trb_address, size, 0) == -1) {
+    if (programImageBuffer(trb_address, size, PMODE_PROGRAMM) == -1) {
       exit(EXIT_FAILURE);
     }
 
@@ -1052,7 +1162,12 @@ int main(int argc, char ** argv)
     if ((size = readImageFile(imageFileName)) == -1) {
       exit(EXIT_FAILURE);
     }
-    
+    if (size > (PAGE_SIZE * (NUM_PAGES - 1))) {
+      fprintf(stderr, "ImageFile to large (%d bytes, maximum is %d bytes)\n",
+              size, PAGE_SIZE * (NUM_PAGES - 1));
+      exit(EXIT_FAILURE);
+    }
+
     /* Validate Id and prepare imageBuffer */
     if (prepareImageBuffer() == -1) {
       exit(EXIT_FAILURE); 
@@ -1073,7 +1188,7 @@ int main(int argc, char ** argv)
     fprintf(stderr, "Start verifying ImageFile '%s'\n",
             imageFileName);
     
-    if (programImageBuffer(trb_address, size, 1) == -1) {
+    if (programImageBuffer(trb_address, size, PMODE_VERIFY) == -1) {
       exit(EXIT_FAILURE);
     }
     
@@ -1196,10 +1311,65 @@ int main(int argc, char ** argv)
     fprintf(stderr, "Start restoring ImageFile '%s'\n",
             imageFileName);
     
-    if (programImageBuffer(trb_address, size, 0) == -1) {
+    if (programImageBuffer(trb_address, size, PMODE_PROGRAMM_FULL) == -1) {
       exit(EXIT_FAILURE);
     }
     
+  } else if (strcmp(argv[optind], "info") == 0) {
+    
+    /*********************************************************/
+    /* Dump Info-Page                                        */
+    /*********************************************************/
+    
+    uint16_t trb_address;
+    unsigned int i;
+    int status;
+    
+    if (argc - optind != 2) {
+      usage(basename(argv[0]));
+      exit(EXIT_FAILURE);
+    }
+    trb_address = strtoul(argv[optind + 1], NULL, 0);
+    
+    /* Check for correct ManId */
+    if (initTransfer(trb_address) == -1) {
+      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, NUM_PAGES - 1, PAGE_SIZE)) == -1) {
+      fprintf(stderr, "Error reading InfoPage, aborting\n");
+      exit(EXIT_FAILURE);
+    }
+
+    for (i = 0; i < status; i++) {
+      pageBuffer[i][63] = '\0';
+      pageBuffer[i][95] = '\0';
+      pageBuffer[i][255] = '\0';
+      fprintf(stdout, "\nEndPoint: 0x%04x   InfoPage\n",
+              pageBufferAddress[i]);
+      if (
+          (strncmp(pageBuffer[i] + 0, "NAME:", 5) != 0) ||
+          (strncmp(pageBuffer[i] + 64, "DATE:", 5) != 0) ||
+          (strncmp(pageBuffer[i] + 96, "USER:", 5) != 0)
+          ) {
+        fprintf(stdout, "INVALID CONTENT\n");
+        continue;
+      }
+      
+      fprintf(stdout, "%s\n", pageBuffer[i]); 
+      fprintf(stdout, "%s\n", pageBuffer[i] + 64); 
+      fprintf(stdout, "%s\n", pageBuffer[i] + 96);
+    }
+    fprintf(stdout, "\n");
+          
   } else if (strcmp(argv[optind], "dumppage") == 0) {
     
     /*********************************************************/
@@ -1231,7 +1401,7 @@ int main(int argc, char ** argv)
       }
     }
 
-    if ((status = readPage(trb_address, pageNumber, 256)) == -1) {
+    if ((status = readPage(trb_address, pageNumber, PAGE_SIZE)) == -1) {
       fprintf(stderr, "Error reading Page, aborting\n");
       exit(EXIT_FAILURE);
     }
@@ -1241,8 +1411,8 @@ int main(int argc, char ** argv)
       char cc[2] = " \0";
       int c;
       fprintf(stdout, "\nEndPoint: 0x%04x   Page #%d\n",
-              pageBufferAddress[i],pageNumber); 
-      for (c = 0; c < 256; c++) {
+              pageBufferAddress[i], pageNumber); 
+      for (c = 0; c < PAGE_SIZE; c++) {
         if ((c % 16) == 0) {
           text[0] = '\0';
           fprintf(stdout, "0x%02x  ", c);