blob: 34514b65af629dcc9e664480555d1d00bfd4f4c3 [file] [log] [blame]
Doug Zongker9931f7f2009-06-10 14:11:53 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Tao Bao0c7839a2016-10-10 15:48:37 -070017#include "updater/install.h"
18
Doug Zongkerfbf3c102009-06-24 09:36:20 -070019#include <ctype.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070020#include <errno.h>
Tao Bao361342c2016-02-08 11:15:50 -080021#include <fcntl.h>
22#include <ftw.h>
23#include <inttypes.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070024#include <stdarg.h>
Doug Zongkerfbf3c102009-06-24 09:36:20 -070025#include <stdio.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070026#include <stdlib.h>
27#include <string.h>
Tao Bao361342c2016-02-08 11:15:50 -080028#include <sys/capability.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070029#include <sys/mount.h>
30#include <sys/stat.h>
31#include <sys/types.h>
Doug Zongkera3f89ea2009-09-10 14:10:48 -070032#include <sys/wait.h>
Nick Kralevich5dbdef02013-09-07 14:41:06 -070033#include <sys/xattr.h>
Tao Bao361342c2016-02-08 11:15:50 -080034#include <time.h>
35#include <unistd.h>
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070036#include <utime.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070037
Yabin Cui64be2132016-02-03 18:16:02 -080038#include <memory>
Tao Bao361342c2016-02-08 11:15:50 -080039#include <string>
Yabin Cui64be2132016-02-03 18:16:02 -080040#include <vector>
41
Tao Baod0f30882016-11-03 23:52:01 -070042#include <android-base/file.h>
Tao Bao039f2da2016-11-22 16:29:50 -080043#include <android-base/logging.h>
Tianjie Xu5fe280a2016-10-17 18:15:20 -070044#include <android-base/parsedouble.h>
Tao Baod0f30882016-11-03 23:52:01 -070045#include <android-base/parseint.h>
Elliott Hughescb220402016-09-23 15:30:55 -070046#include <android-base/properties.h>
Elliott Hughes4b166f02015-12-04 15:30:20 -080047#include <android-base/stringprintf.h>
Tao Baod0f30882016-11-03 23:52:01 -070048#include <android-base/strings.h>
Tianjie Xu22f11202018-08-27 10:50:31 -070049#include <android-base/unique_fd.h>
Tao Bao0d3f84f2016-12-28 15:09:20 -080050#include <applypatch/applypatch.h>
51#include <bootloader_message/bootloader_message.h>
Tao Baode40ba52016-10-05 23:17:01 -070052#include <ext4_utils/wipe.h>
Tao Bao361342c2016-02-08 11:15:50 -080053#include <openssl/sha.h>
Elliott Hughes4bbd5bf2016-04-01 18:24:39 -070054#include <selinux/label.h>
55#include <selinux/selinux.h>
Tao Bao09e468f2017-09-29 14:39:33 -070056#include <tune2fs.h>
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070057#include <ziparchive/zip_archive.h>
Tao Bao1107d962015-09-09 17:16:55 -070058
Doug Zongker9931f7f2009-06-10 14:11:53 -070059#include "edify/expr.h"
Tao Bao17054c02018-05-03 22:41:23 -070060#include "otautil/dirutil.h"
Tao Bao1fc5bf32017-10-06 07:43:41 -070061#include "otautil/error_code.h"
Tao Bao9a319f02018-01-04 13:19:11 -080062#include "otautil/mounts.h"
Tao Bao09e468f2017-09-29 14:39:33 -070063#include "otautil/print_sha1.h"
Tao Bao2c526392018-05-03 23:01:13 -070064#include "otautil/sysutil.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070065#include "updater/updater.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070066
Tao Bao1107d962015-09-09 17:16:55 -070067// Send over the buffer to recovery though the command pipe.
68static void uiPrint(State* state, const std::string& buffer) {
Tao Bao039f2da2016-11-22 16:29:50 -080069 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
Tao Baob6918c72015-05-19 17:02:16 -070070
Tao Bao039f2da2016-11-22 16:29:50 -080071 // "line1\nline2\n" will be split into 3 tokens: "line1", "line2" and "".
72 // So skip sending empty strings to UI.
73 std::vector<std::string> lines = android::base::Split(buffer, "\n");
74 for (auto& line : lines) {
75 if (!line.empty()) {
76 fprintf(ui->cmd_pipe, "ui_print %s\n", line.c_str());
Tao Bao1107d962015-09-09 17:16:55 -070077 }
Tao Bao039f2da2016-11-22 16:29:50 -080078 }
Tao Bao1107d962015-09-09 17:16:55 -070079
Tao Bao039f2da2016-11-22 16:29:50 -080080 // On the updater side, we need to dump the contents to stderr (which has
81 // been redirected to the log file). Because the recovery will only print
82 // the contents to screen when processing pipe command ui_print.
83 LOG(INFO) << buffer;
Michael Runged4a63422014-10-22 19:48:41 -070084}
85
Elliott Hughes83ce7552016-06-30 09:28:42 -070086void uiPrintf(State* _Nonnull state, const char* _Nonnull format, ...) {
Tao Bao039f2da2016-11-22 16:29:50 -080087 std::string error_msg;
Tao Bao1107d962015-09-09 17:16:55 -070088
Tao Bao039f2da2016-11-22 16:29:50 -080089 va_list ap;
90 va_start(ap, format);
91 android::base::StringAppendV(&error_msg, format, ap);
92 va_end(ap);
Tao Bao1107d962015-09-09 17:16:55 -070093
Tao Bao039f2da2016-11-22 16:29:50 -080094 uiPrint(state, error_msg);
Michael Runged4a63422014-10-22 19:48:41 -070095}
96
Tianjie Xu5419ad32018-02-02 16:49:15 -080097// This is the updater side handler for ui_print() in edify script. Contents will be sent over to
98// the recovery side for on-screen display.
99Value* UIPrintFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
100 std::vector<std::string> args;
101 if (!ReadArgs(state, argv, &args)) {
102 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
103 }
104
105 std::string buffer = android::base::Join(args, "");
106 uiPrint(state, buffer);
107 return StringValue(buffer);
108}
109
110// package_extract_file(package_file[, dest_file])
111// Extracts a single package_file from the update package and writes it to dest_file,
112// overwriting existing files if necessary. Without the dest_file argument, returns the
113// contents of the package file as a binary blob.
114Value* PackageExtractFileFn(const char* name, State* state,
115 const std::vector<std::unique_ptr<Expr>>& argv) {
116 if (argv.size() < 1 || argv.size() > 2) {
117 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 or 2 args, got %zu", name,
118 argv.size());
119 }
120
121 if (argv.size() == 2) {
122 // The two-argument version extracts to a file.
123
124 std::vector<std::string> args;
125 if (!ReadArgs(state, argv, &args)) {
126 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name,
127 argv.size());
128 }
129 const std::string& zip_path = args[0];
130 const std::string& dest_path = args[1];
131
132 ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip;
133 ZipString zip_string_path(zip_path.c_str());
134 ZipEntry entry;
135 if (FindEntry(za, zip_string_path, &entry) != 0) {
136 LOG(ERROR) << name << ": no " << zip_path << " in package";
137 return StringValue("");
138 }
139
Tianjie Xu22f11202018-08-27 10:50:31 -0700140 android::base::unique_fd fd(TEMP_FAILURE_RETRY(
141 open(dest_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)));
Tianjie Xu5419ad32018-02-02 16:49:15 -0800142 if (fd == -1) {
143 PLOG(ERROR) << name << ": can't open " << dest_path << " for write";
144 return StringValue("");
145 }
146
147 bool success = true;
148 int32_t ret = ExtractEntryToFile(za, &entry, fd);
149 if (ret != 0) {
150 LOG(ERROR) << name << ": Failed to extract entry \"" << zip_path << "\" ("
151 << entry.uncompressed_length << " bytes) to \"" << dest_path
152 << "\": " << ErrorCodeString(ret);
153 success = false;
154 }
Tianjie Xu22f11202018-08-27 10:50:31 -0700155 if (fsync(fd) == -1) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800156 PLOG(ERROR) << "fsync of \"" << dest_path << "\" failed";
157 success = false;
158 }
Tianjie Xu22f11202018-08-27 10:50:31 -0700159
160 if (close(fd.release()) != 0) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800161 PLOG(ERROR) << "close of \"" << dest_path << "\" failed";
162 success = false;
163 }
164
165 return StringValue(success ? "t" : "");
166 } else {
167 // The one-argument version returns the contents of the file as the result.
168
169 std::vector<std::string> args;
170 if (!ReadArgs(state, argv, &args)) {
171 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name,
172 argv.size());
173 }
174 const std::string& zip_path = args[0];
175
176 ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip;
177 ZipString zip_string_path(zip_path.c_str());
178 ZipEntry entry;
179 if (FindEntry(za, zip_string_path, &entry) != 0) {
180 return ErrorAbort(state, kPackageExtractFileFailure, "%s(): no %s in package", name,
181 zip_path.c_str());
182 }
183
184 std::string buffer;
185 buffer.resize(entry.uncompressed_length);
186
187 int32_t ret =
188 ExtractToMemory(za, &entry, reinterpret_cast<uint8_t*>(&buffer[0]), buffer.size());
189 if (ret != 0) {
190 return ErrorAbort(state, kPackageExtractFileFailure,
191 "%s: Failed to extract entry \"%s\" (%zu bytes) to memory: %s", name,
192 zip_path.c_str(), buffer.size(), ErrorCodeString(ret));
193 }
194
Tao Bao511d7592018-06-19 15:56:49 -0700195 return new Value(Value::Type::BLOB, buffer);
Tianjie Xu5419ad32018-02-02 16:49:15 -0800196 }
197}
198
199// apply_patch(src_file, tgt_file, tgt_sha1, tgt_size, patch1_sha1, patch1_blob, [...])
200// Applies a binary patch to the src_file to produce the tgt_file. If the desired target is the
201// same as the source, pass "-" for tgt_file. tgt_sha1 and tgt_size are the expected final SHA1
202// hash and size of the target file. The remaining arguments must come in pairs: a SHA1 hash (a
203// 40-character hex string) and a blob. The blob is the patch to be applied when the source
204// file's current contents have the given SHA1.
205//
206// The patching is done in a safe manner that guarantees the target file either has the desired
207// SHA1 hash and size, or it is untouched -- it will not be left in an unrecoverable intermediate
208// state. If the process is interrupted during patching, the target file may be in an intermediate
209// state; a copy exists in the cache partition so restarting the update can successfully update
210// the file.
211Value* ApplyPatchFn(const char* name, State* state,
212 const std::vector<std::unique_ptr<Expr>>& argv) {
213 if (argv.size() < 6 || (argv.size() % 2) == 1) {
214 return ErrorAbort(state, kArgsParsingFailure,
215 "%s(): expected at least 6 args and an "
216 "even number, got %zu",
217 name, argv.size());
218 }
219
220 std::vector<std::string> args;
221 if (!ReadArgs(state, argv, &args, 0, 4)) {
222 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
223 }
224 const std::string& source_filename = args[0];
225 const std::string& target_filename = args[1];
226 const std::string& target_sha1 = args[2];
227 const std::string& target_size_str = args[3];
228
229 size_t target_size;
230 if (!android::base::ParseUint(target_size_str.c_str(), &target_size)) {
231 return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count", name,
232 target_size_str.c_str());
233 }
234
235 int patchcount = (argv.size() - 4) / 2;
236 std::vector<std::unique_ptr<Value>> arg_values;
237 if (!ReadValueArgs(state, argv, &arg_values, 4, argv.size() - 4)) {
238 return nullptr;
239 }
240
241 for (int i = 0; i < patchcount; ++i) {
Tao Bao511d7592018-06-19 15:56:49 -0700242 if (arg_values[i * 2]->type != Value::Type::STRING) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800243 return ErrorAbort(state, kArgsParsingFailure, "%s(): sha-1 #%d is not string", name, i * 2);
244 }
Tao Bao511d7592018-06-19 15:56:49 -0700245 if (arg_values[i * 2 + 1]->type != Value::Type::BLOB) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800246 return ErrorAbort(state, kArgsParsingFailure, "%s(): patch #%d is not blob", name, i * 2 + 1);
247 }
248 }
249
250 std::vector<std::string> patch_sha_str;
251 std::vector<std::unique_ptr<Value>> patches;
252 for (int i = 0; i < patchcount; ++i) {
253 patch_sha_str.push_back(arg_values[i * 2]->data);
254 patches.push_back(std::move(arg_values[i * 2 + 1]));
255 }
256
257 int result = applypatch(source_filename.c_str(), target_filename.c_str(), target_sha1.c_str(),
258 target_size, patch_sha_str, patches, nullptr);
259
260 return StringValue(result == 0 ? "t" : "");
261}
262
263// apply_patch_check(filename, [sha1, ...])
264// Returns true if the contents of filename or the temporary copy in the cache partition (if
265// present) have a SHA-1 checksum equal to one of the given sha1 values. sha1 values are
Tao Bao0b58e9a2018-07-06 15:01:05 -0700266// specified as 40 hex digits.
Tianjie Xu5419ad32018-02-02 16:49:15 -0800267Value* ApplyPatchCheckFn(const char* name, State* state,
268 const std::vector<std::unique_ptr<Expr>>& argv) {
269 if (argv.size() < 1) {
270 return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 1 arg, got %zu", name,
271 argv.size());
272 }
273
274 std::vector<std::string> args;
275 if (!ReadArgs(state, argv, &args, 0, 1)) {
276 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
277 }
278 const std::string& filename = args[0];
279
280 std::vector<std::string> sha1s;
281 if (argv.size() > 1 && !ReadArgs(state, argv, &sha1s, 1, argv.size() - 1)) {
282 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
283 }
284 int result = applypatch_check(filename.c_str(), sha1s);
285
286 return StringValue(result == 0 ? "t" : "");
287}
288
Doug Zongker3d177d02010-07-01 09:18:44 -0700289// mount(fs_type, partition_type, location, mount_point)
Tao Bao0831d0b2016-11-03 23:25:04 -0700290// mount(fs_type, partition_type, location, mount_point, mount_options)
Tianjie Xuc4447322017-03-06 14:44:59 -0800291
Doug Zongker3d177d02010-07-01 09:18:44 -0700292// fs_type="ext4" partition_type="EMMC" location=device
Tianjie Xuc4447322017-03-06 14:44:59 -0800293Value* MountFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
294 if (argv.size() != 4 && argv.size() != 5) {
295 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 4-5 args, got %zu", name,
296 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800297 }
298
299 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800300 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800301 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
302 }
303 const std::string& fs_type = args[0];
304 const std::string& partition_type = args[1];
305 const std::string& location = args[2];
306 const std::string& mount_point = args[3];
307 std::string mount_options;
308
Tianjie Xuc4447322017-03-06 14:44:59 -0800309 if (argv.size() == 5) {
Tao Bao039f2da2016-11-22 16:29:50 -0800310 mount_options = args[4];
311 }
312
313 if (fs_type.empty()) {
314 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
315 }
316 if (partition_type.empty()) {
317 return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
318 name);
319 }
320 if (location.empty()) {
321 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
322 }
323 if (mount_point.empty()) {
324 return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
325 name);
326 }
327
328 {
329 char* secontext = nullptr;
330
331 if (sehandle) {
332 selabel_lookup(sehandle, &secontext, mount_point.c_str(), 0755);
333 setfscreatecon(secontext);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700334 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700335
Tao Bao039f2da2016-11-22 16:29:50 -0800336 mkdir(mount_point.c_str(), 0755);
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700337
Tao Bao039f2da2016-11-22 16:29:50 -0800338 if (secontext) {
339 freecon(secontext);
340 setfscreatecon(nullptr);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700341 }
Tao Bao039f2da2016-11-22 16:29:50 -0800342 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700343
Tao Bao039f2da2016-11-22 16:29:50 -0800344 if (mount(location.c_str(), mount_point.c_str(), fs_type.c_str(),
345 MS_NOATIME | MS_NODEV | MS_NODIRATIME, mount_options.c_str()) < 0) {
Tao Bao0bbc7642017-03-29 23:57:47 -0700346 uiPrintf(state, "%s: Failed to mount %s at %s: %s", name, location.c_str(), mount_point.c_str(),
347 strerror(errno));
Tao Bao039f2da2016-11-22 16:29:50 -0800348 return StringValue("");
349 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700350
Tao Bao039f2da2016-11-22 16:29:50 -0800351 return StringValue(mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700352}
353
Doug Zongker8edb00c2009-06-11 17:21:44 -0700354// is_mounted(mount_point)
Tianjie Xuc4447322017-03-06 14:44:59 -0800355Value* IsMountedFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
356 if (argv.size() != 1) {
357 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800358 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700359
Tao Bao039f2da2016-11-22 16:29:50 -0800360 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800361 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800362 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
363 }
364 const std::string& mount_point = args[0];
365 if (mount_point.empty()) {
366 return ErrorAbort(state, kArgsParsingFailure,
367 "mount_point argument to unmount() can't be empty");
368 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700369
Tao Bao039f2da2016-11-22 16:29:50 -0800370 scan_mounted_volumes();
371 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str());
372 if (vol == nullptr) {
373 return StringValue("");
374 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700375
Tao Bao039f2da2016-11-22 16:29:50 -0800376 return StringValue(mount_point);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700377}
378
Tianjie Xuc4447322017-03-06 14:44:59 -0800379Value* UnmountFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
380 if (argv.size() != 1) {
381 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800382 }
383 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800384 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800385 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
386 }
387 const std::string& mount_point = args[0];
388 if (mount_point.empty()) {
389 return ErrorAbort(state, kArgsParsingFailure,
390 "mount_point argument to unmount() can't be empty");
391 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700392
Tao Bao039f2da2016-11-22 16:29:50 -0800393 scan_mounted_volumes();
394 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str());
395 if (vol == nullptr) {
Tao Bao0bbc7642017-03-29 23:57:47 -0700396 uiPrintf(state, "Failed to unmount %s: No such volume", mount_point.c_str());
Tao Bao039f2da2016-11-22 16:29:50 -0800397 return nullptr;
398 } else {
399 int ret = unmount_mounted_volume(vol);
400 if (ret != 0) {
Tao Bao0bbc7642017-03-29 23:57:47 -0700401 uiPrintf(state, "Failed to unmount %s: %s", mount_point.c_str(), strerror(errno));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700402 }
Tao Bao039f2da2016-11-22 16:29:50 -0800403 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700404
Tao Bao039f2da2016-11-22 16:29:50 -0800405 return StringValue(mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700406}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700407
JP Abgrall37aedb32014-06-16 19:07:39 -0700408static int exec_cmd(const char* path, char* const argv[]) {
Tao Bao039f2da2016-11-22 16:29:50 -0800409 pid_t child;
410 if ((child = vfork()) == 0) {
411 execv(path, argv);
Tao Bao3da88012017-02-03 13:09:23 -0800412 _exit(EXIT_FAILURE);
Tao Bao039f2da2016-11-22 16:29:50 -0800413 }
414
415 int status;
416 waitpid(child, &status, 0);
417 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
418 LOG(ERROR) << path << " failed with status " << WEXITSTATUS(status);
419 }
420 return WEXITSTATUS(status);
JP Abgrall37aedb32014-06-16 19:07:39 -0700421}
422
Stephen Smalley779701d2012-02-09 14:13:23 -0500423// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700424//
Tao Bao039f2da2016-11-22 16:29:50 -0800425// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
426// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700427// if fs_size == 0, then make fs uses the entire partition.
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800428// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700429// 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 -0800430Value* FormatFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
431 if (argv.size() != 5) {
432 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 5 args, got %zu", name,
433 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800434 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500435
Tao Bao039f2da2016-11-22 16:29:50 -0800436 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800437 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800438 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
439 }
440 const std::string& fs_type = args[0];
441 const std::string& partition_type = args[1];
442 const std::string& location = args[2];
443 const std::string& fs_size = args[3];
444 const std::string& mount_point = args[4];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700445
Tao Bao039f2da2016-11-22 16:29:50 -0800446 if (fs_type.empty()) {
447 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
448 }
449 if (partition_type.empty()) {
450 return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
451 name);
452 }
453 if (location.empty()) {
454 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
455 }
456 if (mount_point.empty()) {
457 return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
458 name);
459 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700460
Tao Bao039f2da2016-11-22 16:29:50 -0800461 int64_t size;
462 if (!android::base::ParseInt(fs_size, &size)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800463 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800464 fs_size.c_str());
465 }
466
467 if (fs_type == "ext4") {
Jiyong Park69364fe2018-06-20 14:18:18 +0900468 const char* mke2fs_argv[] = { "/system/bin/mke2fs", "-t", "ext4", "-b", "4096",
469 location.c_str(), nullptr, nullptr };
Jin Qianded2dac2017-04-21 14:36:12 -0700470 std::string size_str;
471 if (size != 0) {
472 size_str = std::to_string(size / 4096LL);
473 mke2fs_argv[6] = size_str.c_str();
474 }
475
476 int status = exec_cmd(mke2fs_argv[0], const_cast<char**>(mke2fs_argv));
Tao Bao039f2da2016-11-22 16:29:50 -0800477 if (status != 0) {
Jin Qian502fd1c2017-11-02 11:58:12 -0700478 LOG(ERROR) << name << ": mke2fs failed (" << status << ") on " << location;
479 return StringValue("");
Jin Qianded2dac2017-04-21 14:36:12 -0700480 }
481
Jiyong Park69364fe2018-06-20 14:18:18 +0900482 const char* e2fsdroid_argv[] = { "/system/bin/e2fsdroid", "-e", "-a", mount_point.c_str(),
483 location.c_str(), nullptr };
Jin Qianded2dac2017-04-21 14:36:12 -0700484 status = exec_cmd(e2fsdroid_argv[0], const_cast<char**>(e2fsdroid_argv));
485 if (status != 0) {
486 LOG(ERROR) << name << ": e2fsdroid failed (" << status << ") on " << location;
Tao Bao039f2da2016-11-22 16:29:50 -0800487 return StringValue("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700488 }
Tao Bao039f2da2016-11-22 16:29:50 -0800489 return StringValue(location);
490 } else if (fs_type == "f2fs") {
491 if (size < 0) {
492 LOG(ERROR) << name << ": fs_size can't be negative for f2fs: " << fs_size;
493 return StringValue("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700494 }
Tao Bao039f2da2016-11-22 16:29:50 -0800495 std::string num_sectors = std::to_string(size / 512);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700496
Tao Bao039f2da2016-11-22 16:29:50 -0800497 const char* f2fs_path = "/sbin/mkfs.f2fs";
Jaegeuk Kim1a8bb0f2017-11-01 13:24:43 -0700498 const char* f2fs_argv[] = { "mkfs.f2fs",
499 "-d1",
500 "-f",
Jaegeuk Kim2e5dc842018-04-05 22:42:13 -0700501 "-O", "encrypt",
502 "-O", "quota",
503 "-O", "verity",
504 "-w", "512",
Jaegeuk Kim1a8bb0f2017-11-01 13:24:43 -0700505 location.c_str(),
506 (size < 512) ? nullptr : num_sectors.c_str(),
507 nullptr };
Tao Bao397a8132017-05-12 11:02:27 -0700508 int status = exec_cmd(f2fs_path, const_cast<char**>(f2fs_argv));
Tao Bao039f2da2016-11-22 16:29:50 -0800509 if (status != 0) {
510 LOG(ERROR) << name << ": mkfs.f2fs failed (" << status << ") on " << location;
511 return StringValue("");
512 }
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800513
514 const char* sload_argv[] = { "/sbin/sload.f2fs", "-t", mount_point.c_str(), location.c_str(),
515 nullptr };
516 status = exec_cmd(sload_argv[0], const_cast<char**>(sload_argv));
517 if (status != 0) {
518 LOG(ERROR) << name << ": sload.f2fs failed (" << status << ") on " << location;
519 return StringValue("");
520 }
521
Tao Bao039f2da2016-11-22 16:29:50 -0800522 return StringValue(location);
523 } else {
524 LOG(ERROR) << name << ": unsupported fs_type \"" << fs_type << "\" partition_type \""
525 << partition_type << "\"";
526 }
527
528 return nullptr;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700529}
530
Tianjie Xuc4447322017-03-06 14:44:59 -0800531Value* ShowProgressFn(const char* name, State* state,
532 const std::vector<std::unique_ptr<Expr>>& argv) {
533 if (argv.size() != 2) {
534 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
535 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800536 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700537
Tao Bao039f2da2016-11-22 16:29:50 -0800538 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800539 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800540 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
541 }
542 const std::string& frac_str = args[0];
543 const std::string& sec_str = args[1];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700544
Tao Bao039f2da2016-11-22 16:29:50 -0800545 double frac;
546 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800547 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800548 frac_str.c_str());
549 }
550 int sec;
551 if (!android::base::ParseInt(sec_str.c_str(), &sec)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800552 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800553 sec_str.c_str());
554 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700555
Tao Bao039f2da2016-11-22 16:29:50 -0800556 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
557 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700558
Tao Bao039f2da2016-11-22 16:29:50 -0800559 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700560}
561
Tianjie Xu5419ad32018-02-02 16:49:15 -0800562Value* SetProgressFn(const char* name, State* state,
563 const std::vector<std::unique_ptr<Expr>>& argv) {
Tianjie Xuc4447322017-03-06 14:44:59 -0800564 if (argv.size() != 1) {
565 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800566 }
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700567
Tao Bao039f2da2016-11-22 16:29:50 -0800568 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800569 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800570 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
571 }
572 const std::string& frac_str = args[0];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700573
Tao Bao039f2da2016-11-22 16:29:50 -0800574 double frac;
575 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800576 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800577 frac_str.c_str());
578 }
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700579
Tao Bao039f2da2016-11-22 16:29:50 -0800580 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
581 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700582
Tao Bao039f2da2016-11-22 16:29:50 -0800583 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700584}
585
Tianjie Xuc4447322017-03-06 14:44:59 -0800586Value* GetPropFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
587 if (argv.size() != 1) {
588 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800589 }
590 std::string key;
591 if (!Evaluate(state, argv[0], &key)) {
592 return nullptr;
593 }
594 std::string value = android::base::GetProperty(key, "");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700595
Tao Bao039f2da2016-11-22 16:29:50 -0800596 return StringValue(value);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700597}
598
Doug Zongker47cace92009-06-18 10:11:50 -0700599// file_getprop(file, key)
600//
601// interprets 'file' as a getprop-style file (key=value pairs, one
Tao Bao51d516e2016-11-03 14:49:01 -0700602// per line. # comment lines, blank lines, lines without '=' ignored),
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700603// and returns the value for 'key' (or "" if it isn't defined).
Tianjie Xu5419ad32018-02-02 16:49:15 -0800604Value* FileGetPropFn(const char* name, State* state,
605 const std::vector<std::unique_ptr<Expr>>& argv) {
Tianjie Xuc4447322017-03-06 14:44:59 -0800606 if (argv.size() != 2) {
607 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
608 argv.size());
Tao Bao358c2ec2016-11-28 11:48:43 -0800609 }
610
611 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800612 if (!ReadArgs(state, argv, &args)) {
Tao Bao358c2ec2016-11-28 11:48:43 -0800613 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
614 }
615 const std::string& filename = args[0];
616 const std::string& key = args[1];
617
Tianjie Xu22f11202018-08-27 10:50:31 -0700618 std::string buffer;
619 if (!android::base::ReadFileToString(filename, &buffer)) {
620 ErrorAbort(state, kFreadFailure, "%s: failed to read %s", name, filename.c_str());
Tao Bao358c2ec2016-11-28 11:48:43 -0800621 return nullptr;
622 }
623
Tao Bao358c2ec2016-11-28 11:48:43 -0800624 std::vector<std::string> lines = android::base::Split(buffer, "\n");
625 for (size_t i = 0; i < lines.size(); i++) {
626 std::string line = android::base::Trim(lines[i]);
627
628 // comment or blank line: skip to next line
629 if (line.empty() || line[0] == '#') {
630 continue;
631 }
632 size_t equal_pos = line.find('=');
633 if (equal_pos == std::string::npos) {
634 continue;
Tao Bao51d516e2016-11-03 14:49:01 -0700635 }
636
Tao Bao358c2ec2016-11-28 11:48:43 -0800637 // trim whitespace between key and '='
638 std::string str = android::base::Trim(line.substr(0, equal_pos));
Doug Zongker47cace92009-06-18 10:11:50 -0700639
Tao Bao358c2ec2016-11-28 11:48:43 -0800640 // not the key we're looking for
641 if (key != str) continue;
Doug Zongker47cace92009-06-18 10:11:50 -0700642
Tao Bao358c2ec2016-11-28 11:48:43 -0800643 return StringValue(android::base::Trim(line.substr(equal_pos + 1)));
644 }
Doug Zongker47cace92009-06-18 10:11:50 -0700645
Tao Bao358c2ec2016-11-28 11:48:43 -0800646 return StringValue("");
Doug Zongker47cace92009-06-18 10:11:50 -0700647}
648
Doug Zongker8edb00c2009-06-11 17:21:44 -0700649// apply_patch_space(bytes)
Tianjie Xu5419ad32018-02-02 16:49:15 -0800650Value* ApplyPatchSpaceFn(const char* name, State* state,
651 const std::vector<std::unique_ptr<Expr>>& argv) {
Tianjie Xuc4447322017-03-06 14:44:59 -0800652 if (argv.size() != 1) {
653 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 args, got %zu", name,
654 argv.size());
655 }
Tao Bao039f2da2016-11-22 16:29:50 -0800656 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800657 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800658 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
659 }
660 const std::string& bytes_str = args[0];
Doug Zongkerc4351c72010-02-22 14:46:32 -0800661
Tao Bao039f2da2016-11-22 16:29:50 -0800662 size_t bytes;
663 if (!android::base::ParseUint(bytes_str.c_str(), &bytes)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800664 return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count", name,
665 bytes_str.c_str());
Tao Bao039f2da2016-11-22 16:29:50 -0800666 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800667
Tianjie Xu99b73be2017-11-28 17:23:06 -0800668 // Skip the cache size check if the update is a retry.
Tao Bao5ee25662018-07-11 15:55:32 -0700669 if (state->is_retry || CheckAndFreeSpaceOnCache(bytes)) {
Tianjie Xu99b73be2017-11-28 17:23:06 -0800670 return StringValue("t");
671 }
672 return StringValue("");
Doug Zongkerc4351c72010-02-22 14:46:32 -0800673}
674
Tianjie Xuc4447322017-03-06 14:44:59 -0800675Value* WipeCacheFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
676 if (!argv.empty()) {
677 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %zu", name,
678 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800679 }
680 fprintf(static_cast<UpdaterInfo*>(state->cookie)->cmd_pipe, "wipe_cache\n");
681 return StringValue("t");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700682}
683
Tianjie Xuc4447322017-03-06 14:44:59 -0800684Value* RunProgramFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
685 if (argv.size() < 1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800686 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
687 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -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 }
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700693
Tianjie Xuc4447322017-03-06 14:44:59 -0800694 char* args2[argv.size() + 1];
695 for (size_t i = 0; i < argv.size(); i++) {
Tao Bao039f2da2016-11-22 16:29:50 -0800696 args2[i] = &args[i][0];
697 }
Tianjie Xuc4447322017-03-06 14:44:59 -0800698 args2[argv.size()] = nullptr;
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700699
Tianjie Xuc4447322017-03-06 14:44:59 -0800700 LOG(INFO) << "about to run program [" << args2[0] << "] with " << argv.size() << " args";
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700701
Tao Bao039f2da2016-11-22 16:29:50 -0800702 pid_t child = fork();
703 if (child == 0) {
704 execv(args2[0], args2);
705 PLOG(ERROR) << "run_program: execv failed";
Tao Bao3da88012017-02-03 13:09:23 -0800706 _exit(EXIT_FAILURE);
Tao Bao039f2da2016-11-22 16:29:50 -0800707 }
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700708
Tao Bao039f2da2016-11-22 16:29:50 -0800709 int status;
710 waitpid(child, &status, 0);
711 if (WIFEXITED(status)) {
712 if (WEXITSTATUS(status) != 0) {
713 LOG(ERROR) << "run_program: child exited with status " << WEXITSTATUS(status);
714 }
715 } else if (WIFSIGNALED(status)) {
716 LOG(ERROR) << "run_program: child terminated by signal " << WTERMSIG(status);
717 }
718
719 return StringValue(std::to_string(status));
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700720}
721
Tao Bao511d7592018-06-19 15:56:49 -0700722// read_file(filename)
Tao Baobafd6c72018-07-09 15:08:50 -0700723// Reads a local file 'filename' and returns its contents as a string Value.
Tianjie Xuc4447322017-03-06 14:44:59 -0800724Value* ReadFileFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
725 if (argv.size() != 1) {
726 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800727 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700728
Tao Bao039f2da2016-11-22 16:29:50 -0800729 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800730 if (!ReadArgs(state, argv, &args)) {
Tao Bao511d7592018-06-19 15:56:49 -0700731 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
Tao Bao039f2da2016-11-22 16:29:50 -0800732 }
733 const std::string& filename = args[0];
Doug Zongker512536a2010-02-17 16:11:44 -0800734
Tao Baobafd6c72018-07-09 15:08:50 -0700735 std::string contents;
736 if (android::base::ReadFileToString(filename, &contents)) {
737 return new Value(Value::Type::STRING, std::move(contents));
Tao Bao039f2da2016-11-22 16:29:50 -0800738 }
Tao Bao511d7592018-06-19 15:56:49 -0700739
740 // Leave it to caller to handle the failure.
Tao Baobafd6c72018-07-09 15:08:50 -0700741 PLOG(ERROR) << name << ": Failed to read " << filename;
Tao Bao511d7592018-06-19 15:56:49 -0700742 return StringValue("");
Doug Zongker512536a2010-02-17 16:11:44 -0800743}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700744
Tao Baod0f30882016-11-03 23:52:01 -0700745// write_value(value, filename)
746// Writes 'value' to 'filename'.
747// Example: write_value("960000", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq")
Tianjie Xuc4447322017-03-06 14:44:59 -0800748Value* WriteValueFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
749 if (argv.size() != 2) {
750 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
751 argv.size());
Tao Baod0f30882016-11-03 23:52:01 -0700752 }
753
754 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800755 if (!ReadArgs(state, argv, &args)) {
Tao Baod0f30882016-11-03 23:52:01 -0700756 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
757 }
758
759 const std::string& filename = args[1];
760 if (filename.empty()) {
761 return ErrorAbort(state, kArgsParsingFailure, "%s(): Filename cannot be empty", name);
762 }
763
764 const std::string& value = args[0];
765 if (!android::base::WriteStringToFile(value, filename)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800766 PLOG(ERROR) << name << ": Failed to write to \"" << filename << "\"";
Tao Baod0f30882016-11-03 23:52:01 -0700767 return StringValue("");
768 } else {
769 return StringValue("t");
770 }
771}
772
Doug Zongkerc87bab12013-11-25 13:53:25 -0800773// Immediately reboot the device. Recovery is not finished normally,
774// so if you reboot into recovery it will re-start applying the
775// current package (because nothing has cleared the copy of the
776// arguments stored in the BCB).
777//
778// The argument is the partition name passed to the android reboot
779// property. It can be "recovery" to boot from the recovery
780// partition, or "" (empty string) to boot from the regular boot
781// partition.
Tianjie Xuc4447322017-03-06 14:44:59 -0800782Value* RebootNowFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
783 if (argv.size() != 2) {
784 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
785 argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -0800786 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800787
Tao Baobedf5fc2016-11-18 12:01:26 -0800788 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800789 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800790 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
791 }
792 const std::string& filename = args[0];
793 const std::string& property = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -0800794
Tao Baobedf5fc2016-11-18 12:01:26 -0800795 // Zero out the 'command' field of the bootloader message. Leave the rest intact.
796 bootloader_message boot;
797 std::string err;
798 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800799 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800800 return StringValue("");
801 }
802 memset(boot.command, 0, sizeof(boot.command));
803 if (!write_bootloader_message_to(boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800804 LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800805 return StringValue("");
806 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800807
Tao Bao2c526392018-05-03 23:01:13 -0700808 reboot("reboot," + property);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800809
Tao Baobedf5fc2016-11-18 12:01:26 -0800810 sleep(5);
811 return ErrorAbort(state, kRebootFailure, "%s() failed to reboot", name);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800812}
813
814// Store a string value somewhere that future invocations of recovery
815// can access it. This value is called the "stage" and can be used to
816// drive packages that need to do reboots in the middle of
817// installation and keep track of where they are in the multi-stage
818// install.
819//
820// The first argument is the block device for the misc partition
821// ("/misc" in the fstab), which is where this value is stored. The
822// second argument is the string to store; it should not exceed 31
823// bytes.
Tianjie Xuc4447322017-03-06 14:44:59 -0800824Value* SetStageFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
825 if (argv.size() != 2) {
826 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
827 argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -0800828 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800829
Tao Baobedf5fc2016-11-18 12:01:26 -0800830 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800831 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800832 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
833 }
834 const std::string& filename = args[0];
835 const std::string& stagestr = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -0800836
Tao Baobedf5fc2016-11-18 12:01:26 -0800837 // Store this value in the misc partition, immediately after the
838 // bootloader message that the main recovery uses to save its
839 // arguments in case of the device restarting midway through
840 // package installation.
841 bootloader_message boot;
842 std::string err;
843 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800844 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800845 return StringValue("");
846 }
847 strlcpy(boot.stage, stagestr.c_str(), sizeof(boot.stage));
848 if (!write_bootloader_message_to(boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800849 LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800850 return StringValue("");
851 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800852
Tao Baobedf5fc2016-11-18 12:01:26 -0800853 return StringValue(filename);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800854}
855
856// Return the value most recently saved with SetStageFn. The argument
857// is the block device for the misc partition.
Tianjie Xuc4447322017-03-06 14:44:59 -0800858Value* GetStageFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
859 if (argv.size() != 1) {
860 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -0800861 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800862
Tao Baobedf5fc2016-11-18 12:01:26 -0800863 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800864 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800865 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
866 }
867 const std::string& filename = args[0];
Doug Zongkerc87bab12013-11-25 13:53:25 -0800868
Tao Baobedf5fc2016-11-18 12:01:26 -0800869 bootloader_message boot;
870 std::string err;
871 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800872 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800873 return StringValue("");
874 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800875
Tao Baobedf5fc2016-11-18 12:01:26 -0800876 return StringValue(boot.stage);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800877}
878
Tianjie Xuc4447322017-03-06 14:44:59 -0800879Value* WipeBlockDeviceFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
880 if (argv.size() != 2) {
881 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
882 argv.size());
Tao Bao358c2ec2016-11-28 11:48:43 -0800883 }
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800884
Tao Bao358c2ec2016-11-28 11:48:43 -0800885 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800886 if (!ReadArgs(state, argv, &args)) {
Tao Bao358c2ec2016-11-28 11:48:43 -0800887 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
888 }
889 const std::string& filename = args[0];
890 const std::string& len_str = args[1];
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800891
Tao Bao358c2ec2016-11-28 11:48:43 -0800892 size_t len;
893 if (!android::base::ParseUint(len_str.c_str(), &len)) {
894 return nullptr;
895 }
Tianjie Xu22f11202018-08-27 10:50:31 -0700896 android::base::unique_fd fd(open(filename.c_str(), O_WRONLY));
897 if (fd == -1) {
898 PLOG(ERROR) << "Failed to open " << filename;
899 return StringValue("");
900 }
901
Tao Bao358c2ec2016-11-28 11:48:43 -0800902 // The wipe_block_device function in ext4_utils returns 0 on success and 1
903 // for failure.
904 int status = wipe_block_device(fd, len);
905 return StringValue((status == 0) ? "t" : "");
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800906}
907
Tianjie Xuc4447322017-03-06 14:44:59 -0800908Value* EnableRebootFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
909 if (!argv.empty()) {
910 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %zu", name,
911 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800912 }
913 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
914 fprintf(ui->cmd_pipe, "enable_reboot\n");
915 return StringValue("t");
Doug Zongkerc704e062014-05-23 08:40:35 -0700916}
917
Tianjie Xuc4447322017-03-06 14:44:59 -0800918Value* Tune2FsFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
919 if (argv.empty()) {
920 return ErrorAbort(state, kArgsParsingFailure, "%s() expects args, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800921 }
Michael Rungeacf47db2014-11-21 00:12:28 -0800922
Tao Bao039f2da2016-11-22 16:29:50 -0800923 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800924 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800925 return ErrorAbort(state, kArgsParsingFailure, "%s() could not read args", name);
926 }
Michael Rungeacf47db2014-11-21 00:12:28 -0800927
Tianjie Xuc4447322017-03-06 14:44:59 -0800928 char* args2[argv.size() + 1];
Tao Bao039f2da2016-11-22 16:29:50 -0800929 // Tune2fs expects the program name as its args[0]
930 args2[0] = const_cast<char*>(name);
931 if (args2[0] == nullptr) {
932 return nullptr;
933 }
Tianjie Xuc4447322017-03-06 14:44:59 -0800934 for (size_t i = 0; i < argv.size(); ++i) {
Tao Bao039f2da2016-11-22 16:29:50 -0800935 args2[i + 1] = &args[i][0];
936 }
Michael Rungeacf47db2014-11-21 00:12:28 -0800937
Tao Bao039f2da2016-11-22 16:29:50 -0800938 // tune2fs changes the file system parameters on an ext2 file system; it
939 // returns 0 on success.
Tianjie Xuc4447322017-03-06 14:44:59 -0800940 int result = tune2fs_main(argv.size() + 1, args2);
Tao Bao039f2da2016-11-22 16:29:50 -0800941 if (result != 0) {
942 return ErrorAbort(state, kTune2FsFailure, "%s() returned error code %d", name, result);
943 }
944 return StringValue("t");
Michael Rungeacf47db2014-11-21 00:12:28 -0800945}
946
Doug Zongker9931f7f2009-06-10 14:11:53 -0700947void RegisterInstallFunctions() {
Tao Bao039f2da2016-11-22 16:29:50 -0800948 RegisterFunction("mount", MountFn);
949 RegisterFunction("is_mounted", IsMountedFn);
950 RegisterFunction("unmount", UnmountFn);
951 RegisterFunction("format", FormatFn);
952 RegisterFunction("show_progress", ShowProgressFn);
953 RegisterFunction("set_progress", SetProgressFn);
Tao Bao039f2da2016-11-22 16:29:50 -0800954 RegisterFunction("package_extract_file", PackageExtractFileFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700955
Tao Bao039f2da2016-11-22 16:29:50 -0800956 RegisterFunction("getprop", GetPropFn);
957 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700958
Tao Bao039f2da2016-11-22 16:29:50 -0800959 RegisterFunction("apply_patch", ApplyPatchFn);
960 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
961 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700962
Tao Bao039f2da2016-11-22 16:29:50 -0800963 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -0800964
Tao Bao039f2da2016-11-22 16:29:50 -0800965 RegisterFunction("read_file", ReadFileFn);
Tao Bao039f2da2016-11-22 16:29:50 -0800966 RegisterFunction("write_value", WriteValueFn);
Doug Zongker512536a2010-02-17 16:11:44 -0800967
Tao Bao039f2da2016-11-22 16:29:50 -0800968 RegisterFunction("wipe_cache", WipeCacheFn);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700969
Tao Bao039f2da2016-11-22 16:29:50 -0800970 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700971
Tao Bao039f2da2016-11-22 16:29:50 -0800972 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800973
Tao Bao039f2da2016-11-22 16:29:50 -0800974 RegisterFunction("reboot_now", RebootNowFn);
975 RegisterFunction("get_stage", GetStageFn);
976 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -0700977
Tao Bao039f2da2016-11-22 16:29:50 -0800978 RegisterFunction("enable_reboot", EnableRebootFn);
979 RegisterFunction("tune2fs", Tune2FsFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700980}