Now writing edc to sectors.

This commit is contained in:
Filipe Rodrigues 2021-04-24 07:08:50 +01:00
parent e91f348ca7
commit a67f67bb04
2 changed files with 46 additions and 14 deletions

View File

@ -63,19 +63,21 @@ impl Bytes for Sector {
edc : [0x4 ],
ecc : [0x114],
);
let edc = Edc::from_bytes(bytes.edc).into_ok();
let data = *bytes.data;
// TODO: Verify / correct ecc
// TODO: Verify & correct ecc
// Verify edc
let edc = Edc::from_bytes(bytes.edc).into_ok();
let edc_bytes = &byte_array[0x10..0x818];
if let Err(calculated) = edc.is_valid(edc_bytes) {
return Err(FromBytesError::WrongEdc { found: edc.crc, calculated });
return Err(FromBytesError::WrongEdc {
found: edc.crc,
calculated: calculated.crc,
});
}
Data::Form1(data)
Data::Form1(*bytes.data)
},
true => {
@ -83,12 +85,15 @@ impl Bytes for Sector {
data : [0x914],
edc : [0x4 ],
);
let edc = Edc::from_bytes(bytes.edc).into_ok();
// Verify edc
let edc = Edc::from_bytes(bytes.edc).into_ok();
let edc_bytes = &byte_array[0x10..0x92c];
if let Err(calculated) = edc.is_valid(edc_bytes) {
return Err(FromBytesError::WrongEdc { found: edc.crc, calculated });
return Err(FromBytesError::WrongEdc {
found: edc.crc,
calculated: calculated.crc,
});
}
Data::Form2(*bytes.data)
@ -99,6 +104,13 @@ impl Bytes for Sector {
}
fn to_bytes(&self, bytes: &mut Self::ByteArray) -> Result<(), Self::ToError> {
// Calculate edc before writing
let edc = match self.data {
Data::Form1(_) => Edc::calc_ecc(&bytes[0x10..0x818]),
Data::Form2(_) => Edc::calc_ecc(&bytes[0x10..0x92c]),
};
let bytes = array_split_mut!(bytes,
header: [0x18 ],
rest : [0x918],
@ -114,13 +126,26 @@ impl Bytes for Sector {
ecc : [0x114],
);
// Write the data
*bytes.data = data;
// TODO: Edc, Ecc
// Write the edc
edc.to_bytes(bytes.edc).into_ok();
// TODO: Ecc
},
Data::Form2(_) => {
todo!();
Data::Form2(data) => {
let bytes = array_split_mut!(bytes.rest,
data : [0x914],
edc : [0x4 ],
);
// Write the data
*bytes.data = data;
// Write the edc
edc.to_bytes(bytes.edc).into_ok();
},
}

View File

@ -5,6 +5,7 @@ use byteorder::{ByteOrder, LittleEndian};
use dcb_bytes::Bytes;
/// Error detection
#[derive(PartialEq, Eq, Clone, Copy)]
pub struct Edc {
/// Crc
pub crc: u32,
@ -35,18 +36,24 @@ impl Edc {
table
}
/// Checks if `bytes` is valid.
pub fn is_valid(&self, bytes: &[u8]) -> Result<(), u32> {
/// Calculates the `ecc` of some bytes
#[must_use]
pub fn calc_ecc(bytes: &[u8]) -> Self {
let mut crc = 0;
#[allow(clippy::as_conversions)]
for &b in bytes {
let idx = (crc ^ u32::from(b)) & 0xFF;
crc = (crc >> 8u32) ^ Self::CRC_TABLE[idx as usize];
}
Self { crc }
}
match crc == self.crc {
/// Checks if `bytes` is valid.
pub fn is_valid(self, bytes: &[u8]) -> Result<(), Self> {
let ecc = Self::calc_ecc(bytes);
match ecc == self {
true => Ok(()),
false => Err(crc),
false => Err(ecc),
}
}
}