blob: 4f526840130d99d0e7cabc0e2b157bae736b90d3 [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"
Sen Jiangc48cb5e2016-02-04 16:23:21 +080051#include "openssl/sha.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070052#include "minzip/DirUtil.h"
53#include "mtdutils/mounts.h"
54#include "mtdutils/mtdutils.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#ifdef USE_EXT4
61#include "make_ext4fs.h"
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -080062#include "wipe.h"
Doug Zongker3d177d02010-07-01 09:18:44 -070063#endif
64
Tao Bao1107d962015-09-09 17:16:55 -070065// Send over the buffer to recovery though the command pipe.
66static void uiPrint(State* state, const std::string& buffer) {
67 UpdaterInfo* ui = reinterpret_cast<UpdaterInfo*>(state->cookie);
Tao Baob6918c72015-05-19 17:02:16 -070068
Tao Bao1107d962015-09-09 17:16:55 -070069 // "line1\nline2\n" will be split into 3 tokens: "line1", "line2" and "".
70 // So skip sending empty strings to UI.
71 std::vector<std::string> lines = android::base::Split(buffer, "\n");
72 for (auto& line: lines) {
73 if (!line.empty()) {
74 fprintf(ui->cmd_pipe, "ui_print %s\n", line.c_str());
75 fprintf(ui->cmd_pipe, "ui_print\n");
76 }
77 }
78
79 // On the updater side, we need to dump the contents to stderr (which has
80 // been redirected to the log file). Because the recovery will only print
81 // the contents to screen when processing pipe command ui_print.
82 fprintf(stderr, "%s", buffer.c_str());
Michael Runged4a63422014-10-22 19:48:41 -070083}
84
85__attribute__((__format__(printf, 2, 3))) __nonnull((2))
86void uiPrintf(State* state, const char* format, ...) {
Tao Bao1107d962015-09-09 17:16:55 -070087 std::string error_msg;
88
Michael Runged4a63422014-10-22 19:48:41 -070089 va_list ap;
90 va_start(ap, format);
Tao Bao1107d962015-09-09 17:16:55 -070091 android::base::StringAppendV(&error_msg, format, ap);
Michael Runged4a63422014-10-22 19:48:41 -070092 va_end(ap);
Tao Bao1107d962015-09-09 17:16:55 -070093
Michael Runged4a63422014-10-22 19:48:41 -070094 uiPrint(state, error_msg);
95}
96
Doug Zongker52b40362014-02-10 15:30:30 -080097// Take a sha-1 digest and return it as a newly-allocated hex string.
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070098char* PrintSha1(const uint8_t* digest) {
Sen Jiangc48cb5e2016-02-04 16:23:21 +080099 char* buffer = reinterpret_cast<char*>(malloc(SHA_DIGEST_LENGTH*2 + 1));
Doug Zongker52b40362014-02-10 15:30:30 -0800100 const char* alphabet = "0123456789abcdef";
Tao Baoba9a42a2015-06-23 23:23:33 -0700101 size_t i;
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800102 for (i = 0; i < SHA_DIGEST_LENGTH; ++i) {
Doug Zongker52b40362014-02-10 15:30:30 -0800103 buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf];
104 buffer[i*2+1] = alphabet[digest[i] & 0xf];
105 }
106 buffer[i*2] = '\0';
107 return buffer;
108}
109
Doug Zongker3d177d02010-07-01 09:18:44 -0700110// mount(fs_type, partition_type, location, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700111//
Doug Zongker3d177d02010-07-01 09:18:44 -0700112// fs_type="yaffs2" partition_type="MTD" location=partition
113// fs_type="ext4" partition_type="EMMC" location=device
Doug Zongker512536a2010-02-17 16:11:44 -0800114Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700115 char* result = NULL;
Michael Runge168f7772014-10-22 17:05:08 -0700116 if (argc != 4 && argc != 5) {
117 return ErrorAbort(state, "%s() expects 4-5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700118 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700119 char* fs_type;
120 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700121 char* location;
122 char* mount_point;
Michael Runge168f7772014-10-22 17:05:08 -0700123 char* mount_options;
124 bool has_mount_options;
125 if (argc == 5) {
126 has_mount_options = true;
127 if (ReadArgs(state, argv, 5, &fs_type, &partition_type,
128 &location, &mount_point, &mount_options) < 0) {
129 return NULL;
130 }
131 } else {
132 has_mount_options = false;
133 if (ReadArgs(state, argv, 4, &fs_type, &partition_type,
Doug Zongker3d177d02010-07-01 09:18:44 -0700134 &location, &mount_point) < 0) {
Michael Runge168f7772014-10-22 17:05:08 -0700135 return NULL;
136 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700137 }
138
Doug Zongker3d177d02010-07-01 09:18:44 -0700139 if (strlen(fs_type) == 0) {
140 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
141 goto done;
142 }
143 if (strlen(partition_type) == 0) {
144 ErrorAbort(state, "partition_type argument to %s() can't be empty",
145 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700146 goto done;
147 }
148 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700149 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700150 goto done;
151 }
152 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700153 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700154 goto done;
155 }
156
Tao Baoba9a42a2015-06-23 23:23:33 -0700157 {
158 char *secontext = NULL;
Stephen Smalley779701d2012-02-09 14:13:23 -0500159
Tao Baoba9a42a2015-06-23 23:23:33 -0700160 if (sehandle) {
161 selabel_lookup(sehandle, &secontext, mount_point, 0755);
162 setfscreatecon(secontext);
163 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500164
Tao Baoba9a42a2015-06-23 23:23:33 -0700165 mkdir(mount_point, 0755);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700166
Tao Baoba9a42a2015-06-23 23:23:33 -0700167 if (secontext) {
168 freecon(secontext);
169 setfscreatecon(NULL);
170 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500171 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500172
Doug Zongker3d177d02010-07-01 09:18:44 -0700173 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700174 mtd_scan_partitions();
175 const MtdPartition* mtd;
176 mtd = mtd_find_partition_by_name(location);
177 if (mtd == NULL) {
Tao Bao1107d962015-09-09 17:16:55 -0700178 uiPrintf(state, "%s: no mtd partition named \"%s\"\n",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700179 name, location);
180 result = strdup("");
181 goto done;
182 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700183 if (mtd_mount_partition(mtd, mount_point, fs_type, 0 /* rw */) != 0) {
Michael Runge5ddf4292014-10-24 14:14:41 -0700184 uiPrintf(state, "mtd mount of %s failed: %s\n",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700185 location, strerror(errno));
186 result = strdup("");
187 goto done;
188 }
189 result = mount_point;
190 } else {
Doug Zongker3d177d02010-07-01 09:18:44 -0700191 if (mount(location, mount_point, fs_type,
Michael Runge168f7772014-10-22 17:05:08 -0700192 MS_NOATIME | MS_NODEV | MS_NODIRATIME,
193 has_mount_options ? mount_options : "") < 0) {
Michael Runge5ddf4292014-10-24 14:14:41 -0700194 uiPrintf(state, "%s: failed to mount %s at %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700195 name, location, mount_point, strerror(errno));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700196 result = strdup("");
197 } else {
198 result = mount_point;
199 }
200 }
201
202done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700203 free(fs_type);
204 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700205 free(location);
206 if (result != mount_point) free(mount_point);
Michael Runge168f7772014-10-22 17:05:08 -0700207 if (has_mount_options) free(mount_options);
Doug Zongker512536a2010-02-17 16:11:44 -0800208 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700209}
210
Doug Zongker8edb00c2009-06-11 17:21:44 -0700211
212// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800213Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700214 char* result = NULL;
215 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700216 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700217 }
218 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700219 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700220 return NULL;
221 }
222 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700223 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700224 goto done;
225 }
226
227 scan_mounted_volumes();
Tao Baoba9a42a2015-06-23 23:23:33 -0700228 {
229 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
230 if (vol == NULL) {
231 result = strdup("");
232 } else {
233 result = mount_point;
234 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700235 }
236
237done:
238 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800239 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700240}
241
242
Doug Zongker512536a2010-02-17 16:11:44 -0800243Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700244 char* result = NULL;
245 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700246 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700247 }
248 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700249 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700250 return NULL;
251 }
252 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700253 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700254 goto done;
255 }
256
257 scan_mounted_volumes();
Tao Baoba9a42a2015-06-23 23:23:33 -0700258 {
259 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
260 if (vol == NULL) {
261 uiPrintf(state, "unmount of %s failed; no such volume\n", mount_point);
262 result = strdup("");
263 } else {
264 int ret = unmount_mounted_volume(vol);
265 if (ret != 0) {
266 uiPrintf(state, "unmount of %s failed (%d): %s\n",
267 mount_point, ret, strerror(errno));
268 }
269 result = mount_point;
Michael Runge5ddf4292014-10-24 14:14:41 -0700270 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700271 }
272
273done:
274 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800275 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700276}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700277
JP Abgrall37aedb32014-06-16 19:07:39 -0700278static int exec_cmd(const char* path, char* const argv[]) {
279 int status;
280 pid_t child;
281 if ((child = vfork()) == 0) {
282 execv(path, argv);
283 _exit(-1);
284 }
285 waitpid(child, &status, 0);
286 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
287 printf("%s failed with status %d\n", path, WEXITSTATUS(status));
288 }
289 return WEXITSTATUS(status);
290}
291
Doug Zongker8edb00c2009-06-11 17:21:44 -0700292
Stephen Smalley779701d2012-02-09 14:13:23 -0500293// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700294//
Stephen Smalley779701d2012-02-09 14:13:23 -0500295// fs_type="yaffs2" partition_type="MTD" location=partition fs_size=<bytes> mount_point=<location>
296// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700297// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
298// if fs_size == 0, then make fs uses the entire partition.
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800299// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700300// 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 -0800301Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700302 char* result = NULL;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400303 if (argc != 5) {
304 return ErrorAbort(state, "%s() expects 5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700305 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700306 char* fs_type;
307 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700308 char* location;
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800309 char* fs_size;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400310 char* mount_point;
Stephen Smalley779701d2012-02-09 14:13:23 -0500311
Stephen Smalley779701d2012-02-09 14:13:23 -0500312 if (ReadArgs(state, argv, 5, &fs_type, &partition_type, &location, &fs_size, &mount_point) < 0) {
313 return NULL;
314 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700315
Doug Zongker3d177d02010-07-01 09:18:44 -0700316 if (strlen(fs_type) == 0) {
317 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
318 goto done;
319 }
320 if (strlen(partition_type) == 0) {
321 ErrorAbort(state, "partition_type argument to %s() can't be empty",
322 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700323 goto done;
324 }
325 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700326 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700327 goto done;
328 }
329
Stephen Smalley516e4e22012-04-03 13:35:11 -0400330 if (strlen(mount_point) == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500331 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
332 goto done;
333 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500334
Doug Zongker3d177d02010-07-01 09:18:44 -0700335 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700336 mtd_scan_partitions();
337 const MtdPartition* mtd = mtd_find_partition_by_name(location);
338 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700339 printf("%s: no mtd partition named \"%s\"",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700340 name, location);
341 result = strdup("");
342 goto done;
343 }
344 MtdWriteContext* ctx = mtd_write_partition(mtd);
345 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700346 printf("%s: can't write \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700347 result = strdup("");
348 goto done;
349 }
350 if (mtd_erase_blocks(ctx, -1) == -1) {
351 mtd_write_close(ctx);
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700352 printf("%s: failed to erase \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700353 result = strdup("");
354 goto done;
355 }
356 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700357 printf("%s: failed to close \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700358 result = strdup("");
359 goto done;
360 }
361 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700362#ifdef USE_EXT4
363 } else if (strcmp(fs_type, "ext4") == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500364 int status = make_ext4fs(location, atoll(fs_size), mount_point, sehandle);
Doug Zongker3d177d02010-07-01 09:18:44 -0700365 if (status != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700366 printf("%s: make_ext4fs failed (%d) on %s",
Doug Zongker3d177d02010-07-01 09:18:44 -0700367 name, status, location);
368 result = strdup("");
369 goto done;
370 }
371 result = location;
JP Abgrall37aedb32014-06-16 19:07:39 -0700372 } else if (strcmp(fs_type, "f2fs") == 0) {
373 char *num_sectors;
374 if (asprintf(&num_sectors, "%lld", atoll(fs_size) / 512) <= 0) {
375 printf("format_volume: failed to create %s command for %s\n", fs_type, location);
376 result = strdup("");
377 goto done;
378 }
379 const char *f2fs_path = "/sbin/mkfs.f2fs";
380 const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", location, num_sectors, NULL};
381 int status = exec_cmd(f2fs_path, (char* const*)f2fs_argv);
382 free(num_sectors);
383 if (status != 0) {
384 printf("%s: mkfs.f2fs failed (%d) on %s",
385 name, status, location);
386 result = strdup("");
387 goto done;
388 }
389 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700390#endif
Doug Zongker9931f7f2009-06-10 14:11:53 -0700391 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700392 printf("%s: unsupported fs_type \"%s\" partition_type \"%s\"",
Doug Zongker3d177d02010-07-01 09:18:44 -0700393 name, fs_type, partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700394 }
395
396done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700397 free(fs_type);
398 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700399 if (result != location) free(location);
Doug Zongker512536a2010-02-17 16:11:44 -0800400 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700401}
402
Michael Rungece7ca712013-11-06 17:42:20 -0800403Value* RenameFn(const char* name, State* state, int argc, Expr* argv[]) {
404 char* result = NULL;
405 if (argc != 2) {
406 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
407 }
408
409 char* src_name;
410 char* dst_name;
411
412 if (ReadArgs(state, argv, 2, &src_name, &dst_name) < 0) {
413 return NULL;
414 }
415 if (strlen(src_name) == 0) {
416 ErrorAbort(state, "src_name argument to %s() can't be empty", name);
417 goto done;
418 }
419 if (strlen(dst_name) == 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700420 ErrorAbort(state, "dst_name argument to %s() can't be empty", name);
Michael Rungece7ca712013-11-06 17:42:20 -0800421 goto done;
422 }
Michael Rungea91ecc52014-07-21 17:40:02 -0700423 if (make_parents(dst_name) != 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700424 ErrorAbort(state, "Creating parent of %s failed, error %s",
Michael Rungea91ecc52014-07-21 17:40:02 -0700425 dst_name, strerror(errno));
Michael Runge2f0ef732014-10-22 14:28:23 -0700426 } else if (access(dst_name, F_OK) == 0 && access(src_name, F_OK) != 0) {
427 // File was already moved
428 result = dst_name;
Michael Rungea91ecc52014-07-21 17:40:02 -0700429 } else if (rename(src_name, dst_name) != 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700430 ErrorAbort(state, "Rename of %s to %s failed, error %s",
Michael Rungece7ca712013-11-06 17:42:20 -0800431 src_name, dst_name, strerror(errno));
432 } else {
433 result = dst_name;
434 }
435
436done:
437 free(src_name);
438 if (result != dst_name) free(dst_name);
439 return StringValue(result);
440}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700441
Doug Zongker512536a2010-02-17 16:11:44 -0800442Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700443 char** paths = reinterpret_cast<char**>(malloc(argc * sizeof(char*)));
444 for (int i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700445 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700446 if (paths[i] == NULL) {
Yabin Cui64be2132016-02-03 18:16:02 -0800447 for (int j = 0; j < i; ++j) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700448 free(paths[j]);
449 }
450 free(paths);
451 return NULL;
452 }
453 }
454
455 bool recursive = (strcmp(name, "delete_recursive") == 0);
456
457 int success = 0;
Tao Baoba9a42a2015-06-23 23:23:33 -0700458 for (int i = 0; i < argc; ++i) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700459 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
460 ++success;
461 free(paths[i]);
462 }
463 free(paths);
464
465 char buffer[10];
466 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800467 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700468}
469
Doug Zongker8edb00c2009-06-11 17:21:44 -0700470
Doug Zongker512536a2010-02-17 16:11:44 -0800471Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700472 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700473 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700474 }
475 char* frac_str;
476 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700477 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700478 return NULL;
479 }
480
481 double frac = strtod(frac_str, NULL);
Tao Baob15fd222015-09-24 11:10:51 -0700482 int sec;
483 android::base::ParseInt(sec_str, &sec);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700484
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700485 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700486 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
487
Doug Zongker9931f7f2009-06-10 14:11:53 -0700488 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800489 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700490}
491
Doug Zongker512536a2010-02-17 16:11:44 -0800492Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700493 if (argc != 1) {
494 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
495 }
496 char* frac_str;
497 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
498 return NULL;
499 }
500
501 double frac = strtod(frac_str, NULL);
502
503 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
504 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
505
Doug Zongker512536a2010-02-17 16:11:44 -0800506 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700507}
508
Doug Zongker8edb00c2009-06-11 17:21:44 -0700509// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800510Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700511 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700512 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700513 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700514 }
515 char* zip_path;
516 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700517 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700518
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700519 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700520
521 // To create a consistent system image, never use the clock for timestamps.
522 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
523
524 bool success = mzExtractRecursive(za, zip_path, dest_path,
Narayan Kamath9c0f5d62015-02-23 14:09:31 +0000525 &timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500526 NULL, NULL, sehandle);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700527 free(zip_path);
528 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800529 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700530}
531
Doug Zongker8edb00c2009-06-11 17:21:44 -0700532
533// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800534// or
535// package_extract_file(package_path)
536// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800537// function (the char* returned is actually a FileContents*).
538Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700539 int argc, Expr* argv[]) {
Doug Zongker5f875bf2014-08-22 14:53:43 -0700540 if (argc < 1 || argc > 2) {
541 return ErrorAbort(state, "%s() expects 1 or 2 args, got %d",
Doug Zongker6aece332010-02-01 14:40:12 -0800542 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700543 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700544 bool success = false;
Doug Zongker43772d22014-06-09 14:13:19 -0700545
Doug Zongker5f875bf2014-08-22 14:53:43 -0700546 if (argc == 2) {
547 // The two-argument version extracts to a file.
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800548
549 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700550
Doug Zongker6aece332010-02-01 14:40:12 -0800551 char* zip_path;
552 char* dest_path;
Doug Zongker5f875bf2014-08-22 14:53:43 -0700553 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800554
Doug Zongker6aece332010-02-01 14:40:12 -0800555 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
556 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700557 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800558 goto done2;
559 }
560
Tao Baoba9a42a2015-06-23 23:23:33 -0700561 {
Jed Estepa7b9a462015-12-15 16:04:53 -0800562 int fd = TEMP_FAILURE_RETRY(ota_open(dest_path, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC,
Tao Baod3cac342015-12-14 15:53:25 -0800563 S_IRUSR | S_IWUSR));
564 if (fd == -1) {
565 printf("%s: can't open %s for write: %s\n", name, dest_path, strerror(errno));
Tao Baoba9a42a2015-06-23 23:23:33 -0700566 goto done2;
567 }
Tao Baod3cac342015-12-14 15:53:25 -0800568 success = mzExtractZipEntryToFile(za, entry, fd);
Jed Estepa7b9a462015-12-15 16:04:53 -0800569 if (ota_fsync(fd) == -1) {
Tao Baod3cac342015-12-14 15:53:25 -0800570 printf("fsync of \"%s\" failed: %s\n", dest_path, strerror(errno));
571 success = false;
572 }
Jed Estepa7b9a462015-12-15 16:04:53 -0800573 if (ota_close(fd) == -1) {
Tao Baod3cac342015-12-14 15:53:25 -0800574 printf("close of \"%s\" failed: %s\n", dest_path, strerror(errno));
575 success = false;
576 }
Doug Zongker6aece332010-02-01 14:40:12 -0800577 }
Doug Zongker6aece332010-02-01 14:40:12 -0800578
579 done2:
580 free(zip_path);
581 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800582 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800583 } else {
584 // The one-argument version returns the contents of the file
585 // as the result.
586
587 char* zip_path;
Yabin Cui64be2132016-02-03 18:16:02 -0800588 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
589
Tao Baoba9a42a2015-06-23 23:23:33 -0700590 Value* v = reinterpret_cast<Value*>(malloc(sizeof(Value)));
Doug Zongker512536a2010-02-17 16:11:44 -0800591 v->type = VAL_BLOB;
592 v->size = -1;
593 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800594
Doug Zongker6aece332010-02-01 14:40:12 -0800595 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
596 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
597 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700598 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800599 goto done1;
600 }
601
Doug Zongker512536a2010-02-17 16:11:44 -0800602 v->size = mzGetZipEntryUncompLen(entry);
Tao Baoba9a42a2015-06-23 23:23:33 -0700603 v->data = reinterpret_cast<char*>(malloc(v->size));
Doug Zongker512536a2010-02-17 16:11:44 -0800604 if (v->data == NULL) {
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700605 printf("%s: failed to allocate %zd bytes for %s\n",
606 name, v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800607 goto done1;
608 }
609
Doug Zongker512536a2010-02-17 16:11:44 -0800610 success = mzExtractZipEntryToBuffer(za, entry,
611 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800612
613 done1:
614 free(zip_path);
615 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800616 free(v->data);
617 v->data = NULL;
618 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800619 }
Doug Zongker512536a2010-02-17 16:11:44 -0800620 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700621 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700622}
623
Doug Zongkera23075f2012-08-06 16:19:09 -0700624// Create all parent directories of name, if necessary.
625static int make_parents(char* name) {
626 char* p;
627 for (p = name + (strlen(name)-1); p > name; --p) {
628 if (*p != '/') continue;
629 *p = '\0';
630 if (make_parents(name) < 0) return -1;
631 int result = mkdir(name, 0700);
Michael Rungea91ecc52014-07-21 17:40:02 -0700632 if (result == 0) printf("created [%s]\n", name);
Doug Zongkera23075f2012-08-06 16:19:09 -0700633 *p = '/';
634 if (result == 0 || errno == EEXIST) {
635 // successfully created or already existed; we're done
636 return 0;
637 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700638 printf("failed to mkdir %s: %s\n", name, strerror(errno));
Doug Zongkera23075f2012-08-06 16:19:09 -0700639 return -1;
640 }
641 }
642 return 0;
643}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700644
Doug Zongker9931f7f2009-06-10 14:11:53 -0700645// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700646// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800647Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700648 if (argc == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700649 return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700650 }
651 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700652 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700653 if (target == NULL) return NULL;
654
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700655 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700656 if (srcs == NULL) {
657 free(target);
658 return NULL;
659 }
660
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700661 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700662 int i;
663 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700664 if (unlink(srcs[i]) < 0) {
665 if (errno != ENOENT) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700666 printf("%s: failed to remove %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700667 name, srcs[i], strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700668 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700669 }
670 }
Doug Zongkera23075f2012-08-06 16:19:09 -0700671 if (make_parents(srcs[i])) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700672 printf("%s: failed to symlink %s to %s: making parents failed\n",
Doug Zongkera23075f2012-08-06 16:19:09 -0700673 name, srcs[i], target);
674 ++bad;
675 }
Doug Zongker60babf82009-09-18 15:11:24 -0700676 if (symlink(target, srcs[i]) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700677 printf("%s: failed to symlink %s to %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700678 name, srcs[i], target, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700679 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700680 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700681 free(srcs[i]);
682 }
683 free(srcs);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700684 if (bad) {
685 return ErrorAbort(state, "%s: some symlinks failed", name);
686 }
Doug Zongker512536a2010-02-17 16:11:44 -0800687 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700688}
689
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700690struct perm_parsed_args {
691 bool has_uid;
692 uid_t uid;
693 bool has_gid;
694 gid_t gid;
695 bool has_mode;
696 mode_t mode;
697 bool has_fmode;
698 mode_t fmode;
699 bool has_dmode;
700 mode_t dmode;
701 bool has_selabel;
702 char* selabel;
703 bool has_capabilities;
704 uint64_t capabilities;
705};
706
Michael Runged4a63422014-10-22 19:48:41 -0700707static struct perm_parsed_args ParsePermArgs(State * state, int argc, char** args) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700708 int i;
709 struct perm_parsed_args parsed;
710 int bad = 0;
711 static int max_warnings = 20;
712
713 memset(&parsed, 0, sizeof(parsed));
714
715 for (i = 1; i < argc; i += 2) {
716 if (strcmp("uid", args[i]) == 0) {
717 int64_t uid;
718 if (sscanf(args[i+1], "%" SCNd64, &uid) == 1) {
719 parsed.uid = uid;
720 parsed.has_uid = true;
721 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700722 uiPrintf(state, "ParsePermArgs: invalid UID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700723 bad++;
724 }
725 continue;
726 }
727 if (strcmp("gid", args[i]) == 0) {
728 int64_t gid;
729 if (sscanf(args[i+1], "%" SCNd64, &gid) == 1) {
730 parsed.gid = gid;
731 parsed.has_gid = true;
732 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700733 uiPrintf(state, "ParsePermArgs: invalid GID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700734 bad++;
735 }
736 continue;
737 }
738 if (strcmp("mode", args[i]) == 0) {
739 int32_t mode;
740 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
741 parsed.mode = mode;
742 parsed.has_mode = true;
743 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700744 uiPrintf(state, "ParsePermArgs: invalid mode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700745 bad++;
746 }
747 continue;
748 }
749 if (strcmp("dmode", args[i]) == 0) {
750 int32_t mode;
751 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
752 parsed.dmode = mode;
753 parsed.has_dmode = true;
754 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700755 uiPrintf(state, "ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700756 bad++;
757 }
758 continue;
759 }
760 if (strcmp("fmode", args[i]) == 0) {
761 int32_t mode;
762 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
763 parsed.fmode = mode;
764 parsed.has_fmode = true;
765 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700766 uiPrintf(state, "ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700767 bad++;
768 }
769 continue;
770 }
771 if (strcmp("capabilities", args[i]) == 0) {
772 int64_t capabilities;
773 if (sscanf(args[i+1], "%" SCNi64, &capabilities) == 1) {
774 parsed.capabilities = capabilities;
775 parsed.has_capabilities = true;
776 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700777 uiPrintf(state, "ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700778 bad++;
779 }
780 continue;
781 }
782 if (strcmp("selabel", args[i]) == 0) {
783 if (args[i+1][0] != '\0') {
784 parsed.selabel = args[i+1];
785 parsed.has_selabel = true;
786 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700787 uiPrintf(state, "ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700788 bad++;
789 }
790 continue;
791 }
792 if (max_warnings != 0) {
793 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i]);
794 max_warnings--;
795 if (max_warnings == 0) {
796 printf("ParsedPermArgs: suppressing further warnings\n");
797 }
798 }
799 }
800 return parsed;
801}
802
803static int ApplyParsedPerms(
Michael Runged4a63422014-10-22 19:48:41 -0700804 State * state,
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700805 const char* filename,
806 const struct stat *statptr,
807 struct perm_parsed_args parsed)
808{
809 int bad = 0;
810
Nick Kralevich68802412014-10-23 20:36:42 -0700811 if (parsed.has_selabel) {
812 if (lsetfilecon(filename, parsed.selabel) != 0) {
813 uiPrintf(state, "ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n",
814 filename, parsed.selabel, strerror(errno));
815 bad++;
816 }
817 }
818
Nick Kraleviche4612512013-09-10 15:34:19 -0700819 /* ignore symlinks */
820 if (S_ISLNK(statptr->st_mode)) {
Nick Kralevich68802412014-10-23 20:36:42 -0700821 return bad;
Nick Kraleviche4612512013-09-10 15:34:19 -0700822 }
823
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700824 if (parsed.has_uid) {
825 if (chown(filename, parsed.uid, -1) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700826 uiPrintf(state, "ApplyParsedPerms: chown of %s to %d failed: %s\n",
827 filename, parsed.uid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700828 bad++;
829 }
830 }
831
832 if (parsed.has_gid) {
833 if (chown(filename, -1, parsed.gid) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700834 uiPrintf(state, "ApplyParsedPerms: chgrp of %s to %d failed: %s\n",
835 filename, parsed.gid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700836 bad++;
837 }
838 }
839
840 if (parsed.has_mode) {
841 if (chmod(filename, parsed.mode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700842 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
843 filename, parsed.mode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700844 bad++;
845 }
846 }
847
848 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
849 if (chmod(filename, parsed.dmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700850 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
851 filename, parsed.dmode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700852 bad++;
853 }
854 }
855
856 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
857 if (chmod(filename, parsed.fmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700858 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700859 filename, parsed.fmode, strerror(errno));
860 bad++;
861 }
862 }
863
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700864 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
865 if (parsed.capabilities == 0) {
866 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
867 // Report failure unless it's ENODATA (attribute not set)
Michael Runged4a63422014-10-22 19:48:41 -0700868 uiPrintf(state, "ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700869 filename, parsed.capabilities, strerror(errno));
870 bad++;
871 }
872 } else {
873 struct vfs_cap_data cap_data;
874 memset(&cap_data, 0, sizeof(cap_data));
875 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
876 cap_data.data[0].permitted = (uint32_t) (parsed.capabilities & 0xffffffff);
877 cap_data.data[0].inheritable = 0;
878 cap_data.data[1].permitted = (uint32_t) (parsed.capabilities >> 32);
879 cap_data.data[1].inheritable = 0;
880 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700881 uiPrintf(state, "ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n",
882 filename, parsed.capabilities, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700883 bad++;
884 }
885 }
886 }
887
888 return bad;
889}
890
891// nftw doesn't allow us to pass along context, so we need to use
892// global variables. *sigh*
893static struct perm_parsed_args recursive_parsed_args;
Michael Runged4a63422014-10-22 19:48:41 -0700894static State* recursive_state;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700895
896static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr,
897 int fileflags, struct FTW *pfwt) {
Michael Runged4a63422014-10-22 19:48:41 -0700898 return ApplyParsedPerms(recursive_state, filename, statptr, recursive_parsed_args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700899}
900
901static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700902 int bad = 0;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700903 struct stat sb;
904 Value* result = NULL;
905
906 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
907
908 if ((argc % 2) != 1) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700909 return ErrorAbort(state, "%s() expects an odd number of arguments, got %d", name, argc);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700910 }
911
912 char** args = ReadVarArgs(state, argc, argv);
913 if (args == NULL) return NULL;
914
915 if (lstat(args[0], &sb) == -1) {
916 result = ErrorAbort(state, "%s: Error on lstat of \"%s\": %s", name, args[0], strerror(errno));
917 goto done;
918 }
919
Tao Baoba9a42a2015-06-23 23:23:33 -0700920 {
921 struct perm_parsed_args parsed = ParsePermArgs(state, argc, args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700922
Tao Baoba9a42a2015-06-23 23:23:33 -0700923 if (recursive) {
924 recursive_parsed_args = parsed;
925 recursive_state = state;
926 bad += nftw(args[0], do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
927 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
928 recursive_state = NULL;
929 } else {
930 bad += ApplyParsedPerms(state, args[0], &sb, parsed);
931 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700932 }
933
934done:
Tao Baoba9a42a2015-06-23 23:23:33 -0700935 for (int i = 0; i < argc; ++i) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700936 free(args[i]);
937 }
938 free(args);
939
940 if (result != NULL) {
941 return result;
942 }
943
944 if (bad > 0) {
945 return ErrorAbort(state, "%s: some changes failed", name);
946 }
947
948 return StringValue(strdup(""));
949}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700950
Doug Zongker512536a2010-02-17 16:11:44 -0800951Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700952 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700953 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700954 }
Tao Baoba9a42a2015-06-23 23:23:33 -0700955 char* key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700956 if (key == NULL) return NULL;
957
958 char value[PROPERTY_VALUE_MAX];
959 property_get(key, value, "");
960 free(key);
961
Doug Zongker512536a2010-02-17 16:11:44 -0800962 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700963}
964
965
Doug Zongker47cace92009-06-18 10:11:50 -0700966// file_getprop(file, key)
967//
968// interprets 'file' as a getprop-style file (key=value pairs, one
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700969// per line. # comment lines,blank lines, lines without '=' ignored),
970// and returns the value for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800971Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700972 char* result = NULL;
973 char* buffer = NULL;
974 char* filename;
975 char* key;
976 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
977 return NULL;
978 }
979
980 struct stat st;
981 if (stat(filename, &st) < 0) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700982 ErrorAbort(state, "%s: failed to stat \"%s\": %s", name, filename, strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -0700983 goto done;
984 }
985
986#define MAX_FILE_GETPROP_SIZE 65536
987
988 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700989 ErrorAbort(state, "%s too large for %s (max %d)", filename, name, MAX_FILE_GETPROP_SIZE);
Doug Zongker47cace92009-06-18 10:11:50 -0700990 goto done;
991 }
992
Tao Baoba9a42a2015-06-23 23:23:33 -0700993 buffer = reinterpret_cast<char*>(malloc(st.st_size+1));
Doug Zongker47cace92009-06-18 10:11:50 -0700994 if (buffer == NULL) {
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700995 ErrorAbort(state, "%s: failed to alloc %zu bytes", name,
996 static_cast<size_t>(st.st_size+1));
Doug Zongker47cace92009-06-18 10:11:50 -0700997 goto done;
998 }
999
Tao Baoba9a42a2015-06-23 23:23:33 -07001000 FILE* f;
Jed Estepa7b9a462015-12-15 16:04:53 -08001001 f = ota_fopen(filename, "rb");
Doug Zongker47cace92009-06-18 10:11:50 -07001002 if (f == NULL) {
Tao Baoba9a42a2015-06-23 23:23:33 -07001003 ErrorAbort(state, "%s: failed to open %s: %s", name, filename, strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -07001004 goto done;
1005 }
1006
Jed Estepa7b9a462015-12-15 16:04:53 -08001007 if (ota_fread(buffer, 1, st.st_size, f) != static_cast<size_t>(st.st_size)) {
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -07001008 ErrorAbort(state, "%s: failed to read %zu bytes from %s",
1009 name, static_cast<size_t>(st.st_size), filename);
Jed Estepa7b9a462015-12-15 16:04:53 -08001010 ota_fclose(f);
Doug Zongker47cace92009-06-18 10:11:50 -07001011 goto done;
1012 }
1013 buffer[st.st_size] = '\0';
1014
Jed Estepa7b9a462015-12-15 16:04:53 -08001015 ota_fclose(f);
Doug Zongker47cace92009-06-18 10:11:50 -07001016
Tao Baoba9a42a2015-06-23 23:23:33 -07001017 char* line;
1018 line = strtok(buffer, "\n");
Doug Zongker47cace92009-06-18 10:11:50 -07001019 do {
1020 // skip whitespace at start of line
1021 while (*line && isspace(*line)) ++line;
1022
1023 // comment or blank line: skip to next line
1024 if (*line == '\0' || *line == '#') continue;
1025
1026 char* equal = strchr(line, '=');
1027 if (equal == NULL) {
Michael Rungeaa1a31e2014-04-25 18:47:18 -07001028 continue;
Doug Zongker47cace92009-06-18 10:11:50 -07001029 }
1030
1031 // trim whitespace between key and '='
1032 char* key_end = equal-1;
1033 while (key_end > line && isspace(*key_end)) --key_end;
1034 key_end[1] = '\0';
1035
1036 // not the key we're looking for
1037 if (strcmp(key, line) != 0) continue;
1038
1039 // skip whitespace after the '=' to the start of the value
1040 char* val_start = equal+1;
1041 while(*val_start && isspace(*val_start)) ++val_start;
1042
1043 // trim trailing whitespace
1044 char* val_end = val_start + strlen(val_start)-1;
1045 while (val_end > val_start && isspace(*val_end)) --val_end;
1046 val_end[1] = '\0';
1047
1048 result = strdup(val_start);
1049 break;
1050
1051 } while ((line = strtok(NULL, "\n")));
1052
1053 if (result == NULL) result = strdup("");
1054
1055 done:
1056 free(filename);
1057 free(key);
1058 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -08001059 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -07001060}
1061
Doug Zongker179b2d92011-04-12 15:49:04 -07001062// write_raw_image(filename_or_blob, partition)
Doug Zongker512536a2010-02-17 16:11:44 -08001063Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001064 char* result = NULL;
1065
Doug Zongker179b2d92011-04-12 15:49:04 -07001066 Value* partition_value;
1067 Value* contents;
1068 if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001069 return NULL;
1070 }
1071
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001072 char* partition = NULL;
Doug Zongker179b2d92011-04-12 15:49:04 -07001073 if (partition_value->type != VAL_STRING) {
1074 ErrorAbort(state, "partition argument to %s must be string", name);
1075 goto done;
1076 }
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001077 partition = partition_value->data;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001078 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001079 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001080 goto done;
1081 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001082 if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001083 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001084 goto done;
1085 }
1086
1087 mtd_scan_partitions();
Tao Baoba9a42a2015-06-23 23:23:33 -07001088 const MtdPartition* mtd;
1089 mtd = mtd_find_partition_by_name(partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001090 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001091 printf("%s: no mtd partition named \"%s\"\n", name, partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001092 result = strdup("");
1093 goto done;
1094 }
1095
Tao Baoba9a42a2015-06-23 23:23:33 -07001096 MtdWriteContext* ctx;
1097 ctx = mtd_write_partition(mtd);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001098 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001099 printf("%s: can't write mtd partition \"%s\"\n",
Doug Zongker8edb00c2009-06-11 17:21:44 -07001100 name, partition);
1101 result = strdup("");
1102 goto done;
1103 }
1104
1105 bool success;
1106
Doug Zongker179b2d92011-04-12 15:49:04 -07001107 if (contents->type == VAL_STRING) {
1108 // we're given a filename as the contents
1109 char* filename = contents->data;
Jed Estepa7b9a462015-12-15 16:04:53 -08001110 FILE* f = ota_fopen(filename, "rb");
Doug Zongker179b2d92011-04-12 15:49:04 -07001111 if (f == NULL) {
Tao Baoba9a42a2015-06-23 23:23:33 -07001112 printf("%s: can't open %s: %s\n", name, filename, strerror(errno));
Doug Zongker179b2d92011-04-12 15:49:04 -07001113 result = strdup("");
1114 goto done;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001115 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001116
1117 success = true;
Tao Baoba9a42a2015-06-23 23:23:33 -07001118 char* buffer = reinterpret_cast<char*>(malloc(BUFSIZ));
Doug Zongker179b2d92011-04-12 15:49:04 -07001119 int read;
Jed Estepa7b9a462015-12-15 16:04:53 -08001120 while (success && (read = ota_fread(buffer, 1, BUFSIZ, f)) > 0) {
Doug Zongker179b2d92011-04-12 15:49:04 -07001121 int wrote = mtd_write_data(ctx, buffer, read);
1122 success = success && (wrote == read);
1123 }
1124 free(buffer);
Jed Estepa7b9a462015-12-15 16:04:53 -08001125 ota_fclose(f);
Doug Zongker179b2d92011-04-12 15:49:04 -07001126 } else {
1127 // we're given a blob as the contents
1128 ssize_t wrote = mtd_write_data(ctx, contents->data, contents->size);
1129 success = (wrote == contents->size);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001130 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001131 if (!success) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001132 printf("mtd_write_data to %s failed: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -07001133 partition, strerror(errno));
1134 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001135
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001136 if (mtd_erase_blocks(ctx, -1) == -1) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001137 printf("%s: error erasing blocks of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001138 }
1139 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001140 printf("%s: error closing write of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001141 }
1142
Doug Zongker179b2d92011-04-12 15:49:04 -07001143 printf("%s %s partition\n",
1144 success ? "wrote" : "failed to write", partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001145
1146 result = success ? partition : strdup("");
1147
1148done:
Doug Zongker179b2d92011-04-12 15:49:04 -07001149 if (result != partition) FreeValue(partition_value);
1150 FreeValue(contents);
Doug Zongker512536a2010-02-17 16:11:44 -08001151 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001152}
1153
Doug Zongker8edb00c2009-06-11 17:21:44 -07001154// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001155Value* ApplyPatchSpaceFn(const char* name, State* state,
1156 int argc, Expr* argv[]) {
1157 char* bytes_str;
1158 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
1159 return NULL;
1160 }
1161
Tao Baob15fd222015-09-24 11:10:51 -07001162 size_t bytes;
1163 if (!android::base::ParseUint(bytes_str, &bytes)) {
Tao Baoba9a42a2015-06-23 23:23:33 -07001164 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n", name, bytes_str);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001165 free(bytes_str);
Tao Baob15fd222015-09-24 11:10:51 -07001166 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001167 }
1168
1169 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
1170}
1171
Doug Zongker52b40362014-02-10 15:30:30 -08001172// apply_patch(file, size, init_sha1, tgt_sha1, patch)
1173
Doug Zongker512536a2010-02-17 16:11:44 -08001174Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001175 if (argc < 6 || (argc % 2) == 1) {
1176 return ErrorAbort(state, "%s(): expected at least 6 args and an "
1177 "even number, got %d",
1178 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001179 }
1180
Doug Zongkerc4351c72010-02-22 14:46:32 -08001181 char* source_filename;
1182 char* target_filename;
1183 char* target_sha1;
1184 char* target_size_str;
1185 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
1186 &target_sha1, &target_size_str) < 0) {
1187 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001188 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001189
Tao Baob15fd222015-09-24 11:10:51 -07001190 size_t target_size;
1191 if (!android::base::ParseUint(target_size_str, &target_size)) {
1192 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count", name, target_size_str);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001193 free(source_filename);
1194 free(target_filename);
1195 free(target_sha1);
1196 free(target_size_str);
Tao Baob15fd222015-09-24 11:10:51 -07001197 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001198 }
1199
1200 int patchcount = (argc-4) / 2;
Yabin Cui64be2132016-02-03 18:16:02 -08001201 std::unique_ptr<Value*, decltype(&free)> arg_values(ReadValueVarArgs(state, argc-4, argv+4),
1202 free);
1203 if (!arg_values) {
1204 return nullptr;
1205 }
1206 std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> patch_shas;
1207 std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> patches;
1208 // Protect values by unique_ptrs first to get rid of memory leak.
1209 for (int i = 0; i < patchcount * 2; i += 2) {
1210 patch_shas.emplace_back(arg_values.get()[i], FreeValue);
1211 patches.emplace_back(arg_values.get()[i+1], FreeValue);
1212 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001213
Yabin Cui64be2132016-02-03 18:16:02 -08001214 for (int i = 0; i < patchcount; ++i) {
1215 if (patch_shas[i]->type != VAL_STRING) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001216 ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i);
Yabin Cui64be2132016-02-03 18:16:02 -08001217 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001218 }
Yabin Cui64be2132016-02-03 18:16:02 -08001219 if (patches[i]->type != VAL_BLOB) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001220 ErrorAbort(state, "%s(): patch #%d is not blob", name, i);
Yabin Cui64be2132016-02-03 18:16:02 -08001221 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001222 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001223 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001224
Yabin Cui64be2132016-02-03 18:16:02 -08001225 std::vector<char*> patch_sha_str;
1226 std::vector<Value*> patch_ptrs;
1227 for (int i = 0; i < patchcount; ++i) {
1228 patch_sha_str.push_back(patch_shas[i]->data);
1229 patch_ptrs.push_back(patches[i].get());
Doug Zongker8edb00c2009-06-11 17:21:44 -07001230 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001231
1232 int result = applypatch(source_filename, target_filename,
1233 target_sha1, target_size,
Yabin Cui64be2132016-02-03 18:16:02 -08001234 patchcount, patch_sha_str.data(), patch_ptrs.data(), NULL);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001235
1236 return StringValue(strdup(result == 0 ? "t" : ""));
1237}
1238
1239// apply_patch_check(file, [sha1_1, ...])
1240Value* ApplyPatchCheckFn(const char* name, State* state,
1241 int argc, Expr* argv[]) {
1242 if (argc < 1) {
1243 return ErrorAbort(state, "%s(): expected at least 1 arg, got %d",
1244 name, argc);
1245 }
1246
1247 char* filename;
1248 if (ReadArgs(state, argv, 1, &filename) < 0) {
1249 return NULL;
1250 }
1251
1252 int patchcount = argc-1;
1253 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
1254
1255 int result = applypatch_check(filename, patchcount, sha1s);
1256
1257 int i;
1258 for (i = 0; i < patchcount; ++i) {
1259 free(sha1s[i]);
1260 }
1261 free(sha1s);
1262
1263 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001264}
1265
Tao Bao1107d962015-09-09 17:16:55 -07001266// This is the updater side handler for ui_print() in edify script. Contents
1267// will be sent over to the recovery side for on-screen display.
Doug Zongker512536a2010-02-17 16:11:44 -08001268Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001269 char** args = ReadVarArgs(state, argc, argv);
1270 if (args == NULL) {
1271 return NULL;
1272 }
1273
Tao Bao1107d962015-09-09 17:16:55 -07001274 std::string buffer;
1275 for (int i = 0; i < argc; ++i) {
1276 buffer += args[i];
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001277 free(args[i]);
1278 }
1279 free(args);
Tao Bao1107d962015-09-09 17:16:55 -07001280
1281 buffer += "\n";
Michael Runged4a63422014-10-22 19:48:41 -07001282 uiPrint(state, buffer);
Tao Bao1107d962015-09-09 17:16:55 -07001283 return StringValue(strdup(buffer.c_str()));
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001284}
1285
Doug Zongkerd0181b82011-10-19 10:51:12 -07001286Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
1287 if (argc != 0) {
1288 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1289 }
1290 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
1291 return StringValue(strdup("t"));
1292}
1293
Doug Zongker512536a2010-02-17 16:11:44 -08001294Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001295 if (argc < 1) {
1296 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1297 }
1298 char** args = ReadVarArgs(state, argc, argv);
1299 if (args == NULL) {
1300 return NULL;
1301 }
1302
Tao Baoba9a42a2015-06-23 23:23:33 -07001303 char** args2 = reinterpret_cast<char**>(malloc(sizeof(char*) * (argc+1)));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001304 memcpy(args2, args, sizeof(char*) * argc);
1305 args2[argc] = NULL;
1306
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001307 printf("about to run program [%s] with %d args\n", args2[0], argc);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001308
1309 pid_t child = fork();
1310 if (child == 0) {
1311 execv(args2[0], args2);
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001312 printf("run_program: execv failed: %s\n", strerror(errno));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001313 _exit(1);
1314 }
1315 int status;
1316 waitpid(child, &status, 0);
1317 if (WIFEXITED(status)) {
1318 if (WEXITSTATUS(status) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001319 printf("run_program: child exited with status %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001320 WEXITSTATUS(status));
1321 }
1322 } else if (WIFSIGNALED(status)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001323 printf("run_program: child terminated by signal %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001324 WTERMSIG(status));
1325 }
1326
1327 int i;
1328 for (i = 0; i < argc; ++i) {
1329 free(args[i]);
1330 }
1331 free(args);
1332 free(args2);
1333
1334 char buffer[20];
1335 sprintf(buffer, "%d", status);
1336
Doug Zongker512536a2010-02-17 16:11:44 -08001337 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001338}
1339
Doug Zongker512536a2010-02-17 16:11:44 -08001340// sha1_check(data)
1341// to return the sha1 of the data (given in the format returned by
1342// read_file).
1343//
1344// sha1_check(data, sha1_hex, [sha1_hex, ...])
1345// returns the sha1 of the file if it matches any of the hex
1346// strings passed, or "" if it does not equal any of them.
1347//
1348Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1349 if (argc < 1) {
1350 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1351 }
1352
Yabin Cui64be2132016-02-03 18:16:02 -08001353 std::unique_ptr<Value*, decltype(&free)> arg_values(ReadValueVarArgs(state, argc, argv), free);
1354 if (arg_values == nullptr) {
1355 return nullptr;
1356 }
1357 std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> args;
1358 for (int i = 0; i < argc; ++i) {
1359 args.emplace_back(arg_values.get()[i], FreeValue);
Doug Zongker512536a2010-02-17 16:11:44 -08001360 }
1361
1362 if (args[0]->size < 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001363 return StringValue(strdup(""));
1364 }
Sen Jiangc48cb5e2016-02-04 16:23:21 +08001365 uint8_t digest[SHA_DIGEST_LENGTH];
1366 SHA1(reinterpret_cast<uint8_t*>(args[0]->data), args[0]->size, digest);
Doug Zongker512536a2010-02-17 16:11:44 -08001367
1368 if (argc == 1) {
1369 return StringValue(PrintSha1(digest));
1370 }
1371
1372 int i;
Yabin Cui64be2132016-02-03 18:16:02 -08001373 uint8_t arg_digest[SHA_DIGEST_LENGTH];
Doug Zongker512536a2010-02-17 16:11:44 -08001374 for (i = 1; i < argc; ++i) {
1375 if (args[i]->type != VAL_STRING) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001376 printf("%s(): arg %d is not a string; skipping",
Doug Zongker512536a2010-02-17 16:11:44 -08001377 name, i);
1378 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1379 // Warn about bad args and skip them.
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001380 printf("%s(): error parsing \"%s\" as sha-1; skipping",
1381 name, args[i]->data);
Sen Jiangc48cb5e2016-02-04 16:23:21 +08001382 } else if (memcmp(digest, arg_digest, SHA_DIGEST_LENGTH) == 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001383 break;
1384 }
Doug Zongker512536a2010-02-17 16:11:44 -08001385 }
1386 if (i >= argc) {
1387 // Didn't match any of the hex strings; return false.
1388 return StringValue(strdup(""));
1389 }
Yabin Cui64be2132016-02-03 18:16:02 -08001390 // Found a match.
1391 return args[i].release();
Doug Zongker512536a2010-02-17 16:11:44 -08001392}
1393
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001394// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001395// is actually a FileContents*).
1396Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1397 if (argc != 1) {
1398 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1399 }
1400 char* filename;
1401 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1402
Yabin Cuid6c93af2016-02-10 16:41:10 -08001403 Value* v = static_cast<Value*>(malloc(sizeof(Value)));
1404 if (v == nullptr) {
1405 return nullptr;
1406 }
Doug Zongker512536a2010-02-17 16:11:44 -08001407 v->type = VAL_BLOB;
Yabin Cuid6c93af2016-02-10 16:41:10 -08001408 v->size = -1;
1409 v->data = nullptr;
Doug Zongker512536a2010-02-17 16:11:44 -08001410
1411 FileContents fc;
Doug Zongkera1bc1482014-02-13 15:18:19 -08001412 if (LoadFileContents(filename, &fc) != 0) {
Yabin Cuid6c93af2016-02-10 16:41:10 -08001413 v->data = static_cast<char*>(malloc(fc.data.size()));
1414 if (v->data != nullptr) {
1415 memcpy(v->data, fc.data.data(), fc.data.size());
1416 v->size = fc.data.size();
1417 }
Doug Zongker512536a2010-02-17 16:11:44 -08001418 }
Doug Zongker512536a2010-02-17 16:11:44 -08001419 free(filename);
1420 return v;
1421}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001422
Doug Zongkerc87bab12013-11-25 13:53:25 -08001423// Immediately reboot the device. Recovery is not finished normally,
1424// so if you reboot into recovery it will re-start applying the
1425// current package (because nothing has cleared the copy of the
1426// arguments stored in the BCB).
1427//
1428// The argument is the partition name passed to the android reboot
1429// property. It can be "recovery" to boot from the recovery
1430// partition, or "" (empty string) to boot from the regular boot
1431// partition.
1432Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) {
1433 if (argc != 2) {
1434 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1435 }
1436
1437 char* filename;
1438 char* property;
1439 if (ReadArgs(state, argv, 2, &filename, &property) < 0) return NULL;
1440
1441 char buffer[80];
1442
1443 // zero out the 'command' field of the bootloader message.
1444 memset(buffer, 0, sizeof(((struct bootloader_message*)0)->command));
Jed Estepa7b9a462015-12-15 16:04:53 -08001445 FILE* f = ota_fopen(filename, "r+b");
Doug Zongkerc87bab12013-11-25 13:53:25 -08001446 fseek(f, offsetof(struct bootloader_message, command), SEEK_SET);
Jed Estepa7b9a462015-12-15 16:04:53 -08001447 ota_fwrite(buffer, sizeof(((struct bootloader_message*)0)->command), 1, f);
1448 ota_fclose(f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001449 free(filename);
1450
1451 strcpy(buffer, "reboot,");
1452 if (property != NULL) {
1453 strncat(buffer, property, sizeof(buffer)-10);
1454 }
1455
1456 property_set(ANDROID_RB_PROPERTY, buffer);
1457
1458 sleep(5);
1459 free(property);
1460 ErrorAbort(state, "%s() failed to reboot", name);
1461 return NULL;
1462}
1463
1464// Store a string value somewhere that future invocations of recovery
1465// can access it. This value is called the "stage" and can be used to
1466// drive packages that need to do reboots in the middle of
1467// installation and keep track of where they are in the multi-stage
1468// install.
1469//
1470// The first argument is the block device for the misc partition
1471// ("/misc" in the fstab), which is where this value is stored. The
1472// second argument is the string to store; it should not exceed 31
1473// bytes.
1474Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
1475 if (argc != 2) {
1476 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1477 }
1478
1479 char* filename;
1480 char* stagestr;
1481 if (ReadArgs(state, argv, 2, &filename, &stagestr) < 0) return NULL;
1482
1483 // Store this value in the misc partition, immediately after the
1484 // bootloader message that the main recovery uses to save its
1485 // arguments in case of the device restarting midway through
1486 // package installation.
Jed Estepa7b9a462015-12-15 16:04:53 -08001487 FILE* f = ota_fopen(filename, "r+b");
Doug Zongkerc87bab12013-11-25 13:53:25 -08001488 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1489 int to_write = strlen(stagestr)+1;
1490 int max_size = sizeof(((struct bootloader_message*)0)->stage);
1491 if (to_write > max_size) {
1492 to_write = max_size;
1493 stagestr[max_size-1] = 0;
1494 }
Jed Estepa7b9a462015-12-15 16:04:53 -08001495 ota_fwrite(stagestr, to_write, 1, f);
1496 ota_fclose(f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001497
1498 free(stagestr);
1499 return StringValue(filename);
1500}
1501
1502// Return the value most recently saved with SetStageFn. The argument
1503// is the block device for the misc partition.
1504Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001505 if (argc != 1) {
Doug Zongkerc87bab12013-11-25 13:53:25 -08001506 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1507 }
1508
1509 char* filename;
1510 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1511
1512 char buffer[sizeof(((struct bootloader_message*)0)->stage)];
Jed Estepa7b9a462015-12-15 16:04:53 -08001513 FILE* f = ota_fopen(filename, "rb");
Doug Zongkerc87bab12013-11-25 13:53:25 -08001514 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
Jed Estepa7b9a462015-12-15 16:04:53 -08001515 ota_fread(buffer, sizeof(buffer), 1, f);
1516 ota_fclose(f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001517 buffer[sizeof(buffer)-1] = '\0';
1518
1519 return StringValue(strdup(buffer));
1520}
1521
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001522Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) {
1523 if (argc != 2) {
1524 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1525 }
1526
1527 char* filename;
1528 char* len_str;
1529 if (ReadArgs(state, argv, 2, &filename, &len_str) < 0) return NULL;
1530
Tao Baob15fd222015-09-24 11:10:51 -07001531 size_t len;
1532 android::base::ParseUint(len_str, &len);
Jed Estepa7b9a462015-12-15 16:04:53 -08001533 int fd = ota_open(filename, O_WRONLY, 0644);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001534 int success = wipe_block_device(fd, len);
1535
1536 free(filename);
1537 free(len_str);
1538
Jed Estepa7b9a462015-12-15 16:04:53 -08001539 ota_close(fd);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001540
1541 return StringValue(strdup(success ? "t" : ""));
1542}
1543
Doug Zongkerc704e062014-05-23 08:40:35 -07001544Value* EnableRebootFn(const char* name, State* state, int argc, Expr* argv[]) {
1545 if (argc != 0) {
1546 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1547 }
1548 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
1549 fprintf(ui->cmd_pipe, "enable_reboot\n");
1550 return StringValue(strdup("t"));
1551}
1552
Michael Rungeacf47db2014-11-21 00:12:28 -08001553Value* Tune2FsFn(const char* name, State* state, int argc, Expr* argv[]) {
1554 if (argc == 0) {
1555 return ErrorAbort(state, "%s() expects args, got %d", name, argc);
1556 }
1557
1558 char** args = ReadVarArgs(state, argc, argv);
1559 if (args == NULL) {
1560 return ErrorAbort(state, "%s() could not read args", name);
1561 }
1562
Tao Baoba9a42a2015-06-23 23:23:33 -07001563 char** args2 = reinterpret_cast<char**>(malloc(sizeof(char*) * (argc+1)));
Michael Rungeacf47db2014-11-21 00:12:28 -08001564 // Tune2fs expects the program name as its args[0]
1565 args2[0] = strdup(name);
Tao Baoba9a42a2015-06-23 23:23:33 -07001566 for (int i = 0; i < argc; ++i) {
Michael Rungeacf47db2014-11-21 00:12:28 -08001567 args2[i + 1] = args[i];
1568 }
1569 int result = tune2fs_main(argc + 1, args2);
Tao Baoba9a42a2015-06-23 23:23:33 -07001570 for (int i = 0; i < argc; ++i) {
Michael Rungeacf47db2014-11-21 00:12:28 -08001571 free(args[i]);
1572 }
1573 free(args);
1574
1575 free(args2[0]);
1576 free(args2);
1577 if (result != 0) {
1578 return ErrorAbort(state, "%s() returned error code %d", name, result);
1579 }
1580 return StringValue(strdup("t"));
1581}
1582
Doug Zongker9931f7f2009-06-10 14:11:53 -07001583void RegisterInstallFunctions() {
1584 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001585 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001586 RegisterFunction("unmount", UnmountFn);
1587 RegisterFunction("format", FormatFn);
1588 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001589 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001590 RegisterFunction("delete", DeleteFn);
1591 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001592 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1593 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001594 RegisterFunction("symlink", SymlinkFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001595
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001596 // Usage:
1597 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1598 // Example:
1599 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1600 RegisterFunction("set_metadata", SetMetadataFn);
1601
1602 // Usage:
1603 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1604 // Example:
1605 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1606 RegisterFunction("set_metadata_recursive", SetMetadataFn);
1607
Doug Zongker8edb00c2009-06-11 17:21:44 -07001608 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001609 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001610 RegisterFunction("write_raw_image", WriteRawImageFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001611
1612 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001613 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1614 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001615
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001616 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001617
Doug Zongker512536a2010-02-17 16:11:44 -08001618 RegisterFunction("read_file", ReadFileFn);
1619 RegisterFunction("sha1_check", Sha1CheckFn);
Michael Rungece7ca712013-11-06 17:42:20 -08001620 RegisterFunction("rename", RenameFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001621
Doug Zongkerd0181b82011-10-19 10:51:12 -07001622 RegisterFunction("wipe_cache", WipeCacheFn);
1623
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001624 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001625
1626 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001627
1628 RegisterFunction("reboot_now", RebootNowFn);
1629 RegisterFunction("get_stage", GetStageFn);
1630 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -07001631
1632 RegisterFunction("enable_reboot", EnableRebootFn);
Michael Rungeacf47db2014-11-21 00:12:28 -08001633 RegisterFunction("tune2fs", Tune2FsFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001634}