blob: 24e35cfb809a82998e16b69f1bcbd87659e9eddd [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
Doug Zongkerfbf3c102009-06-24 09:36:20 -070017#include <ctype.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070018#include <errno.h>
19#include <stdarg.h>
Doug Zongkerfbf3c102009-06-24 09:36:20 -070020#include <stdio.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070021#include <stdlib.h>
22#include <string.h>
23#include <sys/mount.h>
24#include <sys/stat.h>
25#include <sys/types.h>
Doug Zongkera3f89ea2009-09-10 14:10:48 -070026#include <sys/wait.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070027#include <unistd.h>
Hristo Bojinovdb314d62010-08-02 10:29:49 -070028#include <fcntl.h>
29#include <time.h>
Nick Kralevich5dbdef02013-09-07 14:41:06 -070030#include <ftw.h>
31#include <sys/capability.h>
32#include <sys/xattr.h>
33#include <linux/xattr.h>
34#include <inttypes.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070035
Yabin Cui64be2132016-02-03 18:16:02 -080036#include <memory>
37#include <vector>
38
Elliott Hughes4b166f02015-12-04 15:30:20 -080039#include <android-base/parseint.h>
Elliott Hughescb220402016-09-23 15:30:55 -070040#include <android-base/properties.h>
Elliott Hughes4b166f02015-12-04 15:30:20 -080041#include <android-base/strings.h>
42#include <android-base/stringprintf.h>
Tao Baode40ba52016-10-05 23:17:01 -070043#include <ext4_utils/make_ext4fs.h>
44#include <ext4_utils/wipe.h>
Elliott Hughes4bbd5bf2016-04-01 18:24:39 -070045#include <selinux/label.h>
46#include <selinux/selinux.h>
Tao Bao1107d962015-09-09 17:16:55 -070047
Doug Zongkerc87bab12013-11-25 13:53:25 -080048#include "bootloader.h"
49#include "applypatch/applypatch.h"
50#include "cutils/android_reboot.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070051#include "cutils/misc.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070052#include "edify/expr.h"
Tianjie Xu16255832016-04-30 11:49:59 -070053#include "error_code.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070054#include "minzip/DirUtil.h"
Elliott Hughes63a31922016-06-09 17:41:22 -070055#include "mounts.h"
Tianjie Xu16255832016-04-30 11:49:59 -070056#include "openssl/sha.h"
Jed Estep39c1b5e2015-12-15 16:04:53 -080057#include "ota_io.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070058#include "updater.h"
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -080059#include "install.h"
Michael Rungeacf47db2014-11-21 00:12:28 -080060#include "tune2fs.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070061
Tao Bao1107d962015-09-09 17:16:55 -070062// Send over the buffer to recovery though the command pipe.
63static void uiPrint(State* state, const std::string& buffer) {
64 UpdaterInfo* ui = reinterpret_cast<UpdaterInfo*>(state->cookie);
Tao Baob6918c72015-05-19 17:02:16 -070065
Tao Bao1107d962015-09-09 17:16:55 -070066 // "line1\nline2\n" will be split into 3 tokens: "line1", "line2" and "".
67 // So skip sending empty strings to UI.
68 std::vector<std::string> lines = android::base::Split(buffer, "\n");
69 for (auto& line: lines) {
70 if (!line.empty()) {
71 fprintf(ui->cmd_pipe, "ui_print %s\n", line.c_str());
72 fprintf(ui->cmd_pipe, "ui_print\n");
73 }
74 }
75
76 // On the updater side, we need to dump the contents to stderr (which has
77 // been redirected to the log file). Because the recovery will only print
78 // the contents to screen when processing pipe command ui_print.
79 fprintf(stderr, "%s", buffer.c_str());
Michael Runged4a63422014-10-22 19:48:41 -070080}
81
Elliott Hughes83ce7552016-06-30 09:28:42 -070082void uiPrintf(State* _Nonnull state, const char* _Nonnull format, ...) {
Tao Bao1107d962015-09-09 17:16:55 -070083 std::string error_msg;
84
Michael Runged4a63422014-10-22 19:48:41 -070085 va_list ap;
86 va_start(ap, format);
Tao Bao1107d962015-09-09 17:16:55 -070087 android::base::StringAppendV(&error_msg, format, ap);
Michael Runged4a63422014-10-22 19:48:41 -070088 va_end(ap);
Tao Bao1107d962015-09-09 17:16:55 -070089
Michael Runged4a63422014-10-22 19:48:41 -070090 uiPrint(state, error_msg);
91}
92
Doug Zongker52b40362014-02-10 15:30:30 -080093// Take a sha-1 digest and return it as a newly-allocated hex string.
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070094char* PrintSha1(const uint8_t* digest) {
Sen Jiangc48cb5e2016-02-04 16:23:21 +080095 char* buffer = reinterpret_cast<char*>(malloc(SHA_DIGEST_LENGTH*2 + 1));
Doug Zongker52b40362014-02-10 15:30:30 -080096 const char* alphabet = "0123456789abcdef";
Tao Baoba9a42a2015-06-23 23:23:33 -070097 size_t i;
Sen Jiangc48cb5e2016-02-04 16:23:21 +080098 for (i = 0; i < SHA_DIGEST_LENGTH; ++i) {
Doug Zongker52b40362014-02-10 15:30:30 -080099 buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf];
100 buffer[i*2+1] = alphabet[digest[i] & 0xf];
101 }
102 buffer[i*2] = '\0';
103 return buffer;
104}
105
Doug Zongker3d177d02010-07-01 09:18:44 -0700106// mount(fs_type, partition_type, location, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700107//
Doug Zongker3d177d02010-07-01 09:18:44 -0700108// fs_type="ext4" partition_type="EMMC" location=device
Doug Zongker512536a2010-02-17 16:11:44 -0800109Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700110 char* result = NULL;
Michael Runge168f7772014-10-22 17:05:08 -0700111 if (argc != 4 && argc != 5) {
Tianjie Xu16255832016-04-30 11:49:59 -0700112 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 4-5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700113 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700114 char* fs_type;
115 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700116 char* location;
117 char* mount_point;
Michael Runge168f7772014-10-22 17:05:08 -0700118 char* mount_options;
119 bool has_mount_options;
120 if (argc == 5) {
121 has_mount_options = true;
122 if (ReadArgs(state, argv, 5, &fs_type, &partition_type,
123 &location, &mount_point, &mount_options) < 0) {
124 return NULL;
125 }
126 } else {
127 has_mount_options = false;
128 if (ReadArgs(state, argv, 4, &fs_type, &partition_type,
Doug Zongker3d177d02010-07-01 09:18:44 -0700129 &location, &mount_point) < 0) {
Michael Runge168f7772014-10-22 17:05:08 -0700130 return NULL;
131 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700132 }
133
Doug Zongker3d177d02010-07-01 09:18:44 -0700134 if (strlen(fs_type) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700135 ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
Doug Zongker3d177d02010-07-01 09:18:44 -0700136 goto done;
137 }
138 if (strlen(partition_type) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700139 ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
Doug Zongker3d177d02010-07-01 09:18:44 -0700140 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700141 goto done;
142 }
143 if (strlen(location) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700144 ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700145 goto done;
146 }
147 if (strlen(mount_point) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700148 ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
149 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700150 goto done;
151 }
152
Tao Baoba9a42a2015-06-23 23:23:33 -0700153 {
154 char *secontext = NULL;
Stephen Smalley779701d2012-02-09 14:13:23 -0500155
Tao Baoba9a42a2015-06-23 23:23:33 -0700156 if (sehandle) {
157 selabel_lookup(sehandle, &secontext, mount_point, 0755);
158 setfscreatecon(secontext);
159 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500160
Tao Baoba9a42a2015-06-23 23:23:33 -0700161 mkdir(mount_point, 0755);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700162
Tao Baoba9a42a2015-06-23 23:23:33 -0700163 if (secontext) {
164 freecon(secontext);
165 setfscreatecon(NULL);
166 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500167 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500168
Elliott Hughes63a31922016-06-09 17:41:22 -0700169 if (mount(location, mount_point, fs_type,
170 MS_NOATIME | MS_NODEV | MS_NODIRATIME,
171 has_mount_options ? mount_options : "") < 0) {
172 uiPrintf(state, "%s: failed to mount %s at %s: %s\n",
173 name, location, mount_point, strerror(errno));
174 result = strdup("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700175 } else {
Elliott Hughes63a31922016-06-09 17:41:22 -0700176 result = mount_point;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700177 }
178
179done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700180 free(fs_type);
181 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700182 free(location);
183 if (result != mount_point) free(mount_point);
Michael Runge168f7772014-10-22 17:05:08 -0700184 if (has_mount_options) free(mount_options);
Doug Zongker512536a2010-02-17 16:11:44 -0800185 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700186}
187
Doug Zongker8edb00c2009-06-11 17:21:44 -0700188
189// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800190Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700191 char* result = NULL;
192 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700193 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700194 }
195 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700196 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700197 return NULL;
198 }
199 if (strlen(mount_point) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700200 ErrorAbort(state, kArgsParsingFailure, "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700201 goto done;
202 }
203
204 scan_mounted_volumes();
Tao Baoba9a42a2015-06-23 23:23:33 -0700205 {
Elliott Hughes63a31922016-06-09 17:41:22 -0700206 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
Tao Baoba9a42a2015-06-23 23:23:33 -0700207 if (vol == NULL) {
208 result = strdup("");
209 } else {
210 result = mount_point;
211 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700212 }
213
214done:
215 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800216 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700217}
218
219
Doug Zongker512536a2010-02-17 16:11:44 -0800220Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700221 char* result = NULL;
222 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700223 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700224 }
225 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700226 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700227 return NULL;
228 }
229 if (strlen(mount_point) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700230 ErrorAbort(state, kArgsParsingFailure, "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700231 goto done;
232 }
233
234 scan_mounted_volumes();
Tao Baoba9a42a2015-06-23 23:23:33 -0700235 {
Elliott Hughes63a31922016-06-09 17:41:22 -0700236 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
Tao Baoba9a42a2015-06-23 23:23:33 -0700237 if (vol == NULL) {
238 uiPrintf(state, "unmount of %s failed; no such volume\n", mount_point);
239 result = strdup("");
240 } else {
241 int ret = unmount_mounted_volume(vol);
242 if (ret != 0) {
243 uiPrintf(state, "unmount of %s failed (%d): %s\n",
244 mount_point, ret, strerror(errno));
245 }
246 result = mount_point;
Michael Runge5ddf4292014-10-24 14:14:41 -0700247 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700248 }
249
250done:
251 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800252 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700253}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700254
JP Abgrall37aedb32014-06-16 19:07:39 -0700255static int exec_cmd(const char* path, char* const argv[]) {
256 int status;
257 pid_t child;
258 if ((child = vfork()) == 0) {
259 execv(path, argv);
260 _exit(-1);
261 }
262 waitpid(child, &status, 0);
263 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
264 printf("%s failed with status %d\n", path, WEXITSTATUS(status));
265 }
266 return WEXITSTATUS(status);
267}
268
Doug Zongker8edb00c2009-06-11 17:21:44 -0700269
Stephen Smalley779701d2012-02-09 14:13:23 -0500270// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700271//
Stephen Smalley779701d2012-02-09 14:13:23 -0500272// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700273// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
274// if fs_size == 0, then make fs uses the entire partition.
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800275// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700276// 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 -0800277Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700278 char* result = NULL;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400279 if (argc != 5) {
Tianjie Xu16255832016-04-30 11:49:59 -0700280 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700281 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700282 char* fs_type;
283 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700284 char* location;
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800285 char* fs_size;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400286 char* mount_point;
Stephen Smalley779701d2012-02-09 14:13:23 -0500287
Stephen Smalley779701d2012-02-09 14:13:23 -0500288 if (ReadArgs(state, argv, 5, &fs_type, &partition_type, &location, &fs_size, &mount_point) < 0) {
289 return NULL;
290 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700291
Doug Zongker3d177d02010-07-01 09:18:44 -0700292 if (strlen(fs_type) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700293 ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
Doug Zongker3d177d02010-07-01 09:18:44 -0700294 goto done;
295 }
296 if (strlen(partition_type) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700297 ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
Doug Zongker3d177d02010-07-01 09:18:44 -0700298 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700299 goto done;
300 }
301 if (strlen(location) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700302 ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700303 goto done;
304 }
305
Stephen Smalley516e4e22012-04-03 13:35:11 -0400306 if (strlen(mount_point) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700307 ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
308 name);
Stephen Smalley779701d2012-02-09 14:13:23 -0500309 goto done;
310 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500311
Elliott Hughes63a31922016-06-09 17:41:22 -0700312 if (strcmp(fs_type, "ext4") == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500313 int status = make_ext4fs(location, atoll(fs_size), mount_point, sehandle);
Doug Zongker3d177d02010-07-01 09:18:44 -0700314 if (status != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700315 printf("%s: make_ext4fs failed (%d) on %s",
Doug Zongker3d177d02010-07-01 09:18:44 -0700316 name, status, location);
317 result = strdup("");
318 goto done;
319 }
320 result = location;
JP Abgrall37aedb32014-06-16 19:07:39 -0700321 } else if (strcmp(fs_type, "f2fs") == 0) {
322 char *num_sectors;
323 if (asprintf(&num_sectors, "%lld", atoll(fs_size) / 512) <= 0) {
324 printf("format_volume: failed to create %s command for %s\n", fs_type, location);
325 result = strdup("");
326 goto done;
327 }
328 const char *f2fs_path = "/sbin/mkfs.f2fs";
329 const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", location, num_sectors, NULL};
330 int status = exec_cmd(f2fs_path, (char* const*)f2fs_argv);
331 free(num_sectors);
332 if (status != 0) {
333 printf("%s: mkfs.f2fs failed (%d) on %s",
334 name, status, location);
335 result = strdup("");
336 goto done;
337 }
338 result = location;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700339 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700340 printf("%s: unsupported fs_type \"%s\" partition_type \"%s\"",
Doug Zongker3d177d02010-07-01 09:18:44 -0700341 name, fs_type, partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700342 }
343
344done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700345 free(fs_type);
346 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700347 if (result != location) free(location);
Doug Zongker512536a2010-02-17 16:11:44 -0800348 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700349}
350
Michael Rungece7ca712013-11-06 17:42:20 -0800351Value* RenameFn(const char* name, State* state, int argc, Expr* argv[]) {
352 char* result = NULL;
353 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700354 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Michael Rungece7ca712013-11-06 17:42:20 -0800355 }
356
357 char* src_name;
358 char* dst_name;
359
360 if (ReadArgs(state, argv, 2, &src_name, &dst_name) < 0) {
361 return NULL;
362 }
363 if (strlen(src_name) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700364 ErrorAbort(state, kArgsParsingFailure, "src_name argument to %s() can't be empty", name);
Michael Rungece7ca712013-11-06 17:42:20 -0800365 goto done;
366 }
367 if (strlen(dst_name) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700368 ErrorAbort(state, kArgsParsingFailure, "dst_name argument to %s() can't be empty", name);
Michael Rungece7ca712013-11-06 17:42:20 -0800369 goto done;
370 }
Michael Rungea91ecc52014-07-21 17:40:02 -0700371 if (make_parents(dst_name) != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700372 ErrorAbort(state, kFileRenameFailure, "Creating parent of %s failed, error %s",
Michael Rungea91ecc52014-07-21 17:40:02 -0700373 dst_name, strerror(errno));
Michael Runge2f0ef732014-10-22 14:28:23 -0700374 } else if (access(dst_name, F_OK) == 0 && access(src_name, F_OK) != 0) {
375 // File was already moved
376 result = dst_name;
Michael Rungea91ecc52014-07-21 17:40:02 -0700377 } else if (rename(src_name, dst_name) != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700378 ErrorAbort(state, kFileRenameFailure, "Rename of %s to %s failed, error %s",
Michael Rungece7ca712013-11-06 17:42:20 -0800379 src_name, dst_name, strerror(errno));
380 } else {
381 result = dst_name;
382 }
383
384done:
385 free(src_name);
386 if (result != dst_name) free(dst_name);
387 return StringValue(result);
388}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700389
Doug Zongker512536a2010-02-17 16:11:44 -0800390Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700391 char** paths = reinterpret_cast<char**>(malloc(argc * sizeof(char*)));
392 for (int i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700393 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700394 if (paths[i] == NULL) {
Yabin Cui64be2132016-02-03 18:16:02 -0800395 for (int j = 0; j < i; ++j) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700396 free(paths[j]);
397 }
398 free(paths);
399 return NULL;
400 }
401 }
402
403 bool recursive = (strcmp(name, "delete_recursive") == 0);
404
405 int success = 0;
Tao Baoba9a42a2015-06-23 23:23:33 -0700406 for (int i = 0; i < argc; ++i) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700407 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
408 ++success;
409 free(paths[i]);
410 }
411 free(paths);
412
413 char buffer[10];
414 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800415 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700416}
417
Doug Zongker8edb00c2009-06-11 17:21:44 -0700418
Doug Zongker512536a2010-02-17 16:11:44 -0800419Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700420 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700421 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700422 }
423 char* frac_str;
424 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700425 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700426 return NULL;
427 }
428
429 double frac = strtod(frac_str, NULL);
Tao Baob15fd222015-09-24 11:10:51 -0700430 int sec;
431 android::base::ParseInt(sec_str, &sec);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700432
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700433 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700434 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
435
Doug Zongker9931f7f2009-06-10 14:11:53 -0700436 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800437 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700438}
439
Doug Zongker512536a2010-02-17 16:11:44 -0800440Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700441 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700442 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700443 }
444 char* frac_str;
445 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
446 return NULL;
447 }
448
449 double frac = strtod(frac_str, NULL);
450
451 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
452 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
453
Doug Zongker512536a2010-02-17 16:11:44 -0800454 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700455}
456
Doug Zongker8edb00c2009-06-11 17:21:44 -0700457// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800458Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700459 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700460 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700461 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700462 }
463 char* zip_path;
464 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700465 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700466
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700467 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700468
469 // To create a consistent system image, never use the clock for timestamps.
470 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
471
472 bool success = mzExtractRecursive(za, zip_path, dest_path,
Narayan Kamath9c0f5d62015-02-23 14:09:31 +0000473 &timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500474 NULL, NULL, sehandle);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700475 free(zip_path);
476 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800477 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700478}
479
Doug Zongker8edb00c2009-06-11 17:21:44 -0700480
481// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800482// or
483// package_extract_file(package_path)
484// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800485// function (the char* returned is actually a FileContents*).
486Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700487 int argc, Expr* argv[]) {
Doug Zongker5f875bf2014-08-22 14:53:43 -0700488 if (argc < 1 || argc > 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700489 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 or 2 args, got %d",
Doug Zongker6aece332010-02-01 14:40:12 -0800490 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700491 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700492 bool success = false;
Doug Zongker43772d22014-06-09 14:13:19 -0700493
Doug Zongker5f875bf2014-08-22 14:53:43 -0700494 if (argc == 2) {
495 // The two-argument version extracts to a file.
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800496
497 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700498
Doug Zongker6aece332010-02-01 14:40:12 -0800499 char* zip_path;
500 char* dest_path;
Doug Zongker5f875bf2014-08-22 14:53:43 -0700501 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800502
Doug Zongker6aece332010-02-01 14:40:12 -0800503 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
504 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700505 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800506 goto done2;
507 }
508
Tao Baoba9a42a2015-06-23 23:23:33 -0700509 {
Alistair Strachan733285f2016-05-05 16:04:32 -0700510 int fd = TEMP_FAILURE_RETRY(ota_open(dest_path, O_WRONLY | O_CREAT | O_TRUNC,
Tao Baod3cac342015-12-14 15:53:25 -0800511 S_IRUSR | S_IWUSR));
512 if (fd == -1) {
513 printf("%s: can't open %s for write: %s\n", name, dest_path, strerror(errno));
Tao Baoba9a42a2015-06-23 23:23:33 -0700514 goto done2;
515 }
Tao Baod3cac342015-12-14 15:53:25 -0800516 success = mzExtractZipEntryToFile(za, entry, fd);
Jed Estepa7b9a462015-12-15 16:04:53 -0800517 if (ota_fsync(fd) == -1) {
Tao Baod3cac342015-12-14 15:53:25 -0800518 printf("fsync of \"%s\" failed: %s\n", dest_path, strerror(errno));
519 success = false;
520 }
Jed Estepa7b9a462015-12-15 16:04:53 -0800521 if (ota_close(fd) == -1) {
Tao Baod3cac342015-12-14 15:53:25 -0800522 printf("close of \"%s\" failed: %s\n", dest_path, strerror(errno));
523 success = false;
524 }
Doug Zongker6aece332010-02-01 14:40:12 -0800525 }
Doug Zongker6aece332010-02-01 14:40:12 -0800526
527 done2:
528 free(zip_path);
529 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800530 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800531 } else {
532 // The one-argument version returns the contents of the file
533 // as the result.
534
535 char* zip_path;
Yabin Cui64be2132016-02-03 18:16:02 -0800536 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
537
Tao Baoba9a42a2015-06-23 23:23:33 -0700538 Value* v = reinterpret_cast<Value*>(malloc(sizeof(Value)));
Doug Zongker512536a2010-02-17 16:11:44 -0800539 v->type = VAL_BLOB;
540 v->size = -1;
541 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800542
Doug Zongker6aece332010-02-01 14:40:12 -0800543 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
544 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
545 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700546 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800547 goto done1;
548 }
549
Doug Zongker512536a2010-02-17 16:11:44 -0800550 v->size = mzGetZipEntryUncompLen(entry);
Tao Baoba9a42a2015-06-23 23:23:33 -0700551 v->data = reinterpret_cast<char*>(malloc(v->size));
Doug Zongker512536a2010-02-17 16:11:44 -0800552 if (v->data == NULL) {
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700553 printf("%s: failed to allocate %zd bytes for %s\n",
554 name, v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800555 goto done1;
556 }
557
Doug Zongker512536a2010-02-17 16:11:44 -0800558 success = mzExtractZipEntryToBuffer(za, entry,
559 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800560
561 done1:
562 free(zip_path);
563 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800564 free(v->data);
565 v->data = NULL;
566 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800567 }
Doug Zongker512536a2010-02-17 16:11:44 -0800568 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700569 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700570}
571
Doug Zongkera23075f2012-08-06 16:19:09 -0700572// Create all parent directories of name, if necessary.
573static int make_parents(char* name) {
574 char* p;
575 for (p = name + (strlen(name)-1); p > name; --p) {
576 if (*p != '/') continue;
577 *p = '\0';
578 if (make_parents(name) < 0) return -1;
579 int result = mkdir(name, 0700);
Michael Rungea91ecc52014-07-21 17:40:02 -0700580 if (result == 0) printf("created [%s]\n", name);
Doug Zongkera23075f2012-08-06 16:19:09 -0700581 *p = '/';
582 if (result == 0 || errno == EEXIST) {
583 // successfully created or already existed; we're done
584 return 0;
585 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700586 printf("failed to mkdir %s: %s\n", name, strerror(errno));
Doug Zongkera23075f2012-08-06 16:19:09 -0700587 return -1;
588 }
589 }
590 return 0;
591}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700592
Doug Zongker9931f7f2009-06-10 14:11:53 -0700593// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700594// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800595Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700596 if (argc == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700597 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700598 }
599 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700600 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700601 if (target == NULL) return NULL;
602
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700603 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700604 if (srcs == NULL) {
605 free(target);
606 return NULL;
607 }
608
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700609 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700610 int i;
611 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700612 if (unlink(srcs[i]) < 0) {
613 if (errno != ENOENT) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700614 printf("%s: failed to remove %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700615 name, srcs[i], strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700616 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700617 }
618 }
Doug Zongkera23075f2012-08-06 16:19:09 -0700619 if (make_parents(srcs[i])) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700620 printf("%s: failed to symlink %s to %s: making parents failed\n",
Doug Zongkera23075f2012-08-06 16:19:09 -0700621 name, srcs[i], target);
622 ++bad;
623 }
Doug Zongker60babf82009-09-18 15:11:24 -0700624 if (symlink(target, srcs[i]) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700625 printf("%s: failed to symlink %s to %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700626 name, srcs[i], target, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700627 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700628 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700629 free(srcs[i]);
630 }
631 free(srcs);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700632 if (bad) {
Tianjie Xu16255832016-04-30 11:49:59 -0700633 return ErrorAbort(state, kSymlinkFailure, "%s: some symlinks failed", name);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700634 }
Doug Zongker512536a2010-02-17 16:11:44 -0800635 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700636}
637
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700638struct perm_parsed_args {
639 bool has_uid;
640 uid_t uid;
641 bool has_gid;
642 gid_t gid;
643 bool has_mode;
644 mode_t mode;
645 bool has_fmode;
646 mode_t fmode;
647 bool has_dmode;
648 mode_t dmode;
649 bool has_selabel;
650 char* selabel;
651 bool has_capabilities;
652 uint64_t capabilities;
653};
654
Michael Runged4a63422014-10-22 19:48:41 -0700655static struct perm_parsed_args ParsePermArgs(State * state, int argc, char** args) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700656 int i;
657 struct perm_parsed_args parsed;
658 int bad = 0;
659 static int max_warnings = 20;
660
661 memset(&parsed, 0, sizeof(parsed));
662
663 for (i = 1; i < argc; i += 2) {
664 if (strcmp("uid", args[i]) == 0) {
665 int64_t uid;
666 if (sscanf(args[i+1], "%" SCNd64, &uid) == 1) {
667 parsed.uid = uid;
668 parsed.has_uid = true;
669 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700670 uiPrintf(state, "ParsePermArgs: invalid UID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700671 bad++;
672 }
673 continue;
674 }
675 if (strcmp("gid", args[i]) == 0) {
676 int64_t gid;
677 if (sscanf(args[i+1], "%" SCNd64, &gid) == 1) {
678 parsed.gid = gid;
679 parsed.has_gid = true;
680 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700681 uiPrintf(state, "ParsePermArgs: invalid GID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700682 bad++;
683 }
684 continue;
685 }
686 if (strcmp("mode", args[i]) == 0) {
687 int32_t mode;
688 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
689 parsed.mode = mode;
690 parsed.has_mode = true;
691 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700692 uiPrintf(state, "ParsePermArgs: invalid mode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700693 bad++;
694 }
695 continue;
696 }
697 if (strcmp("dmode", args[i]) == 0) {
698 int32_t mode;
699 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
700 parsed.dmode = mode;
701 parsed.has_dmode = true;
702 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700703 uiPrintf(state, "ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700704 bad++;
705 }
706 continue;
707 }
708 if (strcmp("fmode", args[i]) == 0) {
709 int32_t mode;
710 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
711 parsed.fmode = mode;
712 parsed.has_fmode = true;
713 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700714 uiPrintf(state, "ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700715 bad++;
716 }
717 continue;
718 }
719 if (strcmp("capabilities", args[i]) == 0) {
720 int64_t capabilities;
721 if (sscanf(args[i+1], "%" SCNi64, &capabilities) == 1) {
722 parsed.capabilities = capabilities;
723 parsed.has_capabilities = true;
724 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700725 uiPrintf(state, "ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700726 bad++;
727 }
728 continue;
729 }
730 if (strcmp("selabel", args[i]) == 0) {
731 if (args[i+1][0] != '\0') {
732 parsed.selabel = args[i+1];
733 parsed.has_selabel = true;
734 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700735 uiPrintf(state, "ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700736 bad++;
737 }
738 continue;
739 }
740 if (max_warnings != 0) {
741 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i]);
742 max_warnings--;
743 if (max_warnings == 0) {
744 printf("ParsedPermArgs: suppressing further warnings\n");
745 }
746 }
747 }
748 return parsed;
749}
750
751static int ApplyParsedPerms(
Michael Runged4a63422014-10-22 19:48:41 -0700752 State * state,
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700753 const char* filename,
754 const struct stat *statptr,
755 struct perm_parsed_args parsed)
756{
757 int bad = 0;
758
Nick Kralevich68802412014-10-23 20:36:42 -0700759 if (parsed.has_selabel) {
760 if (lsetfilecon(filename, parsed.selabel) != 0) {
761 uiPrintf(state, "ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n",
762 filename, parsed.selabel, strerror(errno));
763 bad++;
764 }
765 }
766
Nick Kraleviche4612512013-09-10 15:34:19 -0700767 /* ignore symlinks */
768 if (S_ISLNK(statptr->st_mode)) {
Nick Kralevich68802412014-10-23 20:36:42 -0700769 return bad;
Nick Kraleviche4612512013-09-10 15:34:19 -0700770 }
771
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700772 if (parsed.has_uid) {
773 if (chown(filename, parsed.uid, -1) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700774 uiPrintf(state, "ApplyParsedPerms: chown of %s to %d failed: %s\n",
775 filename, parsed.uid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700776 bad++;
777 }
778 }
779
780 if (parsed.has_gid) {
781 if (chown(filename, -1, parsed.gid) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700782 uiPrintf(state, "ApplyParsedPerms: chgrp of %s to %d failed: %s\n",
783 filename, parsed.gid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700784 bad++;
785 }
786 }
787
788 if (parsed.has_mode) {
789 if (chmod(filename, parsed.mode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700790 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
791 filename, parsed.mode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700792 bad++;
793 }
794 }
795
796 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
797 if (chmod(filename, parsed.dmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700798 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
799 filename, parsed.dmode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700800 bad++;
801 }
802 }
803
804 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
805 if (chmod(filename, parsed.fmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700806 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700807 filename, parsed.fmode, strerror(errno));
808 bad++;
809 }
810 }
811
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700812 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
813 if (parsed.capabilities == 0) {
814 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
815 // Report failure unless it's ENODATA (attribute not set)
Michael Runged4a63422014-10-22 19:48:41 -0700816 uiPrintf(state, "ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700817 filename, parsed.capabilities, strerror(errno));
818 bad++;
819 }
820 } else {
821 struct vfs_cap_data cap_data;
822 memset(&cap_data, 0, sizeof(cap_data));
823 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
824 cap_data.data[0].permitted = (uint32_t) (parsed.capabilities & 0xffffffff);
825 cap_data.data[0].inheritable = 0;
826 cap_data.data[1].permitted = (uint32_t) (parsed.capabilities >> 32);
827 cap_data.data[1].inheritable = 0;
828 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700829 uiPrintf(state, "ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n",
830 filename, parsed.capabilities, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700831 bad++;
832 }
833 }
834 }
835
836 return bad;
837}
838
839// nftw doesn't allow us to pass along context, so we need to use
840// global variables. *sigh*
841static struct perm_parsed_args recursive_parsed_args;
Michael Runged4a63422014-10-22 19:48:41 -0700842static State* recursive_state;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700843
844static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr,
845 int fileflags, struct FTW *pfwt) {
Michael Runged4a63422014-10-22 19:48:41 -0700846 return ApplyParsedPerms(recursive_state, filename, statptr, recursive_parsed_args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700847}
848
849static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700850 int bad = 0;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700851 struct stat sb;
852 Value* result = NULL;
853
854 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
855
856 if ((argc % 2) != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700857 return ErrorAbort(state, kArgsParsingFailure,
858 "%s() expects an odd number of arguments, got %d", name, argc);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700859 }
860
861 char** args = ReadVarArgs(state, argc, argv);
862 if (args == NULL) return NULL;
863
864 if (lstat(args[0], &sb) == -1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700865 result = ErrorAbort(state, kSetMetadataFailure, "%s: Error on lstat of \"%s\": %s",
866 name, args[0], strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700867 goto done;
868 }
869
Tao Baoba9a42a2015-06-23 23:23:33 -0700870 {
871 struct perm_parsed_args parsed = ParsePermArgs(state, argc, args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700872
Tao Baoba9a42a2015-06-23 23:23:33 -0700873 if (recursive) {
874 recursive_parsed_args = parsed;
875 recursive_state = state;
876 bad += nftw(args[0], do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
877 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
878 recursive_state = NULL;
879 } else {
880 bad += ApplyParsedPerms(state, args[0], &sb, parsed);
881 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700882 }
883
884done:
Tao Baoba9a42a2015-06-23 23:23:33 -0700885 for (int i = 0; i < argc; ++i) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700886 free(args[i]);
887 }
888 free(args);
889
890 if (result != NULL) {
891 return result;
892 }
893
894 if (bad > 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700895 return ErrorAbort(state, kSetMetadataFailure, "%s: some changes failed", name);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700896 }
897
898 return StringValue(strdup(""));
899}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700900
Doug Zongker512536a2010-02-17 16:11:44 -0800901Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700902 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700903 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700904 }
Tao Baoba9a42a2015-06-23 23:23:33 -0700905 char* key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700906 if (key == NULL) return NULL;
907
Elliott Hughescb220402016-09-23 15:30:55 -0700908 std::string value = android::base::GetProperty(key, "");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700909 free(key);
910
Elliott Hughescb220402016-09-23 15:30:55 -0700911 return StringValue(strdup(value.c_str()));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700912}
913
914
Doug Zongker47cace92009-06-18 10:11:50 -0700915// file_getprop(file, key)
916//
917// interprets 'file' as a getprop-style file (key=value pairs, one
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700918// per line. # comment lines,blank lines, lines without '=' ignored),
919// and returns the value for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800920Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700921 char* result = NULL;
922 char* buffer = NULL;
923 char* filename;
924 char* key;
925 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
926 return NULL;
927 }
928
929 struct stat st;
930 if (stat(filename, &st) < 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700931 ErrorAbort(state, kFileGetPropFailure, "%s: failed to stat \"%s\": %s", name, filename,
932 strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -0700933 goto done;
934 }
935
936#define MAX_FILE_GETPROP_SIZE 65536
937
938 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
Tianjie Xu16255832016-04-30 11:49:59 -0700939 ErrorAbort(state, kFileGetPropFailure, "%s too large for %s (max %d)", filename, name,
940 MAX_FILE_GETPROP_SIZE);
Doug Zongker47cace92009-06-18 10:11:50 -0700941 goto done;
942 }
943
Tao Baoba9a42a2015-06-23 23:23:33 -0700944 buffer = reinterpret_cast<char*>(malloc(st.st_size+1));
Doug Zongker47cace92009-06-18 10:11:50 -0700945 if (buffer == NULL) {
Tianjie Xu84478e82016-05-23 11:42:40 -0700946 ErrorAbort(state, kFileGetPropFailure, "%s: failed to alloc %zu bytes", name,
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700947 static_cast<size_t>(st.st_size+1));
Doug Zongker47cace92009-06-18 10:11:50 -0700948 goto done;
949 }
950
Tao Baoba9a42a2015-06-23 23:23:33 -0700951 FILE* f;
Jed Estepa7b9a462015-12-15 16:04:53 -0800952 f = ota_fopen(filename, "rb");
Doug Zongker47cace92009-06-18 10:11:50 -0700953 if (f == NULL) {
Tianjie Xu16255832016-04-30 11:49:59 -0700954 ErrorAbort(state, kFileOpenFailure, "%s: failed to open %s: %s", name, filename,
955 strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -0700956 goto done;
957 }
958
Jed Estepa7b9a462015-12-15 16:04:53 -0800959 if (ota_fread(buffer, 1, st.st_size, f) != static_cast<size_t>(st.st_size)) {
Tianjie Xu84478e82016-05-23 11:42:40 -0700960 ErrorAbort(state, kFreadFailure, "%s: failed to read %zu bytes from %s",
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700961 name, static_cast<size_t>(st.st_size), filename);
Jed Estepa7b9a462015-12-15 16:04:53 -0800962 ota_fclose(f);
Doug Zongker47cace92009-06-18 10:11:50 -0700963 goto done;
964 }
965 buffer[st.st_size] = '\0';
966
Jed Estepa7b9a462015-12-15 16:04:53 -0800967 ota_fclose(f);
Doug Zongker47cace92009-06-18 10:11:50 -0700968
Tao Baoba9a42a2015-06-23 23:23:33 -0700969 char* line;
970 line = strtok(buffer, "\n");
Doug Zongker47cace92009-06-18 10:11:50 -0700971 do {
972 // skip whitespace at start of line
973 while (*line && isspace(*line)) ++line;
974
975 // comment or blank line: skip to next line
976 if (*line == '\0' || *line == '#') continue;
977
978 char* equal = strchr(line, '=');
979 if (equal == NULL) {
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700980 continue;
Doug Zongker47cace92009-06-18 10:11:50 -0700981 }
982
983 // trim whitespace between key and '='
984 char* key_end = equal-1;
985 while (key_end > line && isspace(*key_end)) --key_end;
986 key_end[1] = '\0';
987
988 // not the key we're looking for
989 if (strcmp(key, line) != 0) continue;
990
991 // skip whitespace after the '=' to the start of the value
992 char* val_start = equal+1;
993 while(*val_start && isspace(*val_start)) ++val_start;
994
995 // trim trailing whitespace
996 char* val_end = val_start + strlen(val_start)-1;
997 while (val_end > val_start && isspace(*val_end)) --val_end;
998 val_end[1] = '\0';
999
1000 result = strdup(val_start);
1001 break;
1002
1003 } while ((line = strtok(NULL, "\n")));
1004
1005 if (result == NULL) result = strdup("");
1006
1007 done:
1008 free(filename);
1009 free(key);
1010 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -08001011 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -07001012}
1013
Doug Zongker8edb00c2009-06-11 17:21:44 -07001014// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001015Value* ApplyPatchSpaceFn(const char* name, State* state,
1016 int argc, Expr* argv[]) {
1017 char* bytes_str;
1018 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
1019 return NULL;
1020 }
1021
Tao Baob15fd222015-09-24 11:10:51 -07001022 size_t bytes;
1023 if (!android::base::ParseUint(bytes_str, &bytes)) {
Tianjie Xu16255832016-04-30 11:49:59 -07001024 ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count\n\n",
1025 name, bytes_str);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001026 free(bytes_str);
Tao Baob15fd222015-09-24 11:10:51 -07001027 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001028 }
1029
1030 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
1031}
1032
Doug Zongker52b40362014-02-10 15:30:30 -08001033// apply_patch(file, size, init_sha1, tgt_sha1, patch)
1034
Doug Zongker512536a2010-02-17 16:11:44 -08001035Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001036 if (argc < 6 || (argc % 2) == 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001037 return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 6 args and an "
1038 "even number, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001039 }
1040
Doug Zongkerc4351c72010-02-22 14:46:32 -08001041 char* source_filename;
1042 char* target_filename;
1043 char* target_sha1;
1044 char* target_size_str;
1045 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
1046 &target_sha1, &target_size_str) < 0) {
1047 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001048 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001049
Tao Baob15fd222015-09-24 11:10:51 -07001050 size_t target_size;
1051 if (!android::base::ParseUint(target_size_str, &target_size)) {
Tianjie Xu16255832016-04-30 11:49:59 -07001052 ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count",
1053 name, target_size_str);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001054 free(source_filename);
1055 free(target_filename);
1056 free(target_sha1);
1057 free(target_size_str);
Tao Baob15fd222015-09-24 11:10:51 -07001058 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001059 }
1060
1061 int patchcount = (argc-4) / 2;
Yabin Cui64be2132016-02-03 18:16:02 -08001062 std::unique_ptr<Value*, decltype(&free)> arg_values(ReadValueVarArgs(state, argc-4, argv+4),
1063 free);
1064 if (!arg_values) {
1065 return nullptr;
1066 }
1067 std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> patch_shas;
1068 std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> patches;
1069 // Protect values by unique_ptrs first to get rid of memory leak.
1070 for (int i = 0; i < patchcount * 2; i += 2) {
1071 patch_shas.emplace_back(arg_values.get()[i], FreeValue);
1072 patches.emplace_back(arg_values.get()[i+1], FreeValue);
1073 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001074
Yabin Cui64be2132016-02-03 18:16:02 -08001075 for (int i = 0; i < patchcount; ++i) {
1076 if (patch_shas[i]->type != VAL_STRING) {
Tianjie Xu16255832016-04-30 11:49:59 -07001077 ErrorAbort(state, kArgsParsingFailure, "%s(): sha-1 #%d is not string", name, i);
Yabin Cui64be2132016-02-03 18:16:02 -08001078 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001079 }
Yabin Cui64be2132016-02-03 18:16:02 -08001080 if (patches[i]->type != VAL_BLOB) {
Tianjie Xu16255832016-04-30 11:49:59 -07001081 ErrorAbort(state, kArgsParsingFailure, "%s(): patch #%d is not blob", name, i);
Yabin Cui64be2132016-02-03 18:16:02 -08001082 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001083 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001084 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001085
Yabin Cui64be2132016-02-03 18:16:02 -08001086 std::vector<char*> patch_sha_str;
1087 std::vector<Value*> patch_ptrs;
1088 for (int i = 0; i < patchcount; ++i) {
1089 patch_sha_str.push_back(patch_shas[i]->data);
1090 patch_ptrs.push_back(patches[i].get());
Doug Zongker8edb00c2009-06-11 17:21:44 -07001091 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001092
1093 int result = applypatch(source_filename, target_filename,
1094 target_sha1, target_size,
Yabin Cui64be2132016-02-03 18:16:02 -08001095 patchcount, patch_sha_str.data(), patch_ptrs.data(), NULL);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001096
1097 return StringValue(strdup(result == 0 ? "t" : ""));
1098}
1099
1100// apply_patch_check(file, [sha1_1, ...])
1101Value* ApplyPatchCheckFn(const char* name, State* state,
1102 int argc, Expr* argv[]) {
1103 if (argc < 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001104 return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 1 arg, got %d",
Doug Zongkerc4351c72010-02-22 14:46:32 -08001105 name, argc);
1106 }
1107
1108 char* filename;
1109 if (ReadArgs(state, argv, 1, &filename) < 0) {
1110 return NULL;
1111 }
1112
1113 int patchcount = argc-1;
1114 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
1115
1116 int result = applypatch_check(filename, patchcount, sha1s);
1117
1118 int i;
1119 for (i = 0; i < patchcount; ++i) {
1120 free(sha1s[i]);
1121 }
1122 free(sha1s);
1123
1124 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001125}
1126
Tao Bao1107d962015-09-09 17:16:55 -07001127// This is the updater side handler for ui_print() in edify script. Contents
1128// will be sent over to the recovery side for on-screen display.
Doug Zongker512536a2010-02-17 16:11:44 -08001129Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001130 char** args = ReadVarArgs(state, argc, argv);
1131 if (args == NULL) {
1132 return NULL;
1133 }
1134
Tao Bao1107d962015-09-09 17:16:55 -07001135 std::string buffer;
1136 for (int i = 0; i < argc; ++i) {
1137 buffer += args[i];
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001138 free(args[i]);
1139 }
1140 free(args);
Tao Bao1107d962015-09-09 17:16:55 -07001141
1142 buffer += "\n";
Michael Runged4a63422014-10-22 19:48:41 -07001143 uiPrint(state, buffer);
Tao Bao1107d962015-09-09 17:16:55 -07001144 return StringValue(strdup(buffer.c_str()));
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001145}
1146
Doug Zongkerd0181b82011-10-19 10:51:12 -07001147Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
1148 if (argc != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001149 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %d", name, argc);
Doug Zongkerd0181b82011-10-19 10:51:12 -07001150 }
1151 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
1152 return StringValue(strdup("t"));
1153}
1154
Doug Zongker512536a2010-02-17 16:11:44 -08001155Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001156 if (argc < 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001157 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001158 }
1159 char** args = ReadVarArgs(state, argc, argv);
1160 if (args == NULL) {
1161 return NULL;
1162 }
1163
Tao Baoba9a42a2015-06-23 23:23:33 -07001164 char** args2 = reinterpret_cast<char**>(malloc(sizeof(char*) * (argc+1)));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001165 memcpy(args2, args, sizeof(char*) * argc);
1166 args2[argc] = NULL;
1167
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001168 printf("about to run program [%s] with %d args\n", args2[0], argc);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001169
1170 pid_t child = fork();
1171 if (child == 0) {
1172 execv(args2[0], args2);
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001173 printf("run_program: execv failed: %s\n", strerror(errno));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001174 _exit(1);
1175 }
1176 int status;
1177 waitpid(child, &status, 0);
1178 if (WIFEXITED(status)) {
1179 if (WEXITSTATUS(status) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001180 printf("run_program: child exited with status %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001181 WEXITSTATUS(status));
1182 }
1183 } else if (WIFSIGNALED(status)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001184 printf("run_program: child terminated by signal %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001185 WTERMSIG(status));
1186 }
1187
1188 int i;
1189 for (i = 0; i < argc; ++i) {
1190 free(args[i]);
1191 }
1192 free(args);
1193 free(args2);
1194
1195 char buffer[20];
1196 sprintf(buffer, "%d", status);
1197
Doug Zongker512536a2010-02-17 16:11:44 -08001198 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001199}
1200
Doug Zongker512536a2010-02-17 16:11:44 -08001201// sha1_check(data)
1202// to return the sha1 of the data (given in the format returned by
1203// read_file).
1204//
1205// sha1_check(data, sha1_hex, [sha1_hex, ...])
1206// returns the sha1 of the file if it matches any of the hex
1207// strings passed, or "" if it does not equal any of them.
1208//
1209Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1210 if (argc < 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001211 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
Doug Zongker512536a2010-02-17 16:11:44 -08001212 }
1213
Yabin Cui64be2132016-02-03 18:16:02 -08001214 std::unique_ptr<Value*, decltype(&free)> arg_values(ReadValueVarArgs(state, argc, argv), free);
1215 if (arg_values == nullptr) {
1216 return nullptr;
1217 }
1218 std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> args;
1219 for (int i = 0; i < argc; ++i) {
1220 args.emplace_back(arg_values.get()[i], FreeValue);
Doug Zongker512536a2010-02-17 16:11:44 -08001221 }
1222
1223 if (args[0]->size < 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001224 return StringValue(strdup(""));
1225 }
Sen Jiangc48cb5e2016-02-04 16:23:21 +08001226 uint8_t digest[SHA_DIGEST_LENGTH];
1227 SHA1(reinterpret_cast<uint8_t*>(args[0]->data), args[0]->size, digest);
Doug Zongker512536a2010-02-17 16:11:44 -08001228
1229 if (argc == 1) {
1230 return StringValue(PrintSha1(digest));
1231 }
1232
1233 int i;
Yabin Cui64be2132016-02-03 18:16:02 -08001234 uint8_t arg_digest[SHA_DIGEST_LENGTH];
Doug Zongker512536a2010-02-17 16:11:44 -08001235 for (i = 1; i < argc; ++i) {
1236 if (args[i]->type != VAL_STRING) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001237 printf("%s(): arg %d is not a string; skipping",
Doug Zongker512536a2010-02-17 16:11:44 -08001238 name, i);
1239 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1240 // Warn about bad args and skip them.
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001241 printf("%s(): error parsing \"%s\" as sha-1; skipping",
1242 name, args[i]->data);
Sen Jiangc48cb5e2016-02-04 16:23:21 +08001243 } else if (memcmp(digest, arg_digest, SHA_DIGEST_LENGTH) == 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001244 break;
1245 }
Doug Zongker512536a2010-02-17 16:11:44 -08001246 }
1247 if (i >= argc) {
1248 // Didn't match any of the hex strings; return false.
1249 return StringValue(strdup(""));
1250 }
Yabin Cui64be2132016-02-03 18:16:02 -08001251 // Found a match.
1252 return args[i].release();
Doug Zongker512536a2010-02-17 16:11:44 -08001253}
1254
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001255// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001256// is actually a FileContents*).
1257Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1258 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001259 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker512536a2010-02-17 16:11:44 -08001260 }
1261 char* filename;
1262 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1263
Yabin Cuid6c93af2016-02-10 16:41:10 -08001264 Value* v = static_cast<Value*>(malloc(sizeof(Value)));
1265 if (v == nullptr) {
1266 return nullptr;
1267 }
Doug Zongker512536a2010-02-17 16:11:44 -08001268 v->type = VAL_BLOB;
Yabin Cuid6c93af2016-02-10 16:41:10 -08001269 v->size = -1;
1270 v->data = nullptr;
Doug Zongker512536a2010-02-17 16:11:44 -08001271
1272 FileContents fc;
Tao Baoefacd802016-06-11 03:56:38 -07001273 if (LoadFileContents(filename, &fc) == 0) {
Yabin Cuid6c93af2016-02-10 16:41:10 -08001274 v->data = static_cast<char*>(malloc(fc.data.size()));
1275 if (v->data != nullptr) {
1276 memcpy(v->data, fc.data.data(), fc.data.size());
1277 v->size = fc.data.size();
1278 }
Doug Zongker512536a2010-02-17 16:11:44 -08001279 }
Doug Zongker512536a2010-02-17 16:11:44 -08001280 free(filename);
1281 return v;
1282}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001283
Doug Zongkerc87bab12013-11-25 13:53:25 -08001284// Immediately reboot the device. Recovery is not finished normally,
1285// so if you reboot into recovery it will re-start applying the
1286// current package (because nothing has cleared the copy of the
1287// arguments stored in the BCB).
1288//
1289// The argument is the partition name passed to the android reboot
1290// property. It can be "recovery" to boot from the recovery
1291// partition, or "" (empty string) to boot from the regular boot
1292// partition.
1293Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) {
1294 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -07001295 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001296 }
1297
1298 char* filename;
1299 char* property;
1300 if (ReadArgs(state, argv, 2, &filename, &property) < 0) return NULL;
1301
Doug Zongkerc87bab12013-11-25 13:53:25 -08001302 // zero out the 'command' field of the bootloader message.
Elliott Hughescb220402016-09-23 15:30:55 -07001303 char buffer[80];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001304 memset(buffer, 0, sizeof(((struct bootloader_message*)0)->command));
Jed Estepa7b9a462015-12-15 16:04:53 -08001305 FILE* f = ota_fopen(filename, "r+b");
Doug Zongkerc87bab12013-11-25 13:53:25 -08001306 fseek(f, offsetof(struct bootloader_message, command), SEEK_SET);
Jed Estepa7b9a462015-12-15 16:04:53 -08001307 ota_fwrite(buffer, sizeof(((struct bootloader_message*)0)->command), 1, f);
1308 ota_fclose(f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001309 free(filename);
1310
Elliott Hughescb220402016-09-23 15:30:55 -07001311 std::string reboot_cmd = "reboot,";
1312 if (property != nullptr) reboot_cmd += property;
1313 android::base::SetProperty(ANDROID_RB_PROPERTY, reboot_cmd);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001314
1315 sleep(5);
1316 free(property);
Tianjie Xu16255832016-04-30 11:49:59 -07001317 ErrorAbort(state, kRebootFailure, "%s() failed to reboot", name);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001318 return NULL;
1319}
1320
1321// Store a string value somewhere that future invocations of recovery
1322// can access it. This value is called the "stage" and can be used to
1323// drive packages that need to do reboots in the middle of
1324// installation and keep track of where they are in the multi-stage
1325// install.
1326//
1327// The first argument is the block device for the misc partition
1328// ("/misc" in the fstab), which is where this value is stored. The
1329// second argument is the string to store; it should not exceed 31
1330// bytes.
1331Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
1332 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -07001333 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001334 }
1335
1336 char* filename;
1337 char* stagestr;
1338 if (ReadArgs(state, argv, 2, &filename, &stagestr) < 0) return NULL;
1339
1340 // Store this value in the misc partition, immediately after the
1341 // bootloader message that the main recovery uses to save its
1342 // arguments in case of the device restarting midway through
1343 // package installation.
Jed Estepa7b9a462015-12-15 16:04:53 -08001344 FILE* f = ota_fopen(filename, "r+b");
Doug Zongkerc87bab12013-11-25 13:53:25 -08001345 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1346 int to_write = strlen(stagestr)+1;
1347 int max_size = sizeof(((struct bootloader_message*)0)->stage);
1348 if (to_write > max_size) {
1349 to_write = max_size;
1350 stagestr[max_size-1] = 0;
1351 }
Jed Estepa7b9a462015-12-15 16:04:53 -08001352 ota_fwrite(stagestr, to_write, 1, f);
1353 ota_fclose(f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001354
1355 free(stagestr);
1356 return StringValue(filename);
1357}
1358
1359// Return the value most recently saved with SetStageFn. The argument
1360// is the block device for the misc partition.
1361Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001362 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001363 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001364 }
1365
1366 char* filename;
1367 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1368
1369 char buffer[sizeof(((struct bootloader_message*)0)->stage)];
Jed Estepa7b9a462015-12-15 16:04:53 -08001370 FILE* f = ota_fopen(filename, "rb");
Doug Zongkerc87bab12013-11-25 13:53:25 -08001371 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
Jed Estepa7b9a462015-12-15 16:04:53 -08001372 ota_fread(buffer, sizeof(buffer), 1, f);
1373 ota_fclose(f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001374 buffer[sizeof(buffer)-1] = '\0';
1375
1376 return StringValue(strdup(buffer));
1377}
1378
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001379Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) {
1380 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -07001381 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001382 }
1383
1384 char* filename;
1385 char* len_str;
1386 if (ReadArgs(state, argv, 2, &filename, &len_str) < 0) return NULL;
1387
Tao Baob15fd222015-09-24 11:10:51 -07001388 size_t len;
1389 android::base::ParseUint(len_str, &len);
Jed Estepa7b9a462015-12-15 16:04:53 -08001390 int fd = ota_open(filename, O_WRONLY, 0644);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001391 int success = wipe_block_device(fd, len);
1392
1393 free(filename);
1394 free(len_str);
1395
Jed Estepa7b9a462015-12-15 16:04:53 -08001396 ota_close(fd);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001397
1398 return StringValue(strdup(success ? "t" : ""));
1399}
1400
Doug Zongkerc704e062014-05-23 08:40:35 -07001401Value* EnableRebootFn(const char* name, State* state, int argc, Expr* argv[]) {
1402 if (argc != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001403 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %d", name, argc);
Doug Zongkerc704e062014-05-23 08:40:35 -07001404 }
1405 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
1406 fprintf(ui->cmd_pipe, "enable_reboot\n");
1407 return StringValue(strdup("t"));
1408}
1409
Michael Rungeacf47db2014-11-21 00:12:28 -08001410Value* Tune2FsFn(const char* name, State* state, int argc, Expr* argv[]) {
1411 if (argc == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001412 return ErrorAbort(state, kArgsParsingFailure, "%s() expects args, got %d", name, argc);
Michael Rungeacf47db2014-11-21 00:12:28 -08001413 }
1414
1415 char** args = ReadVarArgs(state, argc, argv);
1416 if (args == NULL) {
Tianjie Xu16255832016-04-30 11:49:59 -07001417 return ErrorAbort(state, kArgsParsingFailure, "%s() could not read args", name);
Michael Rungeacf47db2014-11-21 00:12:28 -08001418 }
1419
Tao Baoba9a42a2015-06-23 23:23:33 -07001420 char** args2 = reinterpret_cast<char**>(malloc(sizeof(char*) * (argc+1)));
Michael Rungeacf47db2014-11-21 00:12:28 -08001421 // Tune2fs expects the program name as its args[0]
1422 args2[0] = strdup(name);
Tao Baoba9a42a2015-06-23 23:23:33 -07001423 for (int i = 0; i < argc; ++i) {
Michael Rungeacf47db2014-11-21 00:12:28 -08001424 args2[i + 1] = args[i];
1425 }
1426 int result = tune2fs_main(argc + 1, args2);
Tao Baoba9a42a2015-06-23 23:23:33 -07001427 for (int i = 0; i < argc; ++i) {
Michael Rungeacf47db2014-11-21 00:12:28 -08001428 free(args[i]);
1429 }
1430 free(args);
1431
1432 free(args2[0]);
1433 free(args2);
1434 if (result != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001435 return ErrorAbort(state, kTune2FsFailure, "%s() returned error code %d",
1436 name, result);
Michael Rungeacf47db2014-11-21 00:12:28 -08001437 }
1438 return StringValue(strdup("t"));
1439}
1440
Doug Zongker9931f7f2009-06-10 14:11:53 -07001441void RegisterInstallFunctions() {
1442 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001443 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001444 RegisterFunction("unmount", UnmountFn);
1445 RegisterFunction("format", FormatFn);
1446 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001447 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001448 RegisterFunction("delete", DeleteFn);
1449 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001450 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1451 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001452 RegisterFunction("symlink", SymlinkFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001453
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001454 // Usage:
1455 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1456 // Example:
1457 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1458 RegisterFunction("set_metadata", SetMetadataFn);
1459
1460 // Usage:
1461 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1462 // Example:
1463 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1464 RegisterFunction("set_metadata_recursive", SetMetadataFn);
1465
Doug Zongker8edb00c2009-06-11 17:21:44 -07001466 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001467 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001468
1469 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001470 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1471 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001472
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001473 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001474
Doug Zongker512536a2010-02-17 16:11:44 -08001475 RegisterFunction("read_file", ReadFileFn);
1476 RegisterFunction("sha1_check", Sha1CheckFn);
Michael Rungece7ca712013-11-06 17:42:20 -08001477 RegisterFunction("rename", RenameFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001478
Doug Zongkerd0181b82011-10-19 10:51:12 -07001479 RegisterFunction("wipe_cache", WipeCacheFn);
1480
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001481 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001482
1483 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001484
1485 RegisterFunction("reboot_now", RebootNowFn);
1486 RegisterFunction("get_stage", GetStageFn);
1487 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -07001488
1489 RegisterFunction("enable_reboot", EnableRebootFn);
Michael Rungeacf47db2014-11-21 00:12:28 -08001490 RegisterFunction("tune2fs", Tune2FsFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001491}