1use crate::ContainerStartInfo;
6use anyhow::{Context, Error, anyhow};
7use fidl_fuchsia_ui_composition as fuicomposition;
8use fidl_fuchsia_ui_input3 as fuiinput;
9use fidl_fuchsia_ui_policy as fuipolicy;
10use fidl_fuchsia_ui_views as fuiviews;
11use starnix_consent_sync::init as consent_sync_init;
12use starnix_container_structured_config::Config as ContainerStructuredConfig;
13use starnix_core::device::block::add_mmc_block_device;
14use starnix_core::mm::MlockPinFlavor;
15use starnix_core::task::{CurrentTask, Kernel, KernelFeatures, SystemLimits, ThreadLockupDetector};
16use starnix_core::vfs::FsString;
17use starnix_features::Feature;
18use starnix_logging::log_error;
19use starnix_modules_android_usb::usb_device_init;
20use starnix_modules_ashmem::ashmem_device_init;
21use starnix_modules_boot::booted_device_init;
22use starnix_modules_fastrpc::fastrpc_device_init;
23use starnix_modules_framebuffer::{AspectRatio, Framebuffer};
24use starnix_modules_gpu::gpu_device_init;
25use starnix_modules_gralloc::gralloc_device_init;
26use starnix_modules_hvdcp_opti::hvdcp_opti_init;
27use starnix_modules_input::uinput::register_uinput_device;
28use starnix_modules_input::{
29 DEFAULT_KEYBOARD_DEVICE_ID, DEFAULT_MOUSE_DEVICE_ID, DEFAULT_TOUCH_DEVICE_ID, EventProxyMode,
30 InputDevice, new_input_relay,
31};
32use starnix_modules_kgsl::kgsl_device_init;
33use starnix_modules_magma::magma_device_init;
34use starnix_modules_nanohub::nanohub_device_init;
35use starnix_modules_nmfs::nmfs_init;
36use starnix_modules_perfetto_consumer::start_perfetto_consumer_thread;
37use starnix_modules_thermal::{cooling_device_init, thermal_device_init};
38use starnix_modules_touch_power_policy::TouchPowerPolicyDevice;
39use starnix_modules_wakeup_test::register_wakeup_test_device;
40use starnix_sync::{Locked, Unlocked};
41use starnix_uapi::error;
42use starnix_uapi::errors::Errno;
43
44#[derive(Default, Debug)]
46pub struct Features {
47 pub kernel: KernelFeatures,
49
50 pub selinux: SELinuxFeature,
52
53 pub system_limits: SystemLimits,
55
56 pub ashmem: bool,
58
59 pub boot_notifier: bool,
61
62 pub data_collection_consent_sync: bool,
64
65 pub boot_notifier_cpu_boost: Option<zx::MonotonicDuration>,
67
68 pub framebuffer: bool,
70
71 pub aspect_ratio: Option<AspectRatio>,
73
74 pub enable_visual_debugging: bool,
77
78 pub initial_view_id_annotation: String,
80
81 pub gralloc: bool,
83
84 pub kgsl: bool,
86
87 pub magma_supported_vendors: Option<Vec<u16>>,
89
90 pub gfxstream: bool,
92
93 pub container: bool,
95
96 pub test_data: bool,
98
99 pub custom_artifacts: bool,
101
102 pub android_serialno: bool,
104
105 pub perfetto: Option<FsString>,
107
108 pub rootfs_rw: bool,
110
111 pub network_manager: bool,
113
114 pub nanohub: bool,
116
117 pub fastrpc: bool,
119
120 pub enable_utc_time_adjustment: bool,
121
122 pub thermal: bool,
123
124 pub cooling: Option<Vec<String>>,
127
128 pub android_bootreason: bool,
130
131 pub hvdcp_opti: bool,
132
133 pub additional_mounts: Option<Vec<String>>,
134
135 pub wakeup_test: bool,
137
138 pub mmcblk_stub: bool,
141
142 pub android_usb: bool,
144}
145
146#[derive(Default, Debug, PartialEq)]
147pub struct SELinuxFeature {
148 pub enabled: bool,
150
151 pub options: String,
153
154 pub exceptions: Vec<String>,
156}
157
158impl Features {
159 pub fn record_inspect(&self, parent_node: &fuchsia_inspect::Node) {
160 parent_node.record_child("features", |inspect_node| match self {
161 Features {
162 kernel:
163 KernelFeatures {
164 bpf_v2,
165 enable_suid,
166 io_uring,
167 error_on_failed_reboot,
168 default_uid,
169 default_seclabel,
170 selinux_test_suite,
171 default_ns_mount_options,
172 mlock_always_onfault,
173 mlock_pin_flavor,
174 crash_report_throttling,
175 wifi,
176 cached_zx_map_info_bytes,
177 dirent_cache_size,
178 fake_ion,
179 },
180 system_limits,
181 selinux,
182 ashmem,
183 boot_notifier,
184 boot_notifier_cpu_boost,
185 data_collection_consent_sync,
186 framebuffer,
187 aspect_ratio,
188 enable_visual_debugging,
189 gralloc,
190 kgsl,
191 magma_supported_vendors,
192 gfxstream,
193 container,
194 test_data,
195 custom_artifacts,
196 android_serialno,
197 perfetto,
198 rootfs_rw,
199 network_manager,
200 nanohub,
201 fastrpc,
202 enable_utc_time_adjustment,
203 thermal,
204 cooling,
205 android_bootreason,
206 hvdcp_opti,
207 additional_mounts,
208 wakeup_test,
209 mmcblk_stub,
210 android_usb,
211 initial_view_id_annotation,
212 } => {
213 inspect_node.record_bool("selinux", selinux.enabled);
214 inspect_node.record_bool("ashmem", *ashmem);
215 inspect_node.record_bool("boot_notifier", *boot_notifier);
216 inspect_node.record_string(
217 "initial_view_id_annotation",
218 initial_view_id_annotation.as_str(),
219 );
220 inspect_node
221 .record_bool("data_collection_consent_sync", *data_collection_consent_sync);
222 inspect_node.record_string(
223 "boot_notifier_cpu_boost",
224 boot_notifier_cpu_boost
225 .map(|d| format!("{}s", d.into_seconds()))
226 .unwrap_or_else(|| "none".to_string()),
227 );
228 inspect_node.record_bool("framebuffer", *framebuffer);
229 inspect_node.record_bool("gralloc", *gralloc);
230 inspect_node.record_bool("kgsl", *kgsl);
231 inspect_node.record_string(
232 "magma_supported_vendors",
233 match magma_supported_vendors {
234 Some(vendors) => vendors
235 .iter()
236 .map(|vendor| format!("0x{:x}", vendor))
237 .collect::<Vec<String>>()
238 .join(","),
239 None => "".to_string(),
240 },
241 );
242 inspect_node.record_bool("gfxstream", *gfxstream);
243 inspect_node.record_bool("container", *container);
244 inspect_node.record_bool("test_data", *test_data);
245 inspect_node.record_bool("custom_artifacts", *custom_artifacts);
246 inspect_node.record_bool("android_serialno", *android_serialno);
247 inspect_node.record_string(
248 "aspect_ratio",
249 aspect_ratio
250 .as_ref()
251 .map(|aspect_ratio| {
252 format!("width: {} height: {}", aspect_ratio.width, aspect_ratio.height)
253 })
254 .unwrap_or_default(),
255 );
256 inspect_node.record_string(
257 "perfetto",
258 perfetto.as_ref().map(|p| p.to_string()).unwrap_or_default(),
259 );
260 inspect_node.record_bool("rootfs_rw", *rootfs_rw);
261 inspect_node.record_bool("network_manager", *network_manager);
262 inspect_node.record_bool("nanohub", *nanohub);
263 inspect_node.record_bool("fastrpc", *fastrpc);
264 inspect_node.record_bool("thermal", *thermal);
265 inspect_node.record_string(
266 "cooling",
267 match cooling {
268 Some(devices) => devices.join(","),
269 None => "".to_string(),
270 },
271 );
272 inspect_node.record_bool("android_bootreason", *android_bootreason);
273 inspect_node.record_bool("hvdcp_opti", *hvdcp_opti);
274 inspect_node.record_string("ping_group_range", {
275 let range = system_limits.socket.icmp_ping_gids.lock();
276 std::format!("{},{}", range.start, range.end - 1)
277 });
278
279 inspect_node.record_child("kernel", |kernel_node| {
280 kernel_node.record_bool("bpf_v2", *bpf_v2);
281 kernel_node.record_bool("enable_suid", *enable_suid);
282 kernel_node.record_bool("io_uring", *io_uring);
283 kernel_node.record_bool("error_on_failed_reboot", *error_on_failed_reboot);
284 kernel_node.record_bool("enable_visual_debugging", *enable_visual_debugging);
285 kernel_node.record_int("default_uid", (*default_uid).into());
286 kernel_node.record_string(
287 "default_seclabel",
288 default_seclabel.as_deref().unwrap_or_default(),
289 );
290 kernel_node.record_bool("selinux_test_suite", *selinux_test_suite);
291 kernel_node.record_bool("crash_report_throttling", *crash_report_throttling);
292 kernel_node
293 .record_uint("cached_zx_map_info_bytes", *cached_zx_map_info_bytes as u64);
294 inspect_node.record_string(
295 "default_ns_mount_options",
296 format!("{:?}", default_ns_mount_options),
297 );
298 inspect_node
299 .record_bool("enable_utc_time_adjustment", *enable_utc_time_adjustment);
300 inspect_node.record_bool("mlock_always_onfault", *mlock_always_onfault);
301 inspect_node
302 .record_string("mlock_pin_flavor", format!("{:?}", mlock_pin_flavor));
303 inspect_node.record_bool("wifi", *wifi);
304 inspect_node
305 .record_string("additional_mounts", format!("{:?}", additional_mounts));
306 inspect_node.record_uint("dirent_cache_size", *dirent_cache_size as u64);
307 inspect_node.record_bool("wakeup_test", *wakeup_test);
308 inspect_node.record_bool("mmcblk_stub", *mmcblk_stub);
309 inspect_node.record_bool("android_usb", *android_usb);
310 inspect_node.record_bool("fake_ion", *fake_ion);
311 });
312 }
313 });
314 }
315}
316
317pub fn parse_features(
321 start_info: &ContainerStartInfo,
322 kernel_extra_features: &[String],
323) -> Result<Features, Error> {
324 let ContainerStructuredConfig {
325 crash_report_throttling,
326 enable_utc_time_adjustment,
327 extra_features,
328 cached_zx_map_info_bytes,
329 mlock_always_onfault,
330 mlock_pin_flavor,
331 selinux_exceptions,
332 ui_visual_debugging_level,
333 additional_mounts,
334 dirent_cache_size,
335 initial_view_id_annotation,
336 } = &start_info.config;
337
338 let mut features = Features::default();
339 for entry in start_info
340 .program
341 .features
342 .iter()
343 .chain(kernel_extra_features.iter())
344 .chain(extra_features.iter())
345 {
346 let (feature, raw_args) = Feature::try_parse_feature_and_args(entry)?;
347 match (feature, raw_args) {
348 (Feature::AndroidSerialno, _) => features.android_serialno = true,
349 (Feature::AndroidBootreason, _) => features.android_bootreason = true,
350 (Feature::AspectRatio, Some(args)) => {
351 let e = anyhow!("Invalid aspect_ratio: {:?}", args);
352 let components: Vec<_> = args.split(':').collect();
353 if components.len() != 2 {
354 return Err(e);
355 }
356 let width: u32 =
357 components[0].parse().map_err(|_| anyhow!("Invalid aspect ratio width"))?;
358 let height: u32 =
359 components[1].parse().map_err(|_| anyhow!("Invalid aspect ratio height"))?;
360 features.aspect_ratio = Some(AspectRatio { width, height });
361 }
362 (Feature::AspectRatio, None) => {
363 return Err(anyhow!(
364 "Aspect ratio feature must contain the aspect ratio in the format: aspect_ratio:w:h"
365 ));
366 }
367 (Feature::Container, _) => features.container = true,
368 (Feature::CustomArtifacts, _) => features.custom_artifacts = true,
369 (Feature::Ashmem, _) => features.ashmem = true,
370 (Feature::BootNotifier, _) => features.boot_notifier = true,
371 (Feature::DataCollectionConsentSync, _) => features.data_collection_consent_sync = true,
372 (Feature::BootNotifierCpuBoost, Some(arg)) => {
373 let duration = zx::MonotonicDuration::from_seconds(arg.parse::<i64>()?);
374 features.boot_notifier_cpu_boost = Some(duration);
375 }
376 (Feature::BootNotifierCpuBoost, None) => {
377 return Err(anyhow!(
378 "boot_notifier_cpu_boost feature must have an argument (e.g. \"boot_notifier_cpu_boost:60\")"
379 ));
380 }
381 (Feature::Framebuffer, _) => features.framebuffer = true,
382 (Feature::Gralloc, _) => features.gralloc = true,
383 (Feature::Kgsl, _) => features.kgsl = true,
384 (Feature::Magma, _) => {
385 if features.magma_supported_vendors.is_none() {
386 const VENDOR_ARM: u16 = 0x13B5;
387 const VENDOR_INTEL: u16 = 0x8086;
388 features.magma_supported_vendors = Some(vec![VENDOR_ARM, VENDOR_INTEL])
389 }
390 }
391 (Feature::MagmaSupportedVendors, Some(arg)) => {
392 features.magma_supported_vendors = Some(
393 arg.split(',')
394 .map(|s| {
395 let err = anyhow!(
396 "Feature format must be: magma_supported_vendors:0x1234[,0xabcd]"
397 );
398 let trimmed = s.trim_start_matches("0x");
399 u16::from_str_radix(trimmed, 16).map_err(|_| err)
400 })
401 .collect::<Result<Vec<u16>, Error>>()?,
402 );
403 }
404 (Feature::MagmaSupportedVendors, None) => {
405 return Err(anyhow!(
406 "Feature format must be: magma_supported_vendors:0x1234[,0xabcd]"
407 ));
408 }
409 (Feature::Nanohub, _) => features.nanohub = true,
410 (Feature::Fastrpc, _) => features.fastrpc = true,
411 (Feature::NetworkManager, _) => features.network_manager = true,
412 (Feature::Gfxstream, _) => features.gfxstream = true,
413 (Feature::Bpf, Some(version)) => features.kernel.bpf_v2 = version == "v2",
414 (Feature::Bpf, None) => {
415 return Err(anyhow!("bpf feature must have an argument (e.g. \"bpf:v2\")"));
416 }
417 (Feature::EnableSuid, _) => features.kernel.enable_suid = true,
418 (Feature::IoUring, _) => features.kernel.io_uring = true,
419 (Feature::ErrorOnFailedReboot, _) => features.kernel.error_on_failed_reboot = true,
420 (Feature::Perfetto, Some(socket_path)) => {
421 features.perfetto = Some(socket_path.into());
422 }
423 (Feature::Perfetto, None) => {
424 return Err(anyhow!("Perfetto feature must contain a socket path"));
425 }
426 (Feature::PingGroupRange, Some(arg)) => {
427 let mut args = arg.split(',');
428 let (min, max) = (|| {
429 let min = args.next()?.trim_ascii().parse::<u32>().ok()?;
430 let max = args.next()?.trim_ascii().parse::<u32>().ok()?.checked_add(1)?;
431 if args.next().is_some() {
432 return None;
433 }
434 Some((min, max))
435 })()
436 .ok_or_else(|| anyhow!("Feature format must be: ping_group_range:0,100"))?;
437 *features.system_limits.socket.icmp_ping_gids.lock() = min..max;
438 }
439 (Feature::PingGroupRange, None) => {
440 return Err(anyhow!("Feature format must be: ping_group_range:0,100"));
441 }
442 (Feature::RootfsRw, _) => features.rootfs_rw = true,
443 (Feature::Selinux, arg) => {
444 features.selinux = SELinuxFeature {
445 enabled: true,
446 options: arg.unwrap_or_default(),
447 exceptions: selinux_exceptions.clone(),
448 };
449 }
450 (Feature::SelinuxTestSuite, _) => features.kernel.selinux_test_suite = true,
451 (Feature::TestData, _) => features.test_data = true,
452 (Feature::Thermal, _) => features.thermal = true,
453 (Feature::Cooling, Some(arg)) => {
454 features.cooling = Some(arg.split(',').map(String::from).collect::<Vec<String>>())
455 }
456 (Feature::Cooling, None) => {
457 return Err(anyhow!("cooling feature must have an argument"));
458 }
459 (Feature::HvdcpOpti, _) => features.hvdcp_opti = true,
460 (Feature::Wifi, _) => features.kernel.wifi = true,
461 (Feature::AdditionalMounts, _) => {
462 features.additional_mounts = Some(additional_mounts.clone())
463 }
464 (Feature::WakeupTest, _) => features.wakeup_test = true,
465 (Feature::MmcblkStub, _) => features.mmcblk_stub = true,
466 (Feature::FakeIon, _) => features.kernel.fake_ion = true,
467 (Feature::AndroidUsb, _) => features.android_usb = true,
468 };
469 }
470
471 if features.boot_notifier_cpu_boost.is_some() && !features.boot_notifier {
472 return Err(anyhow!("boot_notifier_cpu_boost feature requires boot_notifier"));
473 }
474
475 if *ui_visual_debugging_level > 0 {
476 features.enable_visual_debugging = true;
477 }
478 features.enable_utc_time_adjustment = *enable_utc_time_adjustment;
479
480 features.kernel.default_uid = start_info.program.default_uid.0;
481 features.kernel.default_seclabel = start_info.program.default_seclabel.clone();
482 features.kernel.default_ns_mount_options =
483 if let Some(mount_options) = &start_info.program.default_ns_mount_options {
484 let options = mount_options
485 .iter()
486 .map(|item| {
487 let mut splitter = item.splitn(2, ":");
488 let key = splitter.next().expect("Failed to parse mount options");
489 let value = splitter.next().expect("Failed to parse mount options");
490 (key.to_string(), value.to_string())
491 })
492 .collect();
493 Some(options)
494 } else {
495 None
496 };
497
498 features.kernel.mlock_always_onfault = *mlock_always_onfault;
499 features.kernel.mlock_pin_flavor = MlockPinFlavor::parse(mlock_pin_flavor.as_str())?;
500 features.kernel.crash_report_throttling = *crash_report_throttling;
501 features.kernel.cached_zx_map_info_bytes = *cached_zx_map_info_bytes;
502 features.kernel.dirent_cache_size = *dirent_cache_size;
503 features.initial_view_id_annotation = initial_view_id_annotation.clone();
504
505 Ok(features)
506}
507
508pub fn run_container_features(
511 locked: &mut Locked<Unlocked>,
512 system_task: &CurrentTask,
513 features: &Features,
514) -> Result<(), Error> {
515 let kernel = system_task.kernel();
516
517 if features.framebuffer {
518 let framebuffer = Framebuffer::device_init(
519 locked,
520 system_task,
521 features.aspect_ratio,
522 features.enable_visual_debugging,
523 features.initial_view_id_annotation.clone(),
524 )
525 .context("initializing framebuffer")?;
526
527 let (touch_source_client, touch_source_server) = fidl::endpoints::create_endpoints();
528 let (mouse_source_client, mouse_source_server) = fidl::endpoints::create_endpoints();
529 let view_bound_protocols = fuicomposition::ViewBoundProtocols {
530 touch_source: Some(touch_source_server),
531 mouse_source: Some(mouse_source_server),
532 ..Default::default()
533 };
534 let view_identity = fuiviews::ViewIdentityOnCreation::from(
535 fuchsia_scenic::ViewRefPair::new().expect("Failed to create ViewRefPair"),
536 );
537 let view_ref = fuchsia_scenic::duplicate_view_ref(&view_identity.view_ref)
538 .expect("Failed to dup view ref.");
539 let keyboard =
540 fuchsia_component::client::connect_to_protocol_sync::<fuiinput::KeyboardMarker>()
541 .expect("Failed to connect to keyboard");
542 let registry_proxy = fuchsia_component::client::connect_to_protocol_sync::<
543 fuipolicy::DeviceListenerRegistryMarker,
544 >()
545 .expect("Failed to connect to device listener registry");
546
547 *framebuffer.view_identity.lock() = Some(view_identity);
556 *framebuffer.view_bound_protocols.lock() = Some(view_bound_protocols);
557
558 let framebuffer_info = framebuffer.info.read();
559
560 let display_width = framebuffer_info.xres as i32;
561 let display_height = framebuffer_info.yres as i32;
562
563 let touch_device =
564 InputDevice::new_touch(display_width, display_height, &kernel.inspect_node);
565 let keyboard_device = InputDevice::new_keyboard(&kernel.inspect_node);
566 let mouse_device = InputDevice::new_mouse(&kernel.inspect_node);
567
568 touch_device.clone().register(
569 locked,
570 &kernel.kthreads.system_task(),
571 DEFAULT_TOUCH_DEVICE_ID,
572 )?;
573 keyboard_device.clone().register(
574 locked,
575 &kernel.kthreads.system_task(),
576 DEFAULT_KEYBOARD_DEVICE_ID,
577 )?;
578 mouse_device.clone().register(
579 locked,
580 &kernel.kthreads.system_task(),
581 DEFAULT_MOUSE_DEVICE_ID,
582 )?;
583
584 let (input_events_relay, input_events_relay_handle) = new_input_relay();
585 input_events_relay.start_relays(
586 &kernel,
587 EventProxyMode::WakeContainer,
588 touch_source_client,
589 keyboard,
590 mouse_source_client,
591 view_ref,
592 registry_proxy,
593 touch_device.open_files.clone(),
594 keyboard_device.open_files.clone(),
595 mouse_device.open_files.clone(),
596 Some(touch_device.inspect_status),
597 Some(keyboard_device.inspect_status),
598 Some(mouse_device.inspect_status),
599 );
600
601 register_uinput_device(locked, &kernel.kthreads.system_task(), input_events_relay_handle)?;
602
603 let (touch_standby_sender, touch_standby_receiver) =
605 ThreadLockupDetector::tracked_channel::<bool>();
606 let touch_policy_device = TouchPowerPolicyDevice::new(touch_standby_sender);
607 touch_policy_device.clone().register(locked, &kernel.kthreads.system_task());
608 touch_policy_device.start_relay(&kernel, touch_standby_receiver);
609
610 framebuffer.start_server(kernel, None);
611 }
612 if features.gralloc {
613 gralloc_device_init(locked, system_task);
623 }
624 if features.kgsl {
625 kgsl_device_init(locked, system_task);
626 }
627 if let Some(supported_vendors) = &features.magma_supported_vendors {
628 magma_device_init(locked, system_task, supported_vendors.clone());
629 }
630 if features.gfxstream {
631 gpu_device_init(locked, system_task);
632 }
633 if let Some(socket_path) = features.perfetto.clone() {
634 start_perfetto_consumer_thread(kernel, socket_path)
635 .context("Failed to start perfetto consumer thread")?;
636 }
637 if features.ashmem {
638 ashmem_device_init(locked, system_task);
639 }
640 if features.boot_notifier {
641 booted_device_init(locked, system_task, features.boot_notifier_cpu_boost);
642 }
643 if features.data_collection_consent_sync {
644 consent_sync_init(locked, system_task);
645 }
646 if features.network_manager {
647 if let Err(e) = nmfs_init(system_task) {
648 log_error!("Network manager initialization failed: ({e:?})");
649 }
650 }
651 if features.nanohub {
652 nanohub_device_init(locked, system_task);
653 }
654 if features.thermal {
655 thermal_device_init(locked, kernel)?;
656 }
657 if let Some(devices) = &features.cooling {
658 cooling_device_init(locked, kernel, devices.clone())?;
659 }
660 if features.hvdcp_opti {
661 hvdcp_opti_init(locked, system_task)?;
662 }
663 if features.fastrpc {
664 fastrpc_device_init(locked, system_task);
665 }
666 if features.wakeup_test {
667 register_wakeup_test_device(locked, system_task)?;
668 }
669 if features.mmcblk_stub {
670 let _device = add_mmc_block_device(locked, system_task)
671 .context("Failed to add stub mmcblk0 device")?;
672 }
673 if features.android_usb {
674 usb_device_init(locked, system_task).context("Failed to add android usb device nodes")?;
675 }
676 Ok(())
677}
678
679pub fn run_component_features(
681 kernel: &Kernel,
682 entries: &Vec<String>,
683 mut incoming_dir: Option<fidl_fuchsia_io::DirectoryProxy>,
684) -> Result<(), Errno> {
685 for entry in entries {
686 match entry.as_str() {
687 "framebuffer" => {
688 Framebuffer::get(kernel)?.start_server(kernel, incoming_dir.take());
689 }
690 feature => {
691 return error!(ENOSYS, format!("Unsupported feature: {}", feature));
692 }
693 }
694 }
695 Ok(())
696}