/* Strip header off of a Xilinx .bit file, to make * something that corresponds to the raw Altera .rbf file, * suitable for checksum and download with usrper. * Or, with -s option, coerce timestamp to one given. */ #include #include #include #include #include #include typedef unsigned int uint32; /* utilities involved in reading the Xilinx .bit file. * File format documented by Alan Nishioka */ static int xget_char(FILE *o) { int c; if ((c = getchar()) == EOF) exit(1); if (o) fputc(c, o); return c; } static uint32 get_long(FILE *o) { uint32 l; l = xget_char(o); l = xget_char(o) | l<<8; l = xget_char(o) | l<<8; l = xget_char(o) | l<<8; return l; } static int get_short(FILE *o) { int c, l; if ((c = getchar()) == EOF) return -1; l = c; if (o) fputc(c, o); if ((c = getchar()) == EOF) return -1; l = l<<8 | c; if (o) fputc(c, o); return l; } static void find_key(int key, FILE *o) { int c; if ((c = getchar()) != key) { fprintf(stderr, "find_key found 0x%.2x instead of 0x%.2x\n", (unsigned)c, (unsigned)key); exit(1); } if (o) fputc(c, o); } static void emit_string(const char *str, FILE *o) { if (!o) return; int l = strlen(str)+1; /* include the trailing 0 */ if (l > SHRT_MAX) { fprintf(stderr,"emit_string: str too long\n"); exit(1); } fputc((l>>8) & 0xff, o); putc(l & 0xff, o); for (int ix=0; ix 1 && argv[1][0] == '-' && argv[1][1] != 's') { printf("Usage: %s [-s timestamp] [outfile] < xilinx_bitfile\n", argv[0]); printf(" maybe %s -s $(git log -1 --pretty=%%ct)\n", argv[0]); exit(1); } if (argc > 1 && (strcmp(argv[1], "-s")==0)) { if (argc > 2) { stime = argv[2]; argc -= 2; argv += 2; } } outfile = argv[1]; /* argument will be NULL if no file specified */ load_main(stime, outfile); return 0; }