Skip to content

Commit

Permalink
Merge pull request #787 from wolfSSL/devin/1740666408-add-fatfs-test-…
Browse files Browse the repository at this point in the history
…action

FATFS improvements, test and Linux example
  • Loading branch information
dgarske authored Mar 7, 2025
2 parents c1b983a + d8ad0c3 commit cea99e5
Show file tree
Hide file tree
Showing 10 changed files with 720 additions and 9 deletions.
178 changes: 178 additions & 0 deletions .github/workflows/test-fatfs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
name: Test FATFS Support

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
workflow_dispatch:

jobs:
get-fatfs:
runs-on: ubuntu-latest
steps:
- name: Set up cache key
id: cache-key
run: echo "CACHE_KEY=ff15a-cache" >> $GITHUB_ENV

- name: Download FF15a.zip
id: download
run: |
wget http://elm-chan.org/fsw/ff/arc/ff15a.zip -O ff15a.zip
- name: Cache the downloaded ZIP file
uses: actions/cache@v4
with:
path: ./ff15a.zip
key: ${{ env.CACHE_KEY }}
restore-keys: |
${{ env.CACHE_KEY }}
test-fatfs:
needs: get-fatfs
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Checking cache for FATFS
uses: actions/cache@v4
with:
path: ./ff15a.zip
key: ff15a-cache
fail-on-cache-miss: true

- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -y build-essential autoconf automake libtool pkg-config openssh-server dosfstools
- name: Clone wolfSSL
run: |
cd ..
git clone https://github.com/wolfSSL/wolfssl.git
cd wolfssl
./autogen.sh
- name: Configure and build wolfSSL
run: |
cd ../wolfssl
./configure --enable-wolfssh --enable-intelasm --disable-crl --disable-examples --disable-filesystem CFLAGS="-DNO_WOLFSSL_DIR"
make
sudo make install
sudo ldconfig
- name: Compile FATFS library
run: |
cd ide/Linux-FATFS
unzip ../../ff15a.zip
cp ffconf.h source/
make
- name: Configure and build wolfSSH with FATFS
run: |
./autogen.sh
export LD_LIBRARY_PATH=$(pwd)/ide/Linux-FATFS
./configure --enable-sftp CFLAGS="-DWOLFSSH_FATFS -Iide/Linux-FATFS/source -DSTDIN_FILENO=0 -DPRINTF=printf" LDFLAGS="-Lide/Linux-FATFS -lfatfs"
make
- name: Create test file
run: |
dd if=/dev/urandom of=test_file.bin bs=1M count=1
- name: Setup SSH server
run: |
sudo mkdir -p /run/sshd
sudo /usr/sbin/sshd
mkdir -p ~/.ssh
ssh-keygen -t rsa -f ~/.ssh/id_rsa -N ""
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
echo "Host localhost" > ~/.ssh/config
echo " StrictHostKeyChecking no" >> ~/.ssh/config
echo " UserKnownHostsFile=/dev/null" >> ~/.ssh/config
chmod 600 ~/.ssh/config
- name: Install expect
run: |
sudo apt-get update
sudo apt-get install -y expect
- name: Run wolfsftp client to get file
run: |
# Export the library path
export LD_LIBRARY_PATH=$(pwd)/ide/Linux-FATFS
# Get the full path to the test file
TEST_FILE_PATH=$(pwd)/test_file.bin
# Change to the sftpclient directory
cd examples/sftpclient
# Create the FATFS image file directly in the sftpclient directory
dd if=/dev/zero of=fatfs_image.img bs=1M count=32
mkdosfs fatfs_image.img
# Create a test user with a known password and add to the same group as the runner
sudo useradd -m testuser
echo "testuser:password123" | sudo chpasswd
sudo usermod -aG $(id -gn) testuser
# Make the test file accessible to testuser
chmod 644 ${TEST_FILE_PATH}
sudo chown testuser:$(id -gn) ${TEST_FILE_PATH}
# Configure SSH server to allow SFTP access
sudo sed -i 's/^#Subsystem\s\+sftp.*/Subsystem sftp \/usr\/lib\/openssh\/sftp-server/' /etc/ssh/sshd_config
sudo service ssh restart || sudo /etc/init.d/ssh restart || sudo /usr/sbin/sshd -t && sudo kill -HUP $(pgrep -f sshd)
# Create expect script to automate the wolfsftp client interaction
cat > /tmp/sftp_test.exp << EOF
#!/usr/bin/expect -f
set timeout 60
spawn ./wolfsftp -N -h localhost -p 22 -u testuser
expect "Password:"
send "password123\r"
expect "wolfSSH sftp>"
send "get ${TEST_FILE_PATH} test_file.bin\r"
expect "wolfSSH sftp>"
send "exit\r"
expect eof
EOF
chmod +x /tmp/sftp_test.exp
# Run the expect script
/tmp/sftp_test.exp
- name: Verify file in FATFS image
run: |
cd examples/sftpclient
sudo mkdir -p /mnt/fatfs
LOOPDEV=$(sudo losetup -f)
sudo losetup $LOOPDEV fatfs_image.img
sudo mount $LOOPDEV /mnt/fatfs
if [ -f /mnt/fatfs/test_file.bin ]; then
echo "File exists in FATFS image!"
ls -la /mnt/fatfs/
# Verify file contents match
if cmp -s ../../test_file.bin /mnt/fatfs/test_file.bin; then
echo "File contents match! FATFS test PASSED."
sudo umount /mnt/fatfs
sudo losetup -d $LOOPDEV
exit 0
else
echo "File contents do not match! FATFS test FAILED."
sudo umount /mnt/fatfs
sudo losetup -d $LOOPDEV
exit 1
fi
else
echo "File does not exist in FATFS image! FATFS test FAILED."
ls -la /mnt/fatfs/
sudo umount /mnt/fatfs
sudo losetup -d $LOOPDEV
exit 1
fi
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 -Isource
LDFLAGS = -shared

# Source files
SRCS = source/ff.c source/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
49 changes: 49 additions & 0 deletions ide/Linux-FATFS/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# FATFS Linux Example

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

## Obtaining FATFS

You can download the source code from
[The FATFS download site](http://elm-chan.org/fsw/ff/archives.html). Extract it
into this directory.

The example has been tested against FATFS 0.15a

## Compiling Library

First copy the config file into the correct place:

```sh
cp ffconf.h source/
```

Then 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
LD_LIBRARY_PATH=ide/Linux-FATFS ./configure --enable-sftp CFLAGS="-DWOLFSSH_FATFS -Iide/Linux-FATFS/source -DSTDIN_FILENO=0 -DPRINTF=printf" LDFLAGS="-Lide/Linux-FATFS -lfatfs"
```

Loading

0 comments on commit cea99e5

Please sign in to comment.