hex_file.cc revision 8852
114184Sgabeblack@google.com/*
214184Sgabeblack@google.com * Copyright (c) 2007 MIPS Technologies, Inc.
314184Sgabeblack@google.com * All rights reserved.
414184Sgabeblack@google.com *
514184Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
614184Sgabeblack@google.com * modification, are permitted provided that the following conditions are
714184Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
814184Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
914184Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
1014184Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1114184Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1214184Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1314184Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1414184Sgabeblack@google.com * this software without specific prior written permission.
1514184Sgabeblack@google.com *
1614184Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1714184Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1814184Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1914184Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2014184Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2114184Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2214184Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2314184Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2414184Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2514184Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2614184Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2714184Sgabeblack@google.com *
2814184Sgabeblack@google.com * Authors: Jaidev Patwardhan
2914184Sgabeblack@google.com */
3014184Sgabeblack@google.com
3114184Sgabeblack@google.com#include <cctype>
3214184Sgabeblack@google.com#include <cstdio>
3314184Sgabeblack@google.com#include <list>
3414184Sgabeblack@google.com#include <string>
3514184Sgabeblack@google.com
3614184Sgabeblack@google.com#include "base/loader/hex_file.hh"
3714184Sgabeblack@google.com#include "base/loader/symtab.hh"
3814184Sgabeblack@google.com#include "base/cprintf.hh"
3914184Sgabeblack@google.com#include "mem/port_proxy.hh"
4014184Sgabeblack@google.com
4114184Sgabeblack@google.comusing namespace std;
4214184Sgabeblack@google.com/*
4314184Sgabeblack@google.com * Load a Hex File into memory. Currently only used with MIPS
4414184Sgabeblack@google.com * BARE_IRON mode. A hex file consists of [Address Data] tuples that
4514184Sgabeblack@google.com * get directly loaded into physical memory. The address specified is
4614184Sgabeblack@google.com * a word address (i.e., to get the byte address, shift left by 2) The
4714184Sgabeblack@google.com * data is a full 32-bit hex value.
4814184Sgabeblack@google.com*/
4914184Sgabeblack@google.comHexFile::HexFile(const string _filename)
5014184Sgabeblack@google.com    : filename(_filename)
5114184Sgabeblack@google.com{
5214184Sgabeblack@google.com    fp = fopen(filename.c_str(), "r");
5314184Sgabeblack@google.com    if (fp == NULL)
5414184Sgabeblack@google.com        panic("Unable to open %s\n", filename.c_str());
5514184Sgabeblack@google.com}
5614184Sgabeblack@google.com
5714184Sgabeblack@google.comHexFile::~HexFile()
5814184Sgabeblack@google.com{
5914184Sgabeblack@google.com}
6014184Sgabeblack@google.com
6114184Sgabeblack@google.combool
6214184Sgabeblack@google.comHexFile::loadSections(PortProxy& memProxy)
6314184Sgabeblack@google.com{
6414184Sgabeblack@google.com    char Line[64];
6514184Sgabeblack@google.com    Addr MemAddr;
6614184Sgabeblack@google.com    uint32_t Data;
6714184Sgabeblack@google.com    while (!feof(fp)) {
6814184Sgabeblack@google.com        char *ret = fgets(Line, sizeof(Line), fp);
6914184Sgabeblack@google.com        if (!ret)
7014184Sgabeblack@google.com            panic("malformed file");
7114184Sgabeblack@google.com        parseLine(Line, &MemAddr, &Data);
7214184Sgabeblack@google.com        if (MemAddr != 0) {
7314184Sgabeblack@google.com            // Now, write to memory
7414184Sgabeblack@google.com            memProxy.writeBlob(MemAddr << 2, (uint8_t *)&Data, sizeof(Data));
7514184Sgabeblack@google.com        }
7614184Sgabeblack@google.com    }
7714184Sgabeblack@google.com    return true;
7814184Sgabeblack@google.com}
7914184Sgabeblack@google.com
8014184Sgabeblack@google.comvoid
8114184Sgabeblack@google.comHexFile::parseLine(char *Str, Addr *A, uint32_t *D)
8214184Sgabeblack@google.com{
8314184Sgabeblack@google.com    int i = 0;
8414184Sgabeblack@google.com    bool Flag = false;
8514184Sgabeblack@google.com    *A = 0;
8614184Sgabeblack@google.com    *D = 0;
8714184Sgabeblack@google.com    int Digit = 0;
8814184Sgabeblack@google.com    unsigned Number = 0;
8914184Sgabeblack@google.com
9014184Sgabeblack@google.com    /* Skip white spaces */
9114184Sgabeblack@google.com    while (Str[i] != '\0' && Str[i]==' ')
9214184Sgabeblack@google.com        i++;
9314184Sgabeblack@google.com
9414184Sgabeblack@google.com    /* Ok, we're at some character...process things */
9514184Sgabeblack@google.com    while (Str[i] != '\0') {
9614184Sgabeblack@google.com        if (Str[i] >= '0' && Str[i] <= '9') {
9714184Sgabeblack@google.com            Digit = Str[i] - '0';
9814184Sgabeblack@google.com        } else if (Str[i] >= 'a' && Str[i] <= 'f') {
9914184Sgabeblack@google.com            Digit = Str[i] - 'a' + 10;
10014184Sgabeblack@google.com        } else if (Str[i] >= 'A' && Str[i] <= 'F') {
10114184Sgabeblack@google.com          Digit=Str[i]-'A'+10;
10214184Sgabeblack@google.com        } else if (Str[i] == ' ' || Str[i] == '\n') {
10314184Sgabeblack@google.com            if (Number == 0)
10414184Sgabeblack@google.com                return;
10514184Sgabeblack@google.com            if (Flag == false) {
10614184Sgabeblack@google.com                *A = Number;
10714184Sgabeblack@google.com                Number = 0;
10814184Sgabeblack@google.com                Flag = true;
10914184Sgabeblack@google.com            } else {
11014184Sgabeblack@google.com                *D = Number;
11114184Sgabeblack@google.com                return;
11214184Sgabeblack@google.com            }
11314184Sgabeblack@google.com        } else {
11414184Sgabeblack@google.com            // Ok, we've encountered a non-hex character, cannot be a
11514184Sgabeblack@google.com            // valid line, skip and return 0's
11614184Sgabeblack@google.com            *A = 0;
11714184Sgabeblack@google.com            *D = 0;
11814184Sgabeblack@google.com            return;
11914184Sgabeblack@google.com        }
12014184Sgabeblack@google.com
12114184Sgabeblack@google.com        Number <<= 4;
12214184Sgabeblack@google.com        Number += Digit;
12314184Sgabeblack@google.com        i++;
12414184Sgabeblack@google.com    }
12514184Sgabeblack@google.com
12614184Sgabeblack@google.com    if (Flag != true) {
12714184Sgabeblack@google.com        *A = 0;
12814184Sgabeblack@google.com        *D = 0;
12914184Sgabeblack@google.com    } else {
13014184Sgabeblack@google.com        *D = Number;
13114184Sgabeblack@google.com    }
13214184Sgabeblack@google.com}
13314184Sgabeblack@google.com
13414184Sgabeblack@google.comvoid
13514184Sgabeblack@google.comHexFile::close()
13614184Sgabeblack@google.com{
13714184Sgabeblack@google.com    fclose(fp);
13814184Sgabeblack@google.com}
13914184Sgabeblack@google.com