summaryrefslogtreecommitdiff
path: root/src/hal/utils/bitfile.c
blob: 44f82ac923e822ef9ffa4681aa1f42ede5594f1f (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
/*************************************************************************
*
* bitfile - a library reading and writing Xilinx bitfiles
*
* Copyright (C) 2007 John Kasunich (jmkasunich at fastmail dot fm)
*
*
**************************************************************************

This program is free software; you can redistribute it and/or
modify it under the terms of version 2 of the GNU General
Public License as published by the Free Software Foundation.
This library 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 library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111 USA

THE AUTHORS OF THIS LIBRARY ACCEPT ABSOLUTELY NO LIABILITY FOR
ANY HARM OR LOSS RESULTING FROM ITS USE.  IT IS _EXTREMELY_ UNWISE
TO RELY ON SOFTWARE ALONE FOR SAFETY.  Any machinery capable of
harming persons must have provisions for completely removing power
from all motors, etc, before persons enter any danger area.  All
machinery must be designed to comply with local and national safety
codes, and the authors of this software can not, and do not, take
any responsibility for such compliance.

This code was written as part of the EMC HAL project.  For more
information, go to www.linuxcnc.org.

************************************************************************/

#define _GNU_SOURCE /* getline() */
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <linux/types.h>
#include "bitfile.h"

#define XILINX_CHUNKS "abcde"

static const unsigned char header[BITFILE_HEADERLEN] = {
    0x00, 0x09,
    0x0f, 0xf0, 0x0f, 0xf0,
    0x0f, 0xf0, 0x0f, 0xf0,
    0x00, 0x00, 0x01
};

/************************************************************************/

static void errmsg(const char *funct, const char *fmt, ...)
{
    va_list vp;

    va_start(vp, fmt);
    fprintf(stderr, "ERROR in %s(): ", funct);
    vfprintf(stderr, fmt, vp);
    fprintf(stderr, "\n");
    va_end(vp);
}

struct bitfile *bitfile_new(void)
{
    struct bitfile *bf;
    int n;

    bf = (struct bitfile *)malloc(sizeof(struct bitfile));
    if ( bf == NULL ) {
	errmsg(__func__,"malloc failure");
	return NULL;
    }
    bf->filename = NULL;
    /* standard header */
    for ( n = 0 ; n < BITFILE_HEADERLEN ; n++ ) {
	bf->header[n] = header[n];
    }
    for ( n = 0 ; n < BITFILE_MAXCHUNKS ; n++ ) {
	bf->chunks[n].tag = '\0';
	bf->chunks[n].len = 0;
	bf->chunks[n].body = NULL;
    }
    bf->num_chunks = 0;
    return bf;
}

void bitfile_free(struct bitfile *bf)
{
    int n;

    if (bf->filename != NULL) {
        free(bf->filename);
    }

    for ( n = 0 ; n < BITFILE_MAXCHUNKS ; n++ ) {
	if ( bf->chunks[n].body != NULL ) {
	    free(bf->chunks[n].body);
	}
    }
    free(bf);
}

static int read_chunk(int fd, struct bitfile_chunk *ch)
{
    int len_len, rv;
    __u8 lenbuf[4];

    /* read tag */
    rv = read(fd, &(ch->tag), 1);
    if ( rv != 1 ) {
	/* end of file is not an error */
	return 1;
    }
    if ( strchr(BITFILE_SMALLCHUNKS, ch->tag) != NULL ) {
	/* its a small chunk, 2 byte size */
	len_len = 2;
    } else {
	/* regular chunk, 4 byte size */
	len_len = 4;
    }
    /* read length */
    rv = read(fd, lenbuf, len_len);
    if ( rv != len_len ) {
	errmsg(__func__,"reading length: %s", strerror(errno));
	return -1;
    }
    /* compute size (note - the format uses big-endian layout */
    if ( len_len == 4 ) {
	ch->len = (int)( ((__u32)(lenbuf[0]) << 24 ) |
			 ((__u32)(lenbuf[1]) << 16 ) |
			 ((__u32)(lenbuf[2]) << 8 ) |
			  (__u32)(lenbuf[3]) );
    } else {
	ch->len = (int)( ((__u32)(lenbuf[0]) << 8 ) |
			  (__u32)(lenbuf[1]) );
    }
    /* allocate space for chunk content */
    ch->body = (__u8 *)malloc(ch->len);
    if ( ch->body == NULL ) {
	errmsg(__func__,"malloc failure");
	return -1;
    }
    /* read chunk content */
    rv = read(fd, ch->body, ch->len);
    if ( rv != ch->len ) {
	errmsg(__func__,"reading content: %s", strerror(errno));
	return -1;
    }
    return 0;
}

struct bitfile *bitfile_read(char *fname)
{
    struct bitfile *bf;
    int fd, rv, n;
    unsigned char c;

    /* create the struct */
    bf = bitfile_new();
    if ( bf == NULL ) {
	errmsg(__func__,"creating struct");
	return NULL;
    }
    /* open the file */
    fd = open(fname, O_RDONLY);
    if ( fd < 0 ) {
	errmsg(__func__,"opening file: %s", strerror(errno));
	goto cleanup0;
    }
    /* first BITFILE_HEADERLEN bytes are a header */
    rv = read(fd, bf->header, BITFILE_HEADERLEN);
    if ( rv < BITFILE_HEADERLEN ) {
	errmsg(__func__,"reading header: %s", strerror(errno));
	goto cleanup1;
    }
    /* test header */
    for ( n = 0 ; n < BITFILE_HEADERLEN ; n++ ) {
	if ( bf->header[n] != header[n] ) {
	    break;
	}
    }
    if ( n != BITFILE_HEADERLEN ) {
	errmsg(__func__,"header mismatch, '%s' is not a bitfile?", fname);
	goto cleanup1;
    }
    /* read chunks */
    while ( bf->num_chunks < BITFILE_MAXCHUNKS ) {
	rv = read_chunk(fd, &(bf->chunks[bf->num_chunks]));
	if ( rv < 0 ) {
	    errmsg(__func__,"reading chunk %d", bf->num_chunks);
	    goto cleanup1;
	}
	if ( rv > 0 ) {
	    /* end of file */
	    break;
	}
	bf->num_chunks++;
    }
    /* is anything left in the file? */
    rv = read(fd, &c, 1);
    if ( rv == 1 ) {
	errmsg(__func__,"more than %d chunks", BITFILE_MAXCHUNKS);
	goto cleanup1;
    }

    // save the filename
    bf->filename = strdup(fname);
    if (bf->filename == NULL) {
        errmsg(__func__, "out of memory\n");
        goto cleanup1;
    }

    /* done */
    close (fd);
    return bf;
cleanup1:
    close(fd);
cleanup0:
    bitfile_free(bf);
    return NULL;
}

static int write_chunk(int fd, struct bitfile_chunk *ch)
{
    int len_len, rv;
    __u8 hdrbuf[5];

    hdrbuf[0] = ch->tag;
    if ( strchr(BITFILE_SMALLCHUNKS, ch->tag) != NULL ) {
	/* its a small chunk, 2 byte size */
	len_len = 2;
    } else {
	/* regular chunk, 4 byte size */
	len_len = 4;
    }
    /* compute size (note - the format uses big-endian layout */
    if ( len_len == 4 ) {
	hdrbuf[1] = (ch->len >> 24) & 0xFF;
	hdrbuf[2] = (ch->len >> 16) & 0xFF;
	hdrbuf[3] = (ch->len >> 8) & 0xFF;
	hdrbuf[4] = ch->len & 0xFF;
    } else {
	hdrbuf[1] = (ch->len >> 8) & 0xFF;
	hdrbuf[2] = ch->len & 0xFF;
    }
    /* write tag and length */
    rv = write(fd, hdrbuf, len_len+1);
    if ( rv != len_len+1 ) {
	errmsg(__func__,"writing headerr: %s", ch->tag, strerror(errno));
	return -1;
    }
    /* write chunk content */
    rv = write(fd, ch->body, ch->len);
    if ( rv != ch->len ) {
	errmsg(__func__,"writing content: %s", strerror(errno));
	return -1;
    }
    return 0;
}

int bitfile_write(struct bitfile *bf, char *fname)
{
    int fd, rv, n;
    char *cp;
    struct bitfile_chunk *ch;

    /* open the file */
    fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 00644 );
    if ( fd < 0 ) {
	errmsg(__func__,"opening file: %s", strerror(errno));
	return -1;
    }
    /* write the header */
    rv = write(fd, bf->header, BITFILE_HEADERLEN);
    if ( rv < BITFILE_HEADERLEN ) {
	errmsg(__func__,"writing header: %s", strerror(errno));
	goto cleanup0;
    }
    /* write xilinx chunks in preferred order */
    cp = XILINX_CHUNKS;
    while ( *cp != '\0' ) {
	ch = bitfile_find_chunk(bf, *cp, 0);
	if ( ch != NULL ) {
	    rv = write_chunk(fd, ch);
	    if ( rv < 0 ) {
		errmsg(__func__,"writing chunk '%c'", ch->tag);
		goto cleanup0;
	    }
	}
	cp++;
    }
    /* write non-xilinx chunks */
    for ( n = 0 ; n < bf->num_chunks ; n++ ) {
	ch = &(bf->chunks[n]);
	if ( strchr(XILINX_CHUNKS, ch->tag) == NULL ) {
	    /* not a xilinx chunk */
	    rv = write_chunk(fd, ch);
	    if ( rv < 0 ) {
		errmsg(__func__,"writing chunk '%c'", ch->tag);
		goto cleanup0;
	    }
	}
    }
    /* done */
    close(fd);
    return 0;
cleanup0:
    close(fd);
    return -1;
}

int bitfile_add_chunk(struct bitfile *bf, char tag, int len, unsigned char *data)
{
    int n;

    if (( strchr(BITFILE_SMALLCHUNKS, tag) != NULL ) &&
	( len > 0xFFFF )) {
	errmsg(__func__,"chunk is too large (%d bytes)", len);
	return -1;
    }
    n = bf->num_chunks;
    if ( n >= (BITFILE_MAXCHUNKS-1) ) {
	errmsg(__func__,"too many chunks");
	return -1;
    }
    bf->chunks[n].body = malloc(len);
    if ( bf->chunks[n].body == NULL ) {
	errmsg(__func__,"malloc failure");
	return -1;
    }
    bf->chunks[n].tag = tag;
    bf->chunks[n].len = len;
    memcpy(bf->chunks[n].body, data, len);
    bf->num_chunks++;
    return 0;
}

struct bitfile_chunk *bitfile_find_chunk(struct bitfile *bf, char tag, int n)
{
    int i;

    for ( i = 0 ; i < bf->num_chunks ; i++ ) {
	if ( bf->chunks[i].tag == tag ) {
	    if ( n == 0 ) {
		return &(bf->chunks[i]);
	    }
	    n--;
	}
    }
    return NULL;
}

void bitfile_print_chunk(struct bitfile *bf, char tag, char *title)
{
    struct bitfile_chunk *ch;

    ch = bitfile_find_chunk(bf, tag, 0);
    if ( ch != NULL ) {
	printf("%s%s\n", title, ch->body);
    }
}

int bitfile_validate_xilinx_info(struct bitfile *bf)
{
    char *cp;

    cp = XILINX_CHUNKS;
    while (*cp != '\0' ) {
	if ( bitfile_find_chunk(bf, *cp, 0) == NULL ) {
	    return -1;
	}
	cp++;
    }
    return 0;
}

void bitfile_print_xilinx_info(struct bitfile *bf)
{
    struct bitfile_chunk *ch;

    bitfile_print_chunk(bf, 'a', "Design name:     ");
    bitfile_print_chunk(bf, 'b', "Part ID:         ");
    bitfile_print_chunk(bf, 'c', "Design date:     ");
    bitfile_print_chunk(bf, 'd', "Design time:     ");
    ch = bitfile_find_chunk(bf, 'e', 0);
    if ( ch != NULL ) {
	printf ( "Bitstream size:  %d\n", ch->len );
    }
}