Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor FATFS/fileio example fixes #130

Merged
merged 3 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions components/fs/fat/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,20 @@ blk_queue_handle_t *blk_queue_handle = &blk_queue_handle_memory;
fs_queue_t *fs_command_queue;
fs_queue_t *fs_completion_queue;

blk_req_queue_t *blk_request;
blk_resp_queue_t *blk_response;
blk_req_queue_t *blk_req_queue;
blk_resp_queue_t *blk_resp_queue;

// Config pointed to the SDDF_blk config
blk_storage_info_t *blk_config;
blk_storage_info_t *blk_storage_info;

uint64_t worker_thread_stack_one;
uint64_t worker_thread_stack_two;
uint64_t worker_thread_stack_three;
uint64_t worker_thread_stack_four;

char *client_data_addr;
char *fs_share;

char *blk_data_region;
char *blk_data;

// Flag for determine if there are blk_requests pushed by the file system
// It is used to determine whether to notify the blk device driver
Expand Down Expand Up @@ -124,7 +124,7 @@ _Static_assert(BLK_QUEUE_CAPACITY_CLI_FAT >= FAT_WORKER_THREAD_NUM,
void init(void) {
// Init the block device queue
// Have to make sure who initialize this SDDF queue
blk_queue_init(blk_queue_handle, blk_request, blk_response, BLK_QUEUE_CAPACITY_CLI_FAT);
blk_queue_init(blk_queue_handle, blk_req_queue, blk_resp_queue, BLK_QUEUE_CAPACITY_CLI_FAT);
/*
This part of the code is for setting up the thread pool by
assign stacks and size of the stack to the pool
Expand Down
48 changes: 24 additions & 24 deletions components/fs/fat/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ extern bool blk_request_pushed;
// This is the offset of the data buffer shared between file system and blk device driver
extern uint64_t fs_metadata;

extern blk_storage_info_t *blk_config;
extern blk_storage_info_t *blk_storage_info;

extern char *blk_data_region;
extern char *blk_data;

/*
* This def restrict the maximum cluster size that the fatfs can have
Expand Down Expand Up @@ -54,32 +54,32 @@ DSTATUS disk_initialize (
}

// Check whether the block device is ready or not
if (!blk_config->ready) {
if (!blk_storage_is_ready(blk_storage_info)) {
return RES_NOTRDY;
}

// The sector size should be a mutiple of 512, BLK_TRANSFER_SIZE % SECTOR_SIZE should be 0
// BLK_TRANSFER_SIZE % SECTOR_SIZE should be a power of 2
assert(blk_config->sector_size % 512 == 0 && "Sector size must be a multiple of 512");
assert(blk_config->sector_size <= BLK_TRANSFER_SIZE && "BLK_TRANSFER_SIZE must be the same or larger than sector size");
assert(IS_POWER_OF_2(BLK_TRANSFER_SIZE / blk_config->sector_size) && "BLK_TRANSFER_SIZE / SECTOR_SIZE must be a power of 2");
assert(blk_storage_info->sector_size % 512 == 0 && "Sector size must be a multiple of 512");
assert(blk_storage_info->sector_size <= BLK_TRANSFER_SIZE && "BLK_TRANSFER_SIZE must be the same or larger than sector size");
assert(IS_POWER_OF_2(BLK_TRANSFER_SIZE / blk_storage_info->sector_size) && "BLK_TRANSFER_SIZE / SECTOR_SIZE must be a power of 2");

LOG_FATFS("Block Storage Information:\n");
LOG_FATFS("--------------------------\n");
LOG_FATFS("Serial Number: %s\n", blk_config->serial_number);
LOG_FATFS("Read-Only: %s\n", blk_config->read_only ? "Yes" : "No");
LOG_FATFS("Ready: %s\n", blk_config->ready ? "Yes" : "No");
LOG_FATFS("Sector Size: %u bytes\n", blk_config->sector_size);
LOG_FATFS("Serial Number: %s\n", blk_storage_info->serial_number);
LOG_FATFS("Read-Only: %s\n", blk_storage_info->read_only ? "Yes" : "No");
LOG_FATFS("Ready: %s\n", blk_storage_info->ready ? "Yes" : "No");
LOG_FATFS("Sector Size: %u bytes\n", blk_storage_info->sector_size);
LOG_FATFS("Optimal Block Size: %u units (%u bytes)\n",
blk_config->block_size, blk_config->block_size * BLK_TRANSFER_SIZE);
LOG_FATFS("Queue Depth: %u\n", blk_config->queue_depth);
blk_storage_info->block_size, blk_storage_info->block_size * BLK_TRANSFER_SIZE);
LOG_FATFS("Queue Depth: %u\n", blk_storage_info->queue_depth);
LOG_FATFS("Geometry:\n");
LOG_FATFS(" Cylinders: %u\n", blk_config->cylinders);
LOG_FATFS(" Heads: %u\n", blk_config->heads);
LOG_FATFS(" Blocks: %u\n", blk_config->blocks);
LOG_FATFS(" Cylinders: %u\n", blk_storage_info->cylinders);
LOG_FATFS(" Heads: %u\n", blk_storage_info->heads);
LOG_FATFS(" Blocks: %u\n", blk_storage_info->blocks);
LOG_FATFS("Total Capacity: %llu units (%llu bytes)\n",
(unsigned long long)blk_config->capacity,
(unsigned long long)(blk_config->capacity * BLK_TRANSFER_SIZE));
(unsigned long long)blk_storage_info->capacity,
(unsigned long long)(blk_storage_info->capacity * BLK_TRANSFER_SIZE));
LOG_FATFS("--------------------------\n");
return RES_OK;
}
Expand All @@ -95,7 +95,7 @@ DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff) {
DRESULT res;
if (cmd == GET_SECTOR_SIZE) {
WORD *size = buff;
*size = blk_config->sector_size;
*size = blk_storage_info->sector_size;
res = RES_OK;
}
if (cmd == CTRL_SYNC) {
Expand All @@ -120,7 +120,7 @@ DRESULT disk_read(BYTE pdrv, BYTE *buff, LBA_t sector, UINT count) {
// Accroding the protocol, all the read/write addr passed to the blk_virt should be page aligned
// Substract the handle with one as the worker thread ID starts at 1, not 0
uint64_t read_data_offset = thread_blk_addr[handle - 1];
uint16_t sector_size = blk_config->sector_size;
uint16_t sector_size = blk_storage_info->sector_size;
// This is the same as BLK_TRANSFER_SIZE / sector_size
uint16_t sector_per_transfer = DIV_POWER_OF_2(BLK_TRANSFER_SIZE, sector_size);
uint32_t sddf_sector = DIV_POWER_OF_2(sector, sector_per_transfer);
Expand Down Expand Up @@ -150,7 +150,7 @@ DRESULT disk_read(BYTE pdrv, BYTE *buff, LBA_t sector, UINT count) {
wait_for_blk_resp();

res = (DRESULT)(uintptr_t)microkit_cothread_my_arg();
memcpy(buff, blk_data_region + read_data_offset + sector_size * MOD_POWER_OF_2(sector, sector_per_transfer), sector_size * count);
memcpy(buff, blk_data + read_data_offset + sector_size * MOD_POWER_OF_2(sector, sector_per_transfer), sector_size * count);
return res;
}

Expand All @@ -159,11 +159,11 @@ DRESULT disk_write(BYTE pdrv, const BYTE *buff, LBA_t sector, UINT count) {
int handle = microkit_cothread_my_handle();
// Substract the handle with one as the worker thread ID starts at 1, not 0
uint64_t write_data_offset = thread_blk_addr[handle - 1];
uint16_t sector_size = blk_config->sector_size;
uint16_t sector_size = blk_storage_info->sector_size;
if (sector_size == BLK_TRANSFER_SIZE) {
assert(MUL_POWER_OF_2(count, BLK_TRANSFER_SIZE) <= MAX_CLUSTER_SIZE);

memcpy(blk_data_region + write_data_offset, buff, sector_size * count);
memcpy(blk_data + write_data_offset, buff, sector_size * count);
int err = blk_enqueue_req(blk_queue_handle, BLK_REQ_WRITE, write_data_offset, sector, count,handle);
assert(!err);
}
Expand All @@ -190,7 +190,7 @@ DRESULT disk_write(BYTE pdrv, const BYTE *buff, LBA_t sector, UINT count) {

// When there is no unaligned sector, we do not need to send a read request
if (unaligned_head_sector == 0 && unaligned_tail_sector == 0) {
memcpy(blk_data_region + write_data_offset, buff, sector_size * count);
memcpy(blk_data + write_data_offset, buff, sector_size * count);
int err = blk_enqueue_req(blk_queue_handle, BLK_REQ_WRITE, write_data_offset, sddf_sector, sddf_count,handle);
assert(!err);
}
Expand All @@ -204,7 +204,7 @@ DRESULT disk_write(BYTE pdrv, const BYTE *buff, LBA_t sector, UINT count) {
if (res != RES_OK) {
return res;
}
memcpy(blk_data_region + write_data_offset + sector_size * MOD_POWER_OF_2(sector, sector_per_transfer), buff, sector_size * count);
memcpy(blk_data + write_data_offset + sector_size * MOD_POWER_OF_2(sector, sector_per_transfer), buff, sector_size * count);
err = blk_enqueue_req(blk_queue_handle, BLK_REQ_WRITE, write_data_offset, sddf_sector, sddf_count,handle);
assert(!err);
}
Expand Down
14 changes: 7 additions & 7 deletions components/fs/fat/op.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ FIL files[FAT_MAX_OPENED_FILENUM];
descriptor_status dir_status[FAT_MAX_OPENED_DIRNUM];
DIR dirs[FAT_MAX_OPENED_DIRNUM];

// Data buffer offset
extern char *client_data_addr;
/* Data shared with client */
extern char *fs_share;

// Sanity check functions
// Checking if the memory region that provided by request is within valid memory region
Expand Down Expand Up @@ -69,7 +69,7 @@ static FRESULT validate_and_copy_path(uint64_t path, uint64_t len, char* memory)
return FR_INVALID_PARAMETER;
}
// Copy the string to our private memory
memcpy(memory, client_data_addr + path, len);
memcpy(memory, fs_share + path, len);
// Return error if the string is not NULL terminated
memory[len] = '\0';

Expand Down Expand Up @@ -227,7 +227,7 @@ void handle_file_write(void) {
args->status = FS_STATUS_INVALID_FD;
return;
}
void* data = client_data_addr + buffer;
void* data = fs_share + buffer;

FIL* file = &(files[fd]);

Expand Down Expand Up @@ -275,7 +275,7 @@ void handle_file_read(void) {
return;
}

void* data = client_data_addr + buffer;
void* data = fs_share + buffer;

// Maybe add validation check of file descriptor here
FIL* file = &(files[fd]);
Expand Down Expand Up @@ -349,7 +349,7 @@ void handle_stat(void) {
return;
}

fs_stat_t* file_stat = (fs_stat_t *)(client_data_addr + output_buffer);
fs_stat_t* file_stat = (fs_stat_t *)(fs_share + output_buffer);

LOG_FATFS("fat_stat:asking for filename: %s\n", filepath);

Expand Down Expand Up @@ -584,7 +584,7 @@ void handle_dir_read(void) {
return;
}

void* name = client_data_addr + buffer;
void* name = fs_share + buffer;

FILINFO fno;
RET = f_readdir(&dirs[fd], &fno);
Expand Down
1 change: 0 additions & 1 deletion examples/fileio/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ ${BUILD_DIR}/Makefile: fileio.mk
cp fileio.mk $@

submodules:
git submodule update --init $(LIONSOS)/dep/libvmm
git submodule update --init $(LIONSOS)/dep/libnfs
git submodule update --init $(LIONSOS)/dep/micropython
git submodule update --init $(LIONSOS)/dep/musllibc
Expand Down
14 changes: 7 additions & 7 deletions examples/fileio/board/maaxboard/fileio.system
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@
<memory_region name="blk_driver_response" size="0x200_000" page_size="0x200_000"/>
<memory_region name="blk_driver_data" size="0x200_000" page_size="0x200_000" />

<memory_region name="blk_client_config" size="0x1000" page_size="0x1000" />
<memory_region name="blk_client_storage_info" size="0x1000" page_size="0x1000" />
<memory_region name="blk_client_request" size="0x200_000" page_size="0x200_000"/>
<memory_region name="blk_client_response" size="0x200_000" page_size="0x200_000"/>
<memory_region name="blk_client_data" size="0x200_000" page_size="0x200_000" />
Expand All @@ -205,7 +205,7 @@
<map mr="blk_driver_data" vaddr="0x40600000" perms="rw" cached="true" setvar_vaddr="blk_driver_data" />
<setvar symbol="blk_data_paddr_driver" region_paddr="blk_driver_data" />

<map mr="blk_client_config" vaddr="0x30000000" perms="rw" cached="false" setvar_vaddr="blk_client_storage_info" />
<map mr="blk_client_storage_info" vaddr="0x30000000" perms="rw" cached="false" setvar_vaddr="blk_client_storage_info" />
<map mr="blk_client_request" vaddr="0x30200000" perms="rw" cached="false" setvar_vaddr="blk_client_req_queue" />
<map mr="blk_client_response" vaddr="0x30400000" perms="rw" cached="false" setvar_vaddr="blk_client_resp_queue" />
<map mr="blk_client_data" vaddr="0x30600000" perms="rw" cached="true" setvar_vaddr="blk_client_data" />
Expand All @@ -218,13 +218,13 @@
<map mr="fs_command_queue" vaddr="0x7_800_000" perms="rw" cached="true" setvar_vaddr="fs_command_queue" />
<map mr="fs_completion_queue" vaddr="0x7_810_000" perms="rw" cached="true" setvar_vaddr="fs_completion_queue" />

<map mr="blk_client_config" vaddr="0x40_000_000" perms="r" cached="false" setvar_vaddr="blk_config" />
<map mr="blk_client_storage_info" vaddr="0x40_000_000" perms="r" cached="false" setvar_vaddr="blk_storage_info" />

<map mr="blk_client_request" vaddr="0x40_200_000" perms="rw" cached="false" setvar_vaddr="blk_request" />
<map mr="blk_client_response" vaddr="0x40_400_000" perms="rw" cached="false" setvar_vaddr="blk_response" />
<map mr="blk_client_data" vaddr="0x40_800_000" perms="rw" cached="true" setvar_vaddr="blk_data_region" />
<map mr="blk_client_request" vaddr="0x40_200_000" perms="rw" cached="false" setvar_vaddr="blk_req_queue" />
<map mr="blk_client_response" vaddr="0x40_400_000" perms="rw" cached="false" setvar_vaddr="blk_resp_queue" />
<map mr="blk_client_data" vaddr="0x40_800_000" perms="rw" cached="true" setvar_vaddr="blk_data" />

<map mr="shared_fs_micropython" vaddr="0x43_000_000" perms="rw" cached="true" setvar_vaddr="client_data_addr"/>
<map mr="shared_fs_micropython" vaddr="0x43_000_000" perms="rw" cached="true" setvar_vaddr="fs_share"/>

<map mr="fat_thread_stack1" vaddr="0xA0_000_000" perms="rw" cached="true" setvar_vaddr="worker_thread_stack_one" />
<map mr="fat_thread_stack2" vaddr="0xB0_000_000" perms="rw" cached="true" setvar_vaddr="worker_thread_stack_two" />
Expand Down
16 changes: 8 additions & 8 deletions examples/fileio/board/qemu_virt_aarch64/fileio.system
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@
<memory_region name="blk_driver_response" size="0x200_000" page_size="0x200_000"/>
<memory_region name="blk_driver_data" size="0x200_000" page_size="0x200_000" />

<memory_region name="blk_client_config" size="0x1000" page_size="0x1000" />
<memory_region name="blk_client_storage_info" size="0x1000" page_size="0x1000" />
<memory_region name="blk_client_request" size="0x200_000" page_size="0x200_000"/>
<memory_region name="blk_client_response" size="0x200_000" page_size="0x200_000"/>
<memory_region name="blk_client_data" size="0x200_000" page_size="0x200_000" />
Expand All @@ -201,13 +201,13 @@
<protection_domain name="BLK_VIRT" priority="101">
<program_image path="blk_virt.elf" />

<map mr="blk_driver_storage_info" vaddr="0x40000000" perms="rw" cached="false" setvar_vaddr="blk_driver_storage_info" />
<map mr="blk_driver_storage_info" vaddr="0x40000000" perms="rw" cached="false" setvar_vaddr="blk_driver_storage_info" />
<map mr="blk_driver_request" vaddr="0x40200000" perms="rw" cached="false" setvar_vaddr="blk_driver_req_queue" />
<map mr="blk_driver_response" vaddr="0x40400000" perms="rw" cached="false" setvar_vaddr="blk_driver_resp_queue" />
<map mr="blk_driver_data" vaddr="0x40600000" perms="rw" cached="true" setvar_vaddr="blk_driver_data" />
<setvar symbol="blk_data_paddr_driver" region_paddr="blk_driver_data" />

<map mr="blk_client_config" vaddr="0x30000000" perms="rw" cached="false" setvar_vaddr="blk_client_storage_info" />
<map mr="blk_client_storage_info" vaddr="0x30000000" perms="rw" cached="false" setvar_vaddr="blk_client_storage_info" />
<map mr="blk_client_request" vaddr="0x30200000" perms="rw" cached="false" setvar_vaddr="blk_client_req_queue" />
<map mr="blk_client_response" vaddr="0x30400000" perms="rw" cached="false" setvar_vaddr="blk_client_resp_queue" />
<map mr="blk_client_data" vaddr="0x30600000" perms="rw" cached="true" setvar_vaddr="blk_client_data" />
Expand All @@ -220,13 +220,13 @@
<map mr="fs_command_queue" vaddr="0x7_800_000" perms="rw" cached="true" setvar_vaddr="fs_command_queue" />
<map mr="fs_completion_queue" vaddr="0x7_810_000" perms="rw" cached="true" setvar_vaddr="fs_completion_queue" />

<map mr="blk_client_config" vaddr="0x40_000_000" perms="r" cached="false" setvar_vaddr="blk_config" />
<map mr="blk_client_storage_info" vaddr="0x40_000_000" perms="r" cached="false" setvar_vaddr="blk_storage_info" />

<map mr="blk_client_request" vaddr="0x40_200_000" perms="rw" cached="false" setvar_vaddr="blk_request" />
<map mr="blk_client_response" vaddr="0x40_400_000" perms="rw" cached="false" setvar_vaddr="blk_response" />
<map mr="blk_client_data" vaddr="0x40_800_000" perms="rw" cached="true" setvar_vaddr="blk_data_region" />
<map mr="blk_client_request" vaddr="0x40_200_000" perms="rw" cached="false" setvar_vaddr="blk_req_queue" />
<map mr="blk_client_response" vaddr="0x40_400_000" perms="rw" cached="false" setvar_vaddr="blk_resp_queue" />
<map mr="blk_client_data" vaddr="0x40_800_000" perms="rw" cached="true" setvar_vaddr="blk_data" />

<map mr="shared_fs_micropython" vaddr="0x43_000_000" perms="rw" cached="true" setvar_vaddr="client_data_addr"/>
<map mr="shared_fs_micropython" vaddr="0x43_000_000" perms="rw" cached="true" setvar_vaddr="fs_share"/>

<map mr="fat_thread_stack1" vaddr="0xA0_000_000" perms="rw" cached="true" setvar_vaddr="worker_thread_stack_one" />
<map mr="fat_thread_stack2" vaddr="0xB0_000_000" perms="rw" cached="true" setvar_vaddr="worker_thread_stack_two" />
Expand Down
8 changes: 0 additions & 8 deletions examples/fileio/fileio.mk
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,6 @@ DTC := dtc
BOARD_DIR := $(MICROKIT_SDK)/board/$(MICROKIT_BOARD)/$(MICROKIT_CONFIG)
PLATFORM := meson
SDDF := $(LIONSOS)/dep/sddf
LIBVMM_DIR := $(LIONSOS)/dep/libvmm

VMM_IMAGE_DIR := ${FILEIO_DIR}/src/vmm/images
LINUX := 90c4247bcd24cbca1a3db4b7489a835ce87a486e-linux
INITRD := 08c10529dc2806559d5c4b7175686a8206e10494-rootfs.cpio.gz
DTS := $(VMM_IMAGE_DIR)/linux.dts
DTB := linux.dtb

LWIP := $(SDDF)/network/ipstacks/lwip/src
FAT := $(LIONSOS)/components/fs/fat
Expand Down Expand Up @@ -98,7 +91,6 @@ BLK_DRIVER := $(SDDF)/drivers/blk/${BLK_DRIV_DIR}
BLK_COMPONENTS := $(SDDF)/blk/components

include ${SDDF}/util/util.mk
include ${LIBVMM_DIR}/vmm.mk
include ${SDDF}/drivers/timer/${TIMER_DRIV_DIR}/timer_driver.mk
include ${SDDF}/drivers/network/${NET_DRIV_DIR}/eth_driver.mk
include ${SDDF}/drivers/serial/${UART_DRIV_DIR}/uart_driver.mk
Expand Down
Loading