blob: dad0d08c9a9074a41b8a0a38b5adb884e06cc89c [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
Doug Zongker52b40362014-02-10 15:30:30 -080055// Take a sha-1 digest and return it as a newly-allocated hex string.
Doug Zongker66f5ce32014-08-15 14:31:52 -070056char* PrintSha1(const uint8_t* digest) {
Doug Zongker52b40362014-02-10 15:30:30 -080057 char* buffer = malloc(SHA_DIGEST_SIZE*2 + 1);
58 int i;
59 const char* alphabet = "0123456789abcdef";
60 for (i = 0; i < SHA_DIGEST_SIZE; ++i) {
61 buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf];
62 buffer[i*2+1] = alphabet[digest[i] & 0xf];
63 }
64 buffer[i*2] = '\0';
65 return buffer;
66}
67
Doug Zongker3d177d02010-07-01 09:18:44 -070068// mount(fs_type, partition_type, location, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -070069//
Doug Zongker3d177d02010-07-01 09:18:44 -070070// fs_type="yaffs2" partition_type="MTD" location=partition
71// fs_type="ext4" partition_type="EMMC" location=device
Doug Zongker512536a2010-02-17 16:11:44 -080072Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070073 char* result = NULL;
Doug Zongker3d177d02010-07-01 09:18:44 -070074 if (argc != 4) {
75 return ErrorAbort(state, "%s() expects 4 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -070076 }
Doug Zongker3d177d02010-07-01 09:18:44 -070077 char* fs_type;
78 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -070079 char* location;
80 char* mount_point;
Doug Zongker3d177d02010-07-01 09:18:44 -070081 if (ReadArgs(state, argv, 4, &fs_type, &partition_type,
82 &location, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070083 return NULL;
84 }
85
Doug Zongker3d177d02010-07-01 09:18:44 -070086 if (strlen(fs_type) == 0) {
87 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
88 goto done;
89 }
90 if (strlen(partition_type) == 0) {
91 ErrorAbort(state, "partition_type argument to %s() can't be empty",
92 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -070093 goto done;
94 }
95 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070096 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -070097 goto done;
98 }
99 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700100 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700101 goto done;
102 }
103
Stephen Smalley779701d2012-02-09 14:13:23 -0500104 char *secontext = NULL;
105
106 if (sehandle) {
107 selabel_lookup(sehandle, &secontext, mount_point, 0755);
108 setfscreatecon(secontext);
109 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500110
Doug Zongker9931f7f2009-06-10 14:11:53 -0700111 mkdir(mount_point, 0755);
112
Stephen Smalley779701d2012-02-09 14:13:23 -0500113 if (secontext) {
114 freecon(secontext);
115 setfscreatecon(NULL);
116 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500117
Doug Zongker3d177d02010-07-01 09:18:44 -0700118 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700119 mtd_scan_partitions();
120 const MtdPartition* mtd;
121 mtd = mtd_find_partition_by_name(location);
122 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700123 printf("%s: no mtd partition named \"%s\"",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700124 name, location);
125 result = strdup("");
126 goto done;
127 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700128 if (mtd_mount_partition(mtd, mount_point, fs_type, 0 /* rw */) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700129 printf("mtd mount of %s failed: %s\n",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700130 location, strerror(errno));
131 result = strdup("");
132 goto done;
133 }
134 result = mount_point;
135 } else {
Doug Zongker3d177d02010-07-01 09:18:44 -0700136 if (mount(location, mount_point, fs_type,
Doug Zongker9931f7f2009-06-10 14:11:53 -0700137 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "") < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700138 printf("%s: failed to mount %s at %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700139 name, location, mount_point, strerror(errno));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700140 result = strdup("");
141 } else {
142 result = mount_point;
143 }
144 }
145
146done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700147 free(fs_type);
148 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700149 free(location);
150 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800151 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700152}
153
Doug Zongker8edb00c2009-06-11 17:21:44 -0700154
155// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800156Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700157 char* result = NULL;
158 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700159 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700160 }
161 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700162 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700163 return NULL;
164 }
165 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700166 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700167 goto done;
168 }
169
170 scan_mounted_volumes();
171 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
172 if (vol == NULL) {
173 result = strdup("");
174 } else {
175 result = mount_point;
176 }
177
178done:
179 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800180 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700181}
182
183
Doug Zongker512536a2010-02-17 16:11:44 -0800184Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700185 char* result = NULL;
186 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700187 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700188 }
189 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700190 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700191 return NULL;
192 }
193 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700194 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700195 goto done;
196 }
197
198 scan_mounted_volumes();
199 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
200 if (vol == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700201 printf("unmount of %s failed; no such volume\n", mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700202 result = strdup("");
203 } else {
204 unmount_mounted_volume(vol);
205 result = mount_point;
206 }
207
208done:
209 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800210 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700211}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700212
JP Abgrall37aedb32014-06-16 19:07:39 -0700213static int exec_cmd(const char* path, char* const argv[]) {
214 int status;
215 pid_t child;
216 if ((child = vfork()) == 0) {
217 execv(path, argv);
218 _exit(-1);
219 }
220 waitpid(child, &status, 0);
221 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
222 printf("%s failed with status %d\n", path, WEXITSTATUS(status));
223 }
224 return WEXITSTATUS(status);
225}
226
Doug Zongker8edb00c2009-06-11 17:21:44 -0700227
Stephen Smalley779701d2012-02-09 14:13:23 -0500228// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700229//
Stephen Smalley779701d2012-02-09 14:13:23 -0500230// fs_type="yaffs2" partition_type="MTD" location=partition fs_size=<bytes> mount_point=<location>
231// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700232// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
233// if fs_size == 0, then make fs uses the entire partition.
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800234// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700235// 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 -0800236Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700237 char* result = NULL;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400238 if (argc != 5) {
239 return ErrorAbort(state, "%s() expects 5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700240 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700241 char* fs_type;
242 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700243 char* location;
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800244 char* fs_size;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400245 char* mount_point;
Stephen Smalley779701d2012-02-09 14:13:23 -0500246
Stephen Smalley779701d2012-02-09 14:13:23 -0500247 if (ReadArgs(state, argv, 5, &fs_type, &partition_type, &location, &fs_size, &mount_point) < 0) {
248 return NULL;
249 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700250
Doug Zongker3d177d02010-07-01 09:18:44 -0700251 if (strlen(fs_type) == 0) {
252 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
253 goto done;
254 }
255 if (strlen(partition_type) == 0) {
256 ErrorAbort(state, "partition_type argument to %s() can't be empty",
257 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700258 goto done;
259 }
260 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700261 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700262 goto done;
263 }
264
Stephen Smalley516e4e22012-04-03 13:35:11 -0400265 if (strlen(mount_point) == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500266 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
267 goto done;
268 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500269
Doug Zongker3d177d02010-07-01 09:18:44 -0700270 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700271 mtd_scan_partitions();
272 const MtdPartition* mtd = mtd_find_partition_by_name(location);
273 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700274 printf("%s: no mtd partition named \"%s\"",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700275 name, location);
276 result = strdup("");
277 goto done;
278 }
279 MtdWriteContext* ctx = mtd_write_partition(mtd);
280 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700281 printf("%s: can't write \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700282 result = strdup("");
283 goto done;
284 }
285 if (mtd_erase_blocks(ctx, -1) == -1) {
286 mtd_write_close(ctx);
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700287 printf("%s: failed to erase \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700288 result = strdup("");
289 goto done;
290 }
291 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700292 printf("%s: failed to close \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700293 result = strdup("");
294 goto done;
295 }
296 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700297#ifdef USE_EXT4
298 } else if (strcmp(fs_type, "ext4") == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500299 int status = make_ext4fs(location, atoll(fs_size), mount_point, sehandle);
Doug Zongker3d177d02010-07-01 09:18:44 -0700300 if (status != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700301 printf("%s: make_ext4fs failed (%d) on %s",
Doug Zongker3d177d02010-07-01 09:18:44 -0700302 name, status, location);
303 result = strdup("");
304 goto done;
305 }
306 result = location;
JP Abgrall37aedb32014-06-16 19:07:39 -0700307 } else if (strcmp(fs_type, "f2fs") == 0) {
308 char *num_sectors;
309 if (asprintf(&num_sectors, "%lld", atoll(fs_size) / 512) <= 0) {
310 printf("format_volume: failed to create %s command for %s\n", fs_type, location);
311 result = strdup("");
312 goto done;
313 }
314 const char *f2fs_path = "/sbin/mkfs.f2fs";
315 const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", location, num_sectors, NULL};
316 int status = exec_cmd(f2fs_path, (char* const*)f2fs_argv);
317 free(num_sectors);
318 if (status != 0) {
319 printf("%s: mkfs.f2fs failed (%d) on %s",
320 name, status, location);
321 result = strdup("");
322 goto done;
323 }
324 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700325#endif
Doug Zongker9931f7f2009-06-10 14:11:53 -0700326 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700327 printf("%s: unsupported fs_type \"%s\" partition_type \"%s\"",
Doug Zongker3d177d02010-07-01 09:18:44 -0700328 name, fs_type, partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700329 }
330
331done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700332 free(fs_type);
333 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700334 if (result != location) free(location);
Doug Zongker512536a2010-02-17 16:11:44 -0800335 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700336}
337
Michael Rungece7ca712013-11-06 17:42:20 -0800338Value* RenameFn(const char* name, State* state, int argc, Expr* argv[]) {
339 char* result = NULL;
340 if (argc != 2) {
341 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
342 }
343
344 char* src_name;
345 char* dst_name;
346
347 if (ReadArgs(state, argv, 2, &src_name, &dst_name) < 0) {
348 return NULL;
349 }
350 if (strlen(src_name) == 0) {
351 ErrorAbort(state, "src_name argument to %s() can't be empty", name);
352 goto done;
353 }
354 if (strlen(dst_name) == 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700355 ErrorAbort(state, "dst_name argument to %s() can't be empty", name);
Michael Rungece7ca712013-11-06 17:42:20 -0800356 goto done;
357 }
Michael Rungea91ecc52014-07-21 17:40:02 -0700358 if (make_parents(dst_name) != 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700359 ErrorAbort(state, "Creating parent of %s failed, error %s",
Michael Rungea91ecc52014-07-21 17:40:02 -0700360 dst_name, strerror(errno));
361 } else if (rename(src_name, dst_name) != 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700362 ErrorAbort(state, "Rename of %s to %s failed, error %s",
Michael Rungece7ca712013-11-06 17:42:20 -0800363 src_name, dst_name, strerror(errno));
364 } else {
365 result = dst_name;
366 }
367
368done:
369 free(src_name);
370 if (result != dst_name) free(dst_name);
371 return StringValue(result);
372}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700373
Doug Zongker512536a2010-02-17 16:11:44 -0800374Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700375 char** paths = malloc(argc * sizeof(char*));
376 int i;
377 for (i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700378 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700379 if (paths[i] == NULL) {
380 int j;
381 for (j = 0; j < i; ++i) {
382 free(paths[j]);
383 }
384 free(paths);
385 return NULL;
386 }
387 }
388
389 bool recursive = (strcmp(name, "delete_recursive") == 0);
390
391 int success = 0;
392 for (i = 0; i < argc; ++i) {
393 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
394 ++success;
395 free(paths[i]);
396 }
397 free(paths);
398
399 char buffer[10];
400 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800401 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700402}
403
Doug Zongker8edb00c2009-06-11 17:21:44 -0700404
Doug Zongker512536a2010-02-17 16:11:44 -0800405Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700406 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700407 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700408 }
409 char* frac_str;
410 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700411 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700412 return NULL;
413 }
414
415 double frac = strtod(frac_str, NULL);
416 int sec = strtol(sec_str, NULL, 10);
417
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700418 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700419 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
420
Doug Zongker9931f7f2009-06-10 14:11:53 -0700421 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800422 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700423}
424
Doug Zongker512536a2010-02-17 16:11:44 -0800425Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700426 if (argc != 1) {
427 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
428 }
429 char* frac_str;
430 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
431 return NULL;
432 }
433
434 double frac = strtod(frac_str, NULL);
435
436 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
437 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
438
Doug Zongker512536a2010-02-17 16:11:44 -0800439 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700440}
441
Doug Zongker8edb00c2009-06-11 17:21:44 -0700442// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800443Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700444 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700445 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700446 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700447 }
448 char* zip_path;
449 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700450 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700451
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700452 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700453
454 // To create a consistent system image, never use the clock for timestamps.
455 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
456
457 bool success = mzExtractRecursive(za, zip_path, dest_path,
458 MZ_EXTRACT_FILES_ONLY, &timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500459 NULL, NULL, sehandle);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700460 free(zip_path);
461 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800462 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700463}
464
Doug Zongker8edb00c2009-06-11 17:21:44 -0700465
466// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800467// or
468// package_extract_file(package_path)
469// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800470// function (the char* returned is actually a FileContents*).
471Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700472 int argc, Expr* argv[]) {
Doug Zongkeradad8ec2014-08-22 14:53:43 -0700473 if (argc < 1 || argc > 2) {
474 return ErrorAbort(state, "%s() expects 1 or 2 args, got %d",
Doug Zongker6aece332010-02-01 14:40:12 -0800475 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700476 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700477 bool success = false;
Doug Zongker43772d22014-06-09 14:13:19 -0700478
479 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker43772d22014-06-09 14:13:19 -0700480
Doug Zongkeradad8ec2014-08-22 14:53:43 -0700481 if (argc == 2) {
482 // The two-argument version extracts to a file.
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800483
484 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700485
Doug Zongker6aece332010-02-01 14:40:12 -0800486 char* zip_path;
487 char* dest_path;
Doug Zongkeradad8ec2014-08-22 14:53:43 -0700488 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800489
Doug Zongker6aece332010-02-01 14:40:12 -0800490 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
491 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700492 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800493 goto done2;
494 }
495
496 FILE* f = fopen(dest_path, "wb");
497 if (f == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700498 printf("%s: can't open %s for write: %s\n",
Doug Zongker6aece332010-02-01 14:40:12 -0800499 name, dest_path, strerror(errno));
500 goto done2;
501 }
Doug Zongkeradad8ec2014-08-22 14:53:43 -0700502 success = mzExtractZipEntryToFile(za, entry, fileno(f));
Doug Zongker6aece332010-02-01 14:40:12 -0800503 fclose(f);
504
505 done2:
506 free(zip_path);
507 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800508 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800509 } else {
510 // The one-argument version returns the contents of the file
511 // as the result.
512
513 char* zip_path;
Doug Zongker512536a2010-02-17 16:11:44 -0800514 Value* v = malloc(sizeof(Value));
515 v->type = VAL_BLOB;
516 v->size = -1;
517 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800518
519 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
520
521 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
522 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
523 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700524 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800525 goto done1;
526 }
527
Doug Zongker512536a2010-02-17 16:11:44 -0800528 v->size = mzGetZipEntryUncompLen(entry);
529 v->data = malloc(v->size);
530 if (v->data == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700531 printf("%s: failed to allocate %ld bytes for %s\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800532 name, (long)v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800533 goto done1;
534 }
535
Doug Zongker512536a2010-02-17 16:11:44 -0800536 success = mzExtractZipEntryToBuffer(za, entry,
537 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800538
539 done1:
540 free(zip_path);
541 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800542 free(v->data);
543 v->data = NULL;
544 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800545 }
Doug Zongker512536a2010-02-17 16:11:44 -0800546 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700547 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700548}
549
Doug Zongkera23075f2012-08-06 16:19:09 -0700550// Create all parent directories of name, if necessary.
551static int make_parents(char* name) {
552 char* p;
553 for (p = name + (strlen(name)-1); p > name; --p) {
554 if (*p != '/') continue;
555 *p = '\0';
556 if (make_parents(name) < 0) return -1;
557 int result = mkdir(name, 0700);
Michael Rungea91ecc52014-07-21 17:40:02 -0700558 if (result == 0) printf("created [%s]\n", name);
Doug Zongkera23075f2012-08-06 16:19:09 -0700559 *p = '/';
560 if (result == 0 || errno == EEXIST) {
561 // successfully created or already existed; we're done
562 return 0;
563 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700564 printf("failed to mkdir %s: %s\n", name, strerror(errno));
Doug Zongkera23075f2012-08-06 16:19:09 -0700565 return -1;
566 }
567 }
568 return 0;
569}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700570
Doug Zongker9931f7f2009-06-10 14:11:53 -0700571// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700572// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800573Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700574 if (argc == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700575 return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700576 }
577 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700578 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700579 if (target == NULL) return NULL;
580
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700581 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700582 if (srcs == NULL) {
583 free(target);
584 return NULL;
585 }
586
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700587 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700588 int i;
589 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700590 if (unlink(srcs[i]) < 0) {
591 if (errno != ENOENT) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700592 printf("%s: failed to remove %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700593 name, srcs[i], strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700594 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700595 }
596 }
Doug Zongkera23075f2012-08-06 16:19:09 -0700597 if (make_parents(srcs[i])) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700598 printf("%s: failed to symlink %s to %s: making parents failed\n",
Doug Zongkera23075f2012-08-06 16:19:09 -0700599 name, srcs[i], target);
600 ++bad;
601 }
Doug Zongker60babf82009-09-18 15:11:24 -0700602 if (symlink(target, srcs[i]) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700603 printf("%s: failed to symlink %s to %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700604 name, srcs[i], target, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700605 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700606 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700607 free(srcs[i]);
608 }
609 free(srcs);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700610 if (bad) {
611 return ErrorAbort(state, "%s: some symlinks failed", name);
612 }
Doug Zongker512536a2010-02-17 16:11:44 -0800613 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700614}
615
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700616struct perm_parsed_args {
617 bool has_uid;
618 uid_t uid;
619 bool has_gid;
620 gid_t gid;
621 bool has_mode;
622 mode_t mode;
623 bool has_fmode;
624 mode_t fmode;
625 bool has_dmode;
626 mode_t dmode;
627 bool has_selabel;
628 char* selabel;
629 bool has_capabilities;
630 uint64_t capabilities;
631};
632
633static struct perm_parsed_args ParsePermArgs(int argc, char** args) {
634 int i;
635 struct perm_parsed_args parsed;
636 int bad = 0;
637 static int max_warnings = 20;
638
639 memset(&parsed, 0, sizeof(parsed));
640
641 for (i = 1; i < argc; i += 2) {
642 if (strcmp("uid", args[i]) == 0) {
643 int64_t uid;
644 if (sscanf(args[i+1], "%" SCNd64, &uid) == 1) {
645 parsed.uid = uid;
646 parsed.has_uid = true;
647 } else {
648 printf("ParsePermArgs: invalid UID \"%s\"\n", args[i + 1]);
649 bad++;
650 }
651 continue;
652 }
653 if (strcmp("gid", args[i]) == 0) {
654 int64_t gid;
655 if (sscanf(args[i+1], "%" SCNd64, &gid) == 1) {
656 parsed.gid = gid;
657 parsed.has_gid = true;
658 } else {
659 printf("ParsePermArgs: invalid GID \"%s\"\n", args[i + 1]);
660 bad++;
661 }
662 continue;
663 }
664 if (strcmp("mode", args[i]) == 0) {
665 int32_t mode;
666 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
667 parsed.mode = mode;
668 parsed.has_mode = true;
669 } else {
670 printf("ParsePermArgs: invalid mode \"%s\"\n", args[i + 1]);
671 bad++;
672 }
673 continue;
674 }
675 if (strcmp("dmode", args[i]) == 0) {
676 int32_t mode;
677 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
678 parsed.dmode = mode;
679 parsed.has_dmode = true;
680 } else {
681 printf("ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1]);
682 bad++;
683 }
684 continue;
685 }
686 if (strcmp("fmode", args[i]) == 0) {
687 int32_t mode;
688 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
689 parsed.fmode = mode;
690 parsed.has_fmode = true;
691 } else {
692 printf("ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1]);
693 bad++;
694 }
695 continue;
696 }
697 if (strcmp("capabilities", args[i]) == 0) {
698 int64_t capabilities;
699 if (sscanf(args[i+1], "%" SCNi64, &capabilities) == 1) {
700 parsed.capabilities = capabilities;
701 parsed.has_capabilities = true;
702 } else {
703 printf("ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1]);
704 bad++;
705 }
706 continue;
707 }
708 if (strcmp("selabel", args[i]) == 0) {
709 if (args[i+1][0] != '\0') {
710 parsed.selabel = args[i+1];
711 parsed.has_selabel = true;
712 } else {
713 printf("ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1]);
714 bad++;
715 }
716 continue;
717 }
718 if (max_warnings != 0) {
719 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i]);
720 max_warnings--;
721 if (max_warnings == 0) {
722 printf("ParsedPermArgs: suppressing further warnings\n");
723 }
724 }
725 }
726 return parsed;
727}
728
729static int ApplyParsedPerms(
730 const char* filename,
731 const struct stat *statptr,
732 struct perm_parsed_args parsed)
733{
734 int bad = 0;
735
Nick Kraleviche4612512013-09-10 15:34:19 -0700736 /* ignore symlinks */
737 if (S_ISLNK(statptr->st_mode)) {
738 return 0;
739 }
740
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700741 if (parsed.has_uid) {
742 if (chown(filename, parsed.uid, -1) < 0) {
743 printf("ApplyParsedPerms: chown of %s to %d failed: %s\n",
744 filename, parsed.uid, strerror(errno));
745 bad++;
746 }
747 }
748
749 if (parsed.has_gid) {
750 if (chown(filename, -1, parsed.gid) < 0) {
751 printf("ApplyParsedPerms: chgrp of %s to %d failed: %s\n",
752 filename, parsed.gid, strerror(errno));
753 bad++;
754 }
755 }
756
757 if (parsed.has_mode) {
758 if (chmod(filename, parsed.mode) < 0) {
759 printf("ApplyParsedPerms: chmod of %s to %d failed: %s\n",
760 filename, parsed.mode, strerror(errno));
761 bad++;
762 }
763 }
764
765 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
766 if (chmod(filename, parsed.dmode) < 0) {
767 printf("ApplyParsedPerms: chmod of %s to %d failed: %s\n",
768 filename, parsed.dmode, strerror(errno));
769 bad++;
770 }
771 }
772
773 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
774 if (chmod(filename, parsed.fmode) < 0) {
775 printf("ApplyParsedPerms: chmod of %s to %d failed: %s\n",
776 filename, parsed.fmode, strerror(errno));
777 bad++;
778 }
779 }
780
781 if (parsed.has_selabel) {
782 // TODO: Don't silently ignore ENOTSUP
783 if (lsetfilecon(filename, parsed.selabel) && (errno != ENOTSUP)) {
784 printf("ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n",
785 filename, parsed.selabel, strerror(errno));
786 bad++;
787 }
788 }
789
790 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
791 if (parsed.capabilities == 0) {
792 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
793 // Report failure unless it's ENODATA (attribute not set)
794 printf("ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n",
795 filename, parsed.capabilities, strerror(errno));
796 bad++;
797 }
798 } else {
799 struct vfs_cap_data cap_data;
800 memset(&cap_data, 0, sizeof(cap_data));
801 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
802 cap_data.data[0].permitted = (uint32_t) (parsed.capabilities & 0xffffffff);
803 cap_data.data[0].inheritable = 0;
804 cap_data.data[1].permitted = (uint32_t) (parsed.capabilities >> 32);
805 cap_data.data[1].inheritable = 0;
806 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
807 printf("ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n",
808 filename, parsed.capabilities, strerror(errno));
809 bad++;
810 }
811 }
812 }
813
814 return bad;
815}
816
817// nftw doesn't allow us to pass along context, so we need to use
818// global variables. *sigh*
819static struct perm_parsed_args recursive_parsed_args;
820
821static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr,
822 int fileflags, struct FTW *pfwt) {
823 return ApplyParsedPerms(filename, statptr, recursive_parsed_args);
824}
825
826static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
827 int i;
828 int bad = 0;
829 static int nwarnings = 0;
830 struct stat sb;
831 Value* result = NULL;
832
833 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
834
835 if ((argc % 2) != 1) {
836 return ErrorAbort(state, "%s() expects an odd number of arguments, got %d",
837 name, argc);
838 }
839
840 char** args = ReadVarArgs(state, argc, argv);
841 if (args == NULL) return NULL;
842
843 if (lstat(args[0], &sb) == -1) {
844 result = ErrorAbort(state, "%s: Error on lstat of \"%s\": %s", name, args[0], strerror(errno));
845 goto done;
846 }
847
848 struct perm_parsed_args parsed = ParsePermArgs(argc, args);
849
850 if (recursive) {
851 recursive_parsed_args = parsed;
852 bad += nftw(args[0], do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
853 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
854 } else {
855 bad += ApplyParsedPerms(args[0], &sb, parsed);
856 }
857
858done:
859 for (i = 0; i < argc; ++i) {
860 free(args[i]);
861 }
862 free(args);
863
864 if (result != NULL) {
865 return result;
866 }
867
868 if (bad > 0) {
869 return ErrorAbort(state, "%s: some changes failed", name);
870 }
871
872 return StringValue(strdup(""));
873}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700874
Doug Zongker512536a2010-02-17 16:11:44 -0800875Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700876 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700877 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700878 }
879 char* key;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700880 key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700881 if (key == NULL) return NULL;
882
883 char value[PROPERTY_VALUE_MAX];
884 property_get(key, value, "");
885 free(key);
886
Doug Zongker512536a2010-02-17 16:11:44 -0800887 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700888}
889
890
Doug Zongker47cace92009-06-18 10:11:50 -0700891// file_getprop(file, key)
892//
893// interprets 'file' as a getprop-style file (key=value pairs, one
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700894// per line. # comment lines,blank lines, lines without '=' ignored),
895// and returns the value for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800896Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700897 char* result = NULL;
898 char* buffer = NULL;
899 char* filename;
900 char* key;
901 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
902 return NULL;
903 }
904
905 struct stat st;
906 if (stat(filename, &st) < 0) {
907 ErrorAbort(state, "%s: failed to stat \"%s\": %s",
908 name, filename, strerror(errno));
909 goto done;
910 }
911
912#define MAX_FILE_GETPROP_SIZE 65536
913
914 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
915 ErrorAbort(state, "%s too large for %s (max %d)",
916 filename, name, MAX_FILE_GETPROP_SIZE);
917 goto done;
918 }
919
920 buffer = malloc(st.st_size+1);
921 if (buffer == NULL) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700922 ErrorAbort(state, "%s: failed to alloc %lld bytes", name, (long long)st.st_size+1);
Doug Zongker47cace92009-06-18 10:11:50 -0700923 goto done;
924 }
925
926 FILE* f = fopen(filename, "rb");
927 if (f == NULL) {
928 ErrorAbort(state, "%s: failed to open %s: %s",
929 name, filename, strerror(errno));
930 goto done;
931 }
932
933 if (fread(buffer, 1, st.st_size, f) != st.st_size) {
Doug Zongkera23075f2012-08-06 16:19:09 -0700934 ErrorAbort(state, "%s: failed to read %lld bytes from %s",
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700935 name, (long long)st.st_size+1, filename);
Doug Zongker47cace92009-06-18 10:11:50 -0700936 fclose(f);
937 goto done;
938 }
939 buffer[st.st_size] = '\0';
940
941 fclose(f);
942
943 char* line = strtok(buffer, "\n");
944 do {
945 // skip whitespace at start of line
946 while (*line && isspace(*line)) ++line;
947
948 // comment or blank line: skip to next line
949 if (*line == '\0' || *line == '#') continue;
950
951 char* equal = strchr(line, '=');
952 if (equal == NULL) {
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700953 continue;
Doug Zongker47cace92009-06-18 10:11:50 -0700954 }
955
956 // trim whitespace between key and '='
957 char* key_end = equal-1;
958 while (key_end > line && isspace(*key_end)) --key_end;
959 key_end[1] = '\0';
960
961 // not the key we're looking for
962 if (strcmp(key, line) != 0) continue;
963
964 // skip whitespace after the '=' to the start of the value
965 char* val_start = equal+1;
966 while(*val_start && isspace(*val_start)) ++val_start;
967
968 // trim trailing whitespace
969 char* val_end = val_start + strlen(val_start)-1;
970 while (val_end > val_start && isspace(*val_end)) --val_end;
971 val_end[1] = '\0';
972
973 result = strdup(val_start);
974 break;
975
976 } while ((line = strtok(NULL, "\n")));
977
978 if (result == NULL) result = strdup("");
979
980 done:
981 free(filename);
982 free(key);
983 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -0800984 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -0700985}
986
987
Doug Zongker8edb00c2009-06-11 17:21:44 -0700988static bool write_raw_image_cb(const unsigned char* data,
989 int data_len, void* ctx) {
990 int r = mtd_write_data((MtdWriteContext*)ctx, (const char *)data, data_len);
991 if (r == data_len) return true;
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700992 printf("%s\n", strerror(errno));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700993 return false;
994}
995
Doug Zongker179b2d92011-04-12 15:49:04 -0700996// write_raw_image(filename_or_blob, partition)
Doug Zongker512536a2010-02-17 16:11:44 -0800997Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700998 char* result = NULL;
999
Doug Zongker179b2d92011-04-12 15:49:04 -07001000 Value* partition_value;
1001 Value* contents;
1002 if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001003 return NULL;
1004 }
1005
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001006 char* partition = NULL;
Doug Zongker179b2d92011-04-12 15:49:04 -07001007 if (partition_value->type != VAL_STRING) {
1008 ErrorAbort(state, "partition argument to %s must be string", name);
1009 goto done;
1010 }
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001011 partition = partition_value->data;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001012 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001013 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001014 goto done;
1015 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001016 if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001017 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001018 goto done;
1019 }
1020
1021 mtd_scan_partitions();
1022 const MtdPartition* mtd = mtd_find_partition_by_name(partition);
1023 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001024 printf("%s: no mtd partition named \"%s\"\n", name, partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001025 result = strdup("");
1026 goto done;
1027 }
1028
1029 MtdWriteContext* ctx = mtd_write_partition(mtd);
1030 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001031 printf("%s: can't write mtd partition \"%s\"\n",
Doug Zongker8edb00c2009-06-11 17:21:44 -07001032 name, partition);
1033 result = strdup("");
1034 goto done;
1035 }
1036
1037 bool success;
1038
Doug Zongker179b2d92011-04-12 15:49:04 -07001039 if (contents->type == VAL_STRING) {
1040 // we're given a filename as the contents
1041 char* filename = contents->data;
1042 FILE* f = fopen(filename, "rb");
1043 if (f == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001044 printf("%s: can't open %s: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -07001045 name, filename, strerror(errno));
1046 result = strdup("");
1047 goto done;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001048 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001049
1050 success = true;
1051 char* buffer = malloc(BUFSIZ);
1052 int read;
1053 while (success && (read = fread(buffer, 1, BUFSIZ, f)) > 0) {
1054 int wrote = mtd_write_data(ctx, buffer, read);
1055 success = success && (wrote == read);
1056 }
1057 free(buffer);
1058 fclose(f);
1059 } else {
1060 // we're given a blob as the contents
1061 ssize_t wrote = mtd_write_data(ctx, contents->data, contents->size);
1062 success = (wrote == contents->size);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001063 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001064 if (!success) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001065 printf("mtd_write_data to %s failed: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -07001066 partition, strerror(errno));
1067 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001068
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001069 if (mtd_erase_blocks(ctx, -1) == -1) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001070 printf("%s: error erasing blocks of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001071 }
1072 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001073 printf("%s: error closing write of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001074 }
1075
Doug Zongker179b2d92011-04-12 15:49:04 -07001076 printf("%s %s partition\n",
1077 success ? "wrote" : "failed to write", partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001078
1079 result = success ? partition : strdup("");
1080
1081done:
Doug Zongker179b2d92011-04-12 15:49:04 -07001082 if (result != partition) FreeValue(partition_value);
1083 FreeValue(contents);
Doug Zongker512536a2010-02-17 16:11:44 -08001084 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001085}
1086
Doug Zongker8edb00c2009-06-11 17:21:44 -07001087// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001088Value* ApplyPatchSpaceFn(const char* name, State* state,
1089 int argc, Expr* argv[]) {
1090 char* bytes_str;
1091 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
1092 return NULL;
1093 }
1094
1095 char* endptr;
1096 size_t bytes = strtol(bytes_str, &endptr, 10);
1097 if (bytes == 0 && endptr == bytes_str) {
1098 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n",
1099 name, bytes_str);
1100 free(bytes_str);
1101 return NULL;
1102 }
1103
1104 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
1105}
1106
Doug Zongker52b40362014-02-10 15:30:30 -08001107// apply_patch(file, size, init_sha1, tgt_sha1, patch)
1108
Doug Zongker512536a2010-02-17 16:11:44 -08001109Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001110 if (argc < 6 || (argc % 2) == 1) {
1111 return ErrorAbort(state, "%s(): expected at least 6 args and an "
1112 "even number, got %d",
1113 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001114 }
1115
Doug Zongkerc4351c72010-02-22 14:46:32 -08001116 char* source_filename;
1117 char* target_filename;
1118 char* target_sha1;
1119 char* target_size_str;
1120 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
1121 &target_sha1, &target_size_str) < 0) {
1122 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001123 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001124
Doug Zongkerc4351c72010-02-22 14:46:32 -08001125 char* endptr;
1126 size_t target_size = strtol(target_size_str, &endptr, 10);
1127 if (target_size == 0 && endptr == target_size_str) {
1128 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count",
1129 name, target_size_str);
1130 free(source_filename);
1131 free(target_filename);
1132 free(target_sha1);
1133 free(target_size_str);
1134 return NULL;
1135 }
1136
1137 int patchcount = (argc-4) / 2;
1138 Value** patches = ReadValueVarArgs(state, argc-4, argv+4);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001139
1140 int i;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001141 for (i = 0; i < patchcount; ++i) {
1142 if (patches[i*2]->type != VAL_STRING) {
1143 ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i);
1144 break;
1145 }
1146 if (patches[i*2+1]->type != VAL_BLOB) {
1147 ErrorAbort(state, "%s(): patch #%d is not blob", name, i);
1148 break;
1149 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001150 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001151 if (i != patchcount) {
1152 for (i = 0; i < patchcount*2; ++i) {
1153 FreeValue(patches[i]);
1154 }
1155 free(patches);
1156 return NULL;
1157 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001158
Doug Zongkerc4351c72010-02-22 14:46:32 -08001159 char** patch_sha_str = malloc(patchcount * sizeof(char*));
1160 for (i = 0; i < patchcount; ++i) {
1161 patch_sha_str[i] = patches[i*2]->data;
1162 patches[i*2]->data = NULL;
1163 FreeValue(patches[i*2]);
1164 patches[i] = patches[i*2+1];
Doug Zongker8edb00c2009-06-11 17:21:44 -07001165 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001166
1167 int result = applypatch(source_filename, target_filename,
1168 target_sha1, target_size,
Doug Zongkera3ccba62012-08-20 15:28:02 -07001169 patchcount, patch_sha_str, patches, NULL);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001170
1171 for (i = 0; i < patchcount; ++i) {
1172 FreeValue(patches[i]);
1173 }
1174 free(patch_sha_str);
1175 free(patches);
1176
1177 return StringValue(strdup(result == 0 ? "t" : ""));
1178}
1179
1180// apply_patch_check(file, [sha1_1, ...])
1181Value* ApplyPatchCheckFn(const char* name, State* state,
1182 int argc, Expr* argv[]) {
1183 if (argc < 1) {
1184 return ErrorAbort(state, "%s(): expected at least 1 arg, got %d",
1185 name, argc);
1186 }
1187
1188 char* filename;
1189 if (ReadArgs(state, argv, 1, &filename) < 0) {
1190 return NULL;
1191 }
1192
1193 int patchcount = argc-1;
1194 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
1195
1196 int result = applypatch_check(filename, patchcount, sha1s);
1197
1198 int i;
1199 for (i = 0; i < patchcount; ++i) {
1200 free(sha1s[i]);
1201 }
1202 free(sha1s);
1203
1204 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001205}
1206
Doug Zongker512536a2010-02-17 16:11:44 -08001207Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001208 char** args = ReadVarArgs(state, argc, argv);
1209 if (args == NULL) {
1210 return NULL;
1211 }
1212
1213 int size = 0;
1214 int i;
1215 for (i = 0; i < argc; ++i) {
1216 size += strlen(args[i]);
1217 }
1218 char* buffer = malloc(size+1);
1219 size = 0;
1220 for (i = 0; i < argc; ++i) {
1221 strcpy(buffer+size, args[i]);
1222 size += strlen(args[i]);
1223 free(args[i]);
1224 }
1225 free(args);
1226 buffer[size] = '\0';
1227
1228 char* line = strtok(buffer, "\n");
1229 while (line) {
1230 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe,
1231 "ui_print %s\n", line);
1232 line = strtok(NULL, "\n");
1233 }
1234 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "ui_print\n");
1235
Doug Zongker512536a2010-02-17 16:11:44 -08001236 return StringValue(buffer);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001237}
1238
Doug Zongkerd0181b82011-10-19 10:51:12 -07001239Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
1240 if (argc != 0) {
1241 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1242 }
1243 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
1244 return StringValue(strdup("t"));
1245}
1246
Doug Zongker512536a2010-02-17 16:11:44 -08001247Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001248 if (argc < 1) {
1249 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1250 }
1251 char** args = ReadVarArgs(state, argc, argv);
1252 if (args == NULL) {
1253 return NULL;
1254 }
1255
1256 char** args2 = malloc(sizeof(char*) * (argc+1));
1257 memcpy(args2, args, sizeof(char*) * argc);
1258 args2[argc] = NULL;
1259
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001260 printf("about to run program [%s] with %d args\n", args2[0], argc);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001261
1262 pid_t child = fork();
1263 if (child == 0) {
1264 execv(args2[0], args2);
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001265 printf("run_program: execv failed: %s\n", strerror(errno));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001266 _exit(1);
1267 }
1268 int status;
1269 waitpid(child, &status, 0);
1270 if (WIFEXITED(status)) {
1271 if (WEXITSTATUS(status) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001272 printf("run_program: child exited with status %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001273 WEXITSTATUS(status));
1274 }
1275 } else if (WIFSIGNALED(status)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001276 printf("run_program: child terminated by signal %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001277 WTERMSIG(status));
1278 }
1279
1280 int i;
1281 for (i = 0; i < argc; ++i) {
1282 free(args[i]);
1283 }
1284 free(args);
1285 free(args2);
1286
1287 char buffer[20];
1288 sprintf(buffer, "%d", status);
1289
Doug Zongker512536a2010-02-17 16:11:44 -08001290 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001291}
1292
Doug Zongker512536a2010-02-17 16:11:44 -08001293// sha1_check(data)
1294// to return the sha1 of the data (given in the format returned by
1295// read_file).
1296//
1297// sha1_check(data, sha1_hex, [sha1_hex, ...])
1298// returns the sha1 of the file if it matches any of the hex
1299// strings passed, or "" if it does not equal any of them.
1300//
1301Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1302 if (argc < 1) {
1303 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1304 }
1305
1306 Value** args = ReadValueVarArgs(state, argc, argv);
1307 if (args == NULL) {
1308 return NULL;
1309 }
1310
1311 if (args[0]->size < 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001312 return StringValue(strdup(""));
1313 }
1314 uint8_t digest[SHA_DIGEST_SIZE];
Doug Zongkerbac7fba2013-04-10 11:32:17 -07001315 SHA_hash(args[0]->data, args[0]->size, digest);
Doug Zongker512536a2010-02-17 16:11:44 -08001316 FreeValue(args[0]);
1317
1318 if (argc == 1) {
1319 return StringValue(PrintSha1(digest));
1320 }
1321
1322 int i;
1323 uint8_t* arg_digest = malloc(SHA_DIGEST_SIZE);
1324 for (i = 1; i < argc; ++i) {
1325 if (args[i]->type != VAL_STRING) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001326 printf("%s(): arg %d is not a string; skipping",
Doug Zongker512536a2010-02-17 16:11:44 -08001327 name, i);
1328 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1329 // Warn about bad args and skip them.
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001330 printf("%s(): error parsing \"%s\" as sha-1; skipping",
1331 name, args[i]->data);
Doug Zongker512536a2010-02-17 16:11:44 -08001332 } else if (memcmp(digest, arg_digest, SHA_DIGEST_SIZE) == 0) {
1333 break;
1334 }
1335 FreeValue(args[i]);
1336 }
1337 if (i >= argc) {
1338 // Didn't match any of the hex strings; return false.
1339 return StringValue(strdup(""));
1340 }
1341 // Found a match; free all the remaining arguments and return the
1342 // matched one.
1343 int j;
1344 for (j = i+1; j < argc; ++j) {
1345 FreeValue(args[j]);
1346 }
1347 return args[i];
1348}
1349
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001350// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001351// is actually a FileContents*).
1352Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1353 if (argc != 1) {
1354 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1355 }
1356 char* filename;
1357 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1358
1359 Value* v = malloc(sizeof(Value));
1360 v->type = VAL_BLOB;
1361
1362 FileContents fc;
Doug Zongkera1bc1482014-02-13 15:18:19 -08001363 if (LoadFileContents(filename, &fc) != 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001364 free(filename);
Michael Runge6eed2242013-12-13 17:13:11 -08001365 v->size = -1;
1366 v->data = NULL;
Doug Zongker512536a2010-02-17 16:11:44 -08001367 free(fc.data);
Michael Runge6eed2242013-12-13 17:13:11 -08001368 return v;
Doug Zongker512536a2010-02-17 16:11:44 -08001369 }
1370
1371 v->size = fc.size;
1372 v->data = (char*)fc.data;
1373
1374 free(filename);
1375 return v;
1376}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001377
Doug Zongkerc87bab12013-11-25 13:53:25 -08001378// Immediately reboot the device. Recovery is not finished normally,
1379// so if you reboot into recovery it will re-start applying the
1380// current package (because nothing has cleared the copy of the
1381// arguments stored in the BCB).
1382//
1383// The argument is the partition name passed to the android reboot
1384// property. It can be "recovery" to boot from the recovery
1385// partition, or "" (empty string) to boot from the regular boot
1386// partition.
1387Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) {
1388 if (argc != 2) {
1389 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1390 }
1391
1392 char* filename;
1393 char* property;
1394 if (ReadArgs(state, argv, 2, &filename, &property) < 0) return NULL;
1395
1396 char buffer[80];
1397
1398 // zero out the 'command' field of the bootloader message.
1399 memset(buffer, 0, sizeof(((struct bootloader_message*)0)->command));
1400 FILE* f = fopen(filename, "r+b");
1401 fseek(f, offsetof(struct bootloader_message, command), SEEK_SET);
1402 fwrite(buffer, sizeof(((struct bootloader_message*)0)->command), 1, f);
1403 fclose(f);
1404 free(filename);
1405
1406 strcpy(buffer, "reboot,");
1407 if (property != NULL) {
1408 strncat(buffer, property, sizeof(buffer)-10);
1409 }
1410
1411 property_set(ANDROID_RB_PROPERTY, buffer);
1412
1413 sleep(5);
1414 free(property);
1415 ErrorAbort(state, "%s() failed to reboot", name);
1416 return NULL;
1417}
1418
1419// Store a string value somewhere that future invocations of recovery
1420// can access it. This value is called the "stage" and can be used to
1421// drive packages that need to do reboots in the middle of
1422// installation and keep track of where they are in the multi-stage
1423// install.
1424//
1425// The first argument is the block device for the misc partition
1426// ("/misc" in the fstab), which is where this value is stored. The
1427// second argument is the string to store; it should not exceed 31
1428// bytes.
1429Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
1430 if (argc != 2) {
1431 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1432 }
1433
1434 char* filename;
1435 char* stagestr;
1436 if (ReadArgs(state, argv, 2, &filename, &stagestr) < 0) return NULL;
1437
1438 // Store this value in the misc partition, immediately after the
1439 // bootloader message that the main recovery uses to save its
1440 // arguments in case of the device restarting midway through
1441 // package installation.
1442 FILE* f = fopen(filename, "r+b");
1443 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1444 int to_write = strlen(stagestr)+1;
1445 int max_size = sizeof(((struct bootloader_message*)0)->stage);
1446 if (to_write > max_size) {
1447 to_write = max_size;
1448 stagestr[max_size-1] = 0;
1449 }
1450 fwrite(stagestr, to_write, 1, f);
1451 fclose(f);
1452
1453 free(stagestr);
1454 return StringValue(filename);
1455}
1456
1457// Return the value most recently saved with SetStageFn. The argument
1458// is the block device for the misc partition.
1459Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001460 if (argc != 1) {
Doug Zongkerc87bab12013-11-25 13:53:25 -08001461 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1462 }
1463
1464 char* filename;
1465 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1466
1467 char buffer[sizeof(((struct bootloader_message*)0)->stage)];
1468 FILE* f = fopen(filename, "rb");
1469 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1470 fread(buffer, sizeof(buffer), 1, f);
1471 fclose(f);
1472 buffer[sizeof(buffer)-1] = '\0';
1473
1474 return StringValue(strdup(buffer));
1475}
1476
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001477Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) {
1478 if (argc != 2) {
1479 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1480 }
1481
1482 char* filename;
1483 char* len_str;
1484 if (ReadArgs(state, argv, 2, &filename, &len_str) < 0) return NULL;
1485
1486 size_t len = strtoull(len_str, NULL, 0);
1487 int fd = open(filename, O_WRONLY, 0644);
1488 int success = wipe_block_device(fd, len);
1489
1490 free(filename);
1491 free(len_str);
1492
1493 close(fd);
1494
1495 return StringValue(strdup(success ? "t" : ""));
1496}
1497
Doug Zongkerc704e062014-05-23 08:40:35 -07001498Value* EnableRebootFn(const char* name, State* state, int argc, Expr* argv[]) {
1499 if (argc != 0) {
1500 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1501 }
1502 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
1503 fprintf(ui->cmd_pipe, "enable_reboot\n");
1504 return StringValue(strdup("t"));
1505}
1506
Doug Zongker9931f7f2009-06-10 14:11:53 -07001507void RegisterInstallFunctions() {
1508 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001509 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001510 RegisterFunction("unmount", UnmountFn);
1511 RegisterFunction("format", FormatFn);
1512 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001513 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001514 RegisterFunction("delete", DeleteFn);
1515 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001516 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1517 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001518 RegisterFunction("symlink", SymlinkFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001519
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001520 // Usage:
1521 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1522 // Example:
1523 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1524 RegisterFunction("set_metadata", SetMetadataFn);
1525
1526 // Usage:
1527 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1528 // Example:
1529 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1530 RegisterFunction("set_metadata_recursive", SetMetadataFn);
1531
Doug Zongker8edb00c2009-06-11 17:21:44 -07001532 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001533 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001534 RegisterFunction("write_raw_image", WriteRawImageFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001535
1536 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001537 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1538 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001539
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001540 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001541
Doug Zongker512536a2010-02-17 16:11:44 -08001542 RegisterFunction("read_file", ReadFileFn);
1543 RegisterFunction("sha1_check", Sha1CheckFn);
Michael Rungece7ca712013-11-06 17:42:20 -08001544 RegisterFunction("rename", RenameFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001545
Doug Zongkerd0181b82011-10-19 10:51:12 -07001546 RegisterFunction("wipe_cache", WipeCacheFn);
1547
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001548 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001549
1550 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001551
1552 RegisterFunction("reboot_now", RebootNowFn);
1553 RegisterFunction("get_stage", GetStageFn);
1554 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -07001555
1556 RegisterFunction("enable_reboot", EnableRebootFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001557}