blob: a59c4edac8aad9abe7c4dea66ae47bbe2a8fd561 [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>
Doug Zongker9931f7f2009-06-10 14:11:53 -070030
Doug Zongker8edb00c2009-06-11 17:21:44 -070031#include "cutils/misc.h"
32#include "cutils/properties.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070033#include "edify/expr.h"
Doug Zongker512536a2010-02-17 16:11:44 -080034#include "mincrypt/sha.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070035#include "minzip/DirUtil.h"
Hristo Bojinovdb314d62010-08-02 10:29:49 -070036#include "minelf/Retouch.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070037#include "mtdutils/mounts.h"
38#include "mtdutils/mtdutils.h"
39#include "updater.h"
Doug Zongker512536a2010-02-17 16:11:44 -080040#include "applypatch/applypatch.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070041
Doug Zongker3d177d02010-07-01 09:18:44 -070042#ifdef USE_EXT4
43#include "make_ext4fs.h"
44#endif
45
46// mount(fs_type, partition_type, location, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -070047//
Doug Zongker3d177d02010-07-01 09:18:44 -070048// fs_type="yaffs2" partition_type="MTD" location=partition
49// fs_type="ext4" partition_type="EMMC" location=device
Doug Zongker512536a2010-02-17 16:11:44 -080050Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070051 char* result = NULL;
Doug Zongker3d177d02010-07-01 09:18:44 -070052 if (argc != 4) {
53 return ErrorAbort(state, "%s() expects 4 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -070054 }
Doug Zongker3d177d02010-07-01 09:18:44 -070055 char* fs_type;
56 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -070057 char* location;
58 char* mount_point;
Doug Zongker3d177d02010-07-01 09:18:44 -070059 if (ReadArgs(state, argv, 4, &fs_type, &partition_type,
60 &location, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070061 return NULL;
62 }
63
Doug Zongker3d177d02010-07-01 09:18:44 -070064 if (strlen(fs_type) == 0) {
65 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
66 goto done;
67 }
68 if (strlen(partition_type) == 0) {
69 ErrorAbort(state, "partition_type argument to %s() can't be empty",
70 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -070071 goto done;
72 }
73 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070074 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -070075 goto done;
76 }
77 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070078 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -070079 goto done;
80 }
81
Stephen Smalley779701d2012-02-09 14:13:23 -050082#ifdef HAVE_SELINUX
83 char *secontext = NULL;
84
85 if (sehandle) {
86 selabel_lookup(sehandle, &secontext, mount_point, 0755);
87 setfscreatecon(secontext);
88 }
89#endif
90
Doug Zongker9931f7f2009-06-10 14:11:53 -070091 mkdir(mount_point, 0755);
92
Stephen Smalley779701d2012-02-09 14:13:23 -050093#ifdef HAVE_SELINUX
94 if (secontext) {
95 freecon(secontext);
96 setfscreatecon(NULL);
97 }
98#endif
99
Doug Zongker3d177d02010-07-01 09:18:44 -0700100 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700101 mtd_scan_partitions();
102 const MtdPartition* mtd;
103 mtd = mtd_find_partition_by_name(location);
104 if (mtd == NULL) {
105 fprintf(stderr, "%s: no mtd partition named \"%s\"",
106 name, location);
107 result = strdup("");
108 goto done;
109 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700110 if (mtd_mount_partition(mtd, mount_point, fs_type, 0 /* rw */) != 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700111 fprintf(stderr, "mtd mount of %s failed: %s\n",
112 location, strerror(errno));
113 result = strdup("");
114 goto done;
115 }
116 result = mount_point;
117 } else {
Doug Zongker3d177d02010-07-01 09:18:44 -0700118 if (mount(location, mount_point, fs_type,
Doug Zongker9931f7f2009-06-10 14:11:53 -0700119 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "") < 0) {
Doug Zongker60babf82009-09-18 15:11:24 -0700120 fprintf(stderr, "%s: failed to mount %s at %s: %s\n",
121 name, location, mount_point, strerror(errno));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700122 result = strdup("");
123 } else {
124 result = mount_point;
125 }
126 }
127
128done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700129 free(fs_type);
130 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700131 free(location);
132 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800133 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700134}
135
Doug Zongker8edb00c2009-06-11 17:21:44 -0700136
137// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800138Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700139 char* result = NULL;
140 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700141 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700142 }
143 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700144 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700145 return NULL;
146 }
147 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700148 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700149 goto done;
150 }
151
152 scan_mounted_volumes();
153 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
154 if (vol == NULL) {
155 result = strdup("");
156 } else {
157 result = mount_point;
158 }
159
160done:
161 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800162 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700163}
164
165
Doug Zongker512536a2010-02-17 16:11:44 -0800166Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700167 char* result = NULL;
168 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700169 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700170 }
171 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700172 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700173 return NULL;
174 }
175 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700176 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700177 goto done;
178 }
179
180 scan_mounted_volumes();
181 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
182 if (vol == NULL) {
183 fprintf(stderr, "unmount of %s failed; no such volume\n", mount_point);
184 result = strdup("");
185 } else {
186 unmount_mounted_volume(vol);
187 result = mount_point;
188 }
189
190done:
191 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800192 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700193}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700194
195
Stephen Smalley779701d2012-02-09 14:13:23 -0500196// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700197//
Stephen Smalley779701d2012-02-09 14:13:23 -0500198// fs_type="yaffs2" partition_type="MTD" location=partition fs_size=<bytes> mount_point=<location>
199// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800200// if fs_size == 0, then make_ext4fs uses the entire partition.
201// if fs_size > 0, that is the size to use
202// if fs_size < 0, then reserve that many bytes at the end of the partition
Stephen Smalley779701d2012-02-09 14:13:23 -0500203// mount_point is used with SELinux as the location of the mount point, absent otherwise
Doug Zongker512536a2010-02-17 16:11:44 -0800204Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700205 char* result = NULL;
Stephen Smalley779701d2012-02-09 14:13:23 -0500206 if (argc != 4 && argc != 5) {
207 return ErrorAbort(state, "%s() expects 4 or 5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700208 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700209 char* fs_type;
210 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700211 char* location;
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800212 char* fs_size;
Stephen Smalley779701d2012-02-09 14:13:23 -0500213 char* mount_point = NULL;
214
215#ifdef HAVE_SELINUX
216 if (ReadArgs(state, argv, 5, &fs_type, &partition_type, &location, &fs_size, &mount_point) < 0) {
217 return NULL;
218 }
219#else
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800220 if (ReadArgs(state, argv, 4, &fs_type, &partition_type, &location, &fs_size) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700221 return NULL;
222 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500223#endif
Doug Zongker9931f7f2009-06-10 14:11:53 -0700224
Doug Zongker3d177d02010-07-01 09:18:44 -0700225 if (strlen(fs_type) == 0) {
226 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
227 goto done;
228 }
229 if (strlen(partition_type) == 0) {
230 ErrorAbort(state, "partition_type argument to %s() can't be empty",
231 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700232 goto done;
233 }
234 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700235 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700236 goto done;
237 }
238
Stephen Smalley779701d2012-02-09 14:13:23 -0500239#ifdef HAVE_SELINUX
240 if (!mount_point || strlen(mount_point) == 0) {
241 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
242 goto done;
243 }
244#endif
245
Doug Zongker3d177d02010-07-01 09:18:44 -0700246 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700247 mtd_scan_partitions();
248 const MtdPartition* mtd = mtd_find_partition_by_name(location);
249 if (mtd == NULL) {
250 fprintf(stderr, "%s: no mtd partition named \"%s\"",
251 name, location);
252 result = strdup("");
253 goto done;
254 }
255 MtdWriteContext* ctx = mtd_write_partition(mtd);
256 if (ctx == NULL) {
257 fprintf(stderr, "%s: can't write \"%s\"", name, location);
258 result = strdup("");
259 goto done;
260 }
261 if (mtd_erase_blocks(ctx, -1) == -1) {
262 mtd_write_close(ctx);
263 fprintf(stderr, "%s: failed to erase \"%s\"", name, location);
264 result = strdup("");
265 goto done;
266 }
267 if (mtd_write_close(ctx) != 0) {
268 fprintf(stderr, "%s: failed to close \"%s\"", name, location);
269 result = strdup("");
270 goto done;
271 }
272 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700273#ifdef USE_EXT4
274 } else if (strcmp(fs_type, "ext4") == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500275 int status = make_ext4fs(location, atoll(fs_size), mount_point, sehandle);
Doug Zongker3d177d02010-07-01 09:18:44 -0700276 if (status != 0) {
277 fprintf(stderr, "%s: make_ext4fs failed (%d) on %s",
278 name, status, location);
279 result = strdup("");
280 goto done;
281 }
282 result = location;
283#endif
Doug Zongker9931f7f2009-06-10 14:11:53 -0700284 } else {
Doug Zongker3d177d02010-07-01 09:18:44 -0700285 fprintf(stderr, "%s: unsupported fs_type \"%s\" partition_type \"%s\"",
286 name, fs_type, partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700287 }
288
289done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700290 free(fs_type);
291 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700292 if (result != location) free(location);
Doug Zongker512536a2010-02-17 16:11:44 -0800293 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700294}
295
Doug Zongker8edb00c2009-06-11 17:21:44 -0700296
Doug Zongker512536a2010-02-17 16:11:44 -0800297Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700298 char** paths = malloc(argc * sizeof(char*));
299 int i;
300 for (i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700301 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700302 if (paths[i] == NULL) {
303 int j;
304 for (j = 0; j < i; ++i) {
305 free(paths[j]);
306 }
307 free(paths);
308 return NULL;
309 }
310 }
311
312 bool recursive = (strcmp(name, "delete_recursive") == 0);
313
314 int success = 0;
315 for (i = 0; i < argc; ++i) {
316 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
317 ++success;
318 free(paths[i]);
319 }
320 free(paths);
321
322 char buffer[10];
323 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800324 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700325}
326
Doug Zongker8edb00c2009-06-11 17:21:44 -0700327
Doug Zongker512536a2010-02-17 16:11:44 -0800328Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700329 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700330 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700331 }
332 char* frac_str;
333 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700334 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700335 return NULL;
336 }
337
338 double frac = strtod(frac_str, NULL);
339 int sec = strtol(sec_str, NULL, 10);
340
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700341 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700342 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
343
Doug Zongker9931f7f2009-06-10 14:11:53 -0700344 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800345 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700346}
347
Doug Zongker512536a2010-02-17 16:11:44 -0800348Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700349 if (argc != 1) {
350 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
351 }
352 char* frac_str;
353 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
354 return NULL;
355 }
356
357 double frac = strtod(frac_str, NULL);
358
359 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
360 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
361
Doug Zongker512536a2010-02-17 16:11:44 -0800362 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700363}
364
Doug Zongker8edb00c2009-06-11 17:21:44 -0700365// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800366Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700367 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700368 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700369 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700370 }
371 char* zip_path;
372 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700373 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700374
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700375 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700376
377 // To create a consistent system image, never use the clock for timestamps.
378 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
379
380 bool success = mzExtractRecursive(za, zip_path, dest_path,
381 MZ_EXTRACT_FILES_ONLY, &timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500382 NULL, NULL, sehandle);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700383 free(zip_path);
384 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800385 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700386}
387
Doug Zongker8edb00c2009-06-11 17:21:44 -0700388
389// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800390// or
391// package_extract_file(package_path)
392// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800393// function (the char* returned is actually a FileContents*).
394Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700395 int argc, Expr* argv[]) {
Doug Zongker6aece332010-02-01 14:40:12 -0800396 if (argc != 1 && argc != 2) {
397 return ErrorAbort(state, "%s() expects 1 or 2 args, got %d",
398 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700399 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700400 bool success = false;
Doug Zongker6aece332010-02-01 14:40:12 -0800401 if (argc == 2) {
402 // The two-argument version extracts to a file.
Doug Zongker8edb00c2009-06-11 17:21:44 -0700403
Doug Zongker6aece332010-02-01 14:40:12 -0800404 char* zip_path;
405 char* dest_path;
406 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
407
408 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
409 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
410 if (entry == NULL) {
411 fprintf(stderr, "%s: no %s in package\n", name, zip_path);
412 goto done2;
413 }
414
415 FILE* f = fopen(dest_path, "wb");
416 if (f == NULL) {
417 fprintf(stderr, "%s: can't open %s for write: %s\n",
418 name, dest_path, strerror(errno));
419 goto done2;
420 }
421 success = mzExtractZipEntryToFile(za, entry, fileno(f));
422 fclose(f);
423
424 done2:
425 free(zip_path);
426 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800427 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800428 } else {
429 // The one-argument version returns the contents of the file
430 // as the result.
431
432 char* zip_path;
Doug Zongker512536a2010-02-17 16:11:44 -0800433 Value* v = malloc(sizeof(Value));
434 v->type = VAL_BLOB;
435 v->size = -1;
436 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800437
438 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
439
440 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
441 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
442 if (entry == NULL) {
443 fprintf(stderr, "%s: no %s in package\n", name, zip_path);
444 goto done1;
445 }
446
Doug Zongker512536a2010-02-17 16:11:44 -0800447 v->size = mzGetZipEntryUncompLen(entry);
448 v->data = malloc(v->size);
449 if (v->data == NULL) {
Doug Zongker6aece332010-02-01 14:40:12 -0800450 fprintf(stderr, "%s: failed to allocate %ld bytes for %s\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800451 name, (long)v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800452 goto done1;
453 }
454
Doug Zongker512536a2010-02-17 16:11:44 -0800455 success = mzExtractZipEntryToBuffer(za, entry,
456 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800457
458 done1:
459 free(zip_path);
460 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800461 free(v->data);
462 v->data = NULL;
463 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800464 }
Doug Zongker512536a2010-02-17 16:11:44 -0800465 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700466 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700467}
468
469
Hristo Bojinovdb314d62010-08-02 10:29:49 -0700470// retouch_binaries(lib1, lib2, ...)
471Value* RetouchBinariesFn(const char* name, State* state,
472 int argc, Expr* argv[]) {
473 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
474
475 char **retouch_entries = ReadVarArgs(state, argc, argv);
476 if (retouch_entries == NULL) {
477 return StringValue(strdup("t"));
478 }
479
480 // some randomness from the clock
481 int32_t override_base;
482 bool override_set = false;
483 int32_t random_base = time(NULL) % 1024;
484 // some more randomness from /dev/random
485 FILE *f_random = fopen("/dev/random", "rb");
486 uint16_t random_bits = 0;
487 if (f_random != NULL) {
488 fread(&random_bits, 2, 1, f_random);
489 random_bits = random_bits % 1024;
490 fclose(f_random);
491 }
492 random_base = (random_base + random_bits) % 1024;
493 fprintf(ui->cmd_pipe, "ui_print Random offset: 0x%x\n", random_base);
494 fprintf(ui->cmd_pipe, "ui_print\n");
495
496 // make sure we never randomize to zero; this let's us look at a file
497 // and know for sure whether it has been processed; important in the
498 // crash recovery process
499 if (random_base == 0) random_base = 1;
500 // make sure our randomization is page-aligned
501 random_base *= -0x1000;
502 override_base = random_base;
503
504 int i = 0;
505 bool success = true;
506 while (i < (argc - 1)) {
507 success = success && retouch_one_library(retouch_entries[i],
508 retouch_entries[i+1],
509 random_base,
510 override_set ?
511 NULL :
512 &override_base);
513 if (!success)
514 ErrorAbort(state, "Failed to retouch '%s'.", retouch_entries[i]);
515
516 free(retouch_entries[i]);
517 free(retouch_entries[i+1]);
518 i += 2;
519
520 if (success && override_base != 0) {
521 random_base = override_base;
522 override_set = true;
523 }
524 }
525 if (i < argc) {
526 free(retouch_entries[i]);
527 success = false;
528 }
529 free(retouch_entries);
530
531 if (!success) {
532 Value* v = malloc(sizeof(Value));
533 v->type = VAL_STRING;
534 v->data = NULL;
535 v->size = -1;
536 return v;
537 }
538 return StringValue(strdup("t"));
539}
540
541
542// undo_retouch_binaries(lib1, lib2, ...)
543Value* UndoRetouchBinariesFn(const char* name, State* state,
544 int argc, Expr* argv[]) {
545 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
546
547 char **retouch_entries = ReadVarArgs(state, argc, argv);
548 if (retouch_entries == NULL) {
549 return StringValue(strdup("t"));
550 }
551
552 int i = 0;
553 bool success = true;
554 int32_t override_base;
555 while (i < (argc-1)) {
556 success = success && retouch_one_library(retouch_entries[i],
557 retouch_entries[i+1],
558 0 /* undo => offset==0 */,
559 NULL);
560 if (!success)
561 ErrorAbort(state, "Failed to unretouch '%s'.",
562 retouch_entries[i]);
563
564 free(retouch_entries[i]);
565 free(retouch_entries[i+1]);
566 i += 2;
567 }
568 if (i < argc) {
569 free(retouch_entries[i]);
570 success = false;
571 }
572 free(retouch_entries);
573
574 if (!success) {
575 Value* v = malloc(sizeof(Value));
576 v->type = VAL_STRING;
577 v->data = NULL;
578 v->size = -1;
579 return v;
580 }
581 return StringValue(strdup("t"));
582}
583
584
Doug Zongker9931f7f2009-06-10 14:11:53 -0700585// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700586// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800587Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700588 if (argc == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700589 return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700590 }
591 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700592 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700593 if (target == NULL) return NULL;
594
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700595 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700596 if (srcs == NULL) {
597 free(target);
598 return NULL;
599 }
600
601 int i;
602 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700603 if (unlink(srcs[i]) < 0) {
604 if (errno != ENOENT) {
605 fprintf(stderr, "%s: failed to remove %s: %s\n",
606 name, srcs[i], strerror(errno));
607 }
608 }
609 if (symlink(target, srcs[i]) < 0) {
610 fprintf(stderr, "%s: failed to symlink %s to %s: %s\n",
611 name, srcs[i], target, strerror(errno));
612 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700613 free(srcs[i]);
614 }
615 free(srcs);
Doug Zongker512536a2010-02-17 16:11:44 -0800616 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700617}
618
Doug Zongker8edb00c2009-06-11 17:21:44 -0700619
Doug Zongker512536a2010-02-17 16:11:44 -0800620Value* SetPermFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700621 char* result = NULL;
622 bool recursive = (strcmp(name, "set_perm_recursive") == 0);
623
624 int min_args = 4 + (recursive ? 1 : 0);
625 if (argc < min_args) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700626 return ErrorAbort(state, "%s() expects %d+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700627 }
628
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700629 char** args = ReadVarArgs(state, argc, argv);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700630 if (args == NULL) return NULL;
631
632 char* end;
633 int i;
634
635 int uid = strtoul(args[0], &end, 0);
636 if (*end != '\0' || args[0][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700637 ErrorAbort(state, "%s: \"%s\" not a valid uid", name, args[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700638 goto done;
639 }
640
641 int gid = strtoul(args[1], &end, 0);
642 if (*end != '\0' || args[1][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700643 ErrorAbort(state, "%s: \"%s\" not a valid gid", name, args[1]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700644 goto done;
645 }
646
647 if (recursive) {
648 int dir_mode = strtoul(args[2], &end, 0);
649 if (*end != '\0' || args[2][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700650 ErrorAbort(state, "%s: \"%s\" not a valid dirmode", name, args[2]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700651 goto done;
652 }
653
654 int file_mode = strtoul(args[3], &end, 0);
655 if (*end != '\0' || args[3][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700656 ErrorAbort(state, "%s: \"%s\" not a valid filemode",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700657 name, args[3]);
658 goto done;
659 }
660
661 for (i = 4; i < argc; ++i) {
662 dirSetHierarchyPermissions(args[i], uid, gid, dir_mode, file_mode);
663 }
664 } else {
665 int mode = strtoul(args[2], &end, 0);
666 if (*end != '\0' || args[2][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700667 ErrorAbort(state, "%s: \"%s\" not a valid mode", name, args[2]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700668 goto done;
669 }
670
Doug Zongker0bbfe3d2009-06-25 13:37:31 -0700671 for (i = 3; i < argc; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700672 if (chown(args[i], uid, gid) < 0) {
673 fprintf(stderr, "%s: chown of %s to %d %d failed: %s\n",
674 name, args[i], uid, gid, strerror(errno));
675 }
676 if (chmod(args[i], mode) < 0) {
677 fprintf(stderr, "%s: chmod of %s to %o failed: %s\n",
678 name, args[i], mode, strerror(errno));
679 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700680 }
681 }
682 result = strdup("");
683
684done:
685 for (i = 0; i < argc; ++i) {
686 free(args[i]);
687 }
688 free(args);
689
Doug Zongker512536a2010-02-17 16:11:44 -0800690 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700691}
692
Doug Zongker8edb00c2009-06-11 17:21:44 -0700693
Doug Zongker512536a2010-02-17 16:11:44 -0800694Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700695 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700696 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700697 }
698 char* key;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700699 key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700700 if (key == NULL) return NULL;
701
702 char value[PROPERTY_VALUE_MAX];
703 property_get(key, value, "");
704 free(key);
705
Doug Zongker512536a2010-02-17 16:11:44 -0800706 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700707}
708
709
Doug Zongker47cace92009-06-18 10:11:50 -0700710// file_getprop(file, key)
711//
712// interprets 'file' as a getprop-style file (key=value pairs, one
713// per line, # comment lines and blank lines okay), and returns the value
714// for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800715Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700716 char* result = NULL;
717 char* buffer = NULL;
718 char* filename;
719 char* key;
720 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
721 return NULL;
722 }
723
724 struct stat st;
725 if (stat(filename, &st) < 0) {
726 ErrorAbort(state, "%s: failed to stat \"%s\": %s",
727 name, filename, strerror(errno));
728 goto done;
729 }
730
731#define MAX_FILE_GETPROP_SIZE 65536
732
733 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
734 ErrorAbort(state, "%s too large for %s (max %d)",
735 filename, name, MAX_FILE_GETPROP_SIZE);
736 goto done;
737 }
738
739 buffer = malloc(st.st_size+1);
740 if (buffer == NULL) {
741 ErrorAbort(state, "%s: failed to alloc %d bytes", name, st.st_size+1);
742 goto done;
743 }
744
745 FILE* f = fopen(filename, "rb");
746 if (f == NULL) {
747 ErrorAbort(state, "%s: failed to open %s: %s",
748 name, filename, strerror(errno));
749 goto done;
750 }
751
752 if (fread(buffer, 1, st.st_size, f) != st.st_size) {
753 ErrorAbort(state, "%s: failed to read %d bytes from %s",
754 name, st.st_size+1, filename);
755 fclose(f);
756 goto done;
757 }
758 buffer[st.st_size] = '\0';
759
760 fclose(f);
761
762 char* line = strtok(buffer, "\n");
763 do {
764 // skip whitespace at start of line
765 while (*line && isspace(*line)) ++line;
766
767 // comment or blank line: skip to next line
768 if (*line == '\0' || *line == '#') continue;
769
770 char* equal = strchr(line, '=');
771 if (equal == NULL) {
772 ErrorAbort(state, "%s: malformed line \"%s\": %s not a prop file?",
773 name, line, filename);
774 goto done;
775 }
776
777 // trim whitespace between key and '='
778 char* key_end = equal-1;
779 while (key_end > line && isspace(*key_end)) --key_end;
780 key_end[1] = '\0';
781
782 // not the key we're looking for
783 if (strcmp(key, line) != 0) continue;
784
785 // skip whitespace after the '=' to the start of the value
786 char* val_start = equal+1;
787 while(*val_start && isspace(*val_start)) ++val_start;
788
789 // trim trailing whitespace
790 char* val_end = val_start + strlen(val_start)-1;
791 while (val_end > val_start && isspace(*val_end)) --val_end;
792 val_end[1] = '\0';
793
794 result = strdup(val_start);
795 break;
796
797 } while ((line = strtok(NULL, "\n")));
798
799 if (result == NULL) result = strdup("");
800
801 done:
802 free(filename);
803 free(key);
804 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -0800805 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -0700806}
807
808
Doug Zongker8edb00c2009-06-11 17:21:44 -0700809static bool write_raw_image_cb(const unsigned char* data,
810 int data_len, void* ctx) {
811 int r = mtd_write_data((MtdWriteContext*)ctx, (const char *)data, data_len);
812 if (r == data_len) return true;
813 fprintf(stderr, "%s\n", strerror(errno));
814 return false;
815}
816
Doug Zongker179b2d92011-04-12 15:49:04 -0700817// write_raw_image(filename_or_blob, partition)
Doug Zongker512536a2010-02-17 16:11:44 -0800818Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700819 char* result = NULL;
820
Doug Zongker179b2d92011-04-12 15:49:04 -0700821 Value* partition_value;
822 Value* contents;
823 if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700824 return NULL;
825 }
826
Doug Zongker179b2d92011-04-12 15:49:04 -0700827 if (partition_value->type != VAL_STRING) {
828 ErrorAbort(state, "partition argument to %s must be string", name);
829 goto done;
830 }
831 char* partition = partition_value->data;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700832 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700833 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700834 goto done;
835 }
Doug Zongker179b2d92011-04-12 15:49:04 -0700836 if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700837 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700838 goto done;
839 }
840
841 mtd_scan_partitions();
842 const MtdPartition* mtd = mtd_find_partition_by_name(partition);
843 if (mtd == NULL) {
844 fprintf(stderr, "%s: no mtd partition named \"%s\"\n", name, partition);
845 result = strdup("");
846 goto done;
847 }
848
849 MtdWriteContext* ctx = mtd_write_partition(mtd);
850 if (ctx == NULL) {
851 fprintf(stderr, "%s: can't write mtd partition \"%s\"\n",
852 name, partition);
853 result = strdup("");
854 goto done;
855 }
856
857 bool success;
858
Doug Zongker179b2d92011-04-12 15:49:04 -0700859 if (contents->type == VAL_STRING) {
860 // we're given a filename as the contents
861 char* filename = contents->data;
862 FILE* f = fopen(filename, "rb");
863 if (f == NULL) {
864 fprintf(stderr, "%s: can't open %s: %s\n",
865 name, filename, strerror(errno));
866 result = strdup("");
867 goto done;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700868 }
Doug Zongker179b2d92011-04-12 15:49:04 -0700869
870 success = true;
871 char* buffer = malloc(BUFSIZ);
872 int read;
873 while (success && (read = fread(buffer, 1, BUFSIZ, f)) > 0) {
874 int wrote = mtd_write_data(ctx, buffer, read);
875 success = success && (wrote == read);
876 }
877 free(buffer);
878 fclose(f);
879 } else {
880 // we're given a blob as the contents
881 ssize_t wrote = mtd_write_data(ctx, contents->data, contents->size);
882 success = (wrote == contents->size);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700883 }
Doug Zongker179b2d92011-04-12 15:49:04 -0700884 if (!success) {
885 fprintf(stderr, "mtd_write_data to %s failed: %s\n",
886 partition, strerror(errno));
887 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700888
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700889 if (mtd_erase_blocks(ctx, -1) == -1) {
890 fprintf(stderr, "%s: error erasing blocks of %s\n", name, partition);
891 }
892 if (mtd_write_close(ctx) != 0) {
893 fprintf(stderr, "%s: error closing write of %s\n", name, partition);
894 }
895
Doug Zongker179b2d92011-04-12 15:49:04 -0700896 printf("%s %s partition\n",
897 success ? "wrote" : "failed to write", partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700898
899 result = success ? partition : strdup("");
900
901done:
Doug Zongker179b2d92011-04-12 15:49:04 -0700902 if (result != partition) FreeValue(partition_value);
903 FreeValue(contents);
Doug Zongker512536a2010-02-17 16:11:44 -0800904 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700905}
906
Doug Zongker8edb00c2009-06-11 17:21:44 -0700907// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -0800908Value* ApplyPatchSpaceFn(const char* name, State* state,
909 int argc, Expr* argv[]) {
910 char* bytes_str;
911 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
912 return NULL;
913 }
914
915 char* endptr;
916 size_t bytes = strtol(bytes_str, &endptr, 10);
917 if (bytes == 0 && endptr == bytes_str) {
918 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n",
919 name, bytes_str);
920 free(bytes_str);
921 return NULL;
922 }
923
924 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
925}
926
927
928// apply_patch(srcfile, tgtfile, tgtsha1, tgtsize, sha1_1, patch_1, ...)
Doug Zongker512536a2010-02-17 16:11:44 -0800929Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800930 if (argc < 6 || (argc % 2) == 1) {
931 return ErrorAbort(state, "%s(): expected at least 6 args and an "
932 "even number, got %d",
933 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700934 }
935
Doug Zongkerc4351c72010-02-22 14:46:32 -0800936 char* source_filename;
937 char* target_filename;
938 char* target_sha1;
939 char* target_size_str;
940 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
941 &target_sha1, &target_size_str) < 0) {
942 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700943 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700944
Doug Zongkerc4351c72010-02-22 14:46:32 -0800945 char* endptr;
946 size_t target_size = strtol(target_size_str, &endptr, 10);
947 if (target_size == 0 && endptr == target_size_str) {
948 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count",
949 name, target_size_str);
950 free(source_filename);
951 free(target_filename);
952 free(target_sha1);
953 free(target_size_str);
954 return NULL;
955 }
956
957 int patchcount = (argc-4) / 2;
958 Value** patches = ReadValueVarArgs(state, argc-4, argv+4);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700959
960 int i;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800961 for (i = 0; i < patchcount; ++i) {
962 if (patches[i*2]->type != VAL_STRING) {
963 ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i);
964 break;
965 }
966 if (patches[i*2+1]->type != VAL_BLOB) {
967 ErrorAbort(state, "%s(): patch #%d is not blob", name, i);
968 break;
969 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700970 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800971 if (i != patchcount) {
972 for (i = 0; i < patchcount*2; ++i) {
973 FreeValue(patches[i]);
974 }
975 free(patches);
976 return NULL;
977 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700978
Doug Zongkerc4351c72010-02-22 14:46:32 -0800979 char** patch_sha_str = malloc(patchcount * sizeof(char*));
980 for (i = 0; i < patchcount; ++i) {
981 patch_sha_str[i] = patches[i*2]->data;
982 patches[i*2]->data = NULL;
983 FreeValue(patches[i*2]);
984 patches[i] = patches[i*2+1];
Doug Zongker8edb00c2009-06-11 17:21:44 -0700985 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800986
987 int result = applypatch(source_filename, target_filename,
988 target_sha1, target_size,
989 patchcount, patch_sha_str, patches);
990
991 for (i = 0; i < patchcount; ++i) {
992 FreeValue(patches[i]);
993 }
994 free(patch_sha_str);
995 free(patches);
996
997 return StringValue(strdup(result == 0 ? "t" : ""));
998}
999
1000// apply_patch_check(file, [sha1_1, ...])
1001Value* ApplyPatchCheckFn(const char* name, State* state,
1002 int argc, Expr* argv[]) {
1003 if (argc < 1) {
1004 return ErrorAbort(state, "%s(): expected at least 1 arg, got %d",
1005 name, argc);
1006 }
1007
1008 char* filename;
1009 if (ReadArgs(state, argv, 1, &filename) < 0) {
1010 return NULL;
1011 }
1012
1013 int patchcount = argc-1;
1014 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
1015
1016 int result = applypatch_check(filename, patchcount, sha1s);
1017
1018 int i;
1019 for (i = 0; i < patchcount; ++i) {
1020 free(sha1s[i]);
1021 }
1022 free(sha1s);
1023
1024 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001025}
1026
Doug Zongker512536a2010-02-17 16:11:44 -08001027Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001028 char** args = ReadVarArgs(state, argc, argv);
1029 if (args == NULL) {
1030 return NULL;
1031 }
1032
1033 int size = 0;
1034 int i;
1035 for (i = 0; i < argc; ++i) {
1036 size += strlen(args[i]);
1037 }
1038 char* buffer = malloc(size+1);
1039 size = 0;
1040 for (i = 0; i < argc; ++i) {
1041 strcpy(buffer+size, args[i]);
1042 size += strlen(args[i]);
1043 free(args[i]);
1044 }
1045 free(args);
1046 buffer[size] = '\0';
1047
1048 char* line = strtok(buffer, "\n");
1049 while (line) {
1050 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe,
1051 "ui_print %s\n", line);
1052 line = strtok(NULL, "\n");
1053 }
1054 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "ui_print\n");
1055
Doug Zongker512536a2010-02-17 16:11:44 -08001056 return StringValue(buffer);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001057}
1058
Doug Zongkerd0181b82011-10-19 10:51:12 -07001059Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
1060 if (argc != 0) {
1061 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1062 }
1063 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
1064 return StringValue(strdup("t"));
1065}
1066
Doug Zongker512536a2010-02-17 16:11:44 -08001067Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001068 if (argc < 1) {
1069 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1070 }
1071 char** args = ReadVarArgs(state, argc, argv);
1072 if (args == NULL) {
1073 return NULL;
1074 }
1075
1076 char** args2 = malloc(sizeof(char*) * (argc+1));
1077 memcpy(args2, args, sizeof(char*) * argc);
1078 args2[argc] = NULL;
1079
1080 fprintf(stderr, "about to run program [%s] with %d args\n", args2[0], argc);
1081
1082 pid_t child = fork();
1083 if (child == 0) {
1084 execv(args2[0], args2);
1085 fprintf(stderr, "run_program: execv failed: %s\n", strerror(errno));
1086 _exit(1);
1087 }
1088 int status;
1089 waitpid(child, &status, 0);
1090 if (WIFEXITED(status)) {
1091 if (WEXITSTATUS(status) != 0) {
1092 fprintf(stderr, "run_program: child exited with status %d\n",
1093 WEXITSTATUS(status));
1094 }
1095 } else if (WIFSIGNALED(status)) {
1096 fprintf(stderr, "run_program: child terminated by signal %d\n",
1097 WTERMSIG(status));
1098 }
1099
1100 int i;
1101 for (i = 0; i < argc; ++i) {
1102 free(args[i]);
1103 }
1104 free(args);
1105 free(args2);
1106
1107 char buffer[20];
1108 sprintf(buffer, "%d", status);
1109
Doug Zongker512536a2010-02-17 16:11:44 -08001110 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001111}
1112
Doug Zongker512536a2010-02-17 16:11:44 -08001113// Take a sha-1 digest and return it as a newly-allocated hex string.
1114static char* PrintSha1(uint8_t* digest) {
1115 char* buffer = malloc(SHA_DIGEST_SIZE*2 + 1);
1116 int i;
1117 const char* alphabet = "0123456789abcdef";
1118 for (i = 0; i < SHA_DIGEST_SIZE; ++i) {
1119 buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf];
1120 buffer[i*2+1] = alphabet[digest[i] & 0xf];
1121 }
1122 buffer[i*2] = '\0';
1123 return buffer;
1124}
1125
1126// sha1_check(data)
1127// to return the sha1 of the data (given in the format returned by
1128// read_file).
1129//
1130// sha1_check(data, sha1_hex, [sha1_hex, ...])
1131// returns the sha1 of the file if it matches any of the hex
1132// strings passed, or "" if it does not equal any of them.
1133//
1134Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1135 if (argc < 1) {
1136 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1137 }
1138
1139 Value** args = ReadValueVarArgs(state, argc, argv);
1140 if (args == NULL) {
1141 return NULL;
1142 }
1143
1144 if (args[0]->size < 0) {
1145 fprintf(stderr, "%s(): no file contents received", name);
1146 return StringValue(strdup(""));
1147 }
1148 uint8_t digest[SHA_DIGEST_SIZE];
1149 SHA(args[0]->data, args[0]->size, digest);
1150 FreeValue(args[0]);
1151
1152 if (argc == 1) {
1153 return StringValue(PrintSha1(digest));
1154 }
1155
1156 int i;
1157 uint8_t* arg_digest = malloc(SHA_DIGEST_SIZE);
1158 for (i = 1; i < argc; ++i) {
1159 if (args[i]->type != VAL_STRING) {
1160 fprintf(stderr, "%s(): arg %d is not a string; skipping",
1161 name, i);
1162 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1163 // Warn about bad args and skip them.
1164 fprintf(stderr, "%s(): error parsing \"%s\" as sha-1; skipping",
1165 name, args[i]->data);
1166 } else if (memcmp(digest, arg_digest, SHA_DIGEST_SIZE) == 0) {
1167 break;
1168 }
1169 FreeValue(args[i]);
1170 }
1171 if (i >= argc) {
1172 // Didn't match any of the hex strings; return false.
1173 return StringValue(strdup(""));
1174 }
1175 // Found a match; free all the remaining arguments and return the
1176 // matched one.
1177 int j;
1178 for (j = i+1; j < argc; ++j) {
1179 FreeValue(args[j]);
1180 }
1181 return args[i];
1182}
1183
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001184// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001185// is actually a FileContents*).
1186Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1187 if (argc != 1) {
1188 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1189 }
1190 char* filename;
1191 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1192
1193 Value* v = malloc(sizeof(Value));
1194 v->type = VAL_BLOB;
1195
1196 FileContents fc;
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001197 if (LoadFileContents(filename, &fc, RETOUCH_DONT_MASK) != 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001198 ErrorAbort(state, "%s() loading \"%s\" failed: %s",
1199 name, filename, strerror(errno));
1200 free(filename);
1201 free(v);
1202 free(fc.data);
1203 return NULL;
1204 }
1205
1206 v->size = fc.size;
1207 v->data = (char*)fc.data;
1208
1209 free(filename);
1210 return v;
1211}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001212
Doug Zongker9931f7f2009-06-10 14:11:53 -07001213void RegisterInstallFunctions() {
1214 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001215 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001216 RegisterFunction("unmount", UnmountFn);
1217 RegisterFunction("format", FormatFn);
1218 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001219 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001220 RegisterFunction("delete", DeleteFn);
1221 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001222 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1223 RegisterFunction("package_extract_file", PackageExtractFileFn);
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001224 RegisterFunction("retouch_binaries", RetouchBinariesFn);
1225 RegisterFunction("undo_retouch_binaries", UndoRetouchBinariesFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001226 RegisterFunction("symlink", SymlinkFn);
1227 RegisterFunction("set_perm", SetPermFn);
1228 RegisterFunction("set_perm_recursive", SetPermFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001229
1230 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001231 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001232 RegisterFunction("write_raw_image", WriteRawImageFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001233
1234 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001235 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1236 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001237
Doug Zongker512536a2010-02-17 16:11:44 -08001238 RegisterFunction("read_file", ReadFileFn);
1239 RegisterFunction("sha1_check", Sha1CheckFn);
1240
Doug Zongkerd0181b82011-10-19 10:51:12 -07001241 RegisterFunction("wipe_cache", WipeCacheFn);
1242
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001243 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001244
1245 RegisterFunction("run_program", RunProgramFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001246}