File: makehex.c

package info (click to toggle)
netboot 0.8.1-9
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,636 kB
  • ctags: 4,744
  • sloc: ansic: 15,152; asm: 11,623; yacc: 2,248; makefile: 1,117; pascal: 1,108; lex: 748; sh: 233
file content (205 lines) | stat: -rw-r--r-- 4,825 bytes parent folder | download | duplicates (3)
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
/*
 * makehex.c  -  Generate hex boot image file
 *
 * Copyright (C) 1998 Gero Kuhlmann   <gero@gkminix.han.de>
 *
 *  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
 *  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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "common.h"
#include "nblib.h"
#include "makerom.h"



/* Size of one hex record */
#define IHEX_RECSIZE	16
#define MHEX_RECSIZE	16
#define THEX_RECSIZE	16



/*
 * Read one byte from temporary rom image file
 */
static int readbyte(romimage)
int romimage;
{
  static unsigned char buf[BLKSIZE];
  static int buflen = 0;
  static int bufofs = 0;

  if (bufofs >= buflen) {
	if ((buflen = nbread(buf, BLKSIZE, romimage)) == 0)
		return(EOF);
	bufofs = 0;
  }
  return(buf[bufofs++]);
}



/*
 * Return a nibble in ASCII hex
 */
static char hex(c)
int c;
{
  if((c &= 0x000f) < 10)
	c += '0';
  else
	c += 'A' - 10;
  return((char) c);
}



/*
 * Convert bootrom image to Intel hex format
 */
void makeihex(fname, romimage)
char *fname;
int romimage;
{
  char *line, buffer[128];
  FILE *outfile;
  int address = 0;
  int c = 1;
  int sum, i;

  /* Open the input and output files */
  if ((outfile = fopen(fname, "w")) == NULL) {
	perror(fname);
	exit(EXIT_CREATE);
  }

  /* Process the boot rom image */
  fprintf(outfile, "# netboot bootrom image (Intel hex)\n");
  fprintf(outfile, "# created with %s, %s\n", progname, VERSION);
  fprintf(outfile, ":020000020000FC\n");
  while (c != EOF) {
	sum = 0;
	line = buffer;
	for (i = 0; i < IHEX_RECSIZE && (c = readbyte(romimage)) != EOF; i++) {
		*line++ = hex(c >> 4);
		*line++ = hex(c);
		sum += c;			/* checksum each character */
	}
	*line = '\0';
	if (i) {
		sum += (address >> 8) & 0xff;	/* checksum high address byte */
		sum += address & 0xff;		/* checksum low address byte */
		sum += i;			/* checksum record byte count */
		fprintf(outfile, ":%02X%04X00%s%02X\n", i,
				(address & 0xffff), buffer, ((0 - sum) & 0xff));
	}
	address += i;
  }
  fprintf(outfile, ":00000001FF\n");		/* end record */
  fclose(outfile);
}



/*
 * Convert bootrom image to Motorola hex format
 */
void makemhex(fname, romimage)
char *fname;
int romimage;
{
  char *line, buffer[128];
  FILE *outfile;
  int address = 0;
  int c = 1;
  int sum, i;

  /* Open the input and output files */
  if ((outfile = fopen(fname, "w")) == NULL) {
	perror(fname);
	exit(EXIT_CREATE);
  }

  /* Process the boot rom image */
  fprintf(outfile, "# netboot bootrom image (Motorola hex)\n");
  fprintf(outfile, "# created with %s, %s\n", progname, VERSION);
  while (c != EOF) {
	sum = 0;
	line = buffer;
	for (i = 0; i < MHEX_RECSIZE && (c = readbyte(romimage)) != EOF; i++) {
		*line++ = hex(c >> 4);
		*line++ = hex(c);
		sum += c;			/* checksum each character */
	}
	*line = '\0';
	if (i) {
		sum += (address >> 8) & 0xff;	/* checksum high address byte */
		sum += address & 0xff;		/* checksum low address byte */
		sum += i + 3;			/* checksum record byte count */
		fprintf(outfile, "S1%02X%04X%s%02X\n", i + 3,
				(address & 0xffff), buffer, ((0 - sum) & 0xff));
	}
	address += i;
  }
  fprintf(outfile, "S9030000FD\n");		/* end record */
  fclose(outfile);
}



/*
 * Convert bootrom image to Tektronix hex format
 */
void makethex(fname, romimage)
char *fname;
int romimage;
{
  char *line, buffer[128];
  FILE *outfile;
  int address = 0;
  int c = 1;
  int psum, dsum, i;

  /* Open the input and output files */
  if ((outfile = fopen(fname, "w")) == NULL) {
	perror(fname);
	exit(EXIT_CREATE);
  }

  /* Process the boot rom image */
  fprintf(outfile, "# netboot bootrom image (Tektronix hex)\n");
  fprintf(outfile, "# created with %s, %s\n", progname, VERSION);
  while (c != EOF) {
	psum = dsum = 0;
	line = buffer;
	for (i = 0; i < THEX_RECSIZE && (c = readbyte(romimage)) != EOF; i++) {
		*line++ = hex(c >> 4);
		*line++ = hex(c);
		dsum += (c & 0x0f) + ((c >> 4) & 0x0f);
	}
	*line = '\0';
	if (i) {
		psum += (i & 0x0f) + ((i >> 4) & 0x0f);
		psum += (address & 0x0f) + ((address >> 4) & 0x0f);
		psum += ((address >> 8) & 0x0f) + ((address >> 12) & 0x0f);
		fprintf(outfile, "/%04X%02X%02X%s%02X\n", (address & 0xffff), i,
				(psum & 0xff), buffer, (dsum & 0xff));
	}
	address += i;
  }
  fprintf(outfile, "/00000000\n");		/* end record */
  fclose(outfile);
}