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