SCSI阅读(10)和Write(10)与SCSI通用接口(SCSI Read (10) and W

2019-07-30 20:55发布

我试着发出SCSI 读(10)和写(10)的SSD。 我用这个例子中的代码作为参考/基本代码。

这是我的SCSI读:

#define READ_REPLY_LEN 32
#define READ_CMDLEN 10
void scsi_read()
{
  unsigned char Readbuffer[ SCSI_OFF + READ_REPLY_LEN ];
  unsigned char cmdblk [ READ_CMDLEN ] =
      {        0x28,  /* command */
                  0,  /* lun/reserved */
                  0,  /* lba */
                  0,  /* lba */
                  0,  /* lba */
                  0,  /* lba */
                  0,  /* reserved */
                  0,  /* transfer length */
     READ_REPLY_LEN,  /* transfer length */
                  0 };/* reserved/flag/link */
  memset(Readbuffer,0,sizeof(Readbuffer));
  memcpy( cmd + SCSI_OFF, cmdblk, sizeof(cmdblk) );

  /*
   * +------------------+
   * | struct sg_header | <- cmd
   * +------------------+
   * | copy of cmdblk   | <- cmd + SCSI_OFF
   * +------------------+
   */

  if (handle_scsi_cmd(sizeof(cmdblk), 0, cmd,
                      sizeof(Readbuffer) - SCSI_OFF, Readbuffer )) {
      fprintf( stderr, "read failed\n" );
      exit(2);
  }
  hex_dump(Readbuffer,sizeof(Readbuffer));
}

这是我的SCSI写:

void scsi_write ( void )
{
  unsigned char Writebuffer[SCSI_OFF];
  unsigned char cmdblk [] =
      {        0x2A,  /* 0: command */
                  0,  /* 1: lun/reserved */
                  0,  /* 2: LBA */
                  0,  /* 3: LBA */
                  0,  /* 4: LBA */
                  0,  /* 5: LBA */
                  0,  /* 6: reserved */
                  0,  /* 7: transfer length */
                  0,  /* 8: transfer length */
                  0 };/* 9: control */

  memset(Writebuffer,0,sizeof(Writebuffer));
  memcpy( cmd + SCSI_OFF, cmdblk, sizeof(cmdblk) );
  cmd[SCSI_OFF+sizeof(cmdblk)+0] = 'A';
  cmd[SCSI_OFF+sizeof(cmdblk)+1] = 'b';
  cmd[SCSI_OFF+sizeof(cmdblk)+2] = 'c';
  cmd[SCSI_OFF+sizeof(cmdblk)+3] = 'd';
  cmd[SCSI_OFF+sizeof(cmdblk)+4] = 'e';
  cmd[SCSI_OFF+sizeof(cmdblk)+5] = 'f';
  cmd[SCSI_OFF+sizeof(cmdblk)+6] = 'g';
  cmd[SCSI_OFF+sizeof(cmdblk)+7] = 0;
  /*
   * +------------------+
   * | struct sg_header | <- cmd
   * +------------------+
   * | copy of cmdblk   | <- cmd + SCSI_OFF
   * +------------------+
   * | data to write    | 
   * +------------------+
   */

  if (handle_scsi_cmd(sizeof(cmdblk), 8, cmd, 
                      sizeof(Writebuffer) - SCSI_OFF, Writebuffer )) {
      fprintf( stderr, "write failed\n" );
      exit(2);
  }
}

在下面的例子中,我做

  1. SCSI读
  2. SCSI写
  3. SCSI读

我打印的写入数据(SCSI写入)什么的hexdumps读取(SCSI读)

Read(10)
[0000]   00 00 00 44 00 00 00 44   00 00 00 00 00 00 00 00   ...D...D ........
[0010]   00 2C 00 00 00 00 00 00   00 00 00 00 00 00 00 00   ........ ........
[0020]   00 00 00 00 00 00 00 00   00 00 00 00 00 00 00 00   ........ ........
[0030]   00 00 00 00 00 00 00 00   00 00 00 00 00 00 00 00   ........ ........
[0040]   00 00 00 00                                         ....

Write(10):
[0000]   00 00 00 00 00 00 00 24   00 00 00 00 00 00 00 00   ........ ........
[0010]   00 00 00 00 00 00 00 00   00 00 00 00 00 00 00 00   ........ ........
[0020]   00 00 00 00 2A 00 00 00   00 00 00 00 00 00 41 62   ........ ......Ab
[0030]   63 64 65 66 67 00                                   cdefg.

Read(10):
[0000]   00 00 00 44 00 00 00 44   00 00 00 00 00 00 00 00   ...D...D ........
[0010]   04 00 20 00 70 00 02 00   00 00 00 0A 00 00 00 00   ....p... ........
[0020]   04 00 00 00 41 62 63 64   65 66 67 00 00 00 00 00   ....Abcd efg.....
[0030]   00 00 00 00 00 00 00 00   00 00 00 00 00 00 00 00   ........ ........
[0040]   00 00 00 00                                         ....

压脚提升再次运行三个命令,我应该读Abcdefg与第一读取。 对? 但是,再次运行它们改变不了什么。 现在,您可以假定,我用的是记忆有因之前funcions的数据,但我得到即使我运行了相同的结果memset(Readbuff,0,sizeof(Readbuff))的前sys_read()发生。

我以为,我尽量写的LBA也许是禁止写的,我读缓存。 但是从0x00-0xFF在作用中LBA不会忽略改变不了什么-这意味着,我读了相同的数据( Abcdefg )。

你知道SCSI的示例实现读取或SCSI通用接口写?

Answer 1:

在SCSI中,LBA和传输长度的单位是块,有时称为扇区。 这几乎总是512字节。 所以,你不能读取或写入只是32个字节。 至少,你必须做的512个字节==一个街区。 这一点是你最需要解决什么。

您的传输长度是在你的scsi_write实现零,所以它实际上没有打算写任何数据。

您应该使用国家开发银行和写入不同的缓冲器/读取数据。 我怀疑这些缓冲区的混乱是导致您的实现来写过去的静态分配的阵列之一的结束,在你ReadBuffer。 下运行它的valgrind看到显示的内容。

最后,有很多可以去错在无论是在handle_scsi_cmd。 它可能会非常棘手建立数据传输......特别是,确保你直上的数据会在I / O头的dxfer_direction哪一种方式:SG_DXFER_TO_DEV写,SG_DXFER_FROM_DEV读。

看看如何做一个读(16)这个例子。 这是沿着你想要完成什么线条更。

https://github.com/hreinecke/sg3_utils/blob/master/examples/sg_simple16.c



文章来源: SCSI Read (10) and Write (10) with the SCSI Generic Interface