// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
movl $0x0, -0x18(%rbp)
movl $0x0, -0x34(%rbp)
cmpl $0x2, -0x8(%rbp)
je 0x1232
movq -0x10(%rbp), %rax
movq (%rax), %rsi
leaq 0xdf5(%rip), %rdi       # 0x2014
movb $0x0, %al
callq 0x1050
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1448
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
leaq 0xde6(%rip), %rsi       # 0x202f
callq 0x10c0
movq %rax, 0x2e33(%rip)      # 0x4088
cmpq $0x0, 0x2e2b(%rip)      # 0x4088
jne 0x127d
movq -0x40(%rbp), %rsi
leaq 0xdc8(%rip), %rdi       # 0x2032
movb $0x0, %al
callq 0x1050
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1448
jmp 0x127f
callq 0x1460
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
jge 0x12b3
leaq 0xdb1(%rip), %rdi       # 0x2045
movb $0x0, %al
callq 0x1050
movq 0x2de6(%rip), %rdi      # 0x4088
callq 0x1040
movl $0x0, -0x4(%rbp)
jmp 0x1448
movq 0x2dce(%rip), %rdi      # 0x4088
movq $-0x1c, %rsi
movl $0x1, %edx
callq 0x10b0
callq 0x1580
movl %eax, -0x1c(%rbp)
callq 0x1580
movl %eax, -0x20(%rbp)
callq 0x1580
movl %eax, -0x24(%rbp)
callq 0x1580
movl %eax, -0x38(%rbp)
callq 0x1580
callq 0x1580
movl %eax, -0x28(%rbp)
callq 0x1580
movl %eax, -0x2c(%rbp)
movq 0x2d81(%rip), %rdi      # 0x4088
movl $0xe4, %esi
movl $0x1, %edx
callq 0x10b0
movq 0x2d6b(%rip), %rdi      # 0x4088
callq 0x1090
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x38(%rbp)
jge 0x1330
jmp 0x127f
cmpl $0x0, -0x14(%rbp)
je 0x13f4
movq 0x2cb7(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xd10(%rip), %rsi       # 0x205b
movb $0x0, %al
callq 0x1080
movq 0x2c9f(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movl -0x1c(%rbp), %edx
leaq 0xd04(%rip), %rsi       # 0x206a
movb $0x0, %al
callq 0x1080
movq 0x2c84(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movl -0x20(%rbp), %edx
leaq 0xcf6(%rip), %rsi       # 0x2077
movb $0x0, %al
callq 0x1080
movq 0x2c69(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movl -0x24(%rbp), %edx
leaq 0xcec(%rip), %rsi       # 0x2088
movb $0x0, %al
callq 0x1080
movq 0x2c4e(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movl -0x38(%rbp), %edx
leaq 0xce0(%rip), %rsi       # 0x2097
movb $0x0, %al
callq 0x1080
movq 0x2c33(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movl -0x28(%rbp), %edx
leaq 0xcd5(%rip), %rsi       # 0x20a7
movb $0x0, %al
callq 0x1080
movq 0x2c18(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movl -0x2c(%rbp), %edx
leaq 0xcc9(%rip), %rsi       # 0x20b6
movb $0x0, %al
callq 0x1080
leaq -0x60(%rbp), %rdi
movl -0x34(%rbp), %ecx
movl $0x14, %esi
leaq 0xcbe(%rip), %rdx       # 0x20c5
movb $0x0, %al
callq 0x1060
movl -0x34(%rbp), %esi
leaq -0x60(%rbp), %rdx
movl -0x30(%rbp), %ecx
movl -0x38(%rbp), %r8d
movl -0x1c(%rbp), %r9d
leaq 0xca9(%rip), %rdi       # 0x20d0
movb $0x0, %al
callq 0x1050
leaq -0x60(%rbp), %rdi
movl -0x38(%rbp), %esi
callq 0x15f0
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x127f
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)