// File: /workspace/github_c_makefile_star2_O2/evilwombat[P]gopro-fw-tools/goprom.c
// Function: main
int main(int argc, char **argv)
{
	int		i, nfiles, update = 0;
	struct inode	d;
	FILE		*fd;

	if (argc != 3) {
		print_usage();
		exit(-1);
	}

	if (strcmp(argv[1], "--unpack") == 0) {
		fprintf(stderr, "Generating unpack script\n");
		update = 0;
	} else if (strcmp(argv[1], "--update") == 0) {
		fprintf(stderr, "Generating update script\n");
		fprintf(stderr, "You really better know what you are doing.\n");
		fprintf(stderr, "You can very easily destroy your camera if you misuse this!\n");
		update = 1;
	} else {
		print_usage();
		exit(-1);
	}
	
	fd = fopen(argv[2], "rb");

	if (!fd) {
		fprintf(stderr, "Could not open %s\n", argv[2]);
		exit(-1);
	}
	
	nfiles = read_word(fd);
	fprintf(stderr, "I see %d files\n", nfiles);

	if (nfiles < 0) {
		fprintf(stderr, "... and that certainly does not seem right\n");
		exit(-1);
	}
	
	fseek(fd, 0x800, SEEK_SET);
	
	for (i = 0; i < nfiles; i++) {
		fread(&d, sizeof(struct inode), 1, fd);

		if (d.magic != INODE_MAGIC) {
			fprintf(stderr, "Unknown inode magic: %08x\n", d.magic);
			exit(-1);
		}

		if (!update) {
			printf("mkdir -p `dirname %s`\n", d.name);
			printf("dd if=$1 bs=%d skip=1 | dd iflag=fullblock of=%s bs=%d count=1\n",
				d.offset, d.name, d.len);
		} else
			printf("dd if=%s of=$1 bs=1 seek=%d count=%d conv=notrunc\n",
				d.name, d.offset, d.len);
	}

	fclose(fd);

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
cmpl $0x3, %edi
jne 0x13d5
movq %rsi, %r15
movq 0x8(%rsi), %r14
leaq 0xe00(%rip), %rsi       # 0x2004
movq %r14, %rdi
callq 0x1080
movl %eax, %ebx
testl %eax, %eax
jne 0x122b
movq 0x2ddf(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xdea(%rip), %rdi       # 0x200d
pushq $0x19
popq %rsi
pushq $0x1
popq %rdx
jmp 0x128a
leaq 0xdf5(%rip), %rsi       # 0x2027
movq %r14, %rdi
callq 0x1080
testl %eax, %eax
jne 0x13d5
movq 0x2daf(%rip), %r12      # 0x3ff8
movq (%r12), %rcx
leaq 0xddc(%rip), %rdi       # 0x2030
pushq $0x19
popq %rsi
pushq $0x1
popq %r14
movq %r14, %rdx
callq 0x10d0
movq (%r12), %rcx
leaq 0xddc(%rip), %rdi       # 0x204a
pushq $0x2b
popq %rsi
movq %r14, %rdx
callq 0x10d0
movq (%r12), %rcx
leaq 0xdf2(%rip), %rdi       # 0x2076
pushq $0x3c
popq %rsi
movq %r14, %rdx
callq 0x10d0
movq 0x10(%r15), %rdi
leaq 0xe19(%rip), %rsi       # 0x20b3
callq 0x10b0
testq %rax, %rax
je 0x13dc
movq %rax, %r14
movq %rax, %rdi
callq 0x1060
movl %eax, %ebp
movq %r14, %rdi
callq 0x1060
movl %eax, %r15d
shll $0x8, %r15d
orl %ebp, %r15d
movq %r14, %rdi
callq 0x1060
movl %eax, %r12d
shll $0x10, %r12d
orl %r15d, %r12d
movq %r14, %rdi
callq 0x1060
movl %eax, %ebp
shll $0x18, %ebp
movq 0x2d0b(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xdd2(%rip), %rsi       # 0x20c9
movl %r12d, %edx
orl %ebp, %edx
xorl %eax, %eax
callq 0x1090
orl %r12d, %ebp
js 0x1400
movl $0x800, %esi            # imm = 0x800
movq %r14, %rdi
xorl %edx, %edx
callq 0x10a0
leaq 0x8(%rsp), %r15
pushq $0x1
popq %r12
leaq 0xe4c(%rip), %r13       # 0x2177
subl $0x1, %ebp
jb 0x139f
movl $0x80, %esi
movq %r15, %rdi
movq %r12, %rdx
movq %r14, %rcx
callq 0x1030
movl 0x84(%rsp), %edx
cmpl $0x2387ab76, %edx       # imm = 0x2387AB76
jne 0x13bb
testl %ebx, %ebx
je 0x1370
movl 0x7c(%rsp), %edx
movl 0x80(%rsp), %ecx
movq %r13, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x1050
jmp 0x132b
leaq 0xda9(%rip), %rdi       # 0x2120
movq %r15, %rsi
xorl %eax, %eax
callq 0x1050
movl 0x7c(%rsp), %esi
movl 0x80(%rsp), %ecx
leaq 0xda4(%rip), %rdi       # 0x2137
movq %r15, %rdx
xorl %eax, %eax
callq 0x1050
jmp 0x132b
movq %r14, %rdi
callq 0x1040
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2c36(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xd39(%rip), %rsi       # 0x2105
xorl %eax, %eax
callq 0x1090
jmp 0x13f8
callq 0x141e
jmp 0x13f8
movq 0x2c15(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq 0x10(%r15), %rdx
leaq 0xcc5(%rip), %rsi       # 0x20b6
xorl %eax, %eax
callq 0x1090
pushq $-0x1
popq %rdi
callq 0x10c0
movq 0x2bf1(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xcc8(%rip), %rdi       # 0x20d9
pushq $0x2b
popq %rsi
pushq $0x1
popq %rdx
callq 0x10d0
jmp 0x13f8