File: intelhex.c

package info (click to toggle)
avrprog 0.2.2-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, lenny, squeeze, wheezy
  • size: 448 kB
  • ctags: 97
  • sloc: ansic: 990; makefile: 62; sh: 13
file content (158 lines) | stat: -rw-r--r-- 5,043 bytes parent folder | download
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
/*
Copyright (C) 2000-2001 Jos Roberto B. de A. Monteiro <jrm@autsens.com>
                        and Pedro Zorzenon Neto <pzn@autsens.com>

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.
*/

#include "intelhex.h"
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <string.h>

int intelhex_fileopen (inteldata *id, char *filename, int mode) {
    /* mode = 1 -> open file for reading */
    /*       0 -> creates a new file for writing */

    /* TODO: please, check if the file already exists before overwriting */
    if (mode==0) {
	id->file=fopen(filename,"w");
	if (id->file==NULL) {
	    printf("File create error\n");
	    abort(); }
        id->eof_found=0;
        return 1;
    }
    id->file=fopen(filename,"r+");
    if (id->file==NULL) {
	printf("Error when opening the file '%s' for reading.\n",filename);
	abort(); }
    id->eof_found=0;
    return 1;
}

int intelhex_fileclose (inteldata * id) {
    return fclose(id->file);
}

int intelhex_readline (inteldata * id) {
    /* returns:
         0 EOF found
         1 line read ok
         >1 some error occoured
    */
    char line[526], conv[5];
    int rectype, chksum, linelen, reccount, i;

    /* verifies eof and eof_record */
    if (id->eof_found==1) return 0; /* eof_record found */
    if (feof(id->file)) {
	printf("error in intelhex file - eof_record not found\n");
	return 2; }

    /* reads a line from stdin (hex file format) */
    fgets(line, 525, id->file);

    /* changes CR+LF to FL if needed and checks if LF is present */
    linelen=strlen(line);
    if (line[linelen-2] == 13)
    {
	line[linelen-2]=line[linelen-1];
	line[linelen-1]=0;
	linelen=strlen(line);
    }
    if (line[linelen-1] != 10) {
	printf("error in intelhex file - linefeed not found at end of line\n");
	return 3; }

    /* checks for record mark */
    if (line[0] != 0x3a) {
	printf("error in intelhex file - ':' missing\n"); return 4; }

    /* verifies the minimum linelength */
    if (linelen < 12) {
	printf("error in intelhex file - less than 11 characters in a line\n");
	return 5; }

    /* verifies the checksum */
    conv[2]=0; chksum=0;
    for(i=1; i<(linelen-1); i+=2) {
	conv[0]=line[i]; conv[1]=line[i+1];
	chksum+=(int)strtoul(conv,(char **)NULL,16); }
    if (chksum % 256) {
	printf("error in intelhex file - bad chksum\n"); return 6; }

    /* reads record length */
    conv[2]=0; i=1;
    conv[0]=line[i]; conv[1]=line[i+1]; i+=2;
    id->reclen=(int)strtoul(conv,(char **)NULL,16);

    /* checks if linelength is (reclength*2 + 12) */
    if (((id->reclen*2)+12) != linelen) {
	printf("error in intelhex file - number of characters doesn't match reclength\n");
	return 7; }

    /* reads load offset */
    conv[4]=0; conv[0]=line[i]; conv[1]=line[i+1];
    conv[2]=line[i+2]; conv[3]=line[i+3]; i+=4;
    id->offset=(int)strtoul(conv,(char **)NULL,16);

    /* reads record type */
    conv[2]=0; conv[0]=line[i]; conv[1]=line[i+1]; i+=2;
    rectype=(int)strtoul(conv,(char **)NULL,16);

    /* returns if rectype is 01 (eof_record) */
    if(rectype==1) {
	id->eof_found=1;
	return 0; /* eof_record found */ }

    /* checks if rectype is 00 */
    if(rectype!=0) {
	printf("error in intelhex file - this program only accepts rectype 00(data) or 01(eof)\n");
	return 8; }

    /* reads record data */
    for(reccount=0; reccount<id->reclen; reccount++) {
	conv[0]=line[i]; conv[1]=line[i+1]; i+=2;
	id->recdata[reccount]=(int)strtoul(conv,(char **)NULL,16); }
    return 1; /* successfull read - data available */
}

int intelhex_writeline (inteldata * id) {
    unsigned int chksum;
    int reccount;
    if (id->reclen==0 || id->reclen>255 || id->offset<0 || id->offset>0xffff) {
	printf("error in intelhex file - offset/reclen invalid when writing file\n");
	return 0;
    }
    fprintf(id->file, ":%02X%04X00", id->reclen, id->offset);
    chksum = id->reclen; /* reclength */
    chksum += id->offset % 256; /* low 8 bits of load offset */
    chksum += id->offset / 256; /* high 8 bits of load offset */
    /* chksum += 0x00; //rectype */
    for (reccount=0; reccount<id->reclen; reccount++) {
	fprintf(id->file, "%02X", id->recdata[reccount]);
	chksum += id->recdata[reccount];
    }
    chksum = 256 - (chksum % 256);
    if (chksum==256) chksum=0;
    fprintf(id->file, "%02X\n", chksum);
    return 1;
}

void intelhex_writeeof (inteldata * id) {
    fputs(":00000001FF\n", id->file);
}