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

📄 tmunpackboot.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 "tmpackedboot.h"

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

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

#include "tmlib/tmFlash.h"

#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;
   void *extract_mem_image;
   Int         alignment, minimal_image_size;
#endif
#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;
 
   /* 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 mem */
   image_mem = (void *)((unsigned long)image_mem + 3*sizeof(unsigned long));
   image_len = image_len - 3*sizeof(unsigned long);
#endif   
   
   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);

   PRINTF(("Writing Boot Image \n"));

   if(!FlashUtil_put_bootimage(image_mem,sdram_base,image_len)) {
      PRINTF(("Error FlashUtil_put_bootimage 0x%x 0x%x 0x%x\n",image_mem, sdram_base,image_len));
   }

#else   

   /* 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 - only used with simulator - no error checking*/
   TMDwnLdr_resolve_symbol(handle, "__syscall", (Int) _syscall); */
#endif
   
   /* 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_mem_image = (void *)malloc(image_len);

   /* extract load image to extract_mem_image */
   if((tmdl_ret = TMDwnLdr_get_memory_image(handle, extract_mem_image)) != 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 ));
   }

   PRINTF(("Writing Booter Image 0x%x 0x%x 0x%x\n",extract_mem_image, sdram_base,image_len));

   if(FlashUtil_put_bootimage(extract_mem_image,sdram_base,image_len) != True) {
      PRINTF(("Error FlashUtil_put_bootimage 0x%x 0x%x 0x%x\n",extract_mem_image, sdram_base,image_len));
   }

   /* free allocated memory */
   free(extract_mem_image);
   
#endif

}

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

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

   
#ifdef _COMPRESSION_
   /* allocate memory for uncompressed image */
   image_mem = (void *)malloc(FDList[count].FileLength);
   if(image_mem == NULL){
      PRINTF(("Error allocating memory for uncompressed memory\n"));
   }
   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);

   /* never reached */
   free(image_mem);
#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         

}

/*------------------------------- Event handler ------------------------------*/

static Int nrof_write_errors;

static void event_handler(FlashEvent);
static EventHandler old_event_handler= event_handler;

static void event_handler(FlashEvent event)
{
    if (event == FlashWriteError) {
        nrof_write_errors++;
    } else 
    if (event == FlashFull) {
        PRINTF(("Flash full\n"));
        exit(-1);
    } else 
    if (event == FlashDangerousWriteError) {
        PRINTF(("Dangerous write error\n"));
        exit(-1);
    }
}

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

   old_event_handler= Flash_install_event_handler( event_handler );

   UnpackRun();
        
   PRINTF(("----------- tmWRB Application terminated ------------\n"));

   exit(0);
}



⌨️ 快捷键说明

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