Skip to content

Commit 50ba717

Browse files
committed
Review fixups
Add extra checks to prevent too big files Fix dirname description Remove short format option Improve erase check (rather than just checking for FF)
1 parent e8c039b commit 50ba717

File tree

1 file changed

+45
-6
lines changed

1 file changed

+45
-6
lines changed

main.cpp

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -713,13 +713,15 @@ auto device_selection =
713713
named_file_types_x(types, i)\
714714
).min(0).doc_non_optional(true)
715715

716-
#define optional_untyped_file_selection_x(name, i)\
716+
#define optional_untyped_path_selection_x(name, desc, i)\
717717
(\
718718
value(name).with_exclusion_filter([](const string &value) {\
719719
return value.find_first_of('-') == 0;\
720-
}).set(settings.filenames[i]).min(0) % "The file name"\
720+
}).set(settings.filenames[i]).min(0) % desc\
721721
).min(0).doc_non_optional(true)
722722

723+
#define optional_untyped_file_selection_x(name, i) optional_untyped_path_selection_x(name, "The file name", i)
724+
723725
#define option_file_selection_x(option, i)\
724726
(\
725727
option & value("filename").with_exclusion_filter([](const string &value) {\
@@ -834,7 +836,7 @@ auto bdev_options = (
834836
integer("partition").set(settings.bdev.partition) % "partition number").force_expand_help(true) +
835837
(option("--filesystem") % "Specify filesystem to use" &
836838
bdev_fs("fs").set(settings.bdev.fs) % "littlefs|fatfs").force_expand_help(true) +
837-
(option('f', "--format").set(settings.bdev.format) % "Format the drive if necessary (may result in data loss)")
839+
(option("--format").set(settings.bdev.format) % "Format the drive if necessary (may result in data loss)")
838840
).min(0).doc_non_optional(true) % "Block device options";
839841

840842
struct bdev_ls_command : public cmd {
@@ -843,7 +845,7 @@ struct bdev_ls_command : public cmd {
843845

844846
group get_cli() override {
845847
return (
846-
optional_untyped_file_selection_x("dirname", 0) +
848+
optional_untyped_path_selection_x("dirname", "The directory name to list (optional)", 0) +
847849
option('r', "--recursive").set(settings.bdev.recursive) % "List files in directories recursively" +
848850
bdev_options +
849851
device_selection % "Target device selection"
@@ -2370,8 +2372,15 @@ struct picoboot_memory_access : public memory_access {
23702372
return;
23712373
}
23722374
// Check if we need to erase (ie check for non 0xff)
2373-
if (!std::all_of(write_data.cbegin(), write_data.cend(), [](uint8_t v) { return v == 0xff; })) {
2374-
// Do automatically erase flash, and make it aligned
2375+
bool do_erase = false;
2376+
for (int i = 0; i < write_data.size(); i++) {
2377+
if (buffer[i] & ~write_data[i]) {
2378+
do_erase = true;
2379+
break;
2380+
}
2381+
}
2382+
if (do_erase) {
2383+
// Automatically erase flash, and make it aligned
23752384
// we have to erase in whole pages
23762385
range aligned_range(address & ~(FLASH_SECTOR_ERASE_SIZE - 1),
23772386
((address + size) & ~(FLASH_SECTOR_ERASE_SIZE - 1)) + FLASH_SECTOR_ERASE_SIZE);
@@ -6230,11 +6239,19 @@ static_assert(FF_MAX_SS == FF_MIN_SS, "FF_MAX_SS must be equal to FF_MIN_SS");
62306239
#define SECTOR_SIZE FF_MAX_SS
62316240

62326241
DRESULT disk_read (void *drv, BYTE* buff, DWORD sector, UINT count) {
6242+
if (sector >= bdevfs_setup.size / SECTOR_SIZE) {
6243+
fail(ERROR_NOT_POSSIBLE, "Sector %d is out of range", sector);
6244+
return RES_PARERR;
6245+
}
62336246
bdevfs_setup.access->read(bdevfs_setup.base_addr + (sector * SECTOR_SIZE), (uint8_t*)buff, count * SECTOR_SIZE, false);
62346247
return RES_OK;
62356248
}
62366249

62376250
DRESULT disk_write (void *drv, const BYTE* buff, DWORD sector, UINT count) {
6251+
if (sector >= bdevfs_setup.size / SECTOR_SIZE) {
6252+
fail(ERROR_NOT_POSSIBLE, "Sector %d is out of range", sector);
6253+
return RES_PARERR;
6254+
}
62386255
if (bdevfs_setup.writeable) {
62396256
bdevfs_setup.access->write(bdevfs_setup.base_addr + (sector * SECTOR_SIZE), (uint8_t*)buff, count * SECTOR_SIZE);
62406257
return RES_OK;
@@ -6276,6 +6293,11 @@ DRESULT disk_ioctl (void *drv, BYTE cmd, void* buff) {
62766293
// Only trim complete flash sectors
62776294
if (start % FLASH_SECTOR_ERASE_SIZE) start += FLASH_SECTOR_ERASE_SIZE - (start % FLASH_SECTOR_ERASE_SIZE);
62786295
end -= end % FLASH_SECTOR_ERASE_SIZE;
6296+
// Check if start and end are in range
6297+
if (start < bdevfs_setup.base_addr || end > bdevfs_setup.base_addr + bdevfs_setup.size) {
6298+
fail(ERROR_NOT_POSSIBLE, "Start or end is out of range");
6299+
return RES_PARERR;
6300+
}
62796301
for (uint32_t addr = start; addr < end; addr += FLASH_SECTOR_ERASE_SIZE) {
62806302
bdevfs_setup.connection->flash_erase(addr, FLASH_SECTOR_ERASE_SIZE);
62816303
}
@@ -6388,11 +6410,19 @@ void do_fatfs_op(fatfs_op_fn fatfs_op) {
63886410

63896411
// LittleFS Functions
63906412
int lfs_read(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size) {
6413+
if (block >= bdevfs_setup.size / c->block_size) {
6414+
fail(ERROR_NOT_POSSIBLE, "Block %d is out of range", block);
6415+
return LFS_ERR_INVAL;
6416+
}
63916417
bdevfs_setup.access->read(bdevfs_setup.base_addr + (block * c->block_size) + off, (uint8_t*)buffer, size, false);
63926418
return LFS_ERR_OK;
63936419
}
63946420

63956421
int lfs_prog(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size) {
6422+
if (block >= bdevfs_setup.size / c->block_size) {
6423+
fail(ERROR_NOT_POSSIBLE, "Block %d is out of range", block);
6424+
return LFS_ERR_INVAL;
6425+
}
63966426
if (bdevfs_setup.writeable) {
63976427
bdevfs_setup.access->write(bdevfs_setup.base_addr + (block * c->block_size) + off, (uint8_t*)buffer, size);
63986428
return LFS_ERR_OK;
@@ -6403,6 +6433,10 @@ int lfs_prog(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const
64036433
};
64046434

64056435
int lfs_erase(const struct lfs_config *c, lfs_block_t block) {
6436+
if (block >= bdevfs_setup.size / c->block_size) {
6437+
fail(ERROR_NOT_POSSIBLE, "Block %d is out of range", block);
6438+
return LFS_ERR_INVAL;
6439+
}
64066440
if (bdevfs_setup.writeable) {
64076441
bdevfs_setup.connection->flash_erase(bdevfs_setup.base_addr + (block * c->block_size), c->block_size);
64086442
return LFS_ERR_OK;
@@ -6613,6 +6647,11 @@ bool bdev_cp_command::execute(device_map &devices) {
66136647
if (!srcRemote) {
66146648
auto infile = get_file_idx(ios::in|ios::binary|ios::ate, 0);
66156649
auto size = infile->tellg();
6650+
uint32_t max_size = bdevfs_setup.access->get_model()->flash_end() - bdevfs_setup.access->get_model()->flash_start();
6651+
if (size > max_size) {
6652+
fail(ERROR_NOT_POSSIBLE, "File size %dMB is too large for flash device (max %dMB)", size / (1024 * 1024), max_size / (1024 * 1024));
6653+
return false;
6654+
}
66166655
infile->seekg(0, std::ios::beg);
66176656
data_buf.resize(size);
66186657
infile->read(data_buf.data(), size);

0 commit comments

Comments
 (0)