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

Subversion Repositories eus100lx

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 freza
/* $Id: boot.c,v 1.1.1.1 2006-02-04 03:35:00 freza Exp $ */
2
 
3
#include <sys/mman.h>
4
 
5
#include <errno.h>
6
#include <fcntl.h>
7
#include <stdarg.h>
8
#include <stdio.h>
9
#include <stdlib.h>
10
#include <string.h>
11
#include <unistd.h>
12
 
13
 
14
extern int mcsdecode(FILE *, u_int8_t **, size_t *);
15
 
16
static int              verbose = 0;
17
static int              is_ftp = 0;
18
 
19
#define VERBOSE(arg)    if (verbose) warnx arg
20
#define TRACE(arg)      if (verbose > 1) warnx arg
21
 
22
 
23
void
24
warnx(char *fmt, ...)
25
{
26
        va_list                 ap;
27
 
28
        if (is_ftp)
29
                fprintf(stdout, "150-");
30
        fprintf(stdout, "boot: ");
31
        va_start(ap, fmt);
32
        vfprintf(stdout, fmt, ap);
33
        va_end(ap);
34
        fprintf(stdout, "\r\n");
35
}
36
 
37
void
38
errx(int ret, char *fmt, ...)
39
{
40
        va_list                 ap;
41
 
42
        if (is_ftp)
43
                fprintf(stdout, "510-");
44
        fprintf(stderr, "boot: ");
45
        va_start(ap, fmt);
46
        vfprintf(stderr, fmt, ap);
47
        va_end(ap);
48
        fprintf(stderr, "\n");
49
 
50
        exit(ret);
51
}
52
 
53
void
54
err(int ret, char *fmt, ...)
55
{
56
        va_list                 ap;
57
 
58
        if (is_ftp)
59
                fprintf(stdout, "510-");
60
        fprintf(stderr, "boot: ");
61
        va_start(ap, fmt);
62
        vfprintf(stderr, fmt, ap);
63
        va_end(ap);
64
        fprintf(stderr, ": %s\n", strerror(errno));
65
 
66
        exit(ret);
67
}
68
 
69
/*
70
 * Main and stuff.
71
 */
72
#define ARGUMENTS       "d:hp:v"
73
 
74
void
75
usage()
76
{
77
        printf("Usage: boot [options] [file.mcs]\n");
78
        printf("-d s   Use device 's' [/dev/virtex0]\n");
79
        printf("-h     Print this help\n");
80
        printf("-p b   Don't boot, set PROG to 'b' (1 or 0)\n");
81
        printf("-v     Verbose operation\n");
82
}
83
 
84
int
85
main(int argc, char *argv[])
86
{
87
        FILE                    *file;
88
        u_int8_t                *data;
89
        char                    *path, *device, *str;
90
        size_t                  count;
91
        int                     fd, progval;
92
        int                     c, do_setprog, do_raw_boot;
93
 
94
        do_raw_boot = 0;         /* via kernel by default */
95
        do_setprog = 0;  /* boot by default */
96
        is_ftp = 0;
97
        progval = 0;             /* gcc */
98
        device = "/dev/virtex0";
99
        path = NULL;
100
 
101
        if ((str = getenv("FTP_METHOD")) != NULL) {
102
                if (strcasecmp(str, "PUT") == 0) {
103
                        is_ftp = 1;
104
                } else {
105
                        errx(1, "FTP method %s, only PUT supported", str);
106
                }
107
        }
108
 
109
        while((c = getopt(argc, argv, ARGUMENTS)) != -1) {
110
                switch (c) {
111
                case 'd':               /* Select device        */
112
                        device = optarg;
113
                        break;
114
 
115
                case 'h':               /* Show help            */
116
                        usage();
117
                        return 0;
118
 
119
                case 'p':
120
                        do_setprog = 1;
121
                        switch (optarg[0]) {
122
                        case '0':
123
                                progval = 0;
124
                                break;
125
                        case '1':
126
                                progval = 1;
127
                                break;
128
                        default:
129
                                errx(1, "Argument to -p should be 0 or 1");
130
                                /*UNREACHED*/
131
                        }
132
                        break;
133
 
134
                case 'v':               /* Verbose operation    */
135
                        verbose++;
136
                        break;
137
 
138
                default:
139
                        errx(1, "unknown argument -%c", optopt);
140
                }
141
        }
142
 
143
        argc -= optind;
144
        argv += optind;
145
 
146
        if (argc > (do_setprog ? 0 : 1))
147
                errx(1, "stray arguments");
148
 
149
        /* We're going to boot, so read the design. */
150
        if (do_setprog == 0) {
151
                if (argc == 1)
152
                        path = argv[0];
153
 
154
                if (path != NULL) {
155
                        VERBOSE(("open design file %s", path));
156
                        file = fopen(path, "r");
157
                        if (file == NULL)
158
                                err(1, "could not open %s", path);
159
                } else {
160
                        /* NOTE: vftpd is broken in that it will blocking
161
                         *     copy statfd (fd 3) to the user until the end,
162
                         *     then it will transfer the file, and in the
163
                         *     end copy messages again. So we have to close
164
                         *     statfd so that we can proceed reading stdin.
165
                         */
166
                        if (is_ftp) {
167
                                write(3, "150-boot: design file is stdin\r\n",
168
                                    32);
169
                                (void) close(3);
170
                        } else {
171
                                VERBOSE(("design file is stdin"));
172
                        }
173
 
174
                        file = stdin;
175
                }
176
 
177
                if (mcsdecode(file, &data, &count) != 0)
178
                        errx(1, "could not decode %s", path);
179
                VERBOSE(("decoded %d bytes", count));
180
        } else {
181
                /* XXX: Control PROG from kernel */
182
                return (1);
183
        }
184
 
185
        /* Let's do the well behaved boot then. */
186
        VERBOSE(("open device"));
187
        fd = open(device, O_RDWR, 0);
188
        if (fd == -1)
189
                err(1, "could not open %s", device);
190
 
191
        VERBOSE(("write configuration stream"));
192
        if (write(fd, (const void *) data, count) != count)
193
                err(1, "failed to boot%s", \
194
                    errno == EIO ? ": CRC error" : "");
195
 
196
        VERBOSE(("acknowledge startup"));
197
        if (close(fd) == -1)
198
                err(1, "failed to boot%s", \
199
                    errno == EIO ? ": DONE timeout" : "");
200
 
201
        VERBOSE(("success"));
202
        return (0);
203
}

powered by: WebSVN 2.1.0

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