// File: /workspace/github_c_makefile_star2_O1/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, %r12
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 0x7cb7
movq 0x8(%r12), %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x31d0
testl %eax, %eax
js 0x7cd6
movl %eax, %ebx
leaq 0xc(%rsp), %rsi
movl %eax, %edi
callq 0x74f4
testl %eax, %eax
jne 0x7cdb
movl 0xc(%rsp), %eax
bswapl %eax
movl %eax, 0x22c(%rsp)
callq 0x3050
movq %rax, %r15
movl $0x0, (%rax)
movq 0x10(%r12), %rdi
xorl %r14d, %r14d
xorl %esi, %esi
xorl %edx, %edx
callq 0x3180
cmpl $0x0, (%r15)
jne 0x7ceb
rolw $0x8, %ax
movw %ax, 0x230(%rsp)
movq 0x18(%r12), %r13
leaq 0x67f1(%rip), %rsi      # 0xe2fd
movq %r13, %rdi
callq 0x3150
testl %eax, %eax
je 0x7b2f
movq %r13, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x31d0
movl %eax, %r14d
testl %eax, %eax
js 0x7d55
leaq 0x11c(%rsp), %r13
xorl %ebp, %ebp
movq %r13, %rsi
addq %rbp, %rsi
movl $0x100, %edx            # imm = 0x100
subq %rbp, %rdx
movl %r14d, %edi
callq 0x3110
testq %rax, %rax
jns 0x7b5a
cmpl $0x4, (%r15)
jne 0x7b79
testq %rax, %rax
movl $0x0, %ecx
cmovgq %rax, %rcx
addq %rcx, %rbp
testq %rax, %rax
je 0x7b80
cmpq $0x100, %rbp            # imm = 0x100
jne 0x7b39
jmp 0x7b80
movq $-0x1, %rbp
testl %ebp, %ebp
js 0x7cf0
movl %ebp, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl $0x100, %eax            # imm = 0x100
jne 0x7cf5
movq 0x20(%r12), %r12
leaq 0x6757(%rip), %rsi      # 0xe2fd
movq %r12, %rdi
callq 0x3150
testl %eax, %eax
je 0x7bc9
movq %r12, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x31d0
movl %eax, %ebp
testl %eax, %eax
jns 0x7bcb
callq 0x3578
xorl %ebp, %ebp
xorl %r12d, %r12d
leaq (%rsp,%r12), %rsi
addq $0x10, %rsi
movl $0x20, %edx
subq %r12, %rdx
movl %ebp, %edi
callq 0x3110
testq %rax, %rax
jns 0x7bf0
cmpl $0x4, (%r15)
jne 0x7c0c
testq %rax, %rax
movl $0x0, %ecx
cmovgq %rax, %rcx
addq %rcx, %r12
testq %rax, %rax
je 0x7c13
cmpq $0x20, %r12
jne 0x7bce
jmp 0x7c13
movq $-0x1, %r12
testl %r12d, %r12d
js 0x7d0a
movl %r12d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl $0x20, %eax
jne 0x7d0f
leaq 0xfc(%rsp), %r8
leaq 0x10(%rsp), %rdi
movl $0x20, %esi
movq %r13, %rdx
movl $0x11c, %ecx            # imm = 0x11C
movl $0x20, %r9d
callq 0xbd3a
leaq 0x38(%rsp), %rsi
leaq 0x238(%rsp), %rdx
movl %ebx, %edi
movl $0x1, %ecx
callq 0x72af
testl %eax, %eax
jne 0x7d2c
movzwl 0x434(%rsp), %eax
testw %ax, %ax
jne 0x7d31
movl %ebx, %edi
callq 0x3100
testl %r14d, %r14d
je 0x7c98
movl %r14d, %edi
callq 0x3100
testl %ebp, %ebp
je 0x7ca3
movl %ebp, %edi
callq 0x3100
xorl %eax, %eax
addq $0x438, %rsp            # imm = 0x438
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xf332(%rip), %rax      # 0x16ff0
movq (%rax), %rdi
leaq 0x95e3(%rip), %rdx      # 0x112ab
movl $0x1, %esi
xorl %eax, %eax
callq 0x3240
jmp 0x7d4b
callq 0x35cc
leaq 0x962c(%rip), %rsi      # 0x1130e
movl $0x1, %edi
movl %eax, %edx
jmp 0x7d44
callq 0x354e
callq 0x35b7
leaq 0x9517(%rip), %rsi      # 0x11213
movl $0x100, %edx            # imm = 0x100
movl $0x1, %edi
movl %ebp, %ecx
jmp 0x7d23
callq 0x35a2
leaq 0x9304(%rip), %rsi      # 0x1101a
movl $0x20, %edx
movl $0x1, %edi
movl %r12d, %ecx
xorl %eax, %eax
callq 0x31c0
jmp 0x7d4b
callq 0x358d
rolw $0x8, %ax
movzwl %ax, %edx
leaq 0x932b(%rip), %rsi      # 0x1106a
movl $0x1, %edi
xorl %eax, %eax
callq 0x31c0
movl $0x1, %edi
callq 0x3220
callq 0x3563