/*-------------------------------------------------------------------* * File Name: sretobin.c * * Author: Jonathan E. Flamm * Date: 6/4/93 * Description: /* SREC Format converter -- converts from srec to bin * * History: * 6/4/94 jef: Adapted from GNU code *-------------------------------------------------------------------* * * SREC Format is as follows: * * S
* * S starting character of a record * type 0 = header record * 1 = 2 byte address data record * 2 = 3 byte address data record * 3 = 4 byte address data record * 7 = 4 byte address termination record * 8 = 3 byte address termination record * 9 = 2 byte address termination record * * address - start address of data * data - data * checksum - sum of raw byte data in record from length * upwards modulo 256 and subtracted from 255 *-------------------------------------------------------------------*/ #include #define True 1 #define False 0 void clearOutFile(); void putc1BHex( unsigned int x ); void putc3BHex( unsigned int x ); unsigned int get1BHex( char *p ); unsigned int get2BHex( char *p ); unsigned int get3BHex( char *p ); unsigned int get4BHex( char *p ); unsigned int htoi( char c ); char itoh( unsigned int x ); FILE *infile, *outfile; unsigned long lowAddr = 0xffffffff; unsigned long highAddr = 0x0; main( int argc, char **argv ) { unsigned int addr; unsigned int bytes; static char line[200]; unsigned int addrBytes; unsigned int checksum; int i; unsigned int check; unsigned char x; if (argc != 3) { printf("\nUsage: %s infile outfile\n", *argv); printf("Converts s37 srec to s28 format\n"); exit(1); } infile = fopen( (const char *)argv[1], "r" ); if ( infile == NULL) { fprintf(stderr, "Cannot open: %s\n",argv[1]); exit(1); } outfile = fopen( (const char *)argv[2], "wb" ); if ( outfile == NULL) { fprintf(stderr, "Cannot open: %s\n",argv[2]); exit(1); } clearOutFile(); while ( fgets(line, 200, infile) ) { switch (line[1]) { case '1': bytes = get1BHex( &line[2] ); addr = get2BHex( &line[4] ); fseek( outfile, addr - lowAddr, SEEK_SET ); addrBytes = addr & 0xff; addrBytes += (addr >> 8) & 0xff; checksum = bytes + addrBytes; for (i = 0; i < (bytes - 3); i++ ) { x = get1BHex( &line[8 + (2*i) ]); fwrite(&x, 1, 1, outfile); checksum += get1BHex( &line[8+(2*i)] ); } checksum = 255 - (checksum % 256); check = get1BHex( &line[8 +(2*i)] ); if (check != checksum) { fprintf(stderr, "BAD Checksum ADDR=%X\n",addr); fprintf(stderr, "checksum read/expected <%X,%X> \n", check, checksum); exit(1); } break; case '2': bytes = get1BHex( &line[2] ); addr = get3BHex( &line[4] ); fseek( outfile, addr - lowAddr, SEEK_SET ); addrBytes = addr & 0xff; addrBytes += (addr >> 8) & 0xff; addrBytes += (addr >> 16) & 0xff; checksum = bytes + addrBytes; for (i = 0; i < (bytes - 4); i++ ) { x = get1BHex( &line[10 + (2*i) ]); fwrite(&x, 1, 1, outfile); checksum += get1BHex( &line[10+(2*i)] ); } checksum = 255 - (checksum % 256); check = get1BHex( &line[10 +(2*i)] ); if (check != checksum) { fprintf(stderr, "BAD Checksum ADDR=%X\n",addr); fprintf(stderr, "checksum read/expected <%X,%X> \n", check, checksum); exit(1); } break; case '3': bytes = get1BHex( &line[2] ); addr = get4BHex( &line[4] ); fseek( outfile, addr - lowAddr, SEEK_SET ); addrBytes = addr & 0xff; addrBytes += (addr >> 8) & 0xff; addrBytes += (addr >> 16) & 0xff; addrBytes += (addr >> 24) & 0xff; checksum = bytes + addrBytes; for (i = 0; i < (bytes - 5); i++ ) { x = get1BHex( &line[12 + (2*i) ]); fwrite( &x, 1, 1, outfile); checksum += get1BHex( &line[12+(2*i)] ); } checksum = 255 - (checksum % 256); check = get1BHex( &line[12 +(2*i)] ); if (check != checksum) { fprintf(stderr, "BAD Checksum ADDR=%X\n",addr); fprintf(stderr, "checksum read/expected <%X,%X> \n", check, checksum); exit(1); } break; case '0': /* Header Record */ break; case '7': /* End Record */ case '8': /* End Record */ case '9': /* End Record */ break; default: fprintf(stderr, "Unknown record type '%c\n", line[1]); break; } /* end switch */ } fclose(infile); fclose(outfile); } void clearOutFile() { unsigned int addr; unsigned int bytes; static char line[200]; int endRecord = True; int i; unsigned int zero = 0; while ( fgets(line, 200, infile) ) { if ( line[0] != 'S') { fprintf(stderr, "Non S Record line in file\n"); exit(1); } switch (line[1]) { case '1': addr = get4BHex( &line[4] ); bytes = get1BHex( &line[2] ) - 3; if (addr < lowAddr) lowAddr = addr; if ( (addr + bytes) > highAddr ) highAddr = addr + bytes; break; case '2': addr = get3BHex( &line[4] ); bytes = get1BHex( &line[2] ) - 4; if (addr < lowAddr) lowAddr = addr; if ( (addr + bytes) > highAddr ) highAddr = addr + bytes; break; case '3': addr = get4BHex( &line[4] ); bytes = get1BHex( &line[2] ) - 5; if (addr < lowAddr) lowAddr = addr; if ( (addr + bytes) > highAddr ) highAddr = addr + bytes; break; case '0': /* Header Record */ break; case '7': /* End Record */ case '8': /* End Record */ case '9': /* End Record */ endRecord = True; break; } } if (endRecord == False) { fprintf(stderr, "No End Record\n"); exit(1); } rewind( infile ); for (i = 0; i < (highAddr - lowAddr); i++) fwrite(&zero, 1, 1, outfile); rewind( outfile ); fprintf(stdout, "address span <%X,%X>\n", lowAddr, highAddr); } void putc1BHex( unsigned int x ) { fputc( itoh( (x >> 4) & 0xF ), outfile ); fputc( itoh(x & 0xF), outfile ); } void putc3BHex( unsigned int x ) { fputc( itoh((x >> 20) & 0xF), outfile ); fputc( itoh((x >> 16) & 0xF), outfile ); fputc( itoh((x >> 12) & 0xF), outfile ); fputc( itoh((x >> 8) & 0xF), outfile ); fputc( itoh((x >> 4) & 0xF), outfile ); fputc( itoh(x & 0xF), outfile ); } unsigned int get1BHex( char *p ) { return ( htoi(*(p+1)) + ( htoi(*p) << 4 ) ); } unsigned int get2BHex( char *p ) { unsigned int x; x = (htoi(*(p+1)) + (htoi(*p) << 4 )) << 8; x += (htoi(*(p+3)) + (htoi(*(p+2)) << 4 )); return x; } unsigned int get3BHex( char *p ) { unsigned int x; x = (htoi(*(p+1)) + (htoi(*p) << 4 )) << 16; x += (htoi(*(p+3)) + (htoi(*(p+2)) << 4 )) << 8; x += (htoi(*(p+5)) + (htoi(*(p+4)) << 4 )); return x; } unsigned int get4BHex( char *p ) { unsigned int x; x = (htoi(*(p+1)) + (htoi(*p) << 4 )) << 24; x += (htoi(*(p+3)) + (htoi(*(p+2)) << 4 )) << 16; x += (htoi(*(p+5)) + (htoi(*(p+4)) << 4 )) << 8; x += (htoi(*(p+7)) + (htoi(*(p+6)) << 4 )); return x; } unsigned int htoi( char c ) { unsigned int val; if ( (c >= '0') && (c <= '9') ) val = c - '0'; else val = c - 'A' + 10; return val; } char itoh( unsigned int x ) { char val; if (x < 10) val = '0' + x; else val = 'A' + (x - 10); return val; }