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