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

Subversion Repositories s6soc

[/] [s6soc/] [trunk/] [sw/] [host/] [zipload.cpp] - Diff between revs 26 and 45

Go to most recent revision | Show entire file | Details | Blame | View Log

Rev 26 Rev 45
Line 16... Line 16...
// Creator:     Dan Gisselquist, Ph.D.
// Creator:     Dan Gisselquist, Ph.D.
//              Gisselquist Technology, LLC
//              Gisselquist Technology, LLC
//
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
//
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
// Copyright (C) 2015-2017, Gisselquist Technology, LLC
//
//
// This program is free software (firmware): you can redistribute it and/or
// This program is free software (firmware): you can redistribute it and/or
// modify it under the terms of  the GNU General Public License as published
// modify it under the terms of  the GNU General Public License as published
// by the Free Software Foundation, either version 3 of the License, or (at
// by the Free Software Foundation, either version 3 of the License, or (at
// your option) any later version.
// your option) any later version.
Line 28... Line 28...
// This program is distributed in the hope that it will be useful, but WITHOUT
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
// for more details.
// for more details.
//
//
 
// You should have received a copy of the GNU General Public License along
 
// with this program.  (It's in the $(ROOT)/doc directory.  Run make with no
 
// target there if the PDF file isn't present.)  If not, see
 
// <http://www.gnu.org/licenses/> for a copy.
 
//
// License:     GPL, v3, as defined and found on www.gnu.org,
// License:     GPL, v3, as defined and found on www.gnu.org,
//              http://www.gnu.org/licenses/gpl.html
//              http://www.gnu.org/licenses/gpl.html
//
//
//
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
//
//
//
//
 
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <fcntl.h>
Line 53... Line 57...
#include "devbus.h"
#include "devbus.h"
#include "llcomms.h"
#include "llcomms.h"
#include "deppi.h"
#include "deppi.h"
#include "regdefs.h"
#include "regdefs.h"
#include "flashdrvr.h"
#include "flashdrvr.h"
 
#include "zipelf.h"
bool    iself(const char *fname) {
 
        FILE    *fp;
 
        bool    ret = true;
 
 
 
        if ((!fname)||(!fname[0]))
 
                return false;
 
 
 
        fp = fopen(fname, "rb");
 
 
 
        if (!fp)        return false;
 
        if (0x7f != fgetc(fp))  ret = false;
 
        if ('E'  != fgetc(fp))  ret = false;
 
        if ('L'  != fgetc(fp))  ret = false;
 
        if ('F'  != fgetc(fp))  ret = false;
 
        fclose(fp);
 
        return  ret;
 
}
 
 
 
long    fgetwords(FILE *fp) {
 
        // Return the number of words in the current file, and return the 
 
        // file as though it had never been adjusted
 
        long    fpos, flen;
 
        fpos = ftell(fp);
 
        if (0 != fseek(fp, 0l, SEEK_END)) {
 
                fprintf(stderr, "ERR: Could not determine file size\n");
 
                perror("O/S Err:");
 
                exit(-2);
 
        } flen = ftell(fp);
 
        if (0 != fseek(fp, fpos, SEEK_SET)) {
 
                fprintf(stderr, "ERR: Could not seek on file\n");
 
                perror("O/S Err:");
 
                exit(-2);
 
        } flen /= sizeof(FPGA::BUSW);
 
        return flen;
 
}
 
 
 
FPGA    *m_fpga;
FPGA    *m_fpga;
class   SECTION {
 
public:
 
        unsigned        m_start, m_len;
 
        FPGA::BUSW      m_data[1];
 
};
 
 
 
SECTION **singlesection(int nwords) {
 
        fprintf(stderr, "NWORDS = %d\n", nwords);
 
        size_t  sz = (2*(sizeof(SECTION)+sizeof(SECTION *))
 
                +(nwords-1)*(sizeof(FPGA::BUSW)));
 
        char    *d = (char *)malloc(sz);
 
        SECTION **r = (SECTION **)d;
 
        memset(r, 0, sz);
 
        r[0] = (SECTION *)(&d[2*sizeof(SECTION *)]);
 
        r[0]->m_len   = nwords;
 
        r[1] = (SECTION *)(&r[0]->m_data[r[0]->m_len]);
 
        r[0]->m_start = 0;
 
        r[1]->m_start = 0;
 
        r[1]->m_len   = 0;
 
 
 
        return r;
 
}
 
 
 
SECTION **rawsection(const char *fname) {
 
        SECTION         **secpp, *secp;
 
        unsigned        num_words;
 
        FILE            *fp;
 
        int             nr;
 
 
 
        fp = fopen(fname, "r");
 
        if (fp == NULL) {
 
                fprintf(stderr, "Could not open: %s\n", fname);
 
                exit(-1);
 
        }
 
 
 
        if ((num_words=fgetwords(fp)) > FLASHWORDS-(RESET_ADDRESS-SPIFLASH)) {
 
                fprintf(stderr, "File overruns flash memory\n");
 
                exit(-1);
 
        }
 
        secpp = singlesection(num_words);
 
        secp = secpp[0];
 
        secp->m_start = RAMBASE;
 
        secp->m_len = num_words;
 
        nr= fread(secp->m_data, sizeof(FPGA::BUSW), num_words, fp);
 
        if (nr != (int)num_words) {
 
                fprintf(stderr, "Could not read entire file\n");
 
                perror("O/S Err:");
 
                exit(-2);
 
        } assert(secpp[1]->m_len == 0);
 
 
 
        return secpp;
 
}
 
 
 
unsigned        byteswap(unsigned n) {
 
        unsigned        r;
 
 
 
        r = (n&0x0ff); n>>= 8;
 
        r = (r<<8) | (n&0x0ff); n>>= 8;
 
        r = (r<<8) | (n&0x0ff); n>>= 8;
 
        r = (r<<8) | (n&0x0ff); n>>= 8;
 
 
 
        return r;
 
}
 
 
 
// #define      CHEAP_AND_EASY
void    usage(void) {
#ifdef  CHEAP_AND_EASY
        printf("USAGE: zipload [-h] [<bit-file> [<alt-bit-file>]] <zip-program-file>\n");
#else
        printf("\n"
#include <libelf.h>
"\t-h\tDisplay this usage statement\n"
#include <gelf.h>
);
 
 
void    elfread(const char *fname, unsigned &entry, SECTION **&sections) {
 
        Elf     *e;
 
        int     fd, i;
 
        size_t  n;
 
        char    *id;
 
        Elf_Kind        ek;
 
        GElf_Ehdr       ehdr;
 
        GElf_Phdr       phdr;
 
        const   bool    dbg = false;
 
 
 
        if (elf_version(EV_CURRENT) == EV_NONE) {
 
                fprintf(stderr, "ELF library initialization err, %s\n", elf_errmsg(-1));
 
                perror("O/S Err:");
 
                exit(EXIT_FAILURE);
 
        } if ((fd = open(fname, O_RDONLY, 0)) < 0) {
 
                fprintf(stderr, "Could not open %s\n", fname);
 
                perror("O/S Err:");
 
                exit(EXIT_FAILURE);
 
        } if ((e = elf_begin(fd, ELF_C_READ, NULL))==NULL) {
 
                fprintf(stderr, "Could not run elf_begin, %s\n", elf_errmsg(-1));
 
                exit(EXIT_FAILURE);
 
        }
 
 
 
        ek = elf_kind(e);
 
        if (ek == ELF_K_ELF) {
 
                ; // This is the kind of file we should expect
 
        } else if (ek == ELF_K_AR) {
 
                fprintf(stderr, "Cannot run an archive!\n");
 
                exit(EXIT_FAILURE);
 
        } else if (ek == ELF_K_NONE) {
 
                ;
 
        } else {
 
                fprintf(stderr, "Unexpected ELF file kind!\n");
 
                exit(EXIT_FAILURE);
 
        }
 
 
 
        if (gelf_getehdr(e, &ehdr) == NULL) {
 
                fprintf(stderr, "getehdr() failed: %s\n", elf_errmsg(-1));
 
                exit(EXIT_FAILURE);
 
        } if ((i=gelf_getclass(e)) == ELFCLASSNONE) {
 
                fprintf(stderr, "getclass() failed: %s\n", elf_errmsg(-1));
 
                exit(EXIT_FAILURE);
 
        } if ((id = elf_getident(e, NULL)) == NULL) {
 
                fprintf(stderr, "getident() failed: %s\n", elf_errmsg(-1));
 
                exit(EXIT_FAILURE);
 
        } if (i != ELFCLASS32) {
 
                fprintf(stderr, "This is a 64-bit ELF file, ZipCPU ELF files are all 32-bit\n");
 
                exit(EXIT_FAILURE);
 
        }
 
 
 
        if (dbg) {
 
        printf("    %-20s 0x%jx\n", "e_type", (uintmax_t)ehdr.e_type);
 
        printf("    %-20s 0x%jx\n", "e_machine", (uintmax_t)ehdr.e_machine);
 
        printf("    %-20s 0x%jx\n", "e_version", (uintmax_t)ehdr.e_version);
 
        printf("    %-20s 0x%jx\n", "e_entry", (uintmax_t)ehdr.e_entry);
 
        printf("    %-20s 0x%jx\n", "e_phoff", (uintmax_t)ehdr.e_phoff);
 
        printf("    %-20s 0x%jx\n", "e_shoff", (uintmax_t)ehdr.e_shoff);
 
        printf("    %-20s 0x%jx\n", "e_flags", (uintmax_t)ehdr.e_flags);
 
        printf("    %-20s 0x%jx\n", "e_ehsize", (uintmax_t)ehdr.e_ehsize);
 
        printf("    %-20s 0x%jx\n", "e_phentsize", (uintmax_t)ehdr.e_phentsize);
 
        printf("    %-20s 0x%jx\n", "e_shentsize", (uintmax_t)ehdr.e_shentsize);
 
        printf("\n");
 
        }
 
 
 
 
 
        // Check whether or not this is an ELF file for the ZipCPU ...
 
        if (ehdr.e_machine != 0x0dadd) {
 
                fprintf(stderr, "This is not a ZipCPU ELF file\n");
 
                exit(EXIT_FAILURE);
 
        }
 
 
 
        // Get our entry address
 
        entry = ehdr.e_entry;
 
 
 
 
 
        // Now, let's go look at the program header
 
        if (elf_getphdrnum(e, &n) != 0) {
 
                fprintf(stderr, "elf_getphdrnum() failed: %s\n", elf_errmsg(-1));
 
                exit(EXIT_FAILURE);
 
        }
 
 
 
        unsigned total_octets = 0, current_offset=0, current_section=0;
 
        for(i=0; i<(int)n; i++) {
 
                total_octets += sizeof(SECTION *)+sizeof(SECTION);
 
 
 
                if (gelf_getphdr(e, i, &phdr) != &phdr) {
 
                        fprintf(stderr, "getphdr() failed: %s\n", elf_errmsg(-1));
 
                        exit(EXIT_FAILURE);
 
                }
 
 
 
                if (dbg) {
 
                printf("    %-20s 0x%x\n", "p_type",   phdr.p_type);
 
                printf("    %-20s 0x%jx\n", "p_offset", phdr.p_offset);
 
                printf("    %-20s 0x%jx\n", "p_vaddr",  phdr.p_vaddr);
 
                printf("    %-20s 0x%jx\n", "p_paddr",  phdr.p_paddr);
 
                printf("    %-20s 0x%jx\n", "p_filesz", phdr.p_filesz);
 
                printf("    %-20s 0x%jx\n", "p_memsz",  phdr.p_memsz);
 
                printf("    %-20s 0x%x [", "p_flags",  phdr.p_flags);
 
 
 
                if (phdr.p_flags & PF_X)        printf(" Execute");
 
                if (phdr.p_flags & PF_R)        printf(" Read");
 
                if (phdr.p_flags & PF_W)        printf(" Write");
 
                printf("]\n");
 
                printf("    %-20s 0x%jx\n", "p_align", phdr.p_align);
 
                }
 
 
 
                total_octets += phdr.p_memsz;
 
        }
        }
 
 
        char    *d = (char *)malloc(total_octets + sizeof(SECTION)+sizeof(SECTION *));
void    skip_bitfile_header(FILE *fp) {
        memset(d, 0, total_octets);
        const unsigned  SEARCHLN = 204, MATCHLN = 16;
 
        const unsigned char matchstr[MATCHLN] = {
        SECTION **r = sections = (SECTION **)d;
                0xff, 0xff, 0xff, 0xff,
        current_offset = (n+1)*sizeof(SECTION *);
                0xff, 0xff, 0xff, 0xff,
        current_section = 0;
                0xff, 0xff, 0xff, 0xff,
 
                //
        for(i=0; i<(int)n; i++) {
                0xaa, 0x99, 0x55, 0x66 };
                r[i] = (SECTION *)(&d[current_offset]);
        unsigned char   buf[SEARCHLN];
 
 
                if (gelf_getphdr(e, i, &phdr) != &phdr) {
        rewind(fp);
                        fprintf(stderr, "getphdr() failed: %s\n", elf_errmsg(-1));
        fread(buf, sizeof(char), SEARCHLN, fp);
                        exit(EXIT_FAILURE);
        for(int start=0; start+MATCHLN<SEARCHLN; start++) {
 
                int     mloc;
 
 
 
                // Search backwards, since the starting bytes just aren't that
 
                // interesting.
 
                for(mloc = MATCHLN-1; mloc >= 0; mloc--)
 
                        if (buf[start+mloc] != matchstr[mloc])
 
                                break;
 
                if (mloc < 0) {
 
                        fseek(fp, start, SEEK_SET);
 
                        return;
                }
                }
 
 
                if (dbg) {
 
                printf("    %-20s 0x%jx\n", "p_offset", phdr.p_offset);
 
                printf("    %-20s 0x%jx\n", "p_vaddr",  phdr.p_vaddr);
 
                printf("    %-20s 0x%jx\n", "p_paddr",  phdr.p_paddr);
 
                printf("    %-20s 0x%jx\n", "p_filesz", phdr.p_filesz);
 
                printf("    %-20s 0x%jx\n", "p_memsz",  phdr.p_memsz);
 
                printf("    %-20s 0x%x [", "p_flags",  phdr.p_flags);
 
 
 
                if (phdr.p_flags & PF_X)        printf(" Execute");
 
                if (phdr.p_flags & PF_R)        printf(" Read");
 
                if (phdr.p_flags & PF_W)        printf(" Write");
 
                printf("]\n");
 
 
 
                printf("    %-20s 0x%jx\n", "p_align", phdr.p_align);
 
                }
                }
 
 
                current_section++;
        fprintf(stderr, "Could not find bin-file header within bit file\n");
 
        fclose(fp);
                r[i]->m_start = phdr.p_paddr;
 
                r[i]->m_len   = phdr.p_filesz/ sizeof(FPGA::BUSW);
 
 
 
                current_offset += phdr.p_memsz + sizeof(SECTION);
 
 
 
                // Now, let's read in our section ...
 
                if (lseek(fd, phdr.p_offset, SEEK_SET) < 0) {
 
                        fprintf(stderr, "Could not seek to file position %08lx\n", phdr.p_offset);
 
                        perror("O/S Err:");
 
                        exit(EXIT_FAILURE);
 
                } if (phdr.p_filesz > phdr.p_memsz)
 
                        phdr.p_filesz = 0;
 
                if (read(fd, r[i]->m_data, phdr.p_filesz) != (int)phdr.p_filesz) {
 
                        fprintf(stderr, "Didnt read entire section\n");
 
                        perror("O/S Err:");
 
                        exit(EXIT_FAILURE);
                        exit(EXIT_FAILURE);
                }
                }
 
 
                // Next, we need to byte swap it from big to little endian
 
                for(unsigned j=0; j<r[i]->m_len; j++)
 
                        r[i]->m_data[j] = byteswap(r[i]->m_data[j]);
 
 
 
                if (dbg) for(unsigned j=0; j<r[i]->m_len; j++)
 
                        fprintf(stderr, "ADR[%04x] = %08x\n", r[i]->m_start+j,
 
                        r[i]->m_data[j]);
 
        }
 
 
 
        r[i] = (SECTION *)(&d[current_offset]);
 
        r[current_section]->m_start = 0;
 
        r[current_section]->m_len   = 0;
 
 
 
        elf_end(e);
 
        close(fd);
 
}
 
#endif
 
 
 
void    usage(void) {
 
        printf("USAGE: ziprun [-h] [<bit-file> [<alt-bit-file>]] <zip-program-file>\n");
 
        printf("\n"
 
"\t-h\tDisplay this usage statement\n");
 
}
 
 
 
int main(int argc, char **argv) {
int main(int argc, char **argv) {
        int             skp=0;
        int             skp=0, argn;
        bool            permit_raw_files = false, debug_only = false;
        bool            debug_only = false, verbose = false;
        unsigned        entry = RAMBASE;
        bool            ignore_missing_memory = false;
 
        unsigned        entry = 0;
        FLASHDRVR       *flash = NULL;
        FLASHDRVR       *flash = NULL;
        const char      *bitfile = NULL, *altbitfile = NULL;
        const char      *bitfile = NULL, *altbitfile = NULL, *execfile = NULL;
 
        size_t          bitsz;
 
        FILE            *fp;
 
 
        if (argc < 2) {
        if (argc < 2) {
                usage();
                usage();
                exit(EXIT_SUCCESS);
                exit(EXIT_SUCCESS);
        }
        }
 
 
        skp=1;
        skp=1;
        for(int argn=0; argn<argc-skp; argn++) {
        for(argn=0; argn<argc-skp; argn++) {
                if (argv[argn+skp][0] == '-') {
                if (argv[argn+skp][0] == '-') {
                        switch(argv[argn+skp][1]) {
                        switch(argv[argn+skp][1]) {
                        case 'd':
                        case 'd':
                                debug_only = true;
                                debug_only = true;
                                break;
                                break;
                        case 'h':
                        case 'h':
                                usage();
                                usage();
                                exit(EXIT_SUCCESS);
                                exit(EXIT_SUCCESS);
                                break;
                                break;
                        case 'r':
                        case 'v':
                                permit_raw_files = true;
                                verbose = true;
                                break;
                                break;
                        default:
                        default:
                                fprintf(stderr, "Unknown option, -%c\n\n",
                                fprintf(stderr, "Unknown option, -%c\n\n",
                                        argv[argn+skp][0]);
                                        argv[argn+skp][0]);
                                usage();
                                usage();
                                exit(EXIT_FAILURE);
                                exit(EXIT_FAILURE);
                                break;
                                break;
                        } skp++; argn--;
                        } skp++; argn--;
                } else { // Check for bit files
                } else {
                        int sl = strlen(argv[argn+skp]);
                        // Anything here must be either the program to load,
                        if ((sl>4)&&(strcmp(&argv[argn+skp][sl-4],".bit")==0)) {
                        // or a bit file to load
                                if (bitfile == NULL)
 
                                        bitfile = argv[argn+skp];
 
                                else if (altbitfile == NULL)
 
                                        altbitfile = argv[argn+skp];
 
                                else {
 
                                        fprintf(stderr, "Err: Too many bit files listed\n");
 
                                        exit(EXIT_FAILURE);
 
                                } skp++; argn--;
 
                        } else
 
                                argv[argn] = argv[argn+skp];
                                argv[argn] = argv[argn+skp];
                }
                }
        } argc -= skp;
        } argc -= skp;
 
 
 
 
 
        for(argn=0; argn<argc; argn++) {
 
                if (iself(argv[argn])) {
 
                        if (execfile) {
 
                                printf("Too many executable files given, %s and %s\n", execfile, argv[argn]);
 
                                usage();
 
                                exit(EXIT_FAILURE);
 
                        } execfile = argv[argn];
 
                } else { // if (isbitfile(argv[argn]))
 
                        if (!bitfile)
 
                                bitfile = argv[argn];
 
                        else if (!altbitfile)
 
                                altbitfile = argv[argn];
 
                        else {
 
                                printf("Unknown file name or too many files, %s\n", argv[argn]);
 
                                usage();
 
                                exit(EXIT_FAILURE);
 
                        }
 
                }
 
        }
 
 
 
if (verbose) {
 
if (bitfile)    printf(" BITFILE: %s\n", bitfile);
 
if (altbitfile) printf("ABITFILE: %s\n", altbitfile);
 
if (execfile)   printf("EXECTFILE: %s\n", execfile);
 
}
 
 
 
        if ((execfile == NULL)&&(bitfile == NULL)) {
 
                printf("No executable or bit file(s) given!\n\n");
 
                usage();
 
                exit(EXIT_FAILURE);
 
        }
 
 
 
        if ((bitfile == NULL)&&(altbitfile != NULL)) {
 
                printf("Cannot program an alternate bitfile without a main bitfile\n\n");
 
                usage();
 
                exit(EXIT_FAILURE);
 
        }
 
 
        if ((bitfile)&&(access(bitfile,R_OK)!=0)) {
        if ((bitfile)&&(access(bitfile,R_OK)!=0)) {
 
                // If there's no code file, or the code file cannot be opened
                fprintf(stderr, "Cannot open bitfile, %s\n", bitfile);
                fprintf(stderr, "Cannot open bitfile, %s\n", bitfile);
                exit(EXIT_FAILURE);
                exit(EXIT_FAILURE);
        } if ((altbitfile)&&(access(altbitfile,R_OK)!=0)) {
        }
                fprintf(stderr, "Cannot open alternate bitfile, %s\n",
 
                        altbitfile);
        if ((altbitfile)&&(access(altbitfile,R_OK)!=0)) {
 
                // If there's no code file, or the code file cannot be opened
 
                fprintf(stderr, "Cannot open alternate bitfile, %s\n", altbitfile);
                exit(EXIT_FAILURE);
                exit(EXIT_FAILURE);
        } if(((!bitfile)&&(argc<=0)) || ((argc>0)&&(access(argv[0],R_OK)!=0))) {
        } if ((execfile)&&(access(execfile,R_OK)!=0)) {
                // If there's no code file, or the code file cannot be opened
                // If there's no code file, or the code file cannot be opened
                if (argc>0)
                fprintf(stderr, "Cannot open executable, %s\n\n", execfile);
                        fprintf(stderr, "Cannot open executable, %s\n", argv[0]);
                usage();
                else
                exit(EXIT_FAILURE);
 
        } else if (!iself(execfile)) {
 
                printf("%s is not an executable file\n\n", execfile);
                        usage();
                        usage();
                exit(EXIT_FAILURE);
                exit(EXIT_FAILURE);
        }
        }
 
 
        const char *codef = (argc>0)?argv[0]:NULL;
        char    *fbuf = new char[FLASHLEN];
        DEVBUS::BUSW    *fbuf = new DEVBUS::BUSW[FLASHWORDS];
 
 
 
        // Set the flash buffer to all ones
        // Set the flash buffer to all ones
        memset(fbuf, -1, FLASHWORDS*sizeof(fbuf[0]));
        memset(fbuf, -1, FLASHLEN);
 
 
        if (debug_only) {
        if (debug_only) {
                m_fpga = NULL;
                m_fpga = NULL;
        } else {
        } else {
                char    szSel[64];
                char    szSel[64];
                strcpy(szSel, "SN:210282768825");
                strcpy(szSel, S6SN);
                m_fpga = new FPGA(new DEPPI(szSel));
                m_fpga = new FPGA(new DEPPI(szSel));
        }
        }
 
 
 
        // Make certain we can talk to the FPGA
 
        try {
 
                unsigned v  = m_fpga->readio(R_VERSION);
 
                if (v < 0x20170000) {
 
                        fprintf(stderr, "Could not communicate with board (invalid version)\n");
 
                        exit(EXIT_FAILURE);
 
                }
 
        } catch(BUSERR b) {
 
                fprintf(stderr, "Could not communicate with board (BUSERR when reading VERSION)\n");
 
                exit(EXIT_FAILURE);
 
        }
 
 
        flash = (debug_only)?NULL : new FLASHDRVR(m_fpga);
        flash = (debug_only)?NULL : new FLASHDRVR(m_fpga);
 
 
        // First, see if we need to load a bit file
        // First, see if we need to load a bit file
        if (bitfile) {
        if (bitfile) {
                int     len;
 
                FILE    *fp = fopen(bitfile, "rb");
 
 
 
                fseek(fp, 0x5dl, SEEK_SET);
                fp = fopen(bitfile, "r");
                len = fread(&fbuf[CONFIG_ADDRESS-SPIFLASH],
                if (strcmp(&argv[argn][strlen(argv[argn])-4],".bit")==0)
 
                        skip_bitfile_header(fp);
 
                bitsz = fread(&fbuf[CONFIG_ADDRESS-SPIFLASH],
                                sizeof(fbuf[0]),
                                sizeof(fbuf[0]),
                                FLASHWORDS-(CONFIG_ADDRESS-SPIFLASH), fp);
                                FLASHLEN - (CONFIG_ADDRESS-SPIFLASH), fp);
                assert(len + CONFIG_ADDRESS < ALTCONFIG_ADDRESS);
 
                fclose(fp);
                fclose(fp);
 
 
                for(int i=0; i<4; i++) {
                try {
                        // printf("0x%08x\n", fbuf[i]);
 
                        assert(fbuf[i] == 0x0ffffffff);
 
                } // printf("0x%08x\n", fbuf[4]);
 
                assert(fbuf[4] == 0x0665599aa);
 
 
 
                printf("Loading: %s\n", bitfile);
                printf("Loading: %s\n", bitfile);
                if ((flash)&&(!flash->write(CONFIG_ADDRESS, len, &fbuf[CONFIG_ADDRESS-SPIFLASH], true))) {
                        flash->write(CONFIG_ADDRESS, bitsz, fbuf, true);
                        fprintf(stderr, "Could not write primary bitfile\n");
                } catch(BUSERR b) {
                        exit(EXIT_FAILURE);
                        fprintf(stderr, "BUS-ERR @0x%08x\n", b.addr);
 
                        exit(-1);
 
                }
                }
                }
        } if (altbitfile) {
 
                int     len;
 
                FILE    *fp = fopen(altbitfile, "rb");
 
 
 
                // The alternate configuration follows the first configuration
        // Then see if we were given an alternate bit file
                len = fread(&fbuf[ALTCONFIG_ADDRESS-SPIFLASH],
        if (altbitfile) {
 
                size_t  altsz;
 
                assert(CONFIG_ADDRESS + bitsz < ALTCONFIG_ADDRESS);
 
 
 
                fp = fopen(altbitfile, "r");
 
                if (strcmp(&argv[argn][strlen(argv[argn])-4],".bit")==0)
 
                        skip_bitfile_header(fp);
 
                altsz = fread(&fbuf[ALTCONFIG_ADDRESS-SPIFLASH],
                                sizeof(fbuf[0]),
                                sizeof(fbuf[0]),
                                FLASHWORDS-(ALTCONFIG_ADDRESS-SPIFLASH), fp);
                                FLASHLEN-(ALTCONFIG_ADDRESS-SPIFLASH), fp);
                assert(len + ALTCONFIG_ADDRESS < RESET_ADDRESS);
                assert(ALTCONFIG_ADDRESS+altsz < RESET_ADDRESS);
                fclose(fp);
                fclose(fp);
                printf("Loading: %s\n", altbitfile);
 
 
 
                if ((flash)&&(!flash->write(ALTCONFIG_ADDRESS, len, &fbuf[ALTCONFIG_ADDRESS-SPIFLASH], true))) {
                try {
                        fprintf(stderr, "Could not write alternate bitfile\n");
                        printf("Loading: %s\n", altbitfile);
                        exit(EXIT_FAILURE);
                        flash->write(ALTCONFIG_ADDRESS, altsz, fbuf, true);
 
                } catch(BUSERR b) {
 
                        fprintf(stderr, "BUS-ERR @0x%08x\n", b.addr);
 
                        exit(-1);
                }
                }
 
        } else {
 
                assert(CONFIG_ADDRESS+bitsz < RESET_ADDRESS);
        }
        }
 
 
        if (codef) try {
        if (execfile) try {
                SECTION **secpp = NULL, *secp;
                ELFSECTION      **secpp = NULL, *secp;
 
 
                if(iself(codef)) {
                if(iself(execfile)) {
                        // zip-readelf will help with both of these ...
                        // zip-readelf will help with both of these ...
                        elfread(codef, entry, secpp);
                        elfread(execfile, entry, secpp);
                        assert(entry == RESET_ADDRESS);
                        assert(entry == RESET_ADDRESS);
                } else if (permit_raw_files) {
 
                        secpp = rawsection(codef);
 
                        entry = RESET_ADDRESS;
 
                } else {
                } else {
                        fprintf(stderr, "ERR: %s is not in ELF format\n", codef);
                        fprintf(stderr, "ERR: %s is not in ELF format\n", execfile);
                        exit(EXIT_FAILURE);
                        exit(EXIT_FAILURE);
                }
                }
 
 
                printf("Loading: %s\n", codef);
                printf("Loading: %s\n", execfile);
                // assert(secpp[1]->m_len = 0);
                // assert(secpp[1]->m_len = 0);
                for(int i=0; secpp[i]->m_len; i++) {
                for(int i=0; secpp[i]->m_len; i++) {
                        bool    valid = false;
                        bool    valid = false;
                        secp=  secpp[i];
                        secp=  secpp[i];
                        if ((secp->m_start >= RESET_ADDRESS)
                        if ((secp->m_start >= RESET_ADDRESS)
                                &&(secp->m_start+secp->m_len
                                &&(secp->m_start+secp->m_len
                                                <= SPIFLASH+FLASHWORDS))
                                                <= SPIFLASH+FLASHLEN))
                                valid = true;
 
                        if ((secp->m_start >= RAMBASE)
 
                                &&(secp->m_start+secp->m_len
 
                                                <= RAMBASE+MEMWORDS))
 
                                valid = true;
                                valid = true;
                        if (!valid) {
                        if (!valid) {
                                fprintf(stderr, "No such memory on board: 0x%08x - %08x\n",
                                fprintf(stderr, "No such memory on board: 0x%08x - %08x\n",
                                        secp->m_start, secp->m_start+secp->m_len);
                                        secp->m_start, secp->m_start+secp->m_len);
 
                                if (!ignore_missing_memory)
                                exit(EXIT_FAILURE);
                                exit(EXIT_FAILURE);
                        }
                        }
                }
                }
 
 
                unsigned        startaddr = RESET_ADDRESS, codelen = 0;
                unsigned        startaddr = RESET_ADDRESS, codelen = 0;
                for(int i=0; secpp[i]->m_len; i++) {
                for(int i=0; secpp[i]->m_len; i++) {
                        secp = secpp[i];
                        secp = secpp[i];
                        if ((secp->m_start >= RAMBASE)
 
                                &&(secp->m_start+secp->m_len
                        unsigned start, idx, ln;
                                                <= RAMBASE+MEMWORDS)) {
 
                                for(int i=0; (unsigned)i<secp->m_len; i++) {
                        start = secp->m_start;
                                        if (secp->m_data[i] != 0) {
                        idx = 0;
                                                fprintf(stderr, "ERR: Cannot set RAM upon bootup!\n");
                        ln = secp->m_len;
                                                fprintf(stderr, "(The bootloaders just not that smart ... yet)\n");
                        if (secp->m_start < SPIFLASH) {
                                                fprintf(stderr, "Attempting to set %08x - %08x\n", secp->m_start, secp->m_start+secp->m_len-1);
                                start = SPIFLASH;
                                                fprintf(stderr, "%08x cannot be set to %08x\n", secp->m_start+i, secp->m_data[i]);
                                idx = SPIFLASH-secp->m_start;
                                                exit(EXIT_FAILURE);
                                if (idx > secp->m_len)
                                        }
                                        continue;
                                }
                                ln = secp->m_len-idx;
                        } else {
                        } if (start + ln > SPIFLASH+FLASHLEN) {
                                if (secp->m_start < startaddr) {
                                if (start > SPIFLASH+FLASHLEN)
 
                                        continue;
 
                                ln = SPIFLASH+FLASHLEN-start;
 
                        }
 
 
 
                        // We only ever write to the flash
 
                        if (start < startaddr) {
 
                                // Keep track of the first address in
 
                                // flash, as well as the last address
 
                                // that we will write
                                        codelen += (startaddr-secp->m_start);
                                        codelen += (startaddr-secp->m_start);
                                        startaddr = secp->m_start;
                                        startaddr = secp->m_start;
                                } if (secp->m_start+secp->m_len > startaddr+codelen) {
                        } if (start+ln > startaddr+codelen) {
                                        codelen = secp->m_start+secp->m_len-startaddr;
                                        codelen = secp->m_start+secp->m_len-startaddr;
                                } memcpy(&fbuf[secp->m_start-SPIFLASH],
                        } memcpy(&fbuf[start-SPIFLASH], &secp->m_data[idx], ln);
                                        secp->m_data,
 
                                        secp->m_len*sizeof(FPGA::BUSW));
 
                        }
 
                }
                }
                if ((flash)&&(!flash->write(startaddr, codelen, &fbuf[startaddr-SPIFLASH], true))) {
                if ((flash)&&(!flash->write(startaddr, codelen, &fbuf[startaddr-SPIFLASH], true))) {
                        fprintf(stderr, "ERR: Could not write program to flash\n");
                        fprintf(stderr, "ERR: Could not write program to flash\n");
                        exit(EXIT_FAILURE);
                        exit(EXIT_FAILURE);
                } else if (!flash)
                } else if (!flash)
                        printf("flash->write(%08x, %d, ... );\n", startaddr,
                        printf("flash->write(%08x, %d, ... );\n", startaddr,
                                codelen);
                                codelen);
                if (m_fpga) m_fpga->readio(R_VERSION); // Check for bus errors
                if (m_fpga) m_fpga->readio(R_VERSION); // Check for bus errors
 
 
                // Now ... how shall we start this CPU?
                // Now ... how shall we start this CPU?
                printf("The CPU should be fully loaded, you may now start\n");
 
                printf("it.  To start the CPU, either toggle power or type\n");
 
                printf("%% wbregs fpgagen1 0 \n");
 
                printf("%% wbregs fpgagen2 0x0300 \n");
 
                printf("%% wbregs fpgacmd  14 \n");
 
        } catch(BUSERR a) {
        } catch(BUSERR a) {
                fprintf(stderr, "XULA-BUS error: %08x\n", a.addr);
                fprintf(stderr, "S6-BUS error: %08x\n", a.addr);
                exit(-2);
                exit(-2);
        }
        }
 
 
        if (m_fpga) delete      m_fpga;
        if (m_fpga) delete      m_fpga;
 
 

powered by: WebSVN 2.1.0

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