// File: /workspace/github_c_makefile_star2_O1/orempel[P]multiboot_tool/funkboot.c
// Function: funk_open
static int funk_open(struct multiboot *mboot)
{
    struct funk_privdata *funk = (struct funk_privdata *)mboot->privdata;

    if (funk->address == 0)
    {
        fprintf(stderr, "abort: no address given\n");
        return -1;
    }

    if (funk->device == NULL)
    {
        fprintf(stderr, "abort: no device given\n");
        return -1;
    }

    if (funk_open_device(funk) < 0)
    {
        return -1;
    }

    printf("funkbridge dev : %-16s\n", funk->device);

    char bridge_version[20];
    if (funk_bridge_version(funk, (uint8_t *)bridge_version, sizeof(bridge_version)))
    {
        fprintf(stderr, "failed to get funkbridge version\n");
        funk_close(mboot);
        return -1;
    }

    printf("funkbridge ver : %-16s\n", bridge_version);

    if (funk_switch_application(funk, BOOTTYPE_BOOTLOADER))
    {
        fprintf(stderr, "failed to switch to bootloader (invalid address?)\n");
        funk_close(mboot);
        return -1;
    }

    printf("address        : 0x%02X\n", funk->address);

    /* wait for watchdog and startup time */
    usleep(100000);

    char version[20];
    if (funk_read_version(funk, (uint8_t *)version, sizeof(version)))
    {
        fprintf(stderr, "failed to get bootloader version\n");
        funk_close(mboot);
        return -1;
    }

    uint8_t chipinfo[8];
    if (funk_read_chipinfo(funk, chipinfo, sizeof(chipinfo)))
    {
        fprintf(stderr, "failed to get bootloader chipinfo\n");
        funk_close(mboot);
        return -1;
    }

    const char *chipname = chipinfo_get_avr_name(chipinfo);

    funk->flashpage  = chipinfo[3];
    funk->flashsize  = (chipinfo[4] << 8) + chipinfo[5];
    funk->eepromsize = (chipinfo[6] << 8) + chipinfo[7];

    printf("version        : %-16s (sig: 0x%02x 0x%02x 0x%02x => %s)\n",
           version, chipinfo[0], chipinfo[1], chipinfo[2], chipname);

    printf("flash size     : 0x%04x / %5d   (0x%02x bytes/page)\n",
           funk->flashsize, funk->flashsize, funk->flashpage);

    printf("eeprom size    : 0x%04x / %5d\n",
           funk->eepromsize, funk->eepromsize);

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x78, %rsp
movq 0x8(%rdi), %rbx
cmpl $0x0, 0x10(%rbx)
je 0x54ac
movq %rdi, %r14
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x54b3
movl $0x80102, %esi          # imm = 0x80102
xorl %eax, %eax
callq 0x2200
movl %eax, 0x8(%rbx)
testl %eax, %eax
js 0x54ba
leaq 0x8(%rbx), %r15
leaq 0x28(%rbx), %rsi
movl %eax, %edi
callq 0x21e0
testl %eax, %eax
js 0x54c8
xorps %xmm0, %xmm0
movq %rsp, %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x10(%rdx)
movups %xmm0, 0x2c(%rdx)
movaps %xmm0, 0x20(%rdx)
movl $0x1, (%rdx)
orl $0x8bf, 0x8(%rdx)       # imm = 0x8BF
movw $0x100, 0x16(%rdx)      # imm = 0x100
movl (%r15), %edi
xorl %esi, %esi
callq 0x21f0
movl %eax, %ebp
testl %eax, %eax
js 0x54d2
movl $0x1, 0xc(%rbx)
testl %ebp, %ebp
js 0x5519
movq (%rbx), %rsi
leaq 0x3a95(%rip), %rdi      # 0x8cee
xorl %eax, %eax
callq 0x20f0
movq %rsp, %rsi
movw $0x56, (%rsi)
movl 0x8(%rbx), %edi
movl $0x2, %edx
callq 0x2090
cmpq $0x2, %rax
jne 0x54a2
leaq 0x40(%rsp), %rdx
movb $0x0, (%rdx)
movzbl (%rsp), %esi
leaq 0x60(%rsp), %rcx
movq %rbx, %rdi
movl $0x14, %r8d
callq 0x5aee
testl %eax, %eax
js 0x54a2
movl %eax, %eax
movb $0x0, 0x60(%rsp,%rax)
cmpb $0x0, 0x40(%rsp)
jne 0x54a2
leaq 0x3a69(%rip), %rdi      # 0x8d28
leaq 0x60(%rsp), %rsi
xorl %eax, %eax
callq 0x20f0
movq %rbx, %rdi
xorl %esi, %esi
callq 0x5a13
testl %eax, %eax
jne 0x54df
movl 0x10(%rbx), %esi
leaq 0x3a8c(%rip), %rdi      # 0x8d73
xorl %eax, %eax
callq 0x20f0
movl $0x186a0, %edi          # imm = 0x186A0
callq 0x22b0
movb 0x10(%rbx), %al
movq %rsp, %rsi
movb %al, (%rsi)
movl $0x21cc03cc, 0x1(%rsi)  # imm = 0x21CC03CC
movl 0x18(%rbx), %eax
incl %eax
movl %eax, 0x18(%rbx)
movb %al, 0x5(%rsi)
movb $0x0, 0x6(%rsi)
movq %rbx, %rdi
movl $0x7, %edx
callq 0x5d4a
testl %eax, %eax
js 0x54e9
leaq 0x54(%rsp), %rdx
movl $0x2b, (%rdx)
movq %rsp, %rsi
movq %rbx, %rdi
callq 0x5db2
testl %eax, %eax
js 0x54f3
cmpb $-0x1f, 0x4(%rsp)
jne 0x54f8
cmpb $0x0, 0x6(%rsp)
jne 0x54f8
movzbl 0x2(%rsp), %eax
cmpl $0x4, %eax
jb 0x538e
cmpl $0x17, %eax
movl $0x17, %ecx
cmovll %eax, %ecx
addl $-0x3, %ecx
xorl %eax, %eax
movb 0x7(%rsp,%rax), %dl
andb $0x7f, %dl
movb %dl, 0x40(%rsp,%rax)
incq %rax
cmpq %rax, %rcx
jne 0x5379
jmp 0x5390
xorl %eax, %eax
xorl %ecx, %ecx
movb %cl, 0x40(%rsp,%rax)
movb 0x10(%rbx), %al
movq %rsp, %rsi
movb %al, (%rsi)
movl $0x22cc03cc, 0x1(%rsi)  # imm = 0x22CC03CC
movl 0x18(%rbx), %eax
incl %eax
movl %eax, 0x18(%rbx)
movb %al, 0x5(%rsi)
movb %cl, 0x6(%rsi)
movq %rbx, %rdi
movl $0x7, %edx
callq 0x5d4a
testl %eax, %eax
js 0x5502
leaq 0x5c(%rsp), %rdx
movl $0x2b, (%rdx)
movq %rsp, %rsi
movq %rbx, %rdi
callq 0x5db2
testl %eax, %eax
js 0x550c
cmpb $-0x1e, 0x4(%rsp)
jne 0x5511
cmpb $0x0, 0x6(%rsp)
jne 0x5511
leaq 0x7(%rsp), %rsi
movzbl -0x5(%rsi), %eax
cmpl $0xb, %eax
movl $0xb, %ecx
cmovll %eax, %ecx
addl $-0x3, %ecx
movslq %ecx, %rdx
leaq 0x54(%rsp), %r14
movq %r14, %rdi
callq 0x21a0
movq %r14, %rdi
callq 0x3b1f
movzbl 0x3(%r14), %ecx
movl %ecx, 0x20(%rbx)
movzwl 0x4(%r14), %ecx
rolw $0x8, %cx
movzwl %cx, %ecx
movl %ecx, 0x1c(%rbx)
movzwl 0x6(%r14), %ecx
rolw $0x8, %cx
movzwl %cx, %ecx
movl %ecx, 0x24(%rbx)
movzbl (%r14), %edx
movzbl 0x1(%r14), %ecx
movzbl 0x2(%r14), %r8d
leaq 0x396b(%rip), %rdi      # 0x8dd1
xorl %ebp, %ebp
leaq 0x40(%rsp), %rsi
movq %rax, %r9
xorl %eax, %eax
callq 0x20f0
movl 0x1c(%rbx), %edx
movl 0x20(%rbx), %ecx
leaq 0x3987(%rip), %rdi      # 0x8e0b
movl %edx, %esi
xorl %eax, %eax
callq 0x20f0
movl 0x24(%rbx), %edx
leaq 0x2f65(%rip), %rdi      # 0x83fc
movl %edx, %esi
xorl %eax, %eax
callq 0x20f0
jmp 0x551e
movq %r14, %rdi
callq 0x28c9
jmp 0x5519
callq 0x2927
jmp 0x5519
callq 0x290b
jmp 0x5519
leaq 0x34b7(%rip), %rdi      # 0x8978
callq 0x2230
jmp 0x5519
movq %r15, %rdi
callq 0x28f3
jmp 0x5519
movq %r15, %rdi
callq 0x27aa
jmp 0x5247
movq %r14, %rdi
callq 0x27c3
jmp 0x5519
movq %rsp, %rdi
callq 0x287d
jmp 0x54f8
callq 0x285b
movq %r14, %rdi
callq 0x289f
jmp 0x5519
movq %rsp, %rdi
callq 0x280f
jmp 0x5511
callq 0x27ed
movq %r14, %rdi
callq 0x2831
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x78, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq