hex_file.cc revision 14010
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 3111793Sbrandon.potter@amd.com#include "base/loader/hex_file.hh" 3211793Sbrandon.potter@amd.com 335541Snate@binkert.org#include <cctype> 345541Snate@binkert.org#include <cstdio> 355222Sksewell@umich.edu#include <list> 365222Sksewell@umich.edu#include <string> 375222Sksewell@umich.edu 3811793Sbrandon.potter@amd.com#include "base/cprintf.hh" 395222Sksewell@umich.edu#include "base/loader/symtab.hh" 408706Sandreas.hansson@arm.com#include "mem/port_proxy.hh" 415222Sksewell@umich.edu 425222Sksewell@umich.eduusing namespace std; 435541Snate@binkert.org/* 445541Snate@binkert.org * Load a Hex File into memory. Currently only used with MIPS 455541Snate@binkert.org * BARE_IRON mode. A hex file consists of [Address Data] tuples that 465541Snate@binkert.org * get directly loaded into physical memory. The address specified is 475541Snate@binkert.org * a word address (i.e., to get the byte address, shift left by 2) The 485541Snate@binkert.org * data is a full 32-bit hex value. 495222Sksewell@umich.edu*/ 505222Sksewell@umich.eduHexFile::HexFile(const string _filename) 515222Sksewell@umich.edu : filename(_filename) 525222Sksewell@umich.edu{ 535541Snate@binkert.org fp = fopen(filename.c_str(), "r"); 545541Snate@binkert.org if (fp == NULL) 555541Snate@binkert.org panic("Unable to open %s\n", filename.c_str()); 565222Sksewell@umich.edu} 575222Sksewell@umich.edu 585222Sksewell@umich.eduHexFile::~HexFile() 595222Sksewell@umich.edu{ 609086Sandreas.hansson@arm.com if (fp != NULL) 619086Sandreas.hansson@arm.com fclose(fp); 625222Sksewell@umich.edu} 635222Sksewell@umich.edu 645222Sksewell@umich.edubool 658852Sandreas.hansson@arm.comHexFile::loadSections(PortProxy& memProxy) 665222Sksewell@umich.edu{ 675541Snate@binkert.org char Line[64]; 685541Snate@binkert.org Addr MemAddr; 695541Snate@binkert.org uint32_t Data; 705541Snate@binkert.org while (!feof(fp)) { 715545Snate@binkert.org char *ret = fgets(Line, sizeof(Line), fp); 725545Snate@binkert.org if (!ret) 735545Snate@binkert.org panic("malformed file"); 745541Snate@binkert.org parseLine(Line, &MemAddr, &Data); 755541Snate@binkert.org if (MemAddr != 0) { 765541Snate@binkert.org // Now, write to memory 7714010Sgabeblack@google.com memProxy.writeBlob(MemAddr << 2, &Data, sizeof(Data)); 785222Sksewell@umich.edu } 795222Sksewell@umich.edu } 805222Sksewell@umich.edu return true; 815222Sksewell@umich.edu} 825541Snate@binkert.org 835541Snate@binkert.orgvoid 845541Snate@binkert.orgHexFile::parseLine(char *Str, Addr *A, uint32_t *D) 855222Sksewell@umich.edu{ 865541Snate@binkert.org int i = 0; 875541Snate@binkert.org bool Flag = false; 885541Snate@binkert.org *A = 0; 895541Snate@binkert.org *D = 0; 905541Snate@binkert.org int Digit = 0; 915541Snate@binkert.org unsigned Number = 0; 925222Sksewell@umich.edu 935541Snate@binkert.org /* Skip white spaces */ 945541Snate@binkert.org while (Str[i] != '\0' && Str[i]==' ') 955541Snate@binkert.org i++; 965541Snate@binkert.org 975541Snate@binkert.org /* Ok, we're at some character...process things */ 985541Snate@binkert.org while (Str[i] != '\0') { 995541Snate@binkert.org if (Str[i] >= '0' && Str[i] <= '9') { 1005541Snate@binkert.org Digit = Str[i] - '0'; 1015541Snate@binkert.org } else if (Str[i] >= 'a' && Str[i] <= 'f') { 1025541Snate@binkert.org Digit = Str[i] - 'a' + 10; 1035541Snate@binkert.org } else if (Str[i] >= 'A' && Str[i] <= 'F') { 1045541Snate@binkert.org Digit=Str[i]-'A'+10; 1055541Snate@binkert.org } else if (Str[i] == ' ' || Str[i] == '\n') { 1065541Snate@binkert.org if (Number == 0) 1075541Snate@binkert.org return; 10810231Ssteve.reinhardt@amd.com if (!Flag) { 1095541Snate@binkert.org *A = Number; 1105541Snate@binkert.org Number = 0; 1115541Snate@binkert.org Flag = true; 1125541Snate@binkert.org } else { 1135541Snate@binkert.org *D = Number; 1145541Snate@binkert.org return; 1155541Snate@binkert.org } 1165541Snate@binkert.org } else { 1175541Snate@binkert.org // Ok, we've encountered a non-hex character, cannot be a 1185541Snate@binkert.org // valid line, skip and return 0's 1195541Snate@binkert.org *A = 0; 1205541Snate@binkert.org *D = 0; 1215541Snate@binkert.org return; 1225222Sksewell@umich.edu } 1235222Sksewell@umich.edu 1245541Snate@binkert.org Number <<= 4; 1255541Snate@binkert.org Number += Digit; 1265541Snate@binkert.org i++; 1275222Sksewell@umich.edu } 1285541Snate@binkert.org 12910231Ssteve.reinhardt@amd.com if (!Flag) { 1305541Snate@binkert.org *A = 0; 1315541Snate@binkert.org *D = 0; 1325541Snate@binkert.org } else { 1335541Snate@binkert.org *D = Number; 1345222Sksewell@umich.edu } 1355222Sksewell@umich.edu} 1365222Sksewell@umich.edu 1375222Sksewell@umich.eduvoid 1385222Sksewell@umich.eduHexFile::close() 1395222Sksewell@umich.edu{ 1405541Snate@binkert.org fclose(fp); 1415222Sksewell@umich.edu} 142