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

Subversion Repositories test_project

[/] [test_project/] [trunk/] [sw/] [utils/] [bin2hex.c] - Blame information for rev 31

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

Line No. Rev Author Line
1 25 julius
#include <stdio.h>
2
#include <stdlib.h>
3 31 julius
#include <string.h>
4 25 julius
/* Number of bytes before line is broken
5
   For example if target flash is 8 bits wide,
6
   define BREAK as 1. If it is 16 bits wide,
7
   define it as 2 etc.
8
*/
9
#define BREAK 1
10
 
11
int main(int argc, char **argv)
12
{
13
 
14
        FILE  *fd;
15
        int c;
16
        int i = 0;
17 31 julius
        int write_size_word=1; // ON BY DEFAULT NOW!
18
        int filename_index=1;
19
        unsigned int image_size;
20 25 julius
 
21
        if(argc < 2) {
22 31 julius
                fprintf(stderr,"\n\tInsufficient options.\n");
23
                fprintf(stderr,"\tPlease specify binary file to convert.\n");
24
                fprintf(stderr,"\tOptionally specify the option -size_word to output,\n");
25
                fprintf(stderr,"\tthe size of the image in the first 4 bytes. This is\n");
26
                fprintf(stderr,"\tused by some of the new OR1k bootloaders.\n\n");
27 25 julius
                exit(1);
28
        }
29
 
30 31 julius
        if(argc == 3)
31
          {
32
            if (strcmp("-size_word", argv[1]) == 0)
33
              {
34
                filename_index=2;
35
                write_size_word=1;
36
              }
37
            else if (strcmp("-size_word", argv[2]) == 0)
38
              {
39
                filename_index=1;
40
                write_size_word=1;
41
              }
42
          }
43
 
44
        fd = fopen( argv[filename_index], "r" );
45
 
46 25 julius
        if (fd == NULL) {
47
                fprintf(stderr,"failed to open input file: %s\n",argv[1]);
48
                exit(1);
49
        }
50
 
51 31 julius
        if (write_size_word)
52
          {
53
            // or1200 startup method of determining size of boot image we're copying by reading out
54
            // the very first word in flash is used. Determine the length of this file
55
            fseek(fd, 0, SEEK_END);
56
            image_size = ftell(fd);
57
            fseek(fd,0,SEEK_SET);
58
 
59
            // Now we should have the size of the file in bytes. Let's ensure it's a word multiple
60
            image_size+=3;
61
            image_size &= 0xfffffffc;
62
 
63
            // Sanity check on image size
64
            if (image_size < 8){
65
              fprintf(stderr, "Bad binary image. Size too small\n");
66
              return 1;
67
            }
68
 
69
            // Now write out the image size
70
            i=0;
71
            printf("%.2x",(image_size >> 24) & 0xff);
72
            if(++i==BREAK){ printf("\n"); i=0; }
73
            printf("%.2x",(image_size >> 16) & 0xff);
74
            if(++i==BREAK){ printf("\n"); i=0; }
75
            printf("%.2x",(image_size >> 8) & 0xff);
76
            if(++i==BREAK){ printf("\n"); i=0; }
77
            printf("%.2x",(image_size) & 0xff);
78
            if(++i==BREAK){ printf("\n"); i=0; }
79
          }
80
 
81
        // Fix for the current bootloader software! Skip the first 4 bytes of application data. Hopefully it's not important. 030509 -- jb
82
        for(i=0;i<4;i++)
83
          c=fgetc(fd);
84
 
85
        i=0;
86
 
87
        // Now write out the binary data to hex format
88 25 julius
        while ((c = fgetc(fd)) != EOF) {
89
                printf("%.2x", (unsigned int) c);
90
                if (++i == BREAK) {
91
                        printf("\n");
92
                        i = 0;
93
                }
94
        }
95
 
96
        return 0;
97
}

powered by: WebSVN 2.1.0

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