blob: 53f5e48cbc2de96448d5d62897514af7ffba6948 [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));
466
467 int i;
468 for (i = 0; i < map->region_count; ++i) {
469 map->regions[i] = strtoul(p, &p, 0);
470 }
471
472 return map;
473}
474
475bool MapWriter(const unsigned char* data, int dataLen, void* cookie) {
476 return write_with_map(data, dataLen, (MapState*) cookie) == dataLen;
477}
478
479// package_extract_file(package_path, destination_path, map_path)
480// or
Doug Zongker8edb00c2009-06-11 17:21:44 -0700481// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800482// or
483// package_extract_file(package_path)
484// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800485// function (the char* returned is actually a FileContents*).
486Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700487 int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800488 if (argc < 1 || argc > 3) {
489 return ErrorAbort(state, "%s() expects 1 or 2 or 3 args, got %d",
Doug Zongker6aece332010-02-01 14:40:12 -0800490 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700491 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700492 bool success = false;
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800493 if (argc >= 2) {
494 // The two-argument version extracts to a file; the three-arg
495 // version extracts to a file, skipping over regions in a
496 // don't care map.
497
498 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700499
Doug Zongker6aece332010-02-01 14:40:12 -0800500 char* zip_path;
501 char* dest_path;
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800502 char* map_path = NULL;
503 DontCareMap* map = NULL;
504 if (argc == 2) {
505 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
506 } else {
507 if (ReadArgs(state, argv, 3, &zip_path, &dest_path, &map_path) < 0) return NULL;
508 map = ReadDontCareMapFromZip(za, map_path);
509 if (map == NULL) goto done2;
510 }
Doug Zongker6aece332010-02-01 14:40:12 -0800511
Doug Zongker6aece332010-02-01 14:40:12 -0800512 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
513 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700514 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800515 goto done2;
516 }
517
518 FILE* f = fopen(dest_path, "wb");
519 if (f == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700520 printf("%s: can't open %s for write: %s\n",
Doug Zongker6aece332010-02-01 14:40:12 -0800521 name, dest_path, strerror(errno));
522 goto done2;
523 }
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800524 if (map) {
525 MapState state;
526 state.map = map;
527 state.cr = 0;
528 state.so_far = 0;
529 state.f = f;
530 success = mzProcessZipEntryContents(za, entry, MapWriter, &state);
531 } else {
532 success = mzExtractZipEntryToFile(za, entry, fileno(f));
533 }
Doug Zongker6aece332010-02-01 14:40:12 -0800534 fclose(f);
535
536 done2:
537 free(zip_path);
538 free(dest_path);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800539 free(map_path);
540 if (map) {
541 free(map->regions);
542 free(map);
543 }
Doug Zongker512536a2010-02-17 16:11:44 -0800544 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800545 } else {
546 // The one-argument version returns the contents of the file
547 // as the result.
548
549 char* zip_path;
Doug Zongker512536a2010-02-17 16:11:44 -0800550 Value* v = malloc(sizeof(Value));
551 v->type = VAL_BLOB;
552 v->size = -1;
553 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800554
555 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
556
557 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
558 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
559 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700560 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800561 goto done1;
562 }
563
Doug Zongker512536a2010-02-17 16:11:44 -0800564 v->size = mzGetZipEntryUncompLen(entry);
565 v->data = malloc(v->size);
566 if (v->data == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700567 printf("%s: failed to allocate %ld bytes for %s\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800568 name, (long)v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800569 goto done1;
570 }
571
Doug Zongker512536a2010-02-17 16:11:44 -0800572 success = mzExtractZipEntryToBuffer(za, entry,
573 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800574
575 done1:
576 free(zip_path);
577 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800578 free(v->data);
579 v->data = NULL;
580 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800581 }
Doug Zongker512536a2010-02-17 16:11:44 -0800582 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700583 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700584}
585
Doug Zongkera23075f2012-08-06 16:19:09 -0700586// Create all parent directories of name, if necessary.
587static int make_parents(char* name) {
588 char* p;
589 for (p = name + (strlen(name)-1); p > name; --p) {
590 if (*p != '/') continue;
591 *p = '\0';
592 if (make_parents(name) < 0) return -1;
593 int result = mkdir(name, 0700);
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700594 if (result == 0) printf("symlink(): created [%s]\n", name);
Doug Zongkera23075f2012-08-06 16:19:09 -0700595 *p = '/';
596 if (result == 0 || errno == EEXIST) {
597 // successfully created or already existed; we're done
598 return 0;
599 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700600 printf("failed to mkdir %s: %s\n", name, strerror(errno));
Doug Zongkera23075f2012-08-06 16:19:09 -0700601 return -1;
602 }
603 }
604 return 0;
605}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700606
Doug Zongker9931f7f2009-06-10 14:11:53 -0700607// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700608// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800609Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700610 if (argc == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700611 return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700612 }
613 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700614 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700615 if (target == NULL) return NULL;
616
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700617 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700618 if (srcs == NULL) {
619 free(target);
620 return NULL;
621 }
622
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700623 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700624 int i;
625 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700626 if (unlink(srcs[i]) < 0) {
627 if (errno != ENOENT) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700628 printf("%s: failed to remove %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700629 name, srcs[i], strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700630 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700631 }
632 }
Doug Zongkera23075f2012-08-06 16:19:09 -0700633 if (make_parents(srcs[i])) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700634 printf("%s: failed to symlink %s to %s: making parents failed\n",
Doug Zongkera23075f2012-08-06 16:19:09 -0700635 name, srcs[i], target);
636 ++bad;
637 }
Doug Zongker60babf82009-09-18 15:11:24 -0700638 if (symlink(target, srcs[i]) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700639 printf("%s: failed to symlink %s to %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700640 name, srcs[i], target, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700641 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700642 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700643 free(srcs[i]);
644 }
645 free(srcs);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700646 if (bad) {
647 return ErrorAbort(state, "%s: some symlinks failed", name);
648 }
Doug Zongker512536a2010-02-17 16:11:44 -0800649 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700650}
651
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700652struct perm_parsed_args {
653 bool has_uid;
654 uid_t uid;
655 bool has_gid;
656 gid_t gid;
657 bool has_mode;
658 mode_t mode;
659 bool has_fmode;
660 mode_t fmode;
661 bool has_dmode;
662 mode_t dmode;
663 bool has_selabel;
664 char* selabel;
665 bool has_capabilities;
666 uint64_t capabilities;
667};
668
669static struct perm_parsed_args ParsePermArgs(int argc, char** args) {
670 int i;
671 struct perm_parsed_args parsed;
672 int bad = 0;
673 static int max_warnings = 20;
674
675 memset(&parsed, 0, sizeof(parsed));
676
677 for (i = 1; i < argc; i += 2) {
678 if (strcmp("uid", args[i]) == 0) {
679 int64_t uid;
680 if (sscanf(args[i+1], "%" SCNd64, &uid) == 1) {
681 parsed.uid = uid;
682 parsed.has_uid = true;
683 } else {
684 printf("ParsePermArgs: invalid UID \"%s\"\n", args[i + 1]);
685 bad++;
686 }
687 continue;
688 }
689 if (strcmp("gid", args[i]) == 0) {
690 int64_t gid;
691 if (sscanf(args[i+1], "%" SCNd64, &gid) == 1) {
692 parsed.gid = gid;
693 parsed.has_gid = true;
694 } else {
695 printf("ParsePermArgs: invalid GID \"%s\"\n", args[i + 1]);
696 bad++;
697 }
698 continue;
699 }
700 if (strcmp("mode", args[i]) == 0) {
701 int32_t mode;
702 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
703 parsed.mode = mode;
704 parsed.has_mode = true;
705 } else {
706 printf("ParsePermArgs: invalid mode \"%s\"\n", args[i + 1]);
707 bad++;
708 }
709 continue;
710 }
711 if (strcmp("dmode", args[i]) == 0) {
712 int32_t mode;
713 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
714 parsed.dmode = mode;
715 parsed.has_dmode = true;
716 } else {
717 printf("ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1]);
718 bad++;
719 }
720 continue;
721 }
722 if (strcmp("fmode", args[i]) == 0) {
723 int32_t mode;
724 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
725 parsed.fmode = mode;
726 parsed.has_fmode = true;
727 } else {
728 printf("ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1]);
729 bad++;
730 }
731 continue;
732 }
733 if (strcmp("capabilities", args[i]) == 0) {
734 int64_t capabilities;
735 if (sscanf(args[i+1], "%" SCNi64, &capabilities) == 1) {
736 parsed.capabilities = capabilities;
737 parsed.has_capabilities = true;
738 } else {
739 printf("ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1]);
740 bad++;
741 }
742 continue;
743 }
744 if (strcmp("selabel", args[i]) == 0) {
745 if (args[i+1][0] != '\0') {
746 parsed.selabel = args[i+1];
747 parsed.has_selabel = true;
748 } else {
749 printf("ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1]);
750 bad++;
751 }
752 continue;
753 }
754 if (max_warnings != 0) {
755 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i]);
756 max_warnings--;
757 if (max_warnings == 0) {
758 printf("ParsedPermArgs: suppressing further warnings\n");
759 }
760 }
761 }
762 return parsed;
763}
764
765static int ApplyParsedPerms(
766 const char* filename,
767 const struct stat *statptr,
768 struct perm_parsed_args parsed)
769{
770 int bad = 0;
771
Nick Kraleviche4612512013-09-10 15:34:19 -0700772 /* ignore symlinks */
773 if (S_ISLNK(statptr->st_mode)) {
774 return 0;
775 }
776
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700777 if (parsed.has_uid) {
778 if (chown(filename, parsed.uid, -1) < 0) {
779 printf("ApplyParsedPerms: chown of %s to %d failed: %s\n",
780 filename, parsed.uid, strerror(errno));
781 bad++;
782 }
783 }
784
785 if (parsed.has_gid) {
786 if (chown(filename, -1, parsed.gid) < 0) {
787 printf("ApplyParsedPerms: chgrp of %s to %d failed: %s\n",
788 filename, parsed.gid, strerror(errno));
789 bad++;
790 }
791 }
792
793 if (parsed.has_mode) {
794 if (chmod(filename, parsed.mode) < 0) {
795 printf("ApplyParsedPerms: chmod of %s to %d failed: %s\n",
796 filename, parsed.mode, strerror(errno));
797 bad++;
798 }
799 }
800
801 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
802 if (chmod(filename, parsed.dmode) < 0) {
803 printf("ApplyParsedPerms: chmod of %s to %d failed: %s\n",
804 filename, parsed.dmode, strerror(errno));
805 bad++;
806 }
807 }
808
809 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
810 if (chmod(filename, parsed.fmode) < 0) {
811 printf("ApplyParsedPerms: chmod of %s to %d failed: %s\n",
812 filename, parsed.fmode, strerror(errno));
813 bad++;
814 }
815 }
816
817 if (parsed.has_selabel) {
818 // TODO: Don't silently ignore ENOTSUP
819 if (lsetfilecon(filename, parsed.selabel) && (errno != ENOTSUP)) {
820 printf("ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n",
821 filename, parsed.selabel, strerror(errno));
822 bad++;
823 }
824 }
825
826 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
827 if (parsed.capabilities == 0) {
828 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
829 // Report failure unless it's ENODATA (attribute not set)
830 printf("ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n",
831 filename, parsed.capabilities, strerror(errno));
832 bad++;
833 }
834 } else {
835 struct vfs_cap_data cap_data;
836 memset(&cap_data, 0, sizeof(cap_data));
837 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
838 cap_data.data[0].permitted = (uint32_t) (parsed.capabilities & 0xffffffff);
839 cap_data.data[0].inheritable = 0;
840 cap_data.data[1].permitted = (uint32_t) (parsed.capabilities >> 32);
841 cap_data.data[1].inheritable = 0;
842 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
843 printf("ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n",
844 filename, parsed.capabilities, strerror(errno));
845 bad++;
846 }
847 }
848 }
849
850 return bad;
851}
852
853// nftw doesn't allow us to pass along context, so we need to use
854// global variables. *sigh*
855static struct perm_parsed_args recursive_parsed_args;
856
857static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr,
858 int fileflags, struct FTW *pfwt) {
859 return ApplyParsedPerms(filename, statptr, recursive_parsed_args);
860}
861
862static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
863 int i;
864 int bad = 0;
865 static int nwarnings = 0;
866 struct stat sb;
867 Value* result = NULL;
868
869 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
870
871 if ((argc % 2) != 1) {
872 return ErrorAbort(state, "%s() expects an odd number of arguments, got %d",
873 name, argc);
874 }
875
876 char** args = ReadVarArgs(state, argc, argv);
877 if (args == NULL) return NULL;
878
879 if (lstat(args[0], &sb) == -1) {
880 result = ErrorAbort(state, "%s: Error on lstat of \"%s\": %s", name, args[0], strerror(errno));
881 goto done;
882 }
883
884 struct perm_parsed_args parsed = ParsePermArgs(argc, args);
885
886 if (recursive) {
887 recursive_parsed_args = parsed;
888 bad += nftw(args[0], do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
889 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
890 } else {
891 bad += ApplyParsedPerms(args[0], &sb, parsed);
892 }
893
894done:
895 for (i = 0; i < argc; ++i) {
896 free(args[i]);
897 }
898 free(args);
899
900 if (result != NULL) {
901 return result;
902 }
903
904 if (bad > 0) {
905 return ErrorAbort(state, "%s: some changes failed", name);
906 }
907
908 return StringValue(strdup(""));
909}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700910
Doug Zongker512536a2010-02-17 16:11:44 -0800911Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700912 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700913 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700914 }
915 char* key;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700916 key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700917 if (key == NULL) return NULL;
918
919 char value[PROPERTY_VALUE_MAX];
920 property_get(key, value, "");
921 free(key);
922
Doug Zongker512536a2010-02-17 16:11:44 -0800923 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700924}
925
926
Doug Zongker47cace92009-06-18 10:11:50 -0700927// file_getprop(file, key)
928//
929// interprets 'file' as a getprop-style file (key=value pairs, one
930// per line, # comment lines and blank lines okay), and returns the value
931// for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800932Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700933 char* result = NULL;
934 char* buffer = NULL;
935 char* filename;
936 char* key;
937 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
938 return NULL;
939 }
940
941 struct stat st;
942 if (stat(filename, &st) < 0) {
943 ErrorAbort(state, "%s: failed to stat \"%s\": %s",
944 name, filename, strerror(errno));
945 goto done;
946 }
947
948#define MAX_FILE_GETPROP_SIZE 65536
949
950 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
951 ErrorAbort(state, "%s too large for %s (max %d)",
952 filename, name, MAX_FILE_GETPROP_SIZE);
953 goto done;
954 }
955
956 buffer = malloc(st.st_size+1);
957 if (buffer == NULL) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700958 ErrorAbort(state, "%s: failed to alloc %lld bytes", name, (long long)st.st_size+1);
Doug Zongker47cace92009-06-18 10:11:50 -0700959 goto done;
960 }
961
962 FILE* f = fopen(filename, "rb");
963 if (f == NULL) {
964 ErrorAbort(state, "%s: failed to open %s: %s",
965 name, filename, strerror(errno));
966 goto done;
967 }
968
969 if (fread(buffer, 1, st.st_size, f) != st.st_size) {
Doug Zongkera23075f2012-08-06 16:19:09 -0700970 ErrorAbort(state, "%s: failed to read %lld bytes from %s",
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700971 name, (long long)st.st_size+1, filename);
Doug Zongker47cace92009-06-18 10:11:50 -0700972 fclose(f);
973 goto done;
974 }
975 buffer[st.st_size] = '\0';
976
977 fclose(f);
978
979 char* line = strtok(buffer, "\n");
980 do {
981 // skip whitespace at start of line
982 while (*line && isspace(*line)) ++line;
983
984 // comment or blank line: skip to next line
985 if (*line == '\0' || *line == '#') continue;
986
987 char* equal = strchr(line, '=');
988 if (equal == NULL) {
989 ErrorAbort(state, "%s: malformed line \"%s\": %s not a prop file?",
990 name, line, filename);
991 goto done;
992 }
993
994 // trim whitespace between key and '='
995 char* key_end = equal-1;
996 while (key_end > line && isspace(*key_end)) --key_end;
997 key_end[1] = '\0';
998
999 // not the key we're looking for
1000 if (strcmp(key, line) != 0) continue;
1001
1002 // skip whitespace after the '=' to the start of the value
1003 char* val_start = equal+1;
1004 while(*val_start && isspace(*val_start)) ++val_start;
1005
1006 // trim trailing whitespace
1007 char* val_end = val_start + strlen(val_start)-1;
1008 while (val_end > val_start && isspace(*val_end)) --val_end;
1009 val_end[1] = '\0';
1010
1011 result = strdup(val_start);
1012 break;
1013
1014 } while ((line = strtok(NULL, "\n")));
1015
1016 if (result == NULL) result = strdup("");
1017
1018 done:
1019 free(filename);
1020 free(key);
1021 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -08001022 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -07001023}
1024
1025
Doug Zongker8edb00c2009-06-11 17:21:44 -07001026static bool write_raw_image_cb(const unsigned char* data,
1027 int data_len, void* ctx) {
1028 int r = mtd_write_data((MtdWriteContext*)ctx, (const char *)data, data_len);
1029 if (r == data_len) return true;
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001030 printf("%s\n", strerror(errno));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001031 return false;
1032}
1033
Doug Zongker179b2d92011-04-12 15:49:04 -07001034// write_raw_image(filename_or_blob, partition)
Doug Zongker512536a2010-02-17 16:11:44 -08001035Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001036 char* result = NULL;
1037
Doug Zongker179b2d92011-04-12 15:49:04 -07001038 Value* partition_value;
1039 Value* contents;
1040 if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001041 return NULL;
1042 }
1043
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001044 char* partition = NULL;
Doug Zongker179b2d92011-04-12 15:49:04 -07001045 if (partition_value->type != VAL_STRING) {
1046 ErrorAbort(state, "partition argument to %s must be string", name);
1047 goto done;
1048 }
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001049 partition = partition_value->data;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001050 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001051 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001052 goto done;
1053 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001054 if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001055 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001056 goto done;
1057 }
1058
1059 mtd_scan_partitions();
1060 const MtdPartition* mtd = mtd_find_partition_by_name(partition);
1061 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001062 printf("%s: no mtd partition named \"%s\"\n", name, partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001063 result = strdup("");
1064 goto done;
1065 }
1066
1067 MtdWriteContext* ctx = mtd_write_partition(mtd);
1068 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001069 printf("%s: can't write mtd partition \"%s\"\n",
Doug Zongker8edb00c2009-06-11 17:21:44 -07001070 name, partition);
1071 result = strdup("");
1072 goto done;
1073 }
1074
1075 bool success;
1076
Doug Zongker179b2d92011-04-12 15:49:04 -07001077 if (contents->type == VAL_STRING) {
1078 // we're given a filename as the contents
1079 char* filename = contents->data;
1080 FILE* f = fopen(filename, "rb");
1081 if (f == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001082 printf("%s: can't open %s: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -07001083 name, filename, strerror(errno));
1084 result = strdup("");
1085 goto done;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001086 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001087
1088 success = true;
1089 char* buffer = malloc(BUFSIZ);
1090 int read;
1091 while (success && (read = fread(buffer, 1, BUFSIZ, f)) > 0) {
1092 int wrote = mtd_write_data(ctx, buffer, read);
1093 success = success && (wrote == read);
1094 }
1095 free(buffer);
1096 fclose(f);
1097 } else {
1098 // we're given a blob as the contents
1099 ssize_t wrote = mtd_write_data(ctx, contents->data, contents->size);
1100 success = (wrote == contents->size);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001101 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001102 if (!success) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001103 printf("mtd_write_data to %s failed: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -07001104 partition, strerror(errno));
1105 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001106
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001107 if (mtd_erase_blocks(ctx, -1) == -1) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001108 printf("%s: error erasing blocks of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001109 }
1110 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001111 printf("%s: error closing write of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001112 }
1113
Doug Zongker179b2d92011-04-12 15:49:04 -07001114 printf("%s %s partition\n",
1115 success ? "wrote" : "failed to write", partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001116
1117 result = success ? partition : strdup("");
1118
1119done:
Doug Zongker179b2d92011-04-12 15:49:04 -07001120 if (result != partition) FreeValue(partition_value);
1121 FreeValue(contents);
Doug Zongker512536a2010-02-17 16:11:44 -08001122 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001123}
1124
Doug Zongker8edb00c2009-06-11 17:21:44 -07001125// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001126Value* ApplyPatchSpaceFn(const char* name, State* state,
1127 int argc, Expr* argv[]) {
1128 char* bytes_str;
1129 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
1130 return NULL;
1131 }
1132
1133 char* endptr;
1134 size_t bytes = strtol(bytes_str, &endptr, 10);
1135 if (bytes == 0 && endptr == bytes_str) {
1136 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n",
1137 name, bytes_str);
1138 free(bytes_str);
1139 return NULL;
1140 }
1141
1142 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
1143}
1144
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001145bool CheckMappedFileSha1(FILE* f, DontCareMap* map, uint8_t* intended_digest) {
1146 MapState state;
1147
1148 state.f = f;
1149 state.so_far = 0;
1150 state.cr = 0;
1151 state.map = map;
1152
1153 SHA_CTX ctx;
1154 SHA_init(&ctx);
1155
1156 unsigned char buffer[32173];
1157 size_t bytes_read;
1158
1159 while ((bytes_read = read_with_map(buffer, sizeof(buffer), &state)) > 0) {
1160 SHA_update(&ctx, buffer, bytes_read);
1161 }
1162 const uint8_t* digest = SHA_final(&ctx);
1163
1164 return memcmp(digest, intended_digest, SHA_DIGEST_SIZE) == 0;
1165}
1166
1167
1168// syspatch(file, tgt_mapfile, tgt_sha1, init_mapfile, init_sha1, patch)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001169
Doug Zongker52b40362014-02-10 15:30:30 -08001170Value* SysPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001171 if (argc != 6) {
1172 return ErrorAbort(state, "%s(): expected 6 args, got %d", name, argc);
Doug Zongker52b40362014-02-10 15:30:30 -08001173 }
1174
1175 char* filename;
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001176 char* target_mapfilename;
Doug Zongker52b40362014-02-10 15:30:30 -08001177 char* target_sha1;
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001178 char* init_mapfilename;
Doug Zongker52b40362014-02-10 15:30:30 -08001179 char* init_sha1;
1180 char* patch_filename;
1181 uint8_t target_digest[SHA_DIGEST_SIZE];
1182 uint8_t init_digest[SHA_DIGEST_SIZE];
1183
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001184 if (ReadArgs(state, argv, 6, &filename,
1185 &target_mapfilename, &target_sha1,
1186 &init_mapfilename, &init_sha1, &patch_filename) < 0) {
Doug Zongker52b40362014-02-10 15:30:30 -08001187 return NULL;
1188 }
1189
1190 if (ParseSha1(target_sha1, target_digest) != 0) {
1191 printf("%s(): failed to parse '%s' as target SHA-1", name, target_sha1);
1192 memset(target_digest, 0, SHA_DIGEST_SIZE);
1193 }
1194 if (ParseSha1(init_sha1, init_digest) != 0) {
1195 printf("%s(): failed to parse '%s' as init SHA-1", name, init_sha1);
1196 memset(init_digest, 0, SHA_DIGEST_SIZE);
1197 }
1198
Doug Zongker52b40362014-02-10 15:30:30 -08001199 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001200 FILE* src = fopen(filename, "r");
Doug Zongker52b40362014-02-10 15:30:30 -08001201
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001202 DontCareMap* init_map = ReadDontCareMapFromZip(za, init_mapfilename);
1203 if (init_map == NULL) return ErrorAbort(state, "%s(): failed to read init map\n", name);
1204 DontCareMap* target_map = ReadDontCareMapFromZip(za, target_mapfilename);
1205 if (target_map == NULL) return ErrorAbort(state, "%s(): failed to read target map\n", name);
1206
1207 if (CheckMappedFileSha1(src, init_map, init_digest)) {
1208 // If the partition contents match the init_digest, then we need to apply the patch.
1209
1210 rewind(src);
1211
1212 const ZipEntry* entry = mzFindZipEntry(za, patch_filename);
1213 if (entry == NULL) {
1214 return ErrorAbort(state, "%s(): no %s in package\n", name, patch_filename);
1215 }
1216
1217 unsigned char* patch_data;
1218 size_t patch_len;
1219 if (!mzGetStoredEntry(za, entry, &patch_data, &patch_len)) {
1220 return ErrorAbort(state, "%s(): failed to get %s entry\n", name, patch_filename);
1221 }
1222
1223 FILE* tgt = fopen(filename, "r+");
1224
1225 int ret = syspatch(src, init_map, patch_data, patch_len, tgt, target_map);
1226
1227 fclose(src);
1228 fclose(tgt);
1229
1230 if (ret != 0) {
1231 return ErrorAbort(state, "%s(): patching failed\n", name);
1232 }
1233 } else {
1234 rewind(src);
1235 if (CheckMappedFileSha1(src, target_map, target_digest)) {
1236 // If the partition contents match the target already, we
1237 // don't need to do anything.
1238 printf("%s: output is already target\n", name);
1239 } else {
1240 return ErrorAbort(state, "%s(): %s in unknown state\n", name, filename);
1241 }
Doug Zongker52b40362014-02-10 15:30:30 -08001242 }
1243
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001244 done:
Doug Zongker52b40362014-02-10 15:30:30 -08001245 free(target_sha1);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001246 free(target_mapfilename);
Doug Zongker52b40362014-02-10 15:30:30 -08001247 free(init_sha1);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001248 free(init_mapfilename);
Doug Zongker52b40362014-02-10 15:30:30 -08001249 free(patch_filename);
1250 return StringValue(filename);
1251
1252}
1253
1254// apply_patch(file, size, init_sha1, tgt_sha1, patch)
1255
Doug Zongker512536a2010-02-17 16:11:44 -08001256Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001257 if (argc < 6 || (argc % 2) == 1) {
1258 return ErrorAbort(state, "%s(): expected at least 6 args and an "
1259 "even number, got %d",
1260 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001261 }
1262
Doug Zongkerc4351c72010-02-22 14:46:32 -08001263 char* source_filename;
1264 char* target_filename;
1265 char* target_sha1;
1266 char* target_size_str;
1267 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
1268 &target_sha1, &target_size_str) < 0) {
1269 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001270 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001271
Doug Zongkerc4351c72010-02-22 14:46:32 -08001272 char* endptr;
1273 size_t target_size = strtol(target_size_str, &endptr, 10);
1274 if (target_size == 0 && endptr == target_size_str) {
1275 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count",
1276 name, target_size_str);
1277 free(source_filename);
1278 free(target_filename);
1279 free(target_sha1);
1280 free(target_size_str);
1281 return NULL;
1282 }
1283
1284 int patchcount = (argc-4) / 2;
1285 Value** patches = ReadValueVarArgs(state, argc-4, argv+4);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001286
1287 int i;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001288 for (i = 0; i < patchcount; ++i) {
1289 if (patches[i*2]->type != VAL_STRING) {
1290 ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i);
1291 break;
1292 }
1293 if (patches[i*2+1]->type != VAL_BLOB) {
1294 ErrorAbort(state, "%s(): patch #%d is not blob", name, i);
1295 break;
1296 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001297 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001298 if (i != patchcount) {
1299 for (i = 0; i < patchcount*2; ++i) {
1300 FreeValue(patches[i]);
1301 }
1302 free(patches);
1303 return NULL;
1304 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001305
Doug Zongkerc4351c72010-02-22 14:46:32 -08001306 char** patch_sha_str = malloc(patchcount * sizeof(char*));
1307 for (i = 0; i < patchcount; ++i) {
1308 patch_sha_str[i] = patches[i*2]->data;
1309 patches[i*2]->data = NULL;
1310 FreeValue(patches[i*2]);
1311 patches[i] = patches[i*2+1];
Doug Zongker8edb00c2009-06-11 17:21:44 -07001312 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001313
1314 int result = applypatch(source_filename, target_filename,
1315 target_sha1, target_size,
Doug Zongkera3ccba62012-08-20 15:28:02 -07001316 patchcount, patch_sha_str, patches, NULL);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001317
1318 for (i = 0; i < patchcount; ++i) {
1319 FreeValue(patches[i]);
1320 }
1321 free(patch_sha_str);
1322 free(patches);
1323
1324 return StringValue(strdup(result == 0 ? "t" : ""));
1325}
1326
1327// apply_patch_check(file, [sha1_1, ...])
1328Value* ApplyPatchCheckFn(const char* name, State* state,
1329 int argc, Expr* argv[]) {
1330 if (argc < 1) {
1331 return ErrorAbort(state, "%s(): expected at least 1 arg, got %d",
1332 name, argc);
1333 }
1334
1335 char* filename;
1336 if (ReadArgs(state, argv, 1, &filename) < 0) {
1337 return NULL;
1338 }
1339
1340 int patchcount = argc-1;
1341 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
1342
1343 int result = applypatch_check(filename, patchcount, sha1s);
1344
1345 int i;
1346 for (i = 0; i < patchcount; ++i) {
1347 free(sha1s[i]);
1348 }
1349 free(sha1s);
1350
1351 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001352}
1353
Doug Zongker512536a2010-02-17 16:11:44 -08001354Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001355 char** args = ReadVarArgs(state, argc, argv);
1356 if (args == NULL) {
1357 return NULL;
1358 }
1359
1360 int size = 0;
1361 int i;
1362 for (i = 0; i < argc; ++i) {
1363 size += strlen(args[i]);
1364 }
1365 char* buffer = malloc(size+1);
1366 size = 0;
1367 for (i = 0; i < argc; ++i) {
1368 strcpy(buffer+size, args[i]);
1369 size += strlen(args[i]);
1370 free(args[i]);
1371 }
1372 free(args);
1373 buffer[size] = '\0';
1374
1375 char* line = strtok(buffer, "\n");
1376 while (line) {
1377 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe,
1378 "ui_print %s\n", line);
1379 line = strtok(NULL, "\n");
1380 }
1381 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "ui_print\n");
1382
Doug Zongker512536a2010-02-17 16:11:44 -08001383 return StringValue(buffer);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001384}
1385
Doug Zongkerd0181b82011-10-19 10:51:12 -07001386Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
1387 if (argc != 0) {
1388 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1389 }
1390 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
1391 return StringValue(strdup("t"));
1392}
1393
Doug Zongker512536a2010-02-17 16:11:44 -08001394Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001395 if (argc < 1) {
1396 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1397 }
1398 char** args = ReadVarArgs(state, argc, argv);
1399 if (args == NULL) {
1400 return NULL;
1401 }
1402
1403 char** args2 = malloc(sizeof(char*) * (argc+1));
1404 memcpy(args2, args, sizeof(char*) * argc);
1405 args2[argc] = NULL;
1406
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001407 printf("about to run program [%s] with %d args\n", args2[0], argc);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001408
1409 pid_t child = fork();
1410 if (child == 0) {
1411 execv(args2[0], args2);
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001412 printf("run_program: execv failed: %s\n", strerror(errno));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001413 _exit(1);
1414 }
1415 int status;
1416 waitpid(child, &status, 0);
1417 if (WIFEXITED(status)) {
1418 if (WEXITSTATUS(status) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001419 printf("run_program: child exited with status %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001420 WEXITSTATUS(status));
1421 }
1422 } else if (WIFSIGNALED(status)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001423 printf("run_program: child terminated by signal %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001424 WTERMSIG(status));
1425 }
1426
1427 int i;
1428 for (i = 0; i < argc; ++i) {
1429 free(args[i]);
1430 }
1431 free(args);
1432 free(args2);
1433
1434 char buffer[20];
1435 sprintf(buffer, "%d", status);
1436
Doug Zongker512536a2010-02-17 16:11:44 -08001437 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001438}
1439
Doug Zongker512536a2010-02-17 16:11:44 -08001440// sha1_check(data)
1441// to return the sha1 of the data (given in the format returned by
1442// read_file).
1443//
1444// sha1_check(data, sha1_hex, [sha1_hex, ...])
1445// returns the sha1 of the file if it matches any of the hex
1446// strings passed, or "" if it does not equal any of them.
1447//
1448Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1449 if (argc < 1) {
1450 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1451 }
1452
1453 Value** args = ReadValueVarArgs(state, argc, argv);
1454 if (args == NULL) {
1455 return NULL;
1456 }
1457
1458 if (args[0]->size < 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001459 return StringValue(strdup(""));
1460 }
1461 uint8_t digest[SHA_DIGEST_SIZE];
Doug Zongkerbac7fba2013-04-10 11:32:17 -07001462 SHA_hash(args[0]->data, args[0]->size, digest);
Doug Zongker512536a2010-02-17 16:11:44 -08001463 FreeValue(args[0]);
1464
1465 if (argc == 1) {
1466 return StringValue(PrintSha1(digest));
1467 }
1468
1469 int i;
1470 uint8_t* arg_digest = malloc(SHA_DIGEST_SIZE);
1471 for (i = 1; i < argc; ++i) {
1472 if (args[i]->type != VAL_STRING) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001473 printf("%s(): arg %d is not a string; skipping",
Doug Zongker512536a2010-02-17 16:11:44 -08001474 name, i);
1475 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1476 // Warn about bad args and skip them.
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001477 printf("%s(): error parsing \"%s\" as sha-1; skipping",
1478 name, args[i]->data);
Doug Zongker512536a2010-02-17 16:11:44 -08001479 } else if (memcmp(digest, arg_digest, SHA_DIGEST_SIZE) == 0) {
1480 break;
1481 }
1482 FreeValue(args[i]);
1483 }
1484 if (i >= argc) {
1485 // Didn't match any of the hex strings; return false.
1486 return StringValue(strdup(""));
1487 }
1488 // Found a match; free all the remaining arguments and return the
1489 // matched one.
1490 int j;
1491 for (j = i+1; j < argc; ++j) {
1492 FreeValue(args[j]);
1493 }
1494 return args[i];
1495}
1496
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001497// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001498// is actually a FileContents*).
1499Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1500 if (argc != 1) {
1501 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1502 }
1503 char* filename;
1504 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1505
1506 Value* v = malloc(sizeof(Value));
1507 v->type = VAL_BLOB;
1508
1509 FileContents fc;
Doug Zongkera1bc1482014-02-13 15:18:19 -08001510 if (LoadFileContents(filename, &fc) != 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001511 free(filename);
Michael Runge6eed2242013-12-13 17:13:11 -08001512 v->size = -1;
1513 v->data = NULL;
Doug Zongker512536a2010-02-17 16:11:44 -08001514 free(fc.data);
Michael Runge6eed2242013-12-13 17:13:11 -08001515 return v;
Doug Zongker512536a2010-02-17 16:11:44 -08001516 }
1517
1518 v->size = fc.size;
1519 v->data = (char*)fc.data;
1520
1521 free(filename);
1522 return v;
1523}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001524
Doug Zongkerc87bab12013-11-25 13:53:25 -08001525// Immediately reboot the device. Recovery is not finished normally,
1526// so if you reboot into recovery it will re-start applying the
1527// current package (because nothing has cleared the copy of the
1528// arguments stored in the BCB).
1529//
1530// The argument is the partition name passed to the android reboot
1531// property. It can be "recovery" to boot from the recovery
1532// partition, or "" (empty string) to boot from the regular boot
1533// partition.
1534Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) {
1535 if (argc != 2) {
1536 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1537 }
1538
1539 char* filename;
1540 char* property;
1541 if (ReadArgs(state, argv, 2, &filename, &property) < 0) return NULL;
1542
1543 char buffer[80];
1544
1545 // zero out the 'command' field of the bootloader message.
1546 memset(buffer, 0, sizeof(((struct bootloader_message*)0)->command));
1547 FILE* f = fopen(filename, "r+b");
1548 fseek(f, offsetof(struct bootloader_message, command), SEEK_SET);
1549 fwrite(buffer, sizeof(((struct bootloader_message*)0)->command), 1, f);
1550 fclose(f);
1551 free(filename);
1552
1553 strcpy(buffer, "reboot,");
1554 if (property != NULL) {
1555 strncat(buffer, property, sizeof(buffer)-10);
1556 }
1557
1558 property_set(ANDROID_RB_PROPERTY, buffer);
1559
1560 sleep(5);
1561 free(property);
1562 ErrorAbort(state, "%s() failed to reboot", name);
1563 return NULL;
1564}
1565
1566// Store a string value somewhere that future invocations of recovery
1567// can access it. This value is called the "stage" and can be used to
1568// drive packages that need to do reboots in the middle of
1569// installation and keep track of where they are in the multi-stage
1570// install.
1571//
1572// The first argument is the block device for the misc partition
1573// ("/misc" in the fstab), which is where this value is stored. The
1574// second argument is the string to store; it should not exceed 31
1575// bytes.
1576Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
1577 if (argc != 2) {
1578 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1579 }
1580
1581 char* filename;
1582 char* stagestr;
1583 if (ReadArgs(state, argv, 2, &filename, &stagestr) < 0) return NULL;
1584
1585 // Store this value in the misc partition, immediately after the
1586 // bootloader message that the main recovery uses to save its
1587 // arguments in case of the device restarting midway through
1588 // package installation.
1589 FILE* f = fopen(filename, "r+b");
1590 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1591 int to_write = strlen(stagestr)+1;
1592 int max_size = sizeof(((struct bootloader_message*)0)->stage);
1593 if (to_write > max_size) {
1594 to_write = max_size;
1595 stagestr[max_size-1] = 0;
1596 }
1597 fwrite(stagestr, to_write, 1, f);
1598 fclose(f);
1599
1600 free(stagestr);
1601 return StringValue(filename);
1602}
1603
1604// Return the value most recently saved with SetStageFn. The argument
1605// is the block device for the misc partition.
1606Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001607 if (argc != 1) {
Doug Zongkerc87bab12013-11-25 13:53:25 -08001608 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1609 }
1610
1611 char* filename;
1612 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1613
1614 char buffer[sizeof(((struct bootloader_message*)0)->stage)];
1615 FILE* f = fopen(filename, "rb");
1616 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1617 fread(buffer, sizeof(buffer), 1, f);
1618 fclose(f);
1619 buffer[sizeof(buffer)-1] = '\0';
1620
1621 return StringValue(strdup(buffer));
1622}
1623
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001624Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) {
1625 if (argc != 2) {
1626 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1627 }
1628
1629 char* filename;
1630 char* len_str;
1631 if (ReadArgs(state, argv, 2, &filename, &len_str) < 0) return NULL;
1632
1633 size_t len = strtoull(len_str, NULL, 0);
1634 int fd = open(filename, O_WRONLY, 0644);
1635 int success = wipe_block_device(fd, len);
1636
1637 free(filename);
1638 free(len_str);
1639
1640 close(fd);
1641
1642 return StringValue(strdup(success ? "t" : ""));
1643}
1644
Doug Zongker9931f7f2009-06-10 14:11:53 -07001645void RegisterInstallFunctions() {
1646 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001647 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001648 RegisterFunction("unmount", UnmountFn);
1649 RegisterFunction("format", FormatFn);
1650 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001651 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001652 RegisterFunction("delete", DeleteFn);
1653 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001654 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1655 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001656 RegisterFunction("symlink", SymlinkFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001657
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001658 // Usage:
1659 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1660 // Example:
1661 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1662 RegisterFunction("set_metadata", SetMetadataFn);
1663
1664 // Usage:
1665 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1666 // Example:
1667 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1668 RegisterFunction("set_metadata_recursive", SetMetadataFn);
1669
Doug Zongker8edb00c2009-06-11 17:21:44 -07001670 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001671 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001672 RegisterFunction("write_raw_image", WriteRawImageFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001673
1674 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001675 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1676 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001677
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001678 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001679 RegisterFunction("syspatch", SysPatchFn);
1680
Doug Zongker512536a2010-02-17 16:11:44 -08001681 RegisterFunction("read_file", ReadFileFn);
1682 RegisterFunction("sha1_check", Sha1CheckFn);
Michael Rungece7ca712013-11-06 17:42:20 -08001683 RegisterFunction("rename", RenameFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001684
Doug Zongkerd0181b82011-10-19 10:51:12 -07001685 RegisterFunction("wipe_cache", WipeCacheFn);
1686
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001687 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001688
1689 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001690
1691 RegisterFunction("reboot_now", RebootNowFn);
1692 RegisterFunction("get_stage", GetStageFn);
1693 RegisterFunction("set_stage", SetStageFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001694}