blob: da6b5778214263d10fe10af0784aaeab0db2b77a [file] [log] [blame]
Doug Zongker9931f7f2009-06-10 14:11:53 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Doug Zongkerfbf3c102009-06-24 09:36:20 -070017#include <ctype.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070018#include <errno.h>
19#include <stdarg.h>
Doug Zongkerfbf3c102009-06-24 09:36:20 -070020#include <stdio.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070021#include <stdlib.h>
22#include <string.h>
23#include <sys/mount.h>
24#include <sys/stat.h>
25#include <sys/types.h>
Doug Zongkera3f89ea2009-09-10 14:10:48 -070026#include <sys/wait.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070027#include <unistd.h>
Hristo Bojinovdb314d62010-08-02 10:29:49 -070028#include <fcntl.h>
29#include <time.h>
Nick Kralevich5dbdef02013-09-07 14:41:06 -070030#include <selinux/selinux.h>
31#include <ftw.h>
32#include <sys/capability.h>
33#include <sys/xattr.h>
34#include <linux/xattr.h>
35#include <inttypes.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070036
Doug Zongkerc87bab12013-11-25 13:53:25 -080037#include "bootloader.h"
38#include "applypatch/applypatch.h"
39#include "cutils/android_reboot.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070040#include "cutils/misc.h"
41#include "cutils/properties.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070042#include "edify/expr.h"
Doug Zongker512536a2010-02-17 16:11:44 -080043#include "mincrypt/sha.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070044#include "minzip/DirUtil.h"
45#include "mtdutils/mounts.h"
46#include "mtdutils/mtdutils.h"
47#include "updater.h"
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -080048#include "install.h"
Michael Rungeacf47db2014-11-21 00:12:28 -080049#include "tune2fs.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070050
Doug Zongker3d177d02010-07-01 09:18:44 -070051#ifdef USE_EXT4
52#include "make_ext4fs.h"
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -080053#include "wipe.h"
Doug Zongker3d177d02010-07-01 09:18:44 -070054#endif
55
Michael Runged4a63422014-10-22 19:48:41 -070056void uiPrint(State* state, char* buffer) {
57 char* line = strtok(buffer, "\n");
58 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
59 while (line) {
60 fprintf(ui->cmd_pipe, "ui_print %s\n", line);
61 line = strtok(NULL, "\n");
62 }
63 fprintf(ui->cmd_pipe, "ui_print\n");
Tao Baob6918c72015-05-19 17:02:16 -070064
65 // The recovery will only print the contents to screen for pipe command
66 // ui_print. We need to dump the contents to stderr (which has been
67 // redirected to the log file) directly.
68 fprintf(stderr, buffer);
69 fprintf(stderr, "\n");
Michael Runged4a63422014-10-22 19:48:41 -070070}
71
72__attribute__((__format__(printf, 2, 3))) __nonnull((2))
73void uiPrintf(State* state, const char* format, ...) {
74 char error_msg[1024];
75 va_list ap;
76 va_start(ap, format);
77 vsnprintf(error_msg, sizeof(error_msg), format, ap);
78 va_end(ap);
79 uiPrint(state, error_msg);
80}
81
Doug Zongker52b40362014-02-10 15:30:30 -080082// Take a sha-1 digest and return it as a newly-allocated hex string.
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070083char* PrintSha1(const uint8_t* digest) {
Doug Zongker52b40362014-02-10 15:30:30 -080084 char* buffer = malloc(SHA_DIGEST_SIZE*2 + 1);
85 int i;
86 const char* alphabet = "0123456789abcdef";
87 for (i = 0; i < SHA_DIGEST_SIZE; ++i) {
88 buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf];
89 buffer[i*2+1] = alphabet[digest[i] & 0xf];
90 }
91 buffer[i*2] = '\0';
92 return buffer;
93}
94
Doug Zongker3d177d02010-07-01 09:18:44 -070095// mount(fs_type, partition_type, location, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -070096//
Doug Zongker3d177d02010-07-01 09:18:44 -070097// fs_type="yaffs2" partition_type="MTD" location=partition
98// fs_type="ext4" partition_type="EMMC" location=device
Doug Zongker512536a2010-02-17 16:11:44 -080099Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700100 char* result = NULL;
Michael Runge168f7772014-10-22 17:05:08 -0700101 if (argc != 4 && argc != 5) {
102 return ErrorAbort(state, "%s() expects 4-5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700103 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700104 char* fs_type;
105 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700106 char* location;
107 char* mount_point;
Michael Runge168f7772014-10-22 17:05:08 -0700108 char* mount_options;
109 bool has_mount_options;
110 if (argc == 5) {
111 has_mount_options = true;
112 if (ReadArgs(state, argv, 5, &fs_type, &partition_type,
113 &location, &mount_point, &mount_options) < 0) {
114 return NULL;
115 }
116 } else {
117 has_mount_options = false;
118 if (ReadArgs(state, argv, 4, &fs_type, &partition_type,
Doug Zongker3d177d02010-07-01 09:18:44 -0700119 &location, &mount_point) < 0) {
Michael Runge168f7772014-10-22 17:05:08 -0700120 return NULL;
121 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700122 }
123
Doug Zongker3d177d02010-07-01 09:18:44 -0700124 if (strlen(fs_type) == 0) {
125 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
126 goto done;
127 }
128 if (strlen(partition_type) == 0) {
129 ErrorAbort(state, "partition_type argument to %s() can't be empty",
130 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700131 goto done;
132 }
133 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700134 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700135 goto done;
136 }
137 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700138 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700139 goto done;
140 }
141
Stephen Smalley779701d2012-02-09 14:13:23 -0500142 char *secontext = NULL;
143
144 if (sehandle) {
145 selabel_lookup(sehandle, &secontext, mount_point, 0755);
146 setfscreatecon(secontext);
147 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500148
Doug Zongker9931f7f2009-06-10 14:11:53 -0700149 mkdir(mount_point, 0755);
150
Stephen Smalley779701d2012-02-09 14:13:23 -0500151 if (secontext) {
152 freecon(secontext);
153 setfscreatecon(NULL);
154 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500155
Doug Zongker3d177d02010-07-01 09:18:44 -0700156 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700157 mtd_scan_partitions();
158 const MtdPartition* mtd;
159 mtd = mtd_find_partition_by_name(location);
160 if (mtd == NULL) {
Michael Runge5ddf4292014-10-24 14:14:41 -0700161 uiPrintf(state, "%s: no mtd partition named \"%s\"",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700162 name, location);
163 result = strdup("");
164 goto done;
165 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700166 if (mtd_mount_partition(mtd, mount_point, fs_type, 0 /* rw */) != 0) {
Michael Runge5ddf4292014-10-24 14:14:41 -0700167 uiPrintf(state, "mtd mount of %s failed: %s\n",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700168 location, strerror(errno));
169 result = strdup("");
170 goto done;
171 }
172 result = mount_point;
173 } else {
Doug Zongker3d177d02010-07-01 09:18:44 -0700174 if (mount(location, mount_point, fs_type,
Michael Runge168f7772014-10-22 17:05:08 -0700175 MS_NOATIME | MS_NODEV | MS_NODIRATIME,
176 has_mount_options ? mount_options : "") < 0) {
Michael Runge5ddf4292014-10-24 14:14:41 -0700177 uiPrintf(state, "%s: failed to mount %s at %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700178 name, location, mount_point, strerror(errno));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700179 result = strdup("");
180 } else {
181 result = mount_point;
182 }
183 }
184
185done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700186 free(fs_type);
187 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700188 free(location);
189 if (result != mount_point) free(mount_point);
Michael Runge168f7772014-10-22 17:05:08 -0700190 if (has_mount_options) free(mount_options);
Doug Zongker512536a2010-02-17 16:11:44 -0800191 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700192}
193
Doug Zongker8edb00c2009-06-11 17:21:44 -0700194
195// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800196Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700197 char* result = NULL;
198 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700199 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700200 }
201 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700202 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700203 return NULL;
204 }
205 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700206 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700207 goto done;
208 }
209
210 scan_mounted_volumes();
211 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
212 if (vol == NULL) {
213 result = strdup("");
214 } else {
215 result = mount_point;
216 }
217
218done:
219 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800220 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700221}
222
223
Doug Zongker512536a2010-02-17 16:11:44 -0800224Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700225 char* result = NULL;
226 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700227 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700228 }
229 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700230 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700231 return NULL;
232 }
233 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700234 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700235 goto done;
236 }
237
238 scan_mounted_volumes();
239 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
240 if (vol == NULL) {
Michael Runge5ddf4292014-10-24 14:14:41 -0700241 uiPrintf(state, "unmount of %s failed; no such volume\n", mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700242 result = strdup("");
243 } else {
Michael Runge5ddf4292014-10-24 14:14:41 -0700244 int ret = unmount_mounted_volume(vol);
245 if (ret != 0) {
246 uiPrintf(state, "unmount of %s failed (%d): %s\n",
247 mount_point, ret, strerror(errno));
248 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700249 result = mount_point;
250 }
251
252done:
253 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800254 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700255}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700256
JP Abgrall37aedb32014-06-16 19:07:39 -0700257static int exec_cmd(const char* path, char* const argv[]) {
258 int status;
259 pid_t child;
260 if ((child = vfork()) == 0) {
261 execv(path, argv);
262 _exit(-1);
263 }
264 waitpid(child, &status, 0);
265 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
266 printf("%s failed with status %d\n", path, WEXITSTATUS(status));
267 }
268 return WEXITSTATUS(status);
269}
270
Doug Zongker8edb00c2009-06-11 17:21:44 -0700271
Stephen Smalley779701d2012-02-09 14:13:23 -0500272// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700273//
Stephen Smalley779701d2012-02-09 14:13:23 -0500274// fs_type="yaffs2" partition_type="MTD" location=partition fs_size=<bytes> mount_point=<location>
275// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700276// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
277// if fs_size == 0, then make fs uses the entire partition.
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800278// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700279// if fs_size < 0, then reserve that many bytes at the end of the partition (not for "f2fs")
Doug Zongker512536a2010-02-17 16:11:44 -0800280Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700281 char* result = NULL;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400282 if (argc != 5) {
283 return ErrorAbort(state, "%s() expects 5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700284 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700285 char* fs_type;
286 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700287 char* location;
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800288 char* fs_size;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400289 char* mount_point;
Stephen Smalley779701d2012-02-09 14:13:23 -0500290
Stephen Smalley779701d2012-02-09 14:13:23 -0500291 if (ReadArgs(state, argv, 5, &fs_type, &partition_type, &location, &fs_size, &mount_point) < 0) {
292 return NULL;
293 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700294
Doug Zongker3d177d02010-07-01 09:18:44 -0700295 if (strlen(fs_type) == 0) {
296 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
297 goto done;
298 }
299 if (strlen(partition_type) == 0) {
300 ErrorAbort(state, "partition_type argument to %s() can't be empty",
301 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700302 goto done;
303 }
304 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700305 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700306 goto done;
307 }
308
Stephen Smalley516e4e22012-04-03 13:35:11 -0400309 if (strlen(mount_point) == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500310 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
311 goto done;
312 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500313
Doug Zongker3d177d02010-07-01 09:18:44 -0700314 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700315 mtd_scan_partitions();
316 const MtdPartition* mtd = mtd_find_partition_by_name(location);
317 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700318 printf("%s: no mtd partition named \"%s\"",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700319 name, location);
320 result = strdup("");
321 goto done;
322 }
323 MtdWriteContext* ctx = mtd_write_partition(mtd);
324 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700325 printf("%s: can't write \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700326 result = strdup("");
327 goto done;
328 }
329 if (mtd_erase_blocks(ctx, -1) == -1) {
330 mtd_write_close(ctx);
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700331 printf("%s: failed to erase \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700332 result = strdup("");
333 goto done;
334 }
335 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700336 printf("%s: failed to close \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700337 result = strdup("");
338 goto done;
339 }
340 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700341#ifdef USE_EXT4
342 } else if (strcmp(fs_type, "ext4") == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500343 int status = make_ext4fs(location, atoll(fs_size), mount_point, sehandle);
Doug Zongker3d177d02010-07-01 09:18:44 -0700344 if (status != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700345 printf("%s: make_ext4fs failed (%d) on %s",
Doug Zongker3d177d02010-07-01 09:18:44 -0700346 name, status, location);
347 result = strdup("");
348 goto done;
349 }
350 result = location;
JP Abgrall37aedb32014-06-16 19:07:39 -0700351 } else if (strcmp(fs_type, "f2fs") == 0) {
352 char *num_sectors;
353 if (asprintf(&num_sectors, "%lld", atoll(fs_size) / 512) <= 0) {
354 printf("format_volume: failed to create %s command for %s\n", fs_type, location);
355 result = strdup("");
356 goto done;
357 }
358 const char *f2fs_path = "/sbin/mkfs.f2fs";
359 const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", location, num_sectors, NULL};
360 int status = exec_cmd(f2fs_path, (char* const*)f2fs_argv);
361 free(num_sectors);
362 if (status != 0) {
363 printf("%s: mkfs.f2fs failed (%d) on %s",
364 name, status, location);
365 result = strdup("");
366 goto done;
367 }
368 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700369#endif
Doug Zongker9931f7f2009-06-10 14:11:53 -0700370 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700371 printf("%s: unsupported fs_type \"%s\" partition_type \"%s\"",
Doug Zongker3d177d02010-07-01 09:18:44 -0700372 name, fs_type, partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700373 }
374
375done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700376 free(fs_type);
377 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700378 if (result != location) free(location);
Doug Zongker512536a2010-02-17 16:11:44 -0800379 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700380}
381
Michael Rungece7ca712013-11-06 17:42:20 -0800382Value* RenameFn(const char* name, State* state, int argc, Expr* argv[]) {
383 char* result = NULL;
384 if (argc != 2) {
385 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
386 }
387
388 char* src_name;
389 char* dst_name;
390
391 if (ReadArgs(state, argv, 2, &src_name, &dst_name) < 0) {
392 return NULL;
393 }
394 if (strlen(src_name) == 0) {
395 ErrorAbort(state, "src_name argument to %s() can't be empty", name);
396 goto done;
397 }
398 if (strlen(dst_name) == 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700399 ErrorAbort(state, "dst_name argument to %s() can't be empty", name);
Michael Rungece7ca712013-11-06 17:42:20 -0800400 goto done;
401 }
Michael Rungea91ecc52014-07-21 17:40:02 -0700402 if (make_parents(dst_name) != 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700403 ErrorAbort(state, "Creating parent of %s failed, error %s",
Michael Rungea91ecc52014-07-21 17:40:02 -0700404 dst_name, strerror(errno));
Michael Runge2f0ef732014-10-22 14:28:23 -0700405 } else if (access(dst_name, F_OK) == 0 && access(src_name, F_OK) != 0) {
406 // File was already moved
407 result = dst_name;
Michael Rungea91ecc52014-07-21 17:40:02 -0700408 } else if (rename(src_name, dst_name) != 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700409 ErrorAbort(state, "Rename of %s to %s failed, error %s",
Michael Rungece7ca712013-11-06 17:42:20 -0800410 src_name, dst_name, strerror(errno));
411 } else {
412 result = dst_name;
413 }
414
415done:
416 free(src_name);
417 if (result != dst_name) free(dst_name);
418 return StringValue(result);
419}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700420
Doug Zongker512536a2010-02-17 16:11:44 -0800421Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700422 char** paths = malloc(argc * sizeof(char*));
423 int i;
424 for (i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700425 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700426 if (paths[i] == NULL) {
427 int j;
428 for (j = 0; j < i; ++i) {
429 free(paths[j]);
430 }
431 free(paths);
432 return NULL;
433 }
434 }
435
436 bool recursive = (strcmp(name, "delete_recursive") == 0);
437
438 int success = 0;
439 for (i = 0; i < argc; ++i) {
440 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
441 ++success;
442 free(paths[i]);
443 }
444 free(paths);
445
446 char buffer[10];
447 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800448 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700449}
450
Doug Zongker8edb00c2009-06-11 17:21:44 -0700451
Doug Zongker512536a2010-02-17 16:11:44 -0800452Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700453 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700454 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700455 }
456 char* frac_str;
457 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700458 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700459 return NULL;
460 }
461
462 double frac = strtod(frac_str, NULL);
463 int sec = strtol(sec_str, NULL, 10);
464
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700465 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700466 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
467
Doug Zongker9931f7f2009-06-10 14:11:53 -0700468 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800469 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700470}
471
Doug Zongker512536a2010-02-17 16:11:44 -0800472Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700473 if (argc != 1) {
474 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
475 }
476 char* frac_str;
477 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
478 return NULL;
479 }
480
481 double frac = strtod(frac_str, NULL);
482
483 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
484 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
485
Doug Zongker512536a2010-02-17 16:11:44 -0800486 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700487}
488
Doug Zongker8edb00c2009-06-11 17:21:44 -0700489// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800490Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700491 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700492 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700493 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700494 }
495 char* zip_path;
496 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700497 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700498
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700499 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700500
501 // To create a consistent system image, never use the clock for timestamps.
502 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
503
504 bool success = mzExtractRecursive(za, zip_path, dest_path,
Narayan Kamath9c0f5d62015-02-23 14:09:31 +0000505 &timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500506 NULL, NULL, sehandle);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700507 free(zip_path);
508 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800509 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700510}
511
Doug Zongker8edb00c2009-06-11 17:21:44 -0700512
513// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800514// or
515// package_extract_file(package_path)
516// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800517// function (the char* returned is actually a FileContents*).
518Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700519 int argc, Expr* argv[]) {
Doug Zongker5f875bf2014-08-22 14:53:43 -0700520 if (argc < 1 || argc > 2) {
521 return ErrorAbort(state, "%s() expects 1 or 2 args, got %d",
Doug Zongker6aece332010-02-01 14:40:12 -0800522 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700523 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700524 bool success = false;
Doug Zongker43772d22014-06-09 14:13:19 -0700525
526 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker43772d22014-06-09 14:13:19 -0700527
Doug Zongker5f875bf2014-08-22 14:53:43 -0700528 if (argc == 2) {
529 // The two-argument version extracts to a file.
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800530
531 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700532
Doug Zongker6aece332010-02-01 14:40:12 -0800533 char* zip_path;
534 char* dest_path;
Doug Zongker5f875bf2014-08-22 14:53:43 -0700535 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800536
Doug Zongker6aece332010-02-01 14:40:12 -0800537 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
538 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700539 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800540 goto done2;
541 }
542
543 FILE* f = fopen(dest_path, "wb");
544 if (f == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700545 printf("%s: can't open %s for write: %s\n",
Doug Zongker6aece332010-02-01 14:40:12 -0800546 name, dest_path, strerror(errno));
547 goto done2;
548 }
Doug Zongker5f875bf2014-08-22 14:53:43 -0700549 success = mzExtractZipEntryToFile(za, entry, fileno(f));
Doug Zongker6aece332010-02-01 14:40:12 -0800550 fclose(f);
551
552 done2:
553 free(zip_path);
554 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800555 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800556 } else {
557 // The one-argument version returns the contents of the file
558 // as the result.
559
560 char* zip_path;
Doug Zongker512536a2010-02-17 16:11:44 -0800561 Value* v = malloc(sizeof(Value));
562 v->type = VAL_BLOB;
563 v->size = -1;
564 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800565
566 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
567
568 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
569 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
570 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700571 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800572 goto done1;
573 }
574
Doug Zongker512536a2010-02-17 16:11:44 -0800575 v->size = mzGetZipEntryUncompLen(entry);
576 v->data = malloc(v->size);
577 if (v->data == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700578 printf("%s: failed to allocate %ld bytes for %s\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800579 name, (long)v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800580 goto done1;
581 }
582
Doug Zongker512536a2010-02-17 16:11:44 -0800583 success = mzExtractZipEntryToBuffer(za, entry,
584 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800585
586 done1:
587 free(zip_path);
588 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800589 free(v->data);
590 v->data = NULL;
591 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800592 }
Doug Zongker512536a2010-02-17 16:11:44 -0800593 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700594 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700595}
596
Doug Zongkera23075f2012-08-06 16:19:09 -0700597// Create all parent directories of name, if necessary.
598static int make_parents(char* name) {
599 char* p;
600 for (p = name + (strlen(name)-1); p > name; --p) {
601 if (*p != '/') continue;
602 *p = '\0';
603 if (make_parents(name) < 0) return -1;
604 int result = mkdir(name, 0700);
Michael Rungea91ecc52014-07-21 17:40:02 -0700605 if (result == 0) printf("created [%s]\n", name);
Doug Zongkera23075f2012-08-06 16:19:09 -0700606 *p = '/';
607 if (result == 0 || errno == EEXIST) {
608 // successfully created or already existed; we're done
609 return 0;
610 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700611 printf("failed to mkdir %s: %s\n", name, strerror(errno));
Doug Zongkera23075f2012-08-06 16:19:09 -0700612 return -1;
613 }
614 }
615 return 0;
616}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700617
Doug Zongker9931f7f2009-06-10 14:11:53 -0700618// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700619// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800620Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700621 if (argc == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700622 return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700623 }
624 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700625 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700626 if (target == NULL) return NULL;
627
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700628 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700629 if (srcs == NULL) {
630 free(target);
631 return NULL;
632 }
633
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700634 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700635 int i;
636 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700637 if (unlink(srcs[i]) < 0) {
638 if (errno != ENOENT) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700639 printf("%s: failed to remove %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700640 name, srcs[i], strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700641 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700642 }
643 }
Doug Zongkera23075f2012-08-06 16:19:09 -0700644 if (make_parents(srcs[i])) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700645 printf("%s: failed to symlink %s to %s: making parents failed\n",
Doug Zongkera23075f2012-08-06 16:19:09 -0700646 name, srcs[i], target);
647 ++bad;
648 }
Doug Zongker60babf82009-09-18 15:11:24 -0700649 if (symlink(target, srcs[i]) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700650 printf("%s: failed to symlink %s to %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700651 name, srcs[i], target, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700652 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700653 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700654 free(srcs[i]);
655 }
656 free(srcs);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700657 if (bad) {
658 return ErrorAbort(state, "%s: some symlinks failed", name);
659 }
Doug Zongker512536a2010-02-17 16:11:44 -0800660 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700661}
662
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700663struct perm_parsed_args {
664 bool has_uid;
665 uid_t uid;
666 bool has_gid;
667 gid_t gid;
668 bool has_mode;
669 mode_t mode;
670 bool has_fmode;
671 mode_t fmode;
672 bool has_dmode;
673 mode_t dmode;
674 bool has_selabel;
675 char* selabel;
676 bool has_capabilities;
677 uint64_t capabilities;
678};
679
Michael Runged4a63422014-10-22 19:48:41 -0700680static struct perm_parsed_args ParsePermArgs(State * state, int argc, char** args) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700681 int i;
682 struct perm_parsed_args parsed;
683 int bad = 0;
684 static int max_warnings = 20;
685
686 memset(&parsed, 0, sizeof(parsed));
687
688 for (i = 1; i < argc; i += 2) {
689 if (strcmp("uid", args[i]) == 0) {
690 int64_t uid;
691 if (sscanf(args[i+1], "%" SCNd64, &uid) == 1) {
692 parsed.uid = uid;
693 parsed.has_uid = true;
694 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700695 uiPrintf(state, "ParsePermArgs: invalid UID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700696 bad++;
697 }
698 continue;
699 }
700 if (strcmp("gid", args[i]) == 0) {
701 int64_t gid;
702 if (sscanf(args[i+1], "%" SCNd64, &gid) == 1) {
703 parsed.gid = gid;
704 parsed.has_gid = true;
705 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700706 uiPrintf(state, "ParsePermArgs: invalid GID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700707 bad++;
708 }
709 continue;
710 }
711 if (strcmp("mode", args[i]) == 0) {
712 int32_t mode;
713 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
714 parsed.mode = mode;
715 parsed.has_mode = true;
716 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700717 uiPrintf(state, "ParsePermArgs: invalid mode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700718 bad++;
719 }
720 continue;
721 }
722 if (strcmp("dmode", args[i]) == 0) {
723 int32_t mode;
724 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
725 parsed.dmode = mode;
726 parsed.has_dmode = true;
727 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700728 uiPrintf(state, "ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700729 bad++;
730 }
731 continue;
732 }
733 if (strcmp("fmode", args[i]) == 0) {
734 int32_t mode;
735 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
736 parsed.fmode = mode;
737 parsed.has_fmode = true;
738 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700739 uiPrintf(state, "ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700740 bad++;
741 }
742 continue;
743 }
744 if (strcmp("capabilities", args[i]) == 0) {
745 int64_t capabilities;
746 if (sscanf(args[i+1], "%" SCNi64, &capabilities) == 1) {
747 parsed.capabilities = capabilities;
748 parsed.has_capabilities = true;
749 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700750 uiPrintf(state, "ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700751 bad++;
752 }
753 continue;
754 }
755 if (strcmp("selabel", args[i]) == 0) {
756 if (args[i+1][0] != '\0') {
757 parsed.selabel = args[i+1];
758 parsed.has_selabel = true;
759 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700760 uiPrintf(state, "ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700761 bad++;
762 }
763 continue;
764 }
765 if (max_warnings != 0) {
766 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i]);
767 max_warnings--;
768 if (max_warnings == 0) {
769 printf("ParsedPermArgs: suppressing further warnings\n");
770 }
771 }
772 }
773 return parsed;
774}
775
776static int ApplyParsedPerms(
Michael Runged4a63422014-10-22 19:48:41 -0700777 State * state,
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700778 const char* filename,
779 const struct stat *statptr,
780 struct perm_parsed_args parsed)
781{
782 int bad = 0;
783
Nick Kralevich68802412014-10-23 20:36:42 -0700784 if (parsed.has_selabel) {
785 if (lsetfilecon(filename, parsed.selabel) != 0) {
786 uiPrintf(state, "ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n",
787 filename, parsed.selabel, strerror(errno));
788 bad++;
789 }
790 }
791
Nick Kraleviche4612512013-09-10 15:34:19 -0700792 /* ignore symlinks */
793 if (S_ISLNK(statptr->st_mode)) {
Nick Kralevich68802412014-10-23 20:36:42 -0700794 return bad;
Nick Kraleviche4612512013-09-10 15:34:19 -0700795 }
796
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700797 if (parsed.has_uid) {
798 if (chown(filename, parsed.uid, -1) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700799 uiPrintf(state, "ApplyParsedPerms: chown of %s to %d failed: %s\n",
800 filename, parsed.uid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700801 bad++;
802 }
803 }
804
805 if (parsed.has_gid) {
806 if (chown(filename, -1, parsed.gid) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700807 uiPrintf(state, "ApplyParsedPerms: chgrp of %s to %d failed: %s\n",
808 filename, parsed.gid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700809 bad++;
810 }
811 }
812
813 if (parsed.has_mode) {
814 if (chmod(filename, parsed.mode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700815 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
816 filename, parsed.mode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700817 bad++;
818 }
819 }
820
821 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
822 if (chmod(filename, parsed.dmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700823 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
824 filename, parsed.dmode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700825 bad++;
826 }
827 }
828
829 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
830 if (chmod(filename, parsed.fmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700831 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700832 filename, parsed.fmode, strerror(errno));
833 bad++;
834 }
835 }
836
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700837 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
838 if (parsed.capabilities == 0) {
839 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
840 // Report failure unless it's ENODATA (attribute not set)
Michael Runged4a63422014-10-22 19:48:41 -0700841 uiPrintf(state, "ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700842 filename, parsed.capabilities, strerror(errno));
843 bad++;
844 }
845 } else {
846 struct vfs_cap_data cap_data;
847 memset(&cap_data, 0, sizeof(cap_data));
848 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
849 cap_data.data[0].permitted = (uint32_t) (parsed.capabilities & 0xffffffff);
850 cap_data.data[0].inheritable = 0;
851 cap_data.data[1].permitted = (uint32_t) (parsed.capabilities >> 32);
852 cap_data.data[1].inheritable = 0;
853 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700854 uiPrintf(state, "ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n",
855 filename, parsed.capabilities, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700856 bad++;
857 }
858 }
859 }
860
861 return bad;
862}
863
864// nftw doesn't allow us to pass along context, so we need to use
865// global variables. *sigh*
866static struct perm_parsed_args recursive_parsed_args;
Michael Runged4a63422014-10-22 19:48:41 -0700867static State* recursive_state;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700868
869static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr,
870 int fileflags, struct FTW *pfwt) {
Michael Runged4a63422014-10-22 19:48:41 -0700871 return ApplyParsedPerms(recursive_state, filename, statptr, recursive_parsed_args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700872}
873
874static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
875 int i;
876 int bad = 0;
877 static int nwarnings = 0;
878 struct stat sb;
879 Value* result = NULL;
880
881 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
882
883 if ((argc % 2) != 1) {
884 return ErrorAbort(state, "%s() expects an odd number of arguments, got %d",
885 name, argc);
886 }
887
888 char** args = ReadVarArgs(state, argc, argv);
889 if (args == NULL) return NULL;
890
891 if (lstat(args[0], &sb) == -1) {
892 result = ErrorAbort(state, "%s: Error on lstat of \"%s\": %s", name, args[0], strerror(errno));
893 goto done;
894 }
895
Michael Runged4a63422014-10-22 19:48:41 -0700896 struct perm_parsed_args parsed = ParsePermArgs(state, argc, args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700897
898 if (recursive) {
899 recursive_parsed_args = parsed;
Michael Runged4a63422014-10-22 19:48:41 -0700900 recursive_state = state;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700901 bad += nftw(args[0], do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
902 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
Michael Runged4a63422014-10-22 19:48:41 -0700903 recursive_state = NULL;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700904 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700905 bad += ApplyParsedPerms(state, args[0], &sb, parsed);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700906 }
907
908done:
909 for (i = 0; i < argc; ++i) {
910 free(args[i]);
911 }
912 free(args);
913
914 if (result != NULL) {
915 return result;
916 }
917
918 if (bad > 0) {
919 return ErrorAbort(state, "%s: some changes failed", name);
920 }
921
922 return StringValue(strdup(""));
923}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700924
Doug Zongker512536a2010-02-17 16:11:44 -0800925Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700926 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700927 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700928 }
929 char* key;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700930 key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700931 if (key == NULL) return NULL;
932
933 char value[PROPERTY_VALUE_MAX];
934 property_get(key, value, "");
935 free(key);
936
Doug Zongker512536a2010-02-17 16:11:44 -0800937 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700938}
939
940
Doug Zongker47cace92009-06-18 10:11:50 -0700941// file_getprop(file, key)
942//
943// interprets 'file' as a getprop-style file (key=value pairs, one
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700944// per line. # comment lines,blank lines, lines without '=' ignored),
945// and returns the value for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800946Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700947 char* result = NULL;
948 char* buffer = NULL;
949 char* filename;
950 char* key;
951 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
952 return NULL;
953 }
954
955 struct stat st;
956 if (stat(filename, &st) < 0) {
957 ErrorAbort(state, "%s: failed to stat \"%s\": %s",
958 name, filename, strerror(errno));
959 goto done;
960 }
961
962#define MAX_FILE_GETPROP_SIZE 65536
963
964 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
965 ErrorAbort(state, "%s too large for %s (max %d)",
966 filename, name, MAX_FILE_GETPROP_SIZE);
967 goto done;
968 }
969
970 buffer = malloc(st.st_size+1);
971 if (buffer == NULL) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700972 ErrorAbort(state, "%s: failed to alloc %lld bytes", name, (long long)st.st_size+1);
Doug Zongker47cace92009-06-18 10:11:50 -0700973 goto done;
974 }
975
976 FILE* f = fopen(filename, "rb");
977 if (f == NULL) {
978 ErrorAbort(state, "%s: failed to open %s: %s",
979 name, filename, strerror(errno));
980 goto done;
981 }
982
983 if (fread(buffer, 1, st.st_size, f) != st.st_size) {
Doug Zongkera23075f2012-08-06 16:19:09 -0700984 ErrorAbort(state, "%s: failed to read %lld bytes from %s",
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700985 name, (long long)st.st_size+1, filename);
Doug Zongker47cace92009-06-18 10:11:50 -0700986 fclose(f);
987 goto done;
988 }
989 buffer[st.st_size] = '\0';
990
991 fclose(f);
992
993 char* line = strtok(buffer, "\n");
994 do {
995 // skip whitespace at start of line
996 while (*line && isspace(*line)) ++line;
997
998 // comment or blank line: skip to next line
999 if (*line == '\0' || *line == '#') continue;
1000
1001 char* equal = strchr(line, '=');
1002 if (equal == NULL) {
Michael Rungeaa1a31e2014-04-25 18:47:18 -07001003 continue;
Doug Zongker47cace92009-06-18 10:11:50 -07001004 }
1005
1006 // trim whitespace between key and '='
1007 char* key_end = equal-1;
1008 while (key_end > line && isspace(*key_end)) --key_end;
1009 key_end[1] = '\0';
1010
1011 // not the key we're looking for
1012 if (strcmp(key, line) != 0) continue;
1013
1014 // skip whitespace after the '=' to the start of the value
1015 char* val_start = equal+1;
1016 while(*val_start && isspace(*val_start)) ++val_start;
1017
1018 // trim trailing whitespace
1019 char* val_end = val_start + strlen(val_start)-1;
1020 while (val_end > val_start && isspace(*val_end)) --val_end;
1021 val_end[1] = '\0';
1022
1023 result = strdup(val_start);
1024 break;
1025
1026 } while ((line = strtok(NULL, "\n")));
1027
1028 if (result == NULL) result = strdup("");
1029
1030 done:
1031 free(filename);
1032 free(key);
1033 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -08001034 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -07001035}
1036
1037
Doug Zongker8edb00c2009-06-11 17:21:44 -07001038static bool write_raw_image_cb(const unsigned char* data,
1039 int data_len, void* ctx) {
1040 int r = mtd_write_data((MtdWriteContext*)ctx, (const char *)data, data_len);
1041 if (r == data_len) return true;
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001042 printf("%s\n", strerror(errno));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001043 return false;
1044}
1045
Doug Zongker179b2d92011-04-12 15:49:04 -07001046// write_raw_image(filename_or_blob, partition)
Doug Zongker512536a2010-02-17 16:11:44 -08001047Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001048 char* result = NULL;
1049
Doug Zongker179b2d92011-04-12 15:49:04 -07001050 Value* partition_value;
1051 Value* contents;
1052 if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001053 return NULL;
1054 }
1055
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001056 char* partition = NULL;
Doug Zongker179b2d92011-04-12 15:49:04 -07001057 if (partition_value->type != VAL_STRING) {
1058 ErrorAbort(state, "partition argument to %s must be string", name);
1059 goto done;
1060 }
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001061 partition = partition_value->data;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001062 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001063 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001064 goto done;
1065 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001066 if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001067 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001068 goto done;
1069 }
1070
1071 mtd_scan_partitions();
1072 const MtdPartition* mtd = mtd_find_partition_by_name(partition);
1073 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001074 printf("%s: no mtd partition named \"%s\"\n", name, partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001075 result = strdup("");
1076 goto done;
1077 }
1078
1079 MtdWriteContext* ctx = mtd_write_partition(mtd);
1080 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001081 printf("%s: can't write mtd partition \"%s\"\n",
Doug Zongker8edb00c2009-06-11 17:21:44 -07001082 name, partition);
1083 result = strdup("");
1084 goto done;
1085 }
1086
1087 bool success;
1088
Doug Zongker179b2d92011-04-12 15:49:04 -07001089 if (contents->type == VAL_STRING) {
1090 // we're given a filename as the contents
1091 char* filename = contents->data;
1092 FILE* f = fopen(filename, "rb");
1093 if (f == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001094 printf("%s: can't open %s: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -07001095 name, filename, strerror(errno));
1096 result = strdup("");
1097 goto done;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001098 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001099
1100 success = true;
1101 char* buffer = malloc(BUFSIZ);
1102 int read;
1103 while (success && (read = fread(buffer, 1, BUFSIZ, f)) > 0) {
1104 int wrote = mtd_write_data(ctx, buffer, read);
1105 success = success && (wrote == read);
1106 }
1107 free(buffer);
1108 fclose(f);
1109 } else {
1110 // we're given a blob as the contents
1111 ssize_t wrote = mtd_write_data(ctx, contents->data, contents->size);
1112 success = (wrote == contents->size);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001113 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001114 if (!success) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001115 printf("mtd_write_data to %s failed: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -07001116 partition, strerror(errno));
1117 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001118
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001119 if (mtd_erase_blocks(ctx, -1) == -1) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001120 printf("%s: error erasing blocks of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001121 }
1122 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001123 printf("%s: error closing write of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001124 }
1125
Doug Zongker179b2d92011-04-12 15:49:04 -07001126 printf("%s %s partition\n",
1127 success ? "wrote" : "failed to write", partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001128
1129 result = success ? partition : strdup("");
1130
1131done:
Doug Zongker179b2d92011-04-12 15:49:04 -07001132 if (result != partition) FreeValue(partition_value);
1133 FreeValue(contents);
Doug Zongker512536a2010-02-17 16:11:44 -08001134 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001135}
1136
Doug Zongker8edb00c2009-06-11 17:21:44 -07001137// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001138Value* ApplyPatchSpaceFn(const char* name, State* state,
1139 int argc, Expr* argv[]) {
1140 char* bytes_str;
1141 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
1142 return NULL;
1143 }
1144
1145 char* endptr;
1146 size_t bytes = strtol(bytes_str, &endptr, 10);
1147 if (bytes == 0 && endptr == bytes_str) {
1148 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n",
1149 name, bytes_str);
1150 free(bytes_str);
1151 return NULL;
1152 }
1153
1154 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
1155}
1156
Doug Zongker52b40362014-02-10 15:30:30 -08001157// apply_patch(file, size, init_sha1, tgt_sha1, patch)
1158
Doug Zongker512536a2010-02-17 16:11:44 -08001159Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001160 if (argc < 6 || (argc % 2) == 1) {
1161 return ErrorAbort(state, "%s(): expected at least 6 args and an "
1162 "even number, got %d",
1163 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001164 }
1165
Doug Zongkerc4351c72010-02-22 14:46:32 -08001166 char* source_filename;
1167 char* target_filename;
1168 char* target_sha1;
1169 char* target_size_str;
1170 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
1171 &target_sha1, &target_size_str) < 0) {
1172 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001173 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001174
Doug Zongkerc4351c72010-02-22 14:46:32 -08001175 char* endptr;
1176 size_t target_size = strtol(target_size_str, &endptr, 10);
1177 if (target_size == 0 && endptr == target_size_str) {
1178 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count",
1179 name, target_size_str);
1180 free(source_filename);
1181 free(target_filename);
1182 free(target_sha1);
1183 free(target_size_str);
1184 return NULL;
1185 }
1186
1187 int patchcount = (argc-4) / 2;
1188 Value** patches = ReadValueVarArgs(state, argc-4, argv+4);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001189
1190 int i;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001191 for (i = 0; i < patchcount; ++i) {
1192 if (patches[i*2]->type != VAL_STRING) {
1193 ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i);
1194 break;
1195 }
1196 if (patches[i*2+1]->type != VAL_BLOB) {
1197 ErrorAbort(state, "%s(): patch #%d is not blob", name, i);
1198 break;
1199 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001200 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001201 if (i != patchcount) {
1202 for (i = 0; i < patchcount*2; ++i) {
1203 FreeValue(patches[i]);
1204 }
1205 free(patches);
1206 return NULL;
1207 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001208
Doug Zongkerc4351c72010-02-22 14:46:32 -08001209 char** patch_sha_str = malloc(patchcount * sizeof(char*));
1210 for (i = 0; i < patchcount; ++i) {
1211 patch_sha_str[i] = patches[i*2]->data;
1212 patches[i*2]->data = NULL;
1213 FreeValue(patches[i*2]);
1214 patches[i] = patches[i*2+1];
Doug Zongker8edb00c2009-06-11 17:21:44 -07001215 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001216
1217 int result = applypatch(source_filename, target_filename,
1218 target_sha1, target_size,
Doug Zongkera3ccba62012-08-20 15:28:02 -07001219 patchcount, patch_sha_str, patches, NULL);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001220
1221 for (i = 0; i < patchcount; ++i) {
1222 FreeValue(patches[i]);
1223 }
1224 free(patch_sha_str);
1225 free(patches);
1226
1227 return StringValue(strdup(result == 0 ? "t" : ""));
1228}
1229
1230// apply_patch_check(file, [sha1_1, ...])
1231Value* ApplyPatchCheckFn(const char* name, State* state,
1232 int argc, Expr* argv[]) {
1233 if (argc < 1) {
1234 return ErrorAbort(state, "%s(): expected at least 1 arg, got %d",
1235 name, argc);
1236 }
1237
1238 char* filename;
1239 if (ReadArgs(state, argv, 1, &filename) < 0) {
1240 return NULL;
1241 }
1242
1243 int patchcount = argc-1;
1244 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
1245
1246 int result = applypatch_check(filename, patchcount, sha1s);
1247
1248 int i;
1249 for (i = 0; i < patchcount; ++i) {
1250 free(sha1s[i]);
1251 }
1252 free(sha1s);
1253
1254 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001255}
1256
Doug Zongker512536a2010-02-17 16:11:44 -08001257Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001258 char** args = ReadVarArgs(state, argc, argv);
1259 if (args == NULL) {
1260 return NULL;
1261 }
1262
1263 int size = 0;
1264 int i;
1265 for (i = 0; i < argc; ++i) {
1266 size += strlen(args[i]);
1267 }
1268 char* buffer = malloc(size+1);
1269 size = 0;
1270 for (i = 0; i < argc; ++i) {
1271 strcpy(buffer+size, args[i]);
1272 size += strlen(args[i]);
1273 free(args[i]);
1274 }
1275 free(args);
1276 buffer[size] = '\0';
Michael Runged4a63422014-10-22 19:48:41 -07001277 uiPrint(state, buffer);
Doug Zongker512536a2010-02-17 16:11:44 -08001278 return StringValue(buffer);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001279}
1280
Doug Zongkerd0181b82011-10-19 10:51:12 -07001281Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
1282 if (argc != 0) {
1283 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1284 }
1285 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
1286 return StringValue(strdup("t"));
1287}
1288
Doug Zongker512536a2010-02-17 16:11:44 -08001289Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001290 if (argc < 1) {
1291 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1292 }
1293 char** args = ReadVarArgs(state, argc, argv);
1294 if (args == NULL) {
1295 return NULL;
1296 }
1297
1298 char** args2 = malloc(sizeof(char*) * (argc+1));
1299 memcpy(args2, args, sizeof(char*) * argc);
1300 args2[argc] = NULL;
1301
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001302 printf("about to run program [%s] with %d args\n", args2[0], argc);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001303
1304 pid_t child = fork();
1305 if (child == 0) {
1306 execv(args2[0], args2);
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001307 printf("run_program: execv failed: %s\n", strerror(errno));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001308 _exit(1);
1309 }
1310 int status;
1311 waitpid(child, &status, 0);
1312 if (WIFEXITED(status)) {
1313 if (WEXITSTATUS(status) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001314 printf("run_program: child exited with status %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001315 WEXITSTATUS(status));
1316 }
1317 } else if (WIFSIGNALED(status)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001318 printf("run_program: child terminated by signal %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001319 WTERMSIG(status));
1320 }
1321
1322 int i;
1323 for (i = 0; i < argc; ++i) {
1324 free(args[i]);
1325 }
1326 free(args);
1327 free(args2);
1328
1329 char buffer[20];
1330 sprintf(buffer, "%d", status);
1331
Doug Zongker512536a2010-02-17 16:11:44 -08001332 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001333}
1334
Doug Zongker512536a2010-02-17 16:11:44 -08001335// sha1_check(data)
1336// to return the sha1 of the data (given in the format returned by
1337// read_file).
1338//
1339// sha1_check(data, sha1_hex, [sha1_hex, ...])
1340// returns the sha1 of the file if it matches any of the hex
1341// strings passed, or "" if it does not equal any of them.
1342//
1343Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1344 if (argc < 1) {
1345 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1346 }
1347
1348 Value** args = ReadValueVarArgs(state, argc, argv);
1349 if (args == NULL) {
1350 return NULL;
1351 }
1352
1353 if (args[0]->size < 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001354 return StringValue(strdup(""));
1355 }
1356 uint8_t digest[SHA_DIGEST_SIZE];
Doug Zongkerbac7fba2013-04-10 11:32:17 -07001357 SHA_hash(args[0]->data, args[0]->size, digest);
Doug Zongker512536a2010-02-17 16:11:44 -08001358 FreeValue(args[0]);
1359
1360 if (argc == 1) {
1361 return StringValue(PrintSha1(digest));
1362 }
1363
1364 int i;
1365 uint8_t* arg_digest = malloc(SHA_DIGEST_SIZE);
1366 for (i = 1; i < argc; ++i) {
1367 if (args[i]->type != VAL_STRING) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001368 printf("%s(): arg %d is not a string; skipping",
Doug Zongker512536a2010-02-17 16:11:44 -08001369 name, i);
1370 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1371 // Warn about bad args and skip them.
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001372 printf("%s(): error parsing \"%s\" as sha-1; skipping",
1373 name, args[i]->data);
Doug Zongker512536a2010-02-17 16:11:44 -08001374 } else if (memcmp(digest, arg_digest, SHA_DIGEST_SIZE) == 0) {
1375 break;
1376 }
1377 FreeValue(args[i]);
1378 }
1379 if (i >= argc) {
1380 // Didn't match any of the hex strings; return false.
1381 return StringValue(strdup(""));
1382 }
1383 // Found a match; free all the remaining arguments and return the
1384 // matched one.
1385 int j;
1386 for (j = i+1; j < argc; ++j) {
1387 FreeValue(args[j]);
1388 }
1389 return args[i];
1390}
1391
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001392// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001393// is actually a FileContents*).
1394Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1395 if (argc != 1) {
1396 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1397 }
1398 char* filename;
1399 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1400
1401 Value* v = malloc(sizeof(Value));
1402 v->type = VAL_BLOB;
1403
1404 FileContents fc;
Doug Zongkera1bc1482014-02-13 15:18:19 -08001405 if (LoadFileContents(filename, &fc) != 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001406 free(filename);
Michael Runge6eed2242013-12-13 17:13:11 -08001407 v->size = -1;
1408 v->data = NULL;
Doug Zongker512536a2010-02-17 16:11:44 -08001409 free(fc.data);
Michael Runge6eed2242013-12-13 17:13:11 -08001410 return v;
Doug Zongker512536a2010-02-17 16:11:44 -08001411 }
1412
1413 v->size = fc.size;
1414 v->data = (char*)fc.data;
1415
1416 free(filename);
1417 return v;
1418}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001419
Doug Zongkerc87bab12013-11-25 13:53:25 -08001420// Immediately reboot the device. Recovery is not finished normally,
1421// so if you reboot into recovery it will re-start applying the
1422// current package (because nothing has cleared the copy of the
1423// arguments stored in the BCB).
1424//
1425// The argument is the partition name passed to the android reboot
1426// property. It can be "recovery" to boot from the recovery
1427// partition, or "" (empty string) to boot from the regular boot
1428// partition.
1429Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) {
1430 if (argc != 2) {
1431 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1432 }
1433
1434 char* filename;
1435 char* property;
1436 if (ReadArgs(state, argv, 2, &filename, &property) < 0) return NULL;
1437
1438 char buffer[80];
1439
1440 // zero out the 'command' field of the bootloader message.
1441 memset(buffer, 0, sizeof(((struct bootloader_message*)0)->command));
1442 FILE* f = fopen(filename, "r+b");
1443 fseek(f, offsetof(struct bootloader_message, command), SEEK_SET);
1444 fwrite(buffer, sizeof(((struct bootloader_message*)0)->command), 1, f);
1445 fclose(f);
1446 free(filename);
1447
1448 strcpy(buffer, "reboot,");
1449 if (property != NULL) {
1450 strncat(buffer, property, sizeof(buffer)-10);
1451 }
1452
1453 property_set(ANDROID_RB_PROPERTY, buffer);
1454
1455 sleep(5);
1456 free(property);
1457 ErrorAbort(state, "%s() failed to reboot", name);
1458 return NULL;
1459}
1460
1461// Store a string value somewhere that future invocations of recovery
1462// can access it. This value is called the "stage" and can be used to
1463// drive packages that need to do reboots in the middle of
1464// installation and keep track of where they are in the multi-stage
1465// install.
1466//
1467// The first argument is the block device for the misc partition
1468// ("/misc" in the fstab), which is where this value is stored. The
1469// second argument is the string to store; it should not exceed 31
1470// bytes.
1471Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
1472 if (argc != 2) {
1473 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1474 }
1475
1476 char* filename;
1477 char* stagestr;
1478 if (ReadArgs(state, argv, 2, &filename, &stagestr) < 0) return NULL;
1479
1480 // Store this value in the misc partition, immediately after the
1481 // bootloader message that the main recovery uses to save its
1482 // arguments in case of the device restarting midway through
1483 // package installation.
1484 FILE* f = fopen(filename, "r+b");
1485 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1486 int to_write = strlen(stagestr)+1;
1487 int max_size = sizeof(((struct bootloader_message*)0)->stage);
1488 if (to_write > max_size) {
1489 to_write = max_size;
1490 stagestr[max_size-1] = 0;
1491 }
1492 fwrite(stagestr, to_write, 1, f);
1493 fclose(f);
1494
1495 free(stagestr);
1496 return StringValue(filename);
1497}
1498
1499// Return the value most recently saved with SetStageFn. The argument
1500// is the block device for the misc partition.
1501Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001502 if (argc != 1) {
Doug Zongkerc87bab12013-11-25 13:53:25 -08001503 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1504 }
1505
1506 char* filename;
1507 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1508
1509 char buffer[sizeof(((struct bootloader_message*)0)->stage)];
1510 FILE* f = fopen(filename, "rb");
1511 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1512 fread(buffer, sizeof(buffer), 1, f);
1513 fclose(f);
1514 buffer[sizeof(buffer)-1] = '\0';
1515
1516 return StringValue(strdup(buffer));
1517}
1518
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001519Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) {
1520 if (argc != 2) {
1521 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1522 }
1523
1524 char* filename;
1525 char* len_str;
1526 if (ReadArgs(state, argv, 2, &filename, &len_str) < 0) return NULL;
1527
1528 size_t len = strtoull(len_str, NULL, 0);
1529 int fd = open(filename, O_WRONLY, 0644);
1530 int success = wipe_block_device(fd, len);
1531
1532 free(filename);
1533 free(len_str);
1534
1535 close(fd);
1536
1537 return StringValue(strdup(success ? "t" : ""));
1538}
1539
Doug Zongkerc704e062014-05-23 08:40:35 -07001540Value* EnableRebootFn(const char* name, State* state, int argc, Expr* argv[]) {
1541 if (argc != 0) {
1542 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1543 }
1544 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
1545 fprintf(ui->cmd_pipe, "enable_reboot\n");
1546 return StringValue(strdup("t"));
1547}
1548
Michael Rungeacf47db2014-11-21 00:12:28 -08001549Value* Tune2FsFn(const char* name, State* state, int argc, Expr* argv[]) {
1550 if (argc == 0) {
1551 return ErrorAbort(state, "%s() expects args, got %d", name, argc);
1552 }
1553
1554 char** args = ReadVarArgs(state, argc, argv);
1555 if (args == NULL) {
1556 return ErrorAbort(state, "%s() could not read args", name);
1557 }
1558
1559 int i;
1560 char** args2 = malloc(sizeof(char*) * (argc+1));
1561 // Tune2fs expects the program name as its args[0]
1562 args2[0] = strdup(name);
1563 for (i = 0; i < argc; ++i) {
1564 args2[i + 1] = args[i];
1565 }
1566 int result = tune2fs_main(argc + 1, args2);
1567 for (i = 0; i < argc; ++i) {
1568 free(args[i]);
1569 }
1570 free(args);
1571
1572 free(args2[0]);
1573 free(args2);
1574 if (result != 0) {
1575 return ErrorAbort(state, "%s() returned error code %d", name, result);
1576 }
1577 return StringValue(strdup("t"));
1578}
1579
Doug Zongker9931f7f2009-06-10 14:11:53 -07001580void RegisterInstallFunctions() {
1581 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001582 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001583 RegisterFunction("unmount", UnmountFn);
1584 RegisterFunction("format", FormatFn);
1585 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001586 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001587 RegisterFunction("delete", DeleteFn);
1588 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001589 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1590 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001591 RegisterFunction("symlink", SymlinkFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001592
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001593 // Usage:
1594 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1595 // Example:
1596 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1597 RegisterFunction("set_metadata", SetMetadataFn);
1598
1599 // Usage:
1600 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1601 // Example:
1602 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1603 RegisterFunction("set_metadata_recursive", SetMetadataFn);
1604
Doug Zongker8edb00c2009-06-11 17:21:44 -07001605 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001606 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001607 RegisterFunction("write_raw_image", WriteRawImageFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001608
1609 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001610 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1611 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001612
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001613 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001614
Doug Zongker512536a2010-02-17 16:11:44 -08001615 RegisterFunction("read_file", ReadFileFn);
1616 RegisterFunction("sha1_check", Sha1CheckFn);
Michael Rungece7ca712013-11-06 17:42:20 -08001617 RegisterFunction("rename", RenameFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001618
Doug Zongkerd0181b82011-10-19 10:51:12 -07001619 RegisterFunction("wipe_cache", WipeCacheFn);
1620
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001621 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001622
1623 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001624
1625 RegisterFunction("reboot_now", RebootNowFn);
1626 RegisterFunction("get_stage", GetStageFn);
1627 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -07001628
1629 RegisterFunction("enable_reboot", EnableRebootFn);
Michael Rungeacf47db2014-11-21 00:12:28 -08001630 RegisterFunction("tune2fs", Tune2FsFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001631}