// File: /workspace/github_c_makefile_star2_O3/orempel[P]multiboot_tool/mpmboot.c
// Function: mpm_open
static int mpm_open(struct multiboot *mboot)
{
    struct mpm_privdata *mpm = (struct mpm_privdata *)mboot->privdata;

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

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

    if (mpm_open_device(mpm) < 0)
    {
        return -1;
    }

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

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

    char version[16 +1];
    if (mpm_read_version(mpm, (uint8_t *)version, sizeof(version) -1))
    {
        fprintf(stderr, "failed to get bootloader version\n");
        mpm_close(mboot);
        return -1;
    }

    version[16] = '\0';

    uint8_t chipinfo[8];
    if (mpm_read_chipinfo(mpm, chipinfo, sizeof(chipinfo)))
    {
        fprintf(stderr, "failed to get bootloader version\n");
        mpm_close(mboot);
        return -1;
    }

    const char *chipname = chipinfo_get_avr_name(chipinfo);

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

    printf("device         : %-16s (address: 0x%02X)\n",
           mpm->device, mpm->address);

    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",
           mpm->flashsize, mpm->flashsize, mpm->flashpage);

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

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x58, %rsp
movq 0x8(%rdi), %rbx
cmpb $0x0, 0x10(%rbx)
je 0x6285
movq %rdi, %r14
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x628c
movl $0x80102, %esi          # imm = 0x80102
xorl %eax, %eax
callq 0x2210
movl %eax, 0x8(%rbx)
testl %eax, %eax
js 0x6293
leaq 0x8(%rbx), %r15
leaq 0x18(%rbx), %rsi
movl %eax, %edi
callq 0x21f0
testl %eax, %eax
js 0x62a1
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rdx
movups %xmm0, 0x4(%rdx)
movups %xmm0, 0x14(%rdx)
movups %xmm0, 0x24(%rdx)
movq $0x0, 0x34(%rdx)
movl $0x1, (%rdx)
movl $0x400019b2, 0x8(%rdx)  # imm = 0x400019B2
movb $0x1, 0x17(%rdx)
movl (%r15), %edi
xorl %esi, %esi
callq 0x2200
testl %eax, %eax
js 0x62ab
movl $0x1, 0xc(%rbx)
movq %rbx, %rdi
xorl %esi, %esi
callq 0x6834
testl %eax, %eax
jne 0x62c1
movl $0x186a0, %edi          # imm = 0x186A0
callq 0x22c0
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq %rbx, %rdi
movl $0x2, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x6893
testl %eax, %eax
js 0x6271
leaq 0x8(%rsp), %rdx
movb $0x0, (%rdx)
leaq 0x10(%rsp), %rcx
movq %rbx, %rdi
movl $0x2, %esi
movl $0x10, %r8d
callq 0x69b1
testl %eax, %eax
js 0x6271
movaps 0x10(%rsp), %xmm0
andps 0x2f6a(%rip), %xmm0     # 0x90e0
movaps %xmm0, 0x10(%rsp)
cmpb $0x0, 0x8(%rsp)
jne 0x6271
movb $0x0, 0x20(%rsp)
movq %rbx, %rdi
movl $0x3, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x6893
testl %eax, %eax
js 0x627b
leaq 0x7(%rsp), %rdx
movb $0x0, (%rdx)
leaq 0x8(%rsp), %rcx
movq %rbx, %rdi
movl $0x3, %esi
movl $0x8, %r8d
callq 0x69b1
testl %eax, %eax
js 0x627b
cmpb $0x0, 0x7(%rsp)
jne 0x627b
leaq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x3af9
movb 0x3(%r14), %cl
movb %cl, 0x14(%rbx)
movzwl 0x4(%r14), %ecx
rolw $0x8, %cx
movw %cx, 0x12(%rbx)
movzwl 0x6(%r14), %ecx
rolw $0x8, %cx
movq %rax, %r15
movw %cx, 0x16(%rbx)
movq (%rbx), %rsi
movzbl 0x10(%rbx), %edx
leaq 0x30e5(%rip), %rdi      # 0x92fb
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x20f0
movzbl (%r14), %edx
movzbl 0x1(%r14), %ecx
movzbl 0x2(%r14), %r8d
leaq 0x2baf(%rip), %rdi      # 0x8de3
leaq 0x10(%rsp), %rsi
movq %r15, %r9
xorl %eax, %eax
callq 0x20f0
movzwl 0x12(%rbx), %edx
movzbl 0x14(%rbx), %ecx
leaq 0x2bcb(%rip), %rdi      # 0x8e1d
movl %edx, %esi
xorl %eax, %eax
callq 0x20f0
movzwl 0x16(%rbx), %edx
leaq 0x2196(%rip), %rdi      # 0x83fc
movl %edx, %esi
xorl %eax, %eax
callq 0x20f0
jmp 0x62ce
movq %r14, %rdi
callq 0x2a99
jmp 0x62c9
movq %r14, %rdi
callq 0x2a6f
jmp 0x62c9
callq 0x2af7
jmp 0x62c9
callq 0x2adb
jmp 0x62c9
leaq 0x26f0(%rip), %rdi      # 0x898a
callq 0x2240
jmp 0x62c9
movq %r15, %rdi
callq 0x2ac3
jmp 0x62c9
leaq 0x2191(%rip), %rdi      # 0x8443
callq 0x2240
movl (%r15), %edi
callq 0x2140
jmp 0x62c9
movq %r14, %rdi
callq 0x2a45
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq