// File: /workspace/github_c_makefile_star2_O2/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 0x13b2
movq 0x8(%rsi), %rbx
leaq 0xe31(%rip), %rsi       # 0x202f
movq %rbx, %rdi
callq 0x10d0
movq %rax, 0x2e7b(%rip)      # 0x4088
testq %rax, %rax
je 0x13da
leaq 0xde7(%rip), %r14       # 0x2004
pushq $0x1
popq %rbx
movl $0x0, 0xc(%rsp)
xorl %ebp, %ebp
movq 0x2e57(%rip), %rdi      # 0x4088
callq 0x10b0
testl %eax, %eax
jne 0x13be
movq 0x2e43(%rip), %rdi      # 0x4088
callq 0x1080
testl %eax, %eax
js 0x13be
movl %ebp, %ecx
movslq (%r14,%rcx,4), %rcx
addq %r14, %rcx
jmpq *%rcx
xorl %ebp, %ebp
cmpl $0x90, %eax
sete %bpl
jmp 0x12f6
xorl %ecx, %ecx
cmpl $0x24, %eax
sete %cl
leal (%rcx,%rcx,2), %ebp
jmp 0x12f6
xorl %ebp, %ebp
cmpl $0xa3, %eax
jne 0x12f6
movq 0x2dfe(%rip), %rdi      # 0x4088
pushq $-0x1c
popq %rsi
movl %ebx, %edx
callq 0x10c0
callq 0x13fd
movl %eax, %r13d
callq 0x13fd
callq 0x13fd
callq 0x13fd
movl %eax, %r12d
callq 0x13fd
callq 0x13fd
callq 0x13fd
movq 0x2dc4(%rip), %rdi      # 0x4088
movl $0xe4, %esi
movl %ebx, %edx
callq 0x10c0
movq 0x2db1(%rip), %rdi      # 0x4088
callq 0x10a0
xorl %ebp, %ebp
testl %r12d, %r12d
js 0x122a
jmp 0x1303
xorl %ebp, %ebp
cmpl $0xeb, %eax
sete %bpl
addl %ebp, %ebp
cmpl $0x90, %eax
cmovel %ebx, %ebp
jmp 0x122a
movq %rax, %r15
leaq 0x10(%rsp), %rdi
pushq $0x14
popq %rsi
leaq 0xd30(%rip), %rdx       # 0x2045
movl 0xc(%rsp), %ebp
movl %ebp, %ecx
xorl %eax, %eax
callq 0x1070
leaq 0xd27(%rip), %rdi       # 0x2050
movl %ebp, %esi
leaq 0x10(%rsp), %rbp
movq %rbp, %rdx
movl %r15d, %ecx
movl %r12d, %r8d
movl %r13d, %r9d
xorl %eax, %eax
callq 0x1060
movq %rbp, %rdi
leaq 0xd3b(%rip), %rsi       # 0x2088
callq 0x10d0
movq %rax, %r13
testq %rax, %rax
leaq 0xb(%rsp), %r15
pushq $0x1
popq %rbp
je 0x1396
subl $0x1, %r12d
jb 0x138c
movq 0x2d19(%rip), %rcx      # 0x4088
movq %r15, %rdi
movq %rbp, %rsi
movq %rbp, %rdx
callq 0x1040
movsbl 0xb(%rsp), %edi
movq %r13, %rsi
callq 0x1090
jmp 0x1362
movq %r13, %rdi
callq 0x1050
jmp 0x13a9
leaq 0xcef(%rip), %rdi       # 0x208c
leaq 0x10(%rsp), %rsi
xorl %eax, %eax
callq 0x1060
incl 0xc(%rsp)
jmp 0x1228
movq (%rsi), %rsi
leaq 0xc58(%rip), %rdi       # 0x2014
jmp 0x13e4
leaq 0xcde(%rip), %rdi       # 0x20a3
callq 0x1030
movq 0x2cb7(%rip), %rdi      # 0x4088
callq 0x1050
xorl %eax, %eax
jmp 0x13ee
leaq 0xc51(%rip), %rdi       # 0x2032
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1060
pushq $-0x1
popq %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq