summaryrefslogtreecommitdiff
path: root/src/buffer.c
blob: e6f4804d50851e75cd105d0e1a0e55211db75221 (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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
/* SP12: A serial programmer for working with Atmel AVR uCs          */
/* Copyright (C) 1997-2003 Ken Huntington, Kevin Towers, Pitronics.  */

/* This program is free software; you can redistribute it and/or     */
/* modify it under the terms of the GNU General Public License       */
/* as published by the Free Software Foundation; either version 2    */
/* of the License, or (at your option) any later version.            */

/* This program is distributed in the hope that it will be useful,   */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of    */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the     */
/* GNU General Public License for more details.                      */

/* You should have received a copy of the GNU General Public License */
/* along with this program; if not, write to the Free Software       */
/* Foundation, Inc., 59 Temple Place - Suite 330, Boston,            */
/* MA  02111-1307, USA.                                              */

/* Pitronics can be reached by email: sbolt@xs4all.nl                */
/* Kevin Towers can be reached by email: ktowers@omnexcontrols.com   */
/* Ken Huntington can be reached by email: kenh@compmore.net         */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "sp12.h"
#include "dos_cpt.h"


/* Converts address or data string to number, automatically          */
/* selecting hex (0xNNN), oct (0NNN), binary (BNNN) or               */
/* dec (NNN) input.                                                  */

unsigned int str2num(char *addressOrData) {

   unsigned int number;
   
  /*
   * Determine format (hex, decimal or binary) by looking at prefix
   * and convert data to unsigned int using base 2, 8, 10 or 16
   */
   if (addressOrData[1] == 'x')
      number = (unsigned int) strtol(addressOrData, NULL, 16);
   else if (addressOrData[0] == '0')
      number = (unsigned int) strtol(addressOrData, NULL, 8);
   else if (addressOrData[0] == 'B')
      number = (unsigned int) strtol(&addressOrData[1], NULL, 2);
   else 
      number = (unsigned int) strtol(addressOrData, NULL, 10);

   return(number);
}

/* convert unsigned int to a string-representation of the            */
/* binary number (ascii `0' is 48, ascii `1' is 49)                  */

void num2bin(unsigned char buffer, char *binary) {
   
   int Bi;

   for (Bi = 7; Bi >= 0; Bi--) {
      binary[Bi] = (buffer & 0x01) + 48;
      buffer = buffer >> 1;
   }
   binary[8] = '\0';
}

/* Reads program file in Atmel generic format (address:data) or      */
/* Intel HEX format; stores data into into flashBuf by address.      */
/* flashBuf by address. The two numbers in the generic format are    */
/* hex without `0x'. For example: 00000c:99e1                        */
/* Note: the 16-bit bufLimit is not suitable for Mega address space. */

int readFlashFile(char flashPath[], unsigned int flashBuf[], long bufLimit) {

   FILE *flashBufPtr;
   char wordstring[WORDLEN];
   long address;
   unsigned int data;
   char buffer[MAXLEN];
   long offsetAddress = 0L;
   int dataByteCount;
   unsigned int lineAddress;
   int recordType;
   int idx;
   int checksum;
   int tmp;
   
   if ((flashBufPtr = fopen(flashPath, "r")) == NULL) {
      return(1);
   } else {
      if (fgets(wordstring, WORDLEN, flashBufPtr) != NULL) {
        /*
         * Try Atmel generic format (default of the Atmel assembler).
         */ 
         if (wordstring[FLASH_DATAPTR] == ':') {
            fseek(flashBufPtr, 0, SEEK_SET);
            while (fgets(wordstring, WORDLEN, flashBufPtr) != NULL) {
               sscanf(wordstring, "%lx", &address);
               if (wordstring[FLASH_DATAPTR] != ':') {
                  return(1);
               }
               sscanf(&wordstring[FLASH_DATAPTR + 1], "%x", &data);
               if (address < bufLimit)
                  flashBuf[address] = data;
               else
                  return(1);
            }
            fclose(flashBufPtr);
            return(0);
         }
        /*
         * Try Intel HEX format.
         */
         if (wordstring[0] == ':') {
            fseek(flashBufPtr, 0, SEEK_SET);
            while (fgets(buffer, MAXLEN, flashBufPtr) != NULL) {
               checksum = 0;
               sscanf(&buffer[1], "%2x", &dataByteCount);
               checksum += dataByteCount;
               sscanf(&buffer[3], "%4x", &lineAddress);
               sscanf(&buffer[3], "%2x", &tmp);
               checksum += tmp;
               sscanf(&buffer[5], "%2x", &tmp);
               checksum += tmp;
               sscanf(&buffer[7], "%2x", &recordType);
               checksum += recordType;
              /*
               * Record type 02 means update offset address
               */
               if (recordType == 2) {
                  sscanf(&buffer[9], "%4lx", &offsetAddress);
                  sscanf(&buffer[9], "%2x", &tmp);
                  checksum += tmp;
                  sscanf(&buffer[11], "%2x", &tmp);
                  checksum += tmp;
                  checksum = (unsigned char) (0x100 - (unsigned char) checksum);
                  sscanf(&buffer[13], "%2x", &tmp);
                  if (checksum != tmp)
                     return(1);
               }
               if (recordType == 0) {
                  for (idx = 9; idx < 9 + dataByteCount * 2; idx += 2) {
                     sscanf(&buffer[idx], "%2x", &data);
                     /*
                      * Use word addresses for the AT90 2-byte data format
                      */
                     address = lineAddress / 2;
                     address =  address + (offsetAddress * 0x10) / 2;
                     if (address < bufLimit) { 
                        if (lineAddress & 0x0001)   /* is it an odd address? */
                                                    /* then set MSB of data  */
                            flashBuf[address] &= (data << 8) | 0x00ff;
                         else                       /* else set LSB of data  */
                            flashBuf[address] &= data | 0xff00;
                     } else {
                        return(1);
                     }
                     lineAddress++;
                     checksum += data;
                  }                     
                  checksum = (unsigned char) (0x100 - (unsigned char) checksum);
                  sscanf(&buffer[idx], "%2x", &tmp);
                  if (checksum != tmp)
                     return(1);
               }
            }
            fclose(flashBufPtr);
            return(0);
         }
      }
   }
   return(1);
}

/* Reads eeprom file in format address:data, stores data into         */
/* eepromBuf by address. Both numbers can be hexadecimal (0xC4),      */
/* decimal (196), octal (0304) or binary (B11000100).                 */

int readEepromFile(char eepromPath[], unsigned int eepromBuf[], long bufLimit) {

   FILE *eepromBufPtr;
   char wordstring[WORDLEN];
   long address;
   unsigned int data;
   char buffer[MAXLEN];
   char *dataPtr;
   long offsetAddress = 0L;
   int dataByteCount;
   unsigned int lineAddress;
   int recordType;
   int idx;
   int checksum;
   int tmp;
   
   if ((eepromBufPtr = fopen(eepromPath, "r")) == NULL) {
      return(1);
   } else {
      if (fgets(wordstring, WORDLEN, eepromBufPtr) != NULL) {
        /*
         * Try sp12 eeprom format.
         */ 
         if (strchr(wordstring, ':') != &wordstring[0]) {
            fseek(eepromBufPtr, 0, SEEK_SET);
            while (fgets(wordstring, WORDLEN, eepromBufPtr) != NULL) {
               if ((dataPtr = strchr(wordstring, ':')) == NULL) {
                  return(1);
               }
               dataPtr[0] = '\0';
               address =str2num(wordstring);
               data = str2num(++dataPtr);
               if (address < bufLimit)
                  eepromBuf[address] = data;
               else
                  return(1);
            }
            fclose(eepromBufPtr);
            return(0);
         }
        /*
         * Try Intel HEX format.
         */
         if (strchr(wordstring, ':') == &wordstring[0]) {
            fseek(eepromBufPtr, 0, SEEK_SET);
            while (fgets(buffer, MAXLEN, eepromBufPtr) != NULL) {
               checksum = 0;
               sscanf(&buffer[1], "%2x", &dataByteCount);
               checksum += dataByteCount;
               sscanf(&buffer[3], "%4x", &lineAddress);
               sscanf(&buffer[3], "%2x", &tmp);
               checksum += tmp;
               sscanf(&buffer[5], "%2x", &tmp);
               checksum += tmp;
               sscanf(&buffer[7], "%2x", &recordType);
               checksum += recordType;
              /*
               * Record type 02 means update offset address
               */
               if (recordType == 2) {
                  sscanf(&buffer[9], "%4lx", &offsetAddress);
                  sscanf(&buffer[9], "%2x", &tmp);
                  checksum += tmp;
                  sscanf(&buffer[11], "%2x", &tmp);
                  checksum += tmp;
                  checksum = (unsigned char) (0x100 - (unsigned char) checksum);
                  sscanf(&buffer[13], "%2x", &tmp);
                  if (checksum != tmp)
                     return(1);
               }
               if (recordType == 0) {
                  address = lineAddress;
                  address =  address + (offsetAddress * 0x10) / 2;
                 /*
                  * Use byte addresses for single byte eeprom data format.
                  */
                  for (idx = 9; idx < 9 + dataByteCount * 2; idx += 2) {
                     sscanf(&buffer[idx], "%2x", &data);
                     if (address < bufLimit)
                        eepromBuf[address] = data;
                     else
                        return(1);
                     address++;
                     checksum += data;
                  }
                  checksum = (unsigned char) (0x100 - (unsigned char) checksum);
                  sscanf(&buffer[idx], "%2x", &tmp);
                  if (checksum != tmp)
                     return(1);
               }
            }
            fclose(eepromBufPtr);
            return(0);
         }
      }
   }
   return(1);
}

/* Calculate checksum over buffer[]; int bufLimit is number of       */
/* elements in buffer.                                               */

unsigned int checksum(unsigned int buffer[], long bufLimit) {

   long idx;
   unsigned int chksum = 0;
   
  /* 
   * cyclic right shift 1 of chksum + buffer element 
   */
   for (idx = 0; idx < bufLimit; idx++) {
      if (buffer[idx] == 0xfff)
         buffer[idx] = 0xff;
      chksum = ((chksum>>1) + ((chksum & 1) ?0x8000:0) + buffer[idx]) & 0xffff;
   }
   return(chksum);
}

/* Routines to write the flash or eeprom buffer into a file,         */ 
/* using Intel HEX format or as a hex dump with ascii translation.   */

#define BYTES_PER_LINE  16

static int flash_intel_hex (FILE *filePtr, unsigned int *buffer, 
                                         long addr, long length) {

   int line_cnt = 0;                     /* line byte count          */
   int line_len = 0;                     /* expected line length     */
   unsigned char checksum = 0; 
   unsigned char byt;

   length *= 2;
   while (length > 0) {
      if (addr > 0xFFFF) {               /* add record type 02       */
         fprintf (filePtr, ":020000021000EC\n");
         addr = 0x10000 - addr;
      }
      if (line_cnt == 0) {               /* write line header        */
         line_len = (length > BYTES_PER_LINE) ? BYTES_PER_LINE : length;
         fprintf (filePtr, ":%02X%04lX00", line_len, addr);
         checksum = line_len;
         checksum += addr & 0xff;
         checksum += addr >> 8;
      }
     
      if (addr & 0x0001) {               /* is it an odd address?    */
         byt = *buffer >> 8;             /* write MSB                */
         buffer++;
      } else {
         byt = *buffer & 0x00ff;         /* write LSB                */
      }
      fprintf (filePtr, "%02X", byt);
      checksum += byt;
     
      if (++line_cnt == line_len) { /* write checksum                */
         fprintf (filePtr, "%02X\n", -checksum & 0xff);
         line_cnt = 0;
      }
      addr++;
      length--;
   }
   fprintf (filePtr, ":00000001FF\n");   /* write the terminator     */
   return (0);
}

static int eeprom_intel_hex (FILE *filePtr, unsigned int *buffer, 
                                       unsigned int addr, long length) {
   int line_cnt = 0;                     /* line byte count          */
   int line_len = 0;                     /* expected line length     */
   unsigned char checksum = 0;

   while (length > 0) {
      if (line_cnt == 0) {               /* write line header        */
         line_len = (length > BYTES_PER_LINE) ? BYTES_PER_LINE : length;
         fprintf (filePtr, ":%02X%04X00", line_len, addr);
         checksum = line_len;
         checksum += addr & 0xff;
         checksum += addr >> 8;
      }
      fprintf (filePtr, "%02X", *buffer);
      checksum += *buffer;
      if (++line_cnt == line_len) {      /* write checksum           */
         fprintf (filePtr, "%02X\n", -checksum & 0xff);
         line_cnt = 0;
      }
      buffer++;
      addr++;
      length--;
   }
   fprintf (filePtr, ":00000001FF\n");   /* write the terminator     */
   return (0);
}

int file_hex_ascii(FILE *filePtr, unsigned int buffer[], 
                                  int bufLimit, int twoByteFlag) {

   unsigned long address;
   char dataStr[MAXLEN];
   char asciiStr[MAXLEN];
   char lineBuf[MAXLEN];
   unsigned char loByte, hiByte;
   int addressPerLine = 0x10;
   
   dataStr[0] = '\0';
   asciiStr[0] = '\0';
   for (address = 0; address < bufLimit; address++) {
      if ((address > 0) && (address % addressPerLine) == 0) {
         if ((fprintf(filePtr, "%06lx  %s  %s\n",
              address - addressPerLine, dataStr, asciiStr)) == EOF)
            return(1);
         dataStr[0] = '\0';
         asciiStr[0] = '\0';
      }
      if (twoByteFlag) {
         addressPerLine = 0x08;
         loByte = (unsigned char) buffer[address];
         hiByte = (unsigned char) (buffer[address] >> 8);
         snprintf(lineBuf, sizeof(lineBuf), "%02x %02x ", hiByte, loByte);
         strlcat(dataStr, lineBuf, sizeof(dataStr));
         if ((hiByte > 31) && (hiByte < 127))
            snprintf(lineBuf, sizeof(lineBuf), "%c", hiByte);
         else
            snprintf(lineBuf, sizeof(lineBuf), ".");
         strlcat(asciiStr, lineBuf, sizeof(asciiStr));
         if ((loByte > 31) && (loByte < 127))
            snprintf(lineBuf, sizeof(lineBuf), "%c", loByte);
         else
            snprintf(lineBuf, sizeof(lineBuf), ".");
         strlcat(asciiStr, lineBuf, sizeof(asciiStr));
      } else {
         snprintf(lineBuf, sizeof(lineBuf), "%02x ", buffer[address]);
         strlcat(dataStr, lineBuf, sizeof(dataStr));
         if ((buffer[address] > 31) && (buffer[address] < 127))
            snprintf(lineBuf, sizeof(lineBuf), "%c", buffer[address]);
         else
            snprintf(lineBuf, sizeof(lineBuf), ".");
         strlcat(asciiStr, lineBuf, sizeof(asciiStr));
      }
   }
   if ((fprintf(filePtr, "%06lx  %s  %s\n",
        address - addressPerLine, dataStr, asciiStr)) == EOF)
      return(1);
   fprintf(filePtr, "Checksum: %04x\n", 
           checksum(buffer, bufLimit));
   return(0);
}

/* Writes flashBuf[] into FILE *flashBufPtr using either Intel HEX   */
/* or the `native' format.                                           */
/* If write to file fails, return(1), else return(0)                 */

int fileFlashBuf(unsigned int flashBuf[], long bufLimit, 
                 char flashPath[], int intel_flag, int hex_ascii_flag) {

   FILE *flashBufPtr;
   long address;

   if ((flashBufPtr = fopen(flashPath, "w")) == NULL) 
      return(1);

   if (hex_ascii_flag) {
      file_hex_ascii(flashBufPtr, flashBuf, bufLimit, 1);
   } else if (intel_flag) {
      flash_intel_hex(flashBufPtr, flashBuf, 0, bufLimit);
   } else {
      for (address = 0; address < bufLimit; address++) {
         if ((fprintf(flashBufPtr, "%06lx:%04x\n", 
                      address, flashBuf[address])) == EOF)
            return(1);
         }
      }
   fclose(flashBufPtr);
   return(0);
}

/* Writes eepromBuf[] into FILE *eepromBufPtr;                       */
/* If write to file fails, return(1), else return(0)                 */

int fileEepromBuf(unsigned int eepromBuf[], long bufLimit, 
                   char eepromPath[], int intel_flag, int hex_ascii_flag) {

   FILE *eepromBufPtr;
   long address;
   char binary[9] = "76543210";

   if ((eepromBufPtr = fopen(eepromPath, "w")) == NULL)
      return(1);

   if (hex_ascii_flag) {
      file_hex_ascii(eepromBufPtr, eepromBuf, bufLimit, 0);
   } else if (intel_flag) {
      eeprom_intel_hex(eepromBufPtr, eepromBuf, 0, bufLimit);
   } else {
      if ((fprintf(eepromBufPtr, 
                  "Address:  Data in hex,  dec, oct,  bin\n")) == EOF)
         return(1);
      for (address = 0; address < bufLimit; address++) {
         num2bin((unsigned char) eepromBuf[address], binary);
         if ((fprintf(eepromBufPtr, 
                      "   %#06lx:        %#04x  %3d  %#4o  %s\n", 
                        address, eepromBuf[address], eepromBuf[address], 
                        eepromBuf[address], binary)) == EOF)
            return(1);
      }
   }
   fclose(eepromBufPtr);
   return(0);
}

/* Writes buffer[] to stdout as a hex dump with ascii translation    */

void printBuffer(unsigned int buffer[], long bufLimit, int twoByteFlag) {

   long address;
   char dataStr[MAXLEN];
   char asciiStr[MAXLEN];
   char lineBuf[MAXLEN];
   unsigned char loByte, hiByte;
   int addressPerLine = 0x10;
   
   dataStr[0] = '\0';
   asciiStr[0] = '\0';
   for (address = 0; address < bufLimit; address++) {
      if ((address > 0) && (address % addressPerLine) == 0) {
         printf("%06lx  %s  %s\n", address - addressPerLine, dataStr, asciiStr);
         dataStr[0] = '\0';
         asciiStr[0] = '\0';
      }
      if (twoByteFlag) {
         addressPerLine = 0x08;
         loByte = (unsigned char) buffer[address];
         hiByte = (unsigned char) (buffer[address] >> 8);
         snprintf(lineBuf, sizeof(lineBuf), "%02x %02x ", hiByte, loByte);
         strlcat(dataStr, lineBuf, sizeof(dataStr));
         if ((hiByte > 31) && (hiByte < 127))
            snprintf(lineBuf, sizeof(lineBuf), "%c", hiByte);
         else
            snprintf(lineBuf, sizeof(lineBuf), ".");
         strlcat(asciiStr, lineBuf, sizeof(asciiStr));
         if ((loByte > 31) && (loByte < 127))
            snprintf(lineBuf, sizeof(lineBuf), "%c", loByte);
         else
            snprintf(lineBuf, sizeof(lineBuf), ".");
         strlcat(asciiStr, lineBuf, sizeof(asciiStr));
      } else {
         snprintf(lineBuf, sizeof(lineBuf), "%02x ", buffer[address]);
         strlcat(dataStr, lineBuf, sizeof(dataStr));
         if ((buffer[address] > 31) && (buffer[address] < 127))
            snprintf(lineBuf, sizeof(lineBuf), "%c", buffer[address]);
         else
            snprintf(lineBuf, sizeof(lineBuf), ".");
         strlcat(asciiStr, lineBuf, sizeof(asciiStr));
      }
   }
   printf("%06lx  %s  %s\n", address - addressPerLine, dataStr, asciiStr);
   printf("Checksum: %04x\n", checksum(buffer, bufLimit));
}

/* Add spaces until lenght or max is reached                         */
/* Try to break line if necessary                                    */

void formatStr(char *logStr, int length, int max) {

   int idx;
   int limit;
   int idxL = 0;

   limit = length;
   for (idx = 0; idx < limit; idx++) {
      if (idx == max) {
         logStr[idx] = '\0';
         break;
      }
      if ((++idxL > length / 2) && logStr[idx] == ' ') {
         logStr[idx] = '\n';
         limit = limit + idxL;
         idxL = 0;
      }
      if (logStr[idx] == '\0') {
         logStr[idx] = ' ';
         logStr[idx + 1] = '\0';
      }
   }
}

/* Add a line to the log about a write command                       */

int logWrites(char *commandStr, unsigned int address, int data, 
     char *path, unsigned int buffer[], int overlayFlag, int *error) {

   FILE *logPtr;
   time_t curTime;
   struct tm *locTime;
   unsigned int areaChksum = 0;
   unsigned int *queryBuf;
   int idx;
   long adrsCnt;
   char deviceName[MAXLEN];
   char fileName[MAXLEN];
   char *fileNamePtr;
   
   if (logging.logging != 1)
      return(0);

   formatStr(commandStr, 6, MAXLEN);
   strlcpy(deviceName, device.name, sizeof(deviceName));
   if (strcmp(deviceName, "AT90(L)S2343 or Tiny22(L)") == 0)
      strlcpy(deviceName, "2343/Tiny22", sizeof(deviceName));
   formatStr(deviceName, 12, MAXLEN);
   queryBuf = malloc(FLASHBUF_UPPERLIMIT * sizeof (unsigned int));
   if (queryBuf == NULL) {
      printf ("ERROR: No memory available for buffer!\n");
      return(0);
   }

   curTime = time(NULL);
   locTime = localtime(&curTime);

   if ((logPtr = fopen(logging.logPath, "a")) == NULL) {
      free(queryBuf);
      return(1);
   }
   fprintf(logPtr, "%s %s ", deviceName, commandStr);
   if (strchr(commandStr, 'f') != NULL) {
      strlcpy(fileName, path, sizeof(fileName));
      fileNamePtr = fileName;
      idx = strlen(fileName);
#if defined(__WIN32__)
      while (idx-- > 0 && fileName[idx] != '\\')
#else
      while (idx-- > 0 && fileName[idx] != '/')
#endif
         fileNamePtr = &fileName[idx];
      formatStr(fileNamePtr, 14, MAXLEN - idx);
      fprintf(logPtr, "%s   ", fileNamePtr);
      if (logging.query) {
         if (strchr(commandStr, 'p') != NULL) {
            readFlashArea(queryBuf, device.flashLimit);
            areaChksum = checksum(queryBuf, device.flashLimit);
            *error = 0;
            for (adrsCnt = 0; adrsCnt < device.flashLimit; adrsCnt++) {
               if (overlayFlag && buffer[adrsCnt] == 0xffff)
                  continue;
               if (queryBuf[adrsCnt] != buffer[adrsCnt]) {
                  *error = 1;
                  break;
               }
            }
         } else {
            readEepromArea(queryBuf, device.eepromLimit);
            areaChksum = checksum(queryBuf, device.eepromLimit);
            *error = 0;
            for (adrsCnt = 0; adrsCnt < device.eepromLimit; adrsCnt++) {
               if (overlayFlag && buffer[adrsCnt] > 0xFF)
                  continue;
               if (queryBuf[adrsCnt] != (unsigned char) buffer[adrsCnt]) {
                  *error = 1;
                  break;
               }
            }
         }
         if (*error)
            fprintf(logPtr, "readback ERROR  %s", asctime(locTime));
         else
            fprintf(logPtr, "checksum %04x   %s", areaChksum, asctime(locTime));
      } else {
         fprintf(logPtr, "not queried     %s", asctime(locTime));
      }   
   } else {
      if (strchr(commandStr, 'p') != NULL)
         fprintf(logPtr, "%08x:%#06x  ", address, data);
      else
         fprintf(logPtr, "%08x:%#04x    ", address, data);
      if (*error)
         fprintf(logPtr, "readback ERROR  %s", asctime(locTime));
      else
         fprintf(logPtr, "verified        %s", asctime(locTime));
   }
   fclose(logPtr);
   free(queryBuf);
   return(0);
}

/* Add a line to the log about a lock command                        */

int logLocks(unsigned int buffer[], char *lockBits) {

   FILE *logPtr;
   time_t curTime;
   struct tm *locTime;
   unsigned int areaChksum = 0;
   char deviceName[MAXLEN];
   char lBits[MAXLEN] = "";

   if (logging.logging != 1)
      return(0);

   snprintf(lBits, sizeof(lBits), "%02lx", strtol(lockBits, NULL, 2));

   strlcpy(deviceName, device.name, sizeof(deviceName));
   formatStr(deviceName, 12, MAXLEN);
   curTime = time(NULL);
   locTime = localtime(&curTime);

   if ((logPtr = fopen(logging.logPath, "a")) == NULL)
      return(1);
   fprintf(logPtr, "%s -L%s   ", deviceName, lBits);
   readFlashArea(buffer, device.flashLimit);
   areaChksum = checksum(buffer, device.flashLimit);
   fprintf(logPtr, "flash %04x       ", areaChksum);
   readEepromArea(buffer, device.eepromLimit);
   areaChksum = checksum(buffer, device.eepromLimit);
   fprintf(logPtr, "eeprom %04x     %s", areaChksum, asctime(locTime));
   fclose(logPtr);
   return(0);
}