@@ -709,7 +709,8 @@ struct config_command : public cmd {
709
709
auto bdev_options = (
710
710
(option('p', "--partition") % "Partition to use as block device" &
711
711
integer("partition").set(settings.bdev.partition) % "partition number").force_expand_help(true) +
712
- (option("--filesystem") & bdev_fs("fs").set(settings.bdev.fs) % "Filesystem (default: littlefs)").force_expand_help(true) +
712
+ (option("--filesystem") % "Specify filesystem to use" &
713
+ bdev_fs("fs").set(settings.bdev.fs) % "littlefs|fatfs").force_expand_help(true) +
713
714
(option('f', "--format").set(settings.bdev.format) % "Format the drive if necessary (may result in data loss)")
714
715
).min(0).doc_non_optional(true) % "Block device options";
715
716
@@ -5432,7 +5433,7 @@ void setup_bdevfs(picoboot::connection con) {
5432
5433
}
5433
5434
5434
5435
// No FS Found
5435
- fail(ERROR_CONNECTION, "No file system detected - to format the drive use -f --filesystem <littlefs|fatfs>");
5436
+ fail(ERROR_CONNECTION, "No file system detected - to format the drive use ` -f --filesystem <littlefs|fatfs>` ");
5436
5437
}
5437
5438
}
5438
5439
@@ -5747,15 +5748,36 @@ bool bdev_mkdir_command::execute(device_map &devices) {
5747
5748
auto con = get_single_bootsel_device_connection(devices);
5748
5749
setup_bdevfs(con);
5749
5750
5750
- lfs_op_fn lfs_op = [&](lfs_t *lfs) {
5751
- int err = lfs_mkdir(lfs, settings.filenames[0].c_str());
5752
- if (err == LFS_ERR_EXIST) {
5753
- fos << "Directory already exists\n";
5754
- } else if (err) {
5755
- fos << "LittleFS Error " << err << "\n";
5751
+ switch (settings.bdev.fs) {
5752
+ case fs_littlefs: {
5753
+ lfs_op_fn lfs_op = [&](lfs_t *lfs) {
5754
+ int err = lfs_mkdir(lfs, settings.filenames[0].c_str());
5755
+ if (err == LFS_ERR_EXIST) {
5756
+ fos << "Directory already exists\n";
5757
+ } else if (err) {
5758
+ fos << "LittleFS Error " << err << "\n";
5759
+ }
5760
+ };
5761
+ do_lfs_op(lfs_op);
5762
+ break;
5756
5763
}
5757
- };
5758
- do_lfs_op(lfs_op);
5764
+
5765
+ case fs_fatfs: {
5766
+ fatfs_op_fn fatfs_op = [&](FATFS *fatfs) {
5767
+ int err = f_mkdir(fatfs, settings.filenames[0].c_str());
5768
+ if (err == FR_EXIST) {
5769
+ fos << "Directory already exists\n";
5770
+ } else if (err) {
5771
+ fos << "FatFS Error " << err << "\n";
5772
+ }
5773
+ };
5774
+ do_fatfs_op(fatfs_op);
5775
+ break;
5776
+ }
5777
+
5778
+ default:
5779
+ fail(ERROR_ARGS, "Unknown filesystem specified");
5780
+ }
5759
5781
return false;
5760
5782
}
5761
5783
@@ -5892,17 +5914,40 @@ bool bdev_rm_command::execute(device_map &devices) {
5892
5914
auto con = get_single_bootsel_device_connection(devices);
5893
5915
setup_bdevfs(con);
5894
5916
5895
- lfs_op_fn lfs_op = [&](lfs_t *lfs) {
5896
- int err = lfs_remove(lfs, settings.filenames[0].c_str());
5897
- if (err == LFS_ERR_NOTEMPTY) {
5898
- fail(ERROR_NOT_POSSIBLE, "Directory to remove is not empty");
5899
- } else if (err == LFS_ERR_NOENT) {
5900
- fail(ERROR_NOT_POSSIBLE, "File to remove does not exist");
5901
- } else if (err) {
5902
- fail(ERROR_WRITE_FAILED, "LittleFS Error %d", err);
5917
+ switch (settings.bdev.fs) {
5918
+ case fs_littlefs: {
5919
+ lfs_op_fn lfs_op = [&](lfs_t *lfs) {
5920
+ int err = lfs_remove(lfs, settings.filenames[0].c_str());
5921
+ if (err == LFS_ERR_NOTEMPTY) {
5922
+ fail(ERROR_NOT_POSSIBLE, "Directory to remove is not empty");
5923
+ } else if (err == LFS_ERR_NOENT) {
5924
+ fail(ERROR_NOT_POSSIBLE, "File to remove does not exist");
5925
+ } else if (err) {
5926
+ fail(ERROR_WRITE_FAILED, "LittleFS Error %d", err);
5927
+ }
5928
+ };
5929
+ do_lfs_op(lfs_op);
5930
+ break;
5903
5931
}
5904
- };
5905
- do_lfs_op(lfs_op);
5932
+
5933
+ case fs_fatfs: {
5934
+ fatfs_op_fn fatfs_op = [&](FATFS *fatfs) {
5935
+ int err = f_unlink(fatfs, settings.filenames[0].c_str());
5936
+ if (err == FR_DENIED) {
5937
+ fail(ERROR_NOT_POSSIBLE, "Directory to remove is not empty");
5938
+ } else if (err == FR_NO_FILE) {
5939
+ fail(ERROR_NOT_POSSIBLE, "File to remove does not exist");
5940
+ } else if (err) {
5941
+ fail(ERROR_WRITE_FAILED, "FatFS Error %d", err);
5942
+ }
5943
+ };
5944
+ do_fatfs_op(fatfs_op);
5945
+ break;
5946
+ }
5947
+
5948
+ default:
5949
+ fail(ERROR_ARGS, "Unknown filesystem specified");
5950
+ }
5906
5951
return false;
5907
5952
}
5908
5953
@@ -5913,27 +5958,61 @@ bool bdev_cat_command::execute(device_map &devices) {
5913
5958
auto con = get_single_bootsel_device_connection(devices);
5914
5959
setup_bdevfs(con);
5915
5960
5916
- lfs_op_fn lfs_op = [&](lfs_t *lfs) {
5917
- lfs_file_t file;
5918
- int err = lfs_file_open(lfs, &file, settings.filenames[0].c_str(), LFS_O_RDONLY);
5919
- if (err) {
5920
- fail(ERROR_READ_FAILED, "LittleFS Open Error %d", err);
5921
- }
5922
- auto size = lfs_file_size(lfs, &file);
5923
- err = lfs_file_rewind(lfs, &file);
5924
- std::vector<char> data_buf(size);
5925
- err = lfs_file_read(lfs, &file, data_buf.data(), data_buf.size());
5926
- if (err < 0) {
5927
- fail(ERROR_READ_FAILED, "LittleFS Read Error %d", err);
5928
- } else if (err != data_buf.size()) {
5929
- fail(ERROR_READ_FAILED, "LittleFS Read too short - got %d bytes expected %d bytes", err, data_buf.size());
5930
- }
5931
- err = lfs_file_close(lfs, &file);
5932
-
5933
- string out(data_buf.begin(), data_buf.end());
5934
- printf("%s", out.c_str());
5935
- };
5936
- do_lfs_op(lfs_op);
5961
+ switch (settings.bdev.fs) {
5962
+ case fs_littlefs: {
5963
+ lfs_op_fn lfs_op = [&](lfs_t *lfs) {
5964
+ lfs_file_t file;
5965
+ int err = lfs_file_open(lfs, &file, settings.filenames[0].c_str(), LFS_O_RDONLY);
5966
+ if (err) {
5967
+ fail(ERROR_READ_FAILED, "LittleFS Open Error %d", err);
5968
+ }
5969
+ auto size = lfs_file_size(lfs, &file);
5970
+ err = lfs_file_rewind(lfs, &file);
5971
+ std::vector<char> data_buf(size);
5972
+ err = lfs_file_read(lfs, &file, data_buf.data(), data_buf.size());
5973
+ if (err < 0) {
5974
+ fail(ERROR_READ_FAILED, "LittleFS Read Error %d", err);
5975
+ } else if (err != data_buf.size()) {
5976
+ fail(ERROR_READ_FAILED, "LittleFS Read too short - got %d bytes expected %d bytes", err, data_buf.size());
5977
+ }
5978
+ err = lfs_file_close(lfs, &file);
5979
+
5980
+ string out(data_buf.begin(), data_buf.end());
5981
+ printf("%s", out.c_str());
5982
+ };
5983
+ do_lfs_op(lfs_op);
5984
+ break;
5985
+ }
5986
+
5987
+ case fs_fatfs: {
5988
+ fatfs_op_fn fatfs_op = [&](FATFS *fatfs) {
5989
+ FIL file;
5990
+ int err = f_open(fatfs, &file, settings.filenames[0].c_str(), FA_READ);
5991
+ if (err) {
5992
+ fail(ERROR_READ_FAILED, "FatFS Open Error %d", err);
5993
+ }
5994
+ auto size = f_size(&file);
5995
+ err = f_rewind(&file);
5996
+ std::vector<char> data_buf(size);
5997
+ UINT bytes_read;
5998
+ err = f_read(&file, data_buf.data(), data_buf.size(), &bytes_read);
5999
+ if (err) {
6000
+ fail(ERROR_READ_FAILED, "FatFS Read Error %d", err);
6001
+ } else if (bytes_read != data_buf.size()) {
6002
+ fail(ERROR_READ_FAILED, "FatFS Read too short - got %d bytes expected %d bytes", bytes_read, data_buf.size());
6003
+ }
6004
+ err = f_close(&file);
6005
+
6006
+ string out(data_buf.begin(), data_buf.end());
6007
+ printf("%s", out.c_str());
6008
+ };
6009
+ do_fatfs_op(fatfs_op);
6010
+ break;
6011
+ }
6012
+
6013
+ default:
6014
+ fail(ERROR_ARGS, "Unknown filesystem specified");
6015
+ }
5937
6016
return false;
5938
6017
}
5939
6018
0 commit comments