blob: 42dbb58da438a5b9db3cb4a2c3836c4b0ef19a7f [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 Zongkerbc7ffed2014-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));
Michael Runged5b17272014-10-22 14:28:23 -0700361 } else if (access(dst_name, F_OK) == 0 && access(src_name, F_OK) != 0) {
362 // File was already moved
363 result = dst_name;
Michael Rungea91ecc52014-07-21 17:40:02 -0700364 } else if (rename(src_name, dst_name) != 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700365 ErrorAbort(state, "Rename of %s to %s failed, error %s",
Michael Rungece7ca712013-11-06 17:42:20 -0800366 src_name, dst_name, strerror(errno));
367 } else {
368 result = dst_name;
369 }
370
371done:
372 free(src_name);
373 if (result != dst_name) free(dst_name);
374 return StringValue(result);
375}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700376
Doug Zongker512536a2010-02-17 16:11:44 -0800377Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700378 char** paths = malloc(argc * sizeof(char*));
379 int i;
380 for (i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700381 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700382 if (paths[i] == NULL) {
383 int j;
384 for (j = 0; j < i; ++i) {
385 free(paths[j]);
386 }
387 free(paths);
388 return NULL;
389 }
390 }
391
392 bool recursive = (strcmp(name, "delete_recursive") == 0);
393
394 int success = 0;
395 for (i = 0; i < argc; ++i) {
396 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
397 ++success;
398 free(paths[i]);
399 }
400 free(paths);
401
402 char buffer[10];
403 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800404 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700405}
406
Doug Zongker8edb00c2009-06-11 17:21:44 -0700407
Doug Zongker512536a2010-02-17 16:11:44 -0800408Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700409 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700410 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700411 }
412 char* frac_str;
413 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700414 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700415 return NULL;
416 }
417
418 double frac = strtod(frac_str, NULL);
419 int sec = strtol(sec_str, NULL, 10);
420
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700421 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700422 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
423
Doug Zongker9931f7f2009-06-10 14:11:53 -0700424 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800425 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700426}
427
Doug Zongker512536a2010-02-17 16:11:44 -0800428Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700429 if (argc != 1) {
430 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
431 }
432 char* frac_str;
433 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
434 return NULL;
435 }
436
437 double frac = strtod(frac_str, NULL);
438
439 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
440 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
441
Doug Zongker512536a2010-02-17 16:11:44 -0800442 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700443}
444
Doug Zongker8edb00c2009-06-11 17:21:44 -0700445// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800446Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700447 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* zip_path;
452 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700453 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700454
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700455 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700456
457 // To create a consistent system image, never use the clock for timestamps.
458 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
459
460 bool success = mzExtractRecursive(za, zip_path, dest_path,
461 MZ_EXTRACT_FILES_ONLY, &timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500462 NULL, NULL, sehandle);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700463 free(zip_path);
464 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800465 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700466}
467
Doug Zongker8edb00c2009-06-11 17:21:44 -0700468
469// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800470// or
471// package_extract_file(package_path)
472// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800473// function (the char* returned is actually a FileContents*).
474Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700475 int argc, Expr* argv[]) {
Doug Zongker5f875bf2014-08-22 14:53:43 -0700476 if (argc < 1 || argc > 2) {
477 return ErrorAbort(state, "%s() expects 1 or 2 args, got %d",
Doug Zongker6aece332010-02-01 14:40:12 -0800478 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700479 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700480 bool success = false;
Doug Zongker43772d22014-06-09 14:13:19 -0700481
482 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker43772d22014-06-09 14:13:19 -0700483
Doug Zongker5f875bf2014-08-22 14:53:43 -0700484 if (argc == 2) {
485 // The two-argument version extracts to a file.
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800486
487 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700488
Doug Zongker6aece332010-02-01 14:40:12 -0800489 char* zip_path;
490 char* dest_path;
Doug Zongker5f875bf2014-08-22 14:53:43 -0700491 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800492
Doug Zongker6aece332010-02-01 14:40:12 -0800493 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
494 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700495 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800496 goto done2;
497 }
498
499 FILE* f = fopen(dest_path, "wb");
500 if (f == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700501 printf("%s: can't open %s for write: %s\n",
Doug Zongker6aece332010-02-01 14:40:12 -0800502 name, dest_path, strerror(errno));
503 goto done2;
504 }
Doug Zongker5f875bf2014-08-22 14:53:43 -0700505 success = mzExtractZipEntryToFile(za, entry, fileno(f));
Doug Zongker6aece332010-02-01 14:40:12 -0800506 fclose(f);
507
508 done2:
509 free(zip_path);
510 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800511 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800512 } else {
513 // The one-argument version returns the contents of the file
514 // as the result.
515
516 char* zip_path;
Doug Zongker512536a2010-02-17 16:11:44 -0800517 Value* v = malloc(sizeof(Value));
518 v->type = VAL_BLOB;
519 v->size = -1;
520 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800521
522 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
523
524 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
525 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
526 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700527 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800528 goto done1;
529 }
530
Doug Zongker512536a2010-02-17 16:11:44 -0800531 v->size = mzGetZipEntryUncompLen(entry);
532 v->data = malloc(v->size);
533 if (v->data == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700534 printf("%s: failed to allocate %ld bytes for %s\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800535 name, (long)v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800536 goto done1;
537 }
538
Doug Zongker512536a2010-02-17 16:11:44 -0800539 success = mzExtractZipEntryToBuffer(za, entry,
540 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800541
542 done1:
543 free(zip_path);
544 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800545 free(v->data);
546 v->data = NULL;
547 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800548 }
Doug Zongker512536a2010-02-17 16:11:44 -0800549 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700550 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700551}
552
Doug Zongkera23075f2012-08-06 16:19:09 -0700553// Create all parent directories of name, if necessary.
554static int make_parents(char* name) {
555 char* p;
556 for (p = name + (strlen(name)-1); p > name; --p) {
557 if (*p != '/') continue;
558 *p = '\0';
559 if (make_parents(name) < 0) return -1;
560 int result = mkdir(name, 0700);
Michael Rungea91ecc52014-07-21 17:40:02 -0700561 if (result == 0) printf("created [%s]\n", name);
Doug Zongkera23075f2012-08-06 16:19:09 -0700562 *p = '/';
563 if (result == 0 || errno == EEXIST) {
564 // successfully created or already existed; we're done
565 return 0;
566 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700567 printf("failed to mkdir %s: %s\n", name, strerror(errno));
Doug Zongkera23075f2012-08-06 16:19:09 -0700568 return -1;
569 }
570 }
571 return 0;
572}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700573
Doug Zongker9931f7f2009-06-10 14:11:53 -0700574// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700575// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800576Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700577 if (argc == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700578 return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700579 }
580 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700581 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700582 if (target == NULL) return NULL;
583
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700584 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700585 if (srcs == NULL) {
586 free(target);
587 return NULL;
588 }
589
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700590 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700591 int i;
592 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700593 if (unlink(srcs[i]) < 0) {
594 if (errno != ENOENT) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700595 printf("%s: failed to remove %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700596 name, srcs[i], strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700597 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700598 }
599 }
Doug Zongkera23075f2012-08-06 16:19:09 -0700600 if (make_parents(srcs[i])) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700601 printf("%s: failed to symlink %s to %s: making parents failed\n",
Doug Zongkera23075f2012-08-06 16:19:09 -0700602 name, srcs[i], target);
603 ++bad;
604 }
Doug Zongker60babf82009-09-18 15:11:24 -0700605 if (symlink(target, srcs[i]) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700606 printf("%s: failed to symlink %s to %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700607 name, srcs[i], target, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700608 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700609 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700610 free(srcs[i]);
611 }
612 free(srcs);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700613 if (bad) {
614 return ErrorAbort(state, "%s: some symlinks failed", name);
615 }
Doug Zongker512536a2010-02-17 16:11:44 -0800616 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700617}
618
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700619struct perm_parsed_args {
620 bool has_uid;
621 uid_t uid;
622 bool has_gid;
623 gid_t gid;
624 bool has_mode;
625 mode_t mode;
626 bool has_fmode;
627 mode_t fmode;
628 bool has_dmode;
629 mode_t dmode;
630 bool has_selabel;
631 char* selabel;
632 bool has_capabilities;
633 uint64_t capabilities;
634};
635
636static struct perm_parsed_args ParsePermArgs(int argc, char** args) {
637 int i;
638 struct perm_parsed_args parsed;
639 int bad = 0;
640 static int max_warnings = 20;
641
642 memset(&parsed, 0, sizeof(parsed));
643
644 for (i = 1; i < argc; i += 2) {
645 if (strcmp("uid", args[i]) == 0) {
646 int64_t uid;
647 if (sscanf(args[i+1], "%" SCNd64, &uid) == 1) {
648 parsed.uid = uid;
649 parsed.has_uid = true;
650 } else {
651 printf("ParsePermArgs: invalid UID \"%s\"\n", args[i + 1]);
652 bad++;
653 }
654 continue;
655 }
656 if (strcmp("gid", args[i]) == 0) {
657 int64_t gid;
658 if (sscanf(args[i+1], "%" SCNd64, &gid) == 1) {
659 parsed.gid = gid;
660 parsed.has_gid = true;
661 } else {
662 printf("ParsePermArgs: invalid GID \"%s\"\n", args[i + 1]);
663 bad++;
664 }
665 continue;
666 }
667 if (strcmp("mode", args[i]) == 0) {
668 int32_t mode;
669 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
670 parsed.mode = mode;
671 parsed.has_mode = true;
672 } else {
673 printf("ParsePermArgs: invalid mode \"%s\"\n", args[i + 1]);
674 bad++;
675 }
676 continue;
677 }
678 if (strcmp("dmode", args[i]) == 0) {
679 int32_t mode;
680 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
681 parsed.dmode = mode;
682 parsed.has_dmode = true;
683 } else {
684 printf("ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1]);
685 bad++;
686 }
687 continue;
688 }
689 if (strcmp("fmode", args[i]) == 0) {
690 int32_t mode;
691 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
692 parsed.fmode = mode;
693 parsed.has_fmode = true;
694 } else {
695 printf("ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1]);
696 bad++;
697 }
698 continue;
699 }
700 if (strcmp("capabilities", args[i]) == 0) {
701 int64_t capabilities;
702 if (sscanf(args[i+1], "%" SCNi64, &capabilities) == 1) {
703 parsed.capabilities = capabilities;
704 parsed.has_capabilities = true;
705 } else {
706 printf("ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1]);
707 bad++;
708 }
709 continue;
710 }
711 if (strcmp("selabel", args[i]) == 0) {
712 if (args[i+1][0] != '\0') {
713 parsed.selabel = args[i+1];
714 parsed.has_selabel = true;
715 } else {
716 printf("ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1]);
717 bad++;
718 }
719 continue;
720 }
721 if (max_warnings != 0) {
722 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i]);
723 max_warnings--;
724 if (max_warnings == 0) {
725 printf("ParsedPermArgs: suppressing further warnings\n");
726 }
727 }
728 }
729 return parsed;
730}
731
732static int ApplyParsedPerms(
733 const char* filename,
734 const struct stat *statptr,
735 struct perm_parsed_args parsed)
736{
737 int bad = 0;
738
Nick Kraleviche4612512013-09-10 15:34:19 -0700739 /* ignore symlinks */
740 if (S_ISLNK(statptr->st_mode)) {
741 return 0;
742 }
743
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700744 if (parsed.has_uid) {
745 if (chown(filename, parsed.uid, -1) < 0) {
746 printf("ApplyParsedPerms: chown of %s to %d failed: %s\n",
747 filename, parsed.uid, strerror(errno));
748 bad++;
749 }
750 }
751
752 if (parsed.has_gid) {
753 if (chown(filename, -1, parsed.gid) < 0) {
754 printf("ApplyParsedPerms: chgrp of %s to %d failed: %s\n",
755 filename, parsed.gid, strerror(errno));
756 bad++;
757 }
758 }
759
760 if (parsed.has_mode) {
761 if (chmod(filename, parsed.mode) < 0) {
762 printf("ApplyParsedPerms: chmod of %s to %d failed: %s\n",
763 filename, parsed.mode, strerror(errno));
764 bad++;
765 }
766 }
767
768 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
769 if (chmod(filename, parsed.dmode) < 0) {
770 printf("ApplyParsedPerms: chmod of %s to %d failed: %s\n",
771 filename, parsed.dmode, strerror(errno));
772 bad++;
773 }
774 }
775
776 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
777 if (chmod(filename, parsed.fmode) < 0) {
778 printf("ApplyParsedPerms: chmod of %s to %d failed: %s\n",
779 filename, parsed.fmode, strerror(errno));
780 bad++;
781 }
782 }
783
784 if (parsed.has_selabel) {
785 // TODO: Don't silently ignore ENOTSUP
786 if (lsetfilecon(filename, parsed.selabel) && (errno != ENOTSUP)) {
787 printf("ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n",
788 filename, parsed.selabel, strerror(errno));
789 bad++;
790 }
791 }
792
793 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
794 if (parsed.capabilities == 0) {
795 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
796 // Report failure unless it's ENODATA (attribute not set)
797 printf("ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n",
798 filename, parsed.capabilities, strerror(errno));
799 bad++;
800 }
801 } else {
802 struct vfs_cap_data cap_data;
803 memset(&cap_data, 0, sizeof(cap_data));
804 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
805 cap_data.data[0].permitted = (uint32_t) (parsed.capabilities & 0xffffffff);
806 cap_data.data[0].inheritable = 0;
807 cap_data.data[1].permitted = (uint32_t) (parsed.capabilities >> 32);
808 cap_data.data[1].inheritable = 0;
809 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
810 printf("ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n",
811 filename, parsed.capabilities, strerror(errno));
812 bad++;
813 }
814 }
815 }
816
817 return bad;
818}
819
820// nftw doesn't allow us to pass along context, so we need to use
821// global variables. *sigh*
822static struct perm_parsed_args recursive_parsed_args;
823
824static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr,
825 int fileflags, struct FTW *pfwt) {
826 return ApplyParsedPerms(filename, statptr, recursive_parsed_args);
827}
828
829static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
830 int i;
831 int bad = 0;
832 static int nwarnings = 0;
833 struct stat sb;
834 Value* result = NULL;
835
836 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
837
838 if ((argc % 2) != 1) {
839 return ErrorAbort(state, "%s() expects an odd number of arguments, got %d",
840 name, argc);
841 }
842
843 char** args = ReadVarArgs(state, argc, argv);
844 if (args == NULL) return NULL;
845
846 if (lstat(args[0], &sb) == -1) {
847 result = ErrorAbort(state, "%s: Error on lstat of \"%s\": %s", name, args[0], strerror(errno));
848 goto done;
849 }
850
851 struct perm_parsed_args parsed = ParsePermArgs(argc, args);
852
853 if (recursive) {
854 recursive_parsed_args = parsed;
855 bad += nftw(args[0], do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
856 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
857 } else {
858 bad += ApplyParsedPerms(args[0], &sb, parsed);
859 }
860
861done:
862 for (i = 0; i < argc; ++i) {
863 free(args[i]);
864 }
865 free(args);
866
867 if (result != NULL) {
868 return result;
869 }
870
871 if (bad > 0) {
872 return ErrorAbort(state, "%s: some changes failed", name);
873 }
874
875 return StringValue(strdup(""));
876}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700877
Doug Zongker512536a2010-02-17 16:11:44 -0800878Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700879 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700880 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700881 }
882 char* key;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700883 key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700884 if (key == NULL) return NULL;
885
886 char value[PROPERTY_VALUE_MAX];
887 property_get(key, value, "");
888 free(key);
889
Doug Zongker512536a2010-02-17 16:11:44 -0800890 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700891}
892
893
Doug Zongker47cace92009-06-18 10:11:50 -0700894// file_getprop(file, key)
895//
896// interprets 'file' as a getprop-style file (key=value pairs, one
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700897// per line. # comment lines,blank lines, lines without '=' ignored),
898// and returns the value for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800899Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700900 char* result = NULL;
901 char* buffer = NULL;
902 char* filename;
903 char* key;
904 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
905 return NULL;
906 }
907
908 struct stat st;
909 if (stat(filename, &st) < 0) {
910 ErrorAbort(state, "%s: failed to stat \"%s\": %s",
911 name, filename, strerror(errno));
912 goto done;
913 }
914
915#define MAX_FILE_GETPROP_SIZE 65536
916
917 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
918 ErrorAbort(state, "%s too large for %s (max %d)",
919 filename, name, MAX_FILE_GETPROP_SIZE);
920 goto done;
921 }
922
923 buffer = malloc(st.st_size+1);
924 if (buffer == NULL) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700925 ErrorAbort(state, "%s: failed to alloc %lld bytes", name, (long long)st.st_size+1);
Doug Zongker47cace92009-06-18 10:11:50 -0700926 goto done;
927 }
928
929 FILE* f = fopen(filename, "rb");
930 if (f == NULL) {
931 ErrorAbort(state, "%s: failed to open %s: %s",
932 name, filename, strerror(errno));
933 goto done;
934 }
935
936 if (fread(buffer, 1, st.st_size, f) != st.st_size) {
Doug Zongkera23075f2012-08-06 16:19:09 -0700937 ErrorAbort(state, "%s: failed to read %lld bytes from %s",
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700938 name, (long long)st.st_size+1, filename);
Doug Zongker47cace92009-06-18 10:11:50 -0700939 fclose(f);
940 goto done;
941 }
942 buffer[st.st_size] = '\0';
943
944 fclose(f);
945
946 char* line = strtok(buffer, "\n");
947 do {
948 // skip whitespace at start of line
949 while (*line && isspace(*line)) ++line;
950
951 // comment or blank line: skip to next line
952 if (*line == '\0' || *line == '#') continue;
953
954 char* equal = strchr(line, '=');
955 if (equal == NULL) {
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700956 continue;
Doug Zongker47cace92009-06-18 10:11:50 -0700957 }
958
959 // trim whitespace between key and '='
960 char* key_end = equal-1;
961 while (key_end > line && isspace(*key_end)) --key_end;
962 key_end[1] = '\0';
963
964 // not the key we're looking for
965 if (strcmp(key, line) != 0) continue;
966
967 // skip whitespace after the '=' to the start of the value
968 char* val_start = equal+1;
969 while(*val_start && isspace(*val_start)) ++val_start;
970
971 // trim trailing whitespace
972 char* val_end = val_start + strlen(val_start)-1;
973 while (val_end > val_start && isspace(*val_end)) --val_end;
974 val_end[1] = '\0';
975
976 result = strdup(val_start);
977 break;
978
979 } while ((line = strtok(NULL, "\n")));
980
981 if (result == NULL) result = strdup("");
982
983 done:
984 free(filename);
985 free(key);
986 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -0800987 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -0700988}
989
990
Doug Zongker8edb00c2009-06-11 17:21:44 -0700991static bool write_raw_image_cb(const unsigned char* data,
992 int data_len, void* ctx) {
993 int r = mtd_write_data((MtdWriteContext*)ctx, (const char *)data, data_len);
994 if (r == data_len) return true;
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700995 printf("%s\n", strerror(errno));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700996 return false;
997}
998
Doug Zongker179b2d92011-04-12 15:49:04 -0700999// write_raw_image(filename_or_blob, partition)
Doug Zongker512536a2010-02-17 16:11:44 -08001000Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001001 char* result = NULL;
1002
Doug Zongker179b2d92011-04-12 15:49:04 -07001003 Value* partition_value;
1004 Value* contents;
1005 if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001006 return NULL;
1007 }
1008
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001009 char* partition = NULL;
Doug Zongker179b2d92011-04-12 15:49:04 -07001010 if (partition_value->type != VAL_STRING) {
1011 ErrorAbort(state, "partition argument to %s must be string", name);
1012 goto done;
1013 }
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001014 partition = partition_value->data;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001015 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001016 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001017 goto done;
1018 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001019 if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001020 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001021 goto done;
1022 }
1023
1024 mtd_scan_partitions();
1025 const MtdPartition* mtd = mtd_find_partition_by_name(partition);
1026 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001027 printf("%s: no mtd partition named \"%s\"\n", name, partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001028 result = strdup("");
1029 goto done;
1030 }
1031
1032 MtdWriteContext* ctx = mtd_write_partition(mtd);
1033 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001034 printf("%s: can't write mtd partition \"%s\"\n",
Doug Zongker8edb00c2009-06-11 17:21:44 -07001035 name, partition);
1036 result = strdup("");
1037 goto done;
1038 }
1039
1040 bool success;
1041
Doug Zongker179b2d92011-04-12 15:49:04 -07001042 if (contents->type == VAL_STRING) {
1043 // we're given a filename as the contents
1044 char* filename = contents->data;
1045 FILE* f = fopen(filename, "rb");
1046 if (f == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001047 printf("%s: can't open %s: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -07001048 name, filename, strerror(errno));
1049 result = strdup("");
1050 goto done;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001051 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001052
1053 success = true;
1054 char* buffer = malloc(BUFSIZ);
1055 int read;
1056 while (success && (read = fread(buffer, 1, BUFSIZ, f)) > 0) {
1057 int wrote = mtd_write_data(ctx, buffer, read);
1058 success = success && (wrote == read);
1059 }
1060 free(buffer);
1061 fclose(f);
1062 } else {
1063 // we're given a blob as the contents
1064 ssize_t wrote = mtd_write_data(ctx, contents->data, contents->size);
1065 success = (wrote == contents->size);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001066 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001067 if (!success) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001068 printf("mtd_write_data to %s failed: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -07001069 partition, strerror(errno));
1070 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001071
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001072 if (mtd_erase_blocks(ctx, -1) == -1) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001073 printf("%s: error erasing blocks of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001074 }
1075 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001076 printf("%s: error closing write of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001077 }
1078
Doug Zongker179b2d92011-04-12 15:49:04 -07001079 printf("%s %s partition\n",
1080 success ? "wrote" : "failed to write", partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001081
1082 result = success ? partition : strdup("");
1083
1084done:
Doug Zongker179b2d92011-04-12 15:49:04 -07001085 if (result != partition) FreeValue(partition_value);
1086 FreeValue(contents);
Doug Zongker512536a2010-02-17 16:11:44 -08001087 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001088}
1089
Doug Zongker8edb00c2009-06-11 17:21:44 -07001090// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001091Value* ApplyPatchSpaceFn(const char* name, State* state,
1092 int argc, Expr* argv[]) {
1093 char* bytes_str;
1094 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
1095 return NULL;
1096 }
1097
1098 char* endptr;
1099 size_t bytes = strtol(bytes_str, &endptr, 10);
1100 if (bytes == 0 && endptr == bytes_str) {
1101 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n",
1102 name, bytes_str);
1103 free(bytes_str);
1104 return NULL;
1105 }
1106
1107 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
1108}
1109
Doug Zongker52b40362014-02-10 15:30:30 -08001110// apply_patch(file, size, init_sha1, tgt_sha1, patch)
1111
Doug Zongker512536a2010-02-17 16:11:44 -08001112Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001113 if (argc < 6 || (argc % 2) == 1) {
1114 return ErrorAbort(state, "%s(): expected at least 6 args and an "
1115 "even number, got %d",
1116 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001117 }
1118
Doug Zongkerc4351c72010-02-22 14:46:32 -08001119 char* source_filename;
1120 char* target_filename;
1121 char* target_sha1;
1122 char* target_size_str;
1123 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
1124 &target_sha1, &target_size_str) < 0) {
1125 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001126 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001127
Doug Zongkerc4351c72010-02-22 14:46:32 -08001128 char* endptr;
1129 size_t target_size = strtol(target_size_str, &endptr, 10);
1130 if (target_size == 0 && endptr == target_size_str) {
1131 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count",
1132 name, target_size_str);
1133 free(source_filename);
1134 free(target_filename);
1135 free(target_sha1);
1136 free(target_size_str);
1137 return NULL;
1138 }
1139
1140 int patchcount = (argc-4) / 2;
1141 Value** patches = ReadValueVarArgs(state, argc-4, argv+4);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001142
1143 int i;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001144 for (i = 0; i < patchcount; ++i) {
1145 if (patches[i*2]->type != VAL_STRING) {
1146 ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i);
1147 break;
1148 }
1149 if (patches[i*2+1]->type != VAL_BLOB) {
1150 ErrorAbort(state, "%s(): patch #%d is not blob", name, i);
1151 break;
1152 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001153 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001154 if (i != patchcount) {
1155 for (i = 0; i < patchcount*2; ++i) {
1156 FreeValue(patches[i]);
1157 }
1158 free(patches);
1159 return NULL;
1160 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001161
Doug Zongkerc4351c72010-02-22 14:46:32 -08001162 char** patch_sha_str = malloc(patchcount * sizeof(char*));
1163 for (i = 0; i < patchcount; ++i) {
1164 patch_sha_str[i] = patches[i*2]->data;
1165 patches[i*2]->data = NULL;
1166 FreeValue(patches[i*2]);
1167 patches[i] = patches[i*2+1];
Doug Zongker8edb00c2009-06-11 17:21:44 -07001168 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001169
1170 int result = applypatch(source_filename, target_filename,
1171 target_sha1, target_size,
Doug Zongkera3ccba62012-08-20 15:28:02 -07001172 patchcount, patch_sha_str, patches, NULL);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001173
1174 for (i = 0; i < patchcount; ++i) {
1175 FreeValue(patches[i]);
1176 }
1177 free(patch_sha_str);
1178 free(patches);
1179
1180 return StringValue(strdup(result == 0 ? "t" : ""));
1181}
1182
1183// apply_patch_check(file, [sha1_1, ...])
1184Value* ApplyPatchCheckFn(const char* name, State* state,
1185 int argc, Expr* argv[]) {
1186 if (argc < 1) {
1187 return ErrorAbort(state, "%s(): expected at least 1 arg, got %d",
1188 name, argc);
1189 }
1190
1191 char* filename;
1192 if (ReadArgs(state, argv, 1, &filename) < 0) {
1193 return NULL;
1194 }
1195
1196 int patchcount = argc-1;
1197 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
1198
1199 int result = applypatch_check(filename, patchcount, sha1s);
1200
1201 int i;
1202 for (i = 0; i < patchcount; ++i) {
1203 free(sha1s[i]);
1204 }
1205 free(sha1s);
1206
1207 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001208}
1209
Doug Zongker512536a2010-02-17 16:11:44 -08001210Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001211 char** args = ReadVarArgs(state, argc, argv);
1212 if (args == NULL) {
1213 return NULL;
1214 }
1215
1216 int size = 0;
1217 int i;
1218 for (i = 0; i < argc; ++i) {
1219 size += strlen(args[i]);
1220 }
1221 char* buffer = malloc(size+1);
1222 size = 0;
1223 for (i = 0; i < argc; ++i) {
1224 strcpy(buffer+size, args[i]);
1225 size += strlen(args[i]);
1226 free(args[i]);
1227 }
1228 free(args);
1229 buffer[size] = '\0';
1230
1231 char* line = strtok(buffer, "\n");
1232 while (line) {
1233 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe,
1234 "ui_print %s\n", line);
1235 line = strtok(NULL, "\n");
1236 }
1237 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "ui_print\n");
1238
Doug Zongker512536a2010-02-17 16:11:44 -08001239 return StringValue(buffer);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001240}
1241
Doug Zongkerd0181b82011-10-19 10:51:12 -07001242Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
1243 if (argc != 0) {
1244 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1245 }
1246 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
1247 return StringValue(strdup("t"));
1248}
1249
Doug Zongker512536a2010-02-17 16:11:44 -08001250Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001251 if (argc < 1) {
1252 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1253 }
1254 char** args = ReadVarArgs(state, argc, argv);
1255 if (args == NULL) {
1256 return NULL;
1257 }
1258
1259 char** args2 = malloc(sizeof(char*) * (argc+1));
1260 memcpy(args2, args, sizeof(char*) * argc);
1261 args2[argc] = NULL;
1262
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001263 printf("about to run program [%s] with %d args\n", args2[0], argc);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001264
1265 pid_t child = fork();
1266 if (child == 0) {
1267 execv(args2[0], args2);
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001268 printf("run_program: execv failed: %s\n", strerror(errno));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001269 _exit(1);
1270 }
1271 int status;
1272 waitpid(child, &status, 0);
1273 if (WIFEXITED(status)) {
1274 if (WEXITSTATUS(status) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001275 printf("run_program: child exited with status %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001276 WEXITSTATUS(status));
1277 }
1278 } else if (WIFSIGNALED(status)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001279 printf("run_program: child terminated by signal %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001280 WTERMSIG(status));
1281 }
1282
1283 int i;
1284 for (i = 0; i < argc; ++i) {
1285 free(args[i]);
1286 }
1287 free(args);
1288 free(args2);
1289
1290 char buffer[20];
1291 sprintf(buffer, "%d", status);
1292
Doug Zongker512536a2010-02-17 16:11:44 -08001293 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001294}
1295
Doug Zongker512536a2010-02-17 16:11:44 -08001296// sha1_check(data)
1297// to return the sha1 of the data (given in the format returned by
1298// read_file).
1299//
1300// sha1_check(data, sha1_hex, [sha1_hex, ...])
1301// returns the sha1 of the file if it matches any of the hex
1302// strings passed, or "" if it does not equal any of them.
1303//
1304Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1305 if (argc < 1) {
1306 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1307 }
1308
1309 Value** args = ReadValueVarArgs(state, argc, argv);
1310 if (args == NULL) {
1311 return NULL;
1312 }
1313
1314 if (args[0]->size < 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001315 return StringValue(strdup(""));
1316 }
1317 uint8_t digest[SHA_DIGEST_SIZE];
Doug Zongkerbac7fba2013-04-10 11:32:17 -07001318 SHA_hash(args[0]->data, args[0]->size, digest);
Doug Zongker512536a2010-02-17 16:11:44 -08001319 FreeValue(args[0]);
1320
1321 if (argc == 1) {
1322 return StringValue(PrintSha1(digest));
1323 }
1324
1325 int i;
1326 uint8_t* arg_digest = malloc(SHA_DIGEST_SIZE);
1327 for (i = 1; i < argc; ++i) {
1328 if (args[i]->type != VAL_STRING) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001329 printf("%s(): arg %d is not a string; skipping",
Doug Zongker512536a2010-02-17 16:11:44 -08001330 name, i);
1331 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1332 // Warn about bad args and skip them.
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001333 printf("%s(): error parsing \"%s\" as sha-1; skipping",
1334 name, args[i]->data);
Doug Zongker512536a2010-02-17 16:11:44 -08001335 } else if (memcmp(digest, arg_digest, SHA_DIGEST_SIZE) == 0) {
1336 break;
1337 }
1338 FreeValue(args[i]);
1339 }
1340 if (i >= argc) {
1341 // Didn't match any of the hex strings; return false.
1342 return StringValue(strdup(""));
1343 }
1344 // Found a match; free all the remaining arguments and return the
1345 // matched one.
1346 int j;
1347 for (j = i+1; j < argc; ++j) {
1348 FreeValue(args[j]);
1349 }
1350 return args[i];
1351}
1352
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001353// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001354// is actually a FileContents*).
1355Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1356 if (argc != 1) {
1357 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1358 }
1359 char* filename;
1360 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1361
1362 Value* v = malloc(sizeof(Value));
1363 v->type = VAL_BLOB;
1364
1365 FileContents fc;
Doug Zongkera1bc1482014-02-13 15:18:19 -08001366 if (LoadFileContents(filename, &fc) != 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001367 free(filename);
Michael Runge6eed2242013-12-13 17:13:11 -08001368 v->size = -1;
1369 v->data = NULL;
Doug Zongker512536a2010-02-17 16:11:44 -08001370 free(fc.data);
Michael Runge6eed2242013-12-13 17:13:11 -08001371 return v;
Doug Zongker512536a2010-02-17 16:11:44 -08001372 }
1373
1374 v->size = fc.size;
1375 v->data = (char*)fc.data;
1376
1377 free(filename);
1378 return v;
1379}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001380
Doug Zongkerc87bab12013-11-25 13:53:25 -08001381// Immediately reboot the device. Recovery is not finished normally,
1382// so if you reboot into recovery it will re-start applying the
1383// current package (because nothing has cleared the copy of the
1384// arguments stored in the BCB).
1385//
1386// The argument is the partition name passed to the android reboot
1387// property. It can be "recovery" to boot from the recovery
1388// partition, or "" (empty string) to boot from the regular boot
1389// partition.
1390Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) {
1391 if (argc != 2) {
1392 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1393 }
1394
1395 char* filename;
1396 char* property;
1397 if (ReadArgs(state, argv, 2, &filename, &property) < 0) return NULL;
1398
1399 char buffer[80];
1400
1401 // zero out the 'command' field of the bootloader message.
1402 memset(buffer, 0, sizeof(((struct bootloader_message*)0)->command));
1403 FILE* f = fopen(filename, "r+b");
1404 fseek(f, offsetof(struct bootloader_message, command), SEEK_SET);
1405 fwrite(buffer, sizeof(((struct bootloader_message*)0)->command), 1, f);
1406 fclose(f);
1407 free(filename);
1408
1409 strcpy(buffer, "reboot,");
1410 if (property != NULL) {
1411 strncat(buffer, property, sizeof(buffer)-10);
1412 }
1413
1414 property_set(ANDROID_RB_PROPERTY, buffer);
1415
1416 sleep(5);
1417 free(property);
1418 ErrorAbort(state, "%s() failed to reboot", name);
1419 return NULL;
1420}
1421
1422// Store a string value somewhere that future invocations of recovery
1423// can access it. This value is called the "stage" and can be used to
1424// drive packages that need to do reboots in the middle of
1425// installation and keep track of where they are in the multi-stage
1426// install.
1427//
1428// The first argument is the block device for the misc partition
1429// ("/misc" in the fstab), which is where this value is stored. The
1430// second argument is the string to store; it should not exceed 31
1431// bytes.
1432Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
1433 if (argc != 2) {
1434 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1435 }
1436
1437 char* filename;
1438 char* stagestr;
1439 if (ReadArgs(state, argv, 2, &filename, &stagestr) < 0) return NULL;
1440
1441 // Store this value in the misc partition, immediately after the
1442 // bootloader message that the main recovery uses to save its
1443 // arguments in case of the device restarting midway through
1444 // package installation.
1445 FILE* f = fopen(filename, "r+b");
1446 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1447 int to_write = strlen(stagestr)+1;
1448 int max_size = sizeof(((struct bootloader_message*)0)->stage);
1449 if (to_write > max_size) {
1450 to_write = max_size;
1451 stagestr[max_size-1] = 0;
1452 }
1453 fwrite(stagestr, to_write, 1, f);
1454 fclose(f);
1455
1456 free(stagestr);
1457 return StringValue(filename);
1458}
1459
1460// Return the value most recently saved with SetStageFn. The argument
1461// is the block device for the misc partition.
1462Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001463 if (argc != 1) {
Doug Zongkerc87bab12013-11-25 13:53:25 -08001464 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1465 }
1466
1467 char* filename;
1468 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1469
1470 char buffer[sizeof(((struct bootloader_message*)0)->stage)];
1471 FILE* f = fopen(filename, "rb");
1472 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1473 fread(buffer, sizeof(buffer), 1, f);
1474 fclose(f);
1475 buffer[sizeof(buffer)-1] = '\0';
1476
1477 return StringValue(strdup(buffer));
1478}
1479
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001480Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) {
1481 if (argc != 2) {
1482 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1483 }
1484
1485 char* filename;
1486 char* len_str;
1487 if (ReadArgs(state, argv, 2, &filename, &len_str) < 0) return NULL;
1488
1489 size_t len = strtoull(len_str, NULL, 0);
1490 int fd = open(filename, O_WRONLY, 0644);
1491 int success = wipe_block_device(fd, len);
1492
1493 free(filename);
1494 free(len_str);
1495
1496 close(fd);
1497
1498 return StringValue(strdup(success ? "t" : ""));
1499}
1500
Doug Zongkerc704e062014-05-23 08:40:35 -07001501Value* EnableRebootFn(const char* name, State* state, int argc, Expr* argv[]) {
1502 if (argc != 0) {
1503 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1504 }
1505 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
1506 fprintf(ui->cmd_pipe, "enable_reboot\n");
1507 return StringValue(strdup("t"));
1508}
1509
Doug Zongker9931f7f2009-06-10 14:11:53 -07001510void RegisterInstallFunctions() {
1511 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001512 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001513 RegisterFunction("unmount", UnmountFn);
1514 RegisterFunction("format", FormatFn);
1515 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001516 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001517 RegisterFunction("delete", DeleteFn);
1518 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001519 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1520 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001521 RegisterFunction("symlink", SymlinkFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001522
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001523 // Usage:
1524 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1525 // Example:
1526 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1527 RegisterFunction("set_metadata", SetMetadataFn);
1528
1529 // Usage:
1530 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1531 // Example:
1532 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1533 RegisterFunction("set_metadata_recursive", SetMetadataFn);
1534
Doug Zongker8edb00c2009-06-11 17:21:44 -07001535 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001536 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001537 RegisterFunction("write_raw_image", WriteRawImageFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001538
1539 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001540 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1541 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001542
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001543 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001544
Doug Zongker512536a2010-02-17 16:11:44 -08001545 RegisterFunction("read_file", ReadFileFn);
1546 RegisterFunction("sha1_check", Sha1CheckFn);
Michael Rungece7ca712013-11-06 17:42:20 -08001547 RegisterFunction("rename", RenameFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001548
Doug Zongkerd0181b82011-10-19 10:51:12 -07001549 RegisterFunction("wipe_cache", WipeCacheFn);
1550
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001551 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001552
1553 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001554
1555 RegisterFunction("reboot_now", RebootNowFn);
1556 RegisterFunction("get_stage", GetStageFn);
1557 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -07001558
1559 RegisterFunction("enable_reboot", EnableRebootFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001560}