blob: 741d9701412d4b718030c73b8ccf910d1979024f [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 Bao1bf17722016-11-03 23:52:01 -070039#include <string>
Yabin Cui64be2132016-02-03 18:16:02 -080040#include <vector>
41
Tao Bao1bf17722016-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>
Elliott Hughes4b166f02015-12-04 15:30:20 -080045#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>
Tao Bao0d3f84f2016-12-28 15:09:20 -080049#include <applypatch/applypatch.h>
50#include <bootloader_message/bootloader_message.h>
Tao Bao0c7839a2016-10-10 15:48:37 -070051#include <cutils/android_reboot.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"
Elliott Hughes63a31922016-06-09 17:41:22 -070060#include "mounts.h"
Ethan Yonker8373cfe2017-09-08 06:50:54 -050061
Doug Zongker512536a2010-02-17 16:11:44 -080062#include "applypatch/applypatch.h"
Dees_Troy512376c2013-09-03 19:39:41 +000063#include "flashutils/flashutils.h"
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -080064#include "install.h"
Ethan Yonker7e1b9862015-03-19 14:10:01 -050065#ifdef HAVE_LIBTUNE2FS
Michael Rungeb278c252014-11-21 00:12:28 -080066#include "tune2fs.h"
Ethan Yonker7e1b9862015-03-19 14:10:01 -050067#endif
Doug Zongker8edb00c2009-06-11 17:21:44 -070068
Doug Zongker3d177d02010-07-01 09:18:44 -070069#ifdef USE_EXT4
70#include "make_ext4fs.h"
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -080071#include "wipe.h"
Doug Zongker3d177d02010-07-01 09:18:44 -070072#endif
73
Tom Marshall981118e2017-10-25 20:27:08 +020074#include "otautil/ZipUtil.h"
Tao Baod33b2f82017-09-28 21:29:11 -070075#include "otafault/ota_io.h"
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070076#include "otautil/DirUtil.h"
Tao Bao1fc5bf32017-10-06 07:43:41 -070077#include "otautil/error_code.h"
Tao Bao09e468f2017-09-29 14:39:33 -070078#include "otautil/print_sha1.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070079#include "updater/updater.h"
Doug Zongker52b40362014-02-10 15:30:30 -080080
Tao Bao1107d962015-09-09 17:16:55 -070081// Send over the buffer to recovery though the command pipe.
82static void uiPrint(State* state, const std::string& buffer) {
Tao Bao039f2da2016-11-22 16:29:50 -080083 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
Tao Baob6918c72015-05-19 17:02:16 -070084
Tao Bao039f2da2016-11-22 16:29:50 -080085 // "line1\nline2\n" will be split into 3 tokens: "line1", "line2" and "".
86 // So skip sending empty strings to UI.
87 std::vector<std::string> lines = android::base::Split(buffer, "\n");
88 for (auto& line : lines) {
89 if (!line.empty()) {
90 fprintf(ui->cmd_pipe, "ui_print %s\n", line.c_str());
Michael Runge75480252014-10-22 19:48:41 -070091 }
Tao Bao039f2da2016-11-22 16:29:50 -080092 }
Tao Bao1107d962015-09-09 17:16:55 -070093
Tao Bao039f2da2016-11-22 16:29:50 -080094 // On the updater side, we need to dump the contents to stderr (which has
95 // been redirected to the log file). Because the recovery will only print
96 // the contents to screen when processing pipe command ui_print.
97 LOG(INFO) << buffer;
Michael Runge75480252014-10-22 19:48:41 -070098}
99
Elliott Hughes83ce7552016-06-30 09:28:42 -0700100void uiPrintf(State* _Nonnull state, const char* _Nonnull format, ...) {
Tao Bao039f2da2016-11-22 16:29:50 -0800101 std::string error_msg;
Tao Bao1107d962015-09-09 17:16:55 -0700102
Tao Bao039f2da2016-11-22 16:29:50 -0800103 va_list ap;
104 va_start(ap, format);
105 android::base::StringAppendV(&error_msg, format, ap);
106 va_end(ap);
Tao Bao1107d962015-09-09 17:16:55 -0700107
Tao Bao039f2da2016-11-22 16:29:50 -0800108 uiPrint(state, error_msg);
Michael Runge75480252014-10-22 19:48:41 -0700109}
110
Tianjie Xu5419ad32018-02-02 16:49:15 -0800111// This is the updater side handler for ui_print() in edify script. Contents will be sent over to
112// the recovery side for on-screen display.
113Value* UIPrintFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
114 std::vector<std::string> args;
115 if (!ReadArgs(state, argv, &args)) {
116 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
117 }
118
119 std::string buffer = android::base::Join(args, "");
120 uiPrint(state, buffer);
121 return StringValue(buffer);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700122}
123
Tianjie Xu5419ad32018-02-02 16:49:15 -0800124// package_extract_file(package_file[, dest_file])
125// Extracts a single package_file from the update package and writes it to dest_file,
126// overwriting existing files if necessary. Without the dest_file argument, returns the
127// contents of the package file as a binary blob.
128Value* PackageExtractFileFn(const char* name, State* state,
129 const std::vector<std::unique_ptr<Expr>>& argv) {
130 if (argv.size() < 1 || argv.size() > 2) {
131 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 or 2 args, got %zu", name,
132 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800133 }
Tianjie Xu5419ad32018-02-02 16:49:15 -0800134
135 if (argv.size() == 2) {
136 // The two-argument version extracts to a file.
137
138 std::vector<std::string> args;
139 if (!ReadArgs(state, argv, &args)) {
140 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name,
141 argv.size());
142 }
143 const std::string& zip_path = args[0];
144 const std::string& dest_path = args[1];
145
146 ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip;
147 ZipString zip_string_path(zip_path.c_str());
148 ZipEntry entry;
149 if (FindEntry(za, zip_string_path, &entry) != 0) {
150 LOG(ERROR) << name << ": no " << zip_path << " in package";
151 return StringValue("");
152 }
153
154 unique_fd fd(TEMP_FAILURE_RETRY(
155 ota_open(dest_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)));
156 if (fd == -1) {
157 PLOG(ERROR) << name << ": can't open " << dest_path << " for write";
158 return StringValue("");
159 }
160
161 bool success = true;
162 int32_t ret = ExtractEntryToFile(za, &entry, fd);
163 if (ret != 0) {
164 LOG(ERROR) << name << ": Failed to extract entry \"" << zip_path << "\" ("
165 << entry.uncompressed_length << " bytes) to \"" << dest_path
166 << "\": " << ErrorCodeString(ret);
167 success = false;
168 }
169 if (ota_fsync(fd) == -1) {
170 PLOG(ERROR) << "fsync of \"" << dest_path << "\" failed";
171 success = false;
172 }
173 if (ota_close(fd) == -1) {
174 PLOG(ERROR) << "close of \"" << dest_path << "\" failed";
175 success = false;
176 }
177
178 return StringValue(success ? "t" : "");
179 } else {
180 // The one-argument version returns the contents of the file as the result.
181
182 std::vector<std::string> args;
183 if (!ReadArgs(state, argv, &args)) {
184 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name,
185 argv.size());
186 }
187 const std::string& zip_path = args[0];
188
189 ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip;
190 ZipString zip_string_path(zip_path.c_str());
191 ZipEntry entry;
192 if (FindEntry(za, zip_string_path, &entry) != 0) {
193 return ErrorAbort(state, kPackageExtractFileFailure, "%s(): no %s in package", name,
194 zip_path.c_str());
195 }
196
197 std::string buffer;
198 buffer.resize(entry.uncompressed_length);
199
200 int32_t ret =
201 ExtractToMemory(za, &entry, reinterpret_cast<uint8_t*>(&buffer[0]), buffer.size());
202 if (ret != 0) {
203 return ErrorAbort(state, kPackageExtractFileFailure,
204 "%s: Failed to extract entry \"%s\" (%zu bytes) to memory: %s", name,
205 zip_path.c_str(), buffer.size(), ErrorCodeString(ret));
206 }
207
208 return new Value(VAL_BLOB, buffer);
209 }
210}
211
212// apply_patch(src_file, tgt_file, tgt_sha1, tgt_size, patch1_sha1, patch1_blob, [...])
213// Applies a binary patch to the src_file to produce the tgt_file. If the desired target is the
214// same as the source, pass "-" for tgt_file. tgt_sha1 and tgt_size are the expected final SHA1
215// hash and size of the target file. The remaining arguments must come in pairs: a SHA1 hash (a
216// 40-character hex string) and a blob. The blob is the patch to be applied when the source
217// file's current contents have the given SHA1.
218//
219// The patching is done in a safe manner that guarantees the target file either has the desired
220// SHA1 hash and size, or it is untouched -- it will not be left in an unrecoverable intermediate
221// state. If the process is interrupted during patching, the target file may be in an intermediate
222// state; a copy exists in the cache partition so restarting the update can successfully update
223// the file.
224Value* ApplyPatchFn(const char* name, State* state,
225 const std::vector<std::unique_ptr<Expr>>& argv) {
226 if (argv.size() < 6 || (argv.size() % 2) == 1) {
227 return ErrorAbort(state, kArgsParsingFailure,
228 "%s(): expected at least 6 args and an "
229 "even number, got %zu",
230 name, argv.size());
231 }
232
233 std::vector<std::string> args;
234 if (!ReadArgs(state, argv, &args, 0, 4)) {
235 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
236 }
237 const std::string& source_filename = args[0];
238 const std::string& target_filename = args[1];
239 const std::string& target_sha1 = args[2];
240 const std::string& target_size_str = args[3];
241
242 size_t target_size;
243 if (!android::base::ParseUint(target_size_str.c_str(), &target_size)) {
244 return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count", name,
245 target_size_str.c_str());
246 }
247
248 int patchcount = (argv.size() - 4) / 2;
249 std::vector<std::unique_ptr<Value>> arg_values;
250 if (!ReadValueArgs(state, argv, &arg_values, 4, argv.size() - 4)) {
251 return nullptr;
252 }
253
254 for (int i = 0; i < patchcount; ++i) {
255 if (arg_values[i * 2]->type != VAL_STRING) {
256 return ErrorAbort(state, kArgsParsingFailure, "%s(): sha-1 #%d is not string", name, i * 2);
257 }
258 if (arg_values[i * 2 + 1]->type != VAL_BLOB) {
259 return ErrorAbort(state, kArgsParsingFailure, "%s(): patch #%d is not blob", name, i * 2 + 1);
260 }
261 }
262
263 std::vector<std::string> patch_sha_str;
264 std::vector<std::unique_ptr<Value>> patches;
265 for (int i = 0; i < patchcount; ++i) {
266 patch_sha_str.push_back(arg_values[i * 2]->data);
267 patches.push_back(std::move(arg_values[i * 2 + 1]));
268 }
269
270 int result = applypatch(source_filename.c_str(), target_filename.c_str(), target_sha1.c_str(),
271 target_size, patch_sha_str, patches, nullptr);
272
273 return StringValue(result == 0 ? "t" : "");
274}
275
276// apply_patch_check(filename, [sha1, ...])
277// Returns true if the contents of filename or the temporary copy in the cache partition (if
278// present) have a SHA-1 checksum equal to one of the given sha1 values. sha1 values are
279// specified as 40 hex digits. This function differs from sha1_check(read_file(filename),
280// sha1 [, ...]) in that it knows to check the cache partition copy, so apply_patch_check() will
281// succeed even if the file was corrupted by an interrupted apply_patch() update.
282Value* ApplyPatchCheckFn(const char* name, State* state,
283 const std::vector<std::unique_ptr<Expr>>& argv) {
284 if (argv.size() < 1) {
285 return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 1 arg, got %zu", name,
286 argv.size());
287 }
288
289 std::vector<std::string> args;
290 if (!ReadArgs(state, argv, &args, 0, 1)) {
291 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
292 }
293 const std::string& filename = args[0];
294
295 std::vector<std::string> sha1s;
296 if (argv.size() > 1 && !ReadArgs(state, argv, &sha1s, 1, argv.size() - 1)) {
297 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
298 }
299 int result = applypatch_check(filename.c_str(), sha1s);
300
301 return StringValue(result == 0 ? "t" : "");
302}
303
304// sha1_check(data)
305// to return the sha1 of the data (given in the format returned by
306// read_file).
307//
308// sha1_check(data, sha1_hex, [sha1_hex, ...])
309// returns the sha1 of the file if it matches any of the hex
310// strings passed, or "" if it does not equal any of them.
311//
312Value* Sha1CheckFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
313 if (argv.size() < 1) {
314 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
315 }
316
317 std::vector<std::unique_ptr<Value>> args;
318 if (!ReadValueArgs(state, argv, &args)) {
319 return nullptr;
320 }
321
322 if (args[0]->type == VAL_INVALID) {
323 return StringValue("");
324 }
325 uint8_t digest[SHA_DIGEST_LENGTH];
326 SHA1(reinterpret_cast<const uint8_t*>(args[0]->data.c_str()), args[0]->data.size(), digest);
327
328 if (argv.size() == 1) {
329 return StringValue(print_sha1(digest));
330 }
331
332 for (size_t i = 1; i < argv.size(); ++i) {
333 uint8_t arg_digest[SHA_DIGEST_LENGTH];
334 if (args[i]->type != VAL_STRING) {
335 LOG(ERROR) << name << "(): arg " << i << " is not a string; skipping";
336 } else if (ParseSha1(args[i]->data.c_str(), arg_digest) != 0) {
337 // Warn about bad args and skip them.
338 LOG(ERROR) << name << "(): error parsing \"" << args[i]->data << "\" as sha-1; skipping";
339 } else if (memcmp(digest, arg_digest, SHA_DIGEST_LENGTH) == 0) {
340 // Found a match.
341 return args[i].release();
342 }
343 }
344
345 // Didn't match any of the hex strings; return false.
346 return StringValue("");
Doug Zongkera23075f2012-08-06 16:19:09 -0700347}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700348
Doug Zongker9931f7f2009-06-10 14:11:53 -0700349// mount(fs_type, partition_type, location, mount_point)
Tao Bao0831d0b2016-11-03 23:25:04 -0700350// mount(fs_type, partition_type, location, mount_point, mount_options)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700351
Doug Zongker9931f7f2009-06-10 14:11:53 -0700352// fs_type="ext4" partition_type="EMMC" location=device
Tianjie Xuc4447322017-03-06 14:44:59 -0800353Value* MountFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
354 if (argv.size() != 4 && argv.size() != 5) {
355 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 4-5 args, got %zu", name,
356 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800357 }
358
359 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800360 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800361 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
362 }
363 const std::string& fs_type = args[0];
364 const std::string& partition_type = args[1];
365 const std::string& location = args[2];
366 const std::string& mount_point = args[3];
367 std::string mount_options;
368
Tianjie Xuc4447322017-03-06 14:44:59 -0800369 if (argv.size() == 5) {
Tao Bao039f2da2016-11-22 16:29:50 -0800370 mount_options = args[4];
371 }
372
373 if (fs_type.empty()) {
374 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
375 }
376 if (partition_type.empty()) {
377 return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
378 name);
379 }
380 if (location.empty()) {
381 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
382 }
383 if (mount_point.empty()) {
384 return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
385 name);
386 }
387
388 {
389 char* secontext = nullptr;
390
391 if (sehandle) {
392 selabel_lookup(sehandle, &secontext, mount_point.c_str(), 0755);
393 setfscreatecon(secontext);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700394 }
395
Tao Bao039f2da2016-11-22 16:29:50 -0800396 mkdir(mount_point.c_str(), 0755);
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700397
Tao Bao039f2da2016-11-22 16:29:50 -0800398 if (secontext) {
399 freecon(secontext);
400 setfscreatecon(nullptr);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700401 }
Tao Bao039f2da2016-11-22 16:29:50 -0800402 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700403
Tao Bao039f2da2016-11-22 16:29:50 -0800404 if (mount(location.c_str(), mount_point.c_str(), fs_type.c_str(),
405 MS_NOATIME | MS_NODEV | MS_NODIRATIME, mount_options.c_str()) < 0) {
Tao Bao0bbc7642017-03-29 23:57:47 -0700406 uiPrintf(state, "%s: Failed to mount %s at %s: %s", name, location.c_str(), mount_point.c_str(),
407 strerror(errno));
Tao Bao039f2da2016-11-22 16:29:50 -0800408 return StringValue("");
409 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700410
Tao Bao039f2da2016-11-22 16:29:50 -0800411 return StringValue(mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700412}
413
Doug Zongker9931f7f2009-06-10 14:11:53 -0700414// is_mounted(mount_point)
Tianjie Xuc4447322017-03-06 14:44:59 -0800415Value* IsMountedFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
416 if (argv.size() != 1) {
417 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800418 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700419
Tao Bao039f2da2016-11-22 16:29:50 -0800420 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800421 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800422 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
423 }
424 const std::string& mount_point = args[0];
425 if (mount_point.empty()) {
426 return ErrorAbort(state, kArgsParsingFailure,
427 "mount_point argument to unmount() can't be empty");
428 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700429
Tao Bao039f2da2016-11-22 16:29:50 -0800430 scan_mounted_volumes();
431 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str());
432 if (vol == nullptr) {
433 return StringValue("");
434 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700435
Tao Bao039f2da2016-11-22 16:29:50 -0800436 return StringValue(mount_point);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700437}
438
Tianjie Xuc4447322017-03-06 14:44:59 -0800439Value* UnmountFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
440 if (argv.size() != 1) {
441 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800442 }
443 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800444 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800445 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
446 }
447 const std::string& mount_point = args[0];
448 if (mount_point.empty()) {
449 return ErrorAbort(state, kArgsParsingFailure,
450 "mount_point argument to unmount() can't be empty");
451 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700452
Tao Bao039f2da2016-11-22 16:29:50 -0800453 scan_mounted_volumes();
454 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str());
455 if (vol == nullptr) {
Tao Bao0bbc7642017-03-29 23:57:47 -0700456 uiPrintf(state, "Failed to unmount %s: No such volume", mount_point.c_str());
Tao Bao039f2da2016-11-22 16:29:50 -0800457 return nullptr;
458 } else {
459 int ret = unmount_mounted_volume(vol);
460 if (ret != 0) {
Tao Bao0bbc7642017-03-29 23:57:47 -0700461 uiPrintf(state, "Failed to unmount %s: %s", mount_point.c_str(), strerror(errno));
Nick Kralevich6a821fe2014-10-23 20:36:42 -0700462 }
Tao Bao039f2da2016-11-22 16:29:50 -0800463 }
Nick Kralevich6a821fe2014-10-23 20:36:42 -0700464
Tao Bao039f2da2016-11-22 16:29:50 -0800465 return StringValue(mount_point);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700466}
467
JP Abgrall37aedb32014-06-16 19:07:39 -0700468static int exec_cmd(const char* path, char* const argv[]) {
Tao Bao039f2da2016-11-22 16:29:50 -0800469 pid_t child;
470 if ((child = vfork()) == 0) {
471 execv(path, argv);
Tao Bao3da88012017-02-03 13:09:23 -0800472 _exit(EXIT_FAILURE);
Tao Bao039f2da2016-11-22 16:29:50 -0800473 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700474
Tao Bao039f2da2016-11-22 16:29:50 -0800475 int status;
476 waitpid(child, &status, 0);
477 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
478 LOG(ERROR) << path << " failed with status " << WEXITSTATUS(status);
479 }
480 return WEXITSTATUS(status);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700481}
482
Doug Zongker9931f7f2009-06-10 14:11:53 -0700483// format(fs_type, partition_type, location, fs_size, mount_point)
484//
Tao Bao039f2da2016-11-22 16:29:50 -0800485// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
486// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700487// if fs_size == 0, then make fs uses the entire partition.
Doug Zongker9931f7f2009-06-10 14:11:53 -0700488// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700489// 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 -0800490Value* FormatFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
491 if (argv.size() != 5) {
492 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 5 args, got %zu", name,
493 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800494 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700495
Tao Bao039f2da2016-11-22 16:29:50 -0800496 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800497 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800498 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
499 }
500 const std::string& fs_type = args[0];
501 const std::string& partition_type = args[1];
502 const std::string& location = args[2];
503 const std::string& fs_size = args[3];
504 const std::string& mount_point = args[4];
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700505
Tao Bao039f2da2016-11-22 16:29:50 -0800506 if (fs_type.empty()) {
507 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
508 }
509 if (partition_type.empty()) {
510 return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
511 name);
512 }
513 if (location.empty()) {
514 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
515 }
516 if (mount_point.empty()) {
517 return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
518 name);
519 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700520
Tao Bao039f2da2016-11-22 16:29:50 -0800521 int64_t size;
522 if (!android::base::ParseInt(fs_size, &size)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800523 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800524 fs_size.c_str());
525 }
526
527 if (fs_type == "ext4") {
Jin Qianac318082017-04-21 14:36:12 -0700528 const char* mke2fs_argv[] = { "/sbin/mke2fs_static", "-t", "ext4", "-b", "4096",
529 location.c_str(), nullptr, nullptr };
530 std::string size_str;
531 if (size != 0) {
532 size_str = std::to_string(size / 4096LL);
533 mke2fs_argv[6] = size_str.c_str();
534 }
535
536 int status = exec_cmd(mke2fs_argv[0], const_cast<char**>(mke2fs_argv));
Tao Bao039f2da2016-11-22 16:29:50 -0800537 if (status != 0) {
Jin Qian502fd1c2017-11-02 11:58:12 -0700538 LOG(ERROR) << name << ": mke2fs failed (" << status << ") on " << location;
539 return StringValue("");
Jin Qianac318082017-04-21 14:36:12 -0700540 }
541
Tao Bao7af933b2017-07-11 16:45:04 -0700542 const char* e2fsdroid_argv[] = { "/sbin/e2fsdroid_static", "-e", "-a", mount_point.c_str(),
Jin Qianac318082017-04-21 14:36:12 -0700543 location.c_str(), nullptr };
544 status = exec_cmd(e2fsdroid_argv[0], const_cast<char**>(e2fsdroid_argv));
545 if (status != 0) {
546 LOG(ERROR) << name << ": e2fsdroid failed (" << status << ") on " << location;
Tao Bao039f2da2016-11-22 16:29:50 -0800547 return StringValue("");
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700548 }
Tao Bao039f2da2016-11-22 16:29:50 -0800549 return StringValue(location);
550 } else if (fs_type == "f2fs") {
551 if (size < 0) {
552 LOG(ERROR) << name << ": fs_size can't be negative for f2fs: " << fs_size;
553 return StringValue("");
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700554 }
Tao Bao039f2da2016-11-22 16:29:50 -0800555 std::string num_sectors = std::to_string(size / 512);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700556
Tao Bao039f2da2016-11-22 16:29:50 -0800557 const char* f2fs_path = "/sbin/mkfs.f2fs";
Jaegeuk Kim1a8bb0f2017-11-01 13:24:43 -0700558 const char* f2fs_argv[] = { "mkfs.f2fs",
559 "-d1",
560 "-f",
Jaegeuk Kim2e5dc842018-04-05 22:42:13 -0700561 "-O", "encrypt",
562 "-O", "quota",
563 "-O", "verity",
564 "-w", "512",
Jaegeuk Kim1a8bb0f2017-11-01 13:24:43 -0700565 location.c_str(),
566 (size < 512) ? nullptr : num_sectors.c_str(),
567 nullptr };
Tao Bao397a8132017-05-12 11:02:27 -0700568 int status = exec_cmd(f2fs_path, const_cast<char**>(f2fs_argv));
Tao Bao039f2da2016-11-22 16:29:50 -0800569 if (status != 0) {
570 LOG(ERROR) << name << ": mkfs.f2fs failed (" << status << ") on " << location;
571 return StringValue("");
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700572 }
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800573
574 const char* sload_argv[] = { "/sbin/sload.f2fs", "-t", mount_point.c_str(), location.c_str(),
575 nullptr };
576 status = exec_cmd(sload_argv[0], const_cast<char**>(sload_argv));
577 if (status != 0) {
578 LOG(ERROR) << name << ": sload.f2fs failed (" << status << ") on " << location;
579 return StringValue("");
580 }
581
Tao Bao039f2da2016-11-22 16:29:50 -0800582 return StringValue(location);
583 } else {
584 LOG(ERROR) << name << ": unsupported fs_type \"" << fs_type << "\" partition_type \""
585 << partition_type << "\"";
586 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700587
Tao Bao039f2da2016-11-22 16:29:50 -0800588 return nullptr;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700589}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700590
Tom Marshallbf4f24f2017-08-23 18:14:00 +0000591// rename(src_name, dst_name)
592// Renames src_name to dst_name. It automatically creates the necessary directories for dst_name.
593// Example: rename("system/app/Hangouts/Hangouts.apk", "system/priv-app/Hangouts/Hangouts.apk")
594Value* RenameFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
595 if (argv.size() != 2) {
596 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
597 argv.size());
598 }
599
600 std::vector<std::string> args;
601 if (!ReadArgs(state, argv, &args)) {
602 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
603 }
604 const std::string& src_name = args[0];
605 const std::string& dst_name = args[1];
606
607 if (src_name.empty()) {
608 return ErrorAbort(state, kArgsParsingFailure, "src_name argument to %s() can't be empty", name);
609 }
610 if (dst_name.empty()) {
611 return ErrorAbort(state, kArgsParsingFailure, "dst_name argument to %s() can't be empty", name);
612 }
613 if (!make_parents(dst_name)) {
614 return ErrorAbort(state, kFileRenameFailure, "Creating parent of %s failed, error %s",
615 dst_name.c_str(), strerror(errno));
616 } else if (access(dst_name.c_str(), F_OK) == 0 && access(src_name.c_str(), F_OK) != 0) {
617 // File was already moved
618 return StringValue(dst_name);
619 } else if (rename(src_name.c_str(), dst_name.c_str()) != 0) {
620 return ErrorAbort(state, kFileRenameFailure, "Rename of %s to %s failed, error %s",
621 src_name.c_str(), dst_name.c_str(), strerror(errno));
622 }
623
624 return StringValue(dst_name);
625}
626
627// delete([filename, ...])
628// Deletes all the filenames listed. Returns the number of files successfully deleted.
629//
630// delete_recursive([dirname, ...])
631// Recursively deletes dirnames and all their contents. Returns the number of directories
632// successfully deleted.
633Value* DeleteFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
634 std::vector<std::string> paths;
635 if (!ReadArgs(state, argv, &paths)) {
636 return nullptr;
637 }
638
639 bool recursive = (strcmp(name, "delete_recursive") == 0);
640
641 int success = 0;
642 for (const auto& path : paths) {
643 if ((recursive ? dirUnlinkHierarchy(path.c_str()) : unlink(path.c_str())) == 0) {
644 ++success;
645 }
646 }
647
648 return StringValue(std::to_string(success));
649}
650
651
Tianjie Xuc4447322017-03-06 14:44:59 -0800652Value* ShowProgressFn(const char* name, State* state,
653 const std::vector<std::unique_ptr<Expr>>& argv) {
654 if (argv.size() != 2) {
655 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
656 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800657 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700658
Tao Bao039f2da2016-11-22 16:29:50 -0800659 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800660 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800661 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
662 }
663 const std::string& frac_str = args[0];
664 const std::string& sec_str = args[1];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700665
Tao Bao039f2da2016-11-22 16:29:50 -0800666 double frac;
667 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800668 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800669 frac_str.c_str());
670 }
671 int sec;
672 if (!android::base::ParseInt(sec_str.c_str(), &sec)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800673 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800674 sec_str.c_str());
675 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700676
Tao Bao039f2da2016-11-22 16:29:50 -0800677 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
678 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700679
Tao Bao039f2da2016-11-22 16:29:50 -0800680 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700681}
682
Tianjie Xu5419ad32018-02-02 16:49:15 -0800683Value* SetProgressFn(const char* name, State* state,
684 const std::vector<std::unique_ptr<Expr>>& argv) {
Tianjie Xuc4447322017-03-06 14:44:59 -0800685 if (argv.size() != 1) {
686 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800687 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700688
Tao Bao039f2da2016-11-22 16:29:50 -0800689 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800690 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800691 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
692 }
693 const std::string& frac_str = args[0];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700694
Tao Bao039f2da2016-11-22 16:29:50 -0800695 double frac;
696 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800697 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800698 frac_str.c_str());
699 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700700
Tao Bao039f2da2016-11-22 16:29:50 -0800701 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
702 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700703
Tao Bao039f2da2016-11-22 16:29:50 -0800704 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700705}
706
Tom Marshall981118e2017-10-25 20:27:08 +0200707// package_extract_dir(package_dir, dest_dir)
708// Extracts all files from the package underneath package_dir and writes them to the
709// corresponding tree beneath dest_dir. Any existing files are overwritten.
710// Example: package_extract_dir("system", "/system")
711//
712// Note: package_dir needs to be a relative path; dest_dir needs to be an absolute path.
713Value* PackageExtractDirFn(const char* name, State* state,
714 const std::vector<std::unique_ptr<Expr>>&argv) {
715 if (argv.size() != 2) {
716 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
717 argv.size());
718 }
719
720 std::vector<std::string> args;
721 if (!ReadArgs(state, argv, &args)) {
722 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
723 }
724 const std::string& zip_path = args[0];
725 const std::string& dest_path = args[1];
726
727 ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip;
728
729 // To create a consistent system image, never use the clock for timestamps.
730 constexpr struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
731
732 bool success = ExtractPackageRecursive(za, zip_path, dest_path, &timestamp, sehandle);
733
734 return StringValue(success ? "t" : "");
735}
736
Tao Baoef0eb3b2016-11-14 21:29:52 -0800737// package_extract_file(package_file[, dest_file])
738// Extracts a single package_file from the update package and writes it to dest_file,
739// overwriting existing files if necessary. Without the dest_file argument, returns the
740// contents of the package file as a binary blob.
Tianjie Xuc4447322017-03-06 14:44:59 -0800741Value* PackageExtractFileFn(const char* name, State* state,
742 const std::vector<std::unique_ptr<Expr>>& argv) {
743 if (argv.size() < 1 || argv.size() > 2) {
744 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 or 2 args, got %zu", name,
745 argv.size());
Tao Baoef0eb3b2016-11-14 21:29:52 -0800746 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700747
Tianjie Xuc4447322017-03-06 14:44:59 -0800748 if (argv.size() == 2) {
Tao Baoef0eb3b2016-11-14 21:29:52 -0800749 // The two-argument version extracts to a file.
750
751 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800752 if (!ReadArgs(state, argv, &args)) {
753 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name,
754 argv.size());
Doug Zongker8edb00c2009-06-11 17:21:44 -0700755 }
Tao Baoef0eb3b2016-11-14 21:29:52 -0800756 const std::string& zip_path = args[0];
757 const std::string& dest_path = args[1];
Doug Zongker8edb00c2009-06-11 17:21:44 -0700758
Tao Baoef0eb3b2016-11-14 21:29:52 -0800759 ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip;
760 ZipString zip_string_path(zip_path.c_str());
761 ZipEntry entry;
762 if (FindEntry(za, zip_string_path, &entry) != 0) {
Tao Bao039f2da2016-11-22 16:29:50 -0800763 LOG(ERROR) << name << ": no " << zip_path << " in package";
Tao Baoef0eb3b2016-11-14 21:29:52 -0800764 return StringValue("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700765 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700766
Tao Bao358c2ec2016-11-28 11:48:43 -0800767 unique_fd fd(TEMP_FAILURE_RETRY(
768 ota_open(dest_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)));
Tao Baoef0eb3b2016-11-14 21:29:52 -0800769 if (fd == -1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800770 PLOG(ERROR) << name << ": can't open " << dest_path << " for write";
Tao Baoef0eb3b2016-11-14 21:29:52 -0800771 return StringValue("");
772 }
773
774 bool success = true;
775 int32_t ret = ExtractEntryToFile(za, &entry, fd);
776 if (ret != 0) {
Tao Bao039f2da2016-11-22 16:29:50 -0800777 LOG(ERROR) << name << ": Failed to extract entry \"" << zip_path << "\" ("
778 << entry.uncompressed_length << " bytes) to \"" << dest_path
779 << "\": " << ErrorCodeString(ret);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800780 success = false;
781 }
782 if (ota_fsync(fd) == -1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800783 PLOG(ERROR) << "fsync of \"" << dest_path << "\" failed";
Tao Baoef0eb3b2016-11-14 21:29:52 -0800784 success = false;
785 }
786 if (ota_close(fd) == -1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800787 PLOG(ERROR) << "close of \"" << dest_path << "\" failed";
Tao Baoef0eb3b2016-11-14 21:29:52 -0800788 success = false;
789 }
790
791 return StringValue(success ? "t" : "");
792 } else {
793 // The one-argument version returns the contents of the file as the result.
794
795 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800796 if (!ReadArgs(state, argv, &args)) {
797 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name,
798 argv.size());
Tao Baoef0eb3b2016-11-14 21:29:52 -0800799 }
800 const std::string& zip_path = args[0];
801
802 ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip;
803 ZipString zip_string_path(zip_path.c_str());
804 ZipEntry entry;
805 if (FindEntry(za, zip_string_path, &entry) != 0) {
806 return ErrorAbort(state, kPackageExtractFileFailure, "%s(): no %s in package", name,
807 zip_path.c_str());
808 }
809
810 std::string buffer;
811 buffer.resize(entry.uncompressed_length);
812
813 int32_t ret = ExtractToMemory(za, &entry, reinterpret_cast<uint8_t*>(&buffer[0]), buffer.size());
814 if (ret != 0) {
815 return ErrorAbort(state, kPackageExtractFileFailure,
816 "%s: Failed to extract entry \"%s\" (%zu bytes) to memory: %s", name,
817 zip_path.c_str(), buffer.size(), ErrorCodeString(ret));
818 }
819
820 return new Value(VAL_BLOB, buffer);
821 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700822}
823
Tom Marshallbf4f24f2017-08-23 18:14:00 +0000824// symlink(target, [src1, src2, ...])
825// Creates all sources as symlinks to target. It unlinks any previously existing src1, src2, etc
826// before creating symlinks.
827Value* SymlinkFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
828 if (argv.size() == 0) {
829 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1+ args, got %zu", name, argv.size());
830 }
831 std::string target;
832 if (!Evaluate(state, argv[0], &target)) {
833 return nullptr;
834 }
835
836 std::vector<std::string> srcs;
837 if (!ReadArgs(state, argv, &srcs, 1, argv.size())) {
838 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
839 }
840
841 size_t bad = 0;
842 for (const auto& src : srcs) {
843 if (unlink(src.c_str()) == -1 && errno != ENOENT) {
844 PLOG(ERROR) << name << ": failed to remove " << src;
845 ++bad;
846 } else if (!make_parents(src)) {
847 LOG(ERROR) << name << ": failed to symlink " << src << " to " << target
848 << ": making parents failed";
849 ++bad;
850 } else if (symlink(target.c_str(), src.c_str()) == -1) {
851 PLOG(ERROR) << name << ": failed to symlink " << src << " to " << target;
852 ++bad;
853 }
854 }
855 if (bad != 0) {
856 return ErrorAbort(state, kSymlinkFailure, "%s: Failed to create %zu symlink(s)", name, bad);
857 }
858 return StringValue("t");
859}
860
861struct perm_parsed_args {
862 bool has_uid;
863 uid_t uid;
864 bool has_gid;
865 gid_t gid;
866 bool has_mode;
867 mode_t mode;
868 bool has_fmode;
869 mode_t fmode;
870 bool has_dmode;
871 mode_t dmode;
872 bool has_selabel;
873 const char* selabel;
874 bool has_capabilities;
875 uint64_t capabilities;
876};
877
878static struct perm_parsed_args ParsePermArgs(State * state,
879 const std::vector<std::string>& args) {
880 struct perm_parsed_args parsed;
881 int bad = 0;
882 static int max_warnings = 20;
883
884 memset(&parsed, 0, sizeof(parsed));
885
886 for (size_t i = 1; i < args.size(); i += 2) {
887 if (args[i] == "uid") {
888 int64_t uid;
889 if (sscanf(args[i + 1].c_str(), "%" SCNd64, &uid) == 1) {
890 parsed.uid = uid;
891 parsed.has_uid = true;
892 } else {
893 uiPrintf(state, "ParsePermArgs: invalid UID \"%s\"\n", args[i + 1].c_str());
894 bad++;
895 }
896 continue;
897 }
898 if (args[i] == "gid") {
899 int64_t gid;
900 if (sscanf(args[i + 1].c_str(), "%" SCNd64, &gid) == 1) {
901 parsed.gid = gid;
902 parsed.has_gid = true;
903 } else {
904 uiPrintf(state, "ParsePermArgs: invalid GID \"%s\"\n", args[i + 1].c_str());
905 bad++;
906 }
907 continue;
908 }
909 if (args[i] == "mode") {
910 int32_t mode;
911 if (sscanf(args[i + 1].c_str(), "%" SCNi32, &mode) == 1) {
912 parsed.mode = mode;
913 parsed.has_mode = true;
914 } else {
915 uiPrintf(state, "ParsePermArgs: invalid mode \"%s\"\n", args[i + 1].c_str());
916 bad++;
917 }
918 continue;
919 }
920 if (args[i] == "dmode") {
921 int32_t mode;
922 if (sscanf(args[i + 1].c_str(), "%" SCNi32, &mode) == 1) {
923 parsed.dmode = mode;
924 parsed.has_dmode = true;
925 } else {
926 uiPrintf(state, "ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1].c_str());
927 bad++;
928 }
929 continue;
930 }
931 if (args[i] == "fmode") {
932 int32_t mode;
933 if (sscanf(args[i + 1].c_str(), "%" SCNi32, &mode) == 1) {
934 parsed.fmode = mode;
935 parsed.has_fmode = true;
936 } else {
937 uiPrintf(state, "ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1].c_str());
938 bad++;
939 }
940 continue;
941 }
942 if (args[i] == "capabilities") {
943 int64_t capabilities;
944 if (sscanf(args[i + 1].c_str(), "%" SCNi64, &capabilities) == 1) {
945 parsed.capabilities = capabilities;
946 parsed.has_capabilities = true;
947 } else {
948 uiPrintf(state, "ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1].c_str());
949 bad++;
950 }
951 continue;
952 }
953 if (args[i] == "selabel") {
954 if (!args[i + 1].empty()) {
955 parsed.selabel = args[i + 1].c_str();
956 parsed.has_selabel = true;
957 } else {
958 uiPrintf(state, "ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1].c_str());
959 bad++;
960 }
961 continue;
962 }
963 if (max_warnings != 0) {
964 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i].c_str());
965 max_warnings--;
966 if (max_warnings == 0) {
967 LOG(INFO) << "ParsedPermArgs: suppressing further warnings";
968 }
969 }
970 }
971 return parsed;
972}
973
974static int ApplyParsedPerms(State* state, const char* filename, const struct stat* statptr,
975 struct perm_parsed_args parsed) {
976 int bad = 0;
977
978 if (parsed.has_selabel) {
979 if (lsetfilecon(filename, parsed.selabel) != 0) {
980 uiPrintf(state, "ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n", filename,
981 parsed.selabel, strerror(errno));
982 bad++;
983 }
984 }
985
986 /* ignore symlinks */
987 if (S_ISLNK(statptr->st_mode)) {
988 return bad;
989 }
990
991 if (parsed.has_uid) {
992 if (chown(filename, parsed.uid, -1) < 0) {
993 uiPrintf(state, "ApplyParsedPerms: chown of %s to %d failed: %s\n", filename, parsed.uid,
994 strerror(errno));
995 bad++;
996 }
997 }
998
999 if (parsed.has_gid) {
1000 if (chown(filename, -1, parsed.gid) < 0) {
1001 uiPrintf(state, "ApplyParsedPerms: chgrp of %s to %d failed: %s\n", filename, parsed.gid,
1002 strerror(errno));
1003 bad++;
1004 }
1005 }
1006
1007 if (parsed.has_mode) {
1008 if (chmod(filename, parsed.mode) < 0) {
1009 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n", filename, parsed.mode,
1010 strerror(errno));
1011 bad++;
1012 }
1013 }
1014
1015 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
1016 if (chmod(filename, parsed.dmode) < 0) {
1017 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n", filename, parsed.dmode,
1018 strerror(errno));
1019 bad++;
1020 }
1021 }
1022
1023 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
1024 if (chmod(filename, parsed.fmode) < 0) {
1025 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n", filename, parsed.fmode,
1026 strerror(errno));
1027 bad++;
1028 }
1029 }
1030
1031 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
1032 if (parsed.capabilities == 0) {
1033 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
1034 // Report failure unless it's ENODATA (attribute not set)
1035 uiPrintf(state, "ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n", filename,
1036 parsed.capabilities, strerror(errno));
1037 bad++;
1038 }
1039 } else {
1040 struct vfs_cap_data cap_data;
1041 memset(&cap_data, 0, sizeof(cap_data));
1042 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
1043 cap_data.data[0].permitted = (uint32_t)(parsed.capabilities & 0xffffffff);
1044 cap_data.data[0].inheritable = 0;
1045 cap_data.data[1].permitted = (uint32_t)(parsed.capabilities >> 32);
1046 cap_data.data[1].inheritable = 0;
1047 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
1048 uiPrintf(state, "ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n", filename,
1049 parsed.capabilities, strerror(errno));
1050 bad++;
1051 }
1052 }
1053 }
1054
1055 return bad;
1056}
1057
1058// nftw doesn't allow us to pass along context, so we need to use
1059// global variables. *sigh*
1060static struct perm_parsed_args recursive_parsed_args;
1061static State* recursive_state;
1062
1063static int do_SetMetadataRecursive(const char* filename, const struct stat* statptr, int fileflags,
1064 struct FTW* pfwt) {
1065 return ApplyParsedPerms(recursive_state, filename, statptr, recursive_parsed_args);
1066}
1067
1068static Value* SetMetadataFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1069 if ((argv.size() % 2) != 1) {
1070 return ErrorAbort(state, kArgsParsingFailure, "%s() expects an odd number of arguments, got %zu",
1071 name, argv.size());
1072 }
1073
1074 std::vector<std::string> args;
1075 if (!ReadArgs(state, argv, &args)) {
1076 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1077 }
1078
1079 struct stat sb;
1080 if (lstat(args[0].c_str(), &sb) == -1) {
1081 return ErrorAbort(state, kSetMetadataFailure, "%s: Error on lstat of \"%s\": %s", name,
1082 args[0].c_str(), strerror(errno));
1083 }
1084
1085 struct perm_parsed_args parsed = ParsePermArgs(state, args);
1086 int bad = 0;
1087 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
1088
1089 if (recursive) {
1090 recursive_parsed_args = parsed;
1091 recursive_state = state;
1092 bad += nftw(args[0].c_str(), do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
1093 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
1094 recursive_state = NULL;
1095 } else {
1096 bad += ApplyParsedPerms(state, args[0].c_str(), &sb, parsed);
1097 }
1098
1099 if (bad > 0) {
1100 return ErrorAbort(state, kSetMetadataFailure, "%s: some changes failed", name);
1101 }
1102
1103 return StringValue("");
1104}
1105
Tianjie Xuc4447322017-03-06 14:44:59 -08001106Value* GetPropFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1107 if (argv.size() != 1) {
1108 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -08001109 }
1110 std::string key;
1111 if (!Evaluate(state, argv[0], &key)) {
1112 return nullptr;
1113 }
1114 std::string value = android::base::GetProperty(key, "");
Doug Zongker8edb00c2009-06-11 17:21:44 -07001115
Tao Bao039f2da2016-11-22 16:29:50 -08001116 return StringValue(value);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001117}
1118
Doug Zongker47cace92009-06-18 10:11:50 -07001119// file_getprop(file, key)
1120//
1121// interprets 'file' as a getprop-style file (key=value pairs, one
Tao Bao51d516e2016-11-03 14:49:01 -07001122// per line. # comment lines, blank lines, lines without '=' ignored),
Michael Rungeaa1a31e2014-04-25 18:47:18 -07001123// and returns the value for 'key' (or "" if it isn't defined).
Tianjie Xu5419ad32018-02-02 16:49:15 -08001124Value* FileGetPropFn(const char* name, State* state,
1125 const std::vector<std::unique_ptr<Expr>>& argv) {
Tianjie Xuc4447322017-03-06 14:44:59 -08001126 if (argv.size() != 2) {
1127 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
1128 argv.size());
Tao Bao358c2ec2016-11-28 11:48:43 -08001129 }
1130
1131 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001132 if (!ReadArgs(state, argv, &args)) {
Tao Bao358c2ec2016-11-28 11:48:43 -08001133 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1134 }
1135 const std::string& filename = args[0];
1136 const std::string& key = args[1];
1137
1138 struct stat st;
1139 if (stat(filename.c_str(), &st) < 0) {
1140 return ErrorAbort(state, kFileGetPropFailure, "%s: failed to stat \"%s\": %s", name,
1141 filename.c_str(), strerror(errno));
1142 }
1143
1144 constexpr off_t MAX_FILE_GETPROP_SIZE = 65536;
1145 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
1146 return ErrorAbort(state, kFileGetPropFailure, "%s too large for %s (max %lld)",
1147 filename.c_str(), name, static_cast<long long>(MAX_FILE_GETPROP_SIZE));
1148 }
1149
1150 std::string buffer(st.st_size, '\0');
1151 unique_file f(ota_fopen(filename.c_str(), "rb"));
1152 if (f == nullptr) {
1153 return ErrorAbort(state, kFileOpenFailure, "%s: failed to open %s: %s", name, filename.c_str(),
1154 strerror(errno));
1155 }
1156
1157 if (ota_fread(&buffer[0], 1, st.st_size, f.get()) != static_cast<size_t>(st.st_size)) {
1158 ErrorAbort(state, kFreadFailure, "%s: failed to read %zu bytes from %s", name,
1159 static_cast<size_t>(st.st_size), filename.c_str());
1160 return nullptr;
1161 }
1162
1163 ota_fclose(f);
1164
1165 std::vector<std::string> lines = android::base::Split(buffer, "\n");
1166 for (size_t i = 0; i < lines.size(); i++) {
1167 std::string line = android::base::Trim(lines[i]);
1168
1169 // comment or blank line: skip to next line
1170 if (line.empty() || line[0] == '#') {
1171 continue;
1172 }
1173 size_t equal_pos = line.find('=');
1174 if (equal_pos == std::string::npos) {
1175 continue;
Doug Zongker47cace92009-06-18 10:11:50 -07001176 }
1177
Tao Bao358c2ec2016-11-28 11:48:43 -08001178 // trim whitespace between key and '='
1179 std::string str = android::base::Trim(line.substr(0, equal_pos));
Doug Zongker47cace92009-06-18 10:11:50 -07001180
Tao Bao358c2ec2016-11-28 11:48:43 -08001181 // not the key we're looking for
1182 if (key != str) continue;
Doug Zongker47cace92009-06-18 10:11:50 -07001183
Tao Bao358c2ec2016-11-28 11:48:43 -08001184 return StringValue(android::base::Trim(line.substr(equal_pos + 1)));
1185 }
Doug Zongker47cace92009-06-18 10:11:50 -07001186
Tao Bao358c2ec2016-11-28 11:48:43 -08001187 return StringValue("");
Doug Zongker8edb00c2009-06-11 17:21:44 -07001188}
1189
Doug Zongker8edb00c2009-06-11 17:21:44 -07001190// apply_patch_space(bytes)
Tianjie Xu5419ad32018-02-02 16:49:15 -08001191Value* ApplyPatchSpaceFn(const char* name, State* state,
1192 const std::vector<std::unique_ptr<Expr>>& argv) {
Tianjie Xuc4447322017-03-06 14:44:59 -08001193 if (argv.size() != 1) {
1194 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 args, got %zu", name,
1195 argv.size());
1196 }
Tao Bao039f2da2016-11-22 16:29:50 -08001197 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001198 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001199 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1200 }
1201 const std::string& bytes_str = args[0];
Doug Zongkerc4351c72010-02-22 14:46:32 -08001202
Tao Bao039f2da2016-11-22 16:29:50 -08001203 size_t bytes;
1204 if (!android::base::ParseUint(bytes_str.c_str(), &bytes)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -08001205 return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count", name,
1206 bytes_str.c_str());
Tao Bao039f2da2016-11-22 16:29:50 -08001207 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001208
Tianjie Xu99b73be2017-11-28 17:23:06 -08001209 // Skip the cache size check if the update is a retry.
1210 if (state->is_retry || CacheSizeCheck(bytes) == 0) {
1211 return StringValue("t");
Tao Bao039f2da2016-11-22 16:29:50 -08001212 }
Tianjie Xu99b73be2017-11-28 17:23:06 -08001213 return StringValue("");
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001214}
1215
Tianjie Xuc4447322017-03-06 14:44:59 -08001216Value* WipeCacheFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1217 if (!argv.empty()) {
1218 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %zu", name,
1219 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -08001220 }
1221 fprintf(static_cast<UpdaterInfo*>(state->cookie)->cmd_pipe, "wipe_cache\n");
1222 return StringValue("t");
Doug Zongkerd0181b82011-10-19 10:51:12 -07001223}
1224
Tianjie Xuc4447322017-03-06 14:44:59 -08001225Value* RunProgramFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1226 if (argv.size() < 1) {
Tao Bao039f2da2016-11-22 16:29:50 -08001227 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
1228 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001229
Tao Bao039f2da2016-11-22 16:29:50 -08001230 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001231 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001232 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1233 }
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001234
Tianjie Xuc4447322017-03-06 14:44:59 -08001235 char* args2[argv.size() + 1];
1236 for (size_t i = 0; i < argv.size(); i++) {
Tao Bao039f2da2016-11-22 16:29:50 -08001237 args2[i] = &args[i][0];
1238 }
Tianjie Xuc4447322017-03-06 14:44:59 -08001239 args2[argv.size()] = nullptr;
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001240
Tianjie Xuc4447322017-03-06 14:44:59 -08001241 LOG(INFO) << "about to run program [" << args2[0] << "] with " << argv.size() << " args";
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001242
Tao Bao039f2da2016-11-22 16:29:50 -08001243 pid_t child = fork();
1244 if (child == 0) {
1245 execv(args2[0], args2);
1246 PLOG(ERROR) << "run_program: execv failed";
Tao Bao3da88012017-02-03 13:09:23 -08001247 _exit(EXIT_FAILURE);
Tao Bao039f2da2016-11-22 16:29:50 -08001248 }
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001249
Tao Bao039f2da2016-11-22 16:29:50 -08001250 int status;
1251 waitpid(child, &status, 0);
1252 if (WIFEXITED(status)) {
1253 if (WEXITSTATUS(status) != 0) {
1254 LOG(ERROR) << "run_program: child exited with status " << WEXITSTATUS(status);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001255 }
Tao Bao039f2da2016-11-22 16:29:50 -08001256 } else if (WIFSIGNALED(status)) {
1257 LOG(ERROR) << "run_program: child terminated by signal " << WTERMSIG(status);
1258 }
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001259
Tao Bao039f2da2016-11-22 16:29:50 -08001260 return StringValue(std::to_string(status));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001261}
1262
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001263// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001264// is actually a FileContents*).
Tianjie Xuc4447322017-03-06 14:44:59 -08001265Value* ReadFileFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1266 if (argv.size() != 1) {
1267 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -08001268 }
Doug Zongker512536a2010-02-17 16:11:44 -08001269
Tao Bao039f2da2016-11-22 16:29:50 -08001270 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001271 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001272 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1273 }
1274 const std::string& filename = args[0];
Doug Zongker512536a2010-02-17 16:11:44 -08001275
Tao Bao039f2da2016-11-22 16:29:50 -08001276 Value* v = new Value(VAL_INVALID, "");
Doug Zongker512536a2010-02-17 16:11:44 -08001277
Tao Bao039f2da2016-11-22 16:29:50 -08001278 FileContents fc;
1279 if (LoadFileContents(filename.c_str(), &fc) == 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001280 v->type = VAL_BLOB;
Tao Bao039f2da2016-11-22 16:29:50 -08001281 v->data = std::string(fc.data.begin(), fc.data.end());
1282 }
1283 return v;
Doug Zongker512536a2010-02-17 16:11:44 -08001284}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001285
Tao Bao1bf17722016-11-03 23:52:01 -07001286// write_value(value, filename)
1287// Writes 'value' to 'filename'.
1288// Example: write_value("960000", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq")
Tianjie Xuc4447322017-03-06 14:44:59 -08001289Value* WriteValueFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1290 if (argv.size() != 2) {
1291 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
1292 argv.size());
Tao Baod0f30882016-11-03 23:52:01 -07001293 }
Tao Bao1bf17722016-11-03 23:52:01 -07001294
Tao Baod0f30882016-11-03 23:52:01 -07001295 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001296 if (!ReadArgs(state, argv, &args)) {
Tao Baod0f30882016-11-03 23:52:01 -07001297 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
1298 }
Tao Bao1bf17722016-11-03 23:52:01 -07001299
Tao Baod0f30882016-11-03 23:52:01 -07001300 const std::string& filename = args[1];
1301 if (filename.empty()) {
1302 return ErrorAbort(state, kArgsParsingFailure, "%s(): Filename cannot be empty", name);
1303 }
Tao Bao1bf17722016-11-03 23:52:01 -07001304
Tao Baod0f30882016-11-03 23:52:01 -07001305 const std::string& value = args[0];
1306 if (!android::base::WriteStringToFile(value, filename)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001307 PLOG(ERROR) << name << ": Failed to write to \"" << filename << "\"";
Tao Baod0f30882016-11-03 23:52:01 -07001308 return StringValue("");
1309 } else {
1310 return StringValue("t");
1311 }
Tao Bao1bf17722016-11-03 23:52:01 -07001312}
1313
Doug Zongkerc87bab12013-11-25 13:53:25 -08001314// Immediately reboot the device. Recovery is not finished normally,
1315// so if you reboot into recovery it will re-start applying the
1316// current package (because nothing has cleared the copy of the
1317// arguments stored in the BCB).
1318//
1319// The argument is the partition name passed to the android reboot
1320// property. It can be "recovery" to boot from the recovery
1321// partition, or "" (empty string) to boot from the regular boot
1322// partition.
Tianjie Xuc4447322017-03-06 14:44:59 -08001323Value* RebootNowFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1324 if (argv.size() != 2) {
1325 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
1326 argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -08001327 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001328
Tao Baobedf5fc2016-11-18 12:01:26 -08001329 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001330 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -08001331 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
1332 }
1333 const std::string& filename = args[0];
1334 const std::string& property = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001335
Tao Baobedf5fc2016-11-18 12:01:26 -08001336 // Zero out the 'command' field of the bootloader message. Leave the rest intact.
1337 bootloader_message boot;
1338 std::string err;
1339 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001340 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001341 return StringValue("");
1342 }
1343 memset(boot.command, 0, sizeof(boot.command));
1344 if (!write_bootloader_message_to(boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001345 LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001346 return StringValue("");
1347 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001348
Dmitri Plotnikoved9db0f2017-04-18 08:28:26 -07001349 std::string reboot_cmd = "reboot," + property;
1350 if (android::base::GetBoolProperty("ro.boot.quiescent", false)) {
1351 reboot_cmd += ",quiescent";
1352 }
Tao Baobedf5fc2016-11-18 12:01:26 -08001353 android::base::SetProperty(ANDROID_RB_PROPERTY, reboot_cmd);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001354
Tao Baobedf5fc2016-11-18 12:01:26 -08001355 sleep(5);
1356 return ErrorAbort(state, kRebootFailure, "%s() failed to reboot", name);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001357}
1358
1359// Store a string value somewhere that future invocations of recovery
1360// can access it. This value is called the "stage" and can be used to
1361// drive packages that need to do reboots in the middle of
1362// installation and keep track of where they are in the multi-stage
1363// install.
1364//
1365// The first argument is the block device for the misc partition
1366// ("/misc" in the fstab), which is where this value is stored. The
1367// second argument is the string to store; it should not exceed 31
1368// bytes.
Tianjie Xuc4447322017-03-06 14:44:59 -08001369Value* SetStageFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1370 if (argv.size() != 2) {
1371 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
1372 argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -08001373 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001374
Tao Baobedf5fc2016-11-18 12:01:26 -08001375 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001376 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -08001377 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1378 }
1379 const std::string& filename = args[0];
1380 const std::string& stagestr = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001381
Tao Baobedf5fc2016-11-18 12:01:26 -08001382 // Store this value in the misc partition, immediately after the
1383 // bootloader message that the main recovery uses to save its
1384 // arguments in case of the device restarting midway through
1385 // package installation.
1386 bootloader_message boot;
1387 std::string err;
1388 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001389 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001390 return StringValue("");
1391 }
1392 strlcpy(boot.stage, stagestr.c_str(), sizeof(boot.stage));
1393 if (!write_bootloader_message_to(boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001394 LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001395 return StringValue("");
1396 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001397
Tao Baobedf5fc2016-11-18 12:01:26 -08001398 return StringValue(filename);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001399}
1400
1401// Return the value most recently saved with SetStageFn. The argument
1402// is the block device for the misc partition.
Tianjie Xuc4447322017-03-06 14:44:59 -08001403Value* GetStageFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1404 if (argv.size() != 1) {
1405 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -08001406 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001407
Tao Baobedf5fc2016-11-18 12:01:26 -08001408 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001409 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -08001410 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1411 }
1412 const std::string& filename = args[0];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001413
Tao Baobedf5fc2016-11-18 12:01:26 -08001414 bootloader_message boot;
1415 std::string err;
1416 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001417 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001418 return StringValue("");
1419 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001420
Tao Baobedf5fc2016-11-18 12:01:26 -08001421 return StringValue(boot.stage);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001422}
1423
Tianjie Xuc4447322017-03-06 14:44:59 -08001424Value* WipeBlockDeviceFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1425 if (argv.size() != 2) {
1426 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
1427 argv.size());
Tao Bao358c2ec2016-11-28 11:48:43 -08001428 }
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001429
Tao Bao358c2ec2016-11-28 11:48:43 -08001430 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001431 if (!ReadArgs(state, argv, &args)) {
Tao Bao358c2ec2016-11-28 11:48:43 -08001432 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1433 }
1434 const std::string& filename = args[0];
1435 const std::string& len_str = args[1];
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001436
Tao Bao358c2ec2016-11-28 11:48:43 -08001437 size_t len;
1438 if (!android::base::ParseUint(len_str.c_str(), &len)) {
1439 return nullptr;
1440 }
1441 unique_fd fd(ota_open(filename.c_str(), O_WRONLY, 0644));
1442 // The wipe_block_device function in ext4_utils returns 0 on success and 1
1443 // for failure.
1444 int status = wipe_block_device(fd, len);
1445 return StringValue((status == 0) ? "t" : "");
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001446}
1447
Tianjie Xuc4447322017-03-06 14:44:59 -08001448Value* EnableRebootFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1449 if (!argv.empty()) {
1450 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %zu", name,
1451 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -08001452 }
1453 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
1454 fprintf(ui->cmd_pipe, "enable_reboot\n");
1455 return StringValue("t");
Doug Zongkerc704e062014-05-23 08:40:35 -07001456}
1457
Tianjie Xuc4447322017-03-06 14:44:59 -08001458Value* Tune2FsFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
Ethan Yonker7e1b9862015-03-19 14:10:01 -05001459#ifdef HAVE_LIBTUNE2FS
Tianjie Xuc4447322017-03-06 14:44:59 -08001460 if (argv.empty()) {
1461 return ErrorAbort(state, kArgsParsingFailure, "%s() expects args, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -08001462 }
Michael Rungeb278c252014-11-21 00:12:28 -08001463
Tao Bao039f2da2016-11-22 16:29:50 -08001464 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001465 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001466 return ErrorAbort(state, kArgsParsingFailure, "%s() could not read args", name);
1467 }
Michael Rungeb278c252014-11-21 00:12:28 -08001468
Tianjie Xuc4447322017-03-06 14:44:59 -08001469 char* args2[argv.size() + 1];
Tao Bao039f2da2016-11-22 16:29:50 -08001470 // Tune2fs expects the program name as its args[0]
1471 args2[0] = const_cast<char*>(name);
1472 if (args2[0] == nullptr) {
1473 return nullptr;
1474 }
Tianjie Xuc4447322017-03-06 14:44:59 -08001475 for (size_t i = 0; i < argv.size(); ++i) {
Tao Bao039f2da2016-11-22 16:29:50 -08001476 args2[i + 1] = &args[i][0];
1477 }
Michael Rungeb278c252014-11-21 00:12:28 -08001478
Tao Bao039f2da2016-11-22 16:29:50 -08001479 // tune2fs changes the file system parameters on an ext2 file system; it
1480 // returns 0 on success.
Tianjie Xuc4447322017-03-06 14:44:59 -08001481 int result = tune2fs_main(argv.size() + 1, args2);
Tao Bao039f2da2016-11-22 16:29:50 -08001482 if (result != 0) {
1483 return ErrorAbort(state, kTune2FsFailure, "%s() returned error code %d", name, result);
1484 }
1485 return StringValue("t");
Ethan Yonker7e1b9862015-03-19 14:10:01 -05001486#else
Ethan Yonker8373cfe2017-09-08 06:50:54 -05001487 return ErrorAbort(state, kTune2FsFailure, "%s() support not present, no libtune2fs", name);
Ethan Yonker7e1b9862015-03-19 14:10:01 -05001488#endif // HAVE_LIBTUNE2FS
Michael Rungeb278c252014-11-21 00:12:28 -08001489}
1490
Doug Zongker9931f7f2009-06-10 14:11:53 -07001491void RegisterInstallFunctions() {
Tao Bao039f2da2016-11-22 16:29:50 -08001492 RegisterFunction("mount", MountFn);
1493 RegisterFunction("is_mounted", IsMountedFn);
1494 RegisterFunction("unmount", UnmountFn);
1495 RegisterFunction("format", FormatFn);
1496 RegisterFunction("show_progress", ShowProgressFn);
1497 RegisterFunction("set_progress", SetProgressFn);
Tom Marshallbf4f24f2017-08-23 18:14:00 +00001498 RegisterFunction("delete", DeleteFn);
1499 RegisterFunction("delete_recursive", DeleteFn);
Tom Marshall981118e2017-10-25 20:27:08 +02001500 RegisterFunction("package_extract_dir", PackageExtractDirFn);
Tao Bao039f2da2016-11-22 16:29:50 -08001501 RegisterFunction("package_extract_file", PackageExtractFileFn);
Tom Marshallbf4f24f2017-08-23 18:14:00 +00001502 RegisterFunction("symlink", SymlinkFn);
1503
1504 // Usage:
1505 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1506 // Example:
1507 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel",
1508 // "u:object_r:system_file:s0", "capabilities", 0x0);
1509 RegisterFunction("set_metadata", SetMetadataFn);
1510
1511 // Usage:
1512 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1513 // Example:
1514 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755,
1515 // "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1516 RegisterFunction("set_metadata_recursive", SetMetadataFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001517
Tao Bao039f2da2016-11-22 16:29:50 -08001518 RegisterFunction("getprop", GetPropFn);
1519 RegisterFunction("file_getprop", FileGetPropFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001520
Tao Bao039f2da2016-11-22 16:29:50 -08001521 RegisterFunction("apply_patch", ApplyPatchFn);
1522 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1523 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001524
Tao Bao039f2da2016-11-22 16:29:50 -08001525 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001526
Tao Bao039f2da2016-11-22 16:29:50 -08001527 RegisterFunction("read_file", ReadFileFn);
1528 RegisterFunction("sha1_check", Sha1CheckFn);
Tom Marshallbf4f24f2017-08-23 18:14:00 +00001529 RegisterFunction("rename", RenameFn);
Tao Bao039f2da2016-11-22 16:29:50 -08001530 RegisterFunction("write_value", WriteValueFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001531
Tao Bao039f2da2016-11-22 16:29:50 -08001532 RegisterFunction("wipe_cache", WipeCacheFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001533
Tao Bao039f2da2016-11-22 16:29:50 -08001534 RegisterFunction("ui_print", UIPrintFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001535
Tao Bao039f2da2016-11-22 16:29:50 -08001536 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerd0181b82011-10-19 10:51:12 -07001537
Tao Bao039f2da2016-11-22 16:29:50 -08001538 RegisterFunction("reboot_now", RebootNowFn);
1539 RegisterFunction("get_stage", GetStageFn);
1540 RegisterFunction("set_stage", SetStageFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001541
Tao Bao039f2da2016-11-22 16:29:50 -08001542 RegisterFunction("enable_reboot", EnableRebootFn);
1543 RegisterFunction("tune2fs", Tune2FsFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001544}