cat-dev 0.0.13

A library for interacting with the CAT-DEV hardware units distributed by Nintendo (i.e. a type of Wii-U DevKits).
Documentation
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
//! The server implementation for handling ATAPI Emulation.

use crate::{
	errors::{APIError, CatBridgeError, FSError},
	fsemul::{HostFilesystem, dlf::DiskLayoutFile},
	net::{
		DEFAULT_CAT_DEV_CHUNK_SIZE, DEFAULT_CAT_DEV_SLOWDOWN,
		additions::{RequestIDLayer, StreamIDLayer},
		server::{
			Router, TCPServer,
			requestable::{Body, State},
		},
	},
};
use bytes::{BufMut, Bytes, BytesMut};
use local_ip_address::local_ip;
use std::{
	net::{IpAddr, Ipv4Addr, SocketAddrV4},
	time::Duration,
};
use tokio::{
	fs::{File, read as fs_read},
	io::{AsyncReadExt, AsyncSeekExt, SeekFrom},
};
use tower::ServiceBuilder;
use tracing::debug;
use valuable::{Fields, NamedField, NamedValues, StructDef, Structable, Valuable, Value, Visit};

/// The default port to use for hosting the ATAPI Server.
pub const DEFAULT_ATAPI_PORT: u16 = 7974_u16;

/// A builder that is capable of building a brand new ATAPI server.
///
/// This is a small wrapper that tries to make setting the (many) amount of
/// options easier. The shortest version you can do is call
/// [`AtapiServerBuilder::new`], and [`AtapiServerBuilder::build`].
#[derive(Clone, Debug)]
pub struct AtapiServerBuilder {
	/// The explicit bind address if we don't want to use the local address.
	address: Option<Ipv4Addr>,
	/// Allow overriding the amount of time that we wait to make sure everything
	/// is safe for a cat-dev.
	///
	/// *note: `None` does not mean cat-dev sleep is disabled, that is controleld
	/// through [`Self::fully_disable_cat_dev_sleep`]*.
	cat_dev_sleep_override: Option<Duration>,
	/// Override the chunking value, and how many bytes we'll purposefully chunk on
	/// when sending out TCP data.
	///
	/// *note: `None` does not mean cat-dev sleep is disabled, that is controleld
	/// through [`Self::fully_disable_chunk_override`]*.
	chunk_override: Option<usize>,
	/// An override to fully disable any cat-dev sleeping to prevent issues.
	fully_disable_cat_dev_sleep: bool,
	/// An override to fully disable any TCP level chunking.
	fully_disable_chunk_override: bool,
	/// The host filesystem to treat when serving ATAPI requests.
	host_filesystem: HostFilesystem,
	/// The explicit port to bind on, when not using default.
	port: Option<u16>,
	/// If we should trace when in debug mode.
	trace_during_debug: bool,
}

impl AtapiServerBuilder {
	/// Create a new ATAPI Server builder.
	#[must_use]
	pub const fn new(host_filesystem: HostFilesystem) -> Self {
		Self {
			address: None,
			cat_dev_sleep_override: None,
			chunk_override: None,
			fully_disable_cat_dev_sleep: false,
			fully_disable_chunk_override: false,
			host_filesystem,
			port: None,
			trace_during_debug: false,
		}
	}

	#[must_use]
	pub const fn address(&self) -> Option<Ipv4Addr> {
		self.address
	}
	#[must_use]
	pub const fn set_address(mut self, new_address: Option<Ipv4Addr>) -> Self {
		self.address = new_address;
		self
	}

	#[must_use]
	pub const fn cat_dev_sleep_override(&self) -> Option<Duration> {
		self.cat_dev_sleep_override
	}
	#[must_use]
	pub const fn set_cat_dev_sleep_override(mut self, new_override: Option<Duration>) -> Self {
		self.cat_dev_sleep_override = new_override;
		self
	}

	#[must_use]
	pub const fn chunk_override(&self) -> Option<usize> {
		self.chunk_override
	}
	#[must_use]
	pub const fn set_chunk_override(mut self, new_override: Option<usize>) -> Self {
		self.chunk_override = new_override;
		self
	}

	#[must_use]
	pub const fn fully_disable_cat_dev_sleep(&self) -> bool {
		self.fully_disable_cat_dev_sleep
	}
	#[must_use]
	pub const fn set_fully_disable_cat_dev_sleep(mut self, new_value: bool) -> Self {
		self.fully_disable_cat_dev_sleep = new_value;
		self
	}

	#[must_use]
	pub const fn fully_disable_chunking(&self) -> bool {
		self.fully_disable_chunk_override
	}
	#[must_use]
	pub const fn set_fully_disable_chunking(mut self, disable_chunk: bool) -> Self {
		self.fully_disable_chunk_override = disable_chunk;
		self
	}

	#[must_use]
	pub const fn host_filesystem(&self) -> &HostFilesystem {
		&self.host_filesystem
	}
	#[must_use]
	pub fn set_host_filesystem(mut self, new: HostFilesystem) -> Self {
		self.host_filesystem = new;
		self
	}

	#[must_use]
	pub const fn port(&self) -> Option<u16> {
		self.port
	}
	#[must_use]
	pub const fn set_port(mut self, new: Option<u16>) -> Self {
		self.port = new;
		self
	}

	#[must_use]
	pub const fn trace_during_debug(&self) -> bool {
		self.trace_during_debug
	}
	#[must_use]
	pub const fn set_trace_during_debug(mut self, trace: bool) -> Self {
		self.trace_during_debug = trace;
		self
	}

	/// Build this server, and get a TCP server that you can bind and use.
	///
	/// ## Errors
	///
	/// If we cannot find a host ip to bind too, or cannot spin up the workers for
	/// the server.
	pub async fn build(self) -> Result<TCPServer<HostFilesystem>, CatBridgeError> {
		let ip = self
			.address
			.or_else(|| {
				local_ip().ok().map(|ip| match ip {
					IpAddr::V4(v4) => v4,
					IpAddr::V6(_v6) => unreachable!(),
				})
			})
			.ok_or(APIError::NoHostIpFound)?;
		let bound_address = SocketAddrV4::new(ip, self.port.unwrap_or(DEFAULT_ATAPI_PORT));

		let mut router = Router::<HostFilesystem>::new();
		router.fallback_handler(temporary_fallback_handle_all)?;

		let mut server = TCPServer::new_with_state(
			"atapi",
			bound_address,
			router,
			(None, None),
			12,
			self.host_filesystem,
			self.trace_during_debug,
		)
		.await?;
		if self.trace_during_debug {
			server.layer_initial_service(
				ServiceBuilder::new()
					.layer(RequestIDLayer::new("atapi".to_owned()))
					.layer(StreamIDLayer),
			);
		} else {
			server.layer_initial_service(
				ServiceBuilder::new().layer(RequestIDLayer::new("atapi".to_owned())),
			);
		}
		server.set_chunk_output_at_size(if self.fully_disable_chunk_override {
			None
		} else if let Some(over_ride) = self.chunk_override {
			Some(over_ride)
		} else {
			Some(DEFAULT_CAT_DEV_CHUNK_SIZE)
		});
		server.set_cat_dev_slowdown(if self.fully_disable_cat_dev_sleep {
			None
		} else if let Some(over_ride) = self.cat_dev_sleep_override {
			Some(over_ride)
		} else {
			Some(DEFAULT_CAT_DEV_SLOWDOWN)
		});

		Ok(server)
	}
}

const ATAPI_SERVER_BUILDER_FIELDS: &[NamedField<'static>] = &[
	NamedField::new("address"),
	NamedField::new("cat_dev_sleep_override"),
	NamedField::new("chunk_override"),
	NamedField::new("fully_disable_cat_dev_sleep"),
	NamedField::new("fully_disable_chunk_override"),
	NamedField::new("host_filesystem"),
	NamedField::new("port"),
	NamedField::new("trace_during_debug"),
];

impl Structable for AtapiServerBuilder {
	fn definition(&self) -> StructDef<'_> {
		StructDef::new_static(
			"AtapiServerBuilder",
			Fields::Named(ATAPI_SERVER_BUILDER_FIELDS),
		)
	}
}

impl Valuable for AtapiServerBuilder {
	fn as_value(&self) -> Value<'_> {
		Value::Structable(self)
	}

	fn visit(&self, visitor: &mut dyn Visit) {
		visitor.visit_named_fields(&NamedValues::new(
			ATAPI_SERVER_BUILDER_FIELDS,
			&[
				Valuable::as_value(
					&self
						.address
						.map_or_else(|| "<none>".to_owned(), |ip| format!("{ip}")),
				),
				Valuable::as_value(
					&self
						.cat_dev_sleep_override
						.map_or_else(|| "<none>".to_owned(), |dur| format!("{}s", dur.as_secs())),
				),
				Valuable::as_value(&self.chunk_override),
				Valuable::as_value(&self.fully_disable_cat_dev_sleep),
				Valuable::as_value(&self.fully_disable_chunk_override),
				Valuable::as_value(&self.host_filesystem),
				Valuable::as_value(&self.port),
				Valuable::as_value(&self.trace_during_debug),
			],
		));
	}
}

async fn temporary_fallback_handle_all(
	State(fs): State<HostFilesystem>,
	Body(packet): Body<Bytes>,
) -> Result<Option<Bytes>, CatBridgeError> {
	match &packet[..2] {
		[0x3, _] => {
			debug!("Would have sent 32 bytes of various descriptions back... not sure which...");
		}
		[0xCF, 0x80] => {
			debug!("ATAPI Event packet sent");
		}
		[0xF0, _] => {
			return Ok(Some(Bytes::from(vec![0x0; 4])));
		}
		[0xF1, 0x00 | 0x02] => {
			// I think this is just random data?
			return Ok(Some(Bytes::from(vec![0x69; 32])));
		}
		[0xF1, 0x01 | 0x03] => {
			debug!("Unknown 0xF1 packet, doesn't do anything on the network...");
		}
		[0xF2, _] => {
			debug!("Got unknown 0xF2 packet: [{packet:02X?}]");
		}
		[0xF3, 0x00] => {
			return handle_read_dlf(packet, &fs).await;
		}
		[0xF3, 0x01] => {
			let mut data = BytesMut::with_capacity(32);
			data.extend_from_slice(b"PC SATA EMUL");
			data.extend_from_slice(&[0_u8; 20]);
			return Ok(Some(data.freeze()));
		}
		[0xF3, 0x02 | 0x03] | [0xF5 | 0xF7, _] => {
			debug!("Sending empty 32 bytes!");
			return Ok(Some(Bytes::from(vec![0x0; 32])));
		}
		[0xF6, _] => {
			if packet[1] & 3 != 0 {
				debug!("F6 second byte & 3 != 0, not sending reply!");
			} else {
				let mut data = BytesMut::with_capacity(4);
				data.put_u32_le(1);
				debug!("Sent F6 reply!");
				return Ok(Some(data.freeze()));
			}
		}
		[0x12, _] => {
			debug!("Would have sent 96 bytes of various descriptions back... not sure which...");
		}
		_ => {}
	}

	Ok(None)
}

async fn handle_read_dlf(
	packet: Bytes,
	host_filesystem: &HostFilesystem,
) -> Result<Option<Bytes>, CatBridgeError> {
	let read_address = u128::from(u32::from_be_bytes([
		packet[0x4],
		packet[0x5],
		packet[0x6],
		packet[0x7],
	])) << 11_u128;
	let read_length = u128::from(u32::from_be_bytes([
		packet[0x8],
		packet[0x9],
		packet[0xA],
		packet[0xB],
	])) << 11_u128;
	let rl_as_usize =
		usize::try_from(read_length).map_err(|_| CatBridgeError::UnsupportedBitsPerCore)?;

	debug!(
		atapi.packet_type = "read_address",
		atapi.read_address.address = %read_address,
		atapi.read_address.length = %read_length,
		"Handling atapi read request!"
	);

	let bytes_of_dlf = fs_read(host_filesystem.ppc_boot_dlf_path().await?)
		.await
		.map_err(FSError::from)?;
	let dlf = DiskLayoutFile::try_from(Bytes::from(bytes_of_dlf))?;

	if let Some((path, offset)) = dlf.get_path_and_offset_for_file(read_address).await {
		// Read the file contents...
		let buff = {
			let mut handle = File::open(&path).await.map_err(FSError::from)?;
			handle
				.seek(SeekFrom::Start(offset))
				.await
				.map_err(FSError::from)?;

			let mut file_buff = BytesMut::zeroed(rl_as_usize);
			let mut bytes_read = 0;
			while bytes_read < rl_as_usize {
				let read_this_go = handle
					.read(&mut file_buff[bytes_read..])
					.await
					.map_err(FSError::IO)?;
				// EOF, rest of the buff is already 0's, so no need to pad.
				if read_this_go == 0 {
					break;
				}
				bytes_read += read_this_go;
			}

			file_buff
		};

		// Send!
		Ok(Some(buff.freeze()))
	} else {
		Ok(Some(BytesMut::zeroed(rl_as_usize).freeze()))
	}
}

// KNOWN PACKET HEADERS
//
//  - [0x3]
//    - send 32 bytes of various describes, not quite sure
//  - [0x12]
//    - sends 96 bytes, seems to have some random spattering of fields
//  - [0xCF, 0x80] -> Triggers Events in FSEmul, probably just call "EVentTrigger"
//  - [0xF0] -> send back 4, 0x0 bytes
//  - [0xF1]
//    - [0xF1, 0x00]
//      - seems to literally send 32 bytes of random data.... cool
//    - [0xF1, 0x02]
//      - seems to literally send 32 bytes of random data.... cool
//    - [0xF1, 0x01] || [0xF1, 0x03]
//      - seems to do nothing on hthe network
//  - [0xF2]
//    - [0xF2, 0x00]
//    - [0xF2, 0x01]
//    - [0xF2, 0x02]
//    - [0xF2, 0x03]
//      -> ??? calls a dynamically allocated thing
//    - [0xF2, 0x06]
//    - [0xF2, 0x07]
//      -> ??? calls a dynamically allocated thing
//      -> for f207 looks like we don't send _anything_ back by default
//      -> seems some paths check for Dvdroot, so maybe dvd stuff?
//  - [0xF3]
//    - [0xF3, 0x0] -> seems to actually be real "read file"
//      - not quite sure exactly how to parse this yet, but these are examples:
//        - first 4 bytes are "packet id"
//        - second 4 bytes are "read address" (calculate by: cast to u128 `<< 11`)
//        - last 4 bytes are "read length" (calculate by: cast to u128 `<< 11`)
//      - logs seem to indicate we:
//         1. read a dlf file (dlf file is populated in cafe-tmp how get?)
//         2. use that to get a max read address
//         3. read from the file
//         4. then pad
//        see logs below:
//          - `CSataProcessor::could not get lead out from dlffileobj {error code}`
//          - `CSataProcessor::requested read address 0x%I64x is out of bounds.`
//          - `CSataProcessor::could not read from file`
//          - `CSataProcessor::padding`
//          - `CSataProcessor::error writing to MION port`
//          - `CSataProcessor::wrote %d bytes`
//    - [0xF3, 0x1] -> send back "PC SATA EMUL" + 20 0's
//    - [0xF3, 0x2] || [0xF3, 0x3] -> send back 32 0's - seems this is always set to 0. maybe a kind of ping?
//  - [0xF5]
//    - send 32 0's
//  - [0xF6]
//    - second byte &3 != 0 -> doesn't send reply
//    - if not send what looks to be `1` encoded as 4 bytes
//  - [0xF7]
//    - send 32 0's