blob: 9d108e0ed1cb0ba25bfcf8f1b89ba3a3d66f18ae [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>
Tao Bao0d3f84f2016-12-28 15:09:20 -080049#include <applypatch/applypatch.h>
50#include <bootloader_message/bootloader_message.h>
Tao Baode40ba52016-10-05 23:17:01 -070051#include <ext4_utils/wipe.h>
Tao Bao361342c2016-02-08 11:15:50 -080052#include <openssl/sha.h>
Elliott Hughes4bbd5bf2016-04-01 18:24:39 -070053#include <selinux/label.h>
54#include <selinux/selinux.h>
Tao Bao09e468f2017-09-29 14:39:33 -070055#include <tune2fs.h>
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070056#include <ziparchive/zip_archive.h>
Tao Bao1107d962015-09-09 17:16:55 -070057
Doug Zongker9931f7f2009-06-10 14:11:53 -070058#include "edify/expr.h"
Tao Baod33b2f82017-09-28 21:29:11 -070059#include "otafault/ota_io.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
140 unique_fd fd(TEMP_FAILURE_RETRY(
141 ota_open(dest_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)));
142 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 }
155 if (ota_fsync(fd) == -1) {
156 PLOG(ERROR) << "fsync of \"" << dest_path << "\" failed";
157 success = false;
158 }
159 if (ota_close(fd) == -1) {
160 PLOG(ERROR) << "close of \"" << dest_path << "\" failed";
161 success = false;
162 }
163
164 return StringValue(success ? "t" : "");
165 } else {
166 // The one-argument version returns the contents of the file as the result.
167
168 std::vector<std::string> args;
169 if (!ReadArgs(state, argv, &args)) {
170 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name,
171 argv.size());
172 }
173 const std::string& zip_path = args[0];
174
175 ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip;
176 ZipString zip_string_path(zip_path.c_str());
177 ZipEntry entry;
178 if (FindEntry(za, zip_string_path, &entry) != 0) {
179 return ErrorAbort(state, kPackageExtractFileFailure, "%s(): no %s in package", name,
180 zip_path.c_str());
181 }
182
183 std::string buffer;
184 buffer.resize(entry.uncompressed_length);
185
186 int32_t ret =
187 ExtractToMemory(za, &entry, reinterpret_cast<uint8_t*>(&buffer[0]), buffer.size());
188 if (ret != 0) {
189 return ErrorAbort(state, kPackageExtractFileFailure,
190 "%s: Failed to extract entry \"%s\" (%zu bytes) to memory: %s", name,
191 zip_path.c_str(), buffer.size(), ErrorCodeString(ret));
192 }
193
194 return new Value(VAL_BLOB, buffer);
195 }
196}
197
198// apply_patch(src_file, tgt_file, tgt_sha1, tgt_size, patch1_sha1, patch1_blob, [...])
199// Applies a binary patch to the src_file to produce the tgt_file. If the desired target is the
200// same as the source, pass "-" for tgt_file. tgt_sha1 and tgt_size are the expected final SHA1
201// hash and size of the target file. The remaining arguments must come in pairs: a SHA1 hash (a
202// 40-character hex string) and a blob. The blob is the patch to be applied when the source
203// file's current contents have the given SHA1.
204//
205// The patching is done in a safe manner that guarantees the target file either has the desired
206// SHA1 hash and size, or it is untouched -- it will not be left in an unrecoverable intermediate
207// state. If the process is interrupted during patching, the target file may be in an intermediate
208// state; a copy exists in the cache partition so restarting the update can successfully update
209// the file.
210Value* ApplyPatchFn(const char* name, State* state,
211 const std::vector<std::unique_ptr<Expr>>& argv) {
212 if (argv.size() < 6 || (argv.size() % 2) == 1) {
213 return ErrorAbort(state, kArgsParsingFailure,
214 "%s(): expected at least 6 args and an "
215 "even number, got %zu",
216 name, argv.size());
217 }
218
219 std::vector<std::string> args;
220 if (!ReadArgs(state, argv, &args, 0, 4)) {
221 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
222 }
223 const std::string& source_filename = args[0];
224 const std::string& target_filename = args[1];
225 const std::string& target_sha1 = args[2];
226 const std::string& target_size_str = args[3];
227
228 size_t target_size;
229 if (!android::base::ParseUint(target_size_str.c_str(), &target_size)) {
230 return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count", name,
231 target_size_str.c_str());
232 }
233
234 int patchcount = (argv.size() - 4) / 2;
235 std::vector<std::unique_ptr<Value>> arg_values;
236 if (!ReadValueArgs(state, argv, &arg_values, 4, argv.size() - 4)) {
237 return nullptr;
238 }
239
240 for (int i = 0; i < patchcount; ++i) {
241 if (arg_values[i * 2]->type != VAL_STRING) {
242 return ErrorAbort(state, kArgsParsingFailure, "%s(): sha-1 #%d is not string", name, i * 2);
243 }
244 if (arg_values[i * 2 + 1]->type != VAL_BLOB) {
245 return ErrorAbort(state, kArgsParsingFailure, "%s(): patch #%d is not blob", name, i * 2 + 1);
246 }
247 }
248
249 std::vector<std::string> patch_sha_str;
250 std::vector<std::unique_ptr<Value>> patches;
251 for (int i = 0; i < patchcount; ++i) {
252 patch_sha_str.push_back(arg_values[i * 2]->data);
253 patches.push_back(std::move(arg_values[i * 2 + 1]));
254 }
255
256 int result = applypatch(source_filename.c_str(), target_filename.c_str(), target_sha1.c_str(),
257 target_size, patch_sha_str, patches, nullptr);
258
259 return StringValue(result == 0 ? "t" : "");
260}
261
262// apply_patch_check(filename, [sha1, ...])
263// Returns true if the contents of filename or the temporary copy in the cache partition (if
264// present) have a SHA-1 checksum equal to one of the given sha1 values. sha1 values are
Tao Bao0b58e9a2018-07-06 15:01:05 -0700265// specified as 40 hex digits.
Tianjie Xu5419ad32018-02-02 16:49:15 -0800266Value* ApplyPatchCheckFn(const char* name, State* state,
267 const std::vector<std::unique_ptr<Expr>>& argv) {
268 if (argv.size() < 1) {
269 return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 1 arg, got %zu", name,
270 argv.size());
271 }
272
273 std::vector<std::string> args;
274 if (!ReadArgs(state, argv, &args, 0, 1)) {
275 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
276 }
277 const std::string& filename = args[0];
278
279 std::vector<std::string> sha1s;
280 if (argv.size() > 1 && !ReadArgs(state, argv, &sha1s, 1, argv.size() - 1)) {
281 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
282 }
283 int result = applypatch_check(filename.c_str(), sha1s);
284
285 return StringValue(result == 0 ? "t" : "");
286}
287
Doug Zongker3d177d02010-07-01 09:18:44 -0700288// mount(fs_type, partition_type, location, mount_point)
Tao Bao0831d0b2016-11-03 23:25:04 -0700289// mount(fs_type, partition_type, location, mount_point, mount_options)
Tianjie Xuc4447322017-03-06 14:44:59 -0800290
Doug Zongker3d177d02010-07-01 09:18:44 -0700291// fs_type="ext4" partition_type="EMMC" location=device
Tianjie Xuc4447322017-03-06 14:44:59 -0800292Value* MountFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
293 if (argv.size() != 4 && argv.size() != 5) {
294 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 4-5 args, got %zu", name,
295 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800296 }
297
298 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800299 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800300 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
301 }
302 const std::string& fs_type = args[0];
303 const std::string& partition_type = args[1];
304 const std::string& location = args[2];
305 const std::string& mount_point = args[3];
306 std::string mount_options;
307
Tianjie Xuc4447322017-03-06 14:44:59 -0800308 if (argv.size() == 5) {
Tao Bao039f2da2016-11-22 16:29:50 -0800309 mount_options = args[4];
310 }
311
312 if (fs_type.empty()) {
313 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
314 }
315 if (partition_type.empty()) {
316 return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
317 name);
318 }
319 if (location.empty()) {
320 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
321 }
322 if (mount_point.empty()) {
323 return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
324 name);
325 }
326
327 {
328 char* secontext = nullptr;
329
330 if (sehandle) {
331 selabel_lookup(sehandle, &secontext, mount_point.c_str(), 0755);
332 setfscreatecon(secontext);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700333 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700334
Tao Bao039f2da2016-11-22 16:29:50 -0800335 mkdir(mount_point.c_str(), 0755);
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700336
Tao Bao039f2da2016-11-22 16:29:50 -0800337 if (secontext) {
338 freecon(secontext);
339 setfscreatecon(nullptr);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700340 }
Tao Bao039f2da2016-11-22 16:29:50 -0800341 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700342
Tao Bao039f2da2016-11-22 16:29:50 -0800343 if (mount(location.c_str(), mount_point.c_str(), fs_type.c_str(),
344 MS_NOATIME | MS_NODEV | MS_NODIRATIME, mount_options.c_str()) < 0) {
Tao Bao0bbc7642017-03-29 23:57:47 -0700345 uiPrintf(state, "%s: Failed to mount %s at %s: %s", name, location.c_str(), mount_point.c_str(),
346 strerror(errno));
Tao Bao039f2da2016-11-22 16:29:50 -0800347 return StringValue("");
348 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700349
Tao Bao039f2da2016-11-22 16:29:50 -0800350 return StringValue(mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700351}
352
Doug Zongker8edb00c2009-06-11 17:21:44 -0700353// is_mounted(mount_point)
Tianjie Xuc4447322017-03-06 14:44:59 -0800354Value* IsMountedFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
355 if (argv.size() != 1) {
356 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800357 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700358
Tao Bao039f2da2016-11-22 16:29:50 -0800359 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& mount_point = args[0];
364 if (mount_point.empty()) {
365 return ErrorAbort(state, kArgsParsingFailure,
366 "mount_point argument to unmount() can't be empty");
367 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700368
Tao Bao039f2da2016-11-22 16:29:50 -0800369 scan_mounted_volumes();
370 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str());
371 if (vol == nullptr) {
372 return StringValue("");
373 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700374
Tao Bao039f2da2016-11-22 16:29:50 -0800375 return StringValue(mount_point);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700376}
377
Tianjie Xuc4447322017-03-06 14:44:59 -0800378Value* UnmountFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
379 if (argv.size() != 1) {
380 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800381 }
382 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800383 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800384 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
385 }
386 const std::string& mount_point = args[0];
387 if (mount_point.empty()) {
388 return ErrorAbort(state, kArgsParsingFailure,
389 "mount_point argument to unmount() can't be empty");
390 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700391
Tao Bao039f2da2016-11-22 16:29:50 -0800392 scan_mounted_volumes();
393 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str());
394 if (vol == nullptr) {
Tao Bao0bbc7642017-03-29 23:57:47 -0700395 uiPrintf(state, "Failed to unmount %s: No such volume", mount_point.c_str());
Tao Bao039f2da2016-11-22 16:29:50 -0800396 return nullptr;
397 } else {
398 int ret = unmount_mounted_volume(vol);
399 if (ret != 0) {
Tao Bao0bbc7642017-03-29 23:57:47 -0700400 uiPrintf(state, "Failed to unmount %s: %s", mount_point.c_str(), strerror(errno));
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 return StringValue(mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700405}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700406
JP Abgrall37aedb32014-06-16 19:07:39 -0700407static int exec_cmd(const char* path, char* const argv[]) {
Tao Bao039f2da2016-11-22 16:29:50 -0800408 pid_t child;
409 if ((child = vfork()) == 0) {
410 execv(path, argv);
Tao Bao3da88012017-02-03 13:09:23 -0800411 _exit(EXIT_FAILURE);
Tao Bao039f2da2016-11-22 16:29:50 -0800412 }
413
414 int status;
415 waitpid(child, &status, 0);
416 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
417 LOG(ERROR) << path << " failed with status " << WEXITSTATUS(status);
418 }
419 return WEXITSTATUS(status);
JP Abgrall37aedb32014-06-16 19:07:39 -0700420}
421
Stephen Smalley779701d2012-02-09 14:13:23 -0500422// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700423//
Tao Bao039f2da2016-11-22 16:29:50 -0800424// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
425// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700426// if fs_size == 0, then make fs uses the entire partition.
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800427// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700428// 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 -0800429Value* FormatFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
430 if (argv.size() != 5) {
431 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 5 args, got %zu", name,
432 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800433 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500434
Tao Bao039f2da2016-11-22 16:29:50 -0800435 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800436 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800437 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
438 }
439 const std::string& fs_type = args[0];
440 const std::string& partition_type = args[1];
441 const std::string& location = args[2];
442 const std::string& fs_size = args[3];
443 const std::string& mount_point = args[4];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700444
Tao Bao039f2da2016-11-22 16:29:50 -0800445 if (fs_type.empty()) {
446 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
447 }
448 if (partition_type.empty()) {
449 return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
450 name);
451 }
452 if (location.empty()) {
453 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
454 }
455 if (mount_point.empty()) {
456 return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
457 name);
458 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700459
Tao Bao039f2da2016-11-22 16:29:50 -0800460 int64_t size;
461 if (!android::base::ParseInt(fs_size, &size)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800462 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800463 fs_size.c_str());
464 }
465
466 if (fs_type == "ext4") {
Jiyong Park69364fe2018-06-20 14:18:18 +0900467 const char* mke2fs_argv[] = { "/system/bin/mke2fs", "-t", "ext4", "-b", "4096",
468 location.c_str(), nullptr, nullptr };
Jin Qianded2dac2017-04-21 14:36:12 -0700469 std::string size_str;
470 if (size != 0) {
471 size_str = std::to_string(size / 4096LL);
472 mke2fs_argv[6] = size_str.c_str();
473 }
474
475 int status = exec_cmd(mke2fs_argv[0], const_cast<char**>(mke2fs_argv));
Tao Bao039f2da2016-11-22 16:29:50 -0800476 if (status != 0) {
Jin Qian502fd1c2017-11-02 11:58:12 -0700477 LOG(ERROR) << name << ": mke2fs failed (" << status << ") on " << location;
478 return StringValue("");
Jin Qianded2dac2017-04-21 14:36:12 -0700479 }
480
Jiyong Park69364fe2018-06-20 14:18:18 +0900481 const char* e2fsdroid_argv[] = { "/system/bin/e2fsdroid", "-e", "-a", mount_point.c_str(),
482 location.c_str(), nullptr };
Jin Qianded2dac2017-04-21 14:36:12 -0700483 status = exec_cmd(e2fsdroid_argv[0], const_cast<char**>(e2fsdroid_argv));
484 if (status != 0) {
485 LOG(ERROR) << name << ": e2fsdroid failed (" << status << ") on " << location;
Tao Bao039f2da2016-11-22 16:29:50 -0800486 return StringValue("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700487 }
Tao Bao039f2da2016-11-22 16:29:50 -0800488 return StringValue(location);
489 } else if (fs_type == "f2fs") {
490 if (size < 0) {
491 LOG(ERROR) << name << ": fs_size can't be negative for f2fs: " << fs_size;
492 return StringValue("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700493 }
Tao Bao039f2da2016-11-22 16:29:50 -0800494 std::string num_sectors = std::to_string(size / 512);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700495
Tao Bao039f2da2016-11-22 16:29:50 -0800496 const char* f2fs_path = "/sbin/mkfs.f2fs";
Jaegeuk Kim1a8bb0f2017-11-01 13:24:43 -0700497 const char* f2fs_argv[] = { "mkfs.f2fs",
498 "-d1",
499 "-f",
500 "-O",
501 "encrypt",
502 "-O",
503 "quota",
Jaegeuk Kimfc66f3f2018-04-04 21:14:01 -0700504 "-w",
505 "512",
Jaegeuk Kim1a8bb0f2017-11-01 13:24:43 -0700506 location.c_str(),
507 (size < 512) ? nullptr : num_sectors.c_str(),
508 nullptr };
Tao Bao397a8132017-05-12 11:02:27 -0700509 int status = exec_cmd(f2fs_path, const_cast<char**>(f2fs_argv));
Tao Bao039f2da2016-11-22 16:29:50 -0800510 if (status != 0) {
511 LOG(ERROR) << name << ": mkfs.f2fs failed (" << status << ") on " << location;
512 return StringValue("");
513 }
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800514
515 const char* sload_argv[] = { "/sbin/sload.f2fs", "-t", mount_point.c_str(), location.c_str(),
516 nullptr };
517 status = exec_cmd(sload_argv[0], const_cast<char**>(sload_argv));
518 if (status != 0) {
519 LOG(ERROR) << name << ": sload.f2fs failed (" << status << ") on " << location;
520 return StringValue("");
521 }
522
Tao Bao039f2da2016-11-22 16:29:50 -0800523 return StringValue(location);
524 } else {
525 LOG(ERROR) << name << ": unsupported fs_type \"" << fs_type << "\" partition_type \""
526 << partition_type << "\"";
527 }
528
529 return nullptr;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700530}
531
Tianjie Xuc4447322017-03-06 14:44:59 -0800532Value* ShowProgressFn(const char* name, State* state,
533 const std::vector<std::unique_ptr<Expr>>& argv) {
534 if (argv.size() != 2) {
535 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
536 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800537 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700538
Tao Bao039f2da2016-11-22 16:29:50 -0800539 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800540 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800541 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
542 }
543 const std::string& frac_str = args[0];
544 const std::string& sec_str = args[1];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700545
Tao Bao039f2da2016-11-22 16:29:50 -0800546 double frac;
547 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800548 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800549 frac_str.c_str());
550 }
551 int sec;
552 if (!android::base::ParseInt(sec_str.c_str(), &sec)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800553 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800554 sec_str.c_str());
555 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700556
Tao Bao039f2da2016-11-22 16:29:50 -0800557 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
558 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700559
Tao Bao039f2da2016-11-22 16:29:50 -0800560 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700561}
562
Tianjie Xu5419ad32018-02-02 16:49:15 -0800563Value* SetProgressFn(const char* name, State* state,
564 const std::vector<std::unique_ptr<Expr>>& argv) {
Tianjie Xuc4447322017-03-06 14:44:59 -0800565 if (argv.size() != 1) {
566 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800567 }
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700568
Tao Bao039f2da2016-11-22 16:29:50 -0800569 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800570 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800571 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
572 }
573 const std::string& frac_str = args[0];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700574
Tao Bao039f2da2016-11-22 16:29:50 -0800575 double frac;
576 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800577 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800578 frac_str.c_str());
579 }
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700580
Tao Bao039f2da2016-11-22 16:29:50 -0800581 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
582 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700583
Tao Bao039f2da2016-11-22 16:29:50 -0800584 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700585}
586
Tianjie Xuc4447322017-03-06 14:44:59 -0800587Value* GetPropFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
588 if (argv.size() != 1) {
589 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800590 }
591 std::string key;
592 if (!Evaluate(state, argv[0], &key)) {
593 return nullptr;
594 }
595 std::string value = android::base::GetProperty(key, "");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700596
Tao Bao039f2da2016-11-22 16:29:50 -0800597 return StringValue(value);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700598}
599
Doug Zongker47cace92009-06-18 10:11:50 -0700600// file_getprop(file, key)
601//
602// interprets 'file' as a getprop-style file (key=value pairs, one
Tao Bao51d516e2016-11-03 14:49:01 -0700603// per line. # comment lines, blank lines, lines without '=' ignored),
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700604// and returns the value for 'key' (or "" if it isn't defined).
Tianjie Xu5419ad32018-02-02 16:49:15 -0800605Value* FileGetPropFn(const char* name, State* state,
606 const std::vector<std::unique_ptr<Expr>>& argv) {
Tianjie Xuc4447322017-03-06 14:44:59 -0800607 if (argv.size() != 2) {
608 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
609 argv.size());
Tao Bao358c2ec2016-11-28 11:48:43 -0800610 }
611
612 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800613 if (!ReadArgs(state, argv, &args)) {
Tao Bao358c2ec2016-11-28 11:48:43 -0800614 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
615 }
616 const std::string& filename = args[0];
617 const std::string& key = args[1];
618
619 struct stat st;
620 if (stat(filename.c_str(), &st) < 0) {
621 return ErrorAbort(state, kFileGetPropFailure, "%s: failed to stat \"%s\": %s", name,
622 filename.c_str(), strerror(errno));
623 }
624
625 constexpr off_t MAX_FILE_GETPROP_SIZE = 65536;
626 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
627 return ErrorAbort(state, kFileGetPropFailure, "%s too large for %s (max %lld)",
628 filename.c_str(), name, static_cast<long long>(MAX_FILE_GETPROP_SIZE));
629 }
630
631 std::string buffer(st.st_size, '\0');
632 unique_file f(ota_fopen(filename.c_str(), "rb"));
633 if (f == nullptr) {
634 return ErrorAbort(state, kFileOpenFailure, "%s: failed to open %s: %s", name, filename.c_str(),
635 strerror(errno));
636 }
637
638 if (ota_fread(&buffer[0], 1, st.st_size, f.get()) != static_cast<size_t>(st.st_size)) {
639 ErrorAbort(state, kFreadFailure, "%s: failed to read %zu bytes from %s", name,
640 static_cast<size_t>(st.st_size), filename.c_str());
641 return nullptr;
642 }
643
644 ota_fclose(f);
645
646 std::vector<std::string> lines = android::base::Split(buffer, "\n");
647 for (size_t i = 0; i < lines.size(); i++) {
648 std::string line = android::base::Trim(lines[i]);
649
650 // comment or blank line: skip to next line
651 if (line.empty() || line[0] == '#') {
652 continue;
653 }
654 size_t equal_pos = line.find('=');
655 if (equal_pos == std::string::npos) {
656 continue;
Tao Bao51d516e2016-11-03 14:49:01 -0700657 }
658
Tao Bao358c2ec2016-11-28 11:48:43 -0800659 // trim whitespace between key and '='
660 std::string str = android::base::Trim(line.substr(0, equal_pos));
Doug Zongker47cace92009-06-18 10:11:50 -0700661
Tao Bao358c2ec2016-11-28 11:48:43 -0800662 // not the key we're looking for
663 if (key != str) continue;
Doug Zongker47cace92009-06-18 10:11:50 -0700664
Tao Bao358c2ec2016-11-28 11:48:43 -0800665 return StringValue(android::base::Trim(line.substr(equal_pos + 1)));
666 }
Doug Zongker47cace92009-06-18 10:11:50 -0700667
Tao Bao358c2ec2016-11-28 11:48:43 -0800668 return StringValue("");
Doug Zongker47cace92009-06-18 10:11:50 -0700669}
670
Doug Zongker8edb00c2009-06-11 17:21:44 -0700671// apply_patch_space(bytes)
Tianjie Xu5419ad32018-02-02 16:49:15 -0800672Value* ApplyPatchSpaceFn(const char* name, State* state,
673 const std::vector<std::unique_ptr<Expr>>& argv) {
Tianjie Xuc4447322017-03-06 14:44:59 -0800674 if (argv.size() != 1) {
675 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 args, got %zu", name,
676 argv.size());
677 }
Tao Bao039f2da2016-11-22 16:29:50 -0800678 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800679 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800680 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
681 }
682 const std::string& bytes_str = args[0];
Doug Zongkerc4351c72010-02-22 14:46:32 -0800683
Tao Bao039f2da2016-11-22 16:29:50 -0800684 size_t bytes;
685 if (!android::base::ParseUint(bytes_str.c_str(), &bytes)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800686 return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count", name,
687 bytes_str.c_str());
Tao Bao039f2da2016-11-22 16:29:50 -0800688 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800689
Tianjie Xu99b73be2017-11-28 17:23:06 -0800690 // Skip the cache size check if the update is a retry.
691 if (state->is_retry || CacheSizeCheck(bytes) == 0) {
692 return StringValue("t");
693 }
694 return StringValue("");
Doug Zongkerc4351c72010-02-22 14:46:32 -0800695}
696
Tianjie Xuc4447322017-03-06 14:44:59 -0800697Value* WipeCacheFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
698 if (!argv.empty()) {
699 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %zu", name,
700 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800701 }
702 fprintf(static_cast<UpdaterInfo*>(state->cookie)->cmd_pipe, "wipe_cache\n");
703 return StringValue("t");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700704}
705
Tianjie Xuc4447322017-03-06 14:44:59 -0800706Value* RunProgramFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
707 if (argv.size() < 1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800708 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
709 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700710
Tao Bao039f2da2016-11-22 16:29:50 -0800711 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800712 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800713 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
714 }
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700715
Tianjie Xuc4447322017-03-06 14:44:59 -0800716 char* args2[argv.size() + 1];
717 for (size_t i = 0; i < argv.size(); i++) {
Tao Bao039f2da2016-11-22 16:29:50 -0800718 args2[i] = &args[i][0];
719 }
Tianjie Xuc4447322017-03-06 14:44:59 -0800720 args2[argv.size()] = nullptr;
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700721
Tianjie Xuc4447322017-03-06 14:44:59 -0800722 LOG(INFO) << "about to run program [" << args2[0] << "] with " << argv.size() << " args";
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700723
Tao Bao039f2da2016-11-22 16:29:50 -0800724 pid_t child = fork();
725 if (child == 0) {
726 execv(args2[0], args2);
727 PLOG(ERROR) << "run_program: execv failed";
Tao Bao3da88012017-02-03 13:09:23 -0800728 _exit(EXIT_FAILURE);
Tao Bao039f2da2016-11-22 16:29:50 -0800729 }
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700730
Tao Bao039f2da2016-11-22 16:29:50 -0800731 int status;
732 waitpid(child, &status, 0);
733 if (WIFEXITED(status)) {
734 if (WEXITSTATUS(status) != 0) {
735 LOG(ERROR) << "run_program: child exited with status " << WEXITSTATUS(status);
736 }
737 } else if (WIFSIGNALED(status)) {
738 LOG(ERROR) << "run_program: child terminated by signal " << WTERMSIG(status);
739 }
740
741 return StringValue(std::to_string(status));
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700742}
743
Hristo Bojinovdb314d62010-08-02 10:29:49 -0700744// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -0800745// is actually a FileContents*).
Tianjie Xuc4447322017-03-06 14:44:59 -0800746Value* ReadFileFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
747 if (argv.size() != 1) {
748 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800749 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700750
Tao Bao039f2da2016-11-22 16:29:50 -0800751 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800752 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800753 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
754 }
755 const std::string& filename = args[0];
Doug Zongker512536a2010-02-17 16:11:44 -0800756
Tao Bao039f2da2016-11-22 16:29:50 -0800757 Value* v = new Value(VAL_INVALID, "");
Doug Zongker512536a2010-02-17 16:11:44 -0800758
Tao Bao039f2da2016-11-22 16:29:50 -0800759 FileContents fc;
760 if (LoadFileContents(filename.c_str(), &fc) == 0) {
761 v->type = VAL_BLOB;
762 v->data = std::string(fc.data.begin(), fc.data.end());
763 }
764 return v;
Doug Zongker512536a2010-02-17 16:11:44 -0800765}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700766
Tao Baod0f30882016-11-03 23:52:01 -0700767// write_value(value, filename)
768// Writes 'value' to 'filename'.
769// Example: write_value("960000", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq")
Tianjie Xuc4447322017-03-06 14:44:59 -0800770Value* WriteValueFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
771 if (argv.size() != 2) {
772 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
773 argv.size());
Tao Baod0f30882016-11-03 23:52:01 -0700774 }
775
776 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800777 if (!ReadArgs(state, argv, &args)) {
Tao Baod0f30882016-11-03 23:52:01 -0700778 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
779 }
780
781 const std::string& filename = args[1];
782 if (filename.empty()) {
783 return ErrorAbort(state, kArgsParsingFailure, "%s(): Filename cannot be empty", name);
784 }
785
786 const std::string& value = args[0];
787 if (!android::base::WriteStringToFile(value, filename)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800788 PLOG(ERROR) << name << ": Failed to write to \"" << filename << "\"";
Tao Baod0f30882016-11-03 23:52:01 -0700789 return StringValue("");
790 } else {
791 return StringValue("t");
792 }
793}
794
Doug Zongkerc87bab12013-11-25 13:53:25 -0800795// Immediately reboot the device. Recovery is not finished normally,
796// so if you reboot into recovery it will re-start applying the
797// current package (because nothing has cleared the copy of the
798// arguments stored in the BCB).
799//
800// The argument is the partition name passed to the android reboot
801// property. It can be "recovery" to boot from the recovery
802// partition, or "" (empty string) to boot from the regular boot
803// partition.
Tianjie Xuc4447322017-03-06 14:44:59 -0800804Value* RebootNowFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
805 if (argv.size() != 2) {
806 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
807 argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -0800808 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800809
Tao Baobedf5fc2016-11-18 12:01:26 -0800810 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800811 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800812 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
813 }
814 const std::string& filename = args[0];
815 const std::string& property = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -0800816
Tao Baobedf5fc2016-11-18 12:01:26 -0800817 // Zero out the 'command' field of the bootloader message. Leave the rest intact.
818 bootloader_message boot;
819 std::string err;
820 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800821 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800822 return StringValue("");
823 }
824 memset(boot.command, 0, sizeof(boot.command));
825 if (!write_bootloader_message_to(boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800826 LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800827 return StringValue("");
828 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800829
Tao Bao2c526392018-05-03 23:01:13 -0700830 reboot("reboot," + property);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800831
Tao Baobedf5fc2016-11-18 12:01:26 -0800832 sleep(5);
833 return ErrorAbort(state, kRebootFailure, "%s() failed to reboot", name);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800834}
835
836// Store a string value somewhere that future invocations of recovery
837// can access it. This value is called the "stage" and can be used to
838// drive packages that need to do reboots in the middle of
839// installation and keep track of where they are in the multi-stage
840// install.
841//
842// The first argument is the block device for the misc partition
843// ("/misc" in the fstab), which is where this value is stored. The
844// second argument is the string to store; it should not exceed 31
845// bytes.
Tianjie Xuc4447322017-03-06 14:44:59 -0800846Value* SetStageFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
847 if (argv.size() != 2) {
848 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
849 argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -0800850 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800851
Tao Baobedf5fc2016-11-18 12:01:26 -0800852 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800853 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800854 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
855 }
856 const std::string& filename = args[0];
857 const std::string& stagestr = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -0800858
Tao Baobedf5fc2016-11-18 12:01:26 -0800859 // Store this value in the misc partition, immediately after the
860 // bootloader message that the main recovery uses to save its
861 // arguments in case of the device restarting midway through
862 // package installation.
863 bootloader_message boot;
864 std::string err;
865 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800866 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800867 return StringValue("");
868 }
869 strlcpy(boot.stage, stagestr.c_str(), sizeof(boot.stage));
870 if (!write_bootloader_message_to(boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800871 LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800872 return StringValue("");
873 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800874
Tao Baobedf5fc2016-11-18 12:01:26 -0800875 return StringValue(filename);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800876}
877
878// Return the value most recently saved with SetStageFn. The argument
879// is the block device for the misc partition.
Tianjie Xuc4447322017-03-06 14:44:59 -0800880Value* GetStageFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
881 if (argv.size() != 1) {
882 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -0800883 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800884
Tao Baobedf5fc2016-11-18 12:01:26 -0800885 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800886 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800887 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
888 }
889 const std::string& filename = args[0];
Doug Zongkerc87bab12013-11-25 13:53:25 -0800890
Tao Baobedf5fc2016-11-18 12:01:26 -0800891 bootloader_message boot;
892 std::string err;
893 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800894 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800895 return StringValue("");
896 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800897
Tao Baobedf5fc2016-11-18 12:01:26 -0800898 return StringValue(boot.stage);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800899}
900
Tianjie Xuc4447322017-03-06 14:44:59 -0800901Value* WipeBlockDeviceFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
902 if (argv.size() != 2) {
903 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
904 argv.size());
Tao Bao358c2ec2016-11-28 11:48:43 -0800905 }
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800906
Tao Bao358c2ec2016-11-28 11:48:43 -0800907 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800908 if (!ReadArgs(state, argv, &args)) {
Tao Bao358c2ec2016-11-28 11:48:43 -0800909 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
910 }
911 const std::string& filename = args[0];
912 const std::string& len_str = args[1];
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800913
Tao Bao358c2ec2016-11-28 11:48:43 -0800914 size_t len;
915 if (!android::base::ParseUint(len_str.c_str(), &len)) {
916 return nullptr;
917 }
918 unique_fd fd(ota_open(filename.c_str(), O_WRONLY, 0644));
919 // The wipe_block_device function in ext4_utils returns 0 on success and 1
920 // for failure.
921 int status = wipe_block_device(fd, len);
922 return StringValue((status == 0) ? "t" : "");
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800923}
924
Tianjie Xuc4447322017-03-06 14:44:59 -0800925Value* EnableRebootFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
926 if (!argv.empty()) {
927 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %zu", name,
928 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800929 }
930 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
931 fprintf(ui->cmd_pipe, "enable_reboot\n");
932 return StringValue("t");
Doug Zongkerc704e062014-05-23 08:40:35 -0700933}
934
Tianjie Xuc4447322017-03-06 14:44:59 -0800935Value* Tune2FsFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
936 if (argv.empty()) {
937 return ErrorAbort(state, kArgsParsingFailure, "%s() expects args, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800938 }
Michael Rungeacf47db2014-11-21 00:12:28 -0800939
Tao Bao039f2da2016-11-22 16:29:50 -0800940 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800941 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800942 return ErrorAbort(state, kArgsParsingFailure, "%s() could not read args", name);
943 }
Michael Rungeacf47db2014-11-21 00:12:28 -0800944
Tianjie Xuc4447322017-03-06 14:44:59 -0800945 char* args2[argv.size() + 1];
Tao Bao039f2da2016-11-22 16:29:50 -0800946 // Tune2fs expects the program name as its args[0]
947 args2[0] = const_cast<char*>(name);
948 if (args2[0] == nullptr) {
949 return nullptr;
950 }
Tianjie Xuc4447322017-03-06 14:44:59 -0800951 for (size_t i = 0; i < argv.size(); ++i) {
Tao Bao039f2da2016-11-22 16:29:50 -0800952 args2[i + 1] = &args[i][0];
953 }
Michael Rungeacf47db2014-11-21 00:12:28 -0800954
Tao Bao039f2da2016-11-22 16:29:50 -0800955 // tune2fs changes the file system parameters on an ext2 file system; it
956 // returns 0 on success.
Tianjie Xuc4447322017-03-06 14:44:59 -0800957 int result = tune2fs_main(argv.size() + 1, args2);
Tao Bao039f2da2016-11-22 16:29:50 -0800958 if (result != 0) {
959 return ErrorAbort(state, kTune2FsFailure, "%s() returned error code %d", name, result);
960 }
961 return StringValue("t");
Michael Rungeacf47db2014-11-21 00:12:28 -0800962}
963
Doug Zongker9931f7f2009-06-10 14:11:53 -0700964void RegisterInstallFunctions() {
Tao Bao039f2da2016-11-22 16:29:50 -0800965 RegisterFunction("mount", MountFn);
966 RegisterFunction("is_mounted", IsMountedFn);
967 RegisterFunction("unmount", UnmountFn);
968 RegisterFunction("format", FormatFn);
969 RegisterFunction("show_progress", ShowProgressFn);
970 RegisterFunction("set_progress", SetProgressFn);
Tao Bao039f2da2016-11-22 16:29:50 -0800971 RegisterFunction("package_extract_file", PackageExtractFileFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700972
Tao Bao039f2da2016-11-22 16:29:50 -0800973 RegisterFunction("getprop", GetPropFn);
974 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700975
Tao Bao039f2da2016-11-22 16:29:50 -0800976 RegisterFunction("apply_patch", ApplyPatchFn);
977 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
978 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700979
Tao Bao039f2da2016-11-22 16:29:50 -0800980 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -0800981
Tao Bao039f2da2016-11-22 16:29:50 -0800982 RegisterFunction("read_file", ReadFileFn);
Tao Bao039f2da2016-11-22 16:29:50 -0800983 RegisterFunction("write_value", WriteValueFn);
Doug Zongker512536a2010-02-17 16:11:44 -0800984
Tao Bao039f2da2016-11-22 16:29:50 -0800985 RegisterFunction("wipe_cache", WipeCacheFn);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700986
Tao Bao039f2da2016-11-22 16:29:50 -0800987 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700988
Tao Bao039f2da2016-11-22 16:29:50 -0800989 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800990
Tao Bao039f2da2016-11-22 16:29:50 -0800991 RegisterFunction("reboot_now", RebootNowFn);
992 RegisterFunction("get_stage", GetStageFn);
993 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -0700994
Tao Bao039f2da2016-11-22 16:29:50 -0800995 RegisterFunction("enable_reboot", EnableRebootFn);
996 RegisterFunction("tune2fs", Tune2FsFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700997}