Skip to content

Commit

Permalink
Add FATFS Linux example
Browse files Browse the repository at this point in the history
This allows a file on the Linux filesystem to be used as a FATFS file.

It also fixes a bug in `port.h` when opening file on a FATFS filesystem.
  • Loading branch information
LinuxJedi committed Feb 27, 2025
1 parent 0fd8b20 commit 08759f1
Show file tree
Hide file tree
Showing 13 changed files with 24,134 additions and 2 deletions.
14 changes: 14 additions & 0 deletions examples/sftpclient/sftpclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -1476,6 +1476,9 @@ THREAD_RETURN WOLFSSH_THREAD sftpclient_test(void* args)

int main(int argc, char** argv)
{
#ifdef WOLFSSH_FATFS
FATFS fs;
#endif
func_args args;

args.argc = argc;
Expand All @@ -1486,6 +1489,13 @@ THREAD_RETURN WOLFSSH_THREAD sftpclient_test(void* args)
args.sftp_cb = NULL;
#endif

#ifdef WOLFSSH_FATFS
if (f_mount(&fs, "0:", 1) != FR_OK) {
fprintf(stderr, "Failed to mount filesystem\n");
return 1;
}
#endif

WSTARTTCP();

#ifdef DEBUG_WOLFSSH
Expand All @@ -1499,6 +1509,10 @@ THREAD_RETURN WOLFSSH_THREAD sftpclient_test(void* args)

wolfSSH_Cleanup();

#ifdef WOLFSSH_FATFS
f_mount(NULL, "0:", 1);
#endif

#if defined(WOLFSSH_SFTP) && !defined(NO_WOLFSSH_CLIENT)
return args.return_code;
#else
Expand Down
1 change: 1 addition & 0 deletions ide/Linux-FATFS/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fatfs_image.img
26 changes: 26 additions & 0 deletions ide/Linux-FATFS/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Compiler and flags
CC = gcc
CFLAGS = -g -Wall -O2 -fPIC
LDFLAGS = -shared

# Source files
SRCS = ff.c ffunicode.c fatfs_example.c

# Object files
OBJS = $(SRCS:.c=.o)

# Target library
TARGET = libfatfs.so

all: $(TARGET)

$(TARGET): $(OBJS)
$(CC) $(LDFLAGS) -o $@ $^

%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@

clean:
rm -f $(OBJS) $(TARGET)

.PHONY: all clean
36 changes: 36 additions & 0 deletions ide/Linux-FATFS/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# FATFS Linux Example

This is a FATFS example that uses a single file on the Linux filesystem as the
FATFS file system.

## Compiling Library

To compile the FATFS library simply run `make`.

## Setup filesystem

The single file used for FATFS should be generated using:

```sh
dd if=/dev/zero of=fatfs_image.img bs=1M count=32
mkdosfs fatfs_image.img
```

Note that this file will need to be local to wherever you execute anything using
the library.

## Compiling wolfSSH and wolfSSL

### wolfSSL

```sh
./configure --enable-wolfssh --enable-intelasm --disable-crl --disable-examples --disable-filesystem CFLAGS="-DNO_WOLFSSL_DIR"
```

### wolfSSH

```sh
export LD_LIBRARY_PATH=ide/Linux-FATFS
./configure --enable-sftp CFLAGS="-DWOLFSSH_FATFS -Iide/Linux-FATFS -DSTDIN_FILENO=0 -DPRINTF=printf -lfatfs"
```

229 changes: 229 additions & 0 deletions ide/Linux-FATFS/diskio.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
/*-----------------------------------------------------------------------*/
/* 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 */

/* Definitions of physical drive number for each drive */
#define DEV_RAM 0 /* Example: Map Ramdisk to physical drive 0 */
#define DEV_MMC 1 /* Example: Map MMC/SD card to physical drive 1 */
#define DEV_USB 2 /* Example: Map USB MSD to physical drive 2 */


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

DSTATUS disk_status (
BYTE pdrv /* Physical drive nmuber to identify the drive */
)
{
DSTATUS stat;
int result;

switch (pdrv) {
case DEV_RAM :
result = RAM_disk_status();

// translate the reslut code here

return stat;

case DEV_MMC :
result = MMC_disk_status();

// translate the reslut code here

return stat;

case DEV_USB :
result = USB_disk_status();

// translate the reslut code here

return stat;
}
return STA_NOINIT;
}



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

DSTATUS disk_initialize (
BYTE pdrv /* Physical drive nmuber to identify the drive */
)
{
DSTATUS stat;
int result;

switch (pdrv) {
case DEV_RAM :
result = RAM_disk_initialize();

// translate the reslut code here

return stat;

case DEV_MMC :
result = MMC_disk_initialize();

// translate the reslut code here

return stat;

case DEV_USB :
result = USB_disk_initialize();

// translate the reslut code here

return stat;
}
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 */
)
{
DRESULT res;
int result;

switch (pdrv) {
case DEV_RAM :
// translate the arguments here

result = RAM_disk_read(buff, sector, count);

// translate the reslut code here

return res;

case DEV_MMC :
// translate the arguments here

result = MMC_disk_read(buff, sector, count);

// translate the reslut code here

return res;

case DEV_USB :
// translate the arguments here

result = USB_disk_read(buff, sector, count);

// translate the reslut code here

return res;
}

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 */
)
{
DRESULT res;
int result;

switch (pdrv) {
case DEV_RAM :
// translate the arguments here

result = RAM_disk_write(buff, sector, count);

// translate the reslut code here

return res;

case DEV_MMC :
// translate the arguments here

result = MMC_disk_write(buff, sector, count);

// translate the reslut code here

return res;

case DEV_USB :
// translate the arguments here

result = USB_disk_write(buff, sector, count);

// translate the reslut code here

return res;
}

return RES_PARERR;
}

#endif


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

DRESULT disk_ioctl (
BYTE pdrv, /* Physical drive nmuber (0..) */
BYTE cmd, /* Control code */
void *buff /* Buffer to send/receive control data */
)
{
DRESULT res;
int result;

switch (pdrv) {
case DEV_RAM :

// Process of the command for the RAM drive

return res;

case DEV_MMC :

// Process of the command for the MMC/SD card

return res;

case DEV_USB :

// Process of the command the USB drive

return res;
}

return RES_PARERR;
}

Loading

0 comments on commit 08759f1

Please sign in to comment.