// File: /workspace/github_c_makefile_star2_O2/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 0x134e
movq 0x8(%rsi), %rbx
leaq 0xe54(%rip), %rsi       # 0x202f
movq %rbx, %rdi
callq 0x10b0
movq %rax, 0x2e8e(%rip)      # 0x4078
testq %rax, %rax
je 0x1376
leaq 0xe0a(%rip), %r13       # 0x2004
pushq $0x1
popq %rbx
movl $0x0, 0x4(%rsp)
xorl %r14d, %r14d
movq 0x2e69(%rip), %rdi      # 0x4078
callq 0x1090
testl %eax, %eax
jne 0x135a
movq 0x2e55(%rip), %rdi      # 0x4078
callq 0x1070
testl %eax, %eax
js 0x135a
movl %r14d, %ecx
movslq (%r13,%rcx,4), %rcx
addq %r13, %rcx
jmpq *%rcx
xorl %r14d, %r14d
cmpl $0x90, %eax
sete %r14b
jmp 0x12f7
xorl %ecx, %ecx
cmpl $0x24, %eax
sete %cl
leal (%rcx,%rcx,2), %r14d
jmp 0x12f7
xorl %r14d, %r14d
cmpl $0xa3, %eax
jne 0x12f7
movq 0x2e04(%rip), %rdi      # 0x4078
pushq $-0x1c
popq %rsi
movl %ebx, %edx
callq 0x10a0
callq 0x1399
movl %eax, %r15d
callq 0x1399
callq 0x1399
callq 0x1399
movl %eax, %r12d
callq 0x1399
callq 0x1399
callq 0x1399
movq 0x2dca(%rip), %rdi      # 0x4078
movl $0xe4, %esi
movl %ebx, %edx
callq 0x10a0
movq 0x2db7(%rip), %rdi      # 0x4078
callq 0x1080
movq %rax, %rbp
movq 0x2da8(%rip), %rdi      # 0x4078
movslq %r12d, %rsi
movl %ebx, %edx
callq 0x10a0
xorl %r14d, %r14d
testl %r12d, %r12d
js 0x1208
jmp 0x1305
xorl %r14d, %r14d
cmpl $0xeb, %eax
sete %r14b
addl %r14d, %r14d
cmpl $0x90, %eax
cmovel %ebx, %r14d
jmp 0x1208
leaq 0xd39(%rip), %rdi       # 0x2045
movl 0x4(%rsp), %r14d
movl %r14d, %esi
movl %ebp, %edx
movl %r12d, %ecx
movl %r15d, %r8d
xorl %eax, %eax
callq 0x1060
leaq 0xd4b(%rip), %rdi       # 0x2075
movl %ebp, %esi
movl %r14d, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x1060
pushq $0xa
popq %rdi
callq 0x1030
incl %r14d
movl %r14d, 0x4(%rsp)
jmp 0x1205
movq (%rsi), %rsi
leaq 0xcbc(%rip), %rdi       # 0x2014
jmp 0x1380
leaq 0xd4f(%rip), %rdi       # 0x20b0
callq 0x1040
movq 0x2d0b(%rip), %rdi      # 0x4078
callq 0x1050
xorl %eax, %eax
jmp 0x138a
leaq 0xcb5(%rip), %rdi       # 0x2032
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1060
pushq $-0x1
popq %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq