// File: /workspace/github_c_makefile_star2_O1/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 0x1371
movq 0x8(%rsi), %rbx
leaq 0xe44(%rip), %rsi       # 0x202f
movq %rbx, %rdi
callq 0x10c0
movq %rax, 0x2e86(%rip)      # 0x4080
testq %rax, %rax
je 0x137d
leaq 0xdfa(%rip), %r13       # 0x2004
movl $0x1, %ebx
leaq 0xe5f(%rip), %r14       # 0x2075
xorl %ebp, %ebp
movq 0x2e61(%rip), %rdi      # 0x4080
callq 0x10a0
testl %eax, %eax
jne 0x13b0
xorl %r15d, %r15d
movq 0x2e4a(%rip), %rdi      # 0x4080
callq 0x1070
testl %eax, %eax
js 0x13b0
cmpl $0x3, %r15d
ja 0x1395
movl %r15d, %ecx
movslq (%r13,%rcx,4), %rcx
addq %r13, %rcx
jmpq *%rcx
xorl %r15d, %r15d
cmpl $0x90, %eax
sete %r15b
jmp 0x1291
xorl %ecx, %ecx
cmpl $0x24, %eax
sete %cl
leal (%rcx,%rcx,2), %r15d
jmp 0x1291
xorl %r15d, %r15d
cmpl $0xa3, %eax
jne 0x1291
jmp 0x12af
xorl %r15d, %r15d
cmpl $0xeb, %eax
sete %r15b
addl %r15d, %r15d
cmpl $0x90, %eax
cmovel %ebx, %r15d
movq 0x2ddf(%rip), %rdi      # 0x4080
callq 0x10a0
testl %eax, %eax
je 0x122f
jmp 0x13b0
movq 0x2dca(%rip), %rdi      # 0x4080
movq $-0x1c, %rsi
movl $0x1, %edx
callq 0x10b0
callq 0x13d9
movl %eax, 0x4(%rsp)
callq 0x13d9
callq 0x13d9
callq 0x13d9
movl %eax, %r12d
callq 0x13d9
callq 0x13d9
callq 0x13d9
movq 0x2d88(%rip), %rdi      # 0x4080
movl $0xe4, %esi
movl $0x1, %edx
callq 0x10b0
movq 0x2d72(%rip), %rdi      # 0x4080
callq 0x1090
movq %rax, %r15
movq 0x2d63(%rip), %rdi      # 0x4080
movslq %r12d, %rsi
movl $0x1, %edx
callq 0x10b0
testl %r12d, %r12d
js 0x1218
leaq 0xd0b(%rip), %rdi       # 0x2045
movl %ebp, %esi
movl %r15d, %edx
movl %r12d, %ecx
movl 0x4(%rsp), %r8d
xorl %eax, %eax
callq 0x1060
movq %r14, %rdi
movl %r15d, %esi
movl %ebp, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x1060
movl $0xa, %edi
callq 0x1030
incl %ebp
jmp 0x1218
movq (%rsi), %rsi
leaq 0xc99(%rip), %rdi       # 0x2014
jmp 0x1387
leaq 0xcae(%rip), %rdi       # 0x2032
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1060
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x13ca
movq 0x2c5c(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xd0a(%rip), %rsi       # 0x20b0
movl %r15d, %edx
xorl %eax, %eax
callq 0x1080
leaq 0xd1b(%rip), %rdi       # 0x20d2
callq 0x1040
movq 0x2cbd(%rip), %rdi      # 0x4080
callq 0x1050
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq