FreeCalypso > hg > freecalypso-tools
view ffstools/caltools/c1xx-calextr.c @ 300:0cfb2e3aeda4
started documenting c1xx-calextr
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sun, 19 Nov 2017 21:45:12 +0000 |
parents | 7fefa4f73c6a |
children | 31369f326ee3 |
line wrap: on
line source
/* * This program parses Compal's proprietary data structure that contains * the factory RF calibration values among other data, locates those RF * calibration records, extracts their essential content (Rx GMagic and * Tx APC values), converts this distilled content into TCS211 RF calibration * tables (Rx agcparams and Tx levels) and writes these tables out in either * FreeCalypso ASCII or FFS binary format. */ #include <sys/types.h> #include <sys/param.h> #include <sys/file.h> #include <string.h> #include <strings.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #define COMPAL_SECTOR_LENGTH 0x2000 u_char sector[COMPAL_SECTOR_LENGTH]; u_char endmarker[8] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; u_char record_magic[4] = {0xAA, 0x00, 0x00, 0x00}; char *ascii_output_dir, *bin_output_dir; struct band { char *name; unsigned compal_record_id; unsigned record_length; unsigned magic2_offset; unsigned start_plnum; unsigned end_plnum; } bands[] = { {"900", 0x00, 0x94, 0x54, 5, 19}, {"1800", 0x01, 0xC8, 0x74, 0, 15}, {"1900", 0x02, 0xB4, 0x68, 0, 15}, {"850", 0x18, 0x88, 0x4C, 5, 19}, }; read_binfile(filename, offset_arg) char *filename, *offset_arg; { int fd, cc; u_long offset; char *endp; fd = open(filename, O_RDONLY); if (fd < 0) { perror(filename); exit(1); } offset = strtoul(offset_arg, &endp, 0); if (*endp) { fprintf(stderr, "error: invalid offset argument \"%s\"\n", offset_arg); exit(1); } lseek(fd, offset, SEEK_SET); cc = read(fd, sector, COMPAL_SECTOR_LENGTH); if (cc != COMPAL_SECTOR_LENGTH) { fprintf(stderr, "error: unable to read Compal sector of %d bytes from %s at offset %s\n", COMPAL_SECTOR_LENGTH, filename, offset_arg); exit(1); } close(fd); } write_rx_agcparams_ascii(band, table) struct band *band; u_char *table; { char pathname[MAXPATHLEN]; FILE *of; sprintf(pathname, "%s/rx-agcparams.%s", ascii_output_dir, band->name); of = fopen(pathname, "w"); if (!of) { perror(pathname); exit(1); } write_rx_agcparams_table(table, of); fclose(of); } write_rx_agcparams_bin(band, table) struct band *band; u_char *table; { char pathname[MAXPATHLEN]; int fd; sprintf(pathname, "%s/rx", bin_output_dir); mkdir_existok(pathname); sprintf(pathname, "%s/rx/agcparams.%s", bin_output_dir, band->name); fd = open(pathname, O_WRONLY|O_CREAT|O_TRUNC, 0666); if (fd < 0) { perror(pathname); exit(1); } write(fd, table, 8); close(fd); } write_tx_levels_ascii(band, table) struct band *band; u_char *table; { char pathname[MAXPATHLEN]; FILE *of; sprintf(pathname, "%s/tx-levels.%s", ascii_output_dir, band->name); of = fopen(pathname, "w"); if (!of) { perror(pathname); exit(1); } write_tx_levels_table(table, of); fclose(of); } write_tx_levels_bin(band, table) struct band *band; u_char *table; { char pathname[MAXPATHLEN]; int fd; sprintf(pathname, "%s/tx", bin_output_dir); mkdir_existok(pathname); sprintf(pathname, "%s/tx/levels.%s", bin_output_dir, band->name); fd = open(pathname, O_WRONLY|O_CREAT|O_TRUNC, 0666); if (fd < 0) { perror(pathname); exit(1); } write(fd, table, 128); close(fd); } do_rx_agcparams(band, compal_data) struct band *band; u_char *compal_data; { u_char rx_agcparams_table[8]; bcopy(compal_data, rx_agcparams_table, 2); /* the remaining fields are constants unchanged from TI */ rx_agcparams_table[2] = 40; rx_agcparams_table[3] = 0; rx_agcparams_table[4] = 40; rx_agcparams_table[5] = 0; rx_agcparams_table[6] = 44; rx_agcparams_table[7] = 0; if (ascii_output_dir) write_rx_agcparams_ascii(band, rx_agcparams_table); if (bin_output_dir) write_rx_agcparams_bin(band, rx_agcparams_table); } do_tx_levels(band, compal_data) struct band *band; u_char *compal_data; { u_char tx_levels_table[128], *sp, *dp; unsigned num_levels, n; bzero(tx_levels_table, sizeof tx_levels_table); num_levels = band->end_plnum - band->start_plnum + 1; sp = compal_data; dp = tx_levels_table + band->start_plnum * 4; for (n = 0; n < num_levels; n++) { *dp++ = *sp++; *dp++ = *sp++; *dp++ = n; *dp++ = 0; } if (ascii_output_dir) write_tx_levels_ascii(band, tx_levels_table); if (bin_output_dir) write_tx_levels_bin(band, tx_levels_table); } process_band_record(band, offset) struct band *band; unsigned offset; { u_char *record; record = sector + offset + 8; if (bcmp(record, record_magic, 4)) { printf("bad magic1, skipping\n"); return(-1); } if (bcmp(record + band->magic2_offset, record_magic, 4)) { printf("bad magic2, skipping\n"); return(-1); } if (bcmp(record + band->magic2_offset + 8, record_magic, 4)) { printf("bad magic3, skipping\n"); return(-1); } do_rx_agcparams(band, record + 4); do_tx_levels(band, record + band->magic2_offset + 12); return(0); } process_sector_data() { unsigned offset, next_offset; u_char *header; unsigned hdr_words[4]; struct band *band; int i; for (offset = 0; ; offset = next_offset) { if (offset > COMPAL_SECTOR_LENGTH - 12) break; header = sector + offset; if (!bcmp(header, endmarker, 8)) break; for (i = 0; i < 4; i++) hdr_words[i] = header[i*2] | (header[i*2+1] << 8); if (!hdr_words[3]) { fprintf(stderr, "error at offset 0x%X: rounded record length word is 0\n", offset); exit(1); } if (hdr_words[3] & 3) { fprintf(stderr, "error at offset 0x%X: rounded record length word is not aligned to 4\n", offset); exit(1); } if (hdr_words[3] > COMPAL_SECTOR_LENGTH - offset - 8) { fprintf(stderr, "error at offset 0x%X: rounded record length spills past end of sector\n", offset); exit(1); } if (hdr_words[2] > hdr_words[3]) { fprintf(stderr, "error at offset 0x%X: native record length is greater than rounded\n", offset); exit(1); } next_offset = offset + 8 + hdr_words[3]; if (hdr_words[0] != 0x000C) continue; for (band = bands; band->name; band++) if (hdr_words[1] == band->compal_record_id) break; if (!band->name) continue; printf("Found %s MHz calibration record at offset 0x%X\n", band->name, offset); if (hdr_words[2] != band->record_length) { printf("Oops, wrong length, skipping\n"); continue; } process_band_record(band, offset); } } main(argc, argv) char **argv; { int c; extern char *optarg; extern int optind; while ((c = getopt(argc, argv, "a:b:")) != EOF) switch (c) { case 'a': ascii_output_dir = optarg; continue; case 'b': bin_output_dir = optarg; continue; case '?': default: usage: fprintf(stderr, "usage: %s [-a ascii_outdir] [-b bin_outdir] c1xx-binfile offset\n", argv[0]); exit(1); } if (argc - optind != 2) goto usage; read_binfile(argv[optind], argv[optind+1]); if (ascii_output_dir) mkdir_existok(ascii_output_dir); if (bin_output_dir) mkdir_existok(bin_output_dir); process_sector_data(); exit(0); }