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

Subversion Repositories s6soc

[/] [s6soc/] [trunk/] [sw/] [host/] [zipload.cpp] - Blame information for rev 45

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

Line No. Rev Author Line
1 11 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    zipload.cpp
4
//
5
// Project:     CMod S6 System on a Chip, ZipCPU demonstration project
6
//
7
// Purpose:     To load the flash--both a the two configurations and the 
8
//              a program for the ZipCPU into (flash) memory.
9
//
10
//      Steps:
11
//              1. Reboot the CMod into the alternate/debug/command mode
12
//              2. Load flash memory
13
//              3. Reload (reboot) the CMod configuration into ZipCPU mode
14
//              4. Program should start on its own.
15
//
16
// Creator:     Dan Gisselquist, Ph.D.
17
//              Gisselquist Technology, LLC
18
//
19
////////////////////////////////////////////////////////////////////////////////
20
//
21 45 dgisselq
// Copyright (C) 2015-2017, Gisselquist Technology, LLC
22 11 dgisselq
//
23
// This program is free software (firmware): you can redistribute it and/or
24
// modify it under the terms of  the GNU General Public License as published
25
// by the Free Software Foundation, either version 3 of the License, or (at
26
// your option) any later version.
27
//
28
// This program is distributed in the hope that it will be useful, but WITHOUT
29
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
30
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
31
// for more details.
32
//
33 45 dgisselq
// You should have received a copy of the GNU General Public License along
34
// with this program.  (It's in the $(ROOT)/doc directory.  Run make with no
35
// target there if the PDF file isn't present.)  If not, see
36
// <http://www.gnu.org/licenses/> for a copy.
37
//
38 11 dgisselq
// License:     GPL, v3, as defined and found on www.gnu.org,
39
//              http://www.gnu.org/licenses/gpl.html
40
//
41
//
42
////////////////////////////////////////////////////////////////////////////////
43
//
44
//
45
#include <stdio.h>
46
#include <stdlib.h>
47
#include <sys/types.h>
48
#include <sys/stat.h>
49
#include <fcntl.h>
50
#include <unistd.h>
51
#include <strings.h>
52
#include <ctype.h>
53
#include <string.h>
54
#include <signal.h>
55
#include <assert.h>
56
 
57
#include "devbus.h"
58
#include "llcomms.h"
59
#include "deppi.h"
60
#include "regdefs.h"
61
#include "flashdrvr.h"
62 45 dgisselq
#include "zipelf.h"
63 11 dgisselq
 
64
FPGA    *m_fpga;
65
 
66 45 dgisselq
void    usage(void) {
67
        printf("USAGE: zipload [-h] [<bit-file> [<alt-bit-file>]] <zip-program-file>\n");
68
        printf("\n"
69
"\t-h\tDisplay this usage statement\n"
70
);
71 11 dgisselq
}
72
 
73 45 dgisselq
void    skip_bitfile_header(FILE *fp) {
74
        const unsigned  SEARCHLN = 204, MATCHLN = 16;
75
        const unsigned char matchstr[MATCHLN] = {
76
                0xff, 0xff, 0xff, 0xff,
77
                0xff, 0xff, 0xff, 0xff,
78
                0xff, 0xff, 0xff, 0xff,
79
                //
80
                0xaa, 0x99, 0x55, 0x66 };
81
        unsigned char   buf[SEARCHLN];
82 11 dgisselq
 
83 45 dgisselq
        rewind(fp);
84
        fread(buf, sizeof(char), SEARCHLN, fp);
85
        for(int start=0; start+MATCHLN<SEARCHLN; start++) {
86
                int     mloc;
87 11 dgisselq
 
88 45 dgisselq
                // Search backwards, since the starting bytes just aren't that
89
                // interesting.
90
                for(mloc = MATCHLN-1; mloc >= 0; mloc--)
91
                        if (buf[start+mloc] != matchstr[mloc])
92
                                break;
93
                if (mloc < 0) {
94
                        fseek(fp, start, SEEK_SET);
95
                        return;
96 11 dgisselq
                }
97
        }
98
 
99 45 dgisselq
        fprintf(stderr, "Could not find bin-file header within bit file\n");
100
        fclose(fp);
101
        exit(EXIT_FAILURE);
102 11 dgisselq
}
103
 
104
int main(int argc, char **argv) {
105 45 dgisselq
        int             skp=0, argn;
106
        bool            debug_only = false, verbose = false;
107
        bool            ignore_missing_memory = false;
108
        unsigned        entry = 0;
109 11 dgisselq
        FLASHDRVR       *flash = NULL;
110 45 dgisselq
        const char      *bitfile = NULL, *altbitfile = NULL, *execfile = NULL;
111
        size_t          bitsz;
112
        FILE            *fp;
113 11 dgisselq
 
114
        if (argc < 2) {
115
                usage();
116
                exit(EXIT_SUCCESS);
117
        }
118
 
119
        skp=1;
120 45 dgisselq
        for(argn=0; argn<argc-skp; argn++) {
121 11 dgisselq
                if (argv[argn+skp][0] == '-') {
122
                        switch(argv[argn+skp][1]) {
123 26 dgisselq
                        case 'd':
124
                                debug_only = true;
125
                                break;
126 11 dgisselq
                        case 'h':
127
                                usage();
128
                                exit(EXIT_SUCCESS);
129 26 dgisselq
                                break;
130 45 dgisselq
                        case 'v':
131
                                verbose = true;
132 11 dgisselq
                                break;
133 26 dgisselq
                        default:
134
                                fprintf(stderr, "Unknown option, -%c\n\n",
135
                                        argv[argn+skp][0]);
136
                                usage();
137
                                exit(EXIT_FAILURE);
138
                                break;
139 11 dgisselq
                        } skp++; argn--;
140 45 dgisselq
                } else {
141
                        // Anything here must be either the program to load,
142
                        // or a bit file to load
143
                        argv[argn] = argv[argn+skp];
144 11 dgisselq
                }
145
        } argc -= skp;
146
 
147
 
148 45 dgisselq
        for(argn=0; argn<argc; argn++) {
149
                if (iself(argv[argn])) {
150
                        if (execfile) {
151
                                printf("Too many executable files given, %s and %s\n", execfile, argv[argn]);
152
                                usage();
153
                                exit(EXIT_FAILURE);
154
                        } execfile = argv[argn];
155
                } else { // if (isbitfile(argv[argn]))
156
                        if (!bitfile)
157
                                bitfile = argv[argn];
158
                        else if (!altbitfile)
159
                                altbitfile = argv[argn];
160
                        else {
161
                                printf("Unknown file name or too many files, %s\n", argv[argn]);
162
                                usage();
163
                                exit(EXIT_FAILURE);
164
                        }
165
                }
166
        }
167
 
168
if (verbose) {
169
if (bitfile)    printf(" BITFILE: %s\n", bitfile);
170
if (altbitfile) printf("ABITFILE: %s\n", altbitfile);
171
if (execfile)   printf("EXECTFILE: %s\n", execfile);
172
}
173
 
174
        if ((execfile == NULL)&&(bitfile == NULL)) {
175
                printf("No executable or bit file(s) given!\n\n");
176
                usage();
177
                exit(EXIT_FAILURE);
178
        }
179
 
180
        if ((bitfile == NULL)&&(altbitfile != NULL)) {
181
                printf("Cannot program an alternate bitfile without a main bitfile\n\n");
182
                usage();
183
                exit(EXIT_FAILURE);
184
        }
185
 
186 11 dgisselq
        if ((bitfile)&&(access(bitfile,R_OK)!=0)) {
187 45 dgisselq
                // If there's no code file, or the code file cannot be opened
188 11 dgisselq
                fprintf(stderr, "Cannot open bitfile, %s\n", bitfile);
189
                exit(EXIT_FAILURE);
190 45 dgisselq
        }
191
 
192
        if ((altbitfile)&&(access(altbitfile,R_OK)!=0)) {
193
                // If there's no code file, or the code file cannot be opened
194
                fprintf(stderr, "Cannot open alternate bitfile, %s\n", altbitfile);
195 11 dgisselq
                exit(EXIT_FAILURE);
196 45 dgisselq
        } if ((execfile)&&(access(execfile,R_OK)!=0)) {
197 11 dgisselq
                // If there's no code file, or the code file cannot be opened
198 45 dgisselq
                fprintf(stderr, "Cannot open executable, %s\n\n", execfile);
199
                usage();
200 11 dgisselq
                exit(EXIT_FAILURE);
201 45 dgisselq
        } else if (!iself(execfile)) {
202
                printf("%s is not an executable file\n\n", execfile);
203
                usage();
204
                exit(EXIT_FAILURE);
205 11 dgisselq
        }
206
 
207 45 dgisselq
        char    *fbuf = new char[FLASHLEN];
208 11 dgisselq
 
209
        // Set the flash buffer to all ones
210 45 dgisselq
        memset(fbuf, -1, FLASHLEN);
211 11 dgisselq
 
212 26 dgisselq
        if (debug_only) {
213
                m_fpga = NULL;
214
        } else {
215 11 dgisselq
                char    szSel[64];
216 45 dgisselq
                strcpy(szSel, S6SN);
217 11 dgisselq
                m_fpga = new FPGA(new DEPPI(szSel));
218
        }
219
 
220 45 dgisselq
        // Make certain we can talk to the FPGA
221
        try {
222
                unsigned v  = m_fpga->readio(R_VERSION);
223
                if (v < 0x20170000) {
224
                        fprintf(stderr, "Could not communicate with board (invalid version)\n");
225
                        exit(EXIT_FAILURE);
226
                }
227
        } catch(BUSERR b) {
228
                fprintf(stderr, "Could not communicate with board (BUSERR when reading VERSION)\n");
229
                exit(EXIT_FAILURE);
230
        }
231
 
232 26 dgisselq
        flash = (debug_only)?NULL : new FLASHDRVR(m_fpga);
233 11 dgisselq
 
234
        // First, see if we need to load a bit file
235
        if (bitfile) {
236
 
237 45 dgisselq
                fp = fopen(bitfile, "r");
238
                if (strcmp(&argv[argn][strlen(argv[argn])-4],".bit")==0)
239
                        skip_bitfile_header(fp);
240
                bitsz = fread(&fbuf[CONFIG_ADDRESS-SPIFLASH],
241 11 dgisselq
                                sizeof(fbuf[0]),
242 45 dgisselq
                                FLASHLEN - (CONFIG_ADDRESS-SPIFLASH), fp);
243 11 dgisselq
                fclose(fp);
244
 
245 45 dgisselq
                try {
246
                        printf("Loading: %s\n", bitfile);
247
                        flash->write(CONFIG_ADDRESS, bitsz, fbuf, true);
248
                } catch(BUSERR b) {
249
                        fprintf(stderr, "BUS-ERR @0x%08x\n", b.addr);
250
                        exit(-1);
251 11 dgisselq
                }
252 45 dgisselq
        }
253 11 dgisselq
 
254 45 dgisselq
        // Then see if we were given an alternate bit file
255
        if (altbitfile) {
256
                size_t  altsz;
257
                assert(CONFIG_ADDRESS + bitsz < ALTCONFIG_ADDRESS);
258
 
259
                fp = fopen(altbitfile, "r");
260
                if (strcmp(&argv[argn][strlen(argv[argn])-4],".bit")==0)
261
                        skip_bitfile_header(fp);
262
                altsz = fread(&fbuf[ALTCONFIG_ADDRESS-SPIFLASH],
263 11 dgisselq
                                sizeof(fbuf[0]),
264 45 dgisselq
                                FLASHLEN-(ALTCONFIG_ADDRESS-SPIFLASH), fp);
265
                assert(ALTCONFIG_ADDRESS+altsz < RESET_ADDRESS);
266 11 dgisselq
                fclose(fp);
267
 
268 45 dgisselq
                try {
269
                        printf("Loading: %s\n", altbitfile);
270
                        flash->write(ALTCONFIG_ADDRESS, altsz, fbuf, true);
271
                } catch(BUSERR b) {
272
                        fprintf(stderr, "BUS-ERR @0x%08x\n", b.addr);
273
                        exit(-1);
274 11 dgisselq
                }
275 45 dgisselq
        } else {
276
                assert(CONFIG_ADDRESS+bitsz < RESET_ADDRESS);
277 11 dgisselq
        }
278
 
279 45 dgisselq
        if (execfile) try {
280
                ELFSECTION      **secpp = NULL, *secp;
281 11 dgisselq
 
282 45 dgisselq
                if(iself(execfile)) {
283 11 dgisselq
                        // zip-readelf will help with both of these ...
284 45 dgisselq
                        elfread(execfile, entry, secpp);
285 11 dgisselq
                        assert(entry == RESET_ADDRESS);
286
                } else {
287 45 dgisselq
                        fprintf(stderr, "ERR: %s is not in ELF format\n", execfile);
288 11 dgisselq
                        exit(EXIT_FAILURE);
289
                }
290
 
291 45 dgisselq
                printf("Loading: %s\n", execfile);
292 11 dgisselq
                // assert(secpp[1]->m_len = 0);
293
                for(int i=0; secpp[i]->m_len; i++) {
294
                        bool    valid = false;
295
                        secp=  secpp[i];
296
                        if ((secp->m_start >= RESET_ADDRESS)
297
                                &&(secp->m_start+secp->m_len
298 45 dgisselq
                                                <= SPIFLASH+FLASHLEN))
299 11 dgisselq
                                valid = true;
300
                        if (!valid) {
301
                                fprintf(stderr, "No such memory on board: 0x%08x - %08x\n",
302
                                        secp->m_start, secp->m_start+secp->m_len);
303 45 dgisselq
                                if (!ignore_missing_memory)
304
                                        exit(EXIT_FAILURE);
305 11 dgisselq
                        }
306
                }
307
 
308 19 dgisselq
                unsigned        startaddr = RESET_ADDRESS, codelen = 0;
309 11 dgisselq
                for(int i=0; secpp[i]->m_len; i++) {
310
                        secp = secpp[i];
311 45 dgisselq
 
312
                        unsigned start, idx, ln;
313
 
314
                        start = secp->m_start;
315
                        idx = 0;
316
                        ln = secp->m_len;
317
                        if (secp->m_start < SPIFLASH) {
318
                                start = SPIFLASH;
319
                                idx = SPIFLASH-secp->m_start;
320
                                if (idx > secp->m_len)
321
                                        continue;
322
                                ln = secp->m_len-idx;
323
                        } if (start + ln > SPIFLASH+FLASHLEN) {
324
                                if (start > SPIFLASH+FLASHLEN)
325
                                        continue;
326
                                ln = SPIFLASH+FLASHLEN-start;
327 11 dgisselq
                        }
328 45 dgisselq
 
329
                        // We only ever write to the flash
330
                        if (start < startaddr) {
331
                                // Keep track of the first address in
332
                                // flash, as well as the last address
333
                                // that we will write
334
                                codelen += (startaddr-secp->m_start);
335
                                startaddr = secp->m_start;
336
                        } if (start+ln > startaddr+codelen) {
337
                                codelen = secp->m_start+secp->m_len-startaddr;
338
                        } memcpy(&fbuf[start-SPIFLASH], &secp->m_data[idx], ln);
339 11 dgisselq
                }
340 26 dgisselq
                if ((flash)&&(!flash->write(startaddr, codelen, &fbuf[startaddr-SPIFLASH], true))) {
341 19 dgisselq
                        fprintf(stderr, "ERR: Could not write program to flash\n");
342
                        exit(EXIT_FAILURE);
343 26 dgisselq
                } else if (!flash)
344
                        printf("flash->write(%08x, %d, ... );\n", startaddr,
345
                                codelen);
346
                if (m_fpga) m_fpga->readio(R_VERSION); // Check for bus errors
347 11 dgisselq
 
348
                // Now ... how shall we start this CPU?
349
        } catch(BUSERR a) {
350 45 dgisselq
                fprintf(stderr, "S6-BUS error: %08x\n", a.addr);
351 11 dgisselq
                exit(-2);
352
        }
353
 
354 26 dgisselq
        if (m_fpga) delete      m_fpga;
355 11 dgisselq
 
356
        return EXIT_SUCCESS;
357
}
358
 

powered by: WebSVN 2.1.0

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