blob: 3546968d7d9de4fba96e6c77359a9ff6ea34c1be [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>
Doug Zongker9931f7f2009-06-10 14:11:53 -070036
Yabin Cui64be2132016-02-03 18:16:02 -080037#include <memory>
Tao Bao361342c2016-02-08 11:15:50 -080038#include <string>
Yabin Cui64be2132016-02-03 18:16:02 -080039#include <vector>
40
Elliott Hughes4b166f02015-12-04 15:30:20 -080041#include <android-base/parseint.h>
Elliott Hughescb220402016-09-23 15:30:55 -070042#include <android-base/properties.h>
Elliott Hughes4b166f02015-12-04 15:30:20 -080043#include <android-base/strings.h>
44#include <android-base/stringprintf.h>
Tao Bao0c7839a2016-10-10 15:48:37 -070045#include <cutils/android_reboot.h>
Tao Baode40ba52016-10-05 23:17:01 -070046#include <ext4_utils/make_ext4fs.h>
47#include <ext4_utils/wipe.h>
Tao Bao361342c2016-02-08 11:15:50 -080048#include <openssl/sha.h>
Elliott Hughes4bbd5bf2016-04-01 18:24:39 -070049#include <selinux/label.h>
50#include <selinux/selinux.h>
Tao Bao1107d962015-09-09 17:16:55 -070051
Doug Zongkerc87bab12013-11-25 13:53:25 -080052#include "applypatch/applypatch.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070053#include "bootloader.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070054#include "edify/expr.h"
Tianjie Xu16255832016-04-30 11:49:59 -070055#include "error_code.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070056#include "minzip/DirUtil.h"
Elliott Hughes63a31922016-06-09 17:41:22 -070057#include "mounts.h"
Jed Estep39c1b5e2015-12-15 16:04:53 -080058#include "ota_io.h"
Tao Bao361342c2016-02-08 11:15:50 -080059#include "print_sha1.h"
Michael Rungeacf47db2014-11-21 00:12:28 -080060#include "tune2fs.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070061#include "updater/updater.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070062
Tao Bao1107d962015-09-09 17:16:55 -070063// Send over the buffer to recovery though the command pipe.
64static void uiPrint(State* state, const std::string& buffer) {
65 UpdaterInfo* ui = reinterpret_cast<UpdaterInfo*>(state->cookie);
Tao Baob6918c72015-05-19 17:02:16 -070066
Tao Bao1107d962015-09-09 17:16:55 -070067 // "line1\nline2\n" will be split into 3 tokens: "line1", "line2" and "".
68 // So skip sending empty strings to UI.
69 std::vector<std::string> lines = android::base::Split(buffer, "\n");
70 for (auto& line: lines) {
71 if (!line.empty()) {
72 fprintf(ui->cmd_pipe, "ui_print %s\n", line.c_str());
73 fprintf(ui->cmd_pipe, "ui_print\n");
74 }
75 }
76
77 // On the updater side, we need to dump the contents to stderr (which has
78 // been redirected to the log file). Because the recovery will only print
79 // the contents to screen when processing pipe command ui_print.
80 fprintf(stderr, "%s", buffer.c_str());
Michael Runged4a63422014-10-22 19:48:41 -070081}
82
Elliott Hughes83ce7552016-06-30 09:28:42 -070083void uiPrintf(State* _Nonnull state, const char* _Nonnull format, ...) {
Tao Bao1107d962015-09-09 17:16:55 -070084 std::string error_msg;
85
Michael Runged4a63422014-10-22 19:48:41 -070086 va_list ap;
87 va_start(ap, format);
Tao Bao1107d962015-09-09 17:16:55 -070088 android::base::StringAppendV(&error_msg, format, ap);
Michael Runged4a63422014-10-22 19:48:41 -070089 va_end(ap);
Tao Bao1107d962015-09-09 17:16:55 -070090
Michael Runged4a63422014-10-22 19:48:41 -070091 uiPrint(state, error_msg);
92}
93
Tao Bao0c7839a2016-10-10 15:48:37 -070094// Create all parent directories of name, if necessary.
95static int make_parents(char* name) {
96 char* p;
97 for (p = name + (strlen(name)-1); p > name; --p) {
98 if (*p != '/') continue;
99 *p = '\0';
100 if (make_parents(name) < 0) return -1;
101 int result = mkdir(name, 0700);
102 if (result == 0) printf("created [%s]\n", name);
103 *p = '/';
104 if (result == 0 || errno == EEXIST) {
105 // successfully created or already existed; we're done
106 return 0;
107 } else {
108 printf("failed to mkdir %s: %s\n", name, strerror(errno));
109 return -1;
110 }
111 }
112 return 0;
113}
114
Doug Zongker3d177d02010-07-01 09:18:44 -0700115// mount(fs_type, partition_type, location, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700116//
Doug Zongker3d177d02010-07-01 09:18:44 -0700117// fs_type="ext4" partition_type="EMMC" location=device
Doug Zongker512536a2010-02-17 16:11:44 -0800118Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700119 char* result = NULL;
Michael Runge168f7772014-10-22 17:05:08 -0700120 if (argc != 4 && argc != 5) {
Tianjie Xu16255832016-04-30 11:49:59 -0700121 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 4-5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700122 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700123 char* fs_type;
124 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700125 char* location;
126 char* mount_point;
Michael Runge168f7772014-10-22 17:05:08 -0700127 char* mount_options;
128 bool has_mount_options;
129 if (argc == 5) {
130 has_mount_options = true;
131 if (ReadArgs(state, argv, 5, &fs_type, &partition_type,
132 &location, &mount_point, &mount_options) < 0) {
133 return NULL;
134 }
135 } else {
136 has_mount_options = false;
137 if (ReadArgs(state, argv, 4, &fs_type, &partition_type,
Doug Zongker3d177d02010-07-01 09:18:44 -0700138 &location, &mount_point) < 0) {
Michael Runge168f7772014-10-22 17:05:08 -0700139 return NULL;
140 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700141 }
142
Doug Zongker3d177d02010-07-01 09:18:44 -0700143 if (strlen(fs_type) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700144 ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
Doug Zongker3d177d02010-07-01 09:18:44 -0700145 goto done;
146 }
147 if (strlen(partition_type) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700148 ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
Doug Zongker3d177d02010-07-01 09:18:44 -0700149 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700150 goto done;
151 }
152 if (strlen(location) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700153 ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700154 goto done;
155 }
156 if (strlen(mount_point) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700157 ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
158 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700159 goto done;
160 }
161
Tao Baoba9a42a2015-06-23 23:23:33 -0700162 {
163 char *secontext = NULL;
Stephen Smalley779701d2012-02-09 14:13:23 -0500164
Tao Baoba9a42a2015-06-23 23:23:33 -0700165 if (sehandle) {
166 selabel_lookup(sehandle, &secontext, mount_point, 0755);
167 setfscreatecon(secontext);
168 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500169
Tao Baoba9a42a2015-06-23 23:23:33 -0700170 mkdir(mount_point, 0755);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700171
Tao Baoba9a42a2015-06-23 23:23:33 -0700172 if (secontext) {
173 freecon(secontext);
174 setfscreatecon(NULL);
175 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500176 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500177
Elliott Hughes63a31922016-06-09 17:41:22 -0700178 if (mount(location, mount_point, fs_type,
179 MS_NOATIME | MS_NODEV | MS_NODIRATIME,
180 has_mount_options ? mount_options : "") < 0) {
181 uiPrintf(state, "%s: failed to mount %s at %s: %s\n",
182 name, location, mount_point, strerror(errno));
183 result = strdup("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700184 } else {
Elliott Hughes63a31922016-06-09 17:41:22 -0700185 result = mount_point;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700186 }
187
188done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700189 free(fs_type);
190 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700191 free(location);
192 if (result != mount_point) free(mount_point);
Michael Runge168f7772014-10-22 17:05:08 -0700193 if (has_mount_options) free(mount_options);
Doug Zongker512536a2010-02-17 16:11:44 -0800194 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700195}
196
Doug Zongker8edb00c2009-06-11 17:21:44 -0700197
198// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800199Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700200 char* result = NULL;
201 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700202 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700203 }
204 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700205 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700206 return NULL;
207 }
208 if (strlen(mount_point) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700209 ErrorAbort(state, kArgsParsingFailure, "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700210 goto done;
211 }
212
213 scan_mounted_volumes();
Tao Baoba9a42a2015-06-23 23:23:33 -0700214 {
Elliott Hughes63a31922016-06-09 17:41:22 -0700215 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
Tao Baoba9a42a2015-06-23 23:23:33 -0700216 if (vol == NULL) {
217 result = strdup("");
218 } else {
219 result = mount_point;
220 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700221 }
222
223done:
224 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800225 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700226}
227
228
Doug Zongker512536a2010-02-17 16:11:44 -0800229Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700230 char* result = NULL;
231 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700232 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700233 }
234 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700235 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700236 return NULL;
237 }
238 if (strlen(mount_point) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700239 ErrorAbort(state, kArgsParsingFailure, "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700240 goto done;
241 }
242
243 scan_mounted_volumes();
Tao Baoba9a42a2015-06-23 23:23:33 -0700244 {
Elliott Hughes63a31922016-06-09 17:41:22 -0700245 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
Tao Baoba9a42a2015-06-23 23:23:33 -0700246 if (vol == NULL) {
247 uiPrintf(state, "unmount of %s failed; no such volume\n", mount_point);
248 result = strdup("");
249 } else {
250 int ret = unmount_mounted_volume(vol);
251 if (ret != 0) {
252 uiPrintf(state, "unmount of %s failed (%d): %s\n",
253 mount_point, ret, strerror(errno));
254 }
255 result = mount_point;
Michael Runge5ddf4292014-10-24 14:14:41 -0700256 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700257 }
258
259done:
260 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800261 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700262}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700263
JP Abgrall37aedb32014-06-16 19:07:39 -0700264static int exec_cmd(const char* path, char* const argv[]) {
265 int status;
266 pid_t child;
267 if ((child = vfork()) == 0) {
268 execv(path, argv);
269 _exit(-1);
270 }
271 waitpid(child, &status, 0);
272 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
273 printf("%s failed with status %d\n", path, WEXITSTATUS(status));
274 }
275 return WEXITSTATUS(status);
276}
277
Doug Zongker8edb00c2009-06-11 17:21:44 -0700278
Stephen Smalley779701d2012-02-09 14:13:23 -0500279// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700280//
Stephen Smalley779701d2012-02-09 14:13:23 -0500281// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700282// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
283// if fs_size == 0, then make fs uses the entire partition.
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800284// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700285// 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 -0800286Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700287 char* result = NULL;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400288 if (argc != 5) {
Tianjie Xu16255832016-04-30 11:49:59 -0700289 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700290 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700291 char* fs_type;
292 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700293 char* location;
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800294 char* fs_size;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400295 char* mount_point;
Stephen Smalley779701d2012-02-09 14:13:23 -0500296
Stephen Smalley779701d2012-02-09 14:13:23 -0500297 if (ReadArgs(state, argv, 5, &fs_type, &partition_type, &location, &fs_size, &mount_point) < 0) {
298 return NULL;
299 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700300
Doug Zongker3d177d02010-07-01 09:18:44 -0700301 if (strlen(fs_type) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700302 ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
Doug Zongker3d177d02010-07-01 09:18:44 -0700303 goto done;
304 }
305 if (strlen(partition_type) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700306 ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
Doug Zongker3d177d02010-07-01 09:18:44 -0700307 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700308 goto done;
309 }
310 if (strlen(location) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700311 ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700312 goto done;
313 }
314
Stephen Smalley516e4e22012-04-03 13:35:11 -0400315 if (strlen(mount_point) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700316 ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
317 name);
Stephen Smalley779701d2012-02-09 14:13:23 -0500318 goto done;
319 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500320
Elliott Hughes63a31922016-06-09 17:41:22 -0700321 if (strcmp(fs_type, "ext4") == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500322 int status = make_ext4fs(location, atoll(fs_size), mount_point, sehandle);
Doug Zongker3d177d02010-07-01 09:18:44 -0700323 if (status != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700324 printf("%s: make_ext4fs failed (%d) on %s",
Doug Zongker3d177d02010-07-01 09:18:44 -0700325 name, status, location);
326 result = strdup("");
327 goto done;
328 }
329 result = location;
JP Abgrall37aedb32014-06-16 19:07:39 -0700330 } else if (strcmp(fs_type, "f2fs") == 0) {
331 char *num_sectors;
332 if (asprintf(&num_sectors, "%lld", atoll(fs_size) / 512) <= 0) {
333 printf("format_volume: failed to create %s command for %s\n", fs_type, location);
334 result = strdup("");
335 goto done;
336 }
337 const char *f2fs_path = "/sbin/mkfs.f2fs";
338 const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", location, num_sectors, NULL};
339 int status = exec_cmd(f2fs_path, (char* const*)f2fs_argv);
340 free(num_sectors);
341 if (status != 0) {
342 printf("%s: mkfs.f2fs failed (%d) on %s",
343 name, status, location);
344 result = strdup("");
345 goto done;
346 }
347 result = location;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700348 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700349 printf("%s: unsupported fs_type \"%s\" partition_type \"%s\"",
Doug Zongker3d177d02010-07-01 09:18:44 -0700350 name, fs_type, partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700351 }
352
353done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700354 free(fs_type);
355 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700356 if (result != location) free(location);
Doug Zongker512536a2010-02-17 16:11:44 -0800357 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700358}
359
Michael Rungece7ca712013-11-06 17:42:20 -0800360Value* RenameFn(const char* name, State* state, int argc, Expr* argv[]) {
361 char* result = NULL;
362 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700363 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Michael Rungece7ca712013-11-06 17:42:20 -0800364 }
365
366 char* src_name;
367 char* dst_name;
368
369 if (ReadArgs(state, argv, 2, &src_name, &dst_name) < 0) {
370 return NULL;
371 }
372 if (strlen(src_name) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700373 ErrorAbort(state, kArgsParsingFailure, "src_name argument to %s() can't be empty", name);
Michael Rungece7ca712013-11-06 17:42:20 -0800374 goto done;
375 }
376 if (strlen(dst_name) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700377 ErrorAbort(state, kArgsParsingFailure, "dst_name argument to %s() can't be empty", name);
Michael Rungece7ca712013-11-06 17:42:20 -0800378 goto done;
379 }
Michael Rungea91ecc52014-07-21 17:40:02 -0700380 if (make_parents(dst_name) != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700381 ErrorAbort(state, kFileRenameFailure, "Creating parent of %s failed, error %s",
Michael Rungea91ecc52014-07-21 17:40:02 -0700382 dst_name, strerror(errno));
Michael Runge2f0ef732014-10-22 14:28:23 -0700383 } else if (access(dst_name, F_OK) == 0 && access(src_name, F_OK) != 0) {
384 // File was already moved
385 result = dst_name;
Michael Rungea91ecc52014-07-21 17:40:02 -0700386 } else if (rename(src_name, dst_name) != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700387 ErrorAbort(state, kFileRenameFailure, "Rename of %s to %s failed, error %s",
Michael Rungece7ca712013-11-06 17:42:20 -0800388 src_name, dst_name, strerror(errno));
389 } else {
390 result = dst_name;
391 }
392
393done:
394 free(src_name);
395 if (result != dst_name) free(dst_name);
396 return StringValue(result);
397}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700398
Doug Zongker512536a2010-02-17 16:11:44 -0800399Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700400 char** paths = reinterpret_cast<char**>(malloc(argc * sizeof(char*)));
401 for (int i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700402 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700403 if (paths[i] == NULL) {
Yabin Cui64be2132016-02-03 18:16:02 -0800404 for (int j = 0; j < i; ++j) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700405 free(paths[j]);
406 }
407 free(paths);
408 return NULL;
409 }
410 }
411
412 bool recursive = (strcmp(name, "delete_recursive") == 0);
413
414 int success = 0;
Tao Baoba9a42a2015-06-23 23:23:33 -0700415 for (int i = 0; i < argc; ++i) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700416 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
417 ++success;
418 free(paths[i]);
419 }
420 free(paths);
421
422 char buffer[10];
423 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800424 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700425}
426
Doug Zongker8edb00c2009-06-11 17:21:44 -0700427
Doug Zongker512536a2010-02-17 16:11:44 -0800428Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700429 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700430 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700431 }
432 char* frac_str;
433 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700434 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700435 return NULL;
436 }
437
438 double frac = strtod(frac_str, NULL);
Tao Baob15fd222015-09-24 11:10:51 -0700439 int sec;
440 android::base::ParseInt(sec_str, &sec);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700441
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700442 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700443 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
444
Doug Zongker9931f7f2009-06-10 14:11:53 -0700445 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800446 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700447}
448
Doug Zongker512536a2010-02-17 16:11:44 -0800449Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700450 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700451 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700452 }
453 char* frac_str;
454 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
455 return NULL;
456 }
457
458 double frac = strtod(frac_str, NULL);
459
460 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
461 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
462
Doug Zongker512536a2010-02-17 16:11:44 -0800463 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700464}
465
Doug Zongker8edb00c2009-06-11 17:21:44 -0700466// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800467Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700468 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700469 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700470 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700471 }
472 char* zip_path;
473 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700474 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700475
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700476 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700477
478 // To create a consistent system image, never use the clock for timestamps.
479 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
480
481 bool success = mzExtractRecursive(za, zip_path, dest_path,
Narayan Kamath9c0f5d62015-02-23 14:09:31 +0000482 &timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500483 NULL, NULL, sehandle);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700484 free(zip_path);
485 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800486 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700487}
488
Doug Zongker8edb00c2009-06-11 17:21:44 -0700489
490// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800491// or
492// package_extract_file(package_path)
493// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800494// function (the char* returned is actually a FileContents*).
495Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700496 int argc, Expr* argv[]) {
Doug Zongker5f875bf2014-08-22 14:53:43 -0700497 if (argc < 1 || argc > 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700498 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 or 2 args, got %d",
Doug Zongker6aece332010-02-01 14:40:12 -0800499 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700500 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700501 bool success = false;
Doug Zongker43772d22014-06-09 14:13:19 -0700502
Doug Zongker5f875bf2014-08-22 14:53:43 -0700503 if (argc == 2) {
504 // The two-argument version extracts to a file.
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800505
506 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700507
Doug Zongker6aece332010-02-01 14:40:12 -0800508 char* zip_path;
509 char* dest_path;
Doug Zongker5f875bf2014-08-22 14:53:43 -0700510 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800511
Doug Zongker6aece332010-02-01 14:40:12 -0800512 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
513 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700514 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800515 goto done2;
516 }
517
Tao Baoba9a42a2015-06-23 23:23:33 -0700518 {
Alistair Strachan733285f2016-05-05 16:04:32 -0700519 int fd = TEMP_FAILURE_RETRY(ota_open(dest_path, O_WRONLY | O_CREAT | O_TRUNC,
Tao Baod3cac342015-12-14 15:53:25 -0800520 S_IRUSR | S_IWUSR));
521 if (fd == -1) {
522 printf("%s: can't open %s for write: %s\n", name, dest_path, strerror(errno));
Tao Baoba9a42a2015-06-23 23:23:33 -0700523 goto done2;
524 }
Tao Baod3cac342015-12-14 15:53:25 -0800525 success = mzExtractZipEntryToFile(za, entry, fd);
Jed Estepa7b9a462015-12-15 16:04:53 -0800526 if (ota_fsync(fd) == -1) {
Tao Baod3cac342015-12-14 15:53:25 -0800527 printf("fsync of \"%s\" failed: %s\n", dest_path, strerror(errno));
528 success = false;
529 }
Jed Estepa7b9a462015-12-15 16:04:53 -0800530 if (ota_close(fd) == -1) {
Tao Baod3cac342015-12-14 15:53:25 -0800531 printf("close of \"%s\" failed: %s\n", dest_path, strerror(errno));
532 success = false;
533 }
Doug Zongker6aece332010-02-01 14:40:12 -0800534 }
Doug Zongker6aece332010-02-01 14:40:12 -0800535
536 done2:
537 free(zip_path);
538 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800539 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800540 } else {
541 // The one-argument version returns the contents of the file
542 // as the result.
543
544 char* zip_path;
Yabin Cui64be2132016-02-03 18:16:02 -0800545 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
546
Tao Baoba9a42a2015-06-23 23:23:33 -0700547 Value* v = reinterpret_cast<Value*>(malloc(sizeof(Value)));
Doug Zongker512536a2010-02-17 16:11:44 -0800548 v->type = VAL_BLOB;
549 v->size = -1;
550 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800551
Doug Zongker6aece332010-02-01 14:40:12 -0800552 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
553 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
554 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700555 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800556 goto done1;
557 }
558
Doug Zongker512536a2010-02-17 16:11:44 -0800559 v->size = mzGetZipEntryUncompLen(entry);
Tao Baoba9a42a2015-06-23 23:23:33 -0700560 v->data = reinterpret_cast<char*>(malloc(v->size));
Doug Zongker512536a2010-02-17 16:11:44 -0800561 if (v->data == NULL) {
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700562 printf("%s: failed to allocate %zd bytes for %s\n",
563 name, v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800564 goto done1;
565 }
566
Doug Zongker512536a2010-02-17 16:11:44 -0800567 success = mzExtractZipEntryToBuffer(za, entry,
568 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800569
570 done1:
571 free(zip_path);
572 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800573 free(v->data);
574 v->data = NULL;
575 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800576 }
Doug Zongker512536a2010-02-17 16:11:44 -0800577 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700578 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700579}
580
Doug Zongker9931f7f2009-06-10 14:11:53 -0700581// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700582// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800583Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700584 if (argc == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700585 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700586 }
587 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700588 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700589 if (target == NULL) return NULL;
590
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700591 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700592 if (srcs == NULL) {
593 free(target);
594 return NULL;
595 }
596
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700597 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700598 int i;
599 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700600 if (unlink(srcs[i]) < 0) {
601 if (errno != ENOENT) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700602 printf("%s: failed to remove %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700603 name, srcs[i], strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700604 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700605 }
606 }
Doug Zongkera23075f2012-08-06 16:19:09 -0700607 if (make_parents(srcs[i])) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700608 printf("%s: failed to symlink %s to %s: making parents failed\n",
Doug Zongkera23075f2012-08-06 16:19:09 -0700609 name, srcs[i], target);
610 ++bad;
611 }
Doug Zongker60babf82009-09-18 15:11:24 -0700612 if (symlink(target, srcs[i]) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700613 printf("%s: failed to symlink %s to %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700614 name, srcs[i], target, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700615 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700616 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700617 free(srcs[i]);
618 }
619 free(srcs);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700620 if (bad) {
Tianjie Xu16255832016-04-30 11:49:59 -0700621 return ErrorAbort(state, kSymlinkFailure, "%s: some symlinks failed", name);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700622 }
Doug Zongker512536a2010-02-17 16:11:44 -0800623 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700624}
625
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700626struct perm_parsed_args {
627 bool has_uid;
628 uid_t uid;
629 bool has_gid;
630 gid_t gid;
631 bool has_mode;
632 mode_t mode;
633 bool has_fmode;
634 mode_t fmode;
635 bool has_dmode;
636 mode_t dmode;
637 bool has_selabel;
638 char* selabel;
639 bool has_capabilities;
640 uint64_t capabilities;
641};
642
Michael Runged4a63422014-10-22 19:48:41 -0700643static struct perm_parsed_args ParsePermArgs(State * state, int argc, char** args) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700644 int i;
645 struct perm_parsed_args parsed;
646 int bad = 0;
647 static int max_warnings = 20;
648
649 memset(&parsed, 0, sizeof(parsed));
650
651 for (i = 1; i < argc; i += 2) {
652 if (strcmp("uid", args[i]) == 0) {
653 int64_t uid;
654 if (sscanf(args[i+1], "%" SCNd64, &uid) == 1) {
655 parsed.uid = uid;
656 parsed.has_uid = true;
657 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700658 uiPrintf(state, "ParsePermArgs: invalid UID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700659 bad++;
660 }
661 continue;
662 }
663 if (strcmp("gid", args[i]) == 0) {
664 int64_t gid;
665 if (sscanf(args[i+1], "%" SCNd64, &gid) == 1) {
666 parsed.gid = gid;
667 parsed.has_gid = true;
668 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700669 uiPrintf(state, "ParsePermArgs: invalid GID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700670 bad++;
671 }
672 continue;
673 }
674 if (strcmp("mode", args[i]) == 0) {
675 int32_t mode;
676 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
677 parsed.mode = mode;
678 parsed.has_mode = true;
679 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700680 uiPrintf(state, "ParsePermArgs: invalid mode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700681 bad++;
682 }
683 continue;
684 }
685 if (strcmp("dmode", args[i]) == 0) {
686 int32_t mode;
687 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
688 parsed.dmode = mode;
689 parsed.has_dmode = true;
690 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700691 uiPrintf(state, "ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700692 bad++;
693 }
694 continue;
695 }
696 if (strcmp("fmode", args[i]) == 0) {
697 int32_t mode;
698 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
699 parsed.fmode = mode;
700 parsed.has_fmode = true;
701 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700702 uiPrintf(state, "ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700703 bad++;
704 }
705 continue;
706 }
707 if (strcmp("capabilities", args[i]) == 0) {
708 int64_t capabilities;
709 if (sscanf(args[i+1], "%" SCNi64, &capabilities) == 1) {
710 parsed.capabilities = capabilities;
711 parsed.has_capabilities = true;
712 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700713 uiPrintf(state, "ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700714 bad++;
715 }
716 continue;
717 }
718 if (strcmp("selabel", args[i]) == 0) {
719 if (args[i+1][0] != '\0') {
720 parsed.selabel = args[i+1];
721 parsed.has_selabel = true;
722 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700723 uiPrintf(state, "ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700724 bad++;
725 }
726 continue;
727 }
728 if (max_warnings != 0) {
729 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i]);
730 max_warnings--;
731 if (max_warnings == 0) {
732 printf("ParsedPermArgs: suppressing further warnings\n");
733 }
734 }
735 }
736 return parsed;
737}
738
739static int ApplyParsedPerms(
Michael Runged4a63422014-10-22 19:48:41 -0700740 State * state,
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700741 const char* filename,
742 const struct stat *statptr,
743 struct perm_parsed_args parsed)
744{
745 int bad = 0;
746
Nick Kralevich68802412014-10-23 20:36:42 -0700747 if (parsed.has_selabel) {
748 if (lsetfilecon(filename, parsed.selabel) != 0) {
749 uiPrintf(state, "ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n",
750 filename, parsed.selabel, strerror(errno));
751 bad++;
752 }
753 }
754
Nick Kraleviche4612512013-09-10 15:34:19 -0700755 /* ignore symlinks */
756 if (S_ISLNK(statptr->st_mode)) {
Nick Kralevich68802412014-10-23 20:36:42 -0700757 return bad;
Nick Kraleviche4612512013-09-10 15:34:19 -0700758 }
759
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700760 if (parsed.has_uid) {
761 if (chown(filename, parsed.uid, -1) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700762 uiPrintf(state, "ApplyParsedPerms: chown of %s to %d failed: %s\n",
763 filename, parsed.uid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700764 bad++;
765 }
766 }
767
768 if (parsed.has_gid) {
769 if (chown(filename, -1, parsed.gid) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700770 uiPrintf(state, "ApplyParsedPerms: chgrp of %s to %d failed: %s\n",
771 filename, parsed.gid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700772 bad++;
773 }
774 }
775
776 if (parsed.has_mode) {
777 if (chmod(filename, parsed.mode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700778 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
779 filename, parsed.mode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700780 bad++;
781 }
782 }
783
784 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
785 if (chmod(filename, parsed.dmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700786 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
787 filename, parsed.dmode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700788 bad++;
789 }
790 }
791
792 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
793 if (chmod(filename, parsed.fmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700794 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700795 filename, parsed.fmode, strerror(errno));
796 bad++;
797 }
798 }
799
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700800 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
801 if (parsed.capabilities == 0) {
802 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
803 // Report failure unless it's ENODATA (attribute not set)
Michael Runged4a63422014-10-22 19:48:41 -0700804 uiPrintf(state, "ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700805 filename, parsed.capabilities, strerror(errno));
806 bad++;
807 }
808 } else {
809 struct vfs_cap_data cap_data;
810 memset(&cap_data, 0, sizeof(cap_data));
811 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
812 cap_data.data[0].permitted = (uint32_t) (parsed.capabilities & 0xffffffff);
813 cap_data.data[0].inheritable = 0;
814 cap_data.data[1].permitted = (uint32_t) (parsed.capabilities >> 32);
815 cap_data.data[1].inheritable = 0;
816 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700817 uiPrintf(state, "ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n",
818 filename, parsed.capabilities, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700819 bad++;
820 }
821 }
822 }
823
824 return bad;
825}
826
827// nftw doesn't allow us to pass along context, so we need to use
828// global variables. *sigh*
829static struct perm_parsed_args recursive_parsed_args;
Michael Runged4a63422014-10-22 19:48:41 -0700830static State* recursive_state;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700831
832static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr,
833 int fileflags, struct FTW *pfwt) {
Michael Runged4a63422014-10-22 19:48:41 -0700834 return ApplyParsedPerms(recursive_state, filename, statptr, recursive_parsed_args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700835}
836
837static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700838 int bad = 0;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700839 struct stat sb;
840 Value* result = NULL;
841
842 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
843
844 if ((argc % 2) != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700845 return ErrorAbort(state, kArgsParsingFailure,
846 "%s() expects an odd number of arguments, got %d", name, argc);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700847 }
848
849 char** args = ReadVarArgs(state, argc, argv);
850 if (args == NULL) return NULL;
851
852 if (lstat(args[0], &sb) == -1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700853 result = ErrorAbort(state, kSetMetadataFailure, "%s: Error on lstat of \"%s\": %s",
854 name, args[0], strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700855 goto done;
856 }
857
Tao Baoba9a42a2015-06-23 23:23:33 -0700858 {
859 struct perm_parsed_args parsed = ParsePermArgs(state, argc, args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700860
Tao Baoba9a42a2015-06-23 23:23:33 -0700861 if (recursive) {
862 recursive_parsed_args = parsed;
863 recursive_state = state;
864 bad += nftw(args[0], do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
865 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
866 recursive_state = NULL;
867 } else {
868 bad += ApplyParsedPerms(state, args[0], &sb, parsed);
869 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700870 }
871
872done:
Tao Baoba9a42a2015-06-23 23:23:33 -0700873 for (int i = 0; i < argc; ++i) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700874 free(args[i]);
875 }
876 free(args);
877
878 if (result != NULL) {
879 return result;
880 }
881
882 if (bad > 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700883 return ErrorAbort(state, kSetMetadataFailure, "%s: some changes failed", name);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700884 }
885
886 return StringValue(strdup(""));
887}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700888
Doug Zongker512536a2010-02-17 16:11:44 -0800889Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700890 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700891 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700892 }
Tao Baoba9a42a2015-06-23 23:23:33 -0700893 char* key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700894 if (key == NULL) return NULL;
895
Elliott Hughescb220402016-09-23 15:30:55 -0700896 std::string value = android::base::GetProperty(key, "");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700897 free(key);
898
Elliott Hughescb220402016-09-23 15:30:55 -0700899 return StringValue(strdup(value.c_str()));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700900}
901
902
Doug Zongker47cace92009-06-18 10:11:50 -0700903// file_getprop(file, key)
904//
905// interprets 'file' as a getprop-style file (key=value pairs, one
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700906// per line. # comment lines,blank lines, lines without '=' ignored),
907// and returns the value for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800908Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700909 char* result = NULL;
910 char* buffer = NULL;
911 char* filename;
912 char* key;
913 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
914 return NULL;
915 }
916
917 struct stat st;
918 if (stat(filename, &st) < 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700919 ErrorAbort(state, kFileGetPropFailure, "%s: failed to stat \"%s\": %s", name, filename,
920 strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -0700921 goto done;
922 }
923
924#define MAX_FILE_GETPROP_SIZE 65536
925
926 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
Tianjie Xu16255832016-04-30 11:49:59 -0700927 ErrorAbort(state, kFileGetPropFailure, "%s too large for %s (max %d)", filename, name,
928 MAX_FILE_GETPROP_SIZE);
Doug Zongker47cace92009-06-18 10:11:50 -0700929 goto done;
930 }
931
Tao Baoba9a42a2015-06-23 23:23:33 -0700932 buffer = reinterpret_cast<char*>(malloc(st.st_size+1));
Doug Zongker47cace92009-06-18 10:11:50 -0700933 if (buffer == NULL) {
Tianjie Xu84478e82016-05-23 11:42:40 -0700934 ErrorAbort(state, kFileGetPropFailure, "%s: failed to alloc %zu bytes", name,
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700935 static_cast<size_t>(st.st_size+1));
Doug Zongker47cace92009-06-18 10:11:50 -0700936 goto done;
937 }
938
Tao Baoba9a42a2015-06-23 23:23:33 -0700939 FILE* f;
Jed Estepa7b9a462015-12-15 16:04:53 -0800940 f = ota_fopen(filename, "rb");
Doug Zongker47cace92009-06-18 10:11:50 -0700941 if (f == NULL) {
Tianjie Xu16255832016-04-30 11:49:59 -0700942 ErrorAbort(state, kFileOpenFailure, "%s: failed to open %s: %s", name, filename,
943 strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -0700944 goto done;
945 }
946
Jed Estepa7b9a462015-12-15 16:04:53 -0800947 if (ota_fread(buffer, 1, st.st_size, f) != static_cast<size_t>(st.st_size)) {
Tianjie Xu84478e82016-05-23 11:42:40 -0700948 ErrorAbort(state, kFreadFailure, "%s: failed to read %zu bytes from %s",
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700949 name, static_cast<size_t>(st.st_size), filename);
Jed Estepa7b9a462015-12-15 16:04:53 -0800950 ota_fclose(f);
Doug Zongker47cace92009-06-18 10:11:50 -0700951 goto done;
952 }
953 buffer[st.st_size] = '\0';
954
Jed Estepa7b9a462015-12-15 16:04:53 -0800955 ota_fclose(f);
Doug Zongker47cace92009-06-18 10:11:50 -0700956
Tao Baoba9a42a2015-06-23 23:23:33 -0700957 char* line;
958 line = strtok(buffer, "\n");
Doug Zongker47cace92009-06-18 10:11:50 -0700959 do {
960 // skip whitespace at start of line
961 while (*line && isspace(*line)) ++line;
962
963 // comment or blank line: skip to next line
964 if (*line == '\0' || *line == '#') continue;
965
966 char* equal = strchr(line, '=');
967 if (equal == NULL) {
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700968 continue;
Doug Zongker47cace92009-06-18 10:11:50 -0700969 }
970
971 // trim whitespace between key and '='
972 char* key_end = equal-1;
973 while (key_end > line && isspace(*key_end)) --key_end;
974 key_end[1] = '\0';
975
976 // not the key we're looking for
977 if (strcmp(key, line) != 0) continue;
978
979 // skip whitespace after the '=' to the start of the value
980 char* val_start = equal+1;
981 while(*val_start && isspace(*val_start)) ++val_start;
982
983 // trim trailing whitespace
984 char* val_end = val_start + strlen(val_start)-1;
985 while (val_end > val_start && isspace(*val_end)) --val_end;
986 val_end[1] = '\0';
987
988 result = strdup(val_start);
989 break;
990
991 } while ((line = strtok(NULL, "\n")));
992
993 if (result == NULL) result = strdup("");
994
995 done:
996 free(filename);
997 free(key);
998 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -0800999 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -07001000}
1001
Doug Zongker8edb00c2009-06-11 17:21:44 -07001002// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001003Value* ApplyPatchSpaceFn(const char* name, State* state,
1004 int argc, Expr* argv[]) {
1005 char* bytes_str;
1006 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
1007 return NULL;
1008 }
1009
Tao Baob15fd222015-09-24 11:10:51 -07001010 size_t bytes;
1011 if (!android::base::ParseUint(bytes_str, &bytes)) {
Tianjie Xu16255832016-04-30 11:49:59 -07001012 ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count\n\n",
1013 name, bytes_str);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001014 free(bytes_str);
Tao Baob15fd222015-09-24 11:10:51 -07001015 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001016 }
1017
1018 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
1019}
1020
Doug Zongker52b40362014-02-10 15:30:30 -08001021// apply_patch(file, size, init_sha1, tgt_sha1, patch)
1022
Doug Zongker512536a2010-02-17 16:11:44 -08001023Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001024 if (argc < 6 || (argc % 2) == 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001025 return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 6 args and an "
1026 "even number, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001027 }
1028
Doug Zongkerc4351c72010-02-22 14:46:32 -08001029 char* source_filename;
1030 char* target_filename;
1031 char* target_sha1;
1032 char* target_size_str;
1033 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
1034 &target_sha1, &target_size_str) < 0) {
1035 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001036 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001037
Tao Baob15fd222015-09-24 11:10:51 -07001038 size_t target_size;
1039 if (!android::base::ParseUint(target_size_str, &target_size)) {
Tianjie Xu16255832016-04-30 11:49:59 -07001040 ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count",
1041 name, target_size_str);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001042 free(source_filename);
1043 free(target_filename);
1044 free(target_sha1);
1045 free(target_size_str);
Tao Baob15fd222015-09-24 11:10:51 -07001046 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001047 }
1048
1049 int patchcount = (argc-4) / 2;
Yabin Cui64be2132016-02-03 18:16:02 -08001050 std::unique_ptr<Value*, decltype(&free)> arg_values(ReadValueVarArgs(state, argc-4, argv+4),
1051 free);
1052 if (!arg_values) {
1053 return nullptr;
1054 }
1055 std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> patch_shas;
1056 std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> patches;
1057 // Protect values by unique_ptrs first to get rid of memory leak.
1058 for (int i = 0; i < patchcount * 2; i += 2) {
1059 patch_shas.emplace_back(arg_values.get()[i], FreeValue);
1060 patches.emplace_back(arg_values.get()[i+1], FreeValue);
1061 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001062
Yabin Cui64be2132016-02-03 18:16:02 -08001063 for (int i = 0; i < patchcount; ++i) {
1064 if (patch_shas[i]->type != VAL_STRING) {
Tianjie Xu16255832016-04-30 11:49:59 -07001065 ErrorAbort(state, kArgsParsingFailure, "%s(): sha-1 #%d is not string", name, i);
Yabin Cui64be2132016-02-03 18:16:02 -08001066 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001067 }
Yabin Cui64be2132016-02-03 18:16:02 -08001068 if (patches[i]->type != VAL_BLOB) {
Tianjie Xu16255832016-04-30 11:49:59 -07001069 ErrorAbort(state, kArgsParsingFailure, "%s(): patch #%d is not blob", name, i);
Yabin Cui64be2132016-02-03 18:16:02 -08001070 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001071 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001072 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001073
Yabin Cui64be2132016-02-03 18:16:02 -08001074 std::vector<char*> patch_sha_str;
1075 std::vector<Value*> patch_ptrs;
1076 for (int i = 0; i < patchcount; ++i) {
1077 patch_sha_str.push_back(patch_shas[i]->data);
1078 patch_ptrs.push_back(patches[i].get());
Doug Zongker8edb00c2009-06-11 17:21:44 -07001079 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001080
1081 int result = applypatch(source_filename, target_filename,
1082 target_sha1, target_size,
Yabin Cui64be2132016-02-03 18:16:02 -08001083 patchcount, patch_sha_str.data(), patch_ptrs.data(), NULL);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001084
1085 return StringValue(strdup(result == 0 ? "t" : ""));
1086}
1087
1088// apply_patch_check(file, [sha1_1, ...])
1089Value* ApplyPatchCheckFn(const char* name, State* state,
1090 int argc, Expr* argv[]) {
1091 if (argc < 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001092 return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 1 arg, got %d",
Doug Zongkerc4351c72010-02-22 14:46:32 -08001093 name, argc);
1094 }
1095
1096 char* filename;
1097 if (ReadArgs(state, argv, 1, &filename) < 0) {
1098 return NULL;
1099 }
1100
1101 int patchcount = argc-1;
1102 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
1103
1104 int result = applypatch_check(filename, patchcount, sha1s);
1105
1106 int i;
1107 for (i = 0; i < patchcount; ++i) {
1108 free(sha1s[i]);
1109 }
1110 free(sha1s);
1111
1112 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001113}
1114
Tao Bao1107d962015-09-09 17:16:55 -07001115// This is the updater side handler for ui_print() in edify script. Contents
1116// will be sent over to the recovery side for on-screen display.
Doug Zongker512536a2010-02-17 16:11:44 -08001117Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001118 char** args = ReadVarArgs(state, argc, argv);
1119 if (args == NULL) {
1120 return NULL;
1121 }
1122
Tao Bao1107d962015-09-09 17:16:55 -07001123 std::string buffer;
1124 for (int i = 0; i < argc; ++i) {
1125 buffer += args[i];
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001126 free(args[i]);
1127 }
1128 free(args);
Tao Bao1107d962015-09-09 17:16:55 -07001129
1130 buffer += "\n";
Michael Runged4a63422014-10-22 19:48:41 -07001131 uiPrint(state, buffer);
Tao Bao1107d962015-09-09 17:16:55 -07001132 return StringValue(strdup(buffer.c_str()));
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001133}
1134
Doug Zongkerd0181b82011-10-19 10:51:12 -07001135Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
1136 if (argc != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001137 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %d", name, argc);
Doug Zongkerd0181b82011-10-19 10:51:12 -07001138 }
1139 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
1140 return StringValue(strdup("t"));
1141}
1142
Doug Zongker512536a2010-02-17 16:11:44 -08001143Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001144 if (argc < 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001145 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001146 }
1147 char** args = ReadVarArgs(state, argc, argv);
1148 if (args == NULL) {
1149 return NULL;
1150 }
1151
Tao Baoba9a42a2015-06-23 23:23:33 -07001152 char** args2 = reinterpret_cast<char**>(malloc(sizeof(char*) * (argc+1)));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001153 memcpy(args2, args, sizeof(char*) * argc);
1154 args2[argc] = NULL;
1155
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001156 printf("about to run program [%s] with %d args\n", args2[0], argc);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001157
1158 pid_t child = fork();
1159 if (child == 0) {
1160 execv(args2[0], args2);
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001161 printf("run_program: execv failed: %s\n", strerror(errno));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001162 _exit(1);
1163 }
1164 int status;
1165 waitpid(child, &status, 0);
1166 if (WIFEXITED(status)) {
1167 if (WEXITSTATUS(status) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001168 printf("run_program: child exited with status %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001169 WEXITSTATUS(status));
1170 }
1171 } else if (WIFSIGNALED(status)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001172 printf("run_program: child terminated by signal %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001173 WTERMSIG(status));
1174 }
1175
1176 int i;
1177 for (i = 0; i < argc; ++i) {
1178 free(args[i]);
1179 }
1180 free(args);
1181 free(args2);
1182
1183 char buffer[20];
1184 sprintf(buffer, "%d", status);
1185
Doug Zongker512536a2010-02-17 16:11:44 -08001186 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001187}
1188
Doug Zongker512536a2010-02-17 16:11:44 -08001189// sha1_check(data)
1190// to return the sha1 of the data (given in the format returned by
1191// read_file).
1192//
1193// sha1_check(data, sha1_hex, [sha1_hex, ...])
1194// returns the sha1 of the file if it matches any of the hex
1195// strings passed, or "" if it does not equal any of them.
1196//
1197Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1198 if (argc < 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001199 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
Doug Zongker512536a2010-02-17 16:11:44 -08001200 }
1201
Yabin Cui64be2132016-02-03 18:16:02 -08001202 std::unique_ptr<Value*, decltype(&free)> arg_values(ReadValueVarArgs(state, argc, argv), free);
1203 if (arg_values == nullptr) {
1204 return nullptr;
1205 }
1206 std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> args;
1207 for (int i = 0; i < argc; ++i) {
1208 args.emplace_back(arg_values.get()[i], FreeValue);
Doug Zongker512536a2010-02-17 16:11:44 -08001209 }
1210
1211 if (args[0]->size < 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001212 return StringValue(strdup(""));
1213 }
Sen Jiangc48cb5e2016-02-04 16:23:21 +08001214 uint8_t digest[SHA_DIGEST_LENGTH];
1215 SHA1(reinterpret_cast<uint8_t*>(args[0]->data), args[0]->size, digest);
Doug Zongker512536a2010-02-17 16:11:44 -08001216
1217 if (argc == 1) {
Tao Bao361342c2016-02-08 11:15:50 -08001218 return StringValue(strdup(print_sha1(digest).c_str()));
Doug Zongker512536a2010-02-17 16:11:44 -08001219 }
1220
Tao Bao361342c2016-02-08 11:15:50 -08001221 for (int i = 1; i < argc; ++i) {
1222 uint8_t arg_digest[SHA_DIGEST_LENGTH];
Doug Zongker512536a2010-02-17 16:11:44 -08001223 if (args[i]->type != VAL_STRING) {
Tao Bao361342c2016-02-08 11:15:50 -08001224 printf("%s(): arg %d is not a string; skipping", name, i);
Doug Zongker512536a2010-02-17 16:11:44 -08001225 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1226 // Warn about bad args and skip them.
Tao Bao361342c2016-02-08 11:15:50 -08001227 printf("%s(): error parsing \"%s\" as sha-1; skipping", name, args[i]->data);
Sen Jiangc48cb5e2016-02-04 16:23:21 +08001228 } else if (memcmp(digest, arg_digest, SHA_DIGEST_LENGTH) == 0) {
Tao Bao361342c2016-02-08 11:15:50 -08001229 // Found a match.
1230 return args[i].release();
Doug Zongker512536a2010-02-17 16:11:44 -08001231 }
Doug Zongker512536a2010-02-17 16:11:44 -08001232 }
Tao Bao361342c2016-02-08 11:15:50 -08001233
1234 // Didn't match any of the hex strings; return false.
1235 return StringValue(strdup(""));
Doug Zongker512536a2010-02-17 16:11:44 -08001236}
1237
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001238// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001239// is actually a FileContents*).
1240Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1241 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001242 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker512536a2010-02-17 16:11:44 -08001243 }
1244 char* filename;
1245 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1246
Yabin Cuid6c93af2016-02-10 16:41:10 -08001247 Value* v = static_cast<Value*>(malloc(sizeof(Value)));
1248 if (v == nullptr) {
1249 return nullptr;
1250 }
Doug Zongker512536a2010-02-17 16:11:44 -08001251 v->type = VAL_BLOB;
Yabin Cuid6c93af2016-02-10 16:41:10 -08001252 v->size = -1;
1253 v->data = nullptr;
Doug Zongker512536a2010-02-17 16:11:44 -08001254
1255 FileContents fc;
Tao Baoefacd802016-06-11 03:56:38 -07001256 if (LoadFileContents(filename, &fc) == 0) {
Yabin Cuid6c93af2016-02-10 16:41:10 -08001257 v->data = static_cast<char*>(malloc(fc.data.size()));
1258 if (v->data != nullptr) {
1259 memcpy(v->data, fc.data.data(), fc.data.size());
1260 v->size = fc.data.size();
1261 }
Doug Zongker512536a2010-02-17 16:11:44 -08001262 }
Doug Zongker512536a2010-02-17 16:11:44 -08001263 free(filename);
1264 return v;
1265}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001266
Doug Zongkerc87bab12013-11-25 13:53:25 -08001267// Immediately reboot the device. Recovery is not finished normally,
1268// so if you reboot into recovery it will re-start applying the
1269// current package (because nothing has cleared the copy of the
1270// arguments stored in the BCB).
1271//
1272// The argument is the partition name passed to the android reboot
1273// property. It can be "recovery" to boot from the recovery
1274// partition, or "" (empty string) to boot from the regular boot
1275// partition.
1276Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) {
1277 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -07001278 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001279 }
1280
1281 char* filename;
1282 char* property;
1283 if (ReadArgs(state, argv, 2, &filename, &property) < 0) return NULL;
1284
Doug Zongkerc87bab12013-11-25 13:53:25 -08001285 // zero out the 'command' field of the bootloader message.
Elliott Hughescb220402016-09-23 15:30:55 -07001286 char buffer[80];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001287 memset(buffer, 0, sizeof(((struct bootloader_message*)0)->command));
Jed Estepa7b9a462015-12-15 16:04:53 -08001288 FILE* f = ota_fopen(filename, "r+b");
Doug Zongkerc87bab12013-11-25 13:53:25 -08001289 fseek(f, offsetof(struct bootloader_message, command), SEEK_SET);
Jed Estepa7b9a462015-12-15 16:04:53 -08001290 ota_fwrite(buffer, sizeof(((struct bootloader_message*)0)->command), 1, f);
1291 ota_fclose(f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001292 free(filename);
1293
Elliott Hughescb220402016-09-23 15:30:55 -07001294 std::string reboot_cmd = "reboot,";
1295 if (property != nullptr) reboot_cmd += property;
1296 android::base::SetProperty(ANDROID_RB_PROPERTY, reboot_cmd);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001297
1298 sleep(5);
1299 free(property);
Tianjie Xu16255832016-04-30 11:49:59 -07001300 ErrorAbort(state, kRebootFailure, "%s() failed to reboot", name);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001301 return NULL;
1302}
1303
1304// Store a string value somewhere that future invocations of recovery
1305// can access it. This value is called the "stage" and can be used to
1306// drive packages that need to do reboots in the middle of
1307// installation and keep track of where they are in the multi-stage
1308// install.
1309//
1310// The first argument is the block device for the misc partition
1311// ("/misc" in the fstab), which is where this value is stored. The
1312// second argument is the string to store; it should not exceed 31
1313// bytes.
1314Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
1315 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -07001316 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001317 }
1318
1319 char* filename;
1320 char* stagestr;
1321 if (ReadArgs(state, argv, 2, &filename, &stagestr) < 0) return NULL;
1322
1323 // Store this value in the misc partition, immediately after the
1324 // bootloader message that the main recovery uses to save its
1325 // arguments in case of the device restarting midway through
1326 // package installation.
Jed Estepa7b9a462015-12-15 16:04:53 -08001327 FILE* f = ota_fopen(filename, "r+b");
Doug Zongkerc87bab12013-11-25 13:53:25 -08001328 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1329 int to_write = strlen(stagestr)+1;
1330 int max_size = sizeof(((struct bootloader_message*)0)->stage);
1331 if (to_write > max_size) {
1332 to_write = max_size;
1333 stagestr[max_size-1] = 0;
1334 }
Jed Estepa7b9a462015-12-15 16:04:53 -08001335 ota_fwrite(stagestr, to_write, 1, f);
1336 ota_fclose(f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001337
1338 free(stagestr);
1339 return StringValue(filename);
1340}
1341
1342// Return the value most recently saved with SetStageFn. The argument
1343// is the block device for the misc partition.
1344Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001345 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001346 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001347 }
1348
1349 char* filename;
1350 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1351
1352 char buffer[sizeof(((struct bootloader_message*)0)->stage)];
Jed Estepa7b9a462015-12-15 16:04:53 -08001353 FILE* f = ota_fopen(filename, "rb");
Doug Zongkerc87bab12013-11-25 13:53:25 -08001354 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
Jed Estepa7b9a462015-12-15 16:04:53 -08001355 ota_fread(buffer, sizeof(buffer), 1, f);
1356 ota_fclose(f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001357 buffer[sizeof(buffer)-1] = '\0';
1358
1359 return StringValue(strdup(buffer));
1360}
1361
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001362Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) {
1363 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -07001364 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001365 }
1366
1367 char* filename;
1368 char* len_str;
1369 if (ReadArgs(state, argv, 2, &filename, &len_str) < 0) return NULL;
1370
Tao Baob15fd222015-09-24 11:10:51 -07001371 size_t len;
1372 android::base::ParseUint(len_str, &len);
Jed Estepa7b9a462015-12-15 16:04:53 -08001373 int fd = ota_open(filename, O_WRONLY, 0644);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001374 int success = wipe_block_device(fd, len);
1375
1376 free(filename);
1377 free(len_str);
1378
Jed Estepa7b9a462015-12-15 16:04:53 -08001379 ota_close(fd);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001380
1381 return StringValue(strdup(success ? "t" : ""));
1382}
1383
Doug Zongkerc704e062014-05-23 08:40:35 -07001384Value* EnableRebootFn(const char* name, State* state, int argc, Expr* argv[]) {
1385 if (argc != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001386 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %d", name, argc);
Doug Zongkerc704e062014-05-23 08:40:35 -07001387 }
1388 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
1389 fprintf(ui->cmd_pipe, "enable_reboot\n");
1390 return StringValue(strdup("t"));
1391}
1392
Michael Rungeacf47db2014-11-21 00:12:28 -08001393Value* Tune2FsFn(const char* name, State* state, int argc, Expr* argv[]) {
1394 if (argc == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001395 return ErrorAbort(state, kArgsParsingFailure, "%s() expects args, got %d", name, argc);
Michael Rungeacf47db2014-11-21 00:12:28 -08001396 }
1397
1398 char** args = ReadVarArgs(state, argc, argv);
1399 if (args == NULL) {
Tianjie Xu16255832016-04-30 11:49:59 -07001400 return ErrorAbort(state, kArgsParsingFailure, "%s() could not read args", name);
Michael Rungeacf47db2014-11-21 00:12:28 -08001401 }
1402
Tao Baoba9a42a2015-06-23 23:23:33 -07001403 char** args2 = reinterpret_cast<char**>(malloc(sizeof(char*) * (argc+1)));
Michael Rungeacf47db2014-11-21 00:12:28 -08001404 // Tune2fs expects the program name as its args[0]
1405 args2[0] = strdup(name);
Tao Baoba9a42a2015-06-23 23:23:33 -07001406 for (int i = 0; i < argc; ++i) {
Michael Rungeacf47db2014-11-21 00:12:28 -08001407 args2[i + 1] = args[i];
1408 }
1409 int result = tune2fs_main(argc + 1, args2);
Tao Baoba9a42a2015-06-23 23:23:33 -07001410 for (int i = 0; i < argc; ++i) {
Michael Rungeacf47db2014-11-21 00:12:28 -08001411 free(args[i]);
1412 }
1413 free(args);
1414
1415 free(args2[0]);
1416 free(args2);
1417 if (result != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001418 return ErrorAbort(state, kTune2FsFailure, "%s() returned error code %d",
1419 name, result);
Michael Rungeacf47db2014-11-21 00:12:28 -08001420 }
1421 return StringValue(strdup("t"));
1422}
1423
Doug Zongker9931f7f2009-06-10 14:11:53 -07001424void RegisterInstallFunctions() {
1425 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001426 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001427 RegisterFunction("unmount", UnmountFn);
1428 RegisterFunction("format", FormatFn);
1429 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001430 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001431 RegisterFunction("delete", DeleteFn);
1432 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001433 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1434 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001435 RegisterFunction("symlink", SymlinkFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001436
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001437 // Usage:
1438 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1439 // Example:
1440 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1441 RegisterFunction("set_metadata", SetMetadataFn);
1442
1443 // Usage:
1444 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1445 // Example:
1446 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1447 RegisterFunction("set_metadata_recursive", SetMetadataFn);
1448
Doug Zongker8edb00c2009-06-11 17:21:44 -07001449 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001450 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001451
1452 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001453 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1454 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001455
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001456 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001457
Doug Zongker512536a2010-02-17 16:11:44 -08001458 RegisterFunction("read_file", ReadFileFn);
1459 RegisterFunction("sha1_check", Sha1CheckFn);
Michael Rungece7ca712013-11-06 17:42:20 -08001460 RegisterFunction("rename", RenameFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001461
Doug Zongkerd0181b82011-10-19 10:51:12 -07001462 RegisterFunction("wipe_cache", WipeCacheFn);
1463
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001464 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001465
1466 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001467
1468 RegisterFunction("reboot_now", RebootNowFn);
1469 RegisterFunction("get_stage", GetStageFn);
1470 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -07001471
1472 RegisterFunction("enable_reboot", EnableRebootFn);
Michael Rungeacf47db2014-11-21 00:12:28 -08001473 RegisterFunction("tune2fs", Tune2FsFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001474}