blob: 17ea4c2b5d8af657da730d03662cda9a66aa41c6 [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 Zongkerc9d6e4f2014-02-24 16:02:50 -080048#include "install.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070049
Doug Zongker3d177d02010-07-01 09:18:44 -070050#ifdef USE_EXT4
51#include "make_ext4fs.h"
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -080052#include "wipe.h"
Doug Zongker3d177d02010-07-01 09:18:44 -070053#endif
54
Michael Runge75480252014-10-22 19:48:41 -070055void uiPrint(State* state, char* buffer) {
56 char* line = strtok(buffer, "\n");
57 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
58 while (line) {
59 fprintf(ui->cmd_pipe, "ui_print %s\n", line);
60 line = strtok(NULL, "\n");
61 }
62 fprintf(ui->cmd_pipe, "ui_print\n");
63}
64
65__attribute__((__format__(printf, 2, 3))) __nonnull((2))
66void uiPrintf(State* state, const char* format, ...) {
67 char error_msg[1024];
68 va_list ap;
69 va_start(ap, format);
70 vsnprintf(error_msg, sizeof(error_msg), format, ap);
71 va_end(ap);
72 uiPrint(state, error_msg);
73}
74
Doug Zongker52b40362014-02-10 15:30:30 -080075// Take a sha-1 digest and return it as a newly-allocated hex string.
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070076char* PrintSha1(const uint8_t* digest) {
Doug Zongker52b40362014-02-10 15:30:30 -080077 char* buffer = malloc(SHA_DIGEST_SIZE*2 + 1);
78 int i;
79 const char* alphabet = "0123456789abcdef";
80 for (i = 0; i < SHA_DIGEST_SIZE; ++i) {
81 buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf];
82 buffer[i*2+1] = alphabet[digest[i] & 0xf];
83 }
84 buffer[i*2] = '\0';
85 return buffer;
86}
87
Doug Zongker3d177d02010-07-01 09:18:44 -070088// mount(fs_type, partition_type, location, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -070089//
Doug Zongker3d177d02010-07-01 09:18:44 -070090// fs_type="yaffs2" partition_type="MTD" location=partition
91// fs_type="ext4" partition_type="EMMC" location=device
Doug Zongker512536a2010-02-17 16:11:44 -080092Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070093 char* result = NULL;
Doug Zongker3d177d02010-07-01 09:18:44 -070094 if (argc != 4) {
95 return ErrorAbort(state, "%s() expects 4 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -070096 }
Doug Zongker3d177d02010-07-01 09:18:44 -070097 char* fs_type;
98 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -070099 char* location;
100 char* mount_point;
Doug Zongker3d177d02010-07-01 09:18:44 -0700101 if (ReadArgs(state, argv, 4, &fs_type, &partition_type,
102 &location, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700103 return NULL;
104 }
105
Doug Zongker3d177d02010-07-01 09:18:44 -0700106 if (strlen(fs_type) == 0) {
107 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
108 goto done;
109 }
110 if (strlen(partition_type) == 0) {
111 ErrorAbort(state, "partition_type argument to %s() can't be empty",
112 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700113 goto done;
114 }
115 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700116 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700117 goto done;
118 }
119 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700120 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700121 goto done;
122 }
123
Stephen Smalley779701d2012-02-09 14:13:23 -0500124 char *secontext = NULL;
125
126 if (sehandle) {
127 selabel_lookup(sehandle, &secontext, mount_point, 0755);
128 setfscreatecon(secontext);
129 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500130
Doug Zongker9931f7f2009-06-10 14:11:53 -0700131 mkdir(mount_point, 0755);
132
Stephen Smalley779701d2012-02-09 14:13:23 -0500133 if (secontext) {
134 freecon(secontext);
135 setfscreatecon(NULL);
136 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500137
Doug Zongker3d177d02010-07-01 09:18:44 -0700138 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700139 mtd_scan_partitions();
140 const MtdPartition* mtd;
141 mtd = mtd_find_partition_by_name(location);
142 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700143 printf("%s: no mtd partition named \"%s\"",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700144 name, location);
145 result = strdup("");
146 goto done;
147 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700148 if (mtd_mount_partition(mtd, mount_point, fs_type, 0 /* rw */) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700149 printf("mtd mount of %s failed: %s\n",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700150 location, strerror(errno));
151 result = strdup("");
152 goto done;
153 }
154 result = mount_point;
155 } else {
Doug Zongker3d177d02010-07-01 09:18:44 -0700156 if (mount(location, mount_point, fs_type,
Doug Zongker9931f7f2009-06-10 14:11:53 -0700157 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "") < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700158 printf("%s: failed to mount %s at %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700159 name, location, mount_point, strerror(errno));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700160 result = strdup("");
161 } else {
162 result = mount_point;
163 }
164 }
165
166done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700167 free(fs_type);
168 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700169 free(location);
170 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800171 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700172}
173
Doug Zongker8edb00c2009-06-11 17:21:44 -0700174
175// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800176Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700177 char* result = NULL;
178 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700179 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700180 }
181 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700182 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700183 return NULL;
184 }
185 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700186 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700187 goto done;
188 }
189
190 scan_mounted_volumes();
191 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
192 if (vol == NULL) {
193 result = strdup("");
194 } else {
195 result = mount_point;
196 }
197
198done:
199 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800200 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700201}
202
203
Doug Zongker512536a2010-02-17 16:11:44 -0800204Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700205 char* result = NULL;
206 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700207 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700208 }
209 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700210 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700211 return NULL;
212 }
213 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700214 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700215 goto done;
216 }
217
218 scan_mounted_volumes();
219 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
220 if (vol == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700221 printf("unmount of %s failed; no such volume\n", mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700222 result = strdup("");
223 } else {
224 unmount_mounted_volume(vol);
225 result = mount_point;
226 }
227
228done:
229 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800230 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700231}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700232
JP Abgrall37aedb32014-06-16 19:07:39 -0700233static int exec_cmd(const char* path, char* const argv[]) {
234 int status;
235 pid_t child;
236 if ((child = vfork()) == 0) {
237 execv(path, argv);
238 _exit(-1);
239 }
240 waitpid(child, &status, 0);
241 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
242 printf("%s failed with status %d\n", path, WEXITSTATUS(status));
243 }
244 return WEXITSTATUS(status);
245}
246
Doug Zongker8edb00c2009-06-11 17:21:44 -0700247
Stephen Smalley779701d2012-02-09 14:13:23 -0500248// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700249//
Stephen Smalley779701d2012-02-09 14:13:23 -0500250// fs_type="yaffs2" partition_type="MTD" location=partition fs_size=<bytes> mount_point=<location>
251// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700252// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
253// if fs_size == 0, then make fs uses the entire partition.
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800254// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700255// 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 -0800256Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700257 char* result = NULL;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400258 if (argc != 5) {
259 return ErrorAbort(state, "%s() expects 5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700260 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700261 char* fs_type;
262 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700263 char* location;
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800264 char* fs_size;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400265 char* mount_point;
Stephen Smalley779701d2012-02-09 14:13:23 -0500266
Stephen Smalley779701d2012-02-09 14:13:23 -0500267 if (ReadArgs(state, argv, 5, &fs_type, &partition_type, &location, &fs_size, &mount_point) < 0) {
268 return NULL;
269 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700270
Doug Zongker3d177d02010-07-01 09:18:44 -0700271 if (strlen(fs_type) == 0) {
272 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
273 goto done;
274 }
275 if (strlen(partition_type) == 0) {
276 ErrorAbort(state, "partition_type argument to %s() can't be empty",
277 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700278 goto done;
279 }
280 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700281 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700282 goto done;
283 }
284
Stephen Smalley516e4e22012-04-03 13:35:11 -0400285 if (strlen(mount_point) == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500286 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
287 goto done;
288 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500289
Doug Zongker3d177d02010-07-01 09:18:44 -0700290 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700291 mtd_scan_partitions();
292 const MtdPartition* mtd = mtd_find_partition_by_name(location);
293 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700294 printf("%s: no mtd partition named \"%s\"",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700295 name, location);
296 result = strdup("");
297 goto done;
298 }
299 MtdWriteContext* ctx = mtd_write_partition(mtd);
300 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700301 printf("%s: can't write \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700302 result = strdup("");
303 goto done;
304 }
305 if (mtd_erase_blocks(ctx, -1) == -1) {
306 mtd_write_close(ctx);
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700307 printf("%s: failed to erase \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700308 result = strdup("");
309 goto done;
310 }
311 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700312 printf("%s: failed to close \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700313 result = strdup("");
314 goto done;
315 }
316 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700317#ifdef USE_EXT4
318 } else if (strcmp(fs_type, "ext4") == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500319 int status = make_ext4fs(location, atoll(fs_size), mount_point, sehandle);
Doug Zongker3d177d02010-07-01 09:18:44 -0700320 if (status != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700321 printf("%s: make_ext4fs failed (%d) on %s",
Doug Zongker3d177d02010-07-01 09:18:44 -0700322 name, status, location);
323 result = strdup("");
324 goto done;
325 }
326 result = location;
JP Abgrall37aedb32014-06-16 19:07:39 -0700327 } else if (strcmp(fs_type, "f2fs") == 0) {
328 char *num_sectors;
329 if (asprintf(&num_sectors, "%lld", atoll(fs_size) / 512) <= 0) {
330 printf("format_volume: failed to create %s command for %s\n", fs_type, location);
331 result = strdup("");
332 goto done;
333 }
334 const char *f2fs_path = "/sbin/mkfs.f2fs";
335 const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", location, num_sectors, NULL};
336 int status = exec_cmd(f2fs_path, (char* const*)f2fs_argv);
337 free(num_sectors);
338 if (status != 0) {
339 printf("%s: mkfs.f2fs failed (%d) on %s",
340 name, status, location);
341 result = strdup("");
342 goto done;
343 }
344 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700345#endif
Doug Zongker9931f7f2009-06-10 14:11:53 -0700346 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700347 printf("%s: unsupported fs_type \"%s\" partition_type \"%s\"",
Doug Zongker3d177d02010-07-01 09:18:44 -0700348 name, fs_type, partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700349 }
350
351done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700352 free(fs_type);
353 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700354 if (result != location) free(location);
Doug Zongker512536a2010-02-17 16:11:44 -0800355 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700356}
357
Michael Rungece7ca712013-11-06 17:42:20 -0800358Value* RenameFn(const char* name, State* state, int argc, Expr* argv[]) {
359 char* result = NULL;
360 if (argc != 2) {
361 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
362 }
363
364 char* src_name;
365 char* dst_name;
366
367 if (ReadArgs(state, argv, 2, &src_name, &dst_name) < 0) {
368 return NULL;
369 }
370 if (strlen(src_name) == 0) {
371 ErrorAbort(state, "src_name argument to %s() can't be empty", name);
372 goto done;
373 }
374 if (strlen(dst_name) == 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700375 ErrorAbort(state, "dst_name argument to %s() can't be empty", name);
Michael Rungece7ca712013-11-06 17:42:20 -0800376 goto done;
377 }
Michael Rungea91ecc52014-07-21 17:40:02 -0700378 if (make_parents(dst_name) != 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700379 ErrorAbort(state, "Creating parent of %s failed, error %s",
Michael Rungea91ecc52014-07-21 17:40:02 -0700380 dst_name, strerror(errno));
Michael Runged5b17272014-10-22 14:28:23 -0700381 } else if (access(dst_name, F_OK) == 0 && access(src_name, F_OK) != 0) {
382 // File was already moved
383 result = dst_name;
Michael Rungea91ecc52014-07-21 17:40:02 -0700384 } else if (rename(src_name, dst_name) != 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700385 ErrorAbort(state, "Rename of %s to %s failed, error %s",
Michael Rungece7ca712013-11-06 17:42:20 -0800386 src_name, dst_name, strerror(errno));
387 } else {
388 result = dst_name;
389 }
390
391done:
392 free(src_name);
393 if (result != dst_name) free(dst_name);
394 return StringValue(result);
395}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700396
Doug Zongker512536a2010-02-17 16:11:44 -0800397Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700398 char** paths = malloc(argc * sizeof(char*));
399 int i;
400 for (i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700401 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700402 if (paths[i] == NULL) {
403 int j;
404 for (j = 0; j < i; ++i) {
405 free(paths[j]);
406 }
407 free(paths);
408 return NULL;
409 }
410 }
411
412 bool recursive = (strcmp(name, "delete_recursive") == 0);
413
414 int success = 0;
415 for (i = 0; i < argc; ++i) {
416 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
417 ++success;
418 free(paths[i]);
419 }
420 free(paths);
421
422 char buffer[10];
423 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800424 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700425}
426
Doug Zongker8edb00c2009-06-11 17:21:44 -0700427
Doug Zongker512536a2010-02-17 16:11:44 -0800428Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700429 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700430 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700431 }
432 char* frac_str;
433 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700434 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700435 return NULL;
436 }
437
438 double frac = strtod(frac_str, NULL);
439 int sec = strtol(sec_str, NULL, 10);
440
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700441 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700442 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
443
Doug Zongker9931f7f2009-06-10 14:11:53 -0700444 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800445 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700446}
447
Doug Zongker512536a2010-02-17 16:11:44 -0800448Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700449 if (argc != 1) {
450 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
451 }
452 char* frac_str;
453 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
454 return NULL;
455 }
456
457 double frac = strtod(frac_str, NULL);
458
459 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
460 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
461
Doug Zongker512536a2010-02-17 16:11:44 -0800462 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700463}
464
Doug Zongker8edb00c2009-06-11 17:21:44 -0700465// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800466Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700467 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700468 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700469 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700470 }
471 char* zip_path;
472 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700473 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700474
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700475 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700476
477 // To create a consistent system image, never use the clock for timestamps.
478 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
479
480 bool success = mzExtractRecursive(za, zip_path, dest_path,
481 MZ_EXTRACT_FILES_ONLY, &timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500482 NULL, NULL, sehandle);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700483 free(zip_path);
484 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800485 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700486}
487
Doug Zongker8edb00c2009-06-11 17:21:44 -0700488
489// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800490// or
491// package_extract_file(package_path)
492// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800493// function (the char* returned is actually a FileContents*).
494Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700495 int argc, Expr* argv[]) {
Doug Zongker5f875bf2014-08-22 14:53:43 -0700496 if (argc < 1 || argc > 2) {
497 return ErrorAbort(state, "%s() expects 1 or 2 args, got %d",
Doug Zongker6aece332010-02-01 14:40:12 -0800498 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700499 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700500 bool success = false;
Doug Zongker43772d22014-06-09 14:13:19 -0700501
502 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker43772d22014-06-09 14:13:19 -0700503
Doug Zongker5f875bf2014-08-22 14:53:43 -0700504 if (argc == 2) {
505 // The two-argument version extracts to a file.
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800506
507 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700508
Doug Zongker6aece332010-02-01 14:40:12 -0800509 char* zip_path;
510 char* dest_path;
Doug Zongker5f875bf2014-08-22 14:53:43 -0700511 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800512
Doug Zongker6aece332010-02-01 14:40:12 -0800513 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
514 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700515 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800516 goto done2;
517 }
518
519 FILE* f = fopen(dest_path, "wb");
520 if (f == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700521 printf("%s: can't open %s for write: %s\n",
Doug Zongker6aece332010-02-01 14:40:12 -0800522 name, dest_path, strerror(errno));
523 goto done2;
524 }
Doug Zongker5f875bf2014-08-22 14:53:43 -0700525 success = mzExtractZipEntryToFile(za, entry, fileno(f));
Doug Zongker6aece332010-02-01 14:40:12 -0800526 fclose(f);
527
528 done2:
529 free(zip_path);
530 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800531 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800532 } else {
533 // The one-argument version returns the contents of the file
534 // as the result.
535
536 char* zip_path;
Doug Zongker512536a2010-02-17 16:11:44 -0800537 Value* v = malloc(sizeof(Value));
538 v->type = VAL_BLOB;
539 v->size = -1;
540 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800541
542 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
543
544 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
545 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
546 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700547 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800548 goto done1;
549 }
550
Doug Zongker512536a2010-02-17 16:11:44 -0800551 v->size = mzGetZipEntryUncompLen(entry);
552 v->data = malloc(v->size);
553 if (v->data == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700554 printf("%s: failed to allocate %ld bytes for %s\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800555 name, (long)v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800556 goto done1;
557 }
558
Doug Zongker512536a2010-02-17 16:11:44 -0800559 success = mzExtractZipEntryToBuffer(za, entry,
560 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800561
562 done1:
563 free(zip_path);
564 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800565 free(v->data);
566 v->data = NULL;
567 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800568 }
Doug Zongker512536a2010-02-17 16:11:44 -0800569 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700570 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700571}
572
Doug Zongkera23075f2012-08-06 16:19:09 -0700573// Create all parent directories of name, if necessary.
574static int make_parents(char* name) {
575 char* p;
576 for (p = name + (strlen(name)-1); p > name; --p) {
577 if (*p != '/') continue;
578 *p = '\0';
579 if (make_parents(name) < 0) return -1;
580 int result = mkdir(name, 0700);
Michael Rungea91ecc52014-07-21 17:40:02 -0700581 if (result == 0) printf("created [%s]\n", name);
Doug Zongkera23075f2012-08-06 16:19:09 -0700582 *p = '/';
583 if (result == 0 || errno == EEXIST) {
584 // successfully created or already existed; we're done
585 return 0;
586 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700587 printf("failed to mkdir %s: %s\n", name, strerror(errno));
Doug Zongkera23075f2012-08-06 16:19:09 -0700588 return -1;
589 }
590 }
591 return 0;
592}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700593
Doug Zongker9931f7f2009-06-10 14:11:53 -0700594// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700595// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800596Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700597 if (argc == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700598 return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700599 }
600 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700601 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700602 if (target == NULL) return NULL;
603
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700604 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700605 if (srcs == NULL) {
606 free(target);
607 return NULL;
608 }
609
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700610 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700611 int i;
612 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700613 if (unlink(srcs[i]) < 0) {
614 if (errno != ENOENT) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700615 printf("%s: failed to remove %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700616 name, srcs[i], strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700617 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700618 }
619 }
Doug Zongkera23075f2012-08-06 16:19:09 -0700620 if (make_parents(srcs[i])) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700621 printf("%s: failed to symlink %s to %s: making parents failed\n",
Doug Zongkera23075f2012-08-06 16:19:09 -0700622 name, srcs[i], target);
623 ++bad;
624 }
Doug Zongker60babf82009-09-18 15:11:24 -0700625 if (symlink(target, srcs[i]) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700626 printf("%s: failed to symlink %s to %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700627 name, srcs[i], target, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700628 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700629 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700630 free(srcs[i]);
631 }
632 free(srcs);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700633 if (bad) {
634 return ErrorAbort(state, "%s: some symlinks failed", name);
635 }
Doug Zongker512536a2010-02-17 16:11:44 -0800636 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700637}
638
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700639struct perm_parsed_args {
640 bool has_uid;
641 uid_t uid;
642 bool has_gid;
643 gid_t gid;
644 bool has_mode;
645 mode_t mode;
646 bool has_fmode;
647 mode_t fmode;
648 bool has_dmode;
649 mode_t dmode;
650 bool has_selabel;
651 char* selabel;
652 bool has_capabilities;
653 uint64_t capabilities;
654};
655
Michael Runge75480252014-10-22 19:48:41 -0700656static struct perm_parsed_args ParsePermArgs(State * state, int argc, char** args) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700657 int i;
658 struct perm_parsed_args parsed;
659 int bad = 0;
660 static int max_warnings = 20;
661
662 memset(&parsed, 0, sizeof(parsed));
663
664 for (i = 1; i < argc; i += 2) {
665 if (strcmp("uid", args[i]) == 0) {
666 int64_t uid;
667 if (sscanf(args[i+1], "%" SCNd64, &uid) == 1) {
668 parsed.uid = uid;
669 parsed.has_uid = true;
670 } else {
Michael Runge75480252014-10-22 19:48:41 -0700671 uiPrintf(state, "ParsePermArgs: invalid UID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700672 bad++;
673 }
674 continue;
675 }
676 if (strcmp("gid", args[i]) == 0) {
677 int64_t gid;
678 if (sscanf(args[i+1], "%" SCNd64, &gid) == 1) {
679 parsed.gid = gid;
680 parsed.has_gid = true;
681 } else {
Michael Runge75480252014-10-22 19:48:41 -0700682 uiPrintf(state, "ParsePermArgs: invalid GID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700683 bad++;
684 }
685 continue;
686 }
687 if (strcmp("mode", args[i]) == 0) {
688 int32_t mode;
689 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
690 parsed.mode = mode;
691 parsed.has_mode = true;
692 } else {
Michael Runge75480252014-10-22 19:48:41 -0700693 uiPrintf(state, "ParsePermArgs: invalid mode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700694 bad++;
695 }
696 continue;
697 }
698 if (strcmp("dmode", args[i]) == 0) {
699 int32_t mode;
700 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
701 parsed.dmode = mode;
702 parsed.has_dmode = true;
703 } else {
Michael Runge75480252014-10-22 19:48:41 -0700704 uiPrintf(state, "ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700705 bad++;
706 }
707 continue;
708 }
709 if (strcmp("fmode", args[i]) == 0) {
710 int32_t mode;
711 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
712 parsed.fmode = mode;
713 parsed.has_fmode = true;
714 } else {
Michael Runge75480252014-10-22 19:48:41 -0700715 uiPrintf(state, "ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700716 bad++;
717 }
718 continue;
719 }
720 if (strcmp("capabilities", args[i]) == 0) {
721 int64_t capabilities;
722 if (sscanf(args[i+1], "%" SCNi64, &capabilities) == 1) {
723 parsed.capabilities = capabilities;
724 parsed.has_capabilities = true;
725 } else {
Michael Runge75480252014-10-22 19:48:41 -0700726 uiPrintf(state, "ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700727 bad++;
728 }
729 continue;
730 }
731 if (strcmp("selabel", args[i]) == 0) {
732 if (args[i+1][0] != '\0') {
733 parsed.selabel = args[i+1];
734 parsed.has_selabel = true;
735 } else {
Michael Runge75480252014-10-22 19:48:41 -0700736 uiPrintf(state, "ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700737 bad++;
738 }
739 continue;
740 }
741 if (max_warnings != 0) {
742 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i]);
743 max_warnings--;
744 if (max_warnings == 0) {
745 printf("ParsedPermArgs: suppressing further warnings\n");
746 }
747 }
748 }
749 return parsed;
750}
751
752static int ApplyParsedPerms(
Michael Runge75480252014-10-22 19:48:41 -0700753 State * state,
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700754 const char* filename,
755 const struct stat *statptr,
756 struct perm_parsed_args parsed)
757{
758 int bad = 0;
759
Nick Kraleviche4612512013-09-10 15:34:19 -0700760 /* ignore symlinks */
761 if (S_ISLNK(statptr->st_mode)) {
762 return 0;
763 }
764
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700765 if (parsed.has_uid) {
766 if (chown(filename, parsed.uid, -1) < 0) {
Michael Runge75480252014-10-22 19:48:41 -0700767 uiPrintf(state, "ApplyParsedPerms: chown of %s to %d failed: %s\n",
768 filename, parsed.uid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700769 bad++;
770 }
771 }
772
773 if (parsed.has_gid) {
774 if (chown(filename, -1, parsed.gid) < 0) {
Michael Runge75480252014-10-22 19:48:41 -0700775 uiPrintf(state, "ApplyParsedPerms: chgrp of %s to %d failed: %s\n",
776 filename, parsed.gid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700777 bad++;
778 }
779 }
780
781 if (parsed.has_mode) {
782 if (chmod(filename, parsed.mode) < 0) {
Michael Runge75480252014-10-22 19:48:41 -0700783 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
784 filename, parsed.mode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700785 bad++;
786 }
787 }
788
789 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
790 if (chmod(filename, parsed.dmode) < 0) {
Michael Runge75480252014-10-22 19:48:41 -0700791 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
792 filename, parsed.dmode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700793 bad++;
794 }
795 }
796
797 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
798 if (chmod(filename, parsed.fmode) < 0) {
Michael Runge75480252014-10-22 19:48:41 -0700799 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700800 filename, parsed.fmode, strerror(errno));
801 bad++;
802 }
803 }
804
805 if (parsed.has_selabel) {
806 // TODO: Don't silently ignore ENOTSUP
807 if (lsetfilecon(filename, parsed.selabel) && (errno != ENOTSUP)) {
Michael Runge75480252014-10-22 19:48:41 -0700808 uiPrintf(state, "ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n",
809 filename, parsed.selabel, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700810 bad++;
811 }
812 }
813
814 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
815 if (parsed.capabilities == 0) {
816 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
817 // Report failure unless it's ENODATA (attribute not set)
Michael Runge75480252014-10-22 19:48:41 -0700818 uiPrintf(state, "ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700819 filename, parsed.capabilities, strerror(errno));
820 bad++;
821 }
822 } else {
823 struct vfs_cap_data cap_data;
824 memset(&cap_data, 0, sizeof(cap_data));
825 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
826 cap_data.data[0].permitted = (uint32_t) (parsed.capabilities & 0xffffffff);
827 cap_data.data[0].inheritable = 0;
828 cap_data.data[1].permitted = (uint32_t) (parsed.capabilities >> 32);
829 cap_data.data[1].inheritable = 0;
830 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
Michael Runge75480252014-10-22 19:48:41 -0700831 uiPrintf(state, "ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n",
832 filename, parsed.capabilities, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700833 bad++;
834 }
835 }
836 }
837
838 return bad;
839}
840
841// nftw doesn't allow us to pass along context, so we need to use
842// global variables. *sigh*
843static struct perm_parsed_args recursive_parsed_args;
Michael Runge75480252014-10-22 19:48:41 -0700844static State* recursive_state;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700845
846static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr,
847 int fileflags, struct FTW *pfwt) {
Michael Runge75480252014-10-22 19:48:41 -0700848 return ApplyParsedPerms(recursive_state, filename, statptr, recursive_parsed_args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700849}
850
851static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
852 int i;
853 int bad = 0;
854 static int nwarnings = 0;
855 struct stat sb;
856 Value* result = NULL;
857
858 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
859
860 if ((argc % 2) != 1) {
861 return ErrorAbort(state, "%s() expects an odd number of arguments, got %d",
862 name, argc);
863 }
864
865 char** args = ReadVarArgs(state, argc, argv);
866 if (args == NULL) return NULL;
867
868 if (lstat(args[0], &sb) == -1) {
869 result = ErrorAbort(state, "%s: Error on lstat of \"%s\": %s", name, args[0], strerror(errno));
870 goto done;
871 }
872
Michael Runge75480252014-10-22 19:48:41 -0700873 struct perm_parsed_args parsed = ParsePermArgs(state, argc, args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700874
875 if (recursive) {
876 recursive_parsed_args = parsed;
Michael Runge75480252014-10-22 19:48:41 -0700877 recursive_state = state;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700878 bad += nftw(args[0], do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
879 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
Michael Runge75480252014-10-22 19:48:41 -0700880 recursive_state = NULL;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700881 } else {
Michael Runge75480252014-10-22 19:48:41 -0700882 bad += ApplyParsedPerms(state, args[0], &sb, parsed);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700883 }
884
885done:
886 for (i = 0; i < argc; ++i) {
887 free(args[i]);
888 }
889 free(args);
890
891 if (result != NULL) {
892 return result;
893 }
894
895 if (bad > 0) {
896 return ErrorAbort(state, "%s: some changes failed", name);
897 }
898
899 return StringValue(strdup(""));
900}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700901
Doug Zongker512536a2010-02-17 16:11:44 -0800902Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700903 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700904 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700905 }
906 char* key;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700907 key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700908 if (key == NULL) return NULL;
909
910 char value[PROPERTY_VALUE_MAX];
911 property_get(key, value, "");
912 free(key);
913
Doug Zongker512536a2010-02-17 16:11:44 -0800914 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700915}
916
917
Doug Zongker47cace92009-06-18 10:11:50 -0700918// file_getprop(file, key)
919//
920// interprets 'file' as a getprop-style file (key=value pairs, one
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700921// per line. # comment lines,blank lines, lines without '=' ignored),
922// and returns the value for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800923Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700924 char* result = NULL;
925 char* buffer = NULL;
926 char* filename;
927 char* key;
928 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
929 return NULL;
930 }
931
932 struct stat st;
933 if (stat(filename, &st) < 0) {
934 ErrorAbort(state, "%s: failed to stat \"%s\": %s",
935 name, filename, strerror(errno));
936 goto done;
937 }
938
939#define MAX_FILE_GETPROP_SIZE 65536
940
941 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
942 ErrorAbort(state, "%s too large for %s (max %d)",
943 filename, name, MAX_FILE_GETPROP_SIZE);
944 goto done;
945 }
946
947 buffer = malloc(st.st_size+1);
948 if (buffer == NULL) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700949 ErrorAbort(state, "%s: failed to alloc %lld bytes", name, (long long)st.st_size+1);
Doug Zongker47cace92009-06-18 10:11:50 -0700950 goto done;
951 }
952
953 FILE* f = fopen(filename, "rb");
954 if (f == NULL) {
955 ErrorAbort(state, "%s: failed to open %s: %s",
956 name, filename, strerror(errno));
957 goto done;
958 }
959
960 if (fread(buffer, 1, st.st_size, f) != st.st_size) {
Doug Zongkera23075f2012-08-06 16:19:09 -0700961 ErrorAbort(state, "%s: failed to read %lld bytes from %s",
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700962 name, (long long)st.st_size+1, filename);
Doug Zongker47cace92009-06-18 10:11:50 -0700963 fclose(f);
964 goto done;
965 }
966 buffer[st.st_size] = '\0';
967
968 fclose(f);
969
970 char* line = strtok(buffer, "\n");
971 do {
972 // skip whitespace at start of line
973 while (*line && isspace(*line)) ++line;
974
975 // comment or blank line: skip to next line
976 if (*line == '\0' || *line == '#') continue;
977
978 char* equal = strchr(line, '=');
979 if (equal == NULL) {
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700980 continue;
Doug Zongker47cace92009-06-18 10:11:50 -0700981 }
982
983 // trim whitespace between key and '='
984 char* key_end = equal-1;
985 while (key_end > line && isspace(*key_end)) --key_end;
986 key_end[1] = '\0';
987
988 // not the key we're looking for
989 if (strcmp(key, line) != 0) continue;
990
991 // skip whitespace after the '=' to the start of the value
992 char* val_start = equal+1;
993 while(*val_start && isspace(*val_start)) ++val_start;
994
995 // trim trailing whitespace
996 char* val_end = val_start + strlen(val_start)-1;
997 while (val_end > val_start && isspace(*val_end)) --val_end;
998 val_end[1] = '\0';
999
1000 result = strdup(val_start);
1001 break;
1002
1003 } while ((line = strtok(NULL, "\n")));
1004
1005 if (result == NULL) result = strdup("");
1006
1007 done:
1008 free(filename);
1009 free(key);
1010 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -08001011 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -07001012}
1013
1014
Doug Zongker8edb00c2009-06-11 17:21:44 -07001015static bool write_raw_image_cb(const unsigned char* data,
1016 int data_len, void* ctx) {
1017 int r = mtd_write_data((MtdWriteContext*)ctx, (const char *)data, data_len);
1018 if (r == data_len) return true;
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001019 printf("%s\n", strerror(errno));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001020 return false;
1021}
1022
Doug Zongker179b2d92011-04-12 15:49:04 -07001023// write_raw_image(filename_or_blob, partition)
Doug Zongker512536a2010-02-17 16:11:44 -08001024Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001025 char* result = NULL;
1026
Doug Zongker179b2d92011-04-12 15:49:04 -07001027 Value* partition_value;
1028 Value* contents;
1029 if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001030 return NULL;
1031 }
1032
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001033 char* partition = NULL;
Doug Zongker179b2d92011-04-12 15:49:04 -07001034 if (partition_value->type != VAL_STRING) {
1035 ErrorAbort(state, "partition argument to %s must be string", name);
1036 goto done;
1037 }
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001038 partition = partition_value->data;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001039 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001040 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001041 goto done;
1042 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001043 if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001044 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001045 goto done;
1046 }
1047
1048 mtd_scan_partitions();
1049 const MtdPartition* mtd = mtd_find_partition_by_name(partition);
1050 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001051 printf("%s: no mtd partition named \"%s\"\n", name, partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001052 result = strdup("");
1053 goto done;
1054 }
1055
1056 MtdWriteContext* ctx = mtd_write_partition(mtd);
1057 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001058 printf("%s: can't write mtd partition \"%s\"\n",
Doug Zongker8edb00c2009-06-11 17:21:44 -07001059 name, partition);
1060 result = strdup("");
1061 goto done;
1062 }
1063
1064 bool success;
1065
Doug Zongker179b2d92011-04-12 15:49:04 -07001066 if (contents->type == VAL_STRING) {
1067 // we're given a filename as the contents
1068 char* filename = contents->data;
1069 FILE* f = fopen(filename, "rb");
1070 if (f == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001071 printf("%s: can't open %s: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -07001072 name, filename, strerror(errno));
1073 result = strdup("");
1074 goto done;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001075 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001076
1077 success = true;
1078 char* buffer = malloc(BUFSIZ);
1079 int read;
1080 while (success && (read = fread(buffer, 1, BUFSIZ, f)) > 0) {
1081 int wrote = mtd_write_data(ctx, buffer, read);
1082 success = success && (wrote == read);
1083 }
1084 free(buffer);
1085 fclose(f);
1086 } else {
1087 // we're given a blob as the contents
1088 ssize_t wrote = mtd_write_data(ctx, contents->data, contents->size);
1089 success = (wrote == contents->size);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001090 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001091 if (!success) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001092 printf("mtd_write_data to %s failed: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -07001093 partition, strerror(errno));
1094 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001095
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001096 if (mtd_erase_blocks(ctx, -1) == -1) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001097 printf("%s: error erasing blocks of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001098 }
1099 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001100 printf("%s: error closing write of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001101 }
1102
Doug Zongker179b2d92011-04-12 15:49:04 -07001103 printf("%s %s partition\n",
1104 success ? "wrote" : "failed to write", partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001105
1106 result = success ? partition : strdup("");
1107
1108done:
Doug Zongker179b2d92011-04-12 15:49:04 -07001109 if (result != partition) FreeValue(partition_value);
1110 FreeValue(contents);
Doug Zongker512536a2010-02-17 16:11:44 -08001111 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001112}
1113
Doug Zongker8edb00c2009-06-11 17:21:44 -07001114// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001115Value* ApplyPatchSpaceFn(const char* name, State* state,
1116 int argc, Expr* argv[]) {
1117 char* bytes_str;
1118 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
1119 return NULL;
1120 }
1121
1122 char* endptr;
1123 size_t bytes = strtol(bytes_str, &endptr, 10);
1124 if (bytes == 0 && endptr == bytes_str) {
1125 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n",
1126 name, bytes_str);
1127 free(bytes_str);
1128 return NULL;
1129 }
1130
1131 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
1132}
1133
Doug Zongker52b40362014-02-10 15:30:30 -08001134// apply_patch(file, size, init_sha1, tgt_sha1, patch)
1135
Doug Zongker512536a2010-02-17 16:11:44 -08001136Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001137 if (argc < 6 || (argc % 2) == 1) {
1138 return ErrorAbort(state, "%s(): expected at least 6 args and an "
1139 "even number, got %d",
1140 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001141 }
1142
Doug Zongkerc4351c72010-02-22 14:46:32 -08001143 char* source_filename;
1144 char* target_filename;
1145 char* target_sha1;
1146 char* target_size_str;
1147 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
1148 &target_sha1, &target_size_str) < 0) {
1149 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001150 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001151
Doug Zongkerc4351c72010-02-22 14:46:32 -08001152 char* endptr;
1153 size_t target_size = strtol(target_size_str, &endptr, 10);
1154 if (target_size == 0 && endptr == target_size_str) {
1155 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count",
1156 name, target_size_str);
1157 free(source_filename);
1158 free(target_filename);
1159 free(target_sha1);
1160 free(target_size_str);
1161 return NULL;
1162 }
1163
1164 int patchcount = (argc-4) / 2;
1165 Value** patches = ReadValueVarArgs(state, argc-4, argv+4);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001166
1167 int i;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001168 for (i = 0; i < patchcount; ++i) {
1169 if (patches[i*2]->type != VAL_STRING) {
1170 ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i);
1171 break;
1172 }
1173 if (patches[i*2+1]->type != VAL_BLOB) {
1174 ErrorAbort(state, "%s(): patch #%d is not blob", name, i);
1175 break;
1176 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001177 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001178 if (i != patchcount) {
1179 for (i = 0; i < patchcount*2; ++i) {
1180 FreeValue(patches[i]);
1181 }
1182 free(patches);
1183 return NULL;
1184 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001185
Doug Zongkerc4351c72010-02-22 14:46:32 -08001186 char** patch_sha_str = malloc(patchcount * sizeof(char*));
1187 for (i = 0; i < patchcount; ++i) {
1188 patch_sha_str[i] = patches[i*2]->data;
1189 patches[i*2]->data = NULL;
1190 FreeValue(patches[i*2]);
1191 patches[i] = patches[i*2+1];
Doug Zongker8edb00c2009-06-11 17:21:44 -07001192 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001193
1194 int result = applypatch(source_filename, target_filename,
1195 target_sha1, target_size,
Doug Zongkera3ccba62012-08-20 15:28:02 -07001196 patchcount, patch_sha_str, patches, NULL);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001197
1198 for (i = 0; i < patchcount; ++i) {
1199 FreeValue(patches[i]);
1200 }
1201 free(patch_sha_str);
1202 free(patches);
1203
1204 return StringValue(strdup(result == 0 ? "t" : ""));
1205}
1206
1207// apply_patch_check(file, [sha1_1, ...])
1208Value* ApplyPatchCheckFn(const char* name, State* state,
1209 int argc, Expr* argv[]) {
1210 if (argc < 1) {
1211 return ErrorAbort(state, "%s(): expected at least 1 arg, got %d",
1212 name, argc);
1213 }
1214
1215 char* filename;
1216 if (ReadArgs(state, argv, 1, &filename) < 0) {
1217 return NULL;
1218 }
1219
1220 int patchcount = argc-1;
1221 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
1222
1223 int result = applypatch_check(filename, patchcount, sha1s);
1224
1225 int i;
1226 for (i = 0; i < patchcount; ++i) {
1227 free(sha1s[i]);
1228 }
1229 free(sha1s);
1230
1231 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001232}
1233
Doug Zongker512536a2010-02-17 16:11:44 -08001234Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001235 char** args = ReadVarArgs(state, argc, argv);
1236 if (args == NULL) {
1237 return NULL;
1238 }
1239
1240 int size = 0;
1241 int i;
1242 for (i = 0; i < argc; ++i) {
1243 size += strlen(args[i]);
1244 }
1245 char* buffer = malloc(size+1);
1246 size = 0;
1247 for (i = 0; i < argc; ++i) {
1248 strcpy(buffer+size, args[i]);
1249 size += strlen(args[i]);
1250 free(args[i]);
1251 }
1252 free(args);
1253 buffer[size] = '\0';
Michael Runge75480252014-10-22 19:48:41 -07001254 uiPrint(state, buffer);
Doug Zongker512536a2010-02-17 16:11:44 -08001255 return StringValue(buffer);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001256}
1257
Doug Zongkerd0181b82011-10-19 10:51:12 -07001258Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
1259 if (argc != 0) {
1260 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1261 }
1262 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
1263 return StringValue(strdup("t"));
1264}
1265
Doug Zongker512536a2010-02-17 16:11:44 -08001266Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001267 if (argc < 1) {
1268 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1269 }
1270 char** args = ReadVarArgs(state, argc, argv);
1271 if (args == NULL) {
1272 return NULL;
1273 }
1274
1275 char** args2 = malloc(sizeof(char*) * (argc+1));
1276 memcpy(args2, args, sizeof(char*) * argc);
1277 args2[argc] = NULL;
1278
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001279 printf("about to run program [%s] with %d args\n", args2[0], argc);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001280
1281 pid_t child = fork();
1282 if (child == 0) {
1283 execv(args2[0], args2);
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001284 printf("run_program: execv failed: %s\n", strerror(errno));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001285 _exit(1);
1286 }
1287 int status;
1288 waitpid(child, &status, 0);
1289 if (WIFEXITED(status)) {
1290 if (WEXITSTATUS(status) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001291 printf("run_program: child exited with status %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001292 WEXITSTATUS(status));
1293 }
1294 } else if (WIFSIGNALED(status)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001295 printf("run_program: child terminated by signal %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001296 WTERMSIG(status));
1297 }
1298
1299 int i;
1300 for (i = 0; i < argc; ++i) {
1301 free(args[i]);
1302 }
1303 free(args);
1304 free(args2);
1305
1306 char buffer[20];
1307 sprintf(buffer, "%d", status);
1308
Doug Zongker512536a2010-02-17 16:11:44 -08001309 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001310}
1311
Doug Zongker512536a2010-02-17 16:11:44 -08001312// sha1_check(data)
1313// to return the sha1 of the data (given in the format returned by
1314// read_file).
1315//
1316// sha1_check(data, sha1_hex, [sha1_hex, ...])
1317// returns the sha1 of the file if it matches any of the hex
1318// strings passed, or "" if it does not equal any of them.
1319//
1320Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1321 if (argc < 1) {
1322 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1323 }
1324
1325 Value** args = ReadValueVarArgs(state, argc, argv);
1326 if (args == NULL) {
1327 return NULL;
1328 }
1329
1330 if (args[0]->size < 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001331 return StringValue(strdup(""));
1332 }
1333 uint8_t digest[SHA_DIGEST_SIZE];
Doug Zongkerbac7fba2013-04-10 11:32:17 -07001334 SHA_hash(args[0]->data, args[0]->size, digest);
Doug Zongker512536a2010-02-17 16:11:44 -08001335 FreeValue(args[0]);
1336
1337 if (argc == 1) {
1338 return StringValue(PrintSha1(digest));
1339 }
1340
1341 int i;
1342 uint8_t* arg_digest = malloc(SHA_DIGEST_SIZE);
1343 for (i = 1; i < argc; ++i) {
1344 if (args[i]->type != VAL_STRING) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001345 printf("%s(): arg %d is not a string; skipping",
Doug Zongker512536a2010-02-17 16:11:44 -08001346 name, i);
1347 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1348 // Warn about bad args and skip them.
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001349 printf("%s(): error parsing \"%s\" as sha-1; skipping",
1350 name, args[i]->data);
Doug Zongker512536a2010-02-17 16:11:44 -08001351 } else if (memcmp(digest, arg_digest, SHA_DIGEST_SIZE) == 0) {
1352 break;
1353 }
1354 FreeValue(args[i]);
1355 }
1356 if (i >= argc) {
1357 // Didn't match any of the hex strings; return false.
1358 return StringValue(strdup(""));
1359 }
1360 // Found a match; free all the remaining arguments and return the
1361 // matched one.
1362 int j;
1363 for (j = i+1; j < argc; ++j) {
1364 FreeValue(args[j]);
1365 }
1366 return args[i];
1367}
1368
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001369// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001370// is actually a FileContents*).
1371Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1372 if (argc != 1) {
1373 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1374 }
1375 char* filename;
1376 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1377
1378 Value* v = malloc(sizeof(Value));
1379 v->type = VAL_BLOB;
1380
1381 FileContents fc;
Doug Zongkera1bc1482014-02-13 15:18:19 -08001382 if (LoadFileContents(filename, &fc) != 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001383 free(filename);
Michael Runge6eed2242013-12-13 17:13:11 -08001384 v->size = -1;
1385 v->data = NULL;
Doug Zongker512536a2010-02-17 16:11:44 -08001386 free(fc.data);
Michael Runge6eed2242013-12-13 17:13:11 -08001387 return v;
Doug Zongker512536a2010-02-17 16:11:44 -08001388 }
1389
1390 v->size = fc.size;
1391 v->data = (char*)fc.data;
1392
1393 free(filename);
1394 return v;
1395}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001396
Doug Zongkerc87bab12013-11-25 13:53:25 -08001397// Immediately reboot the device. Recovery is not finished normally,
1398// so if you reboot into recovery it will re-start applying the
1399// current package (because nothing has cleared the copy of the
1400// arguments stored in the BCB).
1401//
1402// The argument is the partition name passed to the android reboot
1403// property. It can be "recovery" to boot from the recovery
1404// partition, or "" (empty string) to boot from the regular boot
1405// partition.
1406Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) {
1407 if (argc != 2) {
1408 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1409 }
1410
1411 char* filename;
1412 char* property;
1413 if (ReadArgs(state, argv, 2, &filename, &property) < 0) return NULL;
1414
1415 char buffer[80];
1416
1417 // zero out the 'command' field of the bootloader message.
1418 memset(buffer, 0, sizeof(((struct bootloader_message*)0)->command));
1419 FILE* f = fopen(filename, "r+b");
1420 fseek(f, offsetof(struct bootloader_message, command), SEEK_SET);
1421 fwrite(buffer, sizeof(((struct bootloader_message*)0)->command), 1, f);
1422 fclose(f);
1423 free(filename);
1424
1425 strcpy(buffer, "reboot,");
1426 if (property != NULL) {
1427 strncat(buffer, property, sizeof(buffer)-10);
1428 }
1429
1430 property_set(ANDROID_RB_PROPERTY, buffer);
1431
1432 sleep(5);
1433 free(property);
1434 ErrorAbort(state, "%s() failed to reboot", name);
1435 return NULL;
1436}
1437
1438// Store a string value somewhere that future invocations of recovery
1439// can access it. This value is called the "stage" and can be used to
1440// drive packages that need to do reboots in the middle of
1441// installation and keep track of where they are in the multi-stage
1442// install.
1443//
1444// The first argument is the block device for the misc partition
1445// ("/misc" in the fstab), which is where this value is stored. The
1446// second argument is the string to store; it should not exceed 31
1447// bytes.
1448Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
1449 if (argc != 2) {
1450 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1451 }
1452
1453 char* filename;
1454 char* stagestr;
1455 if (ReadArgs(state, argv, 2, &filename, &stagestr) < 0) return NULL;
1456
1457 // Store this value in the misc partition, immediately after the
1458 // bootloader message that the main recovery uses to save its
1459 // arguments in case of the device restarting midway through
1460 // package installation.
1461 FILE* f = fopen(filename, "r+b");
1462 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1463 int to_write = strlen(stagestr)+1;
1464 int max_size = sizeof(((struct bootloader_message*)0)->stage);
1465 if (to_write > max_size) {
1466 to_write = max_size;
1467 stagestr[max_size-1] = 0;
1468 }
1469 fwrite(stagestr, to_write, 1, f);
1470 fclose(f);
1471
1472 free(stagestr);
1473 return StringValue(filename);
1474}
1475
1476// Return the value most recently saved with SetStageFn. The argument
1477// is the block device for the misc partition.
1478Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001479 if (argc != 1) {
Doug Zongkerc87bab12013-11-25 13:53:25 -08001480 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1481 }
1482
1483 char* filename;
1484 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1485
1486 char buffer[sizeof(((struct bootloader_message*)0)->stage)];
1487 FILE* f = fopen(filename, "rb");
1488 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1489 fread(buffer, sizeof(buffer), 1, f);
1490 fclose(f);
1491 buffer[sizeof(buffer)-1] = '\0';
1492
1493 return StringValue(strdup(buffer));
1494}
1495
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001496Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) {
1497 if (argc != 2) {
1498 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1499 }
1500
1501 char* filename;
1502 char* len_str;
1503 if (ReadArgs(state, argv, 2, &filename, &len_str) < 0) return NULL;
1504
1505 size_t len = strtoull(len_str, NULL, 0);
1506 int fd = open(filename, O_WRONLY, 0644);
1507 int success = wipe_block_device(fd, len);
1508
1509 free(filename);
1510 free(len_str);
1511
1512 close(fd);
1513
1514 return StringValue(strdup(success ? "t" : ""));
1515}
1516
Doug Zongkerc704e062014-05-23 08:40:35 -07001517Value* EnableRebootFn(const char* name, State* state, int argc, Expr* argv[]) {
1518 if (argc != 0) {
1519 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1520 }
1521 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
1522 fprintf(ui->cmd_pipe, "enable_reboot\n");
1523 return StringValue(strdup("t"));
1524}
1525
Doug Zongker9931f7f2009-06-10 14:11:53 -07001526void RegisterInstallFunctions() {
1527 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001528 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001529 RegisterFunction("unmount", UnmountFn);
1530 RegisterFunction("format", FormatFn);
1531 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001532 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001533 RegisterFunction("delete", DeleteFn);
1534 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001535 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1536 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001537 RegisterFunction("symlink", SymlinkFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001538
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001539 // Usage:
1540 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1541 // Example:
1542 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1543 RegisterFunction("set_metadata", SetMetadataFn);
1544
1545 // Usage:
1546 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1547 // Example:
1548 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1549 RegisterFunction("set_metadata_recursive", SetMetadataFn);
1550
Doug Zongker8edb00c2009-06-11 17:21:44 -07001551 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001552 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001553 RegisterFunction("write_raw_image", WriteRawImageFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001554
1555 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001556 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1557 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001558
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001559 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001560
Doug Zongker512536a2010-02-17 16:11:44 -08001561 RegisterFunction("read_file", ReadFileFn);
1562 RegisterFunction("sha1_check", Sha1CheckFn);
Michael Rungece7ca712013-11-06 17:42:20 -08001563 RegisterFunction("rename", RenameFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001564
Doug Zongkerd0181b82011-10-19 10:51:12 -07001565 RegisterFunction("wipe_cache", WipeCacheFn);
1566
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001567 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001568
1569 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001570
1571 RegisterFunction("reboot_now", RebootNowFn);
1572 RegisterFunction("get_stage", GetStageFn);
1573 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -07001574
1575 RegisterFunction("enable_reboot", EnableRebootFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001576}