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

Subversion Repositories ftdi_wb_bridge

[/] [ftdi_wb_bridge/] [trunk/] [sw/] [verify.c] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ultra_embe
#include <stdio.h>
2
#include <string.h>
3
#include <stdlib.h>
4
#include <unistd.h>
5
#include "ftdi_hw.h"
6
 
7
//-----------------------------------------------------------------
8
// Defines:
9
//-----------------------------------------------------------------
10
#define CHUNK_SIZE          256
11
#define DEFAULT_FTDI_IFACE  1
12
 
13
//-----------------------------------------------------------------
14
// load_file_to_mem
15
//-----------------------------------------------------------------
16
static uint8_t* load_file_to_mem(const char *filename, long size_override, int *pSize)
17
{
18
    uint8_t *buf = NULL;
19
    FILE *f = fopen(filename, "rb");
20
 
21
    *pSize = 0;
22
 
23
    if (f)
24
    {
25
        long size;
26
 
27
        // Get size of file
28
        fseek(f, 0, SEEK_END);
29
        size = ftell(f);
30
        rewind(f);
31
 
32
        // User overriden file size
33
        if (size_override >= 0)
34
        {
35
            if (size > size_override)
36
                size = size_override;
37
        }
38
 
39
        buf = (uint8_t*)malloc(size);
40
        if (buf)
41
        {
42
            // Read file data into allocated memory
43
            int len = fread(buf, 1, size, f);
44
            if (len != size)
45
            {
46
                free(buf);
47
                buf = NULL;
48
            }
49
            else
50
                *pSize = size;
51
        }
52
        fclose(f);
53
    }
54
 
55
    return buf;
56
}
57
//-----------------------------------------------------------------
58
// compare
59
//-----------------------------------------------------------------
60
static int compare(uint32_t addr, uint8_t *data, int length)
61
{
62
    uint8_t buf[CHUNK_SIZE];
63
    int res = 1;
64
    int i;
65
    int size;
66
 
67
    for (i=0;i<length;i+=CHUNK_SIZE)
68
    {
69
        size = (length - i);
70
        if (size > CHUNK_SIZE)
71
            size = CHUNK_SIZE;
72
 
73
        if (ftdi_hw_mem_read(addr, buf, size) != size)
74
        {
75
            fprintf(stderr, "Compare: Error downloading file\n");
76
            res = -1;
77
            break;
78
        }
79
 
80
        // Check for differences
81
        if (memcmp(data, buf, size) != 0)
82
        {
83
            res = 0;
84
            break;
85
        }
86
 
87
        addr += CHUNK_SIZE;
88
        data += CHUNK_SIZE;
89
 
90
        printf("\r%d%%", (i * 100) / length);
91
        fflush(stdout);
92
    }
93
 
94
    return res;
95
}
96
//-----------------------------------------------------------------
97
// main
98
//-----------------------------------------------------------------
99
int main(int argc, char *argv[])
100
{
101
    int c;
102
    long size_override = -1;
103
    char *filename = NULL;
104
    int help = 0;
105
    int err = 1;
106
    int res;
107
    int size;
108
    uint32_t address = 0x0;
109
    uint8_t *buf;
110
    int ftdi_iface = DEFAULT_FTDI_IFACE;
111
 
112
    while ((c = getopt (argc, argv, "f:s:a:i:")) != -1)
113
    {
114
        switch(c)
115
        {
116
            case 'f':
117
                 filename = optarg;
118
                 break;
119
            case 's':
120
                 size_override = strtol(optarg, NULL, 0);
121
                 break;
122
            case 'a':
123
                address = strtoul(optarg, NULL, 0);
124
                break;
125
            case 'i':
126
                 ftdi_iface = (int)strtol(optarg, NULL, 0);
127
                 break;
128
            default:
129
                help = 1;
130
                break;
131
        }
132
    }
133
 
134
    if (help || filename == NULL)
135
    {
136
        fprintf (stderr,"Usage:\n");
137
        fprintf (stderr,"-f filename.bin   = Executable to compare (binary)\n");
138
        fprintf (stderr,"-a 0xnnnn         = Address to compare to (default to 0x0)\n");
139
        fprintf (stderr,"-i id             = FTDI interface ID (0 = A, 1 = B)\n");
140
        fprintf (stderr,"-s 0xnnnn         = Size override\n");
141
 
142
        exit(-1);
143
    }
144
 
145
    // Try and communicate with FTDI interface
146
    if (ftdi_hw_init(ftdi_iface) != 0)
147
    {
148
        fprintf(stderr, "ERROR: Could not open FTDI interface, try SUDOing / check connection\n");
149
        exit(-2);
150
    }
151
 
152
    // Read file into memory
153
    buf = load_file_to_mem(filename, size_override, &size);
154
    if (buf)
155
    {
156
        printf("Comparing %s (%dKB) to 0x%x:\n", filename, (size + 1023) / 1024, address);
157
 
158
        // Upload file to target
159
        res = compare(address, buf, size);
160
 
161
        // Free file memory
162
        free(buf);
163
        buf = NULL;
164
 
165
        if (res == 1)
166
            printf("\nMatches!\n");
167
        else if (res == 0)
168
            printf("\nDiffers!\n");
169
        else
170
        {
171
            printf("\n");
172
            err = 1;
173
        }
174
    }
175
    else
176
    {
177
        fprintf (stderr,"Error: Could not open image\n");
178
        err = 1;
179
    }
180
 
181
    ftdi_hw_close();
182
 
183
    return err;
184
}

powered by: WebSVN 2.1.0

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