blob: 95c8c1111c3dcf840fa7d892499cde81823902fe [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 Runged4a63422014-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));
381 } else if (rename(src_name, dst_name) != 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700382 ErrorAbort(state, "Rename of %s to %s failed, error %s",
Michael Rungece7ca712013-11-06 17:42:20 -0800383 src_name, dst_name, strerror(errno));
384 } else {
385 result = dst_name;
386 }
387
388done:
389 free(src_name);
390 if (result != dst_name) free(dst_name);
391 return StringValue(result);
392}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700393
Doug Zongker512536a2010-02-17 16:11:44 -0800394Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700395 char** paths = malloc(argc * sizeof(char*));
396 int i;
397 for (i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700398 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700399 if (paths[i] == NULL) {
400 int j;
401 for (j = 0; j < i; ++i) {
402 free(paths[j]);
403 }
404 free(paths);
405 return NULL;
406 }
407 }
408
409 bool recursive = (strcmp(name, "delete_recursive") == 0);
410
411 int success = 0;
412 for (i = 0; i < argc; ++i) {
413 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
414 ++success;
415 free(paths[i]);
416 }
417 free(paths);
418
419 char buffer[10];
420 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800421 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700422}
423
Doug Zongker8edb00c2009-06-11 17:21:44 -0700424
Doug Zongker512536a2010-02-17 16:11:44 -0800425Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700426 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700427 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700428 }
429 char* frac_str;
430 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700431 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700432 return NULL;
433 }
434
435 double frac = strtod(frac_str, NULL);
436 int sec = strtol(sec_str, NULL, 10);
437
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700438 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700439 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
440
Doug Zongker9931f7f2009-06-10 14:11:53 -0700441 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800442 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700443}
444
Doug Zongker512536a2010-02-17 16:11:44 -0800445Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700446 if (argc != 1) {
447 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
448 }
449 char* frac_str;
450 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
451 return NULL;
452 }
453
454 double frac = strtod(frac_str, NULL);
455
456 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
457 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
458
Doug Zongker512536a2010-02-17 16:11:44 -0800459 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700460}
461
Doug Zongker8edb00c2009-06-11 17:21:44 -0700462// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800463Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700464 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700465 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700466 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700467 }
468 char* zip_path;
469 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700470 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700471
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700472 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700473
474 // To create a consistent system image, never use the clock for timestamps.
475 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
476
477 bool success = mzExtractRecursive(za, zip_path, dest_path,
478 MZ_EXTRACT_FILES_ONLY, &timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500479 NULL, NULL, sehandle);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700480 free(zip_path);
481 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800482 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700483}
484
Doug Zongker8edb00c2009-06-11 17:21:44 -0700485
486// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800487// or
488// package_extract_file(package_path)
489// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800490// function (the char* returned is actually a FileContents*).
491Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700492 int argc, Expr* argv[]) {
Doug Zongker5f875bf2014-08-22 14:53:43 -0700493 if (argc < 1 || argc > 2) {
494 return ErrorAbort(state, "%s() expects 1 or 2 args, got %d",
Doug Zongker6aece332010-02-01 14:40:12 -0800495 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700496 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700497 bool success = false;
Doug Zongker43772d22014-06-09 14:13:19 -0700498
499 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker43772d22014-06-09 14:13:19 -0700500
Doug Zongker5f875bf2014-08-22 14:53:43 -0700501 if (argc == 2) {
502 // The two-argument version extracts to a file.
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800503
504 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700505
Doug Zongker6aece332010-02-01 14:40:12 -0800506 char* zip_path;
507 char* dest_path;
Doug Zongker5f875bf2014-08-22 14:53:43 -0700508 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800509
Doug Zongker6aece332010-02-01 14:40:12 -0800510 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
511 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700512 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800513 goto done2;
514 }
515
516 FILE* f = fopen(dest_path, "wb");
517 if (f == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700518 printf("%s: can't open %s for write: %s\n",
Doug Zongker6aece332010-02-01 14:40:12 -0800519 name, dest_path, strerror(errno));
520 goto done2;
521 }
Doug Zongker5f875bf2014-08-22 14:53:43 -0700522 success = mzExtractZipEntryToFile(za, entry, fileno(f));
Doug Zongker6aece332010-02-01 14:40:12 -0800523 fclose(f);
524
525 done2:
526 free(zip_path);
527 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800528 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800529 } else {
530 // The one-argument version returns the contents of the file
531 // as the result.
532
533 char* zip_path;
Doug Zongker512536a2010-02-17 16:11:44 -0800534 Value* v = malloc(sizeof(Value));
535 v->type = VAL_BLOB;
536 v->size = -1;
537 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800538
539 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
540
541 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
542 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
543 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700544 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800545 goto done1;
546 }
547
Doug Zongker512536a2010-02-17 16:11:44 -0800548 v->size = mzGetZipEntryUncompLen(entry);
549 v->data = malloc(v->size);
550 if (v->data == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700551 printf("%s: failed to allocate %ld bytes for %s\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800552 name, (long)v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800553 goto done1;
554 }
555
Doug Zongker512536a2010-02-17 16:11:44 -0800556 success = mzExtractZipEntryToBuffer(za, entry,
557 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800558
559 done1:
560 free(zip_path);
561 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800562 free(v->data);
563 v->data = NULL;
564 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800565 }
Doug Zongker512536a2010-02-17 16:11:44 -0800566 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700567 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700568}
569
Doug Zongkera23075f2012-08-06 16:19:09 -0700570// Create all parent directories of name, if necessary.
571static int make_parents(char* name) {
572 char* p;
573 for (p = name + (strlen(name)-1); p > name; --p) {
574 if (*p != '/') continue;
575 *p = '\0';
576 if (make_parents(name) < 0) return -1;
577 int result = mkdir(name, 0700);
Michael Rungea91ecc52014-07-21 17:40:02 -0700578 if (result == 0) printf("created [%s]\n", name);
Doug Zongkera23075f2012-08-06 16:19:09 -0700579 *p = '/';
580 if (result == 0 || errno == EEXIST) {
581 // successfully created or already existed; we're done
582 return 0;
583 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700584 printf("failed to mkdir %s: %s\n", name, strerror(errno));
Doug Zongkera23075f2012-08-06 16:19:09 -0700585 return -1;
586 }
587 }
588 return 0;
589}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700590
Doug Zongker9931f7f2009-06-10 14:11:53 -0700591// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700592// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800593Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700594 if (argc == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700595 return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700596 }
597 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700598 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700599 if (target == NULL) return NULL;
600
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700601 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700602 if (srcs == NULL) {
603 free(target);
604 return NULL;
605 }
606
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700607 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700608 int i;
609 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700610 if (unlink(srcs[i]) < 0) {
611 if (errno != ENOENT) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700612 printf("%s: failed to remove %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700613 name, srcs[i], strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700614 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700615 }
616 }
Doug Zongkera23075f2012-08-06 16:19:09 -0700617 if (make_parents(srcs[i])) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700618 printf("%s: failed to symlink %s to %s: making parents failed\n",
Doug Zongkera23075f2012-08-06 16:19:09 -0700619 name, srcs[i], target);
620 ++bad;
621 }
Doug Zongker60babf82009-09-18 15:11:24 -0700622 if (symlink(target, srcs[i]) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700623 printf("%s: failed to symlink %s to %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700624 name, srcs[i], target, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700625 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700626 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700627 free(srcs[i]);
628 }
629 free(srcs);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700630 if (bad) {
631 return ErrorAbort(state, "%s: some symlinks failed", name);
632 }
Doug Zongker512536a2010-02-17 16:11:44 -0800633 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700634}
635
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700636struct perm_parsed_args {
637 bool has_uid;
638 uid_t uid;
639 bool has_gid;
640 gid_t gid;
641 bool has_mode;
642 mode_t mode;
643 bool has_fmode;
644 mode_t fmode;
645 bool has_dmode;
646 mode_t dmode;
647 bool has_selabel;
648 char* selabel;
649 bool has_capabilities;
650 uint64_t capabilities;
651};
652
Michael Runged4a63422014-10-22 19:48:41 -0700653static struct perm_parsed_args ParsePermArgs(State * state, int argc, char** args) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700654 int i;
655 struct perm_parsed_args parsed;
656 int bad = 0;
657 static int max_warnings = 20;
658
659 memset(&parsed, 0, sizeof(parsed));
660
661 for (i = 1; i < argc; i += 2) {
662 if (strcmp("uid", args[i]) == 0) {
663 int64_t uid;
664 if (sscanf(args[i+1], "%" SCNd64, &uid) == 1) {
665 parsed.uid = uid;
666 parsed.has_uid = true;
667 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700668 uiPrintf(state, "ParsePermArgs: invalid UID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700669 bad++;
670 }
671 continue;
672 }
673 if (strcmp("gid", args[i]) == 0) {
674 int64_t gid;
675 if (sscanf(args[i+1], "%" SCNd64, &gid) == 1) {
676 parsed.gid = gid;
677 parsed.has_gid = true;
678 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700679 uiPrintf(state, "ParsePermArgs: invalid GID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700680 bad++;
681 }
682 continue;
683 }
684 if (strcmp("mode", args[i]) == 0) {
685 int32_t mode;
686 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
687 parsed.mode = mode;
688 parsed.has_mode = true;
689 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700690 uiPrintf(state, "ParsePermArgs: invalid mode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700691 bad++;
692 }
693 continue;
694 }
695 if (strcmp("dmode", args[i]) == 0) {
696 int32_t mode;
697 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
698 parsed.dmode = mode;
699 parsed.has_dmode = true;
700 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700701 uiPrintf(state, "ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700702 bad++;
703 }
704 continue;
705 }
706 if (strcmp("fmode", args[i]) == 0) {
707 int32_t mode;
708 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
709 parsed.fmode = mode;
710 parsed.has_fmode = true;
711 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700712 uiPrintf(state, "ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700713 bad++;
714 }
715 continue;
716 }
717 if (strcmp("capabilities", args[i]) == 0) {
718 int64_t capabilities;
719 if (sscanf(args[i+1], "%" SCNi64, &capabilities) == 1) {
720 parsed.capabilities = capabilities;
721 parsed.has_capabilities = true;
722 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700723 uiPrintf(state, "ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700724 bad++;
725 }
726 continue;
727 }
728 if (strcmp("selabel", args[i]) == 0) {
729 if (args[i+1][0] != '\0') {
730 parsed.selabel = args[i+1];
731 parsed.has_selabel = true;
732 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700733 uiPrintf(state, "ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700734 bad++;
735 }
736 continue;
737 }
738 if (max_warnings != 0) {
739 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i]);
740 max_warnings--;
741 if (max_warnings == 0) {
742 printf("ParsedPermArgs: suppressing further warnings\n");
743 }
744 }
745 }
746 return parsed;
747}
748
749static int ApplyParsedPerms(
Michael Runged4a63422014-10-22 19:48:41 -0700750 State * state,
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700751 const char* filename,
752 const struct stat *statptr,
753 struct perm_parsed_args parsed)
754{
755 int bad = 0;
756
Nick Kraleviche4612512013-09-10 15:34:19 -0700757 /* ignore symlinks */
758 if (S_ISLNK(statptr->st_mode)) {
759 return 0;
760 }
761
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700762 if (parsed.has_uid) {
763 if (chown(filename, parsed.uid, -1) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700764 uiPrintf(state, "ApplyParsedPerms: chown of %s to %d failed: %s\n",
765 filename, parsed.uid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700766 bad++;
767 }
768 }
769
770 if (parsed.has_gid) {
771 if (chown(filename, -1, parsed.gid) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700772 uiPrintf(state, "ApplyParsedPerms: chgrp of %s to %d failed: %s\n",
773 filename, parsed.gid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700774 bad++;
775 }
776 }
777
778 if (parsed.has_mode) {
779 if (chmod(filename, parsed.mode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700780 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
781 filename, parsed.mode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700782 bad++;
783 }
784 }
785
786 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
787 if (chmod(filename, parsed.dmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700788 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
789 filename, parsed.dmode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700790 bad++;
791 }
792 }
793
794 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
795 if (chmod(filename, parsed.fmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700796 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700797 filename, parsed.fmode, strerror(errno));
798 bad++;
799 }
800 }
801
802 if (parsed.has_selabel) {
803 // TODO: Don't silently ignore ENOTSUP
804 if (lsetfilecon(filename, parsed.selabel) && (errno != ENOTSUP)) {
Michael Runged4a63422014-10-22 19:48:41 -0700805 uiPrintf(state, "ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n",
806 filename, parsed.selabel, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700807 bad++;
808 }
809 }
810
811 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
812 if (parsed.capabilities == 0) {
813 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
814 // Report failure unless it's ENODATA (attribute not set)
Michael Runged4a63422014-10-22 19:48:41 -0700815 uiPrintf(state, "ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700816 filename, parsed.capabilities, strerror(errno));
817 bad++;
818 }
819 } else {
820 struct vfs_cap_data cap_data;
821 memset(&cap_data, 0, sizeof(cap_data));
822 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
823 cap_data.data[0].permitted = (uint32_t) (parsed.capabilities & 0xffffffff);
824 cap_data.data[0].inheritable = 0;
825 cap_data.data[1].permitted = (uint32_t) (parsed.capabilities >> 32);
826 cap_data.data[1].inheritable = 0;
827 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700828 uiPrintf(state, "ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n",
829 filename, parsed.capabilities, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700830 bad++;
831 }
832 }
833 }
834
835 return bad;
836}
837
838// nftw doesn't allow us to pass along context, so we need to use
839// global variables. *sigh*
840static struct perm_parsed_args recursive_parsed_args;
Michael Runged4a63422014-10-22 19:48:41 -0700841static State* recursive_state;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700842
843static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr,
844 int fileflags, struct FTW *pfwt) {
Michael Runged4a63422014-10-22 19:48:41 -0700845 return ApplyParsedPerms(recursive_state, filename, statptr, recursive_parsed_args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700846}
847
848static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
849 int i;
850 int bad = 0;
851 static int nwarnings = 0;
852 struct stat sb;
853 Value* result = NULL;
854
855 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
856
857 if ((argc % 2) != 1) {
858 return ErrorAbort(state, "%s() expects an odd number of arguments, got %d",
859 name, argc);
860 }
861
862 char** args = ReadVarArgs(state, argc, argv);
863 if (args == NULL) return NULL;
864
865 if (lstat(args[0], &sb) == -1) {
866 result = ErrorAbort(state, "%s: Error on lstat of \"%s\": %s", name, args[0], strerror(errno));
867 goto done;
868 }
869
Michael Runged4a63422014-10-22 19:48:41 -0700870 struct perm_parsed_args parsed = ParsePermArgs(state, argc, args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700871
872 if (recursive) {
873 recursive_parsed_args = parsed;
Michael Runged4a63422014-10-22 19:48:41 -0700874 recursive_state = state;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700875 bad += nftw(args[0], do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
876 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
Michael Runged4a63422014-10-22 19:48:41 -0700877 recursive_state = NULL;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700878 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700879 bad += ApplyParsedPerms(state, args[0], &sb, parsed);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700880 }
881
882done:
883 for (i = 0; i < argc; ++i) {
884 free(args[i]);
885 }
886 free(args);
887
888 if (result != NULL) {
889 return result;
890 }
891
892 if (bad > 0) {
893 return ErrorAbort(state, "%s: some changes failed", name);
894 }
895
896 return StringValue(strdup(""));
897}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700898
Doug Zongker512536a2010-02-17 16:11:44 -0800899Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700900 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700901 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700902 }
903 char* key;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700904 key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700905 if (key == NULL) return NULL;
906
907 char value[PROPERTY_VALUE_MAX];
908 property_get(key, value, "");
909 free(key);
910
Doug Zongker512536a2010-02-17 16:11:44 -0800911 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700912}
913
914
Doug Zongker47cace92009-06-18 10:11:50 -0700915// file_getprop(file, key)
916//
917// interprets 'file' as a getprop-style file (key=value pairs, one
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700918// per line. # comment lines,blank lines, lines without '=' ignored),
919// and returns the value for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800920Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700921 char* result = NULL;
922 char* buffer = NULL;
923 char* filename;
924 char* key;
925 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
926 return NULL;
927 }
928
929 struct stat st;
930 if (stat(filename, &st) < 0) {
931 ErrorAbort(state, "%s: failed to stat \"%s\": %s",
932 name, filename, strerror(errno));
933 goto done;
934 }
935
936#define MAX_FILE_GETPROP_SIZE 65536
937
938 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
939 ErrorAbort(state, "%s too large for %s (max %d)",
940 filename, name, MAX_FILE_GETPROP_SIZE);
941 goto done;
942 }
943
944 buffer = malloc(st.st_size+1);
945 if (buffer == NULL) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700946 ErrorAbort(state, "%s: failed to alloc %lld bytes", name, (long long)st.st_size+1);
Doug Zongker47cace92009-06-18 10:11:50 -0700947 goto done;
948 }
949
950 FILE* f = fopen(filename, "rb");
951 if (f == NULL) {
952 ErrorAbort(state, "%s: failed to open %s: %s",
953 name, filename, strerror(errno));
954 goto done;
955 }
956
957 if (fread(buffer, 1, st.st_size, f) != st.st_size) {
Doug Zongkera23075f2012-08-06 16:19:09 -0700958 ErrorAbort(state, "%s: failed to read %lld bytes from %s",
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700959 name, (long long)st.st_size+1, filename);
Doug Zongker47cace92009-06-18 10:11:50 -0700960 fclose(f);
961 goto done;
962 }
963 buffer[st.st_size] = '\0';
964
965 fclose(f);
966
967 char* line = strtok(buffer, "\n");
968 do {
969 // skip whitespace at start of line
970 while (*line && isspace(*line)) ++line;
971
972 // comment or blank line: skip to next line
973 if (*line == '\0' || *line == '#') continue;
974
975 char* equal = strchr(line, '=');
976 if (equal == NULL) {
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700977 continue;
Doug Zongker47cace92009-06-18 10:11:50 -0700978 }
979
980 // trim whitespace between key and '='
981 char* key_end = equal-1;
982 while (key_end > line && isspace(*key_end)) --key_end;
983 key_end[1] = '\0';
984
985 // not the key we're looking for
986 if (strcmp(key, line) != 0) continue;
987
988 // skip whitespace after the '=' to the start of the value
989 char* val_start = equal+1;
990 while(*val_start && isspace(*val_start)) ++val_start;
991
992 // trim trailing whitespace
993 char* val_end = val_start + strlen(val_start)-1;
994 while (val_end > val_start && isspace(*val_end)) --val_end;
995 val_end[1] = '\0';
996
997 result = strdup(val_start);
998 break;
999
1000 } while ((line = strtok(NULL, "\n")));
1001
1002 if (result == NULL) result = strdup("");
1003
1004 done:
1005 free(filename);
1006 free(key);
1007 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -08001008 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -07001009}
1010
1011
Doug Zongker8edb00c2009-06-11 17:21:44 -07001012static bool write_raw_image_cb(const unsigned char* data,
1013 int data_len, void* ctx) {
1014 int r = mtd_write_data((MtdWriteContext*)ctx, (const char *)data, data_len);
1015 if (r == data_len) return true;
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001016 printf("%s\n", strerror(errno));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001017 return false;
1018}
1019
Doug Zongker179b2d92011-04-12 15:49:04 -07001020// write_raw_image(filename_or_blob, partition)
Doug Zongker512536a2010-02-17 16:11:44 -08001021Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001022 char* result = NULL;
1023
Doug Zongker179b2d92011-04-12 15:49:04 -07001024 Value* partition_value;
1025 Value* contents;
1026 if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001027 return NULL;
1028 }
1029
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001030 char* partition = NULL;
Doug Zongker179b2d92011-04-12 15:49:04 -07001031 if (partition_value->type != VAL_STRING) {
1032 ErrorAbort(state, "partition argument to %s must be string", name);
1033 goto done;
1034 }
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001035 partition = partition_value->data;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001036 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001037 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001038 goto done;
1039 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001040 if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001041 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001042 goto done;
1043 }
1044
1045 mtd_scan_partitions();
1046 const MtdPartition* mtd = mtd_find_partition_by_name(partition);
1047 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001048 printf("%s: no mtd partition named \"%s\"\n", name, partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001049 result = strdup("");
1050 goto done;
1051 }
1052
1053 MtdWriteContext* ctx = mtd_write_partition(mtd);
1054 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001055 printf("%s: can't write mtd partition \"%s\"\n",
Doug Zongker8edb00c2009-06-11 17:21:44 -07001056 name, partition);
1057 result = strdup("");
1058 goto done;
1059 }
1060
1061 bool success;
1062
Doug Zongker179b2d92011-04-12 15:49:04 -07001063 if (contents->type == VAL_STRING) {
1064 // we're given a filename as the contents
1065 char* filename = contents->data;
1066 FILE* f = fopen(filename, "rb");
1067 if (f == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001068 printf("%s: can't open %s: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -07001069 name, filename, strerror(errno));
1070 result = strdup("");
1071 goto done;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001072 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001073
1074 success = true;
1075 char* buffer = malloc(BUFSIZ);
1076 int read;
1077 while (success && (read = fread(buffer, 1, BUFSIZ, f)) > 0) {
1078 int wrote = mtd_write_data(ctx, buffer, read);
1079 success = success && (wrote == read);
1080 }
1081 free(buffer);
1082 fclose(f);
1083 } else {
1084 // we're given a blob as the contents
1085 ssize_t wrote = mtd_write_data(ctx, contents->data, contents->size);
1086 success = (wrote == contents->size);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001087 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001088 if (!success) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001089 printf("mtd_write_data to %s failed: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -07001090 partition, strerror(errno));
1091 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001092
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001093 if (mtd_erase_blocks(ctx, -1) == -1) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001094 printf("%s: error erasing blocks of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001095 }
1096 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001097 printf("%s: error closing write of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001098 }
1099
Doug Zongker179b2d92011-04-12 15:49:04 -07001100 printf("%s %s partition\n",
1101 success ? "wrote" : "failed to write", partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001102
1103 result = success ? partition : strdup("");
1104
1105done:
Doug Zongker179b2d92011-04-12 15:49:04 -07001106 if (result != partition) FreeValue(partition_value);
1107 FreeValue(contents);
Doug Zongker512536a2010-02-17 16:11:44 -08001108 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001109}
1110
Doug Zongker8edb00c2009-06-11 17:21:44 -07001111// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001112Value* ApplyPatchSpaceFn(const char* name, State* state,
1113 int argc, Expr* argv[]) {
1114 char* bytes_str;
1115 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
1116 return NULL;
1117 }
1118
1119 char* endptr;
1120 size_t bytes = strtol(bytes_str, &endptr, 10);
1121 if (bytes == 0 && endptr == bytes_str) {
1122 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n",
1123 name, bytes_str);
1124 free(bytes_str);
1125 return NULL;
1126 }
1127
1128 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
1129}
1130
Doug Zongker52b40362014-02-10 15:30:30 -08001131// apply_patch(file, size, init_sha1, tgt_sha1, patch)
1132
Doug Zongker512536a2010-02-17 16:11:44 -08001133Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001134 if (argc < 6 || (argc % 2) == 1) {
1135 return ErrorAbort(state, "%s(): expected at least 6 args and an "
1136 "even number, got %d",
1137 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001138 }
1139
Doug Zongkerc4351c72010-02-22 14:46:32 -08001140 char* source_filename;
1141 char* target_filename;
1142 char* target_sha1;
1143 char* target_size_str;
1144 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
1145 &target_sha1, &target_size_str) < 0) {
1146 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001147 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001148
Doug Zongkerc4351c72010-02-22 14:46:32 -08001149 char* endptr;
1150 size_t target_size = strtol(target_size_str, &endptr, 10);
1151 if (target_size == 0 && endptr == target_size_str) {
1152 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count",
1153 name, target_size_str);
1154 free(source_filename);
1155 free(target_filename);
1156 free(target_sha1);
1157 free(target_size_str);
1158 return NULL;
1159 }
1160
1161 int patchcount = (argc-4) / 2;
1162 Value** patches = ReadValueVarArgs(state, argc-4, argv+4);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001163
1164 int i;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001165 for (i = 0; i < patchcount; ++i) {
1166 if (patches[i*2]->type != VAL_STRING) {
1167 ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i);
1168 break;
1169 }
1170 if (patches[i*2+1]->type != VAL_BLOB) {
1171 ErrorAbort(state, "%s(): patch #%d is not blob", name, i);
1172 break;
1173 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001174 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001175 if (i != patchcount) {
1176 for (i = 0; i < patchcount*2; ++i) {
1177 FreeValue(patches[i]);
1178 }
1179 free(patches);
1180 return NULL;
1181 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001182
Doug Zongkerc4351c72010-02-22 14:46:32 -08001183 char** patch_sha_str = malloc(patchcount * sizeof(char*));
1184 for (i = 0; i < patchcount; ++i) {
1185 patch_sha_str[i] = patches[i*2]->data;
1186 patches[i*2]->data = NULL;
1187 FreeValue(patches[i*2]);
1188 patches[i] = patches[i*2+1];
Doug Zongker8edb00c2009-06-11 17:21:44 -07001189 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001190
1191 int result = applypatch(source_filename, target_filename,
1192 target_sha1, target_size,
Doug Zongkera3ccba62012-08-20 15:28:02 -07001193 patchcount, patch_sha_str, patches, NULL);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001194
1195 for (i = 0; i < patchcount; ++i) {
1196 FreeValue(patches[i]);
1197 }
1198 free(patch_sha_str);
1199 free(patches);
1200
1201 return StringValue(strdup(result == 0 ? "t" : ""));
1202}
1203
1204// apply_patch_check(file, [sha1_1, ...])
1205Value* ApplyPatchCheckFn(const char* name, State* state,
1206 int argc, Expr* argv[]) {
1207 if (argc < 1) {
1208 return ErrorAbort(state, "%s(): expected at least 1 arg, got %d",
1209 name, argc);
1210 }
1211
1212 char* filename;
1213 if (ReadArgs(state, argv, 1, &filename) < 0) {
1214 return NULL;
1215 }
1216
1217 int patchcount = argc-1;
1218 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
1219
1220 int result = applypatch_check(filename, patchcount, sha1s);
1221
1222 int i;
1223 for (i = 0; i < patchcount; ++i) {
1224 free(sha1s[i]);
1225 }
1226 free(sha1s);
1227
1228 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001229}
1230
Doug Zongker512536a2010-02-17 16:11:44 -08001231Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001232 char** args = ReadVarArgs(state, argc, argv);
1233 if (args == NULL) {
1234 return NULL;
1235 }
1236
1237 int size = 0;
1238 int i;
1239 for (i = 0; i < argc; ++i) {
1240 size += strlen(args[i]);
1241 }
1242 char* buffer = malloc(size+1);
1243 size = 0;
1244 for (i = 0; i < argc; ++i) {
1245 strcpy(buffer+size, args[i]);
1246 size += strlen(args[i]);
1247 free(args[i]);
1248 }
1249 free(args);
1250 buffer[size] = '\0';
Michael Runged4a63422014-10-22 19:48:41 -07001251 uiPrint(state, buffer);
Doug Zongker512536a2010-02-17 16:11:44 -08001252 return StringValue(buffer);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001253}
1254
Doug Zongkerd0181b82011-10-19 10:51:12 -07001255Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
1256 if (argc != 0) {
1257 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1258 }
1259 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
1260 return StringValue(strdup("t"));
1261}
1262
Doug Zongker512536a2010-02-17 16:11:44 -08001263Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001264 if (argc < 1) {
1265 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1266 }
1267 char** args = ReadVarArgs(state, argc, argv);
1268 if (args == NULL) {
1269 return NULL;
1270 }
1271
1272 char** args2 = malloc(sizeof(char*) * (argc+1));
1273 memcpy(args2, args, sizeof(char*) * argc);
1274 args2[argc] = NULL;
1275
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001276 printf("about to run program [%s] with %d args\n", args2[0], argc);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001277
1278 pid_t child = fork();
1279 if (child == 0) {
1280 execv(args2[0], args2);
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001281 printf("run_program: execv failed: %s\n", strerror(errno));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001282 _exit(1);
1283 }
1284 int status;
1285 waitpid(child, &status, 0);
1286 if (WIFEXITED(status)) {
1287 if (WEXITSTATUS(status) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001288 printf("run_program: child exited with status %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001289 WEXITSTATUS(status));
1290 }
1291 } else if (WIFSIGNALED(status)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001292 printf("run_program: child terminated by signal %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001293 WTERMSIG(status));
1294 }
1295
1296 int i;
1297 for (i = 0; i < argc; ++i) {
1298 free(args[i]);
1299 }
1300 free(args);
1301 free(args2);
1302
1303 char buffer[20];
1304 sprintf(buffer, "%d", status);
1305
Doug Zongker512536a2010-02-17 16:11:44 -08001306 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001307}
1308
Doug Zongker512536a2010-02-17 16:11:44 -08001309// sha1_check(data)
1310// to return the sha1 of the data (given in the format returned by
1311// read_file).
1312//
1313// sha1_check(data, sha1_hex, [sha1_hex, ...])
1314// returns the sha1 of the file if it matches any of the hex
1315// strings passed, or "" if it does not equal any of them.
1316//
1317Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1318 if (argc < 1) {
1319 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1320 }
1321
1322 Value** args = ReadValueVarArgs(state, argc, argv);
1323 if (args == NULL) {
1324 return NULL;
1325 }
1326
1327 if (args[0]->size < 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001328 return StringValue(strdup(""));
1329 }
1330 uint8_t digest[SHA_DIGEST_SIZE];
Doug Zongkerbac7fba2013-04-10 11:32:17 -07001331 SHA_hash(args[0]->data, args[0]->size, digest);
Doug Zongker512536a2010-02-17 16:11:44 -08001332 FreeValue(args[0]);
1333
1334 if (argc == 1) {
1335 return StringValue(PrintSha1(digest));
1336 }
1337
1338 int i;
1339 uint8_t* arg_digest = malloc(SHA_DIGEST_SIZE);
1340 for (i = 1; i < argc; ++i) {
1341 if (args[i]->type != VAL_STRING) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001342 printf("%s(): arg %d is not a string; skipping",
Doug Zongker512536a2010-02-17 16:11:44 -08001343 name, i);
1344 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1345 // Warn about bad args and skip them.
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001346 printf("%s(): error parsing \"%s\" as sha-1; skipping",
1347 name, args[i]->data);
Doug Zongker512536a2010-02-17 16:11:44 -08001348 } else if (memcmp(digest, arg_digest, SHA_DIGEST_SIZE) == 0) {
1349 break;
1350 }
1351 FreeValue(args[i]);
1352 }
1353 if (i >= argc) {
1354 // Didn't match any of the hex strings; return false.
1355 return StringValue(strdup(""));
1356 }
1357 // Found a match; free all the remaining arguments and return the
1358 // matched one.
1359 int j;
1360 for (j = i+1; j < argc; ++j) {
1361 FreeValue(args[j]);
1362 }
1363 return args[i];
1364}
1365
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001366// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001367// is actually a FileContents*).
1368Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1369 if (argc != 1) {
1370 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1371 }
1372 char* filename;
1373 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1374
1375 Value* v = malloc(sizeof(Value));
1376 v->type = VAL_BLOB;
1377
1378 FileContents fc;
Doug Zongkera1bc1482014-02-13 15:18:19 -08001379 if (LoadFileContents(filename, &fc) != 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001380 free(filename);
Michael Runge6eed2242013-12-13 17:13:11 -08001381 v->size = -1;
1382 v->data = NULL;
Doug Zongker512536a2010-02-17 16:11:44 -08001383 free(fc.data);
Michael Runge6eed2242013-12-13 17:13:11 -08001384 return v;
Doug Zongker512536a2010-02-17 16:11:44 -08001385 }
1386
1387 v->size = fc.size;
1388 v->data = (char*)fc.data;
1389
1390 free(filename);
1391 return v;
1392}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001393
Doug Zongkerc87bab12013-11-25 13:53:25 -08001394// Immediately reboot the device. Recovery is not finished normally,
1395// so if you reboot into recovery it will re-start applying the
1396// current package (because nothing has cleared the copy of the
1397// arguments stored in the BCB).
1398//
1399// The argument is the partition name passed to the android reboot
1400// property. It can be "recovery" to boot from the recovery
1401// partition, or "" (empty string) to boot from the regular boot
1402// partition.
1403Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) {
1404 if (argc != 2) {
1405 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1406 }
1407
1408 char* filename;
1409 char* property;
1410 if (ReadArgs(state, argv, 2, &filename, &property) < 0) return NULL;
1411
1412 char buffer[80];
1413
1414 // zero out the 'command' field of the bootloader message.
1415 memset(buffer, 0, sizeof(((struct bootloader_message*)0)->command));
1416 FILE* f = fopen(filename, "r+b");
1417 fseek(f, offsetof(struct bootloader_message, command), SEEK_SET);
1418 fwrite(buffer, sizeof(((struct bootloader_message*)0)->command), 1, f);
1419 fclose(f);
1420 free(filename);
1421
1422 strcpy(buffer, "reboot,");
1423 if (property != NULL) {
1424 strncat(buffer, property, sizeof(buffer)-10);
1425 }
1426
1427 property_set(ANDROID_RB_PROPERTY, buffer);
1428
1429 sleep(5);
1430 free(property);
1431 ErrorAbort(state, "%s() failed to reboot", name);
1432 return NULL;
1433}
1434
1435// Store a string value somewhere that future invocations of recovery
1436// can access it. This value is called the "stage" and can be used to
1437// drive packages that need to do reboots in the middle of
1438// installation and keep track of where they are in the multi-stage
1439// install.
1440//
1441// The first argument is the block device for the misc partition
1442// ("/misc" in the fstab), which is where this value is stored. The
1443// second argument is the string to store; it should not exceed 31
1444// bytes.
1445Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
1446 if (argc != 2) {
1447 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1448 }
1449
1450 char* filename;
1451 char* stagestr;
1452 if (ReadArgs(state, argv, 2, &filename, &stagestr) < 0) return NULL;
1453
1454 // Store this value in the misc partition, immediately after the
1455 // bootloader message that the main recovery uses to save its
1456 // arguments in case of the device restarting midway through
1457 // package installation.
1458 FILE* f = fopen(filename, "r+b");
1459 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1460 int to_write = strlen(stagestr)+1;
1461 int max_size = sizeof(((struct bootloader_message*)0)->stage);
1462 if (to_write > max_size) {
1463 to_write = max_size;
1464 stagestr[max_size-1] = 0;
1465 }
1466 fwrite(stagestr, to_write, 1, f);
1467 fclose(f);
1468
1469 free(stagestr);
1470 return StringValue(filename);
1471}
1472
1473// Return the value most recently saved with SetStageFn. The argument
1474// is the block device for the misc partition.
1475Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001476 if (argc != 1) {
Doug Zongkerc87bab12013-11-25 13:53:25 -08001477 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1478 }
1479
1480 char* filename;
1481 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1482
1483 char buffer[sizeof(((struct bootloader_message*)0)->stage)];
1484 FILE* f = fopen(filename, "rb");
1485 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1486 fread(buffer, sizeof(buffer), 1, f);
1487 fclose(f);
1488 buffer[sizeof(buffer)-1] = '\0';
1489
1490 return StringValue(strdup(buffer));
1491}
1492
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001493Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) {
1494 if (argc != 2) {
1495 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1496 }
1497
1498 char* filename;
1499 char* len_str;
1500 if (ReadArgs(state, argv, 2, &filename, &len_str) < 0) return NULL;
1501
1502 size_t len = strtoull(len_str, NULL, 0);
1503 int fd = open(filename, O_WRONLY, 0644);
1504 int success = wipe_block_device(fd, len);
1505
1506 free(filename);
1507 free(len_str);
1508
1509 close(fd);
1510
1511 return StringValue(strdup(success ? "t" : ""));
1512}
1513
Doug Zongkerc704e062014-05-23 08:40:35 -07001514Value* EnableRebootFn(const char* name, State* state, int argc, Expr* argv[]) {
1515 if (argc != 0) {
1516 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1517 }
1518 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
1519 fprintf(ui->cmd_pipe, "enable_reboot\n");
1520 return StringValue(strdup("t"));
1521}
1522
Doug Zongker9931f7f2009-06-10 14:11:53 -07001523void RegisterInstallFunctions() {
1524 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001525 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001526 RegisterFunction("unmount", UnmountFn);
1527 RegisterFunction("format", FormatFn);
1528 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001529 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001530 RegisterFunction("delete", DeleteFn);
1531 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001532 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1533 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001534 RegisterFunction("symlink", SymlinkFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001535
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001536 // Usage:
1537 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1538 // Example:
1539 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1540 RegisterFunction("set_metadata", SetMetadataFn);
1541
1542 // Usage:
1543 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1544 // Example:
1545 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1546 RegisterFunction("set_metadata_recursive", SetMetadataFn);
1547
Doug Zongker8edb00c2009-06-11 17:21:44 -07001548 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001549 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001550 RegisterFunction("write_raw_image", WriteRawImageFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001551
1552 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001553 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1554 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001555
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001556 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001557
Doug Zongker512536a2010-02-17 16:11:44 -08001558 RegisterFunction("read_file", ReadFileFn);
1559 RegisterFunction("sha1_check", Sha1CheckFn);
Michael Rungece7ca712013-11-06 17:42:20 -08001560 RegisterFunction("rename", RenameFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001561
Doug Zongkerd0181b82011-10-19 10:51:12 -07001562 RegisterFunction("wipe_cache", WipeCacheFn);
1563
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001564 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001565
1566 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001567
1568 RegisterFunction("reboot_now", RebootNowFn);
1569 RegisterFunction("get_stage", GetStageFn);
1570 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -07001571
1572 RegisterFunction("enable_reboot", EnableRebootFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001573}