// File: chenhn123[P]wdt_util_src/wdt_ct/wdt_dev_api.cpp
// Function: fw_version_check(ExecParam*, BoardInfo*, ChunkInfoEx*)
int fw_version_check(EXEC_PARAM *pparam, BOARD_INFO *pinfo, CHUNK_INFO_EX *pchunk_info)
{
	if (!pparam || !pinfo || !pchunk_info)
		return 0;

	char	fw_id = ((pchunk_info->chuckInfo.versionNumber >> 12) & 0xF);
	char	chip_id = ((pinfo->firmware_id >> 12) & 0xF);
	
	if (fw_id != chip_id) {
		printf("This firmware is not matched with this chip, fwid(%x), chip_id(%x)\n", fw_id, chip_id);
		return 0;
	} 

	if (pinfo->dev_type & FW_WDT8760) {
		if ((pchunk_info->chuckInfo.temp & 0xFF000000) != 0x31000000) {			
			printf("The device type is not matched with this firmware (%x)\n", pchunk_info->chuckInfo.temp);
			return 0;
		}
	} else if (pinfo->dev_type & FW_WDT8762) {
		if ((pchunk_info->chuckInfo.temp & 0xFF000000) != 0x32000000) { 		
			printf("The device type is not matched with this firmware (%x)\n", pchunk_info->chuckInfo.temp);
			return 0;
		}
	}

	wh_printf("The firmware vession: dev %x, wif %x\n", pinfo->firmware_id,
			  pchunk_info->chuckInfo.versionNumber); 

	unsigned int fw_ver = pchunk_info->chuckInfo.versionNumber & 0xFFF;
	unsigned int chip_fw_ver = pinfo->firmware_id & 0xFFF;

	/* just ignore the recovery firmware */
	if (( fw_ver <= chip_fw_ver) && (chip_fw_ver != 0xFFF)) 
		return 2;

	return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x9bf9
cmpq $0x0, -0x18(%rbp)
je 0x9bf9
cmpq $0x0, -0x20(%rbp)
jne 0x9c05
movl $0x0, -0x4(%rbp)
jmp 0x9d39
movq -0x20(%rbp), %rax
movl 0xc(%rax), %eax
shrl $0xc, %eax
andl $0xf, %eax
movb %al, -0x21(%rbp)
movq -0x18(%rbp), %rax
movl 0x10(%rax), %eax
shrl $0xc, %eax
andl $0xf, %eax
movb %al, -0x22(%rbp)
movsbl -0x21(%rbp), %eax
movsbl -0x22(%rbp), %ecx
cmpl %ecx, %eax
je 0x9c53
movsbl -0x21(%rbp), %esi
movsbl -0x22(%rbp), %edx
leaq 0x63a5(%rip), %rdi      # 0xffe5
movb $0x0, %al
callq 0x2050
movl $0x0, -0x4(%rbp)
jmp 0x9d39
movq -0x18(%rbp), %rax
movl (%rax), %eax
andl $0x800, %eax            # imm = 0x800
cmpl $0x0, %eax
je 0x9c99
movq -0x20(%rbp), %rax
movl 0x14(%rax), %eax
andl $0xff000000, %eax       # imm = 0xFF000000
cmpl $0x31000000, %eax       # imm = 0x31000000
je 0x9c97
movq -0x20(%rbp), %rax
movl 0x14(%rax), %esi
leaq 0x63a5(%rip), %rdi      # 0x10029
movb $0x0, %al
callq 0x2050
movl $0x0, -0x4(%rbp)
jmp 0x9d39
jmp 0x9cde
movq -0x18(%rbp), %rax
movl (%rax), %eax
andl $0x2000, %eax           # imm = 0x2000
cmpl $0x0, %eax
je 0x9cdc
movq -0x20(%rbp), %rax
movl 0x14(%rax), %eax
andl $0xff000000, %eax       # imm = 0xFF000000
cmpl $0x32000000, %eax       # imm = 0x32000000
je 0x9cda
movq -0x20(%rbp), %rax
movl 0x14(%rax), %esi
leaq 0x635f(%rip), %rdi      # 0x10029
movb $0x0, %al
callq 0x2050
movl $0x0, -0x4(%rbp)
jmp 0x9d39
jmp 0x9cdc
jmp 0x9cde
movq -0x18(%rbp), %rax
movl 0x10(%rax), %esi
movq -0x20(%rbp), %rax
movl 0xc(%rax), %edx
leaq 0x636e(%rip), %rdi      # 0x10061
movb $0x0, %al
callq 0x24e0
movq -0x20(%rbp), %rax
movl 0xc(%rax), %eax
andl $0xfff, %eax            # imm = 0xFFF
movl %eax, -0x28(%rbp)
movq -0x18(%rbp), %rax
movl 0x10(%rax), %eax
andl $0xfff, %eax            # imm = 0xFFF
movl %eax, -0x2c(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x2c(%rbp), %eax
ja 0x9d32
cmpl $0xfff, -0x2c(%rbp)     # imm = 0xFFF
je 0x9d32
movl $0x2, -0x4(%rbp)
jmp 0x9d39
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)