Andrew Walbran | d0ef400 | 2022-05-16 16:14:10 +0000 | [diff] [blame^] | 1 | // Copyright 2022, 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 std::fmt::{self, Debug, Display, Formatter}; |
| 16 | use android_system_virtualizationservice::{ |
| 17 | aidl::android::system::virtualizationservice::{ |
| 18 | DeathReason::DeathReason as AidlDeathReason}}; |
| 19 | |
| 20 | /// The reason why a VM died. |
| 21 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 22 | pub enum DeathReason { |
| 23 | /// VirtualizationService died. |
| 24 | VirtualizationServiceDied, |
| 25 | /// There was an error waiting for the VM. |
| 26 | InfrastructureError, |
| 27 | /// The VM was killed. |
| 28 | Killed, |
| 29 | /// The VM died for an unknown reason. |
| 30 | Unknown, |
| 31 | /// The VM requested to shut down. |
| 32 | Shutdown, |
| 33 | /// crosvm had an error starting the VM. |
| 34 | Error, |
| 35 | /// The VM requested to reboot, possibly as the result of a kernel panic. |
| 36 | Reboot, |
| 37 | /// The VM or crosvm crashed. |
| 38 | Crash, |
| 39 | /// The pVM firmware failed to verify the VM because the public key doesn't match. |
| 40 | PvmFirmwarePublicKeyMismatch, |
| 41 | /// The pVM firmware failed to verify the VM because the instance image changed. |
| 42 | PvmFirmwareInstanceImageChanged, |
| 43 | /// The bootloader failed to verify the VM because the public key doesn't match. |
| 44 | BootloaderPublicKeyMismatch, |
| 45 | /// The bootloader failed to verify the VM because the instance image changed. |
| 46 | BootloaderInstanceImageChanged, |
| 47 | /// VirtualizationService sent a death reason which was not recognised by the client library. |
| 48 | Unrecognised(AidlDeathReason), |
| 49 | } |
| 50 | |
| 51 | impl From<AidlDeathReason> for DeathReason { |
| 52 | fn from(reason: AidlDeathReason) -> Self { |
| 53 | match reason { |
| 54 | AidlDeathReason::INFRASTRUCTURE_ERROR => Self::InfrastructureError, |
| 55 | AidlDeathReason::KILLED => Self::Killed, |
| 56 | AidlDeathReason::UNKNOWN => Self::Unknown, |
| 57 | AidlDeathReason::SHUTDOWN => Self::Shutdown, |
| 58 | AidlDeathReason::ERROR => Self::Error, |
| 59 | AidlDeathReason::REBOOT => Self::Reboot, |
| 60 | AidlDeathReason::CRASH => Self::Crash, |
| 61 | AidlDeathReason::PVM_FIRMWARE_PUBLIC_KEY_MISMATCH => Self::PvmFirmwarePublicKeyMismatch, |
| 62 | AidlDeathReason::PVM_FIRMWARE_INSTANCE_IMAGE_CHANGED => { |
| 63 | Self::PvmFirmwareInstanceImageChanged |
| 64 | } |
| 65 | AidlDeathReason::BOOTLOADER_PUBLIC_KEY_MISMATCH => Self::BootloaderPublicKeyMismatch, |
| 66 | AidlDeathReason::BOOTLOADER_INSTANCE_IMAGE_CHANGED => { |
| 67 | Self::BootloaderInstanceImageChanged |
| 68 | } |
| 69 | _ => Self::Unrecognised(reason), |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | impl Display for DeathReason { |
| 75 | fn fmt(&self, f: &mut Formatter) -> fmt::Result { |
| 76 | let s = match self { |
| 77 | Self::VirtualizationServiceDied => "VirtualizationService died.", |
| 78 | Self::InfrastructureError => "Error waiting for VM to finish.", |
| 79 | Self::Killed => "VM was killed.", |
| 80 | Self::Unknown => "VM died for an unknown reason.", |
| 81 | Self::Shutdown => "VM shutdown cleanly.", |
| 82 | Self::Error => "Error starting VM.", |
| 83 | Self::Reboot => "VM tried to reboot, possibly due to a kernel panic.", |
| 84 | Self::Crash => "VM crashed.", |
| 85 | Self::PvmFirmwarePublicKeyMismatch => { |
| 86 | "pVM firmware failed to verify the VM because the public key doesn't match." |
| 87 | } |
| 88 | Self::PvmFirmwareInstanceImageChanged => { |
| 89 | "pVM firmware failed to verify the VM because the instance image changed." |
| 90 | } |
| 91 | Self::BootloaderPublicKeyMismatch => { |
| 92 | "Bootloader failed to verify the VM because the public key doesn't match." |
| 93 | } |
| 94 | Self::BootloaderInstanceImageChanged => { |
| 95 | "Bootloader failed to verify the VM because the instance image changed." |
| 96 | } |
| 97 | Self::Unrecognised(reason) => { |
| 98 | return write!(f, "Unrecognised death reason {:?}.", reason); |
| 99 | } |
| 100 | }; |
| 101 | f.write_str(s) |
| 102 | } |
| 103 | } |