// File: /workspace/github_c_makefile_star2_O2/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 %r14
pushq %rbx
subq $0x78, %rsp
movq 0x8(%rdi), %r14
cmpl $0x0, 0x10(%r14)
je 0x46fd
movq %rdi, %rbx
movq (%r14), %rdi
testq %rdi, %rdi
je 0x4712
movl $0x80102, %esi          # imm = 0x80102
xorl %eax, %eax
callq 0x2210
movl %eax, 0x8(%r14)
testl %eax, %eax
js 0x4733
leaq 0x28(%r14), %rsi
movl %eax, %edi
callq 0x21f0
testl %eax, %eax
js 0x4744
xorps %xmm0, %xmm0
leaq 0x24(%rsp), %rdx
movups %xmm0, 0x4(%rdx)
movups %xmm0, 0x14(%rdx)
movups %xmm0, 0x24(%rdx)
andq $0x0, 0x34(%rdx)
movl $0x1, (%rdx)
movl $0x8bf, 0x8(%rdx)       # imm = 0x8BF
movb $0x1, 0x17(%rdx)
movl 0x8(%r14), %edi
xorl %esi, %esi
callq 0x2200
testl %eax, %eax
js 0x474d
movl $0x1, 0xc(%r14)
movq (%r14), %rsi
leaq 0x4854(%rip), %rdi      # 0x8cee
xorl %eax, %eax
callq 0x20f0
leaq 0x24(%rsp), %rsi
movw $0x56, (%rsi)
movq %r14, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x4db0
testl %eax, %eax
js 0x46e7
movq %rsp, %rdx
movb $0x0, (%rdx)
pushq $0x56
popq %rsi
leaq 0x60(%rsp), %rcx
pushq $0x14
popq %r8
movq %r14, %rdi
callq 0x4dff
testl %eax, %eax
js 0x46e7
movl %eax, %eax
movb $0x0, 0x60(%rsp,%rax)
cmpb $0x0, (%rsp)
jne 0x46e7
leaq 0x482f(%rip), %rdi      # 0x8d28
leaq 0x60(%rsp), %rsi
xorl %eax, %eax
callq 0x20f0
movq %r14, %rdi
xorl %esi, %esi
callq 0x4cfb
testl %eax, %eax
jne 0x4767
movl 0x10(%r14), %esi
leaq 0x4851(%rip), %rdi      # 0x8d73
xorl %eax, %eax
callq 0x20f0
movl $0x186a0, %edi          # imm = 0x186A0
callq 0x22c0
movb 0x10(%r14), %al
leaq 0x24(%rsp), %rsi
movb %al, (%rsi)
movl $0x21cc03cc, 0x1(%rsi)  # imm = 0x21CC03CC
movl 0x18(%r14), %eax
incl %eax
movl %eax, 0x18(%r14)
movb %al, 0x5(%rsi)
movb $0x0, 0x6(%rsi)
pushq $0x7
popq %rdx
movq %r14, %rdi
callq 0x500b
testl %eax, %eax
js 0x477f
leaq 0x18(%rsp), %rdx
movl $0x2b, (%rdx)
leaq 0x24(%rsp), %rsi
movq %r14, %rdi
callq 0x5072
testl %eax, %eax
js 0x4792
cmpb $-0x1f, 0x28(%rsp)
jne 0x47ae
cmpb $0x0, 0x2a(%rsp)
jne 0x47ae
movzbl 0x26(%rsp), %eax
cmpb $0x17, %al
pushq $0x17
popq %rcx
cmovbl %eax, %ecx
cmpb $0x4, %cl
pushq $0x3
popq %rax
cmovael %ecx, %eax
movzbl %al, %eax
addl $-0x3, %eax
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x45d1
movb 0x2b(%rsp,%rcx), %dl
andb $0x7f, %dl
movb %dl, (%rsp,%rcx)
incq %rcx
jmp 0x45bd
xorl %ecx, %ecx
movb %cl, (%rsp,%rax)
movb 0x10(%r14), %al
leaq 0x24(%rsp), %rsi
movb %al, (%rsi)
movl $0x22cc03cc, 0x1(%rsi)  # imm = 0x22CC03CC
movl 0x18(%r14), %eax
incl %eax
movl %eax, 0x18(%r14)
movb %al, 0x5(%rsi)
movb %cl, 0x6(%rsi)
pushq $0x7
popq %rdx
movq %r14, %rdi
callq 0x500b
testl %eax, %eax
js 0x47c3
leaq 0x20(%rsp), %rdx
movl $0x2b, (%rdx)
leaq 0x24(%rsp), %rsi
movq %r14, %rdi
callq 0x5072
testl %eax, %eax
js 0x47d6
cmpb $-0x1e, 0x28(%rsp)
jne 0x47f2
cmpb $0x0, 0x2a(%rsp)
jne 0x47f2
leaq 0x2b(%rsp), %rsi
movzbl -0x5(%rsi), %eax
cmpb $0xb, %al
pushq $0xb
popq %rcx
cmovbl %eax, %ecx
movzbl %cl, %edx
addq $-0x3, %rdx
leaq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x21b0
movq %rbx, %rdi
callq 0x2eb8
movzbl 0x3(%rbx), %ecx
movl %ecx, 0x20(%r14)
movzwl 0x4(%rbx), %ecx
rolw $0x8, %cx
movzwl %cx, %ecx
movl %ecx, 0x1c(%r14)
movzwl 0x6(%rbx), %ecx
rolw $0x8, %cx
movzwl %cx, %ecx
movl %ecx, 0x24(%r14)
movzbl (%rbx), %edx
movzbl 0x1(%rbx), %ecx
movzbl 0x2(%rbx), %r8d
leaq 0x472a(%rip), %rdi      # 0x8dd1
xorl %ebx, %ebx
movq %rsp, %rsi
movq %rax, %r9
xorl %eax, %eax
callq 0x20f0
movl 0x1c(%r14), %edx
movl 0x20(%r14), %ecx
leaq 0x4746(%rip), %rdi      # 0x8e0b
movl %edx, %esi
xorl %eax, %eax
callq 0x20f0
movl 0x24(%r14), %edx
leaq 0x3d23(%rip), %rdi      # 0x83fc
movl %edx, %esi
xorl %eax, %eax
callq 0x20f0
jmp 0x4819
movq 0x890a(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x460e(%rip), %rdi      # 0x8d06
jmp 0x47bf
movq 0x88f4(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x45c7(%rip), %rdi      # 0x8cd5
pushq $0x18
jmp 0x4725
movq 0x88df(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x3bce(%rip), %rdi      # 0x82f1
pushq $0x17
popq %rsi
pushq $0x1
popq %rdx
callq 0x2280
jmp 0x4816
leaq 0x423e(%rip), %rdi      # 0x8978
callq 0x2240
jmp 0x4816
leaq 0x3ce6(%rip), %rdi      # 0x8431
jmp 0x4754
leaq 0x3cef(%rip), %rdi      # 0x8443
callq 0x2240
movl 0x8(%r14), %edi
callq 0x2140
jmp 0x4816
movq 0x888a(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x45c8(%rip), %rdi      # 0x8d40
pushq $0x32
jmp 0x4805
movq 0x8872(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x477a(%rip), %rdi      # 0x8f0a
jmp 0x47a3
movq 0x885f(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x4790(%rip), %rdi      # 0x8f33
pushq $0x28
popq %rsi
pushq $0x1
popq %rdx
callq 0x2280
movq 0x8843(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x45cd(%rip), %rdi      # 0x8d8c
pushq $0x21
jmp 0x4805
movq 0x882e(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x4788(%rip), %rdi      # 0x8f5c
jmp 0x47e7
movq 0x881b(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x479f(%rip), %rdi      # 0x8f86
pushq $0x29
popq %rsi
pushq $0x1
popq %rdx
callq 0x2280
movq 0x87ff(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x45ab(%rip), %rdi      # 0x8dae
pushq $0x22
popq %rsi
pushq $0x1
popq %rdx
callq 0x2280
movq %rbx, %rdi
callq 0x4823
pushq $-0x1
popq %rbx
movl %ebx, %eax
addq $0x78, %rsp
popq %rbx
popq %r14
retq