Skip to content

Commit

Permalink
Ported file system
Browse files Browse the repository at this point in the history
  • Loading branch information
guancio committed Oct 27, 2024
1 parent d5696ba commit 6709369
Show file tree
Hide file tree
Showing 19 changed files with 9,004 additions and 1 deletion.
2 changes: 1 addition & 1 deletion common/inc/plat/qemu_virt4.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
{ \
[0] = cap_mk_pmp(0x20005fff, MEM_RWX), \
[1] = cap_mk_memory(0x80020000, 0x88000000, MEM_RWX), \
[2] = cap_mk_memory(0x10000000, 0x10001000, MEM_RW), \
[2] = cap_mk_memory(0x10000000, 0x10002000, MEM_RW), \
[3] = cap_mk_memory(0x200b000, 0x200c000, MEM_R), \
[4] = cap_mk_time(0, 0, S3K_SLOT_CNT), \
[5] = cap_mk_time(1, 0, S3K_SLOT_CNT), \
Expand Down
12 changes: 12 additions & 0 deletions projects/tutorial-commons/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ void setup_uart()
s3k_sync_mem();
}

void setup_uart_and_virtio()
{
uint64_t uart_addr = s3k_napot_encode(UART0_BASE_ADDR, 0x2000);
// Derive a PMP capability for accessing UART
s3k_cap_derive(UART_MEM, UART_CAP, s3k_mk_pmp(uart_addr, S3K_MEM_RW));
// Load the derive PMP capability to PMP configuration
s3k_pmp_load(UART_CAP, UART_PMP);
// Synchronize PMP unit (hardware) with PMP configuration
// false => not full synchronization.
s3k_sync_mem();
}

void default_trap_handler(void) __attribute__((interrupt("machine")));
void default_trap_handler(void) {
// We enter here on illegal instructions, for example writing to
Expand Down
43 changes: 43 additions & 0 deletions projects/tutorial.09.fs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
.POSIX:

export PLATFORM ?=qemu_virt4
export ROOT :=${abspath ../..}
export BUILD :=${abspath build/${PLATFORM}}
export S3K_CONF_H :=${abspath s3k_conf.h}

include ${ROOT}/tools.mk

APPS=app0

ELFS:=${patsubst %, ${BUILD}/%.elf, kernel ${APPS}}

all: kernel ${APPS}

clean:
rm -rf ${BUILD}

common:
@${MAKE} -C ${ROOT}/common

kernel: common
@${MAKE} -C ${ROOT}/kernel

${APPS}: common
@${MAKE} -f ../build.mk PROGRAM=$@

qemu: kernel ${APPS}
@ELFS="${ELFS}" ${ROOT}/scripts/qemu.sh -drive file=fs.img,if=none,format=raw,id=x0 -device virtio-blk-device,drive=x0,bus=virtio-mmio-bus.0

qemu-gdb: kernel ${APPS}
@ELFS="${ELFS}" ${ROOT}/scripts/qemu.sh -gdb tcp::3333 -S -drive file=fs.img,if=none,format=raw,id=x0 -device virtio-blk-device,drive=x0,bus=virtio-mmio-bus.0

gdb: kernel ${APPS}
@ELFS="${ELFS}" ${ROOT}/scripts/gdb.sh

gdb-openocd: kernel ${APPS}
@ELFS="${ELFS}" ${ROOT}/scripts/gdb-openocd.sh

size: ${ELFS}
${SIZE} ${ELFS}

.PHONY: all clean size qemu qemu-gdb gdb kernel common ${APPS}
5 changes: 5 additions & 0 deletions projects/tutorial.09.fs/app0.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
MEMORY {
RAM (rwx) : ORIGIN = 0x80010000, LENGTH = 0x10000
}

__stack_size = 1024;
19 changes: 19 additions & 0 deletions projects/tutorial.09.fs/app0/buf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef BUF_H_
#define BUF_H_

// FS specific
#define BSIZE 512 // block size

struct buf {
int valid; // has data been read from disk?
int disk; // does disk "own" buf?
uint dev;
uint blockno;
/* struct sleeplock lock; */
uint refcnt;
struct buf *prev; // LRU cache list
struct buf *next;
uchar data[BSIZE];
};

#endif // BUF_H_
167 changes: 167 additions & 0 deletions projects/tutorial.09.fs/app0/diskio.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/*-----------------------------------------------------------------------*/
/* Low level disk I/O module SKELETON for FatFs (C)ChaN, 2019 */
/*-----------------------------------------------------------------------*/
/* If a working storage control module is available, it should be */
/* attached to the FatFs via a glue function rather than modifying it. */
/* This is an example of glue functions to attach various exsisting */
/* storage control modules to the FatFs module with a defined API. */
/*-----------------------------------------------------------------------*/

#include "ff.h" /* Obtains integer types */
#include "diskio.h" /* Declarations of disk functions */
#include "virtio_disk.h"
#include <string.h>

/* Definitions of physical drive number for each drive */
#define DEV_VIRTIO 0 /* Example: Map Virtiodisk to physical drive 0 */


/*-----------------------------------------------------------------------*/
/* Get Drive Status */
/*-----------------------------------------------------------------------*/

DSTATUS disk_status (
BYTE pdrv /* Physical drive nmuber to identify the drive */
)
{
switch (pdrv) {
case DEV_VIRTIO :
if (virtio_disk_status())
return 0;
}
return STA_NOINIT;
}



/*-----------------------------------------------------------------------*/
/* Inidialize a Drive */
/*-----------------------------------------------------------------------*/

DSTATUS disk_initialize (
BYTE pdrv /* Physical drive nmuber to identify the drive */
)
{
switch (pdrv) {
case DEV_VIRTIO :
virtio_disk_init();
return disk_status(pdrv);
}

return STA_NOINIT;
}



/*-----------------------------------------------------------------------*/
/* Read Sector(s) */
/*-----------------------------------------------------------------------*/

DRESULT disk_read (
BYTE pdrv, /* Physical drive nmuber to identify the drive */
BYTE *buff, /* Data buffer to store read data */
LBA_t sector, /* Start sector in LBA */
UINT count /* Number of sectors to read */
)
{
switch (pdrv) {
case DEV_VIRTIO :
// translate the arguments here
if (disk_status(pdrv) & STA_NOINIT) return RES_NOTRDY;

struct buf buffer;
do {
buffer.blockno = sector;
memset(buffer.data, 0, sizeof(buffer.data));
virtio_disk_rw(&buffer, 0);
memcpy(buff, buffer.data, 512);
buff += 512;
} while (--count);
return RES_OK;
}

return RES_PARERR;
}



/*-----------------------------------------------------------------------*/
/* Write Sector(s) */
/*-----------------------------------------------------------------------*/

#if FF_FS_READONLY == 0

DRESULT disk_write (
BYTE pdrv, /* Physical drive nmuber to identify the drive */
const BYTE *buff, /* Data to be written */
LBA_t sector, /* Start sector in LBA */
UINT count /* Number of sectors to write */
)
{
switch (pdrv) {
case DEV_VIRTIO :
// translate the arguments here
if (disk_status(pdrv) & STA_NOINIT) return RES_NOTRDY;

struct buf buffer;
do {
buffer.blockno = sector;
memcpy(buffer.data, buff, 512);
virtio_disk_rw(&buffer, 1);
buff += 512;
} while (--count);
return RES_OK;
}

return RES_PARERR;
}

#endif


/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions */
/*-----------------------------------------------------------------------*/

DRESULT disk_ioctl (
BYTE drv, /* Physical drive nmuber (0) */
BYTE ctrl, /* Control code */
void *buff /* Buffer to send/receive control data */
)
{
DRESULT res;


if (disk_status(drv) & STA_NOINIT) return RES_NOTRDY; /* Check if card is in the socket */

res = RES_ERROR;
switch (ctrl) {
case CTRL_SYNC : /* Make sure that no pending write process */
return RES_OK;
break;

case GET_SECTOR_SIZE : /* Get number of sectors on the disk (DWORD) */
*(DWORD*)buff = 512;
return RES_OK;
break;

case GET_SECTOR_COUNT : /* Get number of sectors on the disk (DWORD) */
*(LBA_t*)buff = 10*1024*1024/512;
return RES_OK;
break;

case GET_BLOCK_SIZE : /* Get erase block size in unit of sector (DWORD) */
*(DWORD*)buff = 1;
res = RES_OK;
break;

default:
res = RES_PARERR;
}

return res;
}

DWORD get_fattime (void) {
return 0;
}
77 changes: 77 additions & 0 deletions projects/tutorial.09.fs/app0/diskio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*-----------------------------------------------------------------------/
/ Low level disk interface modlue include file (C)ChaN, 2019 /
/-----------------------------------------------------------------------*/

#ifndef _DISKIO_DEFINED
#define _DISKIO_DEFINED

#ifdef __cplusplus
extern "C" {
#endif

/* Status of Disk Functions */
typedef BYTE DSTATUS;

/* Results of Disk Functions */
typedef enum {
RES_OK = 0, /* 0: Successful */
RES_ERROR, /* 1: R/W Error */
RES_WRPRT, /* 2: Write Protected */
RES_NOTRDY, /* 3: Not Ready */
RES_PARERR /* 4: Invalid Parameter */
} DRESULT;


/*---------------------------------------*/
/* Prototypes for disk control functions */


DSTATUS disk_initialize (BYTE pdrv);
DSTATUS disk_status (BYTE pdrv);
DRESULT disk_read (BYTE pdrv, BYTE* buff, LBA_t sector, UINT count);
DRESULT disk_write (BYTE pdrv, const BYTE* buff, LBA_t sector, UINT count);
DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff);


/* Disk Status Bits (DSTATUS) */

#define STA_NOINIT 0x01 /* Drive not initialized */
#define STA_NODISK 0x02 /* No medium in the drive */
#define STA_PROTECT 0x04 /* Write protected */


/* Command code for disk_ioctrl fucntion */

/* Generic command (Used by FatFs) */
#define CTRL_SYNC 0 /* Complete pending write process (needed at FF_FS_READONLY == 0) */
#define GET_SECTOR_COUNT 1 /* Get media size (needed at FF_USE_MKFS == 1) */
#define GET_SECTOR_SIZE 2 /* Get sector size (needed at FF_MAX_SS != FF_MIN_SS) */
#define GET_BLOCK_SIZE 3 /* Get erase block size (needed at FF_USE_MKFS == 1) */
#define CTRL_TRIM 4 /* Inform device that the data on the block of sectors is no longer used (needed at FF_USE_TRIM == 1) */

/* Generic command (Not used by FatFs) */
#define CTRL_POWER 5 /* Get/Set power status */
#define CTRL_LOCK 6 /* Lock/Unlock media removal */
#define CTRL_EJECT 7 /* Eject media */
#define CTRL_FORMAT 8 /* Create physical format on the media */

/* MMC/SDC specific ioctl command */
#define MMC_GET_TYPE 10 /* Get card type */
#define MMC_GET_CSD 11 /* Get CSD */
#define MMC_GET_CID 12 /* Get CID */
#define MMC_GET_OCR 13 /* Get OCR */
#define MMC_GET_SDSTAT 14 /* Get SD status */
#define ISDIO_READ 55 /* Read data form SD iSDIO register */
#define ISDIO_WRITE 56 /* Write data to SD iSDIO register */
#define ISDIO_MRITE 57 /* Masked write data to SD iSDIO register */

/* ATA/CF specific ioctl command */
#define ATA_GET_REV 20 /* Get F/W revision */
#define ATA_GET_MODEL 21 /* Get model name */
#define ATA_GET_SN 22 /* Get serial number */

#ifdef __cplusplus
}
#endif

#endif
Loading

0 comments on commit 6709369

Please sign in to comment.