blob: 7b7bfc9030cd08ef50f5ce4381e5fc85f39ac94c [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"
36#include "mtdutils/mounts.h"
37#include "mtdutils/mtdutils.h"
38#include "updater.h"
Doug Zongker512536a2010-02-17 16:11:44 -080039#include "applypatch/applypatch.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070040
Doug Zongker3d177d02010-07-01 09:18:44 -070041#ifdef USE_EXT4
42#include "make_ext4fs.h"
43#endif
44
45// mount(fs_type, partition_type, location, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -070046//
Doug Zongker3d177d02010-07-01 09:18:44 -070047// fs_type="yaffs2" partition_type="MTD" location=partition
48// fs_type="ext4" partition_type="EMMC" location=device
Doug Zongker512536a2010-02-17 16:11:44 -080049Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070050 char* result = NULL;
Doug Zongker3d177d02010-07-01 09:18:44 -070051 if (argc != 4) {
52 return ErrorAbort(state, "%s() expects 4 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -070053 }
Doug Zongker3d177d02010-07-01 09:18:44 -070054 char* fs_type;
55 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -070056 char* location;
57 char* mount_point;
Doug Zongker3d177d02010-07-01 09:18:44 -070058 if (ReadArgs(state, argv, 4, &fs_type, &partition_type,
59 &location, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070060 return NULL;
61 }
62
Doug Zongker3d177d02010-07-01 09:18:44 -070063 if (strlen(fs_type) == 0) {
64 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
65 goto done;
66 }
67 if (strlen(partition_type) == 0) {
68 ErrorAbort(state, "partition_type argument to %s() can't be empty",
69 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -070070 goto done;
71 }
72 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070073 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -070074 goto done;
75 }
76 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070077 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -070078 goto done;
79 }
80
81 mkdir(mount_point, 0755);
82
Doug Zongker3d177d02010-07-01 09:18:44 -070083 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070084 mtd_scan_partitions();
85 const MtdPartition* mtd;
86 mtd = mtd_find_partition_by_name(location);
87 if (mtd == NULL) {
88 fprintf(stderr, "%s: no mtd partition named \"%s\"",
89 name, location);
90 result = strdup("");
91 goto done;
92 }
Doug Zongker3d177d02010-07-01 09:18:44 -070093 if (mtd_mount_partition(mtd, mount_point, fs_type, 0 /* rw */) != 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070094 fprintf(stderr, "mtd mount of %s failed: %s\n",
95 location, strerror(errno));
96 result = strdup("");
97 goto done;
98 }
99 result = mount_point;
100 } else {
Doug Zongker3d177d02010-07-01 09:18:44 -0700101 if (mount(location, mount_point, fs_type,
Doug Zongker9931f7f2009-06-10 14:11:53 -0700102 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "") < 0) {
Doug Zongker60babf82009-09-18 15:11:24 -0700103 fprintf(stderr, "%s: failed to mount %s at %s: %s\n",
104 name, location, mount_point, strerror(errno));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700105 result = strdup("");
106 } else {
107 result = mount_point;
108 }
109 }
110
111done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700112 free(fs_type);
113 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700114 free(location);
115 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800116 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700117}
118
Doug Zongker8edb00c2009-06-11 17:21:44 -0700119
120// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800121Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700122 char* result = NULL;
123 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700124 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700125 }
126 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700127 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700128 return NULL;
129 }
130 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700131 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700132 goto done;
133 }
134
135 scan_mounted_volumes();
136 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
137 if (vol == NULL) {
138 result = strdup("");
139 } else {
140 result = mount_point;
141 }
142
143done:
144 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800145 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700146}
147
148
Doug Zongker512536a2010-02-17 16:11:44 -0800149Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700150 char* result = NULL;
151 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700152 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700153 }
154 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700155 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700156 return NULL;
157 }
158 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700159 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700160 goto done;
161 }
162
163 scan_mounted_volumes();
164 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
165 if (vol == NULL) {
166 fprintf(stderr, "unmount of %s failed; no such volume\n", mount_point);
167 result = strdup("");
168 } else {
169 unmount_mounted_volume(vol);
170 result = mount_point;
171 }
172
173done:
174 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800175 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700176}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700177
178
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800179// format(fs_type, partition_type, location, fs_size)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700180//
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800181// fs_type="yaffs2" partition_type="MTD" location=partition fs_size=<bytes>
182// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes>
183// if fs_size == 0, then make_ext4fs uses the entire partition.
184// if fs_size > 0, that is the size to use
185// if fs_size < 0, then reserve that many bytes at the end of the partition
Doug Zongker512536a2010-02-17 16:11:44 -0800186Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700187 char* result = NULL;
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800188 if (argc != 4) {
189 return ErrorAbort(state, "%s() expects 4 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700190 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700191 char* fs_type;
192 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700193 char* location;
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800194 char* fs_size;
195 if (ReadArgs(state, argv, 4, &fs_type, &partition_type, &location, &fs_size) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700196 return NULL;
197 }
198
Doug Zongker3d177d02010-07-01 09:18:44 -0700199 if (strlen(fs_type) == 0) {
200 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
201 goto done;
202 }
203 if (strlen(partition_type) == 0) {
204 ErrorAbort(state, "partition_type argument to %s() can't be empty",
205 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700206 goto done;
207 }
208 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700209 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700210 goto done;
211 }
212
Doug Zongker3d177d02010-07-01 09:18:44 -0700213 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700214 mtd_scan_partitions();
215 const MtdPartition* mtd = mtd_find_partition_by_name(location);
216 if (mtd == NULL) {
217 fprintf(stderr, "%s: no mtd partition named \"%s\"",
218 name, location);
219 result = strdup("");
220 goto done;
221 }
222 MtdWriteContext* ctx = mtd_write_partition(mtd);
223 if (ctx == NULL) {
224 fprintf(stderr, "%s: can't write \"%s\"", name, location);
225 result = strdup("");
226 goto done;
227 }
228 if (mtd_erase_blocks(ctx, -1) == -1) {
229 mtd_write_close(ctx);
230 fprintf(stderr, "%s: failed to erase \"%s\"", name, location);
231 result = strdup("");
232 goto done;
233 }
234 if (mtd_write_close(ctx) != 0) {
235 fprintf(stderr, "%s: failed to close \"%s\"", name, location);
236 result = strdup("");
237 goto done;
238 }
239 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700240#ifdef USE_EXT4
241 } else if (strcmp(fs_type, "ext4") == 0) {
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800242 int status = make_ext4fs(location, atoll(fs_size));
Doug Zongker3d177d02010-07-01 09:18:44 -0700243 if (status != 0) {
244 fprintf(stderr, "%s: make_ext4fs failed (%d) on %s",
245 name, status, location);
246 result = strdup("");
247 goto done;
248 }
249 result = location;
250#endif
Doug Zongker9931f7f2009-06-10 14:11:53 -0700251 } else {
Doug Zongker3d177d02010-07-01 09:18:44 -0700252 fprintf(stderr, "%s: unsupported fs_type \"%s\" partition_type \"%s\"",
253 name, fs_type, partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700254 }
255
256done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700257 free(fs_type);
258 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700259 if (result != location) free(location);
Doug Zongker512536a2010-02-17 16:11:44 -0800260 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700261}
262
Doug Zongker8edb00c2009-06-11 17:21:44 -0700263
Doug Zongker512536a2010-02-17 16:11:44 -0800264Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700265 char** paths = malloc(argc * sizeof(char*));
266 int i;
267 for (i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700268 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700269 if (paths[i] == NULL) {
270 int j;
271 for (j = 0; j < i; ++i) {
272 free(paths[j]);
273 }
274 free(paths);
275 return NULL;
276 }
277 }
278
279 bool recursive = (strcmp(name, "delete_recursive") == 0);
280
281 int success = 0;
282 for (i = 0; i < argc; ++i) {
283 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
284 ++success;
285 free(paths[i]);
286 }
287 free(paths);
288
289 char buffer[10];
290 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800291 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700292}
293
Doug Zongker8edb00c2009-06-11 17:21:44 -0700294
Doug Zongker512536a2010-02-17 16:11:44 -0800295Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700296 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700297 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700298 }
299 char* frac_str;
300 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700301 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700302 return NULL;
303 }
304
305 double frac = strtod(frac_str, NULL);
306 int sec = strtol(sec_str, NULL, 10);
307
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700308 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700309 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
310
Doug Zongker9931f7f2009-06-10 14:11:53 -0700311 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800312 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700313}
314
Doug Zongker512536a2010-02-17 16:11:44 -0800315Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700316 if (argc != 1) {
317 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
318 }
319 char* frac_str;
320 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
321 return NULL;
322 }
323
324 double frac = strtod(frac_str, NULL);
325
326 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
327 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
328
Doug Zongker512536a2010-02-17 16:11:44 -0800329 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700330}
331
Doug Zongker8edb00c2009-06-11 17:21:44 -0700332// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800333Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700334 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700335 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700336 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700337 }
338 char* zip_path;
339 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700340 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700341
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700342 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700343
344 // To create a consistent system image, never use the clock for timestamps.
345 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
346
347 bool success = mzExtractRecursive(za, zip_path, dest_path,
348 MZ_EXTRACT_FILES_ONLY, &timestamp,
349 NULL, NULL);
350 free(zip_path);
351 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800352 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700353}
354
Doug Zongker8edb00c2009-06-11 17:21:44 -0700355
356// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800357// or
358// package_extract_file(package_path)
359// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800360// function (the char* returned is actually a FileContents*).
361Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700362 int argc, Expr* argv[]) {
Doug Zongker6aece332010-02-01 14:40:12 -0800363 if (argc != 1 && argc != 2) {
364 return ErrorAbort(state, "%s() expects 1 or 2 args, got %d",
365 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700366 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700367 bool success = false;
Doug Zongker6aece332010-02-01 14:40:12 -0800368 if (argc == 2) {
369 // The two-argument version extracts to a file.
Doug Zongker8edb00c2009-06-11 17:21:44 -0700370
Doug Zongker6aece332010-02-01 14:40:12 -0800371 char* zip_path;
372 char* dest_path;
373 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
374
375 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
376 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
377 if (entry == NULL) {
378 fprintf(stderr, "%s: no %s in package\n", name, zip_path);
379 goto done2;
380 }
381
382 FILE* f = fopen(dest_path, "wb");
383 if (f == NULL) {
384 fprintf(stderr, "%s: can't open %s for write: %s\n",
385 name, dest_path, strerror(errno));
386 goto done2;
387 }
388 success = mzExtractZipEntryToFile(za, entry, fileno(f));
389 fclose(f);
390
391 done2:
392 free(zip_path);
393 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800394 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800395 } else {
396 // The one-argument version returns the contents of the file
397 // as the result.
398
399 char* zip_path;
Doug Zongker512536a2010-02-17 16:11:44 -0800400 Value* v = malloc(sizeof(Value));
401 v->type = VAL_BLOB;
402 v->size = -1;
403 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800404
405 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
406
407 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
408 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
409 if (entry == NULL) {
410 fprintf(stderr, "%s: no %s in package\n", name, zip_path);
411 goto done1;
412 }
413
Doug Zongker512536a2010-02-17 16:11:44 -0800414 v->size = mzGetZipEntryUncompLen(entry);
415 v->data = malloc(v->size);
416 if (v->data == NULL) {
Doug Zongker6aece332010-02-01 14:40:12 -0800417 fprintf(stderr, "%s: failed to allocate %ld bytes for %s\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800418 name, (long)v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800419 goto done1;
420 }
421
Doug Zongker512536a2010-02-17 16:11:44 -0800422 success = mzExtractZipEntryToBuffer(za, entry,
423 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800424
425 done1:
426 free(zip_path);
427 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800428 free(v->data);
429 v->data = NULL;
430 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800431 }
Doug Zongker512536a2010-02-17 16:11:44 -0800432 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700433 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700434}
435
436
Doug Zongker9931f7f2009-06-10 14:11:53 -0700437// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700438// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800439Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700440 if (argc == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700441 return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700442 }
443 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700444 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700445 if (target == NULL) return NULL;
446
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700447 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700448 if (srcs == NULL) {
449 free(target);
450 return NULL;
451 }
452
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700453 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700454 int i;
455 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700456 if (unlink(srcs[i]) < 0) {
457 if (errno != ENOENT) {
458 fprintf(stderr, "%s: failed to remove %s: %s\n",
459 name, srcs[i], strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700460 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700461 }
462 }
463 if (symlink(target, srcs[i]) < 0) {
464 fprintf(stderr, "%s: failed to symlink %s to %s: %s\n",
465 name, srcs[i], target, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700466 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700467 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700468 free(srcs[i]);
469 }
470 free(srcs);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700471 if (bad) {
472 return ErrorAbort(state, "%s: some symlinks failed", name);
473 }
Doug Zongker512536a2010-02-17 16:11:44 -0800474 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700475}
476
Doug Zongker8edb00c2009-06-11 17:21:44 -0700477
Doug Zongker512536a2010-02-17 16:11:44 -0800478Value* SetPermFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700479 char* result = NULL;
480 bool recursive = (strcmp(name, "set_perm_recursive") == 0);
481
482 int min_args = 4 + (recursive ? 1 : 0);
483 if (argc < min_args) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700484 return ErrorAbort(state, "%s() expects %d+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700485 }
486
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700487 char** args = ReadVarArgs(state, argc, argv);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700488 if (args == NULL) return NULL;
489
490 char* end;
491 int i;
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700492 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700493
494 int uid = strtoul(args[0], &end, 0);
495 if (*end != '\0' || args[0][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700496 ErrorAbort(state, "%s: \"%s\" not a valid uid", name, args[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700497 goto done;
498 }
499
500 int gid = strtoul(args[1], &end, 0);
501 if (*end != '\0' || args[1][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700502 ErrorAbort(state, "%s: \"%s\" not a valid gid", name, args[1]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700503 goto done;
504 }
505
506 if (recursive) {
507 int dir_mode = strtoul(args[2], &end, 0);
508 if (*end != '\0' || args[2][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700509 ErrorAbort(state, "%s: \"%s\" not a valid dirmode", name, args[2]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700510 goto done;
511 }
512
513 int file_mode = strtoul(args[3], &end, 0);
514 if (*end != '\0' || args[3][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700515 ErrorAbort(state, "%s: \"%s\" not a valid filemode",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700516 name, args[3]);
517 goto done;
518 }
519
520 for (i = 4; i < argc; ++i) {
521 dirSetHierarchyPermissions(args[i], uid, gid, dir_mode, file_mode);
522 }
523 } else {
524 int mode = strtoul(args[2], &end, 0);
525 if (*end != '\0' || args[2][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700526 ErrorAbort(state, "%s: \"%s\" not a valid mode", name, args[2]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700527 goto done;
528 }
529
Doug Zongker0bbfe3d2009-06-25 13:37:31 -0700530 for (i = 3; i < argc; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700531 if (chown(args[i], uid, gid) < 0) {
532 fprintf(stderr, "%s: chown of %s to %d %d failed: %s\n",
533 name, args[i], uid, gid, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700534 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700535 }
536 if (chmod(args[i], mode) < 0) {
537 fprintf(stderr, "%s: chmod of %s to %o failed: %s\n",
538 name, args[i], mode, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700539 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700540 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700541 }
542 }
543 result = strdup("");
544
545done:
546 for (i = 0; i < argc; ++i) {
547 free(args[i]);
548 }
549 free(args);
550
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700551 if (bad) {
552 free(result);
553 return ErrorAbort(state, "%s: some changes failed", name);
554 }
Doug Zongker512536a2010-02-17 16:11:44 -0800555 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700556}
557
Doug Zongker8edb00c2009-06-11 17:21:44 -0700558
Doug Zongker512536a2010-02-17 16:11:44 -0800559Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700560 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700561 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700562 }
563 char* key;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700564 key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700565 if (key == NULL) return NULL;
566
567 char value[PROPERTY_VALUE_MAX];
568 property_get(key, value, "");
569 free(key);
570
Doug Zongker512536a2010-02-17 16:11:44 -0800571 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700572}
573
574
Doug Zongker47cace92009-06-18 10:11:50 -0700575// file_getprop(file, key)
576//
577// interprets 'file' as a getprop-style file (key=value pairs, one
578// per line, # comment lines and blank lines okay), and returns the value
579// for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800580Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700581 char* result = NULL;
582 char* buffer = NULL;
583 char* filename;
584 char* key;
585 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
586 return NULL;
587 }
588
589 struct stat st;
590 if (stat(filename, &st) < 0) {
591 ErrorAbort(state, "%s: failed to stat \"%s\": %s",
592 name, filename, strerror(errno));
593 goto done;
594 }
595
596#define MAX_FILE_GETPROP_SIZE 65536
597
598 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
599 ErrorAbort(state, "%s too large for %s (max %d)",
600 filename, name, MAX_FILE_GETPROP_SIZE);
601 goto done;
602 }
603
604 buffer = malloc(st.st_size+1);
605 if (buffer == NULL) {
606 ErrorAbort(state, "%s: failed to alloc %d bytes", name, st.st_size+1);
607 goto done;
608 }
609
610 FILE* f = fopen(filename, "rb");
611 if (f == NULL) {
612 ErrorAbort(state, "%s: failed to open %s: %s",
613 name, filename, strerror(errno));
614 goto done;
615 }
616
617 if (fread(buffer, 1, st.st_size, f) != st.st_size) {
618 ErrorAbort(state, "%s: failed to read %d bytes from %s",
619 name, st.st_size+1, filename);
620 fclose(f);
621 goto done;
622 }
623 buffer[st.st_size] = '\0';
624
625 fclose(f);
626
627 char* line = strtok(buffer, "\n");
628 do {
629 // skip whitespace at start of line
630 while (*line && isspace(*line)) ++line;
631
632 // comment or blank line: skip to next line
633 if (*line == '\0' || *line == '#') continue;
634
635 char* equal = strchr(line, '=');
636 if (equal == NULL) {
637 ErrorAbort(state, "%s: malformed line \"%s\": %s not a prop file?",
638 name, line, filename);
639 goto done;
640 }
641
642 // trim whitespace between key and '='
643 char* key_end = equal-1;
644 while (key_end > line && isspace(*key_end)) --key_end;
645 key_end[1] = '\0';
646
647 // not the key we're looking for
648 if (strcmp(key, line) != 0) continue;
649
650 // skip whitespace after the '=' to the start of the value
651 char* val_start = equal+1;
652 while(*val_start && isspace(*val_start)) ++val_start;
653
654 // trim trailing whitespace
655 char* val_end = val_start + strlen(val_start)-1;
656 while (val_end > val_start && isspace(*val_end)) --val_end;
657 val_end[1] = '\0';
658
659 result = strdup(val_start);
660 break;
661
662 } while ((line = strtok(NULL, "\n")));
663
664 if (result == NULL) result = strdup("");
665
666 done:
667 free(filename);
668 free(key);
669 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -0800670 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -0700671}
672
673
Doug Zongker8edb00c2009-06-11 17:21:44 -0700674static bool write_raw_image_cb(const unsigned char* data,
675 int data_len, void* ctx) {
676 int r = mtd_write_data((MtdWriteContext*)ctx, (const char *)data, data_len);
677 if (r == data_len) return true;
678 fprintf(stderr, "%s\n", strerror(errno));
679 return false;
680}
681
Doug Zongker179b2d92011-04-12 15:49:04 -0700682// write_raw_image(filename_or_blob, partition)
Doug Zongker512536a2010-02-17 16:11:44 -0800683Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700684 char* result = NULL;
685
Doug Zongker179b2d92011-04-12 15:49:04 -0700686 Value* partition_value;
687 Value* contents;
688 if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700689 return NULL;
690 }
691
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700692 char* partition = NULL;
Doug Zongker179b2d92011-04-12 15:49:04 -0700693 if (partition_value->type != VAL_STRING) {
694 ErrorAbort(state, "partition argument to %s must be string", name);
695 goto done;
696 }
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700697 partition = partition_value->data;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700698 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700699 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700700 goto done;
701 }
Doug Zongker179b2d92011-04-12 15:49:04 -0700702 if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700703 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700704 goto done;
705 }
706
707 mtd_scan_partitions();
708 const MtdPartition* mtd = mtd_find_partition_by_name(partition);
709 if (mtd == NULL) {
710 fprintf(stderr, "%s: no mtd partition named \"%s\"\n", name, partition);
711 result = strdup("");
712 goto done;
713 }
714
715 MtdWriteContext* ctx = mtd_write_partition(mtd);
716 if (ctx == NULL) {
717 fprintf(stderr, "%s: can't write mtd partition \"%s\"\n",
718 name, partition);
719 result = strdup("");
720 goto done;
721 }
722
723 bool success;
724
Doug Zongker179b2d92011-04-12 15:49:04 -0700725 if (contents->type == VAL_STRING) {
726 // we're given a filename as the contents
727 char* filename = contents->data;
728 FILE* f = fopen(filename, "rb");
729 if (f == NULL) {
730 fprintf(stderr, "%s: can't open %s: %s\n",
731 name, filename, strerror(errno));
732 result = strdup("");
733 goto done;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700734 }
Doug Zongker179b2d92011-04-12 15:49:04 -0700735
736 success = true;
737 char* buffer = malloc(BUFSIZ);
738 int read;
739 while (success && (read = fread(buffer, 1, BUFSIZ, f)) > 0) {
740 int wrote = mtd_write_data(ctx, buffer, read);
741 success = success && (wrote == read);
742 }
743 free(buffer);
744 fclose(f);
745 } else {
746 // we're given a blob as the contents
747 ssize_t wrote = mtd_write_data(ctx, contents->data, contents->size);
748 success = (wrote == contents->size);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700749 }
Doug Zongker179b2d92011-04-12 15:49:04 -0700750 if (!success) {
751 fprintf(stderr, "mtd_write_data to %s failed: %s\n",
752 partition, strerror(errno));
753 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700754
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700755 if (mtd_erase_blocks(ctx, -1) == -1) {
756 fprintf(stderr, "%s: error erasing blocks of %s\n", name, partition);
757 }
758 if (mtd_write_close(ctx) != 0) {
759 fprintf(stderr, "%s: error closing write of %s\n", name, partition);
760 }
761
Doug Zongker179b2d92011-04-12 15:49:04 -0700762 printf("%s %s partition\n",
763 success ? "wrote" : "failed to write", partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700764
765 result = success ? partition : strdup("");
766
767done:
Doug Zongker179b2d92011-04-12 15:49:04 -0700768 if (result != partition) FreeValue(partition_value);
769 FreeValue(contents);
Doug Zongker512536a2010-02-17 16:11:44 -0800770 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700771}
772
Doug Zongker8edb00c2009-06-11 17:21:44 -0700773// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -0800774Value* ApplyPatchSpaceFn(const char* name, State* state,
775 int argc, Expr* argv[]) {
776 char* bytes_str;
777 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
778 return NULL;
779 }
780
781 char* endptr;
782 size_t bytes = strtol(bytes_str, &endptr, 10);
783 if (bytes == 0 && endptr == bytes_str) {
784 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n",
785 name, bytes_str);
786 free(bytes_str);
787 return NULL;
788 }
789
790 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
791}
792
793
794// apply_patch(srcfile, tgtfile, tgtsha1, tgtsize, sha1_1, patch_1, ...)
Doug Zongker512536a2010-02-17 16:11:44 -0800795Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800796 if (argc < 6 || (argc % 2) == 1) {
797 return ErrorAbort(state, "%s(): expected at least 6 args and an "
798 "even number, got %d",
799 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700800 }
801
Doug Zongkerc4351c72010-02-22 14:46:32 -0800802 char* source_filename;
803 char* target_filename;
804 char* target_sha1;
805 char* target_size_str;
806 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
807 &target_sha1, &target_size_str) < 0) {
808 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700809 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700810
Doug Zongkerc4351c72010-02-22 14:46:32 -0800811 char* endptr;
812 size_t target_size = strtol(target_size_str, &endptr, 10);
813 if (target_size == 0 && endptr == target_size_str) {
814 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count",
815 name, target_size_str);
816 free(source_filename);
817 free(target_filename);
818 free(target_sha1);
819 free(target_size_str);
820 return NULL;
821 }
822
823 int patchcount = (argc-4) / 2;
824 Value** patches = ReadValueVarArgs(state, argc-4, argv+4);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700825
826 int i;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800827 for (i = 0; i < patchcount; ++i) {
828 if (patches[i*2]->type != VAL_STRING) {
829 ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i);
830 break;
831 }
832 if (patches[i*2+1]->type != VAL_BLOB) {
833 ErrorAbort(state, "%s(): patch #%d is not blob", name, i);
834 break;
835 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700836 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800837 if (i != patchcount) {
838 for (i = 0; i < patchcount*2; ++i) {
839 FreeValue(patches[i]);
840 }
841 free(patches);
842 return NULL;
843 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700844
Doug Zongkerc4351c72010-02-22 14:46:32 -0800845 char** patch_sha_str = malloc(patchcount * sizeof(char*));
846 for (i = 0; i < patchcount; ++i) {
847 patch_sha_str[i] = patches[i*2]->data;
848 patches[i*2]->data = NULL;
849 FreeValue(patches[i*2]);
850 patches[i] = patches[i*2+1];
Doug Zongker8edb00c2009-06-11 17:21:44 -0700851 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800852
853 int result = applypatch(source_filename, target_filename,
854 target_sha1, target_size,
855 patchcount, patch_sha_str, patches);
856
857 for (i = 0; i < patchcount; ++i) {
858 FreeValue(patches[i]);
859 }
860 free(patch_sha_str);
861 free(patches);
862
863 return StringValue(strdup(result == 0 ? "t" : ""));
864}
865
866// apply_patch_check(file, [sha1_1, ...])
867Value* ApplyPatchCheckFn(const char* name, State* state,
868 int argc, Expr* argv[]) {
869 if (argc < 1) {
870 return ErrorAbort(state, "%s(): expected at least 1 arg, got %d",
871 name, argc);
872 }
873
874 char* filename;
875 if (ReadArgs(state, argv, 1, &filename) < 0) {
876 return NULL;
877 }
878
879 int patchcount = argc-1;
880 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
881
882 int result = applypatch_check(filename, patchcount, sha1s);
883
884 int i;
885 for (i = 0; i < patchcount; ++i) {
886 free(sha1s[i]);
887 }
888 free(sha1s);
889
890 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700891}
892
Doug Zongker512536a2010-02-17 16:11:44 -0800893Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700894 char** args = ReadVarArgs(state, argc, argv);
895 if (args == NULL) {
896 return NULL;
897 }
898
899 int size = 0;
900 int i;
901 for (i = 0; i < argc; ++i) {
902 size += strlen(args[i]);
903 }
904 char* buffer = malloc(size+1);
905 size = 0;
906 for (i = 0; i < argc; ++i) {
907 strcpy(buffer+size, args[i]);
908 size += strlen(args[i]);
909 free(args[i]);
910 }
911 free(args);
912 buffer[size] = '\0';
913
914 char* line = strtok(buffer, "\n");
915 while (line) {
916 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe,
917 "ui_print %s\n", line);
918 line = strtok(NULL, "\n");
919 }
920 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "ui_print\n");
921
Doug Zongker512536a2010-02-17 16:11:44 -0800922 return StringValue(buffer);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700923}
924
Doug Zongkerd0181b82011-10-19 10:51:12 -0700925Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
926 if (argc != 0) {
927 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
928 }
929 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
930 return StringValue(strdup("t"));
931}
932
Doug Zongker512536a2010-02-17 16:11:44 -0800933Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700934 if (argc < 1) {
935 return ErrorAbort(state, "%s() expects at least 1 arg", name);
936 }
937 char** args = ReadVarArgs(state, argc, argv);
938 if (args == NULL) {
939 return NULL;
940 }
941
942 char** args2 = malloc(sizeof(char*) * (argc+1));
943 memcpy(args2, args, sizeof(char*) * argc);
944 args2[argc] = NULL;
945
946 fprintf(stderr, "about to run program [%s] with %d args\n", args2[0], argc);
947
948 pid_t child = fork();
949 if (child == 0) {
950 execv(args2[0], args2);
951 fprintf(stderr, "run_program: execv failed: %s\n", strerror(errno));
952 _exit(1);
953 }
954 int status;
955 waitpid(child, &status, 0);
956 if (WIFEXITED(status)) {
957 if (WEXITSTATUS(status) != 0) {
958 fprintf(stderr, "run_program: child exited with status %d\n",
959 WEXITSTATUS(status));
960 }
961 } else if (WIFSIGNALED(status)) {
962 fprintf(stderr, "run_program: child terminated by signal %d\n",
963 WTERMSIG(status));
964 }
965
966 int i;
967 for (i = 0; i < argc; ++i) {
968 free(args[i]);
969 }
970 free(args);
971 free(args2);
972
973 char buffer[20];
974 sprintf(buffer, "%d", status);
975
Doug Zongker512536a2010-02-17 16:11:44 -0800976 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700977}
978
Doug Zongker512536a2010-02-17 16:11:44 -0800979// Take a sha-1 digest and return it as a newly-allocated hex string.
980static char* PrintSha1(uint8_t* digest) {
981 char* buffer = malloc(SHA_DIGEST_SIZE*2 + 1);
982 int i;
983 const char* alphabet = "0123456789abcdef";
984 for (i = 0; i < SHA_DIGEST_SIZE; ++i) {
985 buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf];
986 buffer[i*2+1] = alphabet[digest[i] & 0xf];
987 }
988 buffer[i*2] = '\0';
989 return buffer;
990}
991
992// sha1_check(data)
993// to return the sha1 of the data (given in the format returned by
994// read_file).
995//
996// sha1_check(data, sha1_hex, [sha1_hex, ...])
997// returns the sha1 of the file if it matches any of the hex
998// strings passed, or "" if it does not equal any of them.
999//
1000Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1001 if (argc < 1) {
1002 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1003 }
1004
1005 Value** args = ReadValueVarArgs(state, argc, argv);
1006 if (args == NULL) {
1007 return NULL;
1008 }
1009
1010 if (args[0]->size < 0) {
1011 fprintf(stderr, "%s(): no file contents received", name);
1012 return StringValue(strdup(""));
1013 }
1014 uint8_t digest[SHA_DIGEST_SIZE];
1015 SHA(args[0]->data, args[0]->size, digest);
1016 FreeValue(args[0]);
1017
1018 if (argc == 1) {
1019 return StringValue(PrintSha1(digest));
1020 }
1021
1022 int i;
1023 uint8_t* arg_digest = malloc(SHA_DIGEST_SIZE);
1024 for (i = 1; i < argc; ++i) {
1025 if (args[i]->type != VAL_STRING) {
1026 fprintf(stderr, "%s(): arg %d is not a string; skipping",
1027 name, i);
1028 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1029 // Warn about bad args and skip them.
1030 fprintf(stderr, "%s(): error parsing \"%s\" as sha-1; skipping",
1031 name, args[i]->data);
1032 } else if (memcmp(digest, arg_digest, SHA_DIGEST_SIZE) == 0) {
1033 break;
1034 }
1035 FreeValue(args[i]);
1036 }
1037 if (i >= argc) {
1038 // Didn't match any of the hex strings; return false.
1039 return StringValue(strdup(""));
1040 }
1041 // Found a match; free all the remaining arguments and return the
1042 // matched one.
1043 int j;
1044 for (j = i+1; j < argc; ++j) {
1045 FreeValue(args[j]);
1046 }
1047 return args[i];
1048}
1049
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001050// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001051// is actually a FileContents*).
1052Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1053 if (argc != 1) {
1054 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1055 }
1056 char* filename;
1057 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1058
1059 Value* v = malloc(sizeof(Value));
1060 v->type = VAL_BLOB;
1061
1062 FileContents fc;
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001063 if (LoadFileContents(filename, &fc, RETOUCH_DONT_MASK) != 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001064 ErrorAbort(state, "%s() loading \"%s\" failed: %s",
1065 name, filename, strerror(errno));
1066 free(filename);
1067 free(v);
1068 free(fc.data);
1069 return NULL;
1070 }
1071
1072 v->size = fc.size;
1073 v->data = (char*)fc.data;
1074
1075 free(filename);
1076 return v;
1077}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001078
Doug Zongker9931f7f2009-06-10 14:11:53 -07001079void RegisterInstallFunctions() {
1080 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001081 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001082 RegisterFunction("unmount", UnmountFn);
1083 RegisterFunction("format", FormatFn);
1084 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001085 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001086 RegisterFunction("delete", DeleteFn);
1087 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001088 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1089 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001090 RegisterFunction("symlink", SymlinkFn);
1091 RegisterFunction("set_perm", SetPermFn);
1092 RegisterFunction("set_perm_recursive", SetPermFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001093
1094 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001095 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001096 RegisterFunction("write_raw_image", WriteRawImageFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001097
1098 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001099 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1100 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001101
Doug Zongker512536a2010-02-17 16:11:44 -08001102 RegisterFunction("read_file", ReadFileFn);
1103 RegisterFunction("sha1_check", Sha1CheckFn);
1104
Doug Zongkerd0181b82011-10-19 10:51:12 -07001105 RegisterFunction("wipe_cache", WipeCacheFn);
1106
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001107 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001108
1109 RegisterFunction("run_program", RunProgramFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001110}