00001
00002
00021 #include "edtinc.h"
00022 #include "edt_bitload.h"
00023 #include "pciload.h"
00024 #include <stdlib.h>
00025 #include <ctype.h>
00026
00027 #ifdef WIN32
00028 #include <errno.h>
00029 #endif
00030
00031
00032 static int xa_get_magic(u_char **ba);
00033 static int xf_get_magic(FILE *fp);
00034 static int xb_get_magic(u_char *buf);
00035
00036
00037 #define DEBUG1 EDTLIB_MSG_INFO_1
00038 #define DEBUG2 EDTLIB_MSG_INFO_2
00039
00040
00041 #define MGK_SIZE 13
00042
00043
00044 u_char XilinxMagicArray[MGK_SIZE] = {0x00, 0x09, 0x0f, 0xf0, 0x0f, 0xf0, 0x0f, 0xf0, 0x0f, 0xf0, 0x00, 0x00, 0x01 };
00045
00046
00047 u_char AlteraMagicArray[MGK_SIZE] = {0x00, 0x0e, 0x0d, 0x0f, 0x0a, 0x00, 0x0f, 0x0e, 0x08, 0x08, 0x0a, 0x00, 0x01 };
00048
00049
00050 u_char Altera2MagicArray[MGK_SIZE] = {0x00, 0x0e, 0x0d, 0x0f, 0x0a, 0x02, 0x0f, 0x0e, 0x08, 0x08, 0x0a, 0x00, 0x01 };
00051
00052
00053 #ifdef NO_FS
00054 #include "nofs_bitfiles.h"
00055 #else
00056 #define MAPBITARRAY(a,b,c) (b=NULL,c=0)
00057 #endif
00058
00063 void bfh_init(EdtBitfileHeader *bfh)
00064 {
00065 memset(bfh, 0, sizeof(EdtBitfileHeader));
00066 }
00067
00068
00069
00070
00071
00072
00073
00074
00075 void edt_bitfile_init(EdtBitfile *bfd)
00076
00077 {
00078 bfd->is_file = FALSE;
00079 bfd->filename = NULL;
00080 bfd->f = NULL_HANDLE;
00081
00082 bfd->buffer_allocated = 0;
00083 bfd->full_buffer_size = 0;
00084 bfd->full_buffer= NULL;
00085 bfd->data_size = 0;
00086 bfd->data_size = 0;
00087
00088 bfh_init(&bfd->hdr);
00089 }
00090
00091 int edt_bitfile_open_file(EdtBitfile *bfd, const char *name, u_char writing)
00092
00093 {
00094 if ((bfd->f = edt_open_datafile(NULL, name, writing, FALSE, FALSE)) !=
00095 NULL_HANDLE)
00096 {
00097 bfd->is_file = TRUE;
00098 bfd->filename = strdup(name);
00099 return 0;
00100 }
00101
00102 return -1;
00103 }
00104
00105 int edt_bitfile_close_file(EdtBitfile *bfd)
00106
00107 {
00108 if (bfd->f != NULL_HANDLE)
00109 {
00110 edt_close_datafile(bfd->f);
00111 bfd->f = NULL_HANDLE;
00112 bfd->is_file = FALSE;
00113
00114 }
00115
00116 return 0;
00117 }
00118
00119 void edt_bitfile_destroy(EdtBitfile *bfd)
00120
00121 {
00122 edt_bitfile_close_file(bfd);
00123 if (bfd->full_buffer)
00124 {
00125 free(bfd->full_buffer);
00126 bfd->full_buffer = NULL;
00127 bfd->buffer_allocated = 0;
00128 bfd->full_buffer_size = 0;
00129 }
00130 if (bfd->filename)
00131 {
00132 free(bfd->filename);
00133 bfd->filename = NULL;
00134 }
00135 }
00136
00137 int edt_bitfile_read(EdtBitfile *bfd, void *target, int length)
00138
00139 {
00140
00141 if (bfd->is_file)
00142 return (int) edt_read_datafile(bfd->f,target, length);
00143 else
00144 {
00145
00146 int len;
00147
00148 if (length + bfd->cur_index > bfd->full_buffer_size)
00149 {
00150 len = bfd->full_buffer_size - bfd->cur_index;
00151 }
00152 else
00153 len = length;
00154
00155 if (len)
00156 {
00157 memcpy(target, bfd->full_buffer + bfd->cur_index, len);
00158 bfd->cur_index += len;
00159 }
00160
00161 return len;
00162 }
00163 }
00164
00165 int edt_bitfile_seek(EdtBitfile *bfd, int offset)
00166
00167 {
00168 if (bfd->is_file)
00169 return (int) edt_file_seek(bfd->f, offset);
00170 else
00171 {
00172 if (offset >= 0 && offset <= (int) bfd->full_buffer_size)
00173 bfd->cur_index = offset;
00174
00175 return bfd->cur_index;
00176 }
00177 }
00178
00179 int edt_bitfile_tell(EdtBitfile *bfd)
00180
00181 {
00182 if (bfd->is_file)
00183 return (int) edt_get_file_position(bfd->f);
00184 else
00185 return bfd->cur_index;
00186 }
00187
00188 int edt_bitfile_size(EdtBitfile *bfd)
00189
00190 {
00191 if (bfd->is_file)
00192 {
00193 return (int) edt_get_file_size(bfd->f);
00194 }
00195
00196 return bfd->full_buffer_size;
00197 }
00198
00199
00200 int edt_bitfile_allocate(EdtBitfile *bfd, int size)
00201
00202 {
00203
00204 if (bfd->full_buffer)
00205 {
00206 if (bfd->buffer_allocated != size)
00207 {
00208 free(bfd->full_buffer);
00209 bfd->full_buffer = 0;
00210 bfd->buffer_allocated = 0;
00211 }
00212 else
00213 {
00214 return 0;
00215 }
00216 }
00217
00218 if (size)
00219 {
00220 bfd->full_buffer = (u_char *) calloc(size,1);
00221 if (bfd->full_buffer == NULL)
00222 {
00223 bfd->buffer_allocated = 0;
00224 return -1;
00225 }
00226 else
00227 bfd->buffer_allocated = size;
00228 }
00229
00230 return 0;
00231
00232
00233 }
00234
00235 int edt_bitfile_load_file(EdtBitfile *bfd, const char *name)
00236
00237 {
00238
00239 int rc;
00240 int size;
00241
00242
00243
00244 if ((rc = edt_bitfile_open_file(bfd, name, FALSE)) != 0)
00245 {
00246 edt_msg(EDT_MSG_FATAL, "edt_bitfile_load_file: Unable to open file %s\n",
00247 name);
00248
00249 return rc;
00250 }
00251
00252
00253 size = (int) edt_get_file_size(bfd->f);
00254
00255 if (edt_bitfile_allocate(bfd, size) != 0)
00256 {
00257 edt_msg(EDT_MSG_FATAL, "edt_bitfile_load_file: Unable to allocate %d bytes\n",
00258 size);
00259
00260 return -1;
00261
00262 }
00263
00264
00265
00266 if (edt_read_datafile(bfd->f, bfd->full_buffer, size) != size)
00267 {
00268 edt_msg(EDT_MSG_FATAL, "edt_bitfile_load_file: Unable to read all %d bytes\n",
00269 size);
00270
00271 return rc;
00272
00273 }
00274
00275 edt_get_bitfile_header(bfd, &bfd->hdr);
00276
00277 edt_bitfile_close_file(bfd);
00278
00279 bfd->full_buffer_size = size;
00280 bfd->data = bfd->full_buffer + bfd->hdr.data_start;
00281 bfd->data_size = bfd->hdr.dsize;
00282
00283 return 0;
00284 }
00285
00286 int
00287 edt_bitfile_load_array(EdtBitfile *bfd, u_char *data, int size)
00288
00289 {
00290 int rc = -1;
00291
00292 if (edt_bitfile_allocate(bfd, size) != 0)
00293 {
00294 edt_msg(EDT_MSG_FATAL, "edt_bitfile_load_file: Unable to allocate %d bytes\n",
00295 size);
00296
00297 return rc;
00298
00299 }
00300
00301
00302
00303 memcpy( bfd->full_buffer, data, size);
00304
00305 bfd->full_buffer_size = size;
00306
00307 bfd->is_file = FALSE;
00308
00309 return 0;
00310 }
00311
00312
00313
00314
00315
00316 static int
00317 edt_bitfile_get_magic(EdtBitfile *bp)
00318 {
00319
00320 u_char buf[MGK_SIZE+1];
00321 int rc;
00322
00323 if ((rc = edt_bitfile_read(bp, buf, MGK_SIZE)) != MGK_SIZE)
00324 {
00325 printf("Error reading %d magic bytes got %d\n", MGK_SIZE, rc);
00326 return -1;
00327 }
00328
00329 return xb_get_magic(buf);
00330
00331 }
00332
00333
00334
00335
00336
00337
00338
00339 static int
00340 xb_get_magic(u_char *buf)
00341 {
00342
00343 if (memcmp(buf, XilinxMagicArray, MGK_SIZE) == 0)
00344 return XilinxMagic;
00345
00346 if (memcmp(buf, AlteraMagicArray, MGK_SIZE) == 0)
00347 return AlteraMagic;
00348
00349 if (memcmp(buf, Altera2MagicArray, MGK_SIZE) == 0)
00350 return Altera2Magic;
00351
00352 return -1;
00353 }
00354
00355
00356 static int
00357 edt_bitfile_get_field_id(EdtBitfile *bp, u_char *fi)
00358 {
00359 edt_bitfile_read(bp, fi, 1);
00360 return 0;
00361 }
00362
00363 static int
00364 edt_bitfile_get_string(EdtBitfile *bp, u_char *str, u_int maxcount)
00365 {
00366 u_char tmpcount[2];
00367 u_short count;
00368
00369 edt_bitfile_read(bp, tmpcount, 2);
00370
00371 count = tmpcount[0] << 8 | tmpcount[1];
00372 if (count > maxcount)
00373 {
00374 edt_msg(DEBUG1, "invalid FPGA header string (count %d)\n", count);
00375 return -1;
00376 }
00377
00378 edt_bitfile_read(bp, str, count);
00379
00380 return 0;
00381 }
00382
00383 static int
00384 edt_bitfile_get_long_size(EdtBitfile *bp, u_int *size)
00385 {
00386 u_char tmpsize[4];
00387
00388 edt_bitfile_read(bp, tmpsize, 4);
00389
00390 *size = (tmpsize[0] << 24) | (tmpsize[1] << 16) | (tmpsize[2] << 8) | (tmpsize[3]);
00391
00392 return 0;
00393 }
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407 int
00408 edt_get_bitfile_header(EdtBitfile *bp,
00409 EdtBitfileHeader *bfh)
00410 {
00411 int i;
00412 int remaining;
00413 char *p;
00414 u_char tmpname[MAXPATH];
00415 u_char dmy[2];
00416
00417 edt_msg(DEBUG2, "edt_get_bitfile_header:\n");
00418
00419 bfh_init(bfh);
00420 edt_bitfile_seek(bp, 0);
00421
00422 if ((bfh->magic = edt_bitfile_get_magic(bp)) < 0)
00423 {
00424 edt_msg(DEBUG1, "Invalid FPGA magic bytes in header array\n");
00425 return -1;
00426 }
00427 else
00428 edt_msg(DEBUG2, "magic=%s (%d)\n",
00429 (bfh->magic == XilinxMagic)?"XILINX":
00430 (bfh->magic == AlteraMagic)?"ALTERA":
00431 (bfh->magic == Altera2Magic)?"ALTERA2":
00432 "UNKNOWN (huh?)", bfh->magic);
00433
00434 if ((edt_bitfile_get_field_id(bp, &bfh->fi[0]) != 0)
00435 || (edt_bitfile_get_string(bp, tmpname, sizeof(tmpname)) != 0)
00436 || (edt_bitfile_get_field_id(bp, &bfh->fi[1]) != 0)
00437 || (edt_bitfile_get_string(bp, bfh->id, sizeof(bfh->id)) != 0)
00438 || (edt_bitfile_get_field_id(bp, &bfh->fi[2]) != 0)
00439 || (edt_bitfile_get_string(bp, bfh->date, sizeof(bfh->date)) != 0)
00440 || (edt_bitfile_get_field_id(bp, &bfh->fi[3]) != 0)
00441 || (edt_bitfile_get_string(bp, bfh->time, sizeof(bfh->time)) != 0)
00442 || (edt_bitfile_get_field_id(bp, &bfh->fi[4]) != 0)
00443 || (edt_bitfile_get_long_size(bp, &bfh->dsize) != 0))
00444 {
00445 edt_msg(EDT_MSG_FATAL, "Error: bad FPGA header\n");
00446 return -1;
00447 }
00448
00449
00450 if (bitload_has_slashes((char *)tmpname))
00451 {
00452 edt_back_to_fwd((char *)tmpname);
00453 strcpy((char *)bfh->ncdname, strrchr((char *)tmpname, '/')+1);
00454 edt_msg(DEBUG2, "stripping off FPGA header path\n");
00455 }
00456 else strcpy((char *)bfh->ncdname, (char *)tmpname);
00457
00458
00459
00460
00461
00462 if ((p = strchr((char *)bfh->ncdname, ';')) != NULL)
00463 {
00464 edt_msg(DEBUG2, "FIXING: Xh.name %s => ", bfh->ncdname);
00465 *p = '\0';
00466 edt_msg(DEBUG2, "%s\n", bfh->ncdname);
00467 }
00468 if ((p = strchr((char *)bfh->id, ';')) != NULL)
00469 {
00470 edt_msg(DEBUG2, "FIXING: Xh.id %s => ", bfh->id);
00471 *p = '\0';
00472 edt_msg(DEBUG2, "%s\n",bfh->id);
00473 }
00474
00475 for (i=0; i < 5; i++)
00476 {
00477 if (bfh->fi[i] != 'a' + i)
00478 {
00479 edt_msg(EDT_MSG_FATAL, "invalid fpga field id: '%c' (0x%02x) s/b '%c' (0x%02x)\n", bfh->fi[i], bfh->fi[i], 'a'+i, 'a'+i);
00480 return -1;
00481 }
00482 }
00483
00484 edt_msg(DEBUG2, "%c) '%s' %c) '%s' %c) '%s' %c) '%s' %c) %d\n",
00485 bfh->fi[0], bfh->ncdname,
00486 bfh->fi[1], bfh->id,
00487 bfh->fi[2], bfh->date,
00488 bfh->fi[3], bfh->time,
00489 bfh->fi[4], bfh->dsize);
00490
00491
00492
00493
00494
00495
00496
00497
00498 if ((edt_bitfile_tell(bp) % 2) && ((bfh->magic == AlteraMagic) || (bfh->magic == Altera2Magic)))
00499 edt_bitfile_read(bp, dmy, 1);
00500
00501
00502 bfh->data_start = edt_bitfile_tell(bp);
00503
00504 edt_bitfile_read(bp, bfh->extra, BFH_EXTRASIZE);
00505
00506 remaining = edt_bitfile_size(bp) - bfh->data_start;
00507 bfh->filesize = edt_bitfile_size(bp);
00508
00509 if (bfh->dsize < (unsigned long)(remaining) && bfh->dsize + 12 < (unsigned long)(remaining) )
00510 edt_msg(EDT_MSG_WARNING,
00511 "Warning! dsize/file size mismatch (dsize %u, EOF @ +%u (may work anyway)\n",
00512 bfh->dsize, remaining);
00513 #if 0
00514 else if (bfh->dsize > (unsigned long)(remaining))
00515 edt_msg(EDT_MSG_FATAL,
00516 "Error! dsize/file size mismatch -- dsize %u but EOF at +%u\n",
00517 bfh->dsize, remaining);
00518 #endif
00519
00520 edt_bitfile_seek(bp, 0);
00521
00522 sprintf(bfh->promstr, "%s %s %s %s", bfh->ncdname, bfh->id, bfh->date, bfh->time );
00523
00524 return bfh->data_start;
00525 }
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539 int
00540 edt_bitfile_read_header(const char *bitpath, EdtBitfileHeader *bfh, char *header)
00541
00542 {
00543 int ret;
00544
00545 EdtBitfile bfd;
00546
00547 edt_bitfile_init(&bfd);
00548
00549 if (edt_bitfile_open_file(&bfd, bitpath, EDT_READ) != 0)
00550 {
00551 edt_msg_perror(DEBUG2, bitpath);
00552 return -1;
00553 }
00554
00555 ret = edt_get_bitfile_header(&bfd, bfh);
00556
00557 edt_bitfile_destroy(&bfd);
00558
00559 strncpy(bfh->filename, bitpath, sizeof(bfh->filename)-1);
00560 strncpy(header, bfh->promstr, sizeof(bfh->promstr)-1);
00561
00562 return ret;
00563 }
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578 int
00579 edt_get_x_file_header(const char *bitname, char *header, int *size)
00580
00581 {
00582 EdtBitfileHeader bfh;
00583
00584 int offset = edt_bitfile_read_header(bitname, &bfh, header);
00585
00586 *size = bfh.filesize;
00587
00588 return offset;
00589 }
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600 int
00601 edt_get_x_array_header(u_char *ba,
00602 char *header,
00603 int *size)
00604
00605 {
00606 EdtBitfile bfd;
00607 int ret;
00608
00609 edt_bitfile_init(&bfd);
00610
00611 bfd.full_buffer = ba;
00612
00613 bfd.full_buffer_size = *size;
00614
00615 bfd.cur_index = 0;
00616
00617 ret = edt_get_bitfile_header(&bfd, &bfd.hdr);
00618
00619 if (ret != -1)
00620 {
00621 strncpy(header, bfd.hdr.promstr, sizeof(bfd.hdr.promstr)-1);
00622 return 0;
00623 }
00624
00625 return -1;
00626
00627 }
00628
00629
00630
00631 EdtBoardFpgas board_chips[] =
00632 {
00633
00634 {
00635 PDV_ID,
00636 {"4005","4010", "4013"}
00637 },
00638 {
00639 PCD20_ID,
00640 {"4005","4010", "4013"}
00641 },
00642 {
00643 PCD40_ID,
00644 {"4005","4010", "4013"}
00645 },
00646 {
00647 PCD60_ID,
00648 {"4005","4010", "4013"}
00649 },
00650 {
00651 PDVK_ID,
00652 {"4005","4010", "4013"}
00653 },
00654 {
00655 PDV44_ID,
00656 {"4005","4010", "4013"}
00657 },
00658 {
00659 PDVAERO_ID,
00660 {"4005","4010", "4013"}
00661 },
00662 {
00663 PGP20_ID,
00664 {"4036", "4085"}
00665 },
00666 {
00667 PGP40_ID,
00668 {"4036", "4085"}
00669 },
00670 {
00671 PGP60_ID,
00672 {"4036", "4085"}
00673 },
00674 {
00675 PGP_THARAS_ID,
00676 {"4036", "4085"}
00677 },
00678 {
00679 PGP_ECL_ID,
00680 {"4036", "4085"}
00681 },
00682 {
00683 PCD_16_ID,
00684 {"4036", "4085"}
00685 },
00686 {
00687 PSS4_ID,
00688 {"XCV600E", "XCV1000E", "XCV2000E"}
00689 },
00690 {
00691 PSS16_ID,
00692 {"XCV600E", "XCV1000E", "XCV2000E"}
00693 },
00694 {
00695 PCDA_ID,
00696 {"XC2S100E", "XC2S600E"}
00697 },
00698 {
00699 PCDA16_ID,
00700 {"XC2S100E", "XC2S600E"}
00701 },
00702 {
00703 PDVA_ID,
00704 {"xc2s100e", "xc2s600e"}
00705 },
00706 {
00707 PDVCL2_ID,
00708 {"xc2s400e"}
00709 },
00710 {
00711 PDVFOX_ID,
00712 {"xc2s400e", "xc2vp2"}
00713 },
00714 {
00715 PCDFOX_ID,
00716 {"XC2S400E"}
00717 },
00718 {
00719 PGS4_ID,
00720 {"XC2VP50", "XC2VP70"}
00721 },
00722 {
00723 PGS16_ID,
00724 {"XC2VP50", "XC2VP70"}
00725 },
00726 {
00727 PE8LX16_ID,
00728 {"XC5VLX110T", "XC5VLX220T", "XC5VLX330T", "XC5VFX100T",
00729 "XC5VFX130T", "XC5VFX200T","XC5VSX240T"}
00730 },
00731 {
00732 PE8LX32_ID,
00733 {"XC5VLX110T", "XC5VLX220T", "XC5VLX330T", "XC5VFX100T",
00734 "XC5VFX130T", "XC5VFX200T","XC5VSX240T"}
00735 },
00736 {
00737 PE8G2V7_ID,
00738 {"XVIRTEX7_SOMETHING"}
00739 },
00740 {
00741 PE8LX16_LS_ID,
00742 {"XC5VLX110T", "XC5VLX220T", "XC5VLX330T", "XC5VFX100T",
00743 "XC5VFX130T", "XC5VFX200T","XC5VSX240T"}
00744 },
00745 {
00746 PE4AMC16_ID,
00747 {"XC6VLX240T", "XC6VLX365T", "XC6VLX550T"}
00748 },
00749 {
00750 0
00751 }
00752
00753 };
00754
00755
00756 EdtBoardFpgas mezz_chips[] = {
00757 {
00758 MEZZ_OCM,
00759 {"XC2VP4",0},
00760 {"XC3S200",0}
00761 },
00762 {
00763 MEZZ_OC192 ,
00764 {"XC4VLX40",0}
00765 },
00766 {
00767 MEZZ_SSE,
00768 {"XC2VP2",0}
00769 },
00770 {
00771 MEZZ_SRXL,
00772 {"XC3S1500",0}
00773 },
00774 {
00775 MEZZ_SRXL2,
00776 {"XC4VSX55",0}
00777 },
00778 {
00779 MEZZ_3X3G,
00780 {"XC2VP30",0}
00781 },
00782 {
00783 MEZZ_MSDV,
00784 {"XC5VLX30T",0}
00785 },
00786 {
00787 MEZZ_NET10G,
00788 {"XC5VLX110","XC5VLX220",0},
00789 {"XC3S100E",0}
00790 },
00791 {
00792 MEZZ_DDSP,
00793 {"XC5VFX30T",0},
00794 {"XC5VLX30T",0}
00795 },
00796 {
00797 MEZZ_SRXL2_IDM_LBM,
00798 {"XC4VSX55",0}
00799 },
00800 {
00801 MEZZ_SRXL2_IDM_IDM,
00802 {"XC4VSX55",0}
00803 },
00804 {
00805 MEZZ_SRXL2_IMM_IMM,
00806 {"XC4VSX55",0}
00807 },
00808 {
00809 MEZZ_SRXL2_IMM_LBM,
00810 {"XC4VSX55",0}
00811 },
00812 {
00813 MEZZ_SRXL2_IDM_IMM,
00814 {"XC4VSX55",0}
00815 },
00816 {
00817 MEZZ_DRX16,
00818 {"XC6VLX240T",0}
00819 },
00820 {
00821 MEZZ_OCM2P7G,
00822 {"XC6VLX240T",0}
00823 },
00824 {
00825 MEZZ_THREEP,
00826 {"XC6VLX240T","XC6VLX365T","XC6VSX475T", "XC6VSX315T", 0}
00827 },
00828 {
00829 MEZZ_V4ACL,
00830 {"XC4VLX160", NULL},
00831 {"XC4VLX160", NULL}
00832 },
00833 {
00834 MEZZ_UNKN_EXTBDID,
00835 {0}
00836 }
00837 };
00838
00839 char ** edt_lookup_fpgas(EdtBoardFpgas *fpgas, int id, int channel)
00840
00841 {
00842 int i;
00843
00844 for (i=0;fpgas[i].id;i++)
00845 {
00846 if (fpgas[i].id == id)
00847 return (channel)?
00848 fpgas[i].fpga_1:fpgas[i].fpga_0;
00849 }
00850
00851 return NULL;
00852 }
00853
00854
00855 char ** edt_lookup_ui_names(EdtDev *edt_p)
00856
00857 {
00858 return edt_lookup_fpgas(board_chips, edt_p->devid, 0);
00859 }
00860
00861 char ** edt_lookup_mezz_names(EdtDev *edt_p, int channel)
00862
00863 {
00864 return edt_lookup_fpgas(mezz_chips, edt_p->mezz.id, channel);
00865 }
00866
00872 int
00873 edt_get_sorted_fpga_names(EdtBoardFpgas *fpga_list,
00874 int id,
00875 int channel,
00876 char *fpga_hint,
00877 char **sorted)
00878 {
00879 int first_choice = 0;
00880 int i;
00881
00882 char ** possibles = edt_lookup_fpgas(fpga_list, id, channel);
00883
00884 if (possibles == NULL)
00885 {
00886 sorted[0] = NULL;
00887 return -1;
00888 }
00889
00890 for (i=0;possibles[i];i++)
00891 {
00892 sorted[i] = possibles[i];
00893 if (fpga_hint && !strcasecmp(fpga_hint, possibles[i]))
00894 first_choice = i;
00895 }
00896
00897
00898
00899 sorted[i] = NULL;
00900
00901 if (first_choice != 0)
00902 {
00903 char *tmp;
00904
00905 tmp = sorted[0];
00906 sorted[0] = sorted[first_choice];
00907 for (i=1;i<=first_choice;i++)
00908 {
00909 char *tmp2 = sorted[i];
00910 sorted[i] = tmp;
00911 tmp = tmp2;
00912 }
00913 }
00914
00915 return 0;
00916 }
00917
00918
00919
00920
00921
00922
00923
00924
00925 static int
00926 bf_get_possible_files(const char *basedir,
00927 const char *devdir,
00928 const char *fname,
00929 EdtBitfileList *bf,
00930 char **match_list
00931 )
00932 {
00933
00934 char dirpath[MAXPATH];
00935
00936 sprintf(dirpath, "%s%s%s", basedir, (devdir && (*devdir)) ? "/" : "", devdir);
00937
00938 if (match_list)
00939 {
00940 int i = 0;
00941
00942 while (match_list[i])
00943 {
00944 static char tmppath[MAXPATH];
00945
00946
00947
00948 sprintf(tmppath, "%s/%s/%s", dirpath, match_list[i], fname);
00949
00950 bf_check_and_add(bf, tmppath);
00951
00952 i++;
00953 }
00954 }
00955 else
00956 {
00957 edt_msg(EDT_MSG_FATAL, "No FPGA match...\n");
00958 return -1;
00959
00960 }
00961
00962 return 0;
00963 }
00964
00971 int edt_get_bitfile_list(const char *basedir,
00972 const char *devdir,
00973 const char *fname,
00974 EdtBoardFpgas *fpga_list,
00975 int id,
00976 int channel,
00977 EdtBitfileList *bf,
00978 char *fpga_hint)
00979
00980 {
00981 int fullpath;
00982
00983 fullpath = bitload_has_slashes(fname);
00984
00985 if (fullpath)
00986 {
00987 bf_check_and_add(bf, fname);
00988 }
00989 else
00990 {
00991 char tmppath[MAXPATH];
00992
00993 char * sorted[MAX_CHIPS_PER_ID];
00994
00995 if (id)
00996 {
00997 if (edt_get_sorted_fpga_names(fpga_list,
00998 id,
00999 channel,
01000 fpga_hint,
01001 sorted) != 0)
01002 {
01003 edt_msg(EDT_MSG_FATAL,
01004 "1: Unable to determine FPGA for id 0x%02x\n",
01005 id);
01006 return -1;
01007 }
01008 }
01009
01010
01011 sprintf(tmppath, "%s/%s", basedir, fname);
01012
01013 bf_check_and_add(bf, tmppath);
01014
01015 if (id)
01016 {
01017
01018
01019 bf_get_possible_files(basedir, devdir, fname, bf, sorted);
01020
01021
01022 sprintf(tmppath, "%s/bitfiles", basedir);
01023 edt_correct_slashes(tmppath);
01024 bf_get_possible_files(tmppath, devdir, fname, bf, sorted);
01025
01026 }
01027 }
01028
01029 return (bf->nbfiles)?0:-1;
01030 }
01031
01032
01033
01034
01035
01036
01037 void bf_init(EdtBitfileList *bf)
01038
01039 {
01040 bf->nbfiles = 0;
01041 bf->bitfiles = NULL;
01042
01043 }
01044
01045 void bf_destroy(EdtBitfileList *bf)
01046
01047 {
01048 if (bf->bitfiles)
01049 {
01050 free(bf->bitfiles);
01051 }
01052
01053 bf_init(bf);
01054 }
01055
01056 void bf_add_entry(EdtBitfileList *bf,
01057 EdtBitfileHeader *bfh)
01058
01059 {
01060 if (bf)
01061 {
01062 if (bf->bitfiles)
01063 bf->bitfiles = (EdtBitfileHeader *) realloc(bf->bitfiles,(bf->nbfiles + 1) *
01064 sizeof(EdtBitfileHeader));
01065 else
01066 bf->bitfiles = (EdtBitfileHeader *)calloc(1,sizeof(EdtBitfileHeader));
01067
01068 memcpy(&bf->bitfiles[bf->nbfiles],
01069 bfh, sizeof(EdtBitfileHeader));
01070
01071 bf->nbfiles++;
01072 }
01073 }
01074
01075 void bf_check_and_add(EdtBitfileList *bf, const char *fname)
01076
01077 {
01078 int found_bitfile;
01079 char header[256];
01080 EdtBitfileHeader hdr;
01081
01082 edt_msg(DEBUG2, "Checking existence of %s\n", fname);
01083
01084 found_bitfile = (edt_access_bitfile(fname, 0) == 0);
01085
01086 if (found_bitfile)
01087 {
01088 if (edt_bitfile_read_header(fname,&hdr,header) >= 0)
01089 {
01090 bf_add_entry(bf, &hdr);
01091 edt_msg(DEBUG2, "Adding %s\n", fname);
01092 }
01093 }
01094 }
01095
01096
01097 int bf_allocate_max_buffer(EdtBitfileList *bf, EdtBitfile *data)
01098
01099 {
01100 int i;
01101 u_int size = 0;
01102
01103 for (i=0;i<bf->nbfiles;i++)
01104 if (size < bf->bitfiles[i].filesize)
01105 size = bf->bitfiles[i].filesize;
01106
01107 return edt_bitfile_allocate(data, size);
01108 }
01109
01110 void
01111 ensure_bitfile_name(const char *name, char *bitname)
01112
01113 {
01114 if ((strlen(name) < 4)
01115 || (strcasecmp(&(name[strlen(name)-4]), ".bit") != 0))
01116 sprintf(bitname, "%s.bit", name);
01117 else strcpy(bitname, name);
01118
01119 }
01120
01121
01122 int
01123 edt_get_fpga_hint(EdtDev *edt_p, char *bd_xilinx)
01124
01125 {
01126 char esn[128];
01127 Edt_embinfo ei;
01128
01129 bd_xilinx[0] = 0;
01130
01131 edt_get_esn(edt_p, esn);
01132 if (edt_parse_esn(esn, &ei) == 0)
01133 {
01134
01135 if ((strcmp(&(ei.pn[8]), "00") == 0))
01136 sprintf(&(ei.pn[8]), "%02d", ei.rev);
01137 if (*ei.ifx)
01138 strcpy(bd_xilinx, ei.ifx);
01139 else if (edt_find_xpn(ei.pn, bd_xilinx))
01140 edt_msg(DEBUG2, "matching xilinx found: pn <%s> xilinx <%s>\n", ei.pn, bd_xilinx);
01141 else edt_msg(DEBUG2, "no matching xilinx found: for pn <%s>\n", ei.pn);
01142 }
01143 else edt_msg(DEBUG2, "missing or invalid pn/xilinx, can't xref (esn <%s>)\n", esn);
01144
01145
01146
01147 return bd_xilinx[0]? 0: -1;
01148 }
01149
01150
01151
01152
01162 int edt_oc192_rev2_fname_hack(EdtDev *edt_p, const char *bitname, char *hacked)
01163 {
01164 char work[256];
01165 int rev_id;
01166
01167 edt_get_full_board_id(edt_p, NULL, &rev_id, NULL);
01168
01169
01170
01171
01172 if (rev_id > 1)
01173 {
01174 size_t len = strlen(bitname);
01175
01176 if (len < 4 || strcasecmp(bitname + len - 4, ".bit"))
01177 {
01178 if (bitname[len-1] != '2')
01179 sprintf(work,"%s2",bitname);
01180 else
01181 strcpy(work, bitname);
01182 }
01183 else
01184 {
01185 if (bitname[len-5] != '0' + 2)
01186 {
01187 if (hacked != bitname)
01188 strcpy(hacked,bitname);
01189
01190 hacked[len-4] = 0;
01191 sprintf(work,"%s2.bit",hacked);
01192 }
01193 else
01194 strcpy(work,bitname);
01195 }
01196 }
01197 else
01198 strcpy(work, bitname);
01199
01200 strcpy(hacked,work);
01201
01202 return 0;
01203 }
01204
01205 int
01206 edt_bitload_select_fox_file(EdtDev *edt_p,
01207 char *rbtfile)
01208
01209 {
01210 char name[MAX_STRING+1];
01211 int pci_channels;
01212 char bitname[128];
01213 size_t len;
01214
01215 edt_flash_get_fname(edt_p, name);
01216
01217 edt_msg(DEBUG1, "Looking at board pci id = %s\n", name);
01218
01219 if (!strcmp(name, "dvtlk1"))
01220 {
01221 pci_channels = 1;
01222 }
01223 else if (!strcmp(name, "dvtlk4"))
01224 {
01225 pci_channels = 4;
01226 }
01227 else
01228 {
01229 edt_msg(EDT_MSG_FATAL, "DVFOX pci bitfile <%s> not dvtlk4 or dvtlk4\n", name);
01230 return -1;
01231 }
01232
01233
01234 strcpy(bitname, rbtfile);
01235 edt_msg(DEBUG1, "Expand bitfile name %s -> ", bitname);
01236 len = strlen(bitname);
01237 if ((len >= 4) && (strcasecmp(&bitname[len-4], ".bit") == 0))
01238 {
01239 bitname[len-4] = '\0';
01240
01241 sprintf(rbtfile,"%s%d.bit", bitname,pci_channels);
01242
01243
01244 }
01245 edt_msg(DEBUG1, "%s\n", rbtfile);
01246
01247 return 0;
01248 }
01249
01250
01256 int
01257 edt_access_bitfile(const char *path, int perm)
01258
01259 {
01260 int found_bitfile = 0;
01261 int found_compressed = 0 ;
01262 static char tmppath[MAXPATH];
01263 static char ztmppath[MAXPATH];
01264 static char cmd[MAXPATH];
01265
01266 strcpy(tmppath, path);
01267
01268 edt_correct_slashes(tmppath);
01269
01270 if (edt_access(tmppath, perm) == 0)
01271 found_bitfile = 1 ;
01272
01273 else
01274 {
01275 strcpy(ztmppath, tmppath);
01276 strcat(ztmppath, ".Z");
01277
01278 if (edt_access(ztmppath, perm) == 0)
01279 {
01280 sprintf(cmd, "uncompress %s", tmppath) ;
01281 edt_msg(DEBUG1, "%s\n", cmd);
01282 edt_system(cmd) ;
01283 found_compressed = 1 ;
01284 }
01285
01286 if (!found_compressed)
01287 {
01288 strcpy(ztmppath, tmppath);
01289 strcat(ztmppath, ".gz");
01290 if (edt_access(ztmppath, perm) == 0)
01291 {
01292 sprintf(cmd, "gunzip %s", tmppath) ;
01293 edt_msg(DEBUG1, "%s\n", cmd);
01294 edt_system(cmd) ;
01295 found_compressed = 1 ;
01296 }
01297 }
01298
01299 if (!found_compressed)
01300 {
01301 strcpy(ztmppath, tmppath);
01302 strcat(ztmppath, ".zip");
01303 if (edt_access(ztmppath, perm) == 0)
01304 {
01305
01306 size_t i;
01307 i = strlen(ztmppath)-1;
01308
01309 while (i && ztmppath[i] != '/' && ztmppath[i] != '\\')
01310 i--;
01311
01312 if (i)
01313 {
01314 char *fname;
01315 char ch = ztmppath[i];
01316 ztmppath[i] = 0;
01317 fname = ztmppath + i + 1;
01318
01319 sprintf(cmd,"cd %s ; unzip %s",
01320 ztmppath, fname);
01321
01322 ztmppath[i] = ch;
01323
01324 }
01325 else
01326 sprintf(cmd, "unzip %s", ztmppath);
01327
01328 edt_correct_slashes(tmppath);
01329 edt_msg(DEBUG1, "%s\n", cmd);
01330 edt_system(cmd) ;
01331 edt_msg(DEBUG1, "rm %s\n", ztmppath) ;
01332 unlink(ztmppath) ;
01333 found_compressed = 1 ;
01334 }
01335 }
01336
01337 if (found_compressed && edt_access(tmppath, perm) == 0)
01338 found_bitfile = 1 ;
01339 }
01340
01341
01342
01343 return (found_bitfile) ? 0 : -1;
01344 }
01345
01346
01347 int
01348 bitload_has_slashes(const char *name)
01349 {
01350 const char *p = name;
01351
01352 while (*p)
01353 {
01354 if ((*p == '/') || (*p == '\\'))
01355 return 1;
01356 ++p;
01357 }
01358 return 0;
01359 }
01360
01361
01362
01363
01364
01365
01366
01367 const char *
01368 edt_bitload_basedir(EdtDev *edt_p, const char *in, char *out)
01369
01370 {
01371 if (in)
01372 return in;
01373 else
01374 {
01375 const char * home = edt_home_dir(edt_p);
01376
01377 switch (edt_p->devtype)
01378
01379 {
01380 case pcd:
01381
01382 if (out)
01383 sprintf(out, "%s", home);
01384
01385 break;
01386
01387 case pdv:
01388
01389 if (out)
01390 sprintf(out, "%s/camera_config", home);
01391 break;
01392
01393 }
01394
01395 return (out)?out:home;
01396
01397 }
01398 }
01399
01400
01401 void
01402 edt_bitload_devid_to_bitdir(int id, char *devdir)
01403 {
01404 switch(id)
01405 {
01406 case PDV44_ID:
01407 case PDVK_ID:
01408 strcpy(devdir, "dvk");
01409 break;
01410
01411 case PDV_ID:
01412 case PGP_RGB_ID:
01413 strcpy(devdir, "dv");
01414 break;
01415
01416 case PDVA_ID:
01417 case PDVA16_ID:
01418 strcpy(devdir, "dva");
01419 break;
01420
01421 case PDVFOX_ID:
01422 strcpy(devdir, "dvfox");
01423 break;
01424
01425 case PDVFCI_AIAG_ID:
01426 case PDVFCI_USPS_ID:
01427 strcpy(devdir, "dvfci");
01428 break;
01429
01430 case PDVAERO_ID:
01431 strcpy(devdir, "dvaero");
01432 break;
01433
01434 case PDVCL2_ID:
01435 strcpy(devdir, "dvcl2");
01436 break;
01437
01438 default:
01439 devdir[0] = '\0';
01440 }
01441 }
01460 int
01461 edt_bitload(EdtDev *edt_p,
01462 const char *indir,
01463 const char *name,
01464 int flags,
01465 int skip)
01466 {
01467
01468 int nofs = 0, ovr=0;
01469 int i;
01470
01471 int fullpath;
01472
01473 int do_sleep = 0;
01474 int do_readback = 0;
01475
01476 char devdir[MAXPATH];
01477 char bitpath[MAXPATH];
01478 char basework[MAXPATH];
01479
01480 const char *basedir;
01481
01482 EdtBitfileList boardfiles;
01483
01484 int channel = 0;
01485 int rc = 1;
01486 char *hint = NULL;
01487 char fpga[32];
01488
01489 u_char *ba;
01490
01491
01492 if ( ID_HAS_COMBINED_FPGA(edt_p->devid) )
01493
01494 return 0;
01495
01496
01497 if (flags & BITLOAD_FLAGS_NOFS)
01498 nofs = 1;
01499 if (flags & BITLOAD_FLAGS_OVR)
01500 ovr = 1;
01501 if (flags & BITLOAD_FLAGS_SLEEP)
01502 do_sleep = 1;
01503 if (flags & BITLOAD_FLAGS_READBACK)
01504 do_readback = 1;
01505
01506 edt_ioctl(edt_p, EDTS_PROG_READBACK, &do_readback);
01507
01508 basedir = edt_bitload_basedir(edt_p, indir, basework);
01509
01510 #ifdef PCD
01511
01512 if (flags & (BITLOAD_FLAGS_MEZZANINE | BITLOAD_FLAGS_OCM | BITLOAD_FLAGS_SRXL))
01513 {
01514 if (flags & BITLOAD_FLAGS_CH1)
01515 channel = 1;
01516
01517 return edt_load_mezzfile(edt_p, basedir, name, flags, skip, channel);
01518 }
01519
01520 #endif
01521
01522
01523
01524
01525
01526 ensure_bitfile_name(name, bitpath);
01527
01528
01529
01530
01531 if (edt_p->devid == PDVFOX_ID)
01532 {
01533
01534 if (!strcmp(bitpath,"aiag.bit") ||
01535 !strcmp(bitpath,"aiagcl.bit"))
01536 if (edt_bitload_select_fox_file(edt_p,
01537 bitpath) == -1)
01538 return -1;
01539 }
01540
01541
01542 edt_get_fpga_hint(edt_p, fpga);
01543
01544 if (fpga[0] != 0)
01545 hint = fpga;
01546
01547 if (nofs)
01548 {
01549 char nofsname[MAXPATH];
01550 int size;
01551 int found_one = FALSE;
01552
01553 char * sorted[5];
01554
01555 int which = 0;
01556
01557 if (edt_get_sorted_fpga_names(board_chips,
01558 edt_p->devid,
01559 0,
01560 hint,
01561 sorted) != 0)
01562 {
01563 edt_msg(EDT_MSG_FATAL,
01564 "2: Unable to determine FPGA for id 0x%02x\n",
01565 edt_p->devid);
01566 return -1;
01567 }
01568
01569 which = 0;
01570
01571 while (sorted[which])
01572 {
01573 sprintf(nofsname, "%s_%s", sorted[which], bitpath);
01574
01575 {
01576 int len = strlen(nofsname);
01577 if ((len >= 4) && (strcasecmp(&nofsname[len-4], ".bit") == 0))
01578 nofsname[len-4] = '\0';
01579 }
01580
01581 MAPBITARRAY(nofsname, ba, size);
01582
01583 if (ba != NULL)
01584 {
01585
01586 found_one = TRUE;
01587
01588 edt_msg(DEBUG2, "edt_bitload(basedir=%s array=%x nofs=%d flags=0x%x skip=%d)\n",
01589 basedir, bitpath, nofs, flags, skip);
01590
01591 rc = edt_program_flash(edt_p,
01592 ba,
01593 size,
01594 do_sleep);
01595
01596 if (rc == 0)
01597 {
01598 edt_set_bitpath(edt_p, bitpath) ;
01599 edt_set_mezz_bitpath(edt_p, "");
01600 break;
01601 }
01602
01603
01604
01605 }
01606
01607 which ++;
01608 }
01609
01610 if (found_one == FALSE)
01611 {
01612 edt_msg(EDT_MSG_FATAL, "No matching bitfiles found\n");
01613 return -1;
01614 }
01615
01616 return rc;
01617 }
01618
01619 edt_msg(DEBUG2, "edt_bitload(basedir=%s bitpath=%s nofs=%d flags=0x%x skip=%d)\n",
01620 basedir, bitpath, nofs, flags, skip);
01621
01622 edt_bitload_devid_to_bitdir(edt_p->devid, devdir);
01623
01624 fullpath = bitload_has_slashes(bitpath);
01625
01626 edt_set_bitpath(edt_p, "") ;
01627
01628 bf_init(&boardfiles);
01629
01630 if ((rc = edt_get_bitfile_list(
01631 basedir, devdir , bitpath,
01632 board_chips,
01633 edt_p->devid,
01634 channel,
01635 &boardfiles, hint)) == 0)
01636 {
01637 EdtBitfile data;
01638 edt_bitfile_init(&data);
01639
01640 bf_allocate_max_buffer(&boardfiles, &data);
01641
01642 for (i=0;i<boardfiles.nbfiles;i++)
01643 {
01644 char *bitfile = boardfiles.bitfiles[i].filename;
01645
01646 if (edt_bitfile_load_file(&data,bitfile) == 0)
01647 {
01648 rc = edt_program_flash(edt_p,
01649 data.data,
01650 data.data_size,
01651 do_sleep);
01652
01653 edt_msg(DEBUG1, "Tried %s result = %s\n",
01654 bitfile, (rc == 0) ? "success": "failure");
01655 if (rc == 0)
01656 {
01657 edt_set_bitpath(edt_p, bitpath) ;
01658 edt_set_mezz_bitpath(edt_p, "");
01659 break;
01660 }
01661
01662 }
01663 }
01664
01665 edt_bitfile_destroy(&data);
01666
01667 }
01668 if (rc != 0)
01669 {
01670 edt_msg(EDTAPP_MSG_FATAL, "bitload(name=%s ) failed\n", bitpath);
01671 if (edt_get_verbosity() < 2)
01672 edt_msg(EDTAPP_MSG_FATAL, " (run with -v to look for the cause of the failure)\n");
01673 else edt_msg(EDTAPP_MSG_FATAL, "\n");
01674 bf_destroy(&boardfiles);
01675 return rc;
01676 }
01677 else
01678 {
01679 edt_correct_slashes(bitpath);
01680 edt_msg(EDTAPP_MSG_WARNING,"Loaded: %s %s\n", bitpath, boardfiles.bitfiles[i].promstr);
01681 }
01682 bf_destroy(&boardfiles);
01683 return rc;
01684 }
01685
01695 int
01696 edt_program_flash(EdtDev *edt_p, const u_char *buf, int size, int do_sleep)
01697 {
01698
01699 #ifdef DO_DIRECT_LOAD
01700 return edt_program_flash_direct(edt_p, buf, size, do_sleep);
01701 #else
01702
01703 int ret;
01704 buf_args args;
01705
01706 memset(&args, 0, sizeof(args));
01707 args.addr = (uint64_t) (buf);
01708 args.size = size;
01709 args.index = do_sleep;
01710
01711 ret = edt_ioctl(edt_p, EDTS_BITLOAD, &args);
01712
01713 if (args.index == EINVAL)
01714 puts("Unable to load %s, failed key check.\nThis board doesn't appear to have the correct key loaded for the bitfile\n");
01715
01716 return args.index;
01717 #endif
01718
01719 }
01720
01721
01722
01723
01724
01725
01726
01727
01728
01729 static int
01730 bitload_prom_loadit(EdtDev *edt_p, u_int addr, int size, u_char *out_addr)
01731 {
01732
01733 int i;
01734 u_int addr_p = addr;
01735 u_char *buf_p = out_addr;
01736
01737
01738 edt_msg(DEBUG1, "\naddr_p %06x\n", addr_p);
01739 for (i=0;i<size; i++)
01740 {
01741 *buf_p = edt_flipbits(edt_flash_read8(edt_p, addr_p, FTYPE_BT));
01742
01743
01744 #if DUMP_HEX
01745 if (!(i%16))
01746 printf("\n%07d ", i);
01747 printf("%02x%s", *buf_p, i%2?" ":"" );
01748 #endif
01749
01750 ++addr_p;
01751 ++buf_p;
01752
01753 }
01754
01755
01756
01757 return 0;
01758 }
01759
01760
01761
01766 int
01767 edt_bitload_from_prom(EdtDev *edt_p, u_int addr1, int size1, u_int addr2, int size2, int flags)
01768 {
01769 int ret, do_sleep, do_readback = 0;
01770 int size = size1 + size2;
01771
01772 u_char *buffer;
01773
01774 if (flags & BITLOAD_FLAGS_SLEEP)
01775 do_sleep = 1;
01776 if (flags & BITLOAD_FLAGS_READBACK)
01777 do_readback = 1;
01778
01779 edt_ioctl(edt_p, EDTS_PROG_READBACK, &do_readback);
01780
01781
01782
01783 buffer = edt_alloc(size);
01784
01785
01786
01787
01788 if (bitload_prom_loadit(edt_p, addr1, size1, buffer) != 0)
01789 return -1;
01790 if (bitload_prom_loadit(edt_p, addr2, size2, buffer + size1) != 0)
01791 return -1;
01792
01793
01794
01795 ret = edt_program_flash(edt_p, buffer, size, do_sleep);
01796
01797 edt_free(buffer);
01798
01799 return 0;
01800 }