?? srec.c
字號:
/* * File: srec.c * * An implementation of an srec parser which exposes the srec.h interface. * Please see srec.h for more info. * * See also: * srec.h, 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 "srec.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: Note: See srec.h for description. ******************************/void srec_RegisterGetByte(GetByteCallBack_t CBack){ GetByteCBack = CBack;}/****************************** Routine: Description: Note: See srec.h for description. ******************************/void srec_RegisterPut(PutValAtAddrCallBack_t CBack){ Put_CBack = CBack;}/****************************** Routine: Description: Note: See srec.h for description. ******************************/ParseStatus_t srec_parse(unsigned int *load_addr, unsigned int *entry_addr, unsigned int *num_bytes){ int i, length, address, done, type, target_cksum; int EntryAddr, LoadAddr, NumBytes, first_add; unsigned int cksum; unsigned short val16bits; unsigned char val8bits; if ((!Put_CBack) || (!GetByteCBack)) { return LOAD_MISSING_CBACK; } val16bits = NumBytes = EntryAddr = LoadAddr = 0; first_add = done = 0; while (!done) { while('S' != ReadByte()) continue; cksum = 0; type = ReadByte(); length = GetByteValFromCharPair(); cksum += length; if ((length < 0) || (length >= 256)) { return LOAD_INVALID_FORMAT; } length--; switch(type) { case '0': while (length) { val8bits = GetByteValFromCharPair(); cksum += val8bits; length--; } break; case '3': address = 0; for (i = 0; i < 4; i++) { val8bits = GetByteValFromCharPair(); cksum += val8bits; address <<= 8; address |= val8bits; length--; } if (!first_add) { LoadAddr = address; // memorize 1st seen address. first_add = 1; } while (length) { val8bits = GetByteValFromCharPair(); cksum += val8bits; if (!(NumBytes & 0x1)) { val16bits |= val8bits; // first byte. } else { 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; // Target of next "put" } NumBytes++; length--; } break; case '1': address = 0; for (i = 0; i < 2; i++) { val8bits = GetByteValFromCharPair(); cksum += val8bits; address <<= 8; address |= val8bits; length--; } if (!first_add) { LoadAddr = address; // memorize 1st seen address. first_add = 1; } while (length) { val8bits = GetByteValFromCharPair(); cksum += val8bits; if (!(NumBytes & 0x1)) { val16bits |= val8bits; // first byte. } else { 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; // Target of next "put" } NumBytes++; length--; } break; case '7': EntryAddr = 0; for (i = 0; i < 4; i++) { val8bits = GetByteValFromCharPair(); cksum += val8bits; EntryAddr <<= 8; EntryAddr |= val8bits; length--; } done = 1; break; case '9': EntryAddr = 0; for (i = 0; i < 2; i++) { val8bits = GetByteValFromCharPair(); cksum += val8bits; EntryAddr <<= 8; EntryAddr |= val8bits; length--; } done = 1; break; default: break; } // switch(type) cksum = (~cksum) & 0xff; target_cksum = GetByteValFromCharPair(); if (cksum != target_cksum) { return LOAD_INVALID_CHKSUM; } } // while (!done) if (NumBytes & 0x1) { // Pair up final byte with dummy byte to form 16bit val to write out. val16bits = val16bits | 0xFF00; (*Put_CBack)((unsigned short *)address,val16bits); } *load_addr = LoadAddr; *entry_addr = EntryAddr; *num_bytes = NumBytes; return LOAD_OK;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -