// File: /workspace/github_c_makefile_star2_O2/blasphemy[P]mmc-utils/mmc_cmds.c
// Function: do_rpmb_write_block
int do_rpmb_write_block(int nargs, char **argv)
{
	int ret, dev_fd, key_fd, data_fd;
	unsigned char key[32];
	uint16_t addr;
	unsigned int cnt;
	struct rpmb_frame frame_in = {
		.req_resp    = htobe16(MMC_RPMB_WRITE),
		.block_count = htobe16(1)
	}, frame_out;

	if (nargs != 5) {
		fprintf(stderr, "Usage: mmc rpmb write-block </path/to/mmcblkXrpmb> <address> </path/to/input_file> </path/to/key>\n");
		exit(1);
	}

	dev_fd = open(argv[1], O_RDWR);
	if (dev_fd < 0) {
		perror("device open");
		exit(1);
	}

	ret = rpmb_read_counter(dev_fd, &cnt);
	/* Check RPMB response */
	if (ret != 0) {
		printf("RPMB read counter operation failed, retcode 0x%04x\n", ret);
		exit(1);
	}
	frame_in.write_counter = htobe32(cnt);

	/* Get block address */
	errno = 0;
	addr = strtol(argv[2], NULL, 0);
	if (errno) {
		perror("incorrect address");
		exit(1);
	}
	frame_in.addr = htobe16(addr);

	/* Read 256b data */
	if (0 == strcmp(argv[3], "-"))
		data_fd = STDIN_FILENO;
	else {
		data_fd = open(argv[3], O_RDONLY);
		if (data_fd < 0) {
			perror("can't open input file");
			exit(1);
		}
	}

	ret = DO_IO(read, data_fd, frame_in.data, sizeof(frame_in.data));
	if (ret < 0) {
		perror("read the data");
		exit(1);
	} else if (ret != sizeof(frame_in.data)) {
		printf("Data must be %lu bytes length, but we read only %d, exit\n",
			   (unsigned long)sizeof(frame_in.data),
			   ret);
		exit(1);
	}

	/* Read the auth key */
	if (0 == strcmp(argv[4], "-"))
		key_fd = STDIN_FILENO;
	else {
		key_fd = open(argv[4], O_RDONLY);
		if (key_fd < 0) {
			perror("can't open key file");
			exit(1);
		}
	}

	ret = DO_IO(read, key_fd, key, sizeof(key));
	if (ret < 0) {
		perror("read the key");
		exit(1);
	} else if (ret != sizeof(key)) {
		printf("Auth key must be %lu bytes length, but we read only %d, exit\n",
			   (unsigned long)sizeof(key),
			   ret);
		exit(1);
	}

	/* Calculate HMAC SHA256 */
	hmac_sha256(
		key, sizeof(key),
		frame_in.data, sizeof(frame_in) - offsetof(struct rpmb_frame, data),
		frame_in.key_mac, sizeof(frame_in.key_mac));

	/* Execute RPMB op */
	ret = do_rpmb_op(dev_fd, &frame_in, &frame_out, 1);
	if (ret != 0) {
		perror("RPMB ioctl failed");
		exit(1);
	}

	/* Check RPMB response */
	if (frame_out.result != 0) {
		printf("RPMB operation failed, retcode 0x%04x\n",
			   be16toh(frame_out.result));
		exit(1);
	}

	close(dev_fd);
	if (data_fd != STDIN_FILENO)
		close(data_fd);
	if (key_fd != STDIN_FILENO)
		close(key_fd);

	return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x438, %rsp            # imm = 0x438
movq %rsi, %r15
movl %edi, %ebx
leaq 0x38(%rsp), %r14
movl $0x200, %edx            # imm = 0x200
movq %r14, %rdi
xorl %esi, %esi
callq 0x30e0
movw $0x100, 0x1fa(%r14)     # imm = 0x100
movw $0x300, 0x1fe(%r14)     # imm = 0x300
cmpl $0x5, %ebx
jne 0x7690
movq 0x8(%r15), %rdi
pushq $0x2
popq %rsi
xorl %eax, %eax
callq 0x31e0
testl %eax, %eax
js 0x76af
movl %eax, %ebx
leaq 0xc(%rsp), %rsi
movl %eax, %edi
callq 0x6e90
testl %eax, %eax
jne 0x76b8
movl 0xc(%rsp), %eax
bswapl %eax
movl %eax, 0x22c(%rsp)
callq 0x3050
movq %rax, %r14
andl $0x0, (%rax)
movq 0x10(%r15), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3180
cmpl $0x0, (%r14)
jne 0x76c8
rolw $0x8, %ax
movw %ax, 0x230(%rsp)
movq 0x18(%r15), %rdi
cmpb $0x2d, (%rdi)
jne 0x74da
cmpb $0x0, 0x1(%rdi)
je 0x7664
xorl %esi, %esi
xorl %eax, %eax
callq 0x31e0
movl %eax, %ebp
testl %eax, %eax
js 0x772a
leaq 0x11c(%rsp), %r12
xorl %r13d, %r13d
leaq (%r12,%r13), %rsi
movl $0x100, %edx            # imm = 0x100
subq %r13, %rdx
movl %ebp, %edi
callq 0x3110
testq %rax, %rax
jns 0x751a
cmpl $0x4, (%r14)
jne 0x7673
testq %rax, %rax
movl $0x0, %ecx
cmovgq %rax, %rcx
addq %rcx, %r13
testq %rax, %rax
je 0x7537
cmpq $0x100, %r13            # imm = 0x100
jne 0x74f8
testl %r13d, %r13d
js 0x7673
movl %r13d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl $0x100, %eax            # imm = 0x100
jne 0x76d1
movq 0x20(%r15), %rdi
cmpb $0x2d, (%rdi)
movl %ebx, 0x8(%rsp)
jne 0x756a
cmpb $0x0, 0x1(%rdi)
je 0x766b
xorl %esi, %esi
xorl %eax, %eax
callq 0x31e0
movl %eax, %r15d
testl %eax, %eax
js 0x7736
xorl %r13d, %r13d
pushq $0x20
popq %rbx
leaq (%rsp,%r13), %rsi
addq $0x10, %rsi
movq %rbx, %rdx
subq %r13, %rdx
movl %r15d, %edi
callq 0x3110
testq %rax, %rax
jns 0x75a9
cmpl $0x4, (%r14)
jne 0x767c
testq %rax, %rax
movl $0x0, %ecx
cmovgq %rax, %rcx
addq %rcx, %r13
testq %rax, %rax
je 0x75c3
cmpq $0x20, %r13
jne 0x7584
testl %r13d, %r13d
js 0x767c
movl %r13d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl $0x20, %eax
jne 0x76e2
leaq 0xfc(%rsp), %r8
leaq 0x10(%rsp), %rdi
pushq $0x20
popq %rsi
movq %r12, %rdx
movl $0x11c, %ecx            # imm = 0x11C
movl %esi, %r9d
callq 0xb466
leaq 0x38(%rsp), %rsi
leaq 0x238(%rsp), %rdx
pushq $0x1
popq %rcx
movl 0x8(%rsp), %ebx
movl %ebx, %edi
callq 0x6c66
testl %eax, %eax
jne 0x76fd
movzwl 0x434(%rsp), %eax
testw %ax, %ax
jne 0x7709
movl %ebx, %edi
callq 0x3100
testl %ebp, %ebp
je 0x7643
movl %ebp, %edi
callq 0x3100
testl %r15d, %r15d
je 0x7650
movl %r15d, %edi
callq 0x3100
xorl %eax, %eax
addq $0x438, %rsp            # imm = 0x438
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebp, %ebp
jmp 0x74ed
xorl %r15d, %r15d
jmp 0x757e
leaq 0x9d09(%rip), %rdi      # 0x11383
jmp 0x7683
leaq 0x997b(%rip), %rdi      # 0x10ffe
callq 0x31f0
pushq $0x1
popq %rdi
callq 0x3230
movq 0x10959(%rip), %rax     # 0x17ff0
movq (%rax), %rdi
leaq 0x9c35(%rip), %rdx      # 0x112d6
pushq $0x1
popq %rbx
movl %ebx, %esi
xorl %eax, %eax
callq 0x3250
jmp 0x7723
leaq 0x9928(%rip), %rdi      # 0x10fde
jmp 0x7683
leaq 0x9c7a(%rip), %rsi      # 0x11339
pushq $0x1
popq %rbx
movl %ebx, %edi
movl %eax, %edx
jmp 0x771c
leaq 0x9a71(%rip), %rdi      # 0x11140
jmp 0x7683
leaq 0x9b2c(%rip), %rsi      # 0x11204
pushq $0x1
popq %rbx
movl $0x100, %edx            # imm = 0x100
jmp 0x76ef
leaq 0x9922(%rip), %rsi      # 0x1100b
pushq $0x1
popq %rbx
pushq $0x20
popq %rdx
movl %ebx, %edi
movl %r13d, %ecx
xorl %eax, %eax
callq 0x31d0
jmp 0x7723
leaq 0x9945(%rip), %rdi      # 0x11049
jmp 0x7683
rolw $0x8, %ax
movzwl %ax, %edx
leaq 0x9944(%rip), %rsi      # 0x1105b
pushq $0x1
popq %rbx
movl %ebx, %edi
xorl %eax, %eax
callq 0x31d0
movl %ebx, %edi
callq 0x3230
leaq 0x9c3c(%rip), %rdi      # 0x1136d
jmp 0x7683
leaq 0x98ad(%rip), %rdi      # 0x10fea
jmp 0x7683