// File: /workspace/github_c_makefile_star2_O2/blasphemy[P]mmc-utils/mmc_cmds.c
// Function: do_rpmb_read_block
int do_rpmb_read_block(int nargs, char **argv)
{
	int i, ret, dev_fd, data_fd, key_fd = -1;
	uint16_t addr;
	/*
	 * for reading RPMB, number of blocks is set by CMD23 only, the packet
	 * frame field for that is set to 0. So, the type is not u16 but uint!
	 */
	unsigned int blocks_cnt;
	unsigned char key[32];
	struct rpmb_frame frame_in = {
		.req_resp    = htobe16(MMC_RPMB_READ),
	}, *frame_out_p;

	if (nargs != 5 && nargs != 6) {
		fprintf(stderr, "Usage: mmc rpmb read-block </path/to/mmcblkXrpmb> <address> <blocks count> </path/to/output_file> [/path/to/key]\n");
		exit(1);
	}

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

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

	/* Get blocks count */
	errno = 0;
	blocks_cnt = strtol(argv[3], NULL, 0);
	if (errno) {
		perror("incorrect blocks count");
		exit(1);
	}

	if (!blocks_cnt) {
		printf("please, specify valid blocks count number\n");
		exit(1);
	}

	frame_out_p = calloc(sizeof(*frame_out_p), blocks_cnt);
	if (!frame_out_p) {
		printf("can't allocate memory for RPMB outer frames\n");
		exit(1);
	}

	/* Write 256b data */
	if (0 == strcmp(argv[4], "-"))
		data_fd = STDOUT_FILENO;
	else {
		data_fd = open(argv[4], O_WRONLY | O_CREAT | O_APPEND,
					   S_IRUSR | S_IWUSR);
		if (data_fd < 0) {
			perror("can't open output file");
			exit(1);
		}
	}

	/* Key is specified */
	if (nargs == 6) {
		if (0 == strcmp(argv[5], "-"))
			key_fd = STDIN_FILENO;
		else {
			key_fd = open(argv[5], O_RDONLY);
			if (key_fd < 0) {
				perror("can't open input key file");
				exit(1);
			}
		}

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

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

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

	/* Do we have to verify data against key? */
	if (nargs == 6) {
		unsigned char mac[32];
		hmac_sha256_ctx ctx;
		struct rpmb_frame *frame_out = NULL;

		hmac_sha256_init(&ctx, key, sizeof(key));
		for (i = 0; i < blocks_cnt; i++) {
			frame_out = &frame_out_p[i];
			hmac_sha256_update(&ctx, frame_out->data,
							   sizeof(*frame_out) -
								   offsetof(struct rpmb_frame, data));
		}

		hmac_sha256_final(&ctx, mac, sizeof(mac));

		/* Impossible */
		assert(frame_out);

		/* Compare calculated MAC and MAC from last frame */
		if (memcmp(mac, frame_out->key_mac, sizeof(mac))) {
			printf("RPMB MAC missmatch\n");
			exit(1);
		}
	}

	/* Write data */
	for (i = 0; i < blocks_cnt; i++) {
		struct rpmb_frame *frame_out = &frame_out_p[i];
		ret = DO_IO(write, data_fd, frame_out->data, sizeof(frame_out->data));
		if (ret < 0) {
			perror("write the data");
			exit(1);
		} else if (ret != sizeof(frame_out->data)) {
			printf("Data must be %lu bytes length, but we wrote only %d, exit\n",
				   (unsigned long)sizeof(frame_out->data),
				   ret);
			exit(1);
		}
	}

	free(frame_out_p);
	close(dev_fd);
	if (data_fd != STDOUT_FILENO)
		close(data_fd);
	if (key_fd != -1 && key_fd != STDIN_FILENO)
		close(key_fd);

	return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x588, %rsp            # imm = 0x588
movq %rsi, %r12
movl %edi, %r13d
leaq 0x68(%rsp), %rbx
movl $0x200, %edx            # imm = 0x200
movq %rbx, %rdi
xorl %esi, %esi
callq 0x30e0
movw $0x400, 0x1fe(%rbx)     # imm = 0x400
leal -0x7(%r13), %eax
cmpl $-0x3, %eax
jbe 0x7354
movq 0x8(%r12), %rdi
pushq $0x2
popq %rsi
xorl %eax, %eax
callq 0x31e0
movl %eax, 0xc(%rsp)
testl %eax, %eax
js 0x7376
callq 0x3050
movq %rax, %r14
andl $0x0, (%rax)
movq 0x10(%r12), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3180
cmpl $0x0, (%r14)
jne 0x737f
rolw $0x8, %ax
movw %ax, 0x260(%rsp)
movq 0x18(%r12), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3180
cmpl $0x0, (%r14)
jne 0x7388
movq %rax, %rbx
testl %ebx, %ebx
je 0x7391
movl %ebx, %esi
movl $0x200, %edi            # imm = 0x200
callq 0x3140
testq %rax, %rax
je 0x739a
movq %rax, %r15
movq 0x20(%r12), %rdi
cmpb $0x2d, (%rdi)
jne 0x7093
cmpb $0x0, 0x1(%rdi)
je 0x7328
movl $0x441, %esi            # imm = 0x441
movl $0x180, %edx            # imm = 0x180
xorl %eax, %eax
callq 0x31e0
movl %eax, %ebp
testl %eax, %eax
js 0x73c8
cmpl $0x6, %r13d
movq %r13, 0x10(%rsp)
jne 0x7165
movq 0x28(%r12), %rdi
cmpb $0x2d, (%rdi)
jne 0x70d1
cmpb $0x0, 0x1(%rdi)
je 0x7330
xorl %esi, %esi
xorl %eax, %eax
callq 0x31e0
movl %eax, %edx
testl %eax, %eax
js 0x740d
pushq $0x20
popq %r12
xorl %r13d, %r13d
movq %rdx, 0x18(%rsp)
leaq (%rsp,%r13), %rsi
addq $0x40, %rsi
movq %r12, %rdx
subq %r13, %rdx
movq 0x18(%rsp), %rdi
callq 0x3110
testq %rax, %rax
jns 0x7117
cmpl $0x4, (%r14)
jne 0x734b
testq %rax, %rax
movl $0x0, %ecx
cmovgq %rax, %rcx
addq %rcx, %r13
testq %rax, %rax
je 0x7131
cmpq $0x20, %r13
jne 0x70f0
testl %r13d, %r13d
js 0x734b
movl %r13d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl $0x20, %eax
je 0x716d
leaq 0xa0b6(%rip), %rsi      # 0x11204
pushq $0x1
popq %rbx
pushq $0x20
popq %rdx
movl %ebx, %edi
movl %r13d, %ecx
xorl %eax, %eax
callq 0x31d0
jmp 0x7406
pushq $-0x1
popq %rax
movq %rax, 0x18(%rsp)
leaq 0x68(%rsp), %rsi
movl 0xc(%rsp), %edi
movq %r15, %rdx
movl %ebx, %ecx
callq 0x6c66
testl %eax, %eax
jne 0x73a3
leal -0x1(%rbx), %eax
shlq $0x9, %rax
movzwl 0x1fc(%r15,%rax), %eax
testw %ax, %ax
movq 0x10(%rsp), %rcx
jne 0x73ac
cmpl $0x6, %ecx
movq %r15, 0x10(%rsp)
jne 0x71ff
leaq 0x268(%rsp), %rdi
leaq 0x40(%rsp), %rsi
pushq $0x20
popq %rdx
callq 0xb2c3
movl %ebx, %r12d
xorl %r13d, %r13d
movq %r15, %rax
movq %r12, %r15
subq $0x1, %r15
jb 0x7204
movq %rax, %rbx
leaq 0xe4(%rax), %rsi
leaq 0x268(%rsp), %rdi
movl $0x11c, %edx            # imm = 0x11C
callq 0xb405
leaq 0x200(%rbx), %rax
movq %rbx, %r13
jmp 0x71d1
movl %ebx, %r12d
jmp 0x7246
leaq 0x268(%rsp), %rdi
leaq 0x20(%rsp), %rsi
pushq $0x20
popq %rdx
callq 0xb40a
testq %r13, %r13
je 0x73d4
addq $0xc4, %r13
leaq 0x20(%rsp), %rdi
pushq $0x20
popq %rdx
movq %r13, %rsi
callq 0x31b0
testl %eax, %eax
movq 0x10(%rsp), %r15
jne 0x73f3
xorl %r13d, %r13d
xorl %ebx, %ebx
cmpq %r12, %r13
je 0x72e5
movq %r13, %rax
shlq $0x9, %rax
addq %rax, %r15
addq $0xe4, %r15
xorl %ebx, %ebx
leaq (%r15,%rbx), %rsi
movl $0x100, %edx            # imm = 0x100
subq %rbx, %rdx
movl %ebp, %edi
callq 0x3080
testq %rax, %rax
jns 0x7289
cmpl $0x4, (%r14)
jne 0x7337
testq %rax, %rax
movl $0x0, %ecx
cmovgq %rax, %rcx
addq %rcx, %rbx
testq %rax, %rax
je 0x72a6
cmpq $0x100, %rbx            # imm = 0x100
jne 0x7267
testl %ebx, %ebx
js 0x7337
movl %ebx, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
incq %r13
cmpl $0x100, %eax            # imm = 0x100
movq 0x10(%rsp), %r15
je 0x724b
leaq 0x9fd0(%rip), %rsi      # 0x1129b
pushq $0x1
popq %rbp
movl $0x100, %edx            # imm = 0x100
movl %ebp, %edi
movl %ebx, %ecx
xorl %eax, %eax
callq 0x31d0
movl %ebp, %edi
callq 0x3230
movq %r15, %rdi
callq 0x3030
movl 0xc(%rsp), %edi
callq 0x3100
cmpl $0x1, %ebp
je 0x7302
movl %ebp, %edi
callq 0x3100
movq 0x18(%rsp), %rdi
leal -0x1(%rdi), %eax
cmpl $-0x3, %eax
ja 0x7314
callq 0x3100
movl %ebx, %eax
addq $0x588, %rsp            # imm = 0x588
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x1
popq %rbp
jmp 0x70ae
xorl %edx, %edx
jmp 0x70e4
leaq 0x9f4e(%rip), %rdi      # 0x1128c
callq 0x31f0
pushq $0x1
popq %rdi
callq 0x3230
leaq 0x9ea0(%rip), %rdi      # 0x111f2
jmp 0x733e
movq 0x10c95(%rip), %rax     # 0x17ff0
movq (%rax), %rdi
leaq 0x9d69(%rip), %rdx      # 0x110ce
pushq $0x1
popq %rbx
movl %ebx, %esi
xorl %eax, %eax
callq 0x3250
jmp 0x7406
leaq 0x9c61(%rip), %rdi      # 0x10fde
jmp 0x733e
leaq 0x9dba(%rip), %rdi      # 0x11140
jmp 0x733e
leaq 0x9dc3(%rip), %rdi      # 0x11152
jmp 0x733e
leaq 0x9dd1(%rip), %rsi      # 0x11169
jmp 0x73fa
leaq 0x9df3(%rip), %rsi      # 0x11194
jmp 0x73fa
leaq 0x9c9f(%rip), %rdi      # 0x11049
jmp 0x733e
rolw $0x8, %ax
movzwl %ax, %edx
leaq 0x9ca1(%rip), %rsi      # 0x1105b
pushq $0x1
popq %rbx
movl %ebx, %edi
xorl %eax, %eax
callq 0x31d0
jmp 0x7406
leaq 0x9df2(%rip), %rdi      # 0x111c1
jmp 0x733e
leaq 0x9e63(%rip), %rdi      # 0x1123e
leaq 0x9e66(%rip), %rsi      # 0x11248
leaq 0x9e6a(%rip), %rcx      # 0x11253
movl $0x8b5, %edx            # imm = 0x8B5
callq 0x30d0
leaq 0x9e7e(%rip), %rsi      # 0x11278
pushq $0x1
popq %rbx
movl %ebx, %edi
xorl %eax, %eax
callq 0x31d0
movl %ebx, %edi
callq 0x3230
leaq 0x9dc4(%rip), %rdi      # 0x111d8
jmp 0x733e