blob: ba64143600a64d0b432bd3c72f679cbd45709c52 [file] [log] [blame]
Ethan Yonker941a8992016-12-05 09:04:30 -06001/*
2 * Copyright (C) 2007 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
17#include <stdlib.h>
18#include <string>
19#include <vector>
20
21#ifdef AB_OTA_UPDATER
22#include <map>
23#include <android-base/parseint.h>
24#include <android-base/stringprintf.h>
25#include <android-base/strings.h>
26#endif
27#include <cutils/properties.h>
28
29#include "common.h"
30#include "installcommand.h"
31#include "minzip/SysUtil.h"
32#include "minzip/Zip.h"
33#ifdef USE_OLD_VERIFIER
34#include "verifier24/verifier.h"
35#else
36#include "verifier.h"
37#endif
38
39#ifdef AB_OTA_UPDATER
40
41static constexpr const char* AB_OTA_PAYLOAD_PROPERTIES = "payload_properties.txt";
42static constexpr const char* AB_OTA_PAYLOAD = "payload.bin";
43static constexpr const char* METADATA_PATH = "META-INF/com/android/metadata";
44
45// This function parses and returns the build.version.incremental
46static int parse_build_number(std::string str) {
47 size_t pos = str.find("=");
48 if (pos != std::string::npos) {
49 std::string num_string = android::base::Trim(str.substr(pos+1));
50 int build_number;
51 if (android::base::ParseInt(num_string.c_str(), &build_number, 0)) {
52 return build_number;
53 }
54 }
55
56 printf("Failed to parse build number in %s.\n", str.c_str());
57 return -1;
58}
59
60bool read_metadata_from_package(ZipArchive* zip, std::string* meta_data) {
61 const ZipEntry* meta_entry = mzFindZipEntry(zip, METADATA_PATH);
62 if (meta_entry == nullptr) {
63 printf("Failed to find %s in update package.\n", METADATA_PATH);
64 return false;
65 }
66
67 meta_data->resize(meta_entry->uncompLen, '\0');
68 if (!mzReadZipEntry(zip, meta_entry, &(*meta_data)[0], meta_entry->uncompLen)) {
69 printf("Failed to read metadata in update package.\n");
70 return false;
71 }
72 return true;
73}
74
75// Read the build.version.incremental of src/tgt from the metadata and log it to last_install.
76static void read_source_target_build(ZipArchive* zip, std::vector<std::string>& log_buffer) {
77 std::string meta_data;
78 if (!read_metadata_from_package(zip, &meta_data)) {
79 return;
80 }
81 // Examples of the pre-build and post-build strings in metadata:
82 // pre-build-incremental=2943039
83 // post-build-incremental=2951741
84 std::vector<std::string> lines = android::base::Split(meta_data, "\n");
85 for (const std::string& line : lines) {
86 std::string str = android::base::Trim(line);
87 if (android::base::StartsWith(str, "pre-build-incremental")){
88 int source_build = parse_build_number(str);
89 if (source_build != -1) {
90 log_buffer.push_back(android::base::StringPrintf("source_build: %d",
91 source_build));
92 }
93 } else if (android::base::StartsWith(str, "post-build-incremental")) {
94 int target_build = parse_build_number(str);
95 if (target_build != -1) {
96 log_buffer.push_back(android::base::StringPrintf("target_build: %d",
97 target_build));
98 }
99 }
100 }
101}
102
103// Parses the metadata of the OTA package in |zip| and checks whether we are
104// allowed to accept this A/B package. Downgrading is not allowed unless
105// explicitly enabled in the package and only for incremental packages.
106static int check_newer_ab_build(ZipArchive* zip)
107{
108 std::string metadata_str;
109 if (!read_metadata_from_package(zip, &metadata_str)) {
110 return INSTALL_CORRUPT;
111 }
112 std::map<std::string, std::string> metadata;
113 for (const std::string& line : android::base::Split(metadata_str, "\n")) {
114 size_t eq = line.find('=');
115 if (eq != std::string::npos) {
116 metadata[line.substr(0, eq)] = line.substr(eq + 1);
117 }
118 }
119 char value[PROPERTY_VALUE_MAX];
120
121 property_get("ro.product.device", value, "");
122 const std::string& pkg_device = metadata["pre-device"];
123 if (pkg_device != value || pkg_device.empty()) {
124 printf("Package is for product %s but expected %s\n",
125 pkg_device.c_str(), value);
126 return INSTALL_ERROR;
127 }
128
129 // We allow the package to not have any serialno, but if it has a non-empty
130 // value it should match.
131 property_get("ro.serialno", value, "");
132 const std::string& pkg_serial_no = metadata["serialno"];
133 if (!pkg_serial_no.empty() && pkg_serial_no != value) {
134 printf("Package is for serial %s\n", pkg_serial_no.c_str());
135 return INSTALL_ERROR;
136 }
137
138 if (metadata["ota-type"] != "AB") {
139 printf("Package is not A/B\n");
140 return INSTALL_ERROR;
141 }
142
143 // Incremental updates should match the current build.
144 property_get("ro.build.version.incremental", value, "");
145 const std::string& pkg_pre_build = metadata["pre-build-incremental"];
146 if (!pkg_pre_build.empty() && pkg_pre_build != value) {
147 printf("Package is for source build %s but expected %s\n",
148 pkg_pre_build.c_str(), value);
149 return INSTALL_ERROR;
150 }
151 property_get("ro.build.fingerprint", value, "");
152 const std::string& pkg_pre_build_fingerprint = metadata["pre-build"];
153 if (!pkg_pre_build_fingerprint.empty() &&
154 pkg_pre_build_fingerprint != value) {
155 printf("Package is for source build %s but expected %s\n",
156 pkg_pre_build_fingerprint.c_str(), value);
157 return INSTALL_ERROR;
158 }
159
160 // Check for downgrade version.
161 int64_t build_timestampt = property_get_int64(
162 "ro.build.date.utc", std::numeric_limits<int64_t>::max());
163 int64_t pkg_post_timespampt = 0;
164 // We allow to full update to the same version we are running, in case there
165 // is a problem with the current copy of that version.
166 if (metadata["post-timestamp"].empty() ||
167 !android::base::ParseInt(metadata["post-timestamp"].c_str(),
168 &pkg_post_timespampt) ||
169 pkg_post_timespampt < build_timestampt) {
170 if (metadata["ota-downgrade"] != "yes") {
171 printf("Update package is older than the current build, expected a "
172 "build newer than timestamp %" PRIu64 " but package has "
173 "timestamp %" PRIu64 " and downgrade not allowed.\n",
174 build_timestampt, pkg_post_timespampt);
175 return INSTALL_ERROR;
176 }
177 if (pkg_pre_build_fingerprint.empty()) {
178 printf("Downgrade package must have a pre-build version set, not "
179 "allowed.\n");
180 return INSTALL_ERROR;
181 }
182 }
183
184 return 0;
185}
186
187int
188abupdate_binary_command(const char* path, ZipArchive* zip, int retry_count,
189 int status_fd, std::vector<std::string>* cmd)
190{
191 int ret = check_newer_ab_build(zip);
192 if (ret) {
193 return ret;
194 }
195
196 // For A/B updates we extract the payload properties to a buffer and obtain
197 // the RAW payload offset in the zip file.
198 const ZipEntry* properties_entry =
199 mzFindZipEntry(zip, AB_OTA_PAYLOAD_PROPERTIES);
200 if (!properties_entry) {
201 printf("Can't find %s\n", AB_OTA_PAYLOAD_PROPERTIES);
202 return INSTALL_CORRUPT;
203 }
204 std::vector<unsigned char> payload_properties(
205 mzGetZipEntryUncompLen(properties_entry));
206 if (!mzExtractZipEntryToBuffer(zip, properties_entry,
207 payload_properties.data())) {
208 printf("Can't extract %s\n", AB_OTA_PAYLOAD_PROPERTIES);
209 return INSTALL_CORRUPT;
210 }
211
212 const ZipEntry* payload_entry = mzFindZipEntry(zip, AB_OTA_PAYLOAD);
213 if (!payload_entry) {
214 printf("Can't find %s\n", AB_OTA_PAYLOAD);
215 return INSTALL_CORRUPT;
216 }
217 long payload_offset = mzGetZipEntryOffset(payload_entry);
218 *cmd = {
219 "/sbin/update_engine_sideload",
220 android::base::StringPrintf("--payload=file://%s", path),
221 android::base::StringPrintf("--offset=%ld", payload_offset),
222 "--headers=" + std::string(payload_properties.begin(),
223 payload_properties.end()),
224 android::base::StringPrintf("--status_fd=%d", status_fd),
225 };
226 return INSTALL_SUCCESS;
227}
228
229#else
230
231int
232abupdate_binary_command(const char* path, ZipArchive* zip, int retry_count,
233 int status_fd, std::vector<std::string>* cmd)
234{
235 printf("No support for AB OTA zips included\n");
236 return INSTALL_CORRUPT;
237}
238
239#endif
240
241int
242update_binary_command(const char* path, ZipArchive* zip, int retry_count,
243 int status_fd, std::vector<std::string>* cmd)
244{
245 char charfd[16];
246 sprintf(charfd, "%i", status_fd);
247 cmd->push_back(TMP_UPDATER_BINARY_PATH);
248 cmd->push_back(EXPAND(RECOVERY_API_VERSION));
249 cmd->push_back(charfd);
250 cmd->push_back(path);
251 /**cmd = {
252 TMP_UPDATER_BINARY_PATH,
253 EXPAND(RECOVERY_API_VERSION), // defined in Android.mk
254 charfd,
255 path,
256 };*/
257 if (retry_count > 0)
258 cmd->push_back("retry");
259 return 0;
260}