hex_file.cc revision 10231
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/loader/hex_file.hh" 375222Sksewell@umich.edu#include "base/loader/symtab.hh" 388229Snate@binkert.org#include "base/cprintf.hh" 398706Sandreas.hansson@arm.com#include "mem/port_proxy.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{ 599086Sandreas.hansson@arm.com if (fp != NULL) 609086Sandreas.hansson@arm.com fclose(fp); 615222Sksewell@umich.edu} 625222Sksewell@umich.edu 635222Sksewell@umich.edubool 648852Sandreas.hansson@arm.comHexFile::loadSections(PortProxy& memProxy) 655222Sksewell@umich.edu{ 665541Snate@binkert.org char Line[64]; 675541Snate@binkert.org Addr MemAddr; 685541Snate@binkert.org uint32_t Data; 695541Snate@binkert.org while (!feof(fp)) { 705545Snate@binkert.org char *ret = fgets(Line, sizeof(Line), fp); 715545Snate@binkert.org if (!ret) 725545Snate@binkert.org panic("malformed file"); 735541Snate@binkert.org parseLine(Line, &MemAddr, &Data); 745541Snate@binkert.org if (MemAddr != 0) { 755541Snate@binkert.org // Now, write to memory 768852Sandreas.hansson@arm.com memProxy.writeBlob(MemAddr << 2, (uint8_t *)&Data, sizeof(Data)); 775222Sksewell@umich.edu } 785222Sksewell@umich.edu } 795222Sksewell@umich.edu return true; 805222Sksewell@umich.edu} 815541Snate@binkert.org 825541Snate@binkert.orgvoid 835541Snate@binkert.orgHexFile::parseLine(char *Str, Addr *A, uint32_t *D) 845222Sksewell@umich.edu{ 855541Snate@binkert.org int i = 0; 865541Snate@binkert.org bool Flag = false; 875541Snate@binkert.org *A = 0; 885541Snate@binkert.org *D = 0; 895541Snate@binkert.org int Digit = 0; 905541Snate@binkert.org unsigned Number = 0; 915222Sksewell@umich.edu 925541Snate@binkert.org /* Skip white spaces */ 935541Snate@binkert.org while (Str[i] != '\0' && Str[i]==' ') 945541Snate@binkert.org i++; 955541Snate@binkert.org 965541Snate@binkert.org /* Ok, we're at some character...process things */ 975541Snate@binkert.org while (Str[i] != '\0') { 985541Snate@binkert.org if (Str[i] >= '0' && Str[i] <= '9') { 995541Snate@binkert.org Digit = Str[i] - '0'; 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] >= 'A' && Str[i] <= 'F') { 1035541Snate@binkert.org Digit=Str[i]-'A'+10; 1045541Snate@binkert.org } else if (Str[i] == ' ' || Str[i] == '\n') { 1055541Snate@binkert.org if (Number == 0) 1065541Snate@binkert.org return; 10710231Ssteve.reinhardt@amd.com if (!Flag) { 1085541Snate@binkert.org *A = Number; 1095541Snate@binkert.org Number = 0; 1105541Snate@binkert.org Flag = true; 1115541Snate@binkert.org } else { 1125541Snate@binkert.org *D = Number; 1135541Snate@binkert.org return; 1145541Snate@binkert.org } 1155541Snate@binkert.org } else { 1165541Snate@binkert.org // Ok, we've encountered a non-hex character, cannot be a 1175541Snate@binkert.org // valid line, skip and return 0's 1185541Snate@binkert.org *A = 0; 1195541Snate@binkert.org *D = 0; 1205541Snate@binkert.org return; 1215222Sksewell@umich.edu } 1225222Sksewell@umich.edu 1235541Snate@binkert.org Number <<= 4; 1245541Snate@binkert.org Number += Digit; 1255541Snate@binkert.org i++; 1265222Sksewell@umich.edu } 1275541Snate@binkert.org 12810231Ssteve.reinhardt@amd.com if (!Flag) { 1295541Snate@binkert.org *A = 0; 1305541Snate@binkert.org *D = 0; 1315541Snate@binkert.org } else { 1325541Snate@binkert.org *D = Number; 1335222Sksewell@umich.edu } 1345222Sksewell@umich.edu} 1355222Sksewell@umich.edu 1365222Sksewell@umich.eduvoid 1375222Sksewell@umich.eduHexFile::close() 1385222Sksewell@umich.edu{ 1395541Snate@binkert.org fclose(fp); 1405222Sksewell@umich.edu} 141