blob: 97e39056081c196f3a53ee5638abf6d2810db0aa [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 <selinux/selinux.h>
31#include <ftw.h>
32#include <sys/capability.h>
33#include <sys/xattr.h>
34#include <linux/xattr.h>
35#include <inttypes.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070036
Tao Baob15fd222015-09-24 11:10:51 -070037#include <base/parseint.h>
Tao Bao1107d962015-09-09 17:16:55 -070038#include <base/strings.h>
39#include <base/stringprintf.h>
40
Doug Zongkerc87bab12013-11-25 13:53:25 -080041#include "bootloader.h"
42#include "applypatch/applypatch.h"
43#include "cutils/android_reboot.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070044#include "cutils/misc.h"
45#include "cutils/properties.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070046#include "edify/expr.h"
Doug Zongker512536a2010-02-17 16:11:44 -080047#include "mincrypt/sha.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070048#include "minzip/DirUtil.h"
49#include "mtdutils/mounts.h"
50#include "mtdutils/mtdutils.h"
51#include "updater.h"
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -080052#include "install.h"
Michael Rungeacf47db2014-11-21 00:12:28 -080053#include "tune2fs.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070054
Doug Zongker3d177d02010-07-01 09:18:44 -070055#ifdef USE_EXT4
56#include "make_ext4fs.h"
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -080057#include "wipe.h"
Doug Zongker3d177d02010-07-01 09:18:44 -070058#endif
59
Tao Bao1107d962015-09-09 17:16:55 -070060// Send over the buffer to recovery though the command pipe.
61static void uiPrint(State* state, const std::string& buffer) {
62 UpdaterInfo* ui = reinterpret_cast<UpdaterInfo*>(state->cookie);
Tao Baob6918c72015-05-19 17:02:16 -070063
Tao Bao1107d962015-09-09 17:16:55 -070064 // "line1\nline2\n" will be split into 3 tokens: "line1", "line2" and "".
65 // So skip sending empty strings to UI.
66 std::vector<std::string> lines = android::base::Split(buffer, "\n");
67 for (auto& line: lines) {
68 if (!line.empty()) {
69 fprintf(ui->cmd_pipe, "ui_print %s\n", line.c_str());
70 fprintf(ui->cmd_pipe, "ui_print\n");
71 }
72 }
73
74 // On the updater side, we need to dump the contents to stderr (which has
75 // been redirected to the log file). Because the recovery will only print
76 // the contents to screen when processing pipe command ui_print.
77 fprintf(stderr, "%s", buffer.c_str());
Michael Runged4a63422014-10-22 19:48:41 -070078}
79
80__attribute__((__format__(printf, 2, 3))) __nonnull((2))
81void uiPrintf(State* state, const char* format, ...) {
Tao Bao1107d962015-09-09 17:16:55 -070082 std::string error_msg;
83
Michael Runged4a63422014-10-22 19:48:41 -070084 va_list ap;
85 va_start(ap, format);
Tao Bao1107d962015-09-09 17:16:55 -070086 android::base::StringAppendV(&error_msg, format, ap);
Michael Runged4a63422014-10-22 19:48:41 -070087 va_end(ap);
Tao Bao1107d962015-09-09 17:16:55 -070088
Michael Runged4a63422014-10-22 19:48:41 -070089 uiPrint(state, error_msg);
90}
91
Doug Zongker52b40362014-02-10 15:30:30 -080092// Take a sha-1 digest and return it as a newly-allocated hex string.
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070093char* PrintSha1(const uint8_t* digest) {
Tao Baoba9a42a2015-06-23 23:23:33 -070094 char* buffer = reinterpret_cast<char*>(malloc(SHA_DIGEST_SIZE*2 + 1));
Doug Zongker52b40362014-02-10 15:30:30 -080095 const char* alphabet = "0123456789abcdef";
Tao Baoba9a42a2015-06-23 23:23:33 -070096 size_t i;
Doug Zongker52b40362014-02-10 15:30:30 -080097 for (i = 0; i < SHA_DIGEST_SIZE; ++i) {
98 buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf];
99 buffer[i*2+1] = alphabet[digest[i] & 0xf];
100 }
101 buffer[i*2] = '\0';
102 return buffer;
103}
104
Doug Zongker3d177d02010-07-01 09:18:44 -0700105// mount(fs_type, partition_type, location, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700106//
Doug Zongker3d177d02010-07-01 09:18:44 -0700107// fs_type="yaffs2" partition_type="MTD" location=partition
108// fs_type="ext4" partition_type="EMMC" location=device
Doug Zongker512536a2010-02-17 16:11:44 -0800109Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700110 char* result = NULL;
Michael Runge168f7772014-10-22 17:05:08 -0700111 if (argc != 4 && argc != 5) {
112 return ErrorAbort(state, "%s() expects 4-5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700113 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700114 char* fs_type;
115 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700116 char* location;
117 char* mount_point;
Michael Runge168f7772014-10-22 17:05:08 -0700118 char* mount_options;
119 bool has_mount_options;
120 if (argc == 5) {
121 has_mount_options = true;
122 if (ReadArgs(state, argv, 5, &fs_type, &partition_type,
123 &location, &mount_point, &mount_options) < 0) {
124 return NULL;
125 }
126 } else {
127 has_mount_options = false;
128 if (ReadArgs(state, argv, 4, &fs_type, &partition_type,
Doug Zongker3d177d02010-07-01 09:18:44 -0700129 &location, &mount_point) < 0) {
Michael Runge168f7772014-10-22 17:05:08 -0700130 return NULL;
131 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700132 }
133
Doug Zongker3d177d02010-07-01 09:18:44 -0700134 if (strlen(fs_type) == 0) {
135 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
136 goto done;
137 }
138 if (strlen(partition_type) == 0) {
139 ErrorAbort(state, "partition_type argument to %s() can't be empty",
140 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700141 goto done;
142 }
143 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700144 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700145 goto done;
146 }
147 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700148 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700149 goto done;
150 }
151
Tao Baoba9a42a2015-06-23 23:23:33 -0700152 {
153 char *secontext = NULL;
Stephen Smalley779701d2012-02-09 14:13:23 -0500154
Tao Baoba9a42a2015-06-23 23:23:33 -0700155 if (sehandle) {
156 selabel_lookup(sehandle, &secontext, mount_point, 0755);
157 setfscreatecon(secontext);
158 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500159
Tao Baoba9a42a2015-06-23 23:23:33 -0700160 mkdir(mount_point, 0755);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700161
Tao Baoba9a42a2015-06-23 23:23:33 -0700162 if (secontext) {
163 freecon(secontext);
164 setfscreatecon(NULL);
165 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500166 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500167
Doug Zongker3d177d02010-07-01 09:18:44 -0700168 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700169 mtd_scan_partitions();
170 const MtdPartition* mtd;
171 mtd = mtd_find_partition_by_name(location);
172 if (mtd == NULL) {
Tao Bao1107d962015-09-09 17:16:55 -0700173 uiPrintf(state, "%s: no mtd partition named \"%s\"\n",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700174 name, location);
175 result = strdup("");
176 goto done;
177 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700178 if (mtd_mount_partition(mtd, mount_point, fs_type, 0 /* rw */) != 0) {
Michael Runge5ddf4292014-10-24 14:14:41 -0700179 uiPrintf(state, "mtd mount of %s failed: %s\n",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700180 location, strerror(errno));
181 result = strdup("");
182 goto done;
183 }
184 result = mount_point;
185 } else {
Doug Zongker3d177d02010-07-01 09:18:44 -0700186 if (mount(location, mount_point, fs_type,
Michael Runge168f7772014-10-22 17:05:08 -0700187 MS_NOATIME | MS_NODEV | MS_NODIRATIME,
188 has_mount_options ? mount_options : "") < 0) {
Michael Runge5ddf4292014-10-24 14:14:41 -0700189 uiPrintf(state, "%s: failed to mount %s at %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700190 name, location, mount_point, strerror(errno));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700191 result = strdup("");
192 } else {
193 result = mount_point;
194 }
195 }
196
197done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700198 free(fs_type);
199 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700200 free(location);
201 if (result != mount_point) free(mount_point);
Michael Runge168f7772014-10-22 17:05:08 -0700202 if (has_mount_options) free(mount_options);
Doug Zongker512536a2010-02-17 16:11:44 -0800203 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700204}
205
Doug Zongker8edb00c2009-06-11 17:21:44 -0700206
207// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800208Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700209 char* result = NULL;
210 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700211 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700212 }
213 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700214 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700215 return NULL;
216 }
217 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700218 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700219 goto done;
220 }
221
222 scan_mounted_volumes();
Tao Baoba9a42a2015-06-23 23:23:33 -0700223 {
224 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
225 if (vol == NULL) {
226 result = strdup("");
227 } else {
228 result = mount_point;
229 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700230 }
231
232done:
233 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800234 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700235}
236
237
Doug Zongker512536a2010-02-17 16:11:44 -0800238Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700239 char* result = NULL;
240 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700241 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700242 }
243 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700244 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700245 return NULL;
246 }
247 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700248 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700249 goto done;
250 }
251
252 scan_mounted_volumes();
Tao Baoba9a42a2015-06-23 23:23:33 -0700253 {
254 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
255 if (vol == NULL) {
256 uiPrintf(state, "unmount of %s failed; no such volume\n", mount_point);
257 result = strdup("");
258 } else {
259 int ret = unmount_mounted_volume(vol);
260 if (ret != 0) {
261 uiPrintf(state, "unmount of %s failed (%d): %s\n",
262 mount_point, ret, strerror(errno));
263 }
264 result = mount_point;
Michael Runge5ddf4292014-10-24 14:14:41 -0700265 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700266 }
267
268done:
269 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800270 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700271}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700272
JP Abgrall37aedb32014-06-16 19:07:39 -0700273static int exec_cmd(const char* path, char* const argv[]) {
274 int status;
275 pid_t child;
276 if ((child = vfork()) == 0) {
277 execv(path, argv);
278 _exit(-1);
279 }
280 waitpid(child, &status, 0);
281 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
282 printf("%s failed with status %d\n", path, WEXITSTATUS(status));
283 }
284 return WEXITSTATUS(status);
285}
286
Doug Zongker8edb00c2009-06-11 17:21:44 -0700287
Stephen Smalley779701d2012-02-09 14:13:23 -0500288// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700289//
Stephen Smalley779701d2012-02-09 14:13:23 -0500290// fs_type="yaffs2" partition_type="MTD" location=partition fs_size=<bytes> mount_point=<location>
291// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700292// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
293// if fs_size == 0, then make fs uses the entire partition.
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800294// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700295// 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 -0800296Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700297 char* result = NULL;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400298 if (argc != 5) {
299 return ErrorAbort(state, "%s() expects 5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700300 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700301 char* fs_type;
302 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700303 char* location;
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800304 char* fs_size;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400305 char* mount_point;
Stephen Smalley779701d2012-02-09 14:13:23 -0500306
Stephen Smalley779701d2012-02-09 14:13:23 -0500307 if (ReadArgs(state, argv, 5, &fs_type, &partition_type, &location, &fs_size, &mount_point) < 0) {
308 return NULL;
309 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700310
Doug Zongker3d177d02010-07-01 09:18:44 -0700311 if (strlen(fs_type) == 0) {
312 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
313 goto done;
314 }
315 if (strlen(partition_type) == 0) {
316 ErrorAbort(state, "partition_type argument to %s() can't be empty",
317 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700318 goto done;
319 }
320 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700321 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700322 goto done;
323 }
324
Stephen Smalley516e4e22012-04-03 13:35:11 -0400325 if (strlen(mount_point) == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500326 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
327 goto done;
328 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500329
Doug Zongker3d177d02010-07-01 09:18:44 -0700330 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700331 mtd_scan_partitions();
332 const MtdPartition* mtd = mtd_find_partition_by_name(location);
333 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700334 printf("%s: no mtd partition named \"%s\"",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700335 name, location);
336 result = strdup("");
337 goto done;
338 }
339 MtdWriteContext* ctx = mtd_write_partition(mtd);
340 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700341 printf("%s: can't write \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700342 result = strdup("");
343 goto done;
344 }
345 if (mtd_erase_blocks(ctx, -1) == -1) {
346 mtd_write_close(ctx);
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700347 printf("%s: failed to erase \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700348 result = strdup("");
349 goto done;
350 }
351 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700352 printf("%s: failed to close \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700353 result = strdup("");
354 goto done;
355 }
356 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700357#ifdef USE_EXT4
358 } else if (strcmp(fs_type, "ext4") == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500359 int status = make_ext4fs(location, atoll(fs_size), mount_point, sehandle);
Doug Zongker3d177d02010-07-01 09:18:44 -0700360 if (status != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700361 printf("%s: make_ext4fs failed (%d) on %s",
Doug Zongker3d177d02010-07-01 09:18:44 -0700362 name, status, location);
363 result = strdup("");
364 goto done;
365 }
366 result = location;
JP Abgrall37aedb32014-06-16 19:07:39 -0700367 } else if (strcmp(fs_type, "f2fs") == 0) {
368 char *num_sectors;
369 if (asprintf(&num_sectors, "%lld", atoll(fs_size) / 512) <= 0) {
370 printf("format_volume: failed to create %s command for %s\n", fs_type, location);
371 result = strdup("");
372 goto done;
373 }
374 const char *f2fs_path = "/sbin/mkfs.f2fs";
375 const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", location, num_sectors, NULL};
376 int status = exec_cmd(f2fs_path, (char* const*)f2fs_argv);
377 free(num_sectors);
378 if (status != 0) {
379 printf("%s: mkfs.f2fs failed (%d) on %s",
380 name, status, location);
381 result = strdup("");
382 goto done;
383 }
384 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700385#endif
Doug Zongker9931f7f2009-06-10 14:11:53 -0700386 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700387 printf("%s: unsupported fs_type \"%s\" partition_type \"%s\"",
Doug Zongker3d177d02010-07-01 09:18:44 -0700388 name, fs_type, partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700389 }
390
391done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700392 free(fs_type);
393 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700394 if (result != location) free(location);
Doug Zongker512536a2010-02-17 16:11:44 -0800395 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700396}
397
Michael Rungece7ca712013-11-06 17:42:20 -0800398Value* RenameFn(const char* name, State* state, int argc, Expr* argv[]) {
399 char* result = NULL;
400 if (argc != 2) {
401 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
402 }
403
404 char* src_name;
405 char* dst_name;
406
407 if (ReadArgs(state, argv, 2, &src_name, &dst_name) < 0) {
408 return NULL;
409 }
410 if (strlen(src_name) == 0) {
411 ErrorAbort(state, "src_name argument to %s() can't be empty", name);
412 goto done;
413 }
414 if (strlen(dst_name) == 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700415 ErrorAbort(state, "dst_name argument to %s() can't be empty", name);
Michael Rungece7ca712013-11-06 17:42:20 -0800416 goto done;
417 }
Michael Rungea91ecc52014-07-21 17:40:02 -0700418 if (make_parents(dst_name) != 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700419 ErrorAbort(state, "Creating parent of %s failed, error %s",
Michael Rungea91ecc52014-07-21 17:40:02 -0700420 dst_name, strerror(errno));
Michael Runge2f0ef732014-10-22 14:28:23 -0700421 } else if (access(dst_name, F_OK) == 0 && access(src_name, F_OK) != 0) {
422 // File was already moved
423 result = dst_name;
Michael Rungea91ecc52014-07-21 17:40:02 -0700424 } else if (rename(src_name, dst_name) != 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700425 ErrorAbort(state, "Rename of %s to %s failed, error %s",
Michael Rungece7ca712013-11-06 17:42:20 -0800426 src_name, dst_name, strerror(errno));
427 } else {
428 result = dst_name;
429 }
430
431done:
432 free(src_name);
433 if (result != dst_name) free(dst_name);
434 return StringValue(result);
435}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700436
Doug Zongker512536a2010-02-17 16:11:44 -0800437Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700438 char** paths = reinterpret_cast<char**>(malloc(argc * sizeof(char*)));
439 for (int i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700440 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700441 if (paths[i] == NULL) {
442 int j;
443 for (j = 0; j < i; ++i) {
444 free(paths[j]);
445 }
446 free(paths);
447 return NULL;
448 }
449 }
450
451 bool recursive = (strcmp(name, "delete_recursive") == 0);
452
453 int success = 0;
Tao Baoba9a42a2015-06-23 23:23:33 -0700454 for (int i = 0; i < argc; ++i) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700455 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
456 ++success;
457 free(paths[i]);
458 }
459 free(paths);
460
461 char buffer[10];
462 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800463 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700464}
465
Doug Zongker8edb00c2009-06-11 17:21:44 -0700466
Doug Zongker512536a2010-02-17 16:11:44 -0800467Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700468 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700469 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700470 }
471 char* frac_str;
472 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700473 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700474 return NULL;
475 }
476
477 double frac = strtod(frac_str, NULL);
Tao Baob15fd222015-09-24 11:10:51 -0700478 int sec;
479 android::base::ParseInt(sec_str, &sec);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700480
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700481 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700482 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
483
Doug Zongker9931f7f2009-06-10 14:11:53 -0700484 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800485 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700486}
487
Doug Zongker512536a2010-02-17 16:11:44 -0800488Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700489 if (argc != 1) {
490 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
491 }
492 char* frac_str;
493 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
494 return NULL;
495 }
496
497 double frac = strtod(frac_str, NULL);
498
499 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
500 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
501
Doug Zongker512536a2010-02-17 16:11:44 -0800502 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700503}
504
Doug Zongker8edb00c2009-06-11 17:21:44 -0700505// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800506Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700507 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700508 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700509 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700510 }
511 char* zip_path;
512 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700513 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700514
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700515 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700516
517 // To create a consistent system image, never use the clock for timestamps.
518 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
519
520 bool success = mzExtractRecursive(za, zip_path, dest_path,
Narayan Kamath9c0f5d62015-02-23 14:09:31 +0000521 &timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500522 NULL, NULL, sehandle);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700523 free(zip_path);
524 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800525 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700526}
527
Doug Zongker8edb00c2009-06-11 17:21:44 -0700528
529// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800530// or
531// package_extract_file(package_path)
532// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800533// function (the char* returned is actually a FileContents*).
534Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700535 int argc, Expr* argv[]) {
Doug Zongker5f875bf2014-08-22 14:53:43 -0700536 if (argc < 1 || argc > 2) {
537 return ErrorAbort(state, "%s() expects 1 or 2 args, got %d",
Doug Zongker6aece332010-02-01 14:40:12 -0800538 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700539 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700540 bool success = false;
Doug Zongker43772d22014-06-09 14:13:19 -0700541
Doug Zongker5f875bf2014-08-22 14:53:43 -0700542 if (argc == 2) {
543 // The two-argument version extracts to a file.
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800544
545 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700546
Doug Zongker6aece332010-02-01 14:40:12 -0800547 char* zip_path;
548 char* dest_path;
Doug Zongker5f875bf2014-08-22 14:53:43 -0700549 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800550
Doug Zongker6aece332010-02-01 14:40:12 -0800551 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
552 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700553 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800554 goto done2;
555 }
556
Tao Baoba9a42a2015-06-23 23:23:33 -0700557 {
558 FILE* f = fopen(dest_path, "wb");
559 if (f == NULL) {
560 printf("%s: can't open %s for write: %s\n",
561 name, dest_path, strerror(errno));
562 goto done2;
563 }
564 success = mzExtractZipEntryToFile(za, entry, fileno(f));
565 fclose(f);
Doug Zongker6aece332010-02-01 14:40:12 -0800566 }
Doug Zongker6aece332010-02-01 14:40:12 -0800567
568 done2:
569 free(zip_path);
570 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800571 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800572 } else {
573 // The one-argument version returns the contents of the file
574 // as the result.
575
576 char* zip_path;
Tao Baoba9a42a2015-06-23 23:23:33 -0700577 Value* v = reinterpret_cast<Value*>(malloc(sizeof(Value)));
Doug Zongker512536a2010-02-17 16:11:44 -0800578 v->type = VAL_BLOB;
579 v->size = -1;
580 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800581
582 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
583
584 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
585 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
586 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700587 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800588 goto done1;
589 }
590
Doug Zongker512536a2010-02-17 16:11:44 -0800591 v->size = mzGetZipEntryUncompLen(entry);
Tao Baoba9a42a2015-06-23 23:23:33 -0700592 v->data = reinterpret_cast<char*>(malloc(v->size));
Doug Zongker512536a2010-02-17 16:11:44 -0800593 if (v->data == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700594 printf("%s: failed to allocate %ld bytes for %s\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800595 name, (long)v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800596 goto done1;
597 }
598
Doug Zongker512536a2010-02-17 16:11:44 -0800599 success = mzExtractZipEntryToBuffer(za, entry,
600 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800601
602 done1:
603 free(zip_path);
604 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800605 free(v->data);
606 v->data = NULL;
607 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800608 }
Doug Zongker512536a2010-02-17 16:11:44 -0800609 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700610 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700611}
612
Doug Zongkera23075f2012-08-06 16:19:09 -0700613// Create all parent directories of name, if necessary.
614static int make_parents(char* name) {
615 char* p;
616 for (p = name + (strlen(name)-1); p > name; --p) {
617 if (*p != '/') continue;
618 *p = '\0';
619 if (make_parents(name) < 0) return -1;
620 int result = mkdir(name, 0700);
Michael Rungea91ecc52014-07-21 17:40:02 -0700621 if (result == 0) printf("created [%s]\n", name);
Doug Zongkera23075f2012-08-06 16:19:09 -0700622 *p = '/';
623 if (result == 0 || errno == EEXIST) {
624 // successfully created or already existed; we're done
625 return 0;
626 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700627 printf("failed to mkdir %s: %s\n", name, strerror(errno));
Doug Zongkera23075f2012-08-06 16:19:09 -0700628 return -1;
629 }
630 }
631 return 0;
632}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700633
Doug Zongker9931f7f2009-06-10 14:11:53 -0700634// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700635// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800636Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700637 if (argc == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700638 return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700639 }
640 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700641 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700642 if (target == NULL) return NULL;
643
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700644 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700645 if (srcs == NULL) {
646 free(target);
647 return NULL;
648 }
649
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700650 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700651 int i;
652 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700653 if (unlink(srcs[i]) < 0) {
654 if (errno != ENOENT) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700655 printf("%s: failed to remove %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700656 name, srcs[i], strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700657 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700658 }
659 }
Doug Zongkera23075f2012-08-06 16:19:09 -0700660 if (make_parents(srcs[i])) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700661 printf("%s: failed to symlink %s to %s: making parents failed\n",
Doug Zongkera23075f2012-08-06 16:19:09 -0700662 name, srcs[i], target);
663 ++bad;
664 }
Doug Zongker60babf82009-09-18 15:11:24 -0700665 if (symlink(target, srcs[i]) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700666 printf("%s: failed to symlink %s to %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700667 name, srcs[i], target, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700668 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700669 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700670 free(srcs[i]);
671 }
672 free(srcs);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700673 if (bad) {
674 return ErrorAbort(state, "%s: some symlinks failed", name);
675 }
Doug Zongker512536a2010-02-17 16:11:44 -0800676 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700677}
678
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700679struct perm_parsed_args {
680 bool has_uid;
681 uid_t uid;
682 bool has_gid;
683 gid_t gid;
684 bool has_mode;
685 mode_t mode;
686 bool has_fmode;
687 mode_t fmode;
688 bool has_dmode;
689 mode_t dmode;
690 bool has_selabel;
691 char* selabel;
692 bool has_capabilities;
693 uint64_t capabilities;
694};
695
Michael Runged4a63422014-10-22 19:48:41 -0700696static struct perm_parsed_args ParsePermArgs(State * state, int argc, char** args) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700697 int i;
698 struct perm_parsed_args parsed;
699 int bad = 0;
700 static int max_warnings = 20;
701
702 memset(&parsed, 0, sizeof(parsed));
703
704 for (i = 1; i < argc; i += 2) {
705 if (strcmp("uid", args[i]) == 0) {
706 int64_t uid;
707 if (sscanf(args[i+1], "%" SCNd64, &uid) == 1) {
708 parsed.uid = uid;
709 parsed.has_uid = true;
710 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700711 uiPrintf(state, "ParsePermArgs: invalid UID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700712 bad++;
713 }
714 continue;
715 }
716 if (strcmp("gid", args[i]) == 0) {
717 int64_t gid;
718 if (sscanf(args[i+1], "%" SCNd64, &gid) == 1) {
719 parsed.gid = gid;
720 parsed.has_gid = true;
721 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700722 uiPrintf(state, "ParsePermArgs: invalid GID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700723 bad++;
724 }
725 continue;
726 }
727 if (strcmp("mode", args[i]) == 0) {
728 int32_t mode;
729 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
730 parsed.mode = mode;
731 parsed.has_mode = true;
732 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700733 uiPrintf(state, "ParsePermArgs: invalid mode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700734 bad++;
735 }
736 continue;
737 }
738 if (strcmp("dmode", args[i]) == 0) {
739 int32_t mode;
740 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
741 parsed.dmode = mode;
742 parsed.has_dmode = true;
743 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700744 uiPrintf(state, "ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700745 bad++;
746 }
747 continue;
748 }
749 if (strcmp("fmode", args[i]) == 0) {
750 int32_t mode;
751 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
752 parsed.fmode = mode;
753 parsed.has_fmode = true;
754 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700755 uiPrintf(state, "ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700756 bad++;
757 }
758 continue;
759 }
760 if (strcmp("capabilities", args[i]) == 0) {
761 int64_t capabilities;
762 if (sscanf(args[i+1], "%" SCNi64, &capabilities) == 1) {
763 parsed.capabilities = capabilities;
764 parsed.has_capabilities = true;
765 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700766 uiPrintf(state, "ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700767 bad++;
768 }
769 continue;
770 }
771 if (strcmp("selabel", args[i]) == 0) {
772 if (args[i+1][0] != '\0') {
773 parsed.selabel = args[i+1];
774 parsed.has_selabel = true;
775 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700776 uiPrintf(state, "ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700777 bad++;
778 }
779 continue;
780 }
781 if (max_warnings != 0) {
782 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i]);
783 max_warnings--;
784 if (max_warnings == 0) {
785 printf("ParsedPermArgs: suppressing further warnings\n");
786 }
787 }
788 }
789 return parsed;
790}
791
792static int ApplyParsedPerms(
Michael Runged4a63422014-10-22 19:48:41 -0700793 State * state,
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700794 const char* filename,
795 const struct stat *statptr,
796 struct perm_parsed_args parsed)
797{
798 int bad = 0;
799
Nick Kralevich68802412014-10-23 20:36:42 -0700800 if (parsed.has_selabel) {
801 if (lsetfilecon(filename, parsed.selabel) != 0) {
802 uiPrintf(state, "ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n",
803 filename, parsed.selabel, strerror(errno));
804 bad++;
805 }
806 }
807
Nick Kraleviche4612512013-09-10 15:34:19 -0700808 /* ignore symlinks */
809 if (S_ISLNK(statptr->st_mode)) {
Nick Kralevich68802412014-10-23 20:36:42 -0700810 return bad;
Nick Kraleviche4612512013-09-10 15:34:19 -0700811 }
812
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700813 if (parsed.has_uid) {
814 if (chown(filename, parsed.uid, -1) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700815 uiPrintf(state, "ApplyParsedPerms: chown of %s to %d failed: %s\n",
816 filename, parsed.uid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700817 bad++;
818 }
819 }
820
821 if (parsed.has_gid) {
822 if (chown(filename, -1, parsed.gid) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700823 uiPrintf(state, "ApplyParsedPerms: chgrp of %s to %d failed: %s\n",
824 filename, parsed.gid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700825 bad++;
826 }
827 }
828
829 if (parsed.has_mode) {
830 if (chmod(filename, parsed.mode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700831 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
832 filename, parsed.mode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700833 bad++;
834 }
835 }
836
837 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
838 if (chmod(filename, parsed.dmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700839 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
840 filename, parsed.dmode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700841 bad++;
842 }
843 }
844
845 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
846 if (chmod(filename, parsed.fmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700847 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700848 filename, parsed.fmode, strerror(errno));
849 bad++;
850 }
851 }
852
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700853 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
854 if (parsed.capabilities == 0) {
855 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
856 // Report failure unless it's ENODATA (attribute not set)
Michael Runged4a63422014-10-22 19:48:41 -0700857 uiPrintf(state, "ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700858 filename, parsed.capabilities, strerror(errno));
859 bad++;
860 }
861 } else {
862 struct vfs_cap_data cap_data;
863 memset(&cap_data, 0, sizeof(cap_data));
864 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
865 cap_data.data[0].permitted = (uint32_t) (parsed.capabilities & 0xffffffff);
866 cap_data.data[0].inheritable = 0;
867 cap_data.data[1].permitted = (uint32_t) (parsed.capabilities >> 32);
868 cap_data.data[1].inheritable = 0;
869 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700870 uiPrintf(state, "ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n",
871 filename, parsed.capabilities, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700872 bad++;
873 }
874 }
875 }
876
877 return bad;
878}
879
880// nftw doesn't allow us to pass along context, so we need to use
881// global variables. *sigh*
882static struct perm_parsed_args recursive_parsed_args;
Michael Runged4a63422014-10-22 19:48:41 -0700883static State* recursive_state;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700884
885static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr,
886 int fileflags, struct FTW *pfwt) {
Michael Runged4a63422014-10-22 19:48:41 -0700887 return ApplyParsedPerms(recursive_state, filename, statptr, recursive_parsed_args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700888}
889
890static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700891 int bad = 0;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700892 struct stat sb;
893 Value* result = NULL;
894
895 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
896
897 if ((argc % 2) != 1) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700898 return ErrorAbort(state, "%s() expects an odd number of arguments, got %d", name, argc);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700899 }
900
901 char** args = ReadVarArgs(state, argc, argv);
902 if (args == NULL) return NULL;
903
904 if (lstat(args[0], &sb) == -1) {
905 result = ErrorAbort(state, "%s: Error on lstat of \"%s\": %s", name, args[0], strerror(errno));
906 goto done;
907 }
908
Tao Baoba9a42a2015-06-23 23:23:33 -0700909 {
910 struct perm_parsed_args parsed = ParsePermArgs(state, argc, args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700911
Tao Baoba9a42a2015-06-23 23:23:33 -0700912 if (recursive) {
913 recursive_parsed_args = parsed;
914 recursive_state = state;
915 bad += nftw(args[0], do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
916 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
917 recursive_state = NULL;
918 } else {
919 bad += ApplyParsedPerms(state, args[0], &sb, parsed);
920 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700921 }
922
923done:
Tao Baoba9a42a2015-06-23 23:23:33 -0700924 for (int i = 0; i < argc; ++i) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700925 free(args[i]);
926 }
927 free(args);
928
929 if (result != NULL) {
930 return result;
931 }
932
933 if (bad > 0) {
934 return ErrorAbort(state, "%s: some changes failed", name);
935 }
936
937 return StringValue(strdup(""));
938}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700939
Doug Zongker512536a2010-02-17 16:11:44 -0800940Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700941 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700942 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700943 }
Tao Baoba9a42a2015-06-23 23:23:33 -0700944 char* key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700945 if (key == NULL) return NULL;
946
947 char value[PROPERTY_VALUE_MAX];
948 property_get(key, value, "");
949 free(key);
950
Doug Zongker512536a2010-02-17 16:11:44 -0800951 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700952}
953
954
Doug Zongker47cace92009-06-18 10:11:50 -0700955// file_getprop(file, key)
956//
957// interprets 'file' as a getprop-style file (key=value pairs, one
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700958// per line. # comment lines,blank lines, lines without '=' ignored),
959// and returns the value for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800960Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700961 char* result = NULL;
962 char* buffer = NULL;
963 char* filename;
964 char* key;
965 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
966 return NULL;
967 }
968
969 struct stat st;
970 if (stat(filename, &st) < 0) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700971 ErrorAbort(state, "%s: failed to stat \"%s\": %s", name, filename, strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -0700972 goto done;
973 }
974
975#define MAX_FILE_GETPROP_SIZE 65536
976
977 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700978 ErrorAbort(state, "%s too large for %s (max %d)", filename, name, MAX_FILE_GETPROP_SIZE);
Doug Zongker47cace92009-06-18 10:11:50 -0700979 goto done;
980 }
981
Tao Baoba9a42a2015-06-23 23:23:33 -0700982 buffer = reinterpret_cast<char*>(malloc(st.st_size+1));
Doug Zongker47cace92009-06-18 10:11:50 -0700983 if (buffer == NULL) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700984 ErrorAbort(state, "%s: failed to alloc %lld bytes", name, (long long)st.st_size+1);
Doug Zongker47cace92009-06-18 10:11:50 -0700985 goto done;
986 }
987
Tao Baoba9a42a2015-06-23 23:23:33 -0700988 FILE* f;
989 f = fopen(filename, "rb");
Doug Zongker47cace92009-06-18 10:11:50 -0700990 if (f == NULL) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700991 ErrorAbort(state, "%s: failed to open %s: %s", name, filename, strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -0700992 goto done;
993 }
994
Tao Bao5701d582015-09-24 10:56:48 -0700995 if (fread(buffer, 1, st.st_size, f) != static_cast<size_t>(st.st_size)) {
Doug Zongkera23075f2012-08-06 16:19:09 -0700996 ErrorAbort(state, "%s: failed to read %lld bytes from %s",
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700997 name, (long long)st.st_size+1, filename);
Doug Zongker47cace92009-06-18 10:11:50 -0700998 fclose(f);
999 goto done;
1000 }
1001 buffer[st.st_size] = '\0';
1002
1003 fclose(f);
1004
Tao Baoba9a42a2015-06-23 23:23:33 -07001005 char* line;
1006 line = strtok(buffer, "\n");
Doug Zongker47cace92009-06-18 10:11:50 -07001007 do {
1008 // skip whitespace at start of line
1009 while (*line && isspace(*line)) ++line;
1010
1011 // comment or blank line: skip to next line
1012 if (*line == '\0' || *line == '#') continue;
1013
1014 char* equal = strchr(line, '=');
1015 if (equal == NULL) {
Michael Rungeaa1a31e2014-04-25 18:47:18 -07001016 continue;
Doug Zongker47cace92009-06-18 10:11:50 -07001017 }
1018
1019 // trim whitespace between key and '='
1020 char* key_end = equal-1;
1021 while (key_end > line && isspace(*key_end)) --key_end;
1022 key_end[1] = '\0';
1023
1024 // not the key we're looking for
1025 if (strcmp(key, line) != 0) continue;
1026
1027 // skip whitespace after the '=' to the start of the value
1028 char* val_start = equal+1;
1029 while(*val_start && isspace(*val_start)) ++val_start;
1030
1031 // trim trailing whitespace
1032 char* val_end = val_start + strlen(val_start)-1;
1033 while (val_end > val_start && isspace(*val_end)) --val_end;
1034 val_end[1] = '\0';
1035
1036 result = strdup(val_start);
1037 break;
1038
1039 } while ((line = strtok(NULL, "\n")));
1040
1041 if (result == NULL) result = strdup("");
1042
1043 done:
1044 free(filename);
1045 free(key);
1046 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -08001047 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -07001048}
1049
Doug Zongker179b2d92011-04-12 15:49:04 -07001050// write_raw_image(filename_or_blob, partition)
Doug Zongker512536a2010-02-17 16:11:44 -08001051Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001052 char* result = NULL;
1053
Doug Zongker179b2d92011-04-12 15:49:04 -07001054 Value* partition_value;
1055 Value* contents;
1056 if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001057 return NULL;
1058 }
1059
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001060 char* partition = NULL;
Doug Zongker179b2d92011-04-12 15:49:04 -07001061 if (partition_value->type != VAL_STRING) {
1062 ErrorAbort(state, "partition argument to %s must be string", name);
1063 goto done;
1064 }
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001065 partition = partition_value->data;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001066 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001067 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001068 goto done;
1069 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001070 if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001071 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001072 goto done;
1073 }
1074
1075 mtd_scan_partitions();
Tao Baoba9a42a2015-06-23 23:23:33 -07001076 const MtdPartition* mtd;
1077 mtd = mtd_find_partition_by_name(partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001078 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001079 printf("%s: no mtd partition named \"%s\"\n", name, partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001080 result = strdup("");
1081 goto done;
1082 }
1083
Tao Baoba9a42a2015-06-23 23:23:33 -07001084 MtdWriteContext* ctx;
1085 ctx = mtd_write_partition(mtd);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001086 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001087 printf("%s: can't write mtd partition \"%s\"\n",
Doug Zongker8edb00c2009-06-11 17:21:44 -07001088 name, partition);
1089 result = strdup("");
1090 goto done;
1091 }
1092
1093 bool success;
1094
Doug Zongker179b2d92011-04-12 15:49:04 -07001095 if (contents->type == VAL_STRING) {
1096 // we're given a filename as the contents
1097 char* filename = contents->data;
1098 FILE* f = fopen(filename, "rb");
1099 if (f == NULL) {
Tao Baoba9a42a2015-06-23 23:23:33 -07001100 printf("%s: can't open %s: %s\n", name, filename, strerror(errno));
Doug Zongker179b2d92011-04-12 15:49:04 -07001101 result = strdup("");
1102 goto done;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001103 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001104
1105 success = true;
Tao Baoba9a42a2015-06-23 23:23:33 -07001106 char* buffer = reinterpret_cast<char*>(malloc(BUFSIZ));
Doug Zongker179b2d92011-04-12 15:49:04 -07001107 int read;
1108 while (success && (read = fread(buffer, 1, BUFSIZ, f)) > 0) {
1109 int wrote = mtd_write_data(ctx, buffer, read);
1110 success = success && (wrote == read);
1111 }
1112 free(buffer);
1113 fclose(f);
1114 } else {
1115 // we're given a blob as the contents
1116 ssize_t wrote = mtd_write_data(ctx, contents->data, contents->size);
1117 success = (wrote == contents->size);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001118 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001119 if (!success) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001120 printf("mtd_write_data to %s failed: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -07001121 partition, strerror(errno));
1122 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001123
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001124 if (mtd_erase_blocks(ctx, -1) == -1) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001125 printf("%s: error erasing blocks of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001126 }
1127 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001128 printf("%s: error closing write of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001129 }
1130
Doug Zongker179b2d92011-04-12 15:49:04 -07001131 printf("%s %s partition\n",
1132 success ? "wrote" : "failed to write", partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001133
1134 result = success ? partition : strdup("");
1135
1136done:
Doug Zongker179b2d92011-04-12 15:49:04 -07001137 if (result != partition) FreeValue(partition_value);
1138 FreeValue(contents);
Doug Zongker512536a2010-02-17 16:11:44 -08001139 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001140}
1141
Doug Zongker8edb00c2009-06-11 17:21:44 -07001142// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001143Value* ApplyPatchSpaceFn(const char* name, State* state,
1144 int argc, Expr* argv[]) {
1145 char* bytes_str;
1146 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
1147 return NULL;
1148 }
1149
Tao Baob15fd222015-09-24 11:10:51 -07001150 size_t bytes;
1151 if (!android::base::ParseUint(bytes_str, &bytes)) {
Tao Baoba9a42a2015-06-23 23:23:33 -07001152 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n", name, bytes_str);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001153 free(bytes_str);
Tao Baob15fd222015-09-24 11:10:51 -07001154 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001155 }
1156
1157 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
1158}
1159
Doug Zongker52b40362014-02-10 15:30:30 -08001160// apply_patch(file, size, init_sha1, tgt_sha1, patch)
1161
Doug Zongker512536a2010-02-17 16:11:44 -08001162Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001163 if (argc < 6 || (argc % 2) == 1) {
1164 return ErrorAbort(state, "%s(): expected at least 6 args and an "
1165 "even number, got %d",
1166 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001167 }
1168
Doug Zongkerc4351c72010-02-22 14:46:32 -08001169 char* source_filename;
1170 char* target_filename;
1171 char* target_sha1;
1172 char* target_size_str;
1173 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
1174 &target_sha1, &target_size_str) < 0) {
1175 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001176 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001177
Tao Baob15fd222015-09-24 11:10:51 -07001178 size_t target_size;
1179 if (!android::base::ParseUint(target_size_str, &target_size)) {
1180 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count", name, target_size_str);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001181 free(source_filename);
1182 free(target_filename);
1183 free(target_sha1);
1184 free(target_size_str);
Tao Baob15fd222015-09-24 11:10:51 -07001185 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001186 }
1187
1188 int patchcount = (argc-4) / 2;
1189 Value** patches = ReadValueVarArgs(state, argc-4, argv+4);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001190
1191 int i;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001192 for (i = 0; i < patchcount; ++i) {
1193 if (patches[i*2]->type != VAL_STRING) {
1194 ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i);
1195 break;
1196 }
1197 if (patches[i*2+1]->type != VAL_BLOB) {
1198 ErrorAbort(state, "%s(): patch #%d is not blob", name, i);
1199 break;
1200 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001201 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001202 if (i != patchcount) {
1203 for (i = 0; i < patchcount*2; ++i) {
1204 FreeValue(patches[i]);
1205 }
1206 free(patches);
1207 return NULL;
1208 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001209
Tao Baoba9a42a2015-06-23 23:23:33 -07001210 char** patch_sha_str = reinterpret_cast<char**>(malloc(patchcount * sizeof(char*)));
Doug Zongkerc4351c72010-02-22 14:46:32 -08001211 for (i = 0; i < patchcount; ++i) {
1212 patch_sha_str[i] = patches[i*2]->data;
1213 patches[i*2]->data = NULL;
1214 FreeValue(patches[i*2]);
1215 patches[i] = patches[i*2+1];
Doug Zongker8edb00c2009-06-11 17:21:44 -07001216 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001217
1218 int result = applypatch(source_filename, target_filename,
1219 target_sha1, target_size,
Doug Zongkera3ccba62012-08-20 15:28:02 -07001220 patchcount, patch_sha_str, patches, NULL);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001221
1222 for (i = 0; i < patchcount; ++i) {
1223 FreeValue(patches[i]);
1224 }
1225 free(patch_sha_str);
1226 free(patches);
1227
1228 return StringValue(strdup(result == 0 ? "t" : ""));
1229}
1230
1231// apply_patch_check(file, [sha1_1, ...])
1232Value* ApplyPatchCheckFn(const char* name, State* state,
1233 int argc, Expr* argv[]) {
1234 if (argc < 1) {
1235 return ErrorAbort(state, "%s(): expected at least 1 arg, got %d",
1236 name, argc);
1237 }
1238
1239 char* filename;
1240 if (ReadArgs(state, argv, 1, &filename) < 0) {
1241 return NULL;
1242 }
1243
1244 int patchcount = argc-1;
1245 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
1246
1247 int result = applypatch_check(filename, patchcount, sha1s);
1248
1249 int i;
1250 for (i = 0; i < patchcount; ++i) {
1251 free(sha1s[i]);
1252 }
1253 free(sha1s);
1254
1255 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001256}
1257
Tao Bao1107d962015-09-09 17:16:55 -07001258// This is the updater side handler for ui_print() in edify script. Contents
1259// will be sent over to the recovery side for on-screen display.
Doug Zongker512536a2010-02-17 16:11:44 -08001260Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001261 char** args = ReadVarArgs(state, argc, argv);
1262 if (args == NULL) {
1263 return NULL;
1264 }
1265
Tao Bao1107d962015-09-09 17:16:55 -07001266 std::string buffer;
1267 for (int i = 0; i < argc; ++i) {
1268 buffer += args[i];
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001269 free(args[i]);
1270 }
1271 free(args);
Tao Bao1107d962015-09-09 17:16:55 -07001272
1273 buffer += "\n";
Michael Runged4a63422014-10-22 19:48:41 -07001274 uiPrint(state, buffer);
Tao Bao1107d962015-09-09 17:16:55 -07001275 return StringValue(strdup(buffer.c_str()));
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001276}
1277
Doug Zongkerd0181b82011-10-19 10:51:12 -07001278Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
1279 if (argc != 0) {
1280 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1281 }
1282 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
1283 return StringValue(strdup("t"));
1284}
1285
Doug Zongker512536a2010-02-17 16:11:44 -08001286Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001287 if (argc < 1) {
1288 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1289 }
1290 char** args = ReadVarArgs(state, argc, argv);
1291 if (args == NULL) {
1292 return NULL;
1293 }
1294
Tao Baoba9a42a2015-06-23 23:23:33 -07001295 char** args2 = reinterpret_cast<char**>(malloc(sizeof(char*) * (argc+1)));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001296 memcpy(args2, args, sizeof(char*) * argc);
1297 args2[argc] = NULL;
1298
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001299 printf("about to run program [%s] with %d args\n", args2[0], argc);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001300
1301 pid_t child = fork();
1302 if (child == 0) {
1303 execv(args2[0], args2);
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001304 printf("run_program: execv failed: %s\n", strerror(errno));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001305 _exit(1);
1306 }
1307 int status;
1308 waitpid(child, &status, 0);
1309 if (WIFEXITED(status)) {
1310 if (WEXITSTATUS(status) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001311 printf("run_program: child exited with status %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001312 WEXITSTATUS(status));
1313 }
1314 } else if (WIFSIGNALED(status)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001315 printf("run_program: child terminated by signal %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001316 WTERMSIG(status));
1317 }
1318
1319 int i;
1320 for (i = 0; i < argc; ++i) {
1321 free(args[i]);
1322 }
1323 free(args);
1324 free(args2);
1325
1326 char buffer[20];
1327 sprintf(buffer, "%d", status);
1328
Doug Zongker512536a2010-02-17 16:11:44 -08001329 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001330}
1331
Doug Zongker512536a2010-02-17 16:11:44 -08001332// sha1_check(data)
1333// to return the sha1 of the data (given in the format returned by
1334// read_file).
1335//
1336// sha1_check(data, sha1_hex, [sha1_hex, ...])
1337// returns the sha1 of the file if it matches any of the hex
1338// strings passed, or "" if it does not equal any of them.
1339//
1340Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1341 if (argc < 1) {
1342 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1343 }
1344
1345 Value** args = ReadValueVarArgs(state, argc, argv);
1346 if (args == NULL) {
1347 return NULL;
1348 }
1349
1350 if (args[0]->size < 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001351 return StringValue(strdup(""));
1352 }
1353 uint8_t digest[SHA_DIGEST_SIZE];
Doug Zongkerbac7fba2013-04-10 11:32:17 -07001354 SHA_hash(args[0]->data, args[0]->size, digest);
Doug Zongker512536a2010-02-17 16:11:44 -08001355 FreeValue(args[0]);
1356
1357 if (argc == 1) {
1358 return StringValue(PrintSha1(digest));
1359 }
1360
1361 int i;
Tao Baoba9a42a2015-06-23 23:23:33 -07001362 uint8_t* arg_digest = reinterpret_cast<uint8_t*>(malloc(SHA_DIGEST_SIZE));
Doug Zongker512536a2010-02-17 16:11:44 -08001363 for (i = 1; i < argc; ++i) {
1364 if (args[i]->type != VAL_STRING) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001365 printf("%s(): arg %d is not a string; skipping",
Doug Zongker512536a2010-02-17 16:11:44 -08001366 name, i);
1367 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1368 // Warn about bad args and skip them.
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001369 printf("%s(): error parsing \"%s\" as sha-1; skipping",
1370 name, args[i]->data);
Doug Zongker512536a2010-02-17 16:11:44 -08001371 } else if (memcmp(digest, arg_digest, SHA_DIGEST_SIZE) == 0) {
1372 break;
1373 }
1374 FreeValue(args[i]);
1375 }
1376 if (i >= argc) {
1377 // Didn't match any of the hex strings; return false.
1378 return StringValue(strdup(""));
1379 }
1380 // Found a match; free all the remaining arguments and return the
1381 // matched one.
1382 int j;
1383 for (j = i+1; j < argc; ++j) {
1384 FreeValue(args[j]);
1385 }
1386 return args[i];
1387}
1388
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001389// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001390// is actually a FileContents*).
1391Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1392 if (argc != 1) {
1393 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1394 }
1395 char* filename;
1396 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1397
Tao Baoba9a42a2015-06-23 23:23:33 -07001398 Value* v = reinterpret_cast<Value*>(malloc(sizeof(Value)));
Doug Zongker512536a2010-02-17 16:11:44 -08001399 v->type = VAL_BLOB;
1400
1401 FileContents fc;
Doug Zongkera1bc1482014-02-13 15:18:19 -08001402 if (LoadFileContents(filename, &fc) != 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001403 free(filename);
Michael Runge6eed2242013-12-13 17:13:11 -08001404 v->size = -1;
1405 v->data = NULL;
Doug Zongker512536a2010-02-17 16:11:44 -08001406 free(fc.data);
Michael Runge6eed2242013-12-13 17:13:11 -08001407 return v;
Doug Zongker512536a2010-02-17 16:11:44 -08001408 }
1409
1410 v->size = fc.size;
1411 v->data = (char*)fc.data;
1412
1413 free(filename);
1414 return v;
1415}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001416
Doug Zongkerc87bab12013-11-25 13:53:25 -08001417// Immediately reboot the device. Recovery is not finished normally,
1418// so if you reboot into recovery it will re-start applying the
1419// current package (because nothing has cleared the copy of the
1420// arguments stored in the BCB).
1421//
1422// The argument is the partition name passed to the android reboot
1423// property. It can be "recovery" to boot from the recovery
1424// partition, or "" (empty string) to boot from the regular boot
1425// partition.
1426Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) {
1427 if (argc != 2) {
1428 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1429 }
1430
1431 char* filename;
1432 char* property;
1433 if (ReadArgs(state, argv, 2, &filename, &property) < 0) return NULL;
1434
1435 char buffer[80];
1436
1437 // zero out the 'command' field of the bootloader message.
1438 memset(buffer, 0, sizeof(((struct bootloader_message*)0)->command));
1439 FILE* f = fopen(filename, "r+b");
1440 fseek(f, offsetof(struct bootloader_message, command), SEEK_SET);
1441 fwrite(buffer, sizeof(((struct bootloader_message*)0)->command), 1, f);
1442 fclose(f);
1443 free(filename);
1444
1445 strcpy(buffer, "reboot,");
1446 if (property != NULL) {
1447 strncat(buffer, property, sizeof(buffer)-10);
1448 }
1449
1450 property_set(ANDROID_RB_PROPERTY, buffer);
1451
1452 sleep(5);
1453 free(property);
1454 ErrorAbort(state, "%s() failed to reboot", name);
1455 return NULL;
1456}
1457
1458// Store a string value somewhere that future invocations of recovery
1459// can access it. This value is called the "stage" and can be used to
1460// drive packages that need to do reboots in the middle of
1461// installation and keep track of where they are in the multi-stage
1462// install.
1463//
1464// The first argument is the block device for the misc partition
1465// ("/misc" in the fstab), which is where this value is stored. The
1466// second argument is the string to store; it should not exceed 31
1467// bytes.
1468Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
1469 if (argc != 2) {
1470 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1471 }
1472
1473 char* filename;
1474 char* stagestr;
1475 if (ReadArgs(state, argv, 2, &filename, &stagestr) < 0) return NULL;
1476
1477 // Store this value in the misc partition, immediately after the
1478 // bootloader message that the main recovery uses to save its
1479 // arguments in case of the device restarting midway through
1480 // package installation.
1481 FILE* f = fopen(filename, "r+b");
1482 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1483 int to_write = strlen(stagestr)+1;
1484 int max_size = sizeof(((struct bootloader_message*)0)->stage);
1485 if (to_write > max_size) {
1486 to_write = max_size;
1487 stagestr[max_size-1] = 0;
1488 }
1489 fwrite(stagestr, to_write, 1, f);
1490 fclose(f);
1491
1492 free(stagestr);
1493 return StringValue(filename);
1494}
1495
1496// Return the value most recently saved with SetStageFn. The argument
1497// is the block device for the misc partition.
1498Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001499 if (argc != 1) {
Doug Zongkerc87bab12013-11-25 13:53:25 -08001500 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1501 }
1502
1503 char* filename;
1504 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1505
1506 char buffer[sizeof(((struct bootloader_message*)0)->stage)];
1507 FILE* f = fopen(filename, "rb");
1508 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1509 fread(buffer, sizeof(buffer), 1, f);
1510 fclose(f);
1511 buffer[sizeof(buffer)-1] = '\0';
1512
1513 return StringValue(strdup(buffer));
1514}
1515
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001516Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) {
1517 if (argc != 2) {
1518 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1519 }
1520
1521 char* filename;
1522 char* len_str;
1523 if (ReadArgs(state, argv, 2, &filename, &len_str) < 0) return NULL;
1524
Tao Baob15fd222015-09-24 11:10:51 -07001525 size_t len;
1526 android::base::ParseUint(len_str, &len);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001527 int fd = open(filename, O_WRONLY, 0644);
1528 int success = wipe_block_device(fd, len);
1529
1530 free(filename);
1531 free(len_str);
1532
1533 close(fd);
1534
1535 return StringValue(strdup(success ? "t" : ""));
1536}
1537
Doug Zongkerc704e062014-05-23 08:40:35 -07001538Value* EnableRebootFn(const char* name, State* state, int argc, Expr* argv[]) {
1539 if (argc != 0) {
1540 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1541 }
1542 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
1543 fprintf(ui->cmd_pipe, "enable_reboot\n");
1544 return StringValue(strdup("t"));
1545}
1546
Michael Rungeacf47db2014-11-21 00:12:28 -08001547Value* Tune2FsFn(const char* name, State* state, int argc, Expr* argv[]) {
1548 if (argc == 0) {
1549 return ErrorAbort(state, "%s() expects args, got %d", name, argc);
1550 }
1551
1552 char** args = ReadVarArgs(state, argc, argv);
1553 if (args == NULL) {
1554 return ErrorAbort(state, "%s() could not read args", name);
1555 }
1556
Tao Baoba9a42a2015-06-23 23:23:33 -07001557 char** args2 = reinterpret_cast<char**>(malloc(sizeof(char*) * (argc+1)));
Michael Rungeacf47db2014-11-21 00:12:28 -08001558 // Tune2fs expects the program name as its args[0]
1559 args2[0] = strdup(name);
Tao Baoba9a42a2015-06-23 23:23:33 -07001560 for (int i = 0; i < argc; ++i) {
Michael Rungeacf47db2014-11-21 00:12:28 -08001561 args2[i + 1] = args[i];
1562 }
1563 int result = tune2fs_main(argc + 1, args2);
Tao Baoba9a42a2015-06-23 23:23:33 -07001564 for (int i = 0; i < argc; ++i) {
Michael Rungeacf47db2014-11-21 00:12:28 -08001565 free(args[i]);
1566 }
1567 free(args);
1568
1569 free(args2[0]);
1570 free(args2);
1571 if (result != 0) {
1572 return ErrorAbort(state, "%s() returned error code %d", name, result);
1573 }
1574 return StringValue(strdup("t"));
1575}
1576
Doug Zongker9931f7f2009-06-10 14:11:53 -07001577void RegisterInstallFunctions() {
1578 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001579 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001580 RegisterFunction("unmount", UnmountFn);
1581 RegisterFunction("format", FormatFn);
1582 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001583 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001584 RegisterFunction("delete", DeleteFn);
1585 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001586 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1587 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001588 RegisterFunction("symlink", SymlinkFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001589
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001590 // Usage:
1591 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1592 // Example:
1593 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1594 RegisterFunction("set_metadata", SetMetadataFn);
1595
1596 // Usage:
1597 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1598 // Example:
1599 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1600 RegisterFunction("set_metadata_recursive", SetMetadataFn);
1601
Doug Zongker8edb00c2009-06-11 17:21:44 -07001602 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001603 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001604 RegisterFunction("write_raw_image", WriteRawImageFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001605
1606 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001607 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1608 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001609
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001610 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001611
Doug Zongker512536a2010-02-17 16:11:44 -08001612 RegisterFunction("read_file", ReadFileFn);
1613 RegisterFunction("sha1_check", Sha1CheckFn);
Michael Rungece7ca712013-11-06 17:42:20 -08001614 RegisterFunction("rename", RenameFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001615
Doug Zongkerd0181b82011-10-19 10:51:12 -07001616 RegisterFunction("wipe_cache", WipeCacheFn);
1617
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001618 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001619
1620 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001621
1622 RegisterFunction("reboot_now", RebootNowFn);
1623 RegisterFunction("get_stage", GetStageFn);
1624 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -07001625
1626 RegisterFunction("enable_reboot", EnableRebootFn);
Michael Rungeacf47db2014-11-21 00:12:28 -08001627 RegisterFunction("tune2fs", Tune2FsFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001628}