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

📄 tmwrb.c

📁 可用于TM1300/PNX1300系列DSP(主要用于视频处理)压缩库即应用例子。
💻 C
📖 第 1 页 / 共 3 页
字号:
/*
 *  +-------------------------------------------------------------------+
 *  | Copyright (c) 1995,2000 TriMedia Technologies Inc.                |
 *  |                                                                   |
 *  | This software  is furnished under a license  and may only be used |
 *  | and copied in accordance with the terms  and conditions of such a |
 *  | license  and with  the inclusion of this  copyright notice.  This |
 *  | software or any other copies of this software may not be provided |
 *  | or otherwise  made available  to any other person.  The ownership |
 *  | and title of this software is not transferred.                    |
 *  |                                                                   |
 *  | The information  in this software  is subject  to change  without |
 *  | any  prior notice  and should not be construed as a commitment by |
 *  | TriMedia Technologies.                                            |
 *  |                                                                   |
 *  | This  code  and  information  is  provided  "as is"  without  any |
 *  | warranty of any kind,  either expressed or implied, including but |
 *  | not limited  to the implied warranties  of merchantability and/or |
 *  | fitness for any particular purpose.                               |
 *  +-------------------------------------------------------------------+
 *
 *  Module name              : tmWRB.c    1.19
 *
 *  Module type              : IMPLEMENTATION
 *
 *  Title                    : Write Booter
 *
 *  Last update              : 16:19:55 - 00/06/16 
 *
 *  Description              : Usase zlib compression, Command line parser
 *                             and down loader    
 */
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

#ifndef _WIN32
#include <unistd.h>
#include <dirent.h>
#else
#include <windows.h>
#endif


#include <TMDownLoader.h>
#include <tmhost.h>

/* if using the hp build */
#ifdef hpux
/* undefine Long and uLongf for zlib.h*/   
#undef uLong
#undef uLongf
#undef Byte
#endif


#include <tsaCmdOpt.h>
#include <zlib.h>

#include "tmWRB_options.h"

tsaCmdOptOption_t	options[] = {
#define	_Eg_opts_impl
#undef	Def
#define	Def(name,o1,o2,typ,flags,dflt,descr)	\
  o1,o2,typ,flags,(void *)(dflt),descr,Null,
#include "tmWRB_options.h"
};

#ifndef O_BINARY
#define O_BINARY 0 /* not defined on Unix */
#endif

#ifdef _WIN32
#define SLASH "\\"
#else
#define SLASH "/"
#endif

#define UNIX_PATH_NAME_SEPARATOR '/'
#define WIN_PATH_NAME_SEPARATOR  '\\'

#define MAX_FILENAME 1024
#define BLOCKSIZE    0x800

#define TEMP_DIR     ".tmWRB.temp"
#define TEMP_BASE    TEMP_DIR SLASH "tmpackedboot"

#define	Nelts(vec)	(sizeof(vec) / sizeof((vec)[0]))

static const char *progname = NULL;

static char *out_name_temp = NULL;
static char *out_namet =NULL;
static char *out_nameh =NULL;

static char buffer[MAX_FILENAME];
static char tcspath_buffer[MAX_FILENAME];
static char nostandard_buf[MAX_FILENAME];

static int fo = 0;
static int ft = 0;
static int fh = 0;

static int mem_start = 0;
static int mem_end   = 0;

static char buf[BLOCKSIZE];

static unsigned int NumElements = 0;

/* options arguements intialised as per data
   struct in tmWRB_options.h */
static Bool  NoCompression;
static Bool  inmi;
static Bool  out;
static char* host;
static int   tm_freq;
static int   mmio_base;
static char  *output;
static TMDwnLdr_CachingSupport   cache_support;
static char  *bigendian;
static Bool  unshuffled;
static Bool  verbose;
static Bool  nostandard;
static Bool  flashbsp;
static char  *cflags;
static char  *ldflags;
static char  *tcspath;
static char  *cpath;

static char  *image_file;
static int   host_int;
static Bool  download_unshuffled;

static int text_len;
static int locked_text_len;
static int locked_text_base;

/* used for siumlator Memory image */
static int  lv_syscall;

/* Prototypes for hidden supported functions. */
void TMDwnLdr_set_unshuffled( TMDwnLdr_Object_Handle  handle );
void TMDwnLdr_get_text_positions( TMDwnLdr_Object_Handle handle, 
                                  Int *text_len, Int *locked_text_len, Int *locked_text_base);

/*--------------------------- helper functions -------------------------*/

static int
parse_number( char *repr, char terminator, char **next, int *result )
{
     char *nxt;

    *result= strtol( repr, &nxt, 0 );

    if(*result < 0){
       return False;
    }

    if( *nxt != terminator){
        printf("Wrong Numeric Format\n");
        return False;
    }

    if (next) { *next= nxt; }

    return True;
}

static int
copy( char *old, char *new){

   int src, dest ;
    int lv_read,lv_write,lv_ret,file_size;
    void *inmem;
    struct stat statBuf;
    
    src = open(old, O_RDONLY|O_BINARY,O_RDONLY|O_BINARY);
    if (src == -1) {
        printf("Failed to open %s with error code %d\n", old, errno);
        return False;
    }

    /* get size of source file */
    lv_ret = fstat(src,&statBuf);
    if(lv_ret == -1) {
       printf("Failed to stat %s with error code %d\n", old, errno);
       close(src);
       return False;
    }
    
   file_size = statBuf.st_size;

   /* inmem is input memory */
   inmem = (void *)malloc(file_size);

   /* buffer will be output memory */
   if (inmem == NULL)
   {
      printf("out of memory\n");
      free(inmem);close(src);
      return False;
   }
   
   /* read the source into buf */
   lv_read= read  (src,  inmem, file_size);
   if(lv_read != file_size)
   {
       printf("read failure\n");
      free(inmem);close(src);
      return False;
   }

   close(src);

   errno = 0;
   dest = open(new,O_CREAT|O_WRONLY|O_BINARY,0x1FF);
   if (dest == -1) {
      printf("Failed to create %s with error code %d\n", new, errno);
      free(inmem);
      return False;
   }

   lv_write = write(dest, inmem, file_size);
   if(lv_write != file_size)
   {
       printf("write failure\n");
      free(inmem);   close(dest);
      return False;
   }

   free(inmem);
   close(dest);

   /* ensure that the file is readable */
   chmod(new,0x1FF);

   return file_size;
}

void 
rewrite_slashes( char *cmd )
{
#if defined(_WIN32)
	int i, len = strlen(cmd);

	for ( i = 0; i < len; i++ ) {
		if ( cmd[i] == UNIX_PATH_NAME_SEPARATOR )
			cmd[i] = WIN_PATH_NAME_SEPARATOR;
	}
#endif
}

#if	defined(_WIN32)
/* Windows implementation of Posix.1 directory handling calls:
 *	opendir(), readdir(), rewinddir(), closedir()
 * and also the unrelated system calls
 *	fsync(), sync().
 * These are implemented directly as system calls on SunOS and HP-UX
 * but not on Windows,
 * so this is Windows-specific code, required only for Windows.
 */

/* Directory entry. */
struct	dirent
{
	long	d_ino;
	long	d_namlen;
	char	d_name[MAX_PATH];
};

/* Directory. */
typedef	struct
{
	struct dirent	dirent;
	int		Win32EntryCount;
	HANDLE		Win32DirectoryHandle;
	WIN32_FIND_DATA	Win32FindData;
	char		Win32DirectoryName[1];
} DIR;

#define	_IFMT		0170000	/* type of file				*/
#define		_IFDIR	0040000	/* directory				*/
#define		_IFCHR	0020000	/* character special			*/
#define		_IFBLK	0060000	/* block special			*/
#define		_IFREG	0100000	/* regular				*/
#define		_IFLNK	0120000	/* symbolic link			*/
#define		_IFSOCK	0140000	/* socket				*/
#define		_IFIFO	0010000	/* fifo					*/

#define	S_ISDIR(m)	(((m)&_IFMT) == _IFDIR)

/* Open a directory. */
DIR *
opendir(char const *dirname)
{
	DIR	*dir;

	dir = (DIR *)malloc(sizeof(*dir) + strlen(dirname) + 3);
	if (dir == NULL) {
		/* No memory for DIR structure. */
		errno = ENOMEM;
		return NULL;
	}
	dir->Win32EntryCount = 0;
	sprintf(dir->Win32DirectoryName, "%s\\*", dirname);
	dir->Win32DirectoryHandle = FindFirstFile(dir->Win32DirectoryName,
						  &dir->Win32FindData);
	if (dir->Win32DirectoryHandle == INVALID_HANDLE_VALUE) {
		/* Not found, free and return failure. */
		free(dir);
		errno = ENOENT;
		return NULL;
	}
	return dir;	/* success */
}

/* Read the next entry from a directory. */
struct dirent *
readdir(DIR * dir)
{
	if (dir->Win32EntryCount == 0) {
		FindClose(dir->Win32DirectoryHandle);
		dir->Win32DirectoryHandle = FindFirstFile(dir->Win32DirectoryName,
							  &dir->Win32FindData);
		if (dir->Win32DirectoryHandle == INVALID_HANDLE_VALUE) {
			errno = EINVAL;
			return NULL;
		}
	} else if ((FindNextFile(dir->Win32DirectoryHandle,
				 &dir->Win32FindData) == 0) 
		&& (GetLastError() == ERROR_NO_MORE_FILES)) {
			return NULL;
	}
	dir->Win32EntryCount++;
	dir->dirent.d_ino = dir->Win32EntryCount;	/* this field is unneeded */
	dir->dirent.d_namlen = strlen(dir->Win32FindData.cFileName);
	strcpy(dir->dirent.d_name, dir->Win32FindData.cFileName);
	return &dir->dirent;
}

/* Rewind a directory. */
void
rewinddir(DIR *dir)
{
	dir->Win32EntryCount = 0;
}

/* Close a directory. */
int
closedir(DIR *dir)
{
	if (dir->Win32DirectoryHandle != INVALID_HANDLE_VALUE)
		FindClose(dir->Win32DirectoryHandle);
	free(dir);
	return 0;
}

/* File sync: a nop. */
int
fsync(int fildes)
{
	return 0;
}

/* Sync: a nop. */
int
sync(void)
{
	return 0;
}

#endif	/* defined(_WIN32) */

/*------------------------- conversion functions ----------------------*/

static
void ConvertOutToMI(char *outmem,int outsize,char *mimem,int *misize){

   Pointer     sdram_base = (Pointer)mem_start;
   UInt        sdram_length = mem_end - mem_start;

   TMDwnLdr_Object_Handle handle;
   TMDwnLdr_Status tmdl_ret;

   Int         alignment;
   
   /* load the executable object and create handle */
   if((tmdl_ret = TMDwnLdr_load_object_from_mem(outmem,outsize,Null,&handle)) != TMDwnLdr_OK) {
      fprintf(stdout, "Error TMDwnLdr_load_object_from_mem: %s\n", TMDwnLdr_get_last_error(tmdl_ret) );
      exit(-1);
   }
   
   /* if image should be unshuffled */
   if(unshuffled  == True || download_unshuffled == True){
      /* tell down loader to unshuffle image */
      TMDwnLdr_set_unshuffled(handle);
   }

   /* get the minimal image size */
   if((tmdl_ret = TMDwnLdr_get_image_size(handle, misize, &alignment)) != TMDwnLdr_OK) {
      fprintf(stdout, "Error TMDwnLdr_get_image_size: %s\n", TMDwnLdr_get_last_error(tmdl_ret) );
      exit(-1);
   }
   
   /* check if image size is too large */
   if(*misize > (Int) sdram_length)
      printf("Memory image 0x%x is larger than SDRAM 0x%x\n",*misize,(Int) sdram_length );
      
   if((Int) sdram_base % alignment != 0)
      printf("SDRAM Base 0x%x not aligned on a 64 byte boundary\n",sdram_base );

   /* resolve symbol - not used with nohost */
   if(host_int != tmNoHost){
      /* cheat a little bit */
      lv_syscall = mmio_base;
      
      if((tmdl_ret = TMDwnLdr_resolve_symbol(handle, "__syscall", (Int) lv_syscall)) != TMDwnLdr_OK) {
         fprintf(stdout, "Error TMDwnLdr_resolve_symbol: %s\n", TMDwnLdr_get_last_error(tmdl_ret) );
         exit(-1);
      }
   }

⌨️ 快捷键说明

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