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

Subversion Repositories s6soc

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

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 52 dgisselq
        size_t  nr;
83 11 dgisselq
 
84 45 dgisselq
        rewind(fp);
85 52 dgisselq
        nr = fread(buf, sizeof(char), SEARCHLN, fp);
86
        if (nr != SEARCHLN) {
87
                fprintf(stderr, "Cannot read the Xilinx bitfile header\n");
88
                perror("O/S Err:");
89
                exit(EXIT_FAILURE);
90
        }
91 45 dgisselq
        for(int start=0; start+MATCHLN<SEARCHLN; start++) {
92
                int     mloc;
93 11 dgisselq
 
94 45 dgisselq
                // Search backwards, since the starting bytes just aren't that
95
                // interesting.
96
                for(mloc = MATCHLN-1; mloc >= 0; mloc--)
97
                        if (buf[start+mloc] != matchstr[mloc])
98
                                break;
99
                if (mloc < 0) {
100 52 dgisselq
                        if (fseek(fp, (long)start, SEEK_SET) != 0) {
101
                                fprintf(stderr, "Cannot seek to the end of the Xilinx header\n");
102
                                perror("O/S Err:");
103
                                exit(EXIT_FAILURE);
104
                        } return;
105 11 dgisselq
                }
106
        }
107
 
108 45 dgisselq
        fprintf(stderr, "Could not find bin-file header within bit file\n");
109
        fclose(fp);
110
        exit(EXIT_FAILURE);
111 11 dgisselq
}
112
 
113
int main(int argc, char **argv) {
114 45 dgisselq
        int             skp=0, argn;
115
        bool            debug_only = false, verbose = false;
116 52 dgisselq
        bool            ignore_missing_memory = true;
117 45 dgisselq
        unsigned        entry = 0;
118 11 dgisselq
        FLASHDRVR       *flash = NULL;
119 45 dgisselq
        const char      *bitfile = NULL, *altbitfile = NULL, *execfile = NULL;
120
        size_t          bitsz;
121
        FILE            *fp;
122 11 dgisselq
 
123
        if (argc < 2) {
124
                usage();
125
                exit(EXIT_SUCCESS);
126
        }
127
 
128
        skp=1;
129 45 dgisselq
        for(argn=0; argn<argc-skp; argn++) {
130 11 dgisselq
                if (argv[argn+skp][0] == '-') {
131
                        switch(argv[argn+skp][1]) {
132 26 dgisselq
                        case 'd':
133
                                debug_only = true;
134
                                break;
135 11 dgisselq
                        case 'h':
136
                                usage();
137
                                exit(EXIT_SUCCESS);
138 26 dgisselq
                                break;
139 45 dgisselq
                        case 'v':
140
                                verbose = true;
141 11 dgisselq
                                break;
142 26 dgisselq
                        default:
143
                                fprintf(stderr, "Unknown option, -%c\n\n",
144
                                        argv[argn+skp][0]);
145
                                usage();
146
                                exit(EXIT_FAILURE);
147
                                break;
148 11 dgisselq
                        } skp++; argn--;
149 45 dgisselq
                } else {
150
                        // Anything here must be either the program to load,
151
                        // or a bit file to load
152
                        argv[argn] = argv[argn+skp];
153 11 dgisselq
                }
154
        } argc -= skp;
155
 
156
 
157 45 dgisselq
        for(argn=0; argn<argc; argn++) {
158 52 dgisselq
                if (access(argv[argn], R_OK)!=0) {
159
                        printf("ERR: Cannot open %s\n", argv[argn]);
160
                        usage();
161
                        exit(EXIT_FAILURE);
162
                } else if (iself(argv[argn])) {
163 45 dgisselq
                        if (execfile) {
164
                                printf("Too many executable files given, %s and %s\n", execfile, argv[argn]);
165
                                usage();
166
                                exit(EXIT_FAILURE);
167
                        } execfile = argv[argn];
168
                } else { // if (isbitfile(argv[argn]))
169
                        if (!bitfile)
170
                                bitfile = argv[argn];
171
                        else if (!altbitfile)
172
                                altbitfile = argv[argn];
173
                        else {
174
                                printf("Unknown file name or too many files, %s\n", argv[argn]);
175
                                usage();
176
                                exit(EXIT_FAILURE);
177
                        }
178
                }
179
        }
180
 
181
if (verbose) {
182
if (bitfile)    printf(" BITFILE: %s\n", bitfile);
183
if (altbitfile) printf("ABITFILE: %s\n", altbitfile);
184
if (execfile)   printf("EXECTFILE: %s\n", execfile);
185
}
186
 
187
        if ((execfile == NULL)&&(bitfile == NULL)) {
188
                printf("No executable or bit file(s) given!\n\n");
189
                usage();
190
                exit(EXIT_FAILURE);
191
        }
192
 
193
        if ((bitfile == NULL)&&(altbitfile != NULL)) {
194
                printf("Cannot program an alternate bitfile without a main bitfile\n\n");
195
                usage();
196
                exit(EXIT_FAILURE);
197
        }
198
 
199 11 dgisselq
        if ((bitfile)&&(access(bitfile,R_OK)!=0)) {
200 45 dgisselq
                // If there's no code file, or the code file cannot be opened
201 11 dgisselq
                fprintf(stderr, "Cannot open bitfile, %s\n", bitfile);
202 52 dgisselq
                if (iself(bitfile))
203
                        fprintf(stderr, "Is %s an ELF executable??\n", bitfile);
204 11 dgisselq
                exit(EXIT_FAILURE);
205 45 dgisselq
        }
206
 
207
        if ((altbitfile)&&(access(altbitfile,R_OK)!=0)) {
208
                // If there's no code file, or the code file cannot be opened
209
                fprintf(stderr, "Cannot open alternate bitfile, %s\n", altbitfile);
210 11 dgisselq
                exit(EXIT_FAILURE);
211 45 dgisselq
        } if ((execfile)&&(access(execfile,R_OK)!=0)) {
212 11 dgisselq
                // If there's no code file, or the code file cannot be opened
213 45 dgisselq
                fprintf(stderr, "Cannot open executable, %s\n\n", execfile);
214
                usage();
215 11 dgisselq
                exit(EXIT_FAILURE);
216 52 dgisselq
        } else if ((execfile)&&(!iself(execfile))) {
217 45 dgisselq
                printf("%s is not an executable file\n\n", execfile);
218
                usage();
219
                exit(EXIT_FAILURE);
220 11 dgisselq
        }
221
 
222 45 dgisselq
        char    *fbuf = new char[FLASHLEN];
223 11 dgisselq
 
224
        // Set the flash buffer to all ones
225 45 dgisselq
        memset(fbuf, -1, FLASHLEN);
226 11 dgisselq
 
227 26 dgisselq
        if (debug_only) {
228
                m_fpga = NULL;
229
        } else {
230 11 dgisselq
                char    szSel[64];
231 45 dgisselq
                strcpy(szSel, S6SN);
232 11 dgisselq
                m_fpga = new FPGA(new DEPPI(szSel));
233
        }
234
 
235 45 dgisselq
        // Make certain we can talk to the FPGA
236
        try {
237
                unsigned v  = m_fpga->readio(R_VERSION);
238
                if (v < 0x20170000) {
239
                        fprintf(stderr, "Could not communicate with board (invalid version)\n");
240
                        exit(EXIT_FAILURE);
241
                }
242
        } catch(BUSERR b) {
243
                fprintf(stderr, "Could not communicate with board (BUSERR when reading VERSION)\n");
244
                exit(EXIT_FAILURE);
245
        }
246
 
247 26 dgisselq
        flash = (debug_only)?NULL : new FLASHDRVR(m_fpga);
248 11 dgisselq
 
249
        // First, see if we need to load a bit file
250
        if (bitfile) {
251
 
252 45 dgisselq
                fp = fopen(bitfile, "r");
253 52 dgisselq
                if (strcmp(&bitfile[strlen(bitfile)-4],".bit")==0)
254 45 dgisselq
                        skip_bitfile_header(fp);
255
                bitsz = fread(&fbuf[CONFIG_ADDRESS-SPIFLASH],
256 11 dgisselq
                                sizeof(fbuf[0]),
257 45 dgisselq
                                FLASHLEN - (CONFIG_ADDRESS-SPIFLASH), fp);
258 11 dgisselq
                fclose(fp);
259
 
260 45 dgisselq
                try {
261
                        printf("Loading: %s\n", bitfile);
262
                        flash->write(CONFIG_ADDRESS, bitsz, fbuf, true);
263
                } catch(BUSERR b) {
264
                        fprintf(stderr, "BUS-ERR @0x%08x\n", b.addr);
265
                        exit(-1);
266 11 dgisselq
                }
267 45 dgisselq
        }
268 11 dgisselq
 
269 45 dgisselq
        // Then see if we were given an alternate bit file
270
        if (altbitfile) {
271
                size_t  altsz;
272
                assert(CONFIG_ADDRESS + bitsz < ALTCONFIG_ADDRESS);
273
 
274
                fp = fopen(altbitfile, "r");
275
                if (strcmp(&argv[argn][strlen(argv[argn])-4],".bit")==0)
276
                        skip_bitfile_header(fp);
277
                altsz = fread(&fbuf[ALTCONFIG_ADDRESS-SPIFLASH],
278 11 dgisselq
                                sizeof(fbuf[0]),
279 45 dgisselq
                                FLASHLEN-(ALTCONFIG_ADDRESS-SPIFLASH), fp);
280
                assert(ALTCONFIG_ADDRESS+altsz < RESET_ADDRESS);
281 11 dgisselq
                fclose(fp);
282
 
283 45 dgisselq
                try {
284
                        printf("Loading: %s\n", altbitfile);
285
                        flash->write(ALTCONFIG_ADDRESS, altsz, fbuf, true);
286
                } catch(BUSERR b) {
287
                        fprintf(stderr, "BUS-ERR @0x%08x\n", b.addr);
288
                        exit(-1);
289 11 dgisselq
                }
290 45 dgisselq
        } else {
291
                assert(CONFIG_ADDRESS+bitsz < RESET_ADDRESS);
292 11 dgisselq
        }
293
 
294 45 dgisselq
        if (execfile) try {
295
                ELFSECTION      **secpp = NULL, *secp;
296 11 dgisselq
 
297 45 dgisselq
                if(iself(execfile)) {
298 11 dgisselq
                        // zip-readelf will help with both of these ...
299 45 dgisselq
                        elfread(execfile, entry, secpp);
300 11 dgisselq
                        assert(entry == RESET_ADDRESS);
301
                } else {
302 45 dgisselq
                        fprintf(stderr, "ERR: %s is not in ELF format\n", execfile);
303 11 dgisselq
                        exit(EXIT_FAILURE);
304
                }
305
 
306 45 dgisselq
                printf("Loading: %s\n", execfile);
307 11 dgisselq
                // assert(secpp[1]->m_len = 0);
308
                for(int i=0; secpp[i]->m_len; i++) {
309
                        bool    valid = false;
310
                        secp=  secpp[i];
311
                        if ((secp->m_start >= RESET_ADDRESS)
312
                                &&(secp->m_start+secp->m_len
313 45 dgisselq
                                                <= SPIFLASH+FLASHLEN))
314 11 dgisselq
                                valid = true;
315
                        if (!valid) {
316 52 dgisselq
                                if (ignore_missing_memory)
317
                                        fprintf(stderr, "WARNING: No such memory on board: 0x%08x - %08x\n",
318
                                                secp->m_start, secp->m_start+secp->m_len);
319
                                else {
320
                                        fprintf(stderr, "ERROR: No such memory on board: 0x%08x - %08x\n",
321
                                                secp->m_start, secp->m_start+secp->m_len);
322 45 dgisselq
                                        exit(EXIT_FAILURE);
323 52 dgisselq
                                }
324 11 dgisselq
                        }
325
                }
326
 
327 19 dgisselq
                unsigned        startaddr = RESET_ADDRESS, codelen = 0;
328 11 dgisselq
                for(int i=0; secpp[i]->m_len; i++) {
329
                        secp = secpp[i];
330 45 dgisselq
 
331
                        unsigned start, idx, ln;
332
 
333
                        start = secp->m_start;
334
                        idx = 0;
335
                        ln = secp->m_len;
336
                        if (secp->m_start < SPIFLASH) {
337
                                start = SPIFLASH;
338
                                idx = SPIFLASH-secp->m_start;
339
                                if (idx > secp->m_len)
340
                                        continue;
341
                                ln = secp->m_len-idx;
342
                        } if (start + ln > SPIFLASH+FLASHLEN) {
343
                                if (start > SPIFLASH+FLASHLEN)
344
                                        continue;
345
                                ln = SPIFLASH+FLASHLEN-start;
346 11 dgisselq
                        }
347 45 dgisselq
 
348
                        // We only ever write to the flash
349
                        if (start < startaddr) {
350
                                // Keep track of the first address in
351
                                // flash, as well as the last address
352
                                // that we will write
353
                                codelen += (startaddr-secp->m_start);
354
                                startaddr = secp->m_start;
355
                        } if (start+ln > startaddr+codelen) {
356
                                codelen = secp->m_start+secp->m_len-startaddr;
357
                        } memcpy(&fbuf[start-SPIFLASH], &secp->m_data[idx], ln);
358 11 dgisselq
                }
359 26 dgisselq
                if ((flash)&&(!flash->write(startaddr, codelen, &fbuf[startaddr-SPIFLASH], true))) {
360 19 dgisselq
                        fprintf(stderr, "ERR: Could not write program to flash\n");
361
                        exit(EXIT_FAILURE);
362 26 dgisselq
                } else if (!flash)
363
                        printf("flash->write(%08x, %d, ... );\n", startaddr,
364
                                codelen);
365
                if (m_fpga) m_fpga->readio(R_VERSION); // Check for bus errors
366 11 dgisselq
 
367
                // Now ... how shall we start this CPU?
368
        } catch(BUSERR a) {
369 45 dgisselq
                fprintf(stderr, "S6-BUS error: %08x\n", a.addr);
370 11 dgisselq
                exit(-2);
371
        }
372
 
373 52 dgisselq
        if (flash) delete       flash;
374 26 dgisselq
        if (m_fpga) delete      m_fpga;
375 11 dgisselq
 
376
        return EXIT_SUCCESS;
377
}
378
 

powered by: WebSVN 2.1.0

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