blob: ccafad9c2b6d09fe4ef4a8502076db16fc61718c [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 Zongker8edb00c2009-06-11 17:21:44 -070049
Doug Zongker3d177d02010-07-01 09:18:44 -070050#ifdef USE_EXT4
51#include "make_ext4fs.h"
52#endif
53
Doug Zongker52b40362014-02-10 15:30:30 -080054// Take a sha-1 digest and return it as a newly-allocated hex string.
Doug Zongker0d32f252014-02-13 15:07:56 -080055static char* PrintSha1(const uint8_t* digest) {
Doug Zongker52b40362014-02-10 15:30:30 -080056 char* buffer = malloc(SHA_DIGEST_SIZE*2 + 1);
57 int i;
58 const char* alphabet = "0123456789abcdef";
59 for (i = 0; i < SHA_DIGEST_SIZE; ++i) {
60 buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf];
61 buffer[i*2+1] = alphabet[digest[i] & 0xf];
62 }
63 buffer[i*2] = '\0';
64 return buffer;
65}
66
Doug Zongker3d177d02010-07-01 09:18:44 -070067// mount(fs_type, partition_type, location, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -070068//
Doug Zongker3d177d02010-07-01 09:18:44 -070069// fs_type="yaffs2" partition_type="MTD" location=partition
70// fs_type="ext4" partition_type="EMMC" location=device
Doug Zongker512536a2010-02-17 16:11:44 -080071Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070072 char* result = NULL;
Doug Zongker3d177d02010-07-01 09:18:44 -070073 if (argc != 4) {
74 return ErrorAbort(state, "%s() expects 4 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -070075 }
Doug Zongker3d177d02010-07-01 09:18:44 -070076 char* fs_type;
77 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -070078 char* location;
79 char* mount_point;
Doug Zongker3d177d02010-07-01 09:18:44 -070080 if (ReadArgs(state, argv, 4, &fs_type, &partition_type,
81 &location, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070082 return NULL;
83 }
84
Doug Zongker3d177d02010-07-01 09:18:44 -070085 if (strlen(fs_type) == 0) {
86 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
87 goto done;
88 }
89 if (strlen(partition_type) == 0) {
90 ErrorAbort(state, "partition_type argument to %s() can't be empty",
91 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -070092 goto done;
93 }
94 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070095 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -070096 goto done;
97 }
98 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070099 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700100 goto done;
101 }
102
Stephen Smalley779701d2012-02-09 14:13:23 -0500103 char *secontext = NULL;
104
105 if (sehandle) {
106 selabel_lookup(sehandle, &secontext, mount_point, 0755);
107 setfscreatecon(secontext);
108 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500109
Doug Zongker9931f7f2009-06-10 14:11:53 -0700110 mkdir(mount_point, 0755);
111
Stephen Smalley779701d2012-02-09 14:13:23 -0500112 if (secontext) {
113 freecon(secontext);
114 setfscreatecon(NULL);
115 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500116
Doug Zongker3d177d02010-07-01 09:18:44 -0700117 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700118 mtd_scan_partitions();
119 const MtdPartition* mtd;
120 mtd = mtd_find_partition_by_name(location);
121 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700122 printf("%s: no mtd partition named \"%s\"",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700123 name, location);
124 result = strdup("");
125 goto done;
126 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700127 if (mtd_mount_partition(mtd, mount_point, fs_type, 0 /* rw */) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700128 printf("mtd mount of %s failed: %s\n",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700129 location, strerror(errno));
130 result = strdup("");
131 goto done;
132 }
133 result = mount_point;
134 } else {
Doug Zongker3d177d02010-07-01 09:18:44 -0700135 if (mount(location, mount_point, fs_type,
Doug Zongker9931f7f2009-06-10 14:11:53 -0700136 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "") < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700137 printf("%s: failed to mount %s at %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700138 name, location, mount_point, strerror(errno));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700139 result = strdup("");
140 } else {
141 result = mount_point;
142 }
143 }
144
145done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700146 free(fs_type);
147 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700148 free(location);
149 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800150 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700151}
152
Doug Zongker8edb00c2009-06-11 17:21:44 -0700153
154// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800155Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700156 char* result = NULL;
157 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700158 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700159 }
160 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700161 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700162 return NULL;
163 }
164 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700165 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700166 goto done;
167 }
168
169 scan_mounted_volumes();
170 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
171 if (vol == NULL) {
172 result = strdup("");
173 } else {
174 result = mount_point;
175 }
176
177done:
178 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800179 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700180}
181
182
Doug Zongker512536a2010-02-17 16:11:44 -0800183Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700184 char* result = NULL;
185 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700186 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700187 }
188 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700189 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700190 return NULL;
191 }
192 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700193 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700194 goto done;
195 }
196
197 scan_mounted_volumes();
198 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
199 if (vol == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700200 printf("unmount of %s failed; no such volume\n", mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700201 result = strdup("");
202 } else {
203 unmount_mounted_volume(vol);
204 result = mount_point;
205 }
206
207done:
208 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800209 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700210}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700211
212
Stephen Smalley779701d2012-02-09 14:13:23 -0500213// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700214//
Stephen Smalley779701d2012-02-09 14:13:23 -0500215// fs_type="yaffs2" partition_type="MTD" location=partition fs_size=<bytes> mount_point=<location>
216// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800217// if fs_size == 0, then make_ext4fs uses the entire partition.
218// if fs_size > 0, that is the size to use
219// if fs_size < 0, then reserve that many bytes at the end of the partition
Doug Zongker512536a2010-02-17 16:11:44 -0800220Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700221 char* result = NULL;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400222 if (argc != 5) {
223 return ErrorAbort(state, "%s() expects 5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700224 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700225 char* fs_type;
226 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700227 char* location;
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800228 char* fs_size;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400229 char* mount_point;
Stephen Smalley779701d2012-02-09 14:13:23 -0500230
Stephen Smalley779701d2012-02-09 14:13:23 -0500231 if (ReadArgs(state, argv, 5, &fs_type, &partition_type, &location, &fs_size, &mount_point) < 0) {
232 return NULL;
233 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700234
Doug Zongker3d177d02010-07-01 09:18:44 -0700235 if (strlen(fs_type) == 0) {
236 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
237 goto done;
238 }
239 if (strlen(partition_type) == 0) {
240 ErrorAbort(state, "partition_type argument to %s() can't be empty",
241 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700242 goto done;
243 }
244 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700245 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700246 goto done;
247 }
248
Stephen Smalley516e4e22012-04-03 13:35:11 -0400249 if (strlen(mount_point) == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500250 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
251 goto done;
252 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500253
Doug Zongker3d177d02010-07-01 09:18:44 -0700254 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700255 mtd_scan_partitions();
256 const MtdPartition* mtd = mtd_find_partition_by_name(location);
257 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700258 printf("%s: no mtd partition named \"%s\"",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700259 name, location);
260 result = strdup("");
261 goto done;
262 }
263 MtdWriteContext* ctx = mtd_write_partition(mtd);
264 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700265 printf("%s: can't write \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700266 result = strdup("");
267 goto done;
268 }
269 if (mtd_erase_blocks(ctx, -1) == -1) {
270 mtd_write_close(ctx);
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700271 printf("%s: failed to erase \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700272 result = strdup("");
273 goto done;
274 }
275 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700276 printf("%s: failed to close \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700277 result = strdup("");
278 goto done;
279 }
280 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700281#ifdef USE_EXT4
282 } else if (strcmp(fs_type, "ext4") == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500283 int status = make_ext4fs(location, atoll(fs_size), mount_point, sehandle);
Doug Zongker3d177d02010-07-01 09:18:44 -0700284 if (status != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700285 printf("%s: make_ext4fs failed (%d) on %s",
Doug Zongker3d177d02010-07-01 09:18:44 -0700286 name, status, location);
287 result = strdup("");
288 goto done;
289 }
290 result = location;
291#endif
Doug Zongker9931f7f2009-06-10 14:11:53 -0700292 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700293 printf("%s: unsupported fs_type \"%s\" partition_type \"%s\"",
Doug Zongker3d177d02010-07-01 09:18:44 -0700294 name, fs_type, partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700295 }
296
297done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700298 free(fs_type);
299 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700300 if (result != location) free(location);
Doug Zongker512536a2010-02-17 16:11:44 -0800301 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700302}
303
Michael Rungece7ca712013-11-06 17:42:20 -0800304Value* RenameFn(const char* name, State* state, int argc, Expr* argv[]) {
305 char* result = NULL;
306 if (argc != 2) {
307 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
308 }
309
310 char* src_name;
311 char* dst_name;
312
313 if (ReadArgs(state, argv, 2, &src_name, &dst_name) < 0) {
314 return NULL;
315 }
316 if (strlen(src_name) == 0) {
317 ErrorAbort(state, "src_name argument to %s() can't be empty", name);
318 goto done;
319 }
320 if (strlen(dst_name) == 0) {
321 ErrorAbort(state, "dst_name argument to %s() can't be empty",
322 name);
323 goto done;
324 }
325
326 if (rename(src_name, dst_name) != 0) {
327 ErrorAbort(state, "Rename of %s() to %s() failed, error %s()",
328 src_name, dst_name, strerror(errno));
329 } else {
330 result = dst_name;
331 }
332
333done:
334 free(src_name);
335 if (result != dst_name) free(dst_name);
336 return StringValue(result);
337}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700338
Doug Zongker512536a2010-02-17 16:11:44 -0800339Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700340 char** paths = malloc(argc * sizeof(char*));
341 int i;
342 for (i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700343 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700344 if (paths[i] == NULL) {
345 int j;
346 for (j = 0; j < i; ++i) {
347 free(paths[j]);
348 }
349 free(paths);
350 return NULL;
351 }
352 }
353
354 bool recursive = (strcmp(name, "delete_recursive") == 0);
355
356 int success = 0;
357 for (i = 0; i < argc; ++i) {
358 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
359 ++success;
360 free(paths[i]);
361 }
362 free(paths);
363
364 char buffer[10];
365 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800366 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700367}
368
Doug Zongker8edb00c2009-06-11 17:21:44 -0700369
Doug Zongker512536a2010-02-17 16:11:44 -0800370Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700371 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700372 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700373 }
374 char* frac_str;
375 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700376 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700377 return NULL;
378 }
379
380 double frac = strtod(frac_str, NULL);
381 int sec = strtol(sec_str, NULL, 10);
382
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700383 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700384 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
385
Doug Zongker9931f7f2009-06-10 14:11:53 -0700386 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800387 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700388}
389
Doug Zongker512536a2010-02-17 16:11:44 -0800390Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700391 if (argc != 1) {
392 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
393 }
394 char* frac_str;
395 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
396 return NULL;
397 }
398
399 double frac = strtod(frac_str, NULL);
400
401 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
402 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
403
Doug Zongker512536a2010-02-17 16:11:44 -0800404 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700405}
406
Doug Zongker8edb00c2009-06-11 17:21:44 -0700407// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800408Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700409 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700410 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700411 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700412 }
413 char* zip_path;
414 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700415 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700416
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700417 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700418
419 // To create a consistent system image, never use the clock for timestamps.
420 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
421
422 bool success = mzExtractRecursive(za, zip_path, dest_path,
423 MZ_EXTRACT_FILES_ONLY, &timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500424 NULL, NULL, sehandle);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700425 free(zip_path);
426 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800427 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700428}
429
Doug Zongker8edb00c2009-06-11 17:21:44 -0700430
431// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800432// or
433// package_extract_file(package_path)
434// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800435// function (the char* returned is actually a FileContents*).
436Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700437 int argc, Expr* argv[]) {
Doug Zongker6aece332010-02-01 14:40:12 -0800438 if (argc != 1 && argc != 2) {
439 return ErrorAbort(state, "%s() expects 1 or 2 args, got %d",
440 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700441 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700442 bool success = false;
Doug Zongker6aece332010-02-01 14:40:12 -0800443 if (argc == 2) {
444 // The two-argument version extracts to a file.
Doug Zongker8edb00c2009-06-11 17:21:44 -0700445
Doug Zongker6aece332010-02-01 14:40:12 -0800446 char* zip_path;
447 char* dest_path;
448 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
449
450 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
451 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
452 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700453 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800454 goto done2;
455 }
456
457 FILE* f = fopen(dest_path, "wb");
458 if (f == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700459 printf("%s: can't open %s for write: %s\n",
Doug Zongker6aece332010-02-01 14:40:12 -0800460 name, dest_path, strerror(errno));
461 goto done2;
462 }
463 success = mzExtractZipEntryToFile(za, entry, fileno(f));
464 fclose(f);
465
466 done2:
467 free(zip_path);
468 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800469 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800470 } else {
471 // The one-argument version returns the contents of the file
472 // as the result.
473
474 char* zip_path;
Doug Zongker512536a2010-02-17 16:11:44 -0800475 Value* v = malloc(sizeof(Value));
476 v->type = VAL_BLOB;
477 v->size = -1;
478 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800479
480 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
481
482 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
483 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
484 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700485 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800486 goto done1;
487 }
488
Doug Zongker512536a2010-02-17 16:11:44 -0800489 v->size = mzGetZipEntryUncompLen(entry);
490 v->data = malloc(v->size);
491 if (v->data == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700492 printf("%s: failed to allocate %ld bytes for %s\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800493 name, (long)v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800494 goto done1;
495 }
496
Doug Zongker512536a2010-02-17 16:11:44 -0800497 success = mzExtractZipEntryToBuffer(za, entry,
498 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800499
500 done1:
501 free(zip_path);
502 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800503 free(v->data);
504 v->data = NULL;
505 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800506 }
Doug Zongker512536a2010-02-17 16:11:44 -0800507 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700508 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700509}
510
Doug Zongkera23075f2012-08-06 16:19:09 -0700511// Create all parent directories of name, if necessary.
512static int make_parents(char* name) {
513 char* p;
514 for (p = name + (strlen(name)-1); p > name; --p) {
515 if (*p != '/') continue;
516 *p = '\0';
517 if (make_parents(name) < 0) return -1;
518 int result = mkdir(name, 0700);
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700519 if (result == 0) printf("symlink(): created [%s]\n", name);
Doug Zongkera23075f2012-08-06 16:19:09 -0700520 *p = '/';
521 if (result == 0 || errno == EEXIST) {
522 // successfully created or already existed; we're done
523 return 0;
524 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700525 printf("failed to mkdir %s: %s\n", name, strerror(errno));
Doug Zongkera23075f2012-08-06 16:19:09 -0700526 return -1;
527 }
528 }
529 return 0;
530}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700531
Doug Zongker9931f7f2009-06-10 14:11:53 -0700532// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700533// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800534Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700535 if (argc == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700536 return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700537 }
538 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700539 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700540 if (target == NULL) return NULL;
541
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700542 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700543 if (srcs == NULL) {
544 free(target);
545 return NULL;
546 }
547
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700548 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700549 int i;
550 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700551 if (unlink(srcs[i]) < 0) {
552 if (errno != ENOENT) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700553 printf("%s: failed to remove %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700554 name, srcs[i], strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700555 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700556 }
557 }
Doug Zongkera23075f2012-08-06 16:19:09 -0700558 if (make_parents(srcs[i])) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700559 printf("%s: failed to symlink %s to %s: making parents failed\n",
Doug Zongkera23075f2012-08-06 16:19:09 -0700560 name, srcs[i], target);
561 ++bad;
562 }
Doug Zongker60babf82009-09-18 15:11:24 -0700563 if (symlink(target, srcs[i]) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700564 printf("%s: failed to symlink %s to %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700565 name, srcs[i], target, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700566 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700567 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700568 free(srcs[i]);
569 }
570 free(srcs);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700571 if (bad) {
572 return ErrorAbort(state, "%s: some symlinks failed", name);
573 }
Doug Zongker512536a2010-02-17 16:11:44 -0800574 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700575}
576
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700577struct perm_parsed_args {
578 bool has_uid;
579 uid_t uid;
580 bool has_gid;
581 gid_t gid;
582 bool has_mode;
583 mode_t mode;
584 bool has_fmode;
585 mode_t fmode;
586 bool has_dmode;
587 mode_t dmode;
588 bool has_selabel;
589 char* selabel;
590 bool has_capabilities;
591 uint64_t capabilities;
592};
593
594static struct perm_parsed_args ParsePermArgs(int argc, char** args) {
595 int i;
596 struct perm_parsed_args parsed;
597 int bad = 0;
598 static int max_warnings = 20;
599
600 memset(&parsed, 0, sizeof(parsed));
601
602 for (i = 1; i < argc; i += 2) {
603 if (strcmp("uid", args[i]) == 0) {
604 int64_t uid;
605 if (sscanf(args[i+1], "%" SCNd64, &uid) == 1) {
606 parsed.uid = uid;
607 parsed.has_uid = true;
608 } else {
609 printf("ParsePermArgs: invalid UID \"%s\"\n", args[i + 1]);
610 bad++;
611 }
612 continue;
613 }
614 if (strcmp("gid", args[i]) == 0) {
615 int64_t gid;
616 if (sscanf(args[i+1], "%" SCNd64, &gid) == 1) {
617 parsed.gid = gid;
618 parsed.has_gid = true;
619 } else {
620 printf("ParsePermArgs: invalid GID \"%s\"\n", args[i + 1]);
621 bad++;
622 }
623 continue;
624 }
625 if (strcmp("mode", args[i]) == 0) {
626 int32_t mode;
627 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
628 parsed.mode = mode;
629 parsed.has_mode = true;
630 } else {
631 printf("ParsePermArgs: invalid mode \"%s\"\n", args[i + 1]);
632 bad++;
633 }
634 continue;
635 }
636 if (strcmp("dmode", args[i]) == 0) {
637 int32_t mode;
638 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
639 parsed.dmode = mode;
640 parsed.has_dmode = true;
641 } else {
642 printf("ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1]);
643 bad++;
644 }
645 continue;
646 }
647 if (strcmp("fmode", args[i]) == 0) {
648 int32_t mode;
649 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
650 parsed.fmode = mode;
651 parsed.has_fmode = true;
652 } else {
653 printf("ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1]);
654 bad++;
655 }
656 continue;
657 }
658 if (strcmp("capabilities", args[i]) == 0) {
659 int64_t capabilities;
660 if (sscanf(args[i+1], "%" SCNi64, &capabilities) == 1) {
661 parsed.capabilities = capabilities;
662 parsed.has_capabilities = true;
663 } else {
664 printf("ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1]);
665 bad++;
666 }
667 continue;
668 }
669 if (strcmp("selabel", args[i]) == 0) {
670 if (args[i+1][0] != '\0') {
671 parsed.selabel = args[i+1];
672 parsed.has_selabel = true;
673 } else {
674 printf("ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1]);
675 bad++;
676 }
677 continue;
678 }
679 if (max_warnings != 0) {
680 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i]);
681 max_warnings--;
682 if (max_warnings == 0) {
683 printf("ParsedPermArgs: suppressing further warnings\n");
684 }
685 }
686 }
687 return parsed;
688}
689
690static int ApplyParsedPerms(
691 const char* filename,
692 const struct stat *statptr,
693 struct perm_parsed_args parsed)
694{
695 int bad = 0;
696
Nick Kraleviche4612512013-09-10 15:34:19 -0700697 /* ignore symlinks */
698 if (S_ISLNK(statptr->st_mode)) {
699 return 0;
700 }
701
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700702 if (parsed.has_uid) {
703 if (chown(filename, parsed.uid, -1) < 0) {
704 printf("ApplyParsedPerms: chown of %s to %d failed: %s\n",
705 filename, parsed.uid, strerror(errno));
706 bad++;
707 }
708 }
709
710 if (parsed.has_gid) {
711 if (chown(filename, -1, parsed.gid) < 0) {
712 printf("ApplyParsedPerms: chgrp of %s to %d failed: %s\n",
713 filename, parsed.gid, strerror(errno));
714 bad++;
715 }
716 }
717
718 if (parsed.has_mode) {
719 if (chmod(filename, parsed.mode) < 0) {
720 printf("ApplyParsedPerms: chmod of %s to %d failed: %s\n",
721 filename, parsed.mode, strerror(errno));
722 bad++;
723 }
724 }
725
726 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
727 if (chmod(filename, parsed.dmode) < 0) {
728 printf("ApplyParsedPerms: chmod of %s to %d failed: %s\n",
729 filename, parsed.dmode, strerror(errno));
730 bad++;
731 }
732 }
733
734 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
735 if (chmod(filename, parsed.fmode) < 0) {
736 printf("ApplyParsedPerms: chmod of %s to %d failed: %s\n",
737 filename, parsed.fmode, strerror(errno));
738 bad++;
739 }
740 }
741
742 if (parsed.has_selabel) {
743 // TODO: Don't silently ignore ENOTSUP
744 if (lsetfilecon(filename, parsed.selabel) && (errno != ENOTSUP)) {
745 printf("ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n",
746 filename, parsed.selabel, strerror(errno));
747 bad++;
748 }
749 }
750
751 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
752 if (parsed.capabilities == 0) {
753 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
754 // Report failure unless it's ENODATA (attribute not set)
755 printf("ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n",
756 filename, parsed.capabilities, strerror(errno));
757 bad++;
758 }
759 } else {
760 struct vfs_cap_data cap_data;
761 memset(&cap_data, 0, sizeof(cap_data));
762 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
763 cap_data.data[0].permitted = (uint32_t) (parsed.capabilities & 0xffffffff);
764 cap_data.data[0].inheritable = 0;
765 cap_data.data[1].permitted = (uint32_t) (parsed.capabilities >> 32);
766 cap_data.data[1].inheritable = 0;
767 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
768 printf("ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n",
769 filename, parsed.capabilities, strerror(errno));
770 bad++;
771 }
772 }
773 }
774
775 return bad;
776}
777
778// nftw doesn't allow us to pass along context, so we need to use
779// global variables. *sigh*
780static struct perm_parsed_args recursive_parsed_args;
781
782static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr,
783 int fileflags, struct FTW *pfwt) {
784 return ApplyParsedPerms(filename, statptr, recursive_parsed_args);
785}
786
787static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
788 int i;
789 int bad = 0;
790 static int nwarnings = 0;
791 struct stat sb;
792 Value* result = NULL;
793
794 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
795
796 if ((argc % 2) != 1) {
797 return ErrorAbort(state, "%s() expects an odd number of arguments, got %d",
798 name, argc);
799 }
800
801 char** args = ReadVarArgs(state, argc, argv);
802 if (args == NULL) return NULL;
803
804 if (lstat(args[0], &sb) == -1) {
805 result = ErrorAbort(state, "%s: Error on lstat of \"%s\": %s", name, args[0], strerror(errno));
806 goto done;
807 }
808
809 struct perm_parsed_args parsed = ParsePermArgs(argc, args);
810
811 if (recursive) {
812 recursive_parsed_args = parsed;
813 bad += nftw(args[0], do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
814 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
815 } else {
816 bad += ApplyParsedPerms(args[0], &sb, parsed);
817 }
818
819done:
820 for (i = 0; i < argc; ++i) {
821 free(args[i]);
822 }
823 free(args);
824
825 if (result != NULL) {
826 return result;
827 }
828
829 if (bad > 0) {
830 return ErrorAbort(state, "%s: some changes failed", name);
831 }
832
833 return StringValue(strdup(""));
834}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700835
Doug Zongker512536a2010-02-17 16:11:44 -0800836Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700837 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700838 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700839 }
840 char* key;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700841 key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700842 if (key == NULL) return NULL;
843
844 char value[PROPERTY_VALUE_MAX];
845 property_get(key, value, "");
846 free(key);
847
Doug Zongker512536a2010-02-17 16:11:44 -0800848 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700849}
850
851
Doug Zongker47cace92009-06-18 10:11:50 -0700852// file_getprop(file, key)
853//
854// interprets 'file' as a getprop-style file (key=value pairs, one
855// per line, # comment lines and blank lines okay), and returns the value
856// for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800857Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700858 char* result = NULL;
859 char* buffer = NULL;
860 char* filename;
861 char* key;
862 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
863 return NULL;
864 }
865
866 struct stat st;
867 if (stat(filename, &st) < 0) {
868 ErrorAbort(state, "%s: failed to stat \"%s\": %s",
869 name, filename, strerror(errno));
870 goto done;
871 }
872
873#define MAX_FILE_GETPROP_SIZE 65536
874
875 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
876 ErrorAbort(state, "%s too large for %s (max %d)",
877 filename, name, MAX_FILE_GETPROP_SIZE);
878 goto done;
879 }
880
881 buffer = malloc(st.st_size+1);
882 if (buffer == NULL) {
Doug Zongkera23075f2012-08-06 16:19:09 -0700883 ErrorAbort(state, "%s: failed to alloc %lld bytes", name, st.st_size+1);
Doug Zongker47cace92009-06-18 10:11:50 -0700884 goto done;
885 }
886
887 FILE* f = fopen(filename, "rb");
888 if (f == NULL) {
889 ErrorAbort(state, "%s: failed to open %s: %s",
890 name, filename, strerror(errno));
891 goto done;
892 }
893
894 if (fread(buffer, 1, st.st_size, f) != st.st_size) {
Doug Zongkera23075f2012-08-06 16:19:09 -0700895 ErrorAbort(state, "%s: failed to read %lld bytes from %s",
Doug Zongker47cace92009-06-18 10:11:50 -0700896 name, st.st_size+1, filename);
897 fclose(f);
898 goto done;
899 }
900 buffer[st.st_size] = '\0';
901
902 fclose(f);
903
904 char* line = strtok(buffer, "\n");
905 do {
906 // skip whitespace at start of line
907 while (*line && isspace(*line)) ++line;
908
909 // comment or blank line: skip to next line
910 if (*line == '\0' || *line == '#') continue;
911
912 char* equal = strchr(line, '=');
913 if (equal == NULL) {
914 ErrorAbort(state, "%s: malformed line \"%s\": %s not a prop file?",
915 name, line, filename);
916 goto done;
917 }
918
919 // trim whitespace between key and '='
920 char* key_end = equal-1;
921 while (key_end > line && isspace(*key_end)) --key_end;
922 key_end[1] = '\0';
923
924 // not the key we're looking for
925 if (strcmp(key, line) != 0) continue;
926
927 // skip whitespace after the '=' to the start of the value
928 char* val_start = equal+1;
929 while(*val_start && isspace(*val_start)) ++val_start;
930
931 // trim trailing whitespace
932 char* val_end = val_start + strlen(val_start)-1;
933 while (val_end > val_start && isspace(*val_end)) --val_end;
934 val_end[1] = '\0';
935
936 result = strdup(val_start);
937 break;
938
939 } while ((line = strtok(NULL, "\n")));
940
941 if (result == NULL) result = strdup("");
942
943 done:
944 free(filename);
945 free(key);
946 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -0800947 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -0700948}
949
950
Doug Zongker8edb00c2009-06-11 17:21:44 -0700951static bool write_raw_image_cb(const unsigned char* data,
952 int data_len, void* ctx) {
953 int r = mtd_write_data((MtdWriteContext*)ctx, (const char *)data, data_len);
954 if (r == data_len) return true;
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700955 printf("%s\n", strerror(errno));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700956 return false;
957}
958
Doug Zongker179b2d92011-04-12 15:49:04 -0700959// write_raw_image(filename_or_blob, partition)
Doug Zongker512536a2010-02-17 16:11:44 -0800960Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700961 char* result = NULL;
962
Doug Zongker179b2d92011-04-12 15:49:04 -0700963 Value* partition_value;
964 Value* contents;
965 if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700966 return NULL;
967 }
968
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700969 char* partition = NULL;
Doug Zongker179b2d92011-04-12 15:49:04 -0700970 if (partition_value->type != VAL_STRING) {
971 ErrorAbort(state, "partition argument to %s must be string", name);
972 goto done;
973 }
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700974 partition = partition_value->data;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700975 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700976 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700977 goto done;
978 }
Doug Zongker179b2d92011-04-12 15:49:04 -0700979 if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700980 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700981 goto done;
982 }
983
984 mtd_scan_partitions();
985 const MtdPartition* mtd = mtd_find_partition_by_name(partition);
986 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700987 printf("%s: no mtd partition named \"%s\"\n", name, partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700988 result = strdup("");
989 goto done;
990 }
991
992 MtdWriteContext* ctx = mtd_write_partition(mtd);
993 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700994 printf("%s: can't write mtd partition \"%s\"\n",
Doug Zongker8edb00c2009-06-11 17:21:44 -0700995 name, partition);
996 result = strdup("");
997 goto done;
998 }
999
1000 bool success;
1001
Doug Zongker179b2d92011-04-12 15:49:04 -07001002 if (contents->type == VAL_STRING) {
1003 // we're given a filename as the contents
1004 char* filename = contents->data;
1005 FILE* f = fopen(filename, "rb");
1006 if (f == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001007 printf("%s: can't open %s: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -07001008 name, filename, strerror(errno));
1009 result = strdup("");
1010 goto done;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001011 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001012
1013 success = true;
1014 char* buffer = malloc(BUFSIZ);
1015 int read;
1016 while (success && (read = fread(buffer, 1, BUFSIZ, f)) > 0) {
1017 int wrote = mtd_write_data(ctx, buffer, read);
1018 success = success && (wrote == read);
1019 }
1020 free(buffer);
1021 fclose(f);
1022 } else {
1023 // we're given a blob as the contents
1024 ssize_t wrote = mtd_write_data(ctx, contents->data, contents->size);
1025 success = (wrote == contents->size);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001026 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001027 if (!success) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001028 printf("mtd_write_data to %s failed: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -07001029 partition, strerror(errno));
1030 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001031
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001032 if (mtd_erase_blocks(ctx, -1) == -1) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001033 printf("%s: error erasing blocks of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001034 }
1035 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001036 printf("%s: error closing write of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001037 }
1038
Doug Zongker179b2d92011-04-12 15:49:04 -07001039 printf("%s %s partition\n",
1040 success ? "wrote" : "failed to write", partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001041
1042 result = success ? partition : strdup("");
1043
1044done:
Doug Zongker179b2d92011-04-12 15:49:04 -07001045 if (result != partition) FreeValue(partition_value);
1046 FreeValue(contents);
Doug Zongker512536a2010-02-17 16:11:44 -08001047 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001048}
1049
Doug Zongker8edb00c2009-06-11 17:21:44 -07001050// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001051Value* ApplyPatchSpaceFn(const char* name, State* state,
1052 int argc, Expr* argv[]) {
1053 char* bytes_str;
1054 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
1055 return NULL;
1056 }
1057
1058 char* endptr;
1059 size_t bytes = strtol(bytes_str, &endptr, 10);
1060 if (bytes == 0 && endptr == bytes_str) {
1061 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n",
1062 name, bytes_str);
1063 free(bytes_str);
1064 return NULL;
1065 }
1066
1067 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
1068}
1069
Doug Zongker52b40362014-02-10 15:30:30 -08001070// syspatch(file, size, tgt_sha1, init_sha1, patch)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001071
Doug Zongker52b40362014-02-10 15:30:30 -08001072Value* SysPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
1073 if (argc != 5) {
1074 return ErrorAbort(state, "%s(): expected 5 args, got %d", name, argc);
1075 }
1076
1077 char* filename;
1078 char* filename_size_str;
1079 char* target_sha1;
1080 char* init_sha1;
1081 char* patch_filename;
1082 uint8_t target_digest[SHA_DIGEST_SIZE];
1083 uint8_t init_digest[SHA_DIGEST_SIZE];
1084
1085 if (ReadArgs(state, argv, 5, &filename, &filename_size_str,
1086 &target_sha1, &init_sha1, &patch_filename) < 0) {
1087 return NULL;
1088 }
1089
1090 if (ParseSha1(target_sha1, target_digest) != 0) {
1091 printf("%s(): failed to parse '%s' as target SHA-1", name, target_sha1);
1092 memset(target_digest, 0, SHA_DIGEST_SIZE);
1093 }
1094 if (ParseSha1(init_sha1, init_digest) != 0) {
1095 printf("%s(): failed to parse '%s' as init SHA-1", name, init_sha1);
1096 memset(init_digest, 0, SHA_DIGEST_SIZE);
1097 }
1098
1099 size_t len = strtoull(filename_size_str, NULL, 0);
1100
1101 SHA_CTX ctx;
1102 SHA_init(&ctx);
1103 FILE* src = fopen(filename, "r");
1104 size_t pos = 0;
1105 unsigned char buffer[4096];
1106 while (pos < len) {
1107 size_t to_read = len - pos;
1108 if (to_read > sizeof(buffer)) to_read = sizeof(buffer);
1109 size_t read = fread(buffer, 1, to_read, src);
1110 if (read <= 0) {
1111 printf("%s(): short read after %zu bytes\n", name, pos);
1112 break;
1113 }
1114 SHA_update(&ctx, buffer, read);
1115 pos += read;
1116 }
1117 rewind(src);
Doug Zongker0d32f252014-02-13 15:07:56 -08001118 const uint8_t* digest = SHA_final(&ctx);
Doug Zongker52b40362014-02-10 15:30:30 -08001119
Doug Zongker0d32f252014-02-13 15:07:56 -08001120 const char* hexdigest = PrintSha1(digest);
Doug Zongker52b40362014-02-10 15:30:30 -08001121 printf(" system partition sha1 = %s\n", hexdigest);
1122
1123 if (memcmp(digest, target_digest, SHA_DIGEST_SIZE) == 0) {
1124 printf("%s(): %s is already target\n", name, filename);
1125 fclose(src);
1126 goto done;
1127 }
1128
1129 if (memcmp(digest, init_digest, SHA_DIGEST_SIZE) != 0) {
1130 return ErrorAbort(state, "%s(): %s in unknown state\n", name, filename);
1131 }
1132
1133 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
1134
1135 const ZipEntry* entry = mzFindZipEntry(za, patch_filename);
1136 if (entry == NULL) {
1137 return ErrorAbort(state, "%s(): no %s in package\n", name, patch_filename);
1138 }
1139
1140 unsigned char* patch_data;
1141 size_t patch_len;
1142 if (!mzGetStoredEntry(za, entry, &patch_data, &patch_len)) {
1143 return ErrorAbort(state, "%s(): failed to get %s entry\n", name, patch_filename);
1144 }
1145
1146 FILE* tgt = fopen(filename, "r+");
1147
1148 int ret = syspatch(src, patch_data, patch_len, tgt);
1149
1150 fclose(src);
1151 fclose(tgt);
1152
1153 if (ret != 0) {
1154 return ErrorAbort(state, "%s(): patching failed\n", name);
1155 }
1156
1157 done:
1158 free(filename_size_str);
1159 free(target_sha1);
1160 free(init_sha1);
1161 free(patch_filename);
1162 return StringValue(filename);
1163
1164}
1165
1166// apply_patch(file, size, init_sha1, tgt_sha1, patch)
1167
Doug Zongker512536a2010-02-17 16:11:44 -08001168Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001169 if (argc < 6 || (argc % 2) == 1) {
1170 return ErrorAbort(state, "%s(): expected at least 6 args and an "
1171 "even number, got %d",
1172 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001173 }
1174
Doug Zongkerc4351c72010-02-22 14:46:32 -08001175 char* source_filename;
1176 char* target_filename;
1177 char* target_sha1;
1178 char* target_size_str;
1179 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
1180 &target_sha1, &target_size_str) < 0) {
1181 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001182 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001183
Doug Zongkerc4351c72010-02-22 14:46:32 -08001184 char* endptr;
1185 size_t target_size = strtol(target_size_str, &endptr, 10);
1186 if (target_size == 0 && endptr == target_size_str) {
1187 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count",
1188 name, target_size_str);
1189 free(source_filename);
1190 free(target_filename);
1191 free(target_sha1);
1192 free(target_size_str);
1193 return NULL;
1194 }
1195
1196 int patchcount = (argc-4) / 2;
1197 Value** patches = ReadValueVarArgs(state, argc-4, argv+4);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001198
1199 int i;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001200 for (i = 0; i < patchcount; ++i) {
1201 if (patches[i*2]->type != VAL_STRING) {
1202 ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i);
1203 break;
1204 }
1205 if (patches[i*2+1]->type != VAL_BLOB) {
1206 ErrorAbort(state, "%s(): patch #%d is not blob", name, i);
1207 break;
1208 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001209 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001210 if (i != patchcount) {
1211 for (i = 0; i < patchcount*2; ++i) {
1212 FreeValue(patches[i]);
1213 }
1214 free(patches);
1215 return NULL;
1216 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001217
Doug Zongkerc4351c72010-02-22 14:46:32 -08001218 char** patch_sha_str = malloc(patchcount * sizeof(char*));
1219 for (i = 0; i < patchcount; ++i) {
1220 patch_sha_str[i] = patches[i*2]->data;
1221 patches[i*2]->data = NULL;
1222 FreeValue(patches[i*2]);
1223 patches[i] = patches[i*2+1];
Doug Zongker8edb00c2009-06-11 17:21:44 -07001224 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001225
1226 int result = applypatch(source_filename, target_filename,
1227 target_sha1, target_size,
Doug Zongkera3ccba62012-08-20 15:28:02 -07001228 patchcount, patch_sha_str, patches, NULL);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001229
1230 for (i = 0; i < patchcount; ++i) {
1231 FreeValue(patches[i]);
1232 }
1233 free(patch_sha_str);
1234 free(patches);
1235
1236 return StringValue(strdup(result == 0 ? "t" : ""));
1237}
1238
1239// apply_patch_check(file, [sha1_1, ...])
1240Value* ApplyPatchCheckFn(const char* name, State* state,
1241 int argc, Expr* argv[]) {
1242 if (argc < 1) {
1243 return ErrorAbort(state, "%s(): expected at least 1 arg, got %d",
1244 name, argc);
1245 }
1246
1247 char* filename;
1248 if (ReadArgs(state, argv, 1, &filename) < 0) {
1249 return NULL;
1250 }
1251
1252 int patchcount = argc-1;
1253 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
1254
1255 int result = applypatch_check(filename, patchcount, sha1s);
1256
1257 int i;
1258 for (i = 0; i < patchcount; ++i) {
1259 free(sha1s[i]);
1260 }
1261 free(sha1s);
1262
1263 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001264}
1265
Doug Zongker512536a2010-02-17 16:11:44 -08001266Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001267 char** args = ReadVarArgs(state, argc, argv);
1268 if (args == NULL) {
1269 return NULL;
1270 }
1271
1272 int size = 0;
1273 int i;
1274 for (i = 0; i < argc; ++i) {
1275 size += strlen(args[i]);
1276 }
1277 char* buffer = malloc(size+1);
1278 size = 0;
1279 for (i = 0; i < argc; ++i) {
1280 strcpy(buffer+size, args[i]);
1281 size += strlen(args[i]);
1282 free(args[i]);
1283 }
1284 free(args);
1285 buffer[size] = '\0';
1286
1287 char* line = strtok(buffer, "\n");
1288 while (line) {
1289 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe,
1290 "ui_print %s\n", line);
1291 line = strtok(NULL, "\n");
1292 }
1293 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "ui_print\n");
1294
Doug Zongker512536a2010-02-17 16:11:44 -08001295 return StringValue(buffer);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001296}
1297
Doug Zongkerd0181b82011-10-19 10:51:12 -07001298Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
1299 if (argc != 0) {
1300 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1301 }
1302 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
1303 return StringValue(strdup("t"));
1304}
1305
Doug Zongker512536a2010-02-17 16:11:44 -08001306Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001307 if (argc < 1) {
1308 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1309 }
1310 char** args = ReadVarArgs(state, argc, argv);
1311 if (args == NULL) {
1312 return NULL;
1313 }
1314
1315 char** args2 = malloc(sizeof(char*) * (argc+1));
1316 memcpy(args2, args, sizeof(char*) * argc);
1317 args2[argc] = NULL;
1318
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001319 printf("about to run program [%s] with %d args\n", args2[0], argc);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001320
1321 pid_t child = fork();
1322 if (child == 0) {
1323 execv(args2[0], args2);
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001324 printf("run_program: execv failed: %s\n", strerror(errno));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001325 _exit(1);
1326 }
1327 int status;
1328 waitpid(child, &status, 0);
1329 if (WIFEXITED(status)) {
1330 if (WEXITSTATUS(status) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001331 printf("run_program: child exited with status %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001332 WEXITSTATUS(status));
1333 }
1334 } else if (WIFSIGNALED(status)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001335 printf("run_program: child terminated by signal %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001336 WTERMSIG(status));
1337 }
1338
1339 int i;
1340 for (i = 0; i < argc; ++i) {
1341 free(args[i]);
1342 }
1343 free(args);
1344 free(args2);
1345
1346 char buffer[20];
1347 sprintf(buffer, "%d", status);
1348
Doug Zongker512536a2010-02-17 16:11:44 -08001349 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001350}
1351
Doug Zongker512536a2010-02-17 16:11:44 -08001352// sha1_check(data)
1353// to return the sha1 of the data (given in the format returned by
1354// read_file).
1355//
1356// sha1_check(data, sha1_hex, [sha1_hex, ...])
1357// returns the sha1 of the file if it matches any of the hex
1358// strings passed, or "" if it does not equal any of them.
1359//
1360Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1361 if (argc < 1) {
1362 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1363 }
1364
1365 Value** args = ReadValueVarArgs(state, argc, argv);
1366 if (args == NULL) {
1367 return NULL;
1368 }
1369
1370 if (args[0]->size < 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001371 return StringValue(strdup(""));
1372 }
1373 uint8_t digest[SHA_DIGEST_SIZE];
Doug Zongkerbac7fba2013-04-10 11:32:17 -07001374 SHA_hash(args[0]->data, args[0]->size, digest);
Doug Zongker512536a2010-02-17 16:11:44 -08001375 FreeValue(args[0]);
1376
1377 if (argc == 1) {
1378 return StringValue(PrintSha1(digest));
1379 }
1380
1381 int i;
1382 uint8_t* arg_digest = malloc(SHA_DIGEST_SIZE);
1383 for (i = 1; i < argc; ++i) {
1384 if (args[i]->type != VAL_STRING) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001385 printf("%s(): arg %d is not a string; skipping",
Doug Zongker512536a2010-02-17 16:11:44 -08001386 name, i);
1387 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1388 // Warn about bad args and skip them.
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001389 printf("%s(): error parsing \"%s\" as sha-1; skipping",
1390 name, args[i]->data);
Doug Zongker512536a2010-02-17 16:11:44 -08001391 } else if (memcmp(digest, arg_digest, SHA_DIGEST_SIZE) == 0) {
1392 break;
1393 }
1394 FreeValue(args[i]);
1395 }
1396 if (i >= argc) {
1397 // Didn't match any of the hex strings; return false.
1398 return StringValue(strdup(""));
1399 }
1400 // Found a match; free all the remaining arguments and return the
1401 // matched one.
1402 int j;
1403 for (j = i+1; j < argc; ++j) {
1404 FreeValue(args[j]);
1405 }
1406 return args[i];
1407}
1408
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001409// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001410// is actually a FileContents*).
1411Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1412 if (argc != 1) {
1413 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1414 }
1415 char* filename;
1416 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1417
1418 Value* v = malloc(sizeof(Value));
1419 v->type = VAL_BLOB;
1420
1421 FileContents fc;
Doug Zongkera1bc1482014-02-13 15:18:19 -08001422 if (LoadFileContents(filename, &fc) != 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001423 free(filename);
Michael Runge6eed2242013-12-13 17:13:11 -08001424 v->size = -1;
1425 v->data = NULL;
Doug Zongker512536a2010-02-17 16:11:44 -08001426 free(fc.data);
Michael Runge6eed2242013-12-13 17:13:11 -08001427 return v;
Doug Zongker512536a2010-02-17 16:11:44 -08001428 }
1429
1430 v->size = fc.size;
1431 v->data = (char*)fc.data;
1432
1433 free(filename);
1434 return v;
1435}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001436
Doug Zongkerc87bab12013-11-25 13:53:25 -08001437// Immediately reboot the device. Recovery is not finished normally,
1438// so if you reboot into recovery it will re-start applying the
1439// current package (because nothing has cleared the copy of the
1440// arguments stored in the BCB).
1441//
1442// The argument is the partition name passed to the android reboot
1443// property. It can be "recovery" to boot from the recovery
1444// partition, or "" (empty string) to boot from the regular boot
1445// partition.
1446Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) {
1447 if (argc != 2) {
1448 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1449 }
1450
1451 char* filename;
1452 char* property;
1453 if (ReadArgs(state, argv, 2, &filename, &property) < 0) return NULL;
1454
1455 char buffer[80];
1456
1457 // zero out the 'command' field of the bootloader message.
1458 memset(buffer, 0, sizeof(((struct bootloader_message*)0)->command));
1459 FILE* f = fopen(filename, "r+b");
1460 fseek(f, offsetof(struct bootloader_message, command), SEEK_SET);
1461 fwrite(buffer, sizeof(((struct bootloader_message*)0)->command), 1, f);
1462 fclose(f);
1463 free(filename);
1464
1465 strcpy(buffer, "reboot,");
1466 if (property != NULL) {
1467 strncat(buffer, property, sizeof(buffer)-10);
1468 }
1469
1470 property_set(ANDROID_RB_PROPERTY, buffer);
1471
1472 sleep(5);
1473 free(property);
1474 ErrorAbort(state, "%s() failed to reboot", name);
1475 return NULL;
1476}
1477
1478// Store a string value somewhere that future invocations of recovery
1479// can access it. This value is called the "stage" and can be used to
1480// drive packages that need to do reboots in the middle of
1481// installation and keep track of where they are in the multi-stage
1482// install.
1483//
1484// The first argument is the block device for the misc partition
1485// ("/misc" in the fstab), which is where this value is stored. The
1486// second argument is the string to store; it should not exceed 31
1487// bytes.
1488Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
1489 if (argc != 2) {
1490 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1491 }
1492
1493 char* filename;
1494 char* stagestr;
1495 if (ReadArgs(state, argv, 2, &filename, &stagestr) < 0) return NULL;
1496
1497 // Store this value in the misc partition, immediately after the
1498 // bootloader message that the main recovery uses to save its
1499 // arguments in case of the device restarting midway through
1500 // package installation.
1501 FILE* f = fopen(filename, "r+b");
1502 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1503 int to_write = strlen(stagestr)+1;
1504 int max_size = sizeof(((struct bootloader_message*)0)->stage);
1505 if (to_write > max_size) {
1506 to_write = max_size;
1507 stagestr[max_size-1] = 0;
1508 }
1509 fwrite(stagestr, to_write, 1, f);
1510 fclose(f);
1511
1512 free(stagestr);
1513 return StringValue(filename);
1514}
1515
1516// Return the value most recently saved with SetStageFn. The argument
1517// is the block device for the misc partition.
1518Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
1519 if (argc != 2) {
1520 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1521 }
1522
1523 char* filename;
1524 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1525
1526 char buffer[sizeof(((struct bootloader_message*)0)->stage)];
1527 FILE* f = fopen(filename, "rb");
1528 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1529 fread(buffer, sizeof(buffer), 1, f);
1530 fclose(f);
1531 buffer[sizeof(buffer)-1] = '\0';
1532
1533 return StringValue(strdup(buffer));
1534}
1535
Doug Zongker9931f7f2009-06-10 14:11:53 -07001536void RegisterInstallFunctions() {
1537 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001538 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001539 RegisterFunction("unmount", UnmountFn);
1540 RegisterFunction("format", FormatFn);
1541 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001542 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001543 RegisterFunction("delete", DeleteFn);
1544 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001545 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1546 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001547 RegisterFunction("symlink", SymlinkFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001548
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001549 // Usage:
1550 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1551 // Example:
1552 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1553 RegisterFunction("set_metadata", SetMetadataFn);
1554
1555 // Usage:
1556 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1557 // Example:
1558 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1559 RegisterFunction("set_metadata_recursive", SetMetadataFn);
1560
Doug Zongker8edb00c2009-06-11 17:21:44 -07001561 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001562 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001563 RegisterFunction("write_raw_image", WriteRawImageFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001564
1565 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001566 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1567 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001568
Doug Zongker52b40362014-02-10 15:30:30 -08001569 RegisterFunction("syspatch", SysPatchFn);
1570
Doug Zongker512536a2010-02-17 16:11:44 -08001571 RegisterFunction("read_file", ReadFileFn);
1572 RegisterFunction("sha1_check", Sha1CheckFn);
Michael Rungece7ca712013-11-06 17:42:20 -08001573 RegisterFunction("rename", RenameFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001574
Doug Zongkerd0181b82011-10-19 10:51:12 -07001575 RegisterFunction("wipe_cache", WipeCacheFn);
1576
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001577 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001578
1579 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001580
1581 RegisterFunction("reboot_now", RebootNowFn);
1582 RegisterFunction("get_stage", GetStageFn);
1583 RegisterFunction("set_stage", SetStageFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001584}