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