vendor_devices/
lib.rs

1// Copyright 2026 The ChromiumOS Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5//! Stub implementation of vendor virtio devices.
6//! Downstream may replace this crate by pointing the `vendor_devices` workspace dependency to a
7//! platform specific crate.
8
9use anyhow::bail;
10use anyhow::Result;
11use argh::FromArgValue;
12use devices::virtio::VirtioDevice;
13use devices::VirtioDeviceArgs;
14use devices::VirtioDeviceModule;
15#[cfg(any(target_os = "android", target_os = "linux"))]
16use jail::JailConfig;
17#[cfg(any(target_os = "android", target_os = "linux"))]
18use minijail::Minijail;
19use serde::Deserialize;
20use serde::Serialize;
21
22#[derive(Serialize, Deserialize)]
23#[serde(rename_all = "kebab-case")]
24/// Replace the "Unsupported" variant in downstream crate with the available platform devices
25pub enum VendorDeviceModule {
26    Unsupported,
27}
28
29impl VirtioDeviceModule for VendorDeviceModule {
30    fn sort_name(&self) -> &'static str {
31        "vendor_device"
32    }
33
34    /// The trait methods should match against the VendorDeviceModule variants and dispatch
35    /// to the module implementation:
36    ///     match self {
37    ///         Self::MyDeviceA(m) => m.create(cx),
38    ///         Self::MyDeviceB(m) => m.create(cx),
39    ///     }
40    fn create(&self, _cx: &mut VirtioDeviceArgs<'_>) -> Result<Box<dyn VirtioDevice>> {
41        bail!("no vendor devices are supported in this build")
42    }
43
44    #[cfg(any(target_os = "android", target_os = "linux"))]
45    fn create_jail(&self, _jail_config: Option<&JailConfig>) -> Result<Option<Minijail>> {
46        bail!("no vendor devices are supported in this build")
47    }
48}
49
50/// Receives a string identifier from the command line and returns the corresponding module
51/// if it exists.
52/// Example implementation:
53///     match value {
54///         "mydev" => Ok(VendorDeviceModule::MyDevice(MyDeviceModule)),
55///         _ => Err(format!("unknown vendor device: {value}")),
56///     }
57impl FromArgValue for VendorDeviceModule {
58    fn from_arg_value(_value: &str) -> std::result::Result<Self, String> {
59        Err("no vendor devices are supported in this build".to_string())
60    }
61}