blob: 64da2efd6f8f4825dfd2096f44c4100bd0a5d86c [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
Doug Zongker512536a2010-02-17 16:11:44 -0800203Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700204 char* result = NULL;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400205 if (argc != 5) {
206 return ErrorAbort(state, "%s() expects 5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700207 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700208 char* fs_type;
209 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700210 char* location;
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800211 char* fs_size;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400212 char* mount_point;
Stephen Smalley779701d2012-02-09 14:13:23 -0500213
Stephen Smalley779701d2012-02-09 14:13:23 -0500214 if (ReadArgs(state, argv, 5, &fs_type, &partition_type, &location, &fs_size, &mount_point) < 0) {
215 return NULL;
216 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700217
Doug Zongker3d177d02010-07-01 09:18:44 -0700218 if (strlen(fs_type) == 0) {
219 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
220 goto done;
221 }
222 if (strlen(partition_type) == 0) {
223 ErrorAbort(state, "partition_type argument to %s() can't be empty",
224 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700225 goto done;
226 }
227 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700228 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700229 goto done;
230 }
231
Stephen Smalley516e4e22012-04-03 13:35:11 -0400232 if (strlen(mount_point) == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500233 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
234 goto done;
235 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500236
Doug Zongker3d177d02010-07-01 09:18:44 -0700237 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700238 mtd_scan_partitions();
239 const MtdPartition* mtd = mtd_find_partition_by_name(location);
240 if (mtd == NULL) {
241 fprintf(stderr, "%s: no mtd partition named \"%s\"",
242 name, location);
243 result = strdup("");
244 goto done;
245 }
246 MtdWriteContext* ctx = mtd_write_partition(mtd);
247 if (ctx == NULL) {
248 fprintf(stderr, "%s: can't write \"%s\"", name, location);
249 result = strdup("");
250 goto done;
251 }
252 if (mtd_erase_blocks(ctx, -1) == -1) {
253 mtd_write_close(ctx);
254 fprintf(stderr, "%s: failed to erase \"%s\"", name, location);
255 result = strdup("");
256 goto done;
257 }
258 if (mtd_write_close(ctx) != 0) {
259 fprintf(stderr, "%s: failed to close \"%s\"", name, location);
260 result = strdup("");
261 goto done;
262 }
263 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700264#ifdef USE_EXT4
265 } else if (strcmp(fs_type, "ext4") == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500266 int status = make_ext4fs(location, atoll(fs_size), mount_point, sehandle);
Doug Zongker3d177d02010-07-01 09:18:44 -0700267 if (status != 0) {
268 fprintf(stderr, "%s: make_ext4fs failed (%d) on %s",
269 name, status, location);
270 result = strdup("");
271 goto done;
272 }
273 result = location;
274#endif
Doug Zongker9931f7f2009-06-10 14:11:53 -0700275 } else {
Doug Zongker3d177d02010-07-01 09:18:44 -0700276 fprintf(stderr, "%s: unsupported fs_type \"%s\" partition_type \"%s\"",
277 name, fs_type, partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700278 }
279
280done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700281 free(fs_type);
282 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700283 if (result != location) free(location);
Doug Zongker512536a2010-02-17 16:11:44 -0800284 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700285}
286
Doug Zongker8edb00c2009-06-11 17:21:44 -0700287
Doug Zongker512536a2010-02-17 16:11:44 -0800288Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700289 char** paths = malloc(argc * sizeof(char*));
290 int i;
291 for (i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700292 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700293 if (paths[i] == NULL) {
294 int j;
295 for (j = 0; j < i; ++i) {
296 free(paths[j]);
297 }
298 free(paths);
299 return NULL;
300 }
301 }
302
303 bool recursive = (strcmp(name, "delete_recursive") == 0);
304
305 int success = 0;
306 for (i = 0; i < argc; ++i) {
307 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
308 ++success;
309 free(paths[i]);
310 }
311 free(paths);
312
313 char buffer[10];
314 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800315 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700316}
317
Doug Zongker8edb00c2009-06-11 17:21:44 -0700318
Doug Zongker512536a2010-02-17 16:11:44 -0800319Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700320 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700321 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700322 }
323 char* frac_str;
324 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700325 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700326 return NULL;
327 }
328
329 double frac = strtod(frac_str, NULL);
330 int sec = strtol(sec_str, NULL, 10);
331
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700332 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700333 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
334
Doug Zongker9931f7f2009-06-10 14:11:53 -0700335 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800336 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700337}
338
Doug Zongker512536a2010-02-17 16:11:44 -0800339Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700340 if (argc != 1) {
341 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
342 }
343 char* frac_str;
344 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
345 return NULL;
346 }
347
348 double frac = strtod(frac_str, NULL);
349
350 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
351 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
352
Doug Zongker512536a2010-02-17 16:11:44 -0800353 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700354}
355
Doug Zongker8edb00c2009-06-11 17:21:44 -0700356// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800357Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700358 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700359 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700360 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700361 }
362 char* zip_path;
363 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700364 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700365
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700366 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700367
368 // To create a consistent system image, never use the clock for timestamps.
369 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
370
371 bool success = mzExtractRecursive(za, zip_path, dest_path,
372 MZ_EXTRACT_FILES_ONLY, &timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500373 NULL, NULL, sehandle);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700374 free(zip_path);
375 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800376 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700377}
378
Doug Zongker8edb00c2009-06-11 17:21:44 -0700379
380// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800381// or
382// package_extract_file(package_path)
383// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800384// function (the char* returned is actually a FileContents*).
385Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700386 int argc, Expr* argv[]) {
Doug Zongker6aece332010-02-01 14:40:12 -0800387 if (argc != 1 && argc != 2) {
388 return ErrorAbort(state, "%s() expects 1 or 2 args, got %d",
389 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700390 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700391 bool success = false;
Doug Zongker6aece332010-02-01 14:40:12 -0800392 if (argc == 2) {
393 // The two-argument version extracts to a file.
Doug Zongker8edb00c2009-06-11 17:21:44 -0700394
Doug Zongker6aece332010-02-01 14:40:12 -0800395 char* zip_path;
396 char* dest_path;
397 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
398
399 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
400 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
401 if (entry == NULL) {
402 fprintf(stderr, "%s: no %s in package\n", name, zip_path);
403 goto done2;
404 }
405
406 FILE* f = fopen(dest_path, "wb");
407 if (f == NULL) {
408 fprintf(stderr, "%s: can't open %s for write: %s\n",
409 name, dest_path, strerror(errno));
410 goto done2;
411 }
412 success = mzExtractZipEntryToFile(za, entry, fileno(f));
413 fclose(f);
414
415 done2:
416 free(zip_path);
417 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800418 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800419 } else {
420 // The one-argument version returns the contents of the file
421 // as the result.
422
423 char* zip_path;
Doug Zongker512536a2010-02-17 16:11:44 -0800424 Value* v = malloc(sizeof(Value));
425 v->type = VAL_BLOB;
426 v->size = -1;
427 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800428
429 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
430
431 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
432 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
433 if (entry == NULL) {
434 fprintf(stderr, "%s: no %s in package\n", name, zip_path);
435 goto done1;
436 }
437
Doug Zongker512536a2010-02-17 16:11:44 -0800438 v->size = mzGetZipEntryUncompLen(entry);
439 v->data = malloc(v->size);
440 if (v->data == NULL) {
Doug Zongker6aece332010-02-01 14:40:12 -0800441 fprintf(stderr, "%s: failed to allocate %ld bytes for %s\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800442 name, (long)v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800443 goto done1;
444 }
445
Doug Zongker512536a2010-02-17 16:11:44 -0800446 success = mzExtractZipEntryToBuffer(za, entry,
447 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800448
449 done1:
450 free(zip_path);
451 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800452 free(v->data);
453 v->data = NULL;
454 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800455 }
Doug Zongker512536a2010-02-17 16:11:44 -0800456 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700457 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700458}
459
460
Hristo Bojinovdb314d62010-08-02 10:29:49 -0700461// retouch_binaries(lib1, lib2, ...)
462Value* RetouchBinariesFn(const char* name, State* state,
463 int argc, Expr* argv[]) {
464 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
465
466 char **retouch_entries = ReadVarArgs(state, argc, argv);
467 if (retouch_entries == NULL) {
468 return StringValue(strdup("t"));
469 }
470
471 // some randomness from the clock
472 int32_t override_base;
473 bool override_set = false;
474 int32_t random_base = time(NULL) % 1024;
475 // some more randomness from /dev/random
476 FILE *f_random = fopen("/dev/random", "rb");
477 uint16_t random_bits = 0;
478 if (f_random != NULL) {
479 fread(&random_bits, 2, 1, f_random);
480 random_bits = random_bits % 1024;
481 fclose(f_random);
482 }
483 random_base = (random_base + random_bits) % 1024;
484 fprintf(ui->cmd_pipe, "ui_print Random offset: 0x%x\n", random_base);
485 fprintf(ui->cmd_pipe, "ui_print\n");
486
487 // make sure we never randomize to zero; this let's us look at a file
488 // and know for sure whether it has been processed; important in the
489 // crash recovery process
490 if (random_base == 0) random_base = 1;
491 // make sure our randomization is page-aligned
492 random_base *= -0x1000;
493 override_base = random_base;
494
495 int i = 0;
496 bool success = true;
497 while (i < (argc - 1)) {
498 success = success && retouch_one_library(retouch_entries[i],
499 retouch_entries[i+1],
500 random_base,
501 override_set ?
502 NULL :
503 &override_base);
504 if (!success)
505 ErrorAbort(state, "Failed to retouch '%s'.", retouch_entries[i]);
506
507 free(retouch_entries[i]);
508 free(retouch_entries[i+1]);
509 i += 2;
510
511 if (success && override_base != 0) {
512 random_base = override_base;
513 override_set = true;
514 }
515 }
516 if (i < argc) {
517 free(retouch_entries[i]);
518 success = false;
519 }
520 free(retouch_entries);
521
522 if (!success) {
523 Value* v = malloc(sizeof(Value));
524 v->type = VAL_STRING;
525 v->data = NULL;
526 v->size = -1;
527 return v;
528 }
529 return StringValue(strdup("t"));
530}
531
532
533// undo_retouch_binaries(lib1, lib2, ...)
534Value* UndoRetouchBinariesFn(const char* name, State* state,
535 int argc, Expr* argv[]) {
536 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
537
538 char **retouch_entries = ReadVarArgs(state, argc, argv);
539 if (retouch_entries == NULL) {
540 return StringValue(strdup("t"));
541 }
542
543 int i = 0;
544 bool success = true;
545 int32_t override_base;
546 while (i < (argc-1)) {
547 success = success && retouch_one_library(retouch_entries[i],
548 retouch_entries[i+1],
549 0 /* undo => offset==0 */,
550 NULL);
551 if (!success)
552 ErrorAbort(state, "Failed to unretouch '%s'.",
553 retouch_entries[i]);
554
555 free(retouch_entries[i]);
556 free(retouch_entries[i+1]);
557 i += 2;
558 }
559 if (i < argc) {
560 free(retouch_entries[i]);
561 success = false;
562 }
563 free(retouch_entries);
564
565 if (!success) {
566 Value* v = malloc(sizeof(Value));
567 v->type = VAL_STRING;
568 v->data = NULL;
569 v->size = -1;
570 return v;
571 }
572 return StringValue(strdup("t"));
573}
574
575
Doug Zongker9931f7f2009-06-10 14:11:53 -0700576// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700577// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800578Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700579 if (argc == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700580 return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700581 }
582 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700583 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700584 if (target == NULL) return NULL;
585
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700586 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700587 if (srcs == NULL) {
588 free(target);
589 return NULL;
590 }
591
592 int i;
593 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700594 if (unlink(srcs[i]) < 0) {
595 if (errno != ENOENT) {
596 fprintf(stderr, "%s: failed to remove %s: %s\n",
597 name, srcs[i], strerror(errno));
598 }
599 }
600 if (symlink(target, srcs[i]) < 0) {
601 fprintf(stderr, "%s: failed to symlink %s to %s: %s\n",
602 name, srcs[i], target, strerror(errno));
603 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700604 free(srcs[i]);
605 }
606 free(srcs);
Doug Zongker512536a2010-02-17 16:11:44 -0800607 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700608}
609
Doug Zongker8edb00c2009-06-11 17:21:44 -0700610
Doug Zongker512536a2010-02-17 16:11:44 -0800611Value* SetPermFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700612 char* result = NULL;
613 bool recursive = (strcmp(name, "set_perm_recursive") == 0);
614
615 int min_args = 4 + (recursive ? 1 : 0);
616 if (argc < min_args) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700617 return ErrorAbort(state, "%s() expects %d+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700618 }
619
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700620 char** args = ReadVarArgs(state, argc, argv);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700621 if (args == NULL) return NULL;
622
623 char* end;
624 int i;
625
626 int uid = strtoul(args[0], &end, 0);
627 if (*end != '\0' || args[0][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700628 ErrorAbort(state, "%s: \"%s\" not a valid uid", name, args[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700629 goto done;
630 }
631
632 int gid = strtoul(args[1], &end, 0);
633 if (*end != '\0' || args[1][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700634 ErrorAbort(state, "%s: \"%s\" not a valid gid", name, args[1]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700635 goto done;
636 }
637
638 if (recursive) {
639 int dir_mode = strtoul(args[2], &end, 0);
640 if (*end != '\0' || args[2][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700641 ErrorAbort(state, "%s: \"%s\" not a valid dirmode", name, args[2]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700642 goto done;
643 }
644
645 int file_mode = strtoul(args[3], &end, 0);
646 if (*end != '\0' || args[3][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700647 ErrorAbort(state, "%s: \"%s\" not a valid filemode",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700648 name, args[3]);
649 goto done;
650 }
651
652 for (i = 4; i < argc; ++i) {
653 dirSetHierarchyPermissions(args[i], uid, gid, dir_mode, file_mode);
654 }
655 } else {
656 int mode = strtoul(args[2], &end, 0);
657 if (*end != '\0' || args[2][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700658 ErrorAbort(state, "%s: \"%s\" not a valid mode", name, args[2]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700659 goto done;
660 }
661
Doug Zongker0bbfe3d2009-06-25 13:37:31 -0700662 for (i = 3; i < argc; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700663 if (chown(args[i], uid, gid) < 0) {
664 fprintf(stderr, "%s: chown of %s to %d %d failed: %s\n",
665 name, args[i], uid, gid, strerror(errno));
666 }
667 if (chmod(args[i], mode) < 0) {
668 fprintf(stderr, "%s: chmod of %s to %o failed: %s\n",
669 name, args[i], mode, strerror(errno));
670 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700671 }
672 }
673 result = strdup("");
674
675done:
676 for (i = 0; i < argc; ++i) {
677 free(args[i]);
678 }
679 free(args);
680
Doug Zongker512536a2010-02-17 16:11:44 -0800681 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700682}
683
Doug Zongker8edb00c2009-06-11 17:21:44 -0700684
Doug Zongker512536a2010-02-17 16:11:44 -0800685Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700686 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700687 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700688 }
689 char* key;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700690 key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700691 if (key == NULL) return NULL;
692
693 char value[PROPERTY_VALUE_MAX];
694 property_get(key, value, "");
695 free(key);
696
Doug Zongker512536a2010-02-17 16:11:44 -0800697 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700698}
699
700
Doug Zongker47cace92009-06-18 10:11:50 -0700701// file_getprop(file, key)
702//
703// interprets 'file' as a getprop-style file (key=value pairs, one
704// per line, # comment lines and blank lines okay), and returns the value
705// for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800706Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700707 char* result = NULL;
708 char* buffer = NULL;
709 char* filename;
710 char* key;
711 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
712 return NULL;
713 }
714
715 struct stat st;
716 if (stat(filename, &st) < 0) {
717 ErrorAbort(state, "%s: failed to stat \"%s\": %s",
718 name, filename, strerror(errno));
719 goto done;
720 }
721
722#define MAX_FILE_GETPROP_SIZE 65536
723
724 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
725 ErrorAbort(state, "%s too large for %s (max %d)",
726 filename, name, MAX_FILE_GETPROP_SIZE);
727 goto done;
728 }
729
730 buffer = malloc(st.st_size+1);
731 if (buffer == NULL) {
732 ErrorAbort(state, "%s: failed to alloc %d bytes", name, st.st_size+1);
733 goto done;
734 }
735
736 FILE* f = fopen(filename, "rb");
737 if (f == NULL) {
738 ErrorAbort(state, "%s: failed to open %s: %s",
739 name, filename, strerror(errno));
740 goto done;
741 }
742
743 if (fread(buffer, 1, st.st_size, f) != st.st_size) {
744 ErrorAbort(state, "%s: failed to read %d bytes from %s",
745 name, st.st_size+1, filename);
746 fclose(f);
747 goto done;
748 }
749 buffer[st.st_size] = '\0';
750
751 fclose(f);
752
753 char* line = strtok(buffer, "\n");
754 do {
755 // skip whitespace at start of line
756 while (*line && isspace(*line)) ++line;
757
758 // comment or blank line: skip to next line
759 if (*line == '\0' || *line == '#') continue;
760
761 char* equal = strchr(line, '=');
762 if (equal == NULL) {
763 ErrorAbort(state, "%s: malformed line \"%s\": %s not a prop file?",
764 name, line, filename);
765 goto done;
766 }
767
768 // trim whitespace between key and '='
769 char* key_end = equal-1;
770 while (key_end > line && isspace(*key_end)) --key_end;
771 key_end[1] = '\0';
772
773 // not the key we're looking for
774 if (strcmp(key, line) != 0) continue;
775
776 // skip whitespace after the '=' to the start of the value
777 char* val_start = equal+1;
778 while(*val_start && isspace(*val_start)) ++val_start;
779
780 // trim trailing whitespace
781 char* val_end = val_start + strlen(val_start)-1;
782 while (val_end > val_start && isspace(*val_end)) --val_end;
783 val_end[1] = '\0';
784
785 result = strdup(val_start);
786 break;
787
788 } while ((line = strtok(NULL, "\n")));
789
790 if (result == NULL) result = strdup("");
791
792 done:
793 free(filename);
794 free(key);
795 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -0800796 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -0700797}
798
799
Doug Zongker8edb00c2009-06-11 17:21:44 -0700800static bool write_raw_image_cb(const unsigned char* data,
801 int data_len, void* ctx) {
802 int r = mtd_write_data((MtdWriteContext*)ctx, (const char *)data, data_len);
803 if (r == data_len) return true;
804 fprintf(stderr, "%s\n", strerror(errno));
805 return false;
806}
807
Doug Zongker179b2d92011-04-12 15:49:04 -0700808// write_raw_image(filename_or_blob, partition)
Doug Zongker512536a2010-02-17 16:11:44 -0800809Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700810 char* result = NULL;
811
Doug Zongker179b2d92011-04-12 15:49:04 -0700812 Value* partition_value;
813 Value* contents;
814 if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700815 return NULL;
816 }
817
Doug Zongker179b2d92011-04-12 15:49:04 -0700818 if (partition_value->type != VAL_STRING) {
819 ErrorAbort(state, "partition argument to %s must be string", name);
820 goto done;
821 }
822 char* partition = partition_value->data;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700823 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700824 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700825 goto done;
826 }
Doug Zongker179b2d92011-04-12 15:49:04 -0700827 if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700828 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700829 goto done;
830 }
831
832 mtd_scan_partitions();
833 const MtdPartition* mtd = mtd_find_partition_by_name(partition);
834 if (mtd == NULL) {
835 fprintf(stderr, "%s: no mtd partition named \"%s\"\n", name, partition);
836 result = strdup("");
837 goto done;
838 }
839
840 MtdWriteContext* ctx = mtd_write_partition(mtd);
841 if (ctx == NULL) {
842 fprintf(stderr, "%s: can't write mtd partition \"%s\"\n",
843 name, partition);
844 result = strdup("");
845 goto done;
846 }
847
848 bool success;
849
Doug Zongker179b2d92011-04-12 15:49:04 -0700850 if (contents->type == VAL_STRING) {
851 // we're given a filename as the contents
852 char* filename = contents->data;
853 FILE* f = fopen(filename, "rb");
854 if (f == NULL) {
855 fprintf(stderr, "%s: can't open %s: %s\n",
856 name, filename, strerror(errno));
857 result = strdup("");
858 goto done;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700859 }
Doug Zongker179b2d92011-04-12 15:49:04 -0700860
861 success = true;
862 char* buffer = malloc(BUFSIZ);
863 int read;
864 while (success && (read = fread(buffer, 1, BUFSIZ, f)) > 0) {
865 int wrote = mtd_write_data(ctx, buffer, read);
866 success = success && (wrote == read);
867 }
868 free(buffer);
869 fclose(f);
870 } else {
871 // we're given a blob as the contents
872 ssize_t wrote = mtd_write_data(ctx, contents->data, contents->size);
873 success = (wrote == contents->size);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700874 }
Doug Zongker179b2d92011-04-12 15:49:04 -0700875 if (!success) {
876 fprintf(stderr, "mtd_write_data to %s failed: %s\n",
877 partition, strerror(errno));
878 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700879
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700880 if (mtd_erase_blocks(ctx, -1) == -1) {
881 fprintf(stderr, "%s: error erasing blocks of %s\n", name, partition);
882 }
883 if (mtd_write_close(ctx) != 0) {
884 fprintf(stderr, "%s: error closing write of %s\n", name, partition);
885 }
886
Doug Zongker179b2d92011-04-12 15:49:04 -0700887 printf("%s %s partition\n",
888 success ? "wrote" : "failed to write", partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700889
890 result = success ? partition : strdup("");
891
892done:
Doug Zongker179b2d92011-04-12 15:49:04 -0700893 if (result != partition) FreeValue(partition_value);
894 FreeValue(contents);
Doug Zongker512536a2010-02-17 16:11:44 -0800895 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700896}
897
Doug Zongker8edb00c2009-06-11 17:21:44 -0700898// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -0800899Value* ApplyPatchSpaceFn(const char* name, State* state,
900 int argc, Expr* argv[]) {
901 char* bytes_str;
902 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
903 return NULL;
904 }
905
906 char* endptr;
907 size_t bytes = strtol(bytes_str, &endptr, 10);
908 if (bytes == 0 && endptr == bytes_str) {
909 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n",
910 name, bytes_str);
911 free(bytes_str);
912 return NULL;
913 }
914
915 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
916}
917
918
919// apply_patch(srcfile, tgtfile, tgtsha1, tgtsize, sha1_1, patch_1, ...)
Doug Zongker512536a2010-02-17 16:11:44 -0800920Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800921 if (argc < 6 || (argc % 2) == 1) {
922 return ErrorAbort(state, "%s(): expected at least 6 args and an "
923 "even number, got %d",
924 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700925 }
926
Doug Zongkerc4351c72010-02-22 14:46:32 -0800927 char* source_filename;
928 char* target_filename;
929 char* target_sha1;
930 char* target_size_str;
931 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
932 &target_sha1, &target_size_str) < 0) {
933 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700934 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700935
Doug Zongkerc4351c72010-02-22 14:46:32 -0800936 char* endptr;
937 size_t target_size = strtol(target_size_str, &endptr, 10);
938 if (target_size == 0 && endptr == target_size_str) {
939 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count",
940 name, target_size_str);
941 free(source_filename);
942 free(target_filename);
943 free(target_sha1);
944 free(target_size_str);
945 return NULL;
946 }
947
948 int patchcount = (argc-4) / 2;
949 Value** patches = ReadValueVarArgs(state, argc-4, argv+4);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700950
951 int i;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800952 for (i = 0; i < patchcount; ++i) {
953 if (patches[i*2]->type != VAL_STRING) {
954 ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i);
955 break;
956 }
957 if (patches[i*2+1]->type != VAL_BLOB) {
958 ErrorAbort(state, "%s(): patch #%d is not blob", name, i);
959 break;
960 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700961 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800962 if (i != patchcount) {
963 for (i = 0; i < patchcount*2; ++i) {
964 FreeValue(patches[i]);
965 }
966 free(patches);
967 return NULL;
968 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700969
Doug Zongkerc4351c72010-02-22 14:46:32 -0800970 char** patch_sha_str = malloc(patchcount * sizeof(char*));
971 for (i = 0; i < patchcount; ++i) {
972 patch_sha_str[i] = patches[i*2]->data;
973 patches[i*2]->data = NULL;
974 FreeValue(patches[i*2]);
975 patches[i] = patches[i*2+1];
Doug Zongker8edb00c2009-06-11 17:21:44 -0700976 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800977
978 int result = applypatch(source_filename, target_filename,
979 target_sha1, target_size,
980 patchcount, patch_sha_str, patches);
981
982 for (i = 0; i < patchcount; ++i) {
983 FreeValue(patches[i]);
984 }
985 free(patch_sha_str);
986 free(patches);
987
988 return StringValue(strdup(result == 0 ? "t" : ""));
989}
990
991// apply_patch_check(file, [sha1_1, ...])
992Value* ApplyPatchCheckFn(const char* name, State* state,
993 int argc, Expr* argv[]) {
994 if (argc < 1) {
995 return ErrorAbort(state, "%s(): expected at least 1 arg, got %d",
996 name, argc);
997 }
998
999 char* filename;
1000 if (ReadArgs(state, argv, 1, &filename) < 0) {
1001 return NULL;
1002 }
1003
1004 int patchcount = argc-1;
1005 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
1006
1007 int result = applypatch_check(filename, patchcount, sha1s);
1008
1009 int i;
1010 for (i = 0; i < patchcount; ++i) {
1011 free(sha1s[i]);
1012 }
1013 free(sha1s);
1014
1015 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001016}
1017
Doug Zongker512536a2010-02-17 16:11:44 -08001018Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001019 char** args = ReadVarArgs(state, argc, argv);
1020 if (args == NULL) {
1021 return NULL;
1022 }
1023
1024 int size = 0;
1025 int i;
1026 for (i = 0; i < argc; ++i) {
1027 size += strlen(args[i]);
1028 }
1029 char* buffer = malloc(size+1);
1030 size = 0;
1031 for (i = 0; i < argc; ++i) {
1032 strcpy(buffer+size, args[i]);
1033 size += strlen(args[i]);
1034 free(args[i]);
1035 }
1036 free(args);
1037 buffer[size] = '\0';
1038
1039 char* line = strtok(buffer, "\n");
1040 while (line) {
1041 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe,
1042 "ui_print %s\n", line);
1043 line = strtok(NULL, "\n");
1044 }
1045 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "ui_print\n");
1046
Doug Zongker512536a2010-02-17 16:11:44 -08001047 return StringValue(buffer);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001048}
1049
Doug Zongkerd0181b82011-10-19 10:51:12 -07001050Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
1051 if (argc != 0) {
1052 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1053 }
1054 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
1055 return StringValue(strdup("t"));
1056}
1057
Doug Zongker512536a2010-02-17 16:11:44 -08001058Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001059 if (argc < 1) {
1060 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1061 }
1062 char** args = ReadVarArgs(state, argc, argv);
1063 if (args == NULL) {
1064 return NULL;
1065 }
1066
1067 char** args2 = malloc(sizeof(char*) * (argc+1));
1068 memcpy(args2, args, sizeof(char*) * argc);
1069 args2[argc] = NULL;
1070
1071 fprintf(stderr, "about to run program [%s] with %d args\n", args2[0], argc);
1072
1073 pid_t child = fork();
1074 if (child == 0) {
1075 execv(args2[0], args2);
1076 fprintf(stderr, "run_program: execv failed: %s\n", strerror(errno));
1077 _exit(1);
1078 }
1079 int status;
1080 waitpid(child, &status, 0);
1081 if (WIFEXITED(status)) {
1082 if (WEXITSTATUS(status) != 0) {
1083 fprintf(stderr, "run_program: child exited with status %d\n",
1084 WEXITSTATUS(status));
1085 }
1086 } else if (WIFSIGNALED(status)) {
1087 fprintf(stderr, "run_program: child terminated by signal %d\n",
1088 WTERMSIG(status));
1089 }
1090
1091 int i;
1092 for (i = 0; i < argc; ++i) {
1093 free(args[i]);
1094 }
1095 free(args);
1096 free(args2);
1097
1098 char buffer[20];
1099 sprintf(buffer, "%d", status);
1100
Doug Zongker512536a2010-02-17 16:11:44 -08001101 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001102}
1103
Doug Zongker512536a2010-02-17 16:11:44 -08001104// Take a sha-1 digest and return it as a newly-allocated hex string.
1105static char* PrintSha1(uint8_t* digest) {
1106 char* buffer = malloc(SHA_DIGEST_SIZE*2 + 1);
1107 int i;
1108 const char* alphabet = "0123456789abcdef";
1109 for (i = 0; i < SHA_DIGEST_SIZE; ++i) {
1110 buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf];
1111 buffer[i*2+1] = alphabet[digest[i] & 0xf];
1112 }
1113 buffer[i*2] = '\0';
1114 return buffer;
1115}
1116
1117// sha1_check(data)
1118// to return the sha1 of the data (given in the format returned by
1119// read_file).
1120//
1121// sha1_check(data, sha1_hex, [sha1_hex, ...])
1122// returns the sha1 of the file if it matches any of the hex
1123// strings passed, or "" if it does not equal any of them.
1124//
1125Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1126 if (argc < 1) {
1127 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1128 }
1129
1130 Value** args = ReadValueVarArgs(state, argc, argv);
1131 if (args == NULL) {
1132 return NULL;
1133 }
1134
1135 if (args[0]->size < 0) {
1136 fprintf(stderr, "%s(): no file contents received", name);
1137 return StringValue(strdup(""));
1138 }
1139 uint8_t digest[SHA_DIGEST_SIZE];
1140 SHA(args[0]->data, args[0]->size, digest);
1141 FreeValue(args[0]);
1142
1143 if (argc == 1) {
1144 return StringValue(PrintSha1(digest));
1145 }
1146
1147 int i;
1148 uint8_t* arg_digest = malloc(SHA_DIGEST_SIZE);
1149 for (i = 1; i < argc; ++i) {
1150 if (args[i]->type != VAL_STRING) {
1151 fprintf(stderr, "%s(): arg %d is not a string; skipping",
1152 name, i);
1153 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1154 // Warn about bad args and skip them.
1155 fprintf(stderr, "%s(): error parsing \"%s\" as sha-1; skipping",
1156 name, args[i]->data);
1157 } else if (memcmp(digest, arg_digest, SHA_DIGEST_SIZE) == 0) {
1158 break;
1159 }
1160 FreeValue(args[i]);
1161 }
1162 if (i >= argc) {
1163 // Didn't match any of the hex strings; return false.
1164 return StringValue(strdup(""));
1165 }
1166 // Found a match; free all the remaining arguments and return the
1167 // matched one.
1168 int j;
1169 for (j = i+1; j < argc; ++j) {
1170 FreeValue(args[j]);
1171 }
1172 return args[i];
1173}
1174
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001175// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001176// is actually a FileContents*).
1177Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1178 if (argc != 1) {
1179 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1180 }
1181 char* filename;
1182 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1183
1184 Value* v = malloc(sizeof(Value));
1185 v->type = VAL_BLOB;
1186
1187 FileContents fc;
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001188 if (LoadFileContents(filename, &fc, RETOUCH_DONT_MASK) != 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001189 ErrorAbort(state, "%s() loading \"%s\" failed: %s",
1190 name, filename, strerror(errno));
1191 free(filename);
1192 free(v);
1193 free(fc.data);
1194 return NULL;
1195 }
1196
1197 v->size = fc.size;
1198 v->data = (char*)fc.data;
1199
1200 free(filename);
1201 return v;
1202}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001203
Doug Zongker9931f7f2009-06-10 14:11:53 -07001204void RegisterInstallFunctions() {
1205 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001206 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001207 RegisterFunction("unmount", UnmountFn);
1208 RegisterFunction("format", FormatFn);
1209 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001210 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001211 RegisterFunction("delete", DeleteFn);
1212 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001213 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1214 RegisterFunction("package_extract_file", PackageExtractFileFn);
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001215 RegisterFunction("retouch_binaries", RetouchBinariesFn);
1216 RegisterFunction("undo_retouch_binaries", UndoRetouchBinariesFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001217 RegisterFunction("symlink", SymlinkFn);
1218 RegisterFunction("set_perm", SetPermFn);
1219 RegisterFunction("set_perm_recursive", SetPermFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001220
1221 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001222 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001223 RegisterFunction("write_raw_image", WriteRawImageFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001224
1225 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001226 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1227 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001228
Doug Zongker512536a2010-02-17 16:11:44 -08001229 RegisterFunction("read_file", ReadFileFn);
1230 RegisterFunction("sha1_check", Sha1CheckFn);
1231
Doug Zongkerd0181b82011-10-19 10:51:12 -07001232 RegisterFunction("wipe_cache", WipeCacheFn);
1233
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001234 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001235
1236 RegisterFunction("run_program", RunProgramFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001237}