blob: 8defc77208453445e3dd72944a7ef41b39c1fbe5 [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
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700930// per line. # comment lines,blank lines, lines without '=' ignored),
931// and returns the value 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) {
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700989 continue;
Doug Zongker47cace92009-06-18 10:11:50 -0700990 }
991
992 // trim whitespace between key and '='
993 char* key_end = equal-1;
994 while (key_end > line && isspace(*key_end)) --key_end;
995 key_end[1] = '\0';
996
997 // not the key we're looking for
998 if (strcmp(key, line) != 0) continue;
999
1000 // skip whitespace after the '=' to the start of the value
1001 char* val_start = equal+1;
1002 while(*val_start && isspace(*val_start)) ++val_start;
1003
1004 // trim trailing whitespace
1005 char* val_end = val_start + strlen(val_start)-1;
1006 while (val_end > val_start && isspace(*val_end)) --val_end;
1007 val_end[1] = '\0';
1008
1009 result = strdup(val_start);
1010 break;
1011
1012 } while ((line = strtok(NULL, "\n")));
1013
1014 if (result == NULL) result = strdup("");
1015
1016 done:
1017 free(filename);
1018 free(key);
1019 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -08001020 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -07001021}
1022
1023
Doug Zongker8edb00c2009-06-11 17:21:44 -07001024static bool write_raw_image_cb(const unsigned char* data,
1025 int data_len, void* ctx) {
1026 int r = mtd_write_data((MtdWriteContext*)ctx, (const char *)data, data_len);
1027 if (r == data_len) return true;
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001028 printf("%s\n", strerror(errno));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001029 return false;
1030}
1031
Doug Zongker179b2d92011-04-12 15:49:04 -07001032// write_raw_image(filename_or_blob, partition)
Doug Zongker512536a2010-02-17 16:11:44 -08001033Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001034 char* result = NULL;
1035
Doug Zongker179b2d92011-04-12 15:49:04 -07001036 Value* partition_value;
1037 Value* contents;
1038 if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001039 return NULL;
1040 }
1041
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001042 char* partition = NULL;
Doug Zongker179b2d92011-04-12 15:49:04 -07001043 if (partition_value->type != VAL_STRING) {
1044 ErrorAbort(state, "partition argument to %s must be string", name);
1045 goto done;
1046 }
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001047 partition = partition_value->data;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001048 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001049 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001050 goto done;
1051 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001052 if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001053 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001054 goto done;
1055 }
1056
1057 mtd_scan_partitions();
1058 const MtdPartition* mtd = mtd_find_partition_by_name(partition);
1059 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001060 printf("%s: no mtd partition named \"%s\"\n", name, partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001061 result = strdup("");
1062 goto done;
1063 }
1064
1065 MtdWriteContext* ctx = mtd_write_partition(mtd);
1066 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001067 printf("%s: can't write mtd partition \"%s\"\n",
Doug Zongker8edb00c2009-06-11 17:21:44 -07001068 name, partition);
1069 result = strdup("");
1070 goto done;
1071 }
1072
1073 bool success;
1074
Doug Zongker179b2d92011-04-12 15:49:04 -07001075 if (contents->type == VAL_STRING) {
1076 // we're given a filename as the contents
1077 char* filename = contents->data;
1078 FILE* f = fopen(filename, "rb");
1079 if (f == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001080 printf("%s: can't open %s: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -07001081 name, filename, strerror(errno));
1082 result = strdup("");
1083 goto done;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001084 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001085
1086 success = true;
1087 char* buffer = malloc(BUFSIZ);
1088 int read;
1089 while (success && (read = fread(buffer, 1, BUFSIZ, f)) > 0) {
1090 int wrote = mtd_write_data(ctx, buffer, read);
1091 success = success && (wrote == read);
1092 }
1093 free(buffer);
1094 fclose(f);
1095 } else {
1096 // we're given a blob as the contents
1097 ssize_t wrote = mtd_write_data(ctx, contents->data, contents->size);
1098 success = (wrote == contents->size);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001099 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001100 if (!success) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001101 printf("mtd_write_data to %s failed: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -07001102 partition, strerror(errno));
1103 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001104
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001105 if (mtd_erase_blocks(ctx, -1) == -1) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001106 printf("%s: error erasing blocks of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001107 }
1108 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001109 printf("%s: error closing write of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001110 }
1111
Doug Zongker179b2d92011-04-12 15:49:04 -07001112 printf("%s %s partition\n",
1113 success ? "wrote" : "failed to write", partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001114
1115 result = success ? partition : strdup("");
1116
1117done:
Doug Zongker179b2d92011-04-12 15:49:04 -07001118 if (result != partition) FreeValue(partition_value);
1119 FreeValue(contents);
Doug Zongker512536a2010-02-17 16:11:44 -08001120 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001121}
1122
Doug Zongker8edb00c2009-06-11 17:21:44 -07001123// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001124Value* ApplyPatchSpaceFn(const char* name, State* state,
1125 int argc, Expr* argv[]) {
1126 char* bytes_str;
1127 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
1128 return NULL;
1129 }
1130
1131 char* endptr;
1132 size_t bytes = strtol(bytes_str, &endptr, 10);
1133 if (bytes == 0 && endptr == bytes_str) {
1134 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n",
1135 name, bytes_str);
1136 free(bytes_str);
1137 return NULL;
1138 }
1139
1140 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
1141}
1142
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001143bool CheckMappedFileSha1(FILE* f, DontCareMap* map, uint8_t* intended_digest) {
1144 MapState state;
1145
1146 state.f = f;
1147 state.so_far = 0;
1148 state.cr = 0;
1149 state.map = map;
1150
1151 SHA_CTX ctx;
1152 SHA_init(&ctx);
1153
1154 unsigned char buffer[32173];
1155 size_t bytes_read;
1156
1157 while ((bytes_read = read_with_map(buffer, sizeof(buffer), &state)) > 0) {
1158 SHA_update(&ctx, buffer, bytes_read);
1159 }
1160 const uint8_t* digest = SHA_final(&ctx);
1161
1162 return memcmp(digest, intended_digest, SHA_DIGEST_SIZE) == 0;
1163}
1164
1165
1166// syspatch(file, tgt_mapfile, tgt_sha1, init_mapfile, init_sha1, patch)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001167
Doug Zongker52b40362014-02-10 15:30:30 -08001168Value* SysPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001169 if (argc != 6) {
1170 return ErrorAbort(state, "%s(): expected 6 args, got %d", name, argc);
Doug Zongker52b40362014-02-10 15:30:30 -08001171 }
1172
1173 char* filename;
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001174 char* target_mapfilename;
Doug Zongker52b40362014-02-10 15:30:30 -08001175 char* target_sha1;
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001176 char* init_mapfilename;
Doug Zongker52b40362014-02-10 15:30:30 -08001177 char* init_sha1;
1178 char* patch_filename;
1179 uint8_t target_digest[SHA_DIGEST_SIZE];
1180 uint8_t init_digest[SHA_DIGEST_SIZE];
1181
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001182 if (ReadArgs(state, argv, 6, &filename,
1183 &target_mapfilename, &target_sha1,
1184 &init_mapfilename, &init_sha1, &patch_filename) < 0) {
Doug Zongker52b40362014-02-10 15:30:30 -08001185 return NULL;
1186 }
1187
1188 if (ParseSha1(target_sha1, target_digest) != 0) {
1189 printf("%s(): failed to parse '%s' as target SHA-1", name, target_sha1);
1190 memset(target_digest, 0, SHA_DIGEST_SIZE);
1191 }
1192 if (ParseSha1(init_sha1, init_digest) != 0) {
1193 printf("%s(): failed to parse '%s' as init SHA-1", name, init_sha1);
1194 memset(init_digest, 0, SHA_DIGEST_SIZE);
1195 }
1196
Doug Zongker52b40362014-02-10 15:30:30 -08001197 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001198 FILE* src = fopen(filename, "r");
Doug Zongker52b40362014-02-10 15:30:30 -08001199
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001200 DontCareMap* init_map = ReadDontCareMapFromZip(za, init_mapfilename);
1201 if (init_map == NULL) return ErrorAbort(state, "%s(): failed to read init map\n", name);
1202 DontCareMap* target_map = ReadDontCareMapFromZip(za, target_mapfilename);
1203 if (target_map == NULL) return ErrorAbort(state, "%s(): failed to read target map\n", name);
1204
1205 if (CheckMappedFileSha1(src, init_map, init_digest)) {
1206 // If the partition contents match the init_digest, then we need to apply the patch.
1207
1208 rewind(src);
1209
1210 const ZipEntry* entry = mzFindZipEntry(za, patch_filename);
1211 if (entry == NULL) {
1212 return ErrorAbort(state, "%s(): no %s in package\n", name, patch_filename);
1213 }
1214
1215 unsigned char* patch_data;
1216 size_t patch_len;
1217 if (!mzGetStoredEntry(za, entry, &patch_data, &patch_len)) {
1218 return ErrorAbort(state, "%s(): failed to get %s entry\n", name, patch_filename);
1219 }
1220
1221 FILE* tgt = fopen(filename, "r+");
1222
1223 int ret = syspatch(src, init_map, patch_data, patch_len, tgt, target_map);
1224
1225 fclose(src);
1226 fclose(tgt);
1227
1228 if (ret != 0) {
1229 return ErrorAbort(state, "%s(): patching failed\n", name);
1230 }
1231 } else {
1232 rewind(src);
1233 if (CheckMappedFileSha1(src, target_map, target_digest)) {
1234 // If the partition contents match the target already, we
1235 // don't need to do anything.
1236 printf("%s: output is already target\n", name);
1237 } else {
1238 return ErrorAbort(state, "%s(): %s in unknown state\n", name, filename);
1239 }
Doug Zongker52b40362014-02-10 15:30:30 -08001240 }
1241
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001242 done:
Doug Zongker52b40362014-02-10 15:30:30 -08001243 free(target_sha1);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001244 free(target_mapfilename);
Doug Zongker52b40362014-02-10 15:30:30 -08001245 free(init_sha1);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001246 free(init_mapfilename);
Doug Zongker52b40362014-02-10 15:30:30 -08001247 free(patch_filename);
1248 return StringValue(filename);
1249
1250}
1251
1252// apply_patch(file, size, init_sha1, tgt_sha1, patch)
1253
Doug Zongker512536a2010-02-17 16:11:44 -08001254Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001255 if (argc < 6 || (argc % 2) == 1) {
1256 return ErrorAbort(state, "%s(): expected at least 6 args and an "
1257 "even number, got %d",
1258 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001259 }
1260
Doug Zongkerc4351c72010-02-22 14:46:32 -08001261 char* source_filename;
1262 char* target_filename;
1263 char* target_sha1;
1264 char* target_size_str;
1265 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
1266 &target_sha1, &target_size_str) < 0) {
1267 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001268 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001269
Doug Zongkerc4351c72010-02-22 14:46:32 -08001270 char* endptr;
1271 size_t target_size = strtol(target_size_str, &endptr, 10);
1272 if (target_size == 0 && endptr == target_size_str) {
1273 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count",
1274 name, target_size_str);
1275 free(source_filename);
1276 free(target_filename);
1277 free(target_sha1);
1278 free(target_size_str);
1279 return NULL;
1280 }
1281
1282 int patchcount = (argc-4) / 2;
1283 Value** patches = ReadValueVarArgs(state, argc-4, argv+4);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001284
1285 int i;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001286 for (i = 0; i < patchcount; ++i) {
1287 if (patches[i*2]->type != VAL_STRING) {
1288 ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i);
1289 break;
1290 }
1291 if (patches[i*2+1]->type != VAL_BLOB) {
1292 ErrorAbort(state, "%s(): patch #%d is not blob", name, i);
1293 break;
1294 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001295 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001296 if (i != patchcount) {
1297 for (i = 0; i < patchcount*2; ++i) {
1298 FreeValue(patches[i]);
1299 }
1300 free(patches);
1301 return NULL;
1302 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001303
Doug Zongkerc4351c72010-02-22 14:46:32 -08001304 char** patch_sha_str = malloc(patchcount * sizeof(char*));
1305 for (i = 0; i < patchcount; ++i) {
1306 patch_sha_str[i] = patches[i*2]->data;
1307 patches[i*2]->data = NULL;
1308 FreeValue(patches[i*2]);
1309 patches[i] = patches[i*2+1];
Doug Zongker8edb00c2009-06-11 17:21:44 -07001310 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001311
1312 int result = applypatch(source_filename, target_filename,
1313 target_sha1, target_size,
Doug Zongkera3ccba62012-08-20 15:28:02 -07001314 patchcount, patch_sha_str, patches, NULL);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001315
1316 for (i = 0; i < patchcount; ++i) {
1317 FreeValue(patches[i]);
1318 }
1319 free(patch_sha_str);
1320 free(patches);
1321
1322 return StringValue(strdup(result == 0 ? "t" : ""));
1323}
1324
1325// apply_patch_check(file, [sha1_1, ...])
1326Value* ApplyPatchCheckFn(const char* name, State* state,
1327 int argc, Expr* argv[]) {
1328 if (argc < 1) {
1329 return ErrorAbort(state, "%s(): expected at least 1 arg, got %d",
1330 name, argc);
1331 }
1332
1333 char* filename;
1334 if (ReadArgs(state, argv, 1, &filename) < 0) {
1335 return NULL;
1336 }
1337
1338 int patchcount = argc-1;
1339 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
1340
1341 int result = applypatch_check(filename, patchcount, sha1s);
1342
1343 int i;
1344 for (i = 0; i < patchcount; ++i) {
1345 free(sha1s[i]);
1346 }
1347 free(sha1s);
1348
1349 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001350}
1351
Doug Zongker512536a2010-02-17 16:11:44 -08001352Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001353 char** args = ReadVarArgs(state, argc, argv);
1354 if (args == NULL) {
1355 return NULL;
1356 }
1357
1358 int size = 0;
1359 int i;
1360 for (i = 0; i < argc; ++i) {
1361 size += strlen(args[i]);
1362 }
1363 char* buffer = malloc(size+1);
1364 size = 0;
1365 for (i = 0; i < argc; ++i) {
1366 strcpy(buffer+size, args[i]);
1367 size += strlen(args[i]);
1368 free(args[i]);
1369 }
1370 free(args);
1371 buffer[size] = '\0';
1372
1373 char* line = strtok(buffer, "\n");
1374 while (line) {
1375 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe,
1376 "ui_print %s\n", line);
1377 line = strtok(NULL, "\n");
1378 }
1379 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "ui_print\n");
1380
Doug Zongker512536a2010-02-17 16:11:44 -08001381 return StringValue(buffer);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001382}
1383
Doug Zongkerd0181b82011-10-19 10:51:12 -07001384Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
1385 if (argc != 0) {
1386 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1387 }
1388 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
1389 return StringValue(strdup("t"));
1390}
1391
Doug Zongker512536a2010-02-17 16:11:44 -08001392Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001393 if (argc < 1) {
1394 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1395 }
1396 char** args = ReadVarArgs(state, argc, argv);
1397 if (args == NULL) {
1398 return NULL;
1399 }
1400
1401 char** args2 = malloc(sizeof(char*) * (argc+1));
1402 memcpy(args2, args, sizeof(char*) * argc);
1403 args2[argc] = NULL;
1404
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001405 printf("about to run program [%s] with %d args\n", args2[0], argc);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001406
1407 pid_t child = fork();
1408 if (child == 0) {
1409 execv(args2[0], args2);
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001410 printf("run_program: execv failed: %s\n", strerror(errno));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001411 _exit(1);
1412 }
1413 int status;
1414 waitpid(child, &status, 0);
1415 if (WIFEXITED(status)) {
1416 if (WEXITSTATUS(status) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001417 printf("run_program: child exited with status %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001418 WEXITSTATUS(status));
1419 }
1420 } else if (WIFSIGNALED(status)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001421 printf("run_program: child terminated by signal %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001422 WTERMSIG(status));
1423 }
1424
1425 int i;
1426 for (i = 0; i < argc; ++i) {
1427 free(args[i]);
1428 }
1429 free(args);
1430 free(args2);
1431
1432 char buffer[20];
1433 sprintf(buffer, "%d", status);
1434
Doug Zongker512536a2010-02-17 16:11:44 -08001435 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001436}
1437
Doug Zongker512536a2010-02-17 16:11:44 -08001438// sha1_check(data)
1439// to return the sha1 of the data (given in the format returned by
1440// read_file).
1441//
1442// sha1_check(data, sha1_hex, [sha1_hex, ...])
1443// returns the sha1 of the file if it matches any of the hex
1444// strings passed, or "" if it does not equal any of them.
1445//
1446Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1447 if (argc < 1) {
1448 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1449 }
1450
1451 Value** args = ReadValueVarArgs(state, argc, argv);
1452 if (args == NULL) {
1453 return NULL;
1454 }
1455
1456 if (args[0]->size < 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001457 return StringValue(strdup(""));
1458 }
1459 uint8_t digest[SHA_DIGEST_SIZE];
Doug Zongkerbac7fba2013-04-10 11:32:17 -07001460 SHA_hash(args[0]->data, args[0]->size, digest);
Doug Zongker512536a2010-02-17 16:11:44 -08001461 FreeValue(args[0]);
1462
1463 if (argc == 1) {
1464 return StringValue(PrintSha1(digest));
1465 }
1466
1467 int i;
1468 uint8_t* arg_digest = malloc(SHA_DIGEST_SIZE);
1469 for (i = 1; i < argc; ++i) {
1470 if (args[i]->type != VAL_STRING) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001471 printf("%s(): arg %d is not a string; skipping",
Doug Zongker512536a2010-02-17 16:11:44 -08001472 name, i);
1473 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1474 // Warn about bad args and skip them.
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001475 printf("%s(): error parsing \"%s\" as sha-1; skipping",
1476 name, args[i]->data);
Doug Zongker512536a2010-02-17 16:11:44 -08001477 } else if (memcmp(digest, arg_digest, SHA_DIGEST_SIZE) == 0) {
1478 break;
1479 }
1480 FreeValue(args[i]);
1481 }
1482 if (i >= argc) {
1483 // Didn't match any of the hex strings; return false.
1484 return StringValue(strdup(""));
1485 }
1486 // Found a match; free all the remaining arguments and return the
1487 // matched one.
1488 int j;
1489 for (j = i+1; j < argc; ++j) {
1490 FreeValue(args[j]);
1491 }
1492 return args[i];
1493}
1494
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001495// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001496// is actually a FileContents*).
1497Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1498 if (argc != 1) {
1499 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1500 }
1501 char* filename;
1502 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1503
1504 Value* v = malloc(sizeof(Value));
1505 v->type = VAL_BLOB;
1506
1507 FileContents fc;
Doug Zongkera1bc1482014-02-13 15:18:19 -08001508 if (LoadFileContents(filename, &fc) != 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001509 free(filename);
Michael Runge6eed2242013-12-13 17:13:11 -08001510 v->size = -1;
1511 v->data = NULL;
Doug Zongker512536a2010-02-17 16:11:44 -08001512 free(fc.data);
Michael Runge6eed2242013-12-13 17:13:11 -08001513 return v;
Doug Zongker512536a2010-02-17 16:11:44 -08001514 }
1515
1516 v->size = fc.size;
1517 v->data = (char*)fc.data;
1518
1519 free(filename);
1520 return v;
1521}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001522
Doug Zongkerc87bab12013-11-25 13:53:25 -08001523// Immediately reboot the device. Recovery is not finished normally,
1524// so if you reboot into recovery it will re-start applying the
1525// current package (because nothing has cleared the copy of the
1526// arguments stored in the BCB).
1527//
1528// The argument is the partition name passed to the android reboot
1529// property. It can be "recovery" to boot from the recovery
1530// partition, or "" (empty string) to boot from the regular boot
1531// partition.
1532Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) {
1533 if (argc != 2) {
1534 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1535 }
1536
1537 char* filename;
1538 char* property;
1539 if (ReadArgs(state, argv, 2, &filename, &property) < 0) return NULL;
1540
1541 char buffer[80];
1542
1543 // zero out the 'command' field of the bootloader message.
1544 memset(buffer, 0, sizeof(((struct bootloader_message*)0)->command));
1545 FILE* f = fopen(filename, "r+b");
1546 fseek(f, offsetof(struct bootloader_message, command), SEEK_SET);
1547 fwrite(buffer, sizeof(((struct bootloader_message*)0)->command), 1, f);
1548 fclose(f);
1549 free(filename);
1550
1551 strcpy(buffer, "reboot,");
1552 if (property != NULL) {
1553 strncat(buffer, property, sizeof(buffer)-10);
1554 }
1555
1556 property_set(ANDROID_RB_PROPERTY, buffer);
1557
1558 sleep(5);
1559 free(property);
1560 ErrorAbort(state, "%s() failed to reboot", name);
1561 return NULL;
1562}
1563
1564// Store a string value somewhere that future invocations of recovery
1565// can access it. This value is called the "stage" and can be used to
1566// drive packages that need to do reboots in the middle of
1567// installation and keep track of where they are in the multi-stage
1568// install.
1569//
1570// The first argument is the block device for the misc partition
1571// ("/misc" in the fstab), which is where this value is stored. The
1572// second argument is the string to store; it should not exceed 31
1573// bytes.
1574Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
1575 if (argc != 2) {
1576 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1577 }
1578
1579 char* filename;
1580 char* stagestr;
1581 if (ReadArgs(state, argv, 2, &filename, &stagestr) < 0) return NULL;
1582
1583 // Store this value in the misc partition, immediately after the
1584 // bootloader message that the main recovery uses to save its
1585 // arguments in case of the device restarting midway through
1586 // package installation.
1587 FILE* f = fopen(filename, "r+b");
1588 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1589 int to_write = strlen(stagestr)+1;
1590 int max_size = sizeof(((struct bootloader_message*)0)->stage);
1591 if (to_write > max_size) {
1592 to_write = max_size;
1593 stagestr[max_size-1] = 0;
1594 }
1595 fwrite(stagestr, to_write, 1, f);
1596 fclose(f);
1597
1598 free(stagestr);
1599 return StringValue(filename);
1600}
1601
1602// Return the value most recently saved with SetStageFn. The argument
1603// is the block device for the misc partition.
1604Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001605 if (argc != 1) {
Doug Zongkerc87bab12013-11-25 13:53:25 -08001606 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1607 }
1608
1609 char* filename;
1610 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1611
1612 char buffer[sizeof(((struct bootloader_message*)0)->stage)];
1613 FILE* f = fopen(filename, "rb");
1614 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1615 fread(buffer, sizeof(buffer), 1, f);
1616 fclose(f);
1617 buffer[sizeof(buffer)-1] = '\0';
1618
1619 return StringValue(strdup(buffer));
1620}
1621
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001622Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) {
1623 if (argc != 2) {
1624 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1625 }
1626
1627 char* filename;
1628 char* len_str;
1629 if (ReadArgs(state, argv, 2, &filename, &len_str) < 0) return NULL;
1630
1631 size_t len = strtoull(len_str, NULL, 0);
1632 int fd = open(filename, O_WRONLY, 0644);
1633 int success = wipe_block_device(fd, len);
1634
1635 free(filename);
1636 free(len_str);
1637
1638 close(fd);
1639
1640 return StringValue(strdup(success ? "t" : ""));
1641}
1642
Doug Zongker9931f7f2009-06-10 14:11:53 -07001643void RegisterInstallFunctions() {
1644 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001645 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001646 RegisterFunction("unmount", UnmountFn);
1647 RegisterFunction("format", FormatFn);
1648 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001649 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001650 RegisterFunction("delete", DeleteFn);
1651 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001652 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1653 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001654 RegisterFunction("symlink", SymlinkFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001655
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001656 // Usage:
1657 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1658 // Example:
1659 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1660 RegisterFunction("set_metadata", SetMetadataFn);
1661
1662 // Usage:
1663 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1664 // Example:
1665 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1666 RegisterFunction("set_metadata_recursive", SetMetadataFn);
1667
Doug Zongker8edb00c2009-06-11 17:21:44 -07001668 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001669 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001670 RegisterFunction("write_raw_image", WriteRawImageFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001671
1672 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001673 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1674 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001675
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001676 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001677 RegisterFunction("syspatch", SysPatchFn);
1678
Doug Zongker512536a2010-02-17 16:11:44 -08001679 RegisterFunction("read_file", ReadFileFn);
1680 RegisterFunction("sha1_check", Sha1CheckFn);
Michael Rungece7ca712013-11-06 17:42:20 -08001681 RegisterFunction("rename", RenameFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001682
Doug Zongkerd0181b82011-10-19 10:51:12 -07001683 RegisterFunction("wipe_cache", WipeCacheFn);
1684
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001685 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001686
1687 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001688
1689 RegisterFunction("reboot_now", RebootNowFn);
1690 RegisterFunction("get_stage", GetStageFn);
1691 RegisterFunction("set_stage", SetStageFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001692}