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

Subversion Repositories s6soc

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

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
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
22
//
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
// License:     GPL, v3, as defined and found on www.gnu.org,
34
//              http://www.gnu.org/licenses/gpl.html
35
//
36
//
37
////////////////////////////////////////////////////////////////////////////////
38
//
39
//
40
//
41
#include <stdio.h>
42
#include <stdlib.h>
43
#include <sys/types.h>
44
#include <sys/stat.h>
45
#include <fcntl.h>
46
#include <unistd.h>
47
#include <strings.h>
48
#include <ctype.h>
49
#include <string.h>
50
#include <signal.h>
51
#include <assert.h>
52
 
53
#include "devbus.h"
54
#include "llcomms.h"
55
#include "deppi.h"
56
#include "regdefs.h"
57
#include "flashdrvr.h"
58
 
59
bool    iself(const char *fname) {
60
        FILE    *fp;
61
        bool    ret = true;
62
 
63
        if ((!fname)||(!fname[0]))
64
                return false;
65
 
66
        fp = fopen(fname, "rb");
67
 
68
        if (!fp)        return false;
69
        if (0x7f != fgetc(fp))  ret = false;
70
        if ('E'  != fgetc(fp))  ret = false;
71
        if ('L'  != fgetc(fp))  ret = false;
72
        if ('F'  != fgetc(fp))  ret = false;
73
        fclose(fp);
74
        return  ret;
75
}
76
 
77
long    fgetwords(FILE *fp) {
78
        // Return the number of words in the current file, and return the 
79
        // file as though it had never been adjusted
80
        long    fpos, flen;
81
        fpos = ftell(fp);
82
        if (0 != fseek(fp, 0l, SEEK_END)) {
83
                fprintf(stderr, "ERR: Could not determine file size\n");
84
                perror("O/S Err:");
85
                exit(-2);
86
        } flen = ftell(fp);
87
        if (0 != fseek(fp, fpos, SEEK_SET)) {
88
                fprintf(stderr, "ERR: Could not seek on file\n");
89
                perror("O/S Err:");
90
                exit(-2);
91
        } flen /= sizeof(FPGA::BUSW);
92
        return flen;
93
}
94
 
95
FPGA    *m_fpga;
96
class   SECTION {
97
public:
98
        unsigned        m_start, m_len;
99
        FPGA::BUSW      m_data[1];
100
};
101
 
102
SECTION **singlesection(int nwords) {
103
        fprintf(stderr, "NWORDS = %d\n", nwords);
104
        size_t  sz = (2*(sizeof(SECTION)+sizeof(SECTION *))
105
                +(nwords-1)*(sizeof(FPGA::BUSW)));
106
        char    *d = (char *)malloc(sz);
107
        SECTION **r = (SECTION **)d;
108
        memset(r, 0, sz);
109
        r[0] = (SECTION *)(&d[2*sizeof(SECTION *)]);
110
        r[0]->m_len   = nwords;
111
        r[1] = (SECTION *)(&r[0]->m_data[r[0]->m_len]);
112
        r[0]->m_start = 0;
113
        r[1]->m_start = 0;
114
        r[1]->m_len   = 0;
115
 
116
        return r;
117
}
118
 
119
SECTION **rawsection(const char *fname) {
120
        SECTION         **secpp, *secp;
121
        unsigned        num_words;
122
        FILE            *fp;
123
        int             nr;
124
 
125
        fp = fopen(fname, "r");
126
        if (fp == NULL) {
127
                fprintf(stderr, "Could not open: %s\n", fname);
128
                exit(-1);
129
        }
130
 
131
        if ((num_words=fgetwords(fp)) > FLASHWORDS-(RESET_ADDRESS-SPIFLASH)) {
132
                fprintf(stderr, "File overruns flash memory\n");
133
                exit(-1);
134
        }
135
        secpp = singlesection(num_words);
136
        secp = secpp[0];
137
        secp->m_start = RAMBASE;
138
        secp->m_len = num_words;
139
        nr= fread(secp->m_data, sizeof(FPGA::BUSW), num_words, fp);
140
        if (nr != (int)num_words) {
141
                fprintf(stderr, "Could not read entire file\n");
142
                perror("O/S Err:");
143
                exit(-2);
144
        } assert(secpp[1]->m_len == 0);
145
 
146
        return secpp;
147
}
148
 
149
unsigned        byteswap(unsigned n) {
150
        unsigned        r;
151
 
152
        r = (n&0x0ff); n>>= 8;
153
        r = (r<<8) | (n&0x0ff); n>>= 8;
154
        r = (r<<8) | (n&0x0ff); n>>= 8;
155
        r = (r<<8) | (n&0x0ff); n>>= 8;
156
 
157
        return r;
158
}
159
 
160
// #define      CHEAP_AND_EASY
161
#ifdef  CHEAP_AND_EASY
162
#else
163
#include <libelf.h>
164
#include <gelf.h>
165
 
166
void    elfread(const char *fname, unsigned &entry, SECTION **&sections) {
167
        Elf     *e;
168
        int     fd, i;
169
        size_t  n;
170
        char    *id;
171
        Elf_Kind        ek;
172
        GElf_Ehdr       ehdr;
173
        GElf_Phdr       phdr;
174
        const   bool    dbg = false;
175
 
176
        if (elf_version(EV_CURRENT) == EV_NONE) {
177
                fprintf(stderr, "ELF library initialization err, %s\n", elf_errmsg(-1));
178
                perror("O/S Err:");
179
                exit(EXIT_FAILURE);
180
        } if ((fd = open(fname, O_RDONLY, 0)) < 0) {
181
                fprintf(stderr, "Could not open %s\n", fname);
182
                perror("O/S Err:");
183
                exit(EXIT_FAILURE);
184
        } if ((e = elf_begin(fd, ELF_C_READ, NULL))==NULL) {
185
                fprintf(stderr, "Could not run elf_begin, %s\n", elf_errmsg(-1));
186
                exit(EXIT_FAILURE);
187
        }
188
 
189
        ek = elf_kind(e);
190
        if (ek == ELF_K_ELF) {
191
                ; // This is the kind of file we should expect
192
        } else if (ek == ELF_K_AR) {
193
                fprintf(stderr, "Cannot run an archive!\n");
194
                exit(EXIT_FAILURE);
195
        } else if (ek == ELF_K_NONE) {
196
                ;
197
        } else {
198
                fprintf(stderr, "Unexpected ELF file kind!\n");
199
                exit(EXIT_FAILURE);
200
        }
201
 
202
        if (gelf_getehdr(e, &ehdr) == NULL) {
203
                fprintf(stderr, "getehdr() failed: %s\n", elf_errmsg(-1));
204
                exit(EXIT_FAILURE);
205
        } if ((i=gelf_getclass(e)) == ELFCLASSNONE) {
206
                fprintf(stderr, "getclass() failed: %s\n", elf_errmsg(-1));
207
                exit(EXIT_FAILURE);
208
        } if ((id = elf_getident(e, NULL)) == NULL) {
209
                fprintf(stderr, "getident() failed: %s\n", elf_errmsg(-1));
210
                exit(EXIT_FAILURE);
211
        } if (i != ELFCLASS32) {
212
                fprintf(stderr, "This is a 64-bit ELF file, ZipCPU ELF files are all 32-bit\n");
213
                exit(EXIT_FAILURE);
214
        }
215
 
216
        if (dbg) {
217
        printf("    %-20s 0x%jx\n", "e_type", (uintmax_t)ehdr.e_type);
218
        printf("    %-20s 0x%jx\n", "e_machine", (uintmax_t)ehdr.e_machine);
219
        printf("    %-20s 0x%jx\n", "e_version", (uintmax_t)ehdr.e_version);
220
        printf("    %-20s 0x%jx\n", "e_entry", (uintmax_t)ehdr.e_entry);
221
        printf("    %-20s 0x%jx\n", "e_phoff", (uintmax_t)ehdr.e_phoff);
222
        printf("    %-20s 0x%jx\n", "e_shoff", (uintmax_t)ehdr.e_shoff);
223
        printf("    %-20s 0x%jx\n", "e_flags", (uintmax_t)ehdr.e_flags);
224
        printf("    %-20s 0x%jx\n", "e_ehsize", (uintmax_t)ehdr.e_ehsize);
225
        printf("    %-20s 0x%jx\n", "e_phentsize", (uintmax_t)ehdr.e_phentsize);
226
        printf("    %-20s 0x%jx\n", "e_shentsize", (uintmax_t)ehdr.e_shentsize);
227
        printf("\n");
228
        }
229
 
230
 
231
        // Check whether or not this is an ELF file for the ZipCPU ...
232
        if (ehdr.e_machine != 0x0dadd) {
233
                fprintf(stderr, "This is not a ZipCPU ELF file\n");
234
                exit(EXIT_FAILURE);
235
        }
236
 
237
        // Get our entry address
238
        entry = ehdr.e_entry;
239
 
240
 
241
        // Now, let's go look at the program header
242
        if (elf_getphdrnum(e, &n) != 0) {
243
                fprintf(stderr, "elf_getphdrnum() failed: %s\n", elf_errmsg(-1));
244
                exit(EXIT_FAILURE);
245
        }
246
 
247
        unsigned total_octets = 0, current_offset=0, current_section=0;
248
        for(i=0; i<(int)n; i++) {
249
                total_octets += sizeof(SECTION *)+sizeof(SECTION);
250
 
251
                if (gelf_getphdr(e, i, &phdr) != &phdr) {
252
                        fprintf(stderr, "getphdr() failed: %s\n", elf_errmsg(-1));
253
                        exit(EXIT_FAILURE);
254
                }
255
 
256
                if (dbg) {
257
                printf("    %-20s 0x%x\n", "p_type",   phdr.p_type);
258
                printf("    %-20s 0x%jx\n", "p_offset", phdr.p_offset);
259
                printf("    %-20s 0x%jx\n", "p_vaddr",  phdr.p_vaddr);
260
                printf("    %-20s 0x%jx\n", "p_paddr",  phdr.p_paddr);
261
                printf("    %-20s 0x%jx\n", "p_filesz", phdr.p_filesz);
262
                printf("    %-20s 0x%jx\n", "p_memsz",  phdr.p_memsz);
263
                printf("    %-20s 0x%x [", "p_flags",  phdr.p_flags);
264
 
265
                if (phdr.p_flags & PF_X)        printf(" Execute");
266
                if (phdr.p_flags & PF_R)        printf(" Read");
267
                if (phdr.p_flags & PF_W)        printf(" Write");
268
                printf("]\n");
269
                printf("    %-20s 0x%jx\n", "p_align", phdr.p_align);
270
                }
271
 
272
                total_octets += phdr.p_memsz;
273
        }
274
 
275
        char    *d = (char *)malloc(total_octets + sizeof(SECTION)+sizeof(SECTION *));
276
        memset(d, 0, total_octets);
277
 
278
        SECTION **r = sections = (SECTION **)d;
279
        current_offset = (n+1)*sizeof(SECTION *);
280
        current_section = 0;
281
 
282
        for(i=0; i<(int)n; i++) {
283
                r[i] = (SECTION *)(&d[current_offset]);
284
 
285
                if (gelf_getphdr(e, i, &phdr) != &phdr) {
286
                        fprintf(stderr, "getphdr() failed: %s\n", elf_errmsg(-1));
287
                        exit(EXIT_FAILURE);
288
                }
289
 
290
                if (dbg) {
291
                printf("    %-20s 0x%jx\n", "p_offset", phdr.p_offset);
292
                printf("    %-20s 0x%jx\n", "p_vaddr",  phdr.p_vaddr);
293
                printf("    %-20s 0x%jx\n", "p_paddr",  phdr.p_paddr);
294
                printf("    %-20s 0x%jx\n", "p_filesz", phdr.p_filesz);
295
                printf("    %-20s 0x%jx\n", "p_memsz",  phdr.p_memsz);
296
                printf("    %-20s 0x%x [", "p_flags",  phdr.p_flags);
297
 
298
                if (phdr.p_flags & PF_X)        printf(" Execute");
299
                if (phdr.p_flags & PF_R)        printf(" Read");
300
                if (phdr.p_flags & PF_W)        printf(" Write");
301
                printf("]\n");
302
 
303
                printf("    %-20s 0x%jx\n", "p_align", phdr.p_align);
304
                }
305
 
306
                current_section++;
307
 
308
                r[i]->m_start = phdr.p_vaddr;
309
                r[i]->m_len   = phdr.p_filesz/ sizeof(FPGA::BUSW);
310
 
311
                current_offset += phdr.p_memsz + sizeof(SECTION);
312
 
313
                // Now, let's read in our section ...
314
                if (lseek(fd, phdr.p_offset, SEEK_SET) < 0) {
315
                        fprintf(stderr, "Could not seek to file position %08lx\n", phdr.p_offset);
316
                        perror("O/S Err:");
317
                        exit(EXIT_FAILURE);
318
                } if (phdr.p_filesz > phdr.p_memsz)
319
                        phdr.p_filesz = 0;
320
                if (read(fd, r[i]->m_data, phdr.p_filesz) != (int)phdr.p_filesz) {
321
                        fprintf(stderr, "Didnt read entire section\n");
322
                        perror("O/S Err:");
323
                        exit(EXIT_FAILURE);
324
                }
325
 
326
                // Next, we need to byte swap it from big to little endian
327
                for(unsigned j=0; j<r[i]->m_len; j++)
328
                        r[i]->m_data[j] = byteswap(r[i]->m_data[j]);
329
 
330
                if (dbg) for(unsigned j=0; j<r[i]->m_len; j++)
331
                        fprintf(stderr, "ADR[%04x] = %08x\n", r[i]->m_start+j,
332
                        r[i]->m_data[j]);
333
        }
334
 
335
        r[i] = (SECTION *)(&d[current_offset]);
336
        r[current_section]->m_start = 0;
337
        r[current_section]->m_len   = 0;
338
 
339
        elf_end(e);
340
        close(fd);
341
}
342
#endif
343
 
344
void    usage(void) {
345
        printf("USAGE: ziprun [-h] [<bit-file> [<alt-bit-file>]] <zip-program-file>\n");
346
        printf("\n"
347
"\t-h\tDisplay this usage statement\n");
348
}
349
 
350
int main(int argc, char **argv) {
351
        int             skp=0;
352
        bool            permit_raw_files = false;
353
        unsigned        entry = RAMBASE;
354
        FLASHDRVR       *flash = NULL;
355
        const char      *bitfile = NULL, *altbitfile = NULL;
356
 
357
        if (argc < 2) {
358
                usage();
359
                exit(EXIT_SUCCESS);
360
        }
361
 
362
        skp=1;
363
        for(int argn=0; argn<argc-skp; argn++) {
364
                if (argv[argn+skp][0] == '-') {
365
                        switch(argv[argn+skp][1]) {
366
                        case 'h':
367
                                usage();
368
                                exit(EXIT_SUCCESS);
369
                        case 'r':
370
                                permit_raw_files = true;
371
                                break;
372
                        } skp++; argn--;
373
                } else { // Check for bit files
374
                        int sl = strlen(argv[argn+skp]);
375
                        if ((sl>4)&&(strcmp(&argv[argn+skp][sl-4],".bit")==0)) {
376
                                if (bitfile == NULL)
377
                                        bitfile = argv[argn+skp];
378
                                else if (altbitfile == NULL)
379
                                        altbitfile = argv[argn+skp];
380
                                else {
381
                                        fprintf(stderr, "Err: Too many bit files listed\n");
382
                                        exit(EXIT_FAILURE);
383
                                } skp++; argn--;
384
                        } else
385
                                argv[argn] = argv[argn+skp];
386
                }
387
        } argc -= skp;
388
 
389
 
390
        if ((bitfile)&&(access(bitfile,R_OK)!=0)) {
391
                fprintf(stderr, "Cannot open bitfile, %s\n", bitfile);
392
                exit(EXIT_FAILURE);
393
        } if ((altbitfile)&&(access(altbitfile,R_OK)!=0)) {
394
                fprintf(stderr, "Cannot open alternate bitfile, %s\n",
395
                        altbitfile);
396
                exit(EXIT_FAILURE);
397
        } if(((!bitfile)&&(argc<=0)) || ((argc>0)&&(access(argv[0],R_OK)!=0))) {
398
                // If there's no code file, or the code file cannot be opened
399
                if (argc>0)
400
                        fprintf(stderr, "Cannot open executable, %s\n", argv[0]);
401
                else
402
                        usage();
403
                exit(EXIT_FAILURE);
404
        }
405
 
406
        const char *codef = (argc>0)?argv[0]:NULL;
407
        DEVBUS::BUSW    *fbuf = new DEVBUS::BUSW[FLASHWORDS];
408
 
409
        // Set the flash buffer to all ones
410
        memset(fbuf, -1, FLASHWORDS*sizeof(fbuf[0]));
411
 
412
        {
413
                char    szSel[64];
414
                strcpy(szSel, "SN:210282768825");
415
                m_fpga = new FPGA(new DEPPI(szSel));
416
        }
417
 
418
        flash = new FLASHDRVR(m_fpga);
419
 
420
        // First, see if we need to load a bit file
421
        if (bitfile) {
422
                int     len;
423
                FILE    *fp = fopen(bitfile, "rb");
424
 
425
                fseek(fp, 0x5dl, SEEK_SET);
426
                len = fread(&fbuf[CONFIG_ADDRESS-SPIFLASH],
427
                                sizeof(fbuf[0]),
428
                                FLASHWORDS-(CONFIG_ADDRESS-SPIFLASH), fp);
429
                assert(len + CONFIG_ADDRESS < ALTCONFIG_ADDRESS);
430
                fclose(fp);
431
 
432
                for(int i=0; i<4; i++) {
433
                        // printf("0x%08x\n", fbuf[i]);
434
                        assert(fbuf[i] == 0x0ffffffff);
435
                } // printf("0x%08x\n", fbuf[4]);
436
                assert(fbuf[4] == 0x0665599aa);
437
 
438
                printf("Loading: %s\n", bitfile);
439
                if (!flash->write(CONFIG_ADDRESS, len, &fbuf[CONFIG_ADDRESS-SPIFLASH], true)) {
440
                        fprintf(stderr, "Could not write primary bitfile\n");
441
                        exit(EXIT_FAILURE);
442
                }
443
        } if (altbitfile) {
444
                int     len;
445
                FILE    *fp = fopen(altbitfile, "rb");
446
 
447
                // The alternate configuration follows the first configuration
448
                len = fread(&fbuf[ALTCONFIG_ADDRESS-SPIFLASH],
449
                                sizeof(fbuf[0]),
450
                                FLASHWORDS-(ALTCONFIG_ADDRESS-SPIFLASH), fp);
451
                assert(len + ALTCONFIG_ADDRESS < RESET_ADDRESS);
452
                fclose(fp);
453
                printf("Loading: %s\n", altbitfile);
454
 
455
                if (!flash->write(ALTCONFIG_ADDRESS, len, &fbuf[ALTCONFIG_ADDRESS-SPIFLASH], true)) {
456
                        fprintf(stderr, "Could not write alternate bitfile\n");
457
                        exit(EXIT_FAILURE);
458
                }
459
        }
460
 
461
        if (codef) try {
462
                SECTION **secpp = NULL, *secp;
463
 
464
                if(iself(codef)) {
465
                        // zip-readelf will help with both of these ...
466
                        elfread(codef, entry, secpp);
467
                        assert(entry == RESET_ADDRESS);
468
                } else if (permit_raw_files) {
469
                        secpp = rawsection(codef);
470
                        entry = RESET_ADDRESS;
471
                } else {
472
                        fprintf(stderr, "ERR: %s is not in ELF format\n", codef);
473
                        exit(EXIT_FAILURE);
474
                }
475
 
476
                printf("Loading: %s\n", codef);
477
                // assert(secpp[1]->m_len = 0);
478
                for(int i=0; secpp[i]->m_len; i++) {
479
                        bool    valid = false;
480
                        secp=  secpp[i];
481
                        if ((secp->m_start >= RESET_ADDRESS)
482
                                &&(secp->m_start+secp->m_len
483
                                                <= SPIFLASH+FLASHWORDS))
484
                                valid = true;
485
                        if (!valid) {
486
                                fprintf(stderr, "No such memory on board: 0x%08x - %08x\n",
487
                                        secp->m_start, secp->m_start+secp->m_len);
488
                                exit(EXIT_FAILURE);
489
                        }
490
                }
491
 
492
                for(int i=0; secpp[i]->m_len; i++) {
493
                        secp = secpp[i];
494
                        if (!flash->write(secp->m_start, secp->m_len, secp->m_data, true)) {
495
                                fprintf(stderr, "ERR: Could not write program to flash\n");
496
                                exit(EXIT_FAILURE);
497
                        }
498
                }
499
                m_fpga->readio(R_VERSION); // Check for bus errors
500
 
501
                // Now ... how shall we start this CPU?
502
                printf("The CPU should be fully loaded, you may now start\n");
503
                printf("it.  To start the CPU, either toggle power or type\n");
504
                printf("%% wbregs fpgagen1 0 \n");
505
                printf("%% wbregs fpgagen2 0x0300 \n");
506
                printf("%% wbregs fpgacmd  14 \n");
507
        } catch(BUSERR a) {
508
                fprintf(stderr, "XULA-BUS error: %08x\n", a.addr);
509
                exit(-2);
510
        }
511
 
512
        delete  m_fpga;
513
 
514
        return EXIT_SUCCESS;
515
}
516
 

powered by: WebSVN 2.1.0

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