// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x1048, %rsp           # imm = 0x1048
movq %rdi, 0x1040(%rsp)
movq $0x0, 0x38(%rsp)
movl $0x0, 0x34(%rsp)
movq 0x1040(%rsp), %rax
movq 0x228(%rax), %rdx
movl $0x2, %edi
leaq 0xc07c(%rip), %rsi      # 0xf8a7
movb $0x0, %al
callq 0xade0
movl 0xc08c(%rip), %eax      # 0xf8c4
movl %eax, 0x26(%rsp)
movw 0xc085(%rip), %ax       # 0xf8c8
movw %ax, 0x2a(%rsp)
movl $0x200, %edi            # imm = 0x200
callq 0x32f0
movq %rax, %rcx
movq 0x1040(%rsp), %rax
movq %rcx, 0x2e0(%rax)
movq 0x1040(%rsp), %rax
cmpq $0x0, 0x2e0(%rax)
jne 0x387b
jmp 0x402f
movq 0x1040(%rsp), %rax
movq 0x2e0(%rax), %rdi
xorl %esi, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x31d0
movq 0x1040(%rsp), %rax
movl 0x8(%rax), %edi
movl $0x5382, %esi           # imm = 0x5382
leaq 0x2c(%rsp), %rdx
movb $0x0, %al
callq 0x31e0
cmpl $0x0, %eax
je 0x38fb
movq 0x1040(%rsp), %rax
movq 0x228(%rax), %rax
movq %rax, 0x18(%rsp)
callq 0x3070
movq 0x18(%rsp), %rdx
movl (%rax), %ecx
movq 0x1040(%rsp), %rax
movl 0x8(%rax), %r8d
movl $0x5, %edi
leaq 0xbfdb(%rip), %rsi      # 0xf8ca
movb $0x0, %al
callq 0xade0
jmp 0x3fec
movl 0x2c(%rsp), %ecx
andl $0xff000000, %ecx       # imm = 0xFF000000
shrl $0x18, %ecx
movq 0x1040(%rsp), %rax
movl %ecx, 0xc(%rax)
movl 0x2c(%rsp), %ecx
andl $0xff0000, %ecx         # imm = 0xFF0000
sarl $0x10, %ecx
movq 0x1040(%rsp), %rax
movl %ecx, 0x4(%rax)
movl 0x2c(%rsp), %ecx
andl $0xff00, %ecx           # imm = 0xFF00
sarl $0x8, %ecx
movq 0x1040(%rsp), %rax
movl %ecx, 0x214(%rax)
movl 0x2c(%rsp), %ecx
andl $0xff, %ecx
movq 0x1040(%rsp), %rax
movl %ecx, 0x210(%rax)
movq 0x1040(%rsp), %rax
movq 0x228(%rax), %rdx
movl 0x2c(%rsp), %ecx
movl 0x30(%rsp), %r8d
movq 0x1040(%rsp), %rax
movl 0xc(%rax), %r9d
movq 0x1040(%rsp), %rax
movl 0x4(%rax), %r11d
movq 0x1040(%rsp), %rax
movl 0x210(%rax), %r10d
movq 0x1040(%rsp), %rax
movl 0x214(%rax), %eax
movl $0x2, %edi
leaq 0xbf46(%rip), %rsi      # 0xf8fd
movl %r11d, (%rsp)
movl %r10d, 0x8(%rsp)
movl %eax, 0x10(%rsp)
movb $0x0, %al
callq 0xade0
movq 0x1040(%rsp), %rax
movq 0x228(%rax), %rdi
addq $0x5, %rdi
callq 0x3740
cmpl $0x0, %eax
je 0x3bea
movq 0x1040(%rsp), %rax
movq 0x2e0(%rax), %rdi
leaq 0xbf41(%rip), %rsi      # 0xf943
movl $0x200, %edx            # imm = 0x200
callq 0x3090
movq 0x1040(%rsp), %rax
movq 0x2e0(%rax), %rdi
callq 0x3150
movl %eax, 0x34(%rsp)
movq 0x1040(%rsp), %rax
movq 0x2e0(%rax), %rax
movl 0x34(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movb $0x20, (%rax,%rcx)
movl 0x34(%rsp), %edx
movl $0x5, %edi
leaq 0xbf06(%rip), %rsi      # 0xf957
movb $0x0, %al
callq 0xade0
leaq 0x40(%rsp), %rdi
movq 0x1040(%rsp), %rax
movq 0x228(%rax), %rcx
addq $0x5, %rcx
movl $0x1000, %esi           # imm = 0x1000
leaq 0xbdca(%rip), %rdx      # 0xf846
movb $0x0, %al
callq 0x3180
movb $0x0, 0x103f(%rsp)
leaq 0x40(%rsp), %rdi
leaq 0xb61e(%rip), %rsi      # 0xf0b5
callq 0x3280
movq %rax, 0x38(%rsp)
cmpq $0x0, 0x38(%rsp)
jne 0x3ac6
leaq 0x40(%rsp), %rdx
movl $0x5, %edi
leaq 0xbea9(%rip), %rsi      # 0xf963
movb $0x0, %al
callq 0xade0
jmp 0x3fec
leaq 0x40(%rsp), %rdi
movq 0x38(%rsp), %rdx
movl $0x1000, %esi           # imm = 0x1000
callq 0x3250
cmpq $0x0, %rax
je 0x3bad
movq 0x1040(%rsp), %rax
movq 0x2e0(%rax), %rdi
movslq 0x34(%rsp), %rax
addq %rax, %rdi
leaq 0x40(%rsp), %rsi
movl $0x200, %eax            # imm = 0x200
subl 0x34(%rsp), %eax
movslq %eax, %rdx
callq 0x3090
movq 0x1040(%rsp), %rax
movq 0x2e0(%rax), %rdi
callq 0x3150
movl %eax, 0x34(%rsp)
movq 0x1040(%rsp), %rax
movq 0x2e0(%rax), %rax
movl 0x34(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movb $0x20, (%rax,%rcx)
movl 0x34(%rsp), %edx
movl $0x5, %edi
leaq 0xbe29(%rip), %rsi      # 0xf97f
movb $0x0, %al
callq 0xade0
leaq 0x40(%rsp), %rdi
movq 0x1040(%rsp), %rdx
addq $0x220, %rdx            # imm = 0x220
leaq 0xe2f5(%rip), %rsi      # 0x11e6d
movb $0x0, %al
callq 0x3300
cmpl $0x1, %eax
je 0x3bab
movq 0x1040(%rsp), %rax
movq 0x228(%rax), %rdx
movl $0x5, %edi
leaq 0xbdec(%rip), %rsi      # 0xf98b
movb $0x0, %al
callq 0xade0
jmp 0x3fec
jmp 0x3bd2
movq 0x1040(%rsp), %rax
movq 0x2e0(%rax), %rdi
leaq 0xbdfb(%rip), %rsi      # 0xf9be
movl $0x200, %edx            # imm = 0x200
callq 0x3090
jmp 0x3fec
movq 0x38(%rsp), %rdi
callq 0x3140
movq $0x0, 0x38(%rsp)
jmp 0x3f89
leaq 0x40(%rsp), %rdi
movq 0x1040(%rsp), %rax
movq 0x228(%rax), %rcx
addq $0x5, %rcx
movl $0x1000, %esi           # imm = 0x1000
leaq 0xbdc5(%rip), %rdx      # 0xf9d3
movb $0x0, %al
callq 0x3180
movb $0x0, 0x103f(%rsp)
leaq 0x40(%rsp), %rdi
leaq 0xb48c(%rip), %rsi      # 0xf0b5
callq 0x3280
movq %rax, 0x38(%rsp)
cmpq $0x0, 0x38(%rsp)
jne 0x3c58
leaq 0x40(%rsp), %rdx
movl $0x5, %edi
leaq 0xbd17(%rip), %rsi      # 0xf963
movb $0x0, %al
callq 0xade0
jmp 0x3fec
leaq 0x40(%rsp), %rdi
movq 0x38(%rsp), %rdx
movl $0x1000, %esi           # imm = 0x1000
callq 0x3250
cmpq $0x0, %rax
je 0x3cde
movq 0x1040(%rsp), %rax
movq 0x2e0(%rax), %rdi
leaq 0x40(%rsp), %rsi
movl $0x200, %edx            # imm = 0x200
callq 0x3090
movq 0x1040(%rsp), %rax
movq 0x2e0(%rax), %rdi
callq 0x3150
movl %eax, 0x34(%rsp)
movq 0x1040(%rsp), %rax
movq 0x2e0(%rax), %rax
movl 0x34(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movb $0x20, (%rax,%rcx)
movl 0x34(%rsp), %edx
movl $0x5, %edi
leaq 0xbd20(%rip), %rsi      # 0xf9f5
movb $0x0, %al
callq 0xade0
jmp 0x3d03
movq 0x1040(%rsp), %rax
movq 0x2e0(%rax), %rdi
leaq 0xbcca(%rip), %rsi      # 0xf9be
movl $0x200, %edx            # imm = 0x200
callq 0x3090
jmp 0x3fec
movq 0x38(%rsp), %rdi
callq 0x3140
movq $0x0, 0x38(%rsp)
leaq 0x40(%rsp), %rdi
movq 0x1040(%rsp), %rax
movq 0x228(%rax), %rcx
addq $0x5, %rcx
movl $0x1000, %esi           # imm = 0x1000
leaq 0xbcc7(%rip), %rdx      # 0xfa01
movb $0x0, %al
callq 0x3180
movb $0x0, 0x103f(%rsp)
leaq 0x40(%rsp), %rdi
leaq 0xb360(%rip), %rsi      # 0xf0b5
callq 0x3280
movq %rax, 0x38(%rsp)
cmpq $0x0, 0x38(%rsp)
jne 0x3d84
leaq 0x40(%rsp), %rdx
movl $0x5, %edi
leaq 0xbbeb(%rip), %rsi      # 0xf963
movb $0x0, %al
callq 0xade0
jmp 0x3fec
leaq 0x40(%rsp), %rdi
movq 0x38(%rsp), %rdx
movl $0x1000, %esi           # imm = 0x1000
callq 0x3250
cmpq $0x0, %rax
je 0x3e19
movq 0x1040(%rsp), %rax
movq 0x2e0(%rax), %rdi
movslq 0x34(%rsp), %rax
addq %rax, %rdi
leaq 0x40(%rsp), %rsi
movl $0x200, %eax            # imm = 0x200
subl 0x34(%rsp), %eax
movslq %eax, %rdx
callq 0x3090
movq 0x1040(%rsp), %rax
movq 0x2e0(%rax), %rdi
callq 0x3150
movl %eax, 0x34(%rsp)
movq 0x1040(%rsp), %rax
movq 0x2e0(%rax), %rax
movl 0x34(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movb $0x20, (%rax,%rcx)
movl 0x34(%rsp), %edx
movl $0x5, %edi
leaq 0xbc12(%rip), %rsi      # 0xfa22
movb $0x0, %al
callq 0xade0
jmp 0x3e3e
movq 0x1040(%rsp), %rax
movq 0x2e0(%rax), %rdi
leaq 0xbb8f(%rip), %rsi      # 0xf9be
movl $0x200, %edx            # imm = 0x200
callq 0x3090
jmp 0x3fec
movq 0x38(%rsp), %rdi
callq 0x3140
movq $0x0, 0x38(%rsp)
leaq 0x40(%rsp), %rdi
movq 0x1040(%rsp), %rax
movq 0x228(%rax), %rcx
addq $0x5, %rcx
movl $0x1000, %esi           # imm = 0x1000
leaq 0xbbb9(%rip), %rdx      # 0xfa2e
movb $0x0, %al
callq 0x3180
movb $0x0, 0x103f(%rsp)
leaq 0x40(%rsp), %rdi
leaq 0xb225(%rip), %rsi      # 0xf0b5
callq 0x3280
movq %rax, 0x38(%rsp)
cmpq $0x0, 0x38(%rsp)
jne 0x3ebf
leaq 0x40(%rsp), %rdx
movl $0x5, %edi
leaq 0xbab0(%rip), %rsi      # 0xf963
movb $0x0, %al
callq 0xade0
jmp 0x3fec
leaq 0x40(%rsp), %rdi
movq 0x38(%rsp), %rdx
movl $0x1000, %esi           # imm = 0x1000
callq 0x3250
cmpq $0x0, %rax
je 0x3f54
movq 0x1040(%rsp), %rax
movq 0x2e0(%rax), %rdi
movslq 0x34(%rsp), %rax
addq %rax, %rdi
leaq 0x40(%rsp), %rsi
movl $0x200, %eax            # imm = 0x200
subl 0x34(%rsp), %eax
movslq %eax, %rdx
callq 0x3090
movq 0x1040(%rsp), %rax
movq 0x2e0(%rax), %rdi
callq 0x3150
movl %eax, 0x34(%rsp)
movq 0x1040(%rsp), %rax
movq 0x2e0(%rax), %rax
movl 0x34(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movb $0x20, (%rax,%rcx)
movl 0x34(%rsp), %edx
movl $0x5, %edi
leaq 0xbb02(%rip), %rsi      # 0xfa4d
movb $0x0, %al
callq 0xade0
jmp 0x3f76
movq 0x1040(%rsp), %rax
movq 0x2e0(%rax), %rdi
leaq 0xba54(%rip), %rsi      # 0xf9be
movl $0x200, %edx            # imm = 0x200
callq 0x3090
jmp 0x3fec
movq 0x38(%rsp), %rdi
callq 0x3140
movq $0x0, 0x38(%rsp)
movl 0x34(%rsp), %edx
movl $0x5, %edi
leaq 0xbac0(%rip), %rsi      # 0xfa59
movb $0x0, %al
callq 0xade0
movq 0x1040(%rsp), %rax
movq 0x2e0(%rax), %rdi
movslq 0x34(%rsp), %rax
addq %rax, %rdi
movl $0x200, %esi            # imm = 0x200
subl 0x34(%rsp), %esi
movq 0x1040(%rsp), %rax
movq 0x230(%rax), %rdx
callq 0x3550
movq 0x1040(%rsp), %rax
movq 0x2e0(%rax), %rax
movb $0x0, 0x1ff(%rax)
jmp 0x402f
cmpq $0x0, 0x38(%rsp)
je 0x4007
movq 0x38(%rsp), %rdi
callq 0x3140
movq $0x0, 0x38(%rsp)
movq 0x1040(%rsp), %rax
cmpq $0x0, 0x2e0(%rax)
je 0x402f
movq 0x1040(%rsp), %rax
movq 0x2e0(%rax), %rax
movb $0x0, 0x1ff(%rax)
addq $0x1048, %rsp           # imm = 0x1048
retq
nopw (%rax,%rax)