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
//! API's for interacting with `/dbg/mem_dump.cgi`, a page for live
//! accessing the memory of the memory on the main chip of the MION
//! devices.
//!
//! Prefer this over `dbytes` over telnet, as there are some bytes that
//! cannot be read over telnet, that can be read with this CGI interface.

use crate::{
	errors::{CatBridgeError, NetworkError, NetworkParseError},
	mion::{
		cgis::{AUTHZ_HEADER, encode_url_parameters},
		proto::cgis::MionCGIErrors,
	},
};
use bytes::{Bytes, BytesMut};
use fnv::FnvHashMap;
use futures::{StreamExt, future::Either};
use reqwest::{Client, Response, Version};
use std::{
	fmt::Display,
	net::Ipv4Addr,
	ops::Deref,
	sync::atomic::{AtomicU8, Ordering as AtomicOrdering},
	time::Duration,
};
use tokio::{
	sync::mpsc::{
		Receiver as BoundedReceiver, Sender as BoundedSender, channel as bounded_channel,
	},
	time::timeout,
};
use tracing::debug;

const MEMORY_MAX_ADDRESS: usize = 0xFFFF_FE00;
const TABLE_START_SIGIL: &str = "<table border=0 cellspacing=3 cellpadding=3>";
const TABLE_END_SIGIL: &str = "</table>";
const MAX_RETRIES: u8 = 10;
const BACKOFF_SLEEP_SECONDS: u64 = 10;
const MEMORY_TIMEOUT_SECONDS: u64 = 30;
const MAX_MEMORY_CONCURRENCY: usize = 4;

/// Dump the existing memory for a MION.
///
/// ## Errors
///
/// - If we cannot encode the parameters as a form url encoded.
/// - If we cannot make the HTTP request.
/// - If the server does not respond with a 200.
/// - If we cannot read the body from HTTP.
/// - If we cannot parse the HTML response.
pub async fn dump_memory(
	mion_ip: Ipv4Addr,
	resume_at: Option<usize>,
	early_stop_at: Option<usize>,
) -> Result<Bytes, CatBridgeError> {
	let mut memory_buffer = BytesMut::with_capacity(0xFFFF_FFFF);
	dump_memory_with_raw_client(
		&Client::default(),
		mion_ip,
		resume_at,
		early_stop_at,
		|bytes: Vec<u8>| {
			memory_buffer.extend_from_slice(&bytes);
		},
	)
	.await?;
	Ok(memory_buffer.freeze())
}

/// Dump the existing memory for a MION allowing you to write as
/// dumps happen.
///
/// ## Errors
///
/// - If we cannot encode the parameters as a form url encoded.
/// - If we cannot make the HTTP request.
/// - If the server does not respond with a 200.
/// - If we cannot read the body from HTTP.
/// - If we cannot parse the HTML response.
pub async fn dump_memory_with_writer<FnTy>(
	mion_ip: Ipv4Addr,
	resume_at: Option<usize>,
	early_stop_at: Option<usize>,
	callback: FnTy,
) -> Result<(), CatBridgeError>
where
	FnTy: FnMut(Vec<u8>) + Send + Sync,
{
	dump_memory_with_raw_client(
		&Client::default(),
		mion_ip,
		resume_at,
		early_stop_at,
		callback,
	)
	.await
}

/// Perform a memory dump request, but with an already existing HTTP client.
///
/// ## Errors
///
/// - If we cannot encode the parameters as a form url encoded.
/// - If we cannot make the HTTP request.
/// - If the server does not respond with a 200.
/// - If we cannot read the body from HTTP.
/// - If we cannot parse the HTML response.
pub async fn dump_memory_with_raw_client<FnTy>(
	client: &Client,
	mion_ip: Ipv4Addr,
	resume_at: Option<usize>,
	early_stop_at: Option<usize>,
	buff_callback: FnTy,
) -> Result<(), CatBridgeError>
where
	FnTy: FnMut(Vec<u8>) + Send + Sync,
{
	let (stop_requests_sender, mut stop_requests_consumer) = bounded_channel(1);
	let retry_counter = AtomicU8::new(0);

	let start_address = resume_at.unwrap_or(0);
	let (page_results_sender, page_results_consumer) = bounded_channel(512);

	let retry_counter_ref = &retry_counter;
	let sender_ref = &page_results_sender;
	// This will make memory page requests concurrently in chunks of
	// `MAX_MEMORY_CONCURRENCY`, with each task handling it's own retry
	// logic.
	//
	// If requests start throwing errors, the receiving channel will send
	// a message to `stop_requests_sender` which will shut everything down.
	let buffered_stream_future = futures::stream::iter(
		(start_address..=early_stop_at.unwrap_or(MEMORY_MAX_ADDRESS)).step_by(512),
	)
	.map(|page_start| async move {
		loop {
			if !do_memory_page_fetch(client, mion_ip, page_start, retry_counter_ref, sender_ref)
				.await
			{
				break;
			}
		}
	})
	.buffered(MAX_MEMORY_CONCURRENCY)
	.collect::<Vec<()>>();

	// As requests finish, they may not necissarily be in order.
	// We need to reorder them to ensure they're called back in a
	// serial order.
	let join_handle = do_memory_page_ordering(
		page_results_consumer,
		stop_requests_sender,
		start_address,
		buff_callback,
	);

	{
		let recv_future = stop_requests_consumer.recv();

		futures::pin_mut!(buffered_stream_future);
		futures::pin_mut!(recv_future);

		let (select, _joined) = futures::future::join(
			// Wait for all requests to finish, or a stop signal caused by an error.
			futures::future::select(buffered_stream_future, recv_future),
			// Wait for all of our ordering to finish too.
			join_handle,
		)
		.await;
		match select {
			Either::Right((error, _)) => {
				if let Some(cause) = error {
					return Err(cause);
				}
			}
			Either::Left(_) => {}
		}
	}

	// Double check we didn't get an error ordering all the pages.
	if let Ok(cause) = stop_requests_consumer.try_recv() {
		return Err(cause);
	}

	Ok(())
}

async fn do_memory_page_ordering<FnTy>(
	mut results: BoundedReceiver<Result<(usize, Vec<u8>), CatBridgeError>>,
	stopper: BoundedSender<CatBridgeError>,
	start_at: usize,
	mut callback: FnTy,
) where
	FnTy: FnMut(Vec<u8>),
{
	let mut out_of_order_cache: FnvHashMap<usize, Vec<u8>> = FnvHashMap::default();
	let mut looking_for_page = start_at;

	while looking_for_page <= MEMORY_MAX_ADDRESS {
		if let Some(data) = out_of_order_cache.remove(&looking_for_page) {
			callback(data);
			looking_for_page += 512;
			continue;
		}

		let Some(page_result) = results.recv().await else {
			_ = stopper.send(CatBridgeError::ClosedChannel).await;
			break;
		};
		match page_result {
			Ok((addr, data)) => {
				out_of_order_cache.insert(addr, data);
			}
			Err(cause) => {
				_ = stopper.send(cause).await;
				break;
			}
		}
	}
}

async fn do_memory_page_fetch(
	client: &Client,
	mion_ip: Ipv4Addr,
	page_start: usize,
	retry_counter: &AtomicU8,
	result_stream: &BoundedSender<Result<(usize, Vec<u8>), CatBridgeError>>,
) -> bool {
	let start_addr = format!("{page_start:08X}");
	debug!(
		bridge.ip = %mion_ip,
		addr = %start_addr,
		"Performing memory page fetch",
	);

	let timeout_response = timeout(
		Duration::from_secs(MEMORY_TIMEOUT_SECONDS),
		do_raw_memory_request(client, mion_ip, &[("start_addr", start_addr)]),
	)
	.await;

	let Ok(potential_response) = timeout_response else {
		if retry_counter.fetch_add(1, AtomicOrdering::AcqRel) > MAX_RETRIES {
			_ = result_stream
				.send(Err(NetworkError::Timeout(Duration::from_secs(
					MEMORY_TIMEOUT_SECONDS,
				))
				.into()))
				.await;
			return false;
		}
		debug!(bridge.ip = %mion_ip, "Slamming Memory dump too hard... backing off for a bit");
		tokio::time::sleep(Duration::from_secs(BACKOFF_SLEEP_SECONDS)).await;
		return true;
	};
	let response = match potential_response {
		Ok(value) => value,
		Err(cause) => {
			if retry_counter.fetch_add(1, AtomicOrdering::AcqRel) > MAX_RETRIES {
				_ = result_stream.send(Err(cause.into())).await;
				return false;
			}
			debug!(bridge.ip = %mion_ip, "Slamming Memory dump too hard... backing off for a bit");
			tokio::time::sleep(Duration::from_secs(BACKOFF_SLEEP_SECONDS)).await;
			return true;
		}
	};

	let status = response.status().as_u16();
	let timeout_body_result = timeout(
		Duration::from_secs(MEMORY_TIMEOUT_SECONDS),
		response.bytes(),
	)
	.await;
	let Ok(body_result) = timeout_body_result else {
		if retry_counter.fetch_add(1, AtomicOrdering::AcqRel) > MAX_RETRIES {
			_ = result_stream
				.send(Err(NetworkError::Timeout(Duration::from_secs(
					MEMORY_TIMEOUT_SECONDS,
				))
				.into()))
				.await;
			return false;
		}
		debug!(bridge.ip = %mion_ip, "Slamming Memory dump too hard... backing off for a bit");
		tokio::time::sleep(Duration::from_secs(BACKOFF_SLEEP_SECONDS)).await;
		return true;
	};

	retry_counter.store(0, AtomicOrdering::Release);
	if status != 200 {
		if let Ok(body) = body_result {
			_ = result_stream
				.send(Err(MionCGIErrors::UnexpectedStatusCode(status, body).into()))
				.await;
			return false;
		}

		_ = result_stream
			.send(Err(MionCGIErrors::UnexpectedStatusCodeNoBody(status).into()))
			.await;
		return false;
	}
	let read_body_bytes = match body_result.map_err(NetworkError::HTTP) {
		Ok(value) => value,
		Err(cause) => {
			_ = result_stream.send(Err(cause.into())).await;
			return false;
		}
	};
	let body_as_string =
		match String::from_utf8(read_body_bytes.into()).map_err(NetworkParseError::Utf8Expected) {
			Ok(value) => value,
			Err(cause) => {
				_ = result_stream.send(Err(cause.into())).await;
				return false;
			}
		};

	process_received_page(page_start, result_stream, &body_as_string).await
}

async fn process_received_page(
	page_start: usize,
	result_stream: &BoundedSender<Result<(usize, Vec<u8>), CatBridgeError>>,
	body_as_string: &str,
) -> bool {
	let table = match extract_memory_table_body(body_as_string) {
		Ok(value) => value,
		Err(cause) => {
			_ = result_stream.send(Err(cause.into())).await;
			return false;
		}
	};
	let mut page_of_bytes = Vec::with_capacity(512);
	for table_row in table.split("<tr>").skip(3) {
		for table_column in table_row
			.trim()
			.trim_end_matches("</tbody>")
			.trim_end()
			.trim_end_matches("</tr>")
			.trim_end()
			.replace("</td>", "")
			.split("<td>")
			.skip(3)
		{
			if table_column.trim().len() != 2 {
				_ = result_stream
					.send(Err(MionCGIErrors::HtmlResponseBadByte(
						table_column.to_owned(),
					)
					.into()))
					.await;
				return false;
			}
			let byte = match u8::from_str_radix(table_column.trim(), 16)
				.map_err(|_| MionCGIErrors::HtmlResponseBadByte(table_column.to_owned()))
			{
				Ok(value) => value,
				Err(cause) => {
					_ = result_stream.send(Err(cause.into())).await;
					return false;
				}
			};
			page_of_bytes.push(byte);
		}
	}

	_ = result_stream.send(Ok((page_start, page_of_bytes))).await;
	false
}

fn extract_memory_table_body(body: &str) -> Result<String, MionCGIErrors> {
	let start = body
		.find(TABLE_START_SIGIL)
		.ok_or_else(|| MionCGIErrors::HtmlResponseMissingMemoryDumpSigil(body.to_owned()))?;
	let body_minus_start = &body[start + TABLE_START_SIGIL.len()..];
	let end = body_minus_start
		.find(TABLE_END_SIGIL)
		.ok_or_else(|| MionCGIErrors::HtmlResponseMissingMemoryDumpSigil(body.to_owned()))?;

	Ok(body_minus_start[..end].to_owned())
}

/// Perform a raw request on the MION board's `eeprom_dump.cgi` page.
///
/// *note: you probably want to call one of the actual methods, as this is
/// basically just a thin wrapper around an HTTP Post Request. Not doing much
/// else more. A lot of it requires that you set things up correctly.*
///
/// ## Errors
///
/// - If we cannot make an HTTP request to the MION Request.
/// - If we fail to encode your parameters into a request body.
pub async fn do_raw_memory_request(
	client: &Client,
	mion_ip: Ipv4Addr,
	url_parameters: &[(impl Deref<Target = str>, impl Display)],
) -> Result<Response, NetworkError> {
	Ok(client
		.post(format!("http://{mion_ip}/dbg/mem_dump.cgi"))
		.version(Version::HTTP_11)
		.header("authorization", format!("Basic {AUTHZ_HEADER}"))
		.header("content-type", "application/x-www-form-urlencoded")
		.header(
			"user-agent",
			format!("cat-dev/{}", env!("CARGO_PKG_VERSION")),
		)
		.body::<String>(encode_url_parameters(url_parameters))
		.send()
		.await?)
}