summaryrefslogtreecommitdiffstats
path: root/tools/build/packhex.c
blob: 9d19ce39e17c735f1694664c4f47cb6a95ba762f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533

/*****  P A C K H E X . C  ************************************************
 *
 *   Packhex is a hex-file compaction utility.  It attempts to concatenate
 *   hex records to produce more size-efficient packaging.
 *
 *   Limitations: Input files must be correctly formatted.  This utility
 *                is not robust enough to detect hex-record formatting
 *                errors.
 *
 *   Published:   5/93 Embedded Systems magazine
 *
 *   Compiler:    Microsoft C 6.0
 *                cl /F 1000 packhex.c
 *
 *
 *   $Id$
 *
 **************************************************************************/


/* #define SMALLER_RECORDS */
#ifdef SMALLER_RECORDS
#define MAX_LEN_S1_RECS 128
#define MAX_LEN_S2_RECS 128
#define MAX_LEN_S3_RECS 128
#else
#define MAX_LEN_S1_RECS 252
#define MAX_LEN_S2_RECS 251
#define MAX_LEN_S3_RECS 250
#endif


/*--------------------------------- includes ---------------------------------*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "config.h"

#ifndef VMS
#ifndef HAVE_STRERROR
extern int sys_nerr;
extern char *sys_errlist[];

#define strerror( _err ) \
  ((_err) < sys_nerr) ? sys_errlist [(_err)] : "unknown error"

#else   /* HAVE_STRERROR */
char *strerror ();
#endif
#else   /* VMS */
char *strerror (int,...);
#endif

#if defined(__unix__) && !defined(EXIT_FAILURE)
#define EXIT_FAILURE -1
#define EXIT_SUCCESS  0
#endif

/*--------------------------------- defines ----------------------------------*/

#define YES                   1
#define MAX_LINE_SIZE       600
#define EOS                 '\0'


/*---------------------------------- macros ----------------------------------*/

/* Convert ASCII hexadecimal digit to value. */

#define HEX_DIGIT( C )   ( ( ( ( C ) > '9' ) ? ( C ) + 25 : ( C ) ) & 0xF )


/*--------------------------------- typedefs ---------------------------------*/

typedef unsigned char   Boolean;
typedef unsigned char   Uchar;
typedef unsigned int    Uint;
typedef unsigned long   Ulong;

typedef struct   /* Functions and constant returning Hex-record vital stats. */
{
    Boolean    ( *is_data_record  )( char *              );
    Ulong      ( *get_address     )( char *              );
    Uint       ( *get_data_count  )( char *              );
    const Uint    max_data_count;
    char      *( *get_data_start  )( char *              );
    void       ( *put_data_record )( Uint, Ulong, char * );
} Rec_vitals;


/*--------------------------- function prototypes ----------------------------*/

Rec_vitals  * identify_first_data_record( char * );
Ulong         get_ndigit_hex( char *, int );


/*----------------------------- Intel Hex format -----------------------------*/

/*
 *    Intel Hex data-record layout
 *
 *    :aabbbbccd...dee
 *
 *    :      - header character
 *    aa     - record data byte count, a 2-digit hex value
 *    bbbb   - record address, a 4-digit hex value
 *    cc     - record type, a 2-digit hex value:
 *              "00" is a data record
 *              "01" is an end-of-data record
 *              "02" is an extended-address record
 *              "03" is a start record
 *    d...d  - data (always an even number of chars)
 *    ee     - record checksum, a 2-digit hex value
 *             checksum = 2's complement
 *                 [ (sum of bytes: aabbbbccd...d) modulo 256 ]
 */


Boolean is_intel_data_rec( char * rec_str )
{
    return( ( rec_str[ 0 ] == ':' )  &&  ( rec_str[ 8 ] == '0' ) );
}

Uint get_intel_rec_data_count( char * rec_str )
{
    return( ( Uint ) get_ndigit_hex( rec_str + 1, 2 ) );
}

Ulong get_intel_rec_address( char * rec_str )
{
    return( get_ndigit_hex( rec_str + 3, 4 ) );
}

char * get_intel_rec_data_start( char * rec_str )
{
    return( rec_str + 9 );
}

void put_intel_data_rec( Uint count, Ulong address, char * data_str )
{
    char    *ptr;
    Uint    sum = count + ( address >> 8 & 0xff ) + ( address & 0xff );

    for ( ptr = data_str ; *ptr != EOS ; ptr += 2 )
        sum += ( Uint ) get_ndigit_hex( ptr, 2 );

    printf(
      ":%02X%04lX00%s%02X\n", count, address, data_str, (~sum + 1) & 0xff
    );
}


Rec_vitals  intel_hex =
{
    is_intel_data_rec,
    get_intel_rec_address,
    get_intel_rec_data_count,
    255,                        /* Maximum data bytes in a record. */
    get_intel_rec_data_start,
    put_intel_data_rec
};


/*------------------------- Motorola S1-record format ------------------------*/

/*
 *    Motorola S-record data-record layout
 *
 *    Sabbc...cd...dee
 *
 *    S      - header character
 *    a      - record type, a 1-digit value:
 *               "0" is a header record
 *               "1" is a 2-byte-address data record
 *               "2" is a 3-byte-address data record
 *               "3" is a 4-byte-address data record
 *               "7" is a 4-byte-address end-of-data record
 *               "8" is a 3-byte-address end-of-data record
 *               "9" is a 2-byte-address end-of-data record
 *    bb     - record length in bytes, a 2-digit hex value
 *             (record length doesn't count the header/type
 *              chars and checksum byte)
 *    c...c  - record address, a 4-, 6-, or 8-digit value,
 *               depending on record type
 *    d...d  - data (always an even number of chars)
 *    ee     - record checksum, a 2-digit hex value
 *             checksum = 1's complement
 *             [ (sum of all bytes: bbc..cd...d) modulo 256 ]
 */

#define S1_COUNT_OFFSET         3


Boolean is_moto_s1_data_rec( char * rec_str )
{
    return ( ( rec_str[ 0 ] == 'S' )  &&  ( rec_str[ 1 ] == '1' ) );
}

Uint get_moto_s1_rec_data_count( char * rec_str )
{
    return( ( Uint ) get_ndigit_hex( rec_str + 2, 2 ) - S1_COUNT_OFFSET );
}

Ulong get_moto_s1_rec_address( char * rec_str )
{
    return( get_ndigit_hex( rec_str + 4, 4 ) );
}

char * get_moto_s1_rec_data_start( char * rec_str )
{
    return( rec_str + 8 );
}

void put_moto_s1_data_rec( Uint count, Ulong address, char * data_str )
{
    char   *ptr;
    Uint    sum = S1_COUNT_OFFSET + count +
                    ( address >> 8 & 0xff ) + ( address & 0xff );

    for ( ptr = data_str ; *ptr != EOS ; ptr += 2 )
        sum += ( Uint ) get_ndigit_hex( ptr, 2 );

    printf(
      "S1%02X%04lX%s%02X\n",
      count + S1_COUNT_OFFSET, address, data_str, ~sum & 0xff
    );
}


Rec_vitals  motorola_s1_rec =
{
    is_moto_s1_data_rec,
    get_moto_s1_rec_address,
    get_moto_s1_rec_data_count,
    MAX_LEN_S1_RECS,            /* Maximum data bytes in a record. */
    get_moto_s1_rec_data_start,
    put_moto_s1_data_rec
};


/*------------------------- Motorola S2-record format ------------------------*/

#define S2_COUNT_OFFSET         4

Boolean is_moto_s2_data_rec( char * rec_str )
{
    return ( ( rec_str[ 0 ] == 'S' )  &&  ( rec_str[ 1 ] == '2' ) );
}

Uint get_moto_s2_rec_data_count( char * rec_str )
{
    return( ( Uint ) get_ndigit_hex( rec_str + 2, 2 ) - S2_COUNT_OFFSET );
}

Ulong get_moto_s2_rec_address( char * rec_str )
{
    return( get_ndigit_hex( rec_str + 4, 6 ) );
}

char * get_moto_s2_rec_data_start( char * rec_str )
{
    return( rec_str + 10 );
}

void put_moto_s2_data_rec( Uint count, Ulong address, char * data_str )
{
    char    *ptr;
    Uint    sum = S2_COUNT_OFFSET + count + ( address >> 16 & 0xff ) +
                                            ( address >>  8 & 0xff ) +
                                            ( address       & 0xff );

    for ( ptr = data_str ; *ptr != EOS ; ptr += 2 )
        sum += ( Uint ) get_ndigit_hex( ptr, 2 );

    printf(
      "S2%02X%06lX%s%02X\n",
      count + S2_COUNT_OFFSET, address, data_str, ~sum & 0xff
    );
}


Rec_vitals  motorola_s2_rec =
{
    is_moto_s2_data_rec,
    get_moto_s2_rec_address,
    get_moto_s2_rec_data_count,
    MAX_LEN_S2_RECS,            /* Maximum data bytes in a record. */
    get_moto_s2_rec_data_start,
    put_moto_s2_data_rec
};


/*------------------------- Motorola S3-record format ------------------------*/

#define S3_COUNT_OFFSET         5

Boolean is_moto_s3_data_rec( char * rec_str )
{
    return ( ( rec_str[ 0 ] == 'S' )  &&  ( rec_str[ 1 ] == '3' ) );
}

Uint get_moto_s3_rec_data_count( char * rec_str )
{
    return( ( Uint ) get_ndigit_hex( rec_str + 2, 2 ) - S3_COUNT_OFFSET );
}

Ulong get_moto_s3_rec_address( char * rec_str )
{
    return( get_ndigit_hex( rec_str + 4, 8 ) );
}

char * get_moto_s3_rec_data_start( char * rec_str )
{
    return( rec_str + 12 );
}

void put_moto_s3_data_rec( Uint count, Ulong address, char * data_str )
{
    char    *ptr;
    Uint     sum = S3_COUNT_OFFSET + count + ( address >> 24 & 0xff ) +
                                             ( address >> 16 & 0xff ) +
                                             ( address >>  8 & 0xff ) +
                                             ( address       & 0xff );

    for ( ptr = data_str ; *ptr != EOS ; ptr += 2 )
        sum += ( Uint ) get_ndigit_hex( ptr, 2 );

    printf(
      "S3%02X%08lX%s%02X\n",
      count + S3_COUNT_OFFSET, address, data_str, ~sum & 0xff
   );
}


Rec_vitals  motorola_s3_rec =
{
    is_moto_s3_data_rec,
    get_moto_s3_rec_address,
    get_moto_s3_rec_data_count,
    MAX_LEN_S3_RECS,            /* Maximum data bytes in a record. */
    get_moto_s3_rec_data_start,
    put_moto_s3_data_rec
};


/*-------------------- Put your favorite hex format here ---------------------*/

/*
 *    * * *  The following is a template for an additional hex format:  * * *
 *
 *
 *    Boolean is_X_data_rec( char * rec_str ) {}
 *
 *    Uint get_X_rec_data_count( char * rec_str ) {}
 *
 *    Ulong get_X_rec_address( char * rec_str ) {}
 *
 *    char * get_X_rec_data_start( char * rec_str ) {}
 *
 *    void put_X_data_rec( Uint count, Ulong address, char * data_str ) {}
 *
 *    Rec_vitals  X_rec =
 *    {
 *        is_X_data_rec,
 *        get_X_rec_address,
 *        get_X_rec_data_count,
 *        MAXIMUM DATA BYTES IN A RECORD,
 *        get_X_rec_data_start,
 *        put_X_data_rec
 *    };
 *
 */

/*----------------------------------------------------------------------------*/


/*
 *   Put address of additional Rec_vitals structures
 *   in this array, before the NULL entry.
 */

Rec_vitals  *formats[] =
{
    &intel_hex,
    &motorola_s1_rec,
    &motorola_s2_rec,
    &motorola_s3_rec,
    ( Rec_vitals * ) NULL
};


/****   main   *****************************************************************
*
*
*       Expects: Nothing (no command-line parameters).
*
*       Returns: Exit status (EXIT_SUCCESS or EXIT_FAILURE).
*
*       Reads hex records on the standard input and attempts to
*       splice adjacent data fields together.  Results appear on
*       the standard output.
*
*******************************************************************************/

int main(
  int argc,
  char **argv
)
{

    char       inbuff[ MAX_LINE_SIZE ], outbuff[ MAX_LINE_SIZE ];
    char       *in_dptr, *out_dptr;
    int        d_total, d_count, d_excess, n;
    Ulong      in_rec_addr, out_rec_addr = 0;
    Rec_vitals *rptr;


    /* Sift through file until first hex record is identified.    */
    if ( ( rptr = identify_first_data_record( inbuff ) ) == NULL )
    {
        fputs( "No hex records found.\n", stderr );
        exit( EXIT_FAILURE );
    }


    /* Attempt data-record splicing until end-of-file is reached. */
    d_total = 0;
    do
    {
        if ( rptr->is_data_record( inbuff ) == YES )
        { /* Input record is a data record. */
            d_count     = rptr->get_data_count( inbuff );
            in_rec_addr = rptr->get_address( inbuff );
            in_dptr     = rptr->get_data_start( inbuff );

            if ( d_total == 0  ||  in_rec_addr != out_rec_addr + d_total )
            { /* Begin a new output record. */
                if ( d_total != 0 )
                    rptr->put_data_record( d_total, out_rec_addr, outbuff );
                out_dptr     = outbuff;
                n = d_total  = d_count;
                out_rec_addr = in_rec_addr;
            }
            else if
              ( ( d_excess = d_total + d_count - rptr->max_data_count ) > 0 )
            { /* Output a maximum-length record, then start a new record. */
                strncat( outbuff, in_dptr, 2 * ( d_count - d_excess ) );
                rptr->put_data_record(
                  rptr->max_data_count, out_rec_addr, outbuff
                );
                in_dptr      += 2 * ( d_count - d_excess );
                out_dptr      = outbuff;
                n = d_total   = d_excess;
                out_rec_addr += rptr->max_data_count;
            }
            else
            { /* Append input record's data field with accumulated data. */
                out_dptr = outbuff + ( 2 * d_total );
                d_total += n = d_count;
            }
            strncpy( out_dptr, in_dptr, 2 * n );
            out_dptr[ 2 * n ] = EOS;
        }
        else
        { /* Not a data record;
           * flush accumulated data then echo non-data record.
           */
            if ( d_total != 0 )
            {
                rptr->put_data_record( d_total, out_rec_addr, outbuff );
                d_total = 0;
            }
            puts( inbuff );
        }
    } while ( gets( inbuff ) != NULL );


    return ( EXIT_SUCCESS );

}


/****   identify_first_data_record   *******************************************
*
*       Expects: Pointer to hex-record line buffer.
*
*       Returns: Pointer to hex-record structure (NULL if no match found).
*
*       Reads the standard input, line by line, searching for a valid
*       record header character.  If a valid header is found, a pointer
*       to the hex-record's type structure is returned, otherwise NULL.
*
*       The input-stream pointer is left pointing to the first valid hex record.
*
*******************************************************************************/

Rec_vitals * identify_first_data_record( char * buff_ptr )
{
    Rec_vitals  ** ptr;

    while ( gets( buff_ptr ) != NULL )
    {
        for ( ptr = formats ; *ptr != ( Rec_vitals * ) NULL ; ptr++ )
            if ( ( *ptr )->is_data_record( buff_ptr ) == YES )
                return( *ptr );        /* Successful return.        */

        puts( buff_ptr );              /* Echo non-hex-record line. */
    }

    return( ( Rec_vitals * ) NULL );   /* Unsuccessful return.      */
}


/****   get_ndigit_hex   *******************************************************
*
*       Expects: Pointer to first ASCII hexadecimal digit, number of digits.
*
*       Returns: Value of hexadecimal string as an unsigned long.
*
*******************************************************************************/

Ulong get_ndigit_hex( char * cptr, int digits )
{
    Ulong    value;

    for ( value = 0 ; --digits >= 0 ; cptr++ )
        value = ( value * 16L ) + HEX_DIGIT( *cptr );

    return( value );
}