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

📄 tmunpackimage.c

📁 可用于TM1300/PNX1300系列DSP(主要用于视频处理)压缩库即应用例子。
💻 C
字号:
/*
 *  +-------------------------------------------------------------------+
 *  | 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.                               |
 *  +-------------------------------------------------------------------+
 */

/*------------------------------ Includes ------------------------------------*/

#include <tmlib/tmtypes.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "unistd.h"
#include "errno.h"
#include "assert.h"
#include "tmlib/tmlibc.h"

#include "tmpackedimage.h"

#ifdef _VERBOSE_
#include <stdio.h>
#endif

#ifdef _COMPRESSION_
#include "tmlib/zlib.h"
#endif

#ifndef _MEMORY_IMAGE_
#include "tmlib/TMDownLoader.h"
#include "tm1/mmio.h"
#endif

#ifndef _MEMORY_IMAGE_
void        _syscall();

extern          UInt32	_clock_freq_init[];
extern          UInt32	_host_type_init[];
extern volatile UInt32	_MMIO_base_init[];
#endif

#if defined(_PRE_UNSHUFFLED_) || defined(_DOWN_UNSHUFFLED_)
extern int 
SHFL_shuffle(unsigned char *, unsigned char *, unsigned long );
#endif

#if defined(_DOWN_UNSHUFFLED_)
int text_len;
int locked_text_base;
int locked_text_len;
#endif

custom_op void iclr(void);

typedef void (*Func) ();

#ifdef _VERBOSE_
    #define PRINTF(x) printf x
#else
    #define PRINTF(x)
    void printf(){}
#endif

void load_and_boot(void* image_mem, UInt32 image_len)
{

   Pointer     sdram_base = (Pointer)MEM_START;
   UInt        sdram_length = MEM_END - MEM_START;

#ifndef _MEMORY_IMAGE_
   TMDwnLdr_Object_Handle handle;
   TMDwnLdr_Status tmdl_ret;

   Int         alignment, minimal_image_size;
#endif /*  _MEMORY_IMAGE_ */
   
#ifdef _DOWN_UNSHUFFLED_
   unsigned char *unshuffled;
   unsigned long length;
 
   text_len         = TEXT_LEN;
   locked_text_base = LOCKED_TEXT_BASE;
   locked_text_len  = LOCKED_TEXT_LEN;

   /* get start and length of text */
   unshuffled = (void *)(unsigned long)image_mem;
   length = text_len;

   if (length != 0)
      /* shuffle the text*/
      SHFL_shuffle(unshuffled,unshuffled, length);

   /* get start and length of locked text */
   unshuffled += (locked_text_base - (int)sdram_base);
   length = locked_text_len;

   if (length != 0){
      /* unshuffle the locked test */
      SHFL_shuffle(unshuffled,unshuffled, length);
   }

   /* no need to update image mem */
#endif   
  
#ifdef _PRE_UNSHUFFLED_
   unsigned char *unshuffled;
   unsigned long length;
   unsigned long *shift_mem;
 
   /* get start and length of text */
   unshuffled = (void *)((unsigned long)image_mem + 3*sizeof(unsigned long));
   length = *(unsigned long *)image_mem;

   if (length != 0)
      /* shuffle the text*/
      SHFL_shuffle(unshuffled,unshuffled, length);

   /* get start and length of locked text */
   unshuffled += ((unsigned long *)image_mem)[2];
   length = ((unsigned long *)image_mem)[1];

   if (length != 0)
      /* unshuffle the locked test */
      SHFL_shuffle(unshuffled,unshuffled, length);

   /* update image length */
   image_len = image_len - 3*sizeof(unsigned long);

#ifdef _COMPRESSION_
   /* get length of image in unsigned long and round up if required */
   length = (image_len/sizeof(unsigned long)) + ((image_len%sizeof(unsigned long))?1:0);
   shift_mem = (unsigned long *)image_mem;
   
   /* shift image_mem 3*sizeof(unsigned long) forward */
   while( length-- > 0){
      *shift_mem = *(unsigned long *)(shift_mem + 3);
      shift_mem++;
      
   }
#else 
   /* update memory pointer */
   image_mem = (void *)((unsigned long)image_mem + 3*sizeof(unsigned long));
#endif /* _COMPRESSION_ */
      
#endif /* _PRE_UNSHUFFLED_ */

   PRINTF(("Loading image in 0x%08x - 0x%08x...\n", sdram_base, ((UInt) sdram_base) + sdram_length));
   
#ifdef _MEMORY_IMAGE_
   assert(image_len < (Int)  sdram_length);
   assert((Int) sdram_base % 16  == 0);
   
#ifndef _COMPRESSION_
      /* copy the memory to start address */
      memcpy(sdram_base, image_mem, image_len);
#endif /* _COMPRESSION_ */

#else   /*  _MEMORY_IMAGE_ */

   /* load the executable object and create handle */
   if((tmdl_ret = TMDwnLdr_load_object_from_mem(image_mem,image_len,Null,&handle)) != TMDwnLdr_OK){
      PRINTF(("Error TMDwnLdr_load_object_from_mem %d\n",tmdl_ret));
   }
   
   /* get the minimal image size */
   if((tmdl_ret = TMDwnLdr_get_image_size(handle, &minimal_image_size, &alignment)) != TMDwnLdr_OK){
      PRINTF(("Error TMDwnLdr_get_image_size %d\n",tmdl_ret ));
   }   
   /* check if image size is too large */
   assert(minimal_image_size < (Int) sdram_base + sdram_length);
   assert((Int) sdram_base % alignment == 0);
   
   PRINTF(("minimal_image_size 0x%08x\n", minimal_image_size));

#ifndef _NOHOST_
   /* resolve symbol - not used in nohost - no error checking*/
   TMDwnLdr_resolve_symbol(handle, "__syscall", (Int) _syscall); */
#endif /* _NOHOST_ */
   
   /* relocate the executable */
   if((tmdl_ret = TMDwnLdr_relocate(handle, (tmHostType_t) _host_type_init,
         (Address) _MMIO_base_init,
         (UInt) _clock_freq_init,
         sdram_base, sdram_length,
         TMDwnLdr_LeaveCachingToDownloader)) != TMDwnLdr_OK){
      PRINTF(("Error TMDwnLdr_relocate %d\n",tmdl_ret ));
   }
   
   /* extract load image to sdram_base */
   if((tmdl_ret = TMDwnLdr_get_memory_image(handle, sdram_base)) != TMDwnLdr_OK){
      PRINTF(("Error TMDwnLdr_get_memory_image  %d\n",tmdl_ret ));
   }   
   /* return the handle */
   if((tmdl_ret = TMDwnLdr_unload_object(handle)) != TMDwnLdr_OK){
      PRINTF(("Error TMDwnLdr_unload_object %d\n",tmdl_ret ));
   }
   
#endif /*  _MEMORY_IMAGE_ */

   PRINTF(("Running ...\n"));

   /* flush the data cache */
   _cache_copyback(sdram_base, sdram_length);

   /* invalicate instruction cache */
   iclr();
      
   /* jump to new memory image */
   ((Func) sdram_base) ();

   /* never come back */
}

/******************************************************************************/

void UnpackRun(void)
{
   Int count = 0;
   
#ifdef _COMPRESSION_
   void *image_mem;
   int zlib_ret;
   uint u_size = 0;
#endif

   
#ifdef _COMPRESSION_

#ifndef _MEMORY_IMAGE_
   /* allocate memory for uncompressed image */
   image_mem = (void *)malloc(FDList[count].FileLength);
   if(image_mem == NULL){
      PRINTF(("Error allocating memory for uncompressed memory\n"));
   }
#else
   /* use MEM_START/sdram_base as image memory location */
   image_mem = (Pointer)MEM_START;
#endif

   u_size = FDList[count].FileLength;
   
   /* uncompress data */
   zlib_ret = uncompress(image_mem, (uLongf *)&u_size,FDList[count].Data, FDList[count].CompLen);
   
   if(zlib_ret != Z_OK || u_size != FDList[count].FileLength){
      PRINTF(("Error decompressing %s %d %d\n", FDList[count].Pathname,zlib_ret,u_size));
      PRINTF(("Usize %d Csize %d\n", FDList[count].FileLength,FDList[count].CompLen));
   }
         
   /* close file */
   PRINTF(("Loading  %s from 0x%x of size 0x%x\n",FDList[count].Pathname,
                                               image_mem,
                                               FDList[count].FileLength));
   
   load_and_boot(image_mem,FDList[count].FileLength);

#ifndef _MEMORY_IMAGE_
   /* never reached */
   free(image_mem);
#endif
   
#else
   /* close file */
   PRINTF(("Loading  %s of size %d\n",FDList[count].Pathname,FDList[count].FileLength));
   
   load_and_boot(FDList[count].Data,FDList[count].FileLength);
#endif         

}


/******************************************************************************/
Int main()
{
   PRINTF(("----------- tmunpackimage Application started ------------\n"));

   UnpackRun();
}


⌨️ 快捷键说明

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