-
Notifications
You must be signed in to change notification settings - Fork 2
/
satapi.rs
252 lines (206 loc) · 7.1 KB
/
satapi.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#![allow(dead_code)]
use core::mem::MaybeUninit;
use alloc::vec::Vec;
use syscall::Io;
use {
super::{
hba::{
consts::{SCSI_READ10, SCSI_READ_CAP, SCSI_WRITE10},
structs::{HbaCmdHeader, HbaCmdTable, HbaPort},
},
Disk,
},
crate::refactor_hba_int_err,
byteorder::{BigEndian, ByteOrder},
core::ptr::copy,
syscall::error::{Error, Result, EROFS},
};
pub struct SataPacketInterface {
id: usize,
port: &'static mut HbaPort,
len: u64,
cmd_list: [HbaCmdHeader; 32],
tables: [HbaCmdTable; 32],
_fis_base: [u8; 256],
buffer: [u8; 256 * 512],
}
impl SataPacketInterface {
pub fn new(id: usize, port: &'static mut HbaPort) -> Result<Self> {
let mut cmd_list: [_; 32] = (0..32)
.map(|_| HbaCmdHeader::zeroed())
.collect::<Vec<_>>()
.try_into()
.unwrap_or_else(|_| unreachable!());
let mut tables: [_; 32] = (0..32)
.map(|_| HbaCmdTable::zeroed())
.collect::<Vec<_>>()
.try_into()
.unwrap_or_else(|_| unreachable!());
let mut fis_base: [u8; 256] = (0..256)
.map(|_| unsafe { MaybeUninit::zeroed().assume_init() })
.collect::<Vec<_>>()
.try_into()
.unwrap_or_else(|_| unreachable!());
let buffer: [u8; 256 * 512] = (0..256 * 512)
.map(|_| unsafe { MaybeUninit::zeroed().assume_init() })
.collect::<Vec<_>>()
.try_into()
.unwrap_or_else(|_| unreachable!());
port.init(&mut cmd_list, &mut tables, &mut fis_base);
let len = port.identify(&mut cmd_list, &mut tables).unwrap_or(0);
Ok(SataPacketInterface {
id,
port,
len,
cmd_list,
tables,
_fis_base: fis_base,
buffer,
})
}
pub fn read_cap(&mut self) -> Result<[u32; 2]> {
let mut command = [0; 16];
command[0] = SCSI_READ_CAP;
refactor_hba_int_err!(self.port.satapi_dma(
&command,
8,
&mut self.cmd_list,
&mut self.tables,
&mut self.buffer
));
let count = BigEndian::read_u32(&self.buffer[0..4]) + 1;
let blklen = BigEndian::read_u32(&self.buffer[4..8]);
Ok([count, blklen])
}
}
impl Disk for SataPacketInterface {
fn id(&self) -> usize {
self.id
}
fn size(&mut self) -> u64 {
match self.read_cap() {
Ok([count, blklen]) => (count as u64) * (blklen as u64),
Err(_) => 0,
}
}
fn read(&mut self, block: u64, buffer: &mut [u8]) -> syscall::Result<Option<usize>> {
let sector_count = buffer.len() as u32 / self.blklen()?;
fn read10_cmd(blk: u32, count: u16) -> [u8; 16] {
let mut command = [0; 16];
command[0] = SCSI_READ10;
BigEndian::write_u32(&mut command[2..6], blk as u32);
BigEndian::write_u16(&mut command[7..9], count as u16);
command
}
let mut sector = 0;
let len = (256 * 512) / self.blklen()?;
let buffer_size = len * self.blklen()?;
while sector_count - sector >= len {
let command = read10_cmd(block as u32 + sector, (sector_count - sector) as u16);
refactor_hba_int_err!(self.port.satapi_dma(
&command,
buffer_size,
&mut self.cmd_list,
&mut self.tables,
&mut self.buffer
));
unsafe {
copy(
self.buffer.as_ptr(),
buffer
.as_mut_ptr()
.offset(sector as isize * self.blklen()? as isize),
buffer_size as usize,
);
}
sector += self.blklen()?;
}
if sector < sector_count {
let command = read10_cmd(block as u32 + sector, len as u16);
refactor_hba_int_err!(self.port.satapi_dma(
&command,
buffer_size,
&mut self.cmd_list,
&mut self.tables,
&mut self.buffer
));
unsafe {
copy(
self.buffer.as_mut_ptr(),
buffer
.as_mut_ptr()
.offset(sector as isize * self.blklen()? as isize),
buffer_size as usize,
);
}
sector += sector_count - sector;
}
Ok(Some((sector * self.blklen()?) as usize))
}
fn write(&mut self, block: u64, buffer: &mut [u8]) -> syscall::Result<Option<usize>> {
// TODO: figure out how to refactor this as a macro without pissing off the compiler
let sector_count = buffer.len() as u32 / self.blklen()?;
fn write10_cmd(blk: u32, count: u16) -> [u8; 16] {
let mut command = [0; 16];
command[0] = SCSI_WRITE10;
BigEndian::write_u32(&mut command[2..6], blk as u32);
BigEndian::write_u16(&mut command[7..9], count as u16);
command
}
let mut sector = 0;
let len = (256 * 512) / self.blklen()?;
let buffer_size = len * self.blklen()?;
while sector_count - sector >= len {
let command = write10_cmd(block as u32 + sector, (sector_count - sector) as u16);
refactor_hba_int_err!(self.port.satapi_dma(
&command,
buffer_size,
&mut self.cmd_list,
&mut self.tables,
&mut self.buffer
));
unsafe {
copy(
self.buffer.as_ptr(),
buffer
.as_mut_ptr()
.offset(sector as isize * self.blklen()? as isize),
buffer_size as usize,
);
}
sector += self.blklen()?;
}
if sector < sector_count {
let command = write10_cmd(block as u32 + sector, len as u16);
refactor_hba_int_err!(self.port.satapi_dma(
&command,
buffer_size,
&mut self.cmd_list,
&mut self.tables,
&mut self.buffer
));
unsafe {
copy(
self.buffer.as_mut_ptr(),
buffer
.as_mut_ptr()
.offset(sector as isize * self.blklen()? as isize),
buffer_size as usize,
);
}
sector += sector_count - sector;
}
Ok(Some((sector * self.blklen()?) as usize))
}
fn blklen(&mut self) -> syscall::Result<u32> {
Ok(self.read_cap()?[1])
}
fn write_interrupt_status(&mut self, status: u32) {
self.port.interrupt_status.write(status);
}
fn read_interrupt_status(&mut self) -> u32 {
self.port.interrupt_status.read()
}
}
unsafe impl Send for SataPacketInterface {}
unsafe impl Sync for SataPacketInterface {}