2010-02-18 01:11:44 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2009 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.
|
|
|
|
*/
|
|
|
|
|
2016-10-25 23:17:26 +02:00
|
|
|
#include "applypatch_modes.h"
|
|
|
|
|
2018-07-13 22:11:09 +02:00
|
|
|
#include <getopt.h>
|
2010-02-18 01:11:44 +01:00
|
|
|
#include <stdio.h>
|
2010-02-22 23:46:32 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2016-02-04 02:08:52 +01:00
|
|
|
#include <memory>
|
2016-10-12 19:55:04 +02:00
|
|
|
#include <string>
|
2016-02-04 02:08:52 +01:00
|
|
|
#include <vector>
|
|
|
|
|
2018-07-13 22:11:09 +02:00
|
|
|
#include <android-base/file.h>
|
2018-04-26 08:00:27 +02:00
|
|
|
#include <android-base/logging.h>
|
2016-10-25 23:17:26 +02:00
|
|
|
#include <android-base/parseint.h>
|
|
|
|
#include <android-base/strings.h>
|
2016-10-28 03:16:06 +02:00
|
|
|
#include <openssl/sha.h>
|
|
|
|
|
2016-03-03 20:43:47 +01:00
|
|
|
#include "applypatch/applypatch.h"
|
2010-02-22 23:46:32 +01:00
|
|
|
#include "edify/expr.h"
|
|
|
|
|
applypatch: Refactor applypatch().
applypatch() was initially designed for file-based OTA, operating on
individual files. It was later extended to allow patching eMMC targets
as a whole, in favor of block-based updates.
As we have deprecated file-based OTA since Oreo, part of the code in
applypatch() has become obsolete. This CL refactors the related
functions, by removing the obsolete logic and focusing on eMMC targets.
Since this CL substantially changes applypatch APIs, it adds new
functions to avoid unintentionally mixing them together. In particular,
it removes `applypatch()`, `applypatch_check()`, `applypatch_flash()`,
and adds `PatchPartition()`, `PatchPartitionCheck()`, `FlashPartition()`
and `CheckPartition()`. It also replaces the old Edify functions
`apply_patch()` and `apply_patch_check()` with `patch_partition()` and
`patch_partition_check()` respectively.
This CL requires matching changes to OTA generation script (in the same
topic).
Bug: 110106408
Test: Run recovery_unit_test and recovery_component_test on marlin.
Test: `m dist` with non-A/B target. Verify
/system/bin/install-recovery.sh on device.
Test: `m dist` with non-A/B target using BOARD_USES_FULL_RECOVERY_IMAGE.
Verify /system/bin/install-recovery.sh on device.
Test: Install an incremental OTA with the new updater and scripts.
Change-Id: Ia34a90114bb227f4216eb478c22dc98c8194cb7f
2018-06-20 09:30:48 +02:00
|
|
|
static int CheckMode(const std::string& target_emmc) {
|
|
|
|
std::string err;
|
|
|
|
auto target = Partition::Parse(target_emmc, &err);
|
|
|
|
if (!target) {
|
|
|
|
LOG(ERROR) << "Failed to parse target \"" << target_emmc << "\": " << err;
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
return CheckPartition(target) ? 0 : 1;
|
2015-07-18 03:11:12 +02:00
|
|
|
}
|
|
|
|
|
2018-07-13 22:11:09 +02:00
|
|
|
static int FlashMode(const std::string& target_emmc, const std::string& source_file) {
|
applypatch: Refactor applypatch().
applypatch() was initially designed for file-based OTA, operating on
individual files. It was later extended to allow patching eMMC targets
as a whole, in favor of block-based updates.
As we have deprecated file-based OTA since Oreo, part of the code in
applypatch() has become obsolete. This CL refactors the related
functions, by removing the obsolete logic and focusing on eMMC targets.
Since this CL substantially changes applypatch APIs, it adds new
functions to avoid unintentionally mixing them together. In particular,
it removes `applypatch()`, `applypatch_check()`, `applypatch_flash()`,
and adds `PatchPartition()`, `PatchPartitionCheck()`, `FlashPartition()`
and `CheckPartition()`. It also replaces the old Edify functions
`apply_patch()` and `apply_patch_check()` with `patch_partition()` and
`patch_partition_check()` respectively.
This CL requires matching changes to OTA generation script (in the same
topic).
Bug: 110106408
Test: Run recovery_unit_test and recovery_component_test on marlin.
Test: `m dist` with non-A/B target. Verify
/system/bin/install-recovery.sh on device.
Test: `m dist` with non-A/B target using BOARD_USES_FULL_RECOVERY_IMAGE.
Verify /system/bin/install-recovery.sh on device.
Test: Install an incremental OTA with the new updater and scripts.
Change-Id: Ia34a90114bb227f4216eb478c22dc98c8194cb7f
2018-06-20 09:30:48 +02:00
|
|
|
std::string err;
|
|
|
|
auto target = Partition::Parse(target_emmc, &err);
|
|
|
|
if (!target) {
|
|
|
|
LOG(ERROR) << "Failed to parse target \"" << target_emmc << "\": " << err;
|
2018-07-13 22:11:09 +02:00
|
|
|
return 2;
|
|
|
|
}
|
applypatch: Refactor applypatch().
applypatch() was initially designed for file-based OTA, operating on
individual files. It was later extended to allow patching eMMC targets
as a whole, in favor of block-based updates.
As we have deprecated file-based OTA since Oreo, part of the code in
applypatch() has become obsolete. This CL refactors the related
functions, by removing the obsolete logic and focusing on eMMC targets.
Since this CL substantially changes applypatch APIs, it adds new
functions to avoid unintentionally mixing them together. In particular,
it removes `applypatch()`, `applypatch_check()`, `applypatch_flash()`,
and adds `PatchPartition()`, `PatchPartitionCheck()`, `FlashPartition()`
and `CheckPartition()`. It also replaces the old Edify functions
`apply_patch()` and `apply_patch_check()` with `patch_partition()` and
`patch_partition_check()` respectively.
This CL requires matching changes to OTA generation script (in the same
topic).
Bug: 110106408
Test: Run recovery_unit_test and recovery_component_test on marlin.
Test: `m dist` with non-A/B target. Verify
/system/bin/install-recovery.sh on device.
Test: `m dist` with non-A/B target using BOARD_USES_FULL_RECOVERY_IMAGE.
Verify /system/bin/install-recovery.sh on device.
Test: Install an incremental OTA with the new updater and scripts.
Change-Id: Ia34a90114bb227f4216eb478c22dc98c8194cb7f
2018-06-20 09:30:48 +02:00
|
|
|
return FlashPartition(target, source_file) ? 0 : 1;
|
2018-07-13 22:11:09 +02:00
|
|
|
}
|
2018-06-20 00:56:49 +02:00
|
|
|
|
2018-07-13 22:11:09 +02:00
|
|
|
static int PatchMode(const std::string& target_emmc, const std::string& source_emmc,
|
|
|
|
const std::string& patch_file, const std::string& bonus_file) {
|
applypatch: Refactor applypatch().
applypatch() was initially designed for file-based OTA, operating on
individual files. It was later extended to allow patching eMMC targets
as a whole, in favor of block-based updates.
As we have deprecated file-based OTA since Oreo, part of the code in
applypatch() has become obsolete. This CL refactors the related
functions, by removing the obsolete logic and focusing on eMMC targets.
Since this CL substantially changes applypatch APIs, it adds new
functions to avoid unintentionally mixing them together. In particular,
it removes `applypatch()`, `applypatch_check()`, `applypatch_flash()`,
and adds `PatchPartition()`, `PatchPartitionCheck()`, `FlashPartition()`
and `CheckPartition()`. It also replaces the old Edify functions
`apply_patch()` and `apply_patch_check()` with `patch_partition()` and
`patch_partition_check()` respectively.
This CL requires matching changes to OTA generation script (in the same
topic).
Bug: 110106408
Test: Run recovery_unit_test and recovery_component_test on marlin.
Test: `m dist` with non-A/B target. Verify
/system/bin/install-recovery.sh on device.
Test: `m dist` with non-A/B target using BOARD_USES_FULL_RECOVERY_IMAGE.
Verify /system/bin/install-recovery.sh on device.
Test: Install an incremental OTA with the new updater and scripts.
Change-Id: Ia34a90114bb227f4216eb478c22dc98c8194cb7f
2018-06-20 09:30:48 +02:00
|
|
|
std::string err;
|
|
|
|
auto target = Partition::Parse(target_emmc, &err);
|
|
|
|
if (!target) {
|
|
|
|
LOG(ERROR) << "Failed to parse target \"" << target_emmc << "\": " << err;
|
2018-06-20 00:56:49 +02:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
applypatch: Refactor applypatch().
applypatch() was initially designed for file-based OTA, operating on
individual files. It was later extended to allow patching eMMC targets
as a whole, in favor of block-based updates.
As we have deprecated file-based OTA since Oreo, part of the code in
applypatch() has become obsolete. This CL refactors the related
functions, by removing the obsolete logic and focusing on eMMC targets.
Since this CL substantially changes applypatch APIs, it adds new
functions to avoid unintentionally mixing them together. In particular,
it removes `applypatch()`, `applypatch_check()`, `applypatch_flash()`,
and adds `PatchPartition()`, `PatchPartitionCheck()`, `FlashPartition()`
and `CheckPartition()`. It also replaces the old Edify functions
`apply_patch()` and `apply_patch_check()` with `patch_partition()` and
`patch_partition_check()` respectively.
This CL requires matching changes to OTA generation script (in the same
topic).
Bug: 110106408
Test: Run recovery_unit_test and recovery_component_test on marlin.
Test: `m dist` with non-A/B target. Verify
/system/bin/install-recovery.sh on device.
Test: `m dist` with non-A/B target using BOARD_USES_FULL_RECOVERY_IMAGE.
Verify /system/bin/install-recovery.sh on device.
Test: Install an incremental OTA with the new updater and scripts.
Change-Id: Ia34a90114bb227f4216eb478c22dc98c8194cb7f
2018-06-20 09:30:48 +02:00
|
|
|
auto source = Partition::Parse(source_emmc, &err);
|
|
|
|
if (!source) {
|
|
|
|
LOG(ERROR) << "Failed to parse source \"" << source_emmc << "\": " << err;
|
2018-07-13 22:11:09 +02:00
|
|
|
return 2;
|
2018-06-20 00:56:49 +02:00
|
|
|
}
|
|
|
|
|
applypatch: Refactor applypatch().
applypatch() was initially designed for file-based OTA, operating on
individual files. It was later extended to allow patching eMMC targets
as a whole, in favor of block-based updates.
As we have deprecated file-based OTA since Oreo, part of the code in
applypatch() has become obsolete. This CL refactors the related
functions, by removing the obsolete logic and focusing on eMMC targets.
Since this CL substantially changes applypatch APIs, it adds new
functions to avoid unintentionally mixing them together. In particular,
it removes `applypatch()`, `applypatch_check()`, `applypatch_flash()`,
and adds `PatchPartition()`, `PatchPartitionCheck()`, `FlashPartition()`
and `CheckPartition()`. It also replaces the old Edify functions
`apply_patch()` and `apply_patch_check()` with `patch_partition()` and
`patch_partition_check()` respectively.
This CL requires matching changes to OTA generation script (in the same
topic).
Bug: 110106408
Test: Run recovery_unit_test and recovery_component_test on marlin.
Test: `m dist` with non-A/B target. Verify
/system/bin/install-recovery.sh on device.
Test: `m dist` with non-A/B target using BOARD_USES_FULL_RECOVERY_IMAGE.
Verify /system/bin/install-recovery.sh on device.
Test: Install an incremental OTA with the new updater and scripts.
Change-Id: Ia34a90114bb227f4216eb478c22dc98c8194cb7f
2018-06-20 09:30:48 +02:00
|
|
|
std::string patch_contents;
|
|
|
|
if (!android::base::ReadFileToString(patch_file, &patch_contents)) {
|
2018-07-13 22:11:09 +02:00
|
|
|
PLOG(ERROR) << "Failed to read patch file \"" << patch_file << "\"";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
applypatch: Refactor applypatch().
applypatch() was initially designed for file-based OTA, operating on
individual files. It was later extended to allow patching eMMC targets
as a whole, in favor of block-based updates.
As we have deprecated file-based OTA since Oreo, part of the code in
applypatch() has become obsolete. This CL refactors the related
functions, by removing the obsolete logic and focusing on eMMC targets.
Since this CL substantially changes applypatch APIs, it adds new
functions to avoid unintentionally mixing them together. In particular,
it removes `applypatch()`, `applypatch_check()`, `applypatch_flash()`,
and adds `PatchPartition()`, `PatchPartitionCheck()`, `FlashPartition()`
and `CheckPartition()`. It also replaces the old Edify functions
`apply_patch()` and `apply_patch_check()` with `patch_partition()` and
`patch_partition_check()` respectively.
This CL requires matching changes to OTA generation script (in the same
topic).
Bug: 110106408
Test: Run recovery_unit_test and recovery_component_test on marlin.
Test: `m dist` with non-A/B target. Verify
/system/bin/install-recovery.sh on device.
Test: `m dist` with non-A/B target using BOARD_USES_FULL_RECOVERY_IMAGE.
Verify /system/bin/install-recovery.sh on device.
Test: Install an incremental OTA with the new updater and scripts.
Change-Id: Ia34a90114bb227f4216eb478c22dc98c8194cb7f
2018-06-20 09:30:48 +02:00
|
|
|
Value patch(Value::Type::BLOB, std::move(patch_contents));
|
2018-07-13 22:11:09 +02:00
|
|
|
std::unique_ptr<Value> bonus;
|
|
|
|
if (!bonus_file.empty()) {
|
|
|
|
std::string bonus_contents;
|
|
|
|
if (!android::base::ReadFileToString(bonus_file, &bonus_contents)) {
|
|
|
|
PLOG(ERROR) << "Failed to read bonus file \"" << bonus_file << "\"";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
bonus = std::make_unique<Value>(Value::Type::BLOB, std::move(bonus_contents));
|
2018-06-20 00:56:49 +02:00
|
|
|
}
|
2018-07-13 22:11:09 +02:00
|
|
|
|
2019-09-23 19:28:54 +02:00
|
|
|
return PatchPartition(target, source, patch, bonus.get(), false) ? 0 : 1;
|
2018-07-13 22:11:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void Usage() {
|
|
|
|
printf(
|
|
|
|
"Usage: \n"
|
|
|
|
"check mode\n"
|
|
|
|
" applypatch --check EMMC:<target-file>:<target-size>:<target-sha1>\n\n"
|
|
|
|
"flash mode\n"
|
|
|
|
" applypatch --flash <source-file>\n"
|
|
|
|
" --target EMMC:<target-file>:<target-size>:<target-sha1>\n\n"
|
|
|
|
"patch mode\n"
|
|
|
|
" applypatch [--bonus <bonus-file>]\n"
|
|
|
|
" --patch <patch-file>\n"
|
|
|
|
" --target EMMC:<target-file>:<target-size>:<target-sha1>\n"
|
|
|
|
" --source EMMC:<source-file>:<source-size>:<source-sha1>\n\n"
|
|
|
|
"show license\n"
|
|
|
|
" applypatch --license\n"
|
|
|
|
"\n\n");
|
2010-02-22 23:46:32 +01:00
|
|
|
}
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2018-07-13 22:11:09 +02:00
|
|
|
int applypatch_modes(int argc, char* argv[]) {
|
|
|
|
static constexpr struct option OPTIONS[]{
|
2018-04-26 08:00:27 +02:00
|
|
|
// clang-format off
|
2018-07-13 22:11:09 +02:00
|
|
|
{ "bonus", required_argument, nullptr, 0 },
|
|
|
|
{ "check", required_argument, nullptr, 0 },
|
|
|
|
{ "flash", required_argument, nullptr, 0 },
|
|
|
|
{ "license", no_argument, nullptr, 0 },
|
|
|
|
{ "patch", required_argument, nullptr, 0 },
|
|
|
|
{ "source", required_argument, nullptr, 0 },
|
|
|
|
{ "target", required_argument, nullptr, 0 },
|
|
|
|
{ nullptr, 0, nullptr, 0 },
|
2018-04-26 08:00:27 +02:00
|
|
|
// clang-format on
|
2018-07-13 22:11:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
std::string check_target;
|
|
|
|
std::string source;
|
|
|
|
std::string target;
|
|
|
|
std::string patch;
|
|
|
|
std::string bonus;
|
|
|
|
|
|
|
|
bool check_mode = false;
|
|
|
|
bool flash_mode = false;
|
|
|
|
bool patch_mode = false;
|
|
|
|
|
|
|
|
optind = 1;
|
|
|
|
|
|
|
|
int arg;
|
|
|
|
int option_index;
|
|
|
|
while ((arg = getopt_long(argc, argv, "", OPTIONS, &option_index)) != -1) {
|
|
|
|
switch (arg) {
|
|
|
|
case 0: {
|
|
|
|
std::string option = OPTIONS[option_index].name;
|
|
|
|
if (option == "bonus") {
|
|
|
|
bonus = optarg;
|
|
|
|
} else if (option == "check") {
|
|
|
|
check_target = optarg;
|
|
|
|
check_mode = true;
|
|
|
|
} else if (option == "flash") {
|
|
|
|
source = optarg;
|
|
|
|
flash_mode = true;
|
|
|
|
} else if (option == "license") {
|
|
|
|
return ShowLicenses();
|
|
|
|
} else if (option == "patch") {
|
|
|
|
patch = optarg;
|
|
|
|
patch_mode = true;
|
|
|
|
} else if (option == "source") {
|
|
|
|
source = optarg;
|
|
|
|
} else if (option == "target") {
|
|
|
|
target = optarg;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
LOG(ERROR) << "Invalid argument";
|
|
|
|
Usage();
|
|
|
|
return 2;
|
|
|
|
}
|
2018-04-26 08:00:27 +02:00
|
|
|
}
|
|
|
|
|
2018-07-13 22:11:09 +02:00
|
|
|
if (check_mode) {
|
|
|
|
return CheckMode(check_target);
|
2018-04-26 08:00:27 +02:00
|
|
|
}
|
2018-07-13 22:11:09 +02:00
|
|
|
if (flash_mode) {
|
|
|
|
if (!bonus.empty()) {
|
|
|
|
LOG(ERROR) << "bonus file not supported in flash mode";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return FlashMode(target, source);
|
2018-04-26 08:00:27 +02:00
|
|
|
}
|
2018-07-13 22:11:09 +02:00
|
|
|
if (patch_mode) {
|
|
|
|
return PatchMode(target, source, patch, bonus);
|
|
|
|
}
|
|
|
|
|
|
|
|
Usage();
|
|
|
|
return 2;
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|