OpenCores
URL https://opencores.org/ocsvn/eus100lx/eus100lx/trunk

Subversion Repositories eus100lx

[/] [eus100lx/] [trunk/] [tools/] [cris-boot/] [mcs.c] - Blame information for rev 7

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 freza
/* $Id: mcs.c,v 1.1.1.1 2006-02-04 03:35:01 freza Exp $ */
2
 
3
#include <sys/types.h>
4
#include <errno.h>
5
#include <stdio.h>
6
#include <stdlib.h>
7
 
8
static int
9
decode_byte(FILE *file, u_int8_t *dst)
10
{
11
        int c;
12
 
13
        if ((c = fgetc(file)) == EOF)
14
                return EINVAL;
15
 
16
        if (c >= '0' && c <= '9')
17
                *dst = (c - '0') << 4;
18
        else if (c >= 'a' && c <= 'f')
19
                *dst = ((c - 'a') + 10) << 4;
20
        else if (c >= 'A' && c <= 'F')
21
                *dst = ((c - 'A') + 10) << 4;
22
        else
23
                return EINVAL;
24
 
25
        if ((c = fgetc(file)) == EOF)
26
                return EINVAL;
27
 
28
        if (c >= '0' && c <= '9')
29
                *dst |= (c - '0');
30
        else if (c >= 'a' && c <= 'f')
31
                *dst |= ((c - 'a') + 10);
32
        else if (c >= 'A' && c <= 'F')
33
                *dst |= ((c - 'A') + 10);
34
        else
35
                return EINVAL;
36
 
37
        return 0;
38
}
39
 
40
#define decode_uint8(dst)       \
41
        do {                                                            \
42
                if (decode_byte(file, &(dst)) < 0)                       \
43
                        goto __error;                                   \
44
        } while (0)
45
 
46
#define decode_uint16(dst)      \
47
        do {                                                            \
48
                if (decode_byte(file, &x) < 0)                           \
49
                        goto __error;                                   \
50
                (dst) = x << 8;                                         \
51
                                                                        \
52
                if (decode_byte(file, &x) < 0)                           \
53
                        goto __error;                                   \
54
                (dst) |= x;                                             \
55
        } while (0)
56
 
57
int
58
mcsdecode(FILE *file, u_int8_t **data, size_t *num)
59
{
60
        u_int16_t       lsb, msb;
61
        u_int8_t        bytes, type, x, val;
62
        int             ret;
63
 
64
        *data   = NULL;
65
        ret     = EINVAL;
66
        *num    = 0;
67
        lsb     = 0;
68
        msb     = 0;
69
 
70
        /*
71
         * Every line begins with ':'.
72
         */
73
        if (fgetc(file) != ':')
74
                return EINVAL;
75
 
76
        for(;;) {
77
                decode_uint8(bytes);    /* Line length */
78
                decode_uint16(lsb);     /* Address LSB */
79
                decode_uint8(type);     /* Record type */
80
 
81
                switch (type) {
82
                case 0x00:
83
                        /*
84
                         * Data item.
85
                         */
86
                        break;
87
                case 0x01:
88
                        /*
89
                         * Last record.
90
                         */
91
                        return 0;
92
                case 0x04:
93
                        /*
94
                         * Address MSB.
95
                         */
96
                        decode_uint16(msb);
97
                        bytes -= 2;
98
                        break;
99
                }
100
 
101
                *data = (u_int8_t *) realloc(*data, *num + bytes);
102
                if (*data == NULL) {
103
                        ret = ENOMEM;
104
                        goto __error;
105
                }
106
 
107
                /*
108
                 * Sanity check
109
                 */
110
                if (((((u_int32_t)msb) << 16) | lsb) != *num)
111
                        goto __error;
112
 
113
                while (bytes-- > 0) {
114
                        /*
115
                         * Read a byte of data.
116
                         */
117
                        decode_uint8(val);
118
                        (*data)[*num] = val;
119
                        (*num)++;
120
                }
121
 
122
#if 0
123
                /*
124
                 * Each line has a CRC.
125
                 */
126
                decode_uint8(crc);
127
#endif
128
 
129
                /*
130
                 * Skip newline (may be DOS-ish).
131
                 */
132
                do {
133
                        x = fgetc(file);
134
                } while (x != ':');
135
        }
136
 
137
        /* Bail out if we failed. */
138
__error:
139
        if (*data != NULL) {
140
                free(*data);
141
                *data = NULL;
142
        }
143
 
144
        *num = 0;
145
        return ret;
146
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.