Jaewan Kim | ba8929b | 2023-01-13 11:13:29 +0900 | [diff] [blame] | 1 | // Copyright 2023, 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 | //! Support for the debug policy overlay in pvmfw |
| 16 | |
Jaewan Kim | ba8929b | 2023-01-13 11:13:29 +0900 | [diff] [blame] | 17 | use core::fmt; |
| 18 | use libfdt::FdtError; |
Jaewan Kim | ba8929b | 2023-01-13 11:13:29 +0900 | [diff] [blame] | 19 | |
| 20 | #[derive(Debug, Clone)] |
| 21 | pub enum DebugPolicyError { |
| 22 | /// The provided baseline FDT was invalid or malformed, so cannot access certain node/prop |
| 23 | Fdt(&'static str, FdtError), |
| 24 | /// The provided debug policy FDT was invalid or malformed. |
| 25 | DebugPolicyFdt(&'static str, FdtError), |
| 26 | /// The overlaid result FDT is invalid or malformed, and may be corrupted. |
| 27 | OverlaidFdt(&'static str, FdtError), |
| 28 | } |
| 29 | |
| 30 | impl fmt::Display for DebugPolicyError { |
| 31 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 32 | match self { |
| 33 | Self::Fdt(s, e) => write!(f, "Invalid baseline FDT. {s}: {e}"), |
| 34 | Self::DebugPolicyFdt(s, e) => write!(f, "Invalid overlay FDT. {s}: {e}"), |
| 35 | Self::OverlaidFdt(s, e) => write!(f, "Invalid overlaid FDT. {s}: {e}"), |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | /// Applies the debug policy device tree overlay to the pVM DT. |
| 41 | /// |
| 42 | /// # Safety |
| 43 | /// |
| 44 | /// When an error is returned by this function, the input `Fdt` should be |
| 45 | /// discarded as it may have have been partially corrupted during the overlay |
| 46 | /// application process. |
| 47 | unsafe fn apply_debug_policy( |
| 48 | fdt: &mut libfdt::Fdt, |
| 49 | debug_policy: &mut [u8], |
| 50 | ) -> Result<(), DebugPolicyError> { |
| 51 | let overlay = libfdt::Fdt::from_mut_slice(debug_policy) |
| 52 | .map_err(|e| DebugPolicyError::DebugPolicyFdt("Failed to load debug policy overlay", e))?; |
| 53 | |
| 54 | fdt.unpack().map_err(|e| DebugPolicyError::Fdt("Failed to unpack", e))?; |
| 55 | |
| 56 | let fdt = fdt |
| 57 | .apply_overlay(overlay) |
| 58 | .map_err(|e| DebugPolicyError::DebugPolicyFdt("Failed to apply overlay", e))?; |
| 59 | |
| 60 | fdt.pack().map_err(|e| DebugPolicyError::OverlaidFdt("Failed to re-pack", e)) |
| 61 | } |
| 62 | |
Jaewan Kim | ba8929b | 2023-01-13 11:13:29 +0900 | [diff] [blame] | 63 | /// Handles debug policies. |
| 64 | /// |
| 65 | /// # Safety |
| 66 | /// |
| 67 | /// This may corrupt the input `Fdt` when overlaying debug policy or applying |
| 68 | /// ramdump configuration. |
| 69 | pub unsafe fn handle_debug_policy( |
| 70 | fdt: &mut libfdt::Fdt, |
| 71 | debug_policy: Option<&mut [u8]>, |
| 72 | ) -> Result<(), DebugPolicyError> { |
| 73 | if let Some(dp) = debug_policy { |
| 74 | apply_debug_policy(fdt, dp)?; |
| 75 | } |
| 76 | |
Jaewan Kim | c03f661 | 2023-02-20 00:06:26 +0900 | [diff] [blame] | 77 | Ok(()) |
Jaewan Kim | ba8929b | 2023-01-13 11:13:29 +0900 | [diff] [blame] | 78 | } |