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

Subversion Repositories riscv_vhdl

[/] [riscv_vhdl/] [trunk/] [examples/] [elf2raw64/] [src/] [main.cpp] - Blame information for rev 5

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 5 sergeykhbr
#include <stdlib.h>
2
#include <stdio.h>
3
#include <fstream>
4
#include <cstring>
5
#include "stdtypes.h"
6
#include "elfreader.h"
7
 
8
void printHelp() {
9
    printf("This tool is a property of GNSS Sensor Limited.\n");
10
    printf("Any information maybe requested at chief@gnss-sensor.com\n\n");
11
    printf("Use the following arguments:\n");
12
    printf("    -r    generate raw image file (default)\n");
13
    printf("    -h    generate ROM array file in HEX format\n");
14
    printf("    -f    define fixed image size in Bytes, otherwise "
15
           "the size will be computed\n");
16
    printf("    -l    bytes per line (with -h only). Default 16 bytes/128 "
17
           "bits.\n");
18
    printf("    -b    Base address.\n");
19
    printf("    -o    output file name\n");
20
    printf("Example\n");
21
    printf("    elf2raw input_file_name -h -f 192168 -l 8 -o "
22
           "output_file_name\n");
23
}
24
 
25
int main(int argc, char* argv[]) {
26
    if (argc < 4) {
27
        printHelp();
28
        return 0;
29
    }
30
 
31
    enum EOutputFormat {Format_RAW_IMAGE, Format_ROMHEX};
32
    EOutputFormat outfmt = Format_RAW_IMAGE;
33
    uint32_t uiFixedSizeBytes = 0;
34
    uint32_t uiBytesPerLine = 16;
35
    uint64_t ullBaseAddress = ~0;
36
    int infile_index = 1;
37
    int outfile_index = 3;
38
    for (int i=1; i<argc; i++) {
39
        if (strcmp(argv[i], "-r") == 0) {         // generate raw image file
40
            outfmt = Format_RAW_IMAGE;
41
        } else if (strcmp(argv[i], "-h") == 0) {  // generate rom hex file
42
            outfmt = Format_ROMHEX;
43
        } else if (strcmp(argv[i], "-f") == 0) {  // fixed size in bytes
44
            uiFixedSizeBytes = (uint32_t)strtol(argv[++i], NULL, 0);
45
        } else if (strcmp(argv[i], "-l") == 0) {  // bytes per hex-line
46
            uiBytesPerLine = (uint32_t)strtol(argv[++i], NULL, 0);
47
        } else if (strcmp(argv[i], "-b") == 0) {  // bytes per hex-line
48
            ullBaseAddress = (uint64_t)strtoll(argv[++i], NULL, 0);
49
        } else if (strcmp(argv[i], "-o") == 0) {  // output file name
50
            outfile_index = ++i;
51
        } else {
52
            infile_index = i;
53
        }
54
    };
55
 
56
    std::string arg1(argv[infile_index]);
57
    std::string arg3(argv[outfile_index]);
58
    std::string in = std::string(arg1.begin(), arg1.end());
59
    std::string out = std::string(arg3.begin(), arg3.end());
60
 
61
    ElfReader elf(in.c_str());
62
    if (elf.isOpened() == 0) {
63
        printf("File %s not found\n", in.c_str());
64
        return 0;
65
    }
66
 
67
    switch (outfmt) {
68
    case Format_RAW_IMAGE:
69
        elf.writeRawImage(out.c_str(), uiFixedSizeBytes);
70
        break;
71
    case Format_ROMHEX:
72
        elf.writeRomHexArray(out.c_str(), ullBaseAddress,
73
                             uiBytesPerLine, uiFixedSizeBytes);
74
        break;
75
    default:
76
        printHelp();
77
    }
78
    return 0;
79
}
80
 

powered by: WebSVN 2.1.0

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