blob: cf3e938a465cfd7016b1a2c263eabf42527a7cca [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>
40#include <android-base/strings.h>
41#include <android-base/stringprintf.h>
Elliott Hughes4bbd5bf2016-04-01 18:24:39 -070042#include <selinux/label.h>
43#include <selinux/selinux.h>
Tao Bao1107d962015-09-09 17:16:55 -070044
Doug Zongkerc87bab12013-11-25 13:53:25 -080045#include "bootloader.h"
46#include "applypatch/applypatch.h"
47#include "cutils/android_reboot.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070048#include "cutils/misc.h"
49#include "cutils/properties.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070050#include "edify/expr.h"
Tianjie Xu16255832016-04-30 11:49:59 -070051#include "error_code.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070052#include "minzip/DirUtil.h"
Elliott Hughes63a31922016-06-09 17:41:22 -070053#include "mounts.h"
Tianjie Xu16255832016-04-30 11:49:59 -070054#include "openssl/sha.h"
Jed Estep39c1b5e2015-12-15 16:04:53 -080055#include "ota_io.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070056#include "updater.h"
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -080057#include "install.h"
Michael Rungeacf47db2014-11-21 00:12:28 -080058#include "tune2fs.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070059
Doug Zongker3d177d02010-07-01 09:18:44 -070060#include "make_ext4fs.h"
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -080061#include "wipe.h"
Doug Zongker3d177d02010-07-01 09:18: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
83__attribute__((__format__(printf, 2, 3))) __nonnull((2))
84void uiPrintf(State* state, const char* format, ...) {
Tao Bao1107d962015-09-09 17:16:55 -070085 std::string error_msg;
86
Michael Runged4a63422014-10-22 19:48:41 -070087 va_list ap;
88 va_start(ap, format);
Tao Bao1107d962015-09-09 17:16:55 -070089 android::base::StringAppendV(&error_msg, format, ap);
Michael Runged4a63422014-10-22 19:48:41 -070090 va_end(ap);
Tao Bao1107d962015-09-09 17:16:55 -070091
Michael Runged4a63422014-10-22 19:48:41 -070092 uiPrint(state, error_msg);
93}
94
Doug Zongker52b40362014-02-10 15:30:30 -080095// Take a sha-1 digest and return it as a newly-allocated hex string.
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070096char* PrintSha1(const uint8_t* digest) {
Sen Jiangc48cb5e2016-02-04 16:23:21 +080097 char* buffer = reinterpret_cast<char*>(malloc(SHA_DIGEST_LENGTH*2 + 1));
Doug Zongker52b40362014-02-10 15:30:30 -080098 const char* alphabet = "0123456789abcdef";
Tao Baoba9a42a2015-06-23 23:23:33 -070099 size_t i;
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800100 for (i = 0; i < SHA_DIGEST_LENGTH; ++i) {
Doug Zongker52b40362014-02-10 15:30:30 -0800101 buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf];
102 buffer[i*2+1] = alphabet[digest[i] & 0xf];
103 }
104 buffer[i*2] = '\0';
105 return buffer;
106}
107
Doug Zongker3d177d02010-07-01 09:18:44 -0700108// mount(fs_type, partition_type, location, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700109//
Doug Zongker3d177d02010-07-01 09:18:44 -0700110// fs_type="ext4" partition_type="EMMC" location=device
Doug Zongker512536a2010-02-17 16:11:44 -0800111Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700112 char* result = NULL;
Michael Runge168f7772014-10-22 17:05:08 -0700113 if (argc != 4 && argc != 5) {
Tianjie Xu16255832016-04-30 11:49:59 -0700114 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 4-5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700115 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700116 char* fs_type;
117 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700118 char* location;
119 char* mount_point;
Michael Runge168f7772014-10-22 17:05:08 -0700120 char* mount_options;
121 bool has_mount_options;
122 if (argc == 5) {
123 has_mount_options = true;
124 if (ReadArgs(state, argv, 5, &fs_type, &partition_type,
125 &location, &mount_point, &mount_options) < 0) {
126 return NULL;
127 }
128 } else {
129 has_mount_options = false;
130 if (ReadArgs(state, argv, 4, &fs_type, &partition_type,
Doug Zongker3d177d02010-07-01 09:18:44 -0700131 &location, &mount_point) < 0) {
Michael Runge168f7772014-10-22 17:05:08 -0700132 return NULL;
133 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700134 }
135
Doug Zongker3d177d02010-07-01 09:18:44 -0700136 if (strlen(fs_type) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700137 ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
Doug Zongker3d177d02010-07-01 09:18:44 -0700138 goto done;
139 }
140 if (strlen(partition_type) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700141 ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
Doug Zongker3d177d02010-07-01 09:18:44 -0700142 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700143 goto done;
144 }
145 if (strlen(location) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700146 ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700147 goto done;
148 }
149 if (strlen(mount_point) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700150 ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
151 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700152 goto done;
153 }
154
Tao Baoba9a42a2015-06-23 23:23:33 -0700155 {
156 char *secontext = NULL;
Stephen Smalley779701d2012-02-09 14:13:23 -0500157
Tao Baoba9a42a2015-06-23 23:23:33 -0700158 if (sehandle) {
159 selabel_lookup(sehandle, &secontext, mount_point, 0755);
160 setfscreatecon(secontext);
161 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500162
Tao Baoba9a42a2015-06-23 23:23:33 -0700163 mkdir(mount_point, 0755);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700164
Tao Baoba9a42a2015-06-23 23:23:33 -0700165 if (secontext) {
166 freecon(secontext);
167 setfscreatecon(NULL);
168 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500169 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500170
Elliott Hughes63a31922016-06-09 17:41:22 -0700171 if (mount(location, mount_point, fs_type,
172 MS_NOATIME | MS_NODEV | MS_NODIRATIME,
173 has_mount_options ? mount_options : "") < 0) {
174 uiPrintf(state, "%s: failed to mount %s at %s: %s\n",
175 name, location, mount_point, strerror(errno));
176 result = strdup("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700177 } else {
Elliott Hughes63a31922016-06-09 17:41:22 -0700178 result = mount_point;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700179 }
180
181done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700182 free(fs_type);
183 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700184 free(location);
185 if (result != mount_point) free(mount_point);
Michael Runge168f7772014-10-22 17:05:08 -0700186 if (has_mount_options) free(mount_options);
Doug Zongker512536a2010-02-17 16:11:44 -0800187 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700188}
189
Doug Zongker8edb00c2009-06-11 17:21:44 -0700190
191// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800192Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700193 char* result = NULL;
194 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700195 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700196 }
197 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700198 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700199 return NULL;
200 }
201 if (strlen(mount_point) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700202 ErrorAbort(state, kArgsParsingFailure, "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700203 goto done;
204 }
205
206 scan_mounted_volumes();
Tao Baoba9a42a2015-06-23 23:23:33 -0700207 {
Elliott Hughes63a31922016-06-09 17:41:22 -0700208 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
Tao Baoba9a42a2015-06-23 23:23:33 -0700209 if (vol == NULL) {
210 result = strdup("");
211 } else {
212 result = mount_point;
213 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700214 }
215
216done:
217 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800218 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700219}
220
221
Doug Zongker512536a2010-02-17 16:11:44 -0800222Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700223 char* result = NULL;
224 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700225 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700226 }
227 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700228 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700229 return NULL;
230 }
231 if (strlen(mount_point) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700232 ErrorAbort(state, kArgsParsingFailure, "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700233 goto done;
234 }
235
236 scan_mounted_volumes();
Tao Baoba9a42a2015-06-23 23:23:33 -0700237 {
Elliott Hughes63a31922016-06-09 17:41:22 -0700238 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
Tao Baoba9a42a2015-06-23 23:23:33 -0700239 if (vol == NULL) {
240 uiPrintf(state, "unmount of %s failed; no such volume\n", mount_point);
241 result = strdup("");
242 } else {
243 int ret = unmount_mounted_volume(vol);
244 if (ret != 0) {
245 uiPrintf(state, "unmount of %s failed (%d): %s\n",
246 mount_point, ret, strerror(errno));
247 }
248 result = mount_point;
Michael Runge5ddf4292014-10-24 14:14:41 -0700249 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700250 }
251
252done:
253 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800254 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700255}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700256
JP Abgrall37aedb32014-06-16 19:07:39 -0700257static int exec_cmd(const char* path, char* const argv[]) {
258 int status;
259 pid_t child;
260 if ((child = vfork()) == 0) {
261 execv(path, argv);
262 _exit(-1);
263 }
264 waitpid(child, &status, 0);
265 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
266 printf("%s failed with status %d\n", path, WEXITSTATUS(status));
267 }
268 return WEXITSTATUS(status);
269}
270
Doug Zongker8edb00c2009-06-11 17:21:44 -0700271
Stephen Smalley779701d2012-02-09 14:13:23 -0500272// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700273//
Stephen Smalley779701d2012-02-09 14:13:23 -0500274// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700275// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
276// if fs_size == 0, then make fs uses the entire partition.
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800277// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700278// 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 -0800279Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700280 char* result = NULL;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400281 if (argc != 5) {
Tianjie Xu16255832016-04-30 11:49:59 -0700282 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700283 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700284 char* fs_type;
285 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700286 char* location;
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800287 char* fs_size;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400288 char* mount_point;
Stephen Smalley779701d2012-02-09 14:13:23 -0500289
Stephen Smalley779701d2012-02-09 14:13:23 -0500290 if (ReadArgs(state, argv, 5, &fs_type, &partition_type, &location, &fs_size, &mount_point) < 0) {
291 return NULL;
292 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700293
Doug Zongker3d177d02010-07-01 09:18:44 -0700294 if (strlen(fs_type) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700295 ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
Doug Zongker3d177d02010-07-01 09:18:44 -0700296 goto done;
297 }
298 if (strlen(partition_type) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700299 ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
Doug Zongker3d177d02010-07-01 09:18:44 -0700300 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700301 goto done;
302 }
303 if (strlen(location) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700304 ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700305 goto done;
306 }
307
Stephen Smalley516e4e22012-04-03 13:35:11 -0400308 if (strlen(mount_point) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700309 ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
310 name);
Stephen Smalley779701d2012-02-09 14:13:23 -0500311 goto done;
312 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500313
Elliott Hughes63a31922016-06-09 17:41:22 -0700314 if (strcmp(fs_type, "ext4") == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500315 int status = make_ext4fs(location, atoll(fs_size), mount_point, sehandle);
Doug Zongker3d177d02010-07-01 09:18:44 -0700316 if (status != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700317 printf("%s: make_ext4fs failed (%d) on %s",
Doug Zongker3d177d02010-07-01 09:18:44 -0700318 name, status, location);
319 result = strdup("");
320 goto done;
321 }
322 result = location;
JP Abgrall37aedb32014-06-16 19:07:39 -0700323 } else if (strcmp(fs_type, "f2fs") == 0) {
324 char *num_sectors;
325 if (asprintf(&num_sectors, "%lld", atoll(fs_size) / 512) <= 0) {
326 printf("format_volume: failed to create %s command for %s\n", fs_type, location);
327 result = strdup("");
328 goto done;
329 }
330 const char *f2fs_path = "/sbin/mkfs.f2fs";
331 const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", location, num_sectors, NULL};
332 int status = exec_cmd(f2fs_path, (char* const*)f2fs_argv);
333 free(num_sectors);
334 if (status != 0) {
335 printf("%s: mkfs.f2fs failed (%d) on %s",
336 name, status, location);
337 result = strdup("");
338 goto done;
339 }
340 result = location;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700341 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700342 printf("%s: unsupported fs_type \"%s\" partition_type \"%s\"",
Doug Zongker3d177d02010-07-01 09:18:44 -0700343 name, fs_type, partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700344 }
345
346done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700347 free(fs_type);
348 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700349 if (result != location) free(location);
Doug Zongker512536a2010-02-17 16:11:44 -0800350 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700351}
352
Michael Rungece7ca712013-11-06 17:42:20 -0800353Value* RenameFn(const char* name, State* state, int argc, Expr* argv[]) {
354 char* result = NULL;
355 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700356 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Michael Rungece7ca712013-11-06 17:42:20 -0800357 }
358
359 char* src_name;
360 char* dst_name;
361
362 if (ReadArgs(state, argv, 2, &src_name, &dst_name) < 0) {
363 return NULL;
364 }
365 if (strlen(src_name) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700366 ErrorAbort(state, kArgsParsingFailure, "src_name argument to %s() can't be empty", name);
Michael Rungece7ca712013-11-06 17:42:20 -0800367 goto done;
368 }
369 if (strlen(dst_name) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700370 ErrorAbort(state, kArgsParsingFailure, "dst_name argument to %s() can't be empty", name);
Michael Rungece7ca712013-11-06 17:42:20 -0800371 goto done;
372 }
Michael Rungea91ecc52014-07-21 17:40:02 -0700373 if (make_parents(dst_name) != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700374 ErrorAbort(state, kFileRenameFailure, "Creating parent of %s failed, error %s",
Michael Rungea91ecc52014-07-21 17:40:02 -0700375 dst_name, strerror(errno));
Michael Runge2f0ef732014-10-22 14:28:23 -0700376 } else if (access(dst_name, F_OK) == 0 && access(src_name, F_OK) != 0) {
377 // File was already moved
378 result = dst_name;
Michael Rungea91ecc52014-07-21 17:40:02 -0700379 } else if (rename(src_name, dst_name) != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700380 ErrorAbort(state, kFileRenameFailure, "Rename of %s to %s failed, error %s",
Michael Rungece7ca712013-11-06 17:42:20 -0800381 src_name, dst_name, strerror(errno));
382 } else {
383 result = dst_name;
384 }
385
386done:
387 free(src_name);
388 if (result != dst_name) free(dst_name);
389 return StringValue(result);
390}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700391
Doug Zongker512536a2010-02-17 16:11:44 -0800392Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700393 char** paths = reinterpret_cast<char**>(malloc(argc * sizeof(char*)));
394 for (int i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700395 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700396 if (paths[i] == NULL) {
Yabin Cui64be2132016-02-03 18:16:02 -0800397 for (int j = 0; j < i; ++j) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700398 free(paths[j]);
399 }
400 free(paths);
401 return NULL;
402 }
403 }
404
405 bool recursive = (strcmp(name, "delete_recursive") == 0);
406
407 int success = 0;
Tao Baoba9a42a2015-06-23 23:23:33 -0700408 for (int i = 0; i < argc; ++i) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700409 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
410 ++success;
411 free(paths[i]);
412 }
413 free(paths);
414
415 char buffer[10];
416 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800417 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700418}
419
Doug Zongker8edb00c2009-06-11 17:21:44 -0700420
Doug Zongker512536a2010-02-17 16:11:44 -0800421Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700422 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700423 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700424 }
425 char* frac_str;
426 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700427 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700428 return NULL;
429 }
430
431 double frac = strtod(frac_str, NULL);
Tao Baob15fd222015-09-24 11:10:51 -0700432 int sec;
433 android::base::ParseInt(sec_str, &sec);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700434
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700435 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700436 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
437
Doug Zongker9931f7f2009-06-10 14:11:53 -0700438 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800439 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700440}
441
Doug Zongker512536a2010-02-17 16:11:44 -0800442Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700443 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700444 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700445 }
446 char* frac_str;
447 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
448 return NULL;
449 }
450
451 double frac = strtod(frac_str, NULL);
452
453 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
454 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
455
Doug Zongker512536a2010-02-17 16:11:44 -0800456 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700457}
458
Doug Zongker8edb00c2009-06-11 17:21:44 -0700459// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800460Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700461 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700462 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700463 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700464 }
465 char* zip_path;
466 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700467 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700468
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700469 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700470
471 // To create a consistent system image, never use the clock for timestamps.
472 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
473
474 bool success = mzExtractRecursive(za, zip_path, dest_path,
Narayan Kamath9c0f5d62015-02-23 14:09:31 +0000475 &timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500476 NULL, NULL, sehandle);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700477 free(zip_path);
478 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800479 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700480}
481
Doug Zongker8edb00c2009-06-11 17:21:44 -0700482
483// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800484// or
485// package_extract_file(package_path)
486// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800487// function (the char* returned is actually a FileContents*).
488Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700489 int argc, Expr* argv[]) {
Doug Zongker5f875bf2014-08-22 14:53:43 -0700490 if (argc < 1 || argc > 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700491 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 or 2 args, got %d",
Doug Zongker6aece332010-02-01 14:40:12 -0800492 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700493 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700494 bool success = false;
Doug Zongker43772d22014-06-09 14:13:19 -0700495
Doug Zongker5f875bf2014-08-22 14:53:43 -0700496 if (argc == 2) {
497 // The two-argument version extracts to a file.
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800498
499 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700500
Doug Zongker6aece332010-02-01 14:40:12 -0800501 char* zip_path;
502 char* dest_path;
Doug Zongker5f875bf2014-08-22 14:53:43 -0700503 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800504
Doug Zongker6aece332010-02-01 14:40:12 -0800505 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
506 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700507 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800508 goto done2;
509 }
510
Tao Baoba9a42a2015-06-23 23:23:33 -0700511 {
Alistair Strachan733285f2016-05-05 16:04:32 -0700512 int fd = TEMP_FAILURE_RETRY(ota_open(dest_path, O_WRONLY | O_CREAT | O_TRUNC,
Tao Baod3cac342015-12-14 15:53:25 -0800513 S_IRUSR | S_IWUSR));
514 if (fd == -1) {
515 printf("%s: can't open %s for write: %s\n", name, dest_path, strerror(errno));
Tao Baoba9a42a2015-06-23 23:23:33 -0700516 goto done2;
517 }
Tao Baod3cac342015-12-14 15:53:25 -0800518 success = mzExtractZipEntryToFile(za, entry, fd);
Jed Estepa7b9a462015-12-15 16:04:53 -0800519 if (ota_fsync(fd) == -1) {
Tao Baod3cac342015-12-14 15:53:25 -0800520 printf("fsync of \"%s\" failed: %s\n", dest_path, strerror(errno));
521 success = false;
522 }
Jed Estepa7b9a462015-12-15 16:04:53 -0800523 if (ota_close(fd) == -1) {
Tao Baod3cac342015-12-14 15:53:25 -0800524 printf("close of \"%s\" failed: %s\n", dest_path, strerror(errno));
525 success = false;
526 }
Doug Zongker6aece332010-02-01 14:40:12 -0800527 }
Doug Zongker6aece332010-02-01 14:40:12 -0800528
529 done2:
530 free(zip_path);
531 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800532 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800533 } else {
534 // The one-argument version returns the contents of the file
535 // as the result.
536
537 char* zip_path;
Yabin Cui64be2132016-02-03 18:16:02 -0800538 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
539
Tao Baoba9a42a2015-06-23 23:23:33 -0700540 Value* v = reinterpret_cast<Value*>(malloc(sizeof(Value)));
Doug Zongker512536a2010-02-17 16:11:44 -0800541 v->type = VAL_BLOB;
542 v->size = -1;
543 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800544
Doug Zongker6aece332010-02-01 14:40:12 -0800545 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
546 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
547 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700548 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800549 goto done1;
550 }
551
Doug Zongker512536a2010-02-17 16:11:44 -0800552 v->size = mzGetZipEntryUncompLen(entry);
Tao Baoba9a42a2015-06-23 23:23:33 -0700553 v->data = reinterpret_cast<char*>(malloc(v->size));
Doug Zongker512536a2010-02-17 16:11:44 -0800554 if (v->data == NULL) {
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700555 printf("%s: failed to allocate %zd bytes for %s\n",
556 name, v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800557 goto done1;
558 }
559
Doug Zongker512536a2010-02-17 16:11:44 -0800560 success = mzExtractZipEntryToBuffer(za, entry,
561 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800562
563 done1:
564 free(zip_path);
565 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800566 free(v->data);
567 v->data = NULL;
568 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800569 }
Doug Zongker512536a2010-02-17 16:11:44 -0800570 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700571 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700572}
573
Doug Zongkera23075f2012-08-06 16:19:09 -0700574// Create all parent directories of name, if necessary.
575static int make_parents(char* name) {
576 char* p;
577 for (p = name + (strlen(name)-1); p > name; --p) {
578 if (*p != '/') continue;
579 *p = '\0';
580 if (make_parents(name) < 0) return -1;
581 int result = mkdir(name, 0700);
Michael Rungea91ecc52014-07-21 17:40:02 -0700582 if (result == 0) printf("created [%s]\n", name);
Doug Zongkera23075f2012-08-06 16:19:09 -0700583 *p = '/';
584 if (result == 0 || errno == EEXIST) {
585 // successfully created or already existed; we're done
586 return 0;
587 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700588 printf("failed to mkdir %s: %s\n", name, strerror(errno));
Doug Zongkera23075f2012-08-06 16:19:09 -0700589 return -1;
590 }
591 }
592 return 0;
593}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700594
Doug Zongker9931f7f2009-06-10 14:11:53 -0700595// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700596// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800597Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700598 if (argc == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700599 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700600 }
601 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700602 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700603 if (target == NULL) return NULL;
604
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700605 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700606 if (srcs == NULL) {
607 free(target);
608 return NULL;
609 }
610
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700611 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700612 int i;
613 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700614 if (unlink(srcs[i]) < 0) {
615 if (errno != ENOENT) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700616 printf("%s: failed to remove %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700617 name, srcs[i], strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700618 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700619 }
620 }
Doug Zongkera23075f2012-08-06 16:19:09 -0700621 if (make_parents(srcs[i])) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700622 printf("%s: failed to symlink %s to %s: making parents failed\n",
Doug Zongkera23075f2012-08-06 16:19:09 -0700623 name, srcs[i], target);
624 ++bad;
625 }
Doug Zongker60babf82009-09-18 15:11:24 -0700626 if (symlink(target, srcs[i]) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700627 printf("%s: failed to symlink %s to %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700628 name, srcs[i], target, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700629 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700630 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700631 free(srcs[i]);
632 }
633 free(srcs);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700634 if (bad) {
Tianjie Xu16255832016-04-30 11:49:59 -0700635 return ErrorAbort(state, kSymlinkFailure, "%s: some symlinks failed", name);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700636 }
Doug Zongker512536a2010-02-17 16:11:44 -0800637 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700638}
639
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700640struct perm_parsed_args {
641 bool has_uid;
642 uid_t uid;
643 bool has_gid;
644 gid_t gid;
645 bool has_mode;
646 mode_t mode;
647 bool has_fmode;
648 mode_t fmode;
649 bool has_dmode;
650 mode_t dmode;
651 bool has_selabel;
652 char* selabel;
653 bool has_capabilities;
654 uint64_t capabilities;
655};
656
Michael Runged4a63422014-10-22 19:48:41 -0700657static struct perm_parsed_args ParsePermArgs(State * state, int argc, char** args) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700658 int i;
659 struct perm_parsed_args parsed;
660 int bad = 0;
661 static int max_warnings = 20;
662
663 memset(&parsed, 0, sizeof(parsed));
664
665 for (i = 1; i < argc; i += 2) {
666 if (strcmp("uid", args[i]) == 0) {
667 int64_t uid;
668 if (sscanf(args[i+1], "%" SCNd64, &uid) == 1) {
669 parsed.uid = uid;
670 parsed.has_uid = true;
671 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700672 uiPrintf(state, "ParsePermArgs: invalid UID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700673 bad++;
674 }
675 continue;
676 }
677 if (strcmp("gid", args[i]) == 0) {
678 int64_t gid;
679 if (sscanf(args[i+1], "%" SCNd64, &gid) == 1) {
680 parsed.gid = gid;
681 parsed.has_gid = true;
682 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700683 uiPrintf(state, "ParsePermArgs: invalid GID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700684 bad++;
685 }
686 continue;
687 }
688 if (strcmp("mode", args[i]) == 0) {
689 int32_t mode;
690 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
691 parsed.mode = mode;
692 parsed.has_mode = true;
693 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700694 uiPrintf(state, "ParsePermArgs: invalid mode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700695 bad++;
696 }
697 continue;
698 }
699 if (strcmp("dmode", args[i]) == 0) {
700 int32_t mode;
701 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
702 parsed.dmode = mode;
703 parsed.has_dmode = true;
704 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700705 uiPrintf(state, "ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700706 bad++;
707 }
708 continue;
709 }
710 if (strcmp("fmode", args[i]) == 0) {
711 int32_t mode;
712 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
713 parsed.fmode = mode;
714 parsed.has_fmode = true;
715 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700716 uiPrintf(state, "ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700717 bad++;
718 }
719 continue;
720 }
721 if (strcmp("capabilities", args[i]) == 0) {
722 int64_t capabilities;
723 if (sscanf(args[i+1], "%" SCNi64, &capabilities) == 1) {
724 parsed.capabilities = capabilities;
725 parsed.has_capabilities = true;
726 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700727 uiPrintf(state, "ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700728 bad++;
729 }
730 continue;
731 }
732 if (strcmp("selabel", args[i]) == 0) {
733 if (args[i+1][0] != '\0') {
734 parsed.selabel = args[i+1];
735 parsed.has_selabel = true;
736 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700737 uiPrintf(state, "ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700738 bad++;
739 }
740 continue;
741 }
742 if (max_warnings != 0) {
743 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i]);
744 max_warnings--;
745 if (max_warnings == 0) {
746 printf("ParsedPermArgs: suppressing further warnings\n");
747 }
748 }
749 }
750 return parsed;
751}
752
753static int ApplyParsedPerms(
Michael Runged4a63422014-10-22 19:48:41 -0700754 State * state,
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700755 const char* filename,
756 const struct stat *statptr,
757 struct perm_parsed_args parsed)
758{
759 int bad = 0;
760
Nick Kralevich68802412014-10-23 20:36:42 -0700761 if (parsed.has_selabel) {
762 if (lsetfilecon(filename, parsed.selabel) != 0) {
763 uiPrintf(state, "ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n",
764 filename, parsed.selabel, strerror(errno));
765 bad++;
766 }
767 }
768
Nick Kraleviche4612512013-09-10 15:34:19 -0700769 /* ignore symlinks */
770 if (S_ISLNK(statptr->st_mode)) {
Nick Kralevich68802412014-10-23 20:36:42 -0700771 return bad;
Nick Kraleviche4612512013-09-10 15:34:19 -0700772 }
773
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700774 if (parsed.has_uid) {
775 if (chown(filename, parsed.uid, -1) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700776 uiPrintf(state, "ApplyParsedPerms: chown of %s to %d failed: %s\n",
777 filename, parsed.uid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700778 bad++;
779 }
780 }
781
782 if (parsed.has_gid) {
783 if (chown(filename, -1, parsed.gid) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700784 uiPrintf(state, "ApplyParsedPerms: chgrp of %s to %d failed: %s\n",
785 filename, parsed.gid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700786 bad++;
787 }
788 }
789
790 if (parsed.has_mode) {
791 if (chmod(filename, parsed.mode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700792 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
793 filename, parsed.mode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700794 bad++;
795 }
796 }
797
798 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
799 if (chmod(filename, parsed.dmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700800 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
801 filename, parsed.dmode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700802 bad++;
803 }
804 }
805
806 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
807 if (chmod(filename, parsed.fmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700808 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700809 filename, parsed.fmode, strerror(errno));
810 bad++;
811 }
812 }
813
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700814 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
815 if (parsed.capabilities == 0) {
816 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
817 // Report failure unless it's ENODATA (attribute not set)
Michael Runged4a63422014-10-22 19:48:41 -0700818 uiPrintf(state, "ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700819 filename, parsed.capabilities, strerror(errno));
820 bad++;
821 }
822 } else {
823 struct vfs_cap_data cap_data;
824 memset(&cap_data, 0, sizeof(cap_data));
825 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
826 cap_data.data[0].permitted = (uint32_t) (parsed.capabilities & 0xffffffff);
827 cap_data.data[0].inheritable = 0;
828 cap_data.data[1].permitted = (uint32_t) (parsed.capabilities >> 32);
829 cap_data.data[1].inheritable = 0;
830 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700831 uiPrintf(state, "ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n",
832 filename, parsed.capabilities, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700833 bad++;
834 }
835 }
836 }
837
838 return bad;
839}
840
841// nftw doesn't allow us to pass along context, so we need to use
842// global variables. *sigh*
843static struct perm_parsed_args recursive_parsed_args;
Michael Runged4a63422014-10-22 19:48:41 -0700844static State* recursive_state;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700845
846static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr,
847 int fileflags, struct FTW *pfwt) {
Michael Runged4a63422014-10-22 19:48:41 -0700848 return ApplyParsedPerms(recursive_state, filename, statptr, recursive_parsed_args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700849}
850
851static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700852 int bad = 0;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700853 struct stat sb;
854 Value* result = NULL;
855
856 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
857
858 if ((argc % 2) != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700859 return ErrorAbort(state, kArgsParsingFailure,
860 "%s() expects an odd number of arguments, got %d", name, argc);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700861 }
862
863 char** args = ReadVarArgs(state, argc, argv);
864 if (args == NULL) return NULL;
865
866 if (lstat(args[0], &sb) == -1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700867 result = ErrorAbort(state, kSetMetadataFailure, "%s: Error on lstat of \"%s\": %s",
868 name, args[0], strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700869 goto done;
870 }
871
Tao Baoba9a42a2015-06-23 23:23:33 -0700872 {
873 struct perm_parsed_args parsed = ParsePermArgs(state, argc, args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700874
Tao Baoba9a42a2015-06-23 23:23:33 -0700875 if (recursive) {
876 recursive_parsed_args = parsed;
877 recursive_state = state;
878 bad += nftw(args[0], do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
879 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
880 recursive_state = NULL;
881 } else {
882 bad += ApplyParsedPerms(state, args[0], &sb, parsed);
883 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700884 }
885
886done:
Tao Baoba9a42a2015-06-23 23:23:33 -0700887 for (int i = 0; i < argc; ++i) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700888 free(args[i]);
889 }
890 free(args);
891
892 if (result != NULL) {
893 return result;
894 }
895
896 if (bad > 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700897 return ErrorAbort(state, kSetMetadataFailure, "%s: some changes failed", name);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700898 }
899
900 return StringValue(strdup(""));
901}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700902
Doug Zongker512536a2010-02-17 16:11:44 -0800903Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700904 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700905 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700906 }
Tao Baoba9a42a2015-06-23 23:23:33 -0700907 char* key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700908 if (key == NULL) return NULL;
909
910 char value[PROPERTY_VALUE_MAX];
911 property_get(key, value, "");
912 free(key);
913
Doug Zongker512536a2010-02-17 16:11:44 -0800914 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700915}
916
917
Doug Zongker47cace92009-06-18 10:11:50 -0700918// file_getprop(file, key)
919//
920// interprets 'file' as a getprop-style file (key=value pairs, one
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700921// per line. # comment lines,blank lines, lines without '=' ignored),
922// and returns the value for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800923Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700924 char* result = NULL;
925 char* buffer = NULL;
926 char* filename;
927 char* key;
928 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
929 return NULL;
930 }
931
932 struct stat st;
933 if (stat(filename, &st) < 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700934 ErrorAbort(state, kFileGetPropFailure, "%s: failed to stat \"%s\": %s", name, filename,
935 strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -0700936 goto done;
937 }
938
939#define MAX_FILE_GETPROP_SIZE 65536
940
941 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
Tianjie Xu16255832016-04-30 11:49:59 -0700942 ErrorAbort(state, kFileGetPropFailure, "%s too large for %s (max %d)", filename, name,
943 MAX_FILE_GETPROP_SIZE);
Doug Zongker47cace92009-06-18 10:11:50 -0700944 goto done;
945 }
946
Tao Baoba9a42a2015-06-23 23:23:33 -0700947 buffer = reinterpret_cast<char*>(malloc(st.st_size+1));
Doug Zongker47cace92009-06-18 10:11:50 -0700948 if (buffer == NULL) {
Tianjie Xu84478e82016-05-23 11:42:40 -0700949 ErrorAbort(state, kFileGetPropFailure, "%s: failed to alloc %zu bytes", name,
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700950 static_cast<size_t>(st.st_size+1));
Doug Zongker47cace92009-06-18 10:11:50 -0700951 goto done;
952 }
953
Tao Baoba9a42a2015-06-23 23:23:33 -0700954 FILE* f;
Jed Estepa7b9a462015-12-15 16:04:53 -0800955 f = ota_fopen(filename, "rb");
Doug Zongker47cace92009-06-18 10:11:50 -0700956 if (f == NULL) {
Tianjie Xu16255832016-04-30 11:49:59 -0700957 ErrorAbort(state, kFileOpenFailure, "%s: failed to open %s: %s", name, filename,
958 strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -0700959 goto done;
960 }
961
Jed Estepa7b9a462015-12-15 16:04:53 -0800962 if (ota_fread(buffer, 1, st.st_size, f) != static_cast<size_t>(st.st_size)) {
Tianjie Xu84478e82016-05-23 11:42:40 -0700963 ErrorAbort(state, kFreadFailure, "%s: failed to read %zu bytes from %s",
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700964 name, static_cast<size_t>(st.st_size), filename);
Jed Estepa7b9a462015-12-15 16:04:53 -0800965 ota_fclose(f);
Doug Zongker47cace92009-06-18 10:11:50 -0700966 goto done;
967 }
968 buffer[st.st_size] = '\0';
969
Jed Estepa7b9a462015-12-15 16:04:53 -0800970 ota_fclose(f);
Doug Zongker47cace92009-06-18 10:11:50 -0700971
Tao Baoba9a42a2015-06-23 23:23:33 -0700972 char* line;
973 line = strtok(buffer, "\n");
Doug Zongker47cace92009-06-18 10:11:50 -0700974 do {
975 // skip whitespace at start of line
976 while (*line && isspace(*line)) ++line;
977
978 // comment or blank line: skip to next line
979 if (*line == '\0' || *line == '#') continue;
980
981 char* equal = strchr(line, '=');
982 if (equal == NULL) {
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700983 continue;
Doug Zongker47cace92009-06-18 10:11:50 -0700984 }
985
986 // trim whitespace between key and '='
987 char* key_end = equal-1;
988 while (key_end > line && isspace(*key_end)) --key_end;
989 key_end[1] = '\0';
990
991 // not the key we're looking for
992 if (strcmp(key, line) != 0) continue;
993
994 // skip whitespace after the '=' to the start of the value
995 char* val_start = equal+1;
996 while(*val_start && isspace(*val_start)) ++val_start;
997
998 // trim trailing whitespace
999 char* val_end = val_start + strlen(val_start)-1;
1000 while (val_end > val_start && isspace(*val_end)) --val_end;
1001 val_end[1] = '\0';
1002
1003 result = strdup(val_start);
1004 break;
1005
1006 } while ((line = strtok(NULL, "\n")));
1007
1008 if (result == NULL) result = strdup("");
1009
1010 done:
1011 free(filename);
1012 free(key);
1013 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -08001014 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -07001015}
1016
Doug Zongker8edb00c2009-06-11 17:21:44 -07001017// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001018Value* ApplyPatchSpaceFn(const char* name, State* state,
1019 int argc, Expr* argv[]) {
1020 char* bytes_str;
1021 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
1022 return NULL;
1023 }
1024
Tao Baob15fd222015-09-24 11:10:51 -07001025 size_t bytes;
1026 if (!android::base::ParseUint(bytes_str, &bytes)) {
Tianjie Xu16255832016-04-30 11:49:59 -07001027 ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count\n\n",
1028 name, bytes_str);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001029 free(bytes_str);
Tao Baob15fd222015-09-24 11:10:51 -07001030 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001031 }
1032
1033 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
1034}
1035
Doug Zongker52b40362014-02-10 15:30:30 -08001036// apply_patch(file, size, init_sha1, tgt_sha1, patch)
1037
Doug Zongker512536a2010-02-17 16:11:44 -08001038Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001039 if (argc < 6 || (argc % 2) == 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001040 return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 6 args and an "
1041 "even number, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001042 }
1043
Doug Zongkerc4351c72010-02-22 14:46:32 -08001044 char* source_filename;
1045 char* target_filename;
1046 char* target_sha1;
1047 char* target_size_str;
1048 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
1049 &target_sha1, &target_size_str) < 0) {
1050 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001051 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001052
Tao Baob15fd222015-09-24 11:10:51 -07001053 size_t target_size;
1054 if (!android::base::ParseUint(target_size_str, &target_size)) {
Tianjie Xu16255832016-04-30 11:49:59 -07001055 ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count",
1056 name, target_size_str);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001057 free(source_filename);
1058 free(target_filename);
1059 free(target_sha1);
1060 free(target_size_str);
Tao Baob15fd222015-09-24 11:10:51 -07001061 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001062 }
1063
1064 int patchcount = (argc-4) / 2;
Yabin Cui64be2132016-02-03 18:16:02 -08001065 std::unique_ptr<Value*, decltype(&free)> arg_values(ReadValueVarArgs(state, argc-4, argv+4),
1066 free);
1067 if (!arg_values) {
1068 return nullptr;
1069 }
1070 std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> patch_shas;
1071 std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> patches;
1072 // Protect values by unique_ptrs first to get rid of memory leak.
1073 for (int i = 0; i < patchcount * 2; i += 2) {
1074 patch_shas.emplace_back(arg_values.get()[i], FreeValue);
1075 patches.emplace_back(arg_values.get()[i+1], FreeValue);
1076 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001077
Yabin Cui64be2132016-02-03 18:16:02 -08001078 for (int i = 0; i < patchcount; ++i) {
1079 if (patch_shas[i]->type != VAL_STRING) {
Tianjie Xu16255832016-04-30 11:49:59 -07001080 ErrorAbort(state, kArgsParsingFailure, "%s(): sha-1 #%d is not string", name, i);
Yabin Cui64be2132016-02-03 18:16:02 -08001081 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001082 }
Yabin Cui64be2132016-02-03 18:16:02 -08001083 if (patches[i]->type != VAL_BLOB) {
Tianjie Xu16255832016-04-30 11:49:59 -07001084 ErrorAbort(state, kArgsParsingFailure, "%s(): patch #%d is not blob", name, i);
Yabin Cui64be2132016-02-03 18:16:02 -08001085 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001086 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001087 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001088
Yabin Cui64be2132016-02-03 18:16:02 -08001089 std::vector<char*> patch_sha_str;
1090 std::vector<Value*> patch_ptrs;
1091 for (int i = 0; i < patchcount; ++i) {
1092 patch_sha_str.push_back(patch_shas[i]->data);
1093 patch_ptrs.push_back(patches[i].get());
Doug Zongker8edb00c2009-06-11 17:21:44 -07001094 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001095
1096 int result = applypatch(source_filename, target_filename,
1097 target_sha1, target_size,
Yabin Cui64be2132016-02-03 18:16:02 -08001098 patchcount, patch_sha_str.data(), patch_ptrs.data(), NULL);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001099
1100 return StringValue(strdup(result == 0 ? "t" : ""));
1101}
1102
1103// apply_patch_check(file, [sha1_1, ...])
1104Value* ApplyPatchCheckFn(const char* name, State* state,
1105 int argc, Expr* argv[]) {
1106 if (argc < 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001107 return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 1 arg, got %d",
Doug Zongkerc4351c72010-02-22 14:46:32 -08001108 name, argc);
1109 }
1110
1111 char* filename;
1112 if (ReadArgs(state, argv, 1, &filename) < 0) {
1113 return NULL;
1114 }
1115
1116 int patchcount = argc-1;
1117 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
1118
1119 int result = applypatch_check(filename, patchcount, sha1s);
1120
1121 int i;
1122 for (i = 0; i < patchcount; ++i) {
1123 free(sha1s[i]);
1124 }
1125 free(sha1s);
1126
1127 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001128}
1129
Tao Bao1107d962015-09-09 17:16:55 -07001130// This is the updater side handler for ui_print() in edify script. Contents
1131// will be sent over to the recovery side for on-screen display.
Doug Zongker512536a2010-02-17 16:11:44 -08001132Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001133 char** args = ReadVarArgs(state, argc, argv);
1134 if (args == NULL) {
1135 return NULL;
1136 }
1137
Tao Bao1107d962015-09-09 17:16:55 -07001138 std::string buffer;
1139 for (int i = 0; i < argc; ++i) {
1140 buffer += args[i];
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001141 free(args[i]);
1142 }
1143 free(args);
Tao Bao1107d962015-09-09 17:16:55 -07001144
1145 buffer += "\n";
Michael Runged4a63422014-10-22 19:48:41 -07001146 uiPrint(state, buffer);
Tao Bao1107d962015-09-09 17:16:55 -07001147 return StringValue(strdup(buffer.c_str()));
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001148}
1149
Doug Zongkerd0181b82011-10-19 10:51:12 -07001150Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
1151 if (argc != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001152 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %d", name, argc);
Doug Zongkerd0181b82011-10-19 10:51:12 -07001153 }
1154 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
1155 return StringValue(strdup("t"));
1156}
1157
Doug Zongker512536a2010-02-17 16:11:44 -08001158Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001159 if (argc < 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001160 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001161 }
1162 char** args = ReadVarArgs(state, argc, argv);
1163 if (args == NULL) {
1164 return NULL;
1165 }
1166
Tao Baoba9a42a2015-06-23 23:23:33 -07001167 char** args2 = reinterpret_cast<char**>(malloc(sizeof(char*) * (argc+1)));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001168 memcpy(args2, args, sizeof(char*) * argc);
1169 args2[argc] = NULL;
1170
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001171 printf("about to run program [%s] with %d args\n", args2[0], argc);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001172
1173 pid_t child = fork();
1174 if (child == 0) {
1175 execv(args2[0], args2);
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001176 printf("run_program: execv failed: %s\n", strerror(errno));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001177 _exit(1);
1178 }
1179 int status;
1180 waitpid(child, &status, 0);
1181 if (WIFEXITED(status)) {
1182 if (WEXITSTATUS(status) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001183 printf("run_program: child exited with status %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001184 WEXITSTATUS(status));
1185 }
1186 } else if (WIFSIGNALED(status)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001187 printf("run_program: child terminated by signal %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001188 WTERMSIG(status));
1189 }
1190
1191 int i;
1192 for (i = 0; i < argc; ++i) {
1193 free(args[i]);
1194 }
1195 free(args);
1196 free(args2);
1197
1198 char buffer[20];
1199 sprintf(buffer, "%d", status);
1200
Doug Zongker512536a2010-02-17 16:11:44 -08001201 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001202}
1203
Doug Zongker512536a2010-02-17 16:11:44 -08001204// sha1_check(data)
1205// to return the sha1 of the data (given in the format returned by
1206// read_file).
1207//
1208// sha1_check(data, sha1_hex, [sha1_hex, ...])
1209// returns the sha1 of the file if it matches any of the hex
1210// strings passed, or "" if it does not equal any of them.
1211//
1212Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1213 if (argc < 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001214 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
Doug Zongker512536a2010-02-17 16:11:44 -08001215 }
1216
Yabin Cui64be2132016-02-03 18:16:02 -08001217 std::unique_ptr<Value*, decltype(&free)> arg_values(ReadValueVarArgs(state, argc, argv), free);
1218 if (arg_values == nullptr) {
1219 return nullptr;
1220 }
1221 std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> args;
1222 for (int i = 0; i < argc; ++i) {
1223 args.emplace_back(arg_values.get()[i], FreeValue);
Doug Zongker512536a2010-02-17 16:11:44 -08001224 }
1225
1226 if (args[0]->size < 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001227 return StringValue(strdup(""));
1228 }
Sen Jiangc48cb5e2016-02-04 16:23:21 +08001229 uint8_t digest[SHA_DIGEST_LENGTH];
1230 SHA1(reinterpret_cast<uint8_t*>(args[0]->data), args[0]->size, digest);
Doug Zongker512536a2010-02-17 16:11:44 -08001231
1232 if (argc == 1) {
1233 return StringValue(PrintSha1(digest));
1234 }
1235
1236 int i;
Yabin Cui64be2132016-02-03 18:16:02 -08001237 uint8_t arg_digest[SHA_DIGEST_LENGTH];
Doug Zongker512536a2010-02-17 16:11:44 -08001238 for (i = 1; i < argc; ++i) {
1239 if (args[i]->type != VAL_STRING) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001240 printf("%s(): arg %d is not a string; skipping",
Doug Zongker512536a2010-02-17 16:11:44 -08001241 name, i);
1242 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1243 // Warn about bad args and skip them.
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001244 printf("%s(): error parsing \"%s\" as sha-1; skipping",
1245 name, args[i]->data);
Sen Jiangc48cb5e2016-02-04 16:23:21 +08001246 } else if (memcmp(digest, arg_digest, SHA_DIGEST_LENGTH) == 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001247 break;
1248 }
Doug Zongker512536a2010-02-17 16:11:44 -08001249 }
1250 if (i >= argc) {
1251 // Didn't match any of the hex strings; return false.
1252 return StringValue(strdup(""));
1253 }
Yabin Cui64be2132016-02-03 18:16:02 -08001254 // Found a match.
1255 return args[i].release();
Doug Zongker512536a2010-02-17 16:11:44 -08001256}
1257
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001258// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001259// is actually a FileContents*).
1260Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1261 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001262 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker512536a2010-02-17 16:11:44 -08001263 }
1264 char* filename;
1265 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1266
Yabin Cuid6c93af2016-02-10 16:41:10 -08001267 Value* v = static_cast<Value*>(malloc(sizeof(Value)));
1268 if (v == nullptr) {
1269 return nullptr;
1270 }
Doug Zongker512536a2010-02-17 16:11:44 -08001271 v->type = VAL_BLOB;
Yabin Cuid6c93af2016-02-10 16:41:10 -08001272 v->size = -1;
1273 v->data = nullptr;
Doug Zongker512536a2010-02-17 16:11:44 -08001274
1275 FileContents fc;
Tao Baoefacd802016-06-11 03:56:38 -07001276 if (LoadFileContents(filename, &fc) == 0) {
Yabin Cuid6c93af2016-02-10 16:41:10 -08001277 v->data = static_cast<char*>(malloc(fc.data.size()));
1278 if (v->data != nullptr) {
1279 memcpy(v->data, fc.data.data(), fc.data.size());
1280 v->size = fc.data.size();
1281 }
Doug Zongker512536a2010-02-17 16:11:44 -08001282 }
Doug Zongker512536a2010-02-17 16:11:44 -08001283 free(filename);
1284 return v;
1285}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001286
Doug Zongkerc87bab12013-11-25 13:53:25 -08001287// Immediately reboot the device. Recovery is not finished normally,
1288// so if you reboot into recovery it will re-start applying the
1289// current package (because nothing has cleared the copy of the
1290// arguments stored in the BCB).
1291//
1292// The argument is the partition name passed to the android reboot
1293// property. It can be "recovery" to boot from the recovery
1294// partition, or "" (empty string) to boot from the regular boot
1295// partition.
1296Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) {
1297 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -07001298 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001299 }
1300
1301 char* filename;
1302 char* property;
1303 if (ReadArgs(state, argv, 2, &filename, &property) < 0) return NULL;
1304
1305 char buffer[80];
1306
1307 // zero out the 'command' field of the bootloader message.
1308 memset(buffer, 0, sizeof(((struct bootloader_message*)0)->command));
Jed Estepa7b9a462015-12-15 16:04:53 -08001309 FILE* f = ota_fopen(filename, "r+b");
Doug Zongkerc87bab12013-11-25 13:53:25 -08001310 fseek(f, offsetof(struct bootloader_message, command), SEEK_SET);
Jed Estepa7b9a462015-12-15 16:04:53 -08001311 ota_fwrite(buffer, sizeof(((struct bootloader_message*)0)->command), 1, f);
1312 ota_fclose(f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001313 free(filename);
1314
1315 strcpy(buffer, "reboot,");
1316 if (property != NULL) {
1317 strncat(buffer, property, sizeof(buffer)-10);
1318 }
1319
1320 property_set(ANDROID_RB_PROPERTY, buffer);
1321
1322 sleep(5);
1323 free(property);
Tianjie Xu16255832016-04-30 11:49:59 -07001324 ErrorAbort(state, kRebootFailure, "%s() failed to reboot", name);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001325 return NULL;
1326}
1327
1328// Store a string value somewhere that future invocations of recovery
1329// can access it. This value is called the "stage" and can be used to
1330// drive packages that need to do reboots in the middle of
1331// installation and keep track of where they are in the multi-stage
1332// install.
1333//
1334// The first argument is the block device for the misc partition
1335// ("/misc" in the fstab), which is where this value is stored. The
1336// second argument is the string to store; it should not exceed 31
1337// bytes.
1338Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
1339 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -07001340 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001341 }
1342
1343 char* filename;
1344 char* stagestr;
1345 if (ReadArgs(state, argv, 2, &filename, &stagestr) < 0) return NULL;
1346
1347 // Store this value in the misc partition, immediately after the
1348 // bootloader message that the main recovery uses to save its
1349 // arguments in case of the device restarting midway through
1350 // package installation.
Jed Estepa7b9a462015-12-15 16:04:53 -08001351 FILE* f = ota_fopen(filename, "r+b");
Doug Zongkerc87bab12013-11-25 13:53:25 -08001352 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1353 int to_write = strlen(stagestr)+1;
1354 int max_size = sizeof(((struct bootloader_message*)0)->stage);
1355 if (to_write > max_size) {
1356 to_write = max_size;
1357 stagestr[max_size-1] = 0;
1358 }
Jed Estepa7b9a462015-12-15 16:04:53 -08001359 ota_fwrite(stagestr, to_write, 1, f);
1360 ota_fclose(f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001361
1362 free(stagestr);
1363 return StringValue(filename);
1364}
1365
1366// Return the value most recently saved with SetStageFn. The argument
1367// is the block device for the misc partition.
1368Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001369 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001370 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001371 }
1372
1373 char* filename;
1374 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1375
1376 char buffer[sizeof(((struct bootloader_message*)0)->stage)];
Jed Estepa7b9a462015-12-15 16:04:53 -08001377 FILE* f = ota_fopen(filename, "rb");
Doug Zongkerc87bab12013-11-25 13:53:25 -08001378 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
Jed Estepa7b9a462015-12-15 16:04:53 -08001379 ota_fread(buffer, sizeof(buffer), 1, f);
1380 ota_fclose(f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001381 buffer[sizeof(buffer)-1] = '\0';
1382
1383 return StringValue(strdup(buffer));
1384}
1385
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001386Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) {
1387 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -07001388 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001389 }
1390
1391 char* filename;
1392 char* len_str;
1393 if (ReadArgs(state, argv, 2, &filename, &len_str) < 0) return NULL;
1394
Tao Baob15fd222015-09-24 11:10:51 -07001395 size_t len;
1396 android::base::ParseUint(len_str, &len);
Jed Estepa7b9a462015-12-15 16:04:53 -08001397 int fd = ota_open(filename, O_WRONLY, 0644);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001398 int success = wipe_block_device(fd, len);
1399
1400 free(filename);
1401 free(len_str);
1402
Jed Estepa7b9a462015-12-15 16:04:53 -08001403 ota_close(fd);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001404
1405 return StringValue(strdup(success ? "t" : ""));
1406}
1407
Doug Zongkerc704e062014-05-23 08:40:35 -07001408Value* EnableRebootFn(const char* name, State* state, int argc, Expr* argv[]) {
1409 if (argc != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001410 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %d", name, argc);
Doug Zongkerc704e062014-05-23 08:40:35 -07001411 }
1412 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
1413 fprintf(ui->cmd_pipe, "enable_reboot\n");
1414 return StringValue(strdup("t"));
1415}
1416
Michael Rungeacf47db2014-11-21 00:12:28 -08001417Value* Tune2FsFn(const char* name, State* state, int argc, Expr* argv[]) {
1418 if (argc == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001419 return ErrorAbort(state, kArgsParsingFailure, "%s() expects args, got %d", name, argc);
Michael Rungeacf47db2014-11-21 00:12:28 -08001420 }
1421
1422 char** args = ReadVarArgs(state, argc, argv);
1423 if (args == NULL) {
Tianjie Xu16255832016-04-30 11:49:59 -07001424 return ErrorAbort(state, kArgsParsingFailure, "%s() could not read args", name);
Michael Rungeacf47db2014-11-21 00:12:28 -08001425 }
1426
Tao Baoba9a42a2015-06-23 23:23:33 -07001427 char** args2 = reinterpret_cast<char**>(malloc(sizeof(char*) * (argc+1)));
Michael Rungeacf47db2014-11-21 00:12:28 -08001428 // Tune2fs expects the program name as its args[0]
1429 args2[0] = strdup(name);
Tao Baoba9a42a2015-06-23 23:23:33 -07001430 for (int i = 0; i < argc; ++i) {
Michael Rungeacf47db2014-11-21 00:12:28 -08001431 args2[i + 1] = args[i];
1432 }
1433 int result = tune2fs_main(argc + 1, args2);
Tao Baoba9a42a2015-06-23 23:23:33 -07001434 for (int i = 0; i < argc; ++i) {
Michael Rungeacf47db2014-11-21 00:12:28 -08001435 free(args[i]);
1436 }
1437 free(args);
1438
1439 free(args2[0]);
1440 free(args2);
1441 if (result != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001442 return ErrorAbort(state, kTune2FsFailure, "%s() returned error code %d",
1443 name, result);
Michael Rungeacf47db2014-11-21 00:12:28 -08001444 }
1445 return StringValue(strdup("t"));
1446}
1447
Doug Zongker9931f7f2009-06-10 14:11:53 -07001448void RegisterInstallFunctions() {
1449 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001450 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001451 RegisterFunction("unmount", UnmountFn);
1452 RegisterFunction("format", FormatFn);
1453 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001454 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001455 RegisterFunction("delete", DeleteFn);
1456 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001457 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1458 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001459 RegisterFunction("symlink", SymlinkFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001460
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001461 // Usage:
1462 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1463 // Example:
1464 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1465 RegisterFunction("set_metadata", SetMetadataFn);
1466
1467 // Usage:
1468 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1469 // Example:
1470 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1471 RegisterFunction("set_metadata_recursive", SetMetadataFn);
1472
Doug Zongker8edb00c2009-06-11 17:21:44 -07001473 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001474 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001475
1476 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001477 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1478 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001479
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001480 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001481
Doug Zongker512536a2010-02-17 16:11:44 -08001482 RegisterFunction("read_file", ReadFileFn);
1483 RegisterFunction("sha1_check", Sha1CheckFn);
Michael Rungece7ca712013-11-06 17:42:20 -08001484 RegisterFunction("rename", RenameFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001485
Doug Zongkerd0181b82011-10-19 10:51:12 -07001486 RegisterFunction("wipe_cache", WipeCacheFn);
1487
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001488 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001489
1490 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001491
1492 RegisterFunction("reboot_now", RebootNowFn);
1493 RegisterFunction("get_stage", GetStageFn);
1494 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -07001495
1496 RegisterFunction("enable_reboot", EnableRebootFn);
Michael Rungeacf47db2014-11-21 00:12:28 -08001497 RegisterFunction("tune2fs", Tune2FsFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001498}