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 "base/loader/hex_file.hh"
32
33#include <cctype>
34#include <cstdio>
35#include <list>
36#include <string>
37
38#include "base/cprintf.hh"
39#include "base/loader/symtab.hh"
40#include "mem/port_proxy.hh"
41
42using namespace std;
43/*
44 * Load a Hex File into memory. Currently only used with MIPS
45 * BARE_IRON mode. A hex file consists of [Address Data] tuples that
46 * get directly loaded into physical memory. The address specified is
47 * a word address (i.e., to get the byte address, shift left by 2) The
48 * data is a full 32-bit hex value.
49*/
50HexFile::HexFile(const string _filename)
51    : filename(_filename)
52{
53    fp = fopen(filename.c_str(), "r");
54    if (fp == NULL)
55        panic("Unable to open %s\n", filename.c_str());
56}
57
58HexFile::~HexFile()
59{
60    if (fp != NULL)
61        fclose(fp);
62}
63
64bool
65HexFile::loadSections(PortProxy& memProxy)
66{
67    char Line[64];
68    Addr MemAddr;
69    uint32_t Data;
70    while (!feof(fp)) {
71        char *ret = fgets(Line, sizeof(Line), fp);
72        if (!ret)
73            panic("malformed file");
74        parseLine(Line, &MemAddr, &Data);
75        if (MemAddr != 0) {
76            // Now, write to memory
77            memProxy.writeBlob(MemAddr << 2, &Data, sizeof(Data));
78        }
79    }
80    return true;
81}
82
83void
84HexFile::parseLine(char *Str, Addr *A, uint32_t *D)
85{
86    int i = 0;
87    bool Flag = false;
88    *A = 0;
89    *D = 0;
90    int Digit = 0;
91    unsigned Number = 0;
92
93    /* Skip white spaces */
94    while (Str[i] != '\0' && Str[i]==' ')
95        i++;
96
97    /* Ok, we're at some character...process things */
98    while (Str[i] != '\0') {
99        if (Str[i] >= '0' && Str[i] <= '9') {
100            Digit = Str[i] - '0';
101        } else if (Str[i] >= 'a' && Str[i] <= 'f') {
102            Digit = Str[i] - 'a' + 10;
103        } else if (Str[i] >= 'A' && Str[i] <= 'F') {
104          Digit=Str[i]-'A'+10;
105        } else if (Str[i] == ' ' || Str[i] == '\n') {
106            if (Number == 0)
107                return;
108            if (!Flag) {
109                *A = Number;
110                Number = 0;
111                Flag = true;
112            } else {
113                *D = Number;
114                return;
115            }
116        } else {
117            // Ok, we've encountered a non-hex character, cannot be a
118            // valid line, skip and return 0's
119            *A = 0;
120            *D = 0;
121            return;
122        }
123
124        Number <<= 4;
125        Number += Digit;
126        i++;
127    }
128
129    if (!Flag) {
130        *A = 0;
131        *D = 0;
132    } else {
133        *D = Number;
134    }
135}
136
137void
138HexFile::close()
139{
140    fclose(fp);
141}
142