// File: /workspace/github_c_makefile_star2_O1/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 0x101e(%rip), %rdi      # 0x2692
callq 0x1050
leaq 0x1047(%rip), %rdi      # 0x26c7
callq 0x1050
leaq 0x108b(%rip), %rdi      # 0x2717
callq 0x1050
leaq 0x10d2(%rip), %rdi      # 0x276a
callq 0x1050
leaq 0x111a(%rip), %rdi      # 0x27be
callq 0x1050
leaq 0x115d(%rip), %rdi      # 0x280d
callq 0x1050
leaq 0x104f(%rip), %rdi      # 0x270b
callq 0x1050
movl $0xa, %edi
callq 0x1040
leaq 0x118e(%rip), %rdi      # 0x2860
callq 0x1050
leaq 0x11d6(%rip), %rdi      # 0x28b4
callq 0x1050
leaq 0x121b(%rip), %rdi      # 0x2905
callq 0x1050
leaq 0x1244(%rip), %rdi      # 0x293a
callq 0x1050
leaq 0x128e(%rip), %rdi      # 0x2990
callq 0x1050
leaq 0x12d4(%rip), %rdi      # 0x29e2
callq 0x1050
leaq 0x12e2(%rip), %rdi      # 0x29fc
callq 0x1050
leaq 0x12ec(%rip), %rdi      # 0x2a12
callq 0x1050
leaq 0x1322(%rip), %rdi      # 0x2a54
callq 0x1050
cmpl $0x5, %ebx
jne 0x1949
movq 0x8(%r14), %rbp
movq 0x10(%r14), %rbx
movq 0x18(%r14), %rdi
callq 0x1100
movl %eax, %r12d
movq 0x20(%r14), %r15
leaq 0xa52(%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 0x121c
testq %rax, %rax
je 0x198c
movq %rax, %r14
leaq 0x20(%rsp), %rsi
movq %rbx, %rdi
callq 0x121c
testq %rax, %rax
je 0x1998
movq %rax, %r13
movq %r15, 0x28(%rsp)
leaq 0xaba(%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 0x13ee
testl %eax, %eax
jle 0x19c2
movl %eax, %r15d
leaq 0xaa1(%rip), %rdi       # 0x2286
movl %eax, %esi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x1080
leaq 0x30(%rsp), %rdi
movl %r15d, %esi
callq 0x1332
movl $0xa, %edi
callq 0x1040
cmpl %r15d, %r12d
jge 0x19cb
leaq 0xb0c(%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 0x18e4
leaq 0x1226(%rip), %rdi      # 0x2a6e
movq %rcx, 0x10(%rsp)
callq 0x1050
leaq 0x1265(%rip), %rdi      # 0x2abe
callq 0x1050
leaq 0xaeb(%rip), %rdi       # 0x2350
movl %r12d, %esi
movq %rbp, %rbx
movq 0x8(%rsp), %rbp
movl %ebp, %edx
xorl %eax, %eax
callq 0x1080
leaq 0xaff(%rip), %rdi       # 0x237f
movl %r12d, %esi
movl %ebx, %edx
xorl %eax, %eax
callq 0x1080
movl %ebp, %esi
movq %rbx, %rbp
subl %ebp, %esi
leaq 0xb22(%rip), %rdi       # 0x23bc
xorl %eax, %eax
callq 0x1080
leaq 0x1265(%rip), %rdi      # 0x2b0d
callq 0x1050
leaq 0x12a6(%rip), %rdi      # 0x2b5a
callq 0x1050
leaq 0x11af(%rip), %rdi      # 0x2a6f
callq 0x1050
leaq 0x12d7(%rip), %rdi      # 0x2ba3
callq 0x1050
callq 0x10a0
movq 0x10(%rsp), %rcx
cmpl $0x79, %eax
jne 0x1b16
cmpl %ebp, 0x8(%rsp)
jae 0x19f9
leaq 0x141b(%rip), %rdi      # 0x2d10
callq 0x1050
leaq 0x144f(%rip), %rdi      # 0x2d50
callq 0x1050
leaq 0xaf9(%rip), %rdi       # 0x2406
movl %r12d, %esi
movl %ebp, %edx
xorl %eax, %eax
callq 0x1080
leaq 0xb21(%rip), %rdi       # 0x2441
movq 0x8(%rsp), %rbx
movl %ebx, %esi
xorl %eax, %eax
callq 0x1080
subl %ebx, %ebp
leaq 0xb40(%rip), %rdi       # 0x2477
movl %ebp, %esi
xorl %eax, %eax
callq 0x1080
leaq 0x1449(%rip), %rdi      # 0x2d90
jmp 0x1985
movq (%r14), %rsi
leaq 0xbf3(%rip), %rdi       # 0x2546
xorl %eax, %eax
callq 0x1080
leaq 0x14ad(%rip), %rdi      # 0x2e0e
callq 0x1050
leaq 0x14e7(%rip), %rdi      # 0x2e54
callq 0x1050
leaq 0x1533(%rip), %rdi      # 0x2eac
callq 0x1050
leaq 0x155d(%rip), %rdi      # 0x2ee2
callq 0x1050
jmp 0x19a9
leaq 0x866(%rip), %rdi       # 0x21f9
movq %rbp, %rsi
jmp 0x19a2
leaq 0x891(%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 0x141f(%rip), %rdi      # 0x2de8
jmp 0x1985
leaq 0x8d4(%rip), %rdi       # 0x22a6
movq %rbp, %rsi
movl %r15d, %edx
xorl %eax, %eax
callq 0x1080
leaq 0x900(%rip), %rdi       # 0x22e6
movl %r12d, %esi
xorl %eax, %eax
callq 0x1080
leaq 0x13c7(%rip), %rdi      # 0x2dbe
jmp 0x1985
leaq (%rsp,%rcx), %rbx
addq $0x30, %rbx
leaq 0x11bc(%rip), %rdi      # 0x2bc4
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 0x10d0
leaq 0x119c(%rip), %rdi      # 0x2be1
callq 0x1050
movq %rbx, %rdi
movl %ebp, %esi
callq 0x1bc4
movq %rax, %rbx
leaq 0xa4c(%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 0x13ba
movl %eax, %ebx
leaq 0xa33(%rip), %rdi       # 0x24c1
movl %eax, %esi
xorl %eax, %eax
callq 0x1080
movl %ebx, (%r14)
leaq 0x1151(%rip), %rdi      # 0x2bf2
callq 0x1050
leaq 0x30(%rsp), %rdx
movq %r14, %rdi
movl %ebp, %esi
movl $0x64, %ecx
callq 0x13ee
testl %eax, %eax
jle 0x1b22
movl %eax, %ebx
leaq 0xa10(%rip), %rdi       # 0x24d7
movl %eax, %esi
xorl %eax, %eax
callq 0x1080
leaq 0x30(%rsp), %rdi
movl %ebx, %esi
callq 0x1332
cmpl %r15d, %ebx
jne 0x1b2b
leaq 0xa18(%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 0x1b54
testl %eax, %eax
je 0x1b43
leaq 0x1114(%rip), %rdi      # 0x2c25
jmp 0x1985
leaq 0x1287(%rip), %rdi      # 0x2da4
jmp 0x1985
leaq 0x1164(%rip), %rdi      # 0x2c8d
jmp 0x1b32
leaq 0x1106(%rip), %rdi      # 0x2c38
callq 0x1050
leaq 0x119a(%rip), %rdi      # 0x2cd8
jmp 0x1985
leaq 0x10d5(%rip), %rdi      # 0x2c1f
callq 0x1050
jmp 0x19ae