blob: c4f5e0341c0e9d3fdbebd898bc9837204d40e454 [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>
26#include <unistd.h>
27
Doug Zongker8edb00c2009-06-11 17:21:44 -070028#include "cutils/misc.h"
29#include "cutils/properties.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070030#include "edify/expr.h"
31#include "minzip/DirUtil.h"
32#include "mtdutils/mounts.h"
33#include "mtdutils/mtdutils.h"
34#include "updater.h"
35
Doug Zongker8edb00c2009-06-11 17:21:44 -070036
Doug Zongker9931f7f2009-06-10 14:11:53 -070037// mount(type, location, mount_point)
38//
39// what: type="MTD" location="<partition>" to mount a yaffs2 filesystem
40// type="vfat" location="/dev/block/<whatever>" to mount a device
Doug Zongkerd9c9d102009-06-12 12:24:39 -070041char* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070042 char* result = NULL;
43 if (argc != 3) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070044 return ErrorAbort(state, "%s() expects 3 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -070045 }
46 char* type;
47 char* location;
48 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -070049 if (ReadArgs(state, argv, 3, &type, &location, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070050 return NULL;
51 }
52
53 if (strlen(type) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070054 ErrorAbort(state, "type argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -070055 goto done;
56 }
57 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070058 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -070059 goto done;
60 }
61 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070062 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -070063 goto done;
64 }
65
66 mkdir(mount_point, 0755);
67
68 if (strcmp(type, "MTD") == 0) {
69 mtd_scan_partitions();
70 const MtdPartition* mtd;
71 mtd = mtd_find_partition_by_name(location);
72 if (mtd == NULL) {
73 fprintf(stderr, "%s: no mtd partition named \"%s\"",
74 name, location);
75 result = strdup("");
76 goto done;
77 }
78 if (mtd_mount_partition(mtd, mount_point, "yaffs2", 0 /* rw */) != 0) {
79 fprintf(stderr, "mtd mount of %s failed: %s\n",
80 location, strerror(errno));
81 result = strdup("");
82 goto done;
83 }
84 result = mount_point;
85 } else {
86 if (mount(location, mount_point, type,
87 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "") < 0) {
88 result = strdup("");
89 } else {
90 result = mount_point;
91 }
92 }
93
94done:
95 free(type);
96 free(location);
97 if (result != mount_point) free(mount_point);
98 return result;
99}
100
Doug Zongker8edb00c2009-06-11 17:21:44 -0700101
102// is_mounted(mount_point)
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700103char* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700104 char* result = NULL;
105 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700106 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700107 }
108 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700109 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700110 return NULL;
111 }
112 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700113 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700114 goto done;
115 }
116
117 scan_mounted_volumes();
118 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
119 if (vol == NULL) {
120 result = strdup("");
121 } else {
122 result = mount_point;
123 }
124
125done:
126 if (result != mount_point) free(mount_point);
127 return result;
128}
129
130
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700131char* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700132 char* result = NULL;
133 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700134 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700135 }
136 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700137 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700138 return NULL;
139 }
140 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700141 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700142 goto done;
143 }
144
145 scan_mounted_volumes();
146 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
147 if (vol == NULL) {
148 fprintf(stderr, "unmount of %s failed; no such volume\n", mount_point);
149 result = strdup("");
150 } else {
151 unmount_mounted_volume(vol);
152 result = mount_point;
153 }
154
155done:
156 if (result != mount_point) free(mount_point);
157 return result;
158}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700159
160
Doug Zongker9931f7f2009-06-10 14:11:53 -0700161// format(type, location)
162//
163// type="MTD" location=partition
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700164char* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700165 char* result = NULL;
166 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700167 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700168 }
169 char* type;
170 char* location;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700171 if (ReadArgs(state, argv, 2, &type, &location) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700172 return NULL;
173 }
174
175 if (strlen(type) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700176 ErrorAbort(state, "type argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700177 goto done;
178 }
179 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700180 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700181 goto done;
182 }
183
184 if (strcmp(type, "MTD") == 0) {
185 mtd_scan_partitions();
186 const MtdPartition* mtd = mtd_find_partition_by_name(location);
187 if (mtd == NULL) {
188 fprintf(stderr, "%s: no mtd partition named \"%s\"",
189 name, location);
190 result = strdup("");
191 goto done;
192 }
193 MtdWriteContext* ctx = mtd_write_partition(mtd);
194 if (ctx == NULL) {
195 fprintf(stderr, "%s: can't write \"%s\"", name, location);
196 result = strdup("");
197 goto done;
198 }
199 if (mtd_erase_blocks(ctx, -1) == -1) {
200 mtd_write_close(ctx);
201 fprintf(stderr, "%s: failed to erase \"%s\"", name, location);
202 result = strdup("");
203 goto done;
204 }
205 if (mtd_write_close(ctx) != 0) {
206 fprintf(stderr, "%s: failed to close \"%s\"", name, location);
207 result = strdup("");
208 goto done;
209 }
210 result = location;
211 } else {
212 fprintf(stderr, "%s: unsupported type \"%s\"", name, type);
213 }
214
215done:
216 free(type);
217 if (result != location) free(location);
218 return result;
219}
220
Doug Zongker8edb00c2009-06-11 17:21:44 -0700221
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700222char* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700223 char** paths = malloc(argc * sizeof(char*));
224 int i;
225 for (i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700226 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700227 if (paths[i] == NULL) {
228 int j;
229 for (j = 0; j < i; ++i) {
230 free(paths[j]);
231 }
232 free(paths);
233 return NULL;
234 }
235 }
236
237 bool recursive = (strcmp(name, "delete_recursive") == 0);
238
239 int success = 0;
240 for (i = 0; i < argc; ++i) {
241 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
242 ++success;
243 free(paths[i]);
244 }
245 free(paths);
246
247 char buffer[10];
248 sprintf(buffer, "%d", success);
249 return strdup(buffer);
250}
251
Doug Zongker8edb00c2009-06-11 17:21:44 -0700252
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700253char* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700254 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700255 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700256 }
257 char* frac_str;
258 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700259 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700260 return NULL;
261 }
262
263 double frac = strtod(frac_str, NULL);
264 int sec = strtol(sec_str, NULL, 10);
265
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700266 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700267 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
268
Doug Zongker9931f7f2009-06-10 14:11:53 -0700269 free(sec_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700270 return frac_str;
271}
272
273char* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
274 if (argc != 1) {
275 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
276 }
277 char* frac_str;
278 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
279 return NULL;
280 }
281
282 double frac = strtod(frac_str, NULL);
283
284 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
285 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
286
287 return frac_str;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700288}
289
Doug Zongker8edb00c2009-06-11 17:21:44 -0700290// package_extract_dir(package_path, destination_path)
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700291char* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700292 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700293 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700294 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700295 }
296 char* zip_path;
297 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700298 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700299
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700300 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700301
302 // To create a consistent system image, never use the clock for timestamps.
303 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
304
305 bool success = mzExtractRecursive(za, zip_path, dest_path,
306 MZ_EXTRACT_FILES_ONLY, &timestamp,
307 NULL, NULL);
308 free(zip_path);
309 free(dest_path);
310 return strdup(success ? "t" : "");
311}
312
Doug Zongker8edb00c2009-06-11 17:21:44 -0700313
314// package_extract_file(package_path, destination_path)
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700315char* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700316 int argc, Expr* argv[]) {
317 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700318 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700319 }
320 char* zip_path;
321 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700322 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700323
324 bool success = false;
325
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700326 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700327 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
328 if (entry == NULL) {
329 fprintf(stderr, "%s: no %s in package\n", name, zip_path);
330 goto done;
331 }
332
333 FILE* f = fopen(dest_path, "wb");
334 if (f == NULL) {
335 fprintf(stderr, "%s: can't open %s for write: %s\n",
336 name, dest_path, strerror(errno));
337 goto done;
338 }
339 success = mzExtractZipEntryToFile(za, entry, fileno(f));
340 fclose(f);
341
342 done:
343 free(zip_path);
344 free(dest_path);
345 return strdup(success ? "t" : "");
346}
347
348
Doug Zongker9931f7f2009-06-10 14:11:53 -0700349// symlink target src1 src2 ...
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700350char* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700351 if (argc == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700352 return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700353 }
354 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700355 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700356 if (target == NULL) return NULL;
357
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700358 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700359 if (srcs == NULL) {
360 free(target);
361 return NULL;
362 }
363
364 int i;
365 for (i = 0; i < argc-1; ++i) {
366 symlink(target, srcs[i]);
367 free(srcs[i]);
368 }
369 free(srcs);
370 return strdup("");
371}
372
Doug Zongker8edb00c2009-06-11 17:21:44 -0700373
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700374char* SetPermFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700375 char* result = NULL;
376 bool recursive = (strcmp(name, "set_perm_recursive") == 0);
377
378 int min_args = 4 + (recursive ? 1 : 0);
379 if (argc < min_args) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700380 return ErrorAbort(state, "%s() expects %d+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700381 }
382
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700383 char** args = ReadVarArgs(state, argc, argv);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700384 if (args == NULL) return NULL;
385
386 char* end;
387 int i;
388
389 int uid = strtoul(args[0], &end, 0);
390 if (*end != '\0' || args[0][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700391 ErrorAbort(state, "%s: \"%s\" not a valid uid", name, args[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700392 goto done;
393 }
394
395 int gid = strtoul(args[1], &end, 0);
396 if (*end != '\0' || args[1][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700397 ErrorAbort(state, "%s: \"%s\" not a valid gid", name, args[1]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700398 goto done;
399 }
400
401 if (recursive) {
402 int dir_mode = strtoul(args[2], &end, 0);
403 if (*end != '\0' || args[2][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700404 ErrorAbort(state, "%s: \"%s\" not a valid dirmode", name, args[2]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700405 goto done;
406 }
407
408 int file_mode = strtoul(args[3], &end, 0);
409 if (*end != '\0' || args[3][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700410 ErrorAbort(state, "%s: \"%s\" not a valid filemode",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700411 name, args[3]);
412 goto done;
413 }
414
415 for (i = 4; i < argc; ++i) {
416 dirSetHierarchyPermissions(args[i], uid, gid, dir_mode, file_mode);
417 }
418 } else {
419 int mode = strtoul(args[2], &end, 0);
420 if (*end != '\0' || args[2][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700421 ErrorAbort(state, "%s: \"%s\" not a valid mode", name, args[2]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700422 goto done;
423 }
424
Doug Zongker0bbfe3d2009-06-25 13:37:31 -0700425 for (i = 3; i < argc; ++i) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700426 chown(args[i], uid, gid);
427 chmod(args[i], mode);
428 }
429 }
430 result = strdup("");
431
432done:
433 for (i = 0; i < argc; ++i) {
434 free(args[i]);
435 }
436 free(args);
437
438 return result;
439}
440
Doug Zongker8edb00c2009-06-11 17:21:44 -0700441
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700442char* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700443 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700444 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700445 }
446 char* key;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700447 key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700448 if (key == NULL) return NULL;
449
450 char value[PROPERTY_VALUE_MAX];
451 property_get(key, value, "");
452 free(key);
453
454 return strdup(value);
455}
456
457
Doug Zongker47cace92009-06-18 10:11:50 -0700458// file_getprop(file, key)
459//
460// interprets 'file' as a getprop-style file (key=value pairs, one
461// per line, # comment lines and blank lines okay), and returns the value
462// for 'key' (or "" if it isn't defined).
463char* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
464 char* result = NULL;
465 char* buffer = NULL;
466 char* filename;
467 char* key;
468 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
469 return NULL;
470 }
471
472 struct stat st;
473 if (stat(filename, &st) < 0) {
474 ErrorAbort(state, "%s: failed to stat \"%s\": %s",
475 name, filename, strerror(errno));
476 goto done;
477 }
478
479#define MAX_FILE_GETPROP_SIZE 65536
480
481 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
482 ErrorAbort(state, "%s too large for %s (max %d)",
483 filename, name, MAX_FILE_GETPROP_SIZE);
484 goto done;
485 }
486
487 buffer = malloc(st.st_size+1);
488 if (buffer == NULL) {
489 ErrorAbort(state, "%s: failed to alloc %d bytes", name, st.st_size+1);
490 goto done;
491 }
492
493 FILE* f = fopen(filename, "rb");
494 if (f == NULL) {
495 ErrorAbort(state, "%s: failed to open %s: %s",
496 name, filename, strerror(errno));
497 goto done;
498 }
499
500 if (fread(buffer, 1, st.st_size, f) != st.st_size) {
501 ErrorAbort(state, "%s: failed to read %d bytes from %s",
502 name, st.st_size+1, filename);
503 fclose(f);
504 goto done;
505 }
506 buffer[st.st_size] = '\0';
507
508 fclose(f);
509
510 char* line = strtok(buffer, "\n");
511 do {
512 // skip whitespace at start of line
513 while (*line && isspace(*line)) ++line;
514
515 // comment or blank line: skip to next line
516 if (*line == '\0' || *line == '#') continue;
517
518 char* equal = strchr(line, '=');
519 if (equal == NULL) {
520 ErrorAbort(state, "%s: malformed line \"%s\": %s not a prop file?",
521 name, line, filename);
522 goto done;
523 }
524
525 // trim whitespace between key and '='
526 char* key_end = equal-1;
527 while (key_end > line && isspace(*key_end)) --key_end;
528 key_end[1] = '\0';
529
530 // not the key we're looking for
531 if (strcmp(key, line) != 0) continue;
532
533 // skip whitespace after the '=' to the start of the value
534 char* val_start = equal+1;
535 while(*val_start && isspace(*val_start)) ++val_start;
536
537 // trim trailing whitespace
538 char* val_end = val_start + strlen(val_start)-1;
539 while (val_end > val_start && isspace(*val_end)) --val_end;
540 val_end[1] = '\0';
541
542 result = strdup(val_start);
543 break;
544
545 } while ((line = strtok(NULL, "\n")));
546
547 if (result == NULL) result = strdup("");
548
549 done:
550 free(filename);
551 free(key);
552 free(buffer);
553 return result;
554}
555
556
Doug Zongker8edb00c2009-06-11 17:21:44 -0700557static bool write_raw_image_cb(const unsigned char* data,
558 int data_len, void* ctx) {
559 int r = mtd_write_data((MtdWriteContext*)ctx, (const char *)data, data_len);
560 if (r == data_len) return true;
561 fprintf(stderr, "%s\n", strerror(errno));
562 return false;
563}
564
565// write_raw_image(file, partition)
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700566char* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700567 char* result = NULL;
568
569 char* partition;
570 char* filename;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700571 if (ReadArgs(state, argv, 2, &filename, &partition) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700572 return NULL;
573 }
574
575 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700576 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700577 goto done;
578 }
579 if (strlen(filename) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700580 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700581 goto done;
582 }
583
584 mtd_scan_partitions();
585 const MtdPartition* mtd = mtd_find_partition_by_name(partition);
586 if (mtd == NULL) {
587 fprintf(stderr, "%s: no mtd partition named \"%s\"\n", name, partition);
588 result = strdup("");
589 goto done;
590 }
591
592 MtdWriteContext* ctx = mtd_write_partition(mtd);
593 if (ctx == NULL) {
594 fprintf(stderr, "%s: can't write mtd partition \"%s\"\n",
595 name, partition);
596 result = strdup("");
597 goto done;
598 }
599
600 bool success;
601
602 FILE* f = fopen(filename, "rb");
603 if (f == NULL) {
604 fprintf(stderr, "%s: can't open %s: %s\n",
605 name, filename, strerror(errno));
606 result = strdup("");
607 goto done;
608 }
609
610 success = true;
611 char* buffer = malloc(BUFSIZ);
612 int read;
613 while (success && (read = fread(buffer, 1, BUFSIZ, f)) > 0) {
614 int wrote = mtd_write_data(ctx, buffer, read);
615 success = success && (wrote == read);
616 if (!success) {
617 fprintf(stderr, "mtd_write_data to %s failed: %s\n",
618 partition, strerror(errno));
619 }
620 }
621 free(buffer);
622 fclose(f);
623
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700624 if (mtd_erase_blocks(ctx, -1) == -1) {
625 fprintf(stderr, "%s: error erasing blocks of %s\n", name, partition);
626 }
627 if (mtd_write_close(ctx) != 0) {
628 fprintf(stderr, "%s: error closing write of %s\n", name, partition);
629 }
630
Doug Zongker8edb00c2009-06-11 17:21:44 -0700631 printf("%s %s partition from %s\n",
632 success ? "wrote" : "failed to write", partition, filename);
633
634 result = success ? partition : strdup("");
635
636done:
637 if (result != partition) free(partition);
638 free(filename);
639 return result;
640}
641
642// write_firmware_image(file, partition)
643//
644// partition is "radio" or "hboot"
645// file is not used until after updater exits
646//
647// TODO: this should live in some HTC-specific library
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700648char* WriteFirmwareImageFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700649 int argc, Expr* argv[]) {
650 char* result = NULL;
651
652 char* partition;
653 char* filename;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700654 if (ReadArgs(state, argv, 2, &filename, &partition) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700655 return NULL;
656 }
657
658 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700659 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700660 goto done;
661 }
662 if (strlen(filename) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700663 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700664 goto done;
665 }
666
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700667 FILE* cmd = ((UpdaterInfo*)(state->cookie))->cmd_pipe;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700668 fprintf(cmd, "firmware %s %s\n", partition, filename);
669
670 printf("will write %s firmware from %s\n", partition, filename);
671 result = partition;
672
673done:
674 if (result != partition) free(partition);
675 free(filename);
676 return result;
677}
678
679
680extern int applypatch(int argc, char** argv);
681
682// apply_patch(srcfile, tgtfile, tgtsha1, tgtsize, sha1:patch, ...)
683// apply_patch_check(file, sha1, ...)
684// apply_patch_space(bytes)
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700685char* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700686 printf("in applypatchfn (%s)\n", name);
687
688 char* prepend = NULL;
689 if (strstr(name, "check") != NULL) {
690 prepend = "-c";
691 } else if (strstr(name, "space") != NULL) {
692 prepend = "-s";
693 }
694
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700695 char** args = ReadVarArgs(state, argc, argv);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700696 if (args == NULL) return NULL;
697
698 // insert the "program name" argv[0] and a copy of the "prepend"
699 // string (if any) at the start of the args.
700
701 int extra = 1 + (prepend != NULL ? 1 : 0);
702 char** temp = malloc((argc+extra) * sizeof(char*));
703 memcpy(temp+extra, args, argc * sizeof(char*));
704 temp[0] = strdup("updater");
705 if (prepend) {
706 temp[1] = strdup(prepend);
707 }
708 free(args);
709 args = temp;
710 argc += extra;
711
712 printf("calling applypatch\n");
713 fflush(stdout);
714 int result = applypatch(argc, args);
715 printf("applypatch returned %d\n", result);
716
717 int i;
718 for (i = 0; i < argc; ++i) {
719 free(args[i]);
720 }
721 free(args);
722
723 switch (result) {
724 case 0: return strdup("t");
725 case 1: return strdup("");
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700726 default: return ErrorAbort(state, "applypatch couldn't parse args");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700727 }
728}
729
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700730char* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
731 char** args = ReadVarArgs(state, argc, argv);
732 if (args == NULL) {
733 return NULL;
734 }
735
736 int size = 0;
737 int i;
738 for (i = 0; i < argc; ++i) {
739 size += strlen(args[i]);
740 }
741 char* buffer = malloc(size+1);
742 size = 0;
743 for (i = 0; i < argc; ++i) {
744 strcpy(buffer+size, args[i]);
745 size += strlen(args[i]);
746 free(args[i]);
747 }
748 free(args);
749 buffer[size] = '\0';
750
751 char* line = strtok(buffer, "\n");
752 while (line) {
753 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe,
754 "ui_print %s\n", line);
755 line = strtok(NULL, "\n");
756 }
757 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "ui_print\n");
758
759 return buffer;
760}
761
Doug Zongker8edb00c2009-06-11 17:21:44 -0700762
Doug Zongker9931f7f2009-06-10 14:11:53 -0700763void RegisterInstallFunctions() {
764 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700765 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700766 RegisterFunction("unmount", UnmountFn);
767 RegisterFunction("format", FormatFn);
768 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700769 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700770 RegisterFunction("delete", DeleteFn);
771 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700772 RegisterFunction("package_extract_dir", PackageExtractDirFn);
773 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700774 RegisterFunction("symlink", SymlinkFn);
775 RegisterFunction("set_perm", SetPermFn);
776 RegisterFunction("set_perm_recursive", SetPermFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700777
778 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -0700779 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700780 RegisterFunction("write_raw_image", WriteRawImageFn);
781 RegisterFunction("write_firmware_image", WriteFirmwareImageFn);
782
783 RegisterFunction("apply_patch", ApplyPatchFn);
784 RegisterFunction("apply_patch_check", ApplyPatchFn);
785 RegisterFunction("apply_patch_space", ApplyPatchFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700786
787 RegisterFunction("ui_print", UIPrintFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700788}