AlcapDAQ  1
cvupgrade.c
Go to the documentation of this file.
1 /************************************************************************
2 
3  CAEN SpA - Viareggio
4  www.caen.it
5 
6  Program: cvupgrade
7  Date: 15/02/2010
8  Author: CAEN Computing Division (support.computing@caen.it)
9 
10  ------------------------------------------------------------------------
11  Description
12  ------------------------------------------------------------------------
13  This program allows to upgrade the firmware of any CAEN module that
14  supports the firmware download through the USB/CONET cable.(except for the
15  bridges V1718 and V2718 that use the program 'CAENVMEUpgrade').
16  The firmware upgrade consists in writing the configuration file (Altera
17  Raw Binary Format) into the flash memory of the board. Usually the flash
18  memory contains two images of the configuration file, called 'standard'
19  and 'backup'. The 'standard' image only will normally be overwritten ;
20  the new firmware will be loaded by the FPGA after the first power cycle.
21  If an error occurs while upgrading, it may happen that the FPGA is not
22  properly configured and the board does not respond to the VME access.
23  In this case you can restore the old firmware (i.e. the 'backup' image)
24  just moving the relevant jumper from STD to BKP and power cycling the
25  board. Now you can retry to upgrade the 'standard' image.
26  Warning: never upgrade the 'backup' image until you are sure that the
27  'standard' works properly.
28  This program reads some parameters that define the type of the board
29  being upgraded from a file called CVupgrade_params.txt that must be
30  in the same directory of CVupgrade. There is one CVupgrade_params file
31  for each type of board that can be upgraded.
32 
33  ------------------------------------------------------------------------
34 
35  ------------------------------------------------------------------------
36  Portability
37  ------------------------------------------------------------------------
38  This program is ANSI C and can be compilated on any platform, except
39  for the functions that allow to initialize and access the VME bus.
40  For VME Boards, if CAEN's VME bridges (both V1718 and V2718) are used as VME masters,
41  a proper installation of the CAENComm library DLL on PC is the only requirement.
42  If a different VME Bridge/CPU is used, the CAENComm function calls MUST be reimplemented.
43 
44 *************************************************************************/
45 /*------------------------------------------------------------------------
46  Modification history:
47  ------------------------------------------------------------------------
48  Version | Author | Date | Changes made
49  ------------------------------------------------------------------------
50  1.0 | CTIN | 01.02.2008 | initial version.
51  1.1 | LCOL | 29.05.2008 | Updated WriteFlashPage to account for
52  | | | a longer flash erase times
53  | | | (increase from 20 to 40 ms).
54  1.2 | LCOL | 25.06.2008 | Added flash enable polarity parameter.
55  1.3 | CTIN | 26.01.2009 | Extended compatibility to 16, 32 and 64
56  | | | Mbit flash size;
57  | | | Replaced fixed wait time (page erasing)
58  | | | with busy polling;
59  | | | Add -link and -bdnum command line options
60  2.0 | ALUK | 03.10.2009 | Add the CAENComm as reference library for the
61  | | | low level access.
62  2.1 | ALUK | 14.02.2010 | Add Keyinfo and KeyWrite functionalities
63  | | |(only Digitizer boards)
64  2.2 | NDA | 15.04.2010 | Added optional command line parameter: modelname
65  | | | Added model name (if defined) to the KeyInfo filename
66  | | | Changed KeyInfo filename from KeyInfo### to BoardInfo###
67  | | | Added main switch -License: this will print (default) or delete ( -delete option)
68  | | | the license data
69  2.3 | NDA | 29.07.2010 | Add -Key main switch to set the DPP key file without have to upgrade the firmware
70  2.4 | ALUK | 15.07.2012 | Add -fwrel main switch to get the firmware release of the board
71  ------------------------------------------------------------------------
72 
73  ------------------------------------------------------------------------*/
74 
75 #define REVISION "2.4"
76 
77 #include <stdio.h>
78 #include <stdlib.h>
79 #include <malloc.h>
80 #include <string.h>
81 
82 #include "flash.h"
83 
84 #include "CFASegment.h"
85 
86 typedef struct UpgradeParams {
87  unsigned int Type ;
88  unsigned int Link ;
89  unsigned int BdNum ;
90  unsigned long BaseAddress;
91  unsigned int FirstPageStd; // First page of the Standard image
92  unsigned int FirstPageBck; // First page of the Backup image
93  uint64_t key;
94  unsigned int image ;
95  unsigned int noverify ;
96  unsigned int verifyonly;
97  unsigned int delete ;
98  unsigned int cfa ; // CFA mode
99  char ConfFile[1000];
100  char Models[1000];
101  char modelName[50];
102  char family[50];
103  char filepath[1000];
104  unsigned int ROCFwm;
105  unsigned int AMCFwm;
107 
108 
109 static void Usage() {
110  printf("Syntax: cvUpgrade ConfFile ConnType[USB|OPTLINK] [ConfigOptions][DeviceOptions]\n");
111  printf("Syntax: cvUpgrade -KeyInfo ConnType[USB|OPTLINK] [DeviceOptions]\n");
112  printf("Syntax: cvUpgrade -License ConnType[USB|OPTLINK] [LicenseOptions]\n");
113  printf("Syntax: cvUpgrade -Key N ConnType[USB|OPTLINK] [DeviceOptions]\n\n");
114  printf("Syntax: cvUpgrade -fvrel ConnType[USB|OPTLINK] [DeviceOptions]\n\n");
115  printf("Description: write the file 'ConfFile' (Altera Raw Binary Format) into\n");
116  printf("the flash memory of board connected by the specified connection type OR\n");
117  printf("retrieve the keyinfo data from the board and store it on BoardInfo.dat file OR\n");
118  printf("retrieve or delete the license data from the board:\n");
119  printf(" to delete the license file you must specify -delete option\n\n");
120  printf("ConfigOptions:\n\n");
121  printf("-backup: write backup image\n\n");
122  printf("-key N: The key value to enable the DPP firmware\n\n");
123  printf("-no_verify: disable verify (don't read and compare the flash content)\n\n");
124  printf("-verify_only: read the flash content and compare with the ConfFile file\n\n");
125  printf("DeviceOptions:\n\n");
126  printf("-param filename: allow to specify the file that contain the parameters for the\n");
127  printf(" board that is being upgraded (default is cvUpgrade_params.txt)\n");
128  printf(" With CFA files this option is not required.\n\n");
129  printf("-rbf: specifies that the config file to be updated is in RBF format.\n");
130  printf("-link N: when using CONET, it is the optical link number to be used\n");
131  printf(" when using USB, it is the USB device number to be used (default is 0)\n\n");
132  printf("-bdnum N: select board number in a chainable link (V2718 only)\n\n");
133  printf("-VMEbaseaddress N: The base address of the VME slave board to be upgraded (Hex 32 bit)\n\n");
134  printf("-modelname ModelName: specify the model name (e.g. V1724B)\n\n");
135  printf("-filepath Folder: specify the folder where store the KeyInfo file\n\n");
136  printf("LicenseOptions:\n\n");
137  printf("-delete : deletes the license data\n");
138 }
139 
140 
141 //******************************************************************************
142 // ReadConfigurationROM
143 //******************************************************************************
144 static int ReadConfigurationROM(cvFlashAccess *Flash, CROM_MAP *crom)
145 {
146  unsigned char data[AT45_PAGE_SIZE];
147  int res = 0;
148 
149  res = ReadFlashPage(Flash, data, ROM_FLASH_PAGE);
150 
151  if (res == 0) {
152  crom->crom_chksum = (uint8_t)(data[0]);
153  crom->crom_chksum_len = (uint32_t) ((data[1] << 16) | (data[2] << 8) | data[3]);
154  crom->crom_const = (uint32_t) ((data[4] << 16) | (data[5] << 8) | data[6]);
155  crom->crom_c_code = (uint8_t)(data[7]);
156  crom->crom_r_code = (uint8_t)(data[8]);
157  crom->crom_OUI = (uint32_t) ((data[9] << 16) | (data[10] << 8) | data[11]);
158  crom->crom_version = (uint8_t)(data[12]);
159  crom->crom_board_id = (uint32_t) ((data[13] << 16) | (data[14] << 8) | data[15]);
160  crom->crom_revision = (uint32_t) ( (data[16] << 24) | (data[17] << 16) | (data[18] << 8) | data[19]);
161  crom->crom_serial = (uint16_t) ((data[32] << 8) | (data[33]));
162  crom->crom_VCXO_type = (uint8_t)(data[34]);
163  }
164  return res;
165 }
166 
167 
168 //******************************************************************************
169 // ReadLicense
170 //******************************************************************************
171 static int ReadLicense(cvFlashAccess *Flash, uint8_t licenseData[LICENSE_DATA_SIZE])
172 {
173  unsigned char data[AT45_PAGE_SIZE];
174  int res = 0;
175 
176  if( (res = ReadFlashPage(Flash, data, LICENSE_FLASH_PAGE))!= 0) {
177  return res;
178  }
179  memcpy( licenseData, data, LICENSE_DATA_SIZE);
180  return 0;
181 }
182 
183 
184 //******************************************************************************
185 // EraseLicense
186 //******************************************************************************
187 static int EraseLicense(cvFlashAccess *Flash)
188 {
189  unsigned char data[AT45_PAGE_SIZE];
190 
191  memset( data, 0xff, sizeof( data));
192  return WriteFlashPage(Flash, data, LICENSE_FLASH_PAGE);
193 }
194 
195 //******************************************************************************
196 // PrintCROM2File
197 //******************************************************************************
198 static void PrintCROM2File(FILE *fp, const CROM_MAP *map) {
199  fprintf(fp,"Checksum = %X\n", map->crom_chksum);
200  fprintf(fp,"Checksum Length = %X\n", map->crom_chksum_len);
201  fprintf(fp,"Constant field = %X\n", map->crom_const);
202  fprintf(fp,"C Code = %X\n", map->crom_c_code);
203  fprintf(fp,"R Code = %X\n", map->crom_r_code);
204  fprintf(fp,"OUI = %X\n", map->crom_OUI);
205  fprintf(fp,"Version = %X\n", map->crom_version);
206  fprintf(fp,"Board ID = %X\n", map->crom_board_id);
207  fprintf(fp,"PCB Revision = %d\n", map->crom_revision);
208  fprintf(fp,"Serial Number = %d\n", map->crom_serial);
209  fprintf(fp,"VCXO Type ID = %d\n", map->crom_VCXO_type);
210 }
211 
212 //******************************************************************************
213 // KeyInfo
214 //******************************************************************************
215 static int KeyInfo(cvUpgradeParams *Config, cvFlashAccess *Flash) {
216  int i;
217  unsigned char security_vme[AT45_IDREG_LENGTH];
218  // unsigned long vboard_base_address;
219  char KeyFilename[200];
220  FILE *cf = NULL;
221  int res;
222 
223  CROM_MAP rom;
224 
225  /* initialize the connection */
226  res = CAENComm_OpenDevice(Config->Type, Config->Link, Config->BdNum, Config->BaseAddress, &Flash->Handle);
227  if (res != CAENComm_Success) {
228  printf("Cannot open the Device!\n");
230  }
231 
232  if (ReadFlashSecurityReg(Flash, security_vme)) {
233  printf("Error while reading on-board flash memory!\n");
235  }
236  if (ReadConfigurationROM(Flash, &rom)) {
237  printf("Error while reading on-board flash memory!\n");
239  }
240  if( strlen( Config->modelName)) {
241  sprintf(KeyFilename, "%s/BoardInfo-%s-%d.dat", Config->filepath, Config->modelName, rom.crom_serial);
242  } else {
243  sprintf(KeyFilename, "%s/BoardInfo-%d.dat", Config->filepath, rom.crom_serial);
244  }
245  cf = fopen(KeyFilename,"w");
246  if(cf == 0) {
247  printf("Cannot open %s file! Exiting ...\n", KeyFilename);
249  }
250 
251  printf("Writing output file %s\n", KeyFilename);
252  for(i=0; i < 64; i++) {
253  fprintf(cf, "%02X", security_vme[64+i]);
254  fprintf(cf, (i < 63) ? ":" : "\n");
255  }
256  PrintCROM2File(cf, &rom);
257 
260 
261  printf("\n");
262  printf("Program exits successfully.\n");
263  fclose(cf);
264  return 0;
265 }
266 
267 static int GetFWRel(cvUpgradeParams *Config, cvFlashAccess *Flash) {
268  /* initialize the connection */
269  int ret,d32,i;
270  unsigned char x,y;
271  int chNum = 2;
272  uint16_t BuildHexCode,d16;
273  if( CAENComm_OpenDevice(Config->Type, Config->Link, Config->BdNum, Config->BaseAddress, &Flash->Handle)!= CAENComm_Success){
274  printf("Cannot open the Device!\n");
276  }
277 
278  if (strcmp(Config->Models,"DIGITIZERS") == 0) {
279  // DIGITIZERS
280  if ((ret = CAENComm_Read32(Flash->Handle,Config->ROCFwm, &d32)) != CAENComm_Success) {
281  printf("unable to find ROC FPGA release\n");
282  return -1;
283  }
284  y = (uint8_t) (d32 & 0xff);
285  x = (uint8_t) ((d32 & 0xff00)>> 8);
286  BuildHexCode = (uint16_t) ((d32 & 0xffff0000)>> 16);
287  printf("ROC FPGA Release is %02d.%02d - Build %04X\n",x,y, BuildHexCode);
288 
289  for (i = 0; i< chNum; i++) {
290  if ((ret = CAENComm_Read32(Flash->Handle, Config->AMCFwm | (i<<8), &d32)) == CAENComm_Success) {
291  if ( (uint16_t)((d32 & 0xffff0000)>> 16) != 0x0000 ) break; // HACK this is for x780 support! AMC Build must be != 0000
292  }
293  }
294  if (i == (int32_t)chNum) {
295  printf("unable to find AMC FPGA release\n");
296  return -1;
297  }
298  y = (uint8_t) (d32 & 0xff);
299  x = (uint8_t) ((d32 & 0xff00)>> 8);
300  BuildHexCode = (uint16_t) ((d32 & 0xffff0000)>> 16);
301  printf("AMC FPGA Release is %02d.%02d - Build %04X",x,y, BuildHexCode);
302  }
303  else {
304  if (strcmp(Config->Models,"V1190,V1290") == 0) {
305  if ((ret = CAENComm_Read16(Flash->Handle,Config->ROCFwm, &d16)) != CAENComm_Success) {
306  printf("unable to find firmware release\n");
307  return -1;
308  }
309  y = (uint8_t) (d16 & 0xff);
310  x = (uint8_t) ((d16 & 0xff00)>> 8);
311  printf("Firmware Release is %02d.%02d\n",x,y);
312  }
313  else {
314  if ((ret = CAENComm_Read32(Flash->Handle,Config->ROCFwm, &d32)) != CAENComm_Success) {
315  printf("unable to find firmware release\n");
316  return -1;
317  }
318  y = (uint8_t) (d32 & 0xff);
319  x = (uint8_t) ((d32 & 0xff00)>> 8);
320  printf("Firmware Release is %02d.%02d\n",x,y);
321  }
322  }
325 
326  printf("\n");
327  printf("Program exits successfully.\n");
328  return 0;
329 }
330 
331 //******************************************************************************
332 // License
333 //******************************************************************************
334 static int License(cvUpgradeParams *Config, cvFlashAccess *Flash) {
335 
336  /* initialize the connection */
337  if( CAENComm_OpenDevice(Config->Type, Config->Link, Config->BdNum, Config->BaseAddress, &Flash->Handle)!= CAENComm_Success){
338  printf("Cannot open the Device!\n");
340  }
341 
342  if( Config->delete) {
343  // delete the license data
344  if( EraseLicense( Flash)) {
345  printf("Error deleting license data! Exiting ...\n");
347  }
348  } else {
349  uint8_t licenseData[LICENSE_DATA_SIZE];
350  if( ReadLicense( Flash, licenseData)) {
351  printf("Error reading license data! Exiting ...\n");
353  }
354  // print the license data
355  // check if a valid license
356  printf("\n");
357  if( ( licenseData[0]!= 0x00) || ( licenseData[1]!= 0x04)) {
358  printf("No valid license found!");
359  } else {
360  int i;
361  for( i= 0; i< LICENSE_DATA_SIZE; i++) {
362  printf( "%02X ", licenseData[i]);
363  }
364  }
365  }
368 
369  printf("\n");
370  printf("Program exits successfully.\n");
371  return 0;
372 }
373 
374 //******************************************************************************
375 // WriteKey
376 //******************************************************************************
378 {
379  unsigned int err=0, res, handleToClose= 0;
380  unsigned char key_page[LICENSE_DATA_SIZE];
381  int k;
382 
383  if ((Config->key != 0) &&
384  ((!strncmp(Config->Models,"DIGITIZERS",1000)) ||
385  (!strncmp(Config->Models,"V1495",1000)))) {
386  if( Flash->Handle== CAENCOM_INVALID_FILE_HANDLE) {
387  res = CAENComm_OpenDevice(Config->Type, Config->Link, Config->BdNum, Config->BaseAddress, &Flash->Handle);
388  if (res != CAENComm_Success) {
389  printf("Cannot open the Device!\n");
391  }
392  handleToClose= 1;
393  }
394 
395  memset(key_page, 0, LICENSE_DATA_SIZE);
396  for (k = LICENSE_DATA_SIZE- 1; k > -1; k--) {
397  key_page[LICENSE_DATA_SIZE- 1- k] = (unsigned char) ((Config->key >> (k * 8)) & 0xFF);
398  }
399  WriteFlashPage(Flash, key_page, LICENSE_FLASH_PAGE);
400  if( handleToClose) {
402  Wait_ms(1000);
404  }
405  printf("\nKey written successfully\n");
406  }
407  return err;
408 }
409 
410 //******************************************************************************
411 // validateModel
412 //******************************************************************************
413 int validateModel(cvUpgradeParams *config, cvFlashAccess* flash, char** fwData, int* fwSize) {
414  FILE* fin = fopen(config->ConfFile, "rb");
415  while (!feof(fin)) {
416  int last;
417  CFASegmentPtr segm = newSegment(fin, &last);
418  if (segm == NULL) {
419  fclose(fin);
420  return -2;
421  }
422  if (getSegmentType(segm) == CFA_SEGMENT_TYPE_1) {
423  CFASegmentType1* t1s = toType1(segm);
424  int i, j;
425  for (i = 0; i < t1s->numModels; i++) {
426  for (j = 0; j<t1s->models[i]->numChecks; j++) {
427  uint32_t address = t1s->models[i]->registerChecks[j].address;
428  uint32_t value;
429  if (t1s->accessType == 2) CAENComm_Read16(flash->Handle, address, (uint16_t*)&value);
430  else CAENComm_Read32(flash->Handle, address, &value);
431  if (value != t1s->models[i]->registerChecks[j].value) break;
432  }
433  if (j == t1s->models[i]->numChecks) { /* this means that all the checks match */
434  flash->PageSize = t1s->flashPageSize;
435  flash->RegSize = t1s->accessType;
436  flash->Sel_Flash = t1s->flashCsAddress;
437  flash->RW_Flash = t1s->flashCwAddress;
438  flash->FlashEnable = t1s->flashEnLevel;
439  config->FirstPageStd = t1s->flashStdAddress;
440  config->FirstPageBck = t1s->flashBckAddress;
441 
442  *fwData = malloc(t1s->fwSizeBytes);
443  memcpy(*fwData, t1s->fwData, t1s->fwSizeBytes);
444  *fwSize = t1s->fwSizeBytes;
445 
446  sprintf(config->Models, "id %d", t1s->models[i]->modelId);
447 
448  deleteSegment(segm);
449  fclose(fin);
450  return 0;
451  }
452  }
453  }
454  deleteSegment(segm);
455  if (last) break;
456  }
457  fclose(fin);
458  return -1;
459 }
460 
461 
462 
463 //******************************************************************************
464 // cvUpgrade
465 //******************************************************************************
467 {
468  unsigned int i, j, page, pa, NumPages, err=0, done;
469  unsigned int CFsize;
470  unsigned char c, *CFdata = NULL;
471  unsigned char pdr[2048];
472  int res;
473 
474  FILE *cf;
475 
476  /* initialize the connection */
477  res = CAENComm_OpenDevice(Config->Type, Config->Link, Config->BdNum, Config->BaseAddress, &Flash->Handle);
478  if (res != CAENComm_Success) {
479  printf("Cannot open the Device!\n");
480  return(CvUpgrade_CAENCommError);
481  }
482 
483 
484  // ************************************************
485  // Open Binary Configuration File
486  // ************************************************
487  if (!Config->cfa) {
488  // open and read the configuration file
489  cf = fopen(Config->ConfFile,"rb");
490  if(cf == 0) {
491  printf("Can't open file %s\n",Config->ConfFile);
492  Usage();
495  }
496  // calculate the size
497  fseek (cf, 0, SEEK_END);
498  CFsize = ftell (cf);
499  fseek (cf, 0, SEEK_SET);
500  if ( (CFdata = (unsigned char *)malloc(CFsize)) == NULL ) {
501  printf("Can't allocate %d bytes\n",CFsize);
503  return(-3);
504  }
505 
506  for(i=0; i<CFsize; i++) {
507  // read one byte from file and mirror it (lsb becomes msb)
508  c = (unsigned char)fgetc(cf);
509  CFdata[i]=0;
510  for(j=0; j<8; j++)
511  CFdata[i] |= ( (c >> (7-j)) & 1) << j;
512  }
513  fclose(cf);
514 
515  } else {
516  printf("CFA Mode. Reading archive.\n");
517  if (validateModel(Config, Flash, &CFdata, &CFsize)) {
518  printf("Error: no compatible firmware found in archive!\n");
520  return -99;
521  }
522  printf("Compatible firmware found in archive.\n");
523 
524  for (i=0; i<CFsize; i++) {
525  char c = CFdata[i];
526  CFdata[i] = 0;
527  for (j=0; j<8; j++)
528  CFdata[i] |= ( (c >> (7-j)) & 1) << j;
529  }
530  }
531 
532 
533  NumPages = (CFsize % Flash->PageSize == 0) ? (CFsize / Flash->PageSize) : (CFsize / Flash->PageSize) + 1;
534 
535  printf("Board Types: %s\n", Config->Models);
536  if (Config->image == 0) {
537  if(!Config->verifyonly)
538  printf("Overwriting Standard image of the firmware with %s\n", Config->ConfFile);
539  else
540  printf("Verifying Standard image of the firmware with %s\n", Config->ConfFile);
541  pa = Config->FirstPageStd;
542  }
543  else {
544  if(!Config->verifyonly)
545  printf("Overwriting Backup image of the firmware with %s\n", Config->ConfFile);
546  else
547  printf("Verifying Backup image of the firmware with %s\n", Config->ConfFile);
548  pa = Config->FirstPageBck;
549  }
550 
551  printf("0%% Done\n");
552  done = 10;
553 
554  // ************************************************
555  // Start for loop
556  // ************************************************
557  for(page=0; page < NumPages; page++) {
558  if(!Config->verifyonly) {
559  // Write Page
560  if (WriteFlashPage(Flash, CFdata + page*Flash->PageSize, pa + page) < 0) {
561  printf("\nCommunication Error: the board at Base Address %08X does not respond\n", (unsigned int) (Config->BaseAddress));
562  err = 1;
563  break;
564  }
565  }
566 
567  if(!Config->noverify) {
568  // Read Page
569  if (ReadFlashPage(Flash, pdr, pa + page) < 0) {
570  printf("\nCommunication Error: the board at Base Address %08X does not respond\n", (unsigned int) (Config->BaseAddress));
571  err = 1;
572  break;
573  }
574  // Verify Page
575  for(i=0; (i<Flash->PageSize) && ((page*Flash->PageSize+i) < CFsize); i++) {
576  if(pdr[i] != CFdata[page*Flash->PageSize + i]) {
577  printf("\nFlash verify error (byte %d of page %d)!\n", i, pa + page);
578  if ((Config->image == 0) && !Config->verifyonly)
579  printf("The STD image can be corrupted! \nMove the jumper to Backup image and cycle the power\n");
580  err = 1;
581  break;
582  }
583  }
584  }
585  if (err)
586  break;
587 
588  if (page == (NumPages-1)) {
589  printf("100%% Done\n");
590  } else if ( page >= (NumPages*done/100) ) {
591  printf("%d%% Done\n", done);
592  done += 10;
593  }
594  } // end of for loop
595 
596  if(!err) {
597  if (Config->verifyonly) {
598  printf("\nFirmware verified successfully. Read %d bytes\n", CFsize);
599  } else {
600  printf("\nFirmware updated successfully. Written %d bytes\n", CFsize);
601  printf("The new firmware will be loaded after a power cycle\n");
602  }
603  }
604 
605  if (CFdata != NULL)
606  free(CFdata);
607 
608  if( !err) {
609  err= WriteKey( Config, Flash);
610  }
613  Wait_ms(1000);
614 
615  return err;
616 
617 }
618 
619 
620 //******************************************************************************
621 // MAIN
622 //******************************************************************************
623 int main(int argc,char *argv[])
624 {
625  int err=0;
626  int key = 0;
627  int license = 0;
628  int fwrel = 0;
629  int writeKey= 0;
630  cvUpgradeParams Params;
631  cvFlashAccess FlashInfo;
632  char ParamFile[1000] = "cvUpgrade_params.txt";
633  int argIndex= 1;
634 
635  FILE *bdf;
636 
637  printf("\n");
638  printf("********************************************************\n");
639  printf(" CAEN SpA - Front-End Division \n");
640  printf("--------------------------------------------------------\n");
641  printf(" CAEN Board Firmware Upgrade \n");
642  printf(" Version %s \n", REVISION);
643  printf("********************************************************\n\n");
644 
645  // Check command arguments (must be at least 2)
646  if (argc < 3) {
647  Usage();
648  return(-1);
649  }
650 
651  // Inizialize defaults
653  FlashInfo.FlashEnable = 0;
654  Params.Link = 0;
655  Params.BdNum = 0;
656  Params.image = 0; // Default = standard
657  Params.noverify = 0;
658  Params.verifyonly = 0;
659  Params.BaseAddress = 0;
660  Params.key = 0;
661  *Params.modelName= '\0';
662  Params.delete = 0;
663  Params.cfa = 1;
664 
665  strstr(Params.ConfFile, "cvUpgrade_params.txt");
666  if (strcmp(argv[argIndex],"-KeyInfo") == 0 ) {
667  key = 1;
668  } else if (strcmp(argv[argIndex],"-License") == 0 ) {
669  license = 1;
670  } else if (strcmp(argv[argIndex],"-Key") == 0 ) {
671  sscanf(argv[++argIndex], "%llx", &Params.key);
672  writeKey = 1;
673  } else if (strcmp(argv[argIndex],"-fwrel") == 0 ) {
674  fwrel = 1;
675  } else {
676  sprintf(Params.ConfFile, argv[argIndex]);
677  }
678  argIndex++;
679  if (strcmp(argv[argIndex],"USB") == 0) Params.Type = CAENComm_USB;
680  if (strcmp(argv[argIndex],"OPTLINK") == 0) Params.Type = CAENComm_PCI_OpticalLink;
681 // if (strcmp(argv[argIndex],"PCIE_OPTLINK") == 0) Params.Type = CAENComm_PCIE_OpticalLink;
682  argIndex++;
683  for (; argIndex<argc; argIndex++) {
684  if ( strcmp(argv[argIndex],"-backup") == 0 )
685  Params.image = 1;
686  if ( strcmp(argv[argIndex],"-no_verify") == 0 )
687  Params.noverify = 1;
688  if ( strcmp(argv[argIndex],"-verify_only") == 0 )
689  Params.verifyonly = 1;
690  if ( strcmp(argv[argIndex],"-param") == 0 )
691  sprintf(ParamFile, "%s", argv[++argIndex]);
692  if ( strcmp(argv[argIndex],"-rbf") == 0 )
693  Params.cfa = 0;
694  if ( strcmp(argv[argIndex],"-VMEbaseaddress") == 0 )
695  sscanf(argv[++argIndex], "%x", (unsigned int *) (&Params.BaseAddress));
696  if ( strcmp(argv[argIndex],"-key") == 0 )
697  sscanf(argv[++argIndex], "%llx", &Params.key);
698  if ( strcmp(argv[argIndex],"-link") == 0 )
699  Params.Link = atoi(argv[++argIndex]);
700  if ( strcmp(argv[argIndex],"-bdnum") == 0 )
701  Params.BdNum = atoi(argv[++argIndex]);
702  if ( strcmp(argv[argIndex],"-modelname") == 0 ) {
703  strncpy( Params.modelName, argv[++argIndex], sizeof( Params.modelName));
704  Params.modelName[ sizeof( Params.modelName)- 1]= '\0';
705  }
706  if ( strcmp(argv[argIndex],"-modelfamily") == 0 ) {
707  strncpy( Params.family, argv[++argIndex], sizeof( Params.family));
708  Params.family[ sizeof( Params.family)- 1]= '\0';
709  }
710  if ( strcmp(argv[argIndex],"-filepath") == 0 ) {
711  strncpy( Params.filepath, argv[++argIndex], sizeof( Params.filepath));
712  Params.filepath[ sizeof( Params.filepath)- 1]= '\0';
713  }
714  if ( strcmp(argv[argIndex],"-delete") == 0 )
715  Params.delete = 1;
716  }
717 
718  // open the board descriptor file
719  if ((!Params.cfa) || (fwrel) || (license) || (key) | (writeKey)) {
720  bdf = fopen(ParamFile,"r");
721  if(bdf == 0) {
722  printf("Can't open file %s\n", ParamFile);
723  Usage();
724  return (-2);
725  }
726 
727  fscanf(bdf, "%s", Params.Models);
728  fscanf(bdf, "%x", (unsigned int *) (&FlashInfo.Sel_Flash));
729  fscanf(bdf, "%x", (unsigned int *) (&FlashInfo.RW_Flash));
730  fscanf(bdf, "%d", &FlashInfo.RegSize);
731  fscanf(bdf, "%d", &FlashInfo.PageSize);
732  fscanf(bdf, "%d", &Params.FirstPageStd);
733  fscanf(bdf, "%d", &Params.FirstPageBck);
734  if (!feof(bdf)) {
735  int tmp;
736  if (fscanf(bdf, "%d", &tmp) > 0)
737  FlashInfo.FlashEnable = tmp;
738  }
739  fscanf(bdf, "%x", &Params.ROCFwm);
740  fscanf(bdf, "%x", &Params.AMCFwm);
741  fclose(bdf);
742  }
743 
744  // Call cvUpgrade function
745  //if (key == 0)
746  // err = cvUpgrade(&Params, &FlashInfo);
747  //else {
748  // if ((!strncmp(Params.Models,"DIGITIZERS",1000)) ||
749  // (!strncmp(Params.Models,"V1495",1000)) ) {
750  // err = KeyInfo(&Params, &FlashInfo);
751  // }
752  //}
753  if (key ) {
754  if ((!strncmp(Params.Models,"DIGITIZERS",1000)) ||
755  (!strncmp(Params.Models,"V1495",1000)) ) {
756  err = KeyInfo(&Params, &FlashInfo);
757  }
758  } else if (license ) {
759  if ((!strncmp(Params.Models,"DIGITIZERS",1000)) ||
760  (!strncmp(Params.Models,"V1495",1000)) ) {
761  err = License(&Params, &FlashInfo);
762  }
763  } else if (writeKey ) {
764  err = WriteKey(&Params, &FlashInfo);
765  } else if (fwrel ) {
766  err = GetFWRel(&Params, &FlashInfo);
767  } else {
768  err = cvUpgrade(&Params, &FlashInfo);
769  if (err) return err;
770  }
771  if (err)
772  return (CvUpgrade_GenericError);
773  else
774  return 0;
775 }
776 
777