start_addr=0x30000000 end_addr=0x30000003 data_write=0x55AA55AA count=0 match_count=0 # 写入数据 for ((addr=$start_addr; addr<$end_addr+1; addr=addr+1)); do # echo "addr: $addr" #echo -n -e "\\x$(printf %08X $(($addr / 256)))\n" echo "addr=$(printf '%X' $addr)" echo -ne "\x55\xAA\x55\xAA" | dd of=/dev/mem bs=4 count=1 seek=$(($addr/8)) conv=notrunc 2>/dev/null data=$(echo -ne "\x55\xAA\x55\xAA" | hexdump -e '/4 "%08x\n"') data_text=$(echo -ne "\x55\xAA\x55\xAA" | hexdump -e '/4 "%08x\n"' | sed 's/../& /g') echo "data_text: $data_text" #echo "data: $data" data_read=$(dd if=/dev/mem bs=4 count=1 skip=$(($addr/4)) 2>/dev/null | hexdump -e '/4 "%08x\n"') data_read_text=$(dd if=/dev/mem bs=4 count=1 skip=$(($addr/4)) 2>/dev/null | hexdump -e '/4 "%08x\n"' | sed 's/../& /g') echo "data_read_text: $data_read_text" # echo "data_read: $data_read" if [ "$data_read" == "$data" ]; then match_count=$((match_count+1)) echo "match_count: $match_count" fi count=$((count+1)) echo "count: $count" done # 输出结果 # 判断结果 if [ $match_count -eq $count ]; then echo "PASS" else echo "FAIL" fi判断结果输出为PASS,但是data_read_text输出空白,应该如何解决
时间: 2024-03-01 15:55:33 浏览: 103
在/dev/mem设备中读写内存需要root权限,如果你没有足够的权限,可能会导致读写失败并且无法输出数据。请确保你使用root权限运行脚本或者已经获得了足够的权限。
另外,你在 dd 命令中设置的 bs 参数应该为 8 而不是 4。因为你的数据宽度是 32 位,每个地址应该占用 8 个字节,而不是 4 个字节。修改 bs 参数后重新运行脚本,看看是否能够正确输出数据。
最后,你可以使用 xxd 命令来代替 hexdump 命令,因为 xxd 命令在输出十六进制数据时更为简洁。例如:
```
data_text=$(echo -ne "\x55\xAA\x55\xAA" | xxd -p -c 4)
```
这将输出 "55aa55aa",其中 -p 参数表示输出为纯十六进制字符串,而 -c 4 参数表示每行输出 4 个字节的数据。
综上所述,以下是修改后的脚本:
```
start_addr=0x30000000
end_addr=0x30000003
data_write=0x55AA55AA
count=0
match_count=0
# 写入数据
for ((addr=$start_addr; addr<$end_addr+1; addr=addr+8)); do
echo "addr=$(printf '%X' $addr)"
echo -ne "\x55\xAA\x55\xAA" | dd of=/dev/mem bs=8 count=1 seek=$(($addr/8)) conv=notrunc 2>/dev/null
data_text=$(echo -ne "\x55\xAA\x55\xAA" | xxd -p -c 4)
echo "data_text: $data_text"
data_read_text=$(dd if=/dev/mem bs=8 count=1 skip=$(($addr/8)) 2>/dev/null | xxd -p -c 4)
echo "data_read_text: $data_read_text"
if [ "$data_read_text" == "$data_text" ]; then
match_count=$((match_count+1))
echo "match_count: $match_count"
fi
count=$((count+1))
echo "count: $count"
done
# 输出结果
if [ $match_count -eq $count ]; then
echo "PASS"
else
echo "FAIL"
fi
```
希望这能够帮助你解决问题。
阅读全文