9#include <l4/cxx/ref_ptr>
11#include <l4/libblock-device/device.h>
12#include <l4/libblock-device/partition.h>
18namespace Block_device {
26 template <
typename PART_DEV,
typename BASE_DEV,
27 bool = std::is_base_of<Device_discard_feature, BASE_DEV>::value>
36 template <
typename PART_DEV,
typename BASE_DEV>
40 using Base = BASE_DEV;
41 using Part_device = PART_DEV;
44 typename Base::Discard_info discard_info()
const override
46 return dev()->parent()->discard_info();
50 Inout_callback
const &cb,
bool discard)
override
52 auto sz = dev()->partition_size();
60 if (cur->
sector >= sz - offset)
62 if (cur->num_sectors > sz)
64 if (offset + cur->
sector > sz - cur->num_sectors)
67 cur = cur->next.get();
70 auto start = offset + dev()->partition_start();
71 Dbg::trace(
"partition")
72 .printf(
"Starting sector on disk: 0x%llx\n", start);
73 return dev()->parent()->discard(start, blocks, cb, discard);
77 Part_device
const *dev()
const
78 {
return static_cast<Part_device
const *
>(
this); }
90template <
typename BASE_DEV = Device>
95 using Device_type = BASE_DEV;
106 "Last sector of partition before first sector.");
108 if (partition_id > 999)
110 "Partition ID must be smaller than 1000.");
112 snprintf(_partition_id,
sizeof(_partition_id),
"%d", partition_id);
114 static_assert(
sizeof(_guid) ==
sizeof(pi.
guid),
"String size mismatch");
115 memcpy(_guid, pi.
guid,
sizeof(_guid));
119 {
return _parent->notification_domain(); }
121 bool is_read_only()
const override
122 {
return _parent->is_read_only(); }
124 bool match_hid(
cxx::String const &hid)
const override
129 _Pragma(
"GCC diagnostic push");
130 _Pragma(
"GCC diagnostic ignored \"-Wdeprecated-declarations\"");
131 std::u16string whid =
132 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>,
char16_t>{}
133 .from_bytes(std::string(hid.
start(), hid.
len()));
134 _Pragma(
"GCC diagnostic pop");
139 char const *delim =
":";
140 char const *pos = hid.
rfind(delim);
149 {
return _size * _parent->sector_size(); }
152 {
return _parent->sector_size(); }
155 {
return _parent->max_size(); }
157 unsigned max_segments()
const override
158 {
return _parent->max_segments(); }
160 void reset()
override
166 {
return _parent->dma_map(region, offset, num_sectors, dir, phys); }
170 {
return _parent->dma_unmap(phys, num_sectors, dir); }
173 Inout_callback
const &cb,
183 total += cur->num_sectors;
184 cur = cur->next.get();
187 if (total > _size - sector)
190 Dbg::trace(
"partition").printf(
"Sector on disk: 0x%llx\n", sector + _start);
191 return _parent->inout_data(sector + _start, blocks, cb, dir);
194 int flush(Inout_callback
const &cb)
override
196 return _parent->flush(cb);
199 void start_device_scan(Block_device::Errand::Callback
const &callback)
override
208 Device_type *parent()
const
209 {
return _parent.
get(); }
214 std::u16string _name;
215 char _partition_id[4];
Dummy class used when the device class is not derived from Device_discard_feature.
A partition device for the given device interface.
l4_uint64_t Dma_addr
Data type for DMA addresses.
Direction
Direction of the DMA transfers.
Region of driver memory, that shall be managed locally.
A reference-counting pointer with automatic cleanup.
T * get() const noexcept
Return a raw pointer to the object this shared pointer points to.
Allocation free string class with explicit length field.
Index start() const
Pointer to first character.
Index end() const
Pointer to first byte behind the string.
char const * rfind(char const *c) const
Find right-most character. Return end() if not found.
unsigned int l4_size_t
Unsigned size type.
unsigned long l4_addr_t
Address type.
unsigned long long l4_uint64_t
Unsigned 64bit value.
@ L4_EINVAL
Invalid argument.
long chksys(long err, char const *extra="", long ret=0)
Generate C++ exception on error.
Description of an inout block to be sent to the device.
l4_uint64_t sector
Initial sector. Used only by DISCARD / WRITE_ZEROES requests.
Opaque type for representing a notification domain.
Information about a single partition.
char guid[37]
ID of the partition.
l4_uint64_t last
Last valid sector.
l4_uint64_t first
First valid sector.
std::u16string name
UTF16 name of the partition.