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

📄 srec2flash.c

📁 ADAM2 sources (modified by Oleg)
💻 C
字号:
/************************************************************************
 *
 *  srec2flash.c
 *
 *  Tool for conversion between srecord format and flash format
 *
 *  $Id: srec2flash.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 <fcntl.h>
#include <ctype.h>
#include <string.h>

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

#define TOK	  " :\t"

#define WRITE	  0
#define FLUSH	  1
#define SECTORS	  128

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

static int		   bendian   = 1;  // -EB
static int		   wordsize  = 32; // -W32
static int		   padsize   = 32; // -P32
static int		   addr_bits = 20; // -S21
static unsigned long int   lines_since_addr = 0;
static unsigned int	   sector_erased[SECTORS];
static unsigned int	   sector_size;
static char		   pad[9];

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

static void 
print_data(
    unsigned long int   addr, 
    char	        *byte, 
    int			action);

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

static void 
print_data(
    unsigned long int    addr,		/* Byte address		  */
    char		 *byte,		/* Pointer to byte string */
    int			 action)	/* WRITE/FLUSH		  */
{
    static unsigned long   prev_addr;
    static unsigned long   cur_addr;
    static unsigned long   print_addr;
    static int		   first = 1;
    static unsigned char   word[9];
    int			   byte_no;

    /* Only react to flush if data is not all x */
    if( action == FLUSH )
    {
      if (first) return;
      if(strncmp(word,"xxxxxxxx",strlen(word))) 
      {
	  lines_since_addr++;
	  printf("%s%s\n",pad,word);
      }

      /* Pad the end of the file */
      while ((lines_since_addr & 15) != 0) 
      {
	  printf("f111c0de\n");
	  lines_since_addr++;
      }

      first   = 1;
      return;
    }

    /* Mask unwanted address bits */
    addr = (addr << 32-addr_bits) & 0xffffffff;
    addr = (addr >> 32-addr_bits);

    /* cur_addr is in the type dictated by wordsize */
    if (wordsize==32)     cur_addr = addr >> 2;
    else if(wordsize==16) cur_addr = addr >> 1;
    else                  cur_addr = addr >> 0; // trivial

    /* Initialize on first call */
    if(first)
    {
	if(wordsize==32)      strcpy(word, "00000000" );
	else if(wordsize==16) strcpy(word, "0000" );
	else                  strcpy(word, "00" );

        prev_addr = cur_addr;

	if (wordsize==32) print_addr = 4 * cur_addr;
	if (wordsize==16) print_addr = 2 * cur_addr;

	printf("@%.8x\n",print_addr);
	printf(">%.8x\n",print_addr);

	lines_since_addr = 0;

        first=0;
    }

    /* When current word is complete dump it */
    else if(cur_addr != prev_addr)
    {
        /* TBD : Check if we have to erase this sector */

	printf("%s%s\n",pad,word);

	/* Keep a count of how many lines we have written */
	lines_since_addr++;

	/* Re-initialize word string */
	if(wordsize==32)      strcpy(word, "00000000" );
	else if(wordsize==16) strcpy(word, "0000" );
	else                  strcpy(word, "00" );

	if (wordsize==32) print_addr = 4 * cur_addr;
	if (wordsize==16) print_addr = 2 * cur_addr;
	
        if ((cur_addr != prev_addr + 1L))
	{
	    /*  We're about to print a new address, so pad the previous
	     *  data to x16 lines 
	     */
	    while ((lines_since_addr & 15) != 0) 
	    {
	      printf("f111c0de\n");
	      lines_since_addr++;
	    }
	    lines_since_addr = 0;

	    printf("@%.8x\n",print_addr);
	    printf(">%.8x\n",print_addr);
	}

        prev_addr = cur_addr;
    }

    /*  Place the byte correctly in the word string.
     *  depends on endianess.
     */
    if(wordsize==32)       byte_no = (int) (addr % 4L); // 0, 1, 2, 3
    else if(wordsize==16)  byte_no = (int) (addr % 2L); // 0, 1
    else                   byte_no = (int) (addr % 1L); // 0

    if (!bendian && wordsize==32)      byte_no=byte_no ^ 3;
    else if (!bendian && wordsize==16) byte_no=byte_no ^ 1;

    word[2*byte_no]  =byte[0];
    word[2*byte_no+1]=byte[1];
}

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

int 
main(
    int argc,
    char **argv)
{
    unsigned char	buf[258];
    unsigned char	no_bytes[3];
    unsigned char	addr_str[9];
    unsigned char	byte_str[3];
    unsigned char	commentstr[3] = "//";
    unsigned char	*buf_ptr,*end;
    FILE		*fdi,*fdo;
    unsigned long int	first_addr,last_addr,addr;
    int			i;
    int			sector;

    /* Do we have enough arguments */
    if (argc==1) 
    {
	printf("\n\nUsage:\n",argv[0]);
	printf("%s [-EL -EB -Sxx -W32 -W16 -W8 -P32 -P16] srecfile\n\n",argv[0]);
	printf("-EL : Formats data for little endian memory layout\n");
	printf("-EB : Formats data for big endian memory layout. [DEFAULT]\n");
	printf("-Sxx: Number of byte address bits. Legal range: 1-32. [DEFAULT -S20]. \n");
	printf("-W32: Formats data for a 32 bit wide memory. [DEFAULT]\n");
	printf("-W16: Formats data for a 16 bit wide memory\n");
	printf("-W8:  Formats data for a 8  bit wide memory\n");
	printf("-P32: Pad 'x' on data to get 32 bits data. Only applies to -W16 and -W8. [DEFAULT]\n");
	printf("-P16: Pad 'x' on data to get 16 bits data. Only applies to -W8.\n");
	exit(-1);
    }

    /* Process options */
    i=1;
    while( *argv[i] == '-' )
    {
	if      ( strcmp(  argv[i], "-EL" )   == 0 ) bendian=0;
	else if ( strcmp(  argv[i], "-EB" )   == 0 ) bendian=1;
	else if ( strcmp(  argv[i], "-W32" )  == 0 ) wordsize=32;
	else if ( strcmp(  argv[i], "-W16" )  == 0 ) wordsize=16;
	else if ( strcmp(  argv[i], "-W8" )   == 0 ) wordsize=8;
	else if ( strcmp(  argv[i], "-P32" )  == 0 ) padsize=32;
	else if ( strcmp(  argv[i], "-P16" )  == 0 ) padsize=16;
	else if ( strncmp( argv[i], "-S", 2 ) == 0 )
	{
	    addr_bits = atoi( &argv[i][2] );
	    if (addr_bits>32 || addr_bits<1)
	    {
		printf("Error: No of address bits (%d) out of range [1..32]\n",addr_bits);
		exit (-1);
	    }
	}
	else
	{
	    printf("Error: Unknown argument %s\n",argv[i]);
	    exit(-1);
	}

	i++;
    }

    strcpy(commentstr,"#");

    printf("# Reset the loader state machine.\n!R\n");

    /* Clear the erased record (all = not erased) */
    sector = 0;
    while (sector<SECTORS) 
    {
        sector_erased[sector++] = 0;
    }

    sector_size = 131072;

    /* Setup the pad field */
    if     (wordsize==8  && padsize==32) sprintf(pad, "00000000");
    else if(wordsize==8  && padsize==16) sprintf(pad, "00");
    else if(wordsize==16 && padsize==32) sprintf(pad, "0000");
    else pad[0]=0;

    /* Can we open input file ? */
    if ((fdi=fopen(argv[i],"r"))==(FILE *)0) 
    {
	printf("Can not open %s \n",argv[i]);
	exit(-1);
    }

    /* Now we are ready to read all the s-records */
    while (fgets(buf,200,fdi))
    {
	/* Ignore all entries that does not begin with S3 */
	if (strncmp(buf,"S3",2)) continue;

	/* Get number of bytes */
	strncpy(no_bytes,buf+2,2);
	no_bytes[2]=0;

	/* Get start byte address */
	strncpy(addr_str,buf+4,8);
	addr_str[8]=0;

	/* Get first/last address for this s-record line */
	first_addr = strtoul(addr_str,&end,16);
	last_addr  = strtoul(no_bytes,&end,16) + first_addr - 6L ;

	/* Point to first byte */
	buf_ptr = buf+12;

	/* Loop through all bytes in line */
	for(addr=first_addr; addr <= last_addr; addr++)
        {
	    /* Get byte as a string */
	    strncpy(byte_str,buf_ptr,2);
	    byte_str[2] = 0;
	    buf_ptr+=2;

	    /* Write data to output */
 	    print_data(addr,byte_str,WRITE);
         }
    }

    /* Flush output */
    print_data(0L,NULL,FLUSH);

    fclose(fdi);

    /* Finally make the boot vector info file */
    fdo=fopen("bcfg.flash","w");
    fprintf(fdo,"// srec2flash \n");
    fprintf(fdo,"// Boot configuration:\n");
    fprintf(fdo,"// %s %s\n",(bendian ? "-EB" : "-EL"),
		       (wordsize==8  ? "-W8"  :
                        wordsize==16 ? "-W16" : "-W32"));

    buf[0]=0;
    if(bendian) strcat(buf,"1");
    else        strcat(buf,"0");

    if(wordsize==32)      strcat(buf,"00");
    else if(wordsize==16) strcat(buf,"01");
    else                  strcat(buf,"10");

    fputs(buf,fdo);
    fclose(fdo);

    exit(0);
}




⌨️ 快捷键说明

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