Skip to content

Commit 08759f1

Browse files
committed
Add FATFS Linux example
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.
1 parent 0fd8b20 commit 08759f1

File tree

13 files changed

+24134
-2
lines changed

13 files changed

+24134
-2
lines changed

examples/sftpclient/sftpclient.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,6 +1476,9 @@ THREAD_RETURN WOLFSSH_THREAD sftpclient_test(void* args)
14761476

14771477
int main(int argc, char** argv)
14781478
{
1479+
#ifdef WOLFSSH_FATFS
1480+
FATFS fs;
1481+
#endif
14791482
func_args args;
14801483

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

1492+
#ifdef WOLFSSH_FATFS
1493+
if (f_mount(&fs, "0:", 1) != FR_OK) {
1494+
fprintf(stderr, "Failed to mount filesystem\n");
1495+
return 1;
1496+
}
1497+
#endif
1498+
14891499
WSTARTTCP();
14901500

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

15001510
wolfSSH_Cleanup();
15011511

1512+
#ifdef WOLFSSH_FATFS
1513+
f_mount(NULL, "0:", 1);
1514+
#endif
1515+
15021516
#if defined(WOLFSSH_SFTP) && !defined(NO_WOLFSSH_CLIENT)
15031517
return args.return_code;
15041518
#else

ide/Linux-FATFS/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fatfs_image.img

ide/Linux-FATFS/Makefile

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Compiler and flags
2+
CC = gcc
3+
CFLAGS = -g -Wall -O2 -fPIC
4+
LDFLAGS = -shared
5+
6+
# Source files
7+
SRCS = ff.c ffunicode.c fatfs_example.c
8+
9+
# Object files
10+
OBJS = $(SRCS:.c=.o)
11+
12+
# Target library
13+
TARGET = libfatfs.so
14+
15+
all: $(TARGET)
16+
17+
$(TARGET): $(OBJS)
18+
$(CC) $(LDFLAGS) -o $@ $^
19+
20+
%.o: %.c
21+
$(CC) $(CFLAGS) -c $< -o $@
22+
23+
clean:
24+
rm -f $(OBJS) $(TARGET)
25+
26+
.PHONY: all clean

ide/Linux-FATFS/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# FATFS Linux Example
2+
3+
This is a FATFS example that uses a single file on the Linux filesystem as the
4+
FATFS file system.
5+
6+
## Compiling Library
7+
8+
To compile the FATFS library simply run `make`.
9+
10+
## Setup filesystem
11+
12+
The single file used for FATFS should be generated using:
13+
14+
```sh
15+
dd if=/dev/zero of=fatfs_image.img bs=1M count=32
16+
mkdosfs fatfs_image.img
17+
```
18+
19+
Note that this file will need to be local to wherever you execute anything using
20+
the library.
21+
22+
## Compiling wolfSSH and wolfSSL
23+
24+
### wolfSSL
25+
26+
```sh
27+
./configure --enable-wolfssh --enable-intelasm --disable-crl --disable-examples --disable-filesystem CFLAGS="-DNO_WOLFSSL_DIR"
28+
```
29+
30+
### wolfSSH
31+
32+
```sh
33+
export LD_LIBRARY_PATH=ide/Linux-FATFS
34+
./configure --enable-sftp CFLAGS="-DWOLFSSH_FATFS -Iide/Linux-FATFS -DSTDIN_FILENO=0 -DPRINTF=printf -lfatfs"
35+
```
36+

ide/Linux-FATFS/diskio.c

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
/*-----------------------------------------------------------------------*/
2+
/* Low level disk I/O module SKELETON for FatFs (C)ChaN, 2019 */
3+
/*-----------------------------------------------------------------------*/
4+
/* If a working storage control module is available, it should be */
5+
/* attached to the FatFs via a glue function rather than modifying it. */
6+
/* This is an example of glue functions to attach various exsisting */
7+
/* storage control modules to the FatFs module with a defined API. */
8+
/*-----------------------------------------------------------------------*/
9+
10+
#include "ff.h" /* Obtains integer types */
11+
#include "diskio.h" /* Declarations of disk functions */
12+
13+
/* Definitions of physical drive number for each drive */
14+
#define DEV_RAM 0 /* Example: Map Ramdisk to physical drive 0 */
15+
#define DEV_MMC 1 /* Example: Map MMC/SD card to physical drive 1 */
16+
#define DEV_USB 2 /* Example: Map USB MSD to physical drive 2 */
17+
18+
19+
/*-----------------------------------------------------------------------*/
20+
/* Get Drive Status */
21+
/*-----------------------------------------------------------------------*/
22+
23+
DSTATUS disk_status (
24+
BYTE pdrv /* Physical drive nmuber to identify the drive */
25+
)
26+
{
27+
DSTATUS stat;
28+
int result;
29+
30+
switch (pdrv) {
31+
case DEV_RAM :
32+
result = RAM_disk_status();
33+
34+
// translate the reslut code here
35+
36+
return stat;
37+
38+
case DEV_MMC :
39+
result = MMC_disk_status();
40+
41+
// translate the reslut code here
42+
43+
return stat;
44+
45+
case DEV_USB :
46+
result = USB_disk_status();
47+
48+
// translate the reslut code here
49+
50+
return stat;
51+
}
52+
return STA_NOINIT;
53+
}
54+
55+
56+
57+
/*-----------------------------------------------------------------------*/
58+
/* Inidialize a Drive */
59+
/*-----------------------------------------------------------------------*/
60+
61+
DSTATUS disk_initialize (
62+
BYTE pdrv /* Physical drive nmuber to identify the drive */
63+
)
64+
{
65+
DSTATUS stat;
66+
int result;
67+
68+
switch (pdrv) {
69+
case DEV_RAM :
70+
result = RAM_disk_initialize();
71+
72+
// translate the reslut code here
73+
74+
return stat;
75+
76+
case DEV_MMC :
77+
result = MMC_disk_initialize();
78+
79+
// translate the reslut code here
80+
81+
return stat;
82+
83+
case DEV_USB :
84+
result = USB_disk_initialize();
85+
86+
// translate the reslut code here
87+
88+
return stat;
89+
}
90+
return STA_NOINIT;
91+
}
92+
93+
94+
95+
/*-----------------------------------------------------------------------*/
96+
/* Read Sector(s) */
97+
/*-----------------------------------------------------------------------*/
98+
99+
DRESULT disk_read (
100+
BYTE pdrv, /* Physical drive nmuber to identify the drive */
101+
BYTE *buff, /* Data buffer to store read data */
102+
LBA_t sector, /* Start sector in LBA */
103+
UINT count /* Number of sectors to read */
104+
)
105+
{
106+
DRESULT res;
107+
int result;
108+
109+
switch (pdrv) {
110+
case DEV_RAM :
111+
// translate the arguments here
112+
113+
result = RAM_disk_read(buff, sector, count);
114+
115+
// translate the reslut code here
116+
117+
return res;
118+
119+
case DEV_MMC :
120+
// translate the arguments here
121+
122+
result = MMC_disk_read(buff, sector, count);
123+
124+
// translate the reslut code here
125+
126+
return res;
127+
128+
case DEV_USB :
129+
// translate the arguments here
130+
131+
result = USB_disk_read(buff, sector, count);
132+
133+
// translate the reslut code here
134+
135+
return res;
136+
}
137+
138+
return RES_PARERR;
139+
}
140+
141+
142+
143+
/*-----------------------------------------------------------------------*/
144+
/* Write Sector(s) */
145+
/*-----------------------------------------------------------------------*/
146+
147+
#if FF_FS_READONLY == 0
148+
149+
DRESULT disk_write (
150+
BYTE pdrv, /* Physical drive nmuber to identify the drive */
151+
const BYTE *buff, /* Data to be written */
152+
LBA_t sector, /* Start sector in LBA */
153+
UINT count /* Number of sectors to write */
154+
)
155+
{
156+
DRESULT res;
157+
int result;
158+
159+
switch (pdrv) {
160+
case DEV_RAM :
161+
// translate the arguments here
162+
163+
result = RAM_disk_write(buff, sector, count);
164+
165+
// translate the reslut code here
166+
167+
return res;
168+
169+
case DEV_MMC :
170+
// translate the arguments here
171+
172+
result = MMC_disk_write(buff, sector, count);
173+
174+
// translate the reslut code here
175+
176+
return res;
177+
178+
case DEV_USB :
179+
// translate the arguments here
180+
181+
result = USB_disk_write(buff, sector, count);
182+
183+
// translate the reslut code here
184+
185+
return res;
186+
}
187+
188+
return RES_PARERR;
189+
}
190+
191+
#endif
192+
193+
194+
/*-----------------------------------------------------------------------*/
195+
/* Miscellaneous Functions */
196+
/*-----------------------------------------------------------------------*/
197+
198+
DRESULT disk_ioctl (
199+
BYTE pdrv, /* Physical drive nmuber (0..) */
200+
BYTE cmd, /* Control code */
201+
void *buff /* Buffer to send/receive control data */
202+
)
203+
{
204+
DRESULT res;
205+
int result;
206+
207+
switch (pdrv) {
208+
case DEV_RAM :
209+
210+
// Process of the command for the RAM drive
211+
212+
return res;
213+
214+
case DEV_MMC :
215+
216+
// Process of the command for the MMC/SD card
217+
218+
return res;
219+
220+
case DEV_USB :
221+
222+
// Process of the command the USB drive
223+
224+
return res;
225+
}
226+
227+
return RES_PARERR;
228+
}
229+

0 commit comments

Comments
 (0)