// File: /workspace/github_c_makefile_star2_O1/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 0x13e6
movq 0x8(%rsi), %rbx
leaq 0xe21(%rip), %rsi       # 0x202f
movq %rbx, %rdi
callq 0x10e0
movq %rax, 0x2e73(%rip)      # 0x4090
testq %rax, %rax
je 0x13f2
leaq 0xdd7(%rip), %r12       # 0x2004
movl $0x1, %r13d
leaq 0xb(%rsp), %rbp
xorl %ebx, %ebx
movq 0x2e4f(%rip), %rdi      # 0x4090
callq 0x10c0
testl %eax, %eax
jne 0x1425
xorl %r15d, %r15d
movq 0x2e38(%rip), %rdi      # 0x4090
callq 0x1080
testl %eax, %eax
js 0x1425
cmpl $0x3, %r15d
ja 0x140a
movl %r15d, %ecx
movslq (%r12,%rcx,4), %rcx
addq %r12, %rcx
jmpq *%rcx
xorl %r15d, %r15d
cmpl $0x90, %eax
sete %r15b
jmp 0x12b2
xorl %ecx, %ecx
cmpl $0x24, %eax
sete %cl
leal (%rcx,%rcx,2), %r15d
jmp 0x12b2
xorl %r15d, %r15d
cmpl $0xa3, %eax
jne 0x12b2
jmp 0x12d0
xorl %r15d, %r15d
cmpl $0xeb, %eax
sete %r15b
addl %r15d, %r15d
cmpl $0x90, %eax
cmovel %r13d, %r15d
movq 0x2dce(%rip), %rdi      # 0x4090
callq 0x10c0
testl %eax, %eax
je 0x1251
jmp 0x1425
movq 0x2db9(%rip), %rdi      # 0x4090
movq $-0x1c, %rsi
movl $0x1, %edx
callq 0x10d0
callq 0x144e
movl %eax, 0xc(%rsp)
callq 0x144e
callq 0x144e
callq 0x144e
movl %eax, %r15d
callq 0x144e
callq 0x144e
callq 0x144e
movq 0x2d77(%rip), %rdi      # 0x4090
movl $0xe4, %esi
movl $0x1, %edx
callq 0x10d0
movq 0x2d61(%rip), %rdi      # 0x4090
callq 0x10b0
testl %r15d, %r15d
js 0x123a
movq %rax, %r14
movl $0x14, %esi
leaq 0x10(%rsp), %rdi
leaq 0xcf4(%rip), %rdx       # 0x2045
movl %ebx, %ecx
xorl %eax, %eax
callq 0x1070
leaq 0xcef(%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 0xd23(%rip), %rsi       # 0x20aa
callq 0x10e0
testq %rax, %rax
je 0x13ce
testl %r15d, %r15d
movq %rax, %r14
je 0x13c4
movq 0x2cf0(%rip), %rcx      # 0x4090
movl $0x1, %esi
movl $0x1, %edx
movq %rbp, %rdi
callq 0x1040
movsbl 0xb(%rsp), %edi
movq %r14, %rsi
callq 0x1090
decl %r15d
jne 0x1399
movq %r14, %rdi
callq 0x1050
jmp 0x13df
leaq 0xcd9(%rip), %rdi       # 0x20ae
movq %r14, %rsi
xorl %eax, %eax
callq 0x1060
incl %ebx
jmp 0x123a
movq (%rsi), %rsi
leaq 0xc24(%rip), %rdi       # 0x2014
jmp 0x13fc
leaq 0xc39(%rip), %rdi       # 0x2032
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1060
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x143f
movq 0x2be7(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xc6d(%rip), %rsi       # 0x2088
movl %r15d, %edx
xorl %eax, %eax
callq 0x10a0
leaq 0xc99(%rip), %rdi       # 0x20c5
callq 0x1030
movq 0x2c58(%rip), %rdi      # 0x4090
callq 0x1050
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq