rusEFI
The most advanced open source ECU
null_device.cpp
Go to the documentation of this file.
1 /**
2  * @file zero_device.cpp
3  *
4  * @date Feb 12, 2021
5  * @author Matthew Kennedy, (c) 2021
6  *
7  * This file implements a special block device that simply reports "no media"
8  * Use it when you need to mount *something* but don't have an SD card available.
9  */
10 
11 #include "pch.h"
12 
13 #include <cstring>
14 
15 struct NullDevice {
16  const struct BaseBlockDeviceVMT *vmt;
17  _base_block_device_data
18 };
19 
20 static bool nd_is_inserted(void*) {
21  // This function is the whole point - we have no media!
22  return false;
23 }
24 
25 static bool nd_is_protected(void*) {
26  return false;
27 }
28 
29 static bool nd_return_success(void*) {
30  return HAL_SUCCESS;
31 }
32 
33 static bool nd_return_success_read(void*, uint32_t, uint8_t* buffer, uint32_t n) {
34  // write zeroes to the buffer to prevent somebody reading random memory
35  memset(buffer, 0, n);
36 
37  return HAL_SUCCESS;
38 }
39 
40 static bool nd_return_success_write(void*, uint32_t, const uint8_t*, uint32_t) {
41  return HAL_SUCCESS;
42 }
43 
44 static bool nd_get_info(void*, BlockDeviceInfo* bdip) {
45  // We have to report non-zero size here because Windows
46  // will query the size of the block device even if we indicate
47  // that the device has no media
48  // If we report zeroes, it breaks USB until you unplug this device
49  bdip->blk_num = 1000;
50  bdip->blk_size = 512;
51  return HAL_SUCCESS;
52 }
53 
54 static const struct BaseBlockDeviceVMT ndVmt = {
55  (size_t)0, // instanceOffset
58 
59  // These functions just claim success to make the host happy
66 };
67 
68 // This device is always ready and has no state
69 NullDevice ND1 = { &ndVmt, BLK_READY };
static bool nd_return_success_read(void *, uint32_t, uint8_t *buffer, uint32_t n)
Definition: null_device.cpp:33
static bool nd_get_info(void *, BlockDeviceInfo *bdip)
Definition: null_device.cpp:44
static bool nd_return_success_write(void *, uint32_t, const uint8_t *, uint32_t)
Definition: null_device.cpp:40
static bool nd_is_protected(void *)
Definition: null_device.cpp:25
static bool nd_return_success(void *)
Definition: null_device.cpp:29
static bool nd_is_inserted(void *)
Definition: null_device.cpp:20
NullDevice ND1
Definition: null_device.cpp:69
static const struct BaseBlockDeviceVMT ndVmt
Definition: null_device.cpp:54
static BigBufferHandle buffer