// File: /workspace/github_c_makefile_star2_O3/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 0x543b
movq %rdi, %r14
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x5442
movl $0x80102, %esi          # imm = 0x80102
xorl %eax, %eax
callq 0x2210
movl %eax, 0x8(%rbx)
testl %eax, %eax
js 0x5449
leaq 0x8(%rbx), %r15
leaq 0x28(%rbx), %rsi
movl %eax, %edi
callq 0x21f0
testl %eax, %eax
js 0x5457
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %rdx
movups %xmm0, 0x4(%rdx)
movups %xmm0, 0x14(%rdx)
movups %xmm0, 0x24(%rdx)
movq $0x0, 0x34(%rdx)
movl $0x1, (%rdx)
movl $0x8bf, 0x8(%rdx)       # imm = 0x8BF
movb $0x1, 0x17(%rdx)
movl (%r15), %edi
xorl %esi, %esi
callq 0x2200
testl %eax, %eax
js 0x5461
movl $0x1, 0xc(%rbx)
movq (%rbx), %rsi
leaq 0x3b22(%rip), %rdi      # 0x8d00
xorl %eax, %eax
callq 0x20f0
leaq 0x20(%rsp), %rsi
movw $0x56, (%rsi)
movl 0x8(%rbx), %edi
movl $0x2, %edx
callq 0x2090
cmpq $0x2, %rax
jne 0x5431
movq %rsp, %rdx
movb $0x0, (%rdx)
leaq 0x60(%rsp), %rcx
movq %rbx, %rdi
movl $0x56, %esi
movl $0x14, %r8d
callq 0x5aeb
testl %eax, %eax
js 0x5431
movl %eax, %eax
movb $0x0, 0x60(%rsp,%rax)
cmpb $0x0, (%rsp)
jne 0x5431
leaq 0x3af6(%rip), %rdi      # 0x8d3a
leaq 0x60(%rsp), %rsi
xorl %eax, %eax
callq 0x20f0
movq %rbx, %rdi
xorl %esi, %esi
callq 0x5a0e
testl %eax, %eax
jne 0x5477
movl 0x10(%rbx), %esi
leaq 0x3b19(%rip), %rdi      # 0x8d85
xorl %eax, %eax
callq 0x20f0
movl $0x186a0, %edi          # imm = 0x186A0
callq 0x22c0
movb 0x10(%rbx), %al
leaq 0x20(%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 0x5d1b
testl %eax, %eax
js 0x5481
leaq 0x18(%rsp), %rdx
movl $0x2b, (%rdx)
leaq 0x20(%rsp), %rsi
movq %rbx, %rdi
callq 0x5d86
testl %eax, %eax
js 0x548d
cmpb $-0x1f, 0x24(%rsp)
jne 0x5492
cmpb $0x0, 0x26(%rsp)
jne 0x5492
movb 0x22(%rsp), %al
cmpb $0x4, %al
jb 0x5319
movzbl %al, %ecx
cmpb $0x17, %al
movl $0x17, %eax
cmovbl %ecx, %eax
movzbl %al, %eax
addl $-0x3, %eax
xorl %ecx, %ecx
movb 0x27(%rsp,%rcx), %dl
andb $0x7f, %dl
movb %dl, (%rsp,%rcx)
incq %rcx
cmpq %rcx, %rax
jne 0x5305
jmp 0x531b
xorl %eax, %eax
xorl %ecx, %ecx
movb %cl, (%rsp,%rax)
movb 0x10(%rbx), %al
leaq 0x20(%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 0x5d1b
testl %eax, %eax
js 0x549c
leaq 0x5c(%rsp), %rdx
movl $0x2b, (%rdx)
leaq 0x20(%rsp), %rsi
movq %rbx, %rdi
callq 0x5d86
testl %eax, %eax
js 0x54a8
cmpb $-0x1e, 0x24(%rsp)
jne 0x54ad
cmpb $0x0, 0x26(%rsp)
jne 0x54ad
leaq 0x27(%rsp), %rsi
movzbl -0x5(%rsi), %eax
cmpb $0xb, %al
movl $0xb, %ecx
cmovbl %eax, %ecx
movzbl %cl, %edx
addq $-0x3, %rdx
leaq 0x18(%rsp), %r14
movq %r14, %rdi
callq 0x21b0
movq %r14, %rdi
callq 0x3af9
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 0x39ef(%rip), %rdi      # 0x8de3
xorl %ebp, %ebp
movq %rsp, %rsi
movq %rax, %r9
xorl %eax, %eax
callq 0x20f0
movl 0x1c(%rbx), %edx
movl 0x20(%rbx), %ecx
leaq 0x3a0d(%rip), %rdi      # 0x8e1d
movl %edx, %esi
xorl %eax, %eax
callq 0x20f0
movl 0x24(%rbx), %edx
leaq 0x2fd9(%rip), %rdi      # 0x83fc
movl %edx, %esi
xorl %eax, %eax
callq 0x20f0
jmp 0x54ba
movq %r14, %rdi
callq 0x288a
jmp 0x54b5
callq 0x28e8
jmp 0x54b5
callq 0x28cc
jmp 0x54b5
leaq 0x353a(%rip), %rdi      # 0x898a
callq 0x2240
jmp 0x54b5
movq %r15, %rdi
callq 0x28b4
jmp 0x54b5
leaq 0x2fdb(%rip), %rdi      # 0x8443
callq 0x2240
movl (%r15), %edi
callq 0x2140
jmp 0x54b5
movq %r14, %rdi
callq 0x2784
jmp 0x54b5
leaq 0x20(%rsp), %rdi
callq 0x283e
jmp 0x5492
callq 0x281c
movq %r14, %rdi
callq 0x2860
jmp 0x54b5
leaq 0x20(%rsp), %rdi
callq 0x27d0
jmp 0x54ad
callq 0x27ae
movq %r14, %rdi
callq 0x27f2
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x78, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq