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

Subversion Repositories mips32r1

[/] [mips32r1/] [trunk/] [Software/] [demos/] [util/] [bintoxum.c] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ayersg
/*
2
 * File          : bintoxum.c
3
 * Project       : University of Utah, XUM Project
4
 * Creator(s)    : Grant Ayers (ayers@cs.utah.edu)
5
 *
6
 * Modification History:
7
 *   Rev   Date         Initials  Description of Change
8
 *   1.0   7-3-2012     GEA       Initial Design.
9
 *
10
 * Standards/Formatting:
11
 *   C, 8 hard tab, 80 column
12
 *
13
 * Description:
14
 *    Combines the text (instruction) and data sections of an
15
 *    executable into one file. You can think of this as a very
16
 *    simple version of ELF or a.out files.
17
 *
18
 *    The XUM processor has a simple flat physical address space
19
 *    which contains instructions and data. The output file from
20
 *    this utility is directly loadable into this memory, byte-for-byte,
21
 *    without using any kind of "intelligent" loader. It takes two
22
 *    binary input files, the instructions and data, and an offset
23
 *    address (decimal) for the data segment to begin, and outputs
24
 *    a file which can be sent directly to hardware via the XUM
25
 *    bootloader or other means.
26
 */
27
#include <stdio.h>
28
#include <stdlib.h>
29
#include <string.h>
30
#include <unistd.h>
31
 
32
void usage(void);
33
void read_file(char *name, int *size, char **buf);
34
 
35
int main(int argc, char **argv)
36
{
37
        FILE *file;
38
        char *it_name, *id_name, *o_name;
39
        int it_size, id_size;
40
        char *it_buf, *id_buf;
41
        int data_start_addr;
42
        int pad;
43
        int ch;
44
 
45
        while ((ch = getopt(argc, argv, "d:")) != -1) {
46
                switch (ch) {
47
                        case 'd':
48
                                data_start_addr = (int)strtol(optarg,
49
                                        (char **)NULL, 10);
50
                                break;
51
                        default:
52
                                usage();
53
                }
54
        }
55
 
56
        argc -= optind;
57
        argv += optind;
58
 
59
        if (argc != 3) {
60
                usage();
61
        }
62
 
63
        it_name = argv[0];
64
        id_name = argv[1];
65
        o_name  = argv[2];
66
 
67
        read_file(it_name, &it_size, &it_buf);
68
        read_file(id_name, &id_size, &id_buf);
69
 
70
        /* Open the output file */
71
        file = fopen(o_name, "wb+");
72
        if (file == NULL) {
73
                fprintf(stderr, "Error: Could not open \"%s\" for "
74
                        "writing.\n", o_name);
75
                exit(1);
76
        }
77
 
78
        /* Copy text segment directly to output file */
79
        if (fwrite((void *)it_buf, 1, it_size, file) != it_size) {
80
                fprintf(stderr, "Error writing to output file.\n");
81
                exit(1);
82
        }
83
 
84
        /* Pad until the data segment */
85
        it_buf[0] = 0;
86
        while (it_size < data_start_addr) {
87
                if (fwrite((void *)it_buf, 1, 1, file) != 1) {
88
                        fprintf(stderr, "Error writing to output file.\n");
89
                        exit(1);
90
                }
91
                it_size++;
92
        }
93
 
94
        /* Copy data segment to output file */
95
        if (fwrite((void *)id_buf, 1, id_size, file) != id_size) {
96
                fprintf(stderr, "Error writing to output file.\n");
97
                exit(1);
98
        }
99
 
100
        /* Pad the data section to word length if needed */
101
        /* NOTE: Assumes only padding needed would be at the end. */
102
        pad = ((id_size % 4) != 0) ? 4 - (id_size % 4) : 0;
103
        if (pad != 0) {
104
                memset((void *)id_buf, 0, 4);
105
                if (fwrite((void *)id_buf, 1, pad, file) != pad) {
106
                        fprintf(stderr, "Error writing to output file.\n");
107
                        exit(1);
108
                }
109
        }
110
 
111
        fclose(file);
112
 
113
        return 0;
114
}
115
 
116
void usage(void)
117
{
118
        fprintf(stderr, "Usage: bintoxum [-d data start address] "
119
                "<text file> <data file> <output file>\n");
120
        exit(1);
121
}
122
 
123
void read_file(char *name, int *size, char **buf)
124
{
125
        FILE *file;
126
 
127
        file = fopen(name, "rb");
128
        if (file == NULL) {
129
                fprintf(stderr, "Error: Could not open \"%s\".\n", name);
130
                exit(1);
131
        }
132
        fseek(file, 0L, SEEK_END);
133
        *size = (int)ftell(file);
134
        if ((*size < 0) || (ftell(file) > (long)*size)) {
135
                fprintf(stderr, "Error: Input file is too large.\n");
136
                exit(1);
137
        }
138
        fseek(file, 0L, SEEK_SET);
139
        *buf = (char *)malloc(*size);
140
        if (*buf == NULL) {
141
                fprintf(stderr, "Error: Could not allocate %d bytes "
142
                        "of memory.\n", *size);
143
                exit(1);
144
        }
145
        if (fread(*buf, 1, *size, file) != *size) {
146
                fprintf(stderr, "Error reading input file.\n");
147
                exit(1);
148
        }
149
        fclose(file);
150
}

powered by: WebSVN 2.1.0

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