rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
mass_storage_init.cpp
Go to the documentation of this file.
1#include "pch.h"
2
3#include "mass_storage_init.h"
5#include "null_device.h"
6
7#if HAL_USE_USB_MSD
8
9#if EFI_EMBED_INI_MSD
10 #if EFI_USE_COMPRESSED_INI_MSD
13 #else
14 #include "ramdisk.h"
15 #include "ramdisk_image.h"
16 #endif
17
18 // If the ramdisk image told us not to use it, don't use it.
19 #ifdef RAMDISK_INVALID
20 #undef EFI_EMBED_INI_MSD
21 #define EFI_EMBED_INI_MSD FALSE
22 #endif
23#endif
24
25#if EFI_EMBED_INI_MSD
26 #if EFI_USE_COMPRESSED_INI_MSD
27 /* WARNING: CompressedBlockDevice will consume ~34Kb RAM! */
28 /* Enabling this option will also consume (2048-256) additional RAM bytes for increased USB_MSD_THREAD_WA_SIZE */
30 #else
31 /* WARNING: this will add 128K of ROM data */
32 static RamDisk ramdisk;
33 #endif
34#endif
35
36#if STM32_USB_USE_OTG1
37 USBDriver *usb_driver = &USBD1;
38#elif STM32_USB_USE_OTG2
39 USBDriver *usb_driver = &USBD2;
40#else
41 #error MSD needs OTG1 or OTG2 to be enabled
42#endif
43
44// One block buffer per LUN
45static NO_CACHE uint8_t blkbuf0[MMCSD_BLOCK_SIZE];
46
48
49static const scsi_inquiry_response_t iniDriveInquiry = {
50 0x00, /* direct access block device */
51 0x80, /* removable */
52 0x04, /* SPC-2 */
53 0x02, /* response data format */
54 sizeof(scsi_inquiry_response_t) - 5, // size of this struct, minus bytes up to and including this one
55 0x00,
56 0x00,
57 0x00,
58 "rusEFI",
59 "INI Drive",
60 {'v',CH_KERNEL_MAJOR+'0','.',CH_KERNEL_MINOR+'0'}
61};
62
63static const scsi_inquiry_response_t sdCardInquiry = {
64 0x00, /* direct access block device */
65 0x80, /* removable */
66 0x04, /* SPC-2 */
67 0x02, /* response data format */
68 sizeof(scsi_inquiry_response_t) - 5, // size of this struct, minus bytes up to and including this one
69 0x00,
70 0x00,
71 0x00,
72 "rusEFI",
73 "SD Card",
74 {'v',CH_KERNEL_MAJOR+'0','.',CH_KERNEL_MINOR+'0'}
75};
76
77void attachMsdSdCard(BaseBlockDevice* blkdev, uint8_t *blkbuf, size_t blkbufsize) {
78 if ((blkbuf == NULL) || (blkbufsize == 0)) {
79 // if no specific buffer was provided use default
81 blkbufsize = sizeof(blkbuf0);
82 }
83 msd.attachLun(1, blkdev, blkbuf, blkbufsize, &sdCardInquiry, nullptr);
84
85#if EFI_TUNER_STUDIO
86 // SD MSD attached, enable indicator in TS
88#endif
89}
90
92 // this is safe to use same read/write buffer couse all luns are handled from one thread
93 msd.attachLun(1, (BaseBlockDevice*)&ND1, blkbuf0, sizeof(blkbuf0), &sdCardInquiry, nullptr);
94
95#if EFI_TUNER_STUDIO
96 // SD MSD attached, enable indicator in TS
98#endif
99}
100
101static BaseBlockDevice* getRamdiskDevice() {
102#if EFI_EMBED_INI_MSD
103#if EFI_USE_COMPRESSED_INI_MSD
104 uzlib_init();
106 compressedBlockDeviceStart(&cbd, ramdisk_image_gz, sizeof(ramdisk_image_gz));
107
108 return (BaseBlockDevice*)&cbd;
109#else // not EFI_USE_COMPRESSED_INI_MSD
110 ramdiskObjectInit(&ramdisk);
111
112 constexpr size_t ramdiskSize = sizeof(ramdisk_image);
113 constexpr size_t blockSize = 512;
114 constexpr size_t blockCount = ramdiskSize / blockSize;
115
116 // Ramdisk should be a round number of blocks
117 static_assert(ramdiskSize % blockSize == 0);
118
119 ramdiskStart(&ramdisk, const_cast<uint8_t*>(ramdisk_image), blockSize, blockCount, /*readonly =*/ true);
120
121 return (BaseBlockDevice*)&ramdisk;
122#endif // EFI_USE_COMPRESSED_INI_MSD
123#else // not EFI_EMBED_INI_MSD
124 // No embedded ini file, just mount the null device instead
125 return (BaseBlockDevice*)&ND1;
126#endif
127}
128
130 // Attach the ini ramdisk
131 msd.attachLun(0, getRamdiskDevice(), blkbuf0, sizeof(blkbuf0), &iniDriveInquiry, nullptr);
132
133 // attach a null device in place of the SD card for now - the SD thread may replace it later
134 // this is safe to use same read/write buffer couse all luns are handled from one thread
135 msd.attachLun(1, (BaseBlockDevice*)&ND1, blkbuf0, sizeof(blkbuf0), &sdCardInquiry, nullptr);
136
137 // start the mass storage thread
138 msd.start();
139}
140
141#endif // HAL_USE_USB_MSD
TunerStudioOutputChannels outputChannels
Definition engine.h:109
void attachLun(uint8_t lunIndex, BaseBlockDevice *blkdev, uint8_t *blkbuf, size_t blkbufsize, const scsi_inquiry_response_t *inquiry, const scsi_unit_serial_number_inquiry_response_t *serialInquiry)
void start()
Start the thread.
void compressedBlockDeviceObjectInit(CompressedBlockDevice *cbd)
void compressedBlockDeviceStart(CompressedBlockDevice *cbd, const uint8_t *source, size_t sourceSize)
This file implements a ChibiOS block device backed by a compressed (gzip) store.
static EngineAccessor engine
Definition engine.h:413
static BaseBlockDevice * getRamdiskDevice()
USBDriver * usb_driver
static CompressedBlockDevice cbd
static NO_CACHE uint8_t blkbuf0[MMCSD_BLOCK_SIZE]
static MassStorageController msd(usb_driver)
static const scsi_inquiry_response_t sdCardInquiry
void deattachMsdSdCard(void)
static const scsi_inquiry_response_t iniDriveInquiry
static RamDisk ramdisk
void attachMsdSdCard(BaseBlockDevice *blkdev, uint8_t *blkbuf, size_t blkbufsize)
void initUsbMsd()
static union @47 NO_CACHE
uint8_t blkbuf[4 *MMCSD_BLOCK_SIZE]
Definition mmc_card.cpp:292
NullDevice ND1