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

Subversion Repositories tinyvliw8

[/] [tinyvliw8/] [trunk/] [tools/] [objcopy/] [src/] [main.c] - Blame information for rev 8

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 8 steckol
/**
2
 * \file   main.c
3
 * \author Oliver Stecklina <stecklina@ihp-microelectronics.com>
4
 * \date   12.12.2015
5
 *
6
 * \brief  tinyVLIW8 objcopy program
7
 *
8
 * <p>
9
 *    Copyright (C) 2015 IHP GmbH, Frankfurt (Oder), Germany
10
 *
11
 * This code is free software. It is licensed under the EUPL, Version 1.1
12
 * or - as soon they will be approved by the European Commission - subsequent
13
 * versions of the EUPL (the "License").
14
 * You may redistribute this code and/or modify it under the terms of this
15
 * License.
16
 * You may not use this work except in compliance with the License.
17
 * You may obtain a copy of the License at:
18
 *
19
 * http://joinup.ec.europa.eu/software/page/eupl/licence-eupl
20
 *
21
 * Unless required by applicable law or agreed to in writing, software
22
 * distributed under the License is distributed on an "AS IS" basis,
23
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
 * See the License for the specific language governing permissions and
25
 * limitations under the License.
26
 * </p>
27
 */
28
 
29 5 steckol
#include <sys/stat.h>
30
#include <stdio.h>
31
#include <string.h>
32
#include <unistd.h>
33
#include <fcntl.h>
34
#include <stdlib.h>
35
#include <errno.h>
36
 
37
typedef enum _bfd_e {
38
        _bfd_ascii = 0
39
} _bfd_t;
40
 
41
static unsigned char  _mem_width = 4;
42
static char          *_infile    = NULL;
43
static char          *_outfile   = NULL;
44
static _bfd_t         _bfdname   = _bfd_ascii;
45
 
46
static int _parse_args(int argc, char *argv[])
47
{
48
        int err, i;
49
 
50
        err = 0;
51
        for (i = 1; 0 == err && i < argc; i++) {
52
                if ('-' != *argv[i]) {
53
                        if ((i + 1) == argc) {
54
                                _infile = argv[i];
55
                                break;
56
                        }
57
 
58
                        fprintf(stderr, "\tERROR! bad option <%s>\n", argv[i]);
59
                        err = -EINVAL;
60
 
61
                        break;
62
                }
63
 
64
                switch (*(argv[i] + 1)) {
65
                case 'O':
66
                        if ((i + 1) < argc) {
67
                                i++;
68
 
69
                                if (0 == strcmp("ascii", argv[i])) {
70
                                        _bfdname = _bfd_ascii;
71
                                }
72
 
73
                                break;
74
                        }
75
 
76
                        fprintf(stderr, "\tERROR! option <%s> requires an argument\n",
77
                                argv[i]);
78
                        err = -EINVAL;
79
 
80
                        break;
81
                case 'o':
82
                        if ((i + 1) < argc) {
83
                                i++;
84
                                _outfile = argv[i];
85
 
86
                                break;
87
                        }
88
 
89
                        fprintf(stderr, "\tERROR! option <%s> requires an argument\n",
90
                                argv[i]);
91
                        err = -EINVAL;
92
 
93
                        break;
94
                case 'w':
95
                        if ((i + 1) < argc) {
96
                                long num;
97
 
98
                                i++;
99
                                num = strtol(argv[i], NULL, 0);
100
                                switch (num) {
101
                                case 8:
102
                                        _mem_width = 1;
103
                                        break;
104
                                case 16:
105
                                        _mem_width = 2;
106
                                        break;
107
                                case 32:
108
                                        _mem_width = 4;
109
                                        break;
110
                                default:
111
                                        fprintf(stderr, "\tERROR! invalid memory width\n");
112
                                        err = -ERANGE;
113
                                }
114
                        } else
115
                                err = -EINVAL;
116
 
117
                        break;
118
                case 'h':
119
                        printf("%s [option] <input>\n", argv[0]);
120
                        printf("\t-O <bfdname> output object format (ascii)\n");
121
                        printf("\t-w [8|16|32] memory width in bit (default: 32)\n");
122
                        printf("\t-o <file>    output filename (default: stdout)\n");
123
                        printf("\t-h           useful help\n");
124
 
125
                        exit(0);
126
                }
127
        }
128
 
129
        if (0 == err && NULL == _infile) {
130
                fprintf(stderr, "\tERROR! no input file given\n");
131
                err = -EINVAL;
132
        }
133
 
134
        return err;
135
}
136
 
137
static int _load_file(const char *filename, unsigned char **buf_ptr)
138
{
139
        unsigned char *buf;
140
        int err;
141
 
142
        buf = NULL;
143
 
144
        err = open(filename, O_RDONLY);
145
        if (0 <= err) {
146
                struct stat s;
147
                int fd;
148
 
149
                fd = err;
150
                err = 0;
151
 
152
                err = fstat(fd, &s);
153
                if (0 == err) {
154
                        buf = malloc(s.st_size);
155
                        if (buf) {
156
                                unsigned int r;
157
 
158
                                r = 0;
159
                                do {
160
                                        err = read(fd, buf + r, s.st_size - r);
161
                                        if (0 > err) {
162
                                                err = -errno;
163
                                                if (-EINTR != err)
164
                                                        break;
165
 
166
                                                err = 0;
167
                                        }
168
 
169
                                        r += err;
170
                                        err = 0;
171
                                } while (r < s.st_size);
172
 
173
                                if (0 == err) {
174
                                        *buf_ptr = buf;
175
                                        err = s.st_size;
176
                                }
177
                        } else
178
                                err = -ENOMEM;
179
                }
180
 
181
                close(fd);
182
        } else
183
                err = -errno;
184
 
185
        if (0 > err)
186
                fprintf(stderr, "load file failed, error %d\n", err);
187
 
188
        return err;
189
}
190
 
191
static const char *_get_filename(const char *filename)
192
{
193
        int i;
194
 
195
        i = strlen(filename);
196
        while (0 != i) {
197
                if ('/' == filename[i]) {
198
                        i++;
199
                        break;
200
                }
201
 
202
                i--;
203
        }
204
 
205
        return &filename[i];
206
}
207
 
208
static int _convert_to_ascii(const char *filename, const unsigned char *buf,
209
                             int size)
210
{
211
        char name[64];
212
        int err;
213
 
214
        err = snprintf(&name[0], 64, "%s.asc", _get_filename(filename));
215
        if (64 > err) {
216
                err = open(&name[0], O_WRONLY | O_CREAT | O_TRUNC, 00644);
217
                if (0 <= err) {
218
                        int offset, fd;
219
 
220
                        fd = err;
221
                        err = 0;
222
 
223
                        offset = 0;
224
                        while (offset < size) {
225
                                unsigned char ob[34];
226
                                int i;
227
 
228
                                memset(&ob[0], 0x00, sizeof(ob));
229
                                for (i = 0; i < _mem_width; i++) {
230
                                        unsigned char c;
231
                                        int j;
232
 
233
                                        if ((i + offset) < size) {
234
                                                c = buf[i + offset];
235
                                        } else
236
                                                c = 0x00;
237
 
238
                                        for (j = 0; j < 8; j++) {
239
                                                if (0 != (c & (0x80 >> j)))
240
                                                        ob[(i << 3) + j] = '1';
241
                                                else
242
                                                        ob[(i << 3) + j] = '0';
243
                                        }
244
                                }
245
 
246
                                ob[(i << 3)] = '\n';
247
                                err = write(fd, &ob[0], (i << 3) + 1);
248
                                if (0 > err) {
249
                                        err = -errno;
250
                                        break;
251
                                }
252
 
253
                                offset += i;
254
 
255
                                err = 0;
256
                        }
257
 
258
                        close(fd);
259
                } else
260
                        err = -errno;
261
        } else
262
                err = -ENAMETOOLONG;
263
 
264
        if (0 != err)
265
                fprintf(stderr, "write output file failed, error %d\n", err);
266
 
267
        return err;
268
}
269
 
270
int main(int argc, char *argv[])
271
{
272
        int err;
273
 
274
        err = _parse_args(argc, argv);
275
        if (0 == err) {
276
                unsigned char *inbuf;
277
 
278
                err = _load_file(_infile, &inbuf);
279
                if (0 < err) {
280
                        int len;
281
 
282
                        len = err;
283
                        switch (_bfdname) {
284
                        case _bfd_ascii:
285
                                err = _convert_to_ascii(_infile, inbuf, len);
286
                                break;
287
                        }
288
 
289
                        free(inbuf);
290
                }
291
        }
292
 
293
        return err;
294
}
295
 

powered by: WebSVN 2.1.0

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