// File: /workspace/github_c_makefile_star2_O3/evilwombat[P]gopro-fw-tools/fwunpacker.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;
	char name_buf[20];

	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);

		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);
		}

		snprintf(name_buf, 20, "section_%d", num);
		printf("Saving section %d to %s at offset %d len %d CRC 0x%08x\n",
			num, name_buf, section_offset, length, crc);

		save_section(name_buf, length);
		
		num++;
	}

	fclose(fd);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
cmpl $0x2, %edi
jne 0x13cc
movq 0x8(%rsi), %rbx
leaq 0xe31(%rip), %rsi       # 0x202f
movq %rbx, %rdi
callq 0x10d0
movq %rax, 0x2e7b(%rip)      # 0x4088
testq %rax, %rax
je 0x13f4
leaq 0xde7(%rip), %r12       # 0x2004
movl $0x1, %r13d
leaq 0xb(%rsp), %rbp
xorl %ebx, %ebx
movq 0x2e57(%rip), %rdi      # 0x4088
callq 0x10b0
testl %eax, %eax
jne 0x13d8
xorl %r14d, %r14d
movq 0x2e40(%rip), %rdi      # 0x4088
callq 0x1080
testl %eax, %eax
js 0x13d8
movl %r14d, %ecx
movslq (%r12,%rcx,4), %rcx
addq %r12, %rcx
jmpq *%rcx
xorl %r14d, %r14d
cmpl $0x90, %eax
sete %r14b
jmp 0x1298
xorl %ecx, %ecx
cmpl $0x24, %eax
sete %cl
leal (%rcx,%rcx,2), %r14d
jmp 0x1298
xorl %r14d, %r14d
cmpl $0xa3, %eax
jne 0x1298
jmp 0x12b6
xorl %r14d, %r14d
cmpl $0xeb, %eax
sete %r14b
addl %r14d, %r14d
cmpl $0x90, %eax
cmovel %r13d, %r14d
movq 0x2de0(%rip), %rdi      # 0x4088
callq 0x10b0
testl %eax, %eax
je 0x1241
jmp 0x13d8
movq 0x2dcb(%rip), %rdi      # 0x4088
movq $-0x1c, %rsi
movl $0x1, %edx
callq 0x10c0
callq 0x1419
movl %eax, 0xc(%rsp)
callq 0x1419
callq 0x1419
callq 0x1419
movl %eax, %r15d
callq 0x1419
callq 0x1419
callq 0x1419
movq 0x2d89(%rip), %rdi      # 0x4088
movl $0xe4, %esi
movl $0x1, %edx
callq 0x10c0
movq 0x2d73(%rip), %rdi      # 0x4088
callq 0x10a0
testl %r15d, %r15d
js 0x122a
movq %rax, %r14
movl $0x14, %esi
leaq 0x10(%rsp), %rdi
leaq 0xd0e(%rip), %rdx       # 0x2045
movl %ebx, %ecx
xorl %eax, %eax
callq 0x1070
leaq 0xd09(%rip), %rdi       # 0x2050
movl %ebx, %esi
leaq 0x10(%rsp), %rdx
movl %r14d, %ecx
movq %rdx, %r14
movl %r15d, %r8d
movl 0xc(%rsp), %r9d
xorl %eax, %eax
callq 0x1060
movq %r14, %rdi
leaq 0xd1b(%rip), %rsi       # 0x2088
callq 0x10d0
testq %rax, %rax
je 0x13b4
testl %r15d, %r15d
movq %rax, %r14
je 0x13aa
movq 0x2d02(%rip), %rcx      # 0x4088
movl $0x1, %esi
movl $0x1, %edx
movq %rbp, %rdi
callq 0x1040
movsbl 0xb(%rsp), %edi
movq %r14, %rsi
callq 0x1090
decl %r15d
jne 0x137f
movq %r14, %rdi
callq 0x1050
jmp 0x13c5
leaq 0xcd1(%rip), %rdi       # 0x208c
movq %r14, %rsi
xorl %eax, %eax
callq 0x1060
incl %ebx
jmp 0x122a
movq (%rsi), %rsi
leaq 0xc3e(%rip), %rdi       # 0x2014
jmp 0x13fe
leaq 0xcc4(%rip), %rdi       # 0x20a3
callq 0x1030
movq 0x2c9d(%rip), %rdi      # 0x4088
callq 0x1050
xorl %eax, %eax
jmp 0x140a
leaq 0xc37(%rip), %rdi       # 0x2032
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1060
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq