int nxflat_main(int argc, char *argv[]) { struct binary_s bin; int ret; int i; /* Initialize the NXFLAT binary loader */ message("Initializing the NXFLAT binary loader\n"); ret = nxflat_initialize(); if (ret < 0) { err("ERROR: Initialization of the NXFLAT loader failed: %d\n", ret); exit(1); } /* Create a ROM disk for the ROMFS filesystem */ message("Registering romdisk\n"); ret = romdisk_register(0, romfs_img, NSECTORS(romfs_img_len), SECTORSIZE); if (ret < 0) { err("ERROR: romdisk_register failed: %d\n", ret); nxflat_uninitialize(); exit(1); } /* Mount the file system */ message("Mounting ROMFS filesystem at target=%s with source=%s\n", MOUNTPT, ROMFSDEV); ret = mount(ROMFSDEV, MOUNTPT, "romfs", MS_RDONLY, NULL); if (ret < 0) { err("ERROR: mount(%s,%s,romfs) failed: %s\n", ROMFSDEV, MOUNTPT, errno); nxflat_uninitialize(); } /* Now excercise every progrm in the ROMFS file system */ for (i = 0; dirlist[i]; i++) { testheader(dirlist[i]); memset(&bin, 0, sizeof(struct binary_s)); snprintf(path, 128, "%s/%s", MOUNTPT, dirlist[i]); bin.filename = path; bin.exports = exports; bin.nexports = NEXPORTS; ret = load_module(&bin); if (ret < 0) { err("ERROR: Failed to load program '%s'\n", dirlist[i]); exit(1); } ret = exec_module(&bin, 50); if (ret < 0) { err("ERROR: Failed to execute program '%s'\n", dirlist[i]); unload_module(&bin); } message("Wait a bit for test completion\n"); sleep(4); } message("End-of-Test.. Exit-ing\n"); return 0; }
int elf_main(int argc, char *argv[]) #endif { struct binary_s bin; int ret; int i; /* Initialize the memory monitor */ mm_initmonitor(); /* Initialize the ELF binary loader */ message("Initializing the ELF binary loader\n"); ret = elf_initialize(); if (ret < 0) { err("ERROR: Initialization of the ELF loader failed: %d\n", ret); exit(1); } mm_update(&g_mmstep, "after elf_initialize"); /* Create a ROM disk for the ROMFS filesystem */ message("Registering romdisk at /dev/ram%d\n", CONFIG_EXAMPLES_ELF_DEVMINOR); ret = romdisk_register(CONFIG_EXAMPLES_ELF_DEVMINOR, (FAR uint8_t *)romfs_img, NSECTORS(romfs_img_len), SECTORSIZE); if (ret < 0) { err("ERROR: romdisk_register failed: %d\n", ret); elf_uninitialize(); exit(1); } mm_update(&g_mmstep, "after romdisk_register"); /* Mount the file system */ message("Mounting ROMFS filesystem at target=%s with source=%s\n", MOUNTPT, CONFIG_EXAMPLES_ELF_DEVPATH); ret = mount(CONFIG_EXAMPLES_ELF_DEVPATH, MOUNTPT, "romfs", MS_RDONLY, NULL); if (ret < 0) { err("ERROR: mount(%s,%s,romfs) failed: %s\n", CONFIG_EXAMPLES_ELF_DEVPATH, MOUNTPT, errno); elf_uninitialize(); } mm_update(&g_mmstep, "after mount"); /* Does the system support the PATH variable? Has the PATH variable * already been set? If YES and NO, then set the PATH variable to * the ROMFS mountpoint. */ #if defined(CONFIG_BINFMT_EXEPATH) && !defined(CONFIG_PATH_INITIAL) (void)setenv("PATH", MOUNTPT, 1); #endif /* Now excercise every program in the ROMFS file system */ for (i = 0; dirlist[i]; i++) { /* Output a seperated so that we can clearly discrinmate the output of * this program from the others. */ testheader(dirlist[i]); /* Initialize the binary_s structure */ memset(&bin, 0, sizeof(struct binary_s)); /* If the binary loader does not support the PATH variable, then * create the full path to the executable program. Otherwise, * use the relative path so that the binary loader will have to * search the PATH variable to find the executable. */ #ifdef CONFIG_BINFMT_EXEPATH bin.filename = dirlist[i]; #else snprintf(fullpath, 128, "%s/%s", MOUNTPT, dirlist[i]); bin.filename = fullpath; #endif bin.exports = exports; bin.nexports = nexports; /* Load the ELF module */ ret = load_module(&bin); if (ret < 0) { err("ERROR: Failed to load program '%s'\n", dirlist[i]); exit(1); } mm_update(&g_mmstep, "after load_module"); /* Execute the ELF module */ ret = exec_module(&bin); mm_update(&g_mmstep, "after exec_module"); if (ret < 0) { err("ERROR: Failed to execute program '%s'\n", dirlist[i]); } else { message("Wait a bit for test completion\n"); sleep(4); } unload_module(&bin); mm_update(&g_mmstep, "after unload_module"); } mm_update(&g_mmstep, "End-of-Test"); return 0; }
int elf_main(int argc, char *argv[]) { struct binary_s bin; int ret; int i; /* Initialize the memory monitor */ mm_initmonitor(); /* Initialize the ELF binary loader */ message("Initializing the ELF binary loader\n"); ret = elf_initialize(); if (ret < 0) { err("ERROR: Initialization of the ELF loader failed: %d\n", ret); exit(1); } mm_update(&g_mmstep, "after elf_initialize"); /* Create a ROM disk for the ROMFS filesystem */ message("Registering romdisk at /dev/ram%d\n", CONFIG_EXAMPLES_ELF_DEVMINOR); ret = romdisk_register(CONFIG_EXAMPLES_ELF_DEVMINOR, (FAR uint8_t *)romfs_img, NSECTORS(romfs_img_len), SECTORSIZE); if (ret < 0) { err("ERROR: romdisk_register failed: %d\n", ret); elf_uninitialize(); exit(1); } mm_update(&g_mmstep, "after romdisk_register"); /* Mount the file system */ message("Mounting ROMFS filesystem at target=%s with source=%s\n", MOUNTPT, CONFIG_EXAMPLES_ELF_DEVPATH); ret = mount(CONFIG_EXAMPLES_ELF_DEVPATH, MOUNTPT, "romfs", MS_RDONLY, NULL); if (ret < 0) { err("ERROR: mount(%s,%s,romfs) failed: %s\n", CONFIG_EXAMPLES_ELF_DEVPATH, MOUNTPT, errno); elf_uninitialize(); } mm_update(&g_mmstep, "after mount"); /* Now excercise every program in the ROMFS file system */ for (i = 0; dirlist[i]; i++) { testheader(dirlist[i]); memset(&bin, 0, sizeof(struct binary_s)); snprintf(path, 128, "%s/%s", MOUNTPT, dirlist[i]); bin.filename = path; bin.exports = exports; bin.nexports = nexports; ret = load_module(&bin); if (ret < 0) { err("ERROR: Failed to load program '%s'\n", dirlist[i]); exit(1); } mm_update(&g_mmstep, "after load_module"); ret = exec_module(&bin, 50); mm_update(&g_mmstep, "after exec_module"); if (ret < 0) { err("ERROR: Failed to execute program '%s'\n", dirlist[i]); } else { message("Wait a bit for test completion\n"); sleep(4); } unload_module(&bin); mm_update(&g_mmstep, "after unload_module"); } mm_update(&g_mmstep, "End-of-Test"); return 0; }
int iriver_decode(const char *infile_name, const char *outfile_name, BOOL modify, enum striptype stripmode ) { FILE * infile = NULL; FILE * outfile = NULL; int i = -1; unsigned char headerdata[512]; unsigned long dwLength1, dwLength2, dwLength3, fp = 0; unsigned char blockdata[16+16]; unsigned char out[16]; unsigned char newmunge; signed long lenread; int s = 0; unsigned char * pChecksums, * ppChecksums = 0; unsigned char ck; infile = openinfile(infile_name); outfile = openoutfile(outfile_name); lenread = fread( headerdata, 1, 512, infile ); if( lenread != 512 ) { fprintf( stderr, "This doesn't look like a valid encrypted iHP " "firmware - reason: header length\n" ); fclose(infile); fclose(outfile); return -1; }; i = testheader( headerdata ); if( i == -1 ) { fprintf( stderr, "This firmware is for an unknown model, or is not" " a valid encrypted iHP firmware\n" ); fclose(infile); fclose(outfile); return -2; }; fprintf( stderr, "Model %s\n", models[ i ] ); dwLength1 = headerdata[0] | (headerdata[1]<<8) | (headerdata[2]<<16) | (headerdata[3]<<24); dwLength2 = headerdata[4] | (headerdata[5]<<8) | (headerdata[6]<<16) | (headerdata[7]<<24); dwLength3 = headerdata[8] | (headerdata[9]<<8) | (headerdata[10]<<16) | (headerdata[11]<<24); if( dwLength1 < firmware_minsize[ i ] || dwLength1 > firmware_maxsize[ i ] || dwLength2 < firmware_minsize[ i ] || dwLength2 > dwLength1 || dwLength3 > dwLength1 || dwLength2>>9 != dwLength3 || dwLength2+dwLength3+512 != dwLength1 ) { fprintf( stderr, "This doesn't look like a valid encrypted " "iHP firmware - reason: file 'length' data\n" ); fclose(infile); fclose(outfile); return -3; }; pChecksums = ppChecksums = (unsigned char *)( malloc( dwLength3 ) ); if( modify ) { modifyheader( headerdata ); }; if( stripmode == STRIP_NONE ) fwrite( headerdata, 512, 1, outfile ); memset( blockdata, 0, 16 ); ck = 0; while( ( fp < dwLength2 ) && ( lenread = fread( blockdata+16, 1, 16, infile ) ) == 16 ) { fp += 16; for( i=0; i<16; ++i ) { newmunge = blockdata[16+i] ^ munge[i]; out[i] = newmunge ^ blockdata[i]; blockdata[i] = newmunge; ck += out[i]; } if( fp > ESTF_SIZE || stripmode != STRIP_HEADER_CHECKSUM_ESTF ) { fwrite( out+4, 1, 12, outfile ); fwrite( out, 1, 4, outfile ); } else { if( ESTF_SIZE - fp < 16 ) { memcpy( out+4, blockdata+16, 12 ); memcpy( out, blockdata+28, 4 ); fwrite( blockdata+16+ESTF_SIZE-fp, 1, ESTF_SIZE-fp, outfile ); } } if( s == 496 ) { s = 0; memset( blockdata, 0, 16 ); *ppChecksums++ = ck; ck = 0; } else s+=16; }; if( fp != dwLength2 ) { fprintf( stderr, "This doesn't look like a valid encrypted " "iHP firmware - reason: 'length2' mismatch\n" ); fclose(infile); fclose(outfile); return -4; }; fp = 0; ppChecksums = pChecksums; while( ( fp < dwLength3 ) && ( lenread = fread( blockdata, 1, 32, infile ) ) > 0 ) { fp += lenread; if( stripmode == STRIP_NONE ) fwrite( blockdata, 1, lenread, outfile ); if( memcmp( ppChecksums, blockdata, lenread ) != 0 ) { fprintf( stderr, "This doesn't look like a valid encrypted " "iHP firmware - reason: Checksum mismatch!" ); fclose(infile); fclose(outfile); return -5; }; ppChecksums += lenread; }; if( fp != dwLength3 ) { fprintf( stderr, "This doesn't look like a valid encrypted " "iHP firmware - reason: 'length3' mismatch\n" ); fclose(infile); fclose(outfile); return -6; }; fprintf( stderr, "File decoded correctly and all checksums matched!\n" ); switch( stripmode ) { default: case STRIP_NONE: fprintf(stderr, "Output file contains all headers and " "checksums\n"); break; case STRIP_HEADER_CHECKSUM: fprintf( stderr, "NB: output file contains only ESTFBINR header" " and decoded firmware code\n" ); break; case STRIP_HEADER_CHECKSUM_ESTF: fprintf( stderr, "NB: output file contains only raw decoded " "firmware code\n" ); break; }; return 0; }
int iriver_encode(const char *infile_name, const char *outfile_name, BOOL modify ) { FILE * infile = NULL; FILE * outfile = NULL; int i = -1; unsigned char headerdata[512]; unsigned long dwLength1, dwLength2, dwLength3, fp = 0; unsigned char blockdata[16+16]; unsigned char out[16]; unsigned char newmunge; signed long lenread; int s = 0; unsigned char * pChecksums, * ppChecksums; unsigned char ck; infile = openinfile(infile_name); outfile = openoutfile(outfile_name); lenread = fread( headerdata, 1, 512, infile ); if( lenread != 512 ) { fprintf( stderr, "This doesn't look like a valid decoded " "iHP firmware - reason: header length\n" ); fclose(infile); fclose(outfile); return -1; }; if( modify ) { modifyheader( headerdata ); /* reversible */ }; i = testheader( headerdata ); if( i == -1 ) { fprintf( stderr, "This firmware is for an unknown model, or is not" " a valid decoded iHP firmware\n" ); fclose(infile); fclose(outfile); return -2; }; fprintf( stderr, "Model %s\n", models[ i ] ); dwLength1 = headerdata[0] | (headerdata[1]<<8) | (headerdata[2]<<16) | (headerdata[3]<<24); dwLength2 = headerdata[4] | (headerdata[5]<<8) | (headerdata[6]<<16) | (headerdata[7]<<24); dwLength3 = headerdata[8] | (headerdata[9]<<8) | (headerdata[10]<<16) | (headerdata[11]<<24); if( dwLength1 < firmware_minsize[i] || dwLength1 > firmware_maxsize[i] || dwLength2 < firmware_minsize[i] || dwLength2 > dwLength1 || dwLength3 > dwLength1 || dwLength2+dwLength3+512 != dwLength1 ) { fprintf( stderr, "This doesn't look like a valid decoded iHP" " firmware - reason: file 'length' data\n" ); fclose(infile); fclose(outfile); return -3; }; pChecksums = ppChecksums = (unsigned char *)( malloc( dwLength3 ) ); fwrite( headerdata, 512, 1, outfile ); memset( blockdata, 0, 16 ); ck = 0; while( ( fp < dwLength2 ) && ( lenread = fread( blockdata+16, 1, 16, infile ) ) == 16 ) { fp += 16; for( i=0; i<16; ++i ) { newmunge = blockdata[16+((12+i)&0xf)] ^ blockdata[i]; out[i] = newmunge ^ munge[i]; ck += blockdata[16+i]; blockdata[i] = newmunge; }; fwrite( out, 1, 16, outfile ); if( s == 496 ) { s = 0; memset( blockdata, 0, 16 ); *ppChecksums++ = ck; ck = 0; } else s+=16; }; if( fp != dwLength2 ) { fprintf( stderr, "This doesn't look like a valid decoded " "iHP firmware - reason: 'length1' mismatch\n" ); fclose(infile); fclose(outfile); return -4; }; /* write out remainder w/out applying descrambler */ fp = 0; lenread = dwLength3; ppChecksums = pChecksums; while( ( fp < dwLength3) && ( lenread = fwrite( ppChecksums, 1, lenread, outfile ) ) > 0 ) { fp += lenread; ppChecksums += lenread; lenread = dwLength3 - fp; }; if( fp != dwLength3 ) { fprintf( stderr, "This doesn't look like a valid decoded " "iHP firmware - reason: 'length2' mismatch\n" ); fclose(infile); fclose(outfile); return -5; }; fprintf( stderr, "File encoded successfully and checksum table built!\n" ); fclose(infile); fclose(outfile); return 0; }
int spawn_main(int argc, char *argv[]) { posix_spawn_file_actions_t file_actions; posix_spawnattr_t attr; FAR const char *filepath; pid_t pid; int ret; /* Initialize the memory monitor */ mm_initmonitor(); /* Initialize the ELF binary loader */ message("Initializing the ELF binary loader\n"); ret = elf_initialize(); if (ret < 0) { err("ERROR: Initialization of the ELF loader failed: %d\n", ret); exit(1); } mm_update(&g_mmstep, "after elf_initialize"); /* Create a ROM disk for the ROMFS filesystem */ message("Registering romdisk at /dev/ram%d\n", CONFIG_EXAMPLES_ELF_DEVMINOR); ret = romdisk_register(CONFIG_EXAMPLES_ELF_DEVMINOR, (FAR uint8_t *)romfs_img, NSECTORS(romfs_img_len), SECTORSIZE); if (ret < 0) { err("ERROR: romdisk_register failed: %d\n", ret); elf_uninitialize(); exit(1); } mm_update(&g_mmstep, "after romdisk_register"); /* Mount the file system */ message("Mounting ROMFS filesystem at target=%s with source=%s\n", MOUNTPT, CONFIG_EXAMPLES_ELF_DEVPATH); ret = mount(CONFIG_EXAMPLES_ELF_DEVPATH, MOUNTPT, "romfs", MS_RDONLY, NULL); if (ret < 0) { err("ERROR: mount(%s,%s,romfs) failed: %s\n", CONFIG_EXAMPLES_ELF_DEVPATH, MOUNTPT, errno); elf_uninitialize(); } mm_update(&g_mmstep, "after mount"); /* Does the system support the PATH variable? Has the PATH variable * already been set? If YES and NO, then set the PATH variable to * the ROMFS mountpoint. */ #if defined(CONFIG_BINFMT_EXEPATH) && !defined(CONFIG_PATH_INITIAL) (void)setenv("PATH", MOUNTPT, 1); #endif /* Make sure that we are using our symbol take */ exec_setsymtab(exports, nexports); /************************************************************************* * Case 1: Simple program with arguments *************************************************************************/ /* Output a seperated so that we can clearly discriminate the output of * this program from the others. */ testheader(g_hello); /* Initialize the attributes file actions structure */ ret = posix_spawn_file_actions_init(&file_actions); if (ret != 0) { err("ERROR: posix_spawn_file_actions_init failed: %d\n", ret); } posix_spawn_file_actions_dump(&file_actions); ret = posix_spawnattr_init(&attr); if (ret != 0) { err("ERROR: posix_spawnattr_init failed: %d\n", ret); } posix_spawnattr_dump(&attr); mm_update(&g_mmstep, "after file_action/attr init"); /* If the binary loader does not support the PATH variable, then * create the full path to the executable program. Otherwise, * use the relative path so that the binary loader will have to * search the PATH variable to find the executable. */ #ifdef CONFIG_BINFMT_EXEPATH filepath = g_hello; #else snprintf(fullpath, 128, "%s/%s", MOUNTPT, g_hello); filepath = fullpath; #endif /* Execute the program */ mm_update(&g_mmstep, "before posix_spawn"); ret = posix_spawn(&pid, filepath, &file_actions, &attr, NULL, (FAR char * const*)&g_argv); if (ret != 0) { err("ERROR: posix_spawn failed: %d\n", ret); } sleep(4); mm_update(&g_mmstep, "after posix_spawn"); /* Free attibutes and file actions */ ret = posix_spawn_file_actions_destroy(&file_actions); if (ret != 0) { err("ERROR: posix_spawn_file_actions_destroy failed: %d\n", ret); } posix_spawn_file_actions_dump(&file_actions); ret = posix_spawnattr_destroy(&attr); if (ret != 0) { err("ERROR: posix_spawnattr_destroy failed: %d\n", ret); } posix_spawnattr_dump(&attr); mm_update(&g_mmstep, "after file_action/attr destruction"); /************************************************************************* * Case 2: Simple program with redirection of stdin to a file input *************************************************************************/ /* Output a seperated so that we can clearly discriminate the output of * this program from the others. */ testheader(g_redirect); /* Initialize the attributes file actions structure */ ret = posix_spawn_file_actions_init(&file_actions); if (ret != 0) { err("ERROR: posix_spawn_file_actions_init failed: %d\n", ret); } posix_spawn_file_actions_dump(&file_actions); ret = posix_spawnattr_init(&attr); if (ret != 0) { err("ERROR: posix_spawnattr_init failed: %d\n", ret); } posix_spawnattr_dump(&attr); mm_update(&g_mmstep, "after file_action/attr init"); /* Set up to close stdin (0) and open testdata.txt as the program input */ ret = posix_spawn_file_actions_addclose(&file_actions, 0); if (ret != 0) { err("ERROR: posix_spawn_file_actions_addclose failed: %d\n", ret); } posix_spawn_file_actions_dump(&file_actions); snprintf(fullpath, 128, "%s/%s", MOUNTPT, g_data); ret = posix_spawn_file_actions_addopen(&file_actions, 0, fullpath, O_RDONLY, 0644); if (ret != 0) { err("ERROR: posix_spawn_file_actions_addopen failed: %d\n", ret); } posix_spawn_file_actions_dump(&file_actions); mm_update(&g_mmstep, "after adding file_actions"); /* If the binary loader does not support the PATH variable, then * create the full path to the executable program. Otherwise, * use the relative path so that the binary loader will have to * search the PATH variable to find the executable. */ #ifdef CONFIG_BINFMT_EXEPATH filepath = g_redirect; #else snprintf(fullpath, 128, "%s/%s", MOUNTPT, g_redirect); filepath = fullpath; #endif /* Execute the program */ mm_update(&g_mmstep, "before posix_spawn"); ret = posix_spawn(&pid, filepath, &file_actions, &attr, NULL, NULL); if (ret != 0) { err("ERROR: posix_spawn failed: %d\n", ret); } sleep(2); mm_update(&g_mmstep, "after posix_spawn"); /* Free attibutes and file actions */ ret = posix_spawn_file_actions_destroy(&file_actions); if (ret != 0) { err("ERROR: posix_spawn_file_actions_destroy failed: %d\n", ret); } posix_spawn_file_actions_dump(&file_actions); ret = posix_spawnattr_destroy(&attr); if (ret != 0) { err("ERROR: posix_spawnattr_destroy failed: %d\n", ret); } posix_spawnattr_dump(&attr); mm_update(&g_mmstep, "after file_action/attr destruction"); /* Clean-up */ elf_uninitialize(); mm_update(&g_mmstep, "End-of-Test"); return 0; }