blob: 934acaae4fab3ce1c0746cd5da512a983af6e690 [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"
32#include "minzip/DirUtil.h"
33#include "mtdutils/mounts.h"
34#include "mtdutils/mtdutils.h"
35#include "updater.h"
36
Doug Zongker8edb00c2009-06-11 17:21:44 -070037
Doug Zongker9931f7f2009-06-10 14:11:53 -070038// mount(type, location, mount_point)
39//
40// what: type="MTD" location="<partition>" to mount a yaffs2 filesystem
41// type="vfat" location="/dev/block/<whatever>" to mount a device
Doug Zongkerd9c9d102009-06-12 12:24:39 -070042char* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070043 char* result = NULL;
44 if (argc != 3) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070045 return ErrorAbort(state, "%s() expects 3 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -070046 }
47 char* type;
48 char* location;
49 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -070050 if (ReadArgs(state, argv, 3, &type, &location, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070051 return NULL;
52 }
53
54 if (strlen(type) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070055 ErrorAbort(state, "type argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -070056 goto done;
57 }
58 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070059 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -070060 goto done;
61 }
62 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070063 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -070064 goto done;
65 }
66
67 mkdir(mount_point, 0755);
68
69 if (strcmp(type, "MTD") == 0) {
70 mtd_scan_partitions();
71 const MtdPartition* mtd;
72 mtd = mtd_find_partition_by_name(location);
73 if (mtd == NULL) {
74 fprintf(stderr, "%s: no mtd partition named \"%s\"",
75 name, location);
76 result = strdup("");
77 goto done;
78 }
79 if (mtd_mount_partition(mtd, mount_point, "yaffs2", 0 /* rw */) != 0) {
80 fprintf(stderr, "mtd mount of %s failed: %s\n",
81 location, strerror(errno));
82 result = strdup("");
83 goto done;
84 }
85 result = mount_point;
86 } else {
87 if (mount(location, mount_point, type,
88 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "") < 0) {
Doug Zongker60babf82009-09-18 15:11:24 -070089 fprintf(stderr, "%s: failed to mount %s at %s: %s\n",
90 name, location, mount_point, strerror(errno));
Doug Zongker9931f7f2009-06-10 14:11:53 -070091 result = strdup("");
92 } else {
93 result = mount_point;
94 }
95 }
96
97done:
98 free(type);
99 free(location);
100 if (result != mount_point) free(mount_point);
101 return result;
102}
103
Doug Zongker8edb00c2009-06-11 17:21:44 -0700104
105// is_mounted(mount_point)
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700106char* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700107 char* result = NULL;
108 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700109 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700110 }
111 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700112 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700113 return NULL;
114 }
115 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700116 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700117 goto done;
118 }
119
120 scan_mounted_volumes();
121 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
122 if (vol == NULL) {
123 result = strdup("");
124 } else {
125 result = mount_point;
126 }
127
128done:
129 if (result != mount_point) free(mount_point);
130 return result;
131}
132
133
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700134char* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -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 Zongker9931f7f2009-06-10 14:11:53 -0700138 }
139 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700140 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -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 Zongker9931f7f2009-06-10 14:11:53 -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 fprintf(stderr, "unmount of %s failed; no such volume\n", mount_point);
152 result = strdup("");
153 } else {
154 unmount_mounted_volume(vol);
155 result = mount_point;
156 }
157
158done:
159 if (result != mount_point) free(mount_point);
160 return result;
161}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700162
163
Doug Zongker9931f7f2009-06-10 14:11:53 -0700164// format(type, location)
165//
166// type="MTD" location=partition
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700167char* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700168 char* result = NULL;
169 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700170 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700171 }
172 char* type;
173 char* location;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700174 if (ReadArgs(state, argv, 2, &type, &location) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700175 return NULL;
176 }
177
178 if (strlen(type) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700179 ErrorAbort(state, "type argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700180 goto done;
181 }
182 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700183 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700184 goto done;
185 }
186
187 if (strcmp(type, "MTD") == 0) {
188 mtd_scan_partitions();
189 const MtdPartition* mtd = mtd_find_partition_by_name(location);
190 if (mtd == NULL) {
191 fprintf(stderr, "%s: no mtd partition named \"%s\"",
192 name, location);
193 result = strdup("");
194 goto done;
195 }
196 MtdWriteContext* ctx = mtd_write_partition(mtd);
197 if (ctx == NULL) {
198 fprintf(stderr, "%s: can't write \"%s\"", name, location);
199 result = strdup("");
200 goto done;
201 }
202 if (mtd_erase_blocks(ctx, -1) == -1) {
203 mtd_write_close(ctx);
204 fprintf(stderr, "%s: failed to erase \"%s\"", name, location);
205 result = strdup("");
206 goto done;
207 }
208 if (mtd_write_close(ctx) != 0) {
209 fprintf(stderr, "%s: failed to close \"%s\"", name, location);
210 result = strdup("");
211 goto done;
212 }
213 result = location;
214 } else {
215 fprintf(stderr, "%s: unsupported type \"%s\"", name, type);
216 }
217
218done:
219 free(type);
220 if (result != location) free(location);
221 return result;
222}
223
Doug Zongker8edb00c2009-06-11 17:21:44 -0700224
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700225char* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700226 char** paths = malloc(argc * sizeof(char*));
227 int i;
228 for (i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700229 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700230 if (paths[i] == NULL) {
231 int j;
232 for (j = 0; j < i; ++i) {
233 free(paths[j]);
234 }
235 free(paths);
236 return NULL;
237 }
238 }
239
240 bool recursive = (strcmp(name, "delete_recursive") == 0);
241
242 int success = 0;
243 for (i = 0; i < argc; ++i) {
244 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
245 ++success;
246 free(paths[i]);
247 }
248 free(paths);
249
250 char buffer[10];
251 sprintf(buffer, "%d", success);
252 return strdup(buffer);
253}
254
Doug Zongker8edb00c2009-06-11 17:21:44 -0700255
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700256char* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700257 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700258 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700259 }
260 char* frac_str;
261 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700262 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700263 return NULL;
264 }
265
266 double frac = strtod(frac_str, NULL);
267 int sec = strtol(sec_str, NULL, 10);
268
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700269 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700270 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
271
Doug Zongker9931f7f2009-06-10 14:11:53 -0700272 free(sec_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700273 return frac_str;
274}
275
276char* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
277 if (argc != 1) {
278 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
279 }
280 char* frac_str;
281 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
282 return NULL;
283 }
284
285 double frac = strtod(frac_str, NULL);
286
287 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
288 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
289
290 return frac_str;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700291}
292
Doug Zongker8edb00c2009-06-11 17:21:44 -0700293// package_extract_dir(package_path, destination_path)
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700294char* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700295 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700296 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700297 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700298 }
299 char* zip_path;
300 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700301 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700302
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700303 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700304
305 // To create a consistent system image, never use the clock for timestamps.
306 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
307
308 bool success = mzExtractRecursive(za, zip_path, dest_path,
309 MZ_EXTRACT_FILES_ONLY, &timestamp,
310 NULL, NULL);
311 free(zip_path);
312 free(dest_path);
313 return strdup(success ? "t" : "");
314}
315
Doug Zongker8edb00c2009-06-11 17:21:44 -0700316
317// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800318// or
319// package_extract_file(package_path)
320// to return the entire contents of the file as the result of this
321// function (the char* returned points to a long giving the length
322// followed by that many bytes of data).
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700323char* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700324 int argc, Expr* argv[]) {
Doug Zongker6aece332010-02-01 14:40:12 -0800325 if (argc != 1 && argc != 2) {
326 return ErrorAbort(state, "%s() expects 1 or 2 args, got %d",
327 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700328 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700329 bool success = false;
Doug Zongker6aece332010-02-01 14:40:12 -0800330 if (argc == 2) {
331 // The two-argument version extracts to a file.
Doug Zongker8edb00c2009-06-11 17:21:44 -0700332
Doug Zongker6aece332010-02-01 14:40:12 -0800333 char* zip_path;
334 char* dest_path;
335 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
336
337 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
338 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
339 if (entry == NULL) {
340 fprintf(stderr, "%s: no %s in package\n", name, zip_path);
341 goto done2;
342 }
343
344 FILE* f = fopen(dest_path, "wb");
345 if (f == NULL) {
346 fprintf(stderr, "%s: can't open %s for write: %s\n",
347 name, dest_path, strerror(errno));
348 goto done2;
349 }
350 success = mzExtractZipEntryToFile(za, entry, fileno(f));
351 fclose(f);
352
353 done2:
354 free(zip_path);
355 free(dest_path);
356 return strdup(success ? "t" : "");
357 } else {
358 // The one-argument version returns the contents of the file
359 // as the result.
360
361 char* zip_path;
362 char* buffer = NULL;
363
364 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
365
366 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
367 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
368 if (entry == NULL) {
369 fprintf(stderr, "%s: no %s in package\n", name, zip_path);
370 goto done1;
371 }
372
373 long size = mzGetZipEntryUncompLen(entry);
374 buffer = malloc(size + sizeof(long));
375 if (buffer == NULL) {
376 fprintf(stderr, "%s: failed to allocate %ld bytes for %s\n",
377 name, size+sizeof(long), zip_path);
378 goto done1;
379 }
380
381 *(long *)buffer = size;
382 success = mzExtractZipEntryToBuffer(
383 za, entry, (unsigned char *)(buffer + sizeof(long)));
384
385 done1:
386 free(zip_path);
387 if (!success) {
388 free(buffer);
389 buffer = malloc(sizeof(long));
390 *(long *)buffer = -1L;
391 }
392 return buffer;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700393 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700394}
395
396
Doug Zongker9931f7f2009-06-10 14:11:53 -0700397// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700398// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700399char* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700400 if (argc == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700401 return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700402 }
403 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700404 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700405 if (target == NULL) return NULL;
406
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700407 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700408 if (srcs == NULL) {
409 free(target);
410 return NULL;
411 }
412
413 int i;
414 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700415 if (unlink(srcs[i]) < 0) {
416 if (errno != ENOENT) {
417 fprintf(stderr, "%s: failed to remove %s: %s\n",
418 name, srcs[i], strerror(errno));
419 }
420 }
421 if (symlink(target, srcs[i]) < 0) {
422 fprintf(stderr, "%s: failed to symlink %s to %s: %s\n",
423 name, srcs[i], target, strerror(errno));
424 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700425 free(srcs[i]);
426 }
427 free(srcs);
428 return strdup("");
429}
430
Doug Zongker8edb00c2009-06-11 17:21:44 -0700431
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700432char* SetPermFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700433 char* result = NULL;
434 bool recursive = (strcmp(name, "set_perm_recursive") == 0);
435
436 int min_args = 4 + (recursive ? 1 : 0);
437 if (argc < min_args) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700438 return ErrorAbort(state, "%s() expects %d+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700439 }
440
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700441 char** args = ReadVarArgs(state, argc, argv);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700442 if (args == NULL) return NULL;
443
444 char* end;
445 int i;
446
447 int uid = strtoul(args[0], &end, 0);
448 if (*end != '\0' || args[0][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700449 ErrorAbort(state, "%s: \"%s\" not a valid uid", name, args[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700450 goto done;
451 }
452
453 int gid = strtoul(args[1], &end, 0);
454 if (*end != '\0' || args[1][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700455 ErrorAbort(state, "%s: \"%s\" not a valid gid", name, args[1]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700456 goto done;
457 }
458
459 if (recursive) {
460 int dir_mode = strtoul(args[2], &end, 0);
461 if (*end != '\0' || args[2][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700462 ErrorAbort(state, "%s: \"%s\" not a valid dirmode", name, args[2]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700463 goto done;
464 }
465
466 int file_mode = strtoul(args[3], &end, 0);
467 if (*end != '\0' || args[3][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700468 ErrorAbort(state, "%s: \"%s\" not a valid filemode",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700469 name, args[3]);
470 goto done;
471 }
472
473 for (i = 4; i < argc; ++i) {
474 dirSetHierarchyPermissions(args[i], uid, gid, dir_mode, file_mode);
475 }
476 } else {
477 int mode = strtoul(args[2], &end, 0);
478 if (*end != '\0' || args[2][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700479 ErrorAbort(state, "%s: \"%s\" not a valid mode", name, args[2]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700480 goto done;
481 }
482
Doug Zongker0bbfe3d2009-06-25 13:37:31 -0700483 for (i = 3; i < argc; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700484 if (chown(args[i], uid, gid) < 0) {
485 fprintf(stderr, "%s: chown of %s to %d %d failed: %s\n",
486 name, args[i], uid, gid, strerror(errno));
487 }
488 if (chmod(args[i], mode) < 0) {
489 fprintf(stderr, "%s: chmod of %s to %o failed: %s\n",
490 name, args[i], mode, strerror(errno));
491 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700492 }
493 }
494 result = strdup("");
495
496done:
497 for (i = 0; i < argc; ++i) {
498 free(args[i]);
499 }
500 free(args);
501
502 return result;
503}
504
Doug Zongker8edb00c2009-06-11 17:21:44 -0700505
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700506char* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700507 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700508 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700509 }
510 char* key;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700511 key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700512 if (key == NULL) return NULL;
513
514 char value[PROPERTY_VALUE_MAX];
515 property_get(key, value, "");
516 free(key);
517
518 return strdup(value);
519}
520
521
Doug Zongker47cace92009-06-18 10:11:50 -0700522// file_getprop(file, key)
523//
524// interprets 'file' as a getprop-style file (key=value pairs, one
525// per line, # comment lines and blank lines okay), and returns the value
526// for 'key' (or "" if it isn't defined).
527char* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
528 char* result = NULL;
529 char* buffer = NULL;
530 char* filename;
531 char* key;
532 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
533 return NULL;
534 }
535
536 struct stat st;
537 if (stat(filename, &st) < 0) {
538 ErrorAbort(state, "%s: failed to stat \"%s\": %s",
539 name, filename, strerror(errno));
540 goto done;
541 }
542
543#define MAX_FILE_GETPROP_SIZE 65536
544
545 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
546 ErrorAbort(state, "%s too large for %s (max %d)",
547 filename, name, MAX_FILE_GETPROP_SIZE);
548 goto done;
549 }
550
551 buffer = malloc(st.st_size+1);
552 if (buffer == NULL) {
553 ErrorAbort(state, "%s: failed to alloc %d bytes", name, st.st_size+1);
554 goto done;
555 }
556
557 FILE* f = fopen(filename, "rb");
558 if (f == NULL) {
559 ErrorAbort(state, "%s: failed to open %s: %s",
560 name, filename, strerror(errno));
561 goto done;
562 }
563
564 if (fread(buffer, 1, st.st_size, f) != st.st_size) {
565 ErrorAbort(state, "%s: failed to read %d bytes from %s",
566 name, st.st_size+1, filename);
567 fclose(f);
568 goto done;
569 }
570 buffer[st.st_size] = '\0';
571
572 fclose(f);
573
574 char* line = strtok(buffer, "\n");
575 do {
576 // skip whitespace at start of line
577 while (*line && isspace(*line)) ++line;
578
579 // comment or blank line: skip to next line
580 if (*line == '\0' || *line == '#') continue;
581
582 char* equal = strchr(line, '=');
583 if (equal == NULL) {
584 ErrorAbort(state, "%s: malformed line \"%s\": %s not a prop file?",
585 name, line, filename);
586 goto done;
587 }
588
589 // trim whitespace between key and '='
590 char* key_end = equal-1;
591 while (key_end > line && isspace(*key_end)) --key_end;
592 key_end[1] = '\0';
593
594 // not the key we're looking for
595 if (strcmp(key, line) != 0) continue;
596
597 // skip whitespace after the '=' to the start of the value
598 char* val_start = equal+1;
599 while(*val_start && isspace(*val_start)) ++val_start;
600
601 // trim trailing whitespace
602 char* val_end = val_start + strlen(val_start)-1;
603 while (val_end > val_start && isspace(*val_end)) --val_end;
604 val_end[1] = '\0';
605
606 result = strdup(val_start);
607 break;
608
609 } while ((line = strtok(NULL, "\n")));
610
611 if (result == NULL) result = strdup("");
612
613 done:
614 free(filename);
615 free(key);
616 free(buffer);
617 return result;
618}
619
620
Doug Zongker8edb00c2009-06-11 17:21:44 -0700621static bool write_raw_image_cb(const unsigned char* data,
622 int data_len, void* ctx) {
623 int r = mtd_write_data((MtdWriteContext*)ctx, (const char *)data, data_len);
624 if (r == data_len) return true;
625 fprintf(stderr, "%s\n", strerror(errno));
626 return false;
627}
628
629// write_raw_image(file, partition)
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700630char* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700631 char* result = NULL;
632
633 char* partition;
634 char* filename;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700635 if (ReadArgs(state, argv, 2, &filename, &partition) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700636 return NULL;
637 }
638
639 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700640 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700641 goto done;
642 }
643 if (strlen(filename) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700644 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700645 goto done;
646 }
647
648 mtd_scan_partitions();
649 const MtdPartition* mtd = mtd_find_partition_by_name(partition);
650 if (mtd == NULL) {
651 fprintf(stderr, "%s: no mtd partition named \"%s\"\n", name, partition);
652 result = strdup("");
653 goto done;
654 }
655
656 MtdWriteContext* ctx = mtd_write_partition(mtd);
657 if (ctx == NULL) {
658 fprintf(stderr, "%s: can't write mtd partition \"%s\"\n",
659 name, partition);
660 result = strdup("");
661 goto done;
662 }
663
664 bool success;
665
666 FILE* f = fopen(filename, "rb");
667 if (f == NULL) {
668 fprintf(stderr, "%s: can't open %s: %s\n",
669 name, filename, strerror(errno));
670 result = strdup("");
671 goto done;
672 }
673
674 success = true;
675 char* buffer = malloc(BUFSIZ);
676 int read;
677 while (success && (read = fread(buffer, 1, BUFSIZ, f)) > 0) {
678 int wrote = mtd_write_data(ctx, buffer, read);
679 success = success && (wrote == read);
680 if (!success) {
681 fprintf(stderr, "mtd_write_data to %s failed: %s\n",
682 partition, strerror(errno));
683 }
684 }
685 free(buffer);
686 fclose(f);
687
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700688 if (mtd_erase_blocks(ctx, -1) == -1) {
689 fprintf(stderr, "%s: error erasing blocks of %s\n", name, partition);
690 }
691 if (mtd_write_close(ctx) != 0) {
692 fprintf(stderr, "%s: error closing write of %s\n", name, partition);
693 }
694
Doug Zongker8edb00c2009-06-11 17:21:44 -0700695 printf("%s %s partition from %s\n",
696 success ? "wrote" : "failed to write", partition, filename);
697
698 result = success ? partition : strdup("");
699
700done:
701 if (result != partition) free(partition);
702 free(filename);
703 return result;
704}
705
706// write_firmware_image(file, partition)
707//
708// partition is "radio" or "hboot"
709// file is not used until after updater exits
710//
711// TODO: this should live in some HTC-specific library
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700712char* WriteFirmwareImageFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700713 int argc, Expr* argv[]) {
714 char* result = NULL;
715
716 char* partition;
717 char* filename;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700718 if (ReadArgs(state, argv, 2, &filename, &partition) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700719 return NULL;
720 }
721
722 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700723 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700724 goto done;
725 }
726 if (strlen(filename) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700727 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700728 goto done;
729 }
730
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700731 FILE* cmd = ((UpdaterInfo*)(state->cookie))->cmd_pipe;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700732 fprintf(cmd, "firmware %s %s\n", partition, filename);
733
734 printf("will write %s firmware from %s\n", partition, filename);
735 result = partition;
736
737done:
738 if (result != partition) free(partition);
739 free(filename);
740 return result;
741}
742
743
744extern int applypatch(int argc, char** argv);
745
746// apply_patch(srcfile, tgtfile, tgtsha1, tgtsize, sha1:patch, ...)
747// apply_patch_check(file, sha1, ...)
748// apply_patch_space(bytes)
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700749char* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700750 printf("in applypatchfn (%s)\n", name);
751
752 char* prepend = NULL;
753 if (strstr(name, "check") != NULL) {
754 prepend = "-c";
755 } else if (strstr(name, "space") != NULL) {
756 prepend = "-s";
757 }
758
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700759 char** args = ReadVarArgs(state, argc, argv);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700760 if (args == NULL) return NULL;
761
762 // insert the "program name" argv[0] and a copy of the "prepend"
763 // string (if any) at the start of the args.
764
765 int extra = 1 + (prepend != NULL ? 1 : 0);
766 char** temp = malloc((argc+extra) * sizeof(char*));
767 memcpy(temp+extra, args, argc * sizeof(char*));
768 temp[0] = strdup("updater");
769 if (prepend) {
770 temp[1] = strdup(prepend);
771 }
772 free(args);
773 args = temp;
774 argc += extra;
775
776 printf("calling applypatch\n");
777 fflush(stdout);
778 int result = applypatch(argc, args);
779 printf("applypatch returned %d\n", result);
780
781 int i;
782 for (i = 0; i < argc; ++i) {
783 free(args[i]);
784 }
785 free(args);
786
787 switch (result) {
788 case 0: return strdup("t");
789 case 1: return strdup("");
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700790 default: return ErrorAbort(state, "applypatch couldn't parse args");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700791 }
792}
793
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700794char* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
795 char** args = ReadVarArgs(state, argc, argv);
796 if (args == NULL) {
797 return NULL;
798 }
799
800 int size = 0;
801 int i;
802 for (i = 0; i < argc; ++i) {
803 size += strlen(args[i]);
804 }
805 char* buffer = malloc(size+1);
806 size = 0;
807 for (i = 0; i < argc; ++i) {
808 strcpy(buffer+size, args[i]);
809 size += strlen(args[i]);
810 free(args[i]);
811 }
812 free(args);
813 buffer[size] = '\0';
814
815 char* line = strtok(buffer, "\n");
816 while (line) {
817 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe,
818 "ui_print %s\n", line);
819 line = strtok(NULL, "\n");
820 }
821 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "ui_print\n");
822
823 return buffer;
824}
825
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700826char* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
827 if (argc < 1) {
828 return ErrorAbort(state, "%s() expects at least 1 arg", name);
829 }
830 char** args = ReadVarArgs(state, argc, argv);
831 if (args == NULL) {
832 return NULL;
833 }
834
835 char** args2 = malloc(sizeof(char*) * (argc+1));
836 memcpy(args2, args, sizeof(char*) * argc);
837 args2[argc] = NULL;
838
839 fprintf(stderr, "about to run program [%s] with %d args\n", args2[0], argc);
840
841 pid_t child = fork();
842 if (child == 0) {
843 execv(args2[0], args2);
844 fprintf(stderr, "run_program: execv failed: %s\n", strerror(errno));
845 _exit(1);
846 }
847 int status;
848 waitpid(child, &status, 0);
849 if (WIFEXITED(status)) {
850 if (WEXITSTATUS(status) != 0) {
851 fprintf(stderr, "run_program: child exited with status %d\n",
852 WEXITSTATUS(status));
853 }
854 } else if (WIFSIGNALED(status)) {
855 fprintf(stderr, "run_program: child terminated by signal %d\n",
856 WTERMSIG(status));
857 }
858
859 int i;
860 for (i = 0; i < argc; ++i) {
861 free(args[i]);
862 }
863 free(args);
864 free(args2);
865
866 char buffer[20];
867 sprintf(buffer, "%d", status);
868
869 return strdup(buffer);
870}
871
Doug Zongker8edb00c2009-06-11 17:21:44 -0700872
Doug Zongker9931f7f2009-06-10 14:11:53 -0700873void RegisterInstallFunctions() {
874 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700875 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700876 RegisterFunction("unmount", UnmountFn);
877 RegisterFunction("format", FormatFn);
878 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700879 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700880 RegisterFunction("delete", DeleteFn);
881 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700882 RegisterFunction("package_extract_dir", PackageExtractDirFn);
883 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700884 RegisterFunction("symlink", SymlinkFn);
885 RegisterFunction("set_perm", SetPermFn);
886 RegisterFunction("set_perm_recursive", SetPermFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700887
888 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -0700889 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700890 RegisterFunction("write_raw_image", WriteRawImageFn);
891 RegisterFunction("write_firmware_image", WriteFirmwareImageFn);
892
893 RegisterFunction("apply_patch", ApplyPatchFn);
894 RegisterFunction("apply_patch_check", ApplyPatchFn);
895 RegisterFunction("apply_patch_space", ApplyPatchFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700896
897 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700898
899 RegisterFunction("run_program", RunProgramFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700900}