1 // Copyright 2025, The Android Open Source Project 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 use crate::{ 16 protocol::{ 17 gbl_efi_fastboot::{GblFastbootProtocol, LocalSessionContext}, 18 Protocol, 19 }, 20 utils::RecurringTimer, 21 EfiEntry, 22 }; 23 use core::time::Duration; 24 use fastboot::local_session::LocalSession; 25 use liberror::Result; 26 27 /// Represents a local, usually graphically driven fastboot/bootmenu session. 28 pub struct LocalFastbootSession<'a> { 29 timer: RecurringTimer<'a>, 30 protocol: Protocol<'a, GblFastbootProtocol>, 31 context: LocalSessionContext, 32 } 33 34 impl<'a> LocalFastbootSession<'a> { 35 /// Starts a local fastboot session. start(efi_entry: &'a EfiEntry, timeout: Duration) -> Result<Self>36 pub fn start(efi_entry: &'a EfiEntry, timeout: Duration) -> Result<Self> { 37 let timer = RecurringTimer::new(efi_entry, timeout)?; 38 let protocol = efi_entry 39 .system_table() 40 .boot_services() 41 .find_first_and_open::<GblFastbootProtocol>()?; 42 let context = protocol.start_local_session()?; 43 Ok(Self { timer, protocol, context }) 44 } 45 } 46 47 impl LocalSession for LocalFastbootSession<'_> { update(&mut self, buf: &mut [u8]) -> Result<usize>48 async fn update(&mut self, buf: &mut [u8]) -> Result<usize> { 49 self.timer.wait().await?; 50 let bufsize = self.protocol.update_local_session(&self.context, buf)?; 51 Ok(bufsize) 52 } 53 } 54 55 impl Drop for LocalFastbootSession<'_> { drop(&mut self)56 fn drop(&mut self) { 57 let _ = self.protocol.close_local_session(&self.context); 58 } 59 } 60