From 990bfe763e5acf9330248c14e6f15be59f9865ea Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Sat, 13 Feb 2021 05:53:10 -0800 Subject: [PATCH] implement null block device (#2341) * implement null device * fix build --- firmware/Makefile | 4 +- .../hw_layer/mass_storage/mass_storage.mk | 5 +- .../hw_layer/mass_storage/null_device.cpp | 75 +++++++++++++++++++ firmware/hw_layer/mass_storage/null_device.h | 15 ++++ 4 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 firmware/hw_layer/mass_storage/null_device.cpp create mode 100644 firmware/hw_layer/mass_storage/null_device.h diff --git a/firmware/Makefile b/firmware/Makefile index 64fa245fce..f793c99b5f 100644 --- a/firmware/Makefile +++ b/firmware/Makefile @@ -295,7 +295,9 @@ INCDIR = $(ALLINC) \ $(DEVELOPMENT_DIR) \ development/hw_layer \ development/test \ - $(CONTROLLERS_INC) + $(CONTROLLERS_INC) \ + $(CHIBIOS_CONTRIB)/os/various + # contrib/os/various must go at the end because it also contains pid.h which conflicts with our pid.h # # Project, sources and paths diff --git a/firmware/hw_layer/mass_storage/mass_storage.mk b/firmware/hw_layer/mass_storage/mass_storage.mk index 2f4e743846..0f1c2b6d64 100644 --- a/firmware/hw_layer/mass_storage/mass_storage.mk +++ b/firmware/hw_layer/mass_storage/mass_storage.mk @@ -1,3 +1,4 @@ -HW_MASS_STORAGE_SRC_C = $(PROJECT_DIR)/hw_layer/mass_storage/usb_msd_cfg.c \ - $(PROJECT_DIR)/ChibiOS-Contrib/os/various/lib_scsi.c +HW_MASS_STORAGE_SRC_C = $(PROJECT_DIR)/ChibiOS-Contrib/os/various/lib_scsi.c + +ALLCPPSRC += $(PROJECT_DIR)/hw_layer/mass_storage/null_device.cpp diff --git a/firmware/hw_layer/mass_storage/null_device.cpp b/firmware/hw_layer/mass_storage/null_device.cpp new file mode 100644 index 0000000000..885cf37031 --- /dev/null +++ b/firmware/hw_layer/mass_storage/null_device.cpp @@ -0,0 +1,75 @@ +/** + * @file zero_device.cpp + * + * @date Feb 12, 2021 + * @author Matthew Kennedy, (c) 2021 + * + * This file implements a special block device that simply reports "no media" + * Use it when you need to mount *something* but don't have an SD card available. + */ + +#include "hal.h" + +#include + +struct NullDevice { + const struct BaseBlockDeviceVMT *vmt; + _base_block_device_data +}; + +static bool nd_is_inserted(void*) { + // This function is the whole point - we have no media! + return false; +} + +static bool nd_is_protected(void*) { + return false; +} + +static bool nd_return_success(void*) { + return HAL_SUCCESS; +} + +static bool nd_return_success_read(void*, uint32_t, uint8_t* buffer, uint32_t n) { + // write zeroes to the buffer to prevent somebody reading random memory + memset(buffer, 0, n); + + return HAL_SUCCESS; +} + +static bool nd_return_success_write(void*, uint32_t, const uint8_t*, uint32_t) { + return HAL_SUCCESS; +} + +static bool nd_get_info(void*, BlockDeviceInfo* bdip) { + // We have to report non-zero size here because Windows + // will query the size of the block device even if we indicate + // that the device has no media + // If we report zeroes, it breaks USB until you unplug this device + bdip->blk_num = 1000; + bdip->blk_size = 512; + return HAL_SUCCESS; +} + +static const struct BaseBlockDeviceVMT ndVmt = { + (size_t)0, // instanceOffset + nd_is_inserted, + nd_is_protected, + + // These functions just claim success to make the host happy + nd_return_success, + nd_return_success, + nd_return_success_read, + nd_return_success_write, + nd_return_success, + nd_get_info +}; + +// This device is always ready and has no state +static NullDevice nd = { &ndVmt, BLK_READY }; + +#if HAL_USE_USB_MSD +void msdMountNullDevice(USBMassStorageDriver* msdp, USBDriver *usbp, uint8_t* blkbuf, const scsi_inquiry_response_t* inquiry) { + msdStart(msdp, usbp, (BaseBlockDevice*)&nd, blkbuf, inquiry, nullptr); +} +#endif diff --git a/firmware/hw_layer/mass_storage/null_device.h b/firmware/hw_layer/mass_storage/null_device.h new file mode 100644 index 0000000000..db64b3b979 --- /dev/null +++ b/firmware/hw_layer/mass_storage/null_device.h @@ -0,0 +1,15 @@ +/** + * @file zero_device.cpp + * + * @date Feb 12, 2021 + * @author Matthew Kennedy, (c) 2021 + * + * This file implements a special block device that simply reports "no media" + * Use it when you need to mount *something* but don't have an SD card available. + */ + +#include "hal.h" + +#if HAL_USE_USB_MSD +void msdMountNullDevice(USBMassStorageDriver* msdp, USBDriver *usbp, uint8_t* blkbuf, const scsi_inquiry_response_t* inquiry); +#endif