gpt4 book ai didi

linux - 使用 SCSI 通用接口(interface)的 SCSI 读取 (10) 和写入 (10)

转载 作者:IT王子 更新时间:2023-10-29 00:55:25 27 4
gpt4 key购买 nike

我尝试发出 scsi read(10)write(10)到 SSD。我用 this example code作为引用/基本代码。

这是我的 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 写入)和读取数据(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。正确的?但是再次运行它们没有任何改变。你现在可以假设,我使用的内存仍然有以前函数的数据,但即使我在 之前运行 memset(Readbuff,0,sizeof(Readbuff)) >sys_read() 发生。

我假设,我尝试写入的 LBA 可能被禁止写入,并且我读取了缓存。但是从 0x00-0xFF 对 LBA 地址进行交互不会改变任何东西 - 这意味着,我读取了相同的数据 (Abcdefg)。

您知道使用 scsi 通用接口(interface)执行 scsi 读取或写入的示例吗?

最佳答案

在SCSI中,LBA和传输长度的单位是 block ,有时也称为扇区。这几乎总是 512 字节。所以,你不能只读或写 32 个字节。至少,您必须执行 512 字节 == 一个 block 。这一点是您需要修复的大部分内容。

您的 scsi_write 实现中的传输长度为零,因此它实际上不会写入任何数据。

您应该为 CDB 和写入/读取数据使用不同的缓冲区。我怀疑对这些缓冲区的混淆会导致您的实现写入超过您的静态分配数组之一的末尾并覆盖您的 ReadBuffer。在 valgrind 下运行看看会出现什么。

最后,handle_scsi_cmd 中的任何内容都可能出错。设置数据传输可能很棘手……特别是,请确保您直接了解数据在 I/O header 的 dxfer_direction 中的传输方向:SG_DXFER_TO_DEV 用于写入,SG_DXFER_FROM_DEV 用于读取。

查看此示例,了解如何执行 read(16)。这更符合您要实现的目标。

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

关于linux - 使用 SCSI 通用接口(interface)的 SCSI 读取 (10) 和写入 (10),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11867612/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com