]> jspc29.x-matter.uni-frankfurt.de Git - trbnettools.git/commitdiff
first running alpha version
authorhadaq <hadaq>
Mon, 31 Aug 2009 15:26:36 +0000 (15:26 +0000)
committerhadaq <hadaq>
Mon, 31 Aug 2009 15:26:36 +0000 (15:26 +0000)
.

trbrich/trbflash.c

index 779cb7285b82d9bdca1aeaa234995f1062932e56..0265703eb9710cf7190af7f261ee045b5094e39f 100644 (file)
@@ -7,6 +7,8 @@
 #include <unistd.h>
 #include <stdint.h>
 #include <errno.h>
+#include <time.h>
+#include <ctype.h>
 
 #include <trbnet.h>
 #include <trberror.h>
@@ -40,6 +42,7 @@ static const uint32_t manId[1] = {
 };
 
 static FILE *logFile = NULL;
+static char logFileName[256] = "trbflash.log";
 
 static const char bussy[4][5] = {
   "|\b",
@@ -52,21 +55,23 @@ static const char bussy[4][5] = {
 
 /* ------ MAIN ---------------------------------------------------------- */
 
-static FILE *imageFile = NULL;
-
 static void atexit0()
 {
-  if (imageFile != NULL) {
-    fclose(imageFile);
+  unsigned int i;
+  
+  /* Close files */
+  if ((logFile != NULL) && (logFile != stderr)) {
+    fclose(logFile);
   }
-}
 
-static void sigHandler(int sig)
-{
-  if (sig == SIGTERM) fprintf(stderr, "caught SIGTERM\n");
-  if (sig == SIGINT) fprintf(stderr, "caught SIGINT\n");
-  
-  exit(128 + sig);
+  /* Free memory */
+  free(pageBufferAddress);  
+  for (i = 0; i < NUM_ENDPOINTS; i++) {
+    free(pageBuffer[i]);
+  }
+  free(pageBuffer);
+  free(trbBuffer);
+  free(imageBuffer);
 }
 
 static const unsigned int timeout = 10000;
@@ -83,25 +88,27 @@ static int readSetupRegister(uint16_t trb_address,
   do {
     if ((status = trb_register_read(trb_address, SetupReg,
                                     trbBuffer, TRB_BUFFER_SIZE)) == -1) {
-      trb_error("Error readSetupRegister");
+      fprintf(logFile, "Error > readSetupRegister: TRBNet %s\n",
+              trb_strerror(trb_errno));
       return -1;
     }
     
     /* Check timeout */
     if (ctr >=  timeout) {
-      fprintf(stderr, "Error readSetupRegister, timeout\n");
+      fprintf(logFile, "Error > readSetupRegister: timeout\n");
       return -1;
     }
     ctr++;
   } while (trb_term.status_channel == 0x04);
   
   if (trb_term.status_channel != 0) {
-    trb_error("Error readSetupRegister Status");
+    fprintf(logFile, "Error > readSetupRegister: invalid Status returned %s\n",
+            trb_strerror(trb_errno));
     return -1;
   }
   
   if (status <= 0) {
-    fprintf(stderr, "Error readSetupRegister, length\n");
+    fprintf(logFile, "Error > readSetupRegister: invalid length returned\n");
     return -1;
   }
   
@@ -123,20 +130,23 @@ static int writeSetupRegister(uint16_t trb_address, uint8_t value)
   do {
     if ((status = trb_register_write(trb_address, SetupReg,
                                      (uint32_t)(value << 24))) == -1) {
-      trb_error("Error writeSetupRegister");
+      fprintf(logFile, "Error > writeSetupRegister: TRBNet %s\n",
+              trb_strerror(trb_errno));
       return -1;
     }
     
     /* Check timeout */
     if (ctr >=  timeout) {
-      fprintf(stderr, "Error writeSetupRegister, timeout\n");
+      fprintf(logFile, "Error > writeSetupRegister: timeout\n");
       return -1;
     }
     ctr++;
   } while (trb_term.status_channel != 0);
   
   if (trb_term.status_channel != 0) {
-    trb_error("Error writeSetupRegister Status");
+    fprintf(logFile,
+            "Error > writeSetupRegister: invalid Status returned %s\n",
+            trb_strerror(trb_errno));
     return -1;
   }
 
@@ -155,20 +165,21 @@ static int readCtrlRegister(uint16_t trb_address,
   do {
     if ((status = trb_register_read(trb_address, SetupReg,
                                     trbBuffer, TRB_BUFFER_SIZE)) == -1) {
-      trb_error("Error readCtrlRegister");
+      fprintf(logFile, "Error > readCtrlRegister: TRBNet %s\n",
+              trb_strerror(trb_errno));
       return -1;
     }
     
     /* Check timeout */
     if (ctr >=  timeout) {
-      fprintf(stderr, "Error readCtrlRegister, timeout\n");
+      fprintf(logFile, "Error > readCtrlRegister: timeout\n");
       return -1;
     }
     ctr++;
   } while (trb_term.status_channel != 0);
   
   if (status <= 0) {
-    fprintf(stderr, "Error readCtrlRegister, length\n");
+    fprintf(logFile, "Error > readCtrlRegister: invalid length returned\n");
     return -1;
   }
   
@@ -190,13 +201,14 @@ static int writeCtrlRegister(uint16_t trb_address, uint32_t value)
   do {
     if ((status = trb_register_write(trb_address, CtrlReg,
                                      value)) == -1) {
-      trb_error("Error writeCtrlRegister");
+      fprintf(logFile, "Error > writeCtrlRegister: TRBNet %s\n",
+              trb_strerror(trb_errno));
       return -1;
     }
     
     /* Check timeout */
     if (ctr >=  timeout) {
-      fprintf(stderr, "Error writeCtrlRegister, timeout\n");
+      fprintf(logFile, "Error writeCtrlRegister, timeout\n");
       return -1;
     }
     ctr++;
@@ -236,8 +248,9 @@ static int checkStatus(uint16_t trb_address)
   if ((status = 
        trb_register_read(trb_address, BlockRam, trbBuffer, 
                          TRB_BUFFER_SIZE)) == -1) {
-    trb_error("Error > checkStatus");
-    exit(EXIT_FAILURE);
+    fprintf(logFile, "Error > checkStatus: TRBNet %s\n",
+            trb_strerror(trb_errno));
+    return -1;
   }
   
   if (status <= 0) {
@@ -245,10 +258,9 @@ static int checkStatus(uint16_t trb_address)
   }
 
   for (i = 0; i < status; i += 2) {
-    /* fprintf(stderr, "Status: 0x%02x\n", trbBuffer[i + 1] & 0xff);*/
     if (((trbBuffer[i + 1] >> 5) & 0x01) == 1) {
-      fprintf(stderr, "Erase or program error on EndPoint 0x%04x\n",
-              trbBuffer[i] & 0xffff);
+      fprintf(logFile, "Error > checkStatus: Erase or program error on "
+              "EndPoint 0x%04x\n", trbBuffer[i] & 0xffff);
       return -1; /* Fatal Error */
     }
     if ((trbBuffer[i + 1] & 0x01) == 1) {
@@ -281,8 +293,9 @@ static int readPage(uint16_t trb_address, uint32_t pageNumber,
   if ((status = trb_register_read_mem(trb_address, BlockRam, 0, size,
                                       trbBuffer, TRB_BUFFER_SIZE)) 
       == -1) {
-    trb_error("readPage");
-    exit(EXIT_FAILURE);
+    fprintf(logFile, "Error > readPage: TRBNet %s\n",
+            trb_strerror(trb_errno));
+    return -1;
   }
   
   if (status <= 0) {
@@ -299,7 +312,7 @@ static int readPage(uint16_t trb_address, uint32_t pageNumber,
     unsigned int c;
     
     if (len != size) {
-      fprintf(stderr,
+      fprintf(logFile,
               "Error > readPage: Invalid len %d returned by endpoint 0x%04x\n",
               len, address);
     } 
@@ -347,30 +360,31 @@ static int writePage(uint16_t trb_address, uint32_t pageNumber,
   if ((status = trb_register_write_mem(trb_address, BlockRam, 0,
                                        trbBuffer, size)) 
       == -1) {
-    trb_error("Error > writePage: tranfer trbBuffer");
-    exit(EXIT_FAILURE);
+    fprintf(logFile, "Error > writePage: TRBNet %s\n",
+            trb_strerror(trb_errno));
+    return -1;
   }
  
   /* Enable writing */
   if (sendCommand(trb_address, 0x06 << 24, 0) == -1) {
-    fprintf(stderr, "Error > writePage: write enable\n");
+    fprintf(logFile, "Error > writePage: write enable\n");
     return -1;
   }
   
   /* Write page */
   trbcmd = (0x02 << 24) | ((pageNumber * PAGE_SIZE) & 0xffffff);
   if (sendCommand(trb_address, trbcmd, numBytes - 1) == -1) {
-    fprintf(stderr, "Error > writePage: write page\n");
+    fprintf(logFile, "Error > writePage: write page\n");
     return -1;
   }
   
   /* Wait for not busy and check status */
   while ((status = checkStatus(trb_address) == -2)) {
-    /* fprintf(stderr, "Wait pageProgram ...\n"); */
+    /* fprintf(logFile, "Wait pageProgram ...\n"); */
   }
   
   if (status != 0) {
-    fprintf(stderr, "Error > pageWrite: invalid status\n");
+    fprintf(logFile, "Error > pageWrite: invalid status\n");
     return -1;         
   }
   
@@ -384,7 +398,7 @@ static int checkManId(uint16_t trb_address)
   int i;
 
   
-  /* Read MeanId */
+  /* Read ManId */
   trbcmd = 0x9f << 24;
   if (sendCommand(trb_address, trbcmd, 3) == -1) {
     return -1;
@@ -393,8 +407,9 @@ static int checkManId(uint16_t trb_address)
   if ((status = 
        trb_register_read(trb_address, BlockRam, trbBuffer, 
                          TRB_BUFFER_SIZE)) == -1) {
-    trb_error("Error > checkMeanId");
-    exit(EXIT_FAILURE);
+    fprintf(logFile, "Error > checkManId: TRBNet %s\n",
+            trb_strerror(trb_errno));
+    return -1;
   }
   
   if (status <= 0) {
@@ -411,7 +426,8 @@ static int checkManId(uint16_t trb_address)
       } 
     }
     if (accepted == -1) {
-      fprintf(stderr, "Invalid ManId 0x%04x on EndPoint 0x%04x\n",
+      fprintf(logFile, "Error > checkManId: "
+              "Invalid ManId 0x%04x on EndPoint 0x%04x\n",
               trbBuffer[i + 1], trbBuffer[i] & 0xffff);
       return -1;
     }
@@ -420,18 +436,250 @@ static int checkManId(uint16_t trb_address)
   return 0;
 }
 
+static int programImageBuffer(uint16_t trb_address, unsigned int size, 
+                              int verifyOnly)
+{
+  unsigned int block;
+  int status;
+  int success = 0;
+  unsigned int i;
+  unsigned int page;
+  int bytesWritten = size;
+  int tmp;
+  
+  if (verifyOnly == 0) { 
+    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");
+  } 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");
+  }
+  fprintf(stderr, "Block: 0 1 2 3 4 5 6 7 8 9 A B C D E F");
+  
+  success = 0;
+  for (block = 0; (block < NUM_BLOCKS) && (bytesWritten > 0); block++) {
+    int error = 0;
+    if (block % 16 == 0) {
+      fprintf(stderr, "\n%x      ", block / 16); 
+    }
+    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;
+      }
+    
+      /* Enable writing */
+      if (sendCommand(trb_address, 0x06 << 24, 0) == -1) {
+        fprintf(stderr, "Error > program: write enable, aborting\n");
+        return -1;
+      }
+    
+      /* Erase block */
+      fprintf(stderr, "E\b");
+      if (sendCommand(trb_address, 0xd8 << 24 | (BLOCK_SIZE * block), 3) 
+          == -1) {
+        fprintf(stderr, "Error > program: erase block #%d, aborting\n",
+                block);
+        return -1;
+      }
+      
+      /* Wait for not busy and check status */
+      while ((status = checkStatus(trb_address) == -2)) {
+        /* fprintf(stderr, "Wait erase..\n");*/
+      }
+      
+      if (status != 0) {
+        fprintf(stderr, "Error > program: invalid status, aborting\n");
+        return -1;         
+      }
+      
+      /* Now write pages */
+      fprintf(stderr, "P\b");
+      tmp = bytesWritten;
+      for (i = 0, page = (block * BLOCK_SIZE) / PAGE_SIZE;
+           (i < (BLOCK_SIZE / PAGE_SIZE)) && (tmp > 0); 
+           i++, page++) {
+        int bytes = tmp < PAGE_SIZE ? tmp : PAGE_SIZE;        
+        tmp -= bytes;
+        status = writePage(trb_address, page, 
+                           imageBuffer + page * PAGE_SIZE, bytes);
+        if (status == -1) {
+          fprintf(stderr, "Error > program: pageProgram page #%d, aborting\n",
+                  page);
+          return -1;
+        }
+      }
+      
+      /* 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;
+      }
+    }
+    
+    /* Verify pages */
+    fprintf(stderr, "V\b");
+    tmp = bytesWritten;
+    for (i = 0, page = (block * BLOCK_SIZE) / PAGE_SIZE;
+         (i < (BLOCK_SIZE / PAGE_SIZE)) && (tmp > 0);
+         i++, page++) {
+      unsigned int endPoint;
+      unsigned int c;
+      int bytes = tmp < PAGE_SIZE ? tmp : PAGE_SIZE;        
+      tmp -= bytes;
+      
+      if ((status = readPage(trb_address, page, bytes)) == -1) {
+        fprintf(stderr, "Error > program: reading Page #%d, aborting\n",
+                page);
+        return -1;
+      }
+      
+      for (endPoint = 0; endPoint < status; endPoint++) {
+        for (c = 0; c < bytes; c++) {
+          if (pageBuffer[endPoint][c] != imageBuffer[page * PAGE_SIZE + c]) {
+            error = -1;
+            fprintf(logFile, 
+                    "Error > program: verify failed for page #%d, byte #%d "
+                    "of EndPoint 0x%04x\n",
+                    page, c, pageBufferAddress[endPoint]);
+            
+          }
+        }
+      }
+      
+      bytesWritten = tmp;
+    }
+    
+    if (error == 0) {
+      fprintf(stderr, "@ ");
+    } else {
+      fprintf(stderr, "X ");
+      success = -1;
+    }
+  }
+  if (success == 0) {
+    fprintf(stderr, "\n\nSuccess\n\n");
+  } else {
+    fprintf(stderr, "\n\nErrors have occured, see logFile %s for details\n\n",
+            logFileName);
+  }
+
+  return success;
+}
+
+
+static int readImageFile(const char *imageFileName)
+{ 
+  FILE *imageFile = NULL;
+  int imageSize;
+  
+  /* Read in image */
+  imageFile = fopen(imageFileName, "r");    
+  if (imageFile == NULL) {
+    fprintf(logFile,
+            "Error > readImageFile: Could not open ImageFile %s: %s\n", 
+            imageFileName, strerror(errno));
+  }
+  
+  imageSize = 0;
+  do {
+    imageSize += fread((void*)(imageBuffer + imageSize), sizeof(uint8_t),
+                 PAGE_SIZE, imageFile);
+  } while ((feof(imageFile) == 0) && (imageSize <= PAGE_SIZE * NUM_PAGES));
+  
+  fclose(imageFile);
+  
+  if (imageSize > (PAGE_SIZE * NUM_PAGES)) {
+    fprintf(logFile,
+            "Error > readImageFile: Imagefile '%s' is too large (%d bytes)\n", 
+            imageFileName, imageSize); 
+    return -1;
+  }
+  
+  return imageSize;
+}
+
+static int prepareImageBuffer()
+{
+  unsigned int i;
+  
+  /* Verify imageFile Id */
+  if (strncmp(imageBuffer + 105, "edif_adcmv2", 11) != 0) {
+    fprintf(logFile, "Error > prepareImageBuffer: invalid Firmware-Id\n");
+    return -1;
+  }
+  
+  /* Overwrite the first 301 byte with 0xff */
+  for (i = 0; i < 0x12a; i++) {
+    imageBuffer[i] = 0xff;
+  }
+
+  return 0;
+}
+
+static int openLog() {
+  time_t datet;
+  char date[128];
+    
+  /* Open LogFile */
+  logFile = fopen(logFileName, "w+");
+  if (logFile == NULL) {
+    fprintf(stderr, "Could not open logFile %s: %s\n", 
+            logFileName, strerror(errno));
+    return -1;
+  }
+  
+  datet = time(NULL);
+  ctime_r(&datet, date);
+  date[strlen(date) - 1] = '\0';
+  fprintf(logFile, 
+          "\n\n------------ %s ----------------------------------\n\n", date);
+
+  return 0;
+}
+
 void usage(const char *progName)
 {
   printf("Usage: %s [-h] [-d level] <COMMAND>\n",
          progName);
   printf("Options:\n");
   printf("  -h    give this help\n");
-  printf("  -d    turn on Debugging Information\n");
   printf("\nCommands:\n");
-  printf("   program <trbaddress> <file>      -> \n");
-  printf("   verify <trbaddress> <file>       -> \n");
-  printf("   backup <trbaddress> <file>       -> \n");
-  printf("   restore <trbaddress> <file>      -> \n");
+  printf("   program <trbaddress> <binFile>      -> \n");
+  printf("   verify <trbaddress> <binFile>       -> \n");
+  printf("   backup <trbaddress> <rawFile>       -> \n");
+  printf("   restore <trbaddress> <rawFile>      -> \n");
   printf("   dumppage <trbaddress> pagenumber -> \n");
 }
 
@@ -444,7 +692,8 @@ int main(int argc, char ** argv)
   unsigned int i;
   
   trb_debug = 0;
-  
+  logFile = stderr;
+
   /* Parse Arguments */
   while ((i = getopt(argc, argv, "+hd:")) != -1) {
     switch (i) {
@@ -471,6 +720,8 @@ int main(int argc, char ** argv)
     trb_error("Init_Ports");
     exit(EXIT_FAILURE);
   }
+
+  atexit(atexit0);
   
   /* Allocate memory for buffers */
   
@@ -497,22 +748,104 @@ int main(int argc, char ** argv)
   }
   
   /* Buffer holding the entire rom-image */
-  imageBuffer = (uint8_t*)malloc(sizeof(uint8_t) * PAGE_SIZE * NUM_PAGES);
+  imageBuffer =
+    (uint8_t*)malloc(sizeof(uint8_t) * (PAGE_SIZE * NUM_PAGES + 2));
   if (imageBuffer == NULL) {
     abort();
   }
-  
+
+  /* Execute command */
   if (strcmp(argv[optind], "program") == 0) {
+    
+    /*********************************************************/
+    /* Program Image                                         */
+    /*********************************************************/
+    
+    uint16_t trb_address;
+    char* imageFileName = NULL;
+    int size;
+    
     if (argc - optind != 3) {
       usage(basename(argv[0]));
       exit(EXIT_FAILURE);
     }
-    uint16_t trb_address = strtoul(argv[optind + 1], NULL, 0);
-    char* imageFileName = argv[optind + 2];
     
-    fprintf(stderr, 
-            "Start flashing ImageFile '%s' to TRB_Address 0x%04x\n",
-            imageFileName, trb_address);
+    trb_address = strtoul(argv[optind + 1], NULL, 0);
+    imageFileName = argv[optind + 2];
+    
+    /* Check for correct ManId */
+    if (checkManId(trb_address) == -1) {
+      fprintf(stderr, "Invalid ManId(s), aborting\n");
+      exit(EXIT_FAILURE);
+    }
+    
+    /* Read imageFile to imageBuffer */
+    if ((size = readImageFile(imageFileName)) == -1) {
+      exit(EXIT_FAILURE);
+    }
+    
+    /* Validate Id and prepare imageBuffer */
+    if (prepareImageBuffer() == -1) {
+      exit(EXIT_FAILURE); 
+    }
+    
+    /* Open LogFile */
+    if (openLog() == -1) {
+      exit(EXIT_FAILURE);
+    }
+    
+    fprintf(stderr, "Start programming ImageFile '%s'\n",
+            imageFileName);
+    
+    if (programImageBuffer(trb_address, size, 0) == -1) {
+      exit(EXIT_FAILURE);
+    }
+
+  } else if (strcmp(argv[optind], "verify") == 0) {
+    
+    /*********************************************************/
+    /* Verify Image                                          */
+    /*********************************************************/
+    
+    uint16_t trb_address;
+    char* imageFileName = NULL;
+    int size;
+    
+    if (argc - optind != 3) {
+      usage(basename(argv[0]));
+      exit(EXIT_FAILURE);
+    }
+    
+    trb_address = strtoul(argv[optind + 1], NULL, 0);
+    imageFileName = argv[optind + 2];
+    
+    /* Check for correct ManId */
+    if (checkManId(trb_address) == -1) {
+      fprintf(stderr, "Invalid ManId(s), aborting\n");
+      exit(EXIT_FAILURE);
+    }
+    
+    /* Read imageFile to imageBuffer */
+    if ((size = readImageFile(imageFileName)) == -1) {
+      exit(EXIT_FAILURE);
+    }
+    
+    /* Validate Id and prepare imageBuffer */
+    if (prepareImageBuffer() == -1) {
+      exit(EXIT_FAILURE); 
+    }
+    
+    /* Open LogFile */
+    if (openLog() == -1) {
+      exit(EXIT_FAILURE);
+    }
+    
+    fprintf(stderr, "Start verifying ImageFile '%s'\n",
+            imageFileName);
+    
+    if (programImageBuffer(trb_address, size, 1) == -1) {
+      exit(EXIT_FAILURE);
+    }
     
   } else if (strcmp(argv[optind], "backup") == 0) {
     
@@ -538,11 +871,6 @@ int main(int argc, char ** argv)
       exit(EXIT_FAILURE);
     }
     
-    imageFile = fopen(imageFileName, "w+");    
-    if (imageFile == NULL) {
-      fprintf(stderr, "Could not open Imagefile %s: %s\n", 
-              imageFileName, strerror(errno));
-    }
     
     if (checkManId(trb_address) == -1) {
       exit(EXIT_FAILURE);
@@ -555,8 +883,15 @@ int main(int argc, char ** argv)
     /* Write Pages to file */
     fprintf(stderr, "Writing entire flashrom of EndPoint 0x%04x to file '%s' ",
             trb_address, imageFileName);
+    
+    imageFile = fopen(imageFileName, "w+");    
+    if (imageFile == NULL) {
+      fprintf(stderr, "Could not open Imagefile %s: %s\n", 
+              imageFileName, strerror(errno));
+    }
+
     for (page = 0; page < NUM_PAGES; page++) {
-      if (readPage(trb_address, page, 256) == -1) {
+      if (readPage(trb_address, page, PAGE_SIZE) == -1) {
         fprintf(stderr, "Error reading Page #%d, aborting..\n", page);
         fclose(imageFile);
         exit(EXIT_FAILURE);
@@ -581,13 +916,8 @@ int main(int argc, char ** argv)
         
     uint16_t trb_address;
     char* imageFileName;
-    FILE* imageFile = NULL;
-    size_t size;
-    unsigned int block;
-    unsigned int page;
-    int status;
-    unsigned int i;
-    
+    int size;
+            
     if (argc - optind != 3) {
       usage(basename(argv[0]));
       exit(EXIT_FAILURE);
@@ -601,144 +931,38 @@ int main(int argc, char ** argv)
       exit(EXIT_FAILURE);
     }
     
-    /* Read in image */
-    imageFile = fopen(imageFileName, "r");    
-    if (imageFile == NULL) {
-      fprintf(stderr, "Could not open ImageFile %s: %s\n", 
-              imageFileName, strerror(errno));
+    /* Read imageFile to imageBuffer */
+    if ((size = readImageFile(imageFileName)) == -1) {
+      exit(EXIT_FAILURE);
     }
     
-    size = fread((void*)imageBuffer, PAGE_SIZE, NUM_PAGES, imageFile);
-    
-    if (size != NUM_PAGES) {
+    if (size != PAGE_SIZE * NUM_PAGES) {
       fprintf(stderr, "Invalid ImageFile %s, aborting\n", imageFileName);
       exit(EXIT_FAILURE);
     }
-    fclose(imageFile);
     
     /* Check ImageBuffer ??? */
-    for (i = 0; i < PAGE_SIZE * NUM_PAGES; i++) {
-      imageBuffer[i] = i % 256;
-    }
 
-    fprintf(stderr, "Block: 0 1 2 3 4 5 6 7 8 9 A B C D E F");
+    /* Open LogFile */
+    if (openLog() == -1) {
+      exit(EXIT_FAILURE);
+    }
     
+    fprintf(stderr, "Start restoring ImageFile '%s'\n",
+            imageFileName);
     
-    for (block = 0; block < NUM_BLOCKS; block++) {
-      if (block % 16 == 0) {
-        fprintf(stderr, "\n%x      ", block / 16); 
-      }
-      fprintf(stderr, ".\b");
-      /* Enable writing */
-      if (sendCommand(trb_address, 0x06 << 24, 0) == -1) {
-        fprintf(stderr, "Error > restore: write enable, aborting\n");
-        exit(EXIT_FAILURE);
-      }
-      
-      /* Unprotect sector */
-      if (sendCommand(trb_address, 0x39 << 24 | (BLOCK_SIZE * block), 3) 
-          == -1) {
-        fprintf(stderr, "Error > restore: unprotect sector #%d, aborting\n",
-                block);
-        exit(EXIT_FAILURE);
-      }
-      
-      /* Enable writing */
-      if (sendCommand(trb_address, 0x06 << 24, 0) == -1) {
-        fprintf(stderr, "Error > restore: write enable, aborting\n");
-        exit(EXIT_FAILURE);
-      }
-      
-      /* Erase block */
-      fprintf(stderr, "E\b");
-      if (sendCommand(trb_address, 0xd8 << 24 | (BLOCK_SIZE * block), 3) 
-          == -1) {
-        fprintf(stderr, "Error > restore: erase block #%d, aborting\n",
-                block);
-        exit(EXIT_FAILURE);
-      }
-      
-      /* Wait for not busy and check status */
-      while ((status = checkStatus(trb_address) == -2)) {
-        /* fprintf(stderr, "Wait erase..\n");*/
-      }
-      
-      if (status != 0) {
-        fprintf(stderr, "Error > restore: invalid status, aborting\n");
-        exit(EXIT_FAILURE);         
-      }
-      
-      /* Now write pages */
-      fprintf(stderr, "P\b");
-      page = (block * BLOCK_SIZE) / PAGE_SIZE;
-      for (i = 0, page = (block * BLOCK_SIZE) / PAGE_SIZE;
-           i < (BLOCK_SIZE / PAGE_SIZE); 
-           i++, page++) {
-        
-        status = writePage(trb_address, page, 
-                           imageBuffer + page * PAGE_SIZE, PAGE_SIZE);
-        if (status == -1) {
-          fprintf(stderr, "Error > restore: pageProgram page #%d, aborting\n",
-                  page);
-          exit(EXIT_FAILURE);
-        }
-      }
-      
-      /* Enable writing */
-      if (sendCommand(trb_address, 0x06 << 24, 0) == -1) {
-        fprintf(stderr, "Error > restore: write enable, aborting\n");
-        exit(EXIT_FAILURE);
-      }
-      
-      /* Protect sector */
-      if (sendCommand(trb_address, 0x36 << 24 | (BLOCK_SIZE * block), 3) 
-          == -1) {
-        fprintf(stderr, "Error > restore: unprotect sector #%d, aborting\n",
-                block);
-        exit(EXIT_FAILURE);
-      }
-      
-      /* Verify pages */
-      fprintf(stderr, "V\b");
-      page = (block * BLOCK_SIZE) / PAGE_SIZE;
-      for (i = 0, page = (block * BLOCK_SIZE) / PAGE_SIZE;
-           i < (BLOCK_SIZE / PAGE_SIZE); 
-           i++, page++) {
-        unsigned int endPoint;
-        unsigned int c;
-        
-        if ((status = readPage(trb_address, page, 256)) == -1) {
-          fprintf(stderr, "Error > restore: reading Page #%d, aborting\n",
-                  page);
-          exit(EXIT_FAILURE);
-        }
-       
-        for (endPoint = 0; endPoint < status; endPoint++) {
-          int verifyOK = 0;
-          for (c = 0; c < PAGE_SIZE; c++) {
-            if (pageBuffer[endPoint][c] != 
-                *(imageBuffer + page * PAGE_SIZE + c)) {
-              verifyOK = -1;
-            }
-          }
-          if (verifyOK == -1) {
-            fprintf(stderr, 
-                    "Error > restore: verify failed for page #%d, "
-                    "of EndPoint 0x%04x\n",
-                    page, pageBufferAddress[endPoint]);
-          }
-        }
-      }
-      
-      fprintf(stderr, "@ ");
+    if (programImageBuffer(trb_address, size, 0) == -1) {
+      exit(EXIT_FAILURE);
     }
     
-    fprintf(stderr, "\n\nDone\n");
-    
   } else if (strcmp(argv[optind], "dumppage") == 0) {
+    
+    /*********************************************************/
+    /* Read and dump a page                                  */
+    /*********************************************************/
+    
     uint16_t trb_address;
     uint32_t pageNumber;
-    char* imageFileName;
     unsigned int i;
     int status;
     
@@ -749,103 +973,38 @@ int main(int argc, char ** argv)
     trb_address = strtoul(argv[optind + 1], NULL, 0);
     pageNumber = strtoul(argv[optind + 2], NULL, 0);
     
-    fprintf(stderr, 
-            "Start flashing ImageFile '%s' to TRB_Address 0x%04x\n",
-            imageFileName, trb_address);
-    
-#if 0
-
-    if (sendCommand(trb_address, 0x05 << 24, 0) == -1) {
-      exit(EXIT_FAILURE);
-    }
-    if (trb_register_read(trb_address, BlockRam, buffer, 2) == -1) {
-      trb_error("readStatus");
-      exit(EXIT_FAILURE);
-    }
-    fprintf(stderr, "Satus: 0x%08x\n", buffer[1]);
-#endif
-    
     if ((status = readPage(trb_address, pageNumber, 256)) == -1) {
-      fprintf(stderr, "Error reading Page\n");
+      fprintf(stderr, "Error reading Page, aborting\n");
       exit(EXIT_FAILURE);
     }
     
     for (i = 0; i < status; i++) {
-      fprintf(stderr, "EndPoint: 0x%04x\n", pageBufferAddress[i]); 
+      char text[32] = "";
+      char cc[2] = " \0";
       int c;
+      fprintf(stdout, "EndPoint: 0x%04x   Page #%d\n",
+              pageBufferAddress[i],pageNumber); 
       for (c = 0; c < 256; c++) {
-        fprintf(stderr, "%d: 0x%02x\n", c, pageBuffer[i][c]); 
+        if ((c % 16) == 0) {
+          text[0] = '\0';
+          fprintf(stdout, "0x%02x  ", c);
+        }
+        fprintf(stdout, "%02x ", pageBuffer[i][c]);
+        cc[0] = isprint((char)pageBuffer[i][c]) == 0 
+          ? '.' : (char)pageBuffer[i][c];
+        strncat(text, cc, 32);
+        if ((c % 16) == 15) {
+          fprintf(stdout, "  %s\n", text);
+        }
       }
     }
-#if 0
-    checkManId(trb_address);
-    checkStatus(trb_address);
-    sendCommand(trb_address, 0x06 << 24, 0);
-    checkStatus(trb_address);
-    sendCommand(trb_address, 0x04 << 24, 0);
-    printf("S: %d\n", checkStatus(trb_address));
-#endif
+    fprintf(stdout, "\n");
+  
   } else {
     /* Invalid command */
     usage(basename(argv[0]));
     exit(EXIT_FAILURE);
   }
-
-#if 0
-    fprintf(stderr, 
-            "Start flashing ImageFile '%s' to TRB_Address 0x%04x\n",
-            imageFileName, trb_address);
-
-    /* open port */
-    init_ports();  
-    
-    /* Open ImageFile */
-    imageFile = fopen(imageFileName, "r");
-    if (imageFile == NULL) {
-      perror("Error opening ImageFile");
-      exit(EXIT_FAILURE);
-    }
-
   
-    while (feof(imageFile) == 0) {
-      size_t size;
-      /* clear buffer */
-      for (i = 0 ; i < PAGE_SIZE; i++) {
-        page[i] = 0xffffffff;
-    }
-      
-      if ((size = fread((void*)page, 4, PAGE_SIZE, imageFile)) != PAGE_SIZE) {
-        fprintf(stderr, "Error reading ImageFile, size = %d\n", size);
-        exit(EXIT_FAILURE);
-      }
-    
-      fprintf(stderr, "\n\n");
-      for (i = 0 ; i < PAGE_SIZE; i++) {
-        fprintf(stderr, "0x%08x\n", page[i]);
-      }
-    }
-#endif    
-
-#if 0
-  /* Set Signalhandler */
-  atexit(atexit0);
-  signal(SIGINT, sigHandler);
-  signal(SIGTERM, sigHandler);
-
-  /* Set signal mask for blocking */
-  sigemptyset(&blockSet);
-  sigaddset(&blockSet, SIGINT);
-  sigaddset(&blockSet, SIGTERM);
-#endif
-  
-  /* Free memory */
-  free(pageBufferAddress);  
-  for (i = 0; i < NUM_ENDPOINTS; i++) {
-    free(pageBuffer[i]);
-  }
-  free(pageBuffer);
-  free(trbBuffer);
-  free(imageBuffer);
-
   exit(EXIT_SUCCESS);
 }