blob: bb5eeae9d51520a41f0007ce6753cd43d28274d0 [file] [log] [blame]
Doug Zongker512536a2010-02-17 16:11:44 -08001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Tao Bao36c35112016-10-25 14:17:26 -070017#include "applypatch_modes.h"
18
Tao Baod34e6bc2018-07-13 13:11:09 -070019#include <getopt.h>
Doug Zongker512536a2010-02-17 16:11:44 -080020#include <stdio.h>
Doug Zongkerc4351c72010-02-22 14:46:32 -080021#include <stdlib.h>
22#include <string.h>
23#include <unistd.h>
Doug Zongker512536a2010-02-17 16:11:44 -080024
Yabin Cuid483c202016-02-03 17:08:52 -080025#include <memory>
Tianjie Xuaced5d92016-10-12 10:55:04 -070026#include <string>
Yabin Cuid483c202016-02-03 17:08:52 -080027#include <vector>
28
Tao Baod34e6bc2018-07-13 13:11:09 -070029#include <android-base/file.h>
Tao Bao859bfc52018-04-25 23:00:27 -070030#include <android-base/logging.h>
Tao Bao36c35112016-10-25 14:17:26 -070031#include <android-base/parseint.h>
32#include <android-base/strings.h>
Tao Baofada91c2016-10-27 18:16:06 -070033#include <openssl/sha.h>
34
Tao Baod80a9982016-03-03 11:43:47 -080035#include "applypatch/applypatch.h"
Doug Zongkerc4351c72010-02-22 14:46:32 -080036#include "edify/expr.h"
Doug Zongkerc4351c72010-02-22 14:46:32 -080037
Tao Bao5609bc82018-06-20 00:30:48 -070038static int CheckMode(const std::string& target_emmc) {
39 std::string err;
40 auto target = Partition::Parse(target_emmc, &err);
41 if (!target) {
42 LOG(ERROR) << "Failed to parse target \"" << target_emmc << "\": " << err;
43 return 2;
44 }
45 return CheckPartition(target) ? 0 : 1;
Doug Zongkerc4351c72010-02-22 14:46:32 -080046}
47
Tao Baod34e6bc2018-07-13 13:11:09 -070048static int FlashMode(const std::string& target_emmc, const std::string& source_file) {
Tao Bao5609bc82018-06-20 00:30:48 -070049 std::string err;
50 auto target = Partition::Parse(target_emmc, &err);
51 if (!target) {
52 LOG(ERROR) << "Failed to parse target \"" << target_emmc << "\": " << err;
Tao Baod34e6bc2018-07-13 13:11:09 -070053 return 2;
Tao Bao511d7592018-06-19 15:56:49 -070054 }
Tao Bao5609bc82018-06-20 00:30:48 -070055 return FlashPartition(target, source_file) ? 0 : 1;
Tao Baod34e6bc2018-07-13 13:11:09 -070056}
Doug Zongkerc4351c72010-02-22 14:46:32 -080057
Tao Baod34e6bc2018-07-13 13:11:09 -070058static int PatchMode(const std::string& target_emmc, const std::string& source_emmc,
59 const std::string& patch_file, const std::string& bonus_file) {
Tao Bao5609bc82018-06-20 00:30:48 -070060 std::string err;
61 auto target = Partition::Parse(target_emmc, &err);
62 if (!target) {
63 LOG(ERROR) << "Failed to parse target \"" << target_emmc << "\": " << err;
Tao Bao511d7592018-06-19 15:56:49 -070064 return 2;
65 }
Tao Bao36c35112016-10-25 14:17:26 -070066
Tao Bao5609bc82018-06-20 00:30:48 -070067 auto source = Partition::Parse(source_emmc, &err);
68 if (!source) {
69 LOG(ERROR) << "Failed to parse source \"" << source_emmc << "\": " << err;
Tao Bao859bfc52018-04-25 23:00:27 -070070 return 2;
71 }
Doug Zongkerc4351c72010-02-22 14:46:32 -080072
Tao Bao5609bc82018-06-20 00:30:48 -070073 std::string patch_contents;
74 if (!android::base::ReadFileToString(patch_file, &patch_contents)) {
Tao Baod34e6bc2018-07-13 13:11:09 -070075 PLOG(ERROR) << "Failed to read patch file \"" << patch_file << "\"";
76 return 1;
Tao Bao859bfc52018-04-25 23:00:27 -070077 }
Tao Baod34e6bc2018-07-13 13:11:09 -070078
Tao Bao5609bc82018-06-20 00:30:48 -070079 Value patch(Value::Type::BLOB, std::move(patch_contents));
Tao Baod34e6bc2018-07-13 13:11:09 -070080 std::unique_ptr<Value> bonus;
81 if (!bonus_file.empty()) {
82 std::string bonus_contents;
83 if (!android::base::ReadFileToString(bonus_file, &bonus_contents)) {
84 PLOG(ERROR) << "Failed to read bonus file \"" << bonus_file << "\"";
85 return 1;
86 }
87 bonus = std::make_unique<Value>(Value::Type::BLOB, std::move(bonus_contents));
88 }
89
Tao Bao5234ad42019-09-23 10:28:54 -070090 return PatchPartition(target, source, patch, bonus.get(), false) ? 0 : 1;
Tao Baod34e6bc2018-07-13 13:11:09 -070091}
92
93static void Usage() {
94 printf(
95 "Usage: \n"
96 "check mode\n"
97 " applypatch --check EMMC:<target-file>:<target-size>:<target-sha1>\n\n"
98 "flash mode\n"
99 " applypatch --flash <source-file>\n"
100 " --target EMMC:<target-file>:<target-size>:<target-sha1>\n\n"
101 "patch mode\n"
102 " applypatch [--bonus <bonus-file>]\n"
103 " --patch <patch-file>\n"
104 " --target EMMC:<target-file>:<target-size>:<target-sha1>\n"
105 " --source EMMC:<source-file>:<source-size>:<source-sha1>\n\n"
106 "show license\n"
107 " applypatch --license\n"
108 "\n\n");
109}
110
111int applypatch_modes(int argc, char* argv[]) {
112 static constexpr struct option OPTIONS[]{
113 // clang-format off
114 { "bonus", required_argument, nullptr, 0 },
115 { "check", required_argument, nullptr, 0 },
116 { "flash", required_argument, nullptr, 0 },
117 { "license", no_argument, nullptr, 0 },
118 { "patch", required_argument, nullptr, 0 },
119 { "source", required_argument, nullptr, 0 },
120 { "target", required_argument, nullptr, 0 },
121 { nullptr, 0, nullptr, 0 },
122 // clang-format on
123 };
124
125 std::string check_target;
126 std::string source;
127 std::string target;
128 std::string patch;
129 std::string bonus;
130
131 bool check_mode = false;
132 bool flash_mode = false;
133 bool patch_mode = false;
134
135 optind = 1;
136
137 int arg;
138 int option_index;
139 while ((arg = getopt_long(argc, argv, "", OPTIONS, &option_index)) != -1) {
140 switch (arg) {
141 case 0: {
142 std::string option = OPTIONS[option_index].name;
143 if (option == "bonus") {
144 bonus = optarg;
145 } else if (option == "check") {
146 check_target = optarg;
147 check_mode = true;
148 } else if (option == "flash") {
149 source = optarg;
150 flash_mode = true;
151 } else if (option == "license") {
152 return ShowLicenses();
153 } else if (option == "patch") {
154 patch = optarg;
155 patch_mode = true;
156 } else if (option == "source") {
157 source = optarg;
158 } else if (option == "target") {
159 target = optarg;
160 }
161 break;
162 }
163 case '?':
164 default:
165 LOG(ERROR) << "Invalid argument";
166 Usage();
167 return 2;
168 }
169 }
170
171 if (check_mode) {
172 return CheckMode(check_target);
173 }
174 if (flash_mode) {
175 if (!bonus.empty()) {
176 LOG(ERROR) << "bonus file not supported in flash mode";
177 return 1;
178 }
179 return FlashMode(target, source);
180 }
181 if (patch_mode) {
182 return PatchMode(target, source, patch, bonus);
183 }
184
185 Usage();
186 return 2;
Doug Zongker512536a2010-02-17 16:11:44 -0800187}