hex_file.cc revision 5545:d7c7b6752e2c
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        char *ret = fgets(Line, sizeof(Line), fp);
69        if (!ret)
70            panic("malformed file");
71        parseLine(Line, &MemAddr, &Data);
72        if (MemAddr != 0) {
73            // Now, write to memory
74            memPort->writeBlob(MemAddr << 2, (uint8_t *)&Data, sizeof(Data));
75        }
76    }
77    return true;
78}
79
80void
81HexFile::parseLine(char *Str, Addr *A, uint32_t *D)
82{
83    int i = 0;
84    bool Flag = false;
85    *A = 0;
86    *D = 0;
87    int Digit = 0;
88    unsigned Number = 0;
89
90    /* Skip white spaces */
91    while (Str[i] != '\0' && Str[i]==' ')
92        i++;
93
94    /* Ok, we're at some character...process things */
95    while (Str[i] != '\0') {
96        if (Str[i] >= '0' && Str[i] <= '9') {
97            Digit = Str[i] - '0';
98        } else if (Str[i] >= 'a' && Str[i] <= 'f') {
99            Digit = Str[i] - 'a' + 10;
100        } else if (Str[i] >= 'A' && Str[i] <= 'F') {
101          Digit=Str[i]-'A'+10;
102        } else if (Str[i] == ' ' || Str[i] == '\n') {
103            if (Number == 0)
104                return;
105            if (Flag == false) {
106                *A = Number;
107                Number = 0;
108                Flag = true;
109            } else {
110                *D = Number;
111                return;
112            }
113        } else {
114            // Ok, we've encountered a non-hex character, cannot be a
115            // valid line, skip and return 0's
116            *A = 0;
117            *D = 0;
118            return;
119        }
120
121        Number <<= 4;
122        Number += Digit;
123        i++;
124    }
125
126    if (Flag != true) {
127        *A = 0;
128        *D = 0;
129    } else {
130        *D = Number;
131    }
132}
133
134void
135HexFile::close()
136{
137    fclose(fp);
138}
139