// File: /workspace/github_c_makefile_star2_O1/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 %r12
pushq %rbx
subq $0x50, %rsp
movq 0x8(%rdi), %rbx
cmpb $0x0, 0x10(%rbx)
je 0x62d4
movq %rdi, %r14
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x62db
movl $0x80102, %esi          # imm = 0x80102
xorl %eax, %eax
callq 0x2200
movl %eax, 0x8(%rbx)
testl %eax, %eax
js 0x62e2
leaq 0x8(%rbx), %r15
leaq 0x18(%rbx), %rsi
movl %eax, %edi
callq 0x21e0
testl %eax, %eax
js 0x62f0
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x10(%rdx)
movups %xmm0, 0x2c(%rdx)
movaps %xmm0, 0x20(%rdx)
movl $0x1, (%rdx)
orl $0x400019b2, 0x8(%rdx)  # imm = 0x400019B2
movw $0x100, 0x16(%rdx)      # imm = 0x100
movl (%r15), %edi
xorl %esi, %esi
callq 0x21f0
movl %eax, %r12d
testl %eax, %eax
js 0x62fa
movl $0x1, 0xc(%rbx)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %r12d, %r12d
js 0x6314
movq %rbx, %rdi
xorl %esi, %esi
callq 0x682a
testl %eax, %eax
jne 0x6323
movl $0x186a0, %edi          # imm = 0x186A0
callq 0x22b0
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq %rbx, %rdi
movl $0x2, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x6882
testl %eax, %eax
js 0x62ca
leaq 0x8(%rsp), %rdx
movb $0x0, (%rdx)
leaq 0x10(%rsp), %rcx
movq %rbx, %rdi
movl $0x2, %esi
movl $0x10, %r8d
callq 0x69a0
testl %eax, %eax
js 0x62ca
xorl %eax, %eax
andb $0x7f, 0x10(%rsp,%rax)
incq %rax
cmpq $0x10, %rax
jne 0x61c6
cmpb $0x0, 0x8(%rsp)
jne 0x62ca
movb $0x0, 0x20(%rsp)
movq %rbx, %rdi
movl $0x3, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x6882
testl %eax, %eax
js 0x6307
leaq 0x7(%rsp), %rdx
movb $0x0, (%rdx)
leaq 0x8(%rsp), %rcx
movq %rbx, %rdi
movl $0x3, %esi
movl $0x8, %r8d
callq 0x69a0
testl %eax, %eax
js 0x6307
cmpb $0x0, 0x7(%rsp)
jne 0x6307
leaq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x3b1f
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 0x3054(%rip), %rdi      # 0x92c3
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x20f0
movzbl (%r14), %edx
movzbl 0x1(%r14), %ecx
movzbl 0x2(%r14), %r8d
leaq 0x2b44(%rip), %rdi      # 0x8dd1
leaq 0x10(%rsp), %rsi
movq %r15, %r9
xorl %eax, %eax
callq 0x20f0
movzwl 0x12(%rbx), %edx
movzbl 0x14(%rbx), %ecx
leaq 0x2b60(%rip), %rdi      # 0x8e0b
movl %edx, %esi
xorl %eax, %eax
callq 0x20f0
movzwl 0x16(%rbx), %edx
leaq 0x213d(%rip), %rdi      # 0x83fc
movl %edx, %esi
xorl %eax, %eax
callq 0x20f0
jmp 0x6314
movq %r14, %rdi
callq 0x2af1
jmp 0x630f
callq 0x2b4f
jmp 0x630f
callq 0x2b33
jmp 0x630f
leaq 0x268f(%rip), %rdi      # 0x8978
callq 0x2230
jmp 0x630f
movq %r15, %rdi
callq 0x2b1b
jmp 0x630f
movq %r15, %rdi
callq 0x2a84
jmp 0x6151
movq %r14, %rdi
callq 0x2ac7
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x50, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x2a9d
jmp 0x6314