1*eafedbc7SAlice Ryhl // SPDX-License-Identifier: GPL-2.0 2*eafedbc7SAlice Ryhl 3*eafedbc7SAlice Ryhl // Copyright (C) 2025 Google LLC. 4*eafedbc7SAlice Ryhl 5*eafedbc7SAlice Ryhl use core::mem::MaybeUninit; 6*eafedbc7SAlice Ryhl use core::ops::{Deref, DerefMut}; 7*eafedbc7SAlice Ryhl use kernel::{ 8*eafedbc7SAlice Ryhl transmute::{AsBytes, FromBytes}, 9*eafedbc7SAlice Ryhl uapi::{self, *}, 10*eafedbc7SAlice Ryhl }; 11*eafedbc7SAlice Ryhl 12*eafedbc7SAlice Ryhl macro_rules! pub_no_prefix { 13*eafedbc7SAlice Ryhl ($prefix:ident, $($newname:ident),+ $(,)?) => { 14*eafedbc7SAlice Ryhl $(pub(crate) const $newname: u32 = kernel::macros::concat_idents!($prefix, $newname);)+ 15*eafedbc7SAlice Ryhl }; 16*eafedbc7SAlice Ryhl } 17*eafedbc7SAlice Ryhl 18*eafedbc7SAlice Ryhl pub_no_prefix!( 19*eafedbc7SAlice Ryhl binder_driver_return_protocol_, 20*eafedbc7SAlice Ryhl BR_TRANSACTION, 21*eafedbc7SAlice Ryhl BR_TRANSACTION_SEC_CTX, 22*eafedbc7SAlice Ryhl BR_REPLY, 23*eafedbc7SAlice Ryhl BR_DEAD_REPLY, 24*eafedbc7SAlice Ryhl BR_FAILED_REPLY, 25*eafedbc7SAlice Ryhl BR_FROZEN_REPLY, 26*eafedbc7SAlice Ryhl BR_NOOP, 27*eafedbc7SAlice Ryhl BR_SPAWN_LOOPER, 28*eafedbc7SAlice Ryhl BR_TRANSACTION_COMPLETE, 29*eafedbc7SAlice Ryhl BR_TRANSACTION_PENDING_FROZEN, 30*eafedbc7SAlice Ryhl BR_ONEWAY_SPAM_SUSPECT, 31*eafedbc7SAlice Ryhl BR_OK, 32*eafedbc7SAlice Ryhl BR_ERROR, 33*eafedbc7SAlice Ryhl BR_INCREFS, 34*eafedbc7SAlice Ryhl BR_ACQUIRE, 35*eafedbc7SAlice Ryhl BR_RELEASE, 36*eafedbc7SAlice Ryhl BR_DECREFS, 37*eafedbc7SAlice Ryhl BR_DEAD_BINDER, 38*eafedbc7SAlice Ryhl BR_CLEAR_DEATH_NOTIFICATION_DONE, 39*eafedbc7SAlice Ryhl BR_FROZEN_BINDER, 40*eafedbc7SAlice Ryhl BR_CLEAR_FREEZE_NOTIFICATION_DONE, 41*eafedbc7SAlice Ryhl ); 42*eafedbc7SAlice Ryhl 43*eafedbc7SAlice Ryhl pub_no_prefix!( 44*eafedbc7SAlice Ryhl binder_driver_command_protocol_, 45*eafedbc7SAlice Ryhl BC_TRANSACTION, 46*eafedbc7SAlice Ryhl BC_TRANSACTION_SG, 47*eafedbc7SAlice Ryhl BC_REPLY, 48*eafedbc7SAlice Ryhl BC_REPLY_SG, 49*eafedbc7SAlice Ryhl BC_FREE_BUFFER, 50*eafedbc7SAlice Ryhl BC_ENTER_LOOPER, 51*eafedbc7SAlice Ryhl BC_EXIT_LOOPER, 52*eafedbc7SAlice Ryhl BC_REGISTER_LOOPER, 53*eafedbc7SAlice Ryhl BC_INCREFS, 54*eafedbc7SAlice Ryhl BC_ACQUIRE, 55*eafedbc7SAlice Ryhl BC_RELEASE, 56*eafedbc7SAlice Ryhl BC_DECREFS, 57*eafedbc7SAlice Ryhl BC_INCREFS_DONE, 58*eafedbc7SAlice Ryhl BC_ACQUIRE_DONE, 59*eafedbc7SAlice Ryhl BC_REQUEST_DEATH_NOTIFICATION, 60*eafedbc7SAlice Ryhl BC_CLEAR_DEATH_NOTIFICATION, 61*eafedbc7SAlice Ryhl BC_DEAD_BINDER_DONE, 62*eafedbc7SAlice Ryhl BC_REQUEST_FREEZE_NOTIFICATION, 63*eafedbc7SAlice Ryhl BC_CLEAR_FREEZE_NOTIFICATION, 64*eafedbc7SAlice Ryhl BC_FREEZE_NOTIFICATION_DONE, 65*eafedbc7SAlice Ryhl ); 66*eafedbc7SAlice Ryhl 67*eafedbc7SAlice Ryhl pub_no_prefix!( 68*eafedbc7SAlice Ryhl flat_binder_object_flags_, 69*eafedbc7SAlice Ryhl FLAT_BINDER_FLAG_ACCEPTS_FDS, 70*eafedbc7SAlice Ryhl FLAT_BINDER_FLAG_TXN_SECURITY_CTX 71*eafedbc7SAlice Ryhl ); 72*eafedbc7SAlice Ryhl 73*eafedbc7SAlice Ryhl pub_no_prefix!( 74*eafedbc7SAlice Ryhl transaction_flags_, 75*eafedbc7SAlice Ryhl TF_ONE_WAY, 76*eafedbc7SAlice Ryhl TF_ACCEPT_FDS, 77*eafedbc7SAlice Ryhl TF_CLEAR_BUF, 78*eafedbc7SAlice Ryhl TF_UPDATE_TXN 79*eafedbc7SAlice Ryhl ); 80*eafedbc7SAlice Ryhl 81*eafedbc7SAlice Ryhl pub(crate) use uapi::{ 82*eafedbc7SAlice Ryhl BINDER_TYPE_BINDER, BINDER_TYPE_FD, BINDER_TYPE_FDA, BINDER_TYPE_HANDLE, BINDER_TYPE_PTR, 83*eafedbc7SAlice Ryhl BINDER_TYPE_WEAK_BINDER, BINDER_TYPE_WEAK_HANDLE, 84*eafedbc7SAlice Ryhl }; 85*eafedbc7SAlice Ryhl 86*eafedbc7SAlice Ryhl macro_rules! decl_wrapper { 87*eafedbc7SAlice Ryhl ($newname:ident, $wrapped:ty) => { 88*eafedbc7SAlice Ryhl // Define a wrapper around the C type. Use `MaybeUninit` to enforce that the value of 89*eafedbc7SAlice Ryhl // padding bytes must be preserved. 90*eafedbc7SAlice Ryhl #[derive(Copy, Clone)] 91*eafedbc7SAlice Ryhl #[repr(transparent)] 92*eafedbc7SAlice Ryhl pub(crate) struct $newname(MaybeUninit<$wrapped>); 93*eafedbc7SAlice Ryhl 94*eafedbc7SAlice Ryhl // SAFETY: This macro is only used with types where this is ok. 95*eafedbc7SAlice Ryhl unsafe impl FromBytes for $newname {} 96*eafedbc7SAlice Ryhl // SAFETY: This macro is only used with types where this is ok. 97*eafedbc7SAlice Ryhl unsafe impl AsBytes for $newname {} 98*eafedbc7SAlice Ryhl 99*eafedbc7SAlice Ryhl impl Deref for $newname { 100*eafedbc7SAlice Ryhl type Target = $wrapped; 101*eafedbc7SAlice Ryhl fn deref(&self) -> &Self::Target { 102*eafedbc7SAlice Ryhl // SAFETY: We use `MaybeUninit` only to preserve padding. The value must still 103*eafedbc7SAlice Ryhl // always be valid. 104*eafedbc7SAlice Ryhl unsafe { self.0.assume_init_ref() } 105*eafedbc7SAlice Ryhl } 106*eafedbc7SAlice Ryhl } 107*eafedbc7SAlice Ryhl 108*eafedbc7SAlice Ryhl impl DerefMut for $newname { 109*eafedbc7SAlice Ryhl fn deref_mut(&mut self) -> &mut Self::Target { 110*eafedbc7SAlice Ryhl // SAFETY: We use `MaybeUninit` only to preserve padding. The value must still 111*eafedbc7SAlice Ryhl // always be valid. 112*eafedbc7SAlice Ryhl unsafe { self.0.assume_init_mut() } 113*eafedbc7SAlice Ryhl } 114*eafedbc7SAlice Ryhl } 115*eafedbc7SAlice Ryhl 116*eafedbc7SAlice Ryhl impl Default for $newname { 117*eafedbc7SAlice Ryhl fn default() -> Self { 118*eafedbc7SAlice Ryhl // Create a new value of this type where all bytes (including padding) are zeroed. 119*eafedbc7SAlice Ryhl Self(MaybeUninit::zeroed()) 120*eafedbc7SAlice Ryhl } 121*eafedbc7SAlice Ryhl } 122*eafedbc7SAlice Ryhl }; 123*eafedbc7SAlice Ryhl } 124*eafedbc7SAlice Ryhl 125*eafedbc7SAlice Ryhl decl_wrapper!(BinderNodeDebugInfo, uapi::binder_node_debug_info); 126*eafedbc7SAlice Ryhl decl_wrapper!(BinderNodeInfoForRef, uapi::binder_node_info_for_ref); 127*eafedbc7SAlice Ryhl decl_wrapper!(FlatBinderObject, uapi::flat_binder_object); 128*eafedbc7SAlice Ryhl decl_wrapper!(BinderFdObject, uapi::binder_fd_object); 129*eafedbc7SAlice Ryhl decl_wrapper!(BinderFdArrayObject, uapi::binder_fd_array_object); 130*eafedbc7SAlice Ryhl decl_wrapper!(BinderObjectHeader, uapi::binder_object_header); 131*eafedbc7SAlice Ryhl decl_wrapper!(BinderBufferObject, uapi::binder_buffer_object); 132*eafedbc7SAlice Ryhl decl_wrapper!(BinderTransactionData, uapi::binder_transaction_data); 133*eafedbc7SAlice Ryhl decl_wrapper!( 134*eafedbc7SAlice Ryhl BinderTransactionDataSecctx, 135*eafedbc7SAlice Ryhl uapi::binder_transaction_data_secctx 136*eafedbc7SAlice Ryhl ); 137*eafedbc7SAlice Ryhl decl_wrapper!(BinderTransactionDataSg, uapi::binder_transaction_data_sg); 138*eafedbc7SAlice Ryhl decl_wrapper!(BinderWriteRead, uapi::binder_write_read); 139*eafedbc7SAlice Ryhl decl_wrapper!(BinderVersion, uapi::binder_version); 140*eafedbc7SAlice Ryhl decl_wrapper!(BinderFrozenStatusInfo, uapi::binder_frozen_status_info); 141*eafedbc7SAlice Ryhl decl_wrapper!(BinderFreezeInfo, uapi::binder_freeze_info); 142*eafedbc7SAlice Ryhl decl_wrapper!(BinderFrozenStateInfo, uapi::binder_frozen_state_info); 143*eafedbc7SAlice Ryhl decl_wrapper!(BinderHandleCookie, uapi::binder_handle_cookie); 144*eafedbc7SAlice Ryhl decl_wrapper!(ExtendedError, uapi::binder_extended_error); 145*eafedbc7SAlice Ryhl 146*eafedbc7SAlice Ryhl impl BinderVersion { 147*eafedbc7SAlice Ryhl pub(crate) fn current() -> Self { 148*eafedbc7SAlice Ryhl Self(MaybeUninit::new(uapi::binder_version { 149*eafedbc7SAlice Ryhl protocol_version: BINDER_CURRENT_PROTOCOL_VERSION as _, 150*eafedbc7SAlice Ryhl })) 151*eafedbc7SAlice Ryhl } 152*eafedbc7SAlice Ryhl } 153*eafedbc7SAlice Ryhl 154*eafedbc7SAlice Ryhl impl BinderTransactionData { 155*eafedbc7SAlice Ryhl pub(crate) fn with_buffers_size(self, buffers_size: u64) -> BinderTransactionDataSg { 156*eafedbc7SAlice Ryhl BinderTransactionDataSg(MaybeUninit::new(uapi::binder_transaction_data_sg { 157*eafedbc7SAlice Ryhl transaction_data: *self, 158*eafedbc7SAlice Ryhl buffers_size, 159*eafedbc7SAlice Ryhl })) 160*eafedbc7SAlice Ryhl } 161*eafedbc7SAlice Ryhl } 162*eafedbc7SAlice Ryhl 163*eafedbc7SAlice Ryhl impl BinderTransactionDataSecctx { 164*eafedbc7SAlice Ryhl /// View the inner data as wrapped in `BinderTransactionData`. 165*eafedbc7SAlice Ryhl pub(crate) fn tr_data(&mut self) -> &mut BinderTransactionData { 166*eafedbc7SAlice Ryhl // SAFETY: Transparent wrapper is safe to transmute. 167*eafedbc7SAlice Ryhl unsafe { 168*eafedbc7SAlice Ryhl &mut *(&mut self.transaction_data as *mut uapi::binder_transaction_data 169*eafedbc7SAlice Ryhl as *mut BinderTransactionData) 170*eafedbc7SAlice Ryhl } 171*eafedbc7SAlice Ryhl } 172*eafedbc7SAlice Ryhl } 173*eafedbc7SAlice Ryhl 174*eafedbc7SAlice Ryhl impl ExtendedError { 175*eafedbc7SAlice Ryhl pub(crate) fn new(id: u32, command: u32, param: i32) -> Self { 176*eafedbc7SAlice Ryhl Self(MaybeUninit::new(uapi::binder_extended_error { 177*eafedbc7SAlice Ryhl id, 178*eafedbc7SAlice Ryhl command, 179*eafedbc7SAlice Ryhl param, 180*eafedbc7SAlice Ryhl })) 181*eafedbc7SAlice Ryhl } 182*eafedbc7SAlice Ryhl } 183