// File: /workspace/github_c_makefile_star2_O1/blasphemy[P]mmc-utils/mmc_cmds.c
// Function: do_enh_area_set
int do_enh_area_set(int nargs, char **argv)
{
	__u8 value;
	__u8 ext_csd[512];
	int fd, ret;
	char *device;
	int dry_run = 1;
	unsigned int start_kib, length_kib, enh_start_addr, enh_size_mult;
	unsigned long align;

	if (nargs != 5) {
		fprintf(stderr, "Usage: mmc enh_area set <-y|-n|-c> <start KiB> <length KiB> </path/to/mmcblkX>\n");
		exit(1);
	}

	if (!strcmp("-y", argv[1])) {
		dry_run = 0;
	} else if (!strcmp("-c", argv[1])) {
		dry_run = 2;
	}

	start_kib = strtol(argv[2], NULL, 10);
	length_kib = strtol(argv[3], NULL, 10);
	device = argv[4];

	fd = open(device, O_RDWR);
	if (fd < 0) {
		perror("open");
		exit(1);
	}

	ret = read_extcsd(fd, ext_csd);
	if (ret) {
		fprintf(stderr, "Could not read EXT_CSD from %s\n", device);
		exit(1);
	}

	/* assert ENH_ATTRIBUTE_EN */
	if (!(ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & EXT_CSD_ENH_ATTRIBUTE_EN))
	{
		printf(" Device cannot have enhanced tech.\n");
		exit(1);
	}

	/* assert not PARTITION_SETTING_COMPLETED */
	if (ext_csd[EXT_CSD_PARTITION_SETTING_COMPLETED])
	{
		printf(" Device is already partitioned\n");
		exit(1);
	}

	align = 512l * get_hc_wp_grp_size(ext_csd) * get_hc_erase_grp_size(ext_csd);

	enh_size_mult = (length_kib + align/2l) / align;

	enh_start_addr = start_kib * (1024 / (is_blockaddresed(ext_csd) ? 512 : 1));
	enh_start_addr /= align;
	enh_start_addr *= align;

	/* set EXT_CSD_ERASE_GROUP_DEF bit 0 */
	ret = write_extcsd_value(fd, EXT_CSD_ERASE_GROUP_DEF, 0x1);
	if (ret) {
		fprintf(stderr, "Could not write 0x1 to "
			"EXT_CSD[%d] in %s\n",
			EXT_CSD_ERASE_GROUP_DEF, device);
		exit(1);
	}

	/* write to ENH_START_ADDR and ENH_SIZE_MULT and PARTITIONS_ATTRIBUTE's ENH_USR bit */
	value = (enh_start_addr >> 24) & 0xff;
	ret = write_extcsd_value(fd, EXT_CSD_ENH_START_ADDR_3, value);
	if (ret) {
		fprintf(stderr, "Could not write 0x%02x to "
			"EXT_CSD[%d] in %s\n", value,
			EXT_CSD_ENH_START_ADDR_3, device);
		exit(1);
	}
	value = (enh_start_addr >> 16) & 0xff;
	ret = write_extcsd_value(fd, EXT_CSD_ENH_START_ADDR_2, value);
	if (ret) {
		fprintf(stderr, "Could not write 0x%02x to "
			"EXT_CSD[%d] in %s\n", value,
			EXT_CSD_ENH_START_ADDR_2, device);
		exit(1);
	}
	value = (enh_start_addr >> 8) & 0xff;
	ret = write_extcsd_value(fd, EXT_CSD_ENH_START_ADDR_1, value);
	if (ret) {
		fprintf(stderr, "Could not write 0x%02x to "
			"EXT_CSD[%d] in %s\n", value,
			EXT_CSD_ENH_START_ADDR_1, device);
		exit(1);
	}
	value = enh_start_addr & 0xff;
	ret = write_extcsd_value(fd, EXT_CSD_ENH_START_ADDR_0, value);
	if (ret) {
		fprintf(stderr, "Could not write 0x%02x to "
			"EXT_CSD[%d] in %s\n", value,
			EXT_CSD_ENH_START_ADDR_0, device);
		exit(1);
	}

	value = (enh_size_mult >> 16) & 0xff;
	ret = write_extcsd_value(fd, EXT_CSD_ENH_SIZE_MULT_2, value);
	if (ret) {
		fprintf(stderr, "Could not write 0x%02x to "
			"EXT_CSD[%d] in %s\n", value,
			EXT_CSD_ENH_SIZE_MULT_2, device);
		exit(1);
	}
	value = (enh_size_mult >> 8) & 0xff;
	ret = write_extcsd_value(fd, EXT_CSD_ENH_SIZE_MULT_1, value);
	if (ret) {
		fprintf(stderr, "Could not write 0x%02x to "
			"EXT_CSD[%d] in %s\n", value,
			EXT_CSD_ENH_SIZE_MULT_1, device);
		exit(1);
	}
	value = enh_size_mult & 0xff;
	ret = write_extcsd_value(fd, EXT_CSD_ENH_SIZE_MULT_0, value);
	if (ret) {
		fprintf(stderr, "Could not write 0x%02x to "
			"EXT_CSD[%d] in %s\n", value,
			EXT_CSD_ENH_SIZE_MULT_0, device);
		exit(1);
	}
	value = ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] | EXT_CSD_ENH_USR;
	ret = write_extcsd_value(fd, EXT_CSD_PARTITIONS_ATTRIBUTE, value);
	if (ret) {
		fprintf(stderr, "Could not write EXT_CSD_ENH_USR to "
			"EXT_CSD[%d] in %s\n",
			EXT_CSD_PARTITIONS_ATTRIBUTE, device);
		exit(1);
	}

	ret = check_enhanced_area_total_limit(device, fd);
	if (ret)
		exit(1);

	printf("Done setting ENH_USR area on %s\n", device);

	if (set_partitioning_setting_completed(dry_run, device, fd))
		exit(1);

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x208, %rsp            # imm = 0x208
cmpl $0x5, %edi
jne 0x5b3f
movq %rsi, %rbx
movq 0x8(%rsi), %r14
leaq 0x98c7(%rip), %rdi      # 0xf1c3
movq %r14, %rsi
callq 0x3150
testl %eax, %eax
je 0x5921
leaq 0x98b7(%rip), %rdi      # 0xf1c6
movq %r14, %rsi
callq 0x3150
xorl %ebp, %ebp
cmpl $0x1, %eax
adcl $0x1, %ebp
jmp 0x5923
xorl %ebp, %ebp
movq 0x10(%rbx), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x3180
movq %rax, %r15
movq 0x18(%rbx), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x3180
movq %rax, %r12
movq 0x20(%rbx), %rbx
movq %rbx, %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x31d0
testl %eax, %eax
js 0x5b61
movl %eax, %r14d
movq %rsp, %rsi
movl %eax, %edi
callq 0x3ef8
testl %eax, %eax
jne 0x5b66
testb $0x2, 0xa0(%rsp)
je 0x5b8b
cmpb $0x0, 0x9b(%rsp)
jne 0x5b94
movzbl 0xdd(%rsp), %eax
movzbl 0xe0(%rsp), %esi
imull %eax, %esi
shll $0x9, %esi
movl %r12d, %ecx
movl %esi, %eax
shrl %eax
addq %rcx, %rax
xorl %edx, %edx
divq %rsi
movq %rax, %r12
xorl %eax, %eax
cmpl $0x400001, 0xd4(%rsp) # imm = 0x400001
setb %al
leal (%rax,%rax,8), %ecx
incl %ecx
shll %cl, %r15d
movl %r15d, %eax
xorl %edx, %edx
divl %esi
movl %edx, %r13d
movl %r14d, %edi
movl $0xaf, %esi
movl $0x1, %edx
callq 0x3f8e
testl %eax, %eax
jne 0x5bac
subl %r13d, %r15d
movl %r15d, %r13d
shrl $0x18, %r13d
movl %r14d, %edi
movl $0x8b, %esi
movl %r13d, %edx
callq 0x3f8e
testl %eax, %eax
jne 0x5bcc
movl %r15d, %r13d
shrl $0x10, %r13d
movzbl %r13b, %edx
movl %r14d, %edi
movl $0x8a, %esi
callq 0x3f8e
testl %eax, %eax
jne 0x5bf0
movl %r15d, %r13d
shrl $0x8, %r13d
movzbl %r13b, %edx
movl %r14d, %edi
movl $0x89, %esi
callq 0x3f8e
testl %eax, %eax
jne 0x5c15
movzbl %r15b, %edx
movl %r14d, %edi
movl $0x88, %esi
callq 0x3f8e
testl %eax, %eax
jne 0x5c3a
movq %r12, %r15
shrq $0x10, %r15
movzbl %r15b, %edx
movl %r14d, %edi
movl $0x8e, %esi
callq 0x3f8e
testl %eax, %eax
jne 0x5c5c
movq %r12, %r15
shrq $0x8, %r15
movzbl %r15b, %edx
movl %r14d, %edi
movl $0x8d, %esi
callq 0x3f8e
testl %eax, %eax
jne 0x5c7e
movzbl %r12b, %edx
movl %r14d, %edi
movl $0x8c, %esi
callq 0x3f8e
testl %eax, %eax
jne 0x5ca0
movzbl 0x9c(%rsp), %edx
orl $0x1, %edx
movl %r14d, %edi
movl $0x9c, %esi
callq 0x3f8e
testl %eax, %eax
jne 0x5ccc
movq %rbx, %rdi
movl %r14d, %esi
callq 0x51aa
testl %eax, %eax
jne 0x5cf1
leaq 0x9860(%rip), %rsi      # 0xf367
movl $0x1, %edi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x31c0
movl %ebp, %edi
movq %rbx, %rsi
movl %r14d, %edx
callq 0x5071
testl %eax, %eax
jne 0x5cf1
xorl %eax, %eax
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x114aa(%rip), %rax     # 0x16ff0
movq (%rax), %rdi
leaq 0x976d(%rip), %rdx      # 0xf2bd
movl $0x1, %esi
xorl %eax, %eax
callq 0x3240
jmp 0x5cf1
callq 0x33d4
movq 0x11483(%rip), %rax     # 0x16ff0
movq (%rax), %rdi
leaq 0x8b8e(%rip), %rdx      # 0xe705
movl $0x1, %esi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x3240
jmp 0x5cf1
leaq 0x977b(%rip), %rsi      # 0xf30d
jmp 0x5b9b
leaq 0x96a2(%rip), %rsi      # 0xf23d
movl $0x1, %edi
xorl %eax, %eax
callq 0x31c0
jmp 0x5cf1
movq 0x1143d(%rip), %rax     # 0x16ff0
movq (%rax), %rdi
leaq 0x9258(%rip), %rdx      # 0xee15
movl $0x1, %esi
movl $0xaf, %ecx
jmp 0x5ce7
movq 0x1141d(%rip), %rax     # 0x16ff0
movq (%rax), %rdi
leaq 0x8b7d(%rip), %rdx      # 0xe75a
movl $0x1, %esi
movl %r13d, %ecx
movl $0x8b, %r8d
jmp 0x5cc0
movq 0x113f9(%rip), %rax     # 0x16ff0
movq (%rax), %rdi
movzbl %r13b, %ecx
leaq 0x8b55(%rip), %rdx      # 0xe75a
movl $0x1, %esi
movl $0x8a, %r8d
jmp 0x5cc0
movq 0x113d4(%rip), %rax     # 0x16ff0
movq (%rax), %rdi
movzbl %r13b, %ecx
leaq 0x8b30(%rip), %rdx      # 0xe75a
movl $0x1, %esi
movl $0x89, %r8d
jmp 0x5cc0
movq 0x113af(%rip), %rax     # 0x16ff0
movq (%rax), %rdi
movzbl %r15b, %ecx
leaq 0x8b0b(%rip), %rdx      # 0xe75a
movl $0x1, %esi
movl $0x88, %r8d
jmp 0x5cc0
movq 0x1138d(%rip), %rax     # 0x16ff0
movq (%rax), %rdi
movzbl %r15b, %ecx
leaq 0x8ae9(%rip), %rdx      # 0xe75a
movl $0x1, %esi
movl $0x8e, %r8d
jmp 0x5cc0
movq 0x1136b(%rip), %rax     # 0x16ff0
movq (%rax), %rdi
movzbl %r15b, %ecx
leaq 0x8ac7(%rip), %rdx      # 0xe75a
movl $0x1, %esi
movl $0x8d, %r8d
jmp 0x5cc0
movq 0x11349(%rip), %rax     # 0x16ff0
movq (%rax), %rdi
movzbl %r12b, %ecx
leaq 0x8aa5(%rip), %rdx      # 0xe75a
movl $0x1, %esi
movl $0x8c, %r8d
movq %rbx, %r9
xorl %eax, %eax
callq 0x3240
jmp 0x5cf1
movq 0x1131d(%rip), %rax     # 0x16ff0
movq (%rax), %rdi
leaq 0x9654(%rip), %rdx      # 0xf331
movl $0x1, %esi
movl $0x9c, %ecx
movq %rbx, %r8
xorl %eax, %eax
callq 0x3240
movl $0x1, %edi
callq 0x3220