blob: c30f63960de9e1380e2911702e09b13d0d5dde57 [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;
Tianjie Xu5419ad32018-02-02 16:49:15 -0800133 ZipEntry entry;
Elliott Hughesa86dddb2019-05-03 22:52:37 -0700134 if (FindEntry(za, zip_path, &entry) != 0) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800135 LOG(ERROR) << name << ": no " << zip_path << " in package";
136 return StringValue("");
137 }
138
Tianjie Xu22f11202018-08-27 10:50:31 -0700139 android::base::unique_fd fd(TEMP_FAILURE_RETRY(
140 open(dest_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)));
Tianjie Xu5419ad32018-02-02 16:49:15 -0800141 if (fd == -1) {
142 PLOG(ERROR) << name << ": can't open " << dest_path << " for write";
143 return StringValue("");
144 }
145
146 bool success = true;
147 int32_t ret = ExtractEntryToFile(za, &entry, fd);
148 if (ret != 0) {
149 LOG(ERROR) << name << ": Failed to extract entry \"" << zip_path << "\" ("
150 << entry.uncompressed_length << " bytes) to \"" << dest_path
151 << "\": " << ErrorCodeString(ret);
152 success = false;
153 }
Tianjie Xu22f11202018-08-27 10:50:31 -0700154 if (fsync(fd) == -1) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800155 PLOG(ERROR) << "fsync of \"" << dest_path << "\" failed";
156 success = false;
157 }
Tianjie Xu22f11202018-08-27 10:50:31 -0700158
159 if (close(fd.release()) != 0) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800160 PLOG(ERROR) << "close of \"" << dest_path << "\" failed";
161 success = false;
162 }
163
164 return StringValue(success ? "t" : "");
165 } else {
166 // The one-argument version returns the contents of the file as the result.
167
168 std::vector<std::string> args;
169 if (!ReadArgs(state, argv, &args)) {
170 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name,
171 argv.size());
172 }
173 const std::string& zip_path = args[0];
174
175 ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip;
Tianjie Xu5419ad32018-02-02 16:49:15 -0800176 ZipEntry entry;
Elliott Hughesa86dddb2019-05-03 22:52:37 -0700177 if (FindEntry(za, zip_path, &entry) != 0) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800178 return ErrorAbort(state, kPackageExtractFileFailure, "%s(): no %s in package", name,
179 zip_path.c_str());
180 }
181
182 std::string buffer;
183 buffer.resize(entry.uncompressed_length);
184
185 int32_t ret =
186 ExtractToMemory(za, &entry, reinterpret_cast<uint8_t*>(&buffer[0]), buffer.size());
187 if (ret != 0) {
188 return ErrorAbort(state, kPackageExtractFileFailure,
189 "%s: Failed to extract entry \"%s\" (%zu bytes) to memory: %s", name,
190 zip_path.c_str(), buffer.size(), ErrorCodeString(ret));
191 }
192
Tao Bao511d7592018-06-19 15:56:49 -0700193 return new Value(Value::Type::BLOB, buffer);
Tianjie Xu5419ad32018-02-02 16:49:15 -0800194 }
195}
196
Tao Bao5609bc82018-06-20 00:30:48 -0700197// patch_partition_check(target_partition, source_partition)
198// Checks if the target and source partitions have the desired checksums to be patched. It returns
199// directly, if the target partition already has the expected checksum. Otherwise it in turn
200// checks the integrity of the source partition and the backup file on /cache.
Tianjie Xu5419ad32018-02-02 16:49:15 -0800201//
Tao Bao5609bc82018-06-20 00:30:48 -0700202// For example, patch_partition_check(
203// "EMMC:/dev/block/boot:12342568:8aaacf187a6929d0e9c3e9e46ea7ff495b43424d",
204// "EMMC:/dev/block/boot:12363048:06b0b16299dcefc94900efed01e0763ff644ffa4")
205Value* PatchPartitionCheckFn(const char* name, State* state,
206 const std::vector<std::unique_ptr<Expr>>& argv) {
207 if (argv.size() != 2) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800208 return ErrorAbort(state, kArgsParsingFailure,
Tao Bao5609bc82018-06-20 00:30:48 -0700209 "%s(): Invalid number of args (expected 2, got %zu)", name, argv.size());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800210 }
211
212 std::vector<std::string> args;
Tao Bao5609bc82018-06-20 00:30:48 -0700213 if (!ReadArgs(state, argv, &args, 0, 2)) {
214 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
Tianjie Xu5419ad32018-02-02 16:49:15 -0800215 }
216
Tao Bao5609bc82018-06-20 00:30:48 -0700217 std::string err;
218 auto target = Partition::Parse(args[0], &err);
219 if (!target) {
220 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse target \"%s\": %s", name,
221 args[0].c_str(), err.c_str());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800222 }
223
Tao Bao5609bc82018-06-20 00:30:48 -0700224 auto source = Partition::Parse(args[1], &err);
225 if (!source) {
226 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse source \"%s\": %s", name,
227 args[1].c_str(), err.c_str());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800228 }
229
Tao Bao5609bc82018-06-20 00:30:48 -0700230 bool result = PatchPartitionCheck(target, source);
231 return StringValue(result ? "t" : "");
Tianjie Xu5419ad32018-02-02 16:49:15 -0800232}
233
Tao Bao5609bc82018-06-20 00:30:48 -0700234// patch_partition(target, source, patch)
235// Applies the given patch to the source partition, and writes the result to the target partition.
236//
237// For example, patch_partition(
238// "EMMC:/dev/block/boot:12342568:8aaacf187a6929d0e9c3e9e46ea7ff495b43424d",
239// "EMMC:/dev/block/boot:12363048:06b0b16299dcefc94900efed01e0763ff644ffa4",
240// package_extract_file("boot.img.p"))
241Value* PatchPartitionFn(const char* name, State* state,
242 const std::vector<std::unique_ptr<Expr>>& argv) {
243 if (argv.size() != 3) {
244 return ErrorAbort(state, kArgsParsingFailure,
245 "%s(): Invalid number of args (expected 3, got %zu)", name, argv.size());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800246 }
247
248 std::vector<std::string> args;
Tao Bao5609bc82018-06-20 00:30:48 -0700249 if (!ReadArgs(state, argv, &args, 0, 2)) {
250 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
Tianjie Xu5419ad32018-02-02 16:49:15 -0800251 }
Tianjie Xu5419ad32018-02-02 16:49:15 -0800252
Tao Bao5609bc82018-06-20 00:30:48 -0700253 std::string err;
254 auto target = Partition::Parse(args[0], &err);
255 if (!target) {
256 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse target \"%s\": %s", name,
257 args[0].c_str(), err.c_str());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800258 }
Tianjie Xu5419ad32018-02-02 16:49:15 -0800259
Tao Bao5609bc82018-06-20 00:30:48 -0700260 auto source = Partition::Parse(args[1], &err);
261 if (!source) {
262 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse source \"%s\": %s", name,
263 args[1].c_str(), err.c_str());
264 }
265
266 std::vector<std::unique_ptr<Value>> values;
267 if (!ReadValueArgs(state, argv, &values, 2, 1) || values[0]->type != Value::Type::BLOB) {
268 return ErrorAbort(state, kArgsParsingFailure, "%s(): Invalid patch arg", name);
269 }
270
271 bool result = PatchPartition(target, source, *values[0], nullptr);
272 return StringValue(result ? "t" : "");
Tianjie Xu5419ad32018-02-02 16:49:15 -0800273}
274
Doug Zongker3d177d02010-07-01 09:18:44 -0700275// mount(fs_type, partition_type, location, mount_point)
Tao Bao0831d0b2016-11-03 23:25:04 -0700276// mount(fs_type, partition_type, location, mount_point, mount_options)
Tianjie Xuc4447322017-03-06 14:44:59 -0800277
Doug Zongker3d177d02010-07-01 09:18:44 -0700278// fs_type="ext4" partition_type="EMMC" location=device
Tianjie Xuc4447322017-03-06 14:44:59 -0800279Value* MountFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
280 if (argv.size() != 4 && argv.size() != 5) {
281 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 4-5 args, got %zu", name,
282 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800283 }
284
285 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800286 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800287 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
288 }
289 const std::string& fs_type = args[0];
290 const std::string& partition_type = args[1];
291 const std::string& location = args[2];
292 const std::string& mount_point = args[3];
293 std::string mount_options;
294
Tianjie Xuc4447322017-03-06 14:44:59 -0800295 if (argv.size() == 5) {
Tao Bao039f2da2016-11-22 16:29:50 -0800296 mount_options = args[4];
297 }
298
299 if (fs_type.empty()) {
300 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
301 }
302 if (partition_type.empty()) {
303 return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
304 name);
305 }
306 if (location.empty()) {
307 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
308 }
309 if (mount_point.empty()) {
310 return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
311 name);
312 }
313
314 {
315 char* secontext = nullptr;
316
317 if (sehandle) {
318 selabel_lookup(sehandle, &secontext, mount_point.c_str(), 0755);
319 setfscreatecon(secontext);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700320 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700321
Tao Bao039f2da2016-11-22 16:29:50 -0800322 mkdir(mount_point.c_str(), 0755);
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700323
Tao Bao039f2da2016-11-22 16:29:50 -0800324 if (secontext) {
325 freecon(secontext);
326 setfscreatecon(nullptr);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700327 }
Tao Bao039f2da2016-11-22 16:29:50 -0800328 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700329
Tao Bao039f2da2016-11-22 16:29:50 -0800330 if (mount(location.c_str(), mount_point.c_str(), fs_type.c_str(),
331 MS_NOATIME | MS_NODEV | MS_NODIRATIME, mount_options.c_str()) < 0) {
Tao Bao0bbc7642017-03-29 23:57:47 -0700332 uiPrintf(state, "%s: Failed to mount %s at %s: %s", name, location.c_str(), mount_point.c_str(),
333 strerror(errno));
Tao Bao039f2da2016-11-22 16:29:50 -0800334 return StringValue("");
335 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700336
Tao Bao039f2da2016-11-22 16:29:50 -0800337 return StringValue(mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700338}
339
Doug Zongker8edb00c2009-06-11 17:21:44 -0700340// is_mounted(mount_point)
Tianjie Xuc4447322017-03-06 14:44:59 -0800341Value* IsMountedFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
342 if (argv.size() != 1) {
343 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800344 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700345
Tao Bao039f2da2016-11-22 16:29:50 -0800346 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800347 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800348 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
349 }
350 const std::string& mount_point = args[0];
351 if (mount_point.empty()) {
352 return ErrorAbort(state, kArgsParsingFailure,
353 "mount_point argument to unmount() can't be empty");
354 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700355
Tao Bao039f2da2016-11-22 16:29:50 -0800356 scan_mounted_volumes();
357 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str());
358 if (vol == nullptr) {
359 return StringValue("");
360 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700361
Tao Bao039f2da2016-11-22 16:29:50 -0800362 return StringValue(mount_point);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700363}
364
Tianjie Xuc4447322017-03-06 14:44:59 -0800365Value* UnmountFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
366 if (argv.size() != 1) {
367 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800368 }
369 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800370 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800371 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
372 }
373 const std::string& mount_point = args[0];
374 if (mount_point.empty()) {
375 return ErrorAbort(state, kArgsParsingFailure,
376 "mount_point argument to unmount() can't be empty");
377 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700378
Tao Bao039f2da2016-11-22 16:29:50 -0800379 scan_mounted_volumes();
380 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str());
381 if (vol == nullptr) {
Tao Bao0bbc7642017-03-29 23:57:47 -0700382 uiPrintf(state, "Failed to unmount %s: No such volume", mount_point.c_str());
Tao Bao039f2da2016-11-22 16:29:50 -0800383 return nullptr;
384 } else {
385 int ret = unmount_mounted_volume(vol);
386 if (ret != 0) {
Tao Bao0bbc7642017-03-29 23:57:47 -0700387 uiPrintf(state, "Failed to unmount %s: %s", mount_point.c_str(), strerror(errno));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700388 }
Tao Bao039f2da2016-11-22 16:29:50 -0800389 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700390
Tao Bao039f2da2016-11-22 16:29:50 -0800391 return StringValue(mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700392}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700393
Tao Bao3d69f0d2018-12-20 09:44:06 -0800394static int exec_cmd(const std::vector<std::string>& args) {
395 CHECK(!args.empty());
396 auto argv = StringVectorToNullTerminatedArray(args);
397
Tao Bao039f2da2016-11-22 16:29:50 -0800398 pid_t child;
399 if ((child = vfork()) == 0) {
Tao Bao3d69f0d2018-12-20 09:44:06 -0800400 execv(argv[0], argv.data());
Tao Bao3da88012017-02-03 13:09:23 -0800401 _exit(EXIT_FAILURE);
Tao Bao039f2da2016-11-22 16:29:50 -0800402 }
403
404 int status;
405 waitpid(child, &status, 0);
406 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Tao Bao3d69f0d2018-12-20 09:44:06 -0800407 LOG(ERROR) << args[0] << " failed with status " << WEXITSTATUS(status);
Tao Bao039f2da2016-11-22 16:29:50 -0800408 }
409 return WEXITSTATUS(status);
JP Abgrall37aedb32014-06-16 19:07:39 -0700410}
411
Stephen Smalley779701d2012-02-09 14:13:23 -0500412// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700413//
Tao Bao039f2da2016-11-22 16:29:50 -0800414// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
415// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700416// if fs_size == 0, then make fs uses the entire partition.
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800417// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700418// 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 -0800419Value* FormatFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
420 if (argv.size() != 5) {
421 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 5 args, got %zu", name,
422 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800423 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500424
Tao Bao039f2da2016-11-22 16:29:50 -0800425 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800426 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800427 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
428 }
429 const std::string& fs_type = args[0];
430 const std::string& partition_type = args[1];
431 const std::string& location = args[2];
432 const std::string& fs_size = args[3];
433 const std::string& mount_point = args[4];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700434
Tao Bao039f2da2016-11-22 16:29:50 -0800435 if (fs_type.empty()) {
436 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
437 }
438 if (partition_type.empty()) {
439 return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
440 name);
441 }
442 if (location.empty()) {
443 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
444 }
445 if (mount_point.empty()) {
446 return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
447 name);
448 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700449
Tao Bao039f2da2016-11-22 16:29:50 -0800450 int64_t size;
451 if (!android::base::ParseInt(fs_size, &size)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800452 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800453 fs_size.c_str());
454 }
455
456 if (fs_type == "ext4") {
Tao Bao3d69f0d2018-12-20 09:44:06 -0800457 std::vector<std::string> mke2fs_args = {
458 "/system/bin/mke2fs", "-t", "ext4", "-b", "4096", location
459 };
Jin Qianded2dac2017-04-21 14:36:12 -0700460 if (size != 0) {
Tao Bao3d69f0d2018-12-20 09:44:06 -0800461 mke2fs_args.push_back(std::to_string(size / 4096LL));
Jin Qianded2dac2017-04-21 14:36:12 -0700462 }
463
Tao Bao3d69f0d2018-12-20 09:44:06 -0800464 if (auto status = exec_cmd(mke2fs_args); status != 0) {
Jin Qian502fd1c2017-11-02 11:58:12 -0700465 LOG(ERROR) << name << ": mke2fs failed (" << status << ") on " << location;
466 return StringValue("");
Jin Qianded2dac2017-04-21 14:36:12 -0700467 }
468
Tao Bao3d69f0d2018-12-20 09:44:06 -0800469 if (auto status = exec_cmd({ "/system/bin/e2fsdroid", "-e", "-a", mount_point, location });
470 status != 0) {
Jin Qianded2dac2017-04-21 14:36:12 -0700471 LOG(ERROR) << name << ": e2fsdroid failed (" << status << ") on " << location;
Tao Bao039f2da2016-11-22 16:29:50 -0800472 return StringValue("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700473 }
Tao Bao039f2da2016-11-22 16:29:50 -0800474 return StringValue(location);
Tao Bao3d69f0d2018-12-20 09:44:06 -0800475 }
476
477 if (fs_type == "f2fs") {
Tao Bao039f2da2016-11-22 16:29:50 -0800478 if (size < 0) {
479 LOG(ERROR) << name << ": fs_size can't be negative for f2fs: " << fs_size;
480 return StringValue("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700481 }
Tao Bao3d69f0d2018-12-20 09:44:06 -0800482 std::vector<std::string> f2fs_args = {
Tao Baoc674dfb2018-12-20 14:25:15 -0800483 "/system/bin/make_f2fs", "-g", "android", "-w", "512", location
Tao Bao3d69f0d2018-12-20 09:44:06 -0800484 };
485 if (size >= 512) {
486 f2fs_args.push_back(std::to_string(size / 512));
487 }
488 if (auto status = exec_cmd(f2fs_args); status != 0) {
Tao Baoc674dfb2018-12-20 14:25:15 -0800489 LOG(ERROR) << name << ": make_f2fs failed (" << status << ") on " << location;
Tao Bao039f2da2016-11-22 16:29:50 -0800490 return StringValue("");
491 }
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800492
Tao Baoc674dfb2018-12-20 14:25:15 -0800493 if (auto status = exec_cmd({ "/system/bin/sload_f2fs", "-t", mount_point, location });
494 status != 0) {
495 LOG(ERROR) << name << ": sload_f2fs failed (" << status << ") on " << location;
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800496 return StringValue("");
497 }
498
Tao Bao039f2da2016-11-22 16:29:50 -0800499 return StringValue(location);
Tao Bao039f2da2016-11-22 16:29:50 -0800500 }
501
Tao Bao3d69f0d2018-12-20 09:44:06 -0800502 LOG(ERROR) << name << ": unsupported fs_type \"" << fs_type << "\" partition_type \""
503 << partition_type << "\"";
Tao Bao039f2da2016-11-22 16:29:50 -0800504 return nullptr;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700505}
506
Tianjie Xuc4447322017-03-06 14:44:59 -0800507Value* ShowProgressFn(const char* name, State* state,
508 const std::vector<std::unique_ptr<Expr>>& argv) {
509 if (argv.size() != 2) {
510 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
511 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800512 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700513
Tao Bao039f2da2016-11-22 16:29:50 -0800514 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800515 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800516 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
517 }
518 const std::string& frac_str = args[0];
519 const std::string& sec_str = args[1];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700520
Tao Bao039f2da2016-11-22 16:29:50 -0800521 double frac;
522 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800523 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800524 frac_str.c_str());
525 }
526 int sec;
527 if (!android::base::ParseInt(sec_str.c_str(), &sec)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800528 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800529 sec_str.c_str());
530 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700531
Tao Bao039f2da2016-11-22 16:29:50 -0800532 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
533 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700534
Tao Bao039f2da2016-11-22 16:29:50 -0800535 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700536}
537
Tianjie Xu5419ad32018-02-02 16:49:15 -0800538Value* SetProgressFn(const char* name, State* state,
539 const std::vector<std::unique_ptr<Expr>>& argv) {
Tianjie Xuc4447322017-03-06 14:44:59 -0800540 if (argv.size() != 1) {
541 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800542 }
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700543
Tao Bao039f2da2016-11-22 16:29:50 -0800544 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800545 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800546 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
547 }
548 const std::string& frac_str = args[0];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700549
Tao Bao039f2da2016-11-22 16:29:50 -0800550 double frac;
551 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800552 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800553 frac_str.c_str());
554 }
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700555
Tao Bao039f2da2016-11-22 16:29:50 -0800556 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
557 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700558
Tao Bao039f2da2016-11-22 16:29:50 -0800559 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700560}
561
Tianjie Xuc4447322017-03-06 14:44:59 -0800562Value* GetPropFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
563 if (argv.size() != 1) {
564 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800565 }
566 std::string key;
567 if (!Evaluate(state, argv[0], &key)) {
568 return nullptr;
569 }
570 std::string value = android::base::GetProperty(key, "");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700571
Tao Bao039f2da2016-11-22 16:29:50 -0800572 return StringValue(value);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700573}
574
Doug Zongker47cace92009-06-18 10:11:50 -0700575// file_getprop(file, key)
576//
577// interprets 'file' as a getprop-style file (key=value pairs, one
Tao Bao51d516e2016-11-03 14:49:01 -0700578// per line. # comment lines, blank lines, lines without '=' ignored),
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700579// and returns the value for 'key' (or "" if it isn't defined).
Tianjie Xu5419ad32018-02-02 16:49:15 -0800580Value* FileGetPropFn(const char* name, State* state,
581 const std::vector<std::unique_ptr<Expr>>& argv) {
Tianjie Xuc4447322017-03-06 14:44:59 -0800582 if (argv.size() != 2) {
583 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
584 argv.size());
Tao Bao358c2ec2016-11-28 11:48:43 -0800585 }
586
587 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800588 if (!ReadArgs(state, argv, &args)) {
Tao Bao358c2ec2016-11-28 11:48:43 -0800589 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
590 }
591 const std::string& filename = args[0];
592 const std::string& key = args[1];
593
Tianjie Xu22f11202018-08-27 10:50:31 -0700594 std::string buffer;
595 if (!android::base::ReadFileToString(filename, &buffer)) {
596 ErrorAbort(state, kFreadFailure, "%s: failed to read %s", name, filename.c_str());
Tao Bao358c2ec2016-11-28 11:48:43 -0800597 return nullptr;
598 }
599
Tao Bao358c2ec2016-11-28 11:48:43 -0800600 std::vector<std::string> lines = android::base::Split(buffer, "\n");
601 for (size_t i = 0; i < lines.size(); i++) {
602 std::string line = android::base::Trim(lines[i]);
603
604 // comment or blank line: skip to next line
605 if (line.empty() || line[0] == '#') {
606 continue;
607 }
608 size_t equal_pos = line.find('=');
609 if (equal_pos == std::string::npos) {
610 continue;
Tao Bao51d516e2016-11-03 14:49:01 -0700611 }
612
Tao Bao358c2ec2016-11-28 11:48:43 -0800613 // trim whitespace between key and '='
614 std::string str = android::base::Trim(line.substr(0, equal_pos));
Doug Zongker47cace92009-06-18 10:11:50 -0700615
Tao Bao358c2ec2016-11-28 11:48:43 -0800616 // not the key we're looking for
617 if (key != str) continue;
Doug Zongker47cace92009-06-18 10:11:50 -0700618
Tao Bao358c2ec2016-11-28 11:48:43 -0800619 return StringValue(android::base::Trim(line.substr(equal_pos + 1)));
620 }
Doug Zongker47cace92009-06-18 10:11:50 -0700621
Tao Bao358c2ec2016-11-28 11:48:43 -0800622 return StringValue("");
Doug Zongker47cace92009-06-18 10:11:50 -0700623}
624
Doug Zongker8edb00c2009-06-11 17:21:44 -0700625// apply_patch_space(bytes)
Tianjie Xu5419ad32018-02-02 16:49:15 -0800626Value* ApplyPatchSpaceFn(const char* name, State* state,
627 const std::vector<std::unique_ptr<Expr>>& argv) {
Tianjie Xuc4447322017-03-06 14:44:59 -0800628 if (argv.size() != 1) {
629 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 args, got %zu", name,
630 argv.size());
631 }
Tao Bao039f2da2016-11-22 16:29:50 -0800632 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800633 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800634 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
635 }
636 const std::string& bytes_str = args[0];
Doug Zongkerc4351c72010-02-22 14:46:32 -0800637
Tao Bao039f2da2016-11-22 16:29:50 -0800638 size_t bytes;
639 if (!android::base::ParseUint(bytes_str.c_str(), &bytes)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800640 return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count", name,
641 bytes_str.c_str());
Tao Bao039f2da2016-11-22 16:29:50 -0800642 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800643
Tianjie Xu99b73be2017-11-28 17:23:06 -0800644 // Skip the cache size check if the update is a retry.
Tao Bao5ee25662018-07-11 15:55:32 -0700645 if (state->is_retry || CheckAndFreeSpaceOnCache(bytes)) {
Tianjie Xu99b73be2017-11-28 17:23:06 -0800646 return StringValue("t");
647 }
648 return StringValue("");
Doug Zongkerc4351c72010-02-22 14:46:32 -0800649}
650
Tianjie Xuc4447322017-03-06 14:44:59 -0800651Value* WipeCacheFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
652 if (!argv.empty()) {
653 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %zu", name,
654 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800655 }
656 fprintf(static_cast<UpdaterInfo*>(state->cookie)->cmd_pipe, "wipe_cache\n");
657 return StringValue("t");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700658}
659
Tianjie Xuc4447322017-03-06 14:44:59 -0800660Value* RunProgramFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
661 if (argv.size() < 1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800662 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
663 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700664
Tao Bao039f2da2016-11-22 16:29:50 -0800665 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800666 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800667 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
668 }
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700669
Tao Bao3d69f0d2018-12-20 09:44:06 -0800670 auto exec_args = StringVectorToNullTerminatedArray(args);
671 LOG(INFO) << "about to run program [" << exec_args[0] << "] with " << argv.size() << " args";
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700672
Tao Bao039f2da2016-11-22 16:29:50 -0800673 pid_t child = fork();
674 if (child == 0) {
Tao Bao3d69f0d2018-12-20 09:44:06 -0800675 execv(exec_args[0], exec_args.data());
Tao Bao039f2da2016-11-22 16:29:50 -0800676 PLOG(ERROR) << "run_program: execv failed";
Tao Bao3da88012017-02-03 13:09:23 -0800677 _exit(EXIT_FAILURE);
Tao Bao039f2da2016-11-22 16:29:50 -0800678 }
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700679
Tao Bao039f2da2016-11-22 16:29:50 -0800680 int status;
681 waitpid(child, &status, 0);
682 if (WIFEXITED(status)) {
683 if (WEXITSTATUS(status) != 0) {
684 LOG(ERROR) << "run_program: child exited with status " << WEXITSTATUS(status);
685 }
686 } else if (WIFSIGNALED(status)) {
687 LOG(ERROR) << "run_program: child terminated by signal " << WTERMSIG(status);
688 }
689
690 return StringValue(std::to_string(status));
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700691}
692
Tao Bao511d7592018-06-19 15:56:49 -0700693// read_file(filename)
Tao Baobafd6c72018-07-09 15:08:50 -0700694// Reads a local file 'filename' and returns its contents as a string Value.
Tianjie Xuc4447322017-03-06 14:44:59 -0800695Value* ReadFileFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
696 if (argv.size() != 1) {
697 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800698 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700699
Tao Bao039f2da2016-11-22 16:29:50 -0800700 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800701 if (!ReadArgs(state, argv, &args)) {
Tao Bao511d7592018-06-19 15:56:49 -0700702 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
Tao Bao039f2da2016-11-22 16:29:50 -0800703 }
704 const std::string& filename = args[0];
Doug Zongker512536a2010-02-17 16:11:44 -0800705
Tao Baobafd6c72018-07-09 15:08:50 -0700706 std::string contents;
707 if (android::base::ReadFileToString(filename, &contents)) {
708 return new Value(Value::Type::STRING, std::move(contents));
Tao Bao039f2da2016-11-22 16:29:50 -0800709 }
Tao Bao511d7592018-06-19 15:56:49 -0700710
711 // Leave it to caller to handle the failure.
Tao Baobafd6c72018-07-09 15:08:50 -0700712 PLOG(ERROR) << name << ": Failed to read " << filename;
Tao Bao511d7592018-06-19 15:56:49 -0700713 return StringValue("");
Doug Zongker512536a2010-02-17 16:11:44 -0800714}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700715
Tao Baod0f30882016-11-03 23:52:01 -0700716// write_value(value, filename)
717// Writes 'value' to 'filename'.
718// Example: write_value("960000", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq")
Tianjie Xuc4447322017-03-06 14:44:59 -0800719Value* WriteValueFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
720 if (argv.size() != 2) {
721 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
722 argv.size());
Tao Baod0f30882016-11-03 23:52:01 -0700723 }
724
725 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800726 if (!ReadArgs(state, argv, &args)) {
Tao Baod0f30882016-11-03 23:52:01 -0700727 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
728 }
729
730 const std::string& filename = args[1];
731 if (filename.empty()) {
732 return ErrorAbort(state, kArgsParsingFailure, "%s(): Filename cannot be empty", name);
733 }
734
735 const std::string& value = args[0];
736 if (!android::base::WriteStringToFile(value, filename)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800737 PLOG(ERROR) << name << ": Failed to write to \"" << filename << "\"";
Tao Baod0f30882016-11-03 23:52:01 -0700738 return StringValue("");
739 } else {
740 return StringValue("t");
741 }
742}
743
Doug Zongkerc87bab12013-11-25 13:53:25 -0800744// Immediately reboot the device. Recovery is not finished normally,
745// so if you reboot into recovery it will re-start applying the
746// current package (because nothing has cleared the copy of the
747// arguments stored in the BCB).
748//
749// The argument is the partition name passed to the android reboot
750// property. It can be "recovery" to boot from the recovery
751// partition, or "" (empty string) to boot from the regular boot
752// partition.
Tianjie Xuc4447322017-03-06 14:44:59 -0800753Value* RebootNowFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
754 if (argv.size() != 2) {
755 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
756 argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -0800757 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800758
Tao Baobedf5fc2016-11-18 12:01:26 -0800759 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800760 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800761 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
762 }
763 const std::string& filename = args[0];
764 const std::string& property = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -0800765
Tao Baobedf5fc2016-11-18 12:01:26 -0800766 // Zero out the 'command' field of the bootloader message. Leave the rest intact.
767 bootloader_message boot;
768 std::string err;
769 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800770 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800771 return StringValue("");
772 }
773 memset(boot.command, 0, sizeof(boot.command));
774 if (!write_bootloader_message_to(boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800775 LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800776 return StringValue("");
777 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800778
Tao Bao782dcc12019-04-29 11:23:16 -0700779 Reboot(property);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800780
Tao Baobedf5fc2016-11-18 12:01:26 -0800781 sleep(5);
782 return ErrorAbort(state, kRebootFailure, "%s() failed to reboot", name);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800783}
784
785// Store a string value somewhere that future invocations of recovery
786// can access it. This value is called the "stage" and can be used to
787// drive packages that need to do reboots in the middle of
788// installation and keep track of where they are in the multi-stage
789// install.
790//
791// The first argument is the block device for the misc partition
792// ("/misc" in the fstab), which is where this value is stored. The
793// second argument is the string to store; it should not exceed 31
794// bytes.
Tianjie Xuc4447322017-03-06 14:44:59 -0800795Value* SetStageFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
796 if (argv.size() != 2) {
797 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
798 argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -0800799 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800800
Tao Baobedf5fc2016-11-18 12:01:26 -0800801 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800802 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800803 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
804 }
805 const std::string& filename = args[0];
806 const std::string& stagestr = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -0800807
Tao Baobedf5fc2016-11-18 12:01:26 -0800808 // Store this value in the misc partition, immediately after the
809 // bootloader message that the main recovery uses to save its
810 // arguments in case of the device restarting midway through
811 // package installation.
812 bootloader_message boot;
813 std::string err;
814 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800815 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800816 return StringValue("");
817 }
818 strlcpy(boot.stage, stagestr.c_str(), sizeof(boot.stage));
819 if (!write_bootloader_message_to(boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800820 LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800821 return StringValue("");
822 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800823
Tao Baobedf5fc2016-11-18 12:01:26 -0800824 return StringValue(filename);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800825}
826
827// Return the value most recently saved with SetStageFn. The argument
828// is the block device for the misc partition.
Tianjie Xuc4447322017-03-06 14:44:59 -0800829Value* GetStageFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
830 if (argv.size() != 1) {
831 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -0800832 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800833
Tao Baobedf5fc2016-11-18 12:01:26 -0800834 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800835 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800836 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
837 }
838 const std::string& filename = args[0];
Doug Zongkerc87bab12013-11-25 13:53:25 -0800839
Tao Baobedf5fc2016-11-18 12:01:26 -0800840 bootloader_message boot;
841 std::string err;
842 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800843 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800844 return StringValue("");
845 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800846
Tao Baobedf5fc2016-11-18 12:01:26 -0800847 return StringValue(boot.stage);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800848}
849
Tianjie Xuc4447322017-03-06 14:44:59 -0800850Value* WipeBlockDeviceFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
851 if (argv.size() != 2) {
852 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
853 argv.size());
Tao Bao358c2ec2016-11-28 11:48:43 -0800854 }
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800855
Tao Bao358c2ec2016-11-28 11:48:43 -0800856 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800857 if (!ReadArgs(state, argv, &args)) {
Tao Bao358c2ec2016-11-28 11:48:43 -0800858 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
859 }
860 const std::string& filename = args[0];
861 const std::string& len_str = args[1];
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800862
Tao Bao358c2ec2016-11-28 11:48:43 -0800863 size_t len;
864 if (!android::base::ParseUint(len_str.c_str(), &len)) {
865 return nullptr;
866 }
Tianjie Xu22f11202018-08-27 10:50:31 -0700867 android::base::unique_fd fd(open(filename.c_str(), O_WRONLY));
868 if (fd == -1) {
869 PLOG(ERROR) << "Failed to open " << filename;
870 return StringValue("");
871 }
872
Tao Bao358c2ec2016-11-28 11:48:43 -0800873 // The wipe_block_device function in ext4_utils returns 0 on success and 1
874 // for failure.
875 int status = wipe_block_device(fd, len);
876 return StringValue((status == 0) ? "t" : "");
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800877}
878
Tianjie Xuc4447322017-03-06 14:44:59 -0800879Value* EnableRebootFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
880 if (!argv.empty()) {
881 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %zu", name,
882 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800883 }
884 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
885 fprintf(ui->cmd_pipe, "enable_reboot\n");
886 return StringValue("t");
Doug Zongkerc704e062014-05-23 08:40:35 -0700887}
888
Tianjie Xuc4447322017-03-06 14:44:59 -0800889Value* Tune2FsFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
890 if (argv.empty()) {
891 return ErrorAbort(state, kArgsParsingFailure, "%s() expects args, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800892 }
Michael Rungeacf47db2014-11-21 00:12:28 -0800893
Tao Bao039f2da2016-11-22 16:29:50 -0800894 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800895 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800896 return ErrorAbort(state, kArgsParsingFailure, "%s() could not read args", name);
897 }
Michael Rungeacf47db2014-11-21 00:12:28 -0800898
Tao Bao3d69f0d2018-12-20 09:44:06 -0800899 // tune2fs expects the program name as its first arg.
900 args.insert(args.begin(), "tune2fs");
901 auto tune2fs_args = StringVectorToNullTerminatedArray(args);
Michael Rungeacf47db2014-11-21 00:12:28 -0800902
Tao Bao3d69f0d2018-12-20 09:44:06 -0800903 // tune2fs changes the filesystem parameters on an ext2 filesystem; it returns 0 on success.
904 if (auto result = tune2fs_main(tune2fs_args.size() - 1, tune2fs_args.data()); result != 0) {
Tao Bao039f2da2016-11-22 16:29:50 -0800905 return ErrorAbort(state, kTune2FsFailure, "%s() returned error code %d", name, result);
906 }
907 return StringValue("t");
Michael Rungeacf47db2014-11-21 00:12:28 -0800908}
909
Doug Zongker9931f7f2009-06-10 14:11:53 -0700910void RegisterInstallFunctions() {
Tao Bao039f2da2016-11-22 16:29:50 -0800911 RegisterFunction("mount", MountFn);
912 RegisterFunction("is_mounted", IsMountedFn);
913 RegisterFunction("unmount", UnmountFn);
914 RegisterFunction("format", FormatFn);
915 RegisterFunction("show_progress", ShowProgressFn);
916 RegisterFunction("set_progress", SetProgressFn);
Tao Bao039f2da2016-11-22 16:29:50 -0800917 RegisterFunction("package_extract_file", PackageExtractFileFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700918
Tao Bao039f2da2016-11-22 16:29:50 -0800919 RegisterFunction("getprop", GetPropFn);
920 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700921
Tao Bao039f2da2016-11-22 16:29:50 -0800922 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Tao Bao5609bc82018-06-20 00:30:48 -0700923 RegisterFunction("patch_partition", PatchPartitionFn);
924 RegisterFunction("patch_partition_check", PatchPartitionCheckFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700925
Tao Bao039f2da2016-11-22 16:29:50 -0800926 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -0800927
Tao Bao039f2da2016-11-22 16:29:50 -0800928 RegisterFunction("read_file", ReadFileFn);
Tao Bao039f2da2016-11-22 16:29:50 -0800929 RegisterFunction("write_value", WriteValueFn);
Doug Zongker512536a2010-02-17 16:11:44 -0800930
Tao Bao039f2da2016-11-22 16:29:50 -0800931 RegisterFunction("wipe_cache", WipeCacheFn);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700932
Tao Bao039f2da2016-11-22 16:29:50 -0800933 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700934
Tao Bao039f2da2016-11-22 16:29:50 -0800935 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800936
Tao Bao039f2da2016-11-22 16:29:50 -0800937 RegisterFunction("reboot_now", RebootNowFn);
938 RegisterFunction("get_stage", GetStageFn);
939 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -0700940
Tao Bao039f2da2016-11-22 16:29:50 -0800941 RegisterFunction("enable_reboot", EnableRebootFn);
942 RegisterFunction("tune2fs", Tune2FsFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700943}