blob: c81bbb59d1c594a6803cba702311892f28125a04 [file] [log] [blame]
Doug Zongker9931f7f2009-06-10 14:11:53 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Doug Zongkerfbf3c102009-06-24 09:36:20 -070017#include <ctype.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070018#include <errno.h>
19#include <stdarg.h>
Doug Zongkerfbf3c102009-06-24 09:36:20 -070020#include <stdio.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070021#include <stdlib.h>
22#include <string.h>
23#include <sys/mount.h>
24#include <sys/stat.h>
25#include <sys/types.h>
Doug Zongkera3f89ea2009-09-10 14:10:48 -070026#include <sys/wait.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070027#include <unistd.h>
Hristo Bojinovdb314d62010-08-02 10:29:49 -070028#include <fcntl.h>
29#include <time.h>
Nick Kralevich627eb302013-07-17 19:01:37 -070030#include <selinux/selinux.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070031
Doug Zongker8edb00c2009-06-11 17:21:44 -070032#include "cutils/misc.h"
33#include "cutils/properties.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070034#include "edify/expr.h"
Doug Zongker512536a2010-02-17 16:11:44 -080035#include "mincrypt/sha.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070036#include "minzip/DirUtil.h"
37#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 char *secontext = NULL;
83
84 if (sehandle) {
85 selabel_lookup(sehandle, &secontext, mount_point, 0755);
86 setfscreatecon(secontext);
87 }
Stephen Smalley779701d2012-02-09 14:13:23 -050088
Doug Zongker9931f7f2009-06-10 14:11:53 -070089 mkdir(mount_point, 0755);
90
Stephen Smalley779701d2012-02-09 14:13:23 -050091 if (secontext) {
92 freecon(secontext);
93 setfscreatecon(NULL);
94 }
Stephen Smalley779701d2012-02-09 14:13:23 -050095
Doug Zongker3d177d02010-07-01 09:18:44 -070096 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070097 mtd_scan_partitions();
98 const MtdPartition* mtd;
99 mtd = mtd_find_partition_by_name(location);
100 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700101 printf("%s: no mtd partition named \"%s\"",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700102 name, location);
103 result = strdup("");
104 goto done;
105 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700106 if (mtd_mount_partition(mtd, mount_point, fs_type, 0 /* rw */) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700107 printf("mtd mount of %s failed: %s\n",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700108 location, strerror(errno));
109 result = strdup("");
110 goto done;
111 }
112 result = mount_point;
113 } else {
Doug Zongker3d177d02010-07-01 09:18:44 -0700114 if (mount(location, mount_point, fs_type,
Doug Zongker9931f7f2009-06-10 14:11:53 -0700115 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "") < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700116 printf("%s: failed to mount %s at %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700117 name, location, mount_point, strerror(errno));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700118 result = strdup("");
119 } else {
120 result = mount_point;
121 }
122 }
123
124done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700125 free(fs_type);
126 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700127 free(location);
128 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800129 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700130}
131
Doug Zongker8edb00c2009-06-11 17:21:44 -0700132
133// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800134Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700135 char* result = NULL;
136 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700137 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700138 }
139 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700140 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700141 return NULL;
142 }
143 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700144 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700145 goto done;
146 }
147
148 scan_mounted_volumes();
149 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
150 if (vol == NULL) {
151 result = strdup("");
152 } else {
153 result = mount_point;
154 }
155
156done:
157 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800158 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700159}
160
161
Doug Zongker512536a2010-02-17 16:11:44 -0800162Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700163 char* result = NULL;
164 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700165 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700166 }
167 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700168 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700169 return NULL;
170 }
171 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700172 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700173 goto done;
174 }
175
176 scan_mounted_volumes();
177 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
178 if (vol == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700179 printf("unmount of %s failed; no such volume\n", mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700180 result = strdup("");
181 } else {
182 unmount_mounted_volume(vol);
183 result = mount_point;
184 }
185
186done:
187 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800188 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700189}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700190
191
Stephen Smalley779701d2012-02-09 14:13:23 -0500192// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700193//
Stephen Smalley779701d2012-02-09 14:13:23 -0500194// fs_type="yaffs2" partition_type="MTD" location=partition fs_size=<bytes> mount_point=<location>
195// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800196// if fs_size == 0, then make_ext4fs uses the entire partition.
197// if fs_size > 0, that is the size to use
198// if fs_size < 0, then reserve that many bytes at the end of the partition
Doug Zongker512536a2010-02-17 16:11:44 -0800199Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700200 char* result = NULL;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400201 if (argc != 5) {
202 return ErrorAbort(state, "%s() expects 5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700203 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700204 char* fs_type;
205 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700206 char* location;
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800207 char* fs_size;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400208 char* mount_point;
Stephen Smalley779701d2012-02-09 14:13:23 -0500209
Stephen Smalley779701d2012-02-09 14:13:23 -0500210 if (ReadArgs(state, argv, 5, &fs_type, &partition_type, &location, &fs_size, &mount_point) < 0) {
211 return NULL;
212 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700213
Doug Zongker3d177d02010-07-01 09:18:44 -0700214 if (strlen(fs_type) == 0) {
215 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
216 goto done;
217 }
218 if (strlen(partition_type) == 0) {
219 ErrorAbort(state, "partition_type argument to %s() can't be empty",
220 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700221 goto done;
222 }
223 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700224 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700225 goto done;
226 }
227
Stephen Smalley516e4e22012-04-03 13:35:11 -0400228 if (strlen(mount_point) == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500229 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
230 goto done;
231 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500232
Doug Zongker3d177d02010-07-01 09:18:44 -0700233 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700234 mtd_scan_partitions();
235 const MtdPartition* mtd = mtd_find_partition_by_name(location);
236 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700237 printf("%s: no mtd partition named \"%s\"",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700238 name, location);
239 result = strdup("");
240 goto done;
241 }
242 MtdWriteContext* ctx = mtd_write_partition(mtd);
243 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700244 printf("%s: can't write \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700245 result = strdup("");
246 goto done;
247 }
248 if (mtd_erase_blocks(ctx, -1) == -1) {
249 mtd_write_close(ctx);
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700250 printf("%s: failed to erase \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700251 result = strdup("");
252 goto done;
253 }
254 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700255 printf("%s: failed to close \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700256 result = strdup("");
257 goto done;
258 }
259 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700260#ifdef USE_EXT4
261 } else if (strcmp(fs_type, "ext4") == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500262 int status = make_ext4fs(location, atoll(fs_size), mount_point, sehandle);
Doug Zongker3d177d02010-07-01 09:18:44 -0700263 if (status != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700264 printf("%s: make_ext4fs failed (%d) on %s",
Doug Zongker3d177d02010-07-01 09:18:44 -0700265 name, status, location);
266 result = strdup("");
267 goto done;
268 }
269 result = location;
270#endif
Doug Zongker9931f7f2009-06-10 14:11:53 -0700271 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700272 printf("%s: unsupported fs_type \"%s\" partition_type \"%s\"",
Doug Zongker3d177d02010-07-01 09:18:44 -0700273 name, fs_type, partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700274 }
275
276done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700277 free(fs_type);
278 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700279 if (result != location) free(location);
Doug Zongker512536a2010-02-17 16:11:44 -0800280 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700281}
282
Doug Zongker8edb00c2009-06-11 17:21:44 -0700283
Doug Zongker512536a2010-02-17 16:11:44 -0800284Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700285 char** paths = malloc(argc * sizeof(char*));
286 int i;
287 for (i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700288 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700289 if (paths[i] == NULL) {
290 int j;
291 for (j = 0; j < i; ++i) {
292 free(paths[j]);
293 }
294 free(paths);
295 return NULL;
296 }
297 }
298
299 bool recursive = (strcmp(name, "delete_recursive") == 0);
300
301 int success = 0;
302 for (i = 0; i < argc; ++i) {
303 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
304 ++success;
305 free(paths[i]);
306 }
307 free(paths);
308
309 char buffer[10];
310 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800311 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700312}
313
Doug Zongker8edb00c2009-06-11 17:21:44 -0700314
Doug Zongker512536a2010-02-17 16:11:44 -0800315Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700316 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700317 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700318 }
319 char* frac_str;
320 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700321 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700322 return NULL;
323 }
324
325 double frac = strtod(frac_str, NULL);
326 int sec = strtol(sec_str, NULL, 10);
327
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700328 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700329 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
330
Doug Zongker9931f7f2009-06-10 14:11:53 -0700331 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800332 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700333}
334
Doug Zongker512536a2010-02-17 16:11:44 -0800335Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700336 if (argc != 1) {
337 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
338 }
339 char* frac_str;
340 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
341 return NULL;
342 }
343
344 double frac = strtod(frac_str, NULL);
345
346 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
347 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
348
Doug Zongker512536a2010-02-17 16:11:44 -0800349 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700350}
351
Doug Zongker8edb00c2009-06-11 17:21:44 -0700352// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800353Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700354 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700355 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700356 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700357 }
358 char* zip_path;
359 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700360 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700361
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700362 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700363
364 // To create a consistent system image, never use the clock for timestamps.
365 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
366
367 bool success = mzExtractRecursive(za, zip_path, dest_path,
368 MZ_EXTRACT_FILES_ONLY, &timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500369 NULL, NULL, sehandle);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700370 free(zip_path);
371 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800372 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700373}
374
Doug Zongker8edb00c2009-06-11 17:21:44 -0700375
376// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800377// or
378// package_extract_file(package_path)
379// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800380// function (the char* returned is actually a FileContents*).
381Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700382 int argc, Expr* argv[]) {
Doug Zongker6aece332010-02-01 14:40:12 -0800383 if (argc != 1 && argc != 2) {
384 return ErrorAbort(state, "%s() expects 1 or 2 args, got %d",
385 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700386 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700387 bool success = false;
Doug Zongker6aece332010-02-01 14:40:12 -0800388 if (argc == 2) {
389 // The two-argument version extracts to a file.
Doug Zongker8edb00c2009-06-11 17:21:44 -0700390
Doug Zongker6aece332010-02-01 14:40:12 -0800391 char* zip_path;
392 char* dest_path;
393 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
394
395 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
396 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
397 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700398 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800399 goto done2;
400 }
401
402 FILE* f = fopen(dest_path, "wb");
403 if (f == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700404 printf("%s: can't open %s for write: %s\n",
Doug Zongker6aece332010-02-01 14:40:12 -0800405 name, dest_path, strerror(errno));
406 goto done2;
407 }
408 success = mzExtractZipEntryToFile(za, entry, fileno(f));
409 fclose(f);
410
411 done2:
412 free(zip_path);
413 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800414 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800415 } else {
416 // The one-argument version returns the contents of the file
417 // as the result.
418
419 char* zip_path;
Doug Zongker512536a2010-02-17 16:11:44 -0800420 Value* v = malloc(sizeof(Value));
421 v->type = VAL_BLOB;
422 v->size = -1;
423 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800424
425 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
426
427 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
428 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
429 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700430 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800431 goto done1;
432 }
433
Doug Zongker512536a2010-02-17 16:11:44 -0800434 v->size = mzGetZipEntryUncompLen(entry);
435 v->data = malloc(v->size);
436 if (v->data == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700437 printf("%s: failed to allocate %ld bytes for %s\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800438 name, (long)v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800439 goto done1;
440 }
441
Doug Zongker512536a2010-02-17 16:11:44 -0800442 success = mzExtractZipEntryToBuffer(za, entry,
443 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800444
445 done1:
446 free(zip_path);
447 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800448 free(v->data);
449 v->data = NULL;
450 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800451 }
Doug Zongker512536a2010-02-17 16:11:44 -0800452 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700453 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700454}
455
Doug Zongkera23075f2012-08-06 16:19:09 -0700456// Create all parent directories of name, if necessary.
457static int make_parents(char* name) {
458 char* p;
459 for (p = name + (strlen(name)-1); p > name; --p) {
460 if (*p != '/') continue;
461 *p = '\0';
462 if (make_parents(name) < 0) return -1;
463 int result = mkdir(name, 0700);
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700464 if (result == 0) printf("symlink(): created [%s]\n", name);
Doug Zongkera23075f2012-08-06 16:19:09 -0700465 *p = '/';
466 if (result == 0 || errno == EEXIST) {
467 // successfully created or already existed; we're done
468 return 0;
469 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700470 printf("failed to mkdir %s: %s\n", name, strerror(errno));
Doug Zongkera23075f2012-08-06 16:19:09 -0700471 return -1;
472 }
473 }
474 return 0;
475}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700476
Doug Zongker9931f7f2009-06-10 14:11:53 -0700477// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700478// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800479Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700480 if (argc == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700481 return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700482 }
483 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700484 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700485 if (target == NULL) return NULL;
486
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700487 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700488 if (srcs == NULL) {
489 free(target);
490 return NULL;
491 }
492
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700493 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700494 int i;
495 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700496 if (unlink(srcs[i]) < 0) {
497 if (errno != ENOENT) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700498 printf("%s: failed to remove %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700499 name, srcs[i], strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700500 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700501 }
502 }
Doug Zongkera23075f2012-08-06 16:19:09 -0700503 if (make_parents(srcs[i])) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700504 printf("%s: failed to symlink %s to %s: making parents failed\n",
Doug Zongkera23075f2012-08-06 16:19:09 -0700505 name, srcs[i], target);
506 ++bad;
507 }
Doug Zongker60babf82009-09-18 15:11:24 -0700508 if (symlink(target, srcs[i]) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700509 printf("%s: failed to symlink %s to %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700510 name, srcs[i], target, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700511 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700512 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700513 free(srcs[i]);
514 }
515 free(srcs);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700516 if (bad) {
517 return ErrorAbort(state, "%s: some symlinks failed", name);
518 }
Doug Zongker512536a2010-02-17 16:11:44 -0800519 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700520}
521
Doug Zongker8edb00c2009-06-11 17:21:44 -0700522
Doug Zongker512536a2010-02-17 16:11:44 -0800523Value* SetPermFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700524 char* result = NULL;
Nick Kralevich627eb302013-07-17 19:01:37 -0700525 bool recursive = (strcmp(name, "set_perm_recursive") == 0) || (strcmp(name, "set_perm2_recursive") == 0);
526 bool has_selabel = (strcmp(name, "set_perm2") == 0) || (strcmp(name, "set_perm2_recursive") == 0);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700527
Nick Kralevich627eb302013-07-17 19:01:37 -0700528 int min_args = 4 + (has_selabel ? 1 : 0) + (recursive ? 1 : 0);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700529 if (argc < min_args) {
Doug Zongkera23075f2012-08-06 16:19:09 -0700530 return ErrorAbort(state, "%s() expects %d+ args, got %d",
531 name, min_args, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700532 }
533
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700534 char** args = ReadVarArgs(state, argc, argv);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700535 if (args == NULL) return NULL;
536
537 char* end;
538 int i;
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700539 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700540
541 int uid = strtoul(args[0], &end, 0);
542 if (*end != '\0' || args[0][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700543 ErrorAbort(state, "%s: \"%s\" not a valid uid", name, args[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700544 goto done;
545 }
546
547 int gid = strtoul(args[1], &end, 0);
548 if (*end != '\0' || args[1][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700549 ErrorAbort(state, "%s: \"%s\" not a valid gid", name, args[1]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700550 goto done;
551 }
552
553 if (recursive) {
554 int dir_mode = strtoul(args[2], &end, 0);
555 if (*end != '\0' || args[2][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700556 ErrorAbort(state, "%s: \"%s\" not a valid dirmode", name, args[2]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700557 goto done;
558 }
559
560 int file_mode = strtoul(args[3], &end, 0);
561 if (*end != '\0' || args[3][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700562 ErrorAbort(state, "%s: \"%s\" not a valid filemode",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700563 name, args[3]);
564 goto done;
565 }
566
Nick Kralevich627eb302013-07-17 19:01:37 -0700567 char* secontext = NULL;
568 if (has_selabel) {
569 secontext = args[4];
570 }
571
572 for (i = 4 + (has_selabel ? 1 : 0); i < argc; ++i) {
573 dirSetHierarchyPermissions(args[i], uid, gid, dir_mode, file_mode, secontext);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700574 }
575 } else {
576 int mode = strtoul(args[2], &end, 0);
577 if (*end != '\0' || args[2][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700578 ErrorAbort(state, "%s: \"%s\" not a valid mode", name, args[2]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700579 goto done;
580 }
581
Nick Kralevich627eb302013-07-17 19:01:37 -0700582 char* secontext = NULL;
583 if (has_selabel) {
584 secontext = args[3];
585 }
586
587 for (i = 3 + (has_selabel ? 1 : 0); i < argc; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700588 if (chown(args[i], uid, gid) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700589 printf("%s: chown of %s to %d %d failed: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700590 name, args[i], uid, gid, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700591 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700592 }
593 if (chmod(args[i], mode) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700594 printf("%s: chmod of %s to %o failed: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700595 name, args[i], mode, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700596 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700597 }
Nick Kralevich627eb302013-07-17 19:01:37 -0700598 if (has_selabel && lsetfilecon(args[i], secontext) && (errno != ENOTSUP)) {
599 printf("%s: lsetfilecon of %s to %s failed: %s\n",
600 name, args[i], secontext, strerror(errno));
601 ++bad;
602 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700603 }
604 }
605 result = strdup("");
606
607done:
608 for (i = 0; i < argc; ++i) {
609 free(args[i]);
610 }
611 free(args);
612
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700613 if (bad) {
614 free(result);
615 return ErrorAbort(state, "%s: some changes failed", name);
616 }
Doug Zongker512536a2010-02-17 16:11:44 -0800617 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700618}
619
Doug Zongker8edb00c2009-06-11 17:21:44 -0700620
Doug Zongker512536a2010-02-17 16:11:44 -0800621Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700622 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700623 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700624 }
625 char* key;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700626 key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700627 if (key == NULL) return NULL;
628
629 char value[PROPERTY_VALUE_MAX];
630 property_get(key, value, "");
631 free(key);
632
Doug Zongker512536a2010-02-17 16:11:44 -0800633 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700634}
635
636
Doug Zongker47cace92009-06-18 10:11:50 -0700637// file_getprop(file, key)
638//
639// interprets 'file' as a getprop-style file (key=value pairs, one
640// per line, # comment lines and blank lines okay), and returns the value
641// for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800642Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700643 char* result = NULL;
644 char* buffer = NULL;
645 char* filename;
646 char* key;
647 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
648 return NULL;
649 }
650
651 struct stat st;
652 if (stat(filename, &st) < 0) {
653 ErrorAbort(state, "%s: failed to stat \"%s\": %s",
654 name, filename, strerror(errno));
655 goto done;
656 }
657
658#define MAX_FILE_GETPROP_SIZE 65536
659
660 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
661 ErrorAbort(state, "%s too large for %s (max %d)",
662 filename, name, MAX_FILE_GETPROP_SIZE);
663 goto done;
664 }
665
666 buffer = malloc(st.st_size+1);
667 if (buffer == NULL) {
Doug Zongkera23075f2012-08-06 16:19:09 -0700668 ErrorAbort(state, "%s: failed to alloc %lld bytes", name, st.st_size+1);
Doug Zongker47cace92009-06-18 10:11:50 -0700669 goto done;
670 }
671
672 FILE* f = fopen(filename, "rb");
673 if (f == NULL) {
674 ErrorAbort(state, "%s: failed to open %s: %s",
675 name, filename, strerror(errno));
676 goto done;
677 }
678
679 if (fread(buffer, 1, st.st_size, f) != st.st_size) {
Doug Zongkera23075f2012-08-06 16:19:09 -0700680 ErrorAbort(state, "%s: failed to read %lld bytes from %s",
Doug Zongker47cace92009-06-18 10:11:50 -0700681 name, st.st_size+1, filename);
682 fclose(f);
683 goto done;
684 }
685 buffer[st.st_size] = '\0';
686
687 fclose(f);
688
689 char* line = strtok(buffer, "\n");
690 do {
691 // skip whitespace at start of line
692 while (*line && isspace(*line)) ++line;
693
694 // comment or blank line: skip to next line
695 if (*line == '\0' || *line == '#') continue;
696
697 char* equal = strchr(line, '=');
698 if (equal == NULL) {
699 ErrorAbort(state, "%s: malformed line \"%s\": %s not a prop file?",
700 name, line, filename);
701 goto done;
702 }
703
704 // trim whitespace between key and '='
705 char* key_end = equal-1;
706 while (key_end > line && isspace(*key_end)) --key_end;
707 key_end[1] = '\0';
708
709 // not the key we're looking for
710 if (strcmp(key, line) != 0) continue;
711
712 // skip whitespace after the '=' to the start of the value
713 char* val_start = equal+1;
714 while(*val_start && isspace(*val_start)) ++val_start;
715
716 // trim trailing whitespace
717 char* val_end = val_start + strlen(val_start)-1;
718 while (val_end > val_start && isspace(*val_end)) --val_end;
719 val_end[1] = '\0';
720
721 result = strdup(val_start);
722 break;
723
724 } while ((line = strtok(NULL, "\n")));
725
726 if (result == NULL) result = strdup("");
727
728 done:
729 free(filename);
730 free(key);
731 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -0800732 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -0700733}
734
735
Doug Zongker8edb00c2009-06-11 17:21:44 -0700736static bool write_raw_image_cb(const unsigned char* data,
737 int data_len, void* ctx) {
738 int r = mtd_write_data((MtdWriteContext*)ctx, (const char *)data, data_len);
739 if (r == data_len) return true;
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700740 printf("%s\n", strerror(errno));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700741 return false;
742}
743
Doug Zongker179b2d92011-04-12 15:49:04 -0700744// write_raw_image(filename_or_blob, partition)
Doug Zongker512536a2010-02-17 16:11:44 -0800745Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700746 char* result = NULL;
747
Doug Zongker179b2d92011-04-12 15:49:04 -0700748 Value* partition_value;
749 Value* contents;
750 if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700751 return NULL;
752 }
753
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700754 char* partition = NULL;
Doug Zongker179b2d92011-04-12 15:49:04 -0700755 if (partition_value->type != VAL_STRING) {
756 ErrorAbort(state, "partition argument to %s must be string", name);
757 goto done;
758 }
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700759 partition = partition_value->data;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700760 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700761 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700762 goto done;
763 }
Doug Zongker179b2d92011-04-12 15:49:04 -0700764 if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700765 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700766 goto done;
767 }
768
769 mtd_scan_partitions();
770 const MtdPartition* mtd = mtd_find_partition_by_name(partition);
771 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700772 printf("%s: no mtd partition named \"%s\"\n", name, partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700773 result = strdup("");
774 goto done;
775 }
776
777 MtdWriteContext* ctx = mtd_write_partition(mtd);
778 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700779 printf("%s: can't write mtd partition \"%s\"\n",
Doug Zongker8edb00c2009-06-11 17:21:44 -0700780 name, partition);
781 result = strdup("");
782 goto done;
783 }
784
785 bool success;
786
Doug Zongker179b2d92011-04-12 15:49:04 -0700787 if (contents->type == VAL_STRING) {
788 // we're given a filename as the contents
789 char* filename = contents->data;
790 FILE* f = fopen(filename, "rb");
791 if (f == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700792 printf("%s: can't open %s: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -0700793 name, filename, strerror(errno));
794 result = strdup("");
795 goto done;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700796 }
Doug Zongker179b2d92011-04-12 15:49:04 -0700797
798 success = true;
799 char* buffer = malloc(BUFSIZ);
800 int read;
801 while (success && (read = fread(buffer, 1, BUFSIZ, f)) > 0) {
802 int wrote = mtd_write_data(ctx, buffer, read);
803 success = success && (wrote == read);
804 }
805 free(buffer);
806 fclose(f);
807 } else {
808 // we're given a blob as the contents
809 ssize_t wrote = mtd_write_data(ctx, contents->data, contents->size);
810 success = (wrote == contents->size);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700811 }
Doug Zongker179b2d92011-04-12 15:49:04 -0700812 if (!success) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700813 printf("mtd_write_data to %s failed: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -0700814 partition, strerror(errno));
815 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700816
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700817 if (mtd_erase_blocks(ctx, -1) == -1) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700818 printf("%s: error erasing blocks of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700819 }
820 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700821 printf("%s: error closing write of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700822 }
823
Doug Zongker179b2d92011-04-12 15:49:04 -0700824 printf("%s %s partition\n",
825 success ? "wrote" : "failed to write", partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700826
827 result = success ? partition : strdup("");
828
829done:
Doug Zongker179b2d92011-04-12 15:49:04 -0700830 if (result != partition) FreeValue(partition_value);
831 FreeValue(contents);
Doug Zongker512536a2010-02-17 16:11:44 -0800832 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700833}
834
Doug Zongker8edb00c2009-06-11 17:21:44 -0700835// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -0800836Value* ApplyPatchSpaceFn(const char* name, State* state,
837 int argc, Expr* argv[]) {
838 char* bytes_str;
839 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
840 return NULL;
841 }
842
843 char* endptr;
844 size_t bytes = strtol(bytes_str, &endptr, 10);
845 if (bytes == 0 && endptr == bytes_str) {
846 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n",
847 name, bytes_str);
848 free(bytes_str);
849 return NULL;
850 }
851
852 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
853}
854
855
856// apply_patch(srcfile, tgtfile, tgtsha1, tgtsize, sha1_1, patch_1, ...)
Doug Zongker512536a2010-02-17 16:11:44 -0800857Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800858 if (argc < 6 || (argc % 2) == 1) {
859 return ErrorAbort(state, "%s(): expected at least 6 args and an "
860 "even number, got %d",
861 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700862 }
863
Doug Zongkerc4351c72010-02-22 14:46:32 -0800864 char* source_filename;
865 char* target_filename;
866 char* target_sha1;
867 char* target_size_str;
868 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
869 &target_sha1, &target_size_str) < 0) {
870 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700871 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700872
Doug Zongkerc4351c72010-02-22 14:46:32 -0800873 char* endptr;
874 size_t target_size = strtol(target_size_str, &endptr, 10);
875 if (target_size == 0 && endptr == target_size_str) {
876 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count",
877 name, target_size_str);
878 free(source_filename);
879 free(target_filename);
880 free(target_sha1);
881 free(target_size_str);
882 return NULL;
883 }
884
885 int patchcount = (argc-4) / 2;
886 Value** patches = ReadValueVarArgs(state, argc-4, argv+4);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700887
888 int i;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800889 for (i = 0; i < patchcount; ++i) {
890 if (patches[i*2]->type != VAL_STRING) {
891 ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i);
892 break;
893 }
894 if (patches[i*2+1]->type != VAL_BLOB) {
895 ErrorAbort(state, "%s(): patch #%d is not blob", name, i);
896 break;
897 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700898 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800899 if (i != patchcount) {
900 for (i = 0; i < patchcount*2; ++i) {
901 FreeValue(patches[i]);
902 }
903 free(patches);
904 return NULL;
905 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700906
Doug Zongkerc4351c72010-02-22 14:46:32 -0800907 char** patch_sha_str = malloc(patchcount * sizeof(char*));
908 for (i = 0; i < patchcount; ++i) {
909 patch_sha_str[i] = patches[i*2]->data;
910 patches[i*2]->data = NULL;
911 FreeValue(patches[i*2]);
912 patches[i] = patches[i*2+1];
Doug Zongker8edb00c2009-06-11 17:21:44 -0700913 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800914
915 int result = applypatch(source_filename, target_filename,
916 target_sha1, target_size,
Doug Zongkera3ccba62012-08-20 15:28:02 -0700917 patchcount, patch_sha_str, patches, NULL);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800918
919 for (i = 0; i < patchcount; ++i) {
920 FreeValue(patches[i]);
921 }
922 free(patch_sha_str);
923 free(patches);
924
925 return StringValue(strdup(result == 0 ? "t" : ""));
926}
927
928// apply_patch_check(file, [sha1_1, ...])
929Value* ApplyPatchCheckFn(const char* name, State* state,
930 int argc, Expr* argv[]) {
931 if (argc < 1) {
932 return ErrorAbort(state, "%s(): expected at least 1 arg, got %d",
933 name, argc);
934 }
935
936 char* filename;
937 if (ReadArgs(state, argv, 1, &filename) < 0) {
938 return NULL;
939 }
940
941 int patchcount = argc-1;
942 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
943
944 int result = applypatch_check(filename, patchcount, sha1s);
945
946 int i;
947 for (i = 0; i < patchcount; ++i) {
948 free(sha1s[i]);
949 }
950 free(sha1s);
951
952 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700953}
954
Doug Zongker512536a2010-02-17 16:11:44 -0800955Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700956 char** args = ReadVarArgs(state, argc, argv);
957 if (args == NULL) {
958 return NULL;
959 }
960
961 int size = 0;
962 int i;
963 for (i = 0; i < argc; ++i) {
964 size += strlen(args[i]);
965 }
966 char* buffer = malloc(size+1);
967 size = 0;
968 for (i = 0; i < argc; ++i) {
969 strcpy(buffer+size, args[i]);
970 size += strlen(args[i]);
971 free(args[i]);
972 }
973 free(args);
974 buffer[size] = '\0';
975
976 char* line = strtok(buffer, "\n");
977 while (line) {
978 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe,
979 "ui_print %s\n", line);
980 line = strtok(NULL, "\n");
981 }
982 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "ui_print\n");
983
Doug Zongker512536a2010-02-17 16:11:44 -0800984 return StringValue(buffer);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700985}
986
Doug Zongkerd0181b82011-10-19 10:51:12 -0700987Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
988 if (argc != 0) {
989 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
990 }
991 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
992 return StringValue(strdup("t"));
993}
994
Doug Zongker512536a2010-02-17 16:11:44 -0800995Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700996 if (argc < 1) {
997 return ErrorAbort(state, "%s() expects at least 1 arg", name);
998 }
999 char** args = ReadVarArgs(state, argc, argv);
1000 if (args == NULL) {
1001 return NULL;
1002 }
1003
1004 char** args2 = malloc(sizeof(char*) * (argc+1));
1005 memcpy(args2, args, sizeof(char*) * argc);
1006 args2[argc] = NULL;
1007
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001008 printf("about to run program [%s] with %d args\n", args2[0], argc);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001009
1010 pid_t child = fork();
1011 if (child == 0) {
1012 execv(args2[0], args2);
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001013 printf("run_program: execv failed: %s\n", strerror(errno));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001014 _exit(1);
1015 }
1016 int status;
1017 waitpid(child, &status, 0);
1018 if (WIFEXITED(status)) {
1019 if (WEXITSTATUS(status) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001020 printf("run_program: child exited with status %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001021 WEXITSTATUS(status));
1022 }
1023 } else if (WIFSIGNALED(status)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001024 printf("run_program: child terminated by signal %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001025 WTERMSIG(status));
1026 }
1027
1028 int i;
1029 for (i = 0; i < argc; ++i) {
1030 free(args[i]);
1031 }
1032 free(args);
1033 free(args2);
1034
1035 char buffer[20];
1036 sprintf(buffer, "%d", status);
1037
Doug Zongker512536a2010-02-17 16:11:44 -08001038 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001039}
1040
Doug Zongker512536a2010-02-17 16:11:44 -08001041// Take a sha-1 digest and return it as a newly-allocated hex string.
1042static char* PrintSha1(uint8_t* digest) {
1043 char* buffer = malloc(SHA_DIGEST_SIZE*2 + 1);
1044 int i;
1045 const char* alphabet = "0123456789abcdef";
1046 for (i = 0; i < SHA_DIGEST_SIZE; ++i) {
1047 buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf];
1048 buffer[i*2+1] = alphabet[digest[i] & 0xf];
1049 }
1050 buffer[i*2] = '\0';
1051 return buffer;
1052}
1053
1054// sha1_check(data)
1055// to return the sha1 of the data (given in the format returned by
1056// read_file).
1057//
1058// sha1_check(data, sha1_hex, [sha1_hex, ...])
1059// returns the sha1 of the file if it matches any of the hex
1060// strings passed, or "" if it does not equal any of them.
1061//
1062Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1063 if (argc < 1) {
1064 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1065 }
1066
1067 Value** args = ReadValueVarArgs(state, argc, argv);
1068 if (args == NULL) {
1069 return NULL;
1070 }
1071
1072 if (args[0]->size < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001073 printf("%s(): no file contents received", name);
Doug Zongker512536a2010-02-17 16:11:44 -08001074 return StringValue(strdup(""));
1075 }
1076 uint8_t digest[SHA_DIGEST_SIZE];
Doug Zongkerbac7fba2013-04-10 11:32:17 -07001077 SHA_hash(args[0]->data, args[0]->size, digest);
Doug Zongker512536a2010-02-17 16:11:44 -08001078 FreeValue(args[0]);
1079
1080 if (argc == 1) {
1081 return StringValue(PrintSha1(digest));
1082 }
1083
1084 int i;
1085 uint8_t* arg_digest = malloc(SHA_DIGEST_SIZE);
1086 for (i = 1; i < argc; ++i) {
1087 if (args[i]->type != VAL_STRING) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001088 printf("%s(): arg %d is not a string; skipping",
Doug Zongker512536a2010-02-17 16:11:44 -08001089 name, i);
1090 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1091 // Warn about bad args and skip them.
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001092 printf("%s(): error parsing \"%s\" as sha-1; skipping",
1093 name, args[i]->data);
Doug Zongker512536a2010-02-17 16:11:44 -08001094 } else if (memcmp(digest, arg_digest, SHA_DIGEST_SIZE) == 0) {
1095 break;
1096 }
1097 FreeValue(args[i]);
1098 }
1099 if (i >= argc) {
1100 // Didn't match any of the hex strings; return false.
1101 return StringValue(strdup(""));
1102 }
1103 // Found a match; free all the remaining arguments and return the
1104 // matched one.
1105 int j;
1106 for (j = i+1; j < argc; ++j) {
1107 FreeValue(args[j]);
1108 }
1109 return args[i];
1110}
1111
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001112// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001113// is actually a FileContents*).
1114Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1115 if (argc != 1) {
1116 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1117 }
1118 char* filename;
1119 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1120
1121 Value* v = malloc(sizeof(Value));
1122 v->type = VAL_BLOB;
1123
1124 FileContents fc;
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001125 if (LoadFileContents(filename, &fc, RETOUCH_DONT_MASK) != 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001126 ErrorAbort(state, "%s() loading \"%s\" failed: %s",
1127 name, filename, strerror(errno));
1128 free(filename);
1129 free(v);
1130 free(fc.data);
1131 return NULL;
1132 }
1133
1134 v->size = fc.size;
1135 v->data = (char*)fc.data;
1136
1137 free(filename);
1138 return v;
1139}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001140
Doug Zongker9931f7f2009-06-10 14:11:53 -07001141void RegisterInstallFunctions() {
1142 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001143 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001144 RegisterFunction("unmount", UnmountFn);
1145 RegisterFunction("format", FormatFn);
1146 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001147 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001148 RegisterFunction("delete", DeleteFn);
1149 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001150 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1151 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001152 RegisterFunction("symlink", SymlinkFn);
1153 RegisterFunction("set_perm", SetPermFn);
1154 RegisterFunction("set_perm_recursive", SetPermFn);
Nick Kralevich627eb302013-07-17 19:01:37 -07001155 RegisterFunction("set_perm2", SetPermFn);
1156 RegisterFunction("set_perm2_recursive", SetPermFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001157
1158 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001159 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001160 RegisterFunction("write_raw_image", WriteRawImageFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001161
1162 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001163 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1164 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001165
Doug Zongker512536a2010-02-17 16:11:44 -08001166 RegisterFunction("read_file", ReadFileFn);
1167 RegisterFunction("sha1_check", Sha1CheckFn);
1168
Doug Zongkerd0181b82011-10-19 10:51:12 -07001169 RegisterFunction("wipe_cache", WipeCacheFn);
1170
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001171 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001172
1173 RegisterFunction("run_program", RunProgramFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001174}