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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#![allow(dead_code)]

use modular_bitfield_msb::prelude::*;

use crate::{
    command::bitfield_bound_check,
    data_wrapper::{AnyType, VecBufferWrapper},
    result_data::ResultData,
    Command, DataDirection, Scsi,
};

#[derive(Clone, Debug)]
pub struct ReadBufferCommand<'a> {
    interface: &'a Scsi,
    mode_specific: u8,
    mode: u8,
    buffer_offset: u64,
    allocation_length: u32,
    buffer_id: u8,
    control: u8,
}

impl<'a> ReadBufferCommand<'a> {
    fn new(interface: &'a Scsi) -> Self {
        Self {
            interface,
            mode_specific: 0,
            mode: 0,
            buffer_offset: 0,
            allocation_length: 0,
            buffer_id: 0,
            control: 0,
        }
    }

    // mode_specific must be less than 0x08
    pub fn mode_specific(&mut self, value: u8) -> &mut Self {
        self.mode_specific = value;
        self
    }

    // mode must be less than 0x20
    pub fn mode(&mut self, value: u8) -> &mut Self {
        self.mode = value;
        self
    }

    // buffer_offset must be less than 0xFF_FFFF for issue_10
    pub fn buffer_offset(&mut self, value: u64) -> &mut Self {
        self.buffer_offset = value;
        self
    }

    // allocation_length must be less than 0xFF_FFFF for issue_10
    pub fn allocation_length(&mut self, value: u32) -> &mut Self {
        self.allocation_length = value;
        self
    }

    pub fn buffer_id(&mut self, value: u8) -> &mut Self {
        self.buffer_id = value;
        self
    }

    pub fn control(&mut self, value: u8) -> &mut Self {
        self.control = value;
        self
    }

    fn error_check(
        &self,
        buffer_offset_bits: u32,
        allocation_length_bits: u32,
    ) -> crate::Result<()> {
        bitfield_bound_check!(self.mode_specific, 3, "mode specific")?;
        bitfield_bound_check!(self.mode, 5, "mode")?;
        bitfield_bound_check!(self.buffer_offset, buffer_offset_bits, "buffer offset")?;
        bitfield_bound_check!(
            self.allocation_length,
            allocation_length_bits,
            "allocation length"
        )?;

        Ok(())
    }

    pub fn issue_10(&mut self) -> crate::Result<Vec<u8>> {
        self.error_check(24, 24)?;

        let command_buffer = CommandBuffer10::new()
            .with_operation_code(OPERATION_CODE_10)
            .with_mode_specific(self.mode_specific)
            .with_mode(self.mode)
            .with_buffer_id(self.buffer_id)
            .with_buffer_offset(self.buffer_offset as u32)
            .with_allocation_length(self.allocation_length)
            .with_control(self.control);

        self.interface.issue(&ThisCommand {
            command_buffer,
            allocation_length: self.allocation_length,
        })
    }

    pub fn issue_16(&mut self) -> crate::Result<Vec<u8>> {
        self.error_check(64, 32)?;

        let command_buffer = CommandBuffer16::new()
            .with_operation_code(OPERATION_CODE_16)
            .with_mode_specific(self.mode_specific)
            .with_mode(self.mode)
            .with_buffer_offset(self.buffer_offset)
            .with_allocation_length(self.allocation_length)
            .with_buffer_id(self.buffer_id)
            .with_control(self.control);

        self.interface.issue(&ThisCommand {
            command_buffer,
            allocation_length: self.allocation_length,
        })
    }
}

impl Scsi {
    pub fn read_buffer(&self) -> ReadBufferCommand {
        ReadBufferCommand::new(self)
    }
}

const OPERATION_CODE_10: u8 = 0x3C;
const OPERATION_CODE_16: u8 = 0x9B;

#[bitfield]
#[derive(Clone, Copy)]
struct CommandBuffer10 {
    operation_code: B8,
    mode_specific: B3,
    mode: B5,
    buffer_id: B8,
    buffer_offset: B24,
    allocation_length: B24,
    control: B8,
}

#[bitfield]
#[derive(Clone, Copy)]
struct CommandBuffer16 {
    operation_code: B8,
    mode_specific: B3,
    mode: B5,
    buffer_offset: B64,
    allocation_length: B32,
    buffer_id: B8,
    control: B8,
}

struct ThisCommand<C> {
    command_buffer: C,
    allocation_length: u32,
}

impl<C: Copy> Command for ThisCommand<C> {
    type CommandBuffer = C;

    type DataBuffer = AnyType;

    type DataBufferWrapper = VecBufferWrapper;

    type ReturnType = crate::Result<Vec<u8>>;

    fn direction(&self) -> DataDirection {
        DataDirection::FromDevice
    }

    fn command(&self) -> Self::CommandBuffer {
        self.command_buffer
    }

    fn data(&self) -> Self::DataBufferWrapper {
        unsafe { VecBufferWrapper::with_len(self.allocation_length as usize) }
    }

    fn data_size(&self) -> u32 {
        self.allocation_length
    }

    fn process_result(&self, result: ResultData<Self::DataBufferWrapper>) -> Self::ReturnType {
        result.check_ioctl_error()?;
        result.check_common_error()?;

        Ok(std::mem::take(result.data).0)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::mem::size_of;

    const COMMAND_LENGTH_10: usize = 10;
    const COMMAND_LENGTH_16: usize = 16;

    #[test]
    fn layout_test() {
        assert_eq!(
            size_of::<CommandBuffer10>(),
            COMMAND_LENGTH_10,
            concat!("Size of: ", stringify!(CommandBuffer10))
        );

        assert_eq!(
            size_of::<CommandBuffer16>(),
            COMMAND_LENGTH_16,
            concat!("Size of: ", stringify!(CommandBuffer16))
        );
    }
}