blob: 42f228972344f4b62bdd9d7b330b816b024eac17 [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 Zongker52b40362014-02-10 15:30:30 -080048#include "syspatch.h"
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -080049#include "install.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070050
Doug Zongker3d177d02010-07-01 09:18:44 -070051#ifdef USE_EXT4
52#include "make_ext4fs.h"
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -080053#include "wipe.h"
Doug Zongker3d177d02010-07-01 09:18:44 -070054#endif
55
Doug Zongker52b40362014-02-10 15:30:30 -080056// Take a sha-1 digest and return it as a newly-allocated hex string.
Doug Zongker0d32f252014-02-13 15:07:56 -080057static char* PrintSha1(const uint8_t* digest) {
Doug Zongker52b40362014-02-10 15:30:30 -080058 char* buffer = malloc(SHA_DIGEST_SIZE*2 + 1);
59 int i;
60 const char* alphabet = "0123456789abcdef";
61 for (i = 0; i < SHA_DIGEST_SIZE; ++i) {
62 buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf];
63 buffer[i*2+1] = alphabet[digest[i] & 0xf];
64 }
65 buffer[i*2] = '\0';
66 return buffer;
67}
68
Doug Zongker3d177d02010-07-01 09:18:44 -070069// mount(fs_type, partition_type, location, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -070070//
Doug Zongker3d177d02010-07-01 09:18:44 -070071// fs_type="yaffs2" partition_type="MTD" location=partition
72// fs_type="ext4" partition_type="EMMC" location=device
Doug Zongker512536a2010-02-17 16:11:44 -080073Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070074 char* result = NULL;
Doug Zongker3d177d02010-07-01 09:18:44 -070075 if (argc != 4) {
76 return ErrorAbort(state, "%s() expects 4 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -070077 }
Doug Zongker3d177d02010-07-01 09:18:44 -070078 char* fs_type;
79 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -070080 char* location;
81 char* mount_point;
Doug Zongker3d177d02010-07-01 09:18:44 -070082 if (ReadArgs(state, argv, 4, &fs_type, &partition_type,
83 &location, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070084 return NULL;
85 }
86
Doug Zongker3d177d02010-07-01 09:18:44 -070087 if (strlen(fs_type) == 0) {
88 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
89 goto done;
90 }
91 if (strlen(partition_type) == 0) {
92 ErrorAbort(state, "partition_type argument to %s() can't be empty",
93 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -070094 goto done;
95 }
96 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070097 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -070098 goto done;
99 }
100 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700101 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700102 goto done;
103 }
104
Stephen Smalley779701d2012-02-09 14:13:23 -0500105 char *secontext = NULL;
106
107 if (sehandle) {
108 selabel_lookup(sehandle, &secontext, mount_point, 0755);
109 setfscreatecon(secontext);
110 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500111
Doug Zongker9931f7f2009-06-10 14:11:53 -0700112 mkdir(mount_point, 0755);
113
Stephen Smalley779701d2012-02-09 14:13:23 -0500114 if (secontext) {
115 freecon(secontext);
116 setfscreatecon(NULL);
117 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500118
Doug Zongker3d177d02010-07-01 09:18:44 -0700119 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700120 mtd_scan_partitions();
121 const MtdPartition* mtd;
122 mtd = mtd_find_partition_by_name(location);
123 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700124 printf("%s: no mtd partition named \"%s\"",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700125 name, location);
126 result = strdup("");
127 goto done;
128 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700129 if (mtd_mount_partition(mtd, mount_point, fs_type, 0 /* rw */) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700130 printf("mtd mount of %s failed: %s\n",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700131 location, strerror(errno));
132 result = strdup("");
133 goto done;
134 }
135 result = mount_point;
136 } else {
Doug Zongker3d177d02010-07-01 09:18:44 -0700137 if (mount(location, mount_point, fs_type,
Doug Zongker9931f7f2009-06-10 14:11:53 -0700138 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "") < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700139 printf("%s: failed to mount %s at %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700140 name, location, mount_point, strerror(errno));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700141 result = strdup("");
142 } else {
143 result = mount_point;
144 }
145 }
146
147done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700148 free(fs_type);
149 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700150 free(location);
151 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800152 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700153}
154
Doug Zongker8edb00c2009-06-11 17:21:44 -0700155
156// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800157Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700158 char* result = NULL;
159 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700160 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700161 }
162 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700163 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700164 return NULL;
165 }
166 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700167 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700168 goto done;
169 }
170
171 scan_mounted_volumes();
172 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
173 if (vol == NULL) {
174 result = strdup("");
175 } else {
176 result = mount_point;
177 }
178
179done:
180 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800181 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700182}
183
184
Doug Zongker512536a2010-02-17 16:11:44 -0800185Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700186 char* result = NULL;
187 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700188 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700189 }
190 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700191 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700192 return NULL;
193 }
194 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700195 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700196 goto done;
197 }
198
199 scan_mounted_volumes();
200 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
201 if (vol == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700202 printf("unmount of %s failed; no such volume\n", mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700203 result = strdup("");
204 } else {
205 unmount_mounted_volume(vol);
206 result = mount_point;
207 }
208
209done:
210 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800211 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700212}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700213
214
Stephen Smalley779701d2012-02-09 14:13:23 -0500215// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700216//
Stephen Smalley779701d2012-02-09 14:13:23 -0500217// fs_type="yaffs2" partition_type="MTD" location=partition fs_size=<bytes> mount_point=<location>
218// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800219// if fs_size == 0, then make_ext4fs uses the entire partition.
220// if fs_size > 0, that is the size to use
221// if fs_size < 0, then reserve that many bytes at the end of the partition
Doug Zongker512536a2010-02-17 16:11:44 -0800222Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700223 char* result = NULL;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400224 if (argc != 5) {
225 return ErrorAbort(state, "%s() expects 5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700226 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700227 char* fs_type;
228 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700229 char* location;
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800230 char* fs_size;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400231 char* mount_point;
Stephen Smalley779701d2012-02-09 14:13:23 -0500232
Stephen Smalley779701d2012-02-09 14:13:23 -0500233 if (ReadArgs(state, argv, 5, &fs_type, &partition_type, &location, &fs_size, &mount_point) < 0) {
234 return NULL;
235 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700236
Doug Zongker3d177d02010-07-01 09:18:44 -0700237 if (strlen(fs_type) == 0) {
238 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
239 goto done;
240 }
241 if (strlen(partition_type) == 0) {
242 ErrorAbort(state, "partition_type argument to %s() can't be empty",
243 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700244 goto done;
245 }
246 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700247 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700248 goto done;
249 }
250
Stephen Smalley516e4e22012-04-03 13:35:11 -0400251 if (strlen(mount_point) == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500252 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
253 goto done;
254 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500255
Doug Zongker3d177d02010-07-01 09:18:44 -0700256 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700257 mtd_scan_partitions();
258 const MtdPartition* mtd = mtd_find_partition_by_name(location);
259 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700260 printf("%s: no mtd partition named \"%s\"",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700261 name, location);
262 result = strdup("");
263 goto done;
264 }
265 MtdWriteContext* ctx = mtd_write_partition(mtd);
266 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700267 printf("%s: can't write \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700268 result = strdup("");
269 goto done;
270 }
271 if (mtd_erase_blocks(ctx, -1) == -1) {
272 mtd_write_close(ctx);
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700273 printf("%s: failed to erase \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700274 result = strdup("");
275 goto done;
276 }
277 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700278 printf("%s: failed to close \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700279 result = strdup("");
280 goto done;
281 }
282 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700283#ifdef USE_EXT4
284 } else if (strcmp(fs_type, "ext4") == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500285 int status = make_ext4fs(location, atoll(fs_size), mount_point, sehandle);
Doug Zongker3d177d02010-07-01 09:18:44 -0700286 if (status != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700287 printf("%s: make_ext4fs failed (%d) on %s",
Doug Zongker3d177d02010-07-01 09:18:44 -0700288 name, status, location);
289 result = strdup("");
290 goto done;
291 }
292 result = location;
293#endif
Doug Zongker9931f7f2009-06-10 14:11:53 -0700294 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700295 printf("%s: unsupported fs_type \"%s\" partition_type \"%s\"",
Doug Zongker3d177d02010-07-01 09:18:44 -0700296 name, fs_type, partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700297 }
298
299done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700300 free(fs_type);
301 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700302 if (result != location) free(location);
Doug Zongker512536a2010-02-17 16:11:44 -0800303 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700304}
305
Michael Rungece7ca712013-11-06 17:42:20 -0800306Value* RenameFn(const char* name, State* state, int argc, Expr* argv[]) {
307 char* result = NULL;
308 if (argc != 2) {
309 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
310 }
311
312 char* src_name;
313 char* dst_name;
314
315 if (ReadArgs(state, argv, 2, &src_name, &dst_name) < 0) {
316 return NULL;
317 }
318 if (strlen(src_name) == 0) {
319 ErrorAbort(state, "src_name argument to %s() can't be empty", name);
320 goto done;
321 }
322 if (strlen(dst_name) == 0) {
323 ErrorAbort(state, "dst_name argument to %s() can't be empty",
324 name);
325 goto done;
326 }
327
328 if (rename(src_name, dst_name) != 0) {
329 ErrorAbort(state, "Rename of %s() to %s() failed, error %s()",
330 src_name, dst_name, strerror(errno));
331 } else {
332 result = dst_name;
333 }
334
335done:
336 free(src_name);
337 if (result != dst_name) free(dst_name);
338 return StringValue(result);
339}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700340
Doug Zongker512536a2010-02-17 16:11:44 -0800341Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700342 char** paths = malloc(argc * sizeof(char*));
343 int i;
344 for (i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700345 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700346 if (paths[i] == NULL) {
347 int j;
348 for (j = 0; j < i; ++i) {
349 free(paths[j]);
350 }
351 free(paths);
352 return NULL;
353 }
354 }
355
356 bool recursive = (strcmp(name, "delete_recursive") == 0);
357
358 int success = 0;
359 for (i = 0; i < argc; ++i) {
360 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
361 ++success;
362 free(paths[i]);
363 }
364 free(paths);
365
366 char buffer[10];
367 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800368 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700369}
370
Doug Zongker8edb00c2009-06-11 17:21:44 -0700371
Doug Zongker512536a2010-02-17 16:11:44 -0800372Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700373 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700374 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700375 }
376 char* frac_str;
377 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700378 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700379 return NULL;
380 }
381
382 double frac = strtod(frac_str, NULL);
383 int sec = strtol(sec_str, NULL, 10);
384
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700385 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700386 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
387
Doug Zongker9931f7f2009-06-10 14:11:53 -0700388 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800389 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700390}
391
Doug Zongker512536a2010-02-17 16:11:44 -0800392Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700393 if (argc != 1) {
394 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
395 }
396 char* frac_str;
397 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
398 return NULL;
399 }
400
401 double frac = strtod(frac_str, NULL);
402
403 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
404 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
405
Doug Zongker512536a2010-02-17 16:11:44 -0800406 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700407}
408
Doug Zongker8edb00c2009-06-11 17:21:44 -0700409// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800410Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700411 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700412 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700413 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700414 }
415 char* zip_path;
416 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700417 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700418
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700419 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700420
421 // To create a consistent system image, never use the clock for timestamps.
422 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
423
424 bool success = mzExtractRecursive(za, zip_path, dest_path,
425 MZ_EXTRACT_FILES_ONLY, &timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500426 NULL, NULL, sehandle);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700427 free(zip_path);
428 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800429 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700430}
431
Doug Zongker8edb00c2009-06-11 17:21:44 -0700432
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800433DontCareMap* ReadDontCareMapFromZip(ZipArchive* za, const char* path) {
434 const char* name = "ReadDontCareMapFromZip";
435
436 const ZipEntry* entry = mzFindZipEntry(za, path);
437 if (entry == NULL) {
438 printf("%s: no %s in package\n", name, path);
439 return NULL;
440 }
441
442 size_t map_size = mzGetZipEntryUncompLen(entry);
443 char* map_data = malloc(map_size);
444 if (map_data == NULL) {
445 printf("%s: failed to allocate %zu bytes for %s\n",
446 name, map_size, path);
447 return NULL;
448 }
449
450 if (!mzExtractZipEntryToBuffer(za, entry, (unsigned char*) map_data)) {
451 printf("%s: failed to read %s\n", name, path);
452 return NULL;
453 }
454
455 char* p = map_data;
456 DontCareMap* map = (DontCareMap*) malloc(sizeof(DontCareMap));
457
458 map->block_size = strtoul(p, &p, 0);
459 if (map->block_size != 4096) {
460 printf("%s: unexpected block size %zu\n", name, map->block_size);
461 return NULL;
462 }
463
464 map->region_count = strtoul(p, &p, 0);
465 map->regions = (int*) malloc(map->region_count * sizeof(int));
Doug Zongker43772d22014-06-09 14:13:19 -0700466 map->total_blocks = 0;
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800467
468 int i;
469 for (i = 0; i < map->region_count; ++i) {
470 map->regions[i] = strtoul(p, &p, 0);
Doug Zongker43772d22014-06-09 14:13:19 -0700471 map->total_blocks += map->regions[i];
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800472 }
473
474 return map;
475}
476
Doug Zongker43772d22014-06-09 14:13:19 -0700477static FILE* mapwrite_cmd_pipe;
478
479static void progress_cb(long done, long total) {
480 if (total > 0) {
481 double frac = (double)done / total;
482 fprintf(mapwrite_cmd_pipe, "set_progress %f\n", frac);
483 fflush(mapwrite_cmd_pipe);
484 }
485}
486
487
488
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800489bool MapWriter(const unsigned char* data, int dataLen, void* cookie) {
Doug Zongker43772d22014-06-09 14:13:19 -0700490 return write_with_map(data, dataLen, (MapState*) cookie, progress_cb) == dataLen;
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800491}
492
493// package_extract_file(package_path, destination_path, map_path)
494// or
Doug Zongker8edb00c2009-06-11 17:21:44 -0700495// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800496// or
497// package_extract_file(package_path)
498// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800499// function (the char* returned is actually a FileContents*).
500Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700501 int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800502 if (argc < 1 || argc > 3) {
503 return ErrorAbort(state, "%s() expects 1 or 2 or 3 args, got %d",
Doug Zongker6aece332010-02-01 14:40:12 -0800504 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700505 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700506 bool success = false;
Doug Zongker43772d22014-06-09 14:13:19 -0700507
508 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
509 mapwrite_cmd_pipe = ui->cmd_pipe;
510
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800511 if (argc >= 2) {
512 // The two-argument version extracts to a file; the three-arg
513 // version extracts to a file, skipping over regions in a
514 // don't care map.
515
516 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700517
Doug Zongker6aece332010-02-01 14:40:12 -0800518 char* zip_path;
519 char* dest_path;
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800520 char* map_path = NULL;
521 DontCareMap* map = NULL;
522 if (argc == 2) {
523 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
524 } else {
525 if (ReadArgs(state, argv, 3, &zip_path, &dest_path, &map_path) < 0) return NULL;
526 map = ReadDontCareMapFromZip(za, map_path);
527 if (map == NULL) goto done2;
528 }
Doug Zongker6aece332010-02-01 14:40:12 -0800529
Doug Zongker6aece332010-02-01 14:40:12 -0800530 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
531 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700532 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800533 goto done2;
534 }
535
536 FILE* f = fopen(dest_path, "wb");
537 if (f == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700538 printf("%s: can't open %s for write: %s\n",
Doug Zongker6aece332010-02-01 14:40:12 -0800539 name, dest_path, strerror(errno));
540 goto done2;
541 }
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800542 if (map) {
543 MapState state;
544 state.map = map;
545 state.cr = 0;
546 state.so_far = 0;
547 state.f = f;
548 success = mzProcessZipEntryContents(za, entry, MapWriter, &state);
549 } else {
550 success = mzExtractZipEntryToFile(za, entry, fileno(f));
551 }
Doug Zongker6aece332010-02-01 14:40:12 -0800552 fclose(f);
553
554 done2:
555 free(zip_path);
556 free(dest_path);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800557 free(map_path);
558 if (map) {
559 free(map->regions);
560 free(map);
561 }
Doug Zongker512536a2010-02-17 16:11:44 -0800562 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800563 } else {
564 // The one-argument version returns the contents of the file
565 // as the result.
566
567 char* zip_path;
Doug Zongker512536a2010-02-17 16:11:44 -0800568 Value* v = malloc(sizeof(Value));
569 v->type = VAL_BLOB;
570 v->size = -1;
571 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800572
573 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
574
575 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
576 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
577 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700578 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800579 goto done1;
580 }
581
Doug Zongker512536a2010-02-17 16:11:44 -0800582 v->size = mzGetZipEntryUncompLen(entry);
583 v->data = malloc(v->size);
584 if (v->data == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700585 printf("%s: failed to allocate %ld bytes for %s\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800586 name, (long)v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800587 goto done1;
588 }
589
Doug Zongker512536a2010-02-17 16:11:44 -0800590 success = mzExtractZipEntryToBuffer(za, entry,
591 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800592
593 done1:
594 free(zip_path);
595 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800596 free(v->data);
597 v->data = NULL;
598 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800599 }
Doug Zongker512536a2010-02-17 16:11:44 -0800600 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700601 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700602}
603
Doug Zongkera23075f2012-08-06 16:19:09 -0700604// Create all parent directories of name, if necessary.
605static int make_parents(char* name) {
606 char* p;
607 for (p = name + (strlen(name)-1); p > name; --p) {
608 if (*p != '/') continue;
609 *p = '\0';
610 if (make_parents(name) < 0) return -1;
611 int result = mkdir(name, 0700);
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700612 if (result == 0) printf("symlink(): created [%s]\n", name);
Doug Zongkera23075f2012-08-06 16:19:09 -0700613 *p = '/';
614 if (result == 0 || errno == EEXIST) {
615 // successfully created or already existed; we're done
616 return 0;
617 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700618 printf("failed to mkdir %s: %s\n", name, strerror(errno));
Doug Zongkera23075f2012-08-06 16:19:09 -0700619 return -1;
620 }
621 }
622 return 0;
623}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700624
Doug Zongker9931f7f2009-06-10 14:11:53 -0700625// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700626// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800627Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700628 if (argc == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700629 return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700630 }
631 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700632 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700633 if (target == NULL) return NULL;
634
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700635 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700636 if (srcs == NULL) {
637 free(target);
638 return NULL;
639 }
640
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700641 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700642 int i;
643 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700644 if (unlink(srcs[i]) < 0) {
645 if (errno != ENOENT) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700646 printf("%s: failed to remove %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700647 name, srcs[i], strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700648 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700649 }
650 }
Doug Zongkera23075f2012-08-06 16:19:09 -0700651 if (make_parents(srcs[i])) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700652 printf("%s: failed to symlink %s to %s: making parents failed\n",
Doug Zongkera23075f2012-08-06 16:19:09 -0700653 name, srcs[i], target);
654 ++bad;
655 }
Doug Zongker60babf82009-09-18 15:11:24 -0700656 if (symlink(target, srcs[i]) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700657 printf("%s: failed to symlink %s to %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700658 name, srcs[i], target, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700659 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700660 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700661 free(srcs[i]);
662 }
663 free(srcs);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700664 if (bad) {
665 return ErrorAbort(state, "%s: some symlinks failed", name);
666 }
Doug Zongker512536a2010-02-17 16:11:44 -0800667 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700668}
669
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700670struct perm_parsed_args {
671 bool has_uid;
672 uid_t uid;
673 bool has_gid;
674 gid_t gid;
675 bool has_mode;
676 mode_t mode;
677 bool has_fmode;
678 mode_t fmode;
679 bool has_dmode;
680 mode_t dmode;
681 bool has_selabel;
682 char* selabel;
683 bool has_capabilities;
684 uint64_t capabilities;
685};
686
687static struct perm_parsed_args ParsePermArgs(int argc, char** args) {
688 int i;
689 struct perm_parsed_args parsed;
690 int bad = 0;
691 static int max_warnings = 20;
692
693 memset(&parsed, 0, sizeof(parsed));
694
695 for (i = 1; i < argc; i += 2) {
696 if (strcmp("uid", args[i]) == 0) {
697 int64_t uid;
698 if (sscanf(args[i+1], "%" SCNd64, &uid) == 1) {
699 parsed.uid = uid;
700 parsed.has_uid = true;
701 } else {
702 printf("ParsePermArgs: invalid UID \"%s\"\n", args[i + 1]);
703 bad++;
704 }
705 continue;
706 }
707 if (strcmp("gid", args[i]) == 0) {
708 int64_t gid;
709 if (sscanf(args[i+1], "%" SCNd64, &gid) == 1) {
710 parsed.gid = gid;
711 parsed.has_gid = true;
712 } else {
713 printf("ParsePermArgs: invalid GID \"%s\"\n", args[i + 1]);
714 bad++;
715 }
716 continue;
717 }
718 if (strcmp("mode", args[i]) == 0) {
719 int32_t mode;
720 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
721 parsed.mode = mode;
722 parsed.has_mode = true;
723 } else {
724 printf("ParsePermArgs: invalid mode \"%s\"\n", args[i + 1]);
725 bad++;
726 }
727 continue;
728 }
729 if (strcmp("dmode", args[i]) == 0) {
730 int32_t mode;
731 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
732 parsed.dmode = mode;
733 parsed.has_dmode = true;
734 } else {
735 printf("ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1]);
736 bad++;
737 }
738 continue;
739 }
740 if (strcmp("fmode", args[i]) == 0) {
741 int32_t mode;
742 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
743 parsed.fmode = mode;
744 parsed.has_fmode = true;
745 } else {
746 printf("ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1]);
747 bad++;
748 }
749 continue;
750 }
751 if (strcmp("capabilities", args[i]) == 0) {
752 int64_t capabilities;
753 if (sscanf(args[i+1], "%" SCNi64, &capabilities) == 1) {
754 parsed.capabilities = capabilities;
755 parsed.has_capabilities = true;
756 } else {
757 printf("ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1]);
758 bad++;
759 }
760 continue;
761 }
762 if (strcmp("selabel", args[i]) == 0) {
763 if (args[i+1][0] != '\0') {
764 parsed.selabel = args[i+1];
765 parsed.has_selabel = true;
766 } else {
767 printf("ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1]);
768 bad++;
769 }
770 continue;
771 }
772 if (max_warnings != 0) {
773 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i]);
774 max_warnings--;
775 if (max_warnings == 0) {
776 printf("ParsedPermArgs: suppressing further warnings\n");
777 }
778 }
779 }
780 return parsed;
781}
782
783static int ApplyParsedPerms(
784 const char* filename,
785 const struct stat *statptr,
786 struct perm_parsed_args parsed)
787{
788 int bad = 0;
789
Nick Kraleviche4612512013-09-10 15:34:19 -0700790 /* ignore symlinks */
791 if (S_ISLNK(statptr->st_mode)) {
792 return 0;
793 }
794
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700795 if (parsed.has_uid) {
796 if (chown(filename, parsed.uid, -1) < 0) {
797 printf("ApplyParsedPerms: chown of %s to %d failed: %s\n",
798 filename, parsed.uid, strerror(errno));
799 bad++;
800 }
801 }
802
803 if (parsed.has_gid) {
804 if (chown(filename, -1, parsed.gid) < 0) {
805 printf("ApplyParsedPerms: chgrp of %s to %d failed: %s\n",
806 filename, parsed.gid, strerror(errno));
807 bad++;
808 }
809 }
810
811 if (parsed.has_mode) {
812 if (chmod(filename, parsed.mode) < 0) {
813 printf("ApplyParsedPerms: chmod of %s to %d failed: %s\n",
814 filename, parsed.mode, strerror(errno));
815 bad++;
816 }
817 }
818
819 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
820 if (chmod(filename, parsed.dmode) < 0) {
821 printf("ApplyParsedPerms: chmod of %s to %d failed: %s\n",
822 filename, parsed.dmode, strerror(errno));
823 bad++;
824 }
825 }
826
827 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
828 if (chmod(filename, parsed.fmode) < 0) {
829 printf("ApplyParsedPerms: chmod of %s to %d failed: %s\n",
830 filename, parsed.fmode, strerror(errno));
831 bad++;
832 }
833 }
834
835 if (parsed.has_selabel) {
836 // TODO: Don't silently ignore ENOTSUP
837 if (lsetfilecon(filename, parsed.selabel) && (errno != ENOTSUP)) {
838 printf("ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n",
839 filename, parsed.selabel, strerror(errno));
840 bad++;
841 }
842 }
843
844 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
845 if (parsed.capabilities == 0) {
846 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
847 // Report failure unless it's ENODATA (attribute not set)
848 printf("ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n",
849 filename, parsed.capabilities, strerror(errno));
850 bad++;
851 }
852 } else {
853 struct vfs_cap_data cap_data;
854 memset(&cap_data, 0, sizeof(cap_data));
855 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
856 cap_data.data[0].permitted = (uint32_t) (parsed.capabilities & 0xffffffff);
857 cap_data.data[0].inheritable = 0;
858 cap_data.data[1].permitted = (uint32_t) (parsed.capabilities >> 32);
859 cap_data.data[1].inheritable = 0;
860 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
861 printf("ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n",
862 filename, parsed.capabilities, strerror(errno));
863 bad++;
864 }
865 }
866 }
867
868 return bad;
869}
870
871// nftw doesn't allow us to pass along context, so we need to use
872// global variables. *sigh*
873static struct perm_parsed_args recursive_parsed_args;
874
875static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr,
876 int fileflags, struct FTW *pfwt) {
877 return ApplyParsedPerms(filename, statptr, recursive_parsed_args);
878}
879
880static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
881 int i;
882 int bad = 0;
883 static int nwarnings = 0;
884 struct stat sb;
885 Value* result = NULL;
886
887 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
888
889 if ((argc % 2) != 1) {
890 return ErrorAbort(state, "%s() expects an odd number of arguments, got %d",
891 name, argc);
892 }
893
894 char** args = ReadVarArgs(state, argc, argv);
895 if (args == NULL) return NULL;
896
897 if (lstat(args[0], &sb) == -1) {
898 result = ErrorAbort(state, "%s: Error on lstat of \"%s\": %s", name, args[0], strerror(errno));
899 goto done;
900 }
901
902 struct perm_parsed_args parsed = ParsePermArgs(argc, args);
903
904 if (recursive) {
905 recursive_parsed_args = parsed;
906 bad += nftw(args[0], do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
907 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
908 } else {
909 bad += ApplyParsedPerms(args[0], &sb, parsed);
910 }
911
912done:
913 for (i = 0; i < argc; ++i) {
914 free(args[i]);
915 }
916 free(args);
917
918 if (result != NULL) {
919 return result;
920 }
921
922 if (bad > 0) {
923 return ErrorAbort(state, "%s: some changes failed", name);
924 }
925
926 return StringValue(strdup(""));
927}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700928
Doug Zongker512536a2010-02-17 16:11:44 -0800929Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700930 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700931 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700932 }
933 char* key;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700934 key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700935 if (key == NULL) return NULL;
936
937 char value[PROPERTY_VALUE_MAX];
938 property_get(key, value, "");
939 free(key);
940
Doug Zongker512536a2010-02-17 16:11:44 -0800941 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700942}
943
944
Doug Zongker47cace92009-06-18 10:11:50 -0700945// file_getprop(file, key)
946//
947// interprets 'file' as a getprop-style file (key=value pairs, one
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700948// per line. # comment lines,blank lines, lines without '=' ignored),
949// and returns the value for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800950Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700951 char* result = NULL;
952 char* buffer = NULL;
953 char* filename;
954 char* key;
955 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
956 return NULL;
957 }
958
959 struct stat st;
960 if (stat(filename, &st) < 0) {
961 ErrorAbort(state, "%s: failed to stat \"%s\": %s",
962 name, filename, strerror(errno));
963 goto done;
964 }
965
966#define MAX_FILE_GETPROP_SIZE 65536
967
968 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
969 ErrorAbort(state, "%s too large for %s (max %d)",
970 filename, name, MAX_FILE_GETPROP_SIZE);
971 goto done;
972 }
973
974 buffer = malloc(st.st_size+1);
975 if (buffer == NULL) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700976 ErrorAbort(state, "%s: failed to alloc %lld bytes", name, (long long)st.st_size+1);
Doug Zongker47cace92009-06-18 10:11:50 -0700977 goto done;
978 }
979
980 FILE* f = fopen(filename, "rb");
981 if (f == NULL) {
982 ErrorAbort(state, "%s: failed to open %s: %s",
983 name, filename, strerror(errno));
984 goto done;
985 }
986
987 if (fread(buffer, 1, st.st_size, f) != st.st_size) {
Doug Zongkera23075f2012-08-06 16:19:09 -0700988 ErrorAbort(state, "%s: failed to read %lld bytes from %s",
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700989 name, (long long)st.st_size+1, filename);
Doug Zongker47cace92009-06-18 10:11:50 -0700990 fclose(f);
991 goto done;
992 }
993 buffer[st.st_size] = '\0';
994
995 fclose(f);
996
997 char* line = strtok(buffer, "\n");
998 do {
999 // skip whitespace at start of line
1000 while (*line && isspace(*line)) ++line;
1001
1002 // comment or blank line: skip to next line
1003 if (*line == '\0' || *line == '#') continue;
1004
1005 char* equal = strchr(line, '=');
1006 if (equal == NULL) {
Michael Rungeaa1a31e2014-04-25 18:47:18 -07001007 continue;
Doug Zongker47cace92009-06-18 10:11:50 -07001008 }
1009
1010 // trim whitespace between key and '='
1011 char* key_end = equal-1;
1012 while (key_end > line && isspace(*key_end)) --key_end;
1013 key_end[1] = '\0';
1014
1015 // not the key we're looking for
1016 if (strcmp(key, line) != 0) continue;
1017
1018 // skip whitespace after the '=' to the start of the value
1019 char* val_start = equal+1;
1020 while(*val_start && isspace(*val_start)) ++val_start;
1021
1022 // trim trailing whitespace
1023 char* val_end = val_start + strlen(val_start)-1;
1024 while (val_end > val_start && isspace(*val_end)) --val_end;
1025 val_end[1] = '\0';
1026
1027 result = strdup(val_start);
1028 break;
1029
1030 } while ((line = strtok(NULL, "\n")));
1031
1032 if (result == NULL) result = strdup("");
1033
1034 done:
1035 free(filename);
1036 free(key);
1037 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -08001038 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -07001039}
1040
1041
Doug Zongker8edb00c2009-06-11 17:21:44 -07001042static bool write_raw_image_cb(const unsigned char* data,
1043 int data_len, void* ctx) {
1044 int r = mtd_write_data((MtdWriteContext*)ctx, (const char *)data, data_len);
1045 if (r == data_len) return true;
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001046 printf("%s\n", strerror(errno));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001047 return false;
1048}
1049
Doug Zongker179b2d92011-04-12 15:49:04 -07001050// write_raw_image(filename_or_blob, partition)
Doug Zongker512536a2010-02-17 16:11:44 -08001051Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001052 char* result = NULL;
1053
Doug Zongker179b2d92011-04-12 15:49:04 -07001054 Value* partition_value;
1055 Value* contents;
1056 if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001057 return NULL;
1058 }
1059
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001060 char* partition = NULL;
Doug Zongker179b2d92011-04-12 15:49:04 -07001061 if (partition_value->type != VAL_STRING) {
1062 ErrorAbort(state, "partition argument to %s must be string", name);
1063 goto done;
1064 }
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001065 partition = partition_value->data;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001066 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001067 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001068 goto done;
1069 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001070 if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001071 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001072 goto done;
1073 }
1074
1075 mtd_scan_partitions();
1076 const MtdPartition* mtd = mtd_find_partition_by_name(partition);
1077 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001078 printf("%s: no mtd partition named \"%s\"\n", name, partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001079 result = strdup("");
1080 goto done;
1081 }
1082
1083 MtdWriteContext* ctx = mtd_write_partition(mtd);
1084 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001085 printf("%s: can't write mtd partition \"%s\"\n",
Doug Zongker8edb00c2009-06-11 17:21:44 -07001086 name, partition);
1087 result = strdup("");
1088 goto done;
1089 }
1090
1091 bool success;
1092
Doug Zongker179b2d92011-04-12 15:49:04 -07001093 if (contents->type == VAL_STRING) {
1094 // we're given a filename as the contents
1095 char* filename = contents->data;
1096 FILE* f = fopen(filename, "rb");
1097 if (f == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001098 printf("%s: can't open %s: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -07001099 name, filename, strerror(errno));
1100 result = strdup("");
1101 goto done;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001102 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001103
1104 success = true;
1105 char* buffer = malloc(BUFSIZ);
1106 int read;
1107 while (success && (read = fread(buffer, 1, BUFSIZ, f)) > 0) {
1108 int wrote = mtd_write_data(ctx, buffer, read);
1109 success = success && (wrote == read);
1110 }
1111 free(buffer);
1112 fclose(f);
1113 } else {
1114 // we're given a blob as the contents
1115 ssize_t wrote = mtd_write_data(ctx, contents->data, contents->size);
1116 success = (wrote == contents->size);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001117 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001118 if (!success) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001119 printf("mtd_write_data to %s failed: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -07001120 partition, strerror(errno));
1121 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001122
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001123 if (mtd_erase_blocks(ctx, -1) == -1) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001124 printf("%s: error erasing blocks of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001125 }
1126 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001127 printf("%s: error closing write of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001128 }
1129
Doug Zongker179b2d92011-04-12 15:49:04 -07001130 printf("%s %s partition\n",
1131 success ? "wrote" : "failed to write", partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001132
1133 result = success ? partition : strdup("");
1134
1135done:
Doug Zongker179b2d92011-04-12 15:49:04 -07001136 if (result != partition) FreeValue(partition_value);
1137 FreeValue(contents);
Doug Zongker512536a2010-02-17 16:11:44 -08001138 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001139}
1140
Doug Zongker8edb00c2009-06-11 17:21:44 -07001141// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001142Value* ApplyPatchSpaceFn(const char* name, State* state,
1143 int argc, Expr* argv[]) {
1144 char* bytes_str;
1145 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
1146 return NULL;
1147 }
1148
1149 char* endptr;
1150 size_t bytes = strtol(bytes_str, &endptr, 10);
1151 if (bytes == 0 && endptr == bytes_str) {
1152 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n",
1153 name, bytes_str);
1154 free(bytes_str);
1155 return NULL;
1156 }
1157
1158 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
1159}
1160
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001161bool CheckMappedFileSha1(FILE* f, DontCareMap* map, uint8_t* intended_digest) {
1162 MapState state;
1163
1164 state.f = f;
1165 state.so_far = 0;
1166 state.cr = 0;
1167 state.map = map;
1168
1169 SHA_CTX ctx;
1170 SHA_init(&ctx);
1171
1172 unsigned char buffer[32173];
1173 size_t bytes_read;
1174
1175 while ((bytes_read = read_with_map(buffer, sizeof(buffer), &state)) > 0) {
1176 SHA_update(&ctx, buffer, bytes_read);
1177 }
1178 const uint8_t* digest = SHA_final(&ctx);
1179
1180 return memcmp(digest, intended_digest, SHA_DIGEST_SIZE) == 0;
1181}
1182
1183
1184// syspatch(file, tgt_mapfile, tgt_sha1, init_mapfile, init_sha1, patch)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001185
Doug Zongker52b40362014-02-10 15:30:30 -08001186Value* SysPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001187 if (argc != 6) {
1188 return ErrorAbort(state, "%s(): expected 6 args, got %d", name, argc);
Doug Zongker52b40362014-02-10 15:30:30 -08001189 }
1190
1191 char* filename;
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001192 char* target_mapfilename;
Doug Zongker52b40362014-02-10 15:30:30 -08001193 char* target_sha1;
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001194 char* init_mapfilename;
Doug Zongker52b40362014-02-10 15:30:30 -08001195 char* init_sha1;
1196 char* patch_filename;
1197 uint8_t target_digest[SHA_DIGEST_SIZE];
1198 uint8_t init_digest[SHA_DIGEST_SIZE];
1199
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001200 if (ReadArgs(state, argv, 6, &filename,
1201 &target_mapfilename, &target_sha1,
1202 &init_mapfilename, &init_sha1, &patch_filename) < 0) {
Doug Zongker52b40362014-02-10 15:30:30 -08001203 return NULL;
1204 }
1205
Doug Zongker43772d22014-06-09 14:13:19 -07001206 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
1207 mapwrite_cmd_pipe = ui->cmd_pipe;
1208
Doug Zongker52b40362014-02-10 15:30:30 -08001209 if (ParseSha1(target_sha1, target_digest) != 0) {
1210 printf("%s(): failed to parse '%s' as target SHA-1", name, target_sha1);
1211 memset(target_digest, 0, SHA_DIGEST_SIZE);
1212 }
1213 if (ParseSha1(init_sha1, init_digest) != 0) {
1214 printf("%s(): failed to parse '%s' as init SHA-1", name, init_sha1);
1215 memset(init_digest, 0, SHA_DIGEST_SIZE);
1216 }
1217
Doug Zongker52b40362014-02-10 15:30:30 -08001218 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001219 FILE* src = fopen(filename, "r");
Doug Zongker52b40362014-02-10 15:30:30 -08001220
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001221 DontCareMap* init_map = ReadDontCareMapFromZip(za, init_mapfilename);
1222 if (init_map == NULL) return ErrorAbort(state, "%s(): failed to read init map\n", name);
1223 DontCareMap* target_map = ReadDontCareMapFromZip(za, target_mapfilename);
1224 if (target_map == NULL) return ErrorAbort(state, "%s(): failed to read target map\n", name);
1225
1226 if (CheckMappedFileSha1(src, init_map, init_digest)) {
1227 // If the partition contents match the init_digest, then we need to apply the patch.
1228
1229 rewind(src);
1230
1231 const ZipEntry* entry = mzFindZipEntry(za, patch_filename);
1232 if (entry == NULL) {
1233 return ErrorAbort(state, "%s(): no %s in package\n", name, patch_filename);
1234 }
1235
1236 unsigned char* patch_data;
1237 size_t patch_len;
1238 if (!mzGetStoredEntry(za, entry, &patch_data, &patch_len)) {
1239 return ErrorAbort(state, "%s(): failed to get %s entry\n", name, patch_filename);
1240 }
1241
1242 FILE* tgt = fopen(filename, "r+");
1243
Doug Zongker43772d22014-06-09 14:13:19 -07001244 int ret = syspatch(src, init_map, patch_data, patch_len, tgt, target_map, progress_cb);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001245
1246 fclose(src);
1247 fclose(tgt);
1248
1249 if (ret != 0) {
1250 return ErrorAbort(state, "%s(): patching failed\n", name);
1251 }
1252 } else {
1253 rewind(src);
1254 if (CheckMappedFileSha1(src, target_map, target_digest)) {
1255 // If the partition contents match the target already, we
1256 // don't need to do anything.
1257 printf("%s: output is already target\n", name);
1258 } else {
1259 return ErrorAbort(state, "%s(): %s in unknown state\n", name, filename);
1260 }
Doug Zongker52b40362014-02-10 15:30:30 -08001261 }
1262
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001263 done:
Doug Zongker52b40362014-02-10 15:30:30 -08001264 free(target_sha1);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001265 free(target_mapfilename);
Doug Zongker52b40362014-02-10 15:30:30 -08001266 free(init_sha1);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001267 free(init_mapfilename);
Doug Zongker52b40362014-02-10 15:30:30 -08001268 free(patch_filename);
1269 return StringValue(filename);
1270
1271}
1272
1273// apply_patch(file, size, init_sha1, tgt_sha1, patch)
1274
Doug Zongker512536a2010-02-17 16:11:44 -08001275Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001276 if (argc < 6 || (argc % 2) == 1) {
1277 return ErrorAbort(state, "%s(): expected at least 6 args and an "
1278 "even number, got %d",
1279 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001280 }
1281
Doug Zongkerc4351c72010-02-22 14:46:32 -08001282 char* source_filename;
1283 char* target_filename;
1284 char* target_sha1;
1285 char* target_size_str;
1286 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
1287 &target_sha1, &target_size_str) < 0) {
1288 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001289 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001290
Doug Zongkerc4351c72010-02-22 14:46:32 -08001291 char* endptr;
1292 size_t target_size = strtol(target_size_str, &endptr, 10);
1293 if (target_size == 0 && endptr == target_size_str) {
1294 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count",
1295 name, target_size_str);
1296 free(source_filename);
1297 free(target_filename);
1298 free(target_sha1);
1299 free(target_size_str);
1300 return NULL;
1301 }
1302
1303 int patchcount = (argc-4) / 2;
1304 Value** patches = ReadValueVarArgs(state, argc-4, argv+4);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001305
1306 int i;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001307 for (i = 0; i < patchcount; ++i) {
1308 if (patches[i*2]->type != VAL_STRING) {
1309 ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i);
1310 break;
1311 }
1312 if (patches[i*2+1]->type != VAL_BLOB) {
1313 ErrorAbort(state, "%s(): patch #%d is not blob", name, i);
1314 break;
1315 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001316 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001317 if (i != patchcount) {
1318 for (i = 0; i < patchcount*2; ++i) {
1319 FreeValue(patches[i]);
1320 }
1321 free(patches);
1322 return NULL;
1323 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001324
Doug Zongkerc4351c72010-02-22 14:46:32 -08001325 char** patch_sha_str = malloc(patchcount * sizeof(char*));
1326 for (i = 0; i < patchcount; ++i) {
1327 patch_sha_str[i] = patches[i*2]->data;
1328 patches[i*2]->data = NULL;
1329 FreeValue(patches[i*2]);
1330 patches[i] = patches[i*2+1];
Doug Zongker8edb00c2009-06-11 17:21:44 -07001331 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001332
1333 int result = applypatch(source_filename, target_filename,
1334 target_sha1, target_size,
Doug Zongkera3ccba62012-08-20 15:28:02 -07001335 patchcount, patch_sha_str, patches, NULL);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001336
1337 for (i = 0; i < patchcount; ++i) {
1338 FreeValue(patches[i]);
1339 }
1340 free(patch_sha_str);
1341 free(patches);
1342
1343 return StringValue(strdup(result == 0 ? "t" : ""));
1344}
1345
1346// apply_patch_check(file, [sha1_1, ...])
1347Value* ApplyPatchCheckFn(const char* name, State* state,
1348 int argc, Expr* argv[]) {
1349 if (argc < 1) {
1350 return ErrorAbort(state, "%s(): expected at least 1 arg, got %d",
1351 name, argc);
1352 }
1353
1354 char* filename;
1355 if (ReadArgs(state, argv, 1, &filename) < 0) {
1356 return NULL;
1357 }
1358
1359 int patchcount = argc-1;
1360 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
1361
1362 int result = applypatch_check(filename, patchcount, sha1s);
1363
1364 int i;
1365 for (i = 0; i < patchcount; ++i) {
1366 free(sha1s[i]);
1367 }
1368 free(sha1s);
1369
1370 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001371}
1372
Doug Zongker512536a2010-02-17 16:11:44 -08001373Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001374 char** args = ReadVarArgs(state, argc, argv);
1375 if (args == NULL) {
1376 return NULL;
1377 }
1378
1379 int size = 0;
1380 int i;
1381 for (i = 0; i < argc; ++i) {
1382 size += strlen(args[i]);
1383 }
1384 char* buffer = malloc(size+1);
1385 size = 0;
1386 for (i = 0; i < argc; ++i) {
1387 strcpy(buffer+size, args[i]);
1388 size += strlen(args[i]);
1389 free(args[i]);
1390 }
1391 free(args);
1392 buffer[size] = '\0';
1393
1394 char* line = strtok(buffer, "\n");
1395 while (line) {
1396 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe,
1397 "ui_print %s\n", line);
1398 line = strtok(NULL, "\n");
1399 }
1400 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "ui_print\n");
1401
Doug Zongker512536a2010-02-17 16:11:44 -08001402 return StringValue(buffer);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001403}
1404
Doug Zongkerd0181b82011-10-19 10:51:12 -07001405Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
1406 if (argc != 0) {
1407 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1408 }
1409 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
1410 return StringValue(strdup("t"));
1411}
1412
Doug Zongker512536a2010-02-17 16:11:44 -08001413Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001414 if (argc < 1) {
1415 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1416 }
1417 char** args = ReadVarArgs(state, argc, argv);
1418 if (args == NULL) {
1419 return NULL;
1420 }
1421
1422 char** args2 = malloc(sizeof(char*) * (argc+1));
1423 memcpy(args2, args, sizeof(char*) * argc);
1424 args2[argc] = NULL;
1425
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001426 printf("about to run program [%s] with %d args\n", args2[0], argc);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001427
1428 pid_t child = fork();
1429 if (child == 0) {
1430 execv(args2[0], args2);
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001431 printf("run_program: execv failed: %s\n", strerror(errno));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001432 _exit(1);
1433 }
1434 int status;
1435 waitpid(child, &status, 0);
1436 if (WIFEXITED(status)) {
1437 if (WEXITSTATUS(status) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001438 printf("run_program: child exited with status %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001439 WEXITSTATUS(status));
1440 }
1441 } else if (WIFSIGNALED(status)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001442 printf("run_program: child terminated by signal %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001443 WTERMSIG(status));
1444 }
1445
1446 int i;
1447 for (i = 0; i < argc; ++i) {
1448 free(args[i]);
1449 }
1450 free(args);
1451 free(args2);
1452
1453 char buffer[20];
1454 sprintf(buffer, "%d", status);
1455
Doug Zongker512536a2010-02-17 16:11:44 -08001456 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001457}
1458
Doug Zongker512536a2010-02-17 16:11:44 -08001459// sha1_check(data)
1460// to return the sha1 of the data (given in the format returned by
1461// read_file).
1462//
1463// sha1_check(data, sha1_hex, [sha1_hex, ...])
1464// returns the sha1 of the file if it matches any of the hex
1465// strings passed, or "" if it does not equal any of them.
1466//
1467Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1468 if (argc < 1) {
1469 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1470 }
1471
1472 Value** args = ReadValueVarArgs(state, argc, argv);
1473 if (args == NULL) {
1474 return NULL;
1475 }
1476
1477 if (args[0]->size < 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001478 return StringValue(strdup(""));
1479 }
1480 uint8_t digest[SHA_DIGEST_SIZE];
Doug Zongkerbac7fba2013-04-10 11:32:17 -07001481 SHA_hash(args[0]->data, args[0]->size, digest);
Doug Zongker512536a2010-02-17 16:11:44 -08001482 FreeValue(args[0]);
1483
1484 if (argc == 1) {
1485 return StringValue(PrintSha1(digest));
1486 }
1487
1488 int i;
1489 uint8_t* arg_digest = malloc(SHA_DIGEST_SIZE);
1490 for (i = 1; i < argc; ++i) {
1491 if (args[i]->type != VAL_STRING) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001492 printf("%s(): arg %d is not a string; skipping",
Doug Zongker512536a2010-02-17 16:11:44 -08001493 name, i);
1494 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1495 // Warn about bad args and skip them.
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001496 printf("%s(): error parsing \"%s\" as sha-1; skipping",
1497 name, args[i]->data);
Doug Zongker512536a2010-02-17 16:11:44 -08001498 } else if (memcmp(digest, arg_digest, SHA_DIGEST_SIZE) == 0) {
1499 break;
1500 }
1501 FreeValue(args[i]);
1502 }
1503 if (i >= argc) {
1504 // Didn't match any of the hex strings; return false.
1505 return StringValue(strdup(""));
1506 }
1507 // Found a match; free all the remaining arguments and return the
1508 // matched one.
1509 int j;
1510 for (j = i+1; j < argc; ++j) {
1511 FreeValue(args[j]);
1512 }
1513 return args[i];
1514}
1515
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001516// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001517// is actually a FileContents*).
1518Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1519 if (argc != 1) {
1520 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1521 }
1522 char* filename;
1523 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1524
1525 Value* v = malloc(sizeof(Value));
1526 v->type = VAL_BLOB;
1527
1528 FileContents fc;
Doug Zongkera1bc1482014-02-13 15:18:19 -08001529 if (LoadFileContents(filename, &fc) != 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001530 free(filename);
Michael Runge6eed2242013-12-13 17:13:11 -08001531 v->size = -1;
1532 v->data = NULL;
Doug Zongker512536a2010-02-17 16:11:44 -08001533 free(fc.data);
Michael Runge6eed2242013-12-13 17:13:11 -08001534 return v;
Doug Zongker512536a2010-02-17 16:11:44 -08001535 }
1536
1537 v->size = fc.size;
1538 v->data = (char*)fc.data;
1539
1540 free(filename);
1541 return v;
1542}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001543
Doug Zongkerc87bab12013-11-25 13:53:25 -08001544// Immediately reboot the device. Recovery is not finished normally,
1545// so if you reboot into recovery it will re-start applying the
1546// current package (because nothing has cleared the copy of the
1547// arguments stored in the BCB).
1548//
1549// The argument is the partition name passed to the android reboot
1550// property. It can be "recovery" to boot from the recovery
1551// partition, or "" (empty string) to boot from the regular boot
1552// partition.
1553Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) {
1554 if (argc != 2) {
1555 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1556 }
1557
1558 char* filename;
1559 char* property;
1560 if (ReadArgs(state, argv, 2, &filename, &property) < 0) return NULL;
1561
1562 char buffer[80];
1563
1564 // zero out the 'command' field of the bootloader message.
1565 memset(buffer, 0, sizeof(((struct bootloader_message*)0)->command));
1566 FILE* f = fopen(filename, "r+b");
1567 fseek(f, offsetof(struct bootloader_message, command), SEEK_SET);
1568 fwrite(buffer, sizeof(((struct bootloader_message*)0)->command), 1, f);
1569 fclose(f);
1570 free(filename);
1571
1572 strcpy(buffer, "reboot,");
1573 if (property != NULL) {
1574 strncat(buffer, property, sizeof(buffer)-10);
1575 }
1576
1577 property_set(ANDROID_RB_PROPERTY, buffer);
1578
1579 sleep(5);
1580 free(property);
1581 ErrorAbort(state, "%s() failed to reboot", name);
1582 return NULL;
1583}
1584
1585// Store a string value somewhere that future invocations of recovery
1586// can access it. This value is called the "stage" and can be used to
1587// drive packages that need to do reboots in the middle of
1588// installation and keep track of where they are in the multi-stage
1589// install.
1590//
1591// The first argument is the block device for the misc partition
1592// ("/misc" in the fstab), which is where this value is stored. The
1593// second argument is the string to store; it should not exceed 31
1594// bytes.
1595Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
1596 if (argc != 2) {
1597 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1598 }
1599
1600 char* filename;
1601 char* stagestr;
1602 if (ReadArgs(state, argv, 2, &filename, &stagestr) < 0) return NULL;
1603
1604 // Store this value in the misc partition, immediately after the
1605 // bootloader message that the main recovery uses to save its
1606 // arguments in case of the device restarting midway through
1607 // package installation.
1608 FILE* f = fopen(filename, "r+b");
1609 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1610 int to_write = strlen(stagestr)+1;
1611 int max_size = sizeof(((struct bootloader_message*)0)->stage);
1612 if (to_write > max_size) {
1613 to_write = max_size;
1614 stagestr[max_size-1] = 0;
1615 }
1616 fwrite(stagestr, to_write, 1, f);
1617 fclose(f);
1618
1619 free(stagestr);
1620 return StringValue(filename);
1621}
1622
1623// Return the value most recently saved with SetStageFn. The argument
1624// is the block device for the misc partition.
1625Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001626 if (argc != 1) {
Doug Zongkerc87bab12013-11-25 13:53:25 -08001627 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1628 }
1629
1630 char* filename;
1631 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1632
1633 char buffer[sizeof(((struct bootloader_message*)0)->stage)];
1634 FILE* f = fopen(filename, "rb");
1635 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1636 fread(buffer, sizeof(buffer), 1, f);
1637 fclose(f);
1638 buffer[sizeof(buffer)-1] = '\0';
1639
1640 return StringValue(strdup(buffer));
1641}
1642
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001643Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) {
1644 if (argc != 2) {
1645 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1646 }
1647
1648 char* filename;
1649 char* len_str;
1650 if (ReadArgs(state, argv, 2, &filename, &len_str) < 0) return NULL;
1651
1652 size_t len = strtoull(len_str, NULL, 0);
1653 int fd = open(filename, O_WRONLY, 0644);
1654 int success = wipe_block_device(fd, len);
1655
1656 free(filename);
1657 free(len_str);
1658
1659 close(fd);
1660
1661 return StringValue(strdup(success ? "t" : ""));
1662}
1663
Doug Zongkerc704e062014-05-23 08:40:35 -07001664Value* EnableRebootFn(const char* name, State* state, int argc, Expr* argv[]) {
1665 if (argc != 0) {
1666 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1667 }
1668 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
1669 fprintf(ui->cmd_pipe, "enable_reboot\n");
1670 return StringValue(strdup("t"));
1671}
1672
Doug Zongker9931f7f2009-06-10 14:11:53 -07001673void RegisterInstallFunctions() {
1674 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001675 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001676 RegisterFunction("unmount", UnmountFn);
1677 RegisterFunction("format", FormatFn);
1678 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001679 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001680 RegisterFunction("delete", DeleteFn);
1681 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001682 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1683 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001684 RegisterFunction("symlink", SymlinkFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001685
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001686 // Usage:
1687 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1688 // Example:
1689 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1690 RegisterFunction("set_metadata", SetMetadataFn);
1691
1692 // Usage:
1693 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1694 // Example:
1695 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1696 RegisterFunction("set_metadata_recursive", SetMetadataFn);
1697
Doug Zongker8edb00c2009-06-11 17:21:44 -07001698 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001699 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001700 RegisterFunction("write_raw_image", WriteRawImageFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001701
1702 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001703 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1704 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001705
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001706 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001707 RegisterFunction("syspatch", SysPatchFn);
1708
Doug Zongker512536a2010-02-17 16:11:44 -08001709 RegisterFunction("read_file", ReadFileFn);
1710 RegisterFunction("sha1_check", Sha1CheckFn);
Michael Rungece7ca712013-11-06 17:42:20 -08001711 RegisterFunction("rename", RenameFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001712
Doug Zongkerd0181b82011-10-19 10:51:12 -07001713 RegisterFunction("wipe_cache", WipeCacheFn);
1714
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001715 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001716
1717 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001718
1719 RegisterFunction("reboot_now", RebootNowFn);
1720 RegisterFunction("get_stage", GetStageFn);
1721 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -07001722
1723 RegisterFunction("enable_reboot", EnableRebootFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001724}