blob: 0e1028bd15a5c7bb5b73fcd576a4e3849024fc3b [file] [log] [blame]
Doug Zongker9931f7f2009-06-10 14:11:53 -07001/*
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 Bao0c7839a2016-10-10 15:48:37 -070017#include "updater/install.h"
18
Doug Zongkerfbf3c102009-06-24 09:36:20 -070019#include <ctype.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070020#include <errno.h>
Tao Bao361342c2016-02-08 11:15:50 -080021#include <fcntl.h>
22#include <ftw.h>
23#include <inttypes.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070024#include <stdarg.h>
Doug Zongkerfbf3c102009-06-24 09:36:20 -070025#include <stdio.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070026#include <stdlib.h>
27#include <string.h>
Tao Bao361342c2016-02-08 11:15:50 -080028#include <sys/capability.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070029#include <sys/mount.h>
30#include <sys/stat.h>
31#include <sys/types.h>
Doug Zongkera3f89ea2009-09-10 14:10:48 -070032#include <sys/wait.h>
Nick Kralevich5dbdef02013-09-07 14:41:06 -070033#include <sys/xattr.h>
Tao Bao361342c2016-02-08 11:15:50 -080034#include <time.h>
35#include <unistd.h>
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070036#include <utime.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070037
Yabin Cui64be2132016-02-03 18:16:02 -080038#include <memory>
Tao Bao361342c2016-02-08 11:15:50 -080039#include <string>
Yabin Cui64be2132016-02-03 18:16:02 -080040#include <vector>
41
Tao Baod0f30882016-11-03 23:52:01 -070042#include <android-base/file.h>
Tao Bao039f2da2016-11-22 16:29:50 -080043#include <android-base/logging.h>
Tianjie Xu5fe280a2016-10-17 18:15:20 -070044#include <android-base/parsedouble.h>
Tao Baod0f30882016-11-03 23:52:01 -070045#include <android-base/parseint.h>
Elliott Hughescb220402016-09-23 15:30:55 -070046#include <android-base/properties.h>
Elliott Hughes4b166f02015-12-04 15:30:20 -080047#include <android-base/stringprintf.h>
Tao Baod0f30882016-11-03 23:52:01 -070048#include <android-base/strings.h>
Tianjie Xu22f11202018-08-27 10:50:31 -070049#include <android-base/unique_fd.h>
Tao Bao0d3f84f2016-12-28 15:09:20 -080050#include <applypatch/applypatch.h>
51#include <bootloader_message/bootloader_message.h>
Tao Baode40ba52016-10-05 23:17:01 -070052#include <ext4_utils/wipe.h>
Tao Bao361342c2016-02-08 11:15:50 -080053#include <openssl/sha.h>
Elliott Hughes4bbd5bf2016-04-01 18:24:39 -070054#include <selinux/label.h>
55#include <selinux/selinux.h>
Tao Bao09e468f2017-09-29 14:39:33 -070056#include <tune2fs.h>
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070057#include <ziparchive/zip_archive.h>
Tao Bao1107d962015-09-09 17:16:55 -070058
Doug Zongker9931f7f2009-06-10 14:11:53 -070059#include "edify/expr.h"
Tao Bao17054c02018-05-03 22:41:23 -070060#include "otautil/dirutil.h"
Tao Bao1fc5bf32017-10-06 07:43:41 -070061#include "otautil/error_code.h"
Tao Bao9a319f02018-01-04 13:19:11 -080062#include "otautil/mounts.h"
Tao Bao09e468f2017-09-29 14:39:33 -070063#include "otautil/print_sha1.h"
Tao Bao2c526392018-05-03 23:01:13 -070064#include "otautil/sysutil.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070065#include "updater/updater.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070066
Tao Bao1107d962015-09-09 17:16:55 -070067// Send over the buffer to recovery though the command pipe.
68static void uiPrint(State* state, const std::string& buffer) {
Tao Bao039f2da2016-11-22 16:29:50 -080069 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
Tao Baob6918c72015-05-19 17:02:16 -070070
Tao Bao039f2da2016-11-22 16:29:50 -080071 // "line1\nline2\n" will be split into 3 tokens: "line1", "line2" and "".
72 // So skip sending empty strings to UI.
73 std::vector<std::string> lines = android::base::Split(buffer, "\n");
74 for (auto& line : lines) {
75 if (!line.empty()) {
76 fprintf(ui->cmd_pipe, "ui_print %s\n", line.c_str());
Tao Bao1107d962015-09-09 17:16:55 -070077 }
Tao Bao039f2da2016-11-22 16:29:50 -080078 }
Tao Bao1107d962015-09-09 17:16:55 -070079
Tao Bao039f2da2016-11-22 16:29:50 -080080 // On the updater side, we need to dump the contents to stderr (which has
81 // been redirected to the log file). Because the recovery will only print
82 // the contents to screen when processing pipe command ui_print.
83 LOG(INFO) << buffer;
Michael Runged4a63422014-10-22 19:48:41 -070084}
85
Elliott Hughes83ce7552016-06-30 09:28:42 -070086void uiPrintf(State* _Nonnull state, const char* _Nonnull format, ...) {
Tao Bao039f2da2016-11-22 16:29:50 -080087 std::string error_msg;
Tao Bao1107d962015-09-09 17:16:55 -070088
Tao Bao039f2da2016-11-22 16:29:50 -080089 va_list ap;
90 va_start(ap, format);
91 android::base::StringAppendV(&error_msg, format, ap);
92 va_end(ap);
Tao Bao1107d962015-09-09 17:16:55 -070093
Tao Bao039f2da2016-11-22 16:29:50 -080094 uiPrint(state, error_msg);
Michael Runged4a63422014-10-22 19:48:41 -070095}
96
Tianjie Xu5419ad32018-02-02 16:49:15 -080097// This is the updater side handler for ui_print() in edify script. Contents will be sent over to
98// the recovery side for on-screen display.
99Value* UIPrintFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
100 std::vector<std::string> args;
101 if (!ReadArgs(state, argv, &args)) {
102 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
103 }
104
105 std::string buffer = android::base::Join(args, "");
106 uiPrint(state, buffer);
107 return StringValue(buffer);
108}
109
110// package_extract_file(package_file[, dest_file])
111// Extracts a single package_file from the update package and writes it to dest_file,
112// overwriting existing files if necessary. Without the dest_file argument, returns the
113// contents of the package file as a binary blob.
114Value* PackageExtractFileFn(const char* name, State* state,
115 const std::vector<std::unique_ptr<Expr>>& argv) {
116 if (argv.size() < 1 || argv.size() > 2) {
117 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 or 2 args, got %zu", name,
118 argv.size());
119 }
120
121 if (argv.size() == 2) {
122 // The two-argument version extracts to a file.
123
124 std::vector<std::string> args;
125 if (!ReadArgs(state, argv, &args)) {
126 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name,
127 argv.size());
128 }
129 const std::string& zip_path = args[0];
130 const std::string& dest_path = args[1];
131
132 ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip;
133 ZipString zip_string_path(zip_path.c_str());
134 ZipEntry entry;
135 if (FindEntry(za, zip_string_path, &entry) != 0) {
136 LOG(ERROR) << name << ": no " << zip_path << " in package";
137 return StringValue("");
138 }
139
Tianjie Xu22f11202018-08-27 10:50:31 -0700140 android::base::unique_fd fd(TEMP_FAILURE_RETRY(
141 open(dest_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)));
Tianjie Xu5419ad32018-02-02 16:49:15 -0800142 if (fd == -1) {
143 PLOG(ERROR) << name << ": can't open " << dest_path << " for write";
144 return StringValue("");
145 }
146
147 bool success = true;
148 int32_t ret = ExtractEntryToFile(za, &entry, fd);
149 if (ret != 0) {
150 LOG(ERROR) << name << ": Failed to extract entry \"" << zip_path << "\" ("
151 << entry.uncompressed_length << " bytes) to \"" << dest_path
152 << "\": " << ErrorCodeString(ret);
153 success = false;
154 }
Tianjie Xu22f11202018-08-27 10:50:31 -0700155 if (fsync(fd) == -1) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800156 PLOG(ERROR) << "fsync of \"" << dest_path << "\" failed";
157 success = false;
158 }
Tianjie Xu22f11202018-08-27 10:50:31 -0700159
160 if (close(fd.release()) != 0) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800161 PLOG(ERROR) << "close of \"" << dest_path << "\" failed";
162 success = false;
163 }
164
165 return StringValue(success ? "t" : "");
166 } else {
167 // The one-argument version returns the contents of the file as the result.
168
169 std::vector<std::string> args;
170 if (!ReadArgs(state, argv, &args)) {
171 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name,
172 argv.size());
173 }
174 const std::string& zip_path = args[0];
175
176 ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip;
177 ZipString zip_string_path(zip_path.c_str());
178 ZipEntry entry;
179 if (FindEntry(za, zip_string_path, &entry) != 0) {
180 return ErrorAbort(state, kPackageExtractFileFailure, "%s(): no %s in package", name,
181 zip_path.c_str());
182 }
183
184 std::string buffer;
185 buffer.resize(entry.uncompressed_length);
186
187 int32_t ret =
188 ExtractToMemory(za, &entry, reinterpret_cast<uint8_t*>(&buffer[0]), buffer.size());
189 if (ret != 0) {
190 return ErrorAbort(state, kPackageExtractFileFailure,
191 "%s: Failed to extract entry \"%s\" (%zu bytes) to memory: %s", name,
192 zip_path.c_str(), buffer.size(), ErrorCodeString(ret));
193 }
194
Tao Bao511d7592018-06-19 15:56:49 -0700195 return new Value(Value::Type::BLOB, buffer);
Tianjie Xu5419ad32018-02-02 16:49:15 -0800196 }
197}
198
Tao Bao5609bc82018-06-20 00:30:48 -0700199// patch_partition_check(target_partition, source_partition)
200// Checks if the target and source partitions have the desired checksums to be patched. It returns
201// directly, if the target partition already has the expected checksum. Otherwise it in turn
202// checks the integrity of the source partition and the backup file on /cache.
Tianjie Xu5419ad32018-02-02 16:49:15 -0800203//
Tao Bao5609bc82018-06-20 00:30:48 -0700204// For example, patch_partition_check(
205// "EMMC:/dev/block/boot:12342568:8aaacf187a6929d0e9c3e9e46ea7ff495b43424d",
206// "EMMC:/dev/block/boot:12363048:06b0b16299dcefc94900efed01e0763ff644ffa4")
207Value* PatchPartitionCheckFn(const char* name, State* state,
208 const std::vector<std::unique_ptr<Expr>>& argv) {
209 if (argv.size() != 2) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800210 return ErrorAbort(state, kArgsParsingFailure,
Tao Bao5609bc82018-06-20 00:30:48 -0700211 "%s(): Invalid number of args (expected 2, got %zu)", name, argv.size());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800212 }
213
214 std::vector<std::string> args;
Tao Bao5609bc82018-06-20 00:30:48 -0700215 if (!ReadArgs(state, argv, &args, 0, 2)) {
216 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
Tianjie Xu5419ad32018-02-02 16:49:15 -0800217 }
218
Tao Bao5609bc82018-06-20 00:30:48 -0700219 std::string err;
220 auto target = Partition::Parse(args[0], &err);
221 if (!target) {
222 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse target \"%s\": %s", name,
223 args[0].c_str(), err.c_str());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800224 }
225
Tao Bao5609bc82018-06-20 00:30:48 -0700226 auto source = Partition::Parse(args[1], &err);
227 if (!source) {
228 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse source \"%s\": %s", name,
229 args[1].c_str(), err.c_str());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800230 }
231
Tao Bao5609bc82018-06-20 00:30:48 -0700232 bool result = PatchPartitionCheck(target, source);
233 return StringValue(result ? "t" : "");
Tianjie Xu5419ad32018-02-02 16:49:15 -0800234}
235
Tao Bao5609bc82018-06-20 00:30:48 -0700236// patch_partition(target, source, patch)
237// Applies the given patch to the source partition, and writes the result to the target partition.
238//
239// For example, patch_partition(
240// "EMMC:/dev/block/boot:12342568:8aaacf187a6929d0e9c3e9e46ea7ff495b43424d",
241// "EMMC:/dev/block/boot:12363048:06b0b16299dcefc94900efed01e0763ff644ffa4",
242// package_extract_file("boot.img.p"))
243Value* PatchPartitionFn(const char* name, State* state,
244 const std::vector<std::unique_ptr<Expr>>& argv) {
245 if (argv.size() != 3) {
246 return ErrorAbort(state, kArgsParsingFailure,
247 "%s(): Invalid number of args (expected 3, got %zu)", name, argv.size());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800248 }
249
250 std::vector<std::string> args;
Tao Bao5609bc82018-06-20 00:30:48 -0700251 if (!ReadArgs(state, argv, &args, 0, 2)) {
252 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
Tianjie Xu5419ad32018-02-02 16:49:15 -0800253 }
Tianjie Xu5419ad32018-02-02 16:49:15 -0800254
Tao Bao5609bc82018-06-20 00:30:48 -0700255 std::string err;
256 auto target = Partition::Parse(args[0], &err);
257 if (!target) {
258 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse target \"%s\": %s", name,
259 args[0].c_str(), err.c_str());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800260 }
Tianjie Xu5419ad32018-02-02 16:49:15 -0800261
Tao Bao5609bc82018-06-20 00:30:48 -0700262 auto source = Partition::Parse(args[1], &err);
263 if (!source) {
264 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse source \"%s\": %s", name,
265 args[1].c_str(), err.c_str());
266 }
267
268 std::vector<std::unique_ptr<Value>> values;
269 if (!ReadValueArgs(state, argv, &values, 2, 1) || values[0]->type != Value::Type::BLOB) {
270 return ErrorAbort(state, kArgsParsingFailure, "%s(): Invalid patch arg", name);
271 }
272
273 bool result = PatchPartition(target, source, *values[0], nullptr);
274 return StringValue(result ? "t" : "");
Tianjie Xu5419ad32018-02-02 16:49:15 -0800275}
276
Doug Zongker3d177d02010-07-01 09:18:44 -0700277// mount(fs_type, partition_type, location, mount_point)
Tao Bao0831d0b2016-11-03 23:25:04 -0700278// mount(fs_type, partition_type, location, mount_point, mount_options)
Tianjie Xuc4447322017-03-06 14:44:59 -0800279
Doug Zongker3d177d02010-07-01 09:18:44 -0700280// fs_type="ext4" partition_type="EMMC" location=device
Tianjie Xuc4447322017-03-06 14:44:59 -0800281Value* MountFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
282 if (argv.size() != 4 && argv.size() != 5) {
283 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 4-5 args, got %zu", name,
284 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800285 }
286
287 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800288 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800289 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
290 }
291 const std::string& fs_type = args[0];
292 const std::string& partition_type = args[1];
293 const std::string& location = args[2];
294 const std::string& mount_point = args[3];
295 std::string mount_options;
296
Tianjie Xuc4447322017-03-06 14:44:59 -0800297 if (argv.size() == 5) {
Tao Bao039f2da2016-11-22 16:29:50 -0800298 mount_options = args[4];
299 }
300
301 if (fs_type.empty()) {
302 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
303 }
304 if (partition_type.empty()) {
305 return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
306 name);
307 }
308 if (location.empty()) {
309 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
310 }
311 if (mount_point.empty()) {
312 return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
313 name);
314 }
315
316 {
317 char* secontext = nullptr;
318
319 if (sehandle) {
320 selabel_lookup(sehandle, &secontext, mount_point.c_str(), 0755);
321 setfscreatecon(secontext);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700322 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700323
Tao Bao039f2da2016-11-22 16:29:50 -0800324 mkdir(mount_point.c_str(), 0755);
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700325
Tao Bao039f2da2016-11-22 16:29:50 -0800326 if (secontext) {
327 freecon(secontext);
328 setfscreatecon(nullptr);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700329 }
Tao Bao039f2da2016-11-22 16:29:50 -0800330 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700331
Tao Bao039f2da2016-11-22 16:29:50 -0800332 if (mount(location.c_str(), mount_point.c_str(), fs_type.c_str(),
333 MS_NOATIME | MS_NODEV | MS_NODIRATIME, mount_options.c_str()) < 0) {
Tao Bao0bbc7642017-03-29 23:57:47 -0700334 uiPrintf(state, "%s: Failed to mount %s at %s: %s", name, location.c_str(), mount_point.c_str(),
335 strerror(errno));
Tao Bao039f2da2016-11-22 16:29:50 -0800336 return StringValue("");
337 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700338
Tao Bao039f2da2016-11-22 16:29:50 -0800339 return StringValue(mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700340}
341
Doug Zongker8edb00c2009-06-11 17:21:44 -0700342// is_mounted(mount_point)
Tianjie Xuc4447322017-03-06 14:44:59 -0800343Value* IsMountedFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
344 if (argv.size() != 1) {
345 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800346 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700347
Tao Bao039f2da2016-11-22 16:29:50 -0800348 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800349 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800350 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
351 }
352 const std::string& mount_point = args[0];
353 if (mount_point.empty()) {
354 return ErrorAbort(state, kArgsParsingFailure,
355 "mount_point argument to unmount() can't be empty");
356 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700357
Tao Bao039f2da2016-11-22 16:29:50 -0800358 scan_mounted_volumes();
359 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str());
360 if (vol == nullptr) {
361 return StringValue("");
362 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700363
Tao Bao039f2da2016-11-22 16:29:50 -0800364 return StringValue(mount_point);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700365}
366
Tianjie Xuc4447322017-03-06 14:44:59 -0800367Value* UnmountFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
368 if (argv.size() != 1) {
369 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800370 }
371 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800372 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800373 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
374 }
375 const std::string& mount_point = args[0];
376 if (mount_point.empty()) {
377 return ErrorAbort(state, kArgsParsingFailure,
378 "mount_point argument to unmount() can't be empty");
379 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700380
Tao Bao039f2da2016-11-22 16:29:50 -0800381 scan_mounted_volumes();
382 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str());
383 if (vol == nullptr) {
Tao Bao0bbc7642017-03-29 23:57:47 -0700384 uiPrintf(state, "Failed to unmount %s: No such volume", mount_point.c_str());
Tao Bao039f2da2016-11-22 16:29:50 -0800385 return nullptr;
386 } else {
387 int ret = unmount_mounted_volume(vol);
388 if (ret != 0) {
Tao Bao0bbc7642017-03-29 23:57:47 -0700389 uiPrintf(state, "Failed to unmount %s: %s", mount_point.c_str(), strerror(errno));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700390 }
Tao Bao039f2da2016-11-22 16:29:50 -0800391 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700392
Tao Bao039f2da2016-11-22 16:29:50 -0800393 return StringValue(mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700394}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700395
Tao Bao3d69f0d2018-12-20 09:44:06 -0800396static int exec_cmd(const std::vector<std::string>& args) {
397 CHECK(!args.empty());
398 auto argv = StringVectorToNullTerminatedArray(args);
399
Tao Bao039f2da2016-11-22 16:29:50 -0800400 pid_t child;
401 if ((child = vfork()) == 0) {
Tao Bao3d69f0d2018-12-20 09:44:06 -0800402 execv(argv[0], argv.data());
Tao Bao3da88012017-02-03 13:09:23 -0800403 _exit(EXIT_FAILURE);
Tao Bao039f2da2016-11-22 16:29:50 -0800404 }
405
406 int status;
407 waitpid(child, &status, 0);
408 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Tao Bao3d69f0d2018-12-20 09:44:06 -0800409 LOG(ERROR) << args[0] << " failed with status " << WEXITSTATUS(status);
Tao Bao039f2da2016-11-22 16:29:50 -0800410 }
411 return WEXITSTATUS(status);
JP Abgrall37aedb32014-06-16 19:07:39 -0700412}
413
Stephen Smalley779701d2012-02-09 14:13:23 -0500414// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700415//
Tao Bao039f2da2016-11-22 16:29:50 -0800416// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
417// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700418// if fs_size == 0, then make fs uses the entire partition.
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800419// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700420// if fs_size < 0, then reserve that many bytes at the end of the partition (not for "f2fs")
Tianjie Xuc4447322017-03-06 14:44:59 -0800421Value* FormatFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
422 if (argv.size() != 5) {
423 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 5 args, got %zu", name,
424 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800425 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500426
Tao Bao039f2da2016-11-22 16:29:50 -0800427 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800428 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800429 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
430 }
431 const std::string& fs_type = args[0];
432 const std::string& partition_type = args[1];
433 const std::string& location = args[2];
434 const std::string& fs_size = args[3];
435 const std::string& mount_point = args[4];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700436
Tao Bao039f2da2016-11-22 16:29:50 -0800437 if (fs_type.empty()) {
438 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
439 }
440 if (partition_type.empty()) {
441 return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
442 name);
443 }
444 if (location.empty()) {
445 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
446 }
447 if (mount_point.empty()) {
448 return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
449 name);
450 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700451
Tao Bao039f2da2016-11-22 16:29:50 -0800452 int64_t size;
453 if (!android::base::ParseInt(fs_size, &size)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800454 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800455 fs_size.c_str());
456 }
457
458 if (fs_type == "ext4") {
Tao Bao3d69f0d2018-12-20 09:44:06 -0800459 std::vector<std::string> mke2fs_args = {
460 "/system/bin/mke2fs", "-t", "ext4", "-b", "4096", location
461 };
Jin Qianded2dac2017-04-21 14:36:12 -0700462 if (size != 0) {
Tao Bao3d69f0d2018-12-20 09:44:06 -0800463 mke2fs_args.push_back(std::to_string(size / 4096LL));
Jin Qianded2dac2017-04-21 14:36:12 -0700464 }
465
Tao Bao3d69f0d2018-12-20 09:44:06 -0800466 if (auto status = exec_cmd(mke2fs_args); status != 0) {
Jin Qian502fd1c2017-11-02 11:58:12 -0700467 LOG(ERROR) << name << ": mke2fs failed (" << status << ") on " << location;
468 return StringValue("");
Jin Qianded2dac2017-04-21 14:36:12 -0700469 }
470
Tao Bao3d69f0d2018-12-20 09:44:06 -0800471 if (auto status = exec_cmd({ "/system/bin/e2fsdroid", "-e", "-a", mount_point, location });
472 status != 0) {
Jin Qianded2dac2017-04-21 14:36:12 -0700473 LOG(ERROR) << name << ": e2fsdroid failed (" << status << ") on " << location;
Tao Bao039f2da2016-11-22 16:29:50 -0800474 return StringValue("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700475 }
Tao Bao039f2da2016-11-22 16:29:50 -0800476 return StringValue(location);
Tao Bao3d69f0d2018-12-20 09:44:06 -0800477 }
478
479 if (fs_type == "f2fs") {
Tao Bao039f2da2016-11-22 16:29:50 -0800480 if (size < 0) {
481 LOG(ERROR) << name << ": fs_size can't be negative for f2fs: " << fs_size;
482 return StringValue("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700483 }
Tao Bao3d69f0d2018-12-20 09:44:06 -0800484 std::vector<std::string> f2fs_args = {
485 "/sbin/mkfs.f2fs", "-g", "android", "-w", "512", location
486 };
487 if (size >= 512) {
488 f2fs_args.push_back(std::to_string(size / 512));
489 }
490 if (auto status = exec_cmd(f2fs_args); status != 0) {
Tao Bao039f2da2016-11-22 16:29:50 -0800491 LOG(ERROR) << name << ": mkfs.f2fs failed (" << status << ") on " << location;
492 return StringValue("");
493 }
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800494
Tao Bao3d69f0d2018-12-20 09:44:06 -0800495 if (auto status = exec_cmd({ "/sbin/sload.f2fs", "-t", mount_point, location }); status != 0) {
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800496 LOG(ERROR) << name << ": sload.f2fs failed (" << status << ") on " << location;
497 return StringValue("");
498 }
499
Tao Bao039f2da2016-11-22 16:29:50 -0800500 return StringValue(location);
Tao Bao039f2da2016-11-22 16:29:50 -0800501 }
502
Tao Bao3d69f0d2018-12-20 09:44:06 -0800503 LOG(ERROR) << name << ": unsupported fs_type \"" << fs_type << "\" partition_type \""
504 << partition_type << "\"";
Tao Bao039f2da2016-11-22 16:29:50 -0800505 return nullptr;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700506}
507
Tianjie Xuc4447322017-03-06 14:44:59 -0800508Value* ShowProgressFn(const char* name, State* state,
509 const std::vector<std::unique_ptr<Expr>>& argv) {
510 if (argv.size() != 2) {
511 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
512 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800513 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700514
Tao Bao039f2da2016-11-22 16:29:50 -0800515 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800516 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800517 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
518 }
519 const std::string& frac_str = args[0];
520 const std::string& sec_str = args[1];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700521
Tao Bao039f2da2016-11-22 16:29:50 -0800522 double frac;
523 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800524 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800525 frac_str.c_str());
526 }
527 int sec;
528 if (!android::base::ParseInt(sec_str.c_str(), &sec)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800529 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800530 sec_str.c_str());
531 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700532
Tao Bao039f2da2016-11-22 16:29:50 -0800533 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
534 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700535
Tao Bao039f2da2016-11-22 16:29:50 -0800536 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700537}
538
Tianjie Xu5419ad32018-02-02 16:49:15 -0800539Value* SetProgressFn(const char* name, State* state,
540 const std::vector<std::unique_ptr<Expr>>& argv) {
Tianjie Xuc4447322017-03-06 14:44:59 -0800541 if (argv.size() != 1) {
542 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800543 }
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700544
Tao Bao039f2da2016-11-22 16:29:50 -0800545 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800546 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800547 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
548 }
549 const std::string& frac_str = args[0];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700550
Tao Bao039f2da2016-11-22 16:29:50 -0800551 double frac;
552 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800553 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800554 frac_str.c_str());
555 }
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700556
Tao Bao039f2da2016-11-22 16:29:50 -0800557 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
558 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700559
Tao Bao039f2da2016-11-22 16:29:50 -0800560 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700561}
562
Tianjie Xuc4447322017-03-06 14:44:59 -0800563Value* GetPropFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
564 if (argv.size() != 1) {
565 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800566 }
567 std::string key;
568 if (!Evaluate(state, argv[0], &key)) {
569 return nullptr;
570 }
571 std::string value = android::base::GetProperty(key, "");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700572
Tao Bao039f2da2016-11-22 16:29:50 -0800573 return StringValue(value);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700574}
575
Doug Zongker47cace92009-06-18 10:11:50 -0700576// file_getprop(file, key)
577//
578// interprets 'file' as a getprop-style file (key=value pairs, one
Tao Bao51d516e2016-11-03 14:49:01 -0700579// per line. # comment lines, blank lines, lines without '=' ignored),
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700580// and returns the value for 'key' (or "" if it isn't defined).
Tianjie Xu5419ad32018-02-02 16:49:15 -0800581Value* FileGetPropFn(const char* name, State* state,
582 const std::vector<std::unique_ptr<Expr>>& argv) {
Tianjie Xuc4447322017-03-06 14:44:59 -0800583 if (argv.size() != 2) {
584 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
585 argv.size());
Tao Bao358c2ec2016-11-28 11:48:43 -0800586 }
587
588 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800589 if (!ReadArgs(state, argv, &args)) {
Tao Bao358c2ec2016-11-28 11:48:43 -0800590 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
591 }
592 const std::string& filename = args[0];
593 const std::string& key = args[1];
594
Tianjie Xu22f11202018-08-27 10:50:31 -0700595 std::string buffer;
596 if (!android::base::ReadFileToString(filename, &buffer)) {
597 ErrorAbort(state, kFreadFailure, "%s: failed to read %s", name, filename.c_str());
Tao Bao358c2ec2016-11-28 11:48:43 -0800598 return nullptr;
599 }
600
Tao Bao358c2ec2016-11-28 11:48:43 -0800601 std::vector<std::string> lines = android::base::Split(buffer, "\n");
602 for (size_t i = 0; i < lines.size(); i++) {
603 std::string line = android::base::Trim(lines[i]);
604
605 // comment or blank line: skip to next line
606 if (line.empty() || line[0] == '#') {
607 continue;
608 }
609 size_t equal_pos = line.find('=');
610 if (equal_pos == std::string::npos) {
611 continue;
Tao Bao51d516e2016-11-03 14:49:01 -0700612 }
613
Tao Bao358c2ec2016-11-28 11:48:43 -0800614 // trim whitespace between key and '='
615 std::string str = android::base::Trim(line.substr(0, equal_pos));
Doug Zongker47cace92009-06-18 10:11:50 -0700616
Tao Bao358c2ec2016-11-28 11:48:43 -0800617 // not the key we're looking for
618 if (key != str) continue;
Doug Zongker47cace92009-06-18 10:11:50 -0700619
Tao Bao358c2ec2016-11-28 11:48:43 -0800620 return StringValue(android::base::Trim(line.substr(equal_pos + 1)));
621 }
Doug Zongker47cace92009-06-18 10:11:50 -0700622
Tao Bao358c2ec2016-11-28 11:48:43 -0800623 return StringValue("");
Doug Zongker47cace92009-06-18 10:11:50 -0700624}
625
Doug Zongker8edb00c2009-06-11 17:21:44 -0700626// apply_patch_space(bytes)
Tianjie Xu5419ad32018-02-02 16:49:15 -0800627Value* ApplyPatchSpaceFn(const char* name, State* state,
628 const std::vector<std::unique_ptr<Expr>>& argv) {
Tianjie Xuc4447322017-03-06 14:44:59 -0800629 if (argv.size() != 1) {
630 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 args, got %zu", name,
631 argv.size());
632 }
Tao Bao039f2da2016-11-22 16:29:50 -0800633 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800634 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800635 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
636 }
637 const std::string& bytes_str = args[0];
Doug Zongkerc4351c72010-02-22 14:46:32 -0800638
Tao Bao039f2da2016-11-22 16:29:50 -0800639 size_t bytes;
640 if (!android::base::ParseUint(bytes_str.c_str(), &bytes)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800641 return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count", name,
642 bytes_str.c_str());
Tao Bao039f2da2016-11-22 16:29:50 -0800643 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800644
Tianjie Xu99b73be2017-11-28 17:23:06 -0800645 // Skip the cache size check if the update is a retry.
Tao Bao5ee25662018-07-11 15:55:32 -0700646 if (state->is_retry || CheckAndFreeSpaceOnCache(bytes)) {
Tianjie Xu99b73be2017-11-28 17:23:06 -0800647 return StringValue("t");
648 }
649 return StringValue("");
Doug Zongkerc4351c72010-02-22 14:46:32 -0800650}
651
Tianjie Xuc4447322017-03-06 14:44:59 -0800652Value* WipeCacheFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
653 if (!argv.empty()) {
654 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %zu", name,
655 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800656 }
657 fprintf(static_cast<UpdaterInfo*>(state->cookie)->cmd_pipe, "wipe_cache\n");
658 return StringValue("t");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700659}
660
Tianjie Xuc4447322017-03-06 14:44:59 -0800661Value* RunProgramFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
662 if (argv.size() < 1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800663 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
664 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700665
Tao Bao039f2da2016-11-22 16:29:50 -0800666 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800667 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800668 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
669 }
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700670
Tao Bao3d69f0d2018-12-20 09:44:06 -0800671 auto exec_args = StringVectorToNullTerminatedArray(args);
672 LOG(INFO) << "about to run program [" << exec_args[0] << "] with " << argv.size() << " args";
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700673
Tao Bao039f2da2016-11-22 16:29:50 -0800674 pid_t child = fork();
675 if (child == 0) {
Tao Bao3d69f0d2018-12-20 09:44:06 -0800676 execv(exec_args[0], exec_args.data());
Tao Bao039f2da2016-11-22 16:29:50 -0800677 PLOG(ERROR) << "run_program: execv failed";
Tao Bao3da88012017-02-03 13:09:23 -0800678 _exit(EXIT_FAILURE);
Tao Bao039f2da2016-11-22 16:29:50 -0800679 }
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700680
Tao Bao039f2da2016-11-22 16:29:50 -0800681 int status;
682 waitpid(child, &status, 0);
683 if (WIFEXITED(status)) {
684 if (WEXITSTATUS(status) != 0) {
685 LOG(ERROR) << "run_program: child exited with status " << WEXITSTATUS(status);
686 }
687 } else if (WIFSIGNALED(status)) {
688 LOG(ERROR) << "run_program: child terminated by signal " << WTERMSIG(status);
689 }
690
691 return StringValue(std::to_string(status));
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700692}
693
Tao Bao511d7592018-06-19 15:56:49 -0700694// read_file(filename)
Tao Baobafd6c72018-07-09 15:08:50 -0700695// Reads a local file 'filename' and returns its contents as a string Value.
Tianjie Xuc4447322017-03-06 14:44:59 -0800696Value* ReadFileFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
697 if (argv.size() != 1) {
698 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800699 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700700
Tao Bao039f2da2016-11-22 16:29:50 -0800701 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800702 if (!ReadArgs(state, argv, &args)) {
Tao Bao511d7592018-06-19 15:56:49 -0700703 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
Tao Bao039f2da2016-11-22 16:29:50 -0800704 }
705 const std::string& filename = args[0];
Doug Zongker512536a2010-02-17 16:11:44 -0800706
Tao Baobafd6c72018-07-09 15:08:50 -0700707 std::string contents;
708 if (android::base::ReadFileToString(filename, &contents)) {
709 return new Value(Value::Type::STRING, std::move(contents));
Tao Bao039f2da2016-11-22 16:29:50 -0800710 }
Tao Bao511d7592018-06-19 15:56:49 -0700711
712 // Leave it to caller to handle the failure.
Tao Baobafd6c72018-07-09 15:08:50 -0700713 PLOG(ERROR) << name << ": Failed to read " << filename;
Tao Bao511d7592018-06-19 15:56:49 -0700714 return StringValue("");
Doug Zongker512536a2010-02-17 16:11:44 -0800715}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700716
Tao Baod0f30882016-11-03 23:52:01 -0700717// write_value(value, filename)
718// Writes 'value' to 'filename'.
719// Example: write_value("960000", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq")
Tianjie Xuc4447322017-03-06 14:44:59 -0800720Value* WriteValueFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
721 if (argv.size() != 2) {
722 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
723 argv.size());
Tao Baod0f30882016-11-03 23:52:01 -0700724 }
725
726 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800727 if (!ReadArgs(state, argv, &args)) {
Tao Baod0f30882016-11-03 23:52:01 -0700728 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
729 }
730
731 const std::string& filename = args[1];
732 if (filename.empty()) {
733 return ErrorAbort(state, kArgsParsingFailure, "%s(): Filename cannot be empty", name);
734 }
735
736 const std::string& value = args[0];
737 if (!android::base::WriteStringToFile(value, filename)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800738 PLOG(ERROR) << name << ": Failed to write to \"" << filename << "\"";
Tao Baod0f30882016-11-03 23:52:01 -0700739 return StringValue("");
740 } else {
741 return StringValue("t");
742 }
743}
744
Doug Zongkerc87bab12013-11-25 13:53:25 -0800745// Immediately reboot the device. Recovery is not finished normally,
746// so if you reboot into recovery it will re-start applying the
747// current package (because nothing has cleared the copy of the
748// arguments stored in the BCB).
749//
750// The argument is the partition name passed to the android reboot
751// property. It can be "recovery" to boot from the recovery
752// partition, or "" (empty string) to boot from the regular boot
753// partition.
Tianjie Xuc4447322017-03-06 14:44:59 -0800754Value* RebootNowFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
755 if (argv.size() != 2) {
756 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
757 argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -0800758 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800759
Tao Baobedf5fc2016-11-18 12:01:26 -0800760 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800761 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800762 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
763 }
764 const std::string& filename = args[0];
765 const std::string& property = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -0800766
Tao Baobedf5fc2016-11-18 12:01:26 -0800767 // Zero out the 'command' field of the bootloader message. Leave the rest intact.
768 bootloader_message boot;
769 std::string err;
770 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800771 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800772 return StringValue("");
773 }
774 memset(boot.command, 0, sizeof(boot.command));
775 if (!write_bootloader_message_to(boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800776 LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800777 return StringValue("");
778 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800779
Tao Bao2c526392018-05-03 23:01:13 -0700780 reboot("reboot," + property);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800781
Tao Baobedf5fc2016-11-18 12:01:26 -0800782 sleep(5);
783 return ErrorAbort(state, kRebootFailure, "%s() failed to reboot", name);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800784}
785
786// Store a string value somewhere that future invocations of recovery
787// can access it. This value is called the "stage" and can be used to
788// drive packages that need to do reboots in the middle of
789// installation and keep track of where they are in the multi-stage
790// install.
791//
792// The first argument is the block device for the misc partition
793// ("/misc" in the fstab), which is where this value is stored. The
794// second argument is the string to store; it should not exceed 31
795// bytes.
Tianjie Xuc4447322017-03-06 14:44:59 -0800796Value* SetStageFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
797 if (argv.size() != 2) {
798 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
799 argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -0800800 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800801
Tao Baobedf5fc2016-11-18 12:01:26 -0800802 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800803 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800804 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
805 }
806 const std::string& filename = args[0];
807 const std::string& stagestr = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -0800808
Tao Baobedf5fc2016-11-18 12:01:26 -0800809 // Store this value in the misc partition, immediately after the
810 // bootloader message that the main recovery uses to save its
811 // arguments in case of the device restarting midway through
812 // package installation.
813 bootloader_message boot;
814 std::string err;
815 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800816 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800817 return StringValue("");
818 }
819 strlcpy(boot.stage, stagestr.c_str(), sizeof(boot.stage));
820 if (!write_bootloader_message_to(boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800821 LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800822 return StringValue("");
823 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800824
Tao Baobedf5fc2016-11-18 12:01:26 -0800825 return StringValue(filename);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800826}
827
828// Return the value most recently saved with SetStageFn. The argument
829// is the block device for the misc partition.
Tianjie Xuc4447322017-03-06 14:44:59 -0800830Value* GetStageFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
831 if (argv.size() != 1) {
832 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -0800833 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800834
Tao Baobedf5fc2016-11-18 12:01:26 -0800835 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800836 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800837 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
838 }
839 const std::string& filename = args[0];
Doug Zongkerc87bab12013-11-25 13:53:25 -0800840
Tao Baobedf5fc2016-11-18 12:01:26 -0800841 bootloader_message boot;
842 std::string err;
843 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800844 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800845 return StringValue("");
846 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800847
Tao Baobedf5fc2016-11-18 12:01:26 -0800848 return StringValue(boot.stage);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800849}
850
Tianjie Xuc4447322017-03-06 14:44:59 -0800851Value* WipeBlockDeviceFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
852 if (argv.size() != 2) {
853 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
854 argv.size());
Tao Bao358c2ec2016-11-28 11:48:43 -0800855 }
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800856
Tao Bao358c2ec2016-11-28 11:48:43 -0800857 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800858 if (!ReadArgs(state, argv, &args)) {
Tao Bao358c2ec2016-11-28 11:48:43 -0800859 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
860 }
861 const std::string& filename = args[0];
862 const std::string& len_str = args[1];
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800863
Tao Bao358c2ec2016-11-28 11:48:43 -0800864 size_t len;
865 if (!android::base::ParseUint(len_str.c_str(), &len)) {
866 return nullptr;
867 }
Tianjie Xu22f11202018-08-27 10:50:31 -0700868 android::base::unique_fd fd(open(filename.c_str(), O_WRONLY));
869 if (fd == -1) {
870 PLOG(ERROR) << "Failed to open " << filename;
871 return StringValue("");
872 }
873
Tao Bao358c2ec2016-11-28 11:48:43 -0800874 // The wipe_block_device function in ext4_utils returns 0 on success and 1
875 // for failure.
876 int status = wipe_block_device(fd, len);
877 return StringValue((status == 0) ? "t" : "");
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800878}
879
Tianjie Xuc4447322017-03-06 14:44:59 -0800880Value* EnableRebootFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
881 if (!argv.empty()) {
882 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %zu", name,
883 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800884 }
885 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
886 fprintf(ui->cmd_pipe, "enable_reboot\n");
887 return StringValue("t");
Doug Zongkerc704e062014-05-23 08:40:35 -0700888}
889
Tianjie Xuc4447322017-03-06 14:44:59 -0800890Value* Tune2FsFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
891 if (argv.empty()) {
892 return ErrorAbort(state, kArgsParsingFailure, "%s() expects args, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800893 }
Michael Rungeacf47db2014-11-21 00:12:28 -0800894
Tao Bao039f2da2016-11-22 16:29:50 -0800895 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800896 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800897 return ErrorAbort(state, kArgsParsingFailure, "%s() could not read args", name);
898 }
Michael Rungeacf47db2014-11-21 00:12:28 -0800899
Tao Bao3d69f0d2018-12-20 09:44:06 -0800900 // tune2fs expects the program name as its first arg.
901 args.insert(args.begin(), "tune2fs");
902 auto tune2fs_args = StringVectorToNullTerminatedArray(args);
Michael Rungeacf47db2014-11-21 00:12:28 -0800903
Tao Bao3d69f0d2018-12-20 09:44:06 -0800904 // tune2fs changes the filesystem parameters on an ext2 filesystem; it returns 0 on success.
905 if (auto result = tune2fs_main(tune2fs_args.size() - 1, tune2fs_args.data()); result != 0) {
Tao Bao039f2da2016-11-22 16:29:50 -0800906 return ErrorAbort(state, kTune2FsFailure, "%s() returned error code %d", name, result);
907 }
908 return StringValue("t");
Michael Rungeacf47db2014-11-21 00:12:28 -0800909}
910
Doug Zongker9931f7f2009-06-10 14:11:53 -0700911void RegisterInstallFunctions() {
Tao Bao039f2da2016-11-22 16:29:50 -0800912 RegisterFunction("mount", MountFn);
913 RegisterFunction("is_mounted", IsMountedFn);
914 RegisterFunction("unmount", UnmountFn);
915 RegisterFunction("format", FormatFn);
916 RegisterFunction("show_progress", ShowProgressFn);
917 RegisterFunction("set_progress", SetProgressFn);
Tao Bao039f2da2016-11-22 16:29:50 -0800918 RegisterFunction("package_extract_file", PackageExtractFileFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700919
Tao Bao039f2da2016-11-22 16:29:50 -0800920 RegisterFunction("getprop", GetPropFn);
921 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700922
Tao Bao039f2da2016-11-22 16:29:50 -0800923 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Tao Bao5609bc82018-06-20 00:30:48 -0700924 RegisterFunction("patch_partition", PatchPartitionFn);
925 RegisterFunction("patch_partition_check", PatchPartitionCheckFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700926
Tao Bao039f2da2016-11-22 16:29:50 -0800927 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -0800928
Tao Bao039f2da2016-11-22 16:29:50 -0800929 RegisterFunction("read_file", ReadFileFn);
Tao Bao039f2da2016-11-22 16:29:50 -0800930 RegisterFunction("write_value", WriteValueFn);
Doug Zongker512536a2010-02-17 16:11:44 -0800931
Tao Bao039f2da2016-11-22 16:29:50 -0800932 RegisterFunction("wipe_cache", WipeCacheFn);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700933
Tao Bao039f2da2016-11-22 16:29:50 -0800934 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700935
Tao Bao039f2da2016-11-22 16:29:50 -0800936 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800937
Tao Bao039f2da2016-11-22 16:29:50 -0800938 RegisterFunction("reboot_now", RebootNowFn);
939 RegisterFunction("get_stage", GetStageFn);
940 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -0700941
Tao Bao039f2da2016-11-22 16:29:50 -0800942 RegisterFunction("enable_reboot", EnableRebootFn);
943 RegisterFunction("tune2fs", Tune2FsFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700944}