hex_file.cc revision 5545
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 315541Snate@binkert.org#include <cctype> 325541Snate@binkert.org#include <cstdio> 335222Sksewell@umich.edu#include <list> 345222Sksewell@umich.edu#include <string> 355222Sksewell@umich.edu 365222Sksewell@umich.edu#include "base/cprintf.hh" 375222Sksewell@umich.edu#include "base/loader/hex_file.hh" 385222Sksewell@umich.edu#include "base/loader/symtab.hh" 395222Sksewell@umich.edu#include "mem/translating_port.hh" 405222Sksewell@umich.edu 415222Sksewell@umich.eduusing namespace std; 425541Snate@binkert.org/* 435541Snate@binkert.org * Load a Hex File into memory. Currently only used with MIPS 445541Snate@binkert.org * BARE_IRON mode. A hex file consists of [Address Data] tuples that 455541Snate@binkert.org * get directly loaded into physical memory. The address specified is 465541Snate@binkert.org * a word address (i.e., to get the byte address, shift left by 2) The 475541Snate@binkert.org * data is a full 32-bit hex value. 485222Sksewell@umich.edu*/ 495222Sksewell@umich.eduHexFile::HexFile(const string _filename) 505222Sksewell@umich.edu : filename(_filename) 515222Sksewell@umich.edu{ 525541Snate@binkert.org fp = fopen(filename.c_str(), "r"); 535541Snate@binkert.org if (fp == NULL) 545541Snate@binkert.org panic("Unable to open %s\n", filename.c_str()); 555222Sksewell@umich.edu} 565222Sksewell@umich.edu 575222Sksewell@umich.eduHexFile::~HexFile() 585222Sksewell@umich.edu{ 595222Sksewell@umich.edu} 605222Sksewell@umich.edu 615222Sksewell@umich.edubool 625541Snate@binkert.orgHexFile::loadSections(Port *memPort) 635222Sksewell@umich.edu{ 645541Snate@binkert.org char Line[64]; 655541Snate@binkert.org Addr MemAddr; 665541Snate@binkert.org uint32_t Data; 675541Snate@binkert.org while (!feof(fp)) { 685545Snate@binkert.org char *ret = fgets(Line, sizeof(Line), fp); 695545Snate@binkert.org if (!ret) 705545Snate@binkert.org panic("malformed file"); 715541Snate@binkert.org parseLine(Line, &MemAddr, &Data); 725541Snate@binkert.org if (MemAddr != 0) { 735541Snate@binkert.org // Now, write to memory 745541Snate@binkert.org memPort->writeBlob(MemAddr << 2, (uint8_t *)&Data, sizeof(Data)); 755222Sksewell@umich.edu } 765222Sksewell@umich.edu } 775222Sksewell@umich.edu return true; 785222Sksewell@umich.edu} 795541Snate@binkert.org 805541Snate@binkert.orgvoid 815541Snate@binkert.orgHexFile::parseLine(char *Str, Addr *A, uint32_t *D) 825222Sksewell@umich.edu{ 835541Snate@binkert.org int i = 0; 845541Snate@binkert.org bool Flag = false; 855541Snate@binkert.org *A = 0; 865541Snate@binkert.org *D = 0; 875541Snate@binkert.org int Digit = 0; 885541Snate@binkert.org unsigned Number = 0; 895222Sksewell@umich.edu 905541Snate@binkert.org /* Skip white spaces */ 915541Snate@binkert.org while (Str[i] != '\0' && Str[i]==' ') 925541Snate@binkert.org i++; 935541Snate@binkert.org 945541Snate@binkert.org /* Ok, we're at some character...process things */ 955541Snate@binkert.org while (Str[i] != '\0') { 965541Snate@binkert.org if (Str[i] >= '0' && Str[i] <= '9') { 975541Snate@binkert.org Digit = Str[i] - '0'; 985541Snate@binkert.org } else if (Str[i] >= 'a' && Str[i] <= 'f') { 995541Snate@binkert.org Digit = Str[i] - 'a' + 10; 1005541Snate@binkert.org } else if (Str[i] >= 'A' && Str[i] <= 'F') { 1015541Snate@binkert.org Digit=Str[i]-'A'+10; 1025541Snate@binkert.org } else if (Str[i] == ' ' || Str[i] == '\n') { 1035541Snate@binkert.org if (Number == 0) 1045541Snate@binkert.org return; 1055541Snate@binkert.org if (Flag == false) { 1065541Snate@binkert.org *A = Number; 1075541Snate@binkert.org Number = 0; 1085541Snate@binkert.org Flag = true; 1095541Snate@binkert.org } else { 1105541Snate@binkert.org *D = Number; 1115541Snate@binkert.org return; 1125541Snate@binkert.org } 1135541Snate@binkert.org } else { 1145541Snate@binkert.org // Ok, we've encountered a non-hex character, cannot be a 1155541Snate@binkert.org // valid line, skip and return 0's 1165541Snate@binkert.org *A = 0; 1175541Snate@binkert.org *D = 0; 1185541Snate@binkert.org return; 1195222Sksewell@umich.edu } 1205222Sksewell@umich.edu 1215541Snate@binkert.org Number <<= 4; 1225541Snate@binkert.org Number += Digit; 1235541Snate@binkert.org i++; 1245222Sksewell@umich.edu } 1255541Snate@binkert.org 1265541Snate@binkert.org if (Flag != true) { 1275541Snate@binkert.org *A = 0; 1285541Snate@binkert.org *D = 0; 1295541Snate@binkert.org } else { 1305541Snate@binkert.org *D = Number; 1315222Sksewell@umich.edu } 1325222Sksewell@umich.edu} 1335222Sksewell@umich.edu 1345222Sksewell@umich.eduvoid 1355222Sksewell@umich.eduHexFile::close() 1365222Sksewell@umich.edu{ 1375541Snate@binkert.org fclose(fp); 1385222Sksewell@umich.edu} 139