blob: a6ed078ba5004a8863a6cb62f5df806ab5d3b9b8 [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 Bao1107d962015-09-09 17:16:55 -070037#include <base/strings.h>
38#include <base/stringprintf.h>
39
Doug Zongkerc87bab12013-11-25 13:53:25 -080040#include "bootloader.h"
41#include "applypatch/applypatch.h"
42#include "cutils/android_reboot.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070043#include "cutils/misc.h"
44#include "cutils/properties.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070045#include "edify/expr.h"
Doug Zongker512536a2010-02-17 16:11:44 -080046#include "mincrypt/sha.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070047#include "minzip/DirUtil.h"
48#include "mtdutils/mounts.h"
49#include "mtdutils/mtdutils.h"
50#include "updater.h"
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -080051#include "install.h"
Michael Rungeacf47db2014-11-21 00:12:28 -080052#include "tune2fs.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070053
Doug Zongker3d177d02010-07-01 09:18:44 -070054#ifdef USE_EXT4
55#include "make_ext4fs.h"
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -080056#include "wipe.h"
Doug Zongker3d177d02010-07-01 09:18:44 -070057#endif
58
Tao Bao1107d962015-09-09 17:16:55 -070059// Send over the buffer to recovery though the command pipe.
60static void uiPrint(State* state, const std::string& buffer) {
61 UpdaterInfo* ui = reinterpret_cast<UpdaterInfo*>(state->cookie);
Tao Baob6918c72015-05-19 17:02:16 -070062
Tao Bao1107d962015-09-09 17:16:55 -070063 // "line1\nline2\n" will be split into 3 tokens: "line1", "line2" and "".
64 // So skip sending empty strings to UI.
65 std::vector<std::string> lines = android::base::Split(buffer, "\n");
66 for (auto& line: lines) {
67 if (!line.empty()) {
68 fprintf(ui->cmd_pipe, "ui_print %s\n", line.c_str());
69 fprintf(ui->cmd_pipe, "ui_print\n");
70 }
71 }
72
73 // On the updater side, we need to dump the contents to stderr (which has
74 // been redirected to the log file). Because the recovery will only print
75 // the contents to screen when processing pipe command ui_print.
76 fprintf(stderr, "%s", buffer.c_str());
Michael Runged4a63422014-10-22 19:48:41 -070077}
78
79__attribute__((__format__(printf, 2, 3))) __nonnull((2))
80void uiPrintf(State* state, const char* format, ...) {
Tao Bao1107d962015-09-09 17:16:55 -070081 std::string error_msg;
82
Michael Runged4a63422014-10-22 19:48:41 -070083 va_list ap;
84 va_start(ap, format);
Tao Bao1107d962015-09-09 17:16:55 -070085 android::base::StringAppendV(&error_msg, format, ap);
Michael Runged4a63422014-10-22 19:48:41 -070086 va_end(ap);
Tao Bao1107d962015-09-09 17:16:55 -070087
Michael Runged4a63422014-10-22 19:48:41 -070088 uiPrint(state, error_msg);
89}
90
Doug Zongker52b40362014-02-10 15:30:30 -080091// Take a sha-1 digest and return it as a newly-allocated hex string.
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070092char* PrintSha1(const uint8_t* digest) {
Tao Baoba9a42a2015-06-23 23:23:33 -070093 char* buffer = reinterpret_cast<char*>(malloc(SHA_DIGEST_SIZE*2 + 1));
Doug Zongker52b40362014-02-10 15:30:30 -080094 const char* alphabet = "0123456789abcdef";
Tao Baoba9a42a2015-06-23 23:23:33 -070095 size_t i;
Doug Zongker52b40362014-02-10 15:30:30 -080096 for (i = 0; i < SHA_DIGEST_SIZE; ++i) {
97 buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf];
98 buffer[i*2+1] = alphabet[digest[i] & 0xf];
99 }
100 buffer[i*2] = '\0';
101 return buffer;
102}
103
Doug Zongker3d177d02010-07-01 09:18:44 -0700104// mount(fs_type, partition_type, location, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700105//
Doug Zongker3d177d02010-07-01 09:18:44 -0700106// fs_type="yaffs2" partition_type="MTD" location=partition
107// fs_type="ext4" partition_type="EMMC" location=device
Doug Zongker512536a2010-02-17 16:11:44 -0800108Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700109 char* result = NULL;
Michael Runge168f7772014-10-22 17:05:08 -0700110 if (argc != 4 && argc != 5) {
111 return ErrorAbort(state, "%s() expects 4-5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700112 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700113 char* fs_type;
114 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700115 char* location;
116 char* mount_point;
Michael Runge168f7772014-10-22 17:05:08 -0700117 char* mount_options;
118 bool has_mount_options;
119 if (argc == 5) {
120 has_mount_options = true;
121 if (ReadArgs(state, argv, 5, &fs_type, &partition_type,
122 &location, &mount_point, &mount_options) < 0) {
123 return NULL;
124 }
125 } else {
126 has_mount_options = false;
127 if (ReadArgs(state, argv, 4, &fs_type, &partition_type,
Doug Zongker3d177d02010-07-01 09:18:44 -0700128 &location, &mount_point) < 0) {
Michael Runge168f7772014-10-22 17:05:08 -0700129 return NULL;
130 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700131 }
132
Doug Zongker3d177d02010-07-01 09:18:44 -0700133 if (strlen(fs_type) == 0) {
134 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
135 goto done;
136 }
137 if (strlen(partition_type) == 0) {
138 ErrorAbort(state, "partition_type argument to %s() can't be empty",
139 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700140 goto done;
141 }
142 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700143 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700144 goto done;
145 }
146 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700147 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700148 goto done;
149 }
150
Tao Baoba9a42a2015-06-23 23:23:33 -0700151 {
152 char *secontext = NULL;
Stephen Smalley779701d2012-02-09 14:13:23 -0500153
Tao Baoba9a42a2015-06-23 23:23:33 -0700154 if (sehandle) {
155 selabel_lookup(sehandle, &secontext, mount_point, 0755);
156 setfscreatecon(secontext);
157 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500158
Tao Baoba9a42a2015-06-23 23:23:33 -0700159 mkdir(mount_point, 0755);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700160
Tao Baoba9a42a2015-06-23 23:23:33 -0700161 if (secontext) {
162 freecon(secontext);
163 setfscreatecon(NULL);
164 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500165 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500166
Doug Zongker3d177d02010-07-01 09:18:44 -0700167 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700168 mtd_scan_partitions();
169 const MtdPartition* mtd;
170 mtd = mtd_find_partition_by_name(location);
171 if (mtd == NULL) {
Tao Bao1107d962015-09-09 17:16:55 -0700172 uiPrintf(state, "%s: no mtd partition named \"%s\"\n",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700173 name, location);
174 result = strdup("");
175 goto done;
176 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700177 if (mtd_mount_partition(mtd, mount_point, fs_type, 0 /* rw */) != 0) {
Michael Runge5ddf4292014-10-24 14:14:41 -0700178 uiPrintf(state, "mtd mount of %s failed: %s\n",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700179 location, strerror(errno));
180 result = strdup("");
181 goto done;
182 }
183 result = mount_point;
184 } else {
Doug Zongker3d177d02010-07-01 09:18:44 -0700185 if (mount(location, mount_point, fs_type,
Michael Runge168f7772014-10-22 17:05:08 -0700186 MS_NOATIME | MS_NODEV | MS_NODIRATIME,
187 has_mount_options ? mount_options : "") < 0) {
Michael Runge5ddf4292014-10-24 14:14:41 -0700188 uiPrintf(state, "%s: failed to mount %s at %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700189 name, location, mount_point, strerror(errno));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700190 result = strdup("");
191 } else {
192 result = mount_point;
193 }
194 }
195
196done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700197 free(fs_type);
198 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700199 free(location);
200 if (result != mount_point) free(mount_point);
Michael Runge168f7772014-10-22 17:05:08 -0700201 if (has_mount_options) free(mount_options);
Doug Zongker512536a2010-02-17 16:11:44 -0800202 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700203}
204
Doug Zongker8edb00c2009-06-11 17:21:44 -0700205
206// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800207Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700208 char* result = NULL;
209 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700210 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700211 }
212 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700213 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700214 return NULL;
215 }
216 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700217 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700218 goto done;
219 }
220
221 scan_mounted_volumes();
Tao Baoba9a42a2015-06-23 23:23:33 -0700222 {
223 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
224 if (vol == NULL) {
225 result = strdup("");
226 } else {
227 result = mount_point;
228 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700229 }
230
231done:
232 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800233 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700234}
235
236
Doug Zongker512536a2010-02-17 16:11:44 -0800237Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700238 char* result = NULL;
239 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700240 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700241 }
242 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700243 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700244 return NULL;
245 }
246 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700247 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700248 goto done;
249 }
250
251 scan_mounted_volumes();
Tao Baoba9a42a2015-06-23 23:23:33 -0700252 {
253 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
254 if (vol == NULL) {
255 uiPrintf(state, "unmount of %s failed; no such volume\n", mount_point);
256 result = strdup("");
257 } else {
258 int ret = unmount_mounted_volume(vol);
259 if (ret != 0) {
260 uiPrintf(state, "unmount of %s failed (%d): %s\n",
261 mount_point, ret, strerror(errno));
262 }
263 result = mount_point;
Michael Runge5ddf4292014-10-24 14:14:41 -0700264 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700265 }
266
267done:
268 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800269 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700270}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700271
JP Abgrall37aedb32014-06-16 19:07:39 -0700272static int exec_cmd(const char* path, char* const argv[]) {
273 int status;
274 pid_t child;
275 if ((child = vfork()) == 0) {
276 execv(path, argv);
277 _exit(-1);
278 }
279 waitpid(child, &status, 0);
280 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
281 printf("%s failed with status %d\n", path, WEXITSTATUS(status));
282 }
283 return WEXITSTATUS(status);
284}
285
Doug Zongker8edb00c2009-06-11 17:21:44 -0700286
Stephen Smalley779701d2012-02-09 14:13:23 -0500287// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700288//
Stephen Smalley779701d2012-02-09 14:13:23 -0500289// fs_type="yaffs2" partition_type="MTD" location=partition fs_size=<bytes> mount_point=<location>
290// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700291// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
292// if fs_size == 0, then make fs uses the entire partition.
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800293// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700294// 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 -0800295Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700296 char* result = NULL;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400297 if (argc != 5) {
298 return ErrorAbort(state, "%s() expects 5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700299 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700300 char* fs_type;
301 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700302 char* location;
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800303 char* fs_size;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400304 char* mount_point;
Stephen Smalley779701d2012-02-09 14:13:23 -0500305
Stephen Smalley779701d2012-02-09 14:13:23 -0500306 if (ReadArgs(state, argv, 5, &fs_type, &partition_type, &location, &fs_size, &mount_point) < 0) {
307 return NULL;
308 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700309
Doug Zongker3d177d02010-07-01 09:18:44 -0700310 if (strlen(fs_type) == 0) {
311 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
312 goto done;
313 }
314 if (strlen(partition_type) == 0) {
315 ErrorAbort(state, "partition_type argument to %s() can't be empty",
316 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700317 goto done;
318 }
319 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700320 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700321 goto done;
322 }
323
Stephen Smalley516e4e22012-04-03 13:35:11 -0400324 if (strlen(mount_point) == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500325 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
326 goto done;
327 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500328
Doug Zongker3d177d02010-07-01 09:18:44 -0700329 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700330 mtd_scan_partitions();
331 const MtdPartition* mtd = mtd_find_partition_by_name(location);
332 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700333 printf("%s: no mtd partition named \"%s\"",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700334 name, location);
335 result = strdup("");
336 goto done;
337 }
338 MtdWriteContext* ctx = mtd_write_partition(mtd);
339 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700340 printf("%s: can't write \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700341 result = strdup("");
342 goto done;
343 }
344 if (mtd_erase_blocks(ctx, -1) == -1) {
345 mtd_write_close(ctx);
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700346 printf("%s: failed to erase \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700347 result = strdup("");
348 goto done;
349 }
350 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700351 printf("%s: failed to close \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700352 result = strdup("");
353 goto done;
354 }
355 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700356#ifdef USE_EXT4
357 } else if (strcmp(fs_type, "ext4") == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500358 int status = make_ext4fs(location, atoll(fs_size), mount_point, sehandle);
Doug Zongker3d177d02010-07-01 09:18:44 -0700359 if (status != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700360 printf("%s: make_ext4fs failed (%d) on %s",
Doug Zongker3d177d02010-07-01 09:18:44 -0700361 name, status, location);
362 result = strdup("");
363 goto done;
364 }
365 result = location;
JP Abgrall37aedb32014-06-16 19:07:39 -0700366 } else if (strcmp(fs_type, "f2fs") == 0) {
367 char *num_sectors;
368 if (asprintf(&num_sectors, "%lld", atoll(fs_size) / 512) <= 0) {
369 printf("format_volume: failed to create %s command for %s\n", fs_type, location);
370 result = strdup("");
371 goto done;
372 }
373 const char *f2fs_path = "/sbin/mkfs.f2fs";
374 const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", location, num_sectors, NULL};
375 int status = exec_cmd(f2fs_path, (char* const*)f2fs_argv);
376 free(num_sectors);
377 if (status != 0) {
378 printf("%s: mkfs.f2fs failed (%d) on %s",
379 name, status, location);
380 result = strdup("");
381 goto done;
382 }
383 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700384#endif
Doug Zongker9931f7f2009-06-10 14:11:53 -0700385 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700386 printf("%s: unsupported fs_type \"%s\" partition_type \"%s\"",
Doug Zongker3d177d02010-07-01 09:18:44 -0700387 name, fs_type, partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700388 }
389
390done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700391 free(fs_type);
392 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700393 if (result != location) free(location);
Doug Zongker512536a2010-02-17 16:11:44 -0800394 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700395}
396
Michael Rungece7ca712013-11-06 17:42:20 -0800397Value* RenameFn(const char* name, State* state, int argc, Expr* argv[]) {
398 char* result = NULL;
399 if (argc != 2) {
400 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
401 }
402
403 char* src_name;
404 char* dst_name;
405
406 if (ReadArgs(state, argv, 2, &src_name, &dst_name) < 0) {
407 return NULL;
408 }
409 if (strlen(src_name) == 0) {
410 ErrorAbort(state, "src_name argument to %s() can't be empty", name);
411 goto done;
412 }
413 if (strlen(dst_name) == 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700414 ErrorAbort(state, "dst_name argument to %s() can't be empty", name);
Michael Rungece7ca712013-11-06 17:42:20 -0800415 goto done;
416 }
Michael Rungea91ecc52014-07-21 17:40:02 -0700417 if (make_parents(dst_name) != 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700418 ErrorAbort(state, "Creating parent of %s failed, error %s",
Michael Rungea91ecc52014-07-21 17:40:02 -0700419 dst_name, strerror(errno));
Michael Runge2f0ef732014-10-22 14:28:23 -0700420 } else if (access(dst_name, F_OK) == 0 && access(src_name, F_OK) != 0) {
421 // File was already moved
422 result = dst_name;
Michael Rungea91ecc52014-07-21 17:40:02 -0700423 } else if (rename(src_name, dst_name) != 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700424 ErrorAbort(state, "Rename of %s to %s failed, error %s",
Michael Rungece7ca712013-11-06 17:42:20 -0800425 src_name, dst_name, strerror(errno));
426 } else {
427 result = dst_name;
428 }
429
430done:
431 free(src_name);
432 if (result != dst_name) free(dst_name);
433 return StringValue(result);
434}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700435
Doug Zongker512536a2010-02-17 16:11:44 -0800436Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700437 char** paths = reinterpret_cast<char**>(malloc(argc * sizeof(char*)));
438 for (int i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700439 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700440 if (paths[i] == NULL) {
441 int j;
442 for (j = 0; j < i; ++i) {
443 free(paths[j]);
444 }
445 free(paths);
446 return NULL;
447 }
448 }
449
450 bool recursive = (strcmp(name, "delete_recursive") == 0);
451
452 int success = 0;
Tao Baoba9a42a2015-06-23 23:23:33 -0700453 for (int i = 0; i < argc; ++i) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700454 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
455 ++success;
456 free(paths[i]);
457 }
458 free(paths);
459
460 char buffer[10];
461 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800462 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700463}
464
Doug Zongker8edb00c2009-06-11 17:21:44 -0700465
Doug Zongker512536a2010-02-17 16:11:44 -0800466Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700467 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700468 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700469 }
470 char* frac_str;
471 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700472 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700473 return NULL;
474 }
475
476 double frac = strtod(frac_str, NULL);
477 int sec = strtol(sec_str, NULL, 10);
478
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700479 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700480 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
481
Doug Zongker9931f7f2009-06-10 14:11:53 -0700482 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800483 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700484}
485
Doug Zongker512536a2010-02-17 16:11:44 -0800486Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700487 if (argc != 1) {
488 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
489 }
490 char* frac_str;
491 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
492 return NULL;
493 }
494
495 double frac = strtod(frac_str, NULL);
496
497 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
498 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
499
Doug Zongker512536a2010-02-17 16:11:44 -0800500 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700501}
502
Doug Zongker8edb00c2009-06-11 17:21:44 -0700503// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800504Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700505 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700506 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700507 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700508 }
509 char* zip_path;
510 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700511 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700512
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700513 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700514
515 // To create a consistent system image, never use the clock for timestamps.
516 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
517
518 bool success = mzExtractRecursive(za, zip_path, dest_path,
Narayan Kamath9c0f5d62015-02-23 14:09:31 +0000519 &timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500520 NULL, NULL, sehandle);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700521 free(zip_path);
522 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800523 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700524}
525
Doug Zongker8edb00c2009-06-11 17:21:44 -0700526
527// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800528// or
529// package_extract_file(package_path)
530// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800531// function (the char* returned is actually a FileContents*).
532Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700533 int argc, Expr* argv[]) {
Doug Zongker5f875bf2014-08-22 14:53:43 -0700534 if (argc < 1 || argc > 2) {
535 return ErrorAbort(state, "%s() expects 1 or 2 args, got %d",
Doug Zongker6aece332010-02-01 14:40:12 -0800536 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700537 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700538 bool success = false;
Doug Zongker43772d22014-06-09 14:13:19 -0700539
Doug Zongker5f875bf2014-08-22 14:53:43 -0700540 if (argc == 2) {
541 // The two-argument version extracts to a file.
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800542
543 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700544
Doug Zongker6aece332010-02-01 14:40:12 -0800545 char* zip_path;
546 char* dest_path;
Doug Zongker5f875bf2014-08-22 14:53:43 -0700547 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800548
Doug Zongker6aece332010-02-01 14:40:12 -0800549 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
550 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700551 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800552 goto done2;
553 }
554
Tao Baoba9a42a2015-06-23 23:23:33 -0700555 {
556 FILE* f = fopen(dest_path, "wb");
557 if (f == NULL) {
558 printf("%s: can't open %s for write: %s\n",
559 name, dest_path, strerror(errno));
560 goto done2;
561 }
562 success = mzExtractZipEntryToFile(za, entry, fileno(f));
563 fclose(f);
Doug Zongker6aece332010-02-01 14:40:12 -0800564 }
Doug Zongker6aece332010-02-01 14:40:12 -0800565
566 done2:
567 free(zip_path);
568 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800569 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800570 } else {
571 // The one-argument version returns the contents of the file
572 // as the result.
573
574 char* zip_path;
Tao Baoba9a42a2015-06-23 23:23:33 -0700575 Value* v = reinterpret_cast<Value*>(malloc(sizeof(Value)));
Doug Zongker512536a2010-02-17 16:11:44 -0800576 v->type = VAL_BLOB;
577 v->size = -1;
578 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800579
580 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
581
582 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
583 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
584 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700585 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800586 goto done1;
587 }
588
Doug Zongker512536a2010-02-17 16:11:44 -0800589 v->size = mzGetZipEntryUncompLen(entry);
Tao Baoba9a42a2015-06-23 23:23:33 -0700590 v->data = reinterpret_cast<char*>(malloc(v->size));
Doug Zongker512536a2010-02-17 16:11:44 -0800591 if (v->data == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700592 printf("%s: failed to allocate %ld bytes for %s\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800593 name, (long)v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800594 goto done1;
595 }
596
Doug Zongker512536a2010-02-17 16:11:44 -0800597 success = mzExtractZipEntryToBuffer(za, entry,
598 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800599
600 done1:
601 free(zip_path);
602 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800603 free(v->data);
604 v->data = NULL;
605 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800606 }
Doug Zongker512536a2010-02-17 16:11:44 -0800607 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700608 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700609}
610
Doug Zongkera23075f2012-08-06 16:19:09 -0700611// Create all parent directories of name, if necessary.
612static int make_parents(char* name) {
613 char* p;
614 for (p = name + (strlen(name)-1); p > name; --p) {
615 if (*p != '/') continue;
616 *p = '\0';
617 if (make_parents(name) < 0) return -1;
618 int result = mkdir(name, 0700);
Michael Rungea91ecc52014-07-21 17:40:02 -0700619 if (result == 0) printf("created [%s]\n", name);
Doug Zongkera23075f2012-08-06 16:19:09 -0700620 *p = '/';
621 if (result == 0 || errno == EEXIST) {
622 // successfully created or already existed; we're done
623 return 0;
624 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700625 printf("failed to mkdir %s: %s\n", name, strerror(errno));
Doug Zongkera23075f2012-08-06 16:19:09 -0700626 return -1;
627 }
628 }
629 return 0;
630}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700631
Doug Zongker9931f7f2009-06-10 14:11:53 -0700632// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700633// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800634Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700635 if (argc == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700636 return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700637 }
638 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700639 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700640 if (target == NULL) return NULL;
641
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700642 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700643 if (srcs == NULL) {
644 free(target);
645 return NULL;
646 }
647
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700648 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700649 int i;
650 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700651 if (unlink(srcs[i]) < 0) {
652 if (errno != ENOENT) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700653 printf("%s: failed to remove %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700654 name, srcs[i], strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700655 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700656 }
657 }
Doug Zongkera23075f2012-08-06 16:19:09 -0700658 if (make_parents(srcs[i])) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700659 printf("%s: failed to symlink %s to %s: making parents failed\n",
Doug Zongkera23075f2012-08-06 16:19:09 -0700660 name, srcs[i], target);
661 ++bad;
662 }
Doug Zongker60babf82009-09-18 15:11:24 -0700663 if (symlink(target, srcs[i]) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700664 printf("%s: failed to symlink %s to %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700665 name, srcs[i], target, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700666 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700667 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700668 free(srcs[i]);
669 }
670 free(srcs);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700671 if (bad) {
672 return ErrorAbort(state, "%s: some symlinks failed", name);
673 }
Doug Zongker512536a2010-02-17 16:11:44 -0800674 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700675}
676
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700677struct perm_parsed_args {
678 bool has_uid;
679 uid_t uid;
680 bool has_gid;
681 gid_t gid;
682 bool has_mode;
683 mode_t mode;
684 bool has_fmode;
685 mode_t fmode;
686 bool has_dmode;
687 mode_t dmode;
688 bool has_selabel;
689 char* selabel;
690 bool has_capabilities;
691 uint64_t capabilities;
692};
693
Michael Runged4a63422014-10-22 19:48:41 -0700694static struct perm_parsed_args ParsePermArgs(State * state, int argc, char** args) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700695 int i;
696 struct perm_parsed_args parsed;
697 int bad = 0;
698 static int max_warnings = 20;
699
700 memset(&parsed, 0, sizeof(parsed));
701
702 for (i = 1; i < argc; i += 2) {
703 if (strcmp("uid", args[i]) == 0) {
704 int64_t uid;
705 if (sscanf(args[i+1], "%" SCNd64, &uid) == 1) {
706 parsed.uid = uid;
707 parsed.has_uid = true;
708 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700709 uiPrintf(state, "ParsePermArgs: invalid UID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700710 bad++;
711 }
712 continue;
713 }
714 if (strcmp("gid", args[i]) == 0) {
715 int64_t gid;
716 if (sscanf(args[i+1], "%" SCNd64, &gid) == 1) {
717 parsed.gid = gid;
718 parsed.has_gid = true;
719 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700720 uiPrintf(state, "ParsePermArgs: invalid GID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700721 bad++;
722 }
723 continue;
724 }
725 if (strcmp("mode", args[i]) == 0) {
726 int32_t mode;
727 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
728 parsed.mode = mode;
729 parsed.has_mode = true;
730 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700731 uiPrintf(state, "ParsePermArgs: invalid mode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700732 bad++;
733 }
734 continue;
735 }
736 if (strcmp("dmode", args[i]) == 0) {
737 int32_t mode;
738 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
739 parsed.dmode = mode;
740 parsed.has_dmode = true;
741 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700742 uiPrintf(state, "ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700743 bad++;
744 }
745 continue;
746 }
747 if (strcmp("fmode", args[i]) == 0) {
748 int32_t mode;
749 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
750 parsed.fmode = mode;
751 parsed.has_fmode = true;
752 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700753 uiPrintf(state, "ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700754 bad++;
755 }
756 continue;
757 }
758 if (strcmp("capabilities", args[i]) == 0) {
759 int64_t capabilities;
760 if (sscanf(args[i+1], "%" SCNi64, &capabilities) == 1) {
761 parsed.capabilities = capabilities;
762 parsed.has_capabilities = true;
763 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700764 uiPrintf(state, "ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700765 bad++;
766 }
767 continue;
768 }
769 if (strcmp("selabel", args[i]) == 0) {
770 if (args[i+1][0] != '\0') {
771 parsed.selabel = args[i+1];
772 parsed.has_selabel = true;
773 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700774 uiPrintf(state, "ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700775 bad++;
776 }
777 continue;
778 }
779 if (max_warnings != 0) {
780 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i]);
781 max_warnings--;
782 if (max_warnings == 0) {
783 printf("ParsedPermArgs: suppressing further warnings\n");
784 }
785 }
786 }
787 return parsed;
788}
789
790static int ApplyParsedPerms(
Michael Runged4a63422014-10-22 19:48:41 -0700791 State * state,
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700792 const char* filename,
793 const struct stat *statptr,
794 struct perm_parsed_args parsed)
795{
796 int bad = 0;
797
Nick Kralevich68802412014-10-23 20:36:42 -0700798 if (parsed.has_selabel) {
799 if (lsetfilecon(filename, parsed.selabel) != 0) {
800 uiPrintf(state, "ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n",
801 filename, parsed.selabel, strerror(errno));
802 bad++;
803 }
804 }
805
Nick Kraleviche4612512013-09-10 15:34:19 -0700806 /* ignore symlinks */
807 if (S_ISLNK(statptr->st_mode)) {
Nick Kralevich68802412014-10-23 20:36:42 -0700808 return bad;
Nick Kraleviche4612512013-09-10 15:34:19 -0700809 }
810
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700811 if (parsed.has_uid) {
812 if (chown(filename, parsed.uid, -1) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700813 uiPrintf(state, "ApplyParsedPerms: chown of %s to %d failed: %s\n",
814 filename, parsed.uid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700815 bad++;
816 }
817 }
818
819 if (parsed.has_gid) {
820 if (chown(filename, -1, parsed.gid) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700821 uiPrintf(state, "ApplyParsedPerms: chgrp of %s to %d failed: %s\n",
822 filename, parsed.gid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700823 bad++;
824 }
825 }
826
827 if (parsed.has_mode) {
828 if (chmod(filename, parsed.mode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700829 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
830 filename, parsed.mode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700831 bad++;
832 }
833 }
834
835 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
836 if (chmod(filename, parsed.dmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700837 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
838 filename, parsed.dmode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700839 bad++;
840 }
841 }
842
843 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
844 if (chmod(filename, parsed.fmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700845 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700846 filename, parsed.fmode, strerror(errno));
847 bad++;
848 }
849 }
850
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700851 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
852 if (parsed.capabilities == 0) {
853 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
854 // Report failure unless it's ENODATA (attribute not set)
Michael Runged4a63422014-10-22 19:48:41 -0700855 uiPrintf(state, "ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700856 filename, parsed.capabilities, strerror(errno));
857 bad++;
858 }
859 } else {
860 struct vfs_cap_data cap_data;
861 memset(&cap_data, 0, sizeof(cap_data));
862 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
863 cap_data.data[0].permitted = (uint32_t) (parsed.capabilities & 0xffffffff);
864 cap_data.data[0].inheritable = 0;
865 cap_data.data[1].permitted = (uint32_t) (parsed.capabilities >> 32);
866 cap_data.data[1].inheritable = 0;
867 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700868 uiPrintf(state, "ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n",
869 filename, parsed.capabilities, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700870 bad++;
871 }
872 }
873 }
874
875 return bad;
876}
877
878// nftw doesn't allow us to pass along context, so we need to use
879// global variables. *sigh*
880static struct perm_parsed_args recursive_parsed_args;
Michael Runged4a63422014-10-22 19:48:41 -0700881static State* recursive_state;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700882
883static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr,
884 int fileflags, struct FTW *pfwt) {
Michael Runged4a63422014-10-22 19:48:41 -0700885 return ApplyParsedPerms(recursive_state, filename, statptr, recursive_parsed_args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700886}
887
888static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700889 int bad = 0;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700890 struct stat sb;
891 Value* result = NULL;
892
893 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
894
895 if ((argc % 2) != 1) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700896 return ErrorAbort(state, "%s() expects an odd number of arguments, got %d", name, argc);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700897 }
898
899 char** args = ReadVarArgs(state, argc, argv);
900 if (args == NULL) return NULL;
901
902 if (lstat(args[0], &sb) == -1) {
903 result = ErrorAbort(state, "%s: Error on lstat of \"%s\": %s", name, args[0], strerror(errno));
904 goto done;
905 }
906
Tao Baoba9a42a2015-06-23 23:23:33 -0700907 {
908 struct perm_parsed_args parsed = ParsePermArgs(state, argc, args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700909
Tao Baoba9a42a2015-06-23 23:23:33 -0700910 if (recursive) {
911 recursive_parsed_args = parsed;
912 recursive_state = state;
913 bad += nftw(args[0], do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
914 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
915 recursive_state = NULL;
916 } else {
917 bad += ApplyParsedPerms(state, args[0], &sb, parsed);
918 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700919 }
920
921done:
Tao Baoba9a42a2015-06-23 23:23:33 -0700922 for (int i = 0; i < argc; ++i) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700923 free(args[i]);
924 }
925 free(args);
926
927 if (result != NULL) {
928 return result;
929 }
930
931 if (bad > 0) {
932 return ErrorAbort(state, "%s: some changes failed", name);
933 }
934
935 return StringValue(strdup(""));
936}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700937
Doug Zongker512536a2010-02-17 16:11:44 -0800938Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700939 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700940 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700941 }
Tao Baoba9a42a2015-06-23 23:23:33 -0700942 char* key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700943 if (key == NULL) return NULL;
944
945 char value[PROPERTY_VALUE_MAX];
946 property_get(key, value, "");
947 free(key);
948
Doug Zongker512536a2010-02-17 16:11:44 -0800949 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700950}
951
952
Doug Zongker47cace92009-06-18 10:11:50 -0700953// file_getprop(file, key)
954//
955// interprets 'file' as a getprop-style file (key=value pairs, one
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700956// per line. # comment lines,blank lines, lines without '=' ignored),
957// and returns the value for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800958Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700959 char* result = NULL;
960 char* buffer = NULL;
961 char* filename;
962 char* key;
963 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
964 return NULL;
965 }
966
967 struct stat st;
968 if (stat(filename, &st) < 0) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700969 ErrorAbort(state, "%s: failed to stat \"%s\": %s", name, filename, strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -0700970 goto done;
971 }
972
973#define MAX_FILE_GETPROP_SIZE 65536
974
975 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700976 ErrorAbort(state, "%s too large for %s (max %d)", filename, name, MAX_FILE_GETPROP_SIZE);
Doug Zongker47cace92009-06-18 10:11:50 -0700977 goto done;
978 }
979
Tao Baoba9a42a2015-06-23 23:23:33 -0700980 buffer = reinterpret_cast<char*>(malloc(st.st_size+1));
Doug Zongker47cace92009-06-18 10:11:50 -0700981 if (buffer == NULL) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700982 ErrorAbort(state, "%s: failed to alloc %lld bytes", name, (long long)st.st_size+1);
Doug Zongker47cace92009-06-18 10:11:50 -0700983 goto done;
984 }
985
Tao Baoba9a42a2015-06-23 23:23:33 -0700986 FILE* f;
987 f = fopen(filename, "rb");
Doug Zongker47cace92009-06-18 10:11:50 -0700988 if (f == NULL) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700989 ErrorAbort(state, "%s: failed to open %s: %s", name, filename, strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -0700990 goto done;
991 }
992
993 if (fread(buffer, 1, st.st_size, f) != st.st_size) {
Doug Zongkera23075f2012-08-06 16:19:09 -0700994 ErrorAbort(state, "%s: failed to read %lld bytes from %s",
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700995 name, (long long)st.st_size+1, filename);
Doug Zongker47cace92009-06-18 10:11:50 -0700996 fclose(f);
997 goto done;
998 }
999 buffer[st.st_size] = '\0';
1000
1001 fclose(f);
1002
Tao Baoba9a42a2015-06-23 23:23:33 -07001003 char* line;
1004 line = strtok(buffer, "\n");
Doug Zongker47cace92009-06-18 10:11:50 -07001005 do {
1006 // skip whitespace at start of line
1007 while (*line && isspace(*line)) ++line;
1008
1009 // comment or blank line: skip to next line
1010 if (*line == '\0' || *line == '#') continue;
1011
1012 char* equal = strchr(line, '=');
1013 if (equal == NULL) {
Michael Rungeaa1a31e2014-04-25 18:47:18 -07001014 continue;
Doug Zongker47cace92009-06-18 10:11:50 -07001015 }
1016
1017 // trim whitespace between key and '='
1018 char* key_end = equal-1;
1019 while (key_end > line && isspace(*key_end)) --key_end;
1020 key_end[1] = '\0';
1021
1022 // not the key we're looking for
1023 if (strcmp(key, line) != 0) continue;
1024
1025 // skip whitespace after the '=' to the start of the value
1026 char* val_start = equal+1;
1027 while(*val_start && isspace(*val_start)) ++val_start;
1028
1029 // trim trailing whitespace
1030 char* val_end = val_start + strlen(val_start)-1;
1031 while (val_end > val_start && isspace(*val_end)) --val_end;
1032 val_end[1] = '\0';
1033
1034 result = strdup(val_start);
1035 break;
1036
1037 } while ((line = strtok(NULL, "\n")));
1038
1039 if (result == NULL) result = strdup("");
1040
1041 done:
1042 free(filename);
1043 free(key);
1044 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -08001045 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -07001046}
1047
Doug Zongker179b2d92011-04-12 15:49:04 -07001048// write_raw_image(filename_or_blob, partition)
Doug Zongker512536a2010-02-17 16:11:44 -08001049Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001050 char* result = NULL;
1051
Doug Zongker179b2d92011-04-12 15:49:04 -07001052 Value* partition_value;
1053 Value* contents;
1054 if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001055 return NULL;
1056 }
1057
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001058 char* partition = NULL;
Doug Zongker179b2d92011-04-12 15:49:04 -07001059 if (partition_value->type != VAL_STRING) {
1060 ErrorAbort(state, "partition argument to %s must be string", name);
1061 goto done;
1062 }
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001063 partition = partition_value->data;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001064 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001065 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001066 goto done;
1067 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001068 if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001069 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001070 goto done;
1071 }
1072
1073 mtd_scan_partitions();
Tao Baoba9a42a2015-06-23 23:23:33 -07001074 const MtdPartition* mtd;
1075 mtd = mtd_find_partition_by_name(partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001076 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001077 printf("%s: no mtd partition named \"%s\"\n", name, partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001078 result = strdup("");
1079 goto done;
1080 }
1081
Tao Baoba9a42a2015-06-23 23:23:33 -07001082 MtdWriteContext* ctx;
1083 ctx = mtd_write_partition(mtd);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001084 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001085 printf("%s: can't write mtd partition \"%s\"\n",
Doug Zongker8edb00c2009-06-11 17:21:44 -07001086 name, partition);
1087 result = strdup("");
1088 goto done;
1089 }
1090
1091 bool success;
1092
Doug Zongker179b2d92011-04-12 15:49:04 -07001093 if (contents->type == VAL_STRING) {
1094 // we're given a filename as the contents
1095 char* filename = contents->data;
1096 FILE* f = fopen(filename, "rb");
1097 if (f == NULL) {
Tao Baoba9a42a2015-06-23 23:23:33 -07001098 printf("%s: can't open %s: %s\n", name, filename, strerror(errno));
Doug Zongker179b2d92011-04-12 15:49:04 -07001099 result = strdup("");
1100 goto done;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001101 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001102
1103 success = true;
Tao Baoba9a42a2015-06-23 23:23:33 -07001104 char* buffer = reinterpret_cast<char*>(malloc(BUFSIZ));
Doug Zongker179b2d92011-04-12 15:49:04 -07001105 int read;
1106 while (success && (read = fread(buffer, 1, BUFSIZ, f)) > 0) {
1107 int wrote = mtd_write_data(ctx, buffer, read);
1108 success = success && (wrote == read);
1109 }
1110 free(buffer);
1111 fclose(f);
1112 } else {
1113 // we're given a blob as the contents
1114 ssize_t wrote = mtd_write_data(ctx, contents->data, contents->size);
1115 success = (wrote == contents->size);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001116 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001117 if (!success) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001118 printf("mtd_write_data to %s failed: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -07001119 partition, strerror(errno));
1120 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001121
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001122 if (mtd_erase_blocks(ctx, -1) == -1) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001123 printf("%s: error erasing blocks of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001124 }
1125 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001126 printf("%s: error closing write of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001127 }
1128
Doug Zongker179b2d92011-04-12 15:49:04 -07001129 printf("%s %s partition\n",
1130 success ? "wrote" : "failed to write", partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001131
1132 result = success ? partition : strdup("");
1133
1134done:
Doug Zongker179b2d92011-04-12 15:49:04 -07001135 if (result != partition) FreeValue(partition_value);
1136 FreeValue(contents);
Doug Zongker512536a2010-02-17 16:11:44 -08001137 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001138}
1139
Doug Zongker8edb00c2009-06-11 17:21:44 -07001140// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001141Value* ApplyPatchSpaceFn(const char* name, State* state,
1142 int argc, Expr* argv[]) {
1143 char* bytes_str;
1144 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
1145 return NULL;
1146 }
1147
1148 char* endptr;
1149 size_t bytes = strtol(bytes_str, &endptr, 10);
1150 if (bytes == 0 && endptr == bytes_str) {
Tao Baoba9a42a2015-06-23 23:23:33 -07001151 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n", name, bytes_str);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001152 free(bytes_str);
1153 return NULL;
1154 }
1155
1156 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
1157}
1158
Doug Zongker52b40362014-02-10 15:30:30 -08001159// apply_patch(file, size, init_sha1, tgt_sha1, patch)
1160
Doug Zongker512536a2010-02-17 16:11:44 -08001161Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001162 if (argc < 6 || (argc % 2) == 1) {
1163 return ErrorAbort(state, "%s(): expected at least 6 args and an "
1164 "even number, got %d",
1165 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001166 }
1167
Doug Zongkerc4351c72010-02-22 14:46:32 -08001168 char* source_filename;
1169 char* target_filename;
1170 char* target_sha1;
1171 char* target_size_str;
1172 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
1173 &target_sha1, &target_size_str) < 0) {
1174 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001175 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001176
Doug Zongkerc4351c72010-02-22 14:46:32 -08001177 char* endptr;
1178 size_t target_size = strtol(target_size_str, &endptr, 10);
1179 if (target_size == 0 && endptr == target_size_str) {
1180 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count",
1181 name, target_size_str);
1182 free(source_filename);
1183 free(target_filename);
1184 free(target_sha1);
1185 free(target_size_str);
1186 return NULL;
1187 }
1188
1189 int patchcount = (argc-4) / 2;
1190 Value** patches = ReadValueVarArgs(state, argc-4, argv+4);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001191
1192 int i;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001193 for (i = 0; i < patchcount; ++i) {
1194 if (patches[i*2]->type != VAL_STRING) {
1195 ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i);
1196 break;
1197 }
1198 if (patches[i*2+1]->type != VAL_BLOB) {
1199 ErrorAbort(state, "%s(): patch #%d is not blob", name, i);
1200 break;
1201 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001202 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001203 if (i != patchcount) {
1204 for (i = 0; i < patchcount*2; ++i) {
1205 FreeValue(patches[i]);
1206 }
1207 free(patches);
1208 return NULL;
1209 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001210
Tao Baoba9a42a2015-06-23 23:23:33 -07001211 char** patch_sha_str = reinterpret_cast<char**>(malloc(patchcount * sizeof(char*)));
Doug Zongkerc4351c72010-02-22 14:46:32 -08001212 for (i = 0; i < patchcount; ++i) {
1213 patch_sha_str[i] = patches[i*2]->data;
1214 patches[i*2]->data = NULL;
1215 FreeValue(patches[i*2]);
1216 patches[i] = patches[i*2+1];
Doug Zongker8edb00c2009-06-11 17:21:44 -07001217 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001218
1219 int result = applypatch(source_filename, target_filename,
1220 target_sha1, target_size,
Doug Zongkera3ccba62012-08-20 15:28:02 -07001221 patchcount, patch_sha_str, patches, NULL);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001222
1223 for (i = 0; i < patchcount; ++i) {
1224 FreeValue(patches[i]);
1225 }
1226 free(patch_sha_str);
1227 free(patches);
1228
1229 return StringValue(strdup(result == 0 ? "t" : ""));
1230}
1231
1232// apply_patch_check(file, [sha1_1, ...])
1233Value* ApplyPatchCheckFn(const char* name, State* state,
1234 int argc, Expr* argv[]) {
1235 if (argc < 1) {
1236 return ErrorAbort(state, "%s(): expected at least 1 arg, got %d",
1237 name, argc);
1238 }
1239
1240 char* filename;
1241 if (ReadArgs(state, argv, 1, &filename) < 0) {
1242 return NULL;
1243 }
1244
1245 int patchcount = argc-1;
1246 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
1247
1248 int result = applypatch_check(filename, patchcount, sha1s);
1249
1250 int i;
1251 for (i = 0; i < patchcount; ++i) {
1252 free(sha1s[i]);
1253 }
1254 free(sha1s);
1255
1256 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001257}
1258
Tao Bao1107d962015-09-09 17:16:55 -07001259// This is the updater side handler for ui_print() in edify script. Contents
1260// will be sent over to the recovery side for on-screen display.
Doug Zongker512536a2010-02-17 16:11:44 -08001261Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001262 char** args = ReadVarArgs(state, argc, argv);
1263 if (args == NULL) {
1264 return NULL;
1265 }
1266
Tao Bao1107d962015-09-09 17:16:55 -07001267 std::string buffer;
1268 for (int i = 0; i < argc; ++i) {
1269 buffer += args[i];
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001270 free(args[i]);
1271 }
1272 free(args);
Tao Bao1107d962015-09-09 17:16:55 -07001273
1274 buffer += "\n";
Michael Runged4a63422014-10-22 19:48:41 -07001275 uiPrint(state, buffer);
Tao Bao1107d962015-09-09 17:16:55 -07001276 return StringValue(strdup(buffer.c_str()));
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001277}
1278
Doug Zongkerd0181b82011-10-19 10:51:12 -07001279Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
1280 if (argc != 0) {
1281 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1282 }
1283 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
1284 return StringValue(strdup("t"));
1285}
1286
Doug Zongker512536a2010-02-17 16:11:44 -08001287Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001288 if (argc < 1) {
1289 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1290 }
1291 char** args = ReadVarArgs(state, argc, argv);
1292 if (args == NULL) {
1293 return NULL;
1294 }
1295
Tao Baoba9a42a2015-06-23 23:23:33 -07001296 char** args2 = reinterpret_cast<char**>(malloc(sizeof(char*) * (argc+1)));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001297 memcpy(args2, args, sizeof(char*) * argc);
1298 args2[argc] = NULL;
1299
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001300 printf("about to run program [%s] with %d args\n", args2[0], argc);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001301
1302 pid_t child = fork();
1303 if (child == 0) {
1304 execv(args2[0], args2);
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001305 printf("run_program: execv failed: %s\n", strerror(errno));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001306 _exit(1);
1307 }
1308 int status;
1309 waitpid(child, &status, 0);
1310 if (WIFEXITED(status)) {
1311 if (WEXITSTATUS(status) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001312 printf("run_program: child exited with status %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001313 WEXITSTATUS(status));
1314 }
1315 } else if (WIFSIGNALED(status)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001316 printf("run_program: child terminated by signal %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001317 WTERMSIG(status));
1318 }
1319
1320 int i;
1321 for (i = 0; i < argc; ++i) {
1322 free(args[i]);
1323 }
1324 free(args);
1325 free(args2);
1326
1327 char buffer[20];
1328 sprintf(buffer, "%d", status);
1329
Doug Zongker512536a2010-02-17 16:11:44 -08001330 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001331}
1332
Doug Zongker512536a2010-02-17 16:11:44 -08001333// sha1_check(data)
1334// to return the sha1 of the data (given in the format returned by
1335// read_file).
1336//
1337// sha1_check(data, sha1_hex, [sha1_hex, ...])
1338// returns the sha1 of the file if it matches any of the hex
1339// strings passed, or "" if it does not equal any of them.
1340//
1341Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1342 if (argc < 1) {
1343 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1344 }
1345
1346 Value** args = ReadValueVarArgs(state, argc, argv);
1347 if (args == NULL) {
1348 return NULL;
1349 }
1350
1351 if (args[0]->size < 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001352 return StringValue(strdup(""));
1353 }
1354 uint8_t digest[SHA_DIGEST_SIZE];
Doug Zongkerbac7fba2013-04-10 11:32:17 -07001355 SHA_hash(args[0]->data, args[0]->size, digest);
Doug Zongker512536a2010-02-17 16:11:44 -08001356 FreeValue(args[0]);
1357
1358 if (argc == 1) {
1359 return StringValue(PrintSha1(digest));
1360 }
1361
1362 int i;
Tao Baoba9a42a2015-06-23 23:23:33 -07001363 uint8_t* arg_digest = reinterpret_cast<uint8_t*>(malloc(SHA_DIGEST_SIZE));
Doug Zongker512536a2010-02-17 16:11:44 -08001364 for (i = 1; i < argc; ++i) {
1365 if (args[i]->type != VAL_STRING) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001366 printf("%s(): arg %d is not a string; skipping",
Doug Zongker512536a2010-02-17 16:11:44 -08001367 name, i);
1368 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1369 // Warn about bad args and skip them.
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001370 printf("%s(): error parsing \"%s\" as sha-1; skipping",
1371 name, args[i]->data);
Doug Zongker512536a2010-02-17 16:11:44 -08001372 } else if (memcmp(digest, arg_digest, SHA_DIGEST_SIZE) == 0) {
1373 break;
1374 }
1375 FreeValue(args[i]);
1376 }
1377 if (i >= argc) {
1378 // Didn't match any of the hex strings; return false.
1379 return StringValue(strdup(""));
1380 }
1381 // Found a match; free all the remaining arguments and return the
1382 // matched one.
1383 int j;
1384 for (j = i+1; j < argc; ++j) {
1385 FreeValue(args[j]);
1386 }
1387 return args[i];
1388}
1389
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001390// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001391// is actually a FileContents*).
1392Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1393 if (argc != 1) {
1394 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1395 }
1396 char* filename;
1397 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1398
Tao Baoba9a42a2015-06-23 23:23:33 -07001399 Value* v = reinterpret_cast<Value*>(malloc(sizeof(Value)));
Doug Zongker512536a2010-02-17 16:11:44 -08001400 v->type = VAL_BLOB;
1401
1402 FileContents fc;
Doug Zongkera1bc1482014-02-13 15:18:19 -08001403 if (LoadFileContents(filename, &fc) != 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001404 free(filename);
Michael Runge6eed2242013-12-13 17:13:11 -08001405 v->size = -1;
1406 v->data = NULL;
Doug Zongker512536a2010-02-17 16:11:44 -08001407 free(fc.data);
Michael Runge6eed2242013-12-13 17:13:11 -08001408 return v;
Doug Zongker512536a2010-02-17 16:11:44 -08001409 }
1410
1411 v->size = fc.size;
1412 v->data = (char*)fc.data;
1413
1414 free(filename);
1415 return v;
1416}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001417
Doug Zongkerc87bab12013-11-25 13:53:25 -08001418// Immediately reboot the device. Recovery is not finished normally,
1419// so if you reboot into recovery it will re-start applying the
1420// current package (because nothing has cleared the copy of the
1421// arguments stored in the BCB).
1422//
1423// The argument is the partition name passed to the android reboot
1424// property. It can be "recovery" to boot from the recovery
1425// partition, or "" (empty string) to boot from the regular boot
1426// partition.
1427Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) {
1428 if (argc != 2) {
1429 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1430 }
1431
1432 char* filename;
1433 char* property;
1434 if (ReadArgs(state, argv, 2, &filename, &property) < 0) return NULL;
1435
1436 char buffer[80];
1437
1438 // zero out the 'command' field of the bootloader message.
1439 memset(buffer, 0, sizeof(((struct bootloader_message*)0)->command));
1440 FILE* f = fopen(filename, "r+b");
1441 fseek(f, offsetof(struct bootloader_message, command), SEEK_SET);
1442 fwrite(buffer, sizeof(((struct bootloader_message*)0)->command), 1, f);
1443 fclose(f);
1444 free(filename);
1445
1446 strcpy(buffer, "reboot,");
1447 if (property != NULL) {
1448 strncat(buffer, property, sizeof(buffer)-10);
1449 }
1450
1451 property_set(ANDROID_RB_PROPERTY, buffer);
1452
1453 sleep(5);
1454 free(property);
1455 ErrorAbort(state, "%s() failed to reboot", name);
1456 return NULL;
1457}
1458
1459// Store a string value somewhere that future invocations of recovery
1460// can access it. This value is called the "stage" and can be used to
1461// drive packages that need to do reboots in the middle of
1462// installation and keep track of where they are in the multi-stage
1463// install.
1464//
1465// The first argument is the block device for the misc partition
1466// ("/misc" in the fstab), which is where this value is stored. The
1467// second argument is the string to store; it should not exceed 31
1468// bytes.
1469Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
1470 if (argc != 2) {
1471 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1472 }
1473
1474 char* filename;
1475 char* stagestr;
1476 if (ReadArgs(state, argv, 2, &filename, &stagestr) < 0) return NULL;
1477
1478 // Store this value in the misc partition, immediately after the
1479 // bootloader message that the main recovery uses to save its
1480 // arguments in case of the device restarting midway through
1481 // package installation.
1482 FILE* f = fopen(filename, "r+b");
1483 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1484 int to_write = strlen(stagestr)+1;
1485 int max_size = sizeof(((struct bootloader_message*)0)->stage);
1486 if (to_write > max_size) {
1487 to_write = max_size;
1488 stagestr[max_size-1] = 0;
1489 }
1490 fwrite(stagestr, to_write, 1, f);
1491 fclose(f);
1492
1493 free(stagestr);
1494 return StringValue(filename);
1495}
1496
1497// Return the value most recently saved with SetStageFn. The argument
1498// is the block device for the misc partition.
1499Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001500 if (argc != 1) {
Doug Zongkerc87bab12013-11-25 13:53:25 -08001501 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1502 }
1503
1504 char* filename;
1505 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1506
1507 char buffer[sizeof(((struct bootloader_message*)0)->stage)];
1508 FILE* f = fopen(filename, "rb");
1509 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1510 fread(buffer, sizeof(buffer), 1, f);
1511 fclose(f);
1512 buffer[sizeof(buffer)-1] = '\0';
1513
1514 return StringValue(strdup(buffer));
1515}
1516
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001517Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) {
1518 if (argc != 2) {
1519 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1520 }
1521
1522 char* filename;
1523 char* len_str;
1524 if (ReadArgs(state, argv, 2, &filename, &len_str) < 0) return NULL;
1525
1526 size_t len = strtoull(len_str, NULL, 0);
1527 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}