blob: 003064799771f8df442c60ae3e1a546f1d914045 [file] [log] [blame]
Doug Zongker9931f7f2009-06-10 14:11:53 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Doug Zongkerfbf3c102009-06-24 09:36:20 -070017#include <ctype.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070018#include <errno.h>
19#include <stdarg.h>
Doug Zongkerfbf3c102009-06-24 09:36:20 -070020#include <stdio.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070021#include <stdlib.h>
22#include <string.h>
23#include <sys/mount.h>
24#include <sys/stat.h>
25#include <sys/types.h>
Doug Zongkera3f89ea2009-09-10 14:10:48 -070026#include <sys/wait.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070027#include <unistd.h>
Hristo Bojinovdb314d62010-08-02 10:29:49 -070028#include <fcntl.h>
29#include <time.h>
Nick Kralevich5dbdef02013-09-07 14:41:06 -070030#include <selinux/selinux.h>
31#include <ftw.h>
32#include <sys/capability.h>
33#include <sys/xattr.h>
34#include <linux/xattr.h>
35#include <inttypes.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070036
Doug Zongkerc87bab12013-11-25 13:53:25 -080037#include "bootloader.h"
38#include "applypatch/applypatch.h"
39#include "cutils/android_reboot.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070040#include "cutils/misc.h"
41#include "cutils/properties.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070042#include "edify/expr.h"
Doug Zongker512536a2010-02-17 16:11:44 -080043#include "mincrypt/sha.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070044#include "minzip/DirUtil.h"
45#include "mtdutils/mounts.h"
46#include "mtdutils/mtdutils.h"
47#include "updater.h"
Doug Zongker512536a2010-02-17 16:11:44 -080048#include "applypatch/applypatch.h"
Dees_Troy512376c2013-09-03 19:39:41 +000049#include "flashutils/flashutils.h"
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -080050#include "install.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070051
Doug Zongker3d177d02010-07-01 09:18:44 -070052#ifdef USE_EXT4
53#include "make_ext4fs.h"
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -080054#include "wipe.h"
Doug Zongker3d177d02010-07-01 09:18:44 -070055#endif
56
Michael Runge75480252014-10-22 19:48:41 -070057void uiPrint(State* state, char* buffer) {
58 char* line = strtok(buffer, "\n");
59 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
60 while (line) {
61 fprintf(ui->cmd_pipe, "ui_print %s\n", line);
62 line = strtok(NULL, "\n");
63 }
64 fprintf(ui->cmd_pipe, "ui_print\n");
65}
66
67__attribute__((__format__(printf, 2, 3))) __nonnull((2))
68void uiPrintf(State* state, const char* format, ...) {
69 char error_msg[1024];
70 va_list ap;
71 va_start(ap, format);
72 vsnprintf(error_msg, sizeof(error_msg), format, ap);
73 va_end(ap);
74 uiPrint(state, error_msg);
75}
76
Doug Zongker52b40362014-02-10 15:30:30 -080077// Take a sha-1 digest and return it as a newly-allocated hex string.
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070078char* PrintSha1(const uint8_t* digest) {
Doug Zongker52b40362014-02-10 15:30:30 -080079 char* buffer = malloc(SHA_DIGEST_SIZE*2 + 1);
80 int i;
81 const char* alphabet = "0123456789abcdef";
82 for (i = 0; i < SHA_DIGEST_SIZE; ++i) {
83 buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf];
84 buffer[i*2+1] = alphabet[digest[i] & 0xf];
85 }
86 buffer[i*2] = '\0';
87 return buffer;
88}
89
Doug Zongker3d177d02010-07-01 09:18:44 -070090// mount(fs_type, partition_type, location, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -070091//
Doug Zongker3d177d02010-07-01 09:18:44 -070092// fs_type="yaffs2" partition_type="MTD" location=partition
93// fs_type="ext4" partition_type="EMMC" location=device
Doug Zongker512536a2010-02-17 16:11:44 -080094Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070095 char* result = NULL;
Michael Rungebd6138c2014-10-22 17:05:08 -070096 if (argc != 4 && argc != 5) {
97 return ErrorAbort(state, "%s() expects 4-5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -070098 }
Doug Zongker3d177d02010-07-01 09:18:44 -070099 char* fs_type;
100 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700101 char* location;
102 char* mount_point;
Michael Rungebd6138c2014-10-22 17:05:08 -0700103 char* mount_options;
104 bool has_mount_options;
105 if (argc == 5) {
106 has_mount_options = true;
107 if (ReadArgs(state, argv, 5, &fs_type, &partition_type,
108 &location, &mount_point, &mount_options) < 0) {
109 return NULL;
110 }
111 } else {
112 has_mount_options = false;
113 if (ReadArgs(state, argv, 4, &fs_type, &partition_type,
Doug Zongker3d177d02010-07-01 09:18:44 -0700114 &location, &mount_point) < 0) {
Michael Rungebd6138c2014-10-22 17:05:08 -0700115 return NULL;
116 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700117 }
118
Doug Zongker3d177d02010-07-01 09:18:44 -0700119 if (strlen(fs_type) == 0) {
120 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
121 goto done;
122 }
123 if (strlen(partition_type) == 0) {
124 ErrorAbort(state, "partition_type argument to %s() can't be empty",
125 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700126 goto done;
127 }
128 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700129 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700130 goto done;
131 }
132 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700133 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700134 goto done;
135 }
136
Stephen Smalley779701d2012-02-09 14:13:23 -0500137 char *secontext = NULL;
138
139 if (sehandle) {
140 selabel_lookup(sehandle, &secontext, mount_point, 0755);
141 setfscreatecon(secontext);
142 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500143
Doug Zongker9931f7f2009-06-10 14:11:53 -0700144 mkdir(mount_point, 0755);
145
Stephen Smalley779701d2012-02-09 14:13:23 -0500146 if (secontext) {
147 freecon(secontext);
148 setfscreatecon(NULL);
149 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500150
Doug Zongker3d177d02010-07-01 09:18:44 -0700151 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700152 mtd_scan_partitions();
153 const MtdPartition* mtd;
154 mtd = mtd_find_partition_by_name(location);
155 if (mtd == NULL) {
Michael Rungef15e31e2014-10-24 14:14:41 -0700156 uiPrintf(state, "%s: no mtd partition named \"%s\"",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700157 name, location);
158 result = strdup("");
159 goto done;
160 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700161 if (mtd_mount_partition(mtd, mount_point, fs_type, 0 /* rw */) != 0) {
Michael Rungef15e31e2014-10-24 14:14:41 -0700162 uiPrintf(state, "mtd mount of %s failed: %s\n",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700163 location, strerror(errno));
164 result = strdup("");
165 goto done;
166 }
167 result = mount_point;
168 } else {
Doug Zongker3d177d02010-07-01 09:18:44 -0700169 if (mount(location, mount_point, fs_type,
Michael Rungebd6138c2014-10-22 17:05:08 -0700170 MS_NOATIME | MS_NODEV | MS_NODIRATIME,
171 has_mount_options ? mount_options : "") < 0) {
Michael Rungef15e31e2014-10-24 14:14:41 -0700172 uiPrintf(state, "%s: failed to mount %s at %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700173 name, location, mount_point, strerror(errno));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700174 result = strdup("");
175 } else {
176 result = mount_point;
177 }
178 }
179
180done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700181 free(fs_type);
182 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700183 free(location);
184 if (result != mount_point) free(mount_point);
Michael Rungebd6138c2014-10-22 17:05:08 -0700185 if (has_mount_options) free(mount_options);
Doug Zongker512536a2010-02-17 16:11:44 -0800186 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700187}
188
Doug Zongker8edb00c2009-06-11 17:21:44 -0700189
190// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800191Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700192 char* result = NULL;
193 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700194 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700195 }
196 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700197 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700198 return NULL;
199 }
200 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700201 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700202 goto done;
203 }
204
205 scan_mounted_volumes();
206 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
207 if (vol == NULL) {
208 result = strdup("");
209 } else {
210 result = mount_point;
211 }
212
213done:
214 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800215 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700216}
217
218
Doug Zongker512536a2010-02-17 16:11:44 -0800219Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700220 char* result = NULL;
221 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700222 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700223 }
224 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700225 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700226 return NULL;
227 }
228 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700229 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700230 goto done;
231 }
232
233 scan_mounted_volumes();
234 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
235 if (vol == NULL) {
Michael Rungef15e31e2014-10-24 14:14:41 -0700236 uiPrintf(state, "unmount of %s failed; no such volume\n", mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700237 result = strdup("");
238 } else {
Michael Rungef15e31e2014-10-24 14:14:41 -0700239 int ret = unmount_mounted_volume(vol);
240 if (ret != 0) {
241 uiPrintf(state, "unmount of %s failed (%d): %s\n",
242 mount_point, ret, strerror(errno));
243 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700244 result = mount_point;
245 }
246
247done:
248 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800249 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700250}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700251
JP Abgrall37aedb32014-06-16 19:07:39 -0700252static int exec_cmd(const char* path, char* const argv[]) {
253 int status;
254 pid_t child;
255 if ((child = vfork()) == 0) {
256 execv(path, argv);
257 _exit(-1);
258 }
259 waitpid(child, &status, 0);
260 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
261 printf("%s failed with status %d\n", path, WEXITSTATUS(status));
262 }
263 return WEXITSTATUS(status);
264}
265
Doug Zongker8edb00c2009-06-11 17:21:44 -0700266
Stephen Smalley779701d2012-02-09 14:13:23 -0500267// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700268//
Stephen Smalley779701d2012-02-09 14:13:23 -0500269// fs_type="yaffs2" partition_type="MTD" location=partition fs_size=<bytes> mount_point=<location>
270// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700271// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
272// if fs_size == 0, then make fs uses the entire partition.
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800273// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700274// 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 -0800275Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700276 char* result = NULL;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400277 if (argc != 5) {
278 return ErrorAbort(state, "%s() expects 5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700279 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700280 char* fs_type;
281 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700282 char* location;
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800283 char* fs_size;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400284 char* mount_point;
Stephen Smalley779701d2012-02-09 14:13:23 -0500285
Stephen Smalley779701d2012-02-09 14:13:23 -0500286 if (ReadArgs(state, argv, 5, &fs_type, &partition_type, &location, &fs_size, &mount_point) < 0) {
287 return NULL;
288 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700289
Doug Zongker3d177d02010-07-01 09:18:44 -0700290 if (strlen(fs_type) == 0) {
291 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
292 goto done;
293 }
294 if (strlen(partition_type) == 0) {
295 ErrorAbort(state, "partition_type argument to %s() can't be empty",
296 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700297 goto done;
298 }
299 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700300 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700301 goto done;
302 }
303
Stephen Smalley516e4e22012-04-03 13:35:11 -0400304 if (strlen(mount_point) == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500305 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
306 goto done;
307 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500308
Doug Zongker3d177d02010-07-01 09:18:44 -0700309 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700310 mtd_scan_partitions();
311 const MtdPartition* mtd = mtd_find_partition_by_name(location);
312 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700313 printf("%s: no mtd partition named \"%s\"",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700314 name, location);
315 result = strdup("");
316 goto done;
317 }
318 MtdWriteContext* ctx = mtd_write_partition(mtd);
319 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700320 printf("%s: can't write \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700321 result = strdup("");
322 goto done;
323 }
324 if (mtd_erase_blocks(ctx, -1) == -1) {
325 mtd_write_close(ctx);
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700326 printf("%s: failed to erase \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700327 result = strdup("");
328 goto done;
329 }
330 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700331 printf("%s: failed to close \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700332 result = strdup("");
333 goto done;
334 }
335 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700336#ifdef USE_EXT4
337 } else if (strcmp(fs_type, "ext4") == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500338 int status = make_ext4fs(location, atoll(fs_size), mount_point, sehandle);
Doug Zongker3d177d02010-07-01 09:18:44 -0700339 if (status != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700340 printf("%s: make_ext4fs failed (%d) on %s",
Doug Zongker3d177d02010-07-01 09:18:44 -0700341 name, status, location);
342 result = strdup("");
343 goto done;
344 }
345 result = location;
JP Abgrall37aedb32014-06-16 19:07:39 -0700346 } else if (strcmp(fs_type, "f2fs") == 0) {
347 char *num_sectors;
348 if (asprintf(&num_sectors, "%lld", atoll(fs_size) / 512) <= 0) {
349 printf("format_volume: failed to create %s command for %s\n", fs_type, location);
350 result = strdup("");
351 goto done;
352 }
353 const char *f2fs_path = "/sbin/mkfs.f2fs";
354 const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", location, num_sectors, NULL};
355 int status = exec_cmd(f2fs_path, (char* const*)f2fs_argv);
356 free(num_sectors);
357 if (status != 0) {
358 printf("%s: mkfs.f2fs failed (%d) on %s",
359 name, status, location);
360 result = strdup("");
361 goto done;
362 }
363 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700364#endif
Doug Zongker9931f7f2009-06-10 14:11:53 -0700365 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700366 printf("%s: unsupported fs_type \"%s\" partition_type \"%s\"",
Doug Zongker3d177d02010-07-01 09:18:44 -0700367 name, fs_type, partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700368 }
369
370done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700371 free(fs_type);
372 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700373 if (result != location) free(location);
Doug Zongker512536a2010-02-17 16:11:44 -0800374 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700375}
376
Michael Rungece7ca712013-11-06 17:42:20 -0800377Value* RenameFn(const char* name, State* state, int argc, Expr* argv[]) {
378 char* result = NULL;
379 if (argc != 2) {
380 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
381 }
382
383 char* src_name;
384 char* dst_name;
385
386 if (ReadArgs(state, argv, 2, &src_name, &dst_name) < 0) {
387 return NULL;
388 }
389 if (strlen(src_name) == 0) {
390 ErrorAbort(state, "src_name argument to %s() can't be empty", name);
391 goto done;
392 }
393 if (strlen(dst_name) == 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700394 ErrorAbort(state, "dst_name argument to %s() can't be empty", name);
Michael Rungece7ca712013-11-06 17:42:20 -0800395 goto done;
396 }
Michael Rungea91ecc52014-07-21 17:40:02 -0700397 if (make_parents(dst_name) != 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700398 ErrorAbort(state, "Creating parent of %s failed, error %s",
Michael Rungea91ecc52014-07-21 17:40:02 -0700399 dst_name, strerror(errno));
Michael Runged5b17272014-10-22 14:28:23 -0700400 } else if (access(dst_name, F_OK) == 0 && access(src_name, F_OK) != 0) {
401 // File was already moved
402 result = dst_name;
Michael Rungea91ecc52014-07-21 17:40:02 -0700403 } else if (rename(src_name, dst_name) != 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700404 ErrorAbort(state, "Rename of %s to %s failed, error %s",
Michael Rungece7ca712013-11-06 17:42:20 -0800405 src_name, dst_name, strerror(errno));
406 } else {
407 result = dst_name;
408 }
409
410done:
411 free(src_name);
412 if (result != dst_name) free(dst_name);
413 return StringValue(result);
414}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700415
Doug Zongker512536a2010-02-17 16:11:44 -0800416Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700417 char** paths = malloc(argc * sizeof(char*));
418 int i;
419 for (i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700420 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700421 if (paths[i] == NULL) {
422 int j;
423 for (j = 0; j < i; ++i) {
424 free(paths[j]);
425 }
426 free(paths);
427 return NULL;
428 }
429 }
430
431 bool recursive = (strcmp(name, "delete_recursive") == 0);
432
433 int success = 0;
434 for (i = 0; i < argc; ++i) {
435 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
436 ++success;
437 free(paths[i]);
438 }
439 free(paths);
440
441 char buffer[10];
442 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800443 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700444}
445
Doug Zongker8edb00c2009-06-11 17:21:44 -0700446
Doug Zongker512536a2010-02-17 16:11:44 -0800447Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700448 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700449 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700450 }
451 char* frac_str;
452 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700453 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700454 return NULL;
455 }
456
457 double frac = strtod(frac_str, NULL);
458 int sec = strtol(sec_str, NULL, 10);
459
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700460 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700461 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
462
Doug Zongker9931f7f2009-06-10 14:11:53 -0700463 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800464 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700465}
466
Doug Zongker512536a2010-02-17 16:11:44 -0800467Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700468 if (argc != 1) {
469 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
470 }
471 char* frac_str;
472 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
473 return NULL;
474 }
475
476 double frac = strtod(frac_str, NULL);
477
478 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
479 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
480
Doug Zongker512536a2010-02-17 16:11:44 -0800481 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700482}
483
Doug Zongker8edb00c2009-06-11 17:21:44 -0700484// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800485Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700486 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700487 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700488 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700489 }
490 char* zip_path;
491 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700492 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700493
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700494 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700495
496 // To create a consistent system image, never use the clock for timestamps.
497 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
498
499 bool success = mzExtractRecursive(za, zip_path, dest_path,
500 MZ_EXTRACT_FILES_ONLY, &timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500501 NULL, NULL, sehandle);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700502 free(zip_path);
503 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800504 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700505}
506
Doug Zongker8edb00c2009-06-11 17:21:44 -0700507
508// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800509// or
510// package_extract_file(package_path)
511// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800512// function (the char* returned is actually a FileContents*).
513Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700514 int argc, Expr* argv[]) {
Doug Zongker5f875bf2014-08-22 14:53:43 -0700515 if (argc < 1 || argc > 2) {
Doug Zongker6aece332010-02-01 14:40:12 -0800516 return ErrorAbort(state, "%s() expects 1 or 2 args, got %d",
517 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700518 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700519 bool success = false;
Doug Zongker43772d22014-06-09 14:13:19 -0700520
521 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker43772d22014-06-09 14:13:19 -0700522
Doug Zongker6aece332010-02-01 14:40:12 -0800523 if (argc == 2) {
524 // The two-argument version extracts to a file.
Doug Zongker8edb00c2009-06-11 17:21:44 -0700525
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800526 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700527
Doug Zongker6aece332010-02-01 14:40:12 -0800528 char* zip_path;
529 char* dest_path;
530 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
531
Doug Zongker6aece332010-02-01 14:40:12 -0800532 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
533 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700534 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800535 goto done2;
536 }
537
538 FILE* f = fopen(dest_path, "wb");
539 if (f == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700540 printf("%s: can't open %s for write: %s\n",
Doug Zongker6aece332010-02-01 14:40:12 -0800541 name, dest_path, strerror(errno));
542 goto done2;
543 }
544 success = mzExtractZipEntryToFile(za, entry, fileno(f));
545 fclose(f);
546
547 done2:
548 free(zip_path);
549 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800550 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800551 } else {
552 // The one-argument version returns the contents of the file
553 // as the result.
554
555 char* zip_path;
Doug Zongker512536a2010-02-17 16:11:44 -0800556 Value* v = malloc(sizeof(Value));
557 v->type = VAL_BLOB;
558 v->size = -1;
559 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800560
561 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
562
563 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
564 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
565 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700566 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800567 goto done1;
568 }
569
Doug Zongker512536a2010-02-17 16:11:44 -0800570 v->size = mzGetZipEntryUncompLen(entry);
571 v->data = malloc(v->size);
572 if (v->data == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700573 printf("%s: failed to allocate %ld bytes for %s\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800574 name, (long)v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800575 goto done1;
576 }
577
Doug Zongker512536a2010-02-17 16:11:44 -0800578 success = mzExtractZipEntryToBuffer(za, entry,
579 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800580
581 done1:
582 free(zip_path);
583 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800584 free(v->data);
585 v->data = NULL;
586 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800587 }
Doug Zongker512536a2010-02-17 16:11:44 -0800588 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700589 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700590}
591
Doug Zongkera23075f2012-08-06 16:19:09 -0700592// Create all parent directories of name, if necessary.
593static int make_parents(char* name) {
594 char* p;
595 for (p = name + (strlen(name)-1); p > name; --p) {
596 if (*p != '/') continue;
597 *p = '\0';
598 if (make_parents(name) < 0) return -1;
599 int result = mkdir(name, 0700);
Michael Rungea91ecc52014-07-21 17:40:02 -0700600 if (result == 0) printf("created [%s]\n", name);
Doug Zongkera23075f2012-08-06 16:19:09 -0700601 *p = '/';
602 if (result == 0 || errno == EEXIST) {
603 // successfully created or already existed; we're done
604 return 0;
605 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700606 printf("failed to mkdir %s: %s\n", name, strerror(errno));
Doug Zongkera23075f2012-08-06 16:19:09 -0700607 return -1;
608 }
609 }
610 return 0;
611}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700612
Doug Zongker9931f7f2009-06-10 14:11:53 -0700613// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700614// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800615Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700616 if (argc == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700617 return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700618 }
619 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700620 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700621 if (target == NULL) return NULL;
622
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700623 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700624 if (srcs == NULL) {
625 free(target);
626 return NULL;
627 }
628
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700629 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700630 int i;
631 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700632 if (unlink(srcs[i]) < 0) {
633 if (errno != ENOENT) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700634 printf("%s: failed to remove %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700635 name, srcs[i], strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700636 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700637 }
638 }
Doug Zongkera23075f2012-08-06 16:19:09 -0700639 if (make_parents(srcs[i])) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700640 printf("%s: failed to symlink %s to %s: making parents failed\n",
Doug Zongkera23075f2012-08-06 16:19:09 -0700641 name, srcs[i], target);
642 ++bad;
643 }
Doug Zongker60babf82009-09-18 15:11:24 -0700644 if (symlink(target, srcs[i]) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700645 printf("%s: failed to symlink %s to %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700646 name, srcs[i], target, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700647 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700648 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700649 free(srcs[i]);
650 }
651 free(srcs);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700652 if (bad) {
653 return ErrorAbort(state, "%s: some symlinks failed", name);
654 }
Doug Zongker512536a2010-02-17 16:11:44 -0800655 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700656}
657
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700658struct perm_parsed_args {
659 bool has_uid;
660 uid_t uid;
661 bool has_gid;
662 gid_t gid;
663 bool has_mode;
664 mode_t mode;
665 bool has_fmode;
666 mode_t fmode;
667 bool has_dmode;
668 mode_t dmode;
669 bool has_selabel;
670 char* selabel;
671 bool has_capabilities;
672 uint64_t capabilities;
673};
674
Michael Runge75480252014-10-22 19:48:41 -0700675static struct perm_parsed_args ParsePermArgs(State * state, int argc, char** args) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700676 int i;
677 struct perm_parsed_args parsed;
678 int bad = 0;
679 static int max_warnings = 20;
680
681 memset(&parsed, 0, sizeof(parsed));
682
683 for (i = 1; i < argc; i += 2) {
684 if (strcmp("uid", args[i]) == 0) {
685 int64_t uid;
686 if (sscanf(args[i+1], "%" SCNd64, &uid) == 1) {
687 parsed.uid = uid;
688 parsed.has_uid = true;
689 } else {
Michael Runge75480252014-10-22 19:48:41 -0700690 uiPrintf(state, "ParsePermArgs: invalid UID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700691 bad++;
692 }
693 continue;
694 }
695 if (strcmp("gid", args[i]) == 0) {
696 int64_t gid;
697 if (sscanf(args[i+1], "%" SCNd64, &gid) == 1) {
698 parsed.gid = gid;
699 parsed.has_gid = true;
700 } else {
Michael Runge75480252014-10-22 19:48:41 -0700701 uiPrintf(state, "ParsePermArgs: invalid GID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700702 bad++;
703 }
704 continue;
705 }
706 if (strcmp("mode", args[i]) == 0) {
707 int32_t mode;
708 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
709 parsed.mode = mode;
710 parsed.has_mode = true;
711 } else {
Michael Runge75480252014-10-22 19:48:41 -0700712 uiPrintf(state, "ParsePermArgs: invalid mode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700713 bad++;
714 }
715 continue;
716 }
717 if (strcmp("dmode", args[i]) == 0) {
718 int32_t mode;
719 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
720 parsed.dmode = mode;
721 parsed.has_dmode = true;
722 } else {
Michael Runge75480252014-10-22 19:48:41 -0700723 uiPrintf(state, "ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700724 bad++;
725 }
726 continue;
727 }
728 if (strcmp("fmode", args[i]) == 0) {
729 int32_t mode;
730 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
731 parsed.fmode = mode;
732 parsed.has_fmode = true;
733 } else {
Michael Runge75480252014-10-22 19:48:41 -0700734 uiPrintf(state, "ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700735 bad++;
736 }
737 continue;
738 }
739 if (strcmp("capabilities", args[i]) == 0) {
740 int64_t capabilities;
741 if (sscanf(args[i+1], "%" SCNi64, &capabilities) == 1) {
742 parsed.capabilities = capabilities;
743 parsed.has_capabilities = true;
744 } else {
Michael Runge75480252014-10-22 19:48:41 -0700745 uiPrintf(state, "ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700746 bad++;
747 }
748 continue;
749 }
750 if (strcmp("selabel", args[i]) == 0) {
751 if (args[i+1][0] != '\0') {
752 parsed.selabel = args[i+1];
753 parsed.has_selabel = true;
754 } else {
Michael Runge75480252014-10-22 19:48:41 -0700755 uiPrintf(state, "ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700756 bad++;
757 }
758 continue;
759 }
760 if (max_warnings != 0) {
761 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i]);
762 max_warnings--;
763 if (max_warnings == 0) {
764 printf("ParsedPermArgs: suppressing further warnings\n");
765 }
766 }
767 }
768 return parsed;
769}
770
771static int ApplyParsedPerms(
Michael Runge75480252014-10-22 19:48:41 -0700772 State * state,
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700773 const char* filename,
774 const struct stat *statptr,
775 struct perm_parsed_args parsed)
776{
777 int bad = 0;
778
Nick Kralevich6a821fe2014-10-23 20:36:42 -0700779 if (parsed.has_selabel) {
780 if (lsetfilecon(filename, parsed.selabel) != 0) {
781 uiPrintf(state, "ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n",
782 filename, parsed.selabel, strerror(errno));
783 bad++;
784 }
785 }
786
Nick Kraleviche4612512013-09-10 15:34:19 -0700787 /* ignore symlinks */
788 if (S_ISLNK(statptr->st_mode)) {
Nick Kralevich6a821fe2014-10-23 20:36:42 -0700789 return bad;
Nick Kraleviche4612512013-09-10 15:34:19 -0700790 }
791
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700792 if (parsed.has_uid) {
793 if (chown(filename, parsed.uid, -1) < 0) {
Michael Runge75480252014-10-22 19:48:41 -0700794 uiPrintf(state, "ApplyParsedPerms: chown of %s to %d failed: %s\n",
795 filename, parsed.uid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700796 bad++;
797 }
798 }
799
800 if (parsed.has_gid) {
801 if (chown(filename, -1, parsed.gid) < 0) {
Michael Runge75480252014-10-22 19:48:41 -0700802 uiPrintf(state, "ApplyParsedPerms: chgrp of %s to %d failed: %s\n",
803 filename, parsed.gid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700804 bad++;
805 }
806 }
807
808 if (parsed.has_mode) {
809 if (chmod(filename, parsed.mode) < 0) {
Michael Runge75480252014-10-22 19:48:41 -0700810 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
811 filename, parsed.mode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700812 bad++;
813 }
814 }
815
816 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
817 if (chmod(filename, parsed.dmode) < 0) {
Michael Runge75480252014-10-22 19:48:41 -0700818 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
819 filename, parsed.dmode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700820 bad++;
821 }
822 }
823
824 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
825 if (chmod(filename, parsed.fmode) < 0) {
Michael Runge75480252014-10-22 19:48:41 -0700826 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700827 filename, parsed.fmode, strerror(errno));
828 bad++;
829 }
830 }
831
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700832 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
833 if (parsed.capabilities == 0) {
834 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
835 // Report failure unless it's ENODATA (attribute not set)
Michael Runge75480252014-10-22 19:48:41 -0700836 uiPrintf(state, "ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700837 filename, parsed.capabilities, strerror(errno));
838 bad++;
839 }
840 } else {
841 struct vfs_cap_data cap_data;
842 memset(&cap_data, 0, sizeof(cap_data));
843 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
844 cap_data.data[0].permitted = (uint32_t) (parsed.capabilities & 0xffffffff);
845 cap_data.data[0].inheritable = 0;
846 cap_data.data[1].permitted = (uint32_t) (parsed.capabilities >> 32);
847 cap_data.data[1].inheritable = 0;
848 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
Michael Runge75480252014-10-22 19:48:41 -0700849 uiPrintf(state, "ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n",
850 filename, parsed.capabilities, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700851 bad++;
852 }
853 }
854 }
855
856 return bad;
857}
858
859// nftw doesn't allow us to pass along context, so we need to use
860// global variables. *sigh*
861static struct perm_parsed_args recursive_parsed_args;
Michael Runge75480252014-10-22 19:48:41 -0700862static State* recursive_state;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700863
864static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr,
865 int fileflags, struct FTW *pfwt) {
Michael Runge75480252014-10-22 19:48:41 -0700866 return ApplyParsedPerms(recursive_state, filename, statptr, recursive_parsed_args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700867}
868
869static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
870 int i;
871 int bad = 0;
872 static int nwarnings = 0;
873 struct stat sb;
874 Value* result = NULL;
875
876 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
877
878 if ((argc % 2) != 1) {
879 return ErrorAbort(state, "%s() expects an odd number of arguments, got %d",
880 name, argc);
881 }
882
883 char** args = ReadVarArgs(state, argc, argv);
884 if (args == NULL) return NULL;
885
886 if (lstat(args[0], &sb) == -1) {
887 result = ErrorAbort(state, "%s: Error on lstat of \"%s\": %s", name, args[0], strerror(errno));
888 goto done;
889 }
890
Michael Runge75480252014-10-22 19:48:41 -0700891 struct perm_parsed_args parsed = ParsePermArgs(state, argc, args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700892
893 if (recursive) {
894 recursive_parsed_args = parsed;
Michael Runge75480252014-10-22 19:48:41 -0700895 recursive_state = state;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700896 bad += nftw(args[0], do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
897 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
Michael Runge75480252014-10-22 19:48:41 -0700898 recursive_state = NULL;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700899 } else {
Michael Runge75480252014-10-22 19:48:41 -0700900 bad += ApplyParsedPerms(state, args[0], &sb, parsed);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700901 }
902
903done:
904 for (i = 0; i < argc; ++i) {
905 free(args[i]);
906 }
907 free(args);
908
909 if (result != NULL) {
910 return result;
911 }
912
913 if (bad > 0) {
914 return ErrorAbort(state, "%s: some changes failed", name);
915 }
916
917 return StringValue(strdup(""));
918}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700919
Doug Zongker512536a2010-02-17 16:11:44 -0800920Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700921 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700922 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700923 }
924 char* key;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700925 key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700926 if (key == NULL) return NULL;
927
928 char value[PROPERTY_VALUE_MAX];
929 property_get(key, value, "");
930 free(key);
931
Doug Zongker512536a2010-02-17 16:11:44 -0800932 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700933}
934
935
Doug Zongker47cace92009-06-18 10:11:50 -0700936// file_getprop(file, key)
937//
938// interprets 'file' as a getprop-style file (key=value pairs, one
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700939// per line. # comment lines,blank lines, lines without '=' ignored),
940// and returns the value for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800941Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700942 char* result = NULL;
943 char* buffer = NULL;
944 char* filename;
945 char* key;
946 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
947 return NULL;
948 }
949
950 struct stat st;
951 if (stat(filename, &st) < 0) {
952 ErrorAbort(state, "%s: failed to stat \"%s\": %s",
953 name, filename, strerror(errno));
954 goto done;
955 }
956
957#define MAX_FILE_GETPROP_SIZE 65536
958
959 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
960 ErrorAbort(state, "%s too large for %s (max %d)",
961 filename, name, MAX_FILE_GETPROP_SIZE);
962 goto done;
963 }
964
965 buffer = malloc(st.st_size+1);
966 if (buffer == NULL) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700967 ErrorAbort(state, "%s: failed to alloc %lld bytes", name, (long long)st.st_size+1);
Doug Zongker47cace92009-06-18 10:11:50 -0700968 goto done;
969 }
970
971 FILE* f = fopen(filename, "rb");
972 if (f == NULL) {
973 ErrorAbort(state, "%s: failed to open %s: %s",
974 name, filename, strerror(errno));
975 goto done;
976 }
977
978 if (fread(buffer, 1, st.st_size, f) != st.st_size) {
Doug Zongkera23075f2012-08-06 16:19:09 -0700979 ErrorAbort(state, "%s: failed to read %lld bytes from %s",
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700980 name, (long long)st.st_size+1, filename);
Doug Zongker47cace92009-06-18 10:11:50 -0700981 fclose(f);
982 goto done;
983 }
984 buffer[st.st_size] = '\0';
985
986 fclose(f);
987
988 char* line = strtok(buffer, "\n");
989 do {
990 // skip whitespace at start of line
991 while (*line && isspace(*line)) ++line;
992
993 // comment or blank line: skip to next line
994 if (*line == '\0' || *line == '#') continue;
995
996 char* equal = strchr(line, '=');
997 if (equal == NULL) {
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700998 continue;
Doug Zongker47cace92009-06-18 10:11:50 -0700999 }
1000
1001 // trim whitespace between key and '='
1002 char* key_end = equal-1;
1003 while (key_end > line && isspace(*key_end)) --key_end;
1004 key_end[1] = '\0';
1005
1006 // not the key we're looking for
1007 if (strcmp(key, line) != 0) continue;
1008
1009 // skip whitespace after the '=' to the start of the value
1010 char* val_start = equal+1;
1011 while(*val_start && isspace(*val_start)) ++val_start;
1012
1013 // trim trailing whitespace
1014 char* val_end = val_start + strlen(val_start)-1;
1015 while (val_end > val_start && isspace(*val_end)) --val_end;
1016 val_end[1] = '\0';
1017
1018 result = strdup(val_start);
1019 break;
1020
1021 } while ((line = strtok(NULL, "\n")));
1022
1023 if (result == NULL) result = strdup("");
1024
1025 done:
1026 free(filename);
1027 free(key);
1028 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -08001029 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -07001030}
1031
1032
Doug Zongker8edb00c2009-06-11 17:21:44 -07001033static bool write_raw_image_cb(const unsigned char* data,
1034 int data_len, void* ctx) {
1035 int r = mtd_write_data((MtdWriteContext*)ctx, (const char *)data, data_len);
1036 if (r == data_len) return true;
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001037 printf("%s\n", strerror(errno));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001038 return false;
1039}
1040
Doug Zongker179b2d92011-04-12 15:49:04 -07001041// write_raw_image(filename_or_blob, partition)
Doug Zongker512536a2010-02-17 16:11:44 -08001042Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001043 char* result = NULL;
1044
Doug Zongker179b2d92011-04-12 15:49:04 -07001045 Value* partition_value;
1046 Value* contents;
1047 if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001048 return NULL;
1049 }
1050
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001051 char* partition = NULL;
Doug Zongker179b2d92011-04-12 15:49:04 -07001052 if (partition_value->type != VAL_STRING) {
1053 ErrorAbort(state, "partition argument to %s must be string", name);
1054 goto done;
1055 }
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001056 partition = partition_value->data;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001057 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001058 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001059 goto done;
1060 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001061 if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001062 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001063 goto done;
1064 }
1065
Dees_Troy76a1d412013-04-20 13:54:35 +00001066 char* filename = contents->data;
1067 if (0 == restore_raw_partition(NULL, partition, filename))
1068 result = strdup(partition);
1069 else {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001070 result = strdup("");
1071 goto done;
1072 }
1073
Doug Zongker8edb00c2009-06-11 17:21:44 -07001074done:
Doug Zongker179b2d92011-04-12 15:49:04 -07001075 if (result != partition) FreeValue(partition_value);
1076 FreeValue(contents);
Doug Zongker512536a2010-02-17 16:11:44 -08001077 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001078}
1079
Doug Zongker8edb00c2009-06-11 17:21:44 -07001080// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001081Value* ApplyPatchSpaceFn(const char* name, State* state,
1082 int argc, Expr* argv[]) {
1083 char* bytes_str;
1084 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
1085 return NULL;
1086 }
1087
1088 char* endptr;
1089 size_t bytes = strtol(bytes_str, &endptr, 10);
1090 if (bytes == 0 && endptr == bytes_str) {
1091 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n",
1092 name, bytes_str);
1093 free(bytes_str);
1094 return NULL;
1095 }
1096
1097 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
1098}
1099
Doug Zongker52b40362014-02-10 15:30:30 -08001100// apply_patch(file, size, init_sha1, tgt_sha1, patch)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001101
Doug Zongker512536a2010-02-17 16:11:44 -08001102Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001103 if (argc < 6 || (argc % 2) == 1) {
1104 return ErrorAbort(state, "%s(): expected at least 6 args and an "
1105 "even number, got %d",
1106 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001107 }
1108
Doug Zongkerc4351c72010-02-22 14:46:32 -08001109 char* source_filename;
1110 char* target_filename;
1111 char* target_sha1;
1112 char* target_size_str;
1113 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
1114 &target_sha1, &target_size_str) < 0) {
1115 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001116 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001117
Doug Zongkerc4351c72010-02-22 14:46:32 -08001118 char* endptr;
1119 size_t target_size = strtol(target_size_str, &endptr, 10);
1120 if (target_size == 0 && endptr == target_size_str) {
1121 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count",
1122 name, target_size_str);
1123 free(source_filename);
1124 free(target_filename);
1125 free(target_sha1);
1126 free(target_size_str);
1127 return NULL;
1128 }
1129
1130 int patchcount = (argc-4) / 2;
1131 Value** patches = ReadValueVarArgs(state, argc-4, argv+4);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001132
1133 int i;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001134 for (i = 0; i < patchcount; ++i) {
1135 if (patches[i*2]->type != VAL_STRING) {
1136 ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i);
1137 break;
1138 }
1139 if (patches[i*2+1]->type != VAL_BLOB) {
1140 ErrorAbort(state, "%s(): patch #%d is not blob", name, i);
1141 break;
1142 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001143 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001144 if (i != patchcount) {
1145 for (i = 0; i < patchcount*2; ++i) {
1146 FreeValue(patches[i]);
1147 }
1148 free(patches);
1149 return NULL;
1150 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001151
Doug Zongkerc4351c72010-02-22 14:46:32 -08001152 char** patch_sha_str = malloc(patchcount * sizeof(char*));
1153 for (i = 0; i < patchcount; ++i) {
1154 patch_sha_str[i] = patches[i*2]->data;
1155 patches[i*2]->data = NULL;
1156 FreeValue(patches[i*2]);
1157 patches[i] = patches[i*2+1];
Doug Zongker8edb00c2009-06-11 17:21:44 -07001158 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001159
1160 int result = applypatch(source_filename, target_filename,
1161 target_sha1, target_size,
Doug Zongkera3ccba62012-08-20 15:28:02 -07001162 patchcount, patch_sha_str, patches, NULL);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001163
1164 for (i = 0; i < patchcount; ++i) {
1165 FreeValue(patches[i]);
1166 }
1167 free(patch_sha_str);
1168 free(patches);
1169
1170 return StringValue(strdup(result == 0 ? "t" : ""));
1171}
1172
1173// apply_patch_check(file, [sha1_1, ...])
1174Value* ApplyPatchCheckFn(const char* name, State* state,
1175 int argc, Expr* argv[]) {
1176 if (argc < 1) {
1177 return ErrorAbort(state, "%s(): expected at least 1 arg, got %d",
1178 name, argc);
1179 }
1180
1181 char* filename;
1182 if (ReadArgs(state, argv, 1, &filename) < 0) {
1183 return NULL;
1184 }
1185
1186 int patchcount = argc-1;
1187 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
1188
1189 int result = applypatch_check(filename, patchcount, sha1s);
1190
1191 int i;
1192 for (i = 0; i < patchcount; ++i) {
1193 free(sha1s[i]);
1194 }
1195 free(sha1s);
1196
1197 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001198}
1199
Doug Zongker512536a2010-02-17 16:11:44 -08001200Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001201 char** args = ReadVarArgs(state, argc, argv);
1202 if (args == NULL) {
1203 return NULL;
1204 }
1205
1206 int size = 0;
1207 int i;
1208 for (i = 0; i < argc; ++i) {
1209 size += strlen(args[i]);
1210 }
1211 char* buffer = malloc(size+1);
1212 size = 0;
1213 for (i = 0; i < argc; ++i) {
1214 strcpy(buffer+size, args[i]);
1215 size += strlen(args[i]);
1216 free(args[i]);
1217 }
1218 free(args);
1219 buffer[size] = '\0';
Michael Runge75480252014-10-22 19:48:41 -07001220 uiPrint(state, buffer);
Doug Zongker512536a2010-02-17 16:11:44 -08001221 return StringValue(buffer);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001222}
1223
Doug Zongkerd0181b82011-10-19 10:51:12 -07001224Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
1225 if (argc != 0) {
1226 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1227 }
1228 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
1229 return StringValue(strdup("t"));
1230}
1231
Doug Zongker512536a2010-02-17 16:11:44 -08001232Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001233 if (argc < 1) {
1234 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1235 }
1236 char** args = ReadVarArgs(state, argc, argv);
1237 if (args == NULL) {
1238 return NULL;
1239 }
1240
1241 char** args2 = malloc(sizeof(char*) * (argc+1));
1242 memcpy(args2, args, sizeof(char*) * argc);
1243 args2[argc] = NULL;
1244
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001245 printf("about to run program [%s] with %d args\n", args2[0], argc);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001246
1247 pid_t child = fork();
1248 if (child == 0) {
1249 execv(args2[0], args2);
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001250 printf("run_program: execv failed: %s\n", strerror(errno));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001251 _exit(1);
1252 }
1253 int status;
1254 waitpid(child, &status, 0);
1255 if (WIFEXITED(status)) {
1256 if (WEXITSTATUS(status) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001257 printf("run_program: child exited with status %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001258 WEXITSTATUS(status));
1259 }
1260 } else if (WIFSIGNALED(status)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001261 printf("run_program: child terminated by signal %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001262 WTERMSIG(status));
1263 }
1264
1265 int i;
1266 for (i = 0; i < argc; ++i) {
1267 free(args[i]);
1268 }
1269 free(args);
1270 free(args2);
1271
1272 char buffer[20];
1273 sprintf(buffer, "%d", status);
1274
Doug Zongker512536a2010-02-17 16:11:44 -08001275 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001276}
1277
Doug Zongker512536a2010-02-17 16:11:44 -08001278// sha1_check(data)
1279// to return the sha1 of the data (given in the format returned by
1280// read_file).
1281//
1282// sha1_check(data, sha1_hex, [sha1_hex, ...])
1283// returns the sha1 of the file if it matches any of the hex
1284// strings passed, or "" if it does not equal any of them.
1285//
1286Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1287 if (argc < 1) {
1288 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1289 }
1290
1291 Value** args = ReadValueVarArgs(state, argc, argv);
1292 if (args == NULL) {
1293 return NULL;
1294 }
1295
1296 if (args[0]->size < 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001297 return StringValue(strdup(""));
1298 }
1299 uint8_t digest[SHA_DIGEST_SIZE];
Doug Zongkerbac7fba2013-04-10 11:32:17 -07001300 SHA_hash(args[0]->data, args[0]->size, digest);
Doug Zongker512536a2010-02-17 16:11:44 -08001301 FreeValue(args[0]);
1302
1303 if (argc == 1) {
1304 return StringValue(PrintSha1(digest));
1305 }
1306
1307 int i;
1308 uint8_t* arg_digest = malloc(SHA_DIGEST_SIZE);
1309 for (i = 1; i < argc; ++i) {
1310 if (args[i]->type != VAL_STRING) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001311 printf("%s(): arg %d is not a string; skipping",
Doug Zongker512536a2010-02-17 16:11:44 -08001312 name, i);
1313 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1314 // Warn about bad args and skip them.
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001315 printf("%s(): error parsing \"%s\" as sha-1; skipping",
1316 name, args[i]->data);
Doug Zongker512536a2010-02-17 16:11:44 -08001317 } else if (memcmp(digest, arg_digest, SHA_DIGEST_SIZE) == 0) {
1318 break;
1319 }
1320 FreeValue(args[i]);
1321 }
1322 if (i >= argc) {
1323 // Didn't match any of the hex strings; return false.
1324 return StringValue(strdup(""));
1325 }
1326 // Found a match; free all the remaining arguments and return the
1327 // matched one.
1328 int j;
1329 for (j = i+1; j < argc; ++j) {
1330 FreeValue(args[j]);
1331 }
1332 return args[i];
1333}
1334
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001335// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001336// is actually a FileContents*).
1337Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1338 if (argc != 1) {
1339 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1340 }
1341 char* filename;
1342 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1343
1344 Value* v = malloc(sizeof(Value));
1345 v->type = VAL_BLOB;
1346
1347 FileContents fc;
Doug Zongkera1bc1482014-02-13 15:18:19 -08001348 if (LoadFileContents(filename, &fc) != 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001349 free(filename);
Michael Runge6eed2242013-12-13 17:13:11 -08001350 v->size = -1;
1351 v->data = NULL;
Doug Zongker512536a2010-02-17 16:11:44 -08001352 free(fc.data);
Michael Runge6eed2242013-12-13 17:13:11 -08001353 return v;
Doug Zongker512536a2010-02-17 16:11:44 -08001354 }
1355
1356 v->size = fc.size;
1357 v->data = (char*)fc.data;
1358
1359 free(filename);
1360 return v;
1361}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001362
Doug Zongkerc87bab12013-11-25 13:53:25 -08001363// Immediately reboot the device. Recovery is not finished normally,
1364// so if you reboot into recovery it will re-start applying the
1365// current package (because nothing has cleared the copy of the
1366// arguments stored in the BCB).
1367//
1368// The argument is the partition name passed to the android reboot
1369// property. It can be "recovery" to boot from the recovery
1370// partition, or "" (empty string) to boot from the regular boot
1371// partition.
1372Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) {
1373 if (argc != 2) {
1374 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1375 }
1376
1377 char* filename;
1378 char* property;
1379 if (ReadArgs(state, argv, 2, &filename, &property) < 0) return NULL;
1380
1381 char buffer[80];
1382
1383 // zero out the 'command' field of the bootloader message.
1384 memset(buffer, 0, sizeof(((struct bootloader_message*)0)->command));
1385 FILE* f = fopen(filename, "r+b");
1386 fseek(f, offsetof(struct bootloader_message, command), SEEK_SET);
1387 fwrite(buffer, sizeof(((struct bootloader_message*)0)->command), 1, f);
1388 fclose(f);
1389 free(filename);
1390
1391 strcpy(buffer, "reboot,");
1392 if (property != NULL) {
1393 strncat(buffer, property, sizeof(buffer)-10);
1394 }
1395
1396 property_set(ANDROID_RB_PROPERTY, buffer);
1397
1398 sleep(5);
1399 free(property);
1400 ErrorAbort(state, "%s() failed to reboot", name);
1401 return NULL;
1402}
1403
1404// Store a string value somewhere that future invocations of recovery
1405// can access it. This value is called the "stage" and can be used to
1406// drive packages that need to do reboots in the middle of
1407// installation and keep track of where they are in the multi-stage
1408// install.
1409//
1410// The first argument is the block device for the misc partition
1411// ("/misc" in the fstab), which is where this value is stored. The
1412// second argument is the string to store; it should not exceed 31
1413// bytes.
1414Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
1415 if (argc != 2) {
1416 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1417 }
1418
1419 char* filename;
1420 char* stagestr;
1421 if (ReadArgs(state, argv, 2, &filename, &stagestr) < 0) return NULL;
1422
1423 // Store this value in the misc partition, immediately after the
1424 // bootloader message that the main recovery uses to save its
1425 // arguments in case of the device restarting midway through
1426 // package installation.
1427 FILE* f = fopen(filename, "r+b");
1428 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1429 int to_write = strlen(stagestr)+1;
1430 int max_size = sizeof(((struct bootloader_message*)0)->stage);
1431 if (to_write > max_size) {
1432 to_write = max_size;
1433 stagestr[max_size-1] = 0;
1434 }
1435 fwrite(stagestr, to_write, 1, f);
1436 fclose(f);
1437
1438 free(stagestr);
1439 return StringValue(filename);
1440}
1441
1442// Return the value most recently saved with SetStageFn. The argument
1443// is the block device for the misc partition.
1444Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001445 if (argc != 1) {
Doug Zongkerc87bab12013-11-25 13:53:25 -08001446 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1447 }
1448
1449 char* filename;
1450 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1451
1452 char buffer[sizeof(((struct bootloader_message*)0)->stage)];
1453 FILE* f = fopen(filename, "rb");
1454 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1455 fread(buffer, sizeof(buffer), 1, f);
1456 fclose(f);
1457 buffer[sizeof(buffer)-1] = '\0';
1458
1459 return StringValue(strdup(buffer));
1460}
1461
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001462Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) {
1463 if (argc != 2) {
1464 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1465 }
1466
1467 char* filename;
1468 char* len_str;
1469 if (ReadArgs(state, argv, 2, &filename, &len_str) < 0) return NULL;
1470
1471 size_t len = strtoull(len_str, NULL, 0);
1472 int fd = open(filename, O_WRONLY, 0644);
1473 int success = wipe_block_device(fd, len);
1474
1475 free(filename);
1476 free(len_str);
1477
1478 close(fd);
1479
1480 return StringValue(strdup(success ? "t" : ""));
1481}
1482
Doug Zongkerc704e062014-05-23 08:40:35 -07001483Value* EnableRebootFn(const char* name, State* state, int argc, Expr* argv[]) {
1484 if (argc != 0) {
1485 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1486 }
1487 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
1488 fprintf(ui->cmd_pipe, "enable_reboot\n");
1489 return StringValue(strdup("t"));
1490}
1491
Doug Zongker9931f7f2009-06-10 14:11:53 -07001492void RegisterInstallFunctions() {
1493 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001494 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001495 RegisterFunction("unmount", UnmountFn);
1496 RegisterFunction("format", FormatFn);
1497 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001498 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001499 RegisterFunction("delete", DeleteFn);
1500 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001501 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1502 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001503 RegisterFunction("symlink", SymlinkFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001504
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001505 // Usage:
1506 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1507 // Example:
1508 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1509 RegisterFunction("set_metadata", SetMetadataFn);
1510
1511 // Usage:
1512 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1513 // Example:
1514 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1515 RegisterFunction("set_metadata_recursive", SetMetadataFn);
1516
Doug Zongker8edb00c2009-06-11 17:21:44 -07001517 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001518 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001519 RegisterFunction("write_raw_image", WriteRawImageFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001520
1521 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001522 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1523 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001524
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001525 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001526
Doug Zongker512536a2010-02-17 16:11:44 -08001527 RegisterFunction("read_file", ReadFileFn);
1528 RegisterFunction("sha1_check", Sha1CheckFn);
Michael Rungece7ca712013-11-06 17:42:20 -08001529 RegisterFunction("rename", RenameFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001530
Doug Zongkerd0181b82011-10-19 10:51:12 -07001531 RegisterFunction("wipe_cache", WipeCacheFn);
1532
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001533 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001534
1535 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001536
1537 RegisterFunction("reboot_now", RebootNowFn);
1538 RegisterFunction("get_stage", GetStageFn);
1539 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -07001540
1541 RegisterFunction("enable_reboot", EnableRebootFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001542}