blob: 19ba365349388b581188b09049a615c1b559640c [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
Elliott Hughes4b166f02015-12-04 15:30:20 -080042#include <android-base/parseint.h>
Tianjie Xu5fe280a2016-10-17 18:15:20 -070043#include <android-base/parsedouble.h>
Elliott Hughescb220402016-09-23 15:30:55 -070044#include <android-base/properties.h>
Elliott Hughes4b166f02015-12-04 15:30:20 -080045#include <android-base/strings.h>
46#include <android-base/stringprintf.h>
Tao Bao0c7839a2016-10-10 15:48:37 -070047#include <cutils/android_reboot.h>
Tao Baode40ba52016-10-05 23:17:01 -070048#include <ext4_utils/make_ext4fs.h>
49#include <ext4_utils/wipe.h>
Tao Bao361342c2016-02-08 11:15:50 -080050#include <openssl/sha.h>
Elliott Hughes4bbd5bf2016-04-01 18:24:39 -070051#include <selinux/label.h>
52#include <selinux/selinux.h>
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070053#include <ziparchive/zip_archive.h>
Tao Bao1107d962015-09-09 17:16:55 -070054
Doug Zongkerc87bab12013-11-25 13:53:25 -080055#include "applypatch/applypatch.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070056#include "bootloader.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070057#include "edify/expr.h"
Tianjie Xu16255832016-04-30 11:49:59 -070058#include "error_code.h"
Elliott Hughes63a31922016-06-09 17:41:22 -070059#include "mounts.h"
Jed Estep39c1b5e2015-12-15 16:04:53 -080060#include "ota_io.h"
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070061#include "otautil/DirUtil.h"
62#include "otautil/ZipUtil.h"
Tao Bao361342c2016-02-08 11:15:50 -080063#include "print_sha1.h"
Michael Rungeacf47db2014-11-21 00:12:28 -080064#include "tune2fs.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) {
69 UpdaterInfo* ui = reinterpret_cast<UpdaterInfo*>(state->cookie);
Tao Baob6918c72015-05-19 17:02:16 -070070
Tao Bao1107d962015-09-09 17:16:55 -070071 // "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());
77 fprintf(ui->cmd_pipe, "ui_print\n");
78 }
79 }
80
81 // On the updater side, we need to dump the contents to stderr (which has
82 // been redirected to the log file). Because the recovery will only print
83 // the contents to screen when processing pipe command ui_print.
84 fprintf(stderr, "%s", buffer.c_str());
Michael Runged4a63422014-10-22 19:48:41 -070085}
86
Elliott Hughes83ce7552016-06-30 09:28:42 -070087void uiPrintf(State* _Nonnull state, const char* _Nonnull format, ...) {
Tao Bao1107d962015-09-09 17:16:55 -070088 std::string error_msg;
89
Michael Runged4a63422014-10-22 19:48:41 -070090 va_list ap;
91 va_start(ap, format);
Tao Bao1107d962015-09-09 17:16:55 -070092 android::base::StringAppendV(&error_msg, format, ap);
Michael Runged4a63422014-10-22 19:48:41 -070093 va_end(ap);
Tao Bao1107d962015-09-09 17:16:55 -070094
Michael Runged4a63422014-10-22 19:48:41 -070095 uiPrint(state, error_msg);
96}
97
Tao Bao0c7839a2016-10-10 15:48:37 -070098// Create all parent directories of name, if necessary.
99static int make_parents(char* name) {
100 char* p;
101 for (p = name + (strlen(name)-1); p > name; --p) {
102 if (*p != '/') continue;
103 *p = '\0';
104 if (make_parents(name) < 0) return -1;
105 int result = mkdir(name, 0700);
106 if (result == 0) printf("created [%s]\n", name);
107 *p = '/';
108 if (result == 0 || errno == EEXIST) {
109 // successfully created or already existed; we're done
110 return 0;
111 } else {
112 printf("failed to mkdir %s: %s\n", name, strerror(errno));
113 return -1;
114 }
115 }
116 return 0;
117}
118
Doug Zongker3d177d02010-07-01 09:18:44 -0700119// mount(fs_type, partition_type, location, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700120//
Doug Zongker3d177d02010-07-01 09:18:44 -0700121// fs_type="ext4" partition_type="EMMC" location=device
Doug Zongker512536a2010-02-17 16:11:44 -0800122Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
Michael Runge168f7772014-10-22 17:05:08 -0700123 if (argc != 4 && argc != 5) {
Tianjie Xu16255832016-04-30 11:49:59 -0700124 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 4-5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700125 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700126
127 std::vector<std::string> args;
128 if (!ReadArgs(state, argc, argv, &args)) {
129 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
130 }
131 const std::string& fs_type = args[0];
132 const std::string& partition_type = args[1];
133 const std::string& location = args[2];
134 const std::string& mount_point = args[3];
135 std::string mount_options;
136
Michael Runge168f7772014-10-22 17:05:08 -0700137 if (argc == 5) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700138 mount_options = args[4];
Doug Zongker9931f7f2009-06-10 14:11:53 -0700139 }
140
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700141 if (fs_type.empty()) {
142 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty",
143 name);
Doug Zongker3d177d02010-07-01 09:18:44 -0700144 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700145 if (partition_type.empty()) {
146 return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
147 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700148 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700149 if (location.empty()) {
150 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty",
151 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700152 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700153 if (mount_point.empty()) {
154 return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
155 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700156 }
157
Tao Baoba9a42a2015-06-23 23:23:33 -0700158 {
159 char *secontext = NULL;
Stephen Smalley779701d2012-02-09 14:13:23 -0500160
Tao Baoba9a42a2015-06-23 23:23:33 -0700161 if (sehandle) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700162 selabel_lookup(sehandle, &secontext, mount_point.c_str(), 0755);
Tao Baoba9a42a2015-06-23 23:23:33 -0700163 setfscreatecon(secontext);
164 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500165
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700166 mkdir(mount_point.c_str(), 0755);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700167
Tao Baoba9a42a2015-06-23 23:23:33 -0700168 if (secontext) {
169 freecon(secontext);
170 setfscreatecon(NULL);
171 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500172 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500173
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700174 if (mount(location.c_str(), mount_point.c_str(), fs_type.c_str(),
175 MS_NOATIME | MS_NODEV | MS_NODIRATIME, mount_options.c_str()) < 0) {
Elliott Hughes63a31922016-06-09 17:41:22 -0700176 uiPrintf(state, "%s: failed to mount %s at %s: %s\n",
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700177 name, location.c_str(), mount_point.c_str(), strerror(errno));
178 return StringValue("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700179 }
180
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700181 return StringValue(mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700182}
183
Doug Zongker8edb00c2009-06-11 17:21:44 -0700184
185// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800186Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700187 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700188 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700189 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700190
191 std::vector<std::string> args;
192 if (!ReadArgs(state, argc, argv, &args)) {
193 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700194 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700195 const std::string& mount_point = args[0];
196 if (mount_point.empty()) {
197 return ErrorAbort(state, kArgsParsingFailure,
198 "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700199 }
200
201 scan_mounted_volumes();
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700202 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str());
203 if (vol == nullptr) {
204 return StringValue("");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700205 }
206
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700207 return StringValue(mount_point);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700208}
209
210
Doug Zongker512536a2010-02-17 16:11:44 -0800211Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700212 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700213 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700214 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700215 std::vector<std::string> args;
216 if (!ReadArgs(state, argc, argv, &args)) {
217 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700218 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700219 const std::string& mount_point = args[0];
220 if (mount_point.empty()) {
221 return ErrorAbort(state, kArgsParsingFailure,
222 "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700223 }
224
225 scan_mounted_volumes();
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700226 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str());
227 if (vol == nullptr) {
228 uiPrintf(state, "unmount of %s failed; no such volume\n", mount_point.c_str());
229 return nullptr;
230 } else {
231 int ret = unmount_mounted_volume(vol);
232 if (ret != 0) {
233 uiPrintf(state, "unmount of %s failed (%d): %s\n",
234 mount_point.c_str(), ret, strerror(errno));
Michael Runge5ddf4292014-10-24 14:14:41 -0700235 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700236 }
237
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700238 return StringValue(mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700239}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700240
JP Abgrall37aedb32014-06-16 19:07:39 -0700241static int exec_cmd(const char* path, char* const argv[]) {
242 int status;
243 pid_t child;
244 if ((child = vfork()) == 0) {
245 execv(path, argv);
246 _exit(-1);
247 }
248 waitpid(child, &status, 0);
249 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
250 printf("%s failed with status %d\n", path, WEXITSTATUS(status));
251 }
252 return WEXITSTATUS(status);
253}
254
Doug Zongker8edb00c2009-06-11 17:21:44 -0700255
Stephen Smalley779701d2012-02-09 14:13:23 -0500256// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700257//
Stephen Smalley779701d2012-02-09 14:13:23 -0500258// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700259// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
260// if fs_size == 0, then make fs uses the entire partition.
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800261// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700262// if fs_size < 0, then reserve that many bytes at the end of the partition (not for "f2fs")
Doug Zongker512536a2010-02-17 16:11:44 -0800263Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Stephen Smalley516e4e22012-04-03 13:35:11 -0400264 if (argc != 5) {
Tianjie Xu16255832016-04-30 11:49:59 -0700265 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700266 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500267
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700268 std::vector<std::string> args;
269 if (!ReadArgs(state, argc, argv, &args)) {
270 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
271 }
272 const std::string& fs_type = args[0];
273 const std::string& partition_type = args[1];
274 const std::string& location = args[2];
275 const std::string& fs_size = args[3];
276 const std::string& mount_point = args[4];
277
278 if (fs_type.empty()) {
279 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty",
280 name);
281 }
282 if (partition_type.empty()) {
283 return ErrorAbort(state, kArgsParsingFailure,
284 "partition_type argument to %s() can't be empty", name);
285 }
286 if (location.empty()) {
287 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty",
288 name);
289 }
290 if (mount_point.empty()) {
291 return ErrorAbort(state, kArgsParsingFailure,
292 "mount_point argument to %s() can't be empty", name);
Stephen Smalley779701d2012-02-09 14:13:23 -0500293 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700294
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700295 int64_t size;
296 if (!android::base::ParseInt(fs_size.c_str(), &size)) {
297 return ErrorAbort(state, kArgsParsingFailure,
298 "%s: failed to parse int in %s\n", name, fs_size.c_str());
Doug Zongker9931f7f2009-06-10 14:11:53 -0700299 }
300
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700301 if (fs_type == "ext4") {
302 int status = make_ext4fs(location.c_str(), size, mount_point.c_str(), sehandle);
Doug Zongker3d177d02010-07-01 09:18:44 -0700303 if (status != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700304 printf("%s: make_ext4fs failed (%d) on %s",
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700305 name, status, location.c_str());
306 return StringValue("");
Doug Zongker3d177d02010-07-01 09:18:44 -0700307 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700308 return StringValue(location);
309 } else if (fs_type == "f2fs") {
310 if (size < 0) {
311 printf("fs_size can't be negative for f2fs: %s", fs_size.c_str());
312 return StringValue("");
JP Abgrall37aedb32014-06-16 19:07:39 -0700313 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700314 std::string num_sectors = std::to_string(size / 512);
315
JP Abgrall37aedb32014-06-16 19:07:39 -0700316 const char *f2fs_path = "/sbin/mkfs.f2fs";
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700317 const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", location.c_str(),
318 num_sectors.c_str(), nullptr};
JP Abgrall37aedb32014-06-16 19:07:39 -0700319 int status = exec_cmd(f2fs_path, (char* const*)f2fs_argv);
JP Abgrall37aedb32014-06-16 19:07:39 -0700320 if (status != 0) {
321 printf("%s: mkfs.f2fs failed (%d) on %s",
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700322 name, status, location.c_str());
323 return StringValue("");
JP Abgrall37aedb32014-06-16 19:07:39 -0700324 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700325 return StringValue(location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700326 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700327 printf("%s: unsupported fs_type \"%s\" partition_type \"%s\"",
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700328 name, fs_type.c_str(), partition_type.c_str());
Doug Zongker9931f7f2009-06-10 14:11:53 -0700329 }
330
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700331 return nullptr;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700332}
333
Michael Rungece7ca712013-11-06 17:42:20 -0800334Value* RenameFn(const char* name, State* state, int argc, Expr* argv[]) {
Michael Rungece7ca712013-11-06 17:42:20 -0800335 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700336 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Michael Rungece7ca712013-11-06 17:42:20 -0800337 }
338
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700339 std::vector<std::string> args;
340 if (!ReadArgs(state, argc, argv, &args)) {
341 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
342 }
343 const std::string& src_name = args[0];
344 std::string& dst_name = args[1];
Michael Rungece7ca712013-11-06 17:42:20 -0800345
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700346 if (src_name.empty()) {
347 return ErrorAbort(state, kArgsParsingFailure, "src_name argument to %s() can't be empty",
348 name);
Michael Rungece7ca712013-11-06 17:42:20 -0800349 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700350 if (dst_name.empty()) {
351 return ErrorAbort(state, kArgsParsingFailure, "dst_name argument to %s() can't be empty",
352 name);
Michael Rungece7ca712013-11-06 17:42:20 -0800353 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700354 if (make_parents(&dst_name[0]) != 0) {
355 return ErrorAbort(state, kFileRenameFailure, "Creating parent of %s failed, error %s",
356 dst_name.c_str(), strerror(errno));
357 } else if (access(dst_name.c_str(), F_OK) == 0 && access(src_name.c_str(), F_OK) != 0) {
Michael Runge2f0ef732014-10-22 14:28:23 -0700358 // File was already moved
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700359 return StringValue(dst_name);
360 } else if (rename(src_name.c_str(), dst_name.c_str()) != 0) {
361 return ErrorAbort(state, kFileRenameFailure, "Rename of %s to %s failed, error %s",
362 src_name.c_str(), dst_name.c_str(), strerror(errno));
Michael Rungece7ca712013-11-06 17:42:20 -0800363 }
364
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700365 return StringValue(dst_name);
Michael Rungece7ca712013-11-06 17:42:20 -0800366}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700367
Doug Zongker512536a2010-02-17 16:11:44 -0800368Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Tianjie Xuaced5d92016-10-12 10:55:04 -0700369 std::vector<std::string> paths;
Tao Baoba9a42a2015-06-23 23:23:33 -0700370 for (int i = 0; i < argc; ++i) {
Tianjie Xuaced5d92016-10-12 10:55:04 -0700371 if (!Evaluate(state, argv[i], &paths[i])) {
372 return nullptr;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700373 }
374 }
375
376 bool recursive = (strcmp(name, "delete_recursive") == 0);
377
378 int success = 0;
Tao Baoba9a42a2015-06-23 23:23:33 -0700379 for (int i = 0; i < argc; ++i) {
Tianjie Xuaced5d92016-10-12 10:55:04 -0700380 if ((recursive ? dirUnlinkHierarchy(paths[i].c_str()) : unlink(paths[i].c_str())) == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700381 ++success;
Tianjie Xuaced5d92016-10-12 10:55:04 -0700382 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700383 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700384
Tianjie Xuaced5d92016-10-12 10:55:04 -0700385 return StringValue(android::base::StringPrintf("%d", success));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700386}
387
Doug Zongker8edb00c2009-06-11 17:21:44 -0700388
Doug Zongker512536a2010-02-17 16:11:44 -0800389Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700390 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700391 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700392 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700393
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700394 std::vector<std::string> args;
395 if (!ReadArgs(state, argc, argv, &args)) {
396 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
397 }
398 const std::string& frac_str = args[0];
399 const std::string& sec_str = args[1];
400
401 double frac;
402 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
403 return ErrorAbort(state, kArgsParsingFailure,
404 "%s: failed to parse double in %s\n", name, frac_str.c_str());
405 }
Tao Baob15fd222015-09-24 11:10:51 -0700406 int sec;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700407 if (!android::base::ParseInt(sec_str.c_str(), &sec)) {
408 return ErrorAbort(state, kArgsParsingFailure,
409 "%s: failed to parse int in %s\n", name, sec_str.c_str());
410 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700411
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700412 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700413 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
414
Doug Zongker512536a2010-02-17 16:11:44 -0800415 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700416}
417
Doug Zongker512536a2010-02-17 16:11:44 -0800418Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700419 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700420 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700421 }
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700422
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700423 std::vector<std::string> args;
424 if (!ReadArgs(state, 1, argv, &args)) {
425 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
426 }
427 const std::string& frac_str = args[0];
428
429 double frac;
430 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
431 return ErrorAbort(state, kArgsParsingFailure,
432 "%s: failed to parse double in %s\n", name, frac_str.c_str());
433 }
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700434
435 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
436 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
437
Doug Zongker512536a2010-02-17 16:11:44 -0800438 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700439}
440
Doug Zongker8edb00c2009-06-11 17:21:44 -0700441// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800442Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700443 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700444 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700445 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700446 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700447
448 std::vector<std::string> args;
449 if (!ReadArgs(state, 2, argv, &args)) {
450 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
451 }
452 const std::string& zip_path = args[0];
453 const std::string& dest_path = args[1];
Doug Zongker9931f7f2009-06-10 14:11:53 -0700454
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700455 ZipArchiveHandle za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700456
457 // To create a consistent system image, never use the clock for timestamps.
458 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
459
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700460 bool success = ExtractPackageRecursive(za, zip_path, dest_path, &timestamp, sehandle);
461
Tianjie Xuaced5d92016-10-12 10:55:04 -0700462 return StringValue(success ? "t" : "");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700463}
464
Doug Zongker8edb00c2009-06-11 17:21:44 -0700465
466// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800467// or
468// package_extract_file(package_path)
469// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800470// function (the char* returned is actually a FileContents*).
471Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700472 int argc, Expr* argv[]) {
Doug Zongker5f875bf2014-08-22 14:53:43 -0700473 if (argc < 1 || argc > 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700474 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 or 2 args, got %d",
Doug Zongker6aece332010-02-01 14:40:12 -0800475 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700476 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700477 bool success = false;
Doug Zongker43772d22014-06-09 14:13:19 -0700478
Doug Zongker5f875bf2014-08-22 14:53:43 -0700479 if (argc == 2) {
480 // The two-argument version extracts to a file.
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800481
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700482 ZipArchiveHandle za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700483
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700484 std::vector<std::string> args;
485 if (!ReadArgs(state, 2, argv, &args)) {
486 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %d args", name,
487 argc);
488 }
489 const std::string& zip_path = args[0];
490 const std::string& dest_path = args[1];
Doug Zongker6aece332010-02-01 14:40:12 -0800491
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700492 ZipString zip_string_path(zip_path.c_str());
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700493 ZipEntry entry;
494 if (FindEntry(za, zip_string_path, &entry) != 0) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700495 printf("%s: no %s in package\n", name, zip_path.c_str());
496 return StringValue("");
Doug Zongker6aece332010-02-01 14:40:12 -0800497 }
498
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700499 int fd = TEMP_FAILURE_RETRY(ota_open(dest_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC,
500 S_IRUSR | S_IWUSR));
501 if (fd == -1) {
502 printf("%s: can't open %s for write: %s\n", name, dest_path.c_str(), strerror(errno));
503 return StringValue("");
504 }
505 success = ExtractEntryToFile(za, &entry, fd);
506 if (ota_fsync(fd) == -1) {
507 printf("fsync of \"%s\" failed: %s\n", dest_path.c_str(), strerror(errno));
508 success = false;
509 }
510 if (ota_close(fd) == -1) {
511 printf("close of \"%s\" failed: %s\n", dest_path.c_str(), strerror(errno));
512 success = false;
Doug Zongker6aece332010-02-01 14:40:12 -0800513 }
Doug Zongker6aece332010-02-01 14:40:12 -0800514
Tianjie Xuaced5d92016-10-12 10:55:04 -0700515 return StringValue(success ? "t" : "");
Doug Zongker6aece332010-02-01 14:40:12 -0800516 } else {
517 // The one-argument version returns the contents of the file
518 // as the result.
519
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700520 std::vector<std::string> args;
521 if (!ReadArgs(state, 1, argv, &args)) {
522 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %d args", name,
523 argc);
524 }
525 const std::string& zip_path = args[0];
Yabin Cui64be2132016-02-03 18:16:02 -0800526
Tianjie Xuaced5d92016-10-12 10:55:04 -0700527 Value* v = new Value(VAL_INVALID, "");
Doug Zongker6aece332010-02-01 14:40:12 -0800528
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700529 ZipArchiveHandle za = ((UpdaterInfo*)(state->cookie))->package_zip;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700530 ZipString zip_string_path(zip_path.c_str());
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700531 ZipEntry entry;
532 if (FindEntry(za, zip_string_path, &entry) != 0) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700533 printf("%s: no %s in package\n", name, zip_path.c_str());
534 return v;
Doug Zongker6aece332010-02-01 14:40:12 -0800535 }
536
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700537 v->data.resize(entry.uncompressed_length);
538 if (ExtractToMemory(za, &entry, reinterpret_cast<uint8_t*>(&v->data[0]),
539 v->data.size()) != 0) {
540 printf("%s: faled to extract %zu bytes to memory\n", name, v->data.size());
541 } else {
542 success = true;
543 }
Doug Zongker6aece332010-02-01 14:40:12 -0800544
Doug Zongker6aece332010-02-01 14:40:12 -0800545 if (!success) {
Tianjie Xuaced5d92016-10-12 10:55:04 -0700546 v->data.clear();
547 } else {
548 v->type = VAL_BLOB;
Doug Zongker6aece332010-02-01 14:40:12 -0800549 }
Doug Zongker512536a2010-02-17 16:11:44 -0800550 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700551 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700552}
553
Doug Zongker9931f7f2009-06-10 14:11:53 -0700554// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700555// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800556Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700557 if (argc == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700558 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700559 }
Tianjie Xuaced5d92016-10-12 10:55:04 -0700560 std::string target;
561 if (!Evaluate(state, argv[0], &target)) {
562 return nullptr;
563 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700564
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700565 std::vector<std::string> srcs;
566 if (!ReadArgs(state, argc-1, argv+1, &srcs)) {
567 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700568 }
569
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700570 int bad = 0;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700571 for (int i = 0; i < argc-1; ++i) {
572 if (unlink(srcs[i].c_str()) < 0) {
Doug Zongker60babf82009-09-18 15:11:24 -0700573 if (errno != ENOENT) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700574 printf("%s: failed to remove %s: %s\n",
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700575 name, srcs[i].c_str(), strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700576 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700577 }
578 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700579 if (make_parents(&srcs[i][0])) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700580 printf("%s: failed to symlink %s to %s: making parents failed\n",
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700581 name, srcs[i].c_str(), target.c_str());
Doug Zongkera23075f2012-08-06 16:19:09 -0700582 ++bad;
583 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700584 if (symlink(target.c_str(), srcs[i].c_str()) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700585 printf("%s: failed to symlink %s to %s: %s\n",
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700586 name, srcs[i].c_str(), target.c_str(), strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700587 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700588 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700589 }
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700590 if (bad) {
Tianjie Xu16255832016-04-30 11:49:59 -0700591 return ErrorAbort(state, kSymlinkFailure, "%s: some symlinks failed", name);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700592 }
Tianjie Xuaced5d92016-10-12 10:55:04 -0700593 return StringValue("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700594}
595
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700596struct perm_parsed_args {
597 bool has_uid;
598 uid_t uid;
599 bool has_gid;
600 gid_t gid;
601 bool has_mode;
602 mode_t mode;
603 bool has_fmode;
604 mode_t fmode;
605 bool has_dmode;
606 mode_t dmode;
607 bool has_selabel;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700608 const char* selabel;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700609 bool has_capabilities;
610 uint64_t capabilities;
611};
612
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700613static struct perm_parsed_args ParsePermArgs(State * state, int argc,
614 const std::vector<std::string>& args) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700615 int i;
616 struct perm_parsed_args parsed;
617 int bad = 0;
618 static int max_warnings = 20;
619
620 memset(&parsed, 0, sizeof(parsed));
621
622 for (i = 1; i < argc; i += 2) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700623 if (args[i] == "uid") {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700624 int64_t uid;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700625 if (sscanf(args[i+1].c_str(), "%" SCNd64, &uid) == 1) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700626 parsed.uid = uid;
627 parsed.has_uid = true;
628 } else {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700629 uiPrintf(state, "ParsePermArgs: invalid UID \"%s\"\n", args[i + 1].c_str());
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700630 bad++;
631 }
632 continue;
633 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700634 if (args[i] == "gid") {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700635 int64_t gid;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700636 if (sscanf(args[i+1].c_str(), "%" SCNd64, &gid) == 1) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700637 parsed.gid = gid;
638 parsed.has_gid = true;
639 } else {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700640 uiPrintf(state, "ParsePermArgs: invalid GID \"%s\"\n", args[i + 1].c_str());
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700641 bad++;
642 }
643 continue;
644 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700645 if (args[i] == "mode") {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700646 int32_t mode;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700647 if (sscanf(args[i+1].c_str(), "%" SCNi32, &mode) == 1) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700648 parsed.mode = mode;
649 parsed.has_mode = true;
650 } else {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700651 uiPrintf(state, "ParsePermArgs: invalid mode \"%s\"\n", args[i + 1].c_str());
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700652 bad++;
653 }
654 continue;
655 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700656 if (args[i] == "dmode") {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700657 int32_t mode;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700658 if (sscanf(args[i+1].c_str(), "%" SCNi32, &mode) == 1) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700659 parsed.dmode = mode;
660 parsed.has_dmode = true;
661 } else {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700662 uiPrintf(state, "ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1].c_str());
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700663 bad++;
664 }
665 continue;
666 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700667 if (args[i] == "fmode") {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700668 int32_t mode;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700669 if (sscanf(args[i+1].c_str(), "%" SCNi32, &mode) == 1) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700670 parsed.fmode = mode;
671 parsed.has_fmode = true;
672 } else {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700673 uiPrintf(state, "ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1].c_str());
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700674 bad++;
675 }
676 continue;
677 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700678 if (args[i] == "capabilities") {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700679 int64_t capabilities;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700680 if (sscanf(args[i+1].c_str(), "%" SCNi64, &capabilities) == 1) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700681 parsed.capabilities = capabilities;
682 parsed.has_capabilities = true;
683 } else {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700684 uiPrintf(state, "ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1].c_str());
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700685 bad++;
686 }
687 continue;
688 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700689 if (args[i] == "selabel") {
690 if (!args[i+1].empty()) {
691 parsed.selabel = args[i+1].c_str();
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700692 parsed.has_selabel = true;
693 } else {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700694 uiPrintf(state, "ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1].c_str());
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700695 bad++;
696 }
697 continue;
698 }
699 if (max_warnings != 0) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700700 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i].c_str());
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700701 max_warnings--;
702 if (max_warnings == 0) {
703 printf("ParsedPermArgs: suppressing further warnings\n");
704 }
705 }
706 }
707 return parsed;
708}
709
710static int ApplyParsedPerms(
Michael Runged4a63422014-10-22 19:48:41 -0700711 State * state,
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700712 const char* filename,
713 const struct stat *statptr,
714 struct perm_parsed_args parsed)
715{
716 int bad = 0;
717
Nick Kralevich68802412014-10-23 20:36:42 -0700718 if (parsed.has_selabel) {
719 if (lsetfilecon(filename, parsed.selabel) != 0) {
720 uiPrintf(state, "ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n",
721 filename, parsed.selabel, strerror(errno));
722 bad++;
723 }
724 }
725
Nick Kraleviche4612512013-09-10 15:34:19 -0700726 /* ignore symlinks */
727 if (S_ISLNK(statptr->st_mode)) {
Nick Kralevich68802412014-10-23 20:36:42 -0700728 return bad;
Nick Kraleviche4612512013-09-10 15:34:19 -0700729 }
730
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700731 if (parsed.has_uid) {
732 if (chown(filename, parsed.uid, -1) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700733 uiPrintf(state, "ApplyParsedPerms: chown of %s to %d failed: %s\n",
734 filename, parsed.uid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700735 bad++;
736 }
737 }
738
739 if (parsed.has_gid) {
740 if (chown(filename, -1, parsed.gid) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700741 uiPrintf(state, "ApplyParsedPerms: chgrp of %s to %d failed: %s\n",
742 filename, parsed.gid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700743 bad++;
744 }
745 }
746
747 if (parsed.has_mode) {
748 if (chmod(filename, parsed.mode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700749 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
750 filename, parsed.mode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700751 bad++;
752 }
753 }
754
755 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
756 if (chmod(filename, parsed.dmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700757 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
758 filename, parsed.dmode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700759 bad++;
760 }
761 }
762
763 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
764 if (chmod(filename, parsed.fmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700765 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700766 filename, parsed.fmode, strerror(errno));
767 bad++;
768 }
769 }
770
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700771 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
772 if (parsed.capabilities == 0) {
773 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
774 // Report failure unless it's ENODATA (attribute not set)
Michael Runged4a63422014-10-22 19:48:41 -0700775 uiPrintf(state, "ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700776 filename, parsed.capabilities, strerror(errno));
777 bad++;
778 }
779 } else {
780 struct vfs_cap_data cap_data;
781 memset(&cap_data, 0, sizeof(cap_data));
782 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
783 cap_data.data[0].permitted = (uint32_t) (parsed.capabilities & 0xffffffff);
784 cap_data.data[0].inheritable = 0;
785 cap_data.data[1].permitted = (uint32_t) (parsed.capabilities >> 32);
786 cap_data.data[1].inheritable = 0;
787 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700788 uiPrintf(state, "ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n",
789 filename, parsed.capabilities, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700790 bad++;
791 }
792 }
793 }
794
795 return bad;
796}
797
798// nftw doesn't allow us to pass along context, so we need to use
799// global variables. *sigh*
800static struct perm_parsed_args recursive_parsed_args;
Michael Runged4a63422014-10-22 19:48:41 -0700801static State* recursive_state;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700802
803static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr,
804 int fileflags, struct FTW *pfwt) {
Michael Runged4a63422014-10-22 19:48:41 -0700805 return ApplyParsedPerms(recursive_state, filename, statptr, recursive_parsed_args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700806}
807
808static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700809 if ((argc % 2) != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700810 return ErrorAbort(state, kArgsParsingFailure,
811 "%s() expects an odd number of arguments, got %d", name, argc);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700812 }
813
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700814 std::vector<std::string> args;
815 if (!ReadArgs(state, argc, argv, &args)) {
816 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700817 }
818
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700819 struct stat sb;
820 if (lstat(args[0].c_str(), &sb) == -1) {
821 return ErrorAbort(state, kSetMetadataFailure, "%s: Error on lstat of \"%s\": %s",
822 name, args[0].c_str(), strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700823 }
824
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700825 struct perm_parsed_args parsed = ParsePermArgs(state, argc, args);
826 int bad = 0;
827 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700828
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700829 if (recursive) {
830 recursive_parsed_args = parsed;
831 recursive_state = state;
832 bad += nftw(args[0].c_str(), do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
833 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
834 recursive_state = NULL;
835 } else {
836 bad += ApplyParsedPerms(state, args[0].c_str(), &sb, parsed);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700837 }
838
839 if (bad > 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700840 return ErrorAbort(state, kSetMetadataFailure, "%s: some changes failed", name);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700841 }
842
Tianjie Xuaced5d92016-10-12 10:55:04 -0700843 return StringValue("");
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700844}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700845
Doug Zongker512536a2010-02-17 16:11:44 -0800846Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700847 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700848 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700849 }
Tianjie Xuaced5d92016-10-12 10:55:04 -0700850 std::string key;
851 if (!Evaluate(state, argv[0], &key)) {
852 return nullptr;
853 }
Elliott Hughescb220402016-09-23 15:30:55 -0700854 std::string value = android::base::GetProperty(key, "");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700855
Tianjie Xuaced5d92016-10-12 10:55:04 -0700856 return StringValue(value);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700857}
858
859
Doug Zongker47cace92009-06-18 10:11:50 -0700860// file_getprop(file, key)
861//
862// interprets 'file' as a getprop-style file (key=value pairs, one
Tao Bao51d516e2016-11-03 14:49:01 -0700863// per line. # comment lines, blank lines, lines without '=' ignored),
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700864// and returns the value for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800865Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao51d516e2016-11-03 14:49:01 -0700866 if (argc != 2) {
867 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
868 }
869
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700870 std::vector<std::string> args;
871 if (!ReadArgs(state, 2, argv, &args)) {
872 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongker47cace92009-06-18 10:11:50 -0700873 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700874 const std::string& filename = args[0];
875 const std::string& key = args[1];
Doug Zongker47cace92009-06-18 10:11:50 -0700876
877 struct stat st;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700878 if (stat(filename.c_str(), &st) < 0) {
879 return ErrorAbort(state, kFileGetPropFailure, "%s: failed to stat \"%s\": %s", name,
880 filename.c_str(), strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -0700881 }
882
Tao Bao51d516e2016-11-03 14:49:01 -0700883 constexpr off_t MAX_FILE_GETPROP_SIZE = 65536;
Doug Zongker47cace92009-06-18 10:11:50 -0700884 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
Tao Bao51d516e2016-11-03 14:49:01 -0700885 return ErrorAbort(state, kFileGetPropFailure, "%s too large for %s (max %lld)",
886 filename.c_str(), name, static_cast<long long>(MAX_FILE_GETPROP_SIZE));
Doug Zongker47cace92009-06-18 10:11:50 -0700887 }
888
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700889 std::string buffer(st.st_size, '\0');
890 FILE* f = ota_fopen(filename.c_str(), "rb");
891 if (f == nullptr) {
892 return ErrorAbort(state, kFileOpenFailure, "%s: failed to open %s: %s", name,
893 filename.c_str(), strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -0700894 }
895
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700896 if (ota_fread(&buffer[0], 1, st.st_size, f) != static_cast<size_t>(st.st_size)) {
Tianjie Xu84478e82016-05-23 11:42:40 -0700897 ErrorAbort(state, kFreadFailure, "%s: failed to read %zu bytes from %s",
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700898 name, static_cast<size_t>(st.st_size), filename.c_str());
Jed Estepa7b9a462015-12-15 16:04:53 -0800899 ota_fclose(f);
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700900 return nullptr;
Doug Zongker47cace92009-06-18 10:11:50 -0700901 }
Doug Zongker47cace92009-06-18 10:11:50 -0700902
Jed Estepa7b9a462015-12-15 16:04:53 -0800903 ota_fclose(f);
Doug Zongker47cace92009-06-18 10:11:50 -0700904
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700905 std::vector<std::string> lines = android::base::Split(buffer, "\n");
906 for (size_t i = 0; i < lines.size(); i++) {
907 std::string line = android::base::Trim(lines[i]);
Doug Zongker47cace92009-06-18 10:11:50 -0700908
909 // comment or blank line: skip to next line
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700910 if (line.empty() || line[0] == '#') {
911 continue;
912 }
913 size_t equal_pos = line.find('=');
914 if (equal_pos == std::string::npos) {
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700915 continue;
Doug Zongker47cace92009-06-18 10:11:50 -0700916 }
917
918 // trim whitespace between key and '='
Tao Bao51d516e2016-11-03 14:49:01 -0700919 std::string str = android::base::Trim(line.substr(0, equal_pos));
Doug Zongker47cace92009-06-18 10:11:50 -0700920
921 // not the key we're looking for
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700922 if (key != str) continue;
Doug Zongker47cace92009-06-18 10:11:50 -0700923
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700924 return StringValue(android::base::Trim(line.substr(equal_pos + 1)));
925 }
Doug Zongker47cace92009-06-18 10:11:50 -0700926
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700927 return StringValue("");
Doug Zongker47cace92009-06-18 10:11:50 -0700928}
929
Doug Zongker8edb00c2009-06-11 17:21:44 -0700930// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -0800931Value* ApplyPatchSpaceFn(const char* name, State* state,
932 int argc, Expr* argv[]) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700933 std::vector<std::string> args;
934 if (!ReadArgs(state, 1, argv, &args)) {
935 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800936 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700937 const std::string& bytes_str = args[0];
Doug Zongkerc4351c72010-02-22 14:46:32 -0800938
Tao Baob15fd222015-09-24 11:10:51 -0700939 size_t bytes;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700940 if (!android::base::ParseUint(bytes_str.c_str(), &bytes)) {
941 return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count\n\n",
942 name, bytes_str.c_str());
Doug Zongkerc4351c72010-02-22 14:46:32 -0800943 }
944
Tianjie Xuaced5d92016-10-12 10:55:04 -0700945 return StringValue(CacheSizeCheck(bytes) ? "" : "t");
Doug Zongkerc4351c72010-02-22 14:46:32 -0800946}
947
Doug Zongker52b40362014-02-10 15:30:30 -0800948// apply_patch(file, size, init_sha1, tgt_sha1, patch)
949
Doug Zongker512536a2010-02-17 16:11:44 -0800950Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800951 if (argc < 6 || (argc % 2) == 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700952 return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 6 args and an "
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700953 "even number, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700954 }
955
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700956 std::vector<std::string> args;
957 if (!ReadArgs(state, 4, argv, &args)) {
958 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700959 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700960 const std::string& source_filename = args[0];
961 const std::string& target_filename = args[1];
962 const std::string& target_sha1 = args[2];
963 const std::string& target_size_str = args[3];
Doug Zongker8edb00c2009-06-11 17:21:44 -0700964
Tao Baob15fd222015-09-24 11:10:51 -0700965 size_t target_size;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700966 if (!android::base::ParseUint(target_size_str.c_str(), &target_size)) {
967 return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count",
968 name, target_size_str.c_str());
Doug Zongkerc4351c72010-02-22 14:46:32 -0800969 }
970
971 int patchcount = (argc-4) / 2;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700972 std::vector<std::unique_ptr<Value>> arg_values;
973 if (!ReadValueArgs(state, argc-4, argv+4, &arg_values)) {
Yabin Cui64be2132016-02-03 18:16:02 -0800974 return nullptr;
975 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700976
Yabin Cui64be2132016-02-03 18:16:02 -0800977 for (int i = 0; i < patchcount; ++i) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700978 if (arg_values[i * 2]->type != VAL_STRING) {
979 return ErrorAbort(state, kArgsParsingFailure, "%s(): sha-1 #%d is not string", name,
980 i * 2);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800981 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700982 if (arg_values[i * 2 + 1]->type != VAL_BLOB) {
983 return ErrorAbort(state, kArgsParsingFailure, "%s(): patch #%d is not blob", name,
984 i * 2 + 1);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800985 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700986 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700987
Tianjie Xuaced5d92016-10-12 10:55:04 -0700988 std::vector<std::string> patch_sha_str;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700989 std::vector<std::unique_ptr<Value>> patches;
Yabin Cui64be2132016-02-03 18:16:02 -0800990 for (int i = 0; i < patchcount; ++i) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700991 patch_sha_str.push_back(arg_values[i * 2]->data);
992 patches.push_back(std::move(arg_values[i * 2 + 1]));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700993 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800994
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700995 int result = applypatch(source_filename.c_str(), target_filename.c_str(),
996 target_sha1.c_str(), target_size,
997 patch_sha_str, patches, nullptr);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800998
Tianjie Xuaced5d92016-10-12 10:55:04 -0700999 return StringValue(result == 0 ? "t" : "");
Doug Zongkerc4351c72010-02-22 14:46:32 -08001000}
1001
1002// apply_patch_check(file, [sha1_1, ...])
1003Value* ApplyPatchCheckFn(const char* name, State* state,
1004 int argc, Expr* argv[]) {
1005 if (argc < 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001006 return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 1 arg, got %d",
Doug Zongkerc4351c72010-02-22 14:46:32 -08001007 name, argc);
1008 }
1009
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001010 std::vector<std::string> args;
1011 if (!ReadArgs(state, 1, argv, &args)) {
1012 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001013 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001014 const std::string& filename = args[0];
Doug Zongkerc4351c72010-02-22 14:46:32 -08001015
Tianjie Xuaced5d92016-10-12 10:55:04 -07001016 std::vector<std::string> sha1s;
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001017 if (!ReadArgs(state, argc - 1, argv + 1, &sha1s)) {
1018 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001019 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001020 int result = applypatch_check(filename.c_str(), sha1s);
Tianjie Xuaced5d92016-10-12 10:55:04 -07001021
1022 return StringValue(result == 0 ? "t" : "");
Doug Zongker8edb00c2009-06-11 17:21:44 -07001023}
1024
Tao Bao1107d962015-09-09 17:16:55 -07001025// This is the updater side handler for ui_print() in edify script. Contents
1026// will be sent over to the recovery side for on-screen display.
Doug Zongker512536a2010-02-17 16:11:44 -08001027Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001028 std::vector<std::string> args;
1029 if (!ReadArgs(state, argc, argv, &args)) {
1030 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001031 }
1032
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001033 std::string buffer = android::base::Join(args, "") + "\n";
Michael Runged4a63422014-10-22 19:48:41 -07001034 uiPrint(state, buffer);
Tianjie Xuaced5d92016-10-12 10:55:04 -07001035 return StringValue(buffer);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001036}
1037
Doug Zongkerd0181b82011-10-19 10:51:12 -07001038Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
1039 if (argc != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001040 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %d", name, argc);
Doug Zongkerd0181b82011-10-19 10:51:12 -07001041 }
1042 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
Tianjie Xuaced5d92016-10-12 10:55:04 -07001043 return StringValue("t");
Doug Zongkerd0181b82011-10-19 10:51:12 -07001044}
1045
Doug Zongker512536a2010-02-17 16:11:44 -08001046Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001047 if (argc < 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001048 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001049 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001050
1051 std::vector<std::string> args;
1052 if (!ReadArgs(state, argc, argv, &args)) {
1053 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001054 }
1055
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001056 char* args2[argc+1];
1057 for (int i = 0; i < argc; i++) {
1058 args2[i] = &args[i][0];
1059 }
1060 args2[argc] = nullptr;
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001061
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001062 printf("about to run program [%s] with %d args\n", args2[0], argc);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001063
1064 pid_t child = fork();
1065 if (child == 0) {
1066 execv(args2[0], args2);
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001067 printf("run_program: execv failed: %s\n", strerror(errno));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001068 _exit(1);
1069 }
1070 int status;
1071 waitpid(child, &status, 0);
1072 if (WIFEXITED(status)) {
1073 if (WEXITSTATUS(status) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001074 printf("run_program: child exited with status %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001075 WEXITSTATUS(status));
1076 }
1077 } else if (WIFSIGNALED(status)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001078 printf("run_program: child terminated by signal %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001079 WTERMSIG(status));
1080 }
1081
Tianjie Xuaced5d92016-10-12 10:55:04 -07001082 return StringValue(android::base::StringPrintf("%d", status));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001083}
1084
Doug Zongker512536a2010-02-17 16:11:44 -08001085// sha1_check(data)
1086// to return the sha1 of the data (given in the format returned by
1087// read_file).
1088//
1089// sha1_check(data, sha1_hex, [sha1_hex, ...])
1090// returns the sha1 of the file if it matches any of the hex
1091// strings passed, or "" if it does not equal any of them.
1092//
1093Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1094 if (argc < 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001095 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
Doug Zongker512536a2010-02-17 16:11:44 -08001096 }
1097
Tianjie Xuaced5d92016-10-12 10:55:04 -07001098 std::vector<std::unique_ptr<Value>> args;
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001099 if (!ReadValueArgs(state, argc, argv, &args)) {
1100 return nullptr;
Doug Zongker512536a2010-02-17 16:11:44 -08001101 }
1102
Tianjie Xuaced5d92016-10-12 10:55:04 -07001103 if (args[0]->type == VAL_INVALID) {
1104 return StringValue("");
Doug Zongker512536a2010-02-17 16:11:44 -08001105 }
Sen Jiangc48cb5e2016-02-04 16:23:21 +08001106 uint8_t digest[SHA_DIGEST_LENGTH];
Tianjie Xuaced5d92016-10-12 10:55:04 -07001107 SHA1(reinterpret_cast<const uint8_t*>(args[0]->data.c_str()), args[0]->data.size(), digest);
Doug Zongker512536a2010-02-17 16:11:44 -08001108
1109 if (argc == 1) {
Tianjie Xuaced5d92016-10-12 10:55:04 -07001110 return StringValue(print_sha1(digest));
Doug Zongker512536a2010-02-17 16:11:44 -08001111 }
1112
Tao Bao361342c2016-02-08 11:15:50 -08001113 for (int i = 1; i < argc; ++i) {
1114 uint8_t arg_digest[SHA_DIGEST_LENGTH];
Doug Zongker512536a2010-02-17 16:11:44 -08001115 if (args[i]->type != VAL_STRING) {
Tao Bao361342c2016-02-08 11:15:50 -08001116 printf("%s(): arg %d is not a string; skipping", name, i);
Tianjie Xuaced5d92016-10-12 10:55:04 -07001117 } else if (ParseSha1(args[i]->data.c_str(), arg_digest) != 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001118 // Warn about bad args and skip them.
Tianjie Xuaced5d92016-10-12 10:55:04 -07001119 printf("%s(): error parsing \"%s\" as sha-1; skipping", name, args[i]->data.c_str());
Sen Jiangc48cb5e2016-02-04 16:23:21 +08001120 } else if (memcmp(digest, arg_digest, SHA_DIGEST_LENGTH) == 0) {
Tao Bao361342c2016-02-08 11:15:50 -08001121 // Found a match.
1122 return args[i].release();
Doug Zongker512536a2010-02-17 16:11:44 -08001123 }
Doug Zongker512536a2010-02-17 16:11:44 -08001124 }
Tao Bao361342c2016-02-08 11:15:50 -08001125
1126 // Didn't match any of the hex strings; return false.
Tianjie Xuaced5d92016-10-12 10:55:04 -07001127 return StringValue("");
Doug Zongker512536a2010-02-17 16:11:44 -08001128}
1129
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001130// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001131// is actually a FileContents*).
1132Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1133 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001134 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker512536a2010-02-17 16:11:44 -08001135 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001136
1137 std::vector<std::string> args;
1138 if (!ReadArgs(state, 1, argv, &args)) {
1139 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1140 }
1141 const std::string& filename = args[0];
Doug Zongker512536a2010-02-17 16:11:44 -08001142
Tianjie Xuaced5d92016-10-12 10:55:04 -07001143 Value* v = new Value(VAL_INVALID, "");
Doug Zongker512536a2010-02-17 16:11:44 -08001144
1145 FileContents fc;
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001146 if (LoadFileContents(filename.c_str(), &fc) == 0) {
Tianjie Xuaced5d92016-10-12 10:55:04 -07001147 v->type = VAL_BLOB;
1148 v->data = std::string(fc.data.begin(), fc.data.end());
Doug Zongker512536a2010-02-17 16:11:44 -08001149 }
Doug Zongker512536a2010-02-17 16:11:44 -08001150 return v;
1151}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001152
Doug Zongkerc87bab12013-11-25 13:53:25 -08001153// Immediately reboot the device. Recovery is not finished normally,
1154// so if you reboot into recovery it will re-start applying the
1155// current package (because nothing has cleared the copy of the
1156// arguments stored in the BCB).
1157//
1158// The argument is the partition name passed to the android reboot
1159// property. It can be "recovery" to boot from the recovery
1160// partition, or "" (empty string) to boot from the regular boot
1161// partition.
1162Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) {
1163 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -07001164 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001165 }
1166
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001167 std::vector<std::string> args;
1168 if (!ReadArgs(state, 2, argv, &args)) {
1169 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1170 }
1171 const std::string& filename = args[0];
1172 const std::string& property = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001173
Doug Zongkerc87bab12013-11-25 13:53:25 -08001174 // zero out the 'command' field of the bootloader message.
Elliott Hughescb220402016-09-23 15:30:55 -07001175 char buffer[80];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001176 memset(buffer, 0, sizeof(((struct bootloader_message*)0)->command));
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001177 FILE* f = ota_fopen(filename.c_str(), "r+b");
Doug Zongkerc87bab12013-11-25 13:53:25 -08001178 fseek(f, offsetof(struct bootloader_message, command), SEEK_SET);
Jed Estepa7b9a462015-12-15 16:04:53 -08001179 ota_fwrite(buffer, sizeof(((struct bootloader_message*)0)->command), 1, f);
1180 ota_fclose(f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001181
Elliott Hughescb220402016-09-23 15:30:55 -07001182 std::string reboot_cmd = "reboot,";
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001183 reboot_cmd += property;
Elliott Hughescb220402016-09-23 15:30:55 -07001184 android::base::SetProperty(ANDROID_RB_PROPERTY, reboot_cmd);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001185
1186 sleep(5);
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001187 return ErrorAbort(state, kRebootFailure, "%s() failed to reboot", name);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001188}
1189
1190// Store a string value somewhere that future invocations of recovery
1191// can access it. This value is called the "stage" and can be used to
1192// drive packages that need to do reboots in the middle of
1193// installation and keep track of where they are in the multi-stage
1194// install.
1195//
1196// The first argument is the block device for the misc partition
1197// ("/misc" in the fstab), which is where this value is stored. The
1198// second argument is the string to store; it should not exceed 31
1199// bytes.
1200Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
1201 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -07001202 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001203 }
1204
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001205 std::vector<std::string> args;
1206 if (!ReadArgs(state, 2, argv, &args)) {
1207 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1208 }
1209 const std::string& filename = args[0];
1210 std::string& stagestr = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001211
1212 // Store this value in the misc partition, immediately after the
1213 // bootloader message that the main recovery uses to save its
1214 // arguments in case of the device restarting midway through
1215 // package installation.
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001216 FILE* f = ota_fopen(filename.c_str(), "r+b");
Doug Zongkerc87bab12013-11-25 13:53:25 -08001217 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001218 size_t to_write = stagestr.size();
Tianjie Xuaced5d92016-10-12 10:55:04 -07001219 size_t max_size = sizeof(((struct bootloader_message*)0)->stage);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001220 if (to_write > max_size) {
1221 to_write = max_size;
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001222 stagestr = stagestr.substr(0, max_size-1);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001223 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001224 size_t status = ota_fwrite(stagestr.c_str(), to_write, 1, f);
Jed Estepa7b9a462015-12-15 16:04:53 -08001225 ota_fclose(f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001226
Tianjie Xuaced5d92016-10-12 10:55:04 -07001227 if (status != to_write) {
1228 return StringValue("");
1229 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001230 return StringValue(filename);
1231}
1232
1233// Return the value most recently saved with SetStageFn. The argument
1234// is the block device for the misc partition.
1235Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001236 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001237 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001238 }
1239
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001240 std::vector<std::string> args;
1241 if (!ReadArgs(state, 1, argv, &args)) {
1242 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1243 }
1244 const std::string& filename = args[0];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001245
1246 char buffer[sizeof(((struct bootloader_message*)0)->stage)];
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001247 FILE* f = ota_fopen(filename.c_str(), "rb");
Doug Zongkerc87bab12013-11-25 13:53:25 -08001248 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
Tianjie Xuaced5d92016-10-12 10:55:04 -07001249 size_t status = ota_fread(buffer, sizeof(buffer), 1, f);
Jed Estepa7b9a462015-12-15 16:04:53 -08001250 ota_fclose(f);
Tianjie Xuaced5d92016-10-12 10:55:04 -07001251 if (status != sizeof(buffer)) {
1252 return StringValue("");
1253 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001254
Tianjie Xuaced5d92016-10-12 10:55:04 -07001255 buffer[sizeof(buffer)-1] = '\0';
1256 return StringValue(buffer);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001257}
1258
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001259Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) {
1260 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -07001261 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001262 }
1263
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001264 std::vector<std::string> args;
1265 if (!ReadArgs(state, 2, argv, &args)) {
1266 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1267 }
1268 const std::string& filename = args[0];
1269 const std::string& len_str = args[1];
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001270
Tao Baob15fd222015-09-24 11:10:51 -07001271 size_t len;
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001272 if (!android::base::ParseUint(len_str.c_str(), &len)) {
1273 return nullptr;
1274 }
1275 int fd = ota_open(filename.c_str(), O_WRONLY, 0644);
1276 // The wipe_block_device function in ext4_utils returns 0 on success and 1
1277 // for failure.
1278 int status = wipe_block_device(fd, len);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001279
Jed Estepa7b9a462015-12-15 16:04:53 -08001280 ota_close(fd);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001281
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001282 return StringValue((status == 0) ? "t" : "");
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001283}
1284
Doug Zongkerc704e062014-05-23 08:40:35 -07001285Value* EnableRebootFn(const char* name, State* state, int argc, Expr* argv[]) {
1286 if (argc != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001287 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %d", name, argc);
Doug Zongkerc704e062014-05-23 08:40:35 -07001288 }
1289 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
1290 fprintf(ui->cmd_pipe, "enable_reboot\n");
Tianjie Xuaced5d92016-10-12 10:55:04 -07001291 return StringValue("t");
Doug Zongkerc704e062014-05-23 08:40:35 -07001292}
1293
Michael Rungeacf47db2014-11-21 00:12:28 -08001294Value* Tune2FsFn(const char* name, State* state, int argc, Expr* argv[]) {
1295 if (argc == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001296 return ErrorAbort(state, kArgsParsingFailure, "%s() expects args, got %d", name, argc);
Michael Rungeacf47db2014-11-21 00:12:28 -08001297 }
1298
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001299 std::vector<std::string> args;
1300 if (!ReadArgs(state, argc, argv, &args)) {
Tianjie Xu16255832016-04-30 11:49:59 -07001301 return ErrorAbort(state, kArgsParsingFailure, "%s() could not read args", name);
Michael Rungeacf47db2014-11-21 00:12:28 -08001302 }
1303
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001304 char* args2[argc+1];
Michael Rungeacf47db2014-11-21 00:12:28 -08001305 // Tune2fs expects the program name as its args[0]
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001306 args2[0] = const_cast<char*>(name);
1307 if (args2[0] == nullptr) {
1308 return nullptr;
Michael Rungeacf47db2014-11-21 00:12:28 -08001309 }
Tao Baoba9a42a2015-06-23 23:23:33 -07001310 for (int i = 0; i < argc; ++i) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001311 args2[i + 1] = &args[i][0];
Michael Rungeacf47db2014-11-21 00:12:28 -08001312 }
Michael Rungeacf47db2014-11-21 00:12:28 -08001313
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001314 // tune2fs changes the file system parameters on an ext2 file system; it
1315 // returns 0 on success.
1316 int result = tune2fs_main(argc + 1, args2);
1317
Michael Rungeacf47db2014-11-21 00:12:28 -08001318 if (result != 0) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001319 return ErrorAbort(state, kTune2FsFailure, "%s() returned error code %d", name, result);
Michael Rungeacf47db2014-11-21 00:12:28 -08001320 }
Tianjie Xuaced5d92016-10-12 10:55:04 -07001321 return StringValue("t");
Michael Rungeacf47db2014-11-21 00:12:28 -08001322}
1323
Doug Zongker9931f7f2009-06-10 14:11:53 -07001324void RegisterInstallFunctions() {
1325 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001326 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001327 RegisterFunction("unmount", UnmountFn);
1328 RegisterFunction("format", FormatFn);
1329 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001330 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001331 RegisterFunction("delete", DeleteFn);
1332 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001333 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1334 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001335 RegisterFunction("symlink", SymlinkFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001336
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001337 // Usage:
1338 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1339 // Example:
1340 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1341 RegisterFunction("set_metadata", SetMetadataFn);
1342
1343 // Usage:
1344 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1345 // Example:
1346 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1347 RegisterFunction("set_metadata_recursive", SetMetadataFn);
1348
Doug Zongker8edb00c2009-06-11 17:21:44 -07001349 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001350 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001351
1352 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001353 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1354 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001355
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001356 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001357
Doug Zongker512536a2010-02-17 16:11:44 -08001358 RegisterFunction("read_file", ReadFileFn);
1359 RegisterFunction("sha1_check", Sha1CheckFn);
Michael Rungece7ca712013-11-06 17:42:20 -08001360 RegisterFunction("rename", RenameFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001361
Doug Zongkerd0181b82011-10-19 10:51:12 -07001362 RegisterFunction("wipe_cache", WipeCacheFn);
1363
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001364 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001365
1366 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001367
1368 RegisterFunction("reboot_now", RebootNowFn);
1369 RegisterFunction("get_stage", GetStageFn);
1370 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -07001371
1372 RegisterFunction("enable_reboot", EnableRebootFn);
Michael Rungeacf47db2014-11-21 00:12:28 -08001373 RegisterFunction("tune2fs", Tune2FsFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001374}