Example #1
0
int dump_plot(const char *input_filename, const char *output_dir)
{
	char output_filename[_MAX_PATH];
	char drive[_MAX_DRIVE], dir[_MAX_DIR], fn[_MAX_FNAME];
	FILE *input_fp;
	pointer_struct ptr;
	int ret;
	
	if ((input_fp=fopen(input_filename, "rb"))== NULL)
	{
		printf("Could not open input file\n");
		return -1;
	}

	ptr.offset = 0;
	fseek(input_fp, 0, SEEK_END);
	ptr.size = ftell(input_fp);
	fseek(input_fp, 0, SEEK_SET);

	_splitpath(output_dir, drive, dir, fn, NULL);
	strcat(dir, fn);

	// generate output filename
	_makepath(output_filename, drive, dir, "plot", ".sjs");

	ret = dump_section(input_fp, &ptr, output_filename);
	fclose(input_fp);
	return ret;
}
static int dump_entry (FILE * f, struct ConfigVTable * h, SectionHandle s, 
      unsigned int indent, const SectionEntry * entry)
{
   int ret = 1;
   switch (entry->type)
   {
      case SE_SECTION:
         {
            SectionHandle newsec;

            fprintf (f, "%s { \n", entry->name);

            ret = mymin(ret, cf_openSection (h, s, entry->name, &newsec));
            if (ret >= 0) 
            {
               ret = mymin (dump_section (f, h, newsec, indent + 2), ret);
               show_indent (f, indent);
               fprintf (f, "}\n");
               ret = mymin (ret, cf_closeSection (h, newsec));
            }
            break;
         }
      case SE_KEY:
         {
            char buf[255];
            ret = mymin (ret, cf_getKey (h, s, entry->name, &buf[0], sizeof(buf)));
            if (ret >= 0)
            {
                fprintf (f, "%s = \"%s\";\n", entry->name, buf); 
            }
            break;
         }
      case SE_MULTIKEY:
         {
            char ** ptrs;
            size_t size;
            size_t j;
            ret = mymin (ret, cf_getMultiKey (h, s, entry->name, &ptrs, &size));
            if (ret >= 0)
            {
               fprintf (f,"%s = (", entry->name);
               for (j=0; j<size; ++j)
               {
                  fprintf (f,"\"%s\" ", (ptrs[j] ? ptrs[j] : ""));
                  free (ptrs[j]);
                  if ((j+1) < size)
                     fprintf (f,", ");
               }
               fprintf (f, ");\n");
               free (ptrs);
            }
         }
   }
   return ret;
}
int txtfile_writeConfig (struct ConfigVTable * cf, SectionHandle h, FILE * f, char ** err)
{
   int ret;
   assert(err);
   assert(f);

   *err = 0;

   if ((ret = dump_section (f, cf, h, 0)) < 0)
   {
      *err = strdup ("Error accessing config tree!");
      return ret;
   }

   return ret;
}
Example #4
0
int main(int argc, char* argv[])
{
  if (argc!=2) {
    show_usage();
    return 0;
  }

  //while(true) {
  MAPPED_FILE view = {0};
  if( 0 != map_file( argv[1], &view ) ) {
    printf( "open file failed: %s\n", argv[2]);
    return -1;
  }

  int pe = pe_open((const char*)view.data, view.size);
  if (pe == INVALID_PE) {
    printf( "file is not pe format");
    return -1;
  }

  dump_entry(pe);
  dump_version(pe);
  dump_section(pe);
  dump_export(pe);
  dump_import(pe);
  dump_overlay(pe);
  dump_resource(pe);

  char path[256] = {0};
  strcat(getcwd(path, sizeof(path) - 1), "\\sample.ico");
  dump_icon(pe, path);

  pe_close(pe);

  unmap_file(&view);
  //}
 
  system("pause");
  return 0;
}
Example #5
0
static void
_on_bus_message (GstBus * bus, GstMessage * message, GMainLoop * mainloop)
{
  /* g_printf ("Got message %s\n", GST_MESSAGE_TYPE_NAME (message)); */

  switch (GST_MESSAGE_TYPE (message)) {
    case GST_MESSAGE_ERROR:
    case GST_MESSAGE_EOS:
      g_main_loop_quit (mainloop);
      break;
    case GST_MESSAGE_ELEMENT:
    {
      GstMpegTsSection *section;
      if ((section = gst_message_parse_mpegts_section (message))) {
        g_print
            ("Got section: PID:0x%04x type:%s (table_id 0x%02x (%s)) at offset %"
            G_GUINT64_FORMAT "\n", section->pid,
            enum_name (GST_TYPE_MPEG_TS_SECTION_TYPE, section->section_type),
            section->table_id, enum_name (GST_TYPE_MPEG_TS_SECTION_TABLE_ID,
                section->table_id), section->offset);
        if (!section->short_section) {
          g_print
              ("   subtable_extension:0x%04x, version_number:0x%02x\n",
              section->subtable_extension, section->version_number);
          g_print
              ("   section_number:0x%02x last_section_number:0x%02x crc:0x%08x\n",
              section->section_number, section->last_section_number,
              section->crc);
        }
        dump_section (section);
        g_printf ("\n\n");
        gst_mpegts_section_unref (section);
      }
      break;
    }
    default:
      break;
  }
}
Example #6
0
int dump_d00(const char *input_filename, const char *output_dir)
{
	char drive[_MAX_DRIVE], dir[_MAX_DIR], fn[_MAX_FNAME];
	FILE *input_fp;
	int done = 0;
	unsigned long num_offsets=0;
	unsigned long i;
	pointer_struct *offset_data;

	// Main program
	if (fopen_s(&input_fp, input_filename, "rb") != 0)
	{
		printf("Could not open input file\n");
		return -1;
	}

	// figure out how many text sections to dump
	fread((void *)&num_offsets, 4, 1, input_fp);
	num_offsets = DoubleWordSwap(num_offsets);

	//   printf("number of offsets = %x - %d\n", num_offsets, num_offsets);

	// allocate and grab offset list
	if ((offset_data = (pointer_struct *)malloc(sizeof(pointer_struct) * num_offsets)) == NULL)
	{
		printf("Unable to allocate offset/size list\n");
		fclose(input_fp);
		return -2;
	}

	for (i = 0; i < num_offsets; i++)
	{
		fread((void *)&offset_data[i].offset, 4, 1, input_fp);
		fread((void *)&offset_data[i].size, 4, 1, input_fp);
		offset_data[i].offset = DoubleWordSwap(offset_data[i].offset) * 0x800;
		offset_data[i].size = DoubleWordSwap(offset_data[i].size);

		//      printf("offset_data[%d] offset = %08x size = %08x\n", i, offset_data[i].offset, offset_data[i].size);
	}

	for (i = 0; i < num_offsets; i++)
	{
		unsigned long temp_long=0;
		unsigned long temp_long2=0;

		// figure out the absolute offset of the text section
		fseek(input_fp, offset_data[i].offset, SEEK_SET);
		fread((void *)&temp_long, 4, 1, input_fp);
		temp_long = DoubleWordSwap(temp_long);

		fseek(input_fp, offset_data[i].offset + temp_long + 0x40, SEEK_SET);
		fread((void *)&temp_long2, 4, 1, input_fp);
		temp_long2 = DoubleWordSwap(temp_long2);

		// horray! Now we know the absolute offset of the text section!
		offset_data[i].offset += (temp_long + temp_long2);

		// Now grab the text section's size
		fseek(input_fp, offset_data[i].offset, SEEK_SET);
		fread((void *)&offset_data[i].size, 4, 1, input_fp);
		offset_data[i].size = DoubleWordSwap(offset_data[i].size);
	}

	// start a for loop that goes through each offset
	for (i = 0; i < num_offsets; i++)
	{
		char output_filename[_MAX_PATH];
		_splitpath(output_dir, drive, dir, fn, NULL);
		strcat(dir, fn);

		// generate output filename
		sprintf(fn, "scen%d\0", i + 1);
		_makepath(output_filename, drive, dir, fn, ".sjs");

		dump_section(input_fp, &offset_data[i], output_filename);
	}

	fclose(input_fp);
	free(offset_data);
	return 0;
}
Example #7
0
int main(int argc, char **argv)
{

    if ( argc < 2 )
    {
        printf("fltdump.exe filename.flt [-f]\n  -f = dump content of FLAT sections\n");
        return 1;
    }
    
    if ( argc > 2 && argv[2][0]=='-' && argv[2][1]=='f' )
	  { FLAG_DUMP_FLAT=1;}

    char* filename_flt = argv[1];

    int rv;
    if ( (rv=b_file_preload(filename_flt)) <= 0 )
    {
        fprintf(stderr, "Error load file '%s': loaded %d\n",filename_flt,rv);
        return 1;
    }


	flat = (struct flat_hdr*) b_get_buf();
	flat_buf = (unsigned char*)b_get_buf();

    char magic[5];          // "CFLA"
	memcpy(magic,flat->magic,4);
	magic[4]=0;


	printf("\nFLT Headers:\n");
	printf("->magic        %s (flat rev.%d)\n", magic, flat->rev );

	if ( memcmp(magic,FLAT_MAGIC_NUMBER,4) ) {
		printf("This is not CHDK-FLAT!\n");
		return 1;
	}	

	if ( flat->rev != FLAT_VERSION )
	{
		printf("Bad FLAT revision! It is %d while should be %d\n", flat->rev, FLAT_VERSION);
	}

	int flat_reloc_count;
	flat_reloc_count = (flat->import_start-flat->reloc_start)/sizeof(uint32_t);
	int flat_import_count;
	flat_import_count = (flat->file_size-flat->import_start)/sizeof(uint32_t);


	printf("->entry(.text) 0x%x (size %d)\n", flat->entry, flat->data_start - flat->entry );
	printf("->data_start   0x%x (size %d)\n", flat->data_start,  flat->bss_start - flat->data_start );
	printf("->bss_start    0x%x (size %d)\n", flat->bss_start, flat->reloc_start - flat->bss_start );
	printf("->reloc_start  0x%x (size %d)\n", flat->reloc_start, flat->import_start - flat->reloc_start );
	printf("->import_start 0x%x (size %d)\n", flat->import_start, flat->file_size - flat->import_start );
    printf("\n");

	if ( flat->rev == FLAT_VERSION )
	{
		struct ModuleInfo* _module_info = (struct ModuleInfo*)(flat_buf + flat->_module_info_offset);
		if ( _module_info->magicnum != MODULEINFO_V1_MAGICNUM ) 
		{
		  printf("Malformed module info - bad magicnum!\n");
		  return 1;
		}
		if ( _module_info->sizeof_struct != sizeof(struct ModuleInfo) ) 
		{
		  printf("Malformed module info - bad sizeof!\n");
		  return 1;
		}


		printf("\nModule info:\n");
		printf("->Module Name: %s\n", get_flat_string(_module_info->moduleName) );
		printf("->Module Ver: %d.%d\n", _module_info->module_version.major, _module_info->module_version.minor );
		
		char* branches_str[] = {"any branch","CHDK", "CHDK_DE", "CHDK_SDM", "PRIVATEBUILD"};
		int branch = (_module_info->chdk_required_branch>REQUIRE_CHDK_PRIVATEBUILD) ? 
							REQUIRE_CHDK_PRIVATEBUILD : _module_info->chdk_required_branch;
		printf("->Require: %s-build%d. ", branches_str[branch], _module_info->chdk_required_ver );
		if ( _module_info->chdk_required_platfid == 0 )
		  	printf("Any platform.\n");
		else
		  	printf(" Platform #%d only.\n", _module_info->chdk_required_platfid );
	    printf("->Description: %s\n", get_flat_string(_module_info->description) );
    	print_offs("->lib                 = ", (int)_module_info->lib,"\n");
	    //print_offs("->_module_loader()    = ", (int)_module_info->loader,"\n");
	    //print_offs("->_module_unloader()  = ", (int)_module_info->unloader,"\n");
	    //print_offs("->_module_can_unload()= ", (int)_module_info->can_unload,"\n");
	    //print_offs("->_module_exit_alt()  = ", (int)_module_info->exit_alt,"\n");
	}
		
	if ( !FLAG_DUMP_FLAT )
	  return 0;

    dump_section( "FLT_header", flat_buf, sizeof(struct flat_hdr) );
    dump_section( "FLT_text", flat_buf+flat->entry, flat->data_start-flat->entry );
    dump_section( "FLT_data", flat_buf+flat->data_start, flat->bss_start-flat->data_start);
    dump_section( "FLT_bss",  flat_buf+flat->bss_start, flat->reloc_start-flat->bss_start );

	int i;
    printf("\nDump relocations 0x%x (size=%d):\n",flat->reloc_start,flat_reloc_count*sizeof(uint32_t));
    for( i = 0; i< flat_reloc_count; i++)
        print_offs("Offs: ",*(int*)(flat_buf+flat->reloc_start+i*sizeof(uint32_t)),"\n");

    printf("\nDump imports 0x%x (size=%d):\n",flat->import_start,flat_import_count*sizeof(uint32_t));
    uint32_t *new_import_buf = (uint32_t*)(flat_buf+flat->import_start);
    for( i = 0; i< flat_import_count; i++)
    {
        uint32_t idx = new_import_buf[i++];
        int cnt = new_import_buf[i] >> 24;
        int j;
        for (j=0; j<cnt; j++)
        {
            uint32_t offs = new_import_buf[i++] & 0x00FFFFFF;
            print_offs((j==0)?"Offs: ":"      ",offs,"");
		    int addend = *(uint32_t*)(flat_buf+offs);
		    printf(" = sym_%08x[%s]+0x%x\n",idx,get_import_symbol(idx),addend);
        }
	}

	return 0;

}
Example #8
0
/*---------------------------------------------------------------------------*/
int
elfloader_load(char* filename, char* fltfile)
{
  struct elf32_ehdr ehdr;
  struct elf32_shdr shdr;
  struct elf32_shdr strtable;
  unsigned int strs;
  unsigned int shdrptr;
  unsigned int nameptr;
  char name[12];
  
  int i;
  unsigned short shdrnum, shdrsize;

  int ret;

  /* Ensure that we have a correct and compatible ELF header. */
  ret = b_seek_read( 0, (char *)&ehdr, sizeof(ehdr));
  if (ret != sizeof(ehdr)) return ELFFLT_INPUT_ERROR;

  if(memcmp(ehdr.e_ident, elf_magic_header, sizeof(elf_magic_header)) != 0) {
    	PRINTERR(stderr, "ELF header problems\n");
    return ELFFLT_BAD_ELF_HEADER;
  }

  if ( FLAG_VERBOSE )
    	printf ("Grab section header\n");

  // Grab the section header.
  shdrptr = ehdr.e_shoff;
  ret = b_seek_read( shdrptr, (char *)&shdr, sizeof(shdr));
  if (ret != sizeof(shdr)) return ELFFLT_INPUT_ERROR;
  
  shdrsize = ehdr.e_shentsize;
  shdrnum = ehdr.e_shnum;

  if ( FLAG_VERBOSE )
    	printf ("Grab string table section\n");

  // Grab the string table section for the names of the sections. 
  ret = b_seek_read( ehdr.e_shoff + shdrsize * ehdr.e_shstrndx,
                             (char *)&strtable, sizeof(strtable));
  if (ret != sizeof(strtable)) return ELFFLT_INPUT_ERROR;
  strs = strtable.sh_offset;

  /* Parse segments headers to releavant_section entries.
		.text = actual code from the ELF file
		.data = initialized data
		.rodata = contains read-only data
		.bss = segment holds the size of the unitialized data segment
		.rel.text, .rel.data = relocation information for the contents 
					of the ".text" and ".data" segments, respectively.
		.symtab = symbol table for this file
		.strtab = points to the actual string names used by the symbol table.
  */


  // Zero size is indicator of unitialized (not found) section
  text.size = text.relasize = data.size = data.relasize =
    rodata.size = rodata.relasize = symtabsize = strtabsize = 0;

  bss.number = data.number = rodata.number = text.number = -1;
                
  shdrptr = ehdr.e_shoff;
  for(i = 0; i < shdrnum; ++i) {

    ret = b_seek_read( shdrptr, (char *)&shdr, sizeof(shdr));
	DEBUGPRINTF("==shdrptr=0x%x, sizeof=%d; size=0x%x\n",shdrptr,sizeof(shdr),shdrsize );
    if (ret != sizeof(shdr)) { PRINTERR(stderr, "input error at %s:%d :loaded%d",__FILE__,__LINE__,ret);return ELFFLT_INPUT_ERROR;}
    
    /* The name of the section is contained in the strings table. */
    nameptr = strs + shdr.sh_name;
	DEBUGPRINTF("==nameptr=%x(%x+%x), size=%d\n",nameptr,strs,shdr.sh_name,sizeof(name) );
    ret = b_seek_read( nameptr, name, sizeof(name));
    if (ret != sizeof(name)) {PRINTERR(stderr, "input error at %s:%d",__FILE__,__LINE__); return ELFFLT_INPUT_ERROR;}

	DEBUGPRINTF("==shdrptr=0x%x, sizeof=%d; size=0x%x\n",shdrptr,sizeof(shdr),shdrsize );
	if ( FLAG_DUMP_SECTIONS )
		printf ("Section #%d: %-15s [section header 0x%x, offset=0x%x, size %d, vma=0x%x]\n",i,name,shdrptr,
													shdr.sh_offset,shdr.sh_size, shdr.sh_addr);

    if(strncmp(name, ".text", 5) == 0) {
      text.number = i;
      text.offset = shdr.sh_offset;
      text.size = shdr.sh_size;
	  text.base_addr = shdr.sh_addr;
    } else if(strncmp(name, ".rel.text", 9) == 0) {
      text.relaoff = shdr.sh_offset;
      text.relasize = shdr.sh_size;
    } else if(strncmp(name, ".data", 5) == 0) {
      data.number = i;
      data.offset = shdr.sh_offset;
      data.size = shdr.sh_size;
	  data.base_addr = shdr.sh_addr;
    } else if(strncmp(name, ".rodata", 7) == 0) {
      rodata.number = i;
      rodata.offset = shdr.sh_offset;
      rodata.size = shdr.sh_size;
	  rodata.base_addr = shdr.sh_addr;
    } else if(strncmp(name, ".rel.rodata", 11) == 0) {
      rodata.relaoff = shdr.sh_offset;
      rodata.relasize = shdr.sh_size;
    } else if(strncmp(name, ".rel.data", 9) == 0) {
      data.relaoff = shdr.sh_offset;
      data.relasize = shdr.sh_size;
    } else if(strncmp(name, ".rela.", 6) == 0) {
      PRINTERR(stderr,"RELA relocs are not supported.");
      return ELFFLT_INPUT_ERROR;
    } else if(strncmp(name, ".symtab", 7) == 0) {
      symtaboff = shdr.sh_offset;
      symtabsize = shdr.sh_size;
    } else if(strncmp(name, ".strtab", 7) == 0) {
      strtaboff = shdr.sh_offset;
      strtabsize = shdr.sh_size;
    } else if(strncmp(name, ".bss", 4) == 0) {
      bss.size = shdr.sh_size;
      bss.number = i;
      bss.offset = 0;
    }

    shdrptr += shdrsize;
  }

  if(symtabsize == 0) {
    PRINTERR(stderr,"No symbol table found.");
    return ELFFLT_NO_SYMTAB;
  }
  if(strtabsize == 0) {
    PRINTERR(stderr,"No strings table found.");
    return ELFFLT_NO_STRTAB;
  }
  if(text.size == 0) {
    PRINTERR(stderr, "No .text segment found.");
  	return ELFFLT_NO_TEXT;
  }

  if ( (text.relasize + rodata.relasize+ data.relasize) <=0 ) {
      PRINTERR(stderr,"Found no reloc sections. Please link with -r -d options.\n");
      return ELFFLT_UNHANDLED_RELOC;
  }

  if (bss.size) {
    bss.address = (char *)malloc(bss.size);
    if (!bss.address) return ELFFLT_OUTPUT_ERROR;
  }
  if (data.size) {
    data.address = (char *)malloc(data.size);
    if (!data.address) return ELFFLT_OUTPUT_ERROR;
  }
  if (text.size) {
    text.address = (char *)malloc(text.size);
    if (!text.address) return ELFFLT_OUTPUT_ERROR;
  }
  if (rodata.size) {
    rodata.address =  (char *)malloc(rodata.size);
    if (!rodata.address) return ELFFLT_OUTPUT_ERROR;
  }

  rodata.name=".rodata";
  bss.name=".bss";
  text.name=".text";
  data.name=".data";



  b_seek_read(text.offset, text.address, text.size);
  b_seek_read(data.offset, data.address, data.size);
  b_seek_read(rodata.offset, rodata.address, rodata.size);

  if ( FLAG_DUMP_SOURCE ) {
    dump_section( text.name, (unsigned char *)text.address, text.size );
    dump_section( data.name, (unsigned char *)data.address, data.size );
    dump_section( rodata.name, (unsigned char *)rodata.address, rodata.size );
  }

  if ( FLAG_DUMP_SYMBOLS ) {
    dump_symtable();
  }

  if ( FLAG_DUMP_SYMBOLS || FLAG_DUMP_SOURCE || FLAG_VERBOSE )
	 printf("\n\n");

  if ( FLAG_VERBOSE )
   	 printf ("Prepare flat\n");

  int div0hack_size = sizeof(div0_arm);

  int flatmainsize = sizeof(struct flat_hdr)+text.size+div0hack_size+data.size+rodata.size+bss.size;  
  int flatrelocsize = text.relasize+rodata.relasize+data.relasize;


  // Take to account aligning to int32 each section  
  flatmainsize += align4(text.size) + align4(data.size) + align4(rodata.size) + align4(bss.size);
  
  flat_buf=malloc( flatmainsize+flatrelocsize );      
  if ( !flat_buf) { PRINTERR(stderr, "fail to malloc flat buf\n"); return ELFFLT_OUTPUT_ERROR;}
  memset(flat_buf, 0, flatmainsize+flatrelocsize);
  
  //import is subset of full reloc list, so same count is enough
  // but apply multiplier to take into account difference between sizeofs
  flat_import_buf=malloc( flatrelocsize* sizeof(import_record_t)/sizeof(reloc_record_t) );      		
  if ( !flat_import_buf) { PRINTERR(stderr, "fail to malloc flat import buf\n"); return ELFFLT_OUTPUT_ERROR;}
  memset(flat_import_buf, 0, flatrelocsize);

  // Fill flat with sections aligned to int32

  flat = (struct flat_hdr*) flat_buf;

  if ( FLAG_VERBOSE )
      printf(">>elf2flt: load segments\n");
  int offset=sizeof(struct flat_hdr);
  text.flat_offset = offset;
  memcpy( flat_buf+offset, text.address, text.size );
  DEBUGPRINTF("load .txt to %x (%x->%x)\n",offset,text.size,text.size+align4(text.size));
  offset+=text.size+div0hack_size+align4(text.size);


  rodata.flat_offset = offset;
  DEBUGPRINTF("load .rodata to %x (%x->%x)\n",offset,rodata.size,rodata.size+align4(rodata.size));
  memcpy( flat_buf+offset, rodata.address, rodata.size );
  offset+=rodata.size+align4(rodata.size);

  data.flat_offset = offset;
  DEBUGPRINTF("load .data to %x (%x->%x)\n",offset,data.size,data.size+align4(data.size));
  memcpy( flat_buf+offset, data.address, data.size );
  offset+=data.size+align4(data.size);

  bss.flat_offset = offset;
  DEBUGPRINTF(".bss to %x (%x->%x)\n",offset,bss.size,bss.size+align4(bss.size));
  DEBUGPRINTF("result=%x\n",  flatmainsize);

  // Initialize flat headers
  memcpy(flat->magic, FLAT_MAGIC_NUMBER, sizeof(flat->magic));       // Set magic (CHDK_FLAT)
  flat->rev = FLAT_VERSION;
  flat->entry = text.flat_offset;
  flat->data_start = rodata.flat_offset;
  flat->bss_start = bss.flat_offset;  
  flat->reloc_start = flatmainsize;
  flat_reloc_count = 0;

  flat->import_start = 0;
  flat_import_count = 0;
  
  flat_reloc = (reloc_record_t*)(flat_buf+flatmainsize);  
  flat_reloc_cur = flat_reloc;

  flat_import_cur = flat_import_buf;

  // _div0_arm hack
  add_div0_arm();

  flag_unsafe_sym = 0;

  // Do relocations
  ret = relocate_section( &text);
  if(ret != ELFFLT_OK)
      return ret;  
  ret = relocate_section( &rodata);
  if(ret != ELFFLT_OK)
      return ret;
  ret = relocate_section( &data);
  if(ret != ELFFLT_OK)
      return ret;

  if ( flag_unsafe_sym )
      return ELFFLT_UNSAFE_SYMBOL;

  flat->import_start = flat->reloc_start+flat_reloc_count*sizeof(reloc_record_t);

  // Init offsets to the entry symbols
                  
  if ( FLAG_VERBOSE )
   	  printf(">>elf2flt: lookup entry symbols\n");

  flat->_module_info_offset = find_symbol_inflat("_module_info", &data );
  if ( flat->_module_info_offset <=0 ) {
    PRINTERR(stderr, "No or invalid section of _module_info. This symbol should be initialized as ModuleInfo structure.\n");
    return ELFFLT_NO_MODULEINFO;
  }

  struct ModuleInfo* _module_info = (struct ModuleInfo*) (flat_buf + flat->_module_info_offset);
  if ( _module_info->magicnum != MODULEINFO_V1_MAGICNUM ) 
  {
    PRINTERR(stderr, "Wrong _module_info->magicnum value. Please check correct filling of this structure\n");
    return ELFFLT_NO_MODULEINFO;
  }
  if ( _module_info->sizeof_struct != sizeof(struct ModuleInfo) ) 
  {
    PRINTERR(stderr, "Wrong _module_info->sizeof_struct value. Please check correct filling of this structure\n");
    return ELFFLT_NO_MODULEINFO;
  }

  // Group import relocations
  //  Input = array of offset/index pairs - one for each address to be relocated to a core CHDK symbol
  //  Output = list of entries of the form:
  //        Index, Offset1 | (N<<24), Offset2, ..., OffsetN
  //  where each offset is a reference to the same core CHDK symbol
  uint32_t *new_import_buf = malloc(flat_import_count*3*sizeof(uint32_t));
  uint32_t new_import_cnt = 0;
  int process = 1;
  while (process)
  {
      process = 0;
      for (i=0; i<flat_import_count; i++)
      {
          if (flat_import_buf[i].offs != 0)
          {
              process = 1;
              int cnt = 0;
              uint32_t idx = flat_import_buf[i].importidx;
              new_import_buf[new_import_cnt++] = idx;
              int pcnt = new_import_cnt;
              int j;
              for (j=0; j<flat_import_count; j++)
              {
                  if (flat_import_buf[j].importidx == idx)
                  {
                      new_import_buf[new_import_cnt++] = flat_import_buf[j].offs;
                      flat_import_buf[j].offs = 0;
                      cnt++;
                  }
              }
              new_import_buf[pcnt] = (cnt << 24) | new_import_buf[pcnt];
          }
      }
  }

  flat->file_size = flat->import_start+new_import_cnt*sizeof(uint32_t);

  if ( FLAG_DUMP_FLT_HEADERS ) {
	printf("\nFLT Headers:\n");
	printf("->entry        0x%x (size %d)\n", flat->entry, flat->data_start - flat->entry );
	printf("->data_start   0x%x (size %d)\n", flat->data_start,  flat->bss_start - flat->data_start );
	printf("->bss_start    0x%x (size %d)\n", flat->bss_start,   flat->reloc_start - flat->bss_start );
	printf("->reloc_start  0x%x (size %d)\n", flat->reloc_start, flat_reloc_count*sizeof(reloc_record_t) );
	printf("->import_start 0x%x (size %d %d)\n", flat->import_start, flat->file_size-flat->import_start, flat_import_count*sizeof(import_record_t) );
    printf("\n");

	printf("\nModule info:\n");
	printf("->Module Name: %s\n", get_flat_string(_module_info->moduleName) );
	printf("->Module Ver: %d.%d\n", _module_info->module_version.major, _module_info->module_version.minor );

	char* branches_str[] = {"any branch","CHDK", "CHDK_DE", "CHDK_SDM", "PRIVATEBUILD"};
	int branch = (_module_info->chdk_required_branch>REQUIRE_CHDK_PRIVATEBUILD) ? 
						REQUIRE_CHDK_PRIVATEBUILD : _module_info->chdk_required_branch;
	printf("->Require: %s-build%d. ", branches_str[branch], _module_info->chdk_required_ver );
	if ( _module_info->chdk_required_platfid == 0 )
	  	printf("Any platform.\n");
	else
	  	printf(" Platform #%d only.\n", _module_info->chdk_required_platfid );
	printf("->Description: %s\n", get_flat_string(_module_info->description) );
	print_offs("->lib                 = ", (int)_module_info->lib,"\n");
	//print_offs("->_module_loader()    = ", (int)_module_info->loader,"\n");
	//print_offs("->_module_unloader()  = ", (int)_module_info->unloader,"\n");
	//print_offs("->_module_can_unload()= ", (int)_module_info->can_unload,"\n");
	//print_offs("->_module_exit_alt()  = ", (int)_module_info->exit_alt,"\n");
  }

  if ( FLAG_DUMP_FLAT ) {
    dump_section( "FLT_header", (unsigned char*)flat_buf, sizeof(struct flat_hdr) );
    dump_section( "FLT_text", (unsigned char*)flat_buf+flat->entry, flat->data_start-flat->entry );
    dump_section( "FLT_data", (unsigned char*)flat_buf+flat->data_start, flat->bss_start-flat->data_start);
    dump_section( "FLT_bss",  (unsigned char*)flat_buf+flat->bss_start, flat->reloc_start-flat->bss_start );

    printf("\nDump relocations 0x%x (size=%d):\n",flat->reloc_start,flat_reloc_count*sizeof(reloc_record_t));
    for( i = 0; i< flat_reloc_count; i++)
    {
        print_offs("Offs: ",*(int*)(flat_buf+flat->reloc_start+i*sizeof(reloc_record_t)), "\n");
    }

    printf("\nDump imports 0x%x (size=%d):\n",flat->import_start,new_import_cnt*sizeof(uint32_t));
    for (i = 0; i< new_import_cnt;)
    {
        uint32_t idx = new_import_buf[i++];
        int cnt = new_import_buf[i] >> 24;
        int j;
        for (j=0; j<cnt; j++)
        {
            uint32_t offs = new_import_buf[i++] & 0x00FFFFFF;
            print_offs((j==0)?"Offs: ":"      ",offs,"");
		    int addend = *(uint32_t*)(flat_buf+offs);
		    printf(" = sym_%08x[%s]+0x%x\n",idx,get_import_symbol(idx),addend);
        }
    }
  }

  int filesize = flat->file_size;

  printf("\n\nOutput file %s (size=%d bytes)\n",fltfile,filesize);

  int output_fd = open(fltfile,O_WRONLY|O_CREAT|O_TRUNC|O_BINARY,0777);
  i = write(output_fd, flat_buf, flat->import_start);
  i = write(output_fd, new_import_buf, new_import_cnt*sizeof(uint32_t));
  close(output_fd);

  return ELFFLT_OK;
}
Example #9
0
void Command::dump_usage(std::ostream &out, bool show_hidden) const
{
    std::stringstream ss;

    out << std::endl;
    ss << get_name() << " - ";
    dump_short_description(ss, show_hidden);
    indented_dump(ss.str(), out);
    out << std::endl << std::endl;

    ss.str("");
    dump_section(out, "Synopsis", show_hidden);
    dump_synopsis(ss, show_hidden);
    indented_dump(ss.str(), out);
    out << std::endl;

    ss.str("");
    dump_section(out, "Description", show_hidden);
    dump_full_description(ss, show_hidden);
    indented_dump(ss.str(), out);
    out << std::endl;

    ss.str("");
    dump_output_section(ss, show_hidden);
    indented_dump(ss.str(), out);
    out << std::endl;

    dump_section(out, "Options", show_hidden);

    std::vector<std::string> str_opts;
    size_t max_width(0);

    for (CommandOptions::const_iterator it = _options.begin();
         it != _options.end(); ++it)
    {
        if (show_hidden == false && it->second->is_hidden())
            continue;
        std::stringstream ss;

        std::string short_opt("-");
        short_opt.append(1, it->second->get_short_opt());
        std::string long_opt("--");
        long_opt.append(it->second->get_long_opt());

        ss << "  ";
        ss << Boldify(short_opt)  << ", " << Boldify(long_opt);

        if (it->second->is_arg_expected())
            ss << " " << it->second->get_arg_name();

        str_opts.push_back(ss.str());
        size_t real_size = Get_real_string_size(ss.str());
        if (real_size > max_width)
            max_width = real_size;
    }

    size_t i(0);
    for (CommandOptions::const_iterator it = _options.begin();
         it != _options.end(); ++it)
    {
        if (show_hidden == false && it->second->is_hidden())
            continue;

        indented_dump(str_opts.at(i), out, 0, max_width + 2, true, true);

        std::stringstream ss;

        ss << it->second->get_description();
        if (it->second->is_arg_expected() &&
            it->second->is_mandatory() == false &&
            it->second->get_default_value().empty() == false)
            ss << std::endl << "Default is '" <<
            it->second->get_default_value() << "'.";

        indented_dump(ss.str(), out, max_width + 2,
                      Get_tty_cols(), false, false);
        out << std::endl;

        ++i;
    }
    out << std::endl;

    dump_section(out, "Examples", show_hidden);
    ss.str("");
    dump_examples(ss, show_hidden);
    indented_dump(ss.str(), out);
    out << std::endl;

    if (_see_also.empty() == false)
    {
        dump_section(out, "See also", show_hidden);
        ss.str("");
        dump_see_also(ss, show_hidden);
        indented_dump(ss.str(), out);
        out << std::endl;
        out << std::endl;
    }

    dump_section(out, "Author", show_hidden);
    ss.str("");
    ss << EXA_COPYRIGHT << std::endl;
    indented_dump(ss.str(), out);
    out << std::endl;
}
Example #10
0
int main(int argc, char *argv[])
{
	int fd;
	int exit_code = EXIT_SUCCESS;
	struct stat st;
	unsigned int size;
	uint8_t *buffer;
	uint8_t *buffer_iterator;
	struct actions_firmware_header header;
	struct actions_firmware_section section;
	unsigned int i = 0;
	int ret = 1;
	int err = 0;
	int dump = 1;
	const char *path_prefix;

	if (argc != 3) {
		usage(argv[0]);
		exit(1);
	}

	path_prefix = argv[2];
	err = mkdir(path_prefix, 0755);
	if (err < 0) {
		perror("mkdir");
		exit_code = EXIT_FAILURE;
		goto out;
	}

	fd = open(argv[1], O_RDONLY);
	if (fd < 0) {
		perror("open");
		exit_code = EXIT_FAILURE;
		goto out;
	}
	if (fstat(fd, &st) < 0) {
		perror("fstat");
		exit_code = EXIT_FAILURE;
		goto out_close_fd;
	}
	size = st.st_size;

	buffer = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
	if (buffer == NULL) {
		perror("mmap");
		exit_code = EXIT_FAILURE;
		goto out_close_fd;
	}

	if (size < ACTIONS_HEADER_DISK_SIZE) {
		fprintf(stderr, "File cannot contain an Actions firmware header.\n");
		exit_code = EXIT_FAILURE;
		goto out_munmap;
	}

	/* iterate over a copy of the pointer */
	buffer_iterator = buffer;

	err = parse_header(&buffer_iterator, &header);
	if (err) {
		fprintf(stderr, "Cannot parse the header.\n");
		exit_code = EXIT_FAILURE;
		goto out_munmap;
	}

	print_header(&header);

	while((err = parse_section(&buffer_iterator, &section)) == 0) {
		print_section(&section);
		i++;

		if (strncmp((char *)section.name, "FIRM", 16) == 0) {
			uint8_t *firm_buffer_iterator = buffer + section.start_address + 0x200;
			struct actions_firmware_section firm_section;
			struct actions_firmware_section prev_section = {
				.start_address = section.start_address + 0x2000,
				.length = 0,
			};
			while((err = parse_section(&firm_buffer_iterator, &firm_section)) == 0) {

				/*
				 * unknown1 seems to be some form of checksum for
				 * firm sections, if a sections have the same
				 * checksum of the previous on they are not
				 * duplicated but refer to the same memory
				 * region, so do not increase che start
				 * address
				 */
				if (firm_section.unknown1 == prev_section.unknown1) {
					firm_section.start_address = prev_section.start_address;
				} else {
					firm_section.start_address = prev_section.start_address + prev_section.length;
				}

				printf("\t");
				print_section(&firm_section);

				if (dump)
					dump_section(path_prefix, "FIRM_", buffer, &firm_section);

				prev_section = firm_section;
			}
		} else if (strncmp((char *)section.name, "LINUX", 16) == 0) {
			continue;
		}

		if (dump)
			dump_section(path_prefix, "", buffer, &section);

	}