forked from jkcoxson/idevice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrestore.cpp
More file actions
788 lines (710 loc) · 26.6 KB
/
Copy pathrestore.cpp
File metadata and controls
788 lines (710 loc) · 26.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
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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
// Jackson Coxson
#include <cstdlib>
#include <cstring>
#include <idevice++/bindings.hpp>
#include <idevice++/restore.hpp>
namespace IdeviceFFI {
namespace {
IdeviceFfiError* make_ffi_error(int32_t code, const char* msg) {
auto* err = static_cast<IdeviceFfiError*>(malloc(sizeof(IdeviceFfiError)));
err->code = code;
err->sub_code = 0;
err->message = strdup(msg);
return err;
}
/// Copies an FFI-owned byte buffer into a vector and frees the original.
std::vector<uint8_t> take_bytes(uint8_t* data, size_t len) {
std::vector<uint8_t> out;
if (data && len) {
out.assign(data, data + len);
}
if (data) {
::idevice_data_free(data, len);
}
return out;
}
// ---- ComponentSource trampolines ------------------------------------------
extern "C" IdeviceFfiError*
component_read_trampoline(const char* path, uint8_t** out_data, size_t* out_len, void* ctx) {
auto& cb = *static_cast<ComponentSource*>(ctx);
*out_data = nullptr;
*out_len = 0;
if (!cb.read_component) {
return make_ffi_error(-1, "no read_component callback");
}
try {
auto data = cb.read_component(path ? path : "");
*out_len = data.size();
if (!data.empty()) {
*out_data = static_cast<uint8_t*>(malloc(data.size()));
memcpy(*out_data, data.data(), data.size());
}
return nullptr;
} catch (const std::exception& e) {
return make_ffi_error(-1, e.what());
} catch (...) {
return make_ffi_error(-1, "read_component failed");
}
}
// ---- FilesystemImage trampolines ------------------------------------------
extern "C" IdeviceFfiError* fs_size_trampoline(uint64_t* out_size, void* ctx) {
auto& cb = *static_cast<FilesystemImage*>(ctx);
if (!cb.size) {
return make_ffi_error(-1, "no size callback");
}
try {
*out_size = cb.size();
return nullptr;
} catch (const std::exception& e) {
return make_ffi_error(-1, e.what());
} catch (...) {
return make_ffi_error(-1, "size failed");
}
}
extern "C" IdeviceFfiError*
fs_read_at_trampoline(uint64_t offset, size_t len, uint8_t** out_data, size_t* out_len, void* ctx) {
auto& cb = *static_cast<FilesystemImage*>(ctx);
*out_data = nullptr;
*out_len = 0;
if (!cb.read_at) {
return make_ffi_error(-1, "no read_at callback");
}
try {
auto data = cb.read_at(offset, len);
*out_len = data.size();
if (!data.empty()) {
*out_data = static_cast<uint8_t*>(malloc(data.size()));
memcpy(*out_data, data.data(), data.size());
}
return nullptr;
} catch (const std::exception& e) {
return make_ffi_error(-1, e.what());
} catch (...) {
return make_ffi_error(-1, "read_at failed");
}
}
// ---- DataPortConnector trampoline -----------------------------------------
extern "C" IdeviceFfiError*
data_port_connect_trampoline(uint16_t port, IdeviceHandle** out_idevice, void* ctx) {
auto& cb = *static_cast<DataPortConnector*>(ctx);
*out_idevice = nullptr;
if (!cb.connect) {
return make_ffi_error(-1, "no connect callback");
}
try {
Idevice dev = cb.connect(port);
*out_idevice = dev.release();
return nullptr;
} catch (const std::exception& e) {
return make_ffi_error(-1, e.what());
} catch (...) {
return make_ffi_error(-1, "data port connect failed");
}
}
// ---- RestoreProgress trampolines ------------------------------------------
extern "C" void progress_operation_trampoline(uint64_t operation, uint64_t progress, void* ctx) {
auto& cb = *static_cast<RestoreProgress*>(ctx);
if (cb.operation) {
cb.operation(operation, progress);
}
}
extern "C" void progress_step_trampoline(const char* name, void* ctx) {
auto& cb = *static_cast<RestoreProgress*>(ctx);
if (cb.step) {
cb.step(name ? name : "");
}
}
extern "C" void progress_transfer_trampoline(
const char* component, uint64_t sent, uint64_t total, bool has_total, void* ctx) {
auto& cb = *static_cast<RestoreProgress*>(ctx);
if (cb.transfer) {
cb.transfer(component ? component : "", sent, has_total, total);
}
}
} // anonymous namespace
// ===========================================================================
// Ipsw
// ===========================================================================
Result<Ipsw, FfiError> Ipsw::open(const std::string& path) {
IpswHandle* handle = nullptr;
FfiError e(::idevice_ipsw_open(path.c_str(), &handle));
if (e) {
return Err(e);
}
return Ok(Ipsw::adopt(handle));
}
Result<plist_t, FfiError> Ipsw::build_manifest() {
plist_t manifest = nullptr;
FfiError e(::idevice_ipsw_build_manifest(this->raw(), &manifest));
if (e) {
return Err(e);
}
return Ok(manifest);
}
Result<std::vector<uint8_t>, FfiError> Ipsw::read_component(plist_t build_identity,
const std::string& name) {
uint8_t* data = nullptr;
size_t len = 0;
FfiError e(
::idevice_ipsw_read_component(this->raw(), build_identity, name.c_str(), &data, &len));
if (e) {
return Err(e);
}
return Ok(take_bytes(data, len));
}
Result<std::vector<uint8_t>, FfiError> Ipsw::read_file(const std::string& path) {
uint8_t* data = nullptr;
size_t len = 0;
FfiError e(::idevice_ipsw_read_file(this->raw(), path.c_str(), &data, &len));
if (e) {
return Err(e);
}
return Ok(take_bytes(data, len));
}
Result<void, FfiError> Ipsw::extract_to_file(const std::string& entry, const std::string& dest) {
FfiError e(::idevice_ipsw_extract_to_file(this->raw(), entry.c_str(), dest.c_str()));
if (e) {
return Err(e);
}
return Ok();
}
// ===========================================================================
// RestoredClient
// ===========================================================================
Result<RestoredClient, FfiError> RestoredClient::connect(Idevice&& device) {
RestoredClientHandle* handle = nullptr;
FfiError e(::idevice_restored_connect(device.release(), &handle));
if (e) {
return Err(e);
}
return Ok(RestoredClient::adopt(handle));
}
Result<RestoredClient, FfiError> RestoredClient::connect_by_ecid(const UsbmuxdAddr& addr,
uint64_t ecid,
const std::string& label,
uint64_t timeout_ms) {
RestoredClientHandle* handle = nullptr;
FfiError e(
::idevice_restored_connect_by_ecid(addr.raw(), ecid, label.c_str(), timeout_ms, &handle));
if (e) {
return Err(e);
}
return Ok(RestoredClient::adopt(handle));
}
Result<uint64_t, FfiError> RestoredClient::get_ecid() {
uint64_t ecid = 0;
FfiError e(::idevice_restored_get_ecid(this->raw(), &ecid));
if (e) {
return Err(e);
}
return Ok(ecid);
}
Option<uint32_t> RestoredClient::device_id() {
uint32_t id = 0;
bool has_device_id = false;
FfiError e(::idevice_restored_get_device_id(this->raw(), &id, &has_device_id));
if (e || !has_device_id) {
return None;
}
return Some(id);
}
// ===========================================================================
// Standalone helpers
// ===========================================================================
Result<plist_t, FfiError> restore_options_new() {
plist_t options = nullptr;
FfiError e(::idevice_restore_options_new(&options));
if (e) {
return Err(e);
}
return Ok(options);
}
Result<plist_t, FfiError> select_build_identity(plist_t build_manifest,
uint64_t board_id,
uint64_t chip_id,
Option<std::string> restore_behavior) {
plist_t identity = nullptr;
const char* behavior = restore_behavior.is_some() ? restore_behavior.unwrap().c_str() : nullptr;
FfiError e(::idevice_restore_select_build_identity(
build_manifest, board_id, chip_id, behavior, &identity));
if (e) {
return Err(e);
}
return Ok(identity);
}
Result<std::string, FfiError> component_path(plist_t build_identity, const std::string& name) {
char* path = nullptr;
FfiError e(::idevice_restore_component_path(build_identity, name.c_str(), &path));
if (e) {
return Err(e);
}
std::string out = path ? path : "";
if (path) {
::idevice_string_free(path);
}
return Ok(out);
}
bool has_component(plist_t build_identity, const std::string& name) {
return component_path(build_identity, name).is_ok();
}
Result<std::vector<std::string>, FfiError> boot_component_names(plist_t build_identity) {
char* joined = nullptr;
FfiError e(::idevice_restore_boot_component_names(build_identity, &joined));
if (e) {
return Err(e);
}
std::string s = joined ? joined : "";
if (joined) {
::idevice_string_free(joined);
}
std::vector<std::string> out;
size_t start = 0;
while (start < s.size()) {
size_t nl = s.find('\n', start);
if (nl == std::string::npos) {
out.push_back(s.substr(start));
break;
}
out.push_back(s.substr(start, nl - start));
start = nl + 1;
}
return Ok(out);
}
Result<Idevice, FfiError> connect_usb_port(const UsbmuxdAddr& addr,
uint32_t device_id,
uint16_t port,
const std::string& label) {
IdeviceHandle* handle = nullptr;
FfiError e(
::idevice_restore_connect_usb_port(addr.raw(), device_id, port, label.c_str(), &handle));
if (e) {
return Err(e);
}
return Ok(Idevice::adopt(handle));
}
Result<std::vector<uint8_t>, FfiError> fetch_ap_ticket(plist_t build_identity,
uint64_t board_id,
uint64_t chip_id,
uint64_t ecid,
const std::vector<uint8_t>& ap_nonce,
const std::vector<uint8_t>& sep_nonce) {
uint8_t* ticket = nullptr;
size_t ticket_len = 0;
FfiError e(::idevice_restore_fetch_ap_ticket(build_identity,
board_id,
chip_id,
ecid,
ap_nonce.empty() ? nullptr : ap_nonce.data(),
ap_nonce.size(),
sep_nonce.empty() ? nullptr : sep_nonce.data(),
sep_nonce.size(),
&ticket,
&ticket_len));
if (e) {
return Err(e);
}
return Ok(take_bytes(ticket, ticket_len));
}
Result<std::vector<uint8_t>, FfiError> img4_stitch_component(const std::vector<uint8_t>& im4p,
const std::vector<uint8_t>& ticket,
const std::vector<uint8_t>& fourcc) {
uint8_t* out = nullptr;
size_t out_len = 0;
const uint8_t* fourcc_ptr = fourcc.size() == 4 ? fourcc.data() : nullptr;
FfiError e(::idevice_img4_stitch_component(
im4p.data(), im4p.size(), ticket.data(), ticket.size(), fourcc_ptr, &out, &out_len));
if (e) {
return Err(e);
}
return Ok(take_bytes(out, out_len));
}
Option<std::vector<uint8_t>> img4_restore_fourcc_override(const std::string& component_name) {
uint8_t fourcc[4] = {0, 0, 0, 0};
if (::idevice_img4_restore_fourcc_override(component_name.c_str(), fourcc)) {
return Option<std::vector<uint8_t>>(std::vector<uint8_t>(fourcc, fourcc + 4));
}
return None;
}
// ===========================================================================
// Preboard stashbag
// ===========================================================================
Result<std::vector<uint8_t>, FfiError>
build_preboard_manifest(plist_t build_identity, uint64_t board_id, uint64_t chip_id) {
uint8_t* data = nullptr;
size_t len = 0;
FfiError e(
::idevice_restore_build_preboard_manifest(build_identity, board_id, chip_id, &data, &len));
if (e) {
return Err(e);
}
return Ok(take_bytes(data, len));
}
// ===========================================================================
// CancelHandle
// ===========================================================================
CancelHandle::CancelHandle() : handle_(nullptr) {
// A NULL out-handle is the only failure mode, so this cannot fail here.
::idevice_restore_cancel_handle_new(&handle_);
}
CancelHandle::~CancelHandle() {
if (handle_) {
::idevice_restore_cancel_handle_free(handle_);
}
}
CancelHandle::CancelHandle(CancelHandle&& other) noexcept : handle_(other.handle_) {
other.handle_ = nullptr;
}
CancelHandle& CancelHandle::operator=(CancelHandle&& other) noexcept {
if (this != &other) {
if (handle_) {
::idevice_restore_cancel_handle_free(handle_);
}
handle_ = other.handle_;
other.handle_ = nullptr;
}
return *this;
}
void CancelHandle::cancel() {
if (handle_) {
::idevice_restore_cancel(handle_);
}
}
// ===========================================================================
// run_restore
// ===========================================================================
Result<void, FfiError> restore_run(RestoredClient& client,
plist_t build_identity,
uint64_t board_id,
uint64_t chip_id,
uint64_t ecid,
const std::vector<uint8_t>& tss_ticket,
ComponentSource& components,
FilesystemImage* filesystem,
DataPortConnector& data_ports,
RestoreProgress* progress,
CancelHandle* cancel,
plist_t options) {
IdeviceRestoreComponentSourceFFI comp_ffi{};
comp_ffi.context = &components;
comp_ffi.read_component = component_read_trampoline;
comp_ffi.open_component = nullptr;
comp_ffi.read_chunk = nullptr;
comp_ffi.close_component = nullptr;
IdeviceRestoreDataPortConnectorFFI ports_ffi{};
ports_ffi.context = &data_ports;
ports_ffi.connect = data_port_connect_trampoline;
IdeviceRestoreFilesystemImageFFI fs_ffi{};
IdeviceRestoreFilesystemImageFFI* fs_ptr = nullptr;
if (filesystem) {
fs_ffi.context = filesystem;
fs_ffi.size = fs_size_trampoline;
fs_ffi.read_at = fs_read_at_trampoline;
fs_ptr = &fs_ffi;
}
IdeviceRestoreProgressFFI prog_ffi{};
IdeviceRestoreProgressFFI* prog_ptr = nullptr;
if (progress) {
prog_ffi.context = progress;
prog_ffi.operation = progress->operation ? progress_operation_trampoline : nullptr;
prog_ffi.step = progress->step ? progress_step_trampoline : nullptr;
prog_ffi.transfer = progress->transfer ? progress_transfer_trampoline : nullptr;
prog_ptr = &prog_ffi;
}
FfiError e(::idevice_restore_run(client.raw(),
build_identity,
board_id,
chip_id,
ecid,
tss_ticket.empty() ? nullptr : tss_ticket.data(),
tss_ticket.size(),
&comp_ffi,
fs_ptr,
&ports_ffi,
prog_ptr,
cancel ? cancel->raw() : nullptr,
options));
if (e) {
return Err(e);
}
return Ok();
}
// ===========================================================================
// Recovery / DFU device
// ===========================================================================
namespace {
extern "C" IdeviceFfiError* rt_control_out(uint8_t request_type,
uint8_t request,
uint16_t value,
uint16_t index,
const uint8_t* data,
size_t data_len,
uint32_t timeout_ms,
size_t* out_transferred,
void* ctx) {
auto& cb = *static_cast<RecoveryTransport*>(ctx);
if (!cb.control_out) {
return make_ffi_error(-1, "no control_out callback");
}
try {
*out_transferred =
cb.control_out(request_type, request, value, index, data, data_len, timeout_ms);
return nullptr;
} catch (const std::exception& e) {
return make_ffi_error(-1, e.what());
} catch (...) {
return make_ffi_error(-1, "control_out failed");
}
}
extern "C" IdeviceFfiError* rt_control_in(uint8_t request_type,
uint8_t request,
uint16_t value,
uint16_t index,
uint16_t length,
uint32_t timeout_ms,
uint8_t** out_data,
size_t* out_len,
void* ctx) {
auto& cb = *static_cast<RecoveryTransport*>(ctx);
*out_data = nullptr;
*out_len = 0;
if (!cb.control_in) {
return make_ffi_error(-1, "no control_in callback");
}
try {
auto data = cb.control_in(request_type, request, value, index, length, timeout_ms);
*out_len = data.size();
if (!data.empty()) {
*out_data = static_cast<uint8_t*>(malloc(data.size()));
memcpy(*out_data, data.data(), data.size());
}
return nullptr;
} catch (const std::exception& e) {
return make_ffi_error(-1, e.what());
} catch (...) {
return make_ffi_error(-1, "control_in failed");
}
}
extern "C" IdeviceFfiError* rt_bulk_out(uint8_t endpoint,
const uint8_t* data,
size_t data_len,
uint32_t timeout_ms,
size_t* out_transferred,
void* ctx) {
auto& cb = *static_cast<RecoveryTransport*>(ctx);
if (!cb.bulk_out) {
return make_ffi_error(-1, "no bulk_out callback");
}
try {
*out_transferred = cb.bulk_out(endpoint, data, data_len, timeout_ms);
return nullptr;
} catch (const std::exception& e) {
return make_ffi_error(-1, e.what());
} catch (...) {
return make_ffi_error(-1, "bulk_out failed");
}
}
extern "C" IdeviceFfiError* rt_serial_number(char* buf, size_t buf_len, void* ctx) {
auto& cb = *static_cast<RecoveryTransport*>(ctx);
if (!cb.serial_number) {
return make_ffi_error(-1, "no serial_number callback");
}
try {
std::string s = cb.serial_number();
if (buf_len == 0) {
return make_ffi_error(-1, "serial buffer too small");
}
size_t n = s.size() < buf_len - 1 ? s.size() : buf_len - 1;
memcpy(buf, s.data(), n);
buf[n] = '\0';
return nullptr;
} catch (const std::exception& e) {
return make_ffi_error(-1, e.what());
} catch (...) {
return make_ffi_error(-1, "serial_number failed");
}
}
extern "C" uint16_t rt_product_id(void* ctx) {
auto& cb = *static_cast<RecoveryTransport*>(ctx);
return cb.product_id ? cb.product_id() : 0;
}
extern "C" IdeviceFfiError* rt_set_configuration(uint8_t configuration, void* ctx) {
auto& cb = *static_cast<RecoveryTransport*>(ctx);
if (!cb.set_configuration) {
return nullptr;
}
try {
cb.set_configuration(configuration);
return nullptr;
} catch (const std::exception& e) {
return make_ffi_error(-1, e.what());
} catch (...) {
return make_ffi_error(-1, "set_configuration failed");
}
}
extern "C" IdeviceFfiError* rt_claim_interface(uint8_t interface, uint8_t alt_setting, void* ctx) {
auto& cb = *static_cast<RecoveryTransport*>(ctx);
if (!cb.claim_interface) {
return nullptr;
}
try {
cb.claim_interface(interface, alt_setting);
return nullptr;
} catch (const std::exception& e) {
return make_ffi_error(-1, e.what());
} catch (...) {
return make_ffi_error(-1, "claim_interface failed");
}
}
extern "C" IdeviceFfiError* rt_reset(void* ctx) {
auto& cb = *static_cast<RecoveryTransport*>(ctx);
if (!cb.reset) {
return nullptr;
}
try {
cb.reset();
return nullptr;
} catch (const std::exception& e) {
return make_ffi_error(-1, e.what());
} catch (...) {
return make_ffi_error(-1, "reset failed");
}
}
extern "C" IdeviceFfiError*
fdr_connect_trampoline(uint16_t port, IdeviceHandle** out_idevice, void* ctx) {
auto& cb = *static_cast<FdrConnector*>(ctx);
*out_idevice = nullptr;
if (!cb.connect_device_port) {
return make_ffi_error(-1, "no connect_device_port callback");
}
try {
Idevice dev = cb.connect_device_port(port);
*out_idevice = dev.release();
return nullptr;
} catch (const std::exception& e) {
return make_ffi_error(-1, e.what());
} catch (...) {
return make_ffi_error(-1, "fdr connect failed");
}
}
IdeviceRestoreRecoveryTransportFFI make_transport_ffi(RecoveryTransport& t) {
IdeviceRestoreRecoveryTransportFFI d{};
d.context = &t;
d.control_out = rt_control_out;
d.control_in = rt_control_in;
d.bulk_out = rt_bulk_out;
d.serial_number = rt_serial_number;
d.product_id = rt_product_id;
d.set_configuration = rt_set_configuration;
d.claim_interface = rt_claim_interface;
d.reset = rt_reset;
return d;
}
} // anonymous namespace
Result<RecoveryDevice, FfiError> RecoveryDevice::open(RecoveryTransport& transport) {
auto ffi = make_transport_ffi(transport);
RecoveryDeviceHandle* handle = nullptr;
FfiError e(::idevice_recovery_device_new(&ffi, &handle));
if (e) {
return Err(e);
}
return Ok(RecoveryDevice::adopt(handle));
}
Result<void, FfiError> RecoveryDevice::send_command(const std::string& command, uint8_t b_request) {
FfiError e(::idevice_recovery_send_command(this->raw(), command.c_str(), b_request));
if (e) {
return Err(e);
}
return Ok();
}
Result<void, FfiError> RecoveryDevice::send_buffer(const uint8_t* data, size_t len) {
FfiError e(::idevice_recovery_send_buffer(this->raw(), data, len));
if (e) {
return Err(e);
}
return Ok();
}
Result<std::vector<uint8_t>, FfiError> RecoveryDevice::getenv(const std::string& name) {
uint8_t* data = nullptr;
size_t len = 0;
FfiError e(::idevice_recovery_getenv(this->raw(), name.c_str(), &data, &len));
if (e) {
return Err(e);
}
return Ok(take_bytes(data, len));
}
Result<void, FfiError> RecoveryDevice::setenv(const std::string& name, const std::string& value) {
FfiError e(::idevice_recovery_setenv(this->raw(), name.c_str(), value.c_str()));
if (e) {
return Err(e);
}
return Ok();
}
Result<void, FfiError> RecoveryDevice::set_autoboot(bool enable) {
FfiError e(::idevice_recovery_set_autoboot(this->raw(), enable));
if (e) {
return Err(e);
}
return Ok();
}
Result<void, FfiError> RecoveryDevice::finish_transfer() {
FfiError e(::idevice_recovery_finish_transfer(this->raw()));
if (e) {
return Err(e);
}
return Ok();
}
Result<void, FfiError> RecoveryDevice::reboot() {
FfiError e(::idevice_recovery_reboot(this->raw()));
if (e) {
return Err(e);
}
return Ok();
}
Result<uint16_t, FfiError> RecoveryDevice::product_id() {
uint16_t pid = 0;
bool is_recovery = false;
FfiError e(::idevice_recovery_get_mode(this->raw(), &pid, &is_recovery));
if (e) {
return Err(e);
}
return Ok(pid);
}
Result<RecoveryInfo, FfiError> RecoveryDevice::info() {
RecoveryInfo info;
uint64_t cpid = 0, bdid = 0, ecid = 0;
bool has_cpid = false, has_bdid = false, has_ecid = false;
FfiError e(::idevice_recovery_get_info(
this->raw(), &cpid, &has_cpid, &bdid, &has_bdid, &ecid, &has_ecid));
if (e) {
return Err(e);
}
if (has_cpid) {
info.cpid = cpid;
}
if (has_bdid) {
info.bdid = bdid;
}
if (has_ecid) {
info.ecid = ecid;
}
return Ok(info);
}
Option<std::vector<uint8_t>> RecoveryDevice::ap_nonce() {
uint8_t* data = nullptr;
size_t len = 0;
if (::idevice_recovery_get_ap_nonce(this->raw(), &data, &len)) {
return Option<std::vector<uint8_t>>(take_bytes(data, len));
}
return None;
}
Result<void, FfiError> fdr_start(FdrConnector& connector) {
IdeviceRestoreFdrConnectorFFI ffi{};
ffi.context = &connector;
ffi.connect_device_port = fdr_connect_trampoline;
FfiError e(::idevice_restore_fdr_start(&ffi));
if (e) {
return Err(e);
}
return Ok();
}
} // namespace IdeviceFFI