// File: /workspace/github_c_makefile_star2_O2/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 0xfde(%rip), %rdi       # 0x2670
callq 0x1050
leaq 0x1007(%rip), %rdi      # 0x26a5
callq 0x1050
leaq 0x104b(%rip), %rdi      # 0x26f5
callq 0x1050
leaq 0x1092(%rip), %rdi      # 0x2748
callq 0x1050
leaq 0x10da(%rip), %rdi      # 0x279c
callq 0x1050
leaq 0x111d(%rip), %rdi      # 0x27eb
callq 0x1050
leaq 0x100f(%rip), %rdi      # 0x26e9
callq 0x1050
pushq $0xa
popq %rdi
callq 0x1040
leaq 0x1150(%rip), %rdi      # 0x283e
callq 0x1050
leaq 0x1198(%rip), %rdi      # 0x2892
callq 0x1050
leaq 0x11dd(%rip), %rdi      # 0x28e3
callq 0x1050
leaq 0x1206(%rip), %rdi      # 0x2918
callq 0x1050
leaq 0x1250(%rip), %rdi      # 0x296e
callq 0x1050
leaq 0x1296(%rip), %rdi      # 0x29c0
callq 0x1050
leaq 0x12a4(%rip), %rdi      # 0x29da
callq 0x1050
leaq 0x12ae(%rip), %rdi      # 0x29f0
callq 0x1050
leaq 0x12e4(%rip), %rdi      # 0x2a32
callq 0x1050
cmpl $0x5, %ebx
jne 0x1961
movq 0x8(%r14), %rbp
movq 0x10(%r14), %rbx
movq 0x18(%r14), %rdi
callq 0x10f0
movl %eax, %r12d
movq 0x20(%r14), %r15
leaq 0xa36(%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 0x19a4
movq %rax, %r14
leaq 0x20(%rsp), %rsi
movq %rbx, %rdi
callq 0x120c
testq %rax, %rax
je 0x19b0
movq %rax, %r13
movq %r15, 0x28(%rsp)
leaq 0xa9e(%rip), %rdi       # 0x2269
movq %rbp, %rsi
xorl %eax, %eax
callq 0x1080
movl 0x24(%rsp), %esi
leaq 0x30(%rsp), %rdx
pushq $0x64
popq %rcx
movq %r14, %rdi
movl %esi, 0x1c(%rsp)
callq 0x13e0
testl %eax, %eax
jle 0x19d6
movl %eax, %r15d
leaq 0xa87(%rip), %rdi       # 0x2286
movl %eax, %esi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x1080
leaq 0x30(%rsp), %rdi
movl %r15d, %esi
callq 0x1320
pushq $0xa
popq %rdi
callq 0x1040
cmpl %r15d, %r12d
jge 0x19df
leaq 0xaf4(%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 0x18fc
leaq 0x11ec(%rip), %rdi      # 0x2a4c
movq %rcx, 0x10(%rsp)
callq 0x1050
leaq 0x122b(%rip), %rdi      # 0x2a9c
callq 0x1050
leaq 0xad3(%rip), %rdi       # 0x2350
movl %r12d, %esi
movq %rbp, %rbx
movq 0x8(%rsp), %rbp
movl %ebp, %edx
xorl %eax, %eax
callq 0x1080
leaq 0xae7(%rip), %rdi       # 0x237f
movl %r12d, %esi
movl %ebx, %edx
xorl %eax, %eax
callq 0x1080
movl %ebp, %esi
movq %rbx, %rbp
subl %ebp, %esi
leaq 0xb0a(%rip), %rdi       # 0x23bc
xorl %eax, %eax
callq 0x1080
leaq 0x122b(%rip), %rdi      # 0x2aeb
callq 0x1050
leaq 0x126c(%rip), %rdi      # 0x2b38
callq 0x1050
leaq 0x1175(%rip), %rdi      # 0x2a4d
callq 0x1050
leaq 0x129d(%rip), %rdi      # 0x2b81
callq 0x1050
callq 0x10a0
movq 0x10(%rsp), %rcx
cmpl $0x79, %eax
jne 0x1b59
cmpl %ebp, 0x8(%rsp)
jae 0x1a0d
leaq 0x13e1(%rip), %rdi      # 0x2cee
callq 0x1050
leaq 0x1415(%rip), %rdi      # 0x2d2e
callq 0x1050
leaq 0xae1(%rip), %rdi       # 0x2406
movl %r12d, %esi
movl %ebp, %edx
xorl %eax, %eax
callq 0x1080
leaq 0xb09(%rip), %rdi       # 0x2441
movq 0x8(%rsp), %rbx
movl %ebx, %esi
xorl %eax, %eax
callq 0x1080
subl %ebx, %ebp
leaq 0xb28(%rip), %rdi       # 0x2477
movl %ebp, %esi
xorl %eax, %eax
callq 0x1080
leaq 0x140f(%rip), %rdi      # 0x2d6e
jmp 0x199d
movq (%r14), %rsi
leaq 0xbb9(%rip), %rdi       # 0x2524
xorl %eax, %eax
callq 0x1080
leaq 0x1473(%rip), %rdi      # 0x2dec
callq 0x1050
leaq 0x14ad(%rip), %rdi      # 0x2e32
callq 0x1050
leaq 0x14f9(%rip), %rdi      # 0x2e8a
callq 0x1050
leaq 0x1523(%rip), %rdi      # 0x2ec0
callq 0x1050
jmp 0x19c1
leaq 0x84e(%rip), %rdi       # 0x21f9
movq %rbp, %rsi
jmp 0x19ba
leaq 0x879(%rip), %rdi       # 0x2230
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1080
pushq $-0x1
popq %rax
addq $0xcb8, %rsp            # imm = 0xCB8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x13e9(%rip), %rdi      # 0x2dc6
jmp 0x199d
leaq 0x8c0(%rip), %rdi       # 0x22a6
movq %rbp, %rsi
movl %r15d, %edx
xorl %eax, %eax
callq 0x1080
leaq 0x8ec(%rip), %rdi       # 0x22e6
movl %r12d, %esi
xorl %eax, %eax
callq 0x1080
leaq 0x1391(%rip), %rdi      # 0x2d9c
jmp 0x199d
leaq (%rsp,%rcx), %rbx
addq $0x30, %rbx
leaq 0x1186(%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 0x1166(%rip), %rdi      # 0x2bbf
callq 0x1050
movq %rbx, %rdi
movl %ebp, %esi
callq 0x1ba4
movq %rax, %rbx
leaq 0xa38(%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 0x13ac
movl %eax, %ebx
leaq 0xa1f(%rip), %rdi       # 0x24c1
movl %eax, %esi
xorl %eax, %eax
callq 0x1080
movl %ebx, (%r14)
leaq 0x111b(%rip), %rdi      # 0x2bd0
callq 0x1050
leaq 0x30(%rsp), %rdx
pushq $0x64
popq %rcx
movq %r14, %rdi
movl %ebp, %esi
callq 0x13e0
testl %eax, %eax
jle 0x1b65
movl %eax, %ebx
leaq 0x9fa(%rip), %rdi       # 0x24d7
movl %eax, %esi
xorl %eax, %eax
callq 0x1080
leaq 0x30(%rsp), %rdi
movl %ebx, %esi
callq 0x1320
cmpl %r15d, %ebx
jne 0x1b6e
leaq 0xa02(%rip), %rdi       # 0x2500
movq 0x28(%rsp), %r15
movq %r15, %rsi
xorl %eax, %eax
callq 0x1080
leaq 0xa67(%rip), %rsi       # 0x257b
movq %r15, %rdi
callq 0x10e0
testq %rax, %rax
je 0x1b86
movq %rax, %rbx
movslq %ebp, %rsi
pushq $0x1
popq %rdx
movq %r14, %rdi
movq %rax, %rcx
callq 0x1100
movq %rax, %r14
movq %rbx, %rdi
callq 0x1070
cmpl $0x1, %r14d
jne 0x1b97
leaq 0x10b0(%rip), %rdi      # 0x2bfd
callq 0x1050
xorl %eax, %eax
jmp 0x19c4
leaq 0x1222(%rip), %rdi      # 0x2d82
jmp 0x199d
leaq 0x10ff(%rip), %rdi      # 0x2c6b
jmp 0x1b75
leaq 0x10a1(%rip), %rdi      # 0x2c16
callq 0x1050
leaq 0x1135(%rip), %rdi      # 0x2cb6
jmp 0x199d
leaq 0x9f2(%rip), %rdi       # 0x257f
movq %r15, %rsi
xorl %eax, %eax
callq 0x1080
leaq 0x1065(%rip), %rdi      # 0x2c03
jmp 0x199d
nop