gem5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
hex_file.cc
Go to the documentation of this file.
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 
42 using 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 */
50 HexFile::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 
59 {
60  if (fp != NULL)
61  fclose(fp);
62 }
63 
64 bool
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, (uint8_t *)&Data, sizeof(Data));
78  }
79  }
80  return true;
81 }
82 
83 void
84 HexFile::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 
137 void
139 {
140  fclose(fp);
141 }
HexFile(const std::string _filename)
Definition: hex_file.cc:50
Bitfield< 7 > i
Definition: miscregs.hh:1378
#define panic(...)
Definition: misc.hh:153
void close()
Definition: hex_file.cc:138
const std::string filename
Definition: hex_file.hh:45
void parseLine(char *, Addr *, uint32_t *)
Definition: hex_file.cc:84
PortProxy Object Declaration.
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:142
bool loadSections(PortProxy &memProxy)
Definition: hex_file.cc:65
This object is a proxy for a structural port, to be used for debug accesses.
Definition: port_proxy.hh:84
virtual void writeBlob(Addr addr, const uint8_t *p, int size) const
Write size bytes from p to address.
Definition: port_proxy.cc:58
FILE * fp
Definition: hex_file.hh:46
virtual ~HexFile()
Definition: hex_file.cc:58

Generated on Fri Jun 9 2017 13:03:41 for gem5 by doxygen 1.8.6