// File: /workspace/github_c_makefile_star2_O3/evilwombat[P]gopro-fw-tools/fwparser.c
// Function: main
int main(int argc, char **argv)
{
	int verbose = 0;
	int ret = 0;
	unsigned int crc, version, build_date, flags, magic;
	unsigned int section_offset, num = 0;
	int length;
	char *fname;

	if (argc != 2) {
		printf("Usage: %s [firmware_file]\n", argv[0]);
		return -1;
	}

	fname = argv[1];
	
	fd = fopen(fname, "rb");
	if (!fd) {
		printf("Could not open %s\n", fname);
		return -1;
	}

	while (1) {
		ret = find_magic();
		if (ret < 0) {
			printf("# End of file reached.\n");
			fclose(fd);
			return 0;
		}
	
		fseek(fd, -28, SEEK_CUR);
		crc = read_word();
		version = read_word();
		build_date = read_word();
		length = read_word();
		read_word();
		flags = read_word();
		magic = read_word();
		fseek(fd, 0x100-28, SEEK_CUR);
		section_offset = ftell(fd);
		fseek(fd, length, SEEK_CUR);

		if (length < 0)
			continue;

		if (verbose)
		{
			fprintf(stderr, "Section found\n");
			fprintf(stderr, "\tCRC\t= %08x\n", crc);
			fprintf(stderr, "\tVersion = %08x\n", version);
			fprintf(stderr, "\tBuild\t= %08x\n", build_date);
			fprintf(stderr, "\tLength\t= %08x\n", length);
			fprintf(stderr, "\tFlags\t= %08x\n", flags);
			fprintf(stderr, "\tMagic\t= %08x\n", magic);
		}

		printf("# section_%d at offset %d length %d CRC 0x%08x\n",
			num, section_offset, length, crc);
#ifdef _LINUX
		printf("dd if=$1 bs=%d skip=1 | dd iflag=fullblock of=section_%d bs=%d count=1\n",
			section_offset, num, length);
#else
		printf("dd if=$1 skip=%d conv=notrunc of=section_%d count=%d bs=1\n",
			section_offset, num, length);
#endif
		printf("\n");
		num++;
	}

	fclose(fd);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
cmpl $0x2, %edi
jne 0x1355
movq 0x8(%rsi), %rbx
leaq 0xe54(%rip), %rsi       # 0x202f
movq %rbx, %rdi
callq 0x10b0
movq %rax, 0x2e8e(%rip)      # 0x4078
testq %rax, %rax
je 0x137d
leaq 0xe0a(%rip), %r15       # 0x2004
movl $0x1, %r13d
leaq 0xe6e(%rip), %r14       # 0x2075
xorl %ebp, %ebp
movq 0x2e68(%rip), %rdi      # 0x4078
callq 0x1090
testl %eax, %eax
jne 0x1361
xorl %r12d, %r12d
movq 0x2e51(%rip), %rdi      # 0x4078
callq 0x1070
testl %eax, %eax
js 0x1361
movl %r12d, %ecx
movslq (%r15,%rcx,4), %rcx
addq %r15, %rcx
jmpq *%rcx
xorl %r12d, %r12d
cmpl $0x90, %eax
sete %r12b
jmp 0x1277
xorl %ecx, %ecx
cmpl $0x24, %eax
sete %cl
leal (%rcx,%rcx,2), %r12d
jmp 0x1277
xorl %r12d, %r12d
cmpl $0xa3, %eax
jne 0x1277
jmp 0x1295
xorl %r12d, %r12d
cmpl $0xeb, %eax
sete %r12b
addl %r12d, %r12d
cmpl $0x90, %eax
cmovel %r13d, %r12d
movq 0x2df1(%rip), %rdi      # 0x4078
callq 0x1090
testl %eax, %eax
je 0x1220
jmp 0x1361
movq 0x2ddc(%rip), %rdi      # 0x4078
movq $-0x1c, %rsi
movl $0x1, %edx
callq 0x10a0
callq 0x13a2
movl %eax, 0x4(%rsp)
callq 0x13a2
callq 0x13a2
callq 0x13a2
movl %eax, %r12d
callq 0x13a2
callq 0x13a2
callq 0x13a2
movq 0x2d9a(%rip), %rdi      # 0x4078
movl $0xe4, %esi
movl $0x1, %edx
callq 0x10a0
movq 0x2d84(%rip), %rdi      # 0x4078
callq 0x1080
movq %rax, %rbx
movq 0x2d75(%rip), %rdi      # 0x4078
movslq %r12d, %rsi
movl $0x1, %edx
callq 0x10a0
testl %r12d, %r12d
js 0x1209
leaq 0xd25(%rip), %rdi       # 0x2045
movl %ebp, %esi
movl %ebx, %edx
movl %r12d, %ecx
movl 0x4(%rsp), %r8d
xorl %eax, %eax
callq 0x1060
movq %r14, %rdi
movl %ebx, %esi
movl %ebp, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x1060
movl $0xa, %edi
callq 0x1030
incl %ebp
jmp 0x1209
movq (%rsi), %rsi
leaq 0xcb5(%rip), %rdi       # 0x2014
jmp 0x1387
leaq 0xd48(%rip), %rdi       # 0x20b0
callq 0x1040
movq 0x2d04(%rip), %rdi      # 0x4078
callq 0x1050
xorl %eax, %eax
jmp 0x1393
leaq 0xcae(%rip), %rdi       # 0x2032
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1060
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq