// File: /workspace/github_c_makefile_star2_O3/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 0x7ed6(%rip), %rsi      # 0xb875
movl $0x2, %edi
xorl %eax, %eax
callq 0x7a7c
movl $0x1, %edi
movl $0x200, %esi            # imm = 0x200
callq 0x3260
movq %rax, 0x2e0(%rbx)
testq %rax, %rax
je 0x3ee4
movl 0x8(%rbx), %edi
leaq 0x8(%rsp), %rdx
movl $0x5382, %esi           # imm = 0x5382
xorl %eax, %eax
callq 0x31e0
testl %eax, %eax
je 0x3a0f
movq 0x228(%rbx), %r14
callq 0x3070
movl (%rax), %ecx
movl 0x8(%rbx), %r8d
leaq 0x7e97(%rip), %rsi      # 0xb892
movl $0x5, %edi
movq %r14, %rdx
xorl %eax, %eax
callq 0x7a7c
jmp 0x3ed1
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 0x7e61(%rip), %rsi      # 0xb8c5
movl $0x2, %edi
xorl %eax, %eax
pushq %r14
pushq %r11
pushq %r10
callq 0x7a7c
addq $0x20, %rsp
movq 0x228(%rbx), %rdi
addq $0x5, %rdi
callq 0x38fc
testl %eax, %eax
je 0x3c23
movabsq $-0x100000000, %rbp     # imm = 0xFFFFFFFF00000000
movq 0x2e0(%rbx), %rdi
leaq 0x7e61(%rip), %rsi      # 0xb90b
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 0x7e41(%rip), %rsi      # 0xb91f
movl $0x5, %edi
movl %eax, %edx
xorl %eax, %eax
callq 0x7a7c
movq 0x228(%rbx), %rcx
addq $0x5, %rcx
leaq 0x7d16(%rip), %rdx      # 0xb814
leaq 0x10(%rsp), %r14
movl $0x1000, %esi           # imm = 0x1000
movq %r14, %rdi
xorl %eax, %eax
callq 0x3180
movb $0x0, 0xfff(%r14)
leaq 0x757c(%rip), %rsi      # 0xb09d
movq %r14, %rdi
callq 0x3290
testq %rax, %rax
je 0x3e82
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 0x3e9c
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 0x7d9e(%rip), %rsi      # 0xb947
movl $0x5, %edi
movl %r15d, %edx
xorl %eax, %eax
callq 0x7a7c
leaq 0x220(%rbx), %rdx
leaq 0xa2f3(%rip), %rsi      # 0xdeb9
movq %r12, %rdi
xorl %eax, %eax
callq 0x3310
cmpl $0x1, %eax
jne 0x3eaf
movq %r14, %rdi
callq 0x3140
leaq 0x7e39(%rip), %rsi      # 0xba21
movl $0x5, %edi
movl %r15d, %edx
xorl %eax, %eax
callq 0x7a7c
movslq %r15d, %rax
movq 0x2e0(%rbx), %rdi
addq %rax, %rdi
movl $0x200, %esi            # imm = 0x200
subl %eax, %esi
movq 0x230(%rbx), %rdx
callq 0x384c
movq 0x2e0(%rbx), %rax
jmp 0x3edd
movq 0x228(%rbx), %rcx
addq $0x5, %rcx
leaq 0x7d66(%rip), %rdx      # 0xb99b
leaq 0x10(%rsp), %r14
movl $0x1000, %esi           # imm = 0x1000
movq %r14, %rdi
xorl %eax, %eax
callq 0x3180
movb $0x0, 0xfff(%r14)
leaq 0x7445(%rip), %rsi      # 0xb09d
movq %r14, %rdi
callq 0x3290
testq %rax, %rax
je 0x3e82
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 0x3e9c
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 0x7ce7(%rip), %rsi      # 0xb9bd
movl $0x5, %edi
movl %eax, %edx
xorl %eax, %eax
callq 0x7a7c
movq %r14, %rdi
callq 0x3140
movq 0x228(%rbx), %rcx
addq $0x5, %rcx
leaq 0x7ccb(%rip), %rdx      # 0xb9c9
movl $0x1000, %esi           # imm = 0x1000
movq %r15, %rdi
xorl %eax, %eax
callq 0x3180
movb $0x0, 0xfff(%r15)
leaq 0x7381(%rip), %rsi      # 0xb09d
movq %r15, %rdi
callq 0x3290
testq %rax, %rax
je 0x3e82
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 0x3e9c
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 0x7c43(%rip), %rsi      # 0xb9ea
movl $0x5, %edi
movl %eax, %edx
xorl %eax, %eax
callq 0x7a7c
movq %r14, %rdi
callq 0x3140
movq 0x228(%rbx), %rcx
addq $0x5, %rcx
leaq 0x7c27(%rip), %rdx      # 0xb9f6
movl $0x1000, %esi           # imm = 0x1000
movq %r12, %rdi
xorl %eax, %eax
callq 0x3180
movb $0x0, 0xfff(%r12)
leaq 0x72af(%rip), %rsi      # 0xb09d
movq %r12, %rdi
callq 0x3290
testq %rax, %rax
je 0x3e82
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 0x3e9c
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 0x7ba7(%rip), %rsi      # 0xba15
movl $0x5, %edi
movl %r15d, %edx
xorl %eax, %eax
callq 0x7a7c
jmp 0x3bd9
leaq 0x7aa2(%rip), %rsi      # 0xb92b
leaq 0x10(%rsp), %rdx
movl $0x5, %edi
xorl %eax, %eax
callq 0x7a7c
jmp 0x3ed1
leaq 0x7ae3(%rip), %rsi      # 0xb986
movl $0x200, %edx            # imm = 0x200
callq 0x3090
jmp 0x3ec9
movq 0x228(%rbx), %rdx
leaq 0x7a96(%rip), %rsi      # 0xb953
movl $0x5, %edi
xorl %eax, %eax
callq 0x7a7c
movq %r14, %rdi
callq 0x3140
movq 0x2e0(%rbx), %rax
testq %rax, %rax
je 0x3ee4
movb $0x0, 0x1ff(%rax)
addq $0x1018, %rsp           # imm = 0x1018
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq