1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use crate::UefiDevice;
use inspect::Inspect;
use vmcore::vmtime::VmTimeAccess;
use watchdog_core::platform::WatchdogPlatform;
use watchdog_core::WatchdogServices;

#[derive(Inspect)]
pub struct UefiWatchdogServices {
    pub watchdog: WatchdogServices,
}

impl UefiWatchdogServices {
    pub async fn new(
        vmtime: VmTimeAccess,
        platform: Box<dyn WatchdogPlatform>,
        is_restoring: bool,
    ) -> UefiWatchdogServices {
        UefiWatchdogServices {
            watchdog: WatchdogServices::new("uefi-watchdog", vmtime, platform, is_restoring).await,
        }
    }
}

impl UefiDevice {
    pub(crate) fn handle_watchdog_read(&mut self, reg: watchdog_core::Register) -> u32 {
        match self.service.uefi_watchdog.watchdog.read(reg) {
            Ok(val) => val,
            Err(err) => {
                tracelimit::warn_ratelimited!(
                    error = &err as &dyn std::error::Error,
                    "Error while reading from watchdog device"
                );
                !0
            }
        }
    }

    pub(crate) fn handle_watchdog_write(&mut self, reg: watchdog_core::Register, val: u32) {
        match self.service.uefi_watchdog.watchdog.write(reg, val) {
            Ok(()) => (),
            Err(err) => {
                tracelimit::warn_ratelimited!(
                    error = &err as &dyn std::error::Error,
                    "Error while writing to watchdog device"
                );
            }
        }
    }
}

mod save_restore {
    use super::*;
    use vmcore::save_restore::RestoreError;
    use vmcore::save_restore::SaveError;
    use vmcore::save_restore::SaveRestore;

    mod state {
        use mesh::payload::Protobuf;
        use vmcore::save_restore::SaveRestore;
        use vmcore::save_restore::SavedStateRoot;

        #[derive(Protobuf, SavedStateRoot)]
        #[mesh(package = "firmware.uefi.watchdog")]
        pub struct SavedState {
            #[mesh(1)]
            pub inner: <watchdog_core::WatchdogServices as SaveRestore>::SavedState,
        }
    }

    impl SaveRestore for UefiWatchdogServices {
        type SavedState = state::SavedState;

        fn save(&mut self) -> Result<Self::SavedState, SaveError> {
            let saved_state = state::SavedState {
                inner: self.watchdog.save()?,
            };

            Ok(saved_state)
        }

        fn restore(&mut self, state: Self::SavedState) -> Result<(), RestoreError> {
            let state::SavedState { inner } = state;
            self.watchdog.restore(inner)?;
            Ok(())
        }
    }
}