// File: /workspace/github_c_makefile_star2_O3/evilwombat[P]gopro-fw-tools/h4-section-patch.c
// Function: main
int main(int argc, char **argv)
{
	char *fname, *sname, *oname;
	int ret;
	int target_section;
	unsigned char *fw_buf, *replacement_buf;
	unsigned int fw_size, replacement_size;
	int num_sections;
	int old_num_sections;
	unsigned int new_section_crc, new_global_crc;
	struct section_info sections[MAX_SECTIONS];
	
	printf("evilwombat's magical firmware section patching tool.\n");
	printf("This program is incomplete, undocumented, and unfit for any purpose whatsoever.\n");
	printf("This program is provided entirely AS-IS without ANY warranty, and is unfit for ANY\n");
	printf("purpose. The author bears NO RESPONSIBILITY for ANY consequences of the use of this\n");
	printf("software, including, but not limited to, permanent to damage or destruction of\n");
	printf("your camera, someone else's camera, or any damage to anything, or any consequences\n");
	printf("whatsoever.\n");
	printf("\n");
	printf("Additionally, if you use this program, you are agreeing that your camera's warranty\n");
	printf("is VOIDED, and that you will not attempt to get any kind of warranty coverage or\n");
	printf("ask for any kind of warranty repair for your camera.\n");
	
	printf("IF YOU BRICK IT OR DAMAGE IT, IT'S YOUR FAULT. Don't try to get it replaced. We don't\n");
	printf("need the manufacturer complaining that hobby people are killing their cameras and\n");
	printf("getting mad at us for it.\n");
	printf("\nYOU HAVE BEEN WARNED\n");
	printf("\nMoreover, this program is INCOMPLETE and probably nonfunctional.\n");
	printf("DO NOT USE THIS PROGRAM!\n\n");
	
	if (argc != 5) {
		print_usage(argv[0]);
		return -1;
	}
	
	fname = argv[1];
	sname = argv[2];
	target_section = atoi(argv[3]);
	oname = argv[4];

	printf("Replacing section %d in file %s with file %s, and writing output to %s\n",
	       target_section, fname, sname, oname);

	fw_buf = read_file(fname, &fw_size);
	
	if (!fw_buf) {
		printf("Could not read in original firmware file %s. Exiting.\n", fname);
		return -1;
	}
	
	replacement_buf = read_file(sname, &replacement_size);
	
	if (!replacement_buf) {
		printf("Could not read in replacement section file %s. Exiting.\n", sname);
		return -1;
	}

	printf("\nDecoding contents of %s...\n", fname);
	num_sections = parse_firmware(fw_buf, fw_size, sections, MAX_SECTIONS);
	if (num_sections <= 0) {
		printf("This firmware looks invalid. Exiting.\n");
		return -1;
	}
	printf("\nFound %d sections in file %s:\n", num_sections, fname);
	print_sections(sections, num_sections);
	printf("\n");

	if (target_section >= num_sections) {
		printf("This firmware file (%s) only contains %d sections, and you are\n", fname, num_sections);
		printf("trying to replace section %d (and they are numbered from 0).\n", target_section);
		printf("Are you sure you know what you are doing?\n");
		return -1;
	}
	
	printf("Okay. Trying to replace section_%d with %s\n", target_section, sname);
	
	if (sections[target_section].length > replacement_size) {
		printf("\n******************************************************************************\n");
		printf("WARNING!! The replacement section is smaller than the section in the firmware.\n");
		printf("In the firmware, section_%d is %d bytes long.\n",
		       target_section, sections[target_section].length);
		printf("Your replacement file for section_%d is only %d bytes long.\n",
		       target_section, replacement_size);
		printf("Your replacement section is smaller than the target section by %d bytes.\n",
			sections[target_section].length - replacement_size);
		printf("\nThis might not necessarily be a bad thing, depending on what you are doing.\n");
		printf("If you continue, the section will be zero-padded to the expected length.\n");
		printf("******************************************************************************\n");
		printf("Would you like to proceed? (y/n)\n");
		
		if (getchar() != 'y') {
			printf("Operation aborted by user\n");
			return -1;
		}
	}
	
	if (sections[target_section].length < replacement_size) {
		printf("\n**************************************************************\n");
		printf("ERROR!! The replacement section will not fit into the firmware.\n");
		printf("Your replacement file for section_%d has length %d bytes.\n", target_section, replacement_size);
		printf("In the firmware, this section is only %d bytes long.\n", sections[target_section].length);
		printf("Your replacement section is too long by %d bytes.\n",
			replacement_size - sections[target_section].length);
		printf("This will not work.\n");
		return -1;
	}

	printf("\nReplacing target section...\n");
	/* Zero out replacement section in case we are zero-padding */
	memset(fw_buf + sections[target_section].offset, 0, sections[target_section].length);
	memcpy(fw_buf + sections[target_section].offset, replacement_buf, replacement_size);

	printf("Updating CRCs...\n");
	new_section_crc = crc32(fw_buf + sections[target_section].offset, sections[target_section].length);
	printf("New section CRC: %08x\n", new_section_crc);
	
	write_word_le(fw_buf, sections[target_section].offset - 0x100, new_section_crc);

	new_global_crc = get_global_crc(fw_buf, fw_size);
	printf("New global CRC: %08x\n", new_global_crc);
	
	write_word_le(fw_buf, 0, new_global_crc);
	
	old_num_sections = num_sections;

	printf("\nRescanning resulting firmware for sanity...\n");
	num_sections = parse_firmware(fw_buf, fw_size, sections, MAX_SECTIONS);
	if (num_sections <= 0) {
		printf("The new firmware looks invalid!!\nThis is definitely a bug in this program.\n");
		printf("Please contact evilwombat and report how this happened.\n");
		return -1;
	}
	printf("\nFound %d sections in the new firmware:\n", num_sections);
	print_sections(sections, num_sections);

	if (num_sections != old_num_sections) {
		printf("Old and new section count does not match!!\nThis is definitely a bug in this program.\n");
		printf("Please contact evilwombat and report how this happened.\n");
		return -1;
	}
	
	printf("\nSaving new firmware to file %s...\n", oname);
	ret = save_file(oname, fw_size, fw_buf);
	if (ret) {
		printf("Error saving file!\n");
		return -1;
	}
	printf("Done.\n");

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xcb8, %rsp            # imm = 0xCB8
movq %rsi, %r14
movl %edi, %ebx
leaq 0xfca(%rip), %rdi       # 0x2670
callq 0x1050
leaq 0xff3(%rip), %rdi       # 0x26a5
callq 0x1050
leaq 0x1037(%rip), %rdi      # 0x26f5
callq 0x1050
leaq 0x107e(%rip), %rdi      # 0x2748
callq 0x1050
leaq 0x10c6(%rip), %rdi      # 0x279c
callq 0x1050
leaq 0x1109(%rip), %rdi      # 0x27eb
callq 0x1050
leaq 0xffb(%rip), %rdi       # 0x26e9
callq 0x1050
movl $0xa, %edi
callq 0x1040
leaq 0x113a(%rip), %rdi      # 0x283e
callq 0x1050
leaq 0x1182(%rip), %rdi      # 0x2892
callq 0x1050
leaq 0x11c7(%rip), %rdi      # 0x28e3
callq 0x1050
leaq 0x11f0(%rip), %rdi      # 0x2918
callq 0x1050
leaq 0x123a(%rip), %rdi      # 0x296e
callq 0x1050
leaq 0x1280(%rip), %rdi      # 0x29c0
callq 0x1050
leaq 0x128e(%rip), %rdi      # 0x29da
callq 0x1050
leaq 0x1298(%rip), %rdi      # 0x29f0
callq 0x1050
leaq 0x12ce(%rip), %rdi      # 0x2a32
callq 0x1050
cmpl $0x5, %ebx
jne 0x197b
movq 0x8(%r14), %rbp
movq 0x10(%r14), %rbx
movq 0x18(%r14), %rdi
callq 0x10f0
movl %eax, %r12d
movq 0x20(%r14), %r15
leaq 0xa20(%rip), %rdi       # 0x21b1
movl %eax, %esi
movq %rbp, %rdx
movq %rbx, %rcx
movq %r15, %r8
xorl %eax, %eax
callq 0x1080
leaq 0x24(%rsp), %rsi
movq %rbp, %rdi
callq 0x120c
testq %rax, %rax
je 0x19be
movq %rax, %r14
leaq 0x20(%rsp), %rsi
movq %rbx, %rdi
callq 0x120c
testq %rax, %rax
je 0x19ca
movq %rax, %r13
movq %r15, 0x28(%rsp)
leaq 0xa88(%rip), %rdi       # 0x2269
movq %rbp, %rsi
xorl %eax, %eax
callq 0x1080
movl 0x24(%rsp), %esi
leaq 0x30(%rsp), %rdx
movq %r14, %rdi
movl %esi, 0x1c(%rsp)
movl $0x64, %ecx
callq 0x13de
testl %eax, %eax
jle 0x19f4
movl %eax, %r15d
leaq 0xa6f(%rip), %rdi       # 0x2286
movl %eax, %esi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x1080
leaq 0x30(%rsp), %rdi
movl %r15d, %esi
callq 0x1322
movl $0xa, %edi
callq 0x1040
cmpl %r15d, %r12d
jge 0x19fd
leaq 0xada(%rip), %rdi       # 0x2324
movl %r12d, %esi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x1080
movslq %r12d, %rcx
shlq $0x5, %rcx
movl 0x4c(%rsp,%rcx), %eax
movl 0x20(%rsp), %ebp
movq %rax, 0x8(%rsp)
cmpl %ebp, %eax
jbe 0x1916
leaq 0x11d2(%rip), %rdi      # 0x2a4c
movq %rcx, 0x10(%rsp)
callq 0x1050
leaq 0x1211(%rip), %rdi      # 0x2a9c
callq 0x1050
leaq 0xab9(%rip), %rdi       # 0x2350
movl %r12d, %esi
movq %rbp, %rbx
movq 0x8(%rsp), %rbp
movl %ebp, %edx
xorl %eax, %eax
callq 0x1080
leaq 0xacd(%rip), %rdi       # 0x237f
movl %r12d, %esi
movl %ebx, %edx
xorl %eax, %eax
callq 0x1080
movl %ebp, %esi
movq %rbx, %rbp
subl %ebp, %esi
leaq 0xaf0(%rip), %rdi       # 0x23bc
xorl %eax, %eax
callq 0x1080
leaq 0x1211(%rip), %rdi      # 0x2aeb
callq 0x1050
leaq 0x1252(%rip), %rdi      # 0x2b38
callq 0x1050
leaq 0x115b(%rip), %rdi      # 0x2a4d
callq 0x1050
leaq 0x1283(%rip), %rdi      # 0x2b81
callq 0x1050
callq 0x10a0
movq 0x10(%rsp), %rcx
cmpl $0x79, %eax
jne 0x1b48
cmpl %ebp, 0x8(%rsp)
jae 0x1a2b
leaq 0x13c7(%rip), %rdi      # 0x2cee
callq 0x1050
leaq 0x13fb(%rip), %rdi      # 0x2d2e
callq 0x1050
leaq 0xac7(%rip), %rdi       # 0x2406
movl %r12d, %esi
movl %ebp, %edx
xorl %eax, %eax
callq 0x1080
leaq 0xaef(%rip), %rdi       # 0x2441
movq 0x8(%rsp), %rbx
movl %ebx, %esi
xorl %eax, %eax
callq 0x1080
subl %ebx, %ebp
leaq 0xb0e(%rip), %rdi       # 0x2477
movl %ebp, %esi
xorl %eax, %eax
callq 0x1080
leaq 0x13f5(%rip), %rdi      # 0x2d6e
jmp 0x19b7
movq (%r14), %rsi
leaq 0xb9f(%rip), %rdi       # 0x2524
xorl %eax, %eax
callq 0x1080
leaq 0x1459(%rip), %rdi      # 0x2dec
callq 0x1050
leaq 0x1493(%rip), %rdi      # 0x2e32
callq 0x1050
leaq 0x14df(%rip), %rdi      # 0x2e8a
callq 0x1050
leaq 0x1509(%rip), %rdi      # 0x2ec0
callq 0x1050
jmp 0x19db
leaq 0x834(%rip), %rdi       # 0x21f9
movq %rbp, %rsi
jmp 0x19d4
leaq 0x85f(%rip), %rdi       # 0x2230
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1080
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %ebx, %eax
addq $0xcb8, %rsp            # imm = 0xCB8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x13cb(%rip), %rdi      # 0x2dc6
jmp 0x19b7
leaq 0x8a2(%rip), %rdi       # 0x22a6
movq %rbp, %rsi
movl %r15d, %edx
xorl %eax, %eax
callq 0x1080
leaq 0x8ce(%rip), %rdi       # 0x22e6
movl %r12d, %esi
xorl %eax, %eax
callq 0x1080
leaq 0x1373(%rip), %rdi      # 0x2d9c
jmp 0x19b7
leaq (%rsp,%rcx), %rbx
addq $0x30, %rbx
leaq 0x1168(%rip), %rdi      # 0x2ba2
callq 0x1050
movl 0x18(%rbx), %eax
movq %rax, 0x10(%rsp)
movq %r14, %rbx
addq %rax, %rbx
movq %rbx, %rdi
xorl %esi, %esi
movq %rbp, %r12
movq 0x8(%rsp), %rbp
movq %rbp, %rdx
callq 0x1090
movq %rbx, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x10c0
leaq 0x1148(%rip), %rdi      # 0x2bbf
callq 0x1050
movq %rbx, %rdi
movl %ebp, %esi
callq 0x1bf4
movq %rax, %rbx
leaq 0xa1a(%rip), %rdi       # 0x24aa
movl %ebx, %esi
xorl %eax, %eax
callq 0x1080
movq 0x10(%rsp), %rax
addl $0xffffff00, %eax       # imm = 0xFFFFFF00
cltq
movl %ebx, (%r14,%rax)
movq %r14, %rdi
movl 0x1c(%rsp), %ebp
movl %ebp, %esi
callq 0x13aa
movl %eax, %ebx
leaq 0xa01(%rip), %rdi       # 0x24c1
movl %eax, %esi
xorl %eax, %eax
callq 0x1080
movl %ebx, (%r14)
leaq 0x10fd(%rip), %rdi      # 0x2bd0
callq 0x1050
leaq 0x30(%rsp), %rdx
movq %r14, %rdi
movl %ebp, %esi
movl $0x64, %ecx
callq 0x13de
testl %eax, %eax
jle 0x1b54
movl %eax, %ebx
leaq 0x9de(%rip), %rdi       # 0x24d7
movl %eax, %esi
xorl %eax, %eax
callq 0x1080
leaq 0x30(%rsp), %rdi
movl %ebx, %esi
callq 0x1322
cmpl %r15d, %ebx
jne 0x1b5d
leaq 0x9e6(%rip), %rdi       # 0x2500
xorl %ebx, %ebx
movq 0x28(%rsp), %r15
movq %r15, %rsi
xorl %eax, %eax
callq 0x1080
movq %r15, %rdi
movl %ebp, %esi
movq %r14, %rdx
callq 0x1b86
testl %eax, %eax
je 0x1b75
leaq 0x10c0(%rip), %rdi      # 0x2c03
jmp 0x19b7
leaq 0x1233(%rip), %rdi      # 0x2d82
jmp 0x19b7
leaq 0x1110(%rip), %rdi      # 0x2c6b
jmp 0x1b64
leaq 0x10b2(%rip), %rdi      # 0x2c16
callq 0x1050
leaq 0x1146(%rip), %rdi      # 0x2cb6
jmp 0x19b7
leaq 0x1081(%rip), %rdi      # 0x2bfd
callq 0x1050
jmp 0x19e0