// File: /workspace/github_c_makefile_star2_O0/evilwombat[P]gopro-fw-tools/h3-wifi-address.c
// Function: main
int main(int argc, char **argv)
{
	FILE *in_fd;
	char *fname, *output_name;
	struct stat st;
	int ret;
	unsigned int size, hdr_size;
	unsigned char *buf;
	uint32_t crc, hdr_crc;
	int patch_byte = 8;
	struct wifi_fw_type *wifi_fw = wifi_fw_list;

	printf("MAKE SURE YOU HAVE READ THE INSTRUCTIONS!\n");
	printf("The author makes absolutely NO GUARANTEES of the correctness of this program\n");
	printf("and takes absolutely NO RESPONSIBILITY OR LIABILITY for any consequences that\n");
	printf("arise from its use. This program could SEVERELY mess up your camera, totally\n");
	printf("destroy it, cause it to catch fire. It could also destroy your computer, burn\n");
	printf("down your house, etc. The author takes no responsibility for the consequences\n");
	printf("of using this program. Use it at your own risk! You have been warned.\n");
	printf("\n");

	if (argc != 4) {
		print_usage(argv[0]);
		return -1;
	}

	fname = argv[1];

	patch_byte = atoi(argv[2]);

	if (patch_byte < 0 || patch_byte > 255) {
		printf("Bad patching value: %d\n", patch_byte);
		return -1;
	}

	printf("Patching camera address to 10.%d.5.9\n", patch_byte);
	printf("Patching computer address to 10.%d.5.109\n", patch_byte);
	printf("\n");

	output_name = argv[3];

	ret = stat(fname, &st);
	if (ret) {
		printf("Error: Could not stat %s\n", fname);
		return -1;
	}

	size = st.st_size;
	if (size <= 0) {
		printf("Bad file size: %d\n", size);
		return -1;
	}

	buf = malloc(size);
	if (!buf) {
		printf("Could not allocate %d bytes\n", size);
		return -1;
	}

	in_fd = fopen(fname, "rb");

	ret = read_file(in_fd, buf, size);
	if (ret) {
		printf("Could not read file: %d\n", ret);
		return -1;
	}
	fclose(in_fd);

	hdr_size = read_word(buf, SIZE_OFFSET);
	hdr_crc = read_word(buf, CRC_OFFSET);

	printf("Wifi FW header reports size: %08x\n", hdr_size);
	printf("Wifi FW header reports CRC : %08x\n", hdr_crc);

	printf("Actual file size: %08x\n", size);
	if (size != hdr_size) {
		printf("File size does not match size reported in header.\n");
		goto fail;
	}

	write_word(buf, CRC_OFFSET, 0x00000000);
	crc = crc32(buf, size);

	printf("Actual file CRC : %08x\n", crc);
	if (crc != hdr_crc) {
		printf("File CRC does not match CRC reported in header.\n");
		goto fail;
	}

	/*
	 * Checking both the size and CRC is probably redundant, but we may
	 * as well be careful
	 */
	while (wifi_fw->name) {
		if (wifi_fw->size == size && wifi_fw->crc == crc)
			break;
		wifi_fw++;
	}

	if (wifi_fw->name == NULL) {
		printf("\nUnrecognized firmware file!\n");
		printf("This only works on the following firmware files:\n");

		wifi_fw = wifi_fw_list;
		while (wifi_fw->name) {
			printf("\t * %s\n", wifi_fw->name);
			wifi_fw++;
		}

		goto fail;
	}

	printf("Detected firmware type: \"%s\"\n", wifi_fw->name);

	printf("Patching firmware...\n");
	ret = patch_buffer(buf, wifi_fw->patch, patch_byte, wifi_fw->old_val);
	if (ret) {
		printf("Error patching buffer: %d\n", ret);
		goto fail;
	}

	crc = crc32(buf, size);
	printf("New CRC: %08x\n", crc);
	write_word(buf, CRC_OFFSET, crc);

	printf("Saving output file: %s\n", output_name);
	ret = save_file(output_name, size, buf);
	if (ret) {
		printf("Error saving file: %d\n", ret);
		goto fail;
	}
	printf("Done.\n");

fail:
	free(buf);
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x8, -0xdc(%rbp)
leaq 0x2ee6(%rip), %rax      # 0x40d0
movq %rax, -0xe8(%rbp)
leaq 0xe9e(%rip), %rdi       # 0x2096
movb $0x0, %al
callq 0x1060
leaq 0xebb(%rip), %rdi       # 0x20c1
movb $0x0, %al
callq 0x1060
leaq 0xefb(%rip), %rdi       # 0x210f
movb $0x0, %al
callq 0x1060
leaq 0xf3c(%rip), %rdi       # 0x215e
movb $0x0, %al
callq 0x1060
leaq 0xf7c(%rip), %rdi       # 0x21ac
movb $0x0, %al
callq 0x1060
leaq 0xfbd(%rip), %rdi       # 0x21fb
movb $0x0, %al
callq 0x1060
leaq 0xffe(%rip), %rdi       # 0x224a
movb $0x0, %al
callq 0x1060
leaq 0xe65(%rip), %rdi       # 0x20bf
movb $0x0, %al
callq 0x1060
cmpl $0x4, -0x8(%rbp)
je 0x127f
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x1710
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x16fd
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x10a0
movl %eax, -0xdc(%rbp)
cmpl $0x0, -0xdc(%rbp)
jl 0x12b3
cmpl $0xff, -0xdc(%rbp)
jle 0x12d3
movl -0xdc(%rbp), %esi
leaq 0xfd1(%rip), %rdi       # 0x2291
movb $0x0, %al
callq 0x1060
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x16fd
movl -0xdc(%rbp), %esi
leaq 0xfc9(%rip), %rdi       # 0x22a9
movb $0x0, %al
callq 0x1060
movl -0xdc(%rbp), %esi
leaq 0xfdb(%rip), %rdi       # 0x22cf
movb $0x0, %al
callq 0x1060
leaq 0xdbd(%rip), %rdi       # 0x20bf
movb $0x0, %al
callq 0x1060
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x1070
movl %eax, -0xbc(%rbp)
cmpl $0x0, -0xbc(%rbp)
je 0x1352
movq -0x20(%rbp), %rsi
leaq 0xfba(%rip), %rdi       # 0x22f9
movb $0x0, %al
callq 0x1060
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x16fd
movq -0x88(%rbp), %rax
movl %eax, -0xc0(%rbp)
cmpl $0x0, -0xc0(%rbp)
ja 0x1388
movl -0xc0(%rbp), %esi
leaq 0xf9e(%rip), %rdi       # 0x2313
movb $0x0, %al
callq 0x1060
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x16fd
movl -0xc0(%rbp), %eax
movl %eax, %edi
callq 0x1080
movq %rax, -0xd0(%rbp)
cmpq $0x0, -0xd0(%rbp)
jne 0x13c6
movl -0xc0(%rbp), %esi
leaq 0xf73(%rip), %rdi       # 0x2326
movb $0x0, %al
callq 0x1060
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x16fd
movq -0x20(%rbp), %rdi
leaq 0xf72(%rip), %rsi       # 0x2343
callq 0x1090
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
movq -0xd0(%rbp), %rsi
movl -0xc0(%rbp), %edx
callq 0x1770
movl %eax, -0xbc(%rbp)
cmpl $0x0, -0xbc(%rbp)
je 0x141f
movl -0xbc(%rbp), %esi
leaq 0xf3a(%rip), %rdi       # 0x2346
movb $0x0, %al
callq 0x1060
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x16fd
movq -0x18(%rbp), %rdi
callq 0x1050
movq -0xd0(%rbp), %rdi
movl $0x3f8, %esi            # imm = 0x3F8
callq 0x17c0
movl %eax, -0xc4(%rbp)
movq -0xd0(%rbp), %rdi
movl $0x3fc, %esi            # imm = 0x3FC
callq 0x17c0
movl %eax, -0xd8(%rbp)
movl -0xc4(%rbp), %esi
leaq 0xefc(%rip), %rdi       # 0x235f
movb $0x0, %al
callq 0x1060
movl -0xd8(%rbp), %esi
leaq 0xf0b(%rip), %rdi       # 0x2382
movb $0x0, %al
callq 0x1060
movl -0xc0(%rbp), %esi
leaq 0xf1a(%rip), %rdi       # 0x23a5
movb $0x0, %al
callq 0x1060
movl -0xc0(%rbp), %eax
cmpl -0xc4(%rbp), %eax
je 0x14b3
leaq 0xf16(%rip), %rdi       # 0x23bd
movb $0x0, %al
callq 0x1060
jmp 0x16ea
movq -0xd0(%rbp), %rdi
movl $0x3fc, %esi            # imm = 0x3FC
xorl %edx, %edx
callq 0x1820
movq -0xd0(%rbp), %rdi
movl -0xc0(%rbp), %esi
callq 0x19e0
movl %eax, -0xd4(%rbp)
movl -0xd4(%rbp), %esi
leaq 0xf05(%rip), %rdi       # 0x23f0
movb $0x0, %al
callq 0x1060
movl -0xd4(%rbp), %eax
cmpl -0xd8(%rbp), %eax
je 0x1513
leaq 0xf01(%rip), %rdi       # 0x2408
movb $0x0, %al
callq 0x1060
jmp 0x16ea
jmp 0x1515
movq -0xe8(%rbp), %rax
cmpq $0x0, (%rax)
je 0x155c
movq -0xe8(%rbp), %rax
movl 0x8(%rax), %eax
cmpl -0xc0(%rbp), %eax
jne 0x1548
movq -0xe8(%rbp), %rax
movl 0xc(%rax), %eax
cmpl -0xd4(%rbp), %eax
jne 0x1548
jmp 0x155c
movq -0xe8(%rbp), %rax
addq $0x20, %rax
movq %rax, -0xe8(%rbp)
jmp 0x1515
movq -0xe8(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x15d1
leaq 0xec9(%rip), %rdi       # 0x2439
movb $0x0, %al
callq 0x1060
leaq 0xed9(%rip), %rdi       # 0x2457
movb $0x0, %al
callq 0x1060
leaq 0x2b44(%rip), %rax      # 0x40d0
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rax
cmpq $0x0, (%rax)
je 0x15cc
movq -0xe8(%rbp), %rax
movq (%rax), %rsi
leaq 0xed8(%rip), %rdi       # 0x2489
movb $0x0, %al
callq 0x1060
movq -0xe8(%rbp), %rax
addq $0x20, %rax
movq %rax, -0xe8(%rbp)
jmp 0x1593
jmp 0x16ea
movq -0xe8(%rbp), %rax
movq (%rax), %rsi
leaq 0xeaf(%rip), %rdi       # 0x2491
movb $0x0, %al
callq 0x1060
leaq 0xebf(%rip), %rdi       # 0x24af
movb $0x0, %al
callq 0x1060
movq -0xd0(%rbp), %rdi
movq -0xe8(%rbp), %rax
movq 0x18(%rax), %rsi
movl -0xdc(%rbp), %eax
movb %al, %cl
movq -0xe8(%rbp), %rax
movzbl %cl, %edx
movzbl 0x10(%rax), %ecx
callq 0x1890
movl %eax, -0xbc(%rbp)
cmpl $0x0, -0xbc(%rbp)
je 0x164c
movl -0xbc(%rbp), %esi
leaq 0xe85(%rip), %rdi       # 0x24c5
movb $0x0, %al
callq 0x1060
jmp 0x16ea
movq -0xd0(%rbp), %rdi
movl -0xc0(%rbp), %esi
callq 0x19e0
movl %eax, -0xd4(%rbp)
movl -0xd4(%rbp), %esi
leaq 0xe6f(%rip), %rdi       # 0x24e0
movb $0x0, %al
callq 0x1060
movq -0xd0(%rbp), %rdi
movl -0xd4(%rbp), %edx
movl $0x3fc, %esi            # imm = 0x3FC
callq 0x1820
movq -0x28(%rbp), %rsi
leaq 0xe55(%rip), %rdi       # 0x24ef
movb $0x0, %al
callq 0x1060
movq -0x28(%rbp), %rdi
movl -0xc0(%rbp), %esi
movq -0xd0(%rbp), %rdx
callq 0x1950
movl %eax, -0xbc(%rbp)
cmpl $0x0, -0xbc(%rbp)
je 0x16dc
movl -0xbc(%rbp), %esi
leaq 0xe34(%rip), %rdi       # 0x2507
movb $0x0, %al
callq 0x1060
jmp 0x16ea
leaq 0xe3b(%rip), %rdi       # 0x251e
movb $0x0, %al
callq 0x1060
movq -0xd0(%rbp), %rdi
callq 0x1030
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xf0, %rsp
popq %rbp
retq
nopl (%rax)