blob: 8d2a5b10b3730362beb675a970de1978eb217f39 [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>
28
Doug Zongker8edb00c2009-06-11 17:21:44 -070029#include "cutils/misc.h"
30#include "cutils/properties.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070031#include "edify/expr.h"
Doug Zongker512536a2010-02-17 16:11:44 -080032#include "mincrypt/sha.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070033#include "minzip/DirUtil.h"
Koushik Dutta7c1bffe2010-12-18 19:44:33 -080034#include "mounts.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070035#include "mtdutils/mtdutils.h"
36#include "updater.h"
Doug Zongker512536a2010-02-17 16:11:44 -080037#include "applypatch/applypatch.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070038
Koushik Duttacf5195a2011-05-29 18:45:42 -070039#include "flashutils/flashutils.h"
40
Doug Zongker56c51052010-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 Zongker56c51052010-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 Zongker56c51052010-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 Zongker56c51052010-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 Zongker56c51052010-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 Zongker56c51052010-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
Koushik Dutta8637c732010-12-18 18:18:44 -080083 if (strcmp(partition_type, "MTD") == 0) {
84 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);
Doug Zongker9931f7f2009-06-10 14:11:53 -070090 result = strdup("");
Koushik Dutta8637c732010-12-18 18:18:44 -080091 goto done;
92 }
93 if (mtd_mount_partition(mtd, mount_point, fs_type, 0 /* rw */) != 0) {
94 fprintf(stderr, "mtd mount of %s failed: %s\n",
95 location, strerror(errno));
96 result = strdup("");
97 goto done;
98 }
99 result = mount_point;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700100 } else {
Doug Zongker56c51052010-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 Zongker56c51052010-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
Doug Zongker56c51052010-07-01 09:18:44 -0700179// format(fs_type, partition_type, location)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700180//
Doug Zongker56c51052010-07-01 09:18:44 -0700181// fs_type="yaffs2" partition_type="MTD" location=partition
182// fs_type="ext4" partition_type="EMMC" location=device
Doug Zongker512536a2010-02-17 16:11:44 -0800183Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700184 char* result = NULL;
Doug Zongker56c51052010-07-01 09:18:44 -0700185 if (argc != 3) {
186 return ErrorAbort(state, "%s() expects 3 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700187 }
Doug Zongker56c51052010-07-01 09:18:44 -0700188 char* fs_type;
189 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700190 char* location;
Doug Zongker56c51052010-07-01 09:18:44 -0700191 if (ReadArgs(state, argv, 3, &fs_type, &partition_type, &location) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700192 return NULL;
193 }
194
Doug Zongker56c51052010-07-01 09:18:44 -0700195 if (strlen(fs_type) == 0) {
196 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
197 goto done;
198 }
199 if (strlen(partition_type) == 0) {
200 ErrorAbort(state, "partition_type argument to %s() can't be empty",
201 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700202 goto done;
203 }
204 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700205 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700206 goto done;
207 }
208
Koushik Dutta8637c732010-12-18 18:18:44 -0800209 if (strcmp(partition_type, "MTD") == 0) {
210 mtd_scan_partitions();
211 const MtdPartition* mtd = mtd_find_partition_by_name(location);
212 if (mtd == NULL) {
213 fprintf(stderr, "%s: no mtd partition named \"%s\"",
214 name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700215 result = strdup("");
216 goto done;
217 }
Koushik Dutta8637c732010-12-18 18:18:44 -0800218 MtdWriteContext* ctx = mtd_write_partition(mtd);
219 if (ctx == NULL) {
220 fprintf(stderr, "%s: can't write \"%s\"", name, location);
221 result = strdup("");
222 goto done;
223 }
224 if (mtd_erase_blocks(ctx, -1) == -1) {
225 mtd_write_close(ctx);
226 fprintf(stderr, "%s: failed to erase \"%s\"", name, location);
227 result = strdup("");
228 goto done;
229 }
230 if (mtd_write_close(ctx) != 0) {
231 fprintf(stderr, "%s: failed to close \"%s\"", name, location);
232 result = strdup("");
233 goto done;
234 }
235 result = location;
Doug Zongker56c51052010-07-01 09:18:44 -0700236#ifdef USE_EXT4
237 } else if (strcmp(fs_type, "ext4") == 0) {
238 reset_ext4fs_info();
Brian Swetland792b0072010-09-15 18:03:58 -0700239 int status = make_ext4fs(location, NULL, NULL, 0, 0, 0);
Doug Zongker56c51052010-07-01 09:18:44 -0700240 if (status != 0) {
241 fprintf(stderr, "%s: make_ext4fs failed (%d) on %s",
242 name, status, location);
243 result = strdup("");
244 goto done;
245 }
246 result = location;
247#endif
Koushik Duttab4c5fd62011-01-02 22:54:31 -0800248 } else if (strcmp(fs_type, "ext2") == 0) {
249 int status = format_ext2_device(location);
250 if (status != 0) {
251 fprintf(stderr, "%s: format_ext2_device failed (%d) on %s",
252 name, status, location);
253 result = strdup("");
254 goto done;
255 }
256 result = location;
257 } else if (strcmp(fs_type, "ext3") == 0) {
258 int status = format_ext3_device(location);
259 if (status != 0) {
260 fprintf(stderr, "%s: format_ext3_device failed (%d) on %s",
261 name, status, location);
262 result = strdup("");
263 goto done;
264 }
265 result = location;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700266 } else {
Doug Zongker56c51052010-07-01 09:18:44 -0700267 fprintf(stderr, "%s: unsupported fs_type \"%s\" partition_type \"%s\"",
268 name, fs_type, partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700269 }
Koushik Dutta8637c732010-12-18 18:18:44 -0800270
Doug Zongker9931f7f2009-06-10 14:11:53 -0700271done:
Doug Zongker56c51052010-07-01 09:18:44 -0700272 free(fs_type);
273 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700274 if (result != location) free(location);
Doug Zongker512536a2010-02-17 16:11:44 -0800275 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700276}
277
Doug Zongker8edb00c2009-06-11 17:21:44 -0700278
Doug Zongker512536a2010-02-17 16:11:44 -0800279Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700280 char** paths = malloc(argc * sizeof(char*));
281 int i;
282 for (i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700283 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700284 if (paths[i] == NULL) {
285 int j;
286 for (j = 0; j < i; ++i) {
287 free(paths[j]);
288 }
289 free(paths);
290 return NULL;
291 }
292 }
293
294 bool recursive = (strcmp(name, "delete_recursive") == 0);
295
296 int success = 0;
297 for (i = 0; i < argc; ++i) {
298 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
299 ++success;
300 free(paths[i]);
301 }
302 free(paths);
303
304 char buffer[10];
305 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800306 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700307}
308
Doug Zongker8edb00c2009-06-11 17:21:44 -0700309
Doug Zongker512536a2010-02-17 16:11:44 -0800310Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700311 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700312 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700313 }
314 char* frac_str;
315 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700316 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700317 return NULL;
318 }
319
320 double frac = strtod(frac_str, NULL);
321 int sec = strtol(sec_str, NULL, 10);
322
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700323 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700324 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
325
Doug Zongker9931f7f2009-06-10 14:11:53 -0700326 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800327 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700328}
329
Doug Zongker512536a2010-02-17 16:11:44 -0800330Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700331 if (argc != 1) {
332 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
333 }
334 char* frac_str;
335 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
336 return NULL;
337 }
338
339 double frac = strtod(frac_str, NULL);
340
341 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
342 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
343
Doug Zongker512536a2010-02-17 16:11:44 -0800344 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700345}
346
Doug Zongker8edb00c2009-06-11 17:21:44 -0700347// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800348Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700349 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700350 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700351 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700352 }
353 char* zip_path;
354 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700355 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700356
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700357 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700358
359 // To create a consistent system image, never use the clock for timestamps.
360 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
361
362 bool success = mzExtractRecursive(za, zip_path, dest_path,
363 MZ_EXTRACT_FILES_ONLY, &timestamp,
364 NULL, NULL);
365 free(zip_path);
366 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800367 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700368}
369
Doug Zongker8edb00c2009-06-11 17:21:44 -0700370
371// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800372// or
373// package_extract_file(package_path)
374// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800375// function (the char* returned is actually a FileContents*).
376Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700377 int argc, Expr* argv[]) {
Doug Zongker6aece332010-02-01 14:40:12 -0800378 if (argc != 1 && argc != 2) {
379 return ErrorAbort(state, "%s() expects 1 or 2 args, got %d",
380 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700381 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700382 bool success = false;
Doug Zongker6aece332010-02-01 14:40:12 -0800383 if (argc == 2) {
384 // The two-argument version extracts to a file.
Doug Zongker8edb00c2009-06-11 17:21:44 -0700385
Doug Zongker6aece332010-02-01 14:40:12 -0800386 char* zip_path;
387 char* dest_path;
388 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
389
390 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
391 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
392 if (entry == NULL) {
393 fprintf(stderr, "%s: no %s in package\n", name, zip_path);
394 goto done2;
395 }
396
397 FILE* f = fopen(dest_path, "wb");
398 if (f == NULL) {
399 fprintf(stderr, "%s: can't open %s for write: %s\n",
400 name, dest_path, strerror(errno));
401 goto done2;
402 }
403 success = mzExtractZipEntryToFile(za, entry, fileno(f));
404 fclose(f);
405
406 done2:
407 free(zip_path);
408 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800409 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800410 } else {
411 // The one-argument version returns the contents of the file
412 // as the result.
413
414 char* zip_path;
Doug Zongker512536a2010-02-17 16:11:44 -0800415 Value* v = malloc(sizeof(Value));
416 v->type = VAL_BLOB;
417 v->size = -1;
418 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800419
420 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
421
422 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
423 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
424 if (entry == NULL) {
425 fprintf(stderr, "%s: no %s in package\n", name, zip_path);
426 goto done1;
427 }
428
Doug Zongker512536a2010-02-17 16:11:44 -0800429 v->size = mzGetZipEntryUncompLen(entry);
430 v->data = malloc(v->size);
431 if (v->data == NULL) {
Doug Zongker6aece332010-02-01 14:40:12 -0800432 fprintf(stderr, "%s: failed to allocate %ld bytes for %s\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800433 name, (long)v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800434 goto done1;
435 }
436
Doug Zongker512536a2010-02-17 16:11:44 -0800437 success = mzExtractZipEntryToBuffer(za, entry,
438 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800439
440 done1:
441 free(zip_path);
442 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800443 free(v->data);
444 v->data = NULL;
445 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800446 }
Doug Zongker512536a2010-02-17 16:11:44 -0800447 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700448 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700449}
450
451
Doug Zongker9931f7f2009-06-10 14:11:53 -0700452// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700453// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800454Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700455 if (argc == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700456 return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700457 }
458 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700459 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700460 if (target == NULL) return NULL;
461
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700462 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700463 if (srcs == NULL) {
464 free(target);
465 return NULL;
466 }
467
468 int i;
469 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700470 if (unlink(srcs[i]) < 0) {
471 if (errno != ENOENT) {
472 fprintf(stderr, "%s: failed to remove %s: %s\n",
473 name, srcs[i], strerror(errno));
474 }
475 }
476 if (symlink(target, srcs[i]) < 0) {
477 fprintf(stderr, "%s: failed to symlink %s to %s: %s\n",
478 name, srcs[i], target, strerror(errno));
479 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700480 free(srcs[i]);
481 }
482 free(srcs);
Doug Zongker512536a2010-02-17 16:11:44 -0800483 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700484}
485
Doug Zongker8edb00c2009-06-11 17:21:44 -0700486
Doug Zongker512536a2010-02-17 16:11:44 -0800487Value* SetPermFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700488 char* result = NULL;
489 bool recursive = (strcmp(name, "set_perm_recursive") == 0);
490
491 int min_args = 4 + (recursive ? 1 : 0);
492 if (argc < min_args) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700493 return ErrorAbort(state, "%s() expects %d+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700494 }
495
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700496 char** args = ReadVarArgs(state, argc, argv);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700497 if (args == NULL) return NULL;
498
499 char* end;
500 int i;
501
502 int uid = strtoul(args[0], &end, 0);
503 if (*end != '\0' || args[0][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700504 ErrorAbort(state, "%s: \"%s\" not a valid uid", name, args[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700505 goto done;
506 }
507
508 int gid = strtoul(args[1], &end, 0);
509 if (*end != '\0' || args[1][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700510 ErrorAbort(state, "%s: \"%s\" not a valid gid", name, args[1]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700511 goto done;
512 }
513
514 if (recursive) {
515 int dir_mode = strtoul(args[2], &end, 0);
516 if (*end != '\0' || args[2][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700517 ErrorAbort(state, "%s: \"%s\" not a valid dirmode", name, args[2]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700518 goto done;
519 }
520
521 int file_mode = strtoul(args[3], &end, 0);
522 if (*end != '\0' || args[3][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700523 ErrorAbort(state, "%s: \"%s\" not a valid filemode",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700524 name, args[3]);
525 goto done;
526 }
527
528 for (i = 4; i < argc; ++i) {
529 dirSetHierarchyPermissions(args[i], uid, gid, dir_mode, file_mode);
530 }
531 } else {
532 int mode = strtoul(args[2], &end, 0);
533 if (*end != '\0' || args[2][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700534 ErrorAbort(state, "%s: \"%s\" not a valid mode", name, args[2]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700535 goto done;
536 }
537
Doug Zongker0bbfe3d2009-06-25 13:37:31 -0700538 for (i = 3; i < argc; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700539 if (chown(args[i], uid, gid) < 0) {
540 fprintf(stderr, "%s: chown of %s to %d %d failed: %s\n",
541 name, args[i], uid, gid, strerror(errno));
542 }
543 if (chmod(args[i], mode) < 0) {
544 fprintf(stderr, "%s: chmod of %s to %o failed: %s\n",
545 name, args[i], mode, strerror(errno));
546 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700547 }
548 }
549 result = strdup("");
550
551done:
552 for (i = 0; i < argc; ++i) {
553 free(args[i]);
554 }
555 free(args);
556
Doug Zongker512536a2010-02-17 16:11:44 -0800557 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700558}
559
Doug Zongker8edb00c2009-06-11 17:21:44 -0700560
Doug Zongker512536a2010-02-17 16:11:44 -0800561Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700562 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700563 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700564 }
565 char* key;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700566 key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700567 if (key == NULL) return NULL;
568
569 char value[PROPERTY_VALUE_MAX];
570 property_get(key, value, "");
571 free(key);
572
Doug Zongker512536a2010-02-17 16:11:44 -0800573 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700574}
575
576
Doug Zongker47cace92009-06-18 10:11:50 -0700577// file_getprop(file, key)
578//
579// interprets 'file' as a getprop-style file (key=value pairs, one
580// per line, # comment lines and blank lines okay), and returns the value
581// for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800582Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700583 char* result = NULL;
584 char* buffer = NULL;
585 char* filename;
586 char* key;
587 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
588 return NULL;
589 }
590
591 struct stat st;
592 if (stat(filename, &st) < 0) {
593 ErrorAbort(state, "%s: failed to stat \"%s\": %s",
594 name, filename, strerror(errno));
595 goto done;
596 }
597
598#define MAX_FILE_GETPROP_SIZE 65536
599
600 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
601 ErrorAbort(state, "%s too large for %s (max %d)",
602 filename, name, MAX_FILE_GETPROP_SIZE);
603 goto done;
604 }
605
606 buffer = malloc(st.st_size+1);
607 if (buffer == NULL) {
608 ErrorAbort(state, "%s: failed to alloc %d bytes", name, st.st_size+1);
609 goto done;
610 }
611
612 FILE* f = fopen(filename, "rb");
613 if (f == NULL) {
614 ErrorAbort(state, "%s: failed to open %s: %s",
615 name, filename, strerror(errno));
616 goto done;
617 }
618
619 if (fread(buffer, 1, st.st_size, f) != st.st_size) {
620 ErrorAbort(state, "%s: failed to read %d bytes from %s",
621 name, st.st_size+1, filename);
622 fclose(f);
623 goto done;
624 }
625 buffer[st.st_size] = '\0';
626
627 fclose(f);
628
629 char* line = strtok(buffer, "\n");
630 do {
631 // skip whitespace at start of line
632 while (*line && isspace(*line)) ++line;
633
634 // comment or blank line: skip to next line
635 if (*line == '\0' || *line == '#') continue;
636
637 char* equal = strchr(line, '=');
638 if (equal == NULL) {
639 ErrorAbort(state, "%s: malformed line \"%s\": %s not a prop file?",
640 name, line, filename);
641 goto done;
642 }
643
644 // trim whitespace between key and '='
645 char* key_end = equal-1;
646 while (key_end > line && isspace(*key_end)) --key_end;
647 key_end[1] = '\0';
648
649 // not the key we're looking for
650 if (strcmp(key, line) != 0) continue;
651
652 // skip whitespace after the '=' to the start of the value
653 char* val_start = equal+1;
654 while(*val_start && isspace(*val_start)) ++val_start;
655
656 // trim trailing whitespace
657 char* val_end = val_start + strlen(val_start)-1;
658 while (val_end > val_start && isspace(*val_end)) --val_end;
659 val_end[1] = '\0';
660
661 result = strdup(val_start);
662 break;
663
664 } while ((line = strtok(NULL, "\n")));
665
666 if (result == NULL) result = strdup("");
667
668 done:
669 free(filename);
670 free(key);
671 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -0800672 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -0700673}
674
675
Doug Zongker8edb00c2009-06-11 17:21:44 -0700676static bool write_raw_image_cb(const unsigned char* data,
677 int data_len, void* ctx) {
678 int r = mtd_write_data((MtdWriteContext*)ctx, (const char *)data, data_len);
679 if (r == data_len) return true;
680 fprintf(stderr, "%s\n", strerror(errno));
681 return false;
682}
683
684// write_raw_image(file, partition)
Doug Zongker512536a2010-02-17 16:11:44 -0800685Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700686 char* result = NULL;
687
688 char* partition;
689 char* filename;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700690 if (ReadArgs(state, argv, 2, &filename, &partition) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700691 return NULL;
692 }
693
694 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700695 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700696 goto done;
697 }
698 if (strlen(filename) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700699 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700700 goto done;
701 }
702
Koushik Duttacf5195a2011-05-29 18:45:42 -0700703 if (0 == restore_raw_partition(NULL, partition, filename))
Koushik Dutta1b867542010-11-10 23:52:09 -0800704 result = strdup(partition);
705 else
Koushik Dutta7c1bffe2010-12-18 19:44:33 -0800706 result = strdup("");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700707
708done:
709 if (result != partition) free(partition);
710 free(filename);
Doug Zongker512536a2010-02-17 16:11:44 -0800711 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700712}
713
Doug Zongker8edb00c2009-06-11 17:21:44 -0700714// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -0800715Value* ApplyPatchSpaceFn(const char* name, State* state,
716 int argc, Expr* argv[]) {
717 char* bytes_str;
718 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
719 return NULL;
720 }
721
722 char* endptr;
723 size_t bytes = strtol(bytes_str, &endptr, 10);
724 if (bytes == 0 && endptr == bytes_str) {
725 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n",
726 name, bytes_str);
727 free(bytes_str);
728 return NULL;
729 }
730
731 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
732}
733
734
735// apply_patch(srcfile, tgtfile, tgtsha1, tgtsize, sha1_1, patch_1, ...)
Doug Zongker512536a2010-02-17 16:11:44 -0800736Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800737 if (argc < 6 || (argc % 2) == 1) {
738 return ErrorAbort(state, "%s(): expected at least 6 args and an "
739 "even number, got %d",
740 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700741 }
742
Doug Zongkerc4351c72010-02-22 14:46:32 -0800743 char* source_filename;
744 char* target_filename;
745 char* target_sha1;
746 char* target_size_str;
747 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
748 &target_sha1, &target_size_str) < 0) {
749 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700750 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700751
Doug Zongkerc4351c72010-02-22 14:46:32 -0800752 char* endptr;
753 size_t target_size = strtol(target_size_str, &endptr, 10);
754 if (target_size == 0 && endptr == target_size_str) {
755 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count",
756 name, target_size_str);
757 free(source_filename);
758 free(target_filename);
759 free(target_sha1);
760 free(target_size_str);
761 return NULL;
762 }
763
764 int patchcount = (argc-4) / 2;
765 Value** patches = ReadValueVarArgs(state, argc-4, argv+4);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700766
767 int i;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800768 for (i = 0; i < patchcount; ++i) {
769 if (patches[i*2]->type != VAL_STRING) {
770 ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i);
771 break;
772 }
773 if (patches[i*2+1]->type != VAL_BLOB) {
774 ErrorAbort(state, "%s(): patch #%d is not blob", name, i);
775 break;
776 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700777 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800778 if (i != patchcount) {
779 for (i = 0; i < patchcount*2; ++i) {
780 FreeValue(patches[i]);
781 }
782 free(patches);
783 return NULL;
784 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700785
Doug Zongkerc4351c72010-02-22 14:46:32 -0800786 char** patch_sha_str = malloc(patchcount * sizeof(char*));
787 for (i = 0; i < patchcount; ++i) {
788 patch_sha_str[i] = patches[i*2]->data;
789 patches[i*2]->data = NULL;
790 FreeValue(patches[i*2]);
791 patches[i] = patches[i*2+1];
Doug Zongker8edb00c2009-06-11 17:21:44 -0700792 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800793
794 int result = applypatch(source_filename, target_filename,
795 target_sha1, target_size,
796 patchcount, patch_sha_str, patches);
797
798 for (i = 0; i < patchcount; ++i) {
799 FreeValue(patches[i]);
800 }
801 free(patch_sha_str);
802 free(patches);
803
804 return StringValue(strdup(result == 0 ? "t" : ""));
805}
806
807// apply_patch_check(file, [sha1_1, ...])
808Value* ApplyPatchCheckFn(const char* name, State* state,
809 int argc, Expr* argv[]) {
810 if (argc < 1) {
811 return ErrorAbort(state, "%s(): expected at least 1 arg, got %d",
812 name, argc);
813 }
814
815 char* filename;
816 if (ReadArgs(state, argv, 1, &filename) < 0) {
817 return NULL;
818 }
819
820 int patchcount = argc-1;
821 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
822
823 int result = applypatch_check(filename, patchcount, sha1s);
824
825 int i;
826 for (i = 0; i < patchcount; ++i) {
827 free(sha1s[i]);
828 }
829 free(sha1s);
830
831 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700832}
833
Doug Zongker512536a2010-02-17 16:11:44 -0800834Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700835 char** args = ReadVarArgs(state, argc, argv);
836 if (args == NULL) {
837 return NULL;
838 }
839
840 int size = 0;
841 int i;
842 for (i = 0; i < argc; ++i) {
843 size += strlen(args[i]);
844 }
845 char* buffer = malloc(size+1);
846 size = 0;
847 for (i = 0; i < argc; ++i) {
848 strcpy(buffer+size, args[i]);
849 size += strlen(args[i]);
850 free(args[i]);
851 }
852 free(args);
853 buffer[size] = '\0';
854
855 char* line = strtok(buffer, "\n");
856 while (line) {
857 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe,
858 "ui_print %s\n", line);
859 line = strtok(NULL, "\n");
860 }
861 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "ui_print\n");
862
Doug Zongker512536a2010-02-17 16:11:44 -0800863 return StringValue(buffer);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700864}
865
Doug Zongker512536a2010-02-17 16:11:44 -0800866Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700867 if (argc < 1) {
868 return ErrorAbort(state, "%s() expects at least 1 arg", name);
869 }
870 char** args = ReadVarArgs(state, argc, argv);
871 if (args == NULL) {
872 return NULL;
873 }
874
875 char** args2 = malloc(sizeof(char*) * (argc+1));
876 memcpy(args2, args, sizeof(char*) * argc);
877 args2[argc] = NULL;
878
879 fprintf(stderr, "about to run program [%s] with %d args\n", args2[0], argc);
880
881 pid_t child = fork();
882 if (child == 0) {
883 execv(args2[0], args2);
884 fprintf(stderr, "run_program: execv failed: %s\n", strerror(errno));
885 _exit(1);
886 }
887 int status;
888 waitpid(child, &status, 0);
889 if (WIFEXITED(status)) {
890 if (WEXITSTATUS(status) != 0) {
891 fprintf(stderr, "run_program: child exited with status %d\n",
892 WEXITSTATUS(status));
893 }
894 } else if (WIFSIGNALED(status)) {
895 fprintf(stderr, "run_program: child terminated by signal %d\n",
896 WTERMSIG(status));
897 }
898
899 int i;
900 for (i = 0; i < argc; ++i) {
901 free(args[i]);
902 }
903 free(args);
904 free(args2);
905
906 char buffer[20];
907 sprintf(buffer, "%d", status);
908
Doug Zongker512536a2010-02-17 16:11:44 -0800909 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700910}
911
Doug Zongker512536a2010-02-17 16:11:44 -0800912// Take a sha-1 digest and return it as a newly-allocated hex string.
913static char* PrintSha1(uint8_t* digest) {
914 char* buffer = malloc(SHA_DIGEST_SIZE*2 + 1);
915 int i;
916 const char* alphabet = "0123456789abcdef";
917 for (i = 0; i < SHA_DIGEST_SIZE; ++i) {
918 buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf];
919 buffer[i*2+1] = alphabet[digest[i] & 0xf];
920 }
921 buffer[i*2] = '\0';
922 return buffer;
923}
924
925// sha1_check(data)
926// to return the sha1 of the data (given in the format returned by
927// read_file).
928//
929// sha1_check(data, sha1_hex, [sha1_hex, ...])
930// returns the sha1 of the file if it matches any of the hex
931// strings passed, or "" if it does not equal any of them.
932//
933Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
934 if (argc < 1) {
935 return ErrorAbort(state, "%s() expects at least 1 arg", name);
936 }
937
938 Value** args = ReadValueVarArgs(state, argc, argv);
939 if (args == NULL) {
940 return NULL;
941 }
942
943 if (args[0]->size < 0) {
944 fprintf(stderr, "%s(): no file contents received", name);
945 return StringValue(strdup(""));
946 }
947 uint8_t digest[SHA_DIGEST_SIZE];
948 SHA(args[0]->data, args[0]->size, digest);
949 FreeValue(args[0]);
950
951 if (argc == 1) {
952 return StringValue(PrintSha1(digest));
953 }
954
955 int i;
956 uint8_t* arg_digest = malloc(SHA_DIGEST_SIZE);
957 for (i = 1; i < argc; ++i) {
958 if (args[i]->type != VAL_STRING) {
959 fprintf(stderr, "%s(): arg %d is not a string; skipping",
960 name, i);
961 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
962 // Warn about bad args and skip them.
963 fprintf(stderr, "%s(): error parsing \"%s\" as sha-1; skipping",
964 name, args[i]->data);
965 } else if (memcmp(digest, arg_digest, SHA_DIGEST_SIZE) == 0) {
966 break;
967 }
968 FreeValue(args[i]);
969 }
970 if (i >= argc) {
971 // Didn't match any of the hex strings; return false.
972 return StringValue(strdup(""));
973 }
974 // Found a match; free all the remaining arguments and return the
975 // matched one.
976 int j;
977 for (j = i+1; j < argc; ++j) {
978 FreeValue(args[j]);
979 }
980 return args[i];
981}
982
983// Read a local file and return its contents (the char* returned
984// is actually a FileContents*).
985Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
986 if (argc != 1) {
987 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
988 }
989 char* filename;
990 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
991
992 Value* v = malloc(sizeof(Value));
993 v->type = VAL_BLOB;
994
995 FileContents fc;
996 if (LoadFileContents(filename, &fc) != 0) {
997 ErrorAbort(state, "%s() loading \"%s\" failed: %s",
998 name, filename, strerror(errno));
999 free(filename);
1000 free(v);
1001 free(fc.data);
1002 return NULL;
1003 }
1004
1005 v->size = fc.size;
1006 v->data = (char*)fc.data;
1007
1008 free(filename);
1009 return v;
1010}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001011
Doug Zongker9931f7f2009-06-10 14:11:53 -07001012void RegisterInstallFunctions() {
1013 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001014 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001015 RegisterFunction("unmount", UnmountFn);
1016 RegisterFunction("format", FormatFn);
1017 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001018 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001019 RegisterFunction("delete", DeleteFn);
1020 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001021 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1022 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001023 RegisterFunction("symlink", SymlinkFn);
1024 RegisterFunction("set_perm", SetPermFn);
1025 RegisterFunction("set_perm_recursive", SetPermFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001026
1027 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001028 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001029 RegisterFunction("write_raw_image", WriteRawImageFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001030
1031 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001032 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1033 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001034
Doug Zongker512536a2010-02-17 16:11:44 -08001035 RegisterFunction("read_file", ReadFileFn);
1036 RegisterFunction("sha1_check", Sha1CheckFn);
1037
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001038 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001039
1040 RegisterFunction("run_program", RunProgramFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001041}