// File: /workspace/github_c_makefile_star2_O3/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, %r13
movl %edi, %r14d
leaq 0x68(%rsp), %rbx
movl $0x200, %edx            # imm = 0x200
movq %rbx, %rdi
xorl %esi, %esi
callq 0x30e0
movw $0x400, 0x1fe(%rbx)     # imm = 0x400
movq %r14, (%rsp)
leal -0x7(%r14), %eax
cmpl $-0x3, %eax
jbe 0x78dc
movq 0x8(%r13), %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x31d0
testl %eax, %eax
js 0x78fb
movl %eax, %ebp
callq 0x3050
movq %rax, %r14
movl $0x0, (%rax)
movq 0x10(%r13), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3180
cmpl $0x0, (%r14)
jne 0x7900
rolw $0x8, %ax
movw %ax, 0x260(%rsp)
movq 0x18(%r13), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3180
cmpl $0x0, (%r14)
jne 0x7905
movq %rax, %rbx
testl %ebx, %ebx
je 0x790a
movl %ebx, %esi
movl $0x200, %edi            # imm = 0x200
callq 0x3140
testq %rax, %rax
je 0x7913
movq %rax, %r15
movq 0x20(%r13), %rdi
cmpb $0x2d, (%rdi)
movl %ebp, 0xc(%rsp)
jne 0x7635
cmpb $0x0, 0x1(%rdi)
je 0x789c
movl $0x441, %esi            # imm = 0x441
movl $0x180, %edx            # imm = 0x180
xorl %eax, %eax
callq 0x31d0
movl %eax, %ebp
testl %eax, %eax
js 0x793d
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
cmpl $0x6, (%rsp)
jne 0x76e5
movq 0x28(%r13), %rdi
cmpb $0x2d, (%rdi)
jne 0x7673
cmpb $0x0, 0x1(%rdi)
je 0x78a6
xorl %esi, %esi
xorl %eax, %eax
callq 0x31d0
movl %eax, %r12d
testl %eax, %eax
js 0x7973
xorl %r13d, %r13d
leaq (%rsp,%r13), %rsi
addq $0x20, %rsi
movl $0x20, %edx
subq %r13, %rdx
movl %r12d, %edi
callq 0x3110
testq %rax, %rax
jns 0x76b1
cmpl $0x4, (%r14)
jne 0x78d7
testq %rax, %rax
movl $0x0, %ecx
cmovgq %rax, %rcx
addq %rcx, %r13
testq %rax, %rax
je 0x76cb
cmpq $0x20, %r13
jne 0x768a
testl %r13d, %r13d
js 0x78d7
movl %r13d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl $0x20, %eax
jne 0x795a
movq %r12, 0x18(%rsp)
leaq 0x68(%rsp), %rsi
movl 0xc(%rsp), %r12d
movl %r12d, %edi
movq %r15, %rdx
movl %ebx, %ecx
callq 0x71fb
testl %eax, %eax
jne 0x791c
leal -0x1(%rbx), %eax
shlq $0x9, %rax
movzwl 0x1fc(%r15,%rax), %eax
testw %ax, %ax
jne 0x7921
cmpl $0x6, (%rsp)
movq %r15, (%rsp)
jne 0x77de
leaq 0x268(%rsp), %r13
leaq 0x20(%rsp), %rsi
movq %r13, %rdi
movl $0x20, %edx
callq 0xbaf9
movl %ebx, %r12d
leaq 0xe4(%r15), %rbx
movq %r12, 0x10(%rsp)
shlq $0x9, %r12
xorl %r15d, %r15d
movq %r13, %rdi
movq %rbx, %rsi
movl $0x11c, %edx            # imm = 0x11C
callq 0xbc43
addq $-0x200, %r15           # imm = 0xFE00
addq $0x200, %rbx            # imm = 0x200
movq %r12, %rax
addq %r15, %rax
jne 0x7760
leaq 0x268(%rsp), %rdi
leaq 0x40(%rsp), %rbx
movq %rbx, %rsi
movl $0x20, %edx
callq 0xbc48
movq (%rsp), %rax
subq %r15, %rax
movdqu -0x13c(%rax), %xmm0
movdqu -0x12c(%rax), %xmm1
pcmpeqb 0x10(%rbx), %xmm1
pcmpeqb (%rbx), %xmm0
pand %xmm1, %xmm0
pmovmskb %xmm0, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
movl 0xc(%rsp), %r12d
jne 0x7942
movq (%rsp), %r15
jmp 0x77e5
movl %ebx, %eax
movq %rax, 0x10(%rsp)
xorl %r13d, %r13d
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 0x781d
cmpl $0x4, (%r14)
jne 0x78ae
testq %rax, %rax
movl $0x0, %ecx
cmovgq %rax, %rcx
addq %rcx, %rbx
testq %rax, %rax
je 0x783a
cmpq $0x100, %rbx            # imm = 0x100
jne 0x77fb
testl %ebx, %ebx
js 0x78ae
movl %ebx, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl $0x100, %eax            # imm = 0x100
jne 0x78b3
incq %r13
cmpq 0x10(%rsp), %r13
movq (%rsp), %r15
jne 0x77e8
movq %r15, %rdi
callq 0x3030
movl %r12d, %edi
callq 0x3100
cmpl $0x1, %ebp
je 0x7876
movl %ebp, %edi
callq 0x3100
movq 0x18(%rsp), %rdi
leal -0x1(%rdi), %eax
cmpl $-0x3, %eax
ja 0x7888
callq 0x3100
movl %ebx, %eax
addq $0x588, %rsp            # imm = 0x588
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, %ebp
jmp 0x7650
xorl %r12d, %r12d
jmp 0x7687
callq 0x3523
leaq 0x99a7(%rip), %rsi      # 0x11261
movl $0x100, %edx            # imm = 0x100
movl $0x1, %edi
movl %ebx, %ecx
xorl %eax, %eax
callq 0x31c0
movl $0x1, %edi
callq 0x3220
callq 0x3538
movq 0xf70d(%rip), %rax      # 0x16ff0
movq (%rax), %rdi
leaq 0x97e1(%rip), %rdx      # 0x110ce
movl $0x1, %esi
xorl %eax, %eax
callq 0x3240
jmp 0x78cd
callq 0x354d
callq 0x34ba
callq 0x34cf
leaq 0x9858(%rip), %rsi      # 0x11169
jmp 0x7949
leaq 0x987a(%rip), %rsi      # 0x11194
jmp 0x7949
callq 0x350e
rolw $0x8, %ax
movzwl %ax, %edx
leaq 0x972c(%rip), %rsi      # 0x1105b
movl $0x1, %edi
xorl %eax, %eax
callq 0x31c0
jmp 0x78cd
callq 0x34e4
leaq 0x98f5(%rip), %rsi      # 0x1123e
movl $0x1, %edi
xorl %eax, %eax
callq 0x31c0
jmp 0x78cd
leaq 0x98a3(%rip), %rsi      # 0x11204
movl $0x20, %edx
movl $0x1, %edi
movl %r13d, %ecx
jmp 0x78c6
callq 0x34f9