Skip to content

Commit

Permalink
Add FatFS support to all bdev commands
Browse files Browse the repository at this point in the history
  • Loading branch information
will-v-pi committed Dec 18, 2024
1 parent 005a0c2 commit 693f3b3
Showing 1 changed file with 120 additions and 41 deletions.
161 changes: 120 additions & 41 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,8 @@ struct config_command : public cmd {
auto bdev_options = (
(option('p', "--partition") % "Partition to use as block device" &
integer("partition").set(settings.bdev.partition) % "partition number").force_expand_help(true) +
(option("--filesystem") & bdev_fs("fs").set(settings.bdev.fs) % "Filesystem (default: littlefs)").force_expand_help(true) +
(option("--filesystem") % "Specify filesystem to use" &
bdev_fs("fs").set(settings.bdev.fs) % "littlefs|fatfs").force_expand_help(true) +
(option('f', "--format").set(settings.bdev.format) % "Format the drive if necessary (may result in data loss)")
).min(0).doc_non_optional(true) % "Block device options";

Expand Down Expand Up @@ -5432,7 +5433,7 @@ void setup_bdevfs(picoboot::connection con) {
}

// No FS Found
fail(ERROR_CONNECTION, "No file system detected - to format the drive use -f --filesystem <littlefs|fatfs>");
fail(ERROR_CONNECTION, "No file system detected - to format the drive use `-f --filesystem <littlefs|fatfs>`");
}
}

Expand Down Expand Up @@ -5747,15 +5748,36 @@ bool bdev_mkdir_command::execute(device_map &devices) {
auto con = get_single_bootsel_device_connection(devices);
setup_bdevfs(con);

lfs_op_fn lfs_op = [&](lfs_t *lfs) {
int err = lfs_mkdir(lfs, settings.filenames[0].c_str());
if (err == LFS_ERR_EXIST) {
fos << "Directory already exists\n";
} else if (err) {
fos << "LittleFS Error " << err << "\n";
switch (settings.bdev.fs) {
case fs_littlefs: {
lfs_op_fn lfs_op = [&](lfs_t *lfs) {
int err = lfs_mkdir(lfs, settings.filenames[0].c_str());
if (err == LFS_ERR_EXIST) {
fos << "Directory already exists\n";
} else if (err) {
fos << "LittleFS Error " << err << "\n";
}
};
do_lfs_op(lfs_op);
break;
}
};
do_lfs_op(lfs_op);

case fs_fatfs: {
fatfs_op_fn fatfs_op = [&](FATFS *fatfs) {
int err = f_mkdir(fatfs, settings.filenames[0].c_str());
if (err == FR_EXIST) {
fos << "Directory already exists\n";
} else if (err) {
fos << "FatFS Error " << err << "\n";
}
};
do_fatfs_op(fatfs_op);
break;
}

default:
fail(ERROR_ARGS, "Unknown filesystem specified");
}
return false;
}

Expand Down Expand Up @@ -5892,17 +5914,40 @@ bool bdev_rm_command::execute(device_map &devices) {
auto con = get_single_bootsel_device_connection(devices);
setup_bdevfs(con);

lfs_op_fn lfs_op = [&](lfs_t *lfs) {
int err = lfs_remove(lfs, settings.filenames[0].c_str());
if (err == LFS_ERR_NOTEMPTY) {
fail(ERROR_NOT_POSSIBLE, "Directory to remove is not empty");
} else if (err == LFS_ERR_NOENT) {
fail(ERROR_NOT_POSSIBLE, "File to remove does not exist");
} else if (err) {
fail(ERROR_WRITE_FAILED, "LittleFS Error %d", err);
switch (settings.bdev.fs) {
case fs_littlefs: {
lfs_op_fn lfs_op = [&](lfs_t *lfs) {
int err = lfs_remove(lfs, settings.filenames[0].c_str());
if (err == LFS_ERR_NOTEMPTY) {
fail(ERROR_NOT_POSSIBLE, "Directory to remove is not empty");
} else if (err == LFS_ERR_NOENT) {
fail(ERROR_NOT_POSSIBLE, "File to remove does not exist");
} else if (err) {
fail(ERROR_WRITE_FAILED, "LittleFS Error %d", err);
}
};
do_lfs_op(lfs_op);
break;
}
};
do_lfs_op(lfs_op);

case fs_fatfs: {
fatfs_op_fn fatfs_op = [&](FATFS *fatfs) {
int err = f_unlink(fatfs, settings.filenames[0].c_str());
if (err == FR_DENIED) {
fail(ERROR_NOT_POSSIBLE, "Directory to remove is not empty");
} else if (err == FR_NO_FILE) {
fail(ERROR_NOT_POSSIBLE, "File to remove does not exist");
} else if (err) {
fail(ERROR_WRITE_FAILED, "FatFS Error %d", err);
}
};
do_fatfs_op(fatfs_op);
break;
}

default:
fail(ERROR_ARGS, "Unknown filesystem specified");
}
return false;
}

Expand All @@ -5913,27 +5958,61 @@ bool bdev_cat_command::execute(device_map &devices) {
auto con = get_single_bootsel_device_connection(devices);
setup_bdevfs(con);

lfs_op_fn lfs_op = [&](lfs_t *lfs) {
lfs_file_t file;
int err = lfs_file_open(lfs, &file, settings.filenames[0].c_str(), LFS_O_RDONLY);
if (err) {
fail(ERROR_READ_FAILED, "LittleFS Open Error %d", err);
}
auto size = lfs_file_size(lfs, &file);
err = lfs_file_rewind(lfs, &file);
std::vector<char> data_buf(size);
err = lfs_file_read(lfs, &file, data_buf.data(), data_buf.size());
if (err < 0) {
fail(ERROR_READ_FAILED, "LittleFS Read Error %d", err);
} else if (err != data_buf.size()) {
fail(ERROR_READ_FAILED, "LittleFS Read too short - got %d bytes expected %d bytes", err, data_buf.size());
}
err = lfs_file_close(lfs, &file);

string out(data_buf.begin(), data_buf.end());
printf("%s", out.c_str());
};
do_lfs_op(lfs_op);
switch (settings.bdev.fs) {
case fs_littlefs: {
lfs_op_fn lfs_op = [&](lfs_t *lfs) {
lfs_file_t file;
int err = lfs_file_open(lfs, &file, settings.filenames[0].c_str(), LFS_O_RDONLY);
if (err) {
fail(ERROR_READ_FAILED, "LittleFS Open Error %d", err);
}
auto size = lfs_file_size(lfs, &file);
err = lfs_file_rewind(lfs, &file);
std::vector<char> data_buf(size);
err = lfs_file_read(lfs, &file, data_buf.data(), data_buf.size());
if (err < 0) {
fail(ERROR_READ_FAILED, "LittleFS Read Error %d", err);
} else if (err != data_buf.size()) {
fail(ERROR_READ_FAILED, "LittleFS Read too short - got %d bytes expected %d bytes", err, data_buf.size());
}
err = lfs_file_close(lfs, &file);

string out(data_buf.begin(), data_buf.end());
printf("%s", out.c_str());
};
do_lfs_op(lfs_op);
break;
}

case fs_fatfs: {
fatfs_op_fn fatfs_op = [&](FATFS *fatfs) {
FIL file;
int err = f_open(fatfs, &file, settings.filenames[0].c_str(), FA_READ);
if (err) {
fail(ERROR_READ_FAILED, "FatFS Open Error %d", err);
}
auto size = f_size(&file);
err = f_rewind(&file);
std::vector<char> data_buf(size);
UINT bytes_read;
err = f_read(&file, data_buf.data(), data_buf.size(), &bytes_read);
if (err) {
fail(ERROR_READ_FAILED, "FatFS Read Error %d", err);
} else if (bytes_read != data_buf.size()) {
fail(ERROR_READ_FAILED, "FatFS Read too short - got %d bytes expected %d bytes", bytes_read, data_buf.size());
}
err = f_close(&file);

string out(data_buf.begin(), data_buf.end());
printf("%s", out.c_str());
};
do_fatfs_op(fatfs_op);
break;
}

default:
fail(ERROR_ARGS, "Unknown filesystem specified");
}
return false;
}

Expand Down

0 comments on commit 693f3b3

Please sign in to comment.