]> jspc29.x-matter.uni-frankfurt.de Git - trbnettools.git/commitdiff
minor update
authorhadaq <hadaq>
Fri, 4 Feb 2011 18:00:56 +0000 (18:00 +0000)
committerhadaq <hadaq>
Fri, 4 Feb 2011 18:00:56 +0000 (18:00 +0000)
libtrbnet/trbflash.c

index 9bd6c7e23707a4067e874c70d56b7a38f926a0e4..9874deed784b22d359f8da087821aa7421d58465 100644 (file)
 #include <trberror.h>
 
 static const uint16_t HardwareId = 0x0042;
-static const uint16_t CtrlReg = 0xd000; 
+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_ttrbBuffer = NULL;
-static uint8_t** pageBuffer = NULL;
-static uint16_tpageBufferAddress = NULL;
+static uint32_t *trbBuffer = NULL;
+static uint8_t **pageBuffer = NULL;
+static uint16_t *pageBufferAddress = NULL;
 
-static uint8_timageBuffer = NULL;
+static uint8_t *imageBuffer = NULL;
 
 /* Maximum number of endpoints */
 #define NUM_ENDPOINTS 1024
 
 /* 32-Bit Words: */
 #define BLOCKRAM_SIZE 64
-#define TRB_BUFFER_SIZE (BLOCKRAM_SIZE * NUM_ENDPOINTS + NUM_ENDPOINTS) 
+#define TRB_BUFFER_SIZE (BLOCKRAM_SIZE * NUM_ENDPOINTS + NUM_ENDPOINTS)
 
 /* Bytes: */
 #define PAGE_SIZE 256
@@ -42,7 +42,7 @@ static unsigned int NUM_PAGES = 0;
 #define BLOCK_SIZE (64 * 1024)
 static unsigned int NUM_BLOCKS = 0;
 
-#define PAGE_BUFFER_SIZE (PAGE_SIZE * NUM_ENDPOINTS) 
+#define PAGE_BUFFER_SIZE (PAGE_SIZE * NUM_ENDPOINTS)
 
 static FILE *logFile = NULL;
 static const char logFileName[256] = "trbflash.log";
@@ -56,48 +56,47 @@ static const char busy[4][5] = {
 
 static const unsigned int timeout = 10000;
 
-typedef enum {
-  FLASH_INVALID = 0,
-  
-  FLASH_RICH_ADCM_V1 = 1,
-  FLASH_RICH_ADCM_V2 = 2,
-  FLASH_RICH_ADCM_V3 = 3,
-  
-  FLASH_MDC_HUB_V2_FPGA1234 = 4,
-  FLASH_MDC_HUB_V2_FPGA5 = 5,
-  
-  FLASH_MDC_OEP_V2 = 6,
-  FLASH_MDC_OEP_V3 = 7,
-  
-  FLASH_SHOWER_ADDON_V2_FPGA1 = 8,
-  FLASH_SHOWER_ADDON_V2_FPGA2 = 9,
-  FLASH_SHOWER_ADDON_V2_FPGA3 = 10,
-  
-  FLASH_CTS_FPGA1 = 11,
-  FLASH_CTS_FPGA2 = 12
-} FlashType;
-
-static const char FlashTypeStr[16][32] =
+typedef enum
   {
-    "INVALID",
-    "RICH_ADCM_V1",
-    "RICH_ADCM_V2",
-    "RICH_ADCM_V3", 
-    "MDC_HUB_V2_FPGA1234",
-    "MDC_HUB_V2_FPGA5",
-    "MDC_OEP_V2",
-    "MDC_OEP_V3",
-    "SHOWER_ADDON_V2_FPGA1",
-    "SHOWER_ADDON_V2_FPGA2",
-    "SHOWER_ADDON_V2_FPGA3",
-    "CTS_FPGA1",
-    "CTS_FPGA2"
-  };
-
-static FlashType flashType = FLASH_INVALID; 
+    FLASH_INVALID = 0,
+
+    FLASH_RICH_ADCM_V1 = 1,
+    FLASH_RICH_ADCM_V2 = 2,
+    FLASH_RICH_ADCM_V3 = 3,
+
+    FLASH_MDC_HUB_V2_FPGA1234 = 4,
+    FLASH_MDC_HUB_V2_FPGA5 = 5,
+
+    FLASH_MDC_OEP_V2 = 6,
+    FLASH_MDC_OEP_V3 = 7,
+
+    FLASH_SHOWER_ADDON_V2_FPGA1 = 8,
+    FLASH_SHOWER_ADDON_V2_FPGA2 = 9,
+    FLASH_SHOWER_ADDON_V2_FPGA3 = 10,
+
+    FLASH_CTS_FPGA1 = 11,
+    FLASH_CTS_FPGA2 = 12
+  } FlashType;
+
+static const char FlashTypeStr[16][32] = {
+  "INVALID",
+  "RICH_ADCM_V1",
+  "RICH_ADCM_V2",
+  "RICH_ADCM_V3",
+  "MDC_HUB_V2_FPGA1234",
+  "MDC_HUB_V2_FPGA5",
+  "MDC_OEP_V2",
+  "MDC_OEP_V3",
+  "SHOWER_ADDON_V2_FPGA1",
+  "SHOWER_ADDON_V2_FPGA2",
+  "SHOWER_ADDON_V2_FPGA3",
+  "CTS_FPGA1",
+  "CTS_FPGA2"
+};
+
+static FlashType flashType = FLASH_INVALID;
 static uint32_t manId = 0;
-static const char trbflash_version[] = "$Revision: 2.26 $"; 
+static const char trbflash_version[] = "$Revision: 2.27 $";
 
 static uint32_t mdcFlashSelect = 1;
 
@@ -112,14 +111,14 @@ static int skipVerify = 0;
 static void atexit0()
 {
   unsigned int i;
-  
+
   /* Close files */
   if ((logFile != NULL) && (logFile != stderr)) {
     fclose(logFile);
   }
 
   /* Free memory */
-  free(pageBufferAddress);  
+  free(pageBufferAddress);
   for (i = 0; i < NUM_ENDPOINTS; i++) {
     free(pageBuffer[i]);
   }
@@ -132,24 +131,24 @@ static int writeSetupRegister(uint16_t trb_address, uint8_t value)
 {
   unsigned int ctr = 0;
   int status = -1;
-  
+
   /* Wait until NoMoreData is withdrawn */
   do {
     if ((status = trb_register_write(trb_address, SetupReg,
-                                     (uint32_t)(value << 24))) == -1) {
+                                     (uint32_t) (value << 24))) == -1) {
       fprintf(logFile, "Error > writeSetupRegister: TRBNet %s\n",
               trb_strerror());
       return -1;
     }
-    
+
     /* Check timeout */
-    if (ctr >=  timeout) {
+    if (ctr >= timeout) {
       fprintf(logFile, "Error > writeSetupRegister: timeout\n");
       return -1;
     }
     ctr++;
   } while (trb_term.status_channel != 0);
-  
+
   if (trb_term.status_channel != 0) {
     fprintf(logFile,
             "Error > writeSetupRegister: invalid Status returned %s\n",
@@ -157,7 +156,7 @@ static int writeSetupRegister(uint16_t trb_address, uint8_t value)
     return -1;
   }
 
-  return 0; 
+  return 0;
 }
 
 static int readCtrlRegister(uint16_t trb_address,
@@ -167,7 +166,7 @@ static int readCtrlRegister(uint16_t trb_address,
   int status = -1;
   int c;
   int i;
-  
+
   /* Wait until NoMoreData is withdrawn */
   do {
     if ((status = trb_register_read(trb_address, CtrlReg,
@@ -176,26 +175,27 @@ static int readCtrlRegister(uint16_t trb_address,
               trb_strerror());
       return -1;
     }
-    
+
     /* Check timeout */
-    if (ctr >=  timeout) {
+    if (ctr >= timeout) {
       fprintf(logFile, "Error > readCtrlRegister: timeout\n");
       return -1;
     }
     ctr++;
   } while (trb_term.status_channel != 0);
-  
+
   if (status <= 0) {
     fprintf(logFile, "Error > readCtrlRegister: %s\n", trb_strerror());
     return -1;
   }
-  
+
   c = 0;
   for (i = 1; i < status; i += 2) {
-    if (value != NULL) value[c] = trbBuffer[i];
+    if (value != NULL)
+      value[c] = trbBuffer[i];
     c++;
   }
-  
+
   return c;
 }
 
@@ -203,25 +203,24 @@ static int writeCtrlRegister(uint16_t trb_address, uint32_t value)
 {
   unsigned int ctr = 0;
   int status = -1;
-  
+
   /* Wait until NoMoreData is withdrawn */
   do {
-    if ((status = trb_register_write(trb_address, CtrlReg,
-                                     value)) == -1) {
+    if ((status = trb_register_write(trb_address, CtrlReg, value)) == -1) {
       fprintf(logFile, "Error > writeCtrlRegister: TRBNet %s\n",
               trb_strerror());
       return -1;
     }
-    
+
     /* Check timeout */
-    if (ctr >=  timeout) {
+    if (ctr >= timeout) {
       fprintf(logFile, "Error writeCtrlRegister, timeout\n");
       return -1;
     }
     ctr++;
   } while (trb_term.status_channel != 0);
-    
-  return 0; 
+
+  return 0;
 }
 
 static int sendCommand(uint16_t trb_address, uint32_t cmd, uint8_t max)
@@ -235,7 +234,7 @@ static int sendCommand(uint16_t trb_address, uint32_t cmd, uint8_t max)
   if (readCtrlRegister(trb_address, NULL) == -1) {
     return -1;
   }
-  
+
   return 0;
 }
 
@@ -245,55 +244,53 @@ static int checkStatus(uint16_t trb_address)
   int status;
   int ret;
   int i;
-  
+
   /* Read Status Register */
   trbcmd = 0x05 << 24;
   if (sendCommand(trb_address, trbcmd, 0) == -1) {
     return -1;
   }
-  
-  if ((status = 
-       trb_register_read(trb_address, BlockRam, trbBuffer, 
+
+  if ((status =
+       trb_register_read(trb_address, BlockRam, trbBuffer,
                          TRB_BUFFER_SIZE)) == -1) {
-    fprintf(logFile, "Error > checkStatus: TRBNet %s\n",
-            trb_strerror());
+    fprintf(logFile, "Error > checkStatus: TRBNet %s\n", trb_strerror());
     return -1;
   }
-  
+
   if (status <= 0) {
     return -1;
   }
 
   ret = 0;
   for (i = 0; i < status; i += 2) {
-    if ((manId == 0x01461f) || 
-        (manId == 0x00471f)) {
+    if ((manId == 0x01461f) || (manId == 0x00471f)) {
       /* Check EPE Bit (ADCM and SHOWER) */
       if (((trbBuffer[i + 1] >> 5) & 0x01) == 1) {
         fprintf(logFile, "Error > checkStatus: Erase or program error on "
                 "EndPoint 0x%04x\n", trbBuffer[i] & 0xffff);
-        return -1; /* Fatal Error */
+        return -1;              /* Fatal Error */
       }
     }
     if ((trbBuffer[i + 1] & 0x01) == 1) {
-      ret = -2;  /* One is busy */
+      ret = -2;                 /* One is busy */
     }
   }
-  
+
   return ret;
 }
 
 static int writeStatusRegister(uint16_t trb_address, uint8_t value)
 {
   int status;
-  
+
   /* Enable writing */
   if (sendCommand(trb_address, 0x06 << 24, 0) == -1) {
     fprintf(logFile, "Error > writeStatusRegister: write enable\n");
     return -1;
   }
-    
-  if (trb_register_write(trb_address, BlockRam, (uint32_t)value) == -1) {
+
+  if (trb_register_write(trb_address, BlockRam, (uint32_t) value) == -1) {
     fprintf(logFile, "Error > writeStatusRegister: TRBNet %s\n",
             trb_strerror());
     return -1;
@@ -301,16 +298,17 @@ static int writeStatusRegister(uint16_t trb_address, uint8_t value)
   if (sendCommand(trb_address, 0x01 << 24, 0) == -1) {
     fprintf(logFile, "Error > writeStatusRegister: sendCommand\n");
     return -1;
-  } 
-  
+  }
+
   /* Wait for not busy and check status */
-  while ((status = checkStatus(trb_address) == -2)) {}
-  
+  while ((status = checkStatus(trb_address)) == -2) {
+  }
+
   if (status != 0) {
     fprintf(logFile, "Error > writeStatusRegister: invalid status\n");
-    return -1;         
+    return -1;
   }
-  
+
   return 0;
 }
 
@@ -323,21 +321,21 @@ static int initTransfer(uint16_t trb_address)
   int i;
 
   /* Read HardwareIds from all Boards and validate (all must be the same) */
-  if ((status = 
-       trb_register_read(trb_address, HardwareId, trbBuffer, 
+  if ((status =
+       trb_register_read(trb_address, HardwareId, trbBuffer,
                          TRB_BUFFER_SIZE)) == -1) {
     fprintf(logFile, "Error > initTransfer, read HardwareIds: TRBNet %s\n",
             trb_strerror());
     return -1;
   }
-  
+
   flashType = FLASH_INVALID;
   for (i = 0; i < status; i += 2) {
     FlashType fType = FLASH_INVALID;
 
-    uint16_t hardwareId = (uint16_t)((trbBuffer[i + 1] >> 16) & 0xffff);
+    uint16_t hardwareId = (uint16_t) ((trbBuffer[i + 1] >> 16) & 0xffff);
     switch (hardwareId) {
-      
+
       /* Rich */
     case 0x3100:
       fType = FLASH_RICH_ADCM_V1;
@@ -349,7 +347,7 @@ static int initTransfer(uint16_t trb_address)
     case 0x0002:
       fType = FLASH_RICH_ADCM_V3;
       break;
-      
+
       /* MDC HUB */
     case 0x1210:
       fType = FLASH_MDC_HUB_V2_FPGA1234;
@@ -357,7 +355,7 @@ static int initTransfer(uint16_t trb_address)
     case 0x1250:
       fType = FLASH_MDC_HUB_V2_FPGA5;
       break;
-      
+
       /* MDC OEP */
     case 0x2200:
       fType = FLASH_MDC_OEP_V2;
@@ -366,7 +364,7 @@ static int initTransfer(uint16_t trb_address)
     case 0x1234:
       fType = FLASH_MDC_OEP_V3;
       break;
-      
+
       /* SHOWER */
     case 0x4210:
       fType = FLASH_SHOWER_ADDON_V2_FPGA1;
@@ -377,7 +375,7 @@ static int initTransfer(uint16_t trb_address)
     case 0x4230:
       fType = FLASH_SHOWER_ADDON_V2_FPGA3;
       break;
-      
+
       /* CTS */
     case 0x5100:
       fType = FLASH_CTS_FPGA1;
@@ -393,7 +391,7 @@ static int initTransfer(uint16_t trb_address)
               hardwareId, trbBuffer[i] & 0xffff);
       return -1;
     }
-        
+
     if (flashType == FLASH_INVALID) {
       flashType = fType;
       continue;
@@ -405,33 +403,32 @@ static int initTransfer(uint16_t trb_address)
       return -1;
     }
   }
-    
+
   /* Read ManIds from all Boards and validate (all must be the same as well) */
   trbcmd = 0x9f << 24;
   if (sendCommand(trb_address, trbcmd, 2) == -1) {
     return -1;
   }
-  
-  if ((status = 
-       trb_register_read(trb_address, BlockRam, trbBuffer, 
+
+  if ((status =
+       trb_register_read(trb_address, BlockRam, trbBuffer,
                          TRB_BUFFER_SIZE)) == -1) {
     fprintf(logFile, "Error > initTransfer, read ManIds: TRBNet %s\n",
             trb_strerror());
     return -1;
   }
-  
+
   if (status <= 0) {
     return -1;
   }
-  
+
   manId = 0;
   counter = 0;
-  for (i = 0; i < status; i += 2,counter++) {
+  for (i = 0; i < status; i += 2, counter++) {
     uint32_t tmpManId = (trbBuffer[i + 1] & 0x00ffffff);
-    
-    if (!((tmpManId == 0x01461f) || 
-          (tmpManId == 0x00471f) ||
-          (tmpManId == 0x1520c2))) {
+
+    if (!((tmpManId == 0x01461f) ||
+          (tmpManId == 0x00471f) || (tmpManId == 0x1520c2))) {
       fprintf(logFile, "Error > initTransfer: "
               "Unsupported ManId(s) 0x%04x on EndPoint 0x%04x\n",
               tmpManId, trbBuffer[i] & 0xffff);
@@ -443,36 +440,35 @@ static int initTransfer(uint16_t trb_address)
     }
     if (tmpManId != manId) {
       fprintf(logFile, "Error > initTransfer: ManId 0x%04x differs"
-              "on EndPoint 0x%04x\n",
-              manId, trbBuffer[i] & 0xffff);
+              "on EndPoint 0x%04x\n", manId, trbBuffer[i] & 0xffff);
       return -1;
     }
   }
-  
+
   /* Set NUM_PAGES */
   switch (manId) {
-  
+
   case 0x01461f:
     NUM_PAGES = 8192;
     break;
-  
+
   case 0x00471f:
     NUM_PAGES = 16384;
     break;
-    
+
   case 0x1520c2:
     NUM_PAGES = 8192;
     break;
-  
+
   default:
     abort();
   }
-      
+
   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)));
+    (uint8_t *) malloc(sizeof(uint8_t) * (PAGE_SIZE * (NUM_PAGES + 2)));
   if (imageBuffer == NULL) {
     abort();
   }
@@ -483,19 +479,21 @@ static int initTransfer(uint16_t trb_address)
   return 0;
 }
 
-static int readPage(uint16_t trb_address, uint32_t pageNumber, 
+static int readPage(uint16_t trb_address, uint32_t pageNumber,
                     uint32_t numBytes)
 {
-  uint32_ttemp = NULL;
-  uint32_tend = NULL;
+  uint32_t *temp = NULL;
+  uint32_t *end = NULL;
   unsigned int endPoint;
   uint16_t size = 0;
   uint32_t trbcmd;
   int status = -1;
-    
-  if (pageNumber >= NUM_PAGES) return -1;
-  if ((numBytes > PAGE_SIZE) || (numBytes == 0)) return -1;
-  
+
+  if (pageNumber >= NUM_PAGES)
+    return -1;
+  if ((numBytes > PAGE_SIZE) || (numBytes == 0))
+    return -1;
+
   trbcmd = (0x03 << 24) | ((pageNumber * PAGE_SIZE) & 0xffffff);
   if (sendCommand(trb_address, trbcmd, numBytes - 1) == -1) {
     return -1;
@@ -503,17 +501,16 @@ static int readPage(uint16_t trb_address, uint32_t pageNumber,
 
   size = (numBytes / 4) + (numBytes % 4 != 0 ? 1 : 0);
   if ((status = trb_register_read_mem(trb_address, BlockRam, 0, size,
-                                      trbBuffer, TRB_BUFFER_SIZE)) 
+                                      trbBuffer, TRB_BUFFER_SIZE))
       == -1) {
-    fprintf(logFile, "Error > readPage: TRBNet %s\n",
-            trb_strerror());
+    fprintf(logFile, "Error > readPage: TRBNet %s\n", trb_strerror());
     return -1;
   }
-  
+
   if (status <= 0) {
     return -1;
   }
-  
+
   /* Copy trbBuffer to pageBuffer and check content */
   endPoint = 0;
   temp = trbBuffer;
@@ -522,13 +519,13 @@ static int readPage(uint16_t trb_address, uint32_t pageNumber,
     unsigned int len = (*temp >> 16) & 0xffff;
     unsigned int address = *temp & 0xffff;
     unsigned int c;
-    
+
     if (len != size) {
       fprintf(logFile,
               "Error > readPage: Invalid len %d returned by endpoint 0x%04x\n",
               len, address);
-    } 
-    
+    }
+
     /* read one page */
     pageBufferAddress[endPoint] = address;
     for (c = 0; c < (len * 4); c++) {
@@ -540,23 +537,25 @@ static int readPage(uint16_t trb_address, uint32_t pageNumber,
     temp++;
     endPoint++;
   }
-    
+
   return endPoint;
 }
 
 
-static int writePage(uint16_t trb_address, uint32_t pageNumber, 
-                     const uint8_t* pageBuffer, uint32_t numBytes)
+static int writePage(uint16_t trb_address, uint32_t pageNumber,
+                     const uint8_t * pageBuffer, uint32_t numBytes)
 {
-  uint32_ttemp = NULL;
+  uint32_t *temp = NULL;
   uint16_t size = 0;
   uint32_t trbcmd;
   unsigned int c;
   int status = -1;
-  
-  if (pageNumber >= NUM_PAGES) return -1;
-  if ((numBytes > PAGE_SIZE) || (numBytes == 0)) return -1;
-  
+
+  if (pageNumber >= NUM_PAGES)
+    return -1;
+  if ((numBytes > PAGE_SIZE) || (numBytes == 0))
+    return -1;
+
   /* Copy pageBuffer to trbBuffer */
   temp = trbBuffer - 1;
   for (c = 0; c < numBytes; c++) {
@@ -566,53 +565,51 @@ static int writePage(uint16_t trb_address, uint32_t pageNumber,
     }
     *temp |= (pageBuffer[c] << ((c % 4) * 8));
   }
-  
+
   /* Transfer trbBuffer */
   size = temp - trbBuffer + 1;
   if ((status = trb_register_write_mem(trb_address, BlockRam, 0,
-                                       trbBuffer, size)) 
+                                       trbBuffer, size))
       == -1) {
-    fprintf(logFile, "Error > writePage: TRBNet %s\n",
-            trb_strerror());
+    fprintf(logFile, "Error > writePage: TRBNet %s\n", trb_strerror());
     return -1;
   }
+
   /* Enable writing */
   if (sendCommand(trb_address, 0x06 << 24, 0) == -1) {
     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(logFile, "Error > writePage: write page\n");
     return -1;
   }
-  
+
   /* Wait for not busy and check status */
-  while ((status = checkStatus(trb_address) == -2)) {
+  while ((status = checkStatus(trb_address)) == -2) {
     /* fprintf(logFile, "Wait pageProgram ...\n"); */
   }
-  
+
   if (status != 0) {
     fprintf(logFile, "Error > pageWrite: invalid status\n");
-    return -1;         
+    return -1;
   }
-  
+
   return 0;
 }
 
-typedef enum {
-  PMODE_PROGRAM,
-  PMODE_PROGRAM_FULL,
-  PMODE_VERIFY
-} PMode;
+typedef enum
+  {
+    PMODE_PROGRAM,
+    PMODE_PROGRAM_FULL,
+    PMODE_VERIFY
+  } PMode;
 
 static int programImageBuffer(uint16_t trb_address,
-                              unsigned int size, 
-                              PMode mode, 
-                              int infoPage)
+                              unsigned int size, PMode mode, int infoPage)
 {
   int bla = 0;
   unsigned int block;
@@ -622,91 +619,81 @@ static int programImageBuffer(uint16_t trb_address,
   unsigned int page;
   int bytesWritten = size;
   int tmp;
-  
+
   if ((mode != PMODE_VERIFY) && (yesToAll == 0)) {
     /* Be nice and ask before start flashing the roms */
     char c;
-    if ((flashType == FLASH_MDC_OEP_V2) || 
-        (flashType == FLASH_MDC_OEP_V3)) {
+    if ((flashType == FLASH_MDC_OEP_V2) || (flashType == FLASH_MDC_OEP_V3)) {
       if (mdcFlashSelect == 0) {
         fprintf(stdout,
                 "You decided to reprogram the FlashRom(s) #0 of "
                 "MDC_OEP, the so called 'GoldenImage'. "
                 "Do you really know what you're about to do [N,y]: ");
-      } 
+      }
     } else {
       fprintf(stdout,
               "You decided to reprogram the FlashRom(s) of "
               "%s, are you sure [N,y]: ", FlashTypeStr[flashType]);
     }
-    
+
     c = getc(stdin);
     if (!((c == 'Y') || (c == 'y'))) {
       fprintf(stderr, "\nAborting on user request\n");
       return -1;
     }
   }
-  
-  if (mode != PMODE_VERIFY) { 
+
+  if (mode != PMODE_VERIFY) {
     fprintf(stderr,
-            "Programming Endpoint(s) @ Address 0x%04x\n",
-            trb_address);
+            "Programming Endpoint(s) @ Address 0x%04x\n", trb_address);
     fprintf(stderr,
             "Symbols:\n"
             "  E: Erasing\n"
-            "  P: Programming\n"
-            "  @: Success\n"
-            "  .: Skipped\n\n");
+            "  P: Programming\n" "  @: Success\n" "  .: Skipped\n\n");
   } else {
-    fprintf(stderr,
-            "Verifying Endpoint(s) @ Address 0x%04x\n",
-            trb_address);
+    fprintf(stderr, "Verifying Endpoint(s) @ Address 0x%04x\n", trb_address);
     fprintf(stderr,
             "Symbols:\n"
             "  V: Verifying\n"
             "  X: Failed (see logfile 'trbflash.log' for details)\n"
-            "  @: Success\n"
-            "  .: Skipped\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 ((mode != PMODE_VERIFY) && (manId == 0x1520c2)) {
     /* 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;
     }
-  } 
-    
+  }
+
   for (block = 0; (block < NUM_BLOCKS); block++) {
     int error = 0;
-    int writeInfoPage = 
-      (block == NUM_BLOCKS - 1) && (infoPage == 1) ? 1 : 0;
+    int writeInfoPage = (block == NUM_BLOCKS - 1) && (infoPage == 1) ? 1 : 0;
     if (block % 16 == 0) {
-      fprintf(stderr, "\n%x      ", block / 16); 
+      fprintf(stderr, "\n%x      ", block / 16);
     }
     fprintf(stderr, ".\b");
-    
+
     if (!(bytesWritten > 0) && (writeInfoPage == 0)) {
       fprintf(stderr, ". ");
       continue;
     }
-    
-    if ((mode != PMODE_VERIFY) && 
+
+    if ((mode != PMODE_VERIFY) &&
         ((bytesWritten > 0) || (writeInfoPage == 1))) {
-      if ((manId == 0x01461f) || 
-          (manId == 0x00471f)) {
+      if ((manId == 0x01461f) || (manId == 0x00471f)) {
         /* Enable writing */
         if (sendCommand(trb_address, 0x06 << 24, 0) == -1) {
           fprintf(stderr, "\nError > program: write enable, aborting\n");
           return -1;
         }
-        
+
         /* Unprotect sector */
-        if (sendCommand(trb_address, 0x39 << 24 | (BLOCK_SIZE * block), 3) 
+        if (sendCommand(trb_address, 0x39 << 24 | (BLOCK_SIZE * block), 3)
             == -1) {
           fprintf(stderr,
                   "\nError > program: unprotect sector #%d, aborting\n",
@@ -714,41 +701,41 @@ static int programImageBuffer(uint16_t trb_address,
           return -1;
         }
       }
-      
+
       /* Enable writing */
       if (sendCommand(trb_address, 0x06 << 24, 0) == -1) {
         fprintf(stderr, "\nError > program: write enable, aborting\n");
         return -1;
       }
-    
+
       /* Erase block */
       fprintf(stderr, "E\b");
-      if (sendCommand(trb_address, 0xd8 << 24 | (BLOCK_SIZE * block), 3) 
+      if (sendCommand(trb_address, 0xd8 << 24 | (BLOCK_SIZE * block), 3)
           == -1) {
         fprintf(stderr, "\nError > program: erase block #%d, aborting\n",
                 block);
         return -1;
       }
-      
+
       /* Wait for not busy and check status */
-      while ((status = checkStatus(trb_address) == -2)) {}
-      
+      while ((status = checkStatus(trb_address)) == -2) {
+      }
+
       if (status != 0) {
         fprintf(stderr, "\nError > program: invalid status, aborting\n");
-        return -1;         
+        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;        
+           (i < (BLOCK_SIZE / PAGE_SIZE)) && (tmp > 0); i++, page++) {
+        int bytes = tmp < PAGE_SIZE ? tmp : PAGE_SIZE;
         tmp -= bytes;
         bytesWritten = tmp;
-        
-        status = writePage(trb_address, page, 
+
+        status = writePage(trb_address, page,
                            imageBuffer + page * PAGE_SIZE, bytes);
         if (status == -1) {
           fprintf(stderr,
@@ -760,8 +747,8 @@ static int programImageBuffer(uint16_t trb_address,
 
       if (writeInfoPage == 1) {
         /* Write Info-Page */
-        status = writePage(trb_address, (NUM_PAGES - 1), 
-                           imageBuffer + (NUM_PAGES - 1) * 
+        status = writePage(trb_address, (NUM_PAGES - 1),
+                           imageBuffer + (NUM_PAGES - 1) *
                            PAGE_SIZE, PAGE_SIZE);
         if (status == -1) {
           fprintf(stderr,
@@ -770,17 +757,16 @@ static int programImageBuffer(uint16_t trb_address,
           return -1;
         }
       }
-      
-      if ((manId == 0x01461f) || 
-          (manId == 0x00471f)) {
+
+      if ((manId == 0x01461f) || (manId == 0x00471f)) {
         /* Enable writing */
         if (sendCommand(trb_address, 0x06 << 24, 0x00) == -1) {
           fprintf(stderr, "\nError > program: write enable, aborting\n");
           return -1;
         }
-        
+
         /* Protect sector */
-        if (sendCommand(trb_address, 0x36 << 24 | (BLOCK_SIZE * block), 3) 
+        if (sendCommand(trb_address, 0x36 << 24 | (BLOCK_SIZE * block), 3)
             == -1) {
           fprintf(stderr, "\nError > program: protect sector #%d, aborting\n",
                   block);
@@ -793,83 +779,80 @@ static int programImageBuffer(uint16_t trb_address,
       tmp = bytesWritten;
       bla++;
       for (i = 0, page = (block * BLOCK_SIZE) / PAGE_SIZE;
-           (i < (BLOCK_SIZE / PAGE_SIZE)) && (tmp > 0);
-           i++, page++) {
+           (i < (BLOCK_SIZE / PAGE_SIZE)) && (tmp > 0); i++, page++) {
         unsigned int endPoint;
         unsigned int c;
-        int bytes = tmp < PAGE_SIZE ? tmp : PAGE_SIZE;        
+        int bytes = tmp < PAGE_SIZE ? tmp : PAGE_SIZE;
         tmp -= bytes;
         bytesWritten = tmp;
-        
+
         if ((status = readPage(trb_address, page, bytes)) == -1) {
           fprintf(stderr, "\nError > 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;
               errorCtr++;
-              fprintf(logFile, 
+              fprintf(logFile,
                       "verify failed page #%d, byte #%d "
                       "(exp: 0x%02x rec: 0x%02x), EndPoint: 0x%04x\n",
-                      page, c, 
-                      imageBuffer[page * PAGE_SIZE + c], 
-                      pageBuffer[endPoint][c],
-                      pageBufferAddress[endPoint]);
+                      page, c,
+                      imageBuffer[page * PAGE_SIZE + c],
+                      pageBuffer[endPoint][c], pageBufferAddress[endPoint]);
             }
           }
         }
       }
-      
+
       /* Verify Info-Page */
       if (writeInfoPage == 1) {
         unsigned int endPoint;
         unsigned int c;
-        int bytes = tmp < PAGE_SIZE ? tmp : PAGE_SIZE;        
+        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] != 
+            if (pageBuffer[endPoint][c] !=
                 imageBuffer[(NUM_PAGES - 1) * PAGE_SIZE + c]) {
               error = -1;
               errorCtr++;
-              fprintf(logFile, 
+              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]);
+                      c,
+                      imageBuffer[(NUM_PAGES - 1) * PAGE_SIZE + c],
+                      pageBuffer[endPoint][c], pageBufferAddress[endPoint]);
             }
           }
         }
       }
     }
-    
+
     if (error == 0) {
       fprintf(stderr, "@ ");
     } else {
       fprintf(stderr, "X ");
     }
   }
-  
+
   if ((mode != PMODE_VERIFY) && (manId == 0x1520c2)) {
     /* Protect all Sectors  i.e. write 0x3c to statusRegister */
     if (writeStatusRegister(trb_address, 0x00) == -1) {
       fprintf(stderr, "\nError > program: protect all sectors, aborting\n");
       return -1;
     }
-  } 
-  
+  }
+
   if (errorCtr == 0) {
     fprintf(stderr, "\n\nSuccess\n\n");
   } else {
@@ -883,63 +866,64 @@ static int programImageBuffer(uint16_t trb_address,
 
 
 static int readImageFile(const char *imageFileName)
-{ 
+{
   FILE *imageFile = NULL;
   int imageSize;
   unsigned int i;
-  
+
   /* Cleanup Buffer */
-  for (i = 0; i < (PAGE_SIZE * (NUM_PAGES + 2)); i++) imageBuffer[i] = 0;
-  
+  for (i = 0; i < (PAGE_SIZE * (NUM_PAGES + 2)); i++)
+    imageBuffer[i] = 0;
+
   /* Read in image */
-  imageFile = fopen(imageFileName, "r");    
+  imageFile = fopen(imageFileName, "r");
   if (imageFile == NULL) {
     fprintf(logFile,
-            "Error > readImageFile: Could not open ImageFile %s: %s\n", 
+            "Error > readImageFile: Could not open ImageFile %s: %s\n",
             imageFileName, strerror(errno));
     return -1;
   }
-  
+
   imageSize = 0;
   do {
-    imageSize += fread((void*)(imageBuffer + imageSize), sizeof(uint8_t),
+    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); 
+            "Error > readImageFile: Imagefile '%s' is too large (%d bytes)\n",
+            imageFileName, imageSize);
     return -1;
   }
-  
+
   return imageSize;
 }
 
 static int prepareImageBuffer()
 {
-  charstrId = "INVALID";
+  char *strId = "INVALID";
   int found;
   unsigned int end;
   unsigned int i;
 
   /* Verify imageFile Id */
   switch (flashType) {
-    
+
   case FLASH_RICH_ADCM_V1:
     strId = "adcmv1";
     break;
-    
+
   case FLASH_RICH_ADCM_V2:
     strId = "adcmv2";
     break;
-    
+
   case FLASH_RICH_ADCM_V3:
     strId = "adcmv3";
     break;
-    
+
   case FLASH_MDC_HUB_V2_FPGA1234:
     strId = "mdchub_fpga1234";
     break;
@@ -961,18 +945,18 @@ static int prepareImageBuffer()
   case FLASH_SHOWER_ADDON_V2_FPGA3:
     strId = "shower_fpga3";
     break;
-  
+
   case FLASH_CTS_FPGA1:
     strId = "cts_fpga1";
   case FLASH_CTS_FPGA2:
     strId = "cts_fpga2";
     break;
-    
+
   default:
     abort();
     break;
   }
-  
+
   /* Verify imageFile Id */
   found = 0;
   for (i = 0; i < 2 * PAGE_SIZE; i++) {
@@ -981,24 +965,24 @@ static int prepareImageBuffer()
       break;
     }
   }
-  
+
   if ((skipFirmwareIdCheck == 0) && (found == 0)) {
     fprintf(logFile, "Error > prepareImageBuffer: "
             "invalid Firmware-Id of Image-File, should be: %s\n", strId);
     return -1;
   }
-    
+
   /* Overwrite Header with 0xff */
   end = 0;
   for (i = 0; i < 2 * PAGE_SIZE; i++) {
-    if (strncmp((char*)(imageBuffer + i), "Bitstream CRC: 0x", 17) == 0) {
+    if (strncmp((char *)(imageBuffer + i), "Bitstream CRC: 0x", 17) == 0) {
       end = i + 22;
       break;
     }
   }
   if (end == 0) {
     fprintf(logFile, "Error > prepareImageBuffer: invalid Firmware, "
-            "CRC not found\n"); 
+            "CRC not found\n");
   }
   for (i = 0; i < end; i++) {
     imageBuffer[i] = 0xff;
@@ -1007,62 +991,64 @@ static int prepareImageBuffer()
   return 0;
 }
 
-static int createInfoPage(const char* fileName, const char* userString)
+static int createInfoPage(const char *fileName, const char *userString)
 {
-  char* buffer = (char*)(imageBuffer + (NUM_PAGES - 1) * PAGE_SIZE);
+  char *buffer = (char *)(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));  
+            strerror(errno));
     return -1;
   }
-  snprintf(buffer + 0, 63, "NAME: %s", basename((char*)fileName)); 
+  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);
+  fprintf(stderr, "%s\n", buffer + 64);
   fprintf(stderr, "%s\n", buffer + 96);
-  
+
   return 0;
 }
 
-static int openLog() {
+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", 
+    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, 
+  fprintf(logFile,
           "\n\n------------ %s ----------------------------------\n\n", date);
 
   return 0;
 }
 
-static int selectMdcFlashRom(uint16_t trb_address, uint8_t number) {
+static int selectMdcFlashRom(uint16_t trb_address, uint8_t number)
+{
   /* Select MDC-FlashRom */
-  if (trb_register_write(trb_address, MDCFlashRomSelect, 
-                         (uint32_t)number) == -1) {
+  if (trb_register_write(trb_address, MDCFlashRomSelect,
+                         (uint32_t) number) == -1) {
     fprintf(logFile, "\nError > selcetMdcFlashRom: TRBNet %s\n",
             trb_strerror());
     return -1;
   }
-  
+
   return 0;
 }
 
@@ -1097,11 +1083,11 @@ void usage(const char *progName)
          "dump one flash-memory page to stdout\n");
 }
 
-int main(int argc, char ** argv)
+int main(int argc, char **argv)
 {
   unsigned int i;
   char userInfoStr[256] = "";
-  
+
   trb_debug = 0;
   logFile = stderr;
   mdcFlashSelect = 1;
@@ -1123,7 +1109,7 @@ int main(int argc, char ** argv)
       break;
     case 'g':
       mdcFlashSelect = 0;
-      break;  
+      break;
     case 'd':
       trb_debug = strtoul(optarg, NULL, 0);
       break;
@@ -1152,63 +1138,64 @@ int main(int argc, char ** argv)
     usage(basename(argv[0]));
     exit(EXIT_FAILURE);
   }
+
   if (init_ports() == -1) {
     trb_error("Init_Ports");
     exit(EXIT_FAILURE);
   }
-  
+
   atexit(atexit0);
 
   /* Allocate memory for buffers */
-  
+
   /* Buffer holding the TRBNet packages, e.g. use by trb_read_register */
-  trbBuffer = (uint32_t*)malloc(sizeof(uint32_t) * TRB_BUFFER_SIZE);
+  trbBuffer = (uint32_t *) malloc(sizeof(uint32_t) * TRB_BUFFER_SIZE);
   if (trbBuffer == NULL) {
     abort();
   }
-  
+
   /* Buffer holding the pages of the endpoints */
-  pageBuffer = (uint8_t**)malloc(sizeof(uint8_t*) * NUM_ENDPOINTS);
+  pageBuffer = (uint8_t **) malloc(sizeof(uint8_t *) * NUM_ENDPOINTS);
   if (pageBuffer == NULL) {
     abort();
   }
   for (i = 0; i < NUM_ENDPOINTS; i++) {
-    pageBuffer[i] = (uint8_t*)malloc(sizeof(uint8_t) * PAGE_SIZE);
-    if (pageBuffer[i] == NULL) abort();
+    pageBuffer[i] = (uint8_t *) malloc(sizeof(uint8_t) * PAGE_SIZE);
+    if (pageBuffer[i] == NULL)
+      abort();
   }
-  
+
   /* Buffer holding the corresponding TRBAddresses */
-  pageBufferAddress = (uint16_t*)malloc(sizeof(uint16_t) * NUM_ENDPOINTS);
+  pageBufferAddress = (uint16_t *) malloc(sizeof(uint16_t) * NUM_ENDPOINTS);
   if (pageBufferAddress == NULL) {
     abort();
   }
-  
+
   /* Execute command */
   if (strcmp(argv[optind], "program") == 0) {
-    
+
     /*********************************************************/
     /* Program Image                                         */
     /*********************************************************/
-    
+
     uint16_t trb_address;
-    charimageFileName = NULL;
+    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 (initTransfer(trb_address) == -1) {
       fprintf(stderr, "InitTransfer failed, aborting\n");
       exit(EXIT_FAILURE);
     }
-    
+
     /* Read imageFile to imageBuffer */
     if ((size = readImageFile(imageFileName)) == -1) {
       exit(EXIT_FAILURE);
@@ -1218,31 +1205,29 @@ int main(int argc, char ** argv)
               size, PAGE_SIZE * (NUM_PAGES - 1));
       exit(EXIT_FAILURE);
     }
-    
+
     /* Validate Id and prepare imageBuffer */
     if (prepareImageBuffer() == -1) {
-      exit(EXIT_FAILURE); 
+      exit(EXIT_FAILURE);
     }
-    
+
     /* Create InfoPage */
     createInfoPage(imageFileName, userInfoStr);
-    
+
     /* In case of MDC: select FlashRom */
-    if ((flashType == FLASH_MDC_OEP_V2) || 
-        (flashType == FLASH_MDC_OEP_V3)) {
+    if ((flashType == FLASH_MDC_OEP_V2) || (flashType == FLASH_MDC_OEP_V3)) {
       if (selectMdcFlashRom(trb_address, mdcFlashSelect) == -1) {
         exit(EXIT_FAILURE);
       }
     }
-    
+
     /* Open LogFile */
     if (openLog() == -1) {
       exit(EXIT_FAILURE);
     }
-    
-    fprintf(stderr, "Start programming ImageFile '%s'\n",
-            imageFileName);
-    
+
+    fprintf(stderr, "Start programming ImageFile '%s'\n", imageFileName);
+
     if (programImageBuffer(trb_address, size, PMODE_PROGRAM, 1) == -1) {
       exit(EXIT_FAILURE);
     }
@@ -1252,31 +1237,31 @@ int main(int argc, char ** argv)
         exit(EXIT_FAILURE);
       }
     }
-    
+
   } else if (strcmp(argv[optind], "verify") == 0) {
-    
+
     /*********************************************************/
     /* Verify Image                                          */
     /*********************************************************/
-    
+
     uint16_t trb_address;
-    charimageFileName = NULL;
+    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 (initTransfer(trb_address) == -1) {
       fprintf(stderr, "InitTransfer failed, aborting\n");
       exit(EXIT_FAILURE);
     }
-    
+
     /* Read imageFile to imageBuffer */
     if ((size = readImageFile(imageFileName)) == -1) {
       exit(EXIT_FAILURE);
@@ -1289,12 +1274,11 @@ int main(int argc, char ** argv)
 
     /* Validate Id and prepare imageBuffer */
     if (prepareImageBuffer() == -1) {
-      exit(EXIT_FAILURE); 
+      exit(EXIT_FAILURE);
     }
-    
+
     /* In case of MDC: select FlashRom */
-    if ((flashType == FLASH_MDC_OEP_V2) || 
-        (flashType == FLASH_MDC_OEP_V3)) {
+    if ((flashType == FLASH_MDC_OEP_V2) || (flashType == FLASH_MDC_OEP_V3)) {
       if (selectMdcFlashRom(trb_address, mdcFlashSelect) == -1) {
         exit(EXIT_FAILURE);
       }
@@ -1304,46 +1288,44 @@ int main(int argc, char ** argv)
     if (openLog() == -1) {
       exit(EXIT_FAILURE);
     }
-    
-    fprintf(stderr, "Start verifying ImageFile '%s'\n",
-            imageFileName);
-    
+
+    fprintf(stderr, "Start verifying ImageFile '%s'\n", imageFileName);
+
     if (programImageBuffer(trb_address, size, PMODE_VERIFY, 0) == -1) {
       exit(EXIT_FAILURE);
     }
-    
+
   } else if (strcmp(argv[optind], "backup") == 0) {
-    
+
     /*********************************************************/
     /* Backup Image                                          */
     /*********************************************************/
-    
+
     uint16_t trb_address;
-    charimageFileName;
-    FILEimageFile = NULL;
+    char *imageFileName;
+    FILE *imageFile = NULL;
     unsigned int page;
-    
+
     if (argc - optind != 3) {
       usage(basename(argv[0]));
       exit(EXIT_FAILURE);
     }
     trb_address = strtoul(argv[optind + 1], NULL, 0);
     imageFileName = argv[optind + 2];
-    
+
     if (trb_address >= 0xff00) {
       fprintf(stderr,
               "Broadcast addresses are not supported by this command\n");
       exit(EXIT_FAILURE);
     }
-    
+
     /* In case of MDC: select FlashRom */
-    if ((flashType == FLASH_MDC_OEP_V2) || 
-        (flashType == FLASH_MDC_OEP_V3)) {
+    if ((flashType == FLASH_MDC_OEP_V2) || (flashType == FLASH_MDC_OEP_V3)) {
       if (selectMdcFlashRom(trb_address, mdcFlashSelect) == -1) {
         exit(EXIT_FAILURE);
       }
     }
-    
+
     if (initTransfer(trb_address) == -1) {
       fprintf(stderr, "InitTransfer failed, aborting\n");
       exit(EXIT_FAILURE);
@@ -1352,15 +1334,15 @@ int main(int argc, char ** argv)
     if (checkStatus(trb_address) < 0) {
       exit(EXIT_FAILURE);
     }
-    
+
     /* Write Pages to file */
     fprintf(stderr,
             "Dumping entire flashrom of EndPoint 0x%04x to file '%s' ",
             trb_address, imageFileName);
-    
-    imageFile = fopen(imageFileName, "w+");    
+
+    imageFile = fopen(imageFileName, "w+");
     if (imageFile == NULL) {
-      fprintf(stderr, "Could not open Imagefile %s: %s\n", 
+      fprintf(stderr, "Could not open Imagefile %s: %s\n",
               imageFileName, strerror(errno));
       exit(EXIT_FAILURE);
     }
@@ -1371,57 +1353,56 @@ int main(int argc, char ** argv)
         fclose(imageFile);
         exit(EXIT_FAILURE);
       }
-      if (fwrite((void*)pageBuffer[0], PAGE_SIZE, 1, imageFile) != 1) {
-        fprintf(stderr, 
+      if (fwrite((void *)pageBuffer[0], PAGE_SIZE, 1, imageFile) != 1) {
+        fprintf(stderr,
                 "\nError writing Page #%d to file, aborting..\n", page);
         fclose(imageFile);
         exit(EXIT_FAILURE);
       }
-      
+
       fprintf(stderr, "%s", busy[page % 4]);
     }
     fprintf(stderr, " \nDone\n");
-    
+
     fclose(imageFile);
-    
+
   } else if (strcmp(argv[optind], "restore") == 0) {
-    
+
     /*********************************************************/
     /* Restore Image                                         */
     /*********************************************************/
-        
+
     uint16_t trb_address;
-    charimageFileName;
+    char *imageFileName;
     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 (initTransfer(trb_address) == -1) {
       fprintf(stderr, "InitTransfer failed, aborting\n");
       exit(EXIT_FAILURE);
     }
-    
+
     /* Read imageFile to imageBuffer */
     if ((size = readImageFile(imageFileName)) == -1) {
       exit(EXIT_FAILURE);
     }
-    
+
     if (size != PAGE_SIZE * NUM_PAGES) {
       fprintf(stderr, "Invalid ImageFile %s, aborting\n", imageFileName);
       exit(EXIT_FAILURE);
     }
-    
+
     /* Check ImageBuffer ??? */
 
     /* In case of MDC: select FlashRom */
-    if ((flashType == FLASH_MDC_OEP_V2) || 
-        (flashType == FLASH_MDC_OEP_V3)) {
+    if ((flashType == FLASH_MDC_OEP_V2) || (flashType == FLASH_MDC_OEP_V3)) {
       if (selectMdcFlashRom(trb_address, mdcFlashSelect) == -1) {
         exit(EXIT_FAILURE);
       }
@@ -1431,36 +1412,35 @@ int main(int argc, char ** argv)
     if (openLog() == -1) {
       exit(EXIT_FAILURE);
     }
-    
-    fprintf(stderr, "Start restoring ImageFile '%s'\n",
-            imageFileName);
-    
+
+    fprintf(stderr, "Start restoring ImageFile '%s'\n", imageFileName);
+
     if (programImageBuffer(trb_address, size, PMODE_PROGRAM_FULL, 0) == -1) {
       exit(EXIT_FAILURE);
     }
-    
+
     if (skipVerify == 0) {
       if (programImageBuffer(trb_address, size, PMODE_VERIFY, 0) == -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, "InitTransfer failed, aborting\n");
@@ -1468,8 +1448,7 @@ int main(int argc, char ** argv)
     }
 
     /* In case of MDC: select FlashRom */
-    if ((flashType == FLASH_MDC_OEP_V2) || 
-        (flashType == FLASH_MDC_OEP_V3)) {
+    if ((flashType == FLASH_MDC_OEP_V2) || (flashType == FLASH_MDC_OEP_V3)) {
       if (selectMdcFlashRom(trb_address, mdcFlashSelect) == -1) {
         exit(EXIT_FAILURE);
       }
@@ -1486,39 +1465,38 @@ int main(int argc, char ** argv)
       pageBuffer[i][255] = '\0';
       fprintf(stdout, "\nEndPoint: 0x%04x   InfoPage\n",
               pageBufferAddress[i]);
-      if (
-          (strncmp((char*)(pageBuffer[i] + 0), "NAME:", 5) != 0) ||
-          (strncmp((char*)(pageBuffer[i] + 64), "DATE:", 5) != 0) ||
-          (strncmp((char*)(pageBuffer[i] + 96), "USER:", 5) != 0)
+      if ((strncmp((char *)(pageBuffer[i] + 0), "NAME:", 5) != 0) ||
+          (strncmp((char *)(pageBuffer[i] + 64), "DATE:", 5) != 0) ||
+          (strncmp((char *)(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]);
+      fprintf(stdout, "%s\n", pageBuffer[i] + 64);
       fprintf(stdout, "%s\n", pageBuffer[i] + 96);
     }
     fprintf(stdout, "\n");
-          
+
   } else if (strcmp(argv[optind], "dumppage") == 0) {
-    
+
     /*********************************************************/
     /* Read and dump a page                                  */
     /*********************************************************/
-    
+
     uint16_t trb_address;
     uint32_t pageNumber;
     unsigned int i;
     int status;
-    
+
     if (argc - optind != 3) {
       usage(basename(argv[0]));
       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, "InitTransfer failed, aborting\n");
@@ -1526,8 +1504,7 @@ int main(int argc, char ** argv)
     }
 
     /* In case of MDC: select FlashRom */
-    if ((flashType == FLASH_MDC_OEP_V2) || 
-        (flashType == FLASH_MDC_OEP_V3)) {
+    if ((flashType == FLASH_MDC_OEP_V2) || (flashType == FLASH_MDC_OEP_V3)) {
       if (selectMdcFlashRom(trb_address, mdcFlashSelect) == -1) {
         exit(EXIT_FAILURE);
       }
@@ -1543,14 +1520,14 @@ int main(int argc, char ** argv)
       char cc[2] = " \0";
       int c;
       fprintf(stdout, "\nEndPoint: 0x%04x   Page #%d\n",
-              pageBufferAddress[i], pageNumber); 
+              pageBufferAddress[i], pageNumber);
       for (c = 0; c < PAGE_SIZE; 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 
+        cc[0] = isprint((char)pageBuffer[i][c]) == 0
           ? '.' : (char)pageBuffer[i][c];
         strncat(text, cc, 32);
         if ((c % 16) == 15) {
@@ -1559,13 +1536,13 @@ int main(int argc, char ** argv)
       }
     }
     fprintf(stdout, "\n");
-  
+
   } else {
-    
+
     /* Invalid command */
     usage(basename(argv[0]));
     exit(EXIT_FAILURE);
   }
-  
+
   exit(EXIT_SUCCESS);
 }