aboutsummaryrefslogtreecommitdiff
path: root/payload_consumer/delta_performer.cc
blob: 519ec716d43e782fb931877b2b359ead7aeb2667 (plain)
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
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
//
// Copyright (C) 2012 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#include "update_engine/payload_consumer/delta_performer.h"

#include <linux/fs.h>

#include <algorithm>
#include <chrono>
#include <cstring>
#include <memory>
#include <set>
#include <string>
#include <utility>
#include <vector>

#include <android-base/properties.h>
#include <android-base/strings.h>
#include <base/files/file_util.h>
#include <base/format_macros.h>
#include <base/metrics/histogram_macros.h>
#include <base/strings/string_number_conversions.h>
#include <base/strings/stringprintf.h>
#include <base/time/time.h>
#include <brillo/data_encoding.h>
#include <bsdiff/bspatch.h>
#include <google/protobuf/repeated_field.h>
#include <puffin/puffpatch.h>

#include "libsnapshot/cow_format.h"
#include "update_engine/common/constants.h"
#include "update_engine/common/download_action.h"
#include "update_engine/common/error_code.h"
#include "update_engine/common/error_code_utils.h"
#include "update_engine/common/hardware_interface.h"
#include "update_engine/common/prefs_interface.h"
#include "update_engine/common/terminator.h"
#include "update_engine/common/utils.h"
#include "update_engine/payload_consumer/partition_update_generator_interface.h"
#include "update_engine/payload_consumer/partition_writer.h"
#include "update_engine/update_metadata.pb.h"
#if USE_FEC
#include "update_engine/payload_consumer/fec_file_descriptor.h"
#endif  // USE_FEC
#include "update_engine/payload_consumer/payload_constants.h"
#include "update_engine/payload_consumer/payload_verifier.h"

using google::protobuf::RepeatedPtrField;
using std::min;
using std::string;
using std::vector;

namespace chromeos_update_engine {
const unsigned DeltaPerformer::kProgressLogMaxChunks = 10;
const unsigned DeltaPerformer::kProgressLogTimeoutSeconds = 30;
const unsigned DeltaPerformer::kProgressDownloadWeight = 50;
const unsigned DeltaPerformer::kProgressOperationsWeight = 50;
const uint64_t DeltaPerformer::kCheckpointFrequencySeconds = 1;

namespace {
const int kUpdateStateOperationInvalid = -1;
const int kMaxResumedUpdateFailures = 10;

}  // namespace

// Computes the ratio of |part| and |total|, scaled to |norm|, using integer
// arithmetic.
static uint64_t IntRatio(uint64_t part, uint64_t total, uint64_t norm) {
  return part * norm / total;
}

void DeltaPerformer::LogProgress(const char* message_prefix) {
  // Format operations total count and percentage.
  string total_operations_str("?");
  string completed_percentage_str("");
  if (num_total_operations_) {
    total_operations_str = std::to_string(num_total_operations_);
    // Upcasting to 64-bit to avoid overflow, back to size_t for formatting.
    completed_percentage_str = base::StringPrintf(
        " (%" PRIu64 "%%)",
        IntRatio(next_operation_num_, num_total_operations_, 100));
  }

  // Format download total count and percentage.
  size_t payload_size = payload_->size;
  string payload_size_str("?");
  string downloaded_percentage_str("");
  if (payload_size) {
    payload_size_str = std::to_string(payload_size);
    // Upcasting to 64-bit to avoid overflow, back to size_t for formatting.
    downloaded_percentage_str = base::StringPrintf(
        " (%" PRIu64 "%%)", IntRatio(total_bytes_received_, payload_size, 100));
  }

  LOG(INFO) << (message_prefix ? message_prefix : "") << next_operation_num_
            << "/" << total_operations_str << " operations"
            << completed_percentage_str << ", " << total_bytes_received_ << "/"
            << payload_size_str << " bytes downloaded"
            << downloaded_percentage_str << ", overall progress "
            << overall_progress_ << "%";
}

void DeltaPerformer::UpdateOverallProgress(bool force_log,
                                           const char* message_prefix) {
  // Compute our download and overall progress.
  unsigned new_overall_progress = 0;
  static_assert(kProgressDownloadWeight + kProgressOperationsWeight == 100,
                "Progress weights don't add up");
  // Only consider download progress if its total size is known; otherwise
  // adjust the operations weight to compensate for the absence of download
  // progress. Also, make sure to cap the download portion at
  // kProgressDownloadWeight, in case we end up downloading more than we
  // initially expected (this indicates a problem, but could generally happen).
  // TODO(garnold) the correction of operations weight when we do not have the
  // total payload size, as well as the conditional guard below, should both be
  // eliminated once we ensure that the payload_size in the install plan is
  // always given and is non-zero. This currently isn't the case during unit
  // tests (see chromium-os:37969).
  size_t payload_size = payload_->size;
  unsigned actual_operations_weight = kProgressOperationsWeight;
  if (payload_size)
    new_overall_progress +=
        min(static_cast<unsigned>(IntRatio(
                total_bytes_received_, payload_size, kProgressDownloadWeight)),
            kProgressDownloadWeight);
  else
    actual_operations_weight += kProgressDownloadWeight;

  // Only add completed operations if their total number is known; we definitely
  // expect an update to have at least one operation, so the expectation is that
  // this will eventually reach |actual_operations_weight|.
  if (num_total_operations_)
    new_overall_progress += IntRatio(
        next_operation_num_, num_total_operations_, actual_operations_weight);

  // Progress ratio cannot recede, unless our assumptions about the total
  // payload size, total number of operations, or the monotonicity of progress
  // is breached.
  if (new_overall_progress < overall_progress_) {
    LOG(WARNING) << "progress counter receded from " << overall_progress_
                 << "% down to " << new_overall_progress << "%; this is a bug";
    force_log = true;
  }
  overall_progress_ = new_overall_progress;

  // Update chunk index, log as needed: if forced by called, or we completed a
  // progress chunk, or a timeout has expired.
  base::TimeTicks curr_time = base::TimeTicks::Now();
  unsigned curr_progress_chunk =
      overall_progress_ * kProgressLogMaxChunks / 100;
  if (force_log || curr_progress_chunk > last_progress_chunk_ ||
      curr_time > forced_progress_log_time_) {
    forced_progress_log_time_ = curr_time + forced_progress_log_wait_;
    LogProgress(message_prefix);
  }
  last_progress_chunk_ = curr_progress_chunk;
}

size_t DeltaPerformer::CopyDataToBuffer(const char** bytes_p,
                                        size_t* count_p,
                                        size_t max) {
  const size_t count = *count_p;
  if (!count)
    return 0;  // Special case shortcut.
  size_t read_len = min(count, max - buffer_.size());
  const char* bytes_start = *bytes_p;
  const char* bytes_end = bytes_start + read_len;
  buffer_.reserve(max);
  buffer_.insert(buffer_.end(), bytes_start, bytes_end);
  *bytes_p = bytes_end;
  *count_p = count - read_len;
  return read_len;
}

bool DeltaPerformer::HandleOpResult(bool op_result,
                                    const char* op_type_name,
                                    ErrorCode* error) {
  if (op_result)
    return true;

  LOG(ERROR) << "Failed to perform " << op_type_name << " operation "
             << next_operation_num_ << ", which is the operation "
             << GetPartitionOperationNum() << " in partition \""
             << partitions_[current_partition_].partition_name() << "\"";
  if (*error == ErrorCode::kSuccess)
    *error = ErrorCode::kDownloadOperationExecutionError;
  return false;
}

int DeltaPerformer::Close() {
  // Checkpoint update progress before canceling, so that subsequent attempts
  // can resume from exactly where update_engine left last time.
  CheckpointUpdateProgress(true);
  int err = -CloseCurrentPartition();
  LOG_IF(ERROR,
         !payload_hash_calculator_.Finalize() ||
             !signed_hash_calculator_.Finalize())
      << "Unable to finalize the hash.";
  if (!buffer_.empty()) {
    LOG(INFO) << "Discarding " << buffer_.size() << " unused downloaded bytes";
    if (err >= 0)
      err = 1;
  }
  return -err;
}

int DeltaPerformer::CloseCurrentPartition() {
  if (!partition_writer_) {
    return 0;
  }
  int err = partition_writer_->Close();
  partition_writer_ = nullptr;
  return err;
}

bool DeltaPerformer::OpenCurrentPartition() {
  if (current_partition_ >= partitions_.size())
    return false;

  const PartitionUpdate& partition = partitions_[current_partition_];
  size_t num_previous_partitions =
      install_plan_->partitions.size() - partitions_.size();
  const InstallPlan::Partition& install_part =
      install_plan_->partitions[num_previous_partitions + current_partition_];
  auto dynamic_control = boot_control_->GetDynamicPartitionControl();
  partition_writer_ = CreatePartitionWriter(
      partition,
      install_part,
      dynamic_control,
      block_size_,
      interactive_,
      IsDynamicPartition(install_part.name, install_plan_->target_slot));
  // Open source fds if we have a delta payload, or for partitions in the
  // partial update.
  const bool source_may_exist = manifest_.partial_update() ||
                                payload_->type == InstallPayloadType::kDelta;
  const size_t partition_operation_num = GetPartitionOperationNum();

  TEST_AND_RETURN_FALSE(partition_writer_->Init(
      install_plan_, source_may_exist, partition_operation_num));
  CheckpointUpdateProgress(true);
  return true;
}

size_t DeltaPerformer::GetPartitionOperationNum() {
  return next_operation_num_ -
         (current_partition_ ? acc_num_operations_[current_partition_ - 1] : 0);
}

namespace {

void LogPartitionInfoHash(const PartitionInfo& info, const string& tag) {
  string sha256 = HexEncode(info.hash());
  LOG(INFO) << "PartitionInfo " << tag << " sha256: " << sha256
            << " size: " << info.size();
}

void LogPartitionInfo(const vector<PartitionUpdate>& partitions) {
  for (const PartitionUpdate& partition : partitions) {
    if (partition.has_old_partition_info()) {
      LogPartitionInfoHash(partition.old_partition_info(),
                           "old " + partition.partition_name());
    }
    LogPartitionInfoHash(partition.new_partition_info(),
                         "new " + partition.partition_name());
  }
}

}  // namespace

bool DeltaPerformer::IsHeaderParsed() const {
  return metadata_size_ != 0;
}

MetadataParseResult DeltaPerformer::ParsePayloadMetadata(
    const brillo::Blob& payload, ErrorCode* error) {
  *error = ErrorCode::kSuccess;

  if (!IsHeaderParsed()) {
    MetadataParseResult result =
        payload_metadata_.ParsePayloadHeader(payload, error);
    if (result != MetadataParseResult::kSuccess)
      return result;

    metadata_size_ = payload_metadata_.GetMetadataSize();
    metadata_signature_size_ = payload_metadata_.GetMetadataSignatureSize();
    major_payload_version_ = payload_metadata_.GetMajorVersion();

    // If the metadata size is present in install plan, check for it immediately
    // even before waiting for that many number of bytes to be downloaded in the
    // payload. This will prevent any attack which relies on us downloading data
    // beyond the expected metadata size.
    if (install_plan_->hash_checks_mandatory) {
      if (payload_->metadata_size != metadata_size_) {
        LOG(ERROR) << "Mandatory metadata size in Omaha response ("
                   << payload_->metadata_size
                   << ") is missing/incorrect, actual = " << metadata_size_;
        *error = ErrorCode::kDownloadInvalidMetadataSize;
        return MetadataParseResult::kError;
      }
    }

    // Check that the |metadata signature size_| and |metadata_size_| are not
    // very big numbers. This is necessary since |update_engine| needs to write
    // these values into the buffer before being able to use them, and if an
    // attacker sets these values to a very big number, the buffer will overflow
    // and |update_engine| will crash. A simple way of solving this is to check
    // that the size of both values is smaller than the payload itself.
    if (metadata_size_ + metadata_signature_size_ > payload_->size) {
      LOG(ERROR) << "The size of the metadata_size(" << metadata_size_ << ")"
                 << " or metadata signature(" << metadata_signature_size_ << ")"
                 << " is greater than the size of the payload" << "("
                 << payload_->size << ")";
      *error = ErrorCode::kDownloadInvalidMetadataSize;
      return MetadataParseResult::kError;
    }
  }

  // Now that we have validated the metadata size, we should wait for the full
  // metadata and its signature (if exist) to be read in before we can parse it.
  if (payload.size() < metadata_size_ + metadata_signature_size_)
    return MetadataParseResult::kInsufficientData;

  // Log whether we validated the size or simply trusting what's in the payload
  // here. This is logged here (after we received the full metadata data) so
  // that we just log once (instead of logging n times) if it takes n
  // DeltaPerformer::Write calls to download the full manifest.
  if (payload_->metadata_size == metadata_size_) {
    LOG(INFO) << "Manifest size in payload matches expected value from Omaha";
  } else {
    // For mandatory-cases, we'd have already returned a kMetadataParseError
    // above. We'll be here only for non-mandatory cases. Just send a UMA stat.
    LOG(WARNING) << "Ignoring missing/incorrect metadata size ("
                 << payload_->metadata_size
                 << ") in Omaha response as validation is not mandatory. "
                 << "Trusting metadata size in payload = " << metadata_size_;
  }

  // NOLINTNEXTLINE(whitespace/braces)
  auto [payload_verifier, perform_verification] = CreatePayloadVerifier();
  if (!payload_verifier) {
    LOG(ERROR) << "Failed to create payload verifier.";
    *error = ErrorCode::kDownloadMetadataSignatureVerificationError;
    if (perform_verification) {
      return MetadataParseResult::kError;
    }
  } else {
    // We have the full metadata in |payload|. Verify its integrity
    // and authenticity based on the information we have in Omaha response.
    *error = payload_metadata_.ValidateMetadataSignature(
        payload, payload_->metadata_signature, *payload_verifier);
  }
  if (*error != ErrorCode::kSuccess) {
    if (install_plan_->hash_checks_mandatory) {
      // The autoupdate_CatchBadSignatures test checks for this string
      // in log-files. Keep in sync.
      LOG(ERROR) << "Mandatory metadata signature validation failed";
      return MetadataParseResult::kError;
    }

    // For non-mandatory cases, just send a UMA stat.
    LOG(WARNING) << "Ignoring metadata signature validation failures";
    *error = ErrorCode::kSuccess;
  }

  // The payload metadata is deemed valid, it's safe to parse the protobuf.
  if (!payload_metadata_.GetManifest(payload, &manifest_)) {
    LOG(ERROR) << "Unable to parse manifest in update file.";
    *error = ErrorCode::kDownloadManifestParseError;
    return MetadataParseResult::kError;
  }

  manifest_parsed_ = true;
  return MetadataParseResult::kSuccess;
}

#define OP_DURATION_HISTOGRAM(_op_name, _start_time)                        \
  LOCAL_HISTOGRAM_CUSTOM_TIMES(                                             \
      "UpdateEngine.DownloadAction.InstallOperation::" + string(_op_name) + \
          ".Duration",                                                      \
      (base::TimeTicks::Now() - _start_time),                               \
      base::TimeDelta::FromMilliseconds(10),                                \
      base::TimeDelta::FromMinutes(5),                                      \
      20);

bool DeltaPerformer::CheckSPLDowngrade() {
  if (!manifest_.has_security_patch_level()) {
    return true;
  }
  if (manifest_.security_patch_level().empty()) {
    return true;
  }
  const auto new_spl = manifest_.security_patch_level();
  const auto current_spl =
      android::base::GetProperty("ro.build.version.security_patch", "");
  if (current_spl.empty()) {
    LOG(WARNING) << "Failed to get ro.build.version.security_patch, unable to "
                    "determine if this OTA is a SPL downgrade. Assuming this "
                    "OTA is not SPL downgrade.";
    return true;
  }
  if (new_spl < current_spl) {
    const auto avb_state =
        android::base::GetProperty("ro.boot.verifiedbootstate", "green");
    if (android::base::EqualsIgnoreCase(avb_state, "green")) {
      LOG(ERROR) << "Target build SPL " << new_spl
                 << " is older than current build's SPL " << current_spl
                 << ", this OTA is an SPL downgrade. Your device's "
                    "ro.boot.verifiedbootstate="
                 << avb_state
                 << ", it probably has a locked bootlaoder. Since a locked "
                    "bootloader will reject SPL downgrade no matter what, we "
                    "will reject this OTA.";
      return false;
    }
    install_plan_->powerwash_required = true;
    LOG(WARNING)
        << "Target build SPL " << new_spl
        << " is older than current build's SPL " << current_spl
        << ", this OTA is an SPL downgrade. Data wipe will be required";
  }
  return true;
}

// Wrapper around write. Returns true if all requested bytes
// were written, or false on any error, regardless of progress
// and stores an action exit code in |error|.
bool DeltaPerformer::Write(const void* bytes, size_t count, ErrorCode* error) {
  if (!error) {
    LOG(INFO) << "Error Code is not initialized";
    return false;
  }
  *error = ErrorCode::kSuccess;
  const char* c_bytes = reinterpret_cast<const char*>(bytes);

  // Update the total byte downloaded count and the progress logs.
  total_bytes_received_ += count;
  UpdateOverallProgress(false, "Completed ");

  while (!manifest_valid_) {
    bool insufficient_bytes = false;
    if (!ParseManifest(&c_bytes, &count, error, &insufficient_bytes)) {
      LOG(ERROR) << "Failed to parse manifest";
      return false;
    }
    if (insufficient_bytes) {
      return true;
    }
  }

  while (next_operation_num_ < num_total_operations_) {
    // Check if we should cancel the current attempt for any reason.
    // In this case, *error will have already been populated with the reason
    // why we're canceling.
    if (download_delegate_ && download_delegate_->ShouldCancel(error))
      return false;

    // We know there are more operations to perform because we didn't reach the
    // |num_total_operations_| limit yet.
    if (next_operation_num_ >= acc_num_operations_[current_partition_]) {
      if (partition_writer_) {
        if (!partition_writer_->FinishedInstallOps()) {
          *error = ErrorCode::kDownloadWriteError;
          return false;
        }
      }
      const auto err = CloseCurrentPartition();
      if (err < 0) {
        LOG(ERROR) << "Failed to close partition "
                   << partitions_[current_partition_].partition_name() << " "
                   << strerror(-err);
        return false;
      }
      // Skip until there are operations for current_partition_.
      while (next_operation_num_ >= acc_num_operations_[current_partition_]) {
        current_partition_++;
      }
      if (!OpenCurrentPartition()) {
        *error = ErrorCode::kInstallDeviceOpenError;
        return false;
      }
    }

    const InstallOperation& op =
        partitions_[current_partition_].operations(GetPartitionOperationNum());

    CopyDataToBuffer(&c_bytes, &count, op.data_length());

    // Check whether we received all of the next operation's data payload.
    if (!CanPerformInstallOperation(op))
      return true;
    if (!ProcessOperation(&op, error)) {
      LOG(ERROR) << "unable to process operation: "
                 << InstallOperationTypeName(op.type())
                 << " Error: " << utils::ErrorCodeToString(*error);
      return false;
    }

    next_operation_num_++;
    UpdateOverallProgress(false, "Completed ");
    CheckpointUpdateProgress(false);
  }

  if (partition_writer_) {
    TEST_AND_RETURN_FALSE(partition_writer_->FinishedInstallOps());
  }
  CloseCurrentPartition();

  // In major version 2, we don't add unused operation to the payload.
  // If we already extracted the signature we should skip this step.
  if (manifest_.has_signatures_offset() && manifest_.has_signatures_size() &&
      signatures_message_data_.empty()) {
    if (manifest_.signatures_offset() != buffer_offset_) {
      LOG(ERROR) << "Payload signatures offset points to blob offset "
                 << manifest_.signatures_offset()
                 << " but signatures are expected at offset " << buffer_offset_;
      *error = ErrorCode::kDownloadPayloadVerificationError;
      return false;
    }
    CopyDataToBuffer(&c_bytes, &count, manifest_.signatures_size());
    // Needs more data to cover entire signature.
    if (buffer_.size() < manifest_.signatures_size())
      return true;
    if (!ExtractSignatureMessage()) {
      LOG(ERROR) << "Extract payload signature failed.";
      *error = ErrorCode::kDownloadPayloadVerificationError;
      return false;
    }
    DiscardBuffer(true, 0);
    // Since we extracted the SignatureMessage we need to advance the
    // checkpoint, otherwise we would reload the signature and try to extract
    // it again.
    // This is the last checkpoint for an update, force this checkpoint to be
    // saved.
    CheckpointUpdateProgress(true);
  }

  return true;
}

bool DeltaPerformer::ParseManifest(const char** c_bytes,
                                   size_t* count,
                                   ErrorCode* error,
                                   bool* should_return) {
  // Read data up to the needed limit; this is either maximium payload header
  // size, or the full metadata size (once it becomes known).
  const bool do_read_header = !IsHeaderParsed();
  CopyDataToBuffer(
      c_bytes,
      count,
      (do_read_header ? kMaxPayloadHeaderSize
                      : metadata_size_ + metadata_signature_size_));
  MetadataParseResult result = ParsePayloadMetadata(buffer_, error);
  if (result == MetadataParseResult::kError)
    return false;
  if (result == MetadataParseResult::kInsufficientData) {
    // If we just processed the header, make an attempt on the manifest.
    if (do_read_header && IsHeaderParsed()) {
      return true;
    }
    *should_return = true;
    return true;
  }

  // Checks the integrity of the payload manifest.
  if ((*error = ValidateManifest()) != ErrorCode::kSuccess)
    return false;
  manifest_valid_ = true;
  if (!install_plan_->is_resume) {
    auto begin = reinterpret_cast<const char*>(buffer_.data());
    prefs_->SetString(kPrefsManifestBytes, {begin, buffer_.size()});
  }

  // Clear the download buffer.
  DiscardBuffer(false, metadata_size_);

  block_size_ = manifest_.block_size();

  if (!install_plan_->spl_downgrade && !CheckSPLDowngrade()) {
    *error = ErrorCode::kPayloadTimestampError;
    return false;
  }

  // update estimate_cow_size if VABC is disabled
  // new_cow_size per partition = partition_size - (#blocks in Copy
  // operations part of the partition)
  if (install_plan_->vabc_none) {
    LOG(INFO) << "Setting Virtual AB Compression algorithm to none. This "
                 "would also disable VABC XOR as XOR only saves space if "
                 "compression is enabled.";
    manifest_.mutable_dynamic_partition_metadata()->set_vabc_compression_param(
        "none");
    for (auto& partition : *manifest_.mutable_partitions()) {
      if (!partition.has_estimate_cow_size()) {
        continue;
      }
      auto new_cow_size = partition.new_partition_info().size();
      for (const auto& operation : partition.merge_operations()) {
        if (operation.type() == CowMergeOperation::COW_COPY) {
          new_cow_size -=
              operation.dst_extent().num_blocks() * manifest_.block_size();
        }
      }
      // Remove all COW_XOR merge ops, as XOR without compression is useless.
      // It increases CPU usage but does not reduce space usage at all.
      auto&& merge_ops = *partition.mutable_merge_operations();
      merge_ops.erase(std::remove_if(merge_ops.begin(),
                                     merge_ops.end(),
                                     [](const auto& op) {
                                       return op.type() ==
                                              CowMergeOperation::COW_XOR;
                                     }),
                      merge_ops.end());

      // Every block written to COW device will come with a header which
      // stores src/dst block info along with other data.
      const auto cow_metadata_size = partition.new_partition_info().size() /
                                     manifest_.block_size() *
                                     sizeof(android::snapshot::CowOperation);
      // update_engine will emit a label op every op or every two seconds,
      // whichever one is longer. In the worst case, we add 1 label per
      // InstallOp. So take size of label ops into account.
      const auto label_ops_size =
          partition.operations_size() * sizeof(android::snapshot::CowOperation);
      // Adding extra 2MB headroom just for any unexpected space usage.
      // If we overrun reserved COW size, entire OTA will fail
      // and no way for user to retry OTA
      partition.set_estimate_cow_size(new_cow_size + (1024 * 1024 * 2) +
                                      cow_metadata_size + label_ops_size);
      // Setting op count max to 0 will defer to num_blocks as the op buffer
      // size.
      partition.set_estimate_op_count_max(0);
      LOG(INFO) << "New COW size for partition " << partition.partition_name()
                << " is " << partition.estimate_cow_size();
    }
  }
  if (install_plan_->disable_vabc) {
    manifest_.mutable_dynamic_partition_metadata()->set_vabc_enabled(false);
  }
  if (install_plan_->enable_threading) {
    manifest_.mutable_dynamic_partition_metadata()
        ->mutable_vabc_feature_set()
        ->set_threaded(install_plan_->enable_threading.value());
    LOG(INFO) << "Attempting to "
              << (install_plan_->enable_threading.value() ? "enable"
                                                          : "disable")
              << " multi-threaded compression for VABC";
  }
  if (install_plan_->batched_writes) {
    manifest_.mutable_dynamic_partition_metadata()
        ->mutable_vabc_feature_set()
        ->set_batch_writes(true);
    LOG(INFO) << "Attempting to enable batched writes for VABC";
  }

  // This populates |partitions_| and the |install_plan.partitions| with the
  // list of partitions from the manifest.
  if (!ParseManifestPartitions(error))
    return false;

  // |install_plan.partitions| was filled in, nothing need to be done here if
  // the payload was already applied, returns false to terminate http fetcher,
  // but keep |error| as ErrorCode::kSuccess.
  if (payload_->already_applied)
    return false;

  num_total_operations_ = 0;
  for (const auto& partition : partitions_) {
    num_total_operations_ += partition.operations_size();
    acc_num_operations_.push_back(num_total_operations_);
  }

  LOG_IF(WARNING, !prefs_->SetInt64(kPrefsManifestMetadataSize, metadata_size_))
      << "Unable to save the manifest metadata size.";
  LOG_IF(
      WARNING,
      !prefs_->SetInt64(kPrefsManifestSignatureSize, metadata_signature_size_))
      << "Unable to save the manifest signature size.";

  if (!PrimeUpdateState()) {
    *error = ErrorCode::kDownloadStateInitializationError;
    LOG(ERROR) << "Unable to prime the update state.";
    return false;
  }

  if (next_operation_num_ < acc_num_operations_[current_partition_]) {
    if (!OpenCurrentPartition()) {
      *error = ErrorCode::kInstallDeviceOpenError;
      return false;
    }
  }

  if (next_operation_num_ > 0)
    UpdateOverallProgress(true, "Resuming after ");
  LOG(INFO) << "Starting to apply update payload operations";
  return true;
}
bool DeltaPerformer::ProcessOperation(const InstallOperation* op,
                                      ErrorCode* error) {
  // Validate the operation unconditionally. This helps prevent the
  // exploitation of vulnerabilities in the patching libraries, e.g. bspatch.
  // The hash of the patch data for a given operation is embedded in the
  // payload metadata; and thus has been verified against the public key on
  // device.
  // Note: Validate must be called only if CanPerformInstallOperation is
  // called. Otherwise, we might be failing operations before even if there
  // isn't sufficient data to compute the proper hash.
  *error = ValidateOperationHash(*op);
  if (*error != ErrorCode::kSuccess) {
    if (install_plan_->hash_checks_mandatory) {
      LOG(ERROR) << "Mandatory operation hash check failed";
      return false;
    }

    // For non-mandatory cases, just send a UMA stat.
    LOG(WARNING) << "Ignoring operation validation errors";
    *error = ErrorCode::kSuccess;
  }

  // Makes sure we unblock exit when this operation completes.
  ScopedTerminatorExitUnblocker exit_unblocker =
      ScopedTerminatorExitUnblocker();  // Avoids a compiler unused var bug.

  base::TimeTicks op_start_time = base::TimeTicks::Now();

  bool op_result{};
  const string op_name = InstallOperationTypeName(op->type());
  switch (op->type()) {
    case InstallOperation::REPLACE:
    case InstallOperation::REPLACE_BZ:
    case InstallOperation::REPLACE_XZ:
      op_result = PerformReplaceOperation(*op);
      OP_DURATION_HISTOGRAM("REPLACE", op_start_time);
      break;
    case InstallOperation::ZERO:
    case InstallOperation::DISCARD:
      op_result = PerformZeroOrDiscardOperation(*op);
      OP_DURATION_HISTOGRAM("ZERO_OR_DISCARD", op_start_time);
      break;
    case InstallOperation::SOURCE_COPY:
      op_result = PerformSourceCopyOperation(*op, error);
      OP_DURATION_HISTOGRAM("SOURCE_COPY", op_start_time);
      break;
    case InstallOperation::SOURCE_BSDIFF:
    case InstallOperation::BROTLI_BSDIFF:
    case InstallOperation::PUFFDIFF:
    case InstallOperation::ZUCCHINI:
    case InstallOperation::LZ4DIFF_PUFFDIFF:
    case InstallOperation::LZ4DIFF_BSDIFF:
      op_result = PerformDiffOperation(*op, error);
      OP_DURATION_HISTOGRAM(op_name, op_start_time);
      break;
    default:
      op_result = false;
  }
  if (!HandleOpResult(op_result, op_name.c_str(), error))
    return false;

  return true;
}

bool DeltaPerformer::IsManifestValid() {
  return manifest_valid_;
}

bool DeltaPerformer::ParseManifestPartitions(ErrorCode* error) {
  partitions_.assign(manifest_.partitions().begin(),
                     manifest_.partitions().end());

  // For VAB and partial updates, the partition preparation will copy the
  // dynamic partitions metadata to the target metadata slot, and rename the
  // slot suffix of the partitions in the metadata.
  if (install_plan_->target_slot != BootControlInterface::kInvalidSlot) {
    uint64_t required_size = 0;
    if (!PreparePartitionsForUpdate(&required_size, error)) {
      if (*error == ErrorCode::kOverlayfsenabledError) {
        return false;
      } else if (required_size > 0) {
        *error = ErrorCode::kNotEnoughSpace;
      } else {
        *error = ErrorCode::kInstallDeviceOpenError;
      }
      return false;
    }
  }

  // Partitions in manifest are no longer needed after preparing partitions.
  manifest_.clear_partitions();
  // TODO(xunchang) TBD: allow partial update only on devices with dynamic
  // partition.
  if (manifest_.partial_update()) {
    std::set<std::string> touched_partitions;
    for (const auto& partition_update : partitions_) {
      touched_partitions.insert(partition_update.partition_name());
    }

    auto generator = partition_update_generator::Create(boot_control_,
                                                        manifest_.block_size());
    std::vector<PartitionUpdate> untouched_static_partitions;
    if (!generator->GenerateOperationsForPartitionsNotInPayload(
            install_plan_->source_slot,
            install_plan_->target_slot,
            touched_partitions,
            &untouched_static_partitions)) {
      LOG(ERROR)
          << "Failed to generate operations for partitions not in payload "
          << android::base::Join(touched_partitions, ", ");
      *error = ErrorCode::kDownloadStateInitializationError;
      return false;
    }
    partitions_.insert(partitions_.end(),
                       untouched_static_partitions.begin(),
                       untouched_static_partitions.end());

    // Save the untouched dynamic partitions in install plan.
    std::vector<std::string> dynamic_partitions;
    if (!boot_control_->GetDynamicPartitionControl()
             ->ListDynamicPartitionsForSlot(install_plan_->source_slot,
                                            boot_control_->GetCurrentSlot(),
                                            &dynamic_partitions)) {
      LOG(ERROR) << "Failed to load dynamic partitions from slot "
                 << install_plan_->source_slot;
      return false;
    }
    install_plan_->untouched_dynamic_partitions.clear();
    for (const auto& name : dynamic_partitions) {
      if (touched_partitions.find(name) == touched_partitions.end()) {
        install_plan_->untouched_dynamic_partitions.push_back(name);
      }
    }
  }

  const auto start = std::chrono::system_clock::now();
  if (!install_plan_->ParsePartitions(
          partitions_, boot_control_, block_size_, error)) {
    return false;
  }
  const auto duration = std::chrono::system_clock::now() - start;
  LOG(INFO)
      << "ParsePartitions done. took "
      << std::chrono::duration_cast<std::chrono::milliseconds>(duration).count()
      << " ms";

  auto&& has_verity = [](const auto& part) {
    return part.fec_extent().num_blocks() > 0 ||
           part.hash_tree_extent().num_blocks() > 0;
  };
  if (!std::any_of(partitions_.begin(), partitions_.end(), has_verity)) {
    install_plan_->write_verity = false;
  }

  LogPartitionInfo(partitions_);
  return true;
}

bool DeltaPerformer::PreparePartitionsForUpdate(uint64_t* required_size,
                                                ErrorCode* error) {
  // Call static PreparePartitionsForUpdate with hash from
  // kPrefsUpdateCheckResponseHash to ensure hash of payload that space is
  // preallocated for is the same as the hash of payload being applied.
  string update_check_response_hash;
  ignore_result(prefs_->GetString(kPrefsUpdateCheckResponseHash,
                                  &update_check_response_hash));
  return PreparePartitionsForUpdate(prefs_,
                                    boot_control_,
                                    install_plan_->target_slot,
                                    manifest_,
                                    update_check_response_hash,
                                    required_size,
                                    error);
}

bool DeltaPerformer::PreparePartitionsForUpdate(
    PrefsInterface* prefs,
    BootControlInterface* boot_control,
    BootControlInterface::Slot target_slot,
    const DeltaArchiveManifest& manifest,
    const std::string& update_check_response_hash,
    uint64_t* required_size,
    ErrorCode* error) {
  string last_hash;
  ignore_result(
      prefs->GetString(kPrefsDynamicPartitionMetadataUpdated, &last_hash));

  bool is_resume = !update_check_response_hash.empty() &&
                   last_hash == update_check_response_hash;

  if (is_resume) {
    LOG(INFO) << "Using previously prepared partitions for update. hash = "
              << last_hash;
  } else {
    LOG(INFO) << "Preparing partitions for new update. last hash = "
              << last_hash << ", new hash = " << update_check_response_hash;
    ResetUpdateProgress(prefs, false);
  }

  const auto start = std::chrono::system_clock::now();
  if (!boot_control->GetDynamicPartitionControl()->PreparePartitionsForUpdate(
          boot_control->GetCurrentSlot(),
          target_slot,
          manifest,
          !is_resume /* should update */,
          required_size,
          error)) {
    LOG(ERROR) << "Unable to initialize partition metadata for slot "
               << BootControlInterface::SlotName(target_slot) << " "
               << utils::ErrorCodeToString(*error);
    return false;
  }
  const auto duration = std::chrono::system_clock::now() - start;

  TEST_AND_RETURN_FALSE(prefs->SetString(kPrefsDynamicPartitionMetadataUpdated,
                                         update_check_response_hash));
  LOG(INFO)
      << "PreparePartitionsForUpdate done. took "
      << std::chrono::duration_cast<std::chrono::milliseconds>(duration).count()
      << " ms";

  return true;
}

bool DeltaPerformer::CanPerformInstallOperation(
    const chromeos_update_engine::InstallOperation& operation) {
  // If we don't have a data blob we can apply it right away.
  if (!operation.has_data_offset() && !operation.has_data_length())
    return true;

  // See if we have the entire data blob in the buffer
  if (operation.data_offset() < buffer_offset_) {
    LOG(ERROR) << "we threw away data it seems?";
    return false;
  }

  return (operation.data_offset() + operation.data_length() <=
          buffer_offset_ + buffer_.size());
}

bool DeltaPerformer::PerformReplaceOperation(
    const InstallOperation& operation) {
  CHECK(operation.type() == InstallOperation::REPLACE ||
        operation.type() == InstallOperation::REPLACE_BZ ||
        operation.type() == InstallOperation::REPLACE_XZ);

  // Since we delete data off the beginning of the buffer as we use it,
  // the data we need should be exactly at the beginning of the buffer.
  TEST_AND_RETURN_FALSE(buffer_.size() >= operation.data_length());

  TEST_AND_RETURN_FALSE(partition_writer_->PerformReplaceOperation(
      operation, buffer_.data(), buffer_.size()));
  // Update buffer
  DiscardBuffer(true, buffer_.size());
  return true;
}

bool DeltaPerformer::PerformZeroOrDiscardOperation(
    const InstallOperation& operation) {
  CHECK(operation.type() == InstallOperation::DISCARD ||
        operation.type() == InstallOperation::ZERO);

  // These operations have no blob.
  TEST_AND_RETURN_FALSE(!operation.has_data_offset());
  TEST_AND_RETURN_FALSE(!operation.has_data_length());

  return partition_writer_->PerformZeroOrDiscardOperation(operation);
}

bool DeltaPerformer::PerformSourceCopyOperation(
    const InstallOperation& operation, ErrorCode* error) {
  if (operation.has_src_length())
    TEST_AND_RETURN_FALSE(operation.src_length() % block_size_ == 0);
  if (operation.has_dst_length())
    TEST_AND_RETURN_FALSE(operation.dst_length() % block_size_ == 0);
  return partition_writer_->PerformSourceCopyOperation(operation, error);
}

bool DeltaPerformer::ExtentsToBsdiffPositionsString(
    const RepeatedPtrField<Extent>& extents,
    uint64_t block_size,
    uint64_t full_length,
    string* positions_string) {
  string ret;
  uint64_t length = 0;
  for (const Extent& extent : extents) {
    int64_t start = extent.start_block() * block_size;
    uint64_t this_length =
        min(full_length - length,
            static_cast<uint64_t>(extent.num_blocks()) * block_size);
    ret += base::StringPrintf("%" PRIi64 ":%" PRIu64 ",", start, this_length);
    length += this_length;
  }
  TEST_AND_RETURN_FALSE(length == full_length);
  if (!ret.empty())
    ret.resize(ret.size() - 1);  // Strip trailing comma off
  *positions_string = ret;
  return true;
}

bool DeltaPerformer::PerformDiffOperation(const InstallOperation& operation,
                                          ErrorCode* error) {
  // Since we delete data off the beginning of the buffer as we use it,
  // the data we need should be exactly at the beginning of the buffer.
  TEST_AND_RETURN_FALSE(buffer_offset_ == operation.data_offset());
  TEST_AND_RETURN_FALSE(buffer_.size() >= operation.data_length());
  if (operation.has_src_length())
    TEST_AND_RETURN_FALSE(operation.src_length() % block_size_ == 0);
  if (operation.has_dst_length())
    TEST_AND_RETURN_FALSE(operation.dst_length() % block_size_ == 0);

  TEST_AND_RETURN_FALSE(partition_writer_->PerformDiffOperation(
      operation, error, buffer_.data(), buffer_.size()));
  DiscardBuffer(true, buffer_.size());
  return true;
}

bool DeltaPerformer::ExtractSignatureMessage() {
  TEST_AND_RETURN_FALSE(signatures_message_data_.empty());
  TEST_AND_RETURN_FALSE(buffer_offset_ == manifest_.signatures_offset());
  TEST_AND_RETURN_FALSE(buffer_.size() >= manifest_.signatures_size());
  signatures_message_data_.assign(
      buffer_.begin(), buffer_.begin() + manifest_.signatures_size());

  LOG(INFO) << "Extracted signature data of size "
            << manifest_.signatures_size() << " at "
            << manifest_.signatures_offset();
  return true;
}

bool DeltaPerformer::GetPublicKey(string* out_public_key) {
  out_public_key->clear();

  if (utils::FileExists(public_key_path_.c_str())) {
    LOG(INFO) << "Verifying using public key: " << public_key_path_;
    return utils::ReadFile(public_key_path_, out_public_key);
  }

  // If this is an official build then we are not allowed to use public key
  // from Omaha response.
  if (!hardware_->IsOfficialBuild() && !install_plan_->public_key_rsa.empty()) {
    LOG(INFO) << "Verifying using public key from Omaha response.";
    return brillo::data_encoding::Base64Decode(install_plan_->public_key_rsa,
                                               out_public_key);
  }
  LOG(INFO) << "No public keys found for verification.";
  return true;
}

std::pair<std::unique_ptr<PayloadVerifier>, bool>
DeltaPerformer::CreatePayloadVerifier() {
  if (utils::FileExists(update_certificates_path_.c_str())) {
    LOG(INFO) << "Verifying using certificates: " << update_certificates_path_;
    return {
        PayloadVerifier::CreateInstanceFromZipPath(update_certificates_path_),
        true};
  }

  string public_key;
  if (!GetPublicKey(&public_key)) {
    LOG(ERROR) << "Failed to read public key";
    return {nullptr, true};
  }

  // Skips the verification if the public key is empty.
  if (public_key.empty()) {
    return {nullptr, false};
  }
  LOG(INFO) << "Verifing using public key: " << public_key;
  return {PayloadVerifier::CreateInstance(public_key), true};
}

ErrorCode DeltaPerformer::ValidateManifest() {
  // Perform assorted checks to validation check the manifest, make sure it
  // matches data from other sources, and that it is a supported version.
  bool has_old_fields = std::any_of(manifest_.partitions().begin(),
                                    manifest_.partitions().end(),
                                    [](const PartitionUpdate& partition) {
                                      return partition.has_old_partition_info();
                                    });

  // The presence of an old partition hash is the sole indicator for a delta
  // update. Also, always treat the partial update as delta so that we can
  // perform the minor version check correctly.
  InstallPayloadType actual_payload_type =
      (has_old_fields || manifest_.partial_update())
          ? InstallPayloadType::kDelta
          : InstallPayloadType::kFull;

  if (payload_->type == InstallPayloadType::kUnknown) {
    LOG(INFO) << "Detected a '"
              << InstallPayloadTypeToString(actual_payload_type)
              << "' payload.";
    payload_->type = actual_payload_type;
  } else if (payload_->type != actual_payload_type) {
    LOG(ERROR) << "InstallPlan expected a '"
               << InstallPayloadTypeToString(payload_->type)
               << "' payload but the downloaded manifest contains a '"
               << InstallPayloadTypeToString(actual_payload_type)
               << "' payload.";
    return ErrorCode::kPayloadMismatchedType;
  }
  // Check that the minor version is compatible.
  // TODO(xunchang) increment minor version & add check for partial update
  if (actual_payload_type == InstallPayloadType::kFull) {
    if (manifest_.minor_version() != kFullPayloadMinorVersion) {
      LOG(ERROR) << "Manifest contains minor version "
                 << manifest_.minor_version()
                 << ", but all full payloads should have version "
                 << kFullPayloadMinorVersion << ".";
      return ErrorCode::kUnsupportedMinorPayloadVersion;
    }
  } else {
    if (manifest_.minor_version() < kMinSupportedMinorPayloadVersion ||
        manifest_.minor_version() > kMaxSupportedMinorPayloadVersion) {
      LOG(ERROR) << "Manifest contains minor version "
                 << manifest_.minor_version()
                 << " not in the range of supported minor versions ["
                 << kMinSupportedMinorPayloadVersion << ", "
                 << kMaxSupportedMinorPayloadVersion << "].";
      return ErrorCode::kUnsupportedMinorPayloadVersion;
    }
  }

  ErrorCode error_code = CheckTimestampError();
  if (error_code != ErrorCode::kSuccess) {
    if (error_code == ErrorCode::kPayloadTimestampError) {
      if (!hardware_->AllowDowngrade()) {
        return ErrorCode::kPayloadTimestampError;
      }
      LOG(INFO) << "The current OS build allows downgrade, continuing to apply"
                   " the payload with an older timestamp.";
    } else {
      LOG(ERROR) << "Timestamp check returned "
                 << utils::ErrorCodeToString(error_code);
      return error_code;
    }
  }

  // TODO(crbug.com/37661) we should be adding more and more manifest checks,
  // such as partition boundaries, etc.

  return ErrorCode::kSuccess;
}

ErrorCode DeltaPerformer::CheckTimestampError() const {
  bool is_partial_update =
      manifest_.has_partial_update() && manifest_.partial_update();
  const auto& partitions = manifest_.partitions();

  // Check version field for a given PartitionUpdate object. If an error
  // is encountered, set |error_code| accordingly. If downgrade is detected,
  // |downgrade_detected| is set. Return true if the program should continue
  // to check the next partition or not, or false if it should exit early due
  // to errors.
  auto&& timestamp_valid = [this](const PartitionUpdate& partition,
                                  bool allow_empty_version,
                                  bool* downgrade_detected) -> ErrorCode {
    const auto& partition_name = partition.partition_name();
    if (!partition.has_version()) {
      if (hardware_->GetVersionForLogging(partition_name).empty()) {
        LOG(INFO) << partition_name << " does't have version, skipping "
                  << "downgrade check.";
        return ErrorCode::kSuccess;
      }

      if (allow_empty_version) {
        return ErrorCode::kSuccess;
      }
      LOG(ERROR)
          << "PartitionUpdate " << partition_name
          << " doesn't have a version field. Not allowed in partial updates.";
      return ErrorCode::kDownloadManifestParseError;
    }

    auto error_code =
        hardware_->IsPartitionUpdateValid(partition_name, partition.version());
    switch (error_code) {
      case ErrorCode::kSuccess:
        break;
      case ErrorCode::kPayloadTimestampError:
        *downgrade_detected = true;
        LOG(WARNING) << "PartitionUpdate " << partition_name
                     << " has an older version than partition on device.";
        break;
      default:
        LOG(ERROR) << "IsPartitionUpdateValid(" << partition_name
                   << ") returned" << utils::ErrorCodeToString(error_code);
        break;
    }
    return error_code;
  };

  bool downgrade_detected = false;

  if (is_partial_update) {
    // for partial updates, all partition MUST have valid timestamps
    // But max_timestamp can be empty
    for (const auto& partition : partitions) {
      auto error_code = timestamp_valid(
          partition, false /* allow_empty_version */, &downgrade_detected);
      if (error_code != ErrorCode::kSuccess &&
          error_code != ErrorCode::kPayloadTimestampError) {
        return error_code;
      }
    }
    if (downgrade_detected) {
      return ErrorCode::kPayloadTimestampError;
    }
    return ErrorCode::kSuccess;
  }

  // For non-partial updates, check max_timestamp first.
  if (manifest_.max_timestamp() < hardware_->GetBuildTimestamp()) {
    LOG(ERROR) << "The current OS build timestamp ("
               << hardware_->GetBuildTimestamp()
               << ") is newer than the maximum timestamp in the manifest ("
               << manifest_.max_timestamp() << ")";
    return ErrorCode::kPayloadTimestampError;
  }
  // Otherwise... partitions can have empty timestamps.
  for (const auto& partition : partitions) {
    auto error_code = timestamp_valid(
        partition, true /* allow_empty_version */, &downgrade_detected);
    if (error_code != ErrorCode::kSuccess &&
        error_code != ErrorCode::kPayloadTimestampError) {
      return error_code;
    }
  }
  if (downgrade_detected) {
    return ErrorCode::kPayloadTimestampError;
  }
  return ErrorCode::kSuccess;
}

ErrorCode DeltaPerformer::ValidateOperationHash(
    const InstallOperation& operation) {
  if (!operation.data_sha256_hash().size()) {
    if (!operation.data_length()) {
      // Operations that do not have any data blob won't have any operation
      // hash either. So, these operations are always considered validated
      // since the metadata that contains all the non-data-blob portions of
      // the operation has already been validated. This is true for both HTTP
      // and HTTPS cases.
      return ErrorCode::kSuccess;
    }

    // No hash is present for an operation that has data blobs. This shouldn't
    // happen normally for any client that has this code, because the
    // corresponding update should have been produced with the operation
    // hashes. So if it happens it means either we've turned operation hash
    // generation off in DeltaDiffGenerator or it's a regression of some sort.
    // One caveat though: The last operation is a unused signature operation
    // that doesn't have a hash at the time the manifest is created. So we
    // should not complaint about that operation. This operation can be
    // recognized by the fact that it's offset is mentioned in the manifest.
    if (manifest_.signatures_offset() &&
        manifest_.signatures_offset() == operation.data_offset()) {
      LOG(INFO) << "Skipping hash verification for signature operation "
                << next_operation_num_ + 1;
    } else {
      if (install_plan_->hash_checks_mandatory) {
        LOG(ERROR) << "Missing mandatory operation hash for operation "
                   << next_operation_num_ + 1;
        return ErrorCode::kDownloadOperationHashMissingError;
      }

      LOG(WARNING) << "Cannot validate operation " << next_operation_num_ + 1
                   << " as there's no operation hash in manifest";
    }
    return ErrorCode::kSuccess;
  }

  brillo::Blob expected_op_hash;
  expected_op_hash.assign(operation.data_sha256_hash().data(),
                          (operation.data_sha256_hash().data() +
                           operation.data_sha256_hash().size()));

  brillo::Blob calculated_op_hash;
  if (!HashCalculator::RawHashOfBytes(
          buffer_.data(), operation.data_length(), &calculated_op_hash)) {
    LOG(ERROR) << "Unable to compute actual hash of operation "
               << next_operation_num_;
    return ErrorCode::kDownloadOperationHashVerificationError;
  }

  if (calculated_op_hash != expected_op_hash) {
    LOG(ERROR) << "Hash verification failed for operation "
               << next_operation_num_
               << ". Expected hash = " << HexEncode(expected_op_hash);
    LOG(ERROR) << "Calculated hash over " << operation.data_length()
               << " bytes at offset: " << operation.data_offset() << " = "
               << HexEncode(calculated_op_hash);
    return ErrorCode::kDownloadOperationHashMismatch;
  }

  return ErrorCode::kSuccess;
}

#define TEST_AND_RETURN_VAL(_retval, _condition)              \
  do {                                                        \
    if (!(_condition)) {                                      \
      LOG(ERROR) << "VerifyPayload failure: " << #_condition; \
      return _retval;                                         \
    }                                                         \
  } while (0);

ErrorCode DeltaPerformer::VerifyPayload(
    const brillo::Blob& update_check_response_hash,
    const uint64_t update_check_response_size) {
  // Verifies the download size.
  if (update_check_response_size !=
      metadata_size_ + metadata_signature_size_ + buffer_offset_) {
    LOG(ERROR) << "update_check_response_size (" << update_check_response_size
               << ") doesn't match metadata_size (" << metadata_size_
               << ") + metadata_signature_size (" << metadata_signature_size_
               << ") + buffer_offset (" << buffer_offset_ << ").";
    return ErrorCode::kPayloadSizeMismatchError;
  }

  // Verifies the payload hash.
  TEST_AND_RETURN_VAL(ErrorCode::kDownloadPayloadVerificationError,
                      !payload_hash_calculator_.raw_hash().empty());
  if (payload_hash_calculator_.raw_hash() != update_check_response_hash) {
    LOG(ERROR) << "Actual hash: "
               << HexEncode(payload_hash_calculator_.raw_hash())
               << ", expected hash: " << HexEncode(update_check_response_hash);
    return ErrorCode::kPayloadHashMismatchError;
  }

  // NOLINTNEXTLINE(whitespace/braces)
  auto [payload_verifier, perform_verification] = CreatePayloadVerifier();
  if (!perform_verification) {
    LOG(WARNING) << "Not verifying signed delta payload -- missing public key.";
    return ErrorCode::kSuccess;
  }
  if (!payload_verifier) {
    LOG(ERROR) << "Failed to create the payload verifier.";
    return ErrorCode::kDownloadPayloadPubKeyVerificationError;
  }

  TEST_AND_RETURN_VAL(ErrorCode::kSignedDeltaPayloadExpectedError,
                      !signatures_message_data_.empty());
  brillo::Blob hash_data = signed_hash_calculator_.raw_hash();
  TEST_AND_RETURN_VAL(ErrorCode::kDownloadPayloadPubKeyVerificationError,
                      hash_data.size() == kSHA256Size);

  if (!payload_verifier->VerifySignature(signatures_message_data_, hash_data)) {
    // The autoupdate_CatchBadSignatures test checks for this string
    // in log-files. Keep in sync.
    LOG(ERROR) << "Public key verification failed, thus update failed.";
    return ErrorCode::kDownloadPayloadPubKeyVerificationError;
  }

  LOG(INFO) << "Payload hash matches value in payload.";
  return ErrorCode::kSuccess;
}

void DeltaPerformer::DiscardBuffer(bool do_advance_offset,
                                   size_t signed_hash_buffer_size) {
  // Update the buffer offset.
  if (do_advance_offset)
    buffer_offset_ += buffer_.size();

  // Hash the content.
  payload_hash_calculator_.Update(buffer_.data(), buffer_.size());
  signed_hash_calculator_.Update(buffer_.data(), signed_hash_buffer_size);

  // Swap content with an empty vector to ensure that all memory is released.
  brillo::Blob().swap(buffer_);
}

bool DeltaPerformer::CanResumeUpdate(PrefsInterface* prefs,
                                     const string& update_check_response_hash) {
  int64_t next_operation = kUpdateStateOperationInvalid;
  if (!(prefs->GetInt64(kPrefsUpdateStateNextOperation, &next_operation) &&
        next_operation != kUpdateStateOperationInvalid && next_operation > 0)) {
    LOG(WARNING) << "Failed to resume update " << kPrefsUpdateStateNextOperation
                 << " invalid: " << next_operation;
    return false;
  }

  string interrupted_hash;
  if (!(prefs->GetString(kPrefsUpdateCheckResponseHash, &interrupted_hash) &&
        !interrupted_hash.empty() &&
        interrupted_hash == update_check_response_hash)) {
    LOG(WARNING) << "Failed to resume update " << kPrefsUpdateCheckResponseHash
                 << " mismatch, last hash: " << interrupted_hash
                 << ", current hash: " << update_check_response_hash << "";
    return false;
  }

  int64_t resumed_update_failures{};
  // Note that storing this value is optional, but if it is there it should
  // not be more than the limit.
  if (prefs->GetInt64(kPrefsResumedUpdateFailures, &resumed_update_failures) &&
      resumed_update_failures > kMaxResumedUpdateFailures) {
    LOG(WARNING) << "Failed to resume update " << kPrefsResumedUpdateFailures
                 << " has value " << resumed_update_failures
                 << " is over the limit " << kMaxResumedUpdateFailures;
    return false;
  }

  // Validation check the rest.
  int64_t next_data_offset = -1;
  if (!(prefs->GetInt64(kPrefsUpdateStateNextDataOffset, &next_data_offset) &&
        next_data_offset >= 0)) {
    LOG(WARNING) << "Failed to resume update "
                 << kPrefsUpdateStateNextDataOffset
                 << " invalid: " << next_data_offset;
    return false;
  }

  string sha256_context;
  if (!(prefs->GetString(kPrefsUpdateStateSHA256Context, &sha256_context) &&
        !sha256_context.empty())) {
    LOG(WARNING) << "Failed to resume update " << kPrefsUpdateStateSHA256Context
                 << " is empty.";
    return false;
  }

  int64_t manifest_metadata_size = 0;
  if (!(prefs->GetInt64(kPrefsManifestMetadataSize, &manifest_metadata_size) &&
        manifest_metadata_size > 0)) {
    LOG(WARNING) << "Failed to resume update " << kPrefsManifestMetadataSize
                 << " invalid: " << manifest_metadata_size;
    return false;
  }

  int64_t manifest_signature_size = 0;
  if (!(prefs->GetInt64(kPrefsManifestSignatureSize,
                        &manifest_signature_size) &&
        manifest_signature_size >= 0)) {
    LOG(WARNING) << "Failed to resume update " << kPrefsManifestSignatureSize
                 << " invalid: " << manifest_signature_size;
    return false;
  }

  return true;
}

bool DeltaPerformer::ResetUpdateProgress(
    PrefsInterface* prefs,
    bool quick,
    bool skip_dynamic_partititon_metadata_updated) {
  TEST_AND_RETURN_FALSE(prefs->SetInt64(kPrefsUpdateStateNextOperation,
                                        kUpdateStateOperationInvalid));
  if (!quick) {
    prefs->SetInt64(kPrefsUpdateStateNextDataOffset, -1);
    prefs->SetInt64(kPrefsUpdateStateNextDataLength, 0);
    prefs->SetString(kPrefsUpdateStateSHA256Context, "");
    prefs->SetString(kPrefsUpdateStateSignedSHA256Context, "");
    prefs->SetString(kPrefsUpdateStateSignatureBlob, "");
    prefs->SetInt64(kPrefsManifestMetadataSize, -1);
    prefs->SetInt64(kPrefsManifestSignatureSize, -1);
    prefs->SetInt64(kPrefsResumedUpdateFailures, 0);
    prefs->Delete(kPrefsPostInstallSucceeded);
    prefs->Delete(kPrefsVerityWritten);
    if (!skip_dynamic_partititon_metadata_updated) {
      LOG(INFO) << "Resetting recorded hash for prepared partitions.";
      prefs->Delete(kPrefsDynamicPartitionMetadataUpdated);
    }
  }
  return true;
}

bool DeltaPerformer::ShouldCheckpoint() {
  base::TimeTicks curr_time = base::TimeTicks::Now();
  if (curr_time > update_checkpoint_time_) {
    update_checkpoint_time_ = curr_time + update_checkpoint_wait_;
    return true;
  }
  return false;
}

bool DeltaPerformer::CheckpointUpdateProgress(bool force) {
  if (!force && !ShouldCheckpoint()) {
    return false;
  }
  Terminator::set_exit_blocked(true);
  LOG_IF(WARNING, !prefs_->StartTransaction())
      << "unable to start transaction in checkpointing";
  DEFER {
    prefs_->CancelTransaction();
  };
  if (last_updated_operation_num_ != next_operation_num_ || force) {
    if (!signatures_message_data_.empty()) {
      // Save the signature blob because if the update is interrupted after the
      // download phase we don't go through this path anymore. Some alternatives
      // to consider:
      //
      // 1. On resume, re-download the signature blob from the server and
      // re-verify it.
      //
      // 2. Verify the signature as soon as it's received and don't checkpoint
      // the blob and the signed sha-256 context.
      LOG_IF(WARNING,
             !prefs_->SetString(kPrefsUpdateStateSignatureBlob,
                                signatures_message_data_))
          << "Unable to store the signature blob.";
    }
    TEST_AND_RETURN_FALSE(prefs_->SetString(
        kPrefsUpdateStateSHA256Context, payload_hash_calculator_.GetContext()));
    TEST_AND_RETURN_FALSE(
        prefs_->SetString(kPrefsUpdateStateSignedSHA256Context,
                          signed_hash_calculator_.GetContext()));
    TEST_AND_RETURN_FALSE(
        prefs_->SetInt64(kPrefsUpdateStateNextDataOffset, buffer_offset_));
    last_updated_operation_num_ = next_operation_num_;

    if (next_operation_num_ < num_total_operations_) {
      size_t partition_index = current_partition_;
      while (next_operation_num_ >= acc_num_operations_[partition_index]) {
        partition_index++;
      }
      const size_t partition_operation_num =
          next_operation_num_ -
          (partition_index ? acc_num_operations_[partition_index - 1] : 0);
      const InstallOperation& op =
          partitions_[partition_index].operations(partition_operation_num);
      TEST_AND_RETURN_FALSE(
          prefs_->SetInt64(kPrefsUpdateStateNextDataLength, op.data_length()));
    } else {
      TEST_AND_RETURN_FALSE(
          prefs_->SetInt64(kPrefsUpdateStateNextDataLength, 0));
    }
    if (partition_writer_) {
      partition_writer_->CheckpointUpdateProgress(GetPartitionOperationNum());
    } else {
      CHECK_EQ(next_operation_num_, num_total_operations_)
          << "Partition writer is null, we are expected to finish all "
             "operations: "
          << next_operation_num_ << "/" << num_total_operations_;
    }
  }
  TEST_AND_RETURN_FALSE(
      prefs_->SetInt64(kPrefsUpdateStateNextOperation, next_operation_num_));
  if (!prefs_->SubmitTransaction()) {
    LOG(ERROR) << "Failed to submit transaction in checkpointing";
  }
  return true;
}

bool DeltaPerformer::PrimeUpdateState() {
  CHECK(manifest_valid_);

  int64_t next_operation = kUpdateStateOperationInvalid;
  if (!prefs_->GetInt64(kPrefsUpdateStateNextOperation, &next_operation) ||
      next_operation == kUpdateStateOperationInvalid || next_operation <= 0) {
    // Initiating a new update, no more state needs to be initialized.
    return true;
  }
  next_operation_num_ = next_operation;

  // Resuming an update -- load the rest of the update state.
  int64_t next_data_offset = -1;
  TEST_AND_RETURN_FALSE(
      prefs_->GetInt64(kPrefsUpdateStateNextDataOffset, &next_data_offset) &&
      next_data_offset >= 0);
  buffer_offset_ = next_data_offset;

  // The signed hash context and the signature blob may be empty if the
  // interrupted update didn't reach the signature.
  string signed_hash_context;
  if (prefs_->GetString(kPrefsUpdateStateSignedSHA256Context,
                        &signed_hash_context)) {
    TEST_AND_RETURN_FALSE(
        signed_hash_calculator_.SetContext(signed_hash_context));
  }

  prefs_->GetString(kPrefsUpdateStateSignatureBlob, &signatures_message_data_);

  string hash_context;
  TEST_AND_RETURN_FALSE(
      prefs_->GetString(kPrefsUpdateStateSHA256Context, &hash_context) &&
      payload_hash_calculator_.SetContext(hash_context));

  int64_t manifest_metadata_size = 0;
  TEST_AND_RETURN_FALSE(
      prefs_->GetInt64(kPrefsManifestMetadataSize, &manifest_metadata_size) &&
      manifest_metadata_size > 0);
  metadata_size_ = manifest_metadata_size;

  int64_t manifest_signature_size = 0;
  TEST_AND_RETURN_FALSE(
      prefs_->GetInt64(kPrefsManifestSignatureSize, &manifest_signature_size) &&
      manifest_signature_size >= 0);
  metadata_signature_size_ = manifest_signature_size;

  // Advance the download progress to reflect what doesn't need to be
  // re-downloaded.
  total_bytes_received_ += buffer_offset_;

  // Speculatively count the resume as a failure.
  int64_t resumed_update_failures{};
  if (prefs_->GetInt64(kPrefsResumedUpdateFailures, &resumed_update_failures)) {
    resumed_update_failures++;
  } else {
    resumed_update_failures = 1;
  }
  prefs_->SetInt64(kPrefsResumedUpdateFailures, resumed_update_failures);
  return true;
}

bool DeltaPerformer::IsDynamicPartition(const std::string& part_name,
                                        uint32_t slot) {
  return boot_control_->GetDynamicPartitionControl()->IsDynamicPartition(
      part_name, slot);
}

std::unique_ptr<PartitionWriterInterface> DeltaPerformer::CreatePartitionWriter(
    const PartitionUpdate& partition_update,
    const InstallPlan::Partition& install_part,
    DynamicPartitionControlInterface* dynamic_control,
    size_t block_size,
    bool is_interactive,
    bool is_dynamic_partition) {
  return partition_writer::CreatePartitionWriter(
      partition_update,
      install_part,
      dynamic_control,
      block_size_,
      interactive_,
      IsDynamicPartition(install_part.name, install_plan_->target_slot));
}

}  // namespace chromeos_update_engine