blob: 7a73f28b9b5f09711d85a94e659993ef525fe8a5 [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
Doug Zongkerc87bab12013-11-25 13:53:25 -080037#include "bootloader.h"
38#include "applypatch/applypatch.h"
39#include "cutils/android_reboot.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070040#include "cutils/misc.h"
41#include "cutils/properties.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070042#include "edify/expr.h"
Doug Zongker512536a2010-02-17 16:11:44 -080043#include "mincrypt/sha.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070044#include "minzip/DirUtil.h"
45#include "mtdutils/mounts.h"
46#include "mtdutils/mtdutils.h"
47#include "updater.h"
Doug Zongker512536a2010-02-17 16:11:44 -080048#include "applypatch/applypatch.h"
Dees_Troy512376c2013-09-03 19:39:41 +000049#include "flashutils/flashutils.h"
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -080050#include "install.h"
Ethan Yonker7e1b9862015-03-19 14:10:01 -050051#ifdef HAVE_LIBTUNE2FS
Michael Rungeb278c252014-11-21 00:12:28 -080052#include "tune2fs.h"
Ethan Yonker7e1b9862015-03-19 14:10:01 -050053#endif
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
Michael Runge75480252014-10-22 19:48:41 -070060void uiPrint(State* state, char* buffer) {
61 char* line = strtok(buffer, "\n");
62 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
63 while (line) {
64 fprintf(ui->cmd_pipe, "ui_print %s\n", line);
65 line = strtok(NULL, "\n");
66 }
67 fprintf(ui->cmd_pipe, "ui_print\n");
68}
69
70__attribute__((__format__(printf, 2, 3))) __nonnull((2))
71void uiPrintf(State* state, const char* format, ...) {
72 char error_msg[1024];
73 va_list ap;
74 va_start(ap, format);
75 vsnprintf(error_msg, sizeof(error_msg), format, ap);
76 va_end(ap);
77 uiPrint(state, error_msg);
78}
79
Doug Zongker52b40362014-02-10 15:30:30 -080080// Take a sha-1 digest and return it as a newly-allocated hex string.
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070081char* PrintSha1(const uint8_t* digest) {
Doug Zongker52b40362014-02-10 15:30:30 -080082 char* buffer = malloc(SHA_DIGEST_SIZE*2 + 1);
83 int i;
84 const char* alphabet = "0123456789abcdef";
85 for (i = 0; i < SHA_DIGEST_SIZE; ++i) {
86 buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf];
87 buffer[i*2+1] = alphabet[digest[i] & 0xf];
88 }
89 buffer[i*2] = '\0';
90 return buffer;
91}
92
Doug Zongker3d177d02010-07-01 09:18:44 -070093// mount(fs_type, partition_type, location, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -070094//
Doug Zongker3d177d02010-07-01 09:18:44 -070095// fs_type="yaffs2" partition_type="MTD" location=partition
96// fs_type="ext4" partition_type="EMMC" location=device
Doug Zongker512536a2010-02-17 16:11:44 -080097Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070098 char* result = NULL;
Michael Rungebd6138c2014-10-22 17:05:08 -070099 if (argc != 4 && argc != 5) {
100 return ErrorAbort(state, "%s() expects 4-5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700101 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700102 char* fs_type;
103 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700104 char* location;
105 char* mount_point;
Michael Rungebd6138c2014-10-22 17:05:08 -0700106 char* mount_options;
107 bool has_mount_options;
108 if (argc == 5) {
109 has_mount_options = true;
110 if (ReadArgs(state, argv, 5, &fs_type, &partition_type,
111 &location, &mount_point, &mount_options) < 0) {
112 return NULL;
113 }
114 } else {
115 has_mount_options = false;
116 if (ReadArgs(state, argv, 4, &fs_type, &partition_type,
Doug Zongker3d177d02010-07-01 09:18:44 -0700117 &location, &mount_point) < 0) {
Michael Rungebd6138c2014-10-22 17:05:08 -0700118 return NULL;
119 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700120 }
121
Doug Zongker3d177d02010-07-01 09:18:44 -0700122 if (strlen(fs_type) == 0) {
123 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
124 goto done;
125 }
126 if (strlen(partition_type) == 0) {
127 ErrorAbort(state, "partition_type argument to %s() can't be empty",
128 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700129 goto done;
130 }
131 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700132 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700133 goto done;
134 }
135 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700136 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700137 goto done;
138 }
139
Stephen Smalley779701d2012-02-09 14:13:23 -0500140 char *secontext = NULL;
141
142 if (sehandle) {
143 selabel_lookup(sehandle, &secontext, mount_point, 0755);
144 setfscreatecon(secontext);
145 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500146
Doug Zongker9931f7f2009-06-10 14:11:53 -0700147 mkdir(mount_point, 0755);
148
Stephen Smalley779701d2012-02-09 14:13:23 -0500149 if (secontext) {
150 freecon(secontext);
151 setfscreatecon(NULL);
152 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500153
Doug Zongker3d177d02010-07-01 09:18:44 -0700154 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700155 mtd_scan_partitions();
156 const MtdPartition* mtd;
157 mtd = mtd_find_partition_by_name(location);
158 if (mtd == NULL) {
Michael Rungef15e31e2014-10-24 14:14:41 -0700159 uiPrintf(state, "%s: no mtd partition named \"%s\"",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700160 name, location);
161 result = strdup("");
162 goto done;
163 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700164 if (mtd_mount_partition(mtd, mount_point, fs_type, 0 /* rw */) != 0) {
Michael Rungef15e31e2014-10-24 14:14:41 -0700165 uiPrintf(state, "mtd mount of %s failed: %s\n",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700166 location, strerror(errno));
167 result = strdup("");
168 goto done;
169 }
170 result = mount_point;
171 } else {
Doug Zongker3d177d02010-07-01 09:18:44 -0700172 if (mount(location, mount_point, fs_type,
Michael Rungebd6138c2014-10-22 17:05:08 -0700173 MS_NOATIME | MS_NODEV | MS_NODIRATIME,
174 has_mount_options ? mount_options : "") < 0) {
Michael Rungef15e31e2014-10-24 14:14:41 -0700175 uiPrintf(state, "%s: failed to mount %s at %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700176 name, location, mount_point, strerror(errno));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700177 result = strdup("");
178 } else {
179 result = mount_point;
180 }
181 }
182
183done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700184 free(fs_type);
185 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700186 free(location);
187 if (result != mount_point) free(mount_point);
Michael Rungebd6138c2014-10-22 17:05:08 -0700188 if (has_mount_options) free(mount_options);
Doug Zongker512536a2010-02-17 16:11:44 -0800189 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700190}
191
Doug Zongker8edb00c2009-06-11 17:21:44 -0700192
193// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800194Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700195 char* result = NULL;
196 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700197 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700198 }
199 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700200 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700201 return NULL;
202 }
203 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700204 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700205 goto done;
206 }
207
208 scan_mounted_volumes();
209 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
210 if (vol == NULL) {
211 result = strdup("");
212 } else {
213 result = mount_point;
214 }
215
216done:
217 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800218 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700219}
220
221
Doug Zongker512536a2010-02-17 16:11:44 -0800222Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700223 char* result = NULL;
224 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700225 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700226 }
227 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700228 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700229 return NULL;
230 }
231 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700232 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700233 goto done;
234 }
235
236 scan_mounted_volumes();
237 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
238 if (vol == NULL) {
Michael Rungef15e31e2014-10-24 14:14:41 -0700239 uiPrintf(state, "unmount of %s failed; no such volume\n", mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700240 result = strdup("");
241 } else {
Michael Rungef15e31e2014-10-24 14:14:41 -0700242 int ret = unmount_mounted_volume(vol);
243 if (ret != 0) {
244 uiPrintf(state, "unmount of %s failed (%d): %s\n",
245 mount_point, ret, strerror(errno));
246 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700247 result = mount_point;
248 }
249
250done:
251 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800252 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700253}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700254
JP Abgrall37aedb32014-06-16 19:07:39 -0700255static int exec_cmd(const char* path, char* const argv[]) {
256 int status;
257 pid_t child;
258 if ((child = vfork()) == 0) {
259 execv(path, argv);
260 _exit(-1);
261 }
262 waitpid(child, &status, 0);
263 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
264 printf("%s failed with status %d\n", path, WEXITSTATUS(status));
265 }
266 return WEXITSTATUS(status);
267}
268
Doug Zongker8edb00c2009-06-11 17:21:44 -0700269
Stephen Smalley779701d2012-02-09 14:13:23 -0500270// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700271//
Stephen Smalley779701d2012-02-09 14:13:23 -0500272// fs_type="yaffs2" partition_type="MTD" location=partition fs_size=<bytes> mount_point=<location>
273// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700274// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
275// if fs_size == 0, then make fs uses the entire partition.
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800276// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700277// 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 -0800278Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700279 char* result = NULL;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400280 if (argc != 5) {
281 return ErrorAbort(state, "%s() expects 5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700282 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700283 char* fs_type;
284 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700285 char* location;
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800286 char* fs_size;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400287 char* mount_point;
Stephen Smalley779701d2012-02-09 14:13:23 -0500288
Stephen Smalley779701d2012-02-09 14:13:23 -0500289 if (ReadArgs(state, argv, 5, &fs_type, &partition_type, &location, &fs_size, &mount_point) < 0) {
290 return NULL;
291 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700292
Doug Zongker3d177d02010-07-01 09:18:44 -0700293 if (strlen(fs_type) == 0) {
294 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
295 goto done;
296 }
297 if (strlen(partition_type) == 0) {
298 ErrorAbort(state, "partition_type argument to %s() can't be empty",
299 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700300 goto done;
301 }
302 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700303 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700304 goto done;
305 }
306
Stephen Smalley516e4e22012-04-03 13:35:11 -0400307 if (strlen(mount_point) == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500308 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
309 goto done;
310 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500311
Doug Zongker3d177d02010-07-01 09:18:44 -0700312 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700313 mtd_scan_partitions();
314 const MtdPartition* mtd = mtd_find_partition_by_name(location);
315 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700316 printf("%s: no mtd partition named \"%s\"",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700317 name, location);
318 result = strdup("");
319 goto done;
320 }
321 MtdWriteContext* ctx = mtd_write_partition(mtd);
322 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700323 printf("%s: can't write \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700324 result = strdup("");
325 goto done;
326 }
327 if (mtd_erase_blocks(ctx, -1) == -1) {
328 mtd_write_close(ctx);
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700329 printf("%s: failed to erase \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700330 result = strdup("");
331 goto done;
332 }
333 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700334 printf("%s: failed to close \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700335 result = strdup("");
336 goto done;
337 }
338 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700339#ifdef USE_EXT4
340 } else if (strcmp(fs_type, "ext4") == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500341 int status = make_ext4fs(location, atoll(fs_size), mount_point, sehandle);
Doug Zongker3d177d02010-07-01 09:18:44 -0700342 if (status != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700343 printf("%s: make_ext4fs failed (%d) on %s",
Doug Zongker3d177d02010-07-01 09:18:44 -0700344 name, status, location);
345 result = strdup("");
346 goto done;
347 }
348 result = location;
JP Abgrall37aedb32014-06-16 19:07:39 -0700349 } else if (strcmp(fs_type, "f2fs") == 0) {
350 char *num_sectors;
351 if (asprintf(&num_sectors, "%lld", atoll(fs_size) / 512) <= 0) {
352 printf("format_volume: failed to create %s command for %s\n", fs_type, location);
353 result = strdup("");
354 goto done;
355 }
356 const char *f2fs_path = "/sbin/mkfs.f2fs";
357 const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", location, num_sectors, NULL};
358 int status = exec_cmd(f2fs_path, (char* const*)f2fs_argv);
359 free(num_sectors);
360 if (status != 0) {
361 printf("%s: mkfs.f2fs failed (%d) on %s",
362 name, status, location);
363 result = strdup("");
364 goto done;
365 }
366 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700367#endif
Doug Zongker9931f7f2009-06-10 14:11:53 -0700368 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700369 printf("%s: unsupported fs_type \"%s\" partition_type \"%s\"",
Doug Zongker3d177d02010-07-01 09:18:44 -0700370 name, fs_type, partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700371 }
372
373done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700374 free(fs_type);
375 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700376 if (result != location) free(location);
Doug Zongker512536a2010-02-17 16:11:44 -0800377 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700378}
379
Michael Rungece7ca712013-11-06 17:42:20 -0800380Value* RenameFn(const char* name, State* state, int argc, Expr* argv[]) {
381 char* result = NULL;
382 if (argc != 2) {
383 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
384 }
385
386 char* src_name;
387 char* dst_name;
388
389 if (ReadArgs(state, argv, 2, &src_name, &dst_name) < 0) {
390 return NULL;
391 }
392 if (strlen(src_name) == 0) {
393 ErrorAbort(state, "src_name argument to %s() can't be empty", name);
394 goto done;
395 }
396 if (strlen(dst_name) == 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700397 ErrorAbort(state, "dst_name argument to %s() can't be empty", name);
Michael Rungece7ca712013-11-06 17:42:20 -0800398 goto done;
399 }
Michael Rungea91ecc52014-07-21 17:40:02 -0700400 if (make_parents(dst_name) != 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700401 ErrorAbort(state, "Creating parent of %s failed, error %s",
Michael Rungea91ecc52014-07-21 17:40:02 -0700402 dst_name, strerror(errno));
Michael Runged5b17272014-10-22 14:28:23 -0700403 } else if (access(dst_name, F_OK) == 0 && access(src_name, F_OK) != 0) {
404 // File was already moved
405 result = dst_name;
Michael Rungea91ecc52014-07-21 17:40:02 -0700406 } else if (rename(src_name, dst_name) != 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700407 ErrorAbort(state, "Rename of %s to %s failed, error %s",
Michael Rungece7ca712013-11-06 17:42:20 -0800408 src_name, dst_name, strerror(errno));
409 } else {
410 result = dst_name;
411 }
412
413done:
414 free(src_name);
415 if (result != dst_name) free(dst_name);
416 return StringValue(result);
417}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700418
Doug Zongker512536a2010-02-17 16:11:44 -0800419Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700420 char** paths = malloc(argc * sizeof(char*));
421 int i;
422 for (i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700423 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700424 if (paths[i] == NULL) {
425 int j;
426 for (j = 0; j < i; ++i) {
427 free(paths[j]);
428 }
429 free(paths);
430 return NULL;
431 }
432 }
433
434 bool recursive = (strcmp(name, "delete_recursive") == 0);
435
436 int success = 0;
437 for (i = 0; i < argc; ++i) {
438 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
439 ++success;
440 free(paths[i]);
441 }
442 free(paths);
443
444 char buffer[10];
445 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800446 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700447}
448
Doug Zongker8edb00c2009-06-11 17:21:44 -0700449
Doug Zongker512536a2010-02-17 16:11:44 -0800450Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700451 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700452 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700453 }
454 char* frac_str;
455 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700456 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700457 return NULL;
458 }
459
460 double frac = strtod(frac_str, NULL);
461 int sec = strtol(sec_str, NULL, 10);
462
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700463 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700464 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
465
Doug Zongker9931f7f2009-06-10 14:11:53 -0700466 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800467 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700468}
469
Doug Zongker512536a2010-02-17 16:11:44 -0800470Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700471 if (argc != 1) {
472 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
473 }
474 char* frac_str;
475 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
476 return NULL;
477 }
478
479 double frac = strtod(frac_str, NULL);
480
481 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
482 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
483
Doug Zongker512536a2010-02-17 16:11:44 -0800484 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700485}
486
Doug Zongker8edb00c2009-06-11 17:21:44 -0700487// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800488Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700489 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700490 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700491 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700492 }
493 char* zip_path;
494 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700495 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700496
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700497 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700498
499 // To create a consistent system image, never use the clock for timestamps.
500 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
501
502 bool success = mzExtractRecursive(za, zip_path, dest_path,
503 MZ_EXTRACT_FILES_ONLY, &timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500504 NULL, NULL, sehandle);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700505 free(zip_path);
506 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800507 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700508}
509
Doug Zongker8edb00c2009-06-11 17:21:44 -0700510
511// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800512// or
513// package_extract_file(package_path)
514// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800515// function (the char* returned is actually a FileContents*).
516Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700517 int argc, Expr* argv[]) {
Doug Zongker5f875bf2014-08-22 14:53:43 -0700518 if (argc < 1 || argc > 2) {
Doug Zongker6aece332010-02-01 14:40:12 -0800519 return ErrorAbort(state, "%s() expects 1 or 2 args, got %d",
520 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700521 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700522 bool success = false;
Doug Zongker43772d22014-06-09 14:13:19 -0700523
524 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker43772d22014-06-09 14:13:19 -0700525
Doug Zongker6aece332010-02-01 14:40:12 -0800526 if (argc == 2) {
527 // The two-argument version extracts to a file.
Doug Zongker8edb00c2009-06-11 17:21:44 -0700528
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800529 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700530
Doug Zongker6aece332010-02-01 14:40:12 -0800531 char* zip_path;
532 char* dest_path;
533 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
534
Doug Zongker6aece332010-02-01 14:40:12 -0800535 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
536 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700537 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800538 goto done2;
539 }
540
541 FILE* f = fopen(dest_path, "wb");
542 if (f == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700543 printf("%s: can't open %s for write: %s\n",
Doug Zongker6aece332010-02-01 14:40:12 -0800544 name, dest_path, strerror(errno));
545 goto done2;
546 }
547 success = mzExtractZipEntryToFile(za, entry, fileno(f));
548 fclose(f);
549
550 done2:
551 free(zip_path);
552 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800553 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800554 } else {
555 // The one-argument version returns the contents of the file
556 // as the result.
557
558 char* zip_path;
Doug Zongker512536a2010-02-17 16:11:44 -0800559 Value* v = malloc(sizeof(Value));
560 v->type = VAL_BLOB;
561 v->size = -1;
562 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800563
564 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
565
566 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
567 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
568 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700569 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800570 goto done1;
571 }
572
Doug Zongker512536a2010-02-17 16:11:44 -0800573 v->size = mzGetZipEntryUncompLen(entry);
574 v->data = malloc(v->size);
575 if (v->data == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700576 printf("%s: failed to allocate %ld bytes for %s\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800577 name, (long)v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800578 goto done1;
579 }
580
Doug Zongker512536a2010-02-17 16:11:44 -0800581 success = mzExtractZipEntryToBuffer(za, entry,
582 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800583
584 done1:
585 free(zip_path);
586 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800587 free(v->data);
588 v->data = NULL;
589 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800590 }
Doug Zongker512536a2010-02-17 16:11:44 -0800591 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700592 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700593}
594
Doug Zongkera23075f2012-08-06 16:19:09 -0700595// Create all parent directories of name, if necessary.
596static int make_parents(char* name) {
597 char* p;
598 for (p = name + (strlen(name)-1); p > name; --p) {
599 if (*p != '/') continue;
600 *p = '\0';
601 if (make_parents(name) < 0) return -1;
602 int result = mkdir(name, 0700);
Michael Rungea91ecc52014-07-21 17:40:02 -0700603 if (result == 0) printf("created [%s]\n", name);
Doug Zongkera23075f2012-08-06 16:19:09 -0700604 *p = '/';
605 if (result == 0 || errno == EEXIST) {
606 // successfully created or already existed; we're done
607 return 0;
608 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700609 printf("failed to mkdir %s: %s\n", name, strerror(errno));
Doug Zongkera23075f2012-08-06 16:19:09 -0700610 return -1;
611 }
612 }
613 return 0;
614}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700615
Doug Zongker9931f7f2009-06-10 14:11:53 -0700616// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700617// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800618Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700619 if (argc == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700620 return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700621 }
622 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700623 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700624 if (target == NULL) return NULL;
625
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700626 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700627 if (srcs == NULL) {
628 free(target);
629 return NULL;
630 }
631
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700632 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700633 int i;
634 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700635 if (unlink(srcs[i]) < 0) {
636 if (errno != ENOENT) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700637 printf("%s: failed to remove %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700638 name, srcs[i], strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700639 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700640 }
641 }
Doug Zongkera23075f2012-08-06 16:19:09 -0700642 if (make_parents(srcs[i])) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700643 printf("%s: failed to symlink %s to %s: making parents failed\n",
Doug Zongkera23075f2012-08-06 16:19:09 -0700644 name, srcs[i], target);
645 ++bad;
646 }
Doug Zongker60babf82009-09-18 15:11:24 -0700647 if (symlink(target, srcs[i]) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700648 printf("%s: failed to symlink %s to %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700649 name, srcs[i], target, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700650 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700651 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700652 free(srcs[i]);
653 }
654 free(srcs);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700655 if (bad) {
656 return ErrorAbort(state, "%s: some symlinks failed", name);
657 }
Doug Zongker512536a2010-02-17 16:11:44 -0800658 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700659}
660
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700661struct perm_parsed_args {
662 bool has_uid;
663 uid_t uid;
664 bool has_gid;
665 gid_t gid;
666 bool has_mode;
667 mode_t mode;
668 bool has_fmode;
669 mode_t fmode;
670 bool has_dmode;
671 mode_t dmode;
672 bool has_selabel;
673 char* selabel;
674 bool has_capabilities;
675 uint64_t capabilities;
676};
677
Michael Runge75480252014-10-22 19:48:41 -0700678static struct perm_parsed_args ParsePermArgs(State * state, int argc, char** args) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700679 int i;
680 struct perm_parsed_args parsed;
681 int bad = 0;
682 static int max_warnings = 20;
683
684 memset(&parsed, 0, sizeof(parsed));
685
686 for (i = 1; i < argc; i += 2) {
687 if (strcmp("uid", args[i]) == 0) {
688 int64_t uid;
689 if (sscanf(args[i+1], "%" SCNd64, &uid) == 1) {
690 parsed.uid = uid;
691 parsed.has_uid = true;
692 } else {
Michael Runge75480252014-10-22 19:48:41 -0700693 uiPrintf(state, "ParsePermArgs: invalid UID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700694 bad++;
695 }
696 continue;
697 }
698 if (strcmp("gid", args[i]) == 0) {
699 int64_t gid;
700 if (sscanf(args[i+1], "%" SCNd64, &gid) == 1) {
701 parsed.gid = gid;
702 parsed.has_gid = true;
703 } else {
Michael Runge75480252014-10-22 19:48:41 -0700704 uiPrintf(state, "ParsePermArgs: invalid GID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700705 bad++;
706 }
707 continue;
708 }
709 if (strcmp("mode", args[i]) == 0) {
710 int32_t mode;
711 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
712 parsed.mode = mode;
713 parsed.has_mode = true;
714 } else {
Michael Runge75480252014-10-22 19:48:41 -0700715 uiPrintf(state, "ParsePermArgs: invalid mode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700716 bad++;
717 }
718 continue;
719 }
720 if (strcmp("dmode", args[i]) == 0) {
721 int32_t mode;
722 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
723 parsed.dmode = mode;
724 parsed.has_dmode = true;
725 } else {
Michael Runge75480252014-10-22 19:48:41 -0700726 uiPrintf(state, "ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700727 bad++;
728 }
729 continue;
730 }
731 if (strcmp("fmode", args[i]) == 0) {
732 int32_t mode;
733 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
734 parsed.fmode = mode;
735 parsed.has_fmode = true;
736 } else {
Michael Runge75480252014-10-22 19:48:41 -0700737 uiPrintf(state, "ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700738 bad++;
739 }
740 continue;
741 }
742 if (strcmp("capabilities", args[i]) == 0) {
743 int64_t capabilities;
744 if (sscanf(args[i+1], "%" SCNi64, &capabilities) == 1) {
745 parsed.capabilities = capabilities;
746 parsed.has_capabilities = true;
747 } else {
Michael Runge75480252014-10-22 19:48:41 -0700748 uiPrintf(state, "ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700749 bad++;
750 }
751 continue;
752 }
753 if (strcmp("selabel", args[i]) == 0) {
754 if (args[i+1][0] != '\0') {
755 parsed.selabel = args[i+1];
756 parsed.has_selabel = true;
757 } else {
Michael Runge75480252014-10-22 19:48:41 -0700758 uiPrintf(state, "ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700759 bad++;
760 }
761 continue;
762 }
763 if (max_warnings != 0) {
764 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i]);
765 max_warnings--;
766 if (max_warnings == 0) {
767 printf("ParsedPermArgs: suppressing further warnings\n");
768 }
769 }
770 }
771 return parsed;
772}
773
774static int ApplyParsedPerms(
Michael Runge75480252014-10-22 19:48:41 -0700775 State * state,
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700776 const char* filename,
777 const struct stat *statptr,
778 struct perm_parsed_args parsed)
779{
780 int bad = 0;
781
Nick Kralevich6a821fe2014-10-23 20:36:42 -0700782 if (parsed.has_selabel) {
783 if (lsetfilecon(filename, parsed.selabel) != 0) {
784 uiPrintf(state, "ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n",
785 filename, parsed.selabel, strerror(errno));
786 bad++;
787 }
788 }
789
Nick Kraleviche4612512013-09-10 15:34:19 -0700790 /* ignore symlinks */
791 if (S_ISLNK(statptr->st_mode)) {
Nick Kralevich6a821fe2014-10-23 20:36:42 -0700792 return bad;
Nick Kraleviche4612512013-09-10 15:34:19 -0700793 }
794
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700795 if (parsed.has_uid) {
796 if (chown(filename, parsed.uid, -1) < 0) {
Michael Runge75480252014-10-22 19:48:41 -0700797 uiPrintf(state, "ApplyParsedPerms: chown of %s to %d failed: %s\n",
798 filename, parsed.uid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700799 bad++;
800 }
801 }
802
803 if (parsed.has_gid) {
804 if (chown(filename, -1, parsed.gid) < 0) {
Michael Runge75480252014-10-22 19:48:41 -0700805 uiPrintf(state, "ApplyParsedPerms: chgrp of %s to %d failed: %s\n",
806 filename, parsed.gid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700807 bad++;
808 }
809 }
810
811 if (parsed.has_mode) {
812 if (chmod(filename, parsed.mode) < 0) {
Michael Runge75480252014-10-22 19:48:41 -0700813 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
814 filename, parsed.mode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700815 bad++;
816 }
817 }
818
819 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
820 if (chmod(filename, parsed.dmode) < 0) {
Michael Runge75480252014-10-22 19:48:41 -0700821 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
822 filename, parsed.dmode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700823 bad++;
824 }
825 }
826
827 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
828 if (chmod(filename, parsed.fmode) < 0) {
Michael Runge75480252014-10-22 19:48:41 -0700829 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700830 filename, parsed.fmode, strerror(errno));
831 bad++;
832 }
833 }
834
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700835 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
836 if (parsed.capabilities == 0) {
837 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
838 // Report failure unless it's ENODATA (attribute not set)
Michael Runge75480252014-10-22 19:48:41 -0700839 uiPrintf(state, "ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700840 filename, parsed.capabilities, strerror(errno));
841 bad++;
842 }
843 } else {
844 struct vfs_cap_data cap_data;
845 memset(&cap_data, 0, sizeof(cap_data));
846 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
847 cap_data.data[0].permitted = (uint32_t) (parsed.capabilities & 0xffffffff);
848 cap_data.data[0].inheritable = 0;
849 cap_data.data[1].permitted = (uint32_t) (parsed.capabilities >> 32);
850 cap_data.data[1].inheritable = 0;
851 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
Michael Runge75480252014-10-22 19:48:41 -0700852 uiPrintf(state, "ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n",
853 filename, parsed.capabilities, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700854 bad++;
855 }
856 }
857 }
858
859 return bad;
860}
861
862// nftw doesn't allow us to pass along context, so we need to use
863// global variables. *sigh*
864static struct perm_parsed_args recursive_parsed_args;
Michael Runge75480252014-10-22 19:48:41 -0700865static State* recursive_state;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700866
867static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr,
868 int fileflags, struct FTW *pfwt) {
Michael Runge75480252014-10-22 19:48:41 -0700869 return ApplyParsedPerms(recursive_state, filename, statptr, recursive_parsed_args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700870}
871
872static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
873 int i;
874 int bad = 0;
875 static int nwarnings = 0;
876 struct stat sb;
877 Value* result = NULL;
878
879 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
880
881 if ((argc % 2) != 1) {
882 return ErrorAbort(state, "%s() expects an odd number of arguments, got %d",
883 name, argc);
884 }
885
886 char** args = ReadVarArgs(state, argc, argv);
887 if (args == NULL) return NULL;
888
889 if (lstat(args[0], &sb) == -1) {
890 result = ErrorAbort(state, "%s: Error on lstat of \"%s\": %s", name, args[0], strerror(errno));
891 goto done;
892 }
893
Michael Runge75480252014-10-22 19:48:41 -0700894 struct perm_parsed_args parsed = ParsePermArgs(state, argc, args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700895
896 if (recursive) {
897 recursive_parsed_args = parsed;
Michael Runge75480252014-10-22 19:48:41 -0700898 recursive_state = state;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700899 bad += nftw(args[0], do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
900 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
Michael Runge75480252014-10-22 19:48:41 -0700901 recursive_state = NULL;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700902 } else {
Michael Runge75480252014-10-22 19:48:41 -0700903 bad += ApplyParsedPerms(state, args[0], &sb, parsed);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700904 }
905
906done:
907 for (i = 0; i < argc; ++i) {
908 free(args[i]);
909 }
910 free(args);
911
912 if (result != NULL) {
913 return result;
914 }
915
916 if (bad > 0) {
917 return ErrorAbort(state, "%s: some changes failed", name);
918 }
919
920 return StringValue(strdup(""));
921}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700922
Doug Zongker512536a2010-02-17 16:11:44 -0800923Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700924 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700925 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700926 }
927 char* key;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700928 key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700929 if (key == NULL) return NULL;
930
931 char value[PROPERTY_VALUE_MAX];
932 property_get(key, value, "");
933 free(key);
934
Doug Zongker512536a2010-02-17 16:11:44 -0800935 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700936}
937
938
Doug Zongker47cace92009-06-18 10:11:50 -0700939// file_getprop(file, key)
940//
941// interprets 'file' as a getprop-style file (key=value pairs, one
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700942// per line. # comment lines,blank lines, lines without '=' ignored),
943// and returns the value for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800944Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700945 char* result = NULL;
946 char* buffer = NULL;
947 char* filename;
948 char* key;
949 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
950 return NULL;
951 }
952
953 struct stat st;
954 if (stat(filename, &st) < 0) {
955 ErrorAbort(state, "%s: failed to stat \"%s\": %s",
956 name, filename, strerror(errno));
957 goto done;
958 }
959
960#define MAX_FILE_GETPROP_SIZE 65536
961
962 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
963 ErrorAbort(state, "%s too large for %s (max %d)",
964 filename, name, MAX_FILE_GETPROP_SIZE);
965 goto done;
966 }
967
968 buffer = malloc(st.st_size+1);
969 if (buffer == NULL) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700970 ErrorAbort(state, "%s: failed to alloc %lld bytes", name, (long long)st.st_size+1);
Doug Zongker47cace92009-06-18 10:11:50 -0700971 goto done;
972 }
973
974 FILE* f = fopen(filename, "rb");
975 if (f == NULL) {
976 ErrorAbort(state, "%s: failed to open %s: %s",
977 name, filename, strerror(errno));
978 goto done;
979 }
980
981 if (fread(buffer, 1, st.st_size, f) != st.st_size) {
Doug Zongkera23075f2012-08-06 16:19:09 -0700982 ErrorAbort(state, "%s: failed to read %lld bytes from %s",
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700983 name, (long long)st.st_size+1, filename);
Doug Zongker47cace92009-06-18 10:11:50 -0700984 fclose(f);
985 goto done;
986 }
987 buffer[st.st_size] = '\0';
988
989 fclose(f);
990
991 char* line = strtok(buffer, "\n");
992 do {
993 // skip whitespace at start of line
994 while (*line && isspace(*line)) ++line;
995
996 // comment or blank line: skip to next line
997 if (*line == '\0' || *line == '#') continue;
998
999 char* equal = strchr(line, '=');
1000 if (equal == NULL) {
Michael Rungeaa1a31e2014-04-25 18:47:18 -07001001 continue;
Doug Zongker47cace92009-06-18 10:11:50 -07001002 }
1003
1004 // trim whitespace between key and '='
1005 char* key_end = equal-1;
1006 while (key_end > line && isspace(*key_end)) --key_end;
1007 key_end[1] = '\0';
1008
1009 // not the key we're looking for
1010 if (strcmp(key, line) != 0) continue;
1011
1012 // skip whitespace after the '=' to the start of the value
1013 char* val_start = equal+1;
1014 while(*val_start && isspace(*val_start)) ++val_start;
1015
1016 // trim trailing whitespace
1017 char* val_end = val_start + strlen(val_start)-1;
1018 while (val_end > val_start && isspace(*val_end)) --val_end;
1019 val_end[1] = '\0';
1020
1021 result = strdup(val_start);
1022 break;
1023
1024 } while ((line = strtok(NULL, "\n")));
1025
1026 if (result == NULL) result = strdup("");
1027
1028 done:
1029 free(filename);
1030 free(key);
1031 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -08001032 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -07001033}
1034
1035
Doug Zongker8edb00c2009-06-11 17:21:44 -07001036static bool write_raw_image_cb(const unsigned char* data,
1037 int data_len, void* ctx) {
1038 int r = mtd_write_data((MtdWriteContext*)ctx, (const char *)data, data_len);
1039 if (r == data_len) return true;
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001040 printf("%s\n", strerror(errno));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001041 return false;
1042}
1043
Doug Zongker179b2d92011-04-12 15:49:04 -07001044// write_raw_image(filename_or_blob, partition)
Doug Zongker512536a2010-02-17 16:11:44 -08001045Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001046 char* result = NULL;
1047
Doug Zongker179b2d92011-04-12 15:49:04 -07001048 Value* partition_value;
1049 Value* contents;
1050 if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001051 return NULL;
1052 }
1053
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001054 char* partition = NULL;
Doug Zongker179b2d92011-04-12 15:49:04 -07001055 if (partition_value->type != VAL_STRING) {
1056 ErrorAbort(state, "partition argument to %s must be string", name);
1057 goto done;
1058 }
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001059 partition = partition_value->data;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001060 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001061 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001062 goto done;
1063 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001064 if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001065 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001066 goto done;
1067 }
1068
Dees_Troy76a1d412013-04-20 13:54:35 +00001069 char* filename = contents->data;
1070 if (0 == restore_raw_partition(NULL, partition, filename))
1071 result = strdup(partition);
1072 else {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001073 result = strdup("");
1074 goto done;
1075 }
1076
Doug Zongker8edb00c2009-06-11 17:21:44 -07001077done:
Doug Zongker179b2d92011-04-12 15:49:04 -07001078 if (result != partition) FreeValue(partition_value);
1079 FreeValue(contents);
Doug Zongker512536a2010-02-17 16:11:44 -08001080 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001081}
1082
Doug Zongker8edb00c2009-06-11 17:21:44 -07001083// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001084Value* ApplyPatchSpaceFn(const char* name, State* state,
1085 int argc, Expr* argv[]) {
1086 char* bytes_str;
1087 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
1088 return NULL;
1089 }
1090
1091 char* endptr;
1092 size_t bytes = strtol(bytes_str, &endptr, 10);
1093 if (bytes == 0 && endptr == bytes_str) {
1094 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n",
1095 name, bytes_str);
1096 free(bytes_str);
1097 return NULL;
1098 }
1099
1100 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
1101}
1102
Doug Zongker52b40362014-02-10 15:30:30 -08001103// apply_patch(file, size, init_sha1, tgt_sha1, patch)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001104
Doug Zongker512536a2010-02-17 16:11:44 -08001105Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001106 if (argc < 6 || (argc % 2) == 1) {
1107 return ErrorAbort(state, "%s(): expected at least 6 args and an "
1108 "even number, got %d",
1109 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001110 }
1111
Doug Zongkerc4351c72010-02-22 14:46:32 -08001112 char* source_filename;
1113 char* target_filename;
1114 char* target_sha1;
1115 char* target_size_str;
1116 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
1117 &target_sha1, &target_size_str) < 0) {
1118 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001119 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001120
Doug Zongkerc4351c72010-02-22 14:46:32 -08001121 char* endptr;
1122 size_t target_size = strtol(target_size_str, &endptr, 10);
1123 if (target_size == 0 && endptr == target_size_str) {
1124 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count",
1125 name, target_size_str);
1126 free(source_filename);
1127 free(target_filename);
1128 free(target_sha1);
1129 free(target_size_str);
1130 return NULL;
1131 }
1132
1133 int patchcount = (argc-4) / 2;
1134 Value** patches = ReadValueVarArgs(state, argc-4, argv+4);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001135
1136 int i;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001137 for (i = 0; i < patchcount; ++i) {
1138 if (patches[i*2]->type != VAL_STRING) {
1139 ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i);
1140 break;
1141 }
1142 if (patches[i*2+1]->type != VAL_BLOB) {
1143 ErrorAbort(state, "%s(): patch #%d is not blob", name, i);
1144 break;
1145 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001146 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001147 if (i != patchcount) {
1148 for (i = 0; i < patchcount*2; ++i) {
1149 FreeValue(patches[i]);
1150 }
1151 free(patches);
1152 return NULL;
1153 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001154
Doug Zongkerc4351c72010-02-22 14:46:32 -08001155 char** patch_sha_str = malloc(patchcount * sizeof(char*));
1156 for (i = 0; i < patchcount; ++i) {
1157 patch_sha_str[i] = patches[i*2]->data;
1158 patches[i*2]->data = NULL;
1159 FreeValue(patches[i*2]);
1160 patches[i] = patches[i*2+1];
Doug Zongker8edb00c2009-06-11 17:21:44 -07001161 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001162
1163 int result = applypatch(source_filename, target_filename,
1164 target_sha1, target_size,
Doug Zongkera3ccba62012-08-20 15:28:02 -07001165 patchcount, patch_sha_str, patches, NULL);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001166
1167 for (i = 0; i < patchcount; ++i) {
1168 FreeValue(patches[i]);
1169 }
1170 free(patch_sha_str);
1171 free(patches);
1172
1173 return StringValue(strdup(result == 0 ? "t" : ""));
1174}
1175
1176// apply_patch_check(file, [sha1_1, ...])
1177Value* ApplyPatchCheckFn(const char* name, State* state,
1178 int argc, Expr* argv[]) {
1179 if (argc < 1) {
1180 return ErrorAbort(state, "%s(): expected at least 1 arg, got %d",
1181 name, argc);
1182 }
1183
1184 char* filename;
1185 if (ReadArgs(state, argv, 1, &filename) < 0) {
1186 return NULL;
1187 }
1188
1189 int patchcount = argc-1;
1190 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
1191
1192 int result = applypatch_check(filename, patchcount, sha1s);
1193
1194 int i;
1195 for (i = 0; i < patchcount; ++i) {
1196 free(sha1s[i]);
1197 }
1198 free(sha1s);
1199
1200 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001201}
1202
Doug Zongker512536a2010-02-17 16:11:44 -08001203Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001204 char** args = ReadVarArgs(state, argc, argv);
1205 if (args == NULL) {
1206 return NULL;
1207 }
1208
1209 int size = 0;
1210 int i;
1211 for (i = 0; i < argc; ++i) {
1212 size += strlen(args[i]);
1213 }
1214 char* buffer = malloc(size+1);
1215 size = 0;
1216 for (i = 0; i < argc; ++i) {
1217 strcpy(buffer+size, args[i]);
1218 size += strlen(args[i]);
1219 free(args[i]);
1220 }
1221 free(args);
1222 buffer[size] = '\0';
Michael Runge75480252014-10-22 19:48:41 -07001223 uiPrint(state, buffer);
Doug Zongker512536a2010-02-17 16:11:44 -08001224 return StringValue(buffer);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001225}
1226
Doug Zongkerd0181b82011-10-19 10:51:12 -07001227Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
1228 if (argc != 0) {
1229 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1230 }
1231 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
1232 return StringValue(strdup("t"));
1233}
1234
Doug Zongker512536a2010-02-17 16:11:44 -08001235Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001236 if (argc < 1) {
1237 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1238 }
1239 char** args = ReadVarArgs(state, argc, argv);
1240 if (args == NULL) {
1241 return NULL;
1242 }
1243
1244 char** args2 = malloc(sizeof(char*) * (argc+1));
1245 memcpy(args2, args, sizeof(char*) * argc);
1246 args2[argc] = NULL;
1247
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001248 printf("about to run program [%s] with %d args\n", args2[0], argc);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001249
1250 pid_t child = fork();
1251 if (child == 0) {
1252 execv(args2[0], args2);
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001253 printf("run_program: execv failed: %s\n", strerror(errno));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001254 _exit(1);
1255 }
1256 int status;
1257 waitpid(child, &status, 0);
1258 if (WIFEXITED(status)) {
1259 if (WEXITSTATUS(status) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001260 printf("run_program: child exited with status %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001261 WEXITSTATUS(status));
1262 }
1263 } else if (WIFSIGNALED(status)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001264 printf("run_program: child terminated by signal %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001265 WTERMSIG(status));
1266 }
1267
1268 int i;
1269 for (i = 0; i < argc; ++i) {
1270 free(args[i]);
1271 }
1272 free(args);
1273 free(args2);
1274
1275 char buffer[20];
1276 sprintf(buffer, "%d", status);
1277
Doug Zongker512536a2010-02-17 16:11:44 -08001278 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001279}
1280
Doug Zongker512536a2010-02-17 16:11:44 -08001281// sha1_check(data)
1282// to return the sha1 of the data (given in the format returned by
1283// read_file).
1284//
1285// sha1_check(data, sha1_hex, [sha1_hex, ...])
1286// returns the sha1 of the file if it matches any of the hex
1287// strings passed, or "" if it does not equal any of them.
1288//
1289Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1290 if (argc < 1) {
1291 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1292 }
1293
1294 Value** args = ReadValueVarArgs(state, argc, argv);
1295 if (args == NULL) {
1296 return NULL;
1297 }
1298
1299 if (args[0]->size < 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001300 return StringValue(strdup(""));
1301 }
1302 uint8_t digest[SHA_DIGEST_SIZE];
Doug Zongkerbac7fba2013-04-10 11:32:17 -07001303 SHA_hash(args[0]->data, args[0]->size, digest);
Doug Zongker512536a2010-02-17 16:11:44 -08001304 FreeValue(args[0]);
1305
1306 if (argc == 1) {
1307 return StringValue(PrintSha1(digest));
1308 }
1309
1310 int i;
1311 uint8_t* arg_digest = malloc(SHA_DIGEST_SIZE);
1312 for (i = 1; i < argc; ++i) {
1313 if (args[i]->type != VAL_STRING) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001314 printf("%s(): arg %d is not a string; skipping",
Doug Zongker512536a2010-02-17 16:11:44 -08001315 name, i);
1316 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1317 // Warn about bad args and skip them.
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001318 printf("%s(): error parsing \"%s\" as sha-1; skipping",
1319 name, args[i]->data);
Doug Zongker512536a2010-02-17 16:11:44 -08001320 } else if (memcmp(digest, arg_digest, SHA_DIGEST_SIZE) == 0) {
1321 break;
1322 }
1323 FreeValue(args[i]);
1324 }
1325 if (i >= argc) {
1326 // Didn't match any of the hex strings; return false.
1327 return StringValue(strdup(""));
1328 }
1329 // Found a match; free all the remaining arguments and return the
1330 // matched one.
1331 int j;
1332 for (j = i+1; j < argc; ++j) {
1333 FreeValue(args[j]);
1334 }
1335 return args[i];
1336}
1337
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001338// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001339// is actually a FileContents*).
1340Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1341 if (argc != 1) {
1342 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1343 }
1344 char* filename;
1345 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1346
1347 Value* v = malloc(sizeof(Value));
1348 v->type = VAL_BLOB;
1349
1350 FileContents fc;
Doug Zongkera1bc1482014-02-13 15:18:19 -08001351 if (LoadFileContents(filename, &fc) != 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001352 free(filename);
Michael Runge6eed2242013-12-13 17:13:11 -08001353 v->size = -1;
1354 v->data = NULL;
Doug Zongker512536a2010-02-17 16:11:44 -08001355 free(fc.data);
Michael Runge6eed2242013-12-13 17:13:11 -08001356 return v;
Doug Zongker512536a2010-02-17 16:11:44 -08001357 }
1358
1359 v->size = fc.size;
1360 v->data = (char*)fc.data;
1361
1362 free(filename);
1363 return v;
1364}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001365
Doug Zongkerc87bab12013-11-25 13:53:25 -08001366// Immediately reboot the device. Recovery is not finished normally,
1367// so if you reboot into recovery it will re-start applying the
1368// current package (because nothing has cleared the copy of the
1369// arguments stored in the BCB).
1370//
1371// The argument is the partition name passed to the android reboot
1372// property. It can be "recovery" to boot from the recovery
1373// partition, or "" (empty string) to boot from the regular boot
1374// partition.
1375Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) {
1376 if (argc != 2) {
1377 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1378 }
1379
1380 char* filename;
1381 char* property;
1382 if (ReadArgs(state, argv, 2, &filename, &property) < 0) return NULL;
1383
1384 char buffer[80];
1385
1386 // zero out the 'command' field of the bootloader message.
1387 memset(buffer, 0, sizeof(((struct bootloader_message*)0)->command));
1388 FILE* f = fopen(filename, "r+b");
1389 fseek(f, offsetof(struct bootloader_message, command), SEEK_SET);
1390 fwrite(buffer, sizeof(((struct bootloader_message*)0)->command), 1, f);
1391 fclose(f);
1392 free(filename);
1393
1394 strcpy(buffer, "reboot,");
1395 if (property != NULL) {
1396 strncat(buffer, property, sizeof(buffer)-10);
1397 }
1398
1399 property_set(ANDROID_RB_PROPERTY, buffer);
1400
1401 sleep(5);
1402 free(property);
1403 ErrorAbort(state, "%s() failed to reboot", name);
1404 return NULL;
1405}
1406
1407// Store a string value somewhere that future invocations of recovery
1408// can access it. This value is called the "stage" and can be used to
1409// drive packages that need to do reboots in the middle of
1410// installation and keep track of where they are in the multi-stage
1411// install.
1412//
1413// The first argument is the block device for the misc partition
1414// ("/misc" in the fstab), which is where this value is stored. The
1415// second argument is the string to store; it should not exceed 31
1416// bytes.
1417Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
1418 if (argc != 2) {
1419 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1420 }
1421
1422 char* filename;
1423 char* stagestr;
1424 if (ReadArgs(state, argv, 2, &filename, &stagestr) < 0) return NULL;
1425
1426 // Store this value in the misc partition, immediately after the
1427 // bootloader message that the main recovery uses to save its
1428 // arguments in case of the device restarting midway through
1429 // package installation.
1430 FILE* f = fopen(filename, "r+b");
1431 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1432 int to_write = strlen(stagestr)+1;
1433 int max_size = sizeof(((struct bootloader_message*)0)->stage);
1434 if (to_write > max_size) {
1435 to_write = max_size;
1436 stagestr[max_size-1] = 0;
1437 }
1438 fwrite(stagestr, to_write, 1, f);
1439 fclose(f);
1440
1441 free(stagestr);
1442 return StringValue(filename);
1443}
1444
1445// Return the value most recently saved with SetStageFn. The argument
1446// is the block device for the misc partition.
1447Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001448 if (argc != 1) {
Doug Zongkerc87bab12013-11-25 13:53:25 -08001449 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1450 }
1451
1452 char* filename;
1453 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1454
1455 char buffer[sizeof(((struct bootloader_message*)0)->stage)];
1456 FILE* f = fopen(filename, "rb");
1457 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1458 fread(buffer, sizeof(buffer), 1, f);
1459 fclose(f);
1460 buffer[sizeof(buffer)-1] = '\0';
1461
1462 return StringValue(strdup(buffer));
1463}
1464
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001465Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) {
1466 if (argc != 2) {
1467 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1468 }
1469
1470 char* filename;
1471 char* len_str;
1472 if (ReadArgs(state, argv, 2, &filename, &len_str) < 0) return NULL;
1473
1474 size_t len = strtoull(len_str, NULL, 0);
1475 int fd = open(filename, O_WRONLY, 0644);
1476 int success = wipe_block_device(fd, len);
1477
1478 free(filename);
1479 free(len_str);
1480
1481 close(fd);
1482
1483 return StringValue(strdup(success ? "t" : ""));
1484}
1485
Doug Zongkerc704e062014-05-23 08:40:35 -07001486Value* EnableRebootFn(const char* name, State* state, int argc, Expr* argv[]) {
1487 if (argc != 0) {
1488 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1489 }
1490 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
1491 fprintf(ui->cmd_pipe, "enable_reboot\n");
1492 return StringValue(strdup("t"));
1493}
1494
Michael Rungeb278c252014-11-21 00:12:28 -08001495Value* Tune2FsFn(const char* name, State* state, int argc, Expr* argv[]) {
Ethan Yonker7e1b9862015-03-19 14:10:01 -05001496#ifdef HAVE_LIBTUNE2FS
Michael Rungeb278c252014-11-21 00:12:28 -08001497 if (argc == 0) {
1498 return ErrorAbort(state, "%s() expects args, got %d", name, argc);
1499 }
1500
1501 char** args = ReadVarArgs(state, argc, argv);
1502 if (args == NULL) {
1503 return ErrorAbort(state, "%s() could not read args", name);
1504 }
1505
1506 int i;
1507 char** args2 = malloc(sizeof(char*) * (argc+1));
1508 // Tune2fs expects the program name as its args[0]
1509 args2[0] = strdup(name);
1510 for (i = 0; i < argc; ++i) {
1511 args2[i + 1] = args[i];
1512 }
1513 int result = tune2fs_main(argc + 1, args2);
1514 for (i = 0; i < argc; ++i) {
1515 free(args[i]);
1516 }
1517 free(args);
1518
1519 free(args2[0]);
1520 free(args2);
1521 if (result != 0) {
1522 return ErrorAbort(state, "%s() returned error code %d", name, result);
1523 }
1524 return StringValue(strdup("t"));
Ethan Yonker7e1b9862015-03-19 14:10:01 -05001525#else
1526 return ErrorAbort(state, "%s() support not present, no libtune2fs", name);
1527#endif // HAVE_LIBTUNE2FS
Michael Rungeb278c252014-11-21 00:12:28 -08001528}
1529
Doug Zongker9931f7f2009-06-10 14:11:53 -07001530void RegisterInstallFunctions() {
1531 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001532 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001533 RegisterFunction("unmount", UnmountFn);
1534 RegisterFunction("format", FormatFn);
1535 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001536 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001537 RegisterFunction("delete", DeleteFn);
1538 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001539 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1540 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001541 RegisterFunction("symlink", SymlinkFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001542
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001543 // Usage:
1544 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1545 // Example:
1546 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1547 RegisterFunction("set_metadata", SetMetadataFn);
1548
1549 // Usage:
1550 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1551 // Example:
1552 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1553 RegisterFunction("set_metadata_recursive", SetMetadataFn);
1554
Doug Zongker8edb00c2009-06-11 17:21:44 -07001555 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001556 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001557 RegisterFunction("write_raw_image", WriteRawImageFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001558
1559 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001560 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1561 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001562
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001563 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001564
Doug Zongker512536a2010-02-17 16:11:44 -08001565 RegisterFunction("read_file", ReadFileFn);
1566 RegisterFunction("sha1_check", Sha1CheckFn);
Michael Rungece7ca712013-11-06 17:42:20 -08001567 RegisterFunction("rename", RenameFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001568
Doug Zongkerd0181b82011-10-19 10:51:12 -07001569 RegisterFunction("wipe_cache", WipeCacheFn);
1570
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001571 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001572
1573 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001574
1575 RegisterFunction("reboot_now", RebootNowFn);
1576 RegisterFunction("get_stage", GetStageFn);
1577 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -07001578
1579 RegisterFunction("enable_reboot", EnableRebootFn);
Michael Rungeb278c252014-11-21 00:12:28 -08001580 RegisterFunction("tune2fs", Tune2FsFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001581}