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