xsystem.rs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. //! XSystem: the console's identity and sandbox, and how analytics describe
  2. //! the device. Fixed values: one offline retail console, the model the user
  3. //! chose ([`victory_gpu::console`]).
  4. #![allow(clippy::not_unsafe_ptr_arg_deref)]
  5. use super::xgameruntime::{
  6. Guid, HRESULT, Interface, Methods, Object, add_ref, missing, put_string,
  7. };
  8. pub const PROVIDER: Guid = Guid::new(
  9. 0xe349_bd1a,
  10. 0xfc20,
  11. 0x4e40,
  12. [0xb9, 0x9c, 0x41, 0x78, 0xcc, 0x6b, 0x40, 0x9f],
  13. );
  14. /// The interface as Minecraft (2022) asks for it; an older table
  15. /// ended before the device id.
  16. pub const IID: Guid = Guid::new(
  17. 0x6fd7_1f09,
  18. 0x7513,
  19. 0x49f0,
  20. [0x89, 0xbc, 0xbf, 0xaf, 0x5d, 0xf6, 0xf8, 0x52],
  21. );
  22. /// Analytics information; its interface id is the provider's.
  23. pub const ANALYTICS: Guid = Guid::new(
  24. 0xb884_675d,
  25. 0xb738,
  26. 0x4a9c,
  27. [0x81, 0x5d, 0x9a, 0x9a, 0x1e, 0x0c, 0x6c, 0x9b],
  28. );
  29. /// 38 characters (39 bytes with the NUL).
  30. const CONSOLE_ID: &str = "00000000000000000000000000000000000001";
  31. /// The retail sandbox.
  32. const SANDBOX: &str = "RETAIL";
  33. /// Base64 of 32 bytes: 44 characters (45 bytes with the NUL).
  34. const DEVICE_ID: &str = "VkljdG9yeSBvZmZsaW5lIGRldmljZSBpZGVudGl0eSE=";
  35. pub static OBJECT: Object = Object::new("system", &VTABLE);
  36. pub static ANALYTICS_OBJECT: Object = Object::new("analytics", &ANALYTICS_VTABLE);
  37. /// The interface as the title (2026) asks for it; it calls the
  38. /// same three slots (0x18, 0x20, 0x28) with the same arguments.
  39. pub const IID_2026: Guid = Guid::new(
  40. 0x1861_cf2e,
  41. 0xe18b,
  42. 0x4834,
  43. [0xa9, 0xf5, 0xb4, 0xa4, 0xe6, 0xef, 0xb4, 0xcf],
  44. );
  45. pub const INTERFACES: &[Interface] = &[
  46. (PROVIDER, IID, &OBJECT),
  47. (PROVIDER, IID_2026, &OBJECT),
  48. (ANALYTICS, ANALYTICS, &ANALYTICS_OBJECT),
  49. ];
  50. type This = *const Object;
  51. extern "system" fn console_id(_: This, size: usize, buffer: *mut u8, used: *mut usize) -> HRESULT {
  52. put_string(CONSOLE_ID, size, buffer, used)
  53. }
  54. extern "system" fn sandbox_id(_: This, size: usize, buffer: *mut u8, used: *mut usize) -> HRESULT {
  55. put_string(SANDBOX, size, buffer, used)
  56. }
  57. /// The device id this title sees (titles see different ones).
  58. extern "system" fn device_id(_: This, size: usize, buffer: *mut u8, used: *mut usize) -> HRESULT {
  59. put_string(DEVICE_ID, size, buffer, used)
  60. }
  61. #[repr(C)]
  62. #[derive(Debug, Clone, Copy, PartialEq, Eq)]
  63. pub struct Version {
  64. pub major: u16,
  65. pub minor: u16,
  66. pub build: u16,
  67. pub revision: u16,
  68. }
  69. #[repr(C)]
  70. pub struct AnalyticsInfo {
  71. pub os: Version,
  72. pub hosting_os: Version,
  73. /// NUL-terminated.
  74. pub family: [u8; 64],
  75. pub form: [u8; 64],
  76. }
  77. const OS: Version = Version {
  78. major: 10,
  79. minor: 0,
  80. build: 22000,
  81. revision: 0,
  82. };
  83. fn text(s: &str) -> [u8; 64] {
  84. let mut out = [0; 64];
  85. out[..s.len()].copy_from_slice(s.as_bytes());
  86. out
  87. }
  88. /// A struct this large comes back through the caller's buffer, whose
  89. /// address is also the return value.
  90. extern "system" fn analytics_info(_: This, out: *mut AnalyticsInfo) -> *mut AnalyticsInfo {
  91. let info = AnalyticsInfo {
  92. os: OS,
  93. hosting_os: OS,
  94. family: text("Xbox"),
  95. form: text(victory_gpu::console::current().name()),
  96. };
  97. // SAFETY: the caller passes room for the struct.
  98. unsafe { out.write(info) };
  99. out
  100. }
  101. static VTABLE: Methods<6> = Methods([
  102. missing::<0> as *const (),
  103. add_ref as *const (),
  104. add_ref as *const (),
  105. console_id as *const (), // 0x18, by the older table; titles seen do not call it
  106. sandbox_id as *const (), // 0x20
  107. device_id as *const (), // 0x28
  108. ]);
  109. static ANALYTICS_VTABLE: Methods<4> = Methods([
  110. missing::<0> as *const (),
  111. add_ref as *const (),
  112. add_ref as *const (),
  113. analytics_info as *const (), // 0x18
  114. ]);
  115. /// The device type is not a runtime call: code linked into the title reads
  116. /// `ConsoleMode` from this key (`RegOpenKeyW`, `RegQueryValueExW`) and
  117. /// takes a missing value for a PC.
  118. #[cfg(windows)]
  119. mod device_type {
  120. #![allow(non_snake_case)]
  121. use std::ffi::c_void;
  122. use victory_gpu::console::{self, Console};
  123. use crate::modules::files::host;
  124. use crate::{HleModule, hle_export};
  125. type Key = *mut c_void;
  126. const LOCAL_MACHINE: Key = 0xffff_ffff_8000_0002_u64 as Key;
  127. const KEY: &str = r"Software\Microsoft\Windows NT\CurrentVersion\OEM";
  128. const VALUE: &str = "ConsoleMode";
  129. const SUCCESS: u32 = 0;
  130. const FILE_NOT_FOUND: u32 = 2;
  131. const INVALID_PARAMETER: u32 = 87;
  132. const MORE_DATA: u32 = 234;
  133. const DWORD: u32 = 4;
  134. /// Stands for the key; never a host handle value.
  135. static OEM: u8 = 0;
  136. fn oem() -> Key {
  137. (&raw const OEM).cast_mut().cast()
  138. }
  139. /// What the title's code maps to each model's device type.
  140. pub(super) fn console_mode(console: Console) -> u32 {
  141. match console {
  142. Console::SeriesX => 0x64,
  143. Console::SeriesS => 0x60,
  144. }
  145. }
  146. fn is(text: *const u16, expected: &str) -> bool {
  147. if text.is_null() {
  148. return false;
  149. }
  150. // SAFETY: the title passes a NUL-terminated string.
  151. let text = unsafe {
  152. let len = (0..).take_while(|&i| *text.add(i) != 0).count();
  153. String::from_utf16_lossy(std::slice::from_raw_parts(text, len))
  154. };
  155. text.eq_ignore_ascii_case(expected)
  156. }
  157. /// Opens our key for its path; the key has no subkeys. `None`: not
  158. /// ours, ask the host.
  159. fn open(root: Key, sub: *const u16, out: *mut Key) -> Option<u32> {
  160. if root == oem() {
  161. return Some(FILE_NOT_FOUND);
  162. }
  163. if root == LOCAL_MACHINE && is(sub, KEY) && !out.is_null() {
  164. // SAFETY: checked non-null; the title passes room for a key.
  165. unsafe { out.write(oem()) };
  166. return Some(SUCCESS);
  167. }
  168. None
  169. }
  170. // Our titles import only these registry functions (and `RegOpenKeyExW`
  171. // below), so no other call can reach the host with our key.
  172. pub extern "system" fn RegOpenKeyW(root: Key, sub: *const u16, out: *mut Key) -> u32 {
  173. open(root, sub, out).unwrap_or_else(|| {
  174. host!("advapi32.dll", "RegOpenKeyW": extern "system" fn(Key, *const u16, *mut Key) -> u32)(
  175. root, sub, out,
  176. )
  177. })
  178. }
  179. pub extern "system" fn RegOpenKeyExW(
  180. root: Key,
  181. sub: *const u16,
  182. options: u32,
  183. access: u32,
  184. out: *mut Key,
  185. ) -> u32 {
  186. open(root, sub, out).unwrap_or_else(|| {
  187. host!("advapi32.dll", "RegOpenKeyExW": extern "system" fn(
  188. Key, *const u16, u32, u32, *mut Key,
  189. ) -> u32)(root, sub, options, access, out)
  190. })
  191. }
  192. pub extern "system" fn RegQueryValueExW(
  193. key: Key,
  194. name: *const u16,
  195. reserved: *mut u32,
  196. kind: *mut u32,
  197. data: *mut u8,
  198. size: *mut u32,
  199. ) -> u32 {
  200. if key != oem() {
  201. return host!("advapi32.dll", "RegQueryValueExW": extern "system" fn(
  202. Key, *const u16, *mut u32, *mut u32, *mut u8, *mut u32,
  203. ) -> u32)(key, name, reserved, kind, data, size);
  204. }
  205. if !is(name, VALUE) {
  206. return FILE_NOT_FOUND;
  207. }
  208. if !data.is_null() && size.is_null() {
  209. return INVALID_PARAMETER;
  210. }
  211. // SAFETY: the title passes valid or null pointers, sized by `size`.
  212. unsafe {
  213. if !kind.is_null() {
  214. kind.write(DWORD);
  215. }
  216. if size.is_null() {
  217. return SUCCESS;
  218. }
  219. let room = size.read();
  220. size.write(4);
  221. if data.is_null() {
  222. return SUCCESS;
  223. }
  224. if room < 4 {
  225. return MORE_DATA;
  226. }
  227. data.cast::<u32>()
  228. .write_unaligned(console_mode(console::current()));
  229. }
  230. tracing::debug!(console = console::current().name(), "device type read");
  231. SUCCESS
  232. }
  233. pub extern "system" fn RegCloseKey(key: Key) -> u32 {
  234. if key == oem() {
  235. return SUCCESS;
  236. }
  237. host!("advapi32.dll", "RegCloseKey": extern "system" fn(Key) -> u32)(key)
  238. }
  239. pub(crate) fn module() -> HleModule {
  240. HleModule {
  241. name: "device type",
  242. dlls: &["advapi32.dll", "api-ms-win-core-registry-*"],
  243. exports: vec![
  244. hle_export!(RegOpenKeyW),
  245. hle_export!(RegOpenKeyExW),
  246. hle_export!(RegQueryValueExW),
  247. hle_export!(RegCloseKey),
  248. ],
  249. }
  250. }
  251. #[cfg(test)]
  252. mod tests {
  253. use super::*;
  254. fn w(s: &str) -> Vec<u16> {
  255. s.encode_utf16().chain([0]).collect()
  256. }
  257. #[test]
  258. fn titles_read_the_chosen_model() {
  259. assert_eq!(console_mode(Console::SeriesX), 0x64);
  260. assert_eq!(console_mode(Console::SeriesS), 0x60);
  261. let mut key = std::ptr::null_mut();
  262. let path = w(&KEY.to_ascii_lowercase());
  263. assert_eq!(RegOpenKeyW(LOCAL_MACHINE, path.as_ptr(), &mut key), SUCCESS);
  264. let (mut kind, mut value, mut size) = (0, 0u32, 4);
  265. let name = w(VALUE);
  266. let query = |data: *mut u32, size: *mut u32, kind: *mut u32| {
  267. RegQueryValueExW(
  268. key,
  269. name.as_ptr(),
  270. std::ptr::null_mut(),
  271. kind,
  272. data.cast(),
  273. size,
  274. )
  275. };
  276. assert_eq!(query(&mut value, &mut size, &mut kind), SUCCESS);
  277. assert_eq!((kind, size), (DWORD, 4));
  278. assert_eq!(value, console_mode(console::current()));
  279. let mut small = 2;
  280. assert_eq!(query(&mut value, &mut small, &mut kind), MORE_DATA);
  281. assert_eq!(small, 4);
  282. let other = w("Other");
  283. assert_eq!(
  284. RegQueryValueExW(
  285. key,
  286. other.as_ptr(),
  287. std::ptr::null_mut(),
  288. &mut kind,
  289. std::ptr::null_mut(),
  290. &mut size
  291. ),
  292. FILE_NOT_FOUND
  293. );
  294. let mut sub = std::ptr::null_mut();
  295. assert_eq!(
  296. RegOpenKeyExW(key, other.as_ptr(), 0, 1, &mut sub),
  297. FILE_NOT_FOUND
  298. );
  299. assert_eq!(RegCloseKey(key), SUCCESS);
  300. let mut again = std::ptr::null_mut();
  301. assert_eq!(
  302. RegOpenKeyExW(LOCAL_MACHINE, path.as_ptr(), 0, 1, &mut again),
  303. SUCCESS
  304. );
  305. assert_eq!(again, oem());
  306. }
  307. #[test]
  308. fn other_keys_reach_the_host() {
  309. let mut key = std::ptr::null_mut();
  310. let path = w(r"Software\Microsoft\Windows NT\CurrentVersion");
  311. assert_eq!(RegOpenKeyW(LOCAL_MACHINE, path.as_ptr(), &mut key), SUCCESS);
  312. assert_ne!(key, oem());
  313. let (mut size, name) = (0, w("ProductName"));
  314. assert_eq!(
  315. RegQueryValueExW(
  316. key,
  317. name.as_ptr(),
  318. std::ptr::null_mut(),
  319. std::ptr::null_mut(),
  320. std::ptr::null_mut(),
  321. &mut size
  322. ),
  323. SUCCESS
  324. );
  325. assert!(size > 0);
  326. assert_eq!(RegCloseKey(key), SUCCESS);
  327. }
  328. }
  329. }
  330. #[cfg(windows)]
  331. pub(crate) use device_type::module;
  332. #[cfg(test)]
  333. mod tests {
  334. use super::*;
  335. use crate::modules::xgameruntime::{S_OK, method};
  336. #[test]
  337. fn identity_strings_fit_the_documented_sizes() {
  338. type Get = extern "system" fn(This, usize, *mut u8, *mut usize) -> HRESULT;
  339. let this: This = &OBJECT;
  340. let mut buffer = [0u8; 64];
  341. let mut used = 0;
  342. for (slot, size) in [(0x18, 39), (0x20, 16), (0x28, 45)] {
  343. let get = method::<Get>(&OBJECT, slot);
  344. assert_eq!(get(this, size, buffer.as_mut_ptr(), &mut used), S_OK);
  345. assert!(used <= size && buffer[used - 1] == 0, "slot {slot:#x}");
  346. }
  347. assert_eq!(&buffer[..7], b"VkljdG9");
  348. }
  349. #[test]
  350. fn analytics_info_comes_back_through_the_buffer() {
  351. type Get = extern "system" fn(This, *mut AnalyticsInfo) -> *mut AnalyticsInfo;
  352. let mut info = std::mem::MaybeUninit::<AnalyticsInfo>::uninit();
  353. let get = method::<Get>(&ANALYTICS_OBJECT, 0x18);
  354. let back = get(&ANALYTICS_OBJECT, info.as_mut_ptr());
  355. assert_eq!(back, info.as_mut_ptr());
  356. // SAFETY: written by the call.
  357. let info = unsafe { info.assume_init() };
  358. assert_eq!(size_of::<AnalyticsInfo>(), 0x90);
  359. assert_eq!(info.os.build, 22000);
  360. assert_eq!(&info.form[..14], b"Xbox Series X\0");
  361. }
  362. }