|
1 |
| -use super::{super::RomOutputLen, key1, Contents}; |
| 1 | +use super::{super::RomOutputLen, is_valid_size, key1, Contents}; |
2 | 2 | use crate::{
|
3 | 3 | cpu::arm7,
|
4 | 4 | utils::{make_zero, zero, ByteMutSlice, Bytes, Savestate},
|
| 5 | + Model, |
5 | 6 | };
|
6 |
| -use core::fmt; |
7 | 7 |
|
8 | 8 | #[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
9 | 9 | pub enum CreationError {
|
10 |
| - SizeNotPowerOfTwo, |
11 |
| - SizeTooSmall, |
12 |
| -} |
13 |
| - |
14 |
| -impl fmt::Display for CreationError { |
15 |
| - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
16 |
| - match self { |
17 |
| - CreationError::SizeNotPowerOfTwo => f.write_str("ROM size not power of 2"), |
18 |
| - CreationError::SizeTooSmall => f.write_str("ROM size too small"), |
19 |
| - } |
20 |
| - } |
| 10 | + InvalidSize, |
21 | 11 | }
|
22 | 12 |
|
23 | 13 | #[derive(Clone, Copy, PartialEq, Eq, Savestate)]
|
@@ -47,19 +37,17 @@ pub struct Normal {
|
47 | 37 |
|
48 | 38 | impl Normal {
|
49 | 39 | /// # Errors
|
50 |
| - /// - [`CreationError::SizeNotPowerOfTwo`](CreationError::SizeNotPowerOfTwo): the ROM contents' |
51 |
| - /// size is not a power of two. |
| 40 | + /// - [`CreationError::InvalidSize`](CreationError::InvalidSize): the ROM contents' size is |
| 41 | + /// either not a power of two or too small. |
52 | 42 | pub fn new(
|
53 | 43 | contents: Box<dyn Contents>,
|
54 | 44 | arm7_bios: Option<&Bytes<{ arm7::BIOS_SIZE }>>,
|
| 45 | + model: Model, |
55 | 46 | #[cfg(feature = "log")] logger: slog::Logger,
|
56 | 47 | ) -> Result<Self, CreationError> {
|
57 | 48 | let len = contents.len();
|
58 |
| - if !len.is_power_of_two() { |
59 |
| - return Err(CreationError::SizeNotPowerOfTwo); |
60 |
| - } |
61 |
| - if len < 0x200 { |
62 |
| - return Err(CreationError::SizeTooSmall); |
| 49 | + if !is_valid_size(len, model) { |
| 50 | + return Err(CreationError::InvalidSize); |
63 | 51 | }
|
64 | 52 | let rom_mask = (len - 1) as u32;
|
65 | 53 | let chip_id = 0x0000_00C2
|
@@ -160,7 +148,10 @@ impl super::RomDevice for Normal {
|
160 | 148 | let Some(mut secure_area) = self.contents.secure_area_mut() else {
|
161 | 149 | return Ok(());
|
162 | 150 | };
|
163 |
| - let key_buf = self.key_buf.as_ref().unwrap(); |
| 151 | + let key_buf = self |
| 152 | + .key_buf |
| 153 | + .as_ref() |
| 154 | + .expect("key_buf should be initialized"); |
164 | 155 | if secure_area.read_le::<u64>(0) == 0xE7FF_DEFF_E7FF_DEFF {
|
165 | 156 | secure_area[..8].copy_from_slice(b"encryObj");
|
166 | 157 | let level_3_key_buf = key_buf.level_3::<2>();
|
@@ -244,7 +235,7 @@ impl super::RomDevice for Normal {
|
244 | 235 | let res = self
|
245 | 236 | .key_buf
|
246 | 237 | .as_ref()
|
247 |
| - .unwrap() |
| 238 | + .expect("key_buf should be initialized") |
248 | 239 | .decrypt_64_bit([cmd.read_be(4), cmd.read_be(0)]);
|
249 | 240 | cmd.write_be(4, res[0]);
|
250 | 241 | cmd.write_be(0, res[1]);
|
|
0 commit comments