ide/
drive.rs

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Implements ATA/ATAPI disk drives
//!

mod atapi_drive;
mod hard_drive;

use crate::DmaType;
use crate::DriveMedia;
use crate::DriveType;
use crate::NewDeviceError;
use atapi_drive::AtapiDrive;
use guestmem::GuestMemory;
use hard_drive::HardDrive;
use ide_resources::IdePath;
use inspect::Inspect;
use inspect::InspectMut;
use std::fmt::Debug;
use std::task::Context;

#[derive(Debug, Copy, Clone, Inspect)]
pub enum DriveRegister {
    ErrorFeatures,
    SectorCount,
    LbaLow,
    LbaMid,
    LbaHigh,
    DeviceHead,
    StatusCmd,
    AlternateStatusDeviceControl,
}

#[derive(InspectMut)]
#[inspect(tag = "device_type")]
#[inspect(extra = "DiskDrive::inspect_status")]
pub(crate) enum DiskDrive {
    #[inspect(transparent)]
    HardDevice(HardDrive),
    #[inspect(transparent)]
    OpticalDevice(AtapiDrive),
}

impl DiskDrive {
    pub fn new(media: DriveMedia, disk_path: IdePath) -> Result<Self, NewDeviceError> {
        match media {
            DriveMedia::HardDrive(device) => {
                Ok(DiskDrive::HardDevice(HardDrive::new(device, disk_path)?))
            }
            DriveMedia::OpticalDrive(device) => {
                Ok(DiskDrive::OpticalDevice(AtapiDrive::new(device, disk_path)))
            }
        }
    }

    fn inspect_status(&mut self, resp: &mut inspect::Response<'_>) {
        resp.field(
            "status",
            crate::protocol::Status::from_bits(
                self.read_register(DriveRegister::AlternateStatusDeviceControl),
            ),
        );
    }

    pub fn reset(&mut self) {
        match self {
            DiskDrive::HardDevice(device) => device.reset(),
            DiskDrive::OpticalDevice(device) => device.reset(),
        }
    }
    pub fn drive_type(&self) -> DriveType {
        match self {
            DiskDrive::HardDevice(_) => DriveType::Hard,
            DiskDrive::OpticalDevice(_) => DriveType::Optical,
        }
    }
    pub fn handle_read_dma_descriptor_error(&mut self) -> bool {
        match self {
            DiskDrive::HardDevice(device) => device.handle_read_dma_descriptor_error(),
            DiskDrive::OpticalDevice(device) => device.handle_read_dma_descriptor_error(),
        }
    }
    pub fn read_register(&mut self, register: DriveRegister) -> u8 {
        match self {
            DiskDrive::HardDevice(device) => device.read_register(register),
            DiskDrive::OpticalDevice(device) => device.read_register(register),
        }
    }
    pub fn write_register(&mut self, register: DriveRegister, data: u8) {
        match self {
            DiskDrive::HardDevice(device) => device.write_register(register, data),
            DiskDrive::OpticalDevice(device) => device.write_register(register, data),
        }
    }
    pub(crate) fn pio_read(&mut self, data: &mut [u8]) {
        match self {
            DiskDrive::HardDevice(device) => device.pio_read(data),
            DiskDrive::OpticalDevice(device) => device.pio_read(data),
        }
    }
    pub(crate) fn pio_write(&mut self, data: &[u8]) {
        match self {
            DiskDrive::HardDevice(device) => device.pio_write(data),
            DiskDrive::OpticalDevice(device) => device.pio_write(data),
        }
    }
    pub fn interrupt_pending(&self) -> bool {
        match self {
            DiskDrive::HardDevice(device) => device.interrupt_pending(),
            DiskDrive::OpticalDevice(device) => device.interrupt_pending(),
        }
    }
    pub fn dma_request(&self) -> Option<(&DmaType, usize)> {
        match self {
            DiskDrive::HardDevice(device) => device.dma_request(),
            DiskDrive::OpticalDevice(device) => device.dma_request(),
        }
    }
    pub fn dma_transfer(&mut self, guest_memory: &GuestMemory, gpa: u64, len: usize) {
        match self {
            DiskDrive::HardDevice(device) => device.dma_transfer(guest_memory, gpa, len),
            DiskDrive::OpticalDevice(device) => device.dma_transfer(guest_memory, gpa, len),
        }
    }
    pub fn dma_advance_buffer(&mut self, len: usize) {
        match self {
            DiskDrive::HardDevice(device) => device.dma_advance_buffer(len),
            DiskDrive::OpticalDevice(device) => device.dma_advance_buffer(len),
        }
    }
    pub fn set_prd_exhausted(&mut self) {
        match self {
            DiskDrive::HardDevice(device) => device.set_prd_exhausted(),
            DiskDrive::OpticalDevice(_) => {} // Only needed for enlightened operations on hard drives
        }
    }
    pub fn poll_device(&mut self, cx: &mut Context<'_>) {
        match self {
            DiskDrive::HardDevice(device) => device.poll_device(cx),
            DiskDrive::OpticalDevice(device) => device.poll_device(cx),
        }
    }
}

pub mod save_restore {
    use self::state::SavedDriveState;
    use super::*;
    use vmcore::save_restore::RestoreError;
    use vmcore::save_restore::SaveError;

    pub trait DriveSaveRestore {
        fn save(&self) -> Result<SavedDriveState, SaveError>;
        fn restore(&mut self, state: SavedDriveState) -> Result<(), RestoreError>;
    }

    pub mod state {
        use crate::drive::atapi_drive::save_restore::state::SavedAtapiDriveState;
        use crate::drive::hard_drive::save_restore::state::SavedHardDriveState;
        use mesh::payload::Protobuf;

        #[derive(Protobuf)]
        #[mesh(package = "storage.ide.device")]
        pub enum SavedDriveState {
            #[mesh(1)]
            HardDevice(SavedHardDriveState),
            #[mesh(2)]
            OpticalDevice(SavedAtapiDriveState),
        }
    }

    impl DriveSaveRestore for DiskDrive {
        fn save(&self) -> Result<SavedDriveState, SaveError> {
            match self {
                DiskDrive::HardDevice(device) => Ok(SavedDriveState::HardDevice(device.save()?)),
                DiskDrive::OpticalDevice(device) => {
                    Ok(SavedDriveState::OpticalDevice(device.save()?))
                }
            }
        }
        fn restore(&mut self, state: SavedDriveState) -> Result<(), RestoreError> {
            match self {
                DiskDrive::HardDevice(device) => match state {
                    SavedDriveState::HardDevice(state) => device.restore(state),
                    _ => unreachable!(),
                },
                DiskDrive::OpticalDevice(device) => match state {
                    SavedDriveState::OpticalDevice(state) => device.restore(state),
                    _ => unreachable!(),
                },
            }
        }
    }
}