hex_file.cc revision 5541:bb31ea8583d8
1/*
2 * Copyright (c) 2007 MIPS Technologies, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Jaidev Patwardhan
29 */
30
31#include <cctype>
32#include <cstdio>
33#include <list>
34#include <string>
35
36#include "base/cprintf.hh"
37#include "base/loader/hex_file.hh"
38#include "base/loader/symtab.hh"
39#include "mem/translating_port.hh"
40
41using namespace std;
42/*
43 * Load a Hex File into memory. Currently only used with MIPS
44 * BARE_IRON mode. A hex file consists of [Address Data] tuples that
45 * get directly loaded into physical memory. The address specified is
46 * a word address (i.e., to get the byte address, shift left by 2) The
47 * data is a full 32-bit hex value.
48*/
49HexFile::HexFile(const string _filename)
50    : filename(_filename)
51{
52    fp = fopen(filename.c_str(), "r");
53    if (fp == NULL)
54        panic("Unable to open %s\n", filename.c_str());
55}
56
57HexFile::~HexFile()
58{
59}
60
61bool
62HexFile::loadSections(Port *memPort)
63{
64    char Line[64];
65    Addr MemAddr;
66    uint32_t Data;
67    while (!feof(fp)) {
68        fgets(Line, 64, fp);
69        parseLine(Line, &MemAddr, &Data);
70        if (MemAddr != 0) {
71            // Now, write to memory
72            memPort->writeBlob(MemAddr << 2, (uint8_t *)&Data, sizeof(Data));
73        }
74    }
75    return true;
76}
77
78void
79HexFile::parseLine(char *Str, Addr *A, uint32_t *D)
80{
81    int i = 0;
82    bool Flag = false;
83    *A = 0;
84    *D = 0;
85    int Digit = 0;
86    unsigned Number = 0;
87
88    /* Skip white spaces */
89    while (Str[i] != '\0' && Str[i]==' ')
90        i++;
91
92    /* Ok, we're at some character...process things */
93    while (Str[i] != '\0') {
94        if (Str[i] >= '0' && Str[i] <= '9') {
95            Digit = Str[i] - '0';
96        } else if (Str[i] >= 'a' && Str[i] <= 'f') {
97            Digit = Str[i] - 'a' + 10;
98        } else if (Str[i] >= 'A' && Str[i] <= 'F') {
99          Digit=Str[i]-'A'+10;
100        } else if (Str[i] == ' ' || Str[i] == '\n') {
101            if (Number == 0)
102                return;
103            if (Flag == false) {
104                *A = Number;
105                Number = 0;
106                Flag = true;
107            } else {
108                *D = Number;
109                return;
110            }
111        } else {
112            // Ok, we've encountered a non-hex character, cannot be a
113            // valid line, skip and return 0's
114            *A = 0;
115            *D = 0;
116            return;
117        }
118
119        Number <<= 4;
120        Number += Digit;
121        i++;
122    }
123
124    if (Flag != true) {
125        *A = 0;
126        *D = 0;
127    } else {
128        *D = Number;
129    }
130}
131
132void
133HexFile::close()
134{
135    fclose(fp);
136}
137