blob: a2bc4029fcb4aec73896fa73bcf460cc745838ba [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");
64}
65
66__attribute__((__format__(printf, 2, 3))) __nonnull((2))
67void uiPrintf(State* state, const char* format, ...) {
68 char error_msg[1024];
69 va_list ap;
70 va_start(ap, format);
71 vsnprintf(error_msg, sizeof(error_msg), format, ap);
72 va_end(ap);
73 uiPrint(state, error_msg);
74}
75
Doug Zongker52b40362014-02-10 15:30:30 -080076// Take a sha-1 digest and return it as a newly-allocated hex string.
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070077char* PrintSha1(const uint8_t* digest) {
Tao Bao818fa782015-06-23 23:23:33 -070078 char* buffer = reinterpret_cast<char*>(malloc(SHA_DIGEST_SIZE*2 + 1));
Doug Zongker52b40362014-02-10 15:30:30 -080079 const char* alphabet = "0123456789abcdef";
Tao Bao818fa782015-06-23 23:23:33 -070080 size_t i;
Doug Zongker52b40362014-02-10 15:30:30 -080081 for (i = 0; i < SHA_DIGEST_SIZE; ++i) {
82 buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf];
83 buffer[i*2+1] = alphabet[digest[i] & 0xf];
84 }
85 buffer[i*2] = '\0';
86 return buffer;
87}
88
Doug Zongker3d177d02010-07-01 09:18:44 -070089// mount(fs_type, partition_type, location, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -070090//
Doug Zongker3d177d02010-07-01 09:18:44 -070091// fs_type="yaffs2" partition_type="MTD" location=partition
92// fs_type="ext4" partition_type="EMMC" location=device
Doug Zongker512536a2010-02-17 16:11:44 -080093Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070094 char* result = NULL;
Michael Runge168f7772014-10-22 17:05:08 -070095 if (argc != 4 && argc != 5) {
96 return ErrorAbort(state, "%s() expects 4-5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -070097 }
Doug Zongker3d177d02010-07-01 09:18:44 -070098 char* fs_type;
99 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700100 char* location;
101 char* mount_point;
Michael Runge168f7772014-10-22 17:05:08 -0700102 char* mount_options;
103 bool has_mount_options;
104 if (argc == 5) {
105 has_mount_options = true;
106 if (ReadArgs(state, argv, 5, &fs_type, &partition_type,
107 &location, &mount_point, &mount_options) < 0) {
108 return NULL;
109 }
110 } else {
111 has_mount_options = false;
112 if (ReadArgs(state, argv, 4, &fs_type, &partition_type,
Doug Zongker3d177d02010-07-01 09:18:44 -0700113 &location, &mount_point) < 0) {
Michael Runge168f7772014-10-22 17:05:08 -0700114 return NULL;
115 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700116 }
117
Doug Zongker3d177d02010-07-01 09:18:44 -0700118 if (strlen(fs_type) == 0) {
119 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
120 goto done;
121 }
122 if (strlen(partition_type) == 0) {
123 ErrorAbort(state, "partition_type argument to %s() can't be empty",
124 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700125 goto done;
126 }
127 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700128 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700129 goto done;
130 }
131 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700132 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700133 goto done;
134 }
135
Tao Bao818fa782015-06-23 23:23:33 -0700136 {
137 char *secontext = NULL;
Stephen Smalley779701d2012-02-09 14:13:23 -0500138
Tao Bao818fa782015-06-23 23:23:33 -0700139 if (sehandle) {
140 selabel_lookup(sehandle, &secontext, mount_point, 0755);
141 setfscreatecon(secontext);
142 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500143
Tao Bao818fa782015-06-23 23:23:33 -0700144 mkdir(mount_point, 0755);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700145
Tao Bao818fa782015-06-23 23:23:33 -0700146 if (secontext) {
147 freecon(secontext);
148 setfscreatecon(NULL);
149 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500150 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500151
Doug Zongker3d177d02010-07-01 09:18:44 -0700152 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700153 mtd_scan_partitions();
154 const MtdPartition* mtd;
155 mtd = mtd_find_partition_by_name(location);
156 if (mtd == NULL) {
Michael Runge5ddf4292014-10-24 14:14:41 -0700157 uiPrintf(state, "%s: no mtd partition named \"%s\"",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700158 name, location);
159 result = strdup("");
160 goto done;
161 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700162 if (mtd_mount_partition(mtd, mount_point, fs_type, 0 /* rw */) != 0) {
Michael Runge5ddf4292014-10-24 14:14:41 -0700163 uiPrintf(state, "mtd mount of %s failed: %s\n",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700164 location, strerror(errno));
165 result = strdup("");
166 goto done;
167 }
168 result = mount_point;
169 } else {
Doug Zongker3d177d02010-07-01 09:18:44 -0700170 if (mount(location, mount_point, fs_type,
Michael Runge168f7772014-10-22 17:05:08 -0700171 MS_NOATIME | MS_NODEV | MS_NODIRATIME,
172 has_mount_options ? mount_options : "") < 0) {
Michael Runge5ddf4292014-10-24 14:14:41 -0700173 uiPrintf(state, "%s: failed to mount %s at %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700174 name, location, mount_point, strerror(errno));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700175 result = strdup("");
176 } else {
177 result = mount_point;
178 }
179 }
180
181done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700182 free(fs_type);
183 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700184 free(location);
185 if (result != mount_point) free(mount_point);
Michael Runge168f7772014-10-22 17:05:08 -0700186 if (has_mount_options) free(mount_options);
Doug Zongker512536a2010-02-17 16:11:44 -0800187 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700188}
189
Doug Zongker8edb00c2009-06-11 17:21:44 -0700190
191// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800192Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700193 char* result = NULL;
194 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700195 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700196 }
197 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700198 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700199 return NULL;
200 }
201 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700202 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700203 goto done;
204 }
205
206 scan_mounted_volumes();
Tao Bao818fa782015-06-23 23:23:33 -0700207 {
208 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
209 if (vol == NULL) {
210 result = strdup("");
211 } else {
212 result = mount_point;
213 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700214 }
215
216done:
217 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800218 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700219}
220
221
Doug Zongker512536a2010-02-17 16:11:44 -0800222Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700223 char* result = NULL;
224 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700225 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700226 }
227 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700228 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700229 return NULL;
230 }
231 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700232 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700233 goto done;
234 }
235
236 scan_mounted_volumes();
Tao Bao818fa782015-06-23 23:23:33 -0700237 {
238 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
239 if (vol == NULL) {
240 uiPrintf(state, "unmount of %s failed; no such volume\n", mount_point);
241 result = strdup("");
242 } else {
243 int ret = unmount_mounted_volume(vol);
244 if (ret != 0) {
245 uiPrintf(state, "unmount of %s failed (%d): %s\n",
246 mount_point, ret, strerror(errno));
247 }
248 result = mount_point;
Michael Runge5ddf4292014-10-24 14:14:41 -0700249 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700250 }
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[]) {
Tao Bao818fa782015-06-23 23:23:33 -0700422 char** paths = reinterpret_cast<char**>(malloc(argc * sizeof(char*)));
423 for (int i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700424 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700425 if (paths[i] == NULL) {
426 int j;
427 for (j = 0; j < i; ++i) {
428 free(paths[j]);
429 }
430 free(paths);
431 return NULL;
432 }
433 }
434
435 bool recursive = (strcmp(name, "delete_recursive") == 0);
436
437 int success = 0;
Tao Bao818fa782015-06-23 23:23:33 -0700438 for (int i = 0; i < argc; ++i) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700439 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
440 ++success;
441 free(paths[i]);
442 }
443 free(paths);
444
445 char buffer[10];
446 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800447 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700448}
449
Doug Zongker8edb00c2009-06-11 17:21:44 -0700450
Doug Zongker512536a2010-02-17 16:11:44 -0800451Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700452 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700453 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700454 }
455 char* frac_str;
456 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700457 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700458 return NULL;
459 }
460
461 double frac = strtod(frac_str, NULL);
462 int sec = strtol(sec_str, NULL, 10);
463
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700464 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700465 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
466
Doug Zongker9931f7f2009-06-10 14:11:53 -0700467 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800468 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700469}
470
Doug Zongker512536a2010-02-17 16:11:44 -0800471Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700472 if (argc != 1) {
473 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
474 }
475 char* frac_str;
476 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
477 return NULL;
478 }
479
480 double frac = strtod(frac_str, NULL);
481
482 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
483 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
484
Doug Zongker512536a2010-02-17 16:11:44 -0800485 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700486}
487
Doug Zongker8edb00c2009-06-11 17:21:44 -0700488// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800489Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700490 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700491 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700492 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700493 }
494 char* zip_path;
495 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700496 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700497
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700498 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700499
500 // To create a consistent system image, never use the clock for timestamps.
501 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
502
503 bool success = mzExtractRecursive(za, zip_path, dest_path,
Narayan Kamath9c0f5d62015-02-23 14:09:31 +0000504 &timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500505 NULL, NULL, sehandle);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700506 free(zip_path);
507 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800508 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700509}
510
Doug Zongker8edb00c2009-06-11 17:21:44 -0700511
512// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800513// or
514// package_extract_file(package_path)
515// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800516// function (the char* returned is actually a FileContents*).
517Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700518 int argc, Expr* argv[]) {
Doug Zongker5f875bf2014-08-22 14:53:43 -0700519 if (argc < 1 || argc > 2) {
520 return ErrorAbort(state, "%s() expects 1 or 2 args, got %d",
Doug Zongker6aece332010-02-01 14:40:12 -0800521 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700522 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700523 bool success = false;
Doug Zongker43772d22014-06-09 14:13:19 -0700524
Doug Zongker5f875bf2014-08-22 14:53:43 -0700525 if (argc == 2) {
526 // The two-argument version extracts to a file.
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800527
528 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700529
Doug Zongker6aece332010-02-01 14:40:12 -0800530 char* zip_path;
531 char* dest_path;
Doug Zongker5f875bf2014-08-22 14:53:43 -0700532 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800533
Doug Zongker6aece332010-02-01 14:40:12 -0800534 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
535 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700536 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800537 goto done2;
538 }
539
Tao Bao818fa782015-06-23 23:23:33 -0700540 {
541 FILE* f = fopen(dest_path, "wb");
542 if (f == NULL) {
543 printf("%s: can't open %s for write: %s\n",
544 name, dest_path, strerror(errno));
545 goto done2;
546 }
547 success = mzExtractZipEntryToFile(za, entry, fileno(f));
548 fclose(f);
Doug Zongker6aece332010-02-01 14:40:12 -0800549 }
Doug Zongker6aece332010-02-01 14:40:12 -0800550
551 done2:
552 free(zip_path);
553 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800554 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800555 } else {
556 // The one-argument version returns the contents of the file
557 // as the result.
558
559 char* zip_path;
Tao Bao818fa782015-06-23 23:23:33 -0700560 Value* v = reinterpret_cast<Value*>(malloc(sizeof(Value)));
Doug Zongker512536a2010-02-17 16:11:44 -0800561 v->type = VAL_BLOB;
562 v->size = -1;
563 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800564
565 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
566
567 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
568 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
569 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700570 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800571 goto done1;
572 }
573
Doug Zongker512536a2010-02-17 16:11:44 -0800574 v->size = mzGetZipEntryUncompLen(entry);
Tao Bao818fa782015-06-23 23:23:33 -0700575 v->data = reinterpret_cast<char*>(malloc(v->size));
Doug Zongker512536a2010-02-17 16:11:44 -0800576 if (v->data == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700577 printf("%s: failed to allocate %ld bytes for %s\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800578 name, (long)v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800579 goto done1;
580 }
581
Doug Zongker512536a2010-02-17 16:11:44 -0800582 success = mzExtractZipEntryToBuffer(za, entry,
583 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800584
585 done1:
586 free(zip_path);
587 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800588 free(v->data);
589 v->data = NULL;
590 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800591 }
Doug Zongker512536a2010-02-17 16:11:44 -0800592 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700593 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700594}
595
Doug Zongkera23075f2012-08-06 16:19:09 -0700596// Create all parent directories of name, if necessary.
597static int make_parents(char* name) {
598 char* p;
599 for (p = name + (strlen(name)-1); p > name; --p) {
600 if (*p != '/') continue;
601 *p = '\0';
602 if (make_parents(name) < 0) return -1;
603 int result = mkdir(name, 0700);
Michael Rungea91ecc52014-07-21 17:40:02 -0700604 if (result == 0) printf("created [%s]\n", name);
Doug Zongkera23075f2012-08-06 16:19:09 -0700605 *p = '/';
606 if (result == 0 || errno == EEXIST) {
607 // successfully created or already existed; we're done
608 return 0;
609 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700610 printf("failed to mkdir %s: %s\n", name, strerror(errno));
Doug Zongkera23075f2012-08-06 16:19:09 -0700611 return -1;
612 }
613 }
614 return 0;
615}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700616
Doug Zongker9931f7f2009-06-10 14:11:53 -0700617// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700618// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800619Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700620 if (argc == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700621 return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700622 }
623 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700624 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700625 if (target == NULL) return NULL;
626
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700627 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700628 if (srcs == NULL) {
629 free(target);
630 return NULL;
631 }
632
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700633 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700634 int i;
635 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700636 if (unlink(srcs[i]) < 0) {
637 if (errno != ENOENT) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700638 printf("%s: failed to remove %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700639 name, srcs[i], strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700640 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700641 }
642 }
Doug Zongkera23075f2012-08-06 16:19:09 -0700643 if (make_parents(srcs[i])) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700644 printf("%s: failed to symlink %s to %s: making parents failed\n",
Doug Zongkera23075f2012-08-06 16:19:09 -0700645 name, srcs[i], target);
646 ++bad;
647 }
Doug Zongker60babf82009-09-18 15:11:24 -0700648 if (symlink(target, srcs[i]) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700649 printf("%s: failed to symlink %s to %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700650 name, srcs[i], target, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700651 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700652 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700653 free(srcs[i]);
654 }
655 free(srcs);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700656 if (bad) {
657 return ErrorAbort(state, "%s: some symlinks failed", name);
658 }
Doug Zongker512536a2010-02-17 16:11:44 -0800659 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700660}
661
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700662struct perm_parsed_args {
663 bool has_uid;
664 uid_t uid;
665 bool has_gid;
666 gid_t gid;
667 bool has_mode;
668 mode_t mode;
669 bool has_fmode;
670 mode_t fmode;
671 bool has_dmode;
672 mode_t dmode;
673 bool has_selabel;
674 char* selabel;
675 bool has_capabilities;
676 uint64_t capabilities;
677};
678
Michael Runged4a63422014-10-22 19:48:41 -0700679static struct perm_parsed_args ParsePermArgs(State * state, int argc, char** args) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700680 int i;
681 struct perm_parsed_args parsed;
682 int bad = 0;
683 static int max_warnings = 20;
684
685 memset(&parsed, 0, sizeof(parsed));
686
687 for (i = 1; i < argc; i += 2) {
688 if (strcmp("uid", args[i]) == 0) {
689 int64_t uid;
690 if (sscanf(args[i+1], "%" SCNd64, &uid) == 1) {
691 parsed.uid = uid;
692 parsed.has_uid = true;
693 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700694 uiPrintf(state, "ParsePermArgs: invalid UID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700695 bad++;
696 }
697 continue;
698 }
699 if (strcmp("gid", args[i]) == 0) {
700 int64_t gid;
701 if (sscanf(args[i+1], "%" SCNd64, &gid) == 1) {
702 parsed.gid = gid;
703 parsed.has_gid = true;
704 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700705 uiPrintf(state, "ParsePermArgs: invalid GID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700706 bad++;
707 }
708 continue;
709 }
710 if (strcmp("mode", args[i]) == 0) {
711 int32_t mode;
712 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
713 parsed.mode = mode;
714 parsed.has_mode = true;
715 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700716 uiPrintf(state, "ParsePermArgs: invalid mode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700717 bad++;
718 }
719 continue;
720 }
721 if (strcmp("dmode", args[i]) == 0) {
722 int32_t mode;
723 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
724 parsed.dmode = mode;
725 parsed.has_dmode = true;
726 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700727 uiPrintf(state, "ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700728 bad++;
729 }
730 continue;
731 }
732 if (strcmp("fmode", args[i]) == 0) {
733 int32_t mode;
734 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
735 parsed.fmode = mode;
736 parsed.has_fmode = true;
737 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700738 uiPrintf(state, "ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700739 bad++;
740 }
741 continue;
742 }
743 if (strcmp("capabilities", args[i]) == 0) {
744 int64_t capabilities;
745 if (sscanf(args[i+1], "%" SCNi64, &capabilities) == 1) {
746 parsed.capabilities = capabilities;
747 parsed.has_capabilities = true;
748 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700749 uiPrintf(state, "ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700750 bad++;
751 }
752 continue;
753 }
754 if (strcmp("selabel", args[i]) == 0) {
755 if (args[i+1][0] != '\0') {
756 parsed.selabel = args[i+1];
757 parsed.has_selabel = true;
758 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700759 uiPrintf(state, "ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700760 bad++;
761 }
762 continue;
763 }
764 if (max_warnings != 0) {
765 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i]);
766 max_warnings--;
767 if (max_warnings == 0) {
768 printf("ParsedPermArgs: suppressing further warnings\n");
769 }
770 }
771 }
772 return parsed;
773}
774
775static int ApplyParsedPerms(
Michael Runged4a63422014-10-22 19:48:41 -0700776 State * state,
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700777 const char* filename,
778 const struct stat *statptr,
779 struct perm_parsed_args parsed)
780{
781 int bad = 0;
782
Nick Kralevich68802412014-10-23 20:36:42 -0700783 if (parsed.has_selabel) {
784 if (lsetfilecon(filename, parsed.selabel) != 0) {
785 uiPrintf(state, "ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n",
786 filename, parsed.selabel, strerror(errno));
787 bad++;
788 }
789 }
790
Nick Kraleviche4612512013-09-10 15:34:19 -0700791 /* ignore symlinks */
792 if (S_ISLNK(statptr->st_mode)) {
Nick Kralevich68802412014-10-23 20:36:42 -0700793 return bad;
Nick Kraleviche4612512013-09-10 15:34:19 -0700794 }
795
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700796 if (parsed.has_uid) {
797 if (chown(filename, parsed.uid, -1) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700798 uiPrintf(state, "ApplyParsedPerms: chown of %s to %d failed: %s\n",
799 filename, parsed.uid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700800 bad++;
801 }
802 }
803
804 if (parsed.has_gid) {
805 if (chown(filename, -1, parsed.gid) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700806 uiPrintf(state, "ApplyParsedPerms: chgrp of %s to %d failed: %s\n",
807 filename, parsed.gid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700808 bad++;
809 }
810 }
811
812 if (parsed.has_mode) {
813 if (chmod(filename, parsed.mode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700814 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
815 filename, parsed.mode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700816 bad++;
817 }
818 }
819
820 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
821 if (chmod(filename, parsed.dmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700822 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
823 filename, parsed.dmode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700824 bad++;
825 }
826 }
827
828 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
829 if (chmod(filename, parsed.fmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700830 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700831 filename, parsed.fmode, strerror(errno));
832 bad++;
833 }
834 }
835
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700836 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
837 if (parsed.capabilities == 0) {
838 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
839 // Report failure unless it's ENODATA (attribute not set)
Michael Runged4a63422014-10-22 19:48:41 -0700840 uiPrintf(state, "ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700841 filename, parsed.capabilities, strerror(errno));
842 bad++;
843 }
844 } else {
845 struct vfs_cap_data cap_data;
846 memset(&cap_data, 0, sizeof(cap_data));
847 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
848 cap_data.data[0].permitted = (uint32_t) (parsed.capabilities & 0xffffffff);
849 cap_data.data[0].inheritable = 0;
850 cap_data.data[1].permitted = (uint32_t) (parsed.capabilities >> 32);
851 cap_data.data[1].inheritable = 0;
852 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700853 uiPrintf(state, "ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n",
854 filename, parsed.capabilities, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700855 bad++;
856 }
857 }
858 }
859
860 return bad;
861}
862
863// nftw doesn't allow us to pass along context, so we need to use
864// global variables. *sigh*
865static struct perm_parsed_args recursive_parsed_args;
Michael Runged4a63422014-10-22 19:48:41 -0700866static State* recursive_state;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700867
868static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr,
869 int fileflags, struct FTW *pfwt) {
Michael Runged4a63422014-10-22 19:48:41 -0700870 return ApplyParsedPerms(recursive_state, filename, statptr, recursive_parsed_args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700871}
872
873static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700874 int bad = 0;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700875 struct stat sb;
876 Value* result = NULL;
877
878 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
879
880 if ((argc % 2) != 1) {
Tao Bao818fa782015-06-23 23:23:33 -0700881 return ErrorAbort(state, "%s() expects an odd number of arguments, got %d", name, argc);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700882 }
883
884 char** args = ReadVarArgs(state, argc, argv);
885 if (args == NULL) return NULL;
886
887 if (lstat(args[0], &sb) == -1) {
888 result = ErrorAbort(state, "%s: Error on lstat of \"%s\": %s", name, args[0], strerror(errno));
889 goto done;
890 }
891
Tao Bao818fa782015-06-23 23:23:33 -0700892 {
893 struct perm_parsed_args parsed = ParsePermArgs(state, argc, args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700894
Tao Bao818fa782015-06-23 23:23:33 -0700895 if (recursive) {
896 recursive_parsed_args = parsed;
897 recursive_state = state;
898 bad += nftw(args[0], do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
899 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
900 recursive_state = NULL;
901 } else {
902 bad += ApplyParsedPerms(state, args[0], &sb, parsed);
903 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700904 }
905
906done:
Tao Bao818fa782015-06-23 23:23:33 -0700907 for (int i = 0; i < argc; ++i) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700908 free(args[i]);
909 }
910 free(args);
911
912 if (result != NULL) {
913 return result;
914 }
915
916 if (bad > 0) {
917 return ErrorAbort(state, "%s: some changes failed", name);
918 }
919
920 return StringValue(strdup(""));
921}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700922
Doug Zongker512536a2010-02-17 16:11:44 -0800923Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700924 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700925 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700926 }
Tao Bao818fa782015-06-23 23:23:33 -0700927 char* key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700928 if (key == NULL) return NULL;
929
930 char value[PROPERTY_VALUE_MAX];
931 property_get(key, value, "");
932 free(key);
933
Doug Zongker512536a2010-02-17 16:11:44 -0800934 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700935}
936
937
Doug Zongker47cace92009-06-18 10:11:50 -0700938// file_getprop(file, key)
939//
940// interprets 'file' as a getprop-style file (key=value pairs, one
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700941// per line. # comment lines,blank lines, lines without '=' ignored),
942// and returns the value for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800943Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700944 char* result = NULL;
945 char* buffer = NULL;
946 char* filename;
947 char* key;
948 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
949 return NULL;
950 }
951
952 struct stat st;
953 if (stat(filename, &st) < 0) {
Tao Bao818fa782015-06-23 23:23:33 -0700954 ErrorAbort(state, "%s: failed to stat \"%s\": %s", name, filename, strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -0700955 goto done;
956 }
957
958#define MAX_FILE_GETPROP_SIZE 65536
959
960 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
Tao Bao818fa782015-06-23 23:23:33 -0700961 ErrorAbort(state, "%s too large for %s (max %d)", filename, name, MAX_FILE_GETPROP_SIZE);
Doug Zongker47cace92009-06-18 10:11:50 -0700962 goto done;
963 }
964
Tao Bao818fa782015-06-23 23:23:33 -0700965 buffer = reinterpret_cast<char*>(malloc(st.st_size+1));
Doug Zongker47cace92009-06-18 10:11:50 -0700966 if (buffer == NULL) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700967 ErrorAbort(state, "%s: failed to alloc %lld bytes", name, (long long)st.st_size+1);
Doug Zongker47cace92009-06-18 10:11:50 -0700968 goto done;
969 }
970
Tao Bao818fa782015-06-23 23:23:33 -0700971 FILE* f;
972 f = fopen(filename, "rb");
Doug Zongker47cace92009-06-18 10:11:50 -0700973 if (f == NULL) {
Tao Bao818fa782015-06-23 23:23:33 -0700974 ErrorAbort(state, "%s: failed to open %s: %s", name, filename, strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -0700975 goto done;
976 }
977
978 if (fread(buffer, 1, st.st_size, f) != st.st_size) {
Doug Zongkera23075f2012-08-06 16:19:09 -0700979 ErrorAbort(state, "%s: failed to read %lld bytes from %s",
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700980 name, (long long)st.st_size+1, filename);
Doug Zongker47cace92009-06-18 10:11:50 -0700981 fclose(f);
982 goto done;
983 }
984 buffer[st.st_size] = '\0';
985
986 fclose(f);
987
Tao Bao818fa782015-06-23 23:23:33 -0700988 char* line;
989 line = strtok(buffer, "\n");
Doug Zongker47cace92009-06-18 10:11:50 -0700990 do {
991 // skip whitespace at start of line
992 while (*line && isspace(*line)) ++line;
993
994 // comment or blank line: skip to next line
995 if (*line == '\0' || *line == '#') continue;
996
997 char* equal = strchr(line, '=');
998 if (equal == NULL) {
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700999 continue;
Doug Zongker47cace92009-06-18 10:11:50 -07001000 }
1001
1002 // trim whitespace between key and '='
1003 char* key_end = equal-1;
1004 while (key_end > line && isspace(*key_end)) --key_end;
1005 key_end[1] = '\0';
1006
1007 // not the key we're looking for
1008 if (strcmp(key, line) != 0) continue;
1009
1010 // skip whitespace after the '=' to the start of the value
1011 char* val_start = equal+1;
1012 while(*val_start && isspace(*val_start)) ++val_start;
1013
1014 // trim trailing whitespace
1015 char* val_end = val_start + strlen(val_start)-1;
1016 while (val_end > val_start && isspace(*val_end)) --val_end;
1017 val_end[1] = '\0';
1018
1019 result = strdup(val_start);
1020 break;
1021
1022 } while ((line = strtok(NULL, "\n")));
1023
1024 if (result == NULL) result = strdup("");
1025
1026 done:
1027 free(filename);
1028 free(key);
1029 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -08001030 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -07001031}
1032
Doug Zongker179b2d92011-04-12 15:49:04 -07001033// write_raw_image(filename_or_blob, partition)
Doug Zongker512536a2010-02-17 16:11:44 -08001034Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001035 char* result = NULL;
1036
Doug Zongker179b2d92011-04-12 15:49:04 -07001037 Value* partition_value;
1038 Value* contents;
1039 if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001040 return NULL;
1041 }
1042
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001043 char* partition = NULL;
Doug Zongker179b2d92011-04-12 15:49:04 -07001044 if (partition_value->type != VAL_STRING) {
1045 ErrorAbort(state, "partition argument to %s must be string", name);
1046 goto done;
1047 }
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001048 partition = partition_value->data;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001049 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001050 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001051 goto done;
1052 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001053 if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001054 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001055 goto done;
1056 }
1057
1058 mtd_scan_partitions();
Tao Bao818fa782015-06-23 23:23:33 -07001059 const MtdPartition* mtd;
1060 mtd = mtd_find_partition_by_name(partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001061 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001062 printf("%s: no mtd partition named \"%s\"\n", name, partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001063 result = strdup("");
1064 goto done;
1065 }
1066
Tao Bao818fa782015-06-23 23:23:33 -07001067 MtdWriteContext* ctx;
1068 ctx = mtd_write_partition(mtd);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001069 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001070 printf("%s: can't write mtd partition \"%s\"\n",
Doug Zongker8edb00c2009-06-11 17:21:44 -07001071 name, partition);
1072 result = strdup("");
1073 goto done;
1074 }
1075
1076 bool success;
1077
Doug Zongker179b2d92011-04-12 15:49:04 -07001078 if (contents->type == VAL_STRING) {
1079 // we're given a filename as the contents
1080 char* filename = contents->data;
1081 FILE* f = fopen(filename, "rb");
1082 if (f == NULL) {
Tao Bao818fa782015-06-23 23:23:33 -07001083 printf("%s: can't open %s: %s\n", name, filename, strerror(errno));
Doug Zongker179b2d92011-04-12 15:49:04 -07001084 result = strdup("");
1085 goto done;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001086 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001087
1088 success = true;
Tao Bao818fa782015-06-23 23:23:33 -07001089 char* buffer = reinterpret_cast<char*>(malloc(BUFSIZ));
Doug Zongker179b2d92011-04-12 15:49:04 -07001090 int read;
1091 while (success && (read = fread(buffer, 1, BUFSIZ, f)) > 0) {
1092 int wrote = mtd_write_data(ctx, buffer, read);
1093 success = success && (wrote == read);
1094 }
1095 free(buffer);
1096 fclose(f);
1097 } else {
1098 // we're given a blob as the contents
1099 ssize_t wrote = mtd_write_data(ctx, contents->data, contents->size);
1100 success = (wrote == contents->size);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001101 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001102 if (!success) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001103 printf("mtd_write_data to %s failed: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -07001104 partition, strerror(errno));
1105 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001106
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001107 if (mtd_erase_blocks(ctx, -1) == -1) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001108 printf("%s: error erasing blocks of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001109 }
1110 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001111 printf("%s: error closing write of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001112 }
1113
Doug Zongker179b2d92011-04-12 15:49:04 -07001114 printf("%s %s partition\n",
1115 success ? "wrote" : "failed to write", partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001116
1117 result = success ? partition : strdup("");
1118
1119done:
Doug Zongker179b2d92011-04-12 15:49:04 -07001120 if (result != partition) FreeValue(partition_value);
1121 FreeValue(contents);
Doug Zongker512536a2010-02-17 16:11:44 -08001122 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001123}
1124
Doug Zongker8edb00c2009-06-11 17:21:44 -07001125// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001126Value* ApplyPatchSpaceFn(const char* name, State* state,
1127 int argc, Expr* argv[]) {
1128 char* bytes_str;
1129 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
1130 return NULL;
1131 }
1132
1133 char* endptr;
1134 size_t bytes = strtol(bytes_str, &endptr, 10);
1135 if (bytes == 0 && endptr == bytes_str) {
Tao Bao818fa782015-06-23 23:23:33 -07001136 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n", name, bytes_str);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001137 free(bytes_str);
1138 return NULL;
1139 }
1140
1141 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
1142}
1143
Doug Zongker52b40362014-02-10 15:30:30 -08001144// apply_patch(file, size, init_sha1, tgt_sha1, patch)
1145
Doug Zongker512536a2010-02-17 16:11:44 -08001146Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001147 if (argc < 6 || (argc % 2) == 1) {
1148 return ErrorAbort(state, "%s(): expected at least 6 args and an "
1149 "even number, got %d",
1150 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001151 }
1152
Doug Zongkerc4351c72010-02-22 14:46:32 -08001153 char* source_filename;
1154 char* target_filename;
1155 char* target_sha1;
1156 char* target_size_str;
1157 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
1158 &target_sha1, &target_size_str) < 0) {
1159 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001160 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001161
Doug Zongkerc4351c72010-02-22 14:46:32 -08001162 char* endptr;
1163 size_t target_size = strtol(target_size_str, &endptr, 10);
1164 if (target_size == 0 && endptr == target_size_str) {
1165 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count",
1166 name, target_size_str);
1167 free(source_filename);
1168 free(target_filename);
1169 free(target_sha1);
1170 free(target_size_str);
1171 return NULL;
1172 }
1173
1174 int patchcount = (argc-4) / 2;
1175 Value** patches = ReadValueVarArgs(state, argc-4, argv+4);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001176
1177 int i;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001178 for (i = 0; i < patchcount; ++i) {
1179 if (patches[i*2]->type != VAL_STRING) {
1180 ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i);
1181 break;
1182 }
1183 if (patches[i*2+1]->type != VAL_BLOB) {
1184 ErrorAbort(state, "%s(): patch #%d is not blob", name, i);
1185 break;
1186 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001187 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001188 if (i != patchcount) {
1189 for (i = 0; i < patchcount*2; ++i) {
1190 FreeValue(patches[i]);
1191 }
1192 free(patches);
1193 return NULL;
1194 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001195
Tao Bao818fa782015-06-23 23:23:33 -07001196 char** patch_sha_str = reinterpret_cast<char**>(malloc(patchcount * sizeof(char*)));
Doug Zongkerc4351c72010-02-22 14:46:32 -08001197 for (i = 0; i < patchcount; ++i) {
1198 patch_sha_str[i] = patches[i*2]->data;
1199 patches[i*2]->data = NULL;
1200 FreeValue(patches[i*2]);
1201 patches[i] = patches[i*2+1];
Doug Zongker8edb00c2009-06-11 17:21:44 -07001202 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001203
1204 int result = applypatch(source_filename, target_filename,
1205 target_sha1, target_size,
Doug Zongkera3ccba62012-08-20 15:28:02 -07001206 patchcount, patch_sha_str, patches, NULL);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001207
1208 for (i = 0; i < patchcount; ++i) {
1209 FreeValue(patches[i]);
1210 }
1211 free(patch_sha_str);
1212 free(patches);
1213
1214 return StringValue(strdup(result == 0 ? "t" : ""));
1215}
1216
1217// apply_patch_check(file, [sha1_1, ...])
1218Value* ApplyPatchCheckFn(const char* name, State* state,
1219 int argc, Expr* argv[]) {
1220 if (argc < 1) {
1221 return ErrorAbort(state, "%s(): expected at least 1 arg, got %d",
1222 name, argc);
1223 }
1224
1225 char* filename;
1226 if (ReadArgs(state, argv, 1, &filename) < 0) {
1227 return NULL;
1228 }
1229
1230 int patchcount = argc-1;
1231 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
1232
1233 int result = applypatch_check(filename, patchcount, sha1s);
1234
1235 int i;
1236 for (i = 0; i < patchcount; ++i) {
1237 free(sha1s[i]);
1238 }
1239 free(sha1s);
1240
1241 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001242}
1243
Doug Zongker512536a2010-02-17 16:11:44 -08001244Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001245 char** args = ReadVarArgs(state, argc, argv);
1246 if (args == NULL) {
1247 return NULL;
1248 }
1249
1250 int size = 0;
1251 int i;
1252 for (i = 0; i < argc; ++i) {
1253 size += strlen(args[i]);
1254 }
Tao Bao818fa782015-06-23 23:23:33 -07001255 char* buffer = reinterpret_cast<char*>(malloc(size+1));
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001256 size = 0;
1257 for (i = 0; i < argc; ++i) {
1258 strcpy(buffer+size, args[i]);
1259 size += strlen(args[i]);
1260 free(args[i]);
1261 }
1262 free(args);
1263 buffer[size] = '\0';
Michael Runged4a63422014-10-22 19:48:41 -07001264 uiPrint(state, buffer);
Doug Zongker512536a2010-02-17 16:11:44 -08001265 return StringValue(buffer);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001266}
1267
Doug Zongkerd0181b82011-10-19 10:51:12 -07001268Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
1269 if (argc != 0) {
1270 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1271 }
1272 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
1273 return StringValue(strdup("t"));
1274}
1275
Doug Zongker512536a2010-02-17 16:11:44 -08001276Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001277 if (argc < 1) {
1278 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1279 }
1280 char** args = ReadVarArgs(state, argc, argv);
1281 if (args == NULL) {
1282 return NULL;
1283 }
1284
Tao Bao818fa782015-06-23 23:23:33 -07001285 char** args2 = reinterpret_cast<char**>(malloc(sizeof(char*) * (argc+1)));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001286 memcpy(args2, args, sizeof(char*) * argc);
1287 args2[argc] = NULL;
1288
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001289 printf("about to run program [%s] with %d args\n", args2[0], argc);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001290
1291 pid_t child = fork();
1292 if (child == 0) {
1293 execv(args2[0], args2);
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001294 printf("run_program: execv failed: %s\n", strerror(errno));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001295 _exit(1);
1296 }
1297 int status;
1298 waitpid(child, &status, 0);
1299 if (WIFEXITED(status)) {
1300 if (WEXITSTATUS(status) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001301 printf("run_program: child exited with status %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001302 WEXITSTATUS(status));
1303 }
1304 } else if (WIFSIGNALED(status)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001305 printf("run_program: child terminated by signal %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001306 WTERMSIG(status));
1307 }
1308
1309 int i;
1310 for (i = 0; i < argc; ++i) {
1311 free(args[i]);
1312 }
1313 free(args);
1314 free(args2);
1315
1316 char buffer[20];
1317 sprintf(buffer, "%d", status);
1318
Doug Zongker512536a2010-02-17 16:11:44 -08001319 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001320}
1321
Doug Zongker512536a2010-02-17 16:11:44 -08001322// sha1_check(data)
1323// to return the sha1 of the data (given in the format returned by
1324// read_file).
1325//
1326// sha1_check(data, sha1_hex, [sha1_hex, ...])
1327// returns the sha1 of the file if it matches any of the hex
1328// strings passed, or "" if it does not equal any of them.
1329//
1330Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1331 if (argc < 1) {
1332 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1333 }
1334
1335 Value** args = ReadValueVarArgs(state, argc, argv);
1336 if (args == NULL) {
1337 return NULL;
1338 }
1339
1340 if (args[0]->size < 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001341 return StringValue(strdup(""));
1342 }
1343 uint8_t digest[SHA_DIGEST_SIZE];
Doug Zongkerbac7fba2013-04-10 11:32:17 -07001344 SHA_hash(args[0]->data, args[0]->size, digest);
Doug Zongker512536a2010-02-17 16:11:44 -08001345 FreeValue(args[0]);
1346
1347 if (argc == 1) {
1348 return StringValue(PrintSha1(digest));
1349 }
1350
1351 int i;
Tao Bao818fa782015-06-23 23:23:33 -07001352 uint8_t* arg_digest = reinterpret_cast<uint8_t*>(malloc(SHA_DIGEST_SIZE));
Doug Zongker512536a2010-02-17 16:11:44 -08001353 for (i = 1; i < argc; ++i) {
1354 if (args[i]->type != VAL_STRING) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001355 printf("%s(): arg %d is not a string; skipping",
Doug Zongker512536a2010-02-17 16:11:44 -08001356 name, i);
1357 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1358 // Warn about bad args and skip them.
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001359 printf("%s(): error parsing \"%s\" as sha-1; skipping",
1360 name, args[i]->data);
Doug Zongker512536a2010-02-17 16:11:44 -08001361 } else if (memcmp(digest, arg_digest, SHA_DIGEST_SIZE) == 0) {
1362 break;
1363 }
1364 FreeValue(args[i]);
1365 }
1366 if (i >= argc) {
1367 // Didn't match any of the hex strings; return false.
1368 return StringValue(strdup(""));
1369 }
1370 // Found a match; free all the remaining arguments and return the
1371 // matched one.
1372 int j;
1373 for (j = i+1; j < argc; ++j) {
1374 FreeValue(args[j]);
1375 }
1376 return args[i];
1377}
1378
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001379// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001380// is actually a FileContents*).
1381Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1382 if (argc != 1) {
1383 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1384 }
1385 char* filename;
1386 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1387
Tao Bao818fa782015-06-23 23:23:33 -07001388 Value* v = reinterpret_cast<Value*>(malloc(sizeof(Value)));
Doug Zongker512536a2010-02-17 16:11:44 -08001389 v->type = VAL_BLOB;
1390
1391 FileContents fc;
Doug Zongkera1bc1482014-02-13 15:18:19 -08001392 if (LoadFileContents(filename, &fc) != 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001393 free(filename);
Michael Runge6eed2242013-12-13 17:13:11 -08001394 v->size = -1;
1395 v->data = NULL;
Doug Zongker512536a2010-02-17 16:11:44 -08001396 free(fc.data);
Michael Runge6eed2242013-12-13 17:13:11 -08001397 return v;
Doug Zongker512536a2010-02-17 16:11:44 -08001398 }
1399
1400 v->size = fc.size;
1401 v->data = (char*)fc.data;
1402
1403 free(filename);
1404 return v;
1405}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001406
Doug Zongkerc87bab12013-11-25 13:53:25 -08001407// Immediately reboot the device. Recovery is not finished normally,
1408// so if you reboot into recovery it will re-start applying the
1409// current package (because nothing has cleared the copy of the
1410// arguments stored in the BCB).
1411//
1412// The argument is the partition name passed to the android reboot
1413// property. It can be "recovery" to boot from the recovery
1414// partition, or "" (empty string) to boot from the regular boot
1415// partition.
1416Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) {
1417 if (argc != 2) {
1418 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1419 }
1420
1421 char* filename;
1422 char* property;
1423 if (ReadArgs(state, argv, 2, &filename, &property) < 0) return NULL;
1424
1425 char buffer[80];
1426
1427 // zero out the 'command' field of the bootloader message.
1428 memset(buffer, 0, sizeof(((struct bootloader_message*)0)->command));
1429 FILE* f = fopen(filename, "r+b");
1430 fseek(f, offsetof(struct bootloader_message, command), SEEK_SET);
1431 fwrite(buffer, sizeof(((struct bootloader_message*)0)->command), 1, f);
1432 fclose(f);
1433 free(filename);
1434
1435 strcpy(buffer, "reboot,");
1436 if (property != NULL) {
1437 strncat(buffer, property, sizeof(buffer)-10);
1438 }
1439
1440 property_set(ANDROID_RB_PROPERTY, buffer);
1441
1442 sleep(5);
1443 free(property);
1444 ErrorAbort(state, "%s() failed to reboot", name);
1445 return NULL;
1446}
1447
1448// Store a string value somewhere that future invocations of recovery
1449// can access it. This value is called the "stage" and can be used to
1450// drive packages that need to do reboots in the middle of
1451// installation and keep track of where they are in the multi-stage
1452// install.
1453//
1454// The first argument is the block device for the misc partition
1455// ("/misc" in the fstab), which is where this value is stored. The
1456// second argument is the string to store; it should not exceed 31
1457// bytes.
1458Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
1459 if (argc != 2) {
1460 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1461 }
1462
1463 char* filename;
1464 char* stagestr;
1465 if (ReadArgs(state, argv, 2, &filename, &stagestr) < 0) return NULL;
1466
1467 // Store this value in the misc partition, immediately after the
1468 // bootloader message that the main recovery uses to save its
1469 // arguments in case of the device restarting midway through
1470 // package installation.
1471 FILE* f = fopen(filename, "r+b");
1472 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1473 int to_write = strlen(stagestr)+1;
1474 int max_size = sizeof(((struct bootloader_message*)0)->stage);
1475 if (to_write > max_size) {
1476 to_write = max_size;
1477 stagestr[max_size-1] = 0;
1478 }
1479 fwrite(stagestr, to_write, 1, f);
1480 fclose(f);
1481
1482 free(stagestr);
1483 return StringValue(filename);
1484}
1485
1486// Return the value most recently saved with SetStageFn. The argument
1487// is the block device for the misc partition.
1488Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001489 if (argc != 1) {
Doug Zongkerc87bab12013-11-25 13:53:25 -08001490 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1491 }
1492
1493 char* filename;
1494 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1495
1496 char buffer[sizeof(((struct bootloader_message*)0)->stage)];
1497 FILE* f = fopen(filename, "rb");
1498 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1499 fread(buffer, sizeof(buffer), 1, f);
1500 fclose(f);
1501 buffer[sizeof(buffer)-1] = '\0';
1502
1503 return StringValue(strdup(buffer));
1504}
1505
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001506Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) {
1507 if (argc != 2) {
1508 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1509 }
1510
1511 char* filename;
1512 char* len_str;
1513 if (ReadArgs(state, argv, 2, &filename, &len_str) < 0) return NULL;
1514
1515 size_t len = strtoull(len_str, NULL, 0);
1516 int fd = open(filename, O_WRONLY, 0644);
1517 int success = wipe_block_device(fd, len);
1518
1519 free(filename);
1520 free(len_str);
1521
1522 close(fd);
1523
1524 return StringValue(strdup(success ? "t" : ""));
1525}
1526
Doug Zongkerc704e062014-05-23 08:40:35 -07001527Value* EnableRebootFn(const char* name, State* state, int argc, Expr* argv[]) {
1528 if (argc != 0) {
1529 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1530 }
1531 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
1532 fprintf(ui->cmd_pipe, "enable_reboot\n");
1533 return StringValue(strdup("t"));
1534}
1535
Michael Rungeacf47db2014-11-21 00:12:28 -08001536Value* Tune2FsFn(const char* name, State* state, int argc, Expr* argv[]) {
1537 if (argc == 0) {
1538 return ErrorAbort(state, "%s() expects args, got %d", name, argc);
1539 }
1540
1541 char** args = ReadVarArgs(state, argc, argv);
1542 if (args == NULL) {
1543 return ErrorAbort(state, "%s() could not read args", name);
1544 }
1545
Tao Bao818fa782015-06-23 23:23:33 -07001546 char** args2 = reinterpret_cast<char**>(malloc(sizeof(char*) * (argc+1)));
Michael Rungeacf47db2014-11-21 00:12:28 -08001547 // Tune2fs expects the program name as its args[0]
1548 args2[0] = strdup(name);
Tao Bao818fa782015-06-23 23:23:33 -07001549 for (int i = 0; i < argc; ++i) {
Michael Rungeacf47db2014-11-21 00:12:28 -08001550 args2[i + 1] = args[i];
1551 }
1552 int result = tune2fs_main(argc + 1, args2);
Tao Bao818fa782015-06-23 23:23:33 -07001553 for (int i = 0; i < argc; ++i) {
Michael Rungeacf47db2014-11-21 00:12:28 -08001554 free(args[i]);
1555 }
1556 free(args);
1557
1558 free(args2[0]);
1559 free(args2);
1560 if (result != 0) {
1561 return ErrorAbort(state, "%s() returned error code %d", name, result);
1562 }
1563 return StringValue(strdup("t"));
1564}
1565
Doug Zongker9931f7f2009-06-10 14:11:53 -07001566void RegisterInstallFunctions() {
1567 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001568 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001569 RegisterFunction("unmount", UnmountFn);
1570 RegisterFunction("format", FormatFn);
1571 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001572 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001573 RegisterFunction("delete", DeleteFn);
1574 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001575 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1576 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001577 RegisterFunction("symlink", SymlinkFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001578
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001579 // Usage:
1580 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1581 // Example:
1582 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1583 RegisterFunction("set_metadata", SetMetadataFn);
1584
1585 // Usage:
1586 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1587 // Example:
1588 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1589 RegisterFunction("set_metadata_recursive", SetMetadataFn);
1590
Doug Zongker8edb00c2009-06-11 17:21:44 -07001591 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001592 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001593 RegisterFunction("write_raw_image", WriteRawImageFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001594
1595 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001596 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1597 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001598
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001599 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001600
Doug Zongker512536a2010-02-17 16:11:44 -08001601 RegisterFunction("read_file", ReadFileFn);
1602 RegisterFunction("sha1_check", Sha1CheckFn);
Michael Rungece7ca712013-11-06 17:42:20 -08001603 RegisterFunction("rename", RenameFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001604
Doug Zongkerd0181b82011-10-19 10:51:12 -07001605 RegisterFunction("wipe_cache", WipeCacheFn);
1606
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001607 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001608
1609 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001610
1611 RegisterFunction("reboot_now", RebootNowFn);
1612 RegisterFunction("get_stage", GetStageFn);
1613 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -07001614
1615 RegisterFunction("enable_reboot", EnableRebootFn);
Michael Rungeacf47db2014-11-21 00:12:28 -08001616 RegisterFunction("tune2fs", Tune2FsFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001617}