Repository navigation
Expand file tree
/
Copy pathPayloadUI.cs
More file actions
540 lines (457 loc) · 21.6 KB
/
Copy pathPayloadUI.cs
File metadata and controls
540 lines (457 loc) · 21.6 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Windows;
using ChromeosUpdateEngine;
using FastbootEnhance.Core.Payload;
namespace FastbootEnhance
{
class PayloadUI
{
/// <summary>Only used when a package stores payload.bin deflated and it must be unpacked.</summary>
public static readonly string PAYLOAD_TMP =
Path.Combine(Path.GetTempPath(), "FastbootStudio", "dumper");
enum page_status
{
empty,
loaded
}
static page_status cur_status;
public static PayloadFile payload;
static CancellationTokenSource cancellation;
static Thread extractionWorker;
/// <summary>True while images are being written to the output folder.</summary>
public static bool extracting => extractionWorker != null;
static void switchMainView()
{
switch (cur_status)
{
case page_status.empty:
MainWindow.THIS.payload_before_load.Visibility = Visibility.Visible;
MainWindow.THIS.payload_after_load.Visibility = Visibility.Collapsed;
// The file chip and Remove live in the page header; with no file they mean nothing.
MainWindow.THIS.payload_action_bar.Visibility = Visibility.Collapsed;
break;
case page_status.loaded:
MainWindow.THIS.payload_before_load.Visibility = Visibility.Collapsed;
MainWindow.THIS.payload_after_load.Visibility = Visibility.Visible;
MainWindow.THIS.payload_action_bar.Visibility = Visibility.Visible;
break;
}
}
static void onLoad(string filename)
{
PayloadFile opened = null;
Exception exception = null;
MainWindow.THIS.payload_load_btn.Visibility = Visibility.Hidden;
MainWindow.THIS.payload_opening.Visibility = Visibility.Visible;
Action load = new Action(delegate
{
try
{
opened = PayloadFile.Open(filename, PAYLOAD_TMP);
}
catch (Exception e)
{
exception = e;
}
});
Action afterLoad = new Action(delegate
{
MainWindow.THIS.payload_load_btn.Visibility = Visibility.Visible;
MainWindow.THIS.payload_opening.Visibility = Visibility.Hidden;
if (exception != null)
{
LogStore.Append("Could not open " + filename + ": " + exception.Message);
ThemedDialog.Show(Properties.Resources.payload_unsupported_format
+ "\n" + exception.Message);
return;
}
LogStore.Append("Opened " + filename + ": " + opened.Partitions.Count + " partitions, "
+ (opened.IsIncremental ? "incremental" : "full") + " package");
closeCurrent();
payload = opened;
cur_status = page_status.loaded;
MainWindow.THIS.payload_cur_open.Content = Path.GetFileName(filename);
MainWindow.THIS.payload_cur_open.ToolTip = Properties.Resources.payload_current_file + filename;
refreshData();
switchMainView();
});
Helper.offloadAndRun(load, afterLoad);
}
/// <summary>Opens a payload handed to the app from outside, e.g. on the command line.</summary>
public static void openFromPath(string filename)
{
MainWindow.THIS.main_tabs.SelectedItem = MainWindow.THIS.payload_tab;
onLoad(filename);
}
public static void closeCurrent()
{
if (payload == null)
return;
payload.Dispose();
payload = null;
}
/// <summary>
/// Stops a running extraction and gives its workers a moment to delete the images they
/// had not finished, so closing the window does not leave truncated files behind.
/// </summary>
public static void cancelRunningWork()
{
CancellationTokenSource source = cancellation;
if (source != null)
{
try
{
source.Cancel();
}
catch (ObjectDisposedException)
{
}
}
Thread worker = extractionWorker;
if (worker != null)
worker.Join(TimeSpan.FromSeconds(10));
}
static void actionInit()
{
listHelper = new Helper.ListHelper<payload_partition_info_row>(MainWindow.THIS.payload_partition_info,
new Helper.ListHelper<payload_partition_info_row>.Filter(
delegate (payload_partition_info_row row)
{
if (MainWindow.THIS.payload_partition_name_textbox.Text == "")
return true;
if (row.name.Contains(MainWindow.THIS.payload_partition_name_textbox.Text))
return true;
return false;
}
));
MainWindow.THIS.payload_partition_name_textbox.TextChanged += delegate
{
listHelper.doFilter();
};
MainWindow.THIS.payload_load_btn.Click += delegate
{
Helper.fileSelect(new Helper.PathSelectCallback(delegate (string ret)
{
onLoad(ret);
}), "Payload or OTA package|*.bin;*.zip|All Files|*.*");
};
MainWindow.THIS.payload_remove.Click += delegate
{
closeCurrent();
cur_status = page_status.empty;
switchMainView();
};
MainWindow.THIS.payload_extract.Click += onExtractClicked;
MainWindow.THIS.payload_before_load.DragEnter += delegate (object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effects = DragDropEffects.All;
else
e.Effects = DragDropEffects.None;
};
MainWindow.THIS.payload_before_load.Drop += delegate (object sender, DragEventArgs e)
{
Array files = (Array)e.Data.GetData(DataFormats.FileDrop);
if (files == null || files.Length == 0)
return;
if (files.Length > 1)
{
ThemedDialog.Show(Properties.Resources.payload_unable_drop_multifile);
return;
}
onLoad(files.GetValue(0).ToString());
};
}
static void onExtractClicked(object sender, RoutedEventArgs args)
{
if (payload == null)
return;
if (MainWindow.THIS.payload_partition_info.SelectedItems.Count == 0)
{
ThemedDialog.Show(Properties.Resources.payload_target_partition_not_selected);
return;
}
List<string> selected = MainWindow.THIS.payload_partition_info.SelectedItems
.Cast<payload_partition_info_row>()
.Select(row => row.name)
.ToList();
bool allowIncremental = MainWindow.THIS.payload_ignore_delta.IsChecked == true;
bool ignoreUnsupported = MainWindow.THIS.payload_ignore_unknown_op.IsChecked == true;
bool skipChecks = MainWindow.THIS.payload_ignore_check.IsChecked == true;
// Only block when something actually selected cannot be rebuilt from this package.
List<string> blocked = selected
.Select(name => payload.FindPartition(name))
.Where(info => info != null && !info.CanExtract)
.Select(info => info.Name)
.ToList();
if (blocked.Count > 0 && !allowIncremental && !ignoreUnsupported)
{
ThemedDialog.Show(Properties.Resources.payload_incremental_warning
+ "\n\n" + string.Join(", ", blocked));
return;
}
// "Allow incremental" only lets the extraction start: partitions that can be rebuilt
// from this package come out whole, the rest fail with the reason. Leaving blocks as
// zeros is a separate, explicit choice ("ignore unknown operations").
Helper.pathSelect(new Helper.PathSelectCallback(delegate (string path)
{
startExtraction(selected, path, ignoreUnsupported, skipChecks);
}));
}
static void startExtraction(
List<string> selected, string outputDirectory, bool ignoreUnsupported, bool skipChecks)
{
MainWindow.THIS.payload_progress.Visibility = Visibility.Visible;
MainWindow.THIS.payload_progress.Value = 0;
MainWindow.THIS.payload_action_bar.Visibility = Visibility.Hidden;
MainWindow.THIS.payload_extract.IsEnabled = false;
MainWindow.THIS.payload_extract_options.IsEnabled = false;
Helper.TaskbarItemHelper.start();
ExtractionOptions options = new ExtractionOptions
{
VerifyOperationHashes = !skipChecks,
VerifyImageHash = !skipChecks,
IgnoreUnsupportedOperations = ignoreUnsupported,
MaxDegreeOfParallelism = Math.Max(1, Environment.ProcessorCount)
};
long totalBytes = selected
.Select(name => payload.FindPartition(name))
.Where(info => info != null)
.Sum(info => info.UnpackedSize);
ProgressAggregator aggregator = new ProgressAggregator(totalBytes, delegate (int percent)
{
MainWindow.THIS.Dispatcher.BeginInvoke(new Action(delegate
{
MainWindow.THIS.payload_progress.Value = percent;
Helper.TaskbarItemHelper.update(percent);
}));
});
CancellationTokenSource source = new CancellationTokenSource();
cancellation = source;
PayloadFile target = payload;
Thread worker = new Thread(new ThreadStart(delegate
{
ExtractionReport report = null;
Exception failure = null;
try
{
report = PayloadExtractor.ExtractAll(
target, outputDirectory, selected, options, aggregator, source.Token);
}
catch (OperationCanceledException)
{
failure = null;
}
catch (Exception e)
{
failure = e;
}
MainWindow.THIS.Dispatcher.BeginInvoke(new Action(delegate
{
MainWindow.THIS.payload_progress.Visibility = Visibility.Hidden;
MainWindow.THIS.payload_action_bar.Visibility = Visibility.Visible;
MainWindow.THIS.payload_extract.IsEnabled = true;
MainWindow.THIS.payload_extract_options.IsEnabled = true;
Helper.TaskbarItemHelper.stop();
cancellation = null;
extractionWorker = null;
source.Dispose();
if (failure != null)
{
ThemedDialog.Show(Properties.Resources.payload_error_occur + "\n" + failure.Message);
return;
}
if (report == null)
return;
LogStore.Append("Extracted " + report.SucceededCount + " of " + report.Results.Count
+ " partitions to " + outputDirectory + " in " + report.Elapsed.TotalSeconds.ToString("F1") + " s");
// Images with skipped operations are partly zero and were never checked; say so.
string incomplete = string.Join("\n", report.Results
.Where(r => r.Succeeded && r.SkippedOperations > 0)
.Select(r => r.Partition + ": " + string.Format(Properties.Resources.extract_incomplete_detail, r.SkippedOperations)));
if (report.AllSucceeded && incomplete.Length == 0)
{
ThemedDialog.Done(Properties.Resources.operation_completed + "\n\n"
+ string.Format(Properties.Resources.extract_done_detail,
Helper.byte2AUnit(report.TotalBytes), report.Elapsed.TotalSeconds.ToString("F1")));
return;
}
string details = string.Join("\n", report.Results
.Where(r => !r.Succeeded)
.Select(r => r.Partition + ": " + r.Error));
string message = details.Length > 0 ? Properties.Resources.payload_error_occur + "\n\n" + details : "";
if (incomplete.Length > 0)
message += (message.Length > 0 ? "\n\n" : "") + Properties.Resources.extract_incomplete + "\n\n" + incomplete;
ThemedDialog.Show(message, Properties.Resources.error, MessageBoxButton.OK, MessageBoxImage.Warning);
}));
}));
worker.IsBackground = true;
extractionWorker = worker;
worker.Start();
}
static void refreshData()
{
MainWindow.THIS.payload_progress.Visibility = Visibility.Hidden;
MainWindow.THIS.payload_action_bar.Visibility = Visibility.Visible;
MainWindow.THIS.payload_info.Items.Clear();
listHelper.clear();
MainWindow.THIS.payload_partition_name_textbox.Text = "";
MainWindow.THIS.payload_dynamic_partition_meta.Items.Clear();
DeltaArchiveManifest manifest = payload.Manifest;
payloadInfoListAppend(Properties.Resources.payload_version, payload.FileFormatVersion.ToString());
payloadInfoListAppend(Properties.Resources.payload_data_size, Helper.byte2AUnit(payload.DataLength));
payloadInfoListAppend(Properties.Resources.payload_metadata_signature,
describeSignature(payload.MetadataSignature));
payloadInfoListAppend(Properties.Resources.payload_signature,
describeSignature(payload.PayloadSignature));
payloadInfoListAppend(Properties.Resources.payload_full_package,
payload.IsIncremental
? Properties.Resources.no + " (minor version " + payload.MinorVersion + ")"
: Properties.Resources.yes);
if (manifest.HasMaxTimestamp)
{
DateTime? stamp = Helper.timeStamp2DataTime(manifest.MaxTimestamp);
payloadInfoListAppend(Properties.Resources.payload_timestamp,
stamp != null ? stamp.Value.ToString() : manifest.MaxTimestamp.ToString());
}
payloadInfoListAppend(Properties.Resources.payload_blocksize, Helper.byte2AUnit(payload.BlockSize));
payloadInfoListAppend(Properties.Resources.payload_container, payload.Source.FromZip
? (payload.Source.ReadInPlace ? Properties.Resources.payload_container_inplace : Properties.Resources.payload_container_unpacked)
: "payload.bin");
appendOperationBreakdown();
// The ChromeOS-era image info fields are reserved in Android's manifest; these are
// what Android payloads actually carry.
if (manifest.HasSecurityPatchLevel)
payloadInfoListAppend(Properties.Resources.payload_security_patch, manifest.SecurityPatchLevel);
if (manifest.HasPartialUpdate)
payloadInfoListAppend(Properties.Resources.payload_partial_update,
manifest.PartialUpdate ? Properties.Resources.yes : Properties.Resources.no);
if (manifest.ApexInfo.Count > 0)
payloadInfoListAppend(Properties.Resources.payload_apex, manifest.ApexInfo.Count.ToString());
foreach (PayloadPartitionInfo part in payload.Partitions)
{
payloadPartitionInfoListAppend(
part.Name,
Helper.byte2AUnit(part.UnpackedSize),
part.ExpectedSha256 ?? Properties.Resources.unknown,
string.Join(", ", part.Codecs),
describeSupport(part));
}
listHelper.render();
if (manifest.DynamicPartitionMetadata != null)
{
if (manifest.DynamicPartitionMetadata.HasSnapshotEnabled)
dynamicPartitionMetaAppend(Properties.Resources.payload_snapshot_enabled +
(manifest.DynamicPartitionMetadata.SnapshotEnabled ? Properties.Resources.yes :
Properties.Resources.no));
foreach (DynamicPartitionGroup group in manifest.DynamicPartitionMetadata.Groups)
{
dynamicPartitionMetaAppend(Properties.Resources.payload_partition_group_name + group.Name);
dynamicPartitionMetaAppend(Properties.Resources.payload_partition_group_size
+ Helper.byte2AUnit(group.Size));
dynamicPartitionMetaAppend(Properties.Resources.payload_partition_group_include);
foreach (string partition in group.PartitionNames)
dynamicPartitionMetaAppend(partition);
}
}
else
{
dynamicPartitionMetaAppend(Properties.Resources.payload_no_dynamic_metadata);
}
}
static void appendOperationBreakdown()
{
IReadOnlyList<KeyValuePair<string, int>> counts = payload.OperationCounts();
int total = counts.Sum(pair => pair.Value);
payloadInfoListAppend(Properties.Resources.payload_operations, total.ToString());
foreach (KeyValuePair<string, int> pair in counts)
{
double share = total > 0 ? 100.0 * pair.Value / total : 0;
payloadInfoListAppend(" " + pair.Key, pair.Value + " (" + share.ToString("F1") + "%)");
}
}
/// <summary>
/// Describes a signature block without indexing into it blindly: a payload may carry
/// no signatures at all, which used to throw before anything was shown.
/// </summary>
static string describeSignature(ChromeosUpdateEngine.Signatures signatures)
{
if (signatures == null || signatures.Signatures_.Count == 0)
return Properties.Resources.no;
ChromeosUpdateEngine.Signatures.Types.Signature first = signatures.Signatures_[0];
string summary = signatures.Signatures_.Count + " x ";
return summary + (first.HasData ? first.Data.ToBase64() : Properties.Resources.unknown);
}
static string describeSupport(PayloadPartitionInfo part)
{
switch (part.Support)
{
case OperationSupportKind.Supported:
return Properties.Resources.yes;
case OperationSupportKind.RequiresSource:
return Properties.Resources.no + " (needs source image)";
default:
return Properties.Resources.no;
}
}
class payload_info_row
{
public string title { get; }
public string value { get; }
public payload_info_row(string title, string value)
{
this.title = title;
this.value = Helper.ltr(value);
}
}
static Helper.ListHelper<payload_partition_info_row> listHelper;
static void payloadInfoListAppend(string title, string value)
{
MainWindow.THIS.payload_info.Items.Add(new payload_info_row(title, value));
}
class payload_partition_info_row
{
public string name { get; }
public string size { get; }
public string hash { get; }
public string codecs { get; }
public string extractable { get; }
public payload_partition_info_row(
string name, string size, string hash, string codecs, string extractable)
{
this.name = name;
this.size = size;
this.hash = hash;
this.codecs = codecs;
this.extractable = extractable;
}
}
static void dynamicPartitionMetaAppend(string line)
{
MainWindow.THIS.payload_dynamic_partition_meta.Items.Add(new payload_dynamic_partition_meta_row(line));
}
class payload_dynamic_partition_meta_row
{
public string line { get; }
public payload_dynamic_partition_meta_row(string line)
{
this.line = line;
}
}
static void payloadPartitionInfoListAppend(
string name, string size, string hash, string codecs, string extractable)
{
listHelper.addItem(new payload_partition_info_row(name, size, hash, codecs, extractable));
}
public static void init()
{
cur_status = page_status.empty;
actionInit();
switchMainView();
}
}
}