// File: /workspace/github_c_makefile_star2_O0/evilwombat[P]gopro-fw-tools/fwparser.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;

	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);
		fseek(fd, length, SEEK_CUR);

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

		printf("# section_%d at offset %d length %d CRC 0x%08x\n",
			num, section_offset, length, crc);
#ifdef _LINUX
		printf("dd if=$1 bs=%d skip=1 | dd iflag=fullblock of=section_%d bs=%d count=1\n",
			section_offset, num, length);
#else
		printf("dd if=$1 skip=%d conv=notrunc of=section_%d count=%d bs=1\n",
			section_offset, num, length);
#endif
		printf("\n");
		num++;
	}

	fclose(fd);
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %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 0x1202
movq -0x10(%rbp), %rax
movq (%rax), %rsi
leaq 0xe25(%rip), %rdi       # 0x2014
movb $0x0, %al
callq 0x1040
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1427
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
leaq 0xe16(%rip), %rsi       # 0x202f
callq 0x10a0
movq %rax, 0x2e4b(%rip)      # 0x4070
cmpq $0x0, 0x2e43(%rip)      # 0x4070
jne 0x124d
movq -0x40(%rbp), %rsi
leaq 0xdf8(%rip), %rdi       # 0x2032
movb $0x0, %al
callq 0x1040
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1427
jmp 0x124f
callq 0x1430
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
jge 0x1283
leaq 0xde1(%rip), %rdi       # 0x2045
movb $0x0, %al
callq 0x1040
movq 0x2dfe(%rip), %rdi      # 0x4070
callq 0x1030
movl $0x0, -0x4(%rbp)
jmp 0x1427
movq 0x2de6(%rip), %rdi      # 0x4070
movq $-0x1c, %rsi
movl $0x1, %edx
callq 0x1090
callq 0x1550
movl %eax, -0x1c(%rbp)
callq 0x1550
movl %eax, -0x20(%rbp)
callq 0x1550
movl %eax, -0x24(%rbp)
callq 0x1550
movl %eax, -0x38(%rbp)
callq 0x1550
callq 0x1550
movl %eax, -0x28(%rbp)
callq 0x1550
movl %eax, -0x2c(%rbp)
movq 0x2d99(%rip), %rdi      # 0x4070
movl $0xe4, %esi
movl $0x1, %edx
callq 0x1090
movq 0x2d83(%rip), %rdi      # 0x4070
callq 0x1070
movl %eax, -0x30(%rbp)
movq 0x2d74(%rip), %rdi      # 0x4070
movslq -0x38(%rbp), %rsi
movl $0x1, %edx
callq 0x1090
cmpl $0x0, -0x38(%rbp)
jge 0x1315
jmp 0x124f
cmpl $0x0, -0x14(%rbp)
je 0x13d9
movq 0x2cd2(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xd2d(%rip), %rsi       # 0x205d
movb $0x0, %al
callq 0x1060
movq 0x2cba(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movl -0x1c(%rbp), %edx
leaq 0xd21(%rip), %rsi       # 0x206c
movb $0x0, %al
callq 0x1060
movq 0x2c9f(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movl -0x20(%rbp), %edx
leaq 0xd13(%rip), %rsi       # 0x2079
movb $0x0, %al
callq 0x1060
movq 0x2c84(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movl -0x24(%rbp), %edx
leaq 0xd09(%rip), %rsi       # 0x208a
movb $0x0, %al
callq 0x1060
movq 0x2c69(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movl -0x38(%rbp), %edx
leaq 0xcfd(%rip), %rsi       # 0x2099
movb $0x0, %al
callq 0x1060
movq 0x2c4e(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movl -0x28(%rbp), %edx
leaq 0xcf2(%rip), %rsi       # 0x20a9
movb $0x0, %al
callq 0x1060
movq 0x2c33(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movl -0x2c(%rbp), %edx
leaq 0xce6(%rip), %rsi       # 0x20b8
movb $0x0, %al
callq 0x1060
movl -0x34(%rbp), %esi
movl -0x30(%rbp), %edx
movl -0x38(%rbp), %ecx
movl -0x1c(%rbp), %r8d
leaq 0xcda(%rip), %rdi       # 0x20c7
movb $0x0, %al
callq 0x1040
movl -0x30(%rbp), %esi
movl -0x34(%rbp), %edx
movl -0x38(%rbp), %ecx
leaq 0xcf3(%rip), %rdi       # 0x20f7
movb $0x0, %al
callq 0x1040
leaq 0xc49(%rip), %rdi       # 0x205b
movb $0x0, %al
callq 0x1040
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x124f
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq