Skip to content

Commit

Permalink
device,sdcard: support write
Browse files Browse the repository at this point in the history
* To perform a write to the sdcard, the driver will send a command which
  will wait for the busy interrupt. Since the sdcard device and the
  driver are simplified, and there is no interrupt. Therefore the write
  command can not finish completely.
* To fix this issue, we just do not set the use_busy property.
  • Loading branch information
sashimi-yzh committed Sep 29, 2020
1 parent ac6af7d commit 3bcf376
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 28 deletions.
11 changes: 3 additions & 8 deletions resource/debian/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ apt-get install haveged
agt-get install sbt
```

* 删除登录密码, 登录时输入`root`后可直接登录
```
passwd -d root
```

*`/etc/fstab`中添加swap分区
```
/dev/mmcblk0p2 none swap sw 0 0
Expand All @@ -65,9 +60,7 @@ passwd -d root
chmod +w,+x /root
```

* 在/root/目录下提前写入所需的测试文件, 如hello.c等. 将来以只读方式挂载时, 无法写入文件.

* 若要创建文件, 可在/run/目录下创建, 它是个tmpfs
* 在/root/目录下提前写入所需的测试文件, 如hello.c等.

* 在/root/.bashrc中添加如下内容, 可以实现登录后自动运行命令(根据实际情况修改测试的命令):
```
Expand Down Expand Up @@ -153,3 +146,5 @@ sudo losetup -d /dev/loop0 # 删除loop设备

* 修改`nemu/src/device/sdcard.c``init_sdcard()`中打开的镜像文件路径, 即可使用制作的镜像.
在i9-9900k上测试, 约90s后看到debian的登录提示符.

* 当以可写方式启动镜像是, NEMU遇到错误或通过Ctrl+C直接退出NEMU时, 可能会损坏镜像的崩溃一致性, 此时可以通过fsck命令修复分区.
2 changes: 0 additions & 2 deletions resource/sdcard/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
# NEMU sdhost驱动

本驱动裁剪自`linux/drivers/mmc/host/bcm2835.c`, 去除了DMA和中断, 改成直接轮询, 处理器无需支持DMA和中断即可运行.
不支持写操作, 可以以只读方式启动debian.
若需要测试文件系统的写操作, 可在启动debian后挂载ramdisk, 在ramdisk上进行写入.

## 使用方法

Expand Down
2 changes: 1 addition & 1 deletion resource/sdcard/nemu.c
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ bool nemu_send_command(struct nemu_host *host, struct mmc_command *cmd)
sdcmd |= SDCMD_LONG_RESPONSE;
if (cmd->flags & MMC_RSP_BUSY) {
sdcmd |= SDCMD_BUSYWAIT;
host->use_busy = true;
//host->use_busy = true;
}
}

Expand Down
28 changes: 11 additions & 17 deletions src/device/sdcard.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ static uint32_t addr = 0;
static bool write_cmd = 0;
static bool read_ext_csd = false;

static inline void prepare_rw(int is_write) {
blk_addr = base[SDARG];
addr = 0;
if (fp) fseek(fp, blk_addr << 9, SEEK_SET);
write_cmd = is_write;
}

static void sdcard_io_handler(uint32_t offset, int len, bool is_write) {
int idx = offset / 4;
switch (idx) {
Expand Down Expand Up @@ -63,21 +70,8 @@ static void sdcard_io_handler(uint32_t offset, int len, bool is_write) {
case MMC_SET_RELATIVE_ADDR: break;
case MMC_SELECT_CARD: break;
case MMC_SET_BLOCK_COUNT: blkcnt = base[SDARG] & 0xffff; break;
case MMC_READ_MULTIPLE_BLOCK:
// TODO
blk_addr = base[SDARG];
addr = 0;
if (fp) fseek(fp, blk_addr << 9, SEEK_SET);
//Log("reading from addr = 0x%lx", blk_addr << 9);
write_cmd = false;
break;
case MMC_WRITE_MULTIPLE_BLOCK:
// TODO
addr = base[SDARG];
if (fp) fseek(fp, addr, SEEK_SET);
//Log("writing to addr = 0x%x", base[SDARG]);
write_cmd = true;
break;
case MMC_READ_MULTIPLE_BLOCK: prepare_rw(false); break;
case MMC_WRITE_MULTIPLE_BLOCK: prepare_rw(true); break;
case MMC_SEND_STATUS: base[SDRSP0] = base[SDRSP1] = base[SDRSP2] = base[SDRSP3] = 0; break;
case MMC_STOP_TRANSMISSION: break;
default:
Expand All @@ -96,7 +90,6 @@ static void sdcard_io_handler(uint32_t offset, int len, bool is_write) {
break;
case SDHBLC: break; //Log("@@@@@@@@@@ block count = %d", base[SDHBLC]); break;// only for debug
case SDDATA:
// TODO
if (read_ext_csd) {
// See section 8.1 JEDEC Standard JED84-A441
uint32_t data;
Expand All @@ -110,6 +103,7 @@ static void sdcard_io_handler(uint32_t offset, int len, bool is_write) {
} else if (fp) {
__attribute__((unused)) int ret;
if (!write_cmd) { ret = fread(&base[SDDATA], 4, 1, fp); }
else { ret = fwrite(&base[SDDATA], 4, 1, fp); }
}
addr += 4;
break;
Expand All @@ -129,6 +123,6 @@ void init_sdcard(const char *img) {

Assert(C_SIZE < (1 << 12), "shoule be fit in 12 bits");

fp = fopen(img, "r");
fp = fopen(img, "r+");
if (fp == NULL) Log("Can not find sdcard image: %s", img);
}

0 comments on commit 3bcf376

Please sign in to comment.