Skip to main content

starnix_modules_goog_chip_info/
lib.rs

1// Copyright 2026 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#![cfg_attr(test, recursion_limit = "256")]
6
7use starnix_core::fs::sysfs::build_device_directory;
8use starnix_core::task::Kernel;
9use starnix_core::vfs::pseudo::stub_bytes_file::StubBytesFile;
10use starnix_logging::bug_ref;
11use starnix_uapi::file_mode::mode;
12
13/// Initializes the stub `/sys/devices/system/goog-chip-info` device in sysfs.
14pub fn goog_chip_info_device_init(kernel: &Kernel) {
15    let registry = &kernel.device_registry;
16    registry.add_subsystemless_device(
17        "goog-chip-info".into(),
18        Some(registry.objects.system_device()),
19        |device, dir| {
20            build_device_directory(device, dir);
21            for name in ["device_table", "dvfs_table", "ids_table"] {
22                dir.entry(
23                    name,
24                    StubBytesFile::new_node_with_data(
25                        bug_ref!("https://fxbug.dev/560212202"),
26                        b"Placeholder",
27                    ),
28                    mode!(IFREG, 0o444),
29                );
30            }
31        },
32    );
33}
34
35#[cfg(test)]
36mod tests {
37    use super::*;
38    use starnix_core::testing::{anon_test_file, spawn_kernel_and_run};
39    use starnix_core::vfs::VecOutputBuffer;
40    use starnix_uapi::open_flags::OpenFlags;
41
42    #[::fuchsia::test]
43    async fn goog_chip_info_device_init_registers_system_device() {
44        spawn_kernel_and_run(async |current_task| {
45            let kernel = current_task.kernel();
46            goog_chip_info_device_init(kernel);
47
48            let root = &kernel.device_registry.objects.root;
49            assert!(root.lookup("devices/system/goog-chip-info/uevent".into()).is_some());
50            for name in ["device_table", "dvfs_table", "ids_table"] {
51                let path = format!("devices/system/goog-chip-info/{name}");
52                let node =
53                    root.lookup(path.as_str().into()).unwrap_or_else(|| panic!("{path} missing"));
54                assert_eq!(node.info().mode, mode!(IFREG, 0o444));
55                let file_ops = node.create_file_ops(current_task, OpenFlags::RDONLY).unwrap();
56                let file = anon_test_file(current_task, file_ops, OpenFlags::RDONLY);
57                let mut buf = VecOutputBuffer::new(16);
58                let n = file.read(current_task, &mut buf).unwrap();
59                assert_eq!(&Vec::from(buf)[..n], b"Placeholder");
60            }
61        })
62        .await;
63    }
64}