// File: /workspace/github_c_makefile_star2_O2/sTeeLM[P]dwipe/device.c
// Function: dwipe_device_identify
void dwipe_device_identify( dwipe_context_t* c )
{
    /**
     * Gets device information and creates the context label.
     *
     * @parameter  c         A pointer to a device context.
     * @modifies   c->label  The menu description that the user sees.
     *
     */
     
    char buffer [FILENAME_MAX];
    FILE* fp = NULL;
    int size = 0;
    struct my_scsi_idlun sg_scsi;

    dwipe_log( DWIPE_LOG_INFO, "dwipe_device_identify '%s' .", c->device_name );

    /* Names in the partition file do not have the '/dev/' prefix. */
    const char dprefix [] = DWIPE_KNOB_PARTITIONS_PREFIX;

    /* Allocate memory for the label. */
    c->label = malloc( DWIPE_KNOB_LABEL_SIZE );
    if(!c->label)
        return;
    memset(c->label, 0, DWIPE_KNOB_LABEL_SIZE);

    if(ioctl(c->device_fd, SCSI_IOCTL_GET_IDLUN, &sg_scsi) != 0) 
    {
        dwipe_log( DWIPE_LOG_ERROR, "Error: Probe device %s SCSI ID error %d on fd %d.\n", c->device_name, errno, c->device_fd);
        goto err;
    } 
    else 
    {
        // (scsi_device_id | (lun << 8) | (channel << 16) | (host_no << 24))
        c->device_host = ((sg_scsi.four_in_one) & 0xFF000000) >> 24;
        c->device_bus = ((sg_scsi.four_in_one)  & 0x00FF0000) >> 16;
        c->device_lun = ((sg_scsi.four_in_one)  & 0x0000FF00) >> 8;
        c->device_target = ((sg_scsi.four_in_one) & 0xFF);

        dwipe_log( DWIPE_LOG_INFO, "Device %s at [%08x:%08x] = [Host:Channel:TargetId:Lun] [%d:%d:%d:%d].", 
            c->device_name, 
            sg_scsi.four_in_one,
            sg_scsi.host_unique_id,
            c->device_host, c->device_bus, c->device_target, c->device_lun);
    }

    if(dwipe_is_partition(&(c->device_name[strlen(dprefix)])))
    {
        strncpy( c->label, "  |------PARTATION ", DWIPE_KNOB_LABEL_SIZE);
        size = strlen(c->label);
        c->label[size - 1] = ' ';
        dwipe_log( DWIPE_LOG_ERROR, "size0 is %d", size);

        snprintf(buffer, sizeof(buffer), "/sys/class/block/%s/partition",  &(c->device_name[strlen(dprefix)]));
        buffer[FILENAME_MAX - 1] = 0;

        fp = fopen( buffer, "r" );
        if( fp == NULL )
        {
            dwipe_log( DWIPE_LOG_ERROR, "Error: Unable to open '%s'.", buffer );
            goto err;
        }

        if( fgets( buffer, sizeof(buffer), fp ) != NULL )
        {
            strncpy( c->label + size, buffer, DWIPE_KNOB_LABEL_SIZE - size );
            size = strlen(c->label);
            c->label[size - 1] = ' ';
            dwipe_log( DWIPE_LOG_ERROR, "size1 is %d", size);

            if( sscanf (buffer, "%i", &(c->device_part)) != 1) 
            {
                dwipe_log( DWIPE_LOG_ERROR, "Error: Unable to read device partition number'%s'.", c->device_name );
                goto err;
            }
        }
        else
        {
            strncpy( c->label, "Uninitialized Device", DWIPE_KNOB_LABEL_SIZE );
            goto err;
        }

        fclose(fp);
        fp = NULL;

    } else {

        /* read /sys/class/block/%s/device/vendor */
        snprintf(buffer, sizeof(buffer), "/sys/class/block/%s/device/vendor",  &(c->device_name[strlen(dprefix)]));
        buffer[FILENAME_MAX - 1] = 0;

        fp = fopen( buffer, "r" );
        if( fp == NULL )
        {
            dwipe_log( DWIPE_LOG_ERROR, "Error: Unable to open '%s'.", buffer );
            goto err;
        }

        if( fgets( buffer, sizeof(buffer), fp ) != NULL )
        {
            strncpy( c->label, buffer, DWIPE_KNOB_LABEL_SIZE );
            size = strlen(c->label);
            c->label[size - 1] = ' ';
            dwipe_log( DWIPE_LOG_ERROR, "size2 is %d", size);
        }
        else
        {
            strncpy( c->label, "Uninitialized Device", DWIPE_KNOB_LABEL_SIZE );
            goto err;
        }

        fclose(fp);
        fp = NULL;

        /* read /sys/class/block/%s/device/model */
        snprintf(buffer, sizeof(buffer), "/sys/class/block/%s/device/model",  &(c->device_name[strlen(dprefix)]));
        buffer[FILENAME_MAX - 1] = 0;

        fp = fopen( buffer, "r" );
        if( fp == NULL )
        {
            dwipe_log( DWIPE_LOG_ERROR, "Error: Unable to open '%s'.", buffer );
            goto err;
        }

        if( fgets( buffer, sizeof(buffer), fp ) != NULL )
        {
            strncpy( c->label + size, buffer, DWIPE_KNOB_LABEL_SIZE - size );
            size = strlen(c->label);
            c->label[size - 1] = ' ';
            dwipe_log( DWIPE_LOG_ERROR, "size3 is %d", size);
        }
        else
        {
            strncpy( c->label, "Uninitialized Device", DWIPE_KNOB_LABEL_SIZE );
            goto err;
        }
        fclose(fp);
        fp = NULL;

        /* /sys/class/block/%s/device/rev */
        snprintf(buffer, sizeof(buffer), "/sys/class/block/%s/device/rev",  &(c->device_name[strlen(dprefix)]));
        buffer[FILENAME_MAX - 1] = 0;

        fp = fopen( buffer, "r" );
        if( fp == NULL )
        {
            dwipe_log( DWIPE_LOG_ERROR, "Error: Unable to open '%s'.", buffer );
            goto err;
        }

        if( fgets( buffer, sizeof(buffer), fp ) != NULL )
        {
            strncpy( c->label + size, buffer, DWIPE_KNOB_LABEL_SIZE - size );
            size = strlen(c->label);
            c->label[size - 1] = ' ';
            dwipe_log( DWIPE_LOG_ERROR, "size4 is %d", size);
        }
        else
        {
            strncpy( c->label, "Uninitialized Device", DWIPE_KNOB_LABEL_SIZE );
            goto err;
        }
        fclose(fp);
        fp = NULL;
    }

    dwipe_log( DWIPE_LOG_ERROR, "size5 is %d", size);
    dwipe_device_strsize(c->label + size, DWIPE_KNOB_LABEL_SIZE - size, c->device_size);
    c->label[DWIPE_KNOB_LABEL_SIZE - 1] = 0;

    return;

err:
    if( fp != NULL )
    {
        fclose(fp);
        fp = NULL;
    }
    if(c->label) 
    {
        c->label[DWIPE_KNOB_LABEL_SIZE - 1] = 0;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1018, %rsp           # imm = 0x1018
movq %rdi, %rbx
movq 0x228(%rdi), %rdx
leaq 0x71a7(%rip), %rsi      # 0xa875
pushq $0x2
popq %rdi
xorl %eax, %eax
callq 0x73a8
pushq $0x1
popq %rdi
movl $0x200, %esi            # imm = 0x200
callq 0x3260
movq %rax, 0x2e0(%rbx)
testq %rax, %rax
je 0x3bfb
movl 0x8(%rbx), %edi
leaq 0x8(%rsp), %rdx
movl $0x5382, %esi           # imm = 0x5382
xorl %eax, %eax
callq 0x31e0
testl %eax, %eax
je 0x3738
movq 0x228(%rbx), %r14
callq 0x3070
movl (%rax), %ecx
movl 0x8(%rbx), %r8d
leaq 0x716c(%rip), %rsi      # 0xa892
pushq $0x5
popq %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x73a8
jmp 0x3be8
movl 0x8(%rsp), %ecx
movl %ecx, %r9d
shrl $0x18, %r9d
movl %r9d, 0xc(%rbx)
movl %ecx, %r10d
shrl $0x10, %r10d
movl $0xff, %r11d
andl %r11d, %r10d
movl %r10d, 0x4(%rbx)
movl %ecx, %r14d
shrl $0x8, %r14d
andl %r11d, %r14d
movl %r14d, 0x214(%rbx)
andl %ecx, %r11d
movl %r11d, 0x210(%rbx)
movq 0x228(%rbx), %rdx
movl 0xc(%rsp), %r8d
subq $0x8, %rsp
leaq 0x7138(%rip), %rsi      # 0xa8c5
pushq $0x2
popq %rdi
xorl %eax, %eax
pushq %r14
pushq %r11
pushq %r10
callq 0x73a8
addq $0x20, %rsp
movq 0x228(%rbx), %rdi
addq $0x5, %rdi
callq 0x362c
testl %eax, %eax
je 0x3944
movabsq $-0x100000000, %rbp     # imm = 0xFFFFFFFF00000000
movq 0x2e0(%rbx), %rdi
leaq 0x713a(%rip), %rsi      # 0xa90b
movl $0x200, %edx            # imm = 0x200
callq 0x3090
movq 0x2e0(%rbx), %r14
movq %r14, %rdi
callq 0x3150
movq %rax, %r15
shlq $0x20, %r15
leaq (%r15,%rbp), %rcx
sarq $0x20, %rcx
movb $0x20, (%r14,%rcx)
leaq 0x711a(%rip), %rsi      # 0xa91f
pushq $0x5
popq %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0x73a8
movq 0x228(%rbx), %rcx
addq $0x5, %rcx
leaq 0x6ff1(%rip), %rdx      # 0xa814
leaq 0x10(%rsp), %r14
movl $0x1000, %esi           # imm = 0x1000
movq %r14, %rdi
xorl %eax, %eax
callq 0x3180
movb $0x0, 0xfff(%r14)
leaq 0x6857(%rip), %rsi      # 0xa09d
movq %r14, %rdi
callq 0x3290
testq %rax, %rax
je 0x3b9d
movq %rax, %r14
leaq 0x10(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %rax, %rdx
callq 0x3250
movq 0x2e0(%rbx), %rdi
testq %rax, %rax
je 0x3bb5
movq %r15, %rax
sarq $0x20, %rax
addq %rax, %rdi
movabsq $0x20000000000, %rdx    # imm = 0x20000000000
subq %r15, %rdx
sarq $0x20, %rdx
leaq 0x10(%rsp), %r12
movq %r12, %rsi
callq 0x3090
movq 0x2e0(%rbx), %r13
movq %r13, %rdi
callq 0x3150
movq %rax, %r15
shlq $0x20, %rax
addq %rbp, %rax
sarq $0x20, %rax
movb $0x20, (%r13,%rax)
leaq 0x7079(%rip), %rsi      # 0xa947
pushq $0x5
popq %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0x73a8
leaq 0x220(%rbx), %rdx
leaq 0x959c(%rip), %rsi      # 0xce85
movq %r12, %rdi
xorl %eax, %eax
callq 0x3310
cmpl $0x1, %eax
jne 0x3bc8
movq %r14, %rdi
callq 0x3140
leaq 0x7116(%rip), %rsi      # 0xaa21
pushq $0x5
popq %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0x73a8
movslq %r15d, %rax
movq 0x2e0(%rbx), %rdi
addq %rax, %rdi
movl $0x200, %esi            # imm = 0x200
subl %eax, %esi
movq 0x230(%rbx), %rdx
callq 0x357c
movq 0x2e0(%rbx), %rax
jmp 0x3bf4
movq 0x228(%rbx), %rcx
addq $0x5, %rcx
leaq 0x7045(%rip), %rdx      # 0xa99b
leaq 0x10(%rsp), %r14
movl $0x1000, %esi           # imm = 0x1000
movq %r14, %rdi
xorl %eax, %eax
callq 0x3180
movb $0x0, 0xfff(%r14)
leaq 0x6724(%rip), %rsi      # 0xa09d
movq %r14, %rdi
callq 0x3290
testq %rax, %rax
je 0x3b9d
movq %rax, %r14
leaq 0x10(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %rax, %rdx
callq 0x3250
movq 0x2e0(%rbx), %rdi
testq %rax, %rax
je 0x3bb5
movabsq $-0x100000000, %rbp     # imm = 0xFFFFFFFF00000000
leaq 0x10(%rsp), %r15
movl $0x200, %edx            # imm = 0x200
movq %r15, %rsi
callq 0x3090
movq 0x2e0(%rbx), %r12
movq %r12, %rdi
callq 0x3150
movq %rax, %r13
shlq $0x20, %r13
movq %r13, %rcx
addq %rbp, %rcx
sarq $0x20, %rcx
movb $0x20, (%r12,%rcx)
leaq 0x6fc6(%rip), %rsi      # 0xa9bd
pushq $0x5
popq %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0x73a8
movq %r14, %rdi
callq 0x3140
movq 0x228(%rbx), %rcx
addq $0x5, %rcx
leaq 0x6fac(%rip), %rdx      # 0xa9c9
movl $0x1000, %esi           # imm = 0x1000
movq %r15, %rdi
xorl %eax, %eax
callq 0x3180
movb $0x0, 0xfff(%r15)
leaq 0x6662(%rip), %rsi      # 0xa09d
movq %r15, %rdi
callq 0x3290
testq %rax, %rax
je 0x3b9d
movq %rax, %r14
leaq 0x10(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %rax, %rdx
callq 0x3250
movq 0x2e0(%rbx), %rdi
testq %rax, %rax
je 0x3bb5
movabsq $0x20000000000, %r15    # imm = 0x20000000000
movq %r15, %rdx
subq %r13, %rdx
sarq $0x20, %r13
addq %r13, %rdi
sarq $0x20, %rdx
leaq 0x10(%rsp), %r12
movq %r12, %rsi
callq 0x3090
movq 0x2e0(%rbx), %r13
movq %r13, %rdi
callq 0x3150
movq %rbp, %rcx
movq %rax, %rbp
shlq $0x20, %rbp
addq %rbp, %rcx
sarq $0x20, %rcx
movb $0x20, (%r13,%rcx)
leaq 0x6f24(%rip), %rsi      # 0xa9ea
pushq $0x5
popq %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0x73a8
movq %r14, %rdi
callq 0x3140
movq 0x228(%rbx), %rcx
addq $0x5, %rcx
leaq 0x6f0a(%rip), %rdx      # 0xa9f6
movl $0x1000, %esi           # imm = 0x1000
movq %r12, %rdi
xorl %eax, %eax
callq 0x3180
movb $0x0, 0xfff(%r12)
leaq 0x6592(%rip), %rsi      # 0xa09d
movq %r12, %rdi
callq 0x3290
testq %rax, %rax
je 0x3b9d
movq %rax, %r14
leaq 0x10(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %rax, %rdx
callq 0x3250
movq 0x2e0(%rbx), %rdi
testq %rax, %rax
je 0x3bb5
subq %rbp, %r15
sarq $0x20, %rbp
addq %rbp, %rdi
sarq $0x20, %r15
leaq 0x10(%rsp), %rsi
movq %r15, %rdx
callq 0x3090
movq 0x2e0(%rbx), %r12
movq %r12, %rdi
callq 0x3150
movq %rax, %r15
shlq $0x20, %rax
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
addq %rcx, %rax
sarq $0x20, %rax
movb $0x20, (%r12,%rax)
leaq 0x6e8a(%rip), %rsi      # 0xaa15
pushq $0x5
popq %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0x73a8
jmp 0x38fc
leaq 0x6d87(%rip), %rsi      # 0xa92b
pushq $0x5
popq %rdi
leaq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x73a8
jmp 0x3be8
leaq 0x6dca(%rip), %rsi      # 0xa986
movl $0x200, %edx            # imm = 0x200
callq 0x3090
jmp 0x3be0
movq 0x228(%rbx), %rdx
leaq 0x6d7d(%rip), %rsi      # 0xa953
pushq $0x5
popq %rdi
xorl %eax, %eax
callq 0x73a8
movq %r14, %rdi
callq 0x3140
movq 0x2e0(%rbx), %rax
testq %rax, %rax
je 0x3bfb
movb $0x0, 0x1ff(%rax)
addq $0x1018, %rsp           # imm = 0x1018
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq