hex_file.cc revision 5222
15222Sksewell@umich.edu/* 25222Sksewell@umich.edu * Copyright (c) 2007 MIPS Technologies, Inc. 35222Sksewell@umich.edu * All rights reserved. 45222Sksewell@umich.edu * 55222Sksewell@umich.edu * Redistribution and use in source and binary forms, with or without 65222Sksewell@umich.edu * modification, are permitted provided that the following conditions are 75222Sksewell@umich.edu * met: redistributions of source code must retain the above copyright 85222Sksewell@umich.edu * notice, this list of conditions and the following disclaimer; 95222Sksewell@umich.edu * redistributions in binary form must reproduce the above copyright 105222Sksewell@umich.edu * notice, this list of conditions and the following disclaimer in the 115222Sksewell@umich.edu * documentation and/or other materials provided with the distribution; 125222Sksewell@umich.edu * neither the name of the copyright holders nor the names of its 135222Sksewell@umich.edu * contributors may be used to endorse or promote products derived from 145222Sksewell@umich.edu * this software without specific prior written permission. 155222Sksewell@umich.edu * 165222Sksewell@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 175222Sksewell@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 185222Sksewell@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 195222Sksewell@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 205222Sksewell@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 215222Sksewell@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 225222Sksewell@umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 235222Sksewell@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 245222Sksewell@umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 255222Sksewell@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 265222Sksewell@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 275222Sksewell@umich.edu * 285222Sksewell@umich.edu * Authors: Jaidev Patwardhan 295222Sksewell@umich.edu */ 305222Sksewell@umich.edu 315222Sksewell@umich.edu#include <list> 325222Sksewell@umich.edu#include <string> 335222Sksewell@umich.edu 345222Sksewell@umich.edu#include <sys/types.h> 355222Sksewell@umich.edu#include <sys/mman.h> 365222Sksewell@umich.edu#include <fcntl.h> 375222Sksewell@umich.edu#include <stdio.h> 385222Sksewell@umich.edu#include <unistd.h> 395222Sksewell@umich.edu 405222Sksewell@umich.edu#include "base/cprintf.hh" 415222Sksewell@umich.edu#include "base/loader/hex_file.hh" 425222Sksewell@umich.edu#include "base/loader/symtab.hh" 435222Sksewell@umich.edu 445222Sksewell@umich.edu 455222Sksewell@umich.edu#include "mem/translating_port.hh" 465222Sksewell@umich.edu 475222Sksewell@umich.eduusing namespace std; 485222Sksewell@umich.edu/* Load a Hex File into memory. 495222Sksewell@umich.edu Currently only used with MIPS BARE_IRON mode. 505222Sksewell@umich.edu A hex file consists of [Address Data] tuples that get directly loaded into 515222Sksewell@umich.edu physical memory. The address specified is a word address (i.e., to get the byte address, shift left by 2) 525222Sksewell@umich.edu The data is a full 32-bit hex value. 535222Sksewell@umich.edu*/ 545222Sksewell@umich.eduHexFile::HexFile(const string _filename) 555222Sksewell@umich.edu : filename(_filename) 565222Sksewell@umich.edu{ 575222Sksewell@umich.edu fp = fopen(filename.c_str(),"r"); 585222Sksewell@umich.edu if(fp == NULL) 595222Sksewell@umich.edu { 605222Sksewell@umich.edu panic("Unable to open %s\n",filename.c_str()); 615222Sksewell@umich.edu } 625222Sksewell@umich.edu 635222Sksewell@umich.edu} 645222Sksewell@umich.edu 655222Sksewell@umich.eduHexFile::~HexFile() 665222Sksewell@umich.edu{ 675222Sksewell@umich.edu} 685222Sksewell@umich.edu 695222Sksewell@umich.edu 705222Sksewell@umich.edubool 715222Sksewell@umich.eduHexFile::loadSections(Port *memPort, Addr addrMask) 725222Sksewell@umich.edu{ 735222Sksewell@umich.edu char Line[64]; 745222Sksewell@umich.edu Addr MemAddr; 755222Sksewell@umich.edu uint32_t Data; 765222Sksewell@umich.edu while(!feof(fp)) 775222Sksewell@umich.edu { 785222Sksewell@umich.edu fgets(Line,64,fp); 795222Sksewell@umich.edu parseLine(Line,&MemAddr,&Data); 805222Sksewell@umich.edu // printf("Hex:%u\n",Data); 815222Sksewell@umich.edu 825222Sksewell@umich.edu if(MemAddr != 0) 835222Sksewell@umich.edu { 845222Sksewell@umich.edu // Now, write to memory 855222Sksewell@umich.edu memPort->writeBlob(MemAddr<<2,(uint8_t *)&Data,sizeof(Data)); 865222Sksewell@umich.edu } 875222Sksewell@umich.edu } 885222Sksewell@umich.edu return true; 895222Sksewell@umich.edu} 905222Sksewell@umich.eduvoid HexFile::parseLine(char *Str,Addr *A, uint32_t *D) 915222Sksewell@umich.edu{ 925222Sksewell@umich.edu int i=0; 935222Sksewell@umich.edu bool Flag = false; 945222Sksewell@umich.edu *A = 0; 955222Sksewell@umich.edu *D = 0; 965222Sksewell@umich.edu int Digit = 0; 975222Sksewell@umich.edu unsigned Number = 0; 985222Sksewell@umich.edu /* Skip white spaces */ 995222Sksewell@umich.edu while(Str[i] != '\0' && Str[i]==' ') 1005222Sksewell@umich.edu i++; 1015222Sksewell@umich.edu 1025222Sksewell@umich.edu /* Ok, we're at some character...process things */ 1035222Sksewell@umich.edu while(Str[i] != '\0') 1045222Sksewell@umich.edu { 1055222Sksewell@umich.edu if(Str[i]>='0' && Str[i]<='9') 1065222Sksewell@umich.edu { 1075222Sksewell@umich.edu Digit=Str[i]-'0'; 1085222Sksewell@umich.edu } 1095222Sksewell@umich.edu else if(Str[i]>='a' && Str[i]<='f') 1105222Sksewell@umich.edu { 1115222Sksewell@umich.edu Digit=Str[i]-'a'+10; 1125222Sksewell@umich.edu } 1135222Sksewell@umich.edu else if(Str[i]>='A' && Str[i]<='F') 1145222Sksewell@umich.edu { 1155222Sksewell@umich.edu Digit=Str[i]-'A'+10; 1165222Sksewell@umich.edu } 1175222Sksewell@umich.edu else if(Str[i] == ' ' || Str[i]=='\n') 1185222Sksewell@umich.edu { 1195222Sksewell@umich.edu if(Number == 0) 1205222Sksewell@umich.edu return; 1215222Sksewell@umich.edu if(Flag == false) 1225222Sksewell@umich.edu { 1235222Sksewell@umich.edu *A = Number; 1245222Sksewell@umich.edu Number = 0; 1255222Sksewell@umich.edu Flag = true; 1265222Sksewell@umich.edu } 1275222Sksewell@umich.edu else 1285222Sksewell@umich.edu { 1295222Sksewell@umich.edu *D = Number; 1305222Sksewell@umich.edu return; 1315222Sksewell@umich.edu } 1325222Sksewell@umich.edu } 1335222Sksewell@umich.edu else 1345222Sksewell@umich.edu { 1355222Sksewell@umich.edu // Ok, we've encountered a non-hex character, cannot be a valid line, skip and return 0's 1365222Sksewell@umich.edu *A = 0; 1375222Sksewell@umich.edu *D = 0; 1385222Sksewell@umich.edu return; 1395222Sksewell@umich.edu } 1405222Sksewell@umich.edu Number<<=4; 1415222Sksewell@umich.edu Number+=Digit; 1425222Sksewell@umich.edu i++; 1435222Sksewell@umich.edu 1445222Sksewell@umich.edu } 1455222Sksewell@umich.edu if(Flag != true) 1465222Sksewell@umich.edu { 1475222Sksewell@umich.edu *A = 0; 1485222Sksewell@umich.edu *D = 0; 1495222Sksewell@umich.edu } 1505222Sksewell@umich.edu else 1515222Sksewell@umich.edu *D = Number; 1525222Sksewell@umich.edu 1535222Sksewell@umich.edu} 1545222Sksewell@umich.edu 1555222Sksewell@umich.edu 1565222Sksewell@umich.edu 1575222Sksewell@umich.eduvoid 1585222Sksewell@umich.eduHexFile::close() 1595222Sksewell@umich.edu{ 1605222Sksewell@umich.edu fclose(fp); 1615222Sksewell@umich.edu} 162