⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 flash2bin.c

📁 ADAM2 sources (modified by Oleg)
💻 C
字号:
/************************************************************************
 *
 *  flash2bin.c
 *
 *  Tool for conversion between flash format and binary format
 *
 *  flash2bin [<input-file> [<output-file>]]
 *
 *  $Id: flash2bin.c,v 1.1.1.1 2002/08/06 20:52:00 sneha Exp $
 *
 * ######################################################################
 *
 * Copyright (C) 1999 MIPS Technologies, Inc. All rights reserved.
 *
 * Unpublished rights reserved under the Copyright Laws of the United
 * States of America.
 *
 * This document contains information that is confidential and proprietary
 * to MIPS Technologies, Inc. and may be disclosed only to those employees
 * of MIPS Technologies, Inc. with a need to know, or as otherwise
 * permitted in writing by MIPS Technologies, Inc. or a
 * contractually-authorized third party. Any copying, modifying, use or
 * disclosure of this information (in whole or in part) which is not
 * expressly permitted in writing by MIPS Technologies, Inc. or a
 * contractually-authorized third party is strictly prohibited. At a
 * minimum, this information is protected under trade secret and unfair
 * competition laws and the expression of the information contained herein
 * is protected under federal copyright laws. Violations thereof may
 * result in criminal penalties and fines.
 *
 * MIPS Technologies, Inc. or any contractually-authorized third party
 * reserves the right to change the information contained in this document
 * to improve function, design or otherwise. MIPS Technologies, Inc. does
 * not assume any liability arising out of the application or use of this
 * information. Any license under patent rights or any other intellectual
 * property rights owned by MIPS Technologies or third parties shall be
 * conveyed by MIPS Technologies, Inc. or any contractually-authorized
 * third party in a separate license agreement fully executed by the
 * parties.
 *
 * The information contained in this document constitutes "Commercial
 * Computer Software" or "Commercial Computer Software Documentation," as
 * described in FAR 12.212 for civilian agencies, and DFARS 227.7202 for
 * military agencies. This information may only be disclosed to the U.S.
 * Government, or to U.S. Government users, with prior written consent from
 * MIPS Technologies, Inc. or a contractually-authorized third party. Such
 * disclosure to the U.S. Government, or to U.S. Government users, shall
 * be subject to license terms and conditions at least as restrictive and
 * protective of the confidentiality of this information as the terms and
 * conditions used by MIPS Technologies, Inc. in its license agreements
 * covering this information.
 *
 ************************************************************************/


/************************************************************************
 *  Include files
 ************************************************************************/

#include <stdio.h>
#include <stdlib.h>

/************************************************************************
 *  Definitions
 ************************************************************************/

/************************************************************************
 *  Public variables
 ************************************************************************/

/************************************************************************
 *  Static variables
 ************************************************************************/

/************************************************************************
 *  Static function prototypes
 ************************************************************************/

static int 
getline( FILE *f, char line[], unsigned long *len ) ;


/************************************************************************
 *  Implementation : Public functions
 ************************************************************************/


/************************************************************************
 *                          main
 ************************************************************************/
int 
main(int argc,char **argv)
{
    unsigned long  len, c, current_adr, temp_adr ;
    FILE           *source ;
    FILE	   *destination ;
    int		   i;
    char	   line[200] ;
    char	   char2bin[256] ;
    int            bendian;
    int            sarg;

    sarg=1;
    bendian=1;

    if (!strcmp(argv[1],"-EB"))  
      {
      bendian=1;
      sarg++;
      }

    if (!strcmp(argv[1],"-EL"))  
      {
      bendian=0;
      sarg++;
      }
  
    source      = (argc > sarg) ? fopen( argv[sarg], "r" ) : stdin;

    if (source == NULL) 
    {
        printf("File %s does not exist\n", argv[sarg] );
        exit (EXIT_FAILURE);
    } 

    destination = (argc > sarg+1) ? fopen( argv[sarg+1], "w" ) : stdout;

    if (destination == NULL) 
    {
        printf("Could not create %s\n", argv[sarg+1] );
	fclose( source );
        exit (EXIT_FAILURE);
    } 

    for(i=0;i<256;i++)
    {
        char2bin[i] = -1 ;
    }
   
    char2bin['0'] = 0 ;
    char2bin['1'] = 1 ;
    char2bin['2'] = 2 ;
    char2bin['3'] = 3 ;
    char2bin['4'] = 4 ;
    char2bin['5'] = 5 ;
    char2bin['6'] = 6 ;
    char2bin['7'] = 7 ;
    char2bin['8'] = 8 ;
    char2bin['9'] = 9 ;

    char2bin['a'] = 0xa ;
    char2bin['b'] = 0xb ;
    char2bin['c'] = 0xc ;
    char2bin['d'] = 0xd ;
    char2bin['e'] = 0xe ;
    char2bin['f'] = 0xf ;
    char2bin['A'] = 0xa ;
    char2bin['B'] = 0xb ;
    char2bin['C'] = 0xc ;
    char2bin['D'] = 0xd ;
    char2bin['E'] = 0xe ;
    char2bin['F'] = 0xf ; 

    current_adr = 0 ;
    
    while( getline( source, line, &len ) )
    {
        if ( (line[0] == '@') && (len == 9) )
        {
            temp_adr = 0 ;
            for(i=0;i<4;i++)
            {
                if ( ( (char2bin[(line[(i*2)+1])]) != -1 ) &&
                     ( (char2bin[(line[(i*2)+2])]) != -1 ) )
                {
                    c = ( (char2bin[(line[(i*2)+1])] << 4) |
                          (char2bin[(line[(i*2)+2])])  ) ;
                }
                temp_adr <<= 8;
                temp_adr +=  c ;
            }
            if (current_adr == 0)
            {
                current_adr = temp_adr ;
            }
            else
            {
                /* PAD */
                if (temp_adr > current_adr)
                {
                    len = temp_adr - current_adr ;   
                    c = 0xff ;
                    for(i=0;i<len;i++)
                    {
                        putc(c,destination) ;
                    }
                    current_adr = temp_adr ;
                }
                else
                {
                    fclose(source);
                    fclose(destination);
                    return -1 ;
                }
            }
        }

        if ( isxdigit(line[0]) && len == 8 )
        {
            if (bendian)
	      {
		for(i=0;i<4;i++)
		  {
		    if ( ( (char2bin[(line[i*2])])     != -1 ) &&
			 ( (char2bin[(line[(i*2)+1])]) != -1 ) )
		      {
			c = ( (char2bin[(line[i*2])] << 4) |
			      (char2bin[(line[(i*2)+1])])  ) ;
			putc(c,destination) ;
		      }
		  }
	      }
	    else
	      {
		for(i=3;i>=0;i--)
		  {
		    if ( ( (char2bin[(line[i*2])])     != -1 ) &&
			 ( (char2bin[(line[(i*2)+1])]) != -1 ) )
		      {
			c = ( (char2bin[(line[i*2])] << 4) |
			      (char2bin[(line[(i*2)+1])])  ) ;
			putc(c,destination) ;
		      }
		  }
	      }
            current_adr = current_adr + 4 ;
        }
    }

    fclose(source);
    fclose(destination);

    return EXIT_SUCCESS;
}


/************************************************************************
 *  Implementation : Static functions
 ************************************************************************/


/************************************************************************
 *                          getline
 ************************************************************************/
static int 
getline( FILE *f, char line[], unsigned long *len )
{
    int i ;

    i = 0 ;
    while ( 1 )
    {
        line[i] =  getc(f);
        if ( line[i] == EOF  )
        {
            *len = i ;
            return(0) ;
        }

        if ( ( line[i] == '\r' ) ||
             ( line[i] == '\n' )    )
        {
            *len = i ;
            return(1) ;
        }
        i++ ;
    }
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -