?? tmwrb.c
字號:
/* relocate the executable */
if((tmdl_ret = TMDwnLdr_relocate(handle, (tmHostType_t) host_int,
(Address) mmio_base, (UInt) tm_freq, sdram_base,
sdram_length,cache_support)) != TMDwnLdr_OK) {
fprintf(stdout, "Error TMDwnLdr_relocate: %s\n", TMDwnLdr_get_last_error(tmdl_ret) );
exit(-1);
}
/* extract load image to memory image address */
if((tmdl_ret = TMDwnLdr_get_memory_image(handle, mimem)) != TMDwnLdr_OK) {
fprintf(stdout, "Error TMDwnLdr_get_memory_image : %s\n", TMDwnLdr_get_last_error(tmdl_ret) );
exit(-1);
}
/* get the unshuffled sections of text */
TMDwnLdr_get_text_positions( handle,
&text_len, &locked_text_len, &locked_text_base);
/* return the handle */
if((tmdl_ret = TMDwnLdr_unload_object(handle)) != TMDwnLdr_OK) {
fprintf(stdout, "Error TMDwnLdr_unload_object: %s\n", TMDwnLdr_get_last_error(tmdl_ret) );
exit(-1);
}
}
/*------------------------ compression function ----------------------*/
static int compressSrc (int src, char *buffer, int *blocksize)
{
int usize, misize;
uLongf csize;
int lv_ret;
void *inmem = NULL;
void *mimem = NULL;
/* inmem is input memory */
inmem = (void *)malloc(*blocksize);
/* buffer will be output memory */
if (inmem == NULL)
{
printf("%s: out of memory\n", progname);
free(inmem);
exit(1);
}
/* read the source into buf */
usize= read (src, inmem, *blocksize);
if(usize <= 0 )
{
printf("%s: read failure\n", progname);
free(inmem);
return usize;
}
/* if the input format and output format are different */
if(inmi == False && out == False){
/* inmem is input memory */
mimem = (void *)malloc(usize);
/* buffer will be output memory */
if (mimem == NULL)
{
printf("%s: out of memory\n", progname);
free(inmem);
free(mimem);
exit(-1);
}
/* convert input image from out to mi */
ConvertOutToMI(inmem,usize,mimem,&misize);
/* free inmem ; not required anymore*/
free(inmem);
/*update the new size and memory pointer */
inmem = mimem;
*blocksize = usize = misize;
}
/* fill in output length */
csize = usize + (int)((usize*0.015)+1.0) +12;
lv_ret = compress((unsigned char *)buffer,&csize,inmem,usize);
if (lv_ret != Z_OK)
{
/* this should NEVER happen */
printf("internal error - compression failed: %d\n", lv_ret);
free(inmem);
exit(2);
}
free(inmem);
return (int)csize;
}
/*------------------------ Recursive directory printing ----------------------*/
static int copy_file(char *inpath)
{
int src = open(inpath, O_RDONLY|O_BINARY);
unsigned int size, count = 0;
unsigned int c_size =0,total_size =0;
int ret, misize;
void *mimem;
char *buffer;
struct stat statBuf;
if (src == -1) {
printf("* F %s SOURCE FAILED\n", inpath, errno);
return False;
} else {
printf(" Packing %s \n", inpath );
/* write extern for the datafile in the header */
sprintf(buf,"extern Byte DataFile%d[];\n",NumElements);
write(fh, buf, strlen(buf));
sprintf(buf,"{.global _DataFile%d}\n{_DataFile%d:}\n",NumElements,NumElements);
write(ft, buf, strlen(buf));
/* get size of file */
ret = fstat(src,&statBuf);
if(ret == -1) {
close(src);
return False;
}
total_size = statBuf.st_size;
if(NoCompression == True){
/* allocate space for buffer */
buffer = (char *)malloc(total_size);
/* read data from original file */
size= read (src, buffer, total_size);
/* if the input format and output format are different */
if(inmi == False && out == False){
/* mimem is input memory */
mimem = (void *)malloc(total_size);
/* buffer will be output memory */
if (mimem == NULL)
{
printf("%s: out of memory\n", progname);
free(mimem);
free(mimem);
exit(-1);
}
/* convert input image from out to mi */
ConvertOutToMI(buffer,total_size,mimem,&misize);
/* free inmem ; not required anymore*/
free(buffer);
/*update the new size and memory pointer */
buffer = mimem;
size=total_size = misize;
}
}
else{
/* compressed image can be bigger than uncompressed some times */
buffer = (char *)malloc(total_size+ (unsigned int)((total_size*0.015)+1) +12);
/* need to set value 'size', and have compressed data in 'buffer' */
size = c_size = compressSrc (src, buffer,(int *)&total_size);
}
count = 0;
/* while data left in this block */
while (count < size){
/* if its one of the middle 64 */
if((count % 64 != 0 ) && (count % 64 != 63))
sprintf(buf,",0x%2.2x",(unsigned char)buffer[count]);
/* if this is the first byte of 64 */
else if((count % 64) == 0)
sprintf(buf,"{.byte 0x%2.2x",(unsigned char)buffer[count]);
/*else this is the last of 64 */
else
sprintf(buf,",0x%2.2x}\n",(unsigned char)buffer[count]);
/* write to tree file */
write(ft, buf, strlen(buf));
count++;
}
/* no need to do this - fill in last few bytes */
while(count % 4 != 0 ){
/* write 0 values */
sprintf(buf,",0x00");
write(ft, buf, strlen(buf));
count++;
}
/* if this is the not a 64th byte will need to add end curly brace */
if(size % 64 != 0){
/* write final bit for this file in output file */
sprintf(buf,"}\n");
write(ft, buf, strlen(buf));
}
/* fill in path names in header file */
sprintf(buf,"Char Path%d[] = \"%s\";\n", NumElements,inpath);
write(fh, buf, strlen(buf));
/* fill in data struct in temp output file 3 */
/* if this is not the first element don print a comma at start */
if(NumElements != 0)
sprintf(buf,",\n {\n File, Path%d, %d, %d, DataFile%d\n }",
NumElements, total_size, c_size, NumElements);
else
sprintf(buf,"\n {\n File, Path%d, %d, %d, DataFile%d\n }",
NumElements, total_size, c_size, NumElements);
write(fo, buf, strlen(buf));
/* if the down loaded does unshuffling */
if(download_unshuffled == True){
sprintf(buf,"\n#define TEXT_LEN %d\n", text_len);
write(fh, buf, strlen(buf));
sprintf(buf,"#define LOCKED_TEXT_BASE %d\n", locked_text_base);
write(fh, buf, strlen(buf));
sprintf(buf,"#define LOCKED_TEXT_LEN %d\n", locked_text_len);
write(fh, buf, strlen(buf));
}
}
free(buffer);
if (src != -1) { close(src ); }
return True;
}
/************************************************************************
//
*************************************************************************/
static void InitImage(){
out_name_temp = (char *)malloc( strlen(TEMP_BASE)+5);
out_namet = (char *)malloc( strlen(TEMP_BASE)+3);
out_nameh = (char *)malloc( strlen(TEMP_BASE)+3);
sprintf(out_name_temp,"%s.tmp",TEMP_BASE);
sprintf(out_namet,"%s.t",TEMP_BASE);
sprintf(out_nameh,"%s.h",TEMP_BASE);
/* unlink the files just in case they already exist */
chmod(out_name_temp,0x1FF);
chmod(out_namet,0x1FF);
chmod(out_nameh,0x1FF);
unlink(out_name_temp);
unlink(out_namet);
unlink(out_nameh);
/* open/create specified file this is temp file */
fo = open(out_name_temp,O_RDWR|O_CREAT|O_BINARY,0x1FF);
if (fo == -1)
{
printf("%s: cannot create output file %s\n", progname, out_name_temp);
exit(1);
}
/* open temp output file #2 */
ft = open(out_namet,O_RDWR|O_CREAT|O_BINARY,0x1FF);
if (ft == -1)
{
printf("%s: cannot create temp output file %s\n", progname, out_namet);
return;
}
/* open temp output file #3 */
fh = open(out_nameh,O_RDWR|O_CREAT|O_BINARY,0x1FF);
if (fh == -1)
{
printf("%s: cannot create temp output file %s\n", progname, out_nameh);
return;
}
/* initialize the header file */
sprintf(buf,"#include \"tmlib/tmtypes.h\"\n\n");
write(fh, buf, strlen(buf));
sprintf(buf,"#define File 1\n#define Dir 0\n\n");
write(fh, buf, strlen(buf));
if(NoCompression == False)
sprintf(buf,"#define _COMPRESSION_ 1\n\n");
else
sprintf(buf,"#undef _COMPRESSION_ \n\n");
write(fh, buf, strlen(buf));
if(unshuffled == True)
sprintf(buf,"#define _PRE_UNSHUFFLED_ 1\n\n");
else
sprintf(buf,"#undef _PRE_UNSHUFFLED_ \n\n");
write(fh, buf, strlen(buf));
if(download_unshuffled == True)
sprintf(buf,"#define _DOWN_UNSHUFFLED_ 1\n\n");
else
sprintf(buf,"#undef _DOWN_UNSHUFFLED_ \n\n");
write(fh, buf, strlen(buf));
if(host_int == tmNoHost)
sprintf(buf,"#define _NOHOST_ 1\n\n");
else
sprintf(buf,"#undef _NOHOST_ \n\n");
write(fh, buf, strlen(buf));
if(verbose == True)
sprintf(buf,"#define _VERBOSE_ 1\n\n");
else
sprintf(buf,"#undef _VERBOSE_ \n\n");
write(fh, buf, strlen(buf));
/* if the input image is embedded as a *.out file */
if(out == True)
sprintf(buf,"#undef _MEMORY_IMAGE_ \n\n");
else
sprintf(buf,"#define _MEMORY_IMAGE_ 1\n\n");
write(fh, buf, strlen(buf));
sprintf(buf,"#define MEM_START 0x%x \n",mem_start);
write(fh, buf, strlen(buf));
sprintf(buf,"#define MEM_END 0x%x \n\n",mem_end);
write(fh, buf, strlen(buf));
sprintf(buf,"struct FileDirList\n{\n Int File_Dir; \n");
write(fh, buf, strlen(buf));
sprintf(buf," Char *Pathname; \n UInt32 FileLength;\n");
write(fh, buf, strlen(buf));
sprintf(buf," UInt32 CompLen;\n Byte *Data; \n};\n\n");
write(fh, buf, strlen(buf));
/* initialize temp output file (data struct file) */
sprintf(buf,"struct FileDirList FDList[FDLIST_ARRAY_LENGTH] =\n{");
write(fo, buf, strlen(buf));
/* initialize t file (data file) */
sprintf(buf,"{.data }\n{.align 4}\n");
write(ft, buf, strlen(buf));
}
/* copy the image file */
static int copy_image(char *inpath){
struct stat buf;
if (stat(inpath,&buf) == -1) printf("STAT ERROR \"%s\"\n", inpath);
if(copy_file(inpath)!= True)
return False;
NumElements++;
return True;
}
/* finish the image */
static void FinishImage(){
int size;
/* finish header file (Path file) */
sprintf(buf,"\n\n#define FDLIST_ARRAY_LENGTH %d\n\n",NumElements);
write(fh, buf, strlen(buf));
/* finish temp output file (data struct file) */
sprintf(buf,"\n};\n");
write(fo, buf, strlen(buf));
/* concatinate end of temp output file to header file */
/* seek to start of f2 */
lseek(fo,0,SEEK_SET);
/* copy from f2 to fo */
do {
size= read (fo, buf, BLOCKSIZE);
write (fh, buf, size);
} while (size > 0);
/* close temp output file */
if (fo) close(fo);
/* close t output file */
if (ft) close(ft);
/* close header output file */
if (fh) close(fh);
/* delete temp files */
unlink(out_name_temp);
/* change mode of output files */
chmod(out_namet,0x1FF);
chmod(out_nameh,0x1FF);
free(out_name_temp);
free(out_namet);
free(out_nameh);
}
static int ReadImage(void){
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -