smoo-gadget-core 0.0.1

Reverse USB Mass Storage
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
use anyhow::{Context, Result, anyhow, ensure};
use dma_heap::HeapKind;
use mmap::MemoryMap;
use nix::{
    ioctl_readwrite, ioctl_write_ptr,
    poll::{PollFd, PollFlags, PollTimeout, poll},
};
use std::{
    collections::VecDeque,
    os::fd::{AsFd, AsRawFd, FromRawFd, OwnedFd, RawFd},
};
use tracing::{trace, warn};

const FUNCTIONFS_IOC_MAGIC: u8 = b'g';
const FUNCTIONFS_DMABUF_ATTACH_NR: u8 = 131;
const FUNCTIONFS_DMABUF_DETACH_NR: u8 = 132;
const FUNCTIONFS_DMABUF_TRANSFER_NR: u8 = 133;

#[repr(C, packed)]
struct UsbFfsDmabufTransferReq {
    fd: libc::c_int,
    flags: u32,
    length: u64,
}

ioctl_write_ptr!(
    ffs_dmabuf_attach,
    FUNCTIONFS_IOC_MAGIC,
    FUNCTIONFS_DMABUF_ATTACH_NR,
    libc::c_int
);

ioctl_write_ptr!(
    ffs_dmabuf_detach,
    FUNCTIONFS_IOC_MAGIC,
    FUNCTIONFS_DMABUF_DETACH_NR,
    libc::c_int
);

ioctl_write_ptr!(
    ffs_dmabuf_transfer,
    FUNCTIONFS_IOC_MAGIC,
    FUNCTIONFS_DMABUF_TRANSFER_NR,
    UsbFfsDmabufTransferReq
);

#[repr(C)]
struct dma_buf_export_sync_file_req {
    flags: u32,
    fd: i32,
}

const DMA_BUF_SYNC_READ: u32 = 1 << 0;
const DMA_BUF_SYNC_WRITE: u32 = 1 << 1;
const DMA_BUF_SYNC_RW: u32 = DMA_BUF_SYNC_READ | DMA_BUF_SYNC_WRITE;

ioctl_readwrite!(
    dma_buf_export_sync_file,
    b'b',
    2,
    dma_buf_export_sync_file_req
);

#[repr(C)]
struct DmaBufSync {
    flags: u64,
}

const DMA_BUF_SYNC_NR: u8 = 0;
const DMA_BUF_SYNC_START: u64 = 0 << 2;
const DMA_BUF_SYNC_END: u64 = 1 << 2;
const DMA_BUF_SYNC_READ_FLAG: u64 = 1 << 0;
const DMA_BUF_SYNC_WRITE_FLAG: u64 = 1 << 1;

ioctl_write_ptr!(dma_buf_sync, b'b', DMA_BUF_SYNC_NR, DmaBufSync);

/// Unified pool that hands out DMA-BUF-backed buffers where available, falling back to
/// copy buffers when DMA allocation/attachment fails. Buffers are held for the gadget
/// lifetime; preallocation is attempted but the pool can grow up to `cap`.
pub(crate) struct BufferPool {
    dma: DmaPool,
    copy: CopyPool,
}

// Safe because BufferPool is only accessed behind a Mutex, and its internal raw
// pointers never outlive the pool itself.
unsafe impl Send for BufferPool {}

impl BufferPool {
    pub(crate) fn new(
        bulk_in_fd: RawFd,
        bulk_out_fd: RawFd,
        heap_kind: Option<HeapKind>,
        buf_len: usize,
        prealloc: usize,
        cap: usize,
    ) -> Result<Self> {
        ensure!(buf_len > 0, "buffer length must be positive");
        ensure!(cap > 0, "buffer pool cap must be positive");
        ensure!(prealloc <= cap, "prealloc cannot exceed cap");
        let dma = DmaPool::new(bulk_in_fd, bulk_out_fd, heap_kind, buf_len, prealloc, cap)
            .context("init DMA pool")?;
        let copy = CopyPool::new(buf_len, cap.saturating_sub(dma.len()));
        Ok(Self { dma, copy })
    }

    pub(crate) fn checkout(&mut self) -> BufferHandle {
        if let Some(handle) = self.dma.checkout() {
            return BufferHandle::Dma(handle);
        }
        if let Some(handle) = self.copy.checkout() {
            return BufferHandle::Copy(handle);
        }
        // As a last resort, try to grow DMA then copy.
        if let Some(handle) = self.dma.grow_one() {
            return BufferHandle::Dma(handle);
        }
        BufferHandle::Copy(self.copy.fallback())
    }

    pub(crate) fn checkin(&mut self, handle: BufferHandle) {
        match handle {
            BufferHandle::Dma(buf) => self.dma.checkin(buf),
            BufferHandle::Copy(buf) => self.copy.checkin(buf),
        }
    }
}

pub(crate) enum BufferHandle {
    Dma(DmaBufferHandle),
    Copy(CopyBuffer),
}

unsafe impl Send for BufferHandle {}

impl BufferHandle {
    pub(crate) fn len(&self) -> usize {
        match self {
            BufferHandle::Dma(h) => h.len(),
            BufferHandle::Copy(h) => h.buf.len(),
        }
    }

    pub(crate) fn as_mut_slice(&mut self) -> &mut [u8] {
        match self {
            BufferHandle::Dma(h) => h.as_mut_slice(),
            BufferHandle::Copy(h) => &mut h.buf,
        }
    }

    pub(crate) fn as_slice(&self) -> &[u8] {
        match self {
            BufferHandle::Dma(h) => h.as_slice(),
            BufferHandle::Copy(h) => &h.buf,
        }
    }

    pub(crate) fn dma_fd(&self) -> Option<RawFd> {
        match self {
            BufferHandle::Dma(h) => Some(h.fd()),
            BufferHandle::Copy(_) => None,
        }
    }

    pub(crate) fn prepare_device_read(&mut self) -> Result<()> {
        match self {
            BufferHandle::Dma(h) => h.prepare_device_read(),
            BufferHandle::Copy(_) => Ok(()),
        }
    }

    pub(crate) fn finish_device_write(&mut self) -> Result<()> {
        match self {
            BufferHandle::Dma(h) => h.finish_device_write(),
            BufferHandle::Copy(_) => Ok(()),
        }
    }
}

pub(crate) fn dmabuf_transfer_blocking(
    endpoint_fd: RawFd,
    buf_fd: RawFd,
    len: usize,
) -> Result<()> {
    let length = u64::try_from(len).context("dma-buf transfer length exceeds u64")?;
    trace!(endpoint_fd, buf_fd, len, "FUNCTIONFS_DMABUF_TRANSFER begin");
    unsafe {
        ffs_dmabuf_transfer(
            endpoint_fd,
            &UsbFfsDmabufTransferReq {
                fd: buf_fd,
                flags: 0,
                length,
            },
        )
    }
    .map_err(|err| anyhow!("FUNCTIONFS_DMABUF_TRANSFER failed: {err}"))?;
    trace!(
        endpoint_fd,
        buf_fd, len, "FUNCTIONFS_DMABUF_TRANSFER submitted"
    );
    wait_for_dmabuf_completion(buf_fd)
}

fn wait_for_dmabuf_completion(dmabuf_fd: RawFd) -> Result<()> {
    trace!(buf_fd = dmabuf_fd, "DMA_BUF_IOCTL_EXPORT_SYNC_FILE begin");
    let sync_fd = unsafe {
        let mut req = dma_buf_export_sync_file_req {
            flags: DMA_BUF_SYNC_RW,
            fd: -1,
        };
        dma_buf_export_sync_file(dmabuf_fd, &mut req)
            .map_err(|err| anyhow!("DMA_BUF_IOCTL_EXPORT_SYNC_FILE failed: {err}"))?;
        ensure!(req.fd >= 0, "sync file descriptor invalid");
        OwnedFd::from_raw_fd(req.fd)
    };
    trace!(
        buf_fd = dmabuf_fd,
        sync_fd = sync_fd.as_raw_fd(),
        "DMA_BUF_IOCTL_EXPORT_SYNC_FILE complete"
    );

    let mut fds = [PollFd::new(sync_fd.as_fd(), PollFlags::POLLIN)];
    loop {
        trace!(
            sync_fd = sync_fd.as_raw_fd(),
            "polling for dma-buf completion"
        );
        match poll(&mut fds, PollTimeout::NONE) {
            Ok(0) => continue,
            Ok(_) => {
                let revents = fds[0].revents().unwrap_or(PollFlags::empty());
                if revents.contains(PollFlags::POLLERR) {
                    return Err(anyhow!("dma-buf completion poll error"));
                }
                if revents.intersects(PollFlags::POLLIN) {
                    break;
                }
            }
            Err(err) => return Err(err.into()),
        }
    }
    trace!(buf_fd = dmabuf_fd, "dma-buf completion fence signaled");
    Ok(())
}

struct DmaPool {
    heap: Option<dma_heap::Heap>,
    bulk_in_fd: RawFd,
    bulk_out_fd: RawFd,
    buf_len: usize,
    cap: usize,
    available: VecDeque<DmaBuffer>,
    allocated: usize,
    dma_disabled: bool,
}

impl DmaPool {
    fn new(
        bulk_in_fd: RawFd,
        bulk_out_fd: RawFd,
        heap_kind: Option<HeapKind>,
        buf_len: usize,
        prealloc: usize,
        cap: usize,
    ) -> Result<Self> {
        let heap = match heap_kind {
            Some(kind) => Some(dma_heap::Heap::new(kind).context("open DMA heap")?),
            None => None,
        };
        let mut pool = Self {
            heap,
            bulk_in_fd,
            bulk_out_fd,
            buf_len,
            cap,
            available: VecDeque::new(),
            allocated: 0,
            dma_disabled: false,
        };
        for _ in 0..prealloc {
            if let Some(buf) = pool.try_allocate_buf() {
                pool.available.push_back(buf);
            } else {
                break;
            }
        }
        Ok(pool)
    }

    fn len(&self) -> usize {
        self.allocated
    }

    fn checkout(&mut self) -> Option<DmaBufferHandle> {
        if self.dma_disabled {
            return None;
        }
        self.available
            .pop_front()
            .map(|buf| DmaBufferHandle { buf })
    }

    fn checkin(&mut self, handle: DmaBufferHandle) {
        self.available.push_back(handle.buf);
    }

    fn grow_one(&mut self) -> Option<DmaBufferHandle> {
        if self.dma_disabled {
            return None;
        }
        if let Some(buf) = self.try_allocate_buf() {
            return Some(DmaBufferHandle { buf });
        }
        None
    }

    fn try_allocate_buf(&mut self) -> Option<DmaBuffer> {
        if self.allocated >= self.cap || self.heap.is_none() {
            return None;
        }
        let heap = self.heap.as_ref()?;
        let fd = match heap.allocate(self.buf_len) {
            Ok(fd) => fd,
            Err(err) => {
                warn!(error = %err, "dma-heap allocation failed, disabling dma");
                self.dma_disabled = true;
                return None;
            }
        };
        match DmaBuffer::new(fd, self.buf_len, self.bulk_in_fd, self.bulk_out_fd) {
            Ok(buf) => {
                self.allocated += 1;
                Some(buf)
            }
            Err(err) => {
                warn!(error = %err, "dma buffer init failed, disabling dma");
                self.dma_disabled = true;
                None
            }
        }
    }
}

struct DmaBuffer {
    fd: OwnedFd,
    map: MemoryMap,
    len: usize,
}

impl DmaBuffer {
    fn new(fd: OwnedFd, len: usize, bulk_in_fd: RawFd, bulk_out_fd: RawFd) -> Result<Self> {
        let raw_fd = fd.as_raw_fd();
        let map = MemoryMap::new(
            len,
            &[
                mmap::MapOption::MapReadable,
                mmap::MapOption::MapWritable,
                mmap::MapOption::MapFd(raw_fd),
                mmap::MapOption::MapNonStandardFlags(libc::MAP_SHARED),
            ],
        )
        .map_err(|err| anyhow!("map DMA buffer: {err}"))?;
        let mut attachments = EndpointAttachments::new(bulk_in_fd);
        attachments.attach(raw_fd)?;
        let mut out_attachments = EndpointAttachments::new(bulk_out_fd);
        out_attachments.attach(raw_fd)?;
        Ok(Self { fd, map, len })
    }

    fn raw_fd(&self) -> RawFd {
        self.fd.as_raw_fd()
    }

    fn as_slice(&self) -> &[u8] {
        unsafe { std::slice::from_raw_parts(self.map.data(), self.len) }
    }

    fn as_mut_slice(&mut self) -> &mut [u8] {
        unsafe { std::slice::from_raw_parts_mut(self.map.data(), self.len) }
    }
}

pub(crate) struct DmaBufferHandle {
    buf: DmaBuffer,
}

unsafe impl Send for DmaBufferHandle {}

impl DmaBufferHandle {
    pub(crate) fn fd(&self) -> RawFd {
        self.buf.raw_fd()
    }

    pub(crate) fn len(&self) -> usize {
        self.buf.len
    }

    pub(crate) fn as_slice(&self) -> &[u8] {
        self.buf.as_slice()
    }

    pub(crate) fn as_mut_slice(&mut self) -> &mut [u8] {
        self.buf.as_mut_slice()
    }

    pub(crate) fn prepare_device_read(&mut self) -> Result<()> {
        dma_buf_sync_start(self.fd(), DMA_BUF_SYNC_WRITE_FLAG)?;
        dma_buf_sync_end(self.fd(), DMA_BUF_SYNC_WRITE_FLAG)
    }

    pub(crate) fn finish_device_write(&mut self) -> Result<()> {
        dma_buf_sync_start(self.fd(), DMA_BUF_SYNC_READ_FLAG)?;
        dma_buf_sync_end(self.fd(), DMA_BUF_SYNC_READ_FLAG)
    }
}

struct CopyPool {
    buf_len: usize,
    available: VecDeque<CopyBuffer>,
    cap: usize,
}

impl CopyPool {
    fn new(buf_len: usize, cap: usize) -> Self {
        Self {
            buf_len,
            available: VecDeque::new(),
            cap,
        }
    }

    fn checkout(&mut self) -> Option<CopyBuffer> {
        self.available.pop_front()
    }

    fn checkin(&mut self, buf: CopyBuffer) {
        if self.available.len() < self.cap {
            self.available.push_back(buf);
        }
    }

    fn fallback(&self) -> CopyBuffer {
        CopyBuffer {
            buf: vec![0u8; self.buf_len],
        }
    }
}

pub(crate) struct CopyBuffer {
    buf: Vec<u8>,
}

struct EndpointAttachments {
    fd: RawFd,
    attached: Vec<RawFd>,
}

impl EndpointAttachments {
    fn new(fd: RawFd) -> Self {
        Self {
            fd,
            attached: Vec::new(),
        }
    }

    fn attach(&mut self, buf_fd: RawFd) -> Result<()> {
        trace!(
            endpoint_fd = self.fd,
            buf_fd, "FUNCTIONFS_DMABUF_ATTACH begin"
        );
        unsafe { ffs_dmabuf_attach(self.fd, &buf_fd) }
            .map_err(|err| anyhow!(err))
            .with_context(|| format!("FUNCTIONFS_DMABUF_ATTACH failed for fd {buf_fd}"))?;
        trace!(
            endpoint_fd = self.fd,
            buf_fd, "FUNCTIONFS_DMABUF_ATTACH complete"
        );
        self.attached.push(buf_fd);
        Ok(())
    }

    fn detach_all(&mut self) {
        for buf_fd in self.attached.drain(..) {
            trace!(
                endpoint_fd = self.fd,
                buf_fd, "FUNCTIONFS_DMABUF_DETACH begin"
            );
            let res = unsafe { ffs_dmabuf_detach(self.fd, &buf_fd) };
            match res {
                Ok(_) => trace!(
                    endpoint_fd = self.fd,
                    buf_fd, "FUNCTIONFS_DMABUF_DETACH complete"
                ),
                Err(err) => warn!(
                    endpoint_fd = self.fd,
                    buf_fd,
                    error = %err,
                    "FUNCTIONFS_DMABUF_DETACH failed"
                ),
            }
        }
    }
}

impl Drop for EndpointAttachments {
    fn drop(&mut self) {
        self.detach_all();
    }
}

fn dma_buf_sync_call(fd: RawFd, flags: u64) -> Result<()> {
    let req = DmaBufSync { flags };
    trace!(fd, flags, "DMA_BUF_IOCTL_SYNC begin");
    let res = unsafe { dma_buf_sync(fd, &req) };
    match res {
        Ok(0) => {
            trace!(fd, flags, "DMA_BUF_IOCTL_SYNC complete");
            Ok(())
        }
        Ok(code) => Err(anyhow!("DMA_BUF_IOCTL_SYNC unexpected code {code}")),
        Err(err) => {
            warn!(fd, flags, error = %err, "DMA_BUF_IOCTL_SYNC failed");
            Err(anyhow!("DMA_BUF_IOCTL_SYNC failed: {err}"))
        }
    }
}

fn dma_buf_sync_start(fd: RawFd, dir: u64) -> Result<()> {
    dma_buf_sync_call(fd, DMA_BUF_SYNC_START | dir)
}

fn dma_buf_sync_end(fd: RawFd, dir: u64) -> Result<()> {
    dma_buf_sync_call(fd, DMA_BUF_SYNC_END | dir)
}