?? rawbin.c
字號(hào):
/* * File: rawbin.c * * An implementation of an binary parser which exposes the rawbin.h * interface. Specifically parses RR binary format. Please see rawbin.h for * more info. * * SEE ALSO: * rawbin.h * * Copyright (C) 2002 RidgeRun, Inc. * Author: RidgeRun, Inc <skranz@ridgerun.com> * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2 of the License, or (at your * option) any later version. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 675 Mass Ave, Cambridge, MA 02139, USA. * * Please report all bugs/problems to the author or <support@dsplinux.net> * * key: RRGPLCR (do not remove) * */#include "rawbin.h"#include "util.h"static GetByteCallBack_t GetByteCBack;static PutValAtAddrCallBack_t Put_CBack;/****************************** Routine: Description: ******************************/static unsigned char ReadByte(void){ unsigned char byte = 0; if (GetByteCBack) byte = (*GetByteCBack)(); return byte;}/****************************** Routine: Description: ******************************/static unsigned char GetByteValFromCharPair(void){ unsigned char byte; byte = util_ascii_to_bin(ReadByte()) << 4; byte |= util_ascii_to_bin(ReadByte()); return byte;}/****************************** Routine: Description: ******************************/static unsigned int GetIntValFromCharQuadPair(void){ unsigned int Val = 0; Val = Val | (GetByteValFromCharPair() << 24); Val = Val | (GetByteValFromCharPair() << 16); Val = Val | (GetByteValFromCharPair() << 8); Val = Val | (GetByteValFromCharPair() << 0); return Val;}/****************************** Routine: Description: Note: See rawbin.h for description. ******************************/void rawbin_RegisterGetByte(GetByteCallBack_t CBack){ GetByteCBack = CBack;}/****************************** Routine: Description: Note: See rawbin.h for description. ******************************/void rawbin_RegisterPut(PutValAtAddrCallBack_t CBack){ Put_CBack = CBack;}/****************************** Routine: Description: Note1: See rawbin.h for description. Note2: The RR binary image contains an ascii header that will proceed the binary portion of the file. This header was added to the image when it was constructed by some utility (such as `mkimage`). For referenced, here is an example header found on an RR format Binary: >LoadAddr :0x080480f4 >EntryAddr:0x08048450 >NumBytes :0x00000A50 ******************************/ParseStatus_t rawbin_parse(unsigned int *load_addr, unsigned int *entry_addr, unsigned int *num_bytes, unsigned int *use_header){ int i, NumBytes, EntryAddr, LoadAddr, bytes_left, address; unsigned char val8bits; unsigned short val16bits; if ((!Put_CBack) || (!GetByteCBack)) { return LOAD_MISSING_CBACK; } NumBytes = EntryAddr = LoadAddr = 0; // Next, get "LoadAddr" value. // ------------- while('>' != ReadByte()) continue; if ('L' != ReadByte()) return LOAD_INVALID_FORMAT; for (i=0; i<8; i++) ReadByte(); if (':' != ReadByte()) return LOAD_INVALID_FORMAT; GetByteValFromCharPair(); // discard the "0x". LoadAddr = address = GetIntValFromCharQuadPair(); // Next, get "EntryAddr" value or "NoHeader" flag // ------------- while('>' != ReadByte()) continue; val8bits=ReadByte(); *use_header=('N' != val8bits); //double negative, nasty if (! *use_header) { /* "NoHeader tag, read to end of line */ while ( '\n' != ReadByte()) { } } else { if ('E' == val8bits) { for (i=0; i<8; i++) ReadByte(); if (':' != ReadByte()) return LOAD_INVALID_FORMAT; GetByteValFromCharPair(); // discard the "0x". EntryAddr = GetIntValFromCharQuadPair(); } else { return LOAD_INVALID_FORMAT; } } // Next, get "NumBytes" value. // ------------- while('>' != ReadByte()) continue; if ('N' != ReadByte()) return LOAD_INVALID_FORMAT; for (i=0; i<8; i++) ReadByte(); if (':' != ReadByte()) return LOAD_INVALID_FORMAT; GetByteValFromCharPair(); // discard the "0x". NumBytes = bytes_left = GetIntValFromCharQuadPair(); ReadByte(); // discard final '\n' which ends header. // Next, process the binary image. // ------------- val16bits=0; for (i=0; i<NumBytes; i++) { val8bits = (*GetByteCBack)(); if (!(i & 0x1)) { val16bits = val16bits | val8bits; // first byte. } else { val16bits = val16bits | (val8bits<<8); // second byte. // got all 16 bits now, write it out. (*Put_CBack)((unsigned short *)address,val16bits); val16bits=0; // prepare to "OR" in two more bytes. address+=2; } } if (i & 0x1) { // Handle that last byte if we need to. val16bits = val16bits | 0xFF00; (*Put_CBack)((unsigned short *)address,val16bits); } *load_addr = LoadAddr; *entry_addr = EntryAddr; *num_bytes = NumBytes; return LOAD_OK;}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -