• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Google Inc. All rights reserved
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files
6  * (the "Software"), to deal in the Software without restriction,
7  * including without limitation the rights to use, copy, modify, merge,
8  * publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 //! This library provides and registers a little kernel pre threading init function to map desktop
25 //! specific reserved memory nodes from the device tree into the gsc_svc trusted app.
26 
27 #![no_std]
28 
29 use core::ffi::{c_uint, CStr};
30 use core::slice::from_raw_parts;
31 use dtb_service::sys::{dtb_get, NO_ERROR};
32 use libfdt::FdtNode;
33 use rust_support::{
34     init::lk_init_level,
35     ipc::IPC_PORT_ALLOW_TA_CONNECT,
36     ktipc::{ktipc_port_acl, uuid},
37     LK_INIT_HOOK,
38 };
39 
40 /// UUID of the gsc_svc app.
41 const GSC_SVC_UUID: uuid = uuid {
42     time_low: 0x77026d06,
43     time_mid: 0xbe0f,
44     time_hi_and_version: 0x4604,
45     clock_seq_and_node: [0xa6, 0xd5, 0xf7, 0x29, 0x38, 0x8a, 0x44, 0x5b],
46 };
47 const UUIDS: [*const uuid; 1] = [&GSC_SVC_UUID as *const uuid];
48 const KTIPC_PORT_ACL: ktipc_port_acl = ktipc_port_acl {
49     flags: IPC_PORT_ALLOW_TA_CONNECT,
50     uuid_num: UUIDS.len() as u32,
51     uuids: (&UUIDS).as_ptr(),
52     extra_data: core::ptr::null(),
53 };
54 
share_reserved_memory(reserved_mem: &FdtNode, name: &'static CStr)55 fn share_reserved_memory(reserved_mem: &FdtNode, name: &'static CStr) {
56     let node = reserved_mem
57         .next_compatible(name)
58         .expect("Could not get boot param node")
59         .expect("Could not get boot param node");
60     let mut reg_itr =
61         node.reg().expect("Could not get reg address").expect("Could not get reg address");
62     let reg = reg_itr.next().expect("Could not get reg address");
63     vmm_obj_service_rust::share_sized_buffer(
64         reg.addr as *const u8,
65         reg.size.unwrap_or(0) as usize,
66         0,
67         name,
68         &KTIPC_PORT_ACL,
69     )
70     .expect("Could not share boot params");
71     if reg_itr.next().is_some() {
72         log::warn!("Found unexpected address is reg node. Ignoring.");
73     }
74 }
75 
platform_dtb_init_func(_level: c_uint)76 extern "C" fn platform_dtb_init_func(_level: c_uint) {
77     let mut fdt_ptr: *const u8 = core::ptr::null_mut();
78     let mut fdt_size = 0usize;
79     // SAFETY: Neither pointer is retained by dtb_get. dbt_get does not read from *fdt_ptr.
80     let rc = unsafe { dtb_get(&mut fdt_ptr as *mut *const u8, &mut fdt_size) };
81     if rc != NO_ERROR as i32 || fdt_ptr.is_null() {
82         log::error!("Failed to get device tree (rc: {rc}, ptr: {fdt_ptr:p}, size: {fdt_size}).");
83         return;
84     }
85     // SAFETY: Outputs from dtb_get are defined to be static, read only, and span the size returned.
86     // fdt_ptr has already been checked for null.
87     let fdt = unsafe { from_raw_parts(fdt_ptr, fdt_size) };
88 
89     let fdt = libfdt::Fdt::from_slice(fdt).expect("Device tree should be valid");
90     let reserved_mem = fdt
91         .node(c"/reserved-memory")
92         .expect("Could not get reserved memory node")
93         .expect("Could not get reserved memory node");
94 
95     let boot_params = [
96         c"google,open-dice",
97         c"google,session-key-seed",
98         c"google,early-entropy",
99         c"google,auth-token-key-seed",
100     ];
101 
102     for param in boot_params {
103         share_reserved_memory(&reserved_mem, param);
104     }
105 }
106 
107 LK_INIT_HOOK!(platform_dtb_init, platform_dtb_init_func, lk_init_level::LK_INIT_LEVEL_THREADING);
108