This repository has been archived by the owner on Jan 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Form1.cs
598 lines (555 loc) · 22.7 KB
/
Form1.cs
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
using System.Net.Http.Json;
using System.Net.Http.Headers;
using System.Text.Json;
using System.Diagnostics;
using Microsoft.VisualStudio.Threading;
namespace PS5CodeReader
{
public partial class Form1 : Form
{
private readonly string FileNameCache = @"cache.json";
private readonly string StrAuto = @"Auto";
private PS5ErrorCodeList? errorCodeList;
private CancellationTokenSource? cancellationTokenSource;
/*
* Possible Commands
* version
* bringup
* shutdown
* firmud
* bsn
* halt
* cp ready
* cp busy
* cp reset
* bestat
* powersw
* resetsw
* bootbeep stat
* bootbeep on
* bootbeep off
* reset syscon
* xdrdiag info
* xdrdiag start
* xdrdiag result
* xiodiag
* fandiag
* errlog
* readline
* tmpforcp <zone id>
* cp beepreote
* cp beep2kn1n3
* cp beep2kn2n3
* csum
* osbo
* scopen
* scclose
* ejectsw
*/
public Form1()
{
InitializeComponent();
}
private async void Form1_Load(object sender, EventArgs e)
{
LoadDatabaseTypes();
LoadOperationTypes();
LoadPorts();
ComboBoxOperationType.SelectedValueChanged += ComboBoxOperationType_SelectedValueChanged;
await GetErrorCodesListAsync();
if (errorCodeList == default)
{
LogBox.AppendLine("[-] No Errors List Loaded, Close Application and Try Again!");
}
else
{
LogBox.AppendLine("[+] Please connect your Playstation 5 to UART do not power up the console.", ReadOnlyRichTextBox.ColorInformation);
}
}
private void ComboBoxOperationType_SelectedValueChanged(object? sender, EventArgs e)
{
PanelRawCommand.Visible = ComboBoxOperationType.SelectedValue is OperationType type && type == OperationType.RunRawCommand;
}
private void ComboBoxDevices_DropDown(object sender, EventArgs e)
{
LoadPorts();
}
#region Data Source Information
private void LoadPorts()
{
ComboBoxDevices.DataSource = SerialPort.SelectSerial();
}
private void LoadDatabaseTypes()
{
ComboBoxDeviceType.EnumForComboBox<DataBaseType>();
ComboBoxDeviceType.DisplayMember = "Description";
ComboBoxDeviceType.ValueMember = "Value";
}
private void LoadOperationTypes()
{
ComboBoxOperationType.EnumForComboBox<OperationType>();
ComboBoxOperationType.DisplayMember = "Description";
ComboBoxOperationType.ValueMember = "Value";
}
#endregion
#region Error Codes List From Server
private async Task GetErrorCodesListAsync()
{
LogBox.AppendLine("[+] Loading Errors List", ReadOnlyRichTextBox.ColorInformation);
errorCodeList = default;
try
{
LogBox.Append("Attempting to load from server...");
errorCodeList = await GetErrorCodesGitHubAsync();
if (errorCodeList != default)
{
LogBox.Okay();
//Store errorCode list as a chache.
await CacheErrorListLocalAsync();
}
else
{
LogBox.Fail();
}
}
catch
{
LogBox.Fail();
//todo: Error Handling
//Attempt to get error codes from server failed.
//Lets get errorCodes from a cached local file.
errorCodeList = await GetErrorCodesCacheAsync();
}
if (errorCodeList != default && errorCodeList.PlayStation5 != null && errorCodeList.PlayStation5.ErrorCodes.Any())
{
LogBox.AppendLine($"[+] Loaded {errorCodeList.PlayStation5.ErrorCodes.Count} Errors Succesfully.", ReadOnlyRichTextBox.ColorSuccess);
}
else
{
LogBox.AppendLine("[-] Failed to load Errors List.", ReadOnlyRichTextBox.ColorError);
}
}
/// <summary>
/// Get List of Error Codes for the PS5 from Git hub server.
/// </summary>
/// <returns>Error Code List</returns>
private static async Task<PS5ErrorCodeList?> GetErrorCodesGitHubAsync()
{
using var client = new HttpClient();
client.BaseAddress = new Uri("https://raw.githubusercontent.com/");
client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (compatible; PS5CodeReader/2.1; +https://github.com/amoamare)");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response = await client.GetAsync("amoamare/PS5CodeReader/master/ErrorCodes.json");
response.EnsureSuccessStatusCode();
return await response.Content.ReadFromJsonAsync<PS5ErrorCodeList>();
}
/// <summary>
/// Gets a list of Error Codes from Cached File on System
/// </summary>
/// <returns></returns>
private async Task<PS5ErrorCodeList?> GetErrorCodesCacheAsync()
{
LogBox.Append("Loading Errors List From Cached File...");
if (!File.Exists(FileNameCache))
{
LogBox.Fail();
LogBox.AppendText("No Cached File Saved!");
return default;
}
using var stream = File.OpenRead(FileNameCache);
var cached = await JsonSerializer.DeserializeAsync<PS5ErrorCodeList>(stream);
if (cached == default || cached.PlayStation5 != default && !cached.PlayStation5.ErrorCodes.Any())
{
LogBox.Fail();
return default;
}
LogBox.Okay();
return cached;
}
/// <summary>
/// Save error codes to a cached file on system
/// </summary>
/// <param name="fileName">cached.json</param>
/// <returns></returns>
private async Task SaveCacheFileAsync(string fileName)
{
using var stream = File.Create(fileName);
var options = new JsonSerializerOptions { WriteIndented = true };
await JsonSerializer.SerializeAsync(stream, errorCodeList, options: options);
await stream.DisposeAsync();
return; // only need to store it as a cahce first time creating it
}
/// <summary>
/// Cachce error list on local disk
/// </summary>
/// <returns></returns>
private async Task CacheErrorListLocalAsync()
{
if (errorCodeList == default)
{
//Can't save what we don't have right?
return;
}
if (!File.Exists(FileNameCache) && errorCodeList != default)
{
LogBox.Append("Creating new errors list cache file...");
await SaveCacheFileAsync(FileNameCache);
LogBox.Okay();
return; // only need to store it as a cahce first time creating it
}
else
{
LogBox.Append("Comparing cached version from server version...");
//Lets open and serialize the revision to compare if we need to update the cached file.
using var stream = File.OpenRead(FileNameCache);
var cached = await JsonSerializer.DeserializeAsync<PS5ErrorCodeList>(stream);
if (cached == default || errorCodeList == default)
{
LogBox.Fail();
//todo: Update error handling
return;
}
LogBox.Okay();
if (cached.Revision < errorCodeList.Revision)
{
LogBox.AppendLine($"Cached Version: {cached.Revision}.");
LogBox.AppendLine($"Server Version: {errorCodeList.Revision}.");
LogBox.Append("Updating cached version with server...");
//Our downloaded error codes have updated. Lets update the cached version.
await stream.DisposeAsync();
try
{
File.Delete(FileNameCache);
}
catch
{
try
{
File.Move(FileNameCache, $"{FileNameCache}.old");
}
catch
{
//todo: update error handling if we can not delete or move the file.
LogBox.Fail();
return;
}
}
if (!File.Exists(FileNameCache))
{
//safe to create new file.
await SaveCacheFileAsync(FileNameCache);
LogBox.Okay();
}
}
}
}
#endregion
private bool InterfaceState
{
set
{
ButtonRunOperation.Text = value ? @"Run Operation" : @"Cancel";
ButtonRunOperation.Tag = !value;
ComboBoxDevices.Enabled = value;
ComboBoxDeviceType.Enabled = value;
ComboBoxOperationType.Enabled = value;
TextBoxRawCommand.Enabled = !value;
}
}
private async Task<Device?> AutoDetectDeviceAsync()
{
Device? device = ComboBoxDevices.SelectedItem as Device;
if (device == default || errorCodeList == null) return default;
var autoDetect = device.Port.StartsWith(StrAuto, StringComparison.InvariantCultureIgnoreCase);
if (!autoDetect) return device;
var devices = ComboBoxDevices.Items.OfType<Device>().ToList();
devices.Remove(device); // remove the auto detect device.
if (devices.Count == 1) return devices.FirstOrDefault(); //if only 1 device is detected we can just skipp detecting it.
cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(1));
foreach (var autoDevice in devices)
{
LogBox.AppendLine($"[*] Auto Detecting Playstation 5 on {autoDevice}", ReadOnlyRichTextBox.ColorInformation);
LogBox.AppendLine("\t- Disconnect power cord from PS5\r\n\t- Wait 5 seconds.\r\n\t- Connect Power to PS5 due not power on!", ReadOnlyRichTextBox.ColorError);
using var serial = new SerialPort(autoDevice.Port);
LogBox.Append($"Opening Device on {autoDevice.FriendlyName}...");
serial.Open();
LogBox.Okay();
LogBox.AppendLine("[*] Listening for Playstation 5.", ReadOnlyRichTextBox.ColorInformation);
List<string> Lines = new();
do
{
try
{
var line = await serial.ReadLineAsync(cancellationTokenSource.Token);
Lines.Add(line);
}
catch (OperationCanceledException)
{
cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(10));
await serial.SendBreakAsync(cancellationToken: cancellationTokenSource.Token);
var line = await serial.ReadLineAsync(cancellationTokenSource.Token);
Lines.Add(line);
}
} while (serial.BytesToRead != 0);
var flag = Lines.Any(x => x.StartsWith(@"$$ [MANU] UART CMD READY:36") || x.StartsWith(@"NG E0000003:4D") || x.StartsWith("OK 00000000:3A"));
if (flag)
{
LogBox.AppendLine($@"[+] Detected a Playstation 5 on {autoDevice.FriendlyName}", ReadOnlyRichTextBox.ColorSuccess);
ComboBoxDevices.SelectedItem = autoDevice;
return autoDevice;
}
}
return default;
}
private async void ButtonRunOperations_Click(object sender, EventArgs e)
{
if (ComboBoxOperationType.SelectedValue is not OperationType type) return;
try
{
if (ButtonRunOperation.Tag is not null && ButtonRunOperation.Tag is bool cancel && cancel
&& cancellationTokenSource != null)
{
cancellationTokenSource.Cancel(false);
return;
}
LogBox.Clear();
InterfaceState = false;
await RunOperationsAsync(type);
}
catch (OperationCanceledException)
{
LogBox.AppendLine("[!] Operation Cancelled");
}
catch (Exception ex)
{
//todo: add error handling
Debug.WriteLine(ex);
}
finally
{
InterfaceState = true;
}
}
private async Task RunOperationsAsync(OperationType type)
{
switch (type)
{
default: return;
case OperationType.ReadErrorCodes:
LogBox.AppendLine("[*] Operation: Read UART Codes.", ReadOnlyRichTextBox.ColorError);
await ReadCodesAsync();
break;
case OperationType.ClearErrorCodes:
LogBox.AppendLine("[*] Operation: Clear UART Codes.", ReadOnlyRichTextBox.ColorError);
await ClearLogsAsync();
break;
case OperationType.MonitorMode:
LogBox.AppendLine("[*] Operation: Running Monitor Mode.", ReadOnlyRichTextBox.ColorError);
await RunMonitorModeAsync();
break;
case OperationType.RunCommandList:
LogBox.AppendLine("[*] Operation: Run Command List.", ReadOnlyRichTextBox.ColorError);
await RunCommmandListAsync();
break;
case OperationType.RunRawCommand:
LogBox.AppendLine("[*] Operation: Run Raw Command.", ReadOnlyRichTextBox.ColorError);
await RunRawCommandAsync();
break;
}
}
#region Run Operation Types
/// <summary>
/// Read past 10 error logs
/// </summary>
/// <param name="count"></param>
/// <returns></returns>
private async Task ReadCodesAsync(int count = 10)
{
var device = await AutoDetectDeviceAsync();
if (device == default)
{
LogBox.AppendLine("[-] No Playstation 5 Detected!", ReadOnlyRichTextBox.ColorError);
return;
}
cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(10));
using var serial = new SerialPort(device.Port);
serial.Open();
List<string> Lines = new();
for (var i = 0; i <= count; i++)
{
var command = $"errlog {i}";
var checksum = SerialPort.CalculateChecksum(command);
await serial.WriteLineAsync(command);
do
{
var line = await serial.ReadLineAsync(cancellationTokenSource.Token);
if (!string.Equals($"{command}:{checksum:X2}", line, StringComparison.InvariantCultureIgnoreCase))
{
//ignore the echo'd command capture everything else.
Lines.Add(line);
}
} while (serial.BytesToRead != 0);
}
foreach (var l in Lines)
{
var split = l.Split(' ');
if (!split.Any()) continue;
switch (split[0])
{
case "NG":
LogBox.AppendLine("Failed to read data");
break;
case "OK":
var errorCode = split[2];
var errorLookup = errorCodeList.PlayStation5.ErrorCodes.First(x => x.ID == errorCode);
LogBox.AppendLine($"{errorLookup.ID}: {errorLookup.Message}");
break;
}
}
}
/// <summary>
/// Clears Error Logs
/// </summary>
/// <returns></returns>
private async Task ClearLogsAsync()
{
var device = await AutoDetectDeviceAsync();
if (device == default)
{
LogBox.AppendLine("[-] No Playstation 5 Detected!", ReadOnlyRichTextBox.ColorError);
return;
}
cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(10));
using var serial = new SerialPort(device.Port);
serial.Open();
LogBox.Append("[+]\tClearing Logs...", ReadOnlyRichTextBox.ColorInformation);
var command = "errlog clear";
var checksum = SerialPort.CalculateChecksum(command);
await serial.WriteLineAsync("errlog clear", cancellationTokenSource.Token);
string? response = default;
do
{
var line = await serial.ReadLineAsync(cancellationTokenSource.Token);
if (!string.Equals($"{command}:{checksum:X2}", line, StringComparison.InvariantCultureIgnoreCase))
{
//ignore the echo'd command capture everything else.
response = line;
}
} while (serial.BytesToRead != 0);
var split = response?.Split(' ');
if (split == default || split.Any())
{
LogBox.Okay();
return;
}
switch (split[0])
{
case "NG":
LogBox.Fail();
break;
case "OK":
LogBox.Okay();
break;
}
}
/// <summary>
/// Run in monitor mode. This will listen to anything the console might be saying.
/// </summary>
/// <returns></returns>
private async Task RunMonitorModeAsync()
{
var device = await AutoDetectDeviceAsync();
if (device == default)
{
LogBox.AppendLine("[-] No Playstation 5 Detected!", ReadOnlyRichTextBox.ColorError);
return;
}
cancellationTokenSource = new CancellationTokenSource();
using var serial = new SerialPort(device.Port);
serial.Open();
do
{
var line = await serial.ReadLineAsync(cancellationTokenSource.Token);
LogBox.AppendLine(line);
} while (!cancellationTokenSource.IsCancellationRequested);
}
/// <summary>
/// Run a list of commands saved in a text file.
/// </summary>
/// <returns></returns>
private async Task RunCommmandListAsync()
{
var device = await AutoDetectDeviceAsync();
if (device == default)
{
LogBox.AppendLine("[-] No Playstation 5 Detected!", ReadOnlyRichTextBox.ColorError);
return;
}
using var ofd = new OpenFileDialog();
ofd.InitialDirectory = Directory.GetCurrentDirectory();
ofd.RestoreDirectory = true;
ofd.Title = @"Select Command List";
ofd.DefaultExt = @"txt";
ofd.Filter = @"txt files (*.txt)|*.txt";
ofd.CheckFileExists = true;
ofd.CheckPathExists = true;
if (ofd.ShowDialog() != DialogResult.OK) return;
FileInfo file = new(ofd.FileName);
using var stream = new StreamReader(file.FullName);
string? command = default;
cancellationTokenSource = new CancellationTokenSource();
using var serial = new SerialPort(device.Port);
serial.Open();
do
{
command = await stream.ReadLineAsync();
if (string.IsNullOrEmpty(command)) continue;
await serial.WriteLineAsync(command, cancellationTokenSource.Token);
do
{
var response = await serial.ReadLineAsync(cancellationTokenSource.Token);
LogBox.AppendLine(response);
} while (serial.BytesToRead != 0);
} while (!stream.EndOfStream);
}
private readonly AsyncAutoResetEvent AutoResetEventRawCommand = new AsyncAutoResetEvent(false);
/// <summary>
/// Run raw command from user. Keeps port open.
/// </summary>
/// <returns></returns>
private async Task RunRawCommandAsync()
{
var device = await AutoDetectDeviceAsync();
if (device == default)
{
LogBox.AppendLine("[-] No Playstation 5 Detected!", ReadOnlyRichTextBox.ColorError);
return;
}
using var serial = new SerialPort(device.Port);
serial.Open();
do
{
cancellationTokenSource = new CancellationTokenSource();
await AutoResetEventRawCommand.WaitAsync(cancellationTokenSource.Token);
var command = TextBoxRawCommand.Text.Trim();
TextBoxRawCommand.Clear();
cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(10));
await serial.WriteLineAsync(command);
do
{
var line = await serial.ReadLineAsync(cancellationTokenSource.Token);
LogBox.AppendLine(line);
} while (serial.BytesToRead != 0);
} while (!cancellationTokenSource.IsCancellationRequested);
}
#endregion
private void TextBoxRawCommand_KeyPress(object sender, KeyPressEventArgs e)
{
if (!TextBoxRawCommand.Text.Any()) return; // dont send empty commands
if (e.KeyChar == (char)Keys.Enter)
{
AutoResetEventRawCommand.Set();
}
}
}
}