summaryrefslogtreecommitdiffstats
path: root/updater/install.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'updater/install.cpp')
-rw-r--r--updater/install.cpp544
1 files changed, 177 insertions, 367 deletions
diff --git a/updater/install.cpp b/updater/install.cpp
index 005f9f97d..a41c5db3c 100644
--- a/updater/install.cpp
+++ b/updater/install.cpp
@@ -14,53 +14,54 @@
* limitations under the License.
*/
+#include "updater/install.h"
+
#include <ctype.h>
#include <errno.h>
+#include <fcntl.h>
+#include <ftw.h>
+#include <inttypes.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <sys/capability.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <time.h>
-#include <selinux/selinux.h>
-#include <ftw.h>
-#include <sys/capability.h>
#include <sys/xattr.h>
-#include <linux/xattr.h>
-#include <inttypes.h>
+#include <time.h>
+#include <unistd.h>
+#include <utime.h>
#include <memory>
+#include <string>
#include <vector>
#include <android-base/parseint.h>
+#include <android-base/properties.h>
#include <android-base/strings.h>
#include <android-base/stringprintf.h>
+#include <cutils/android_reboot.h>
+#include <ext4_utils/make_ext4fs.h>
+#include <ext4_utils/wipe.h>
+#include <openssl/sha.h>
+#include <selinux/label.h>
+#include <selinux/selinux.h>
+#include <ziparchive/zip_archive.h>
-#include "bootloader.h"
#include "applypatch/applypatch.h"
-#include "cutils/android_reboot.h"
-#include "cutils/misc.h"
-#include "cutils/properties.h"
+#include "bootloader.h"
#include "edify/expr.h"
#include "error_code.h"
-#include "minzip/DirUtil.h"
-#include "mtdutils/mounts.h"
-#include "mtdutils/mtdutils.h"
-#include "openssl/sha.h"
+#include "mounts.h"
#include "ota_io.h"
-#include "updater.h"
-#include "install.h"
+#include "otautil/DirUtil.h"
+#include "otautil/ZipUtil.h"
+#include "print_sha1.h"
#include "tune2fs.h"
-
-#ifdef USE_EXT4
-#include "make_ext4fs.h"
-#include "wipe.h"
-#endif
+#include "updater/updater.h"
// Send over the buffer to recovery though the command pipe.
static void uiPrint(State* state, const std::string& buffer) {
@@ -82,8 +83,7 @@ static void uiPrint(State* state, const std::string& buffer) {
fprintf(stderr, "%s", buffer.c_str());
}
-__attribute__((__format__(printf, 2, 3))) __nonnull((2))
-void uiPrintf(State* state, const char* format, ...) {
+void uiPrintf(State* _Nonnull state, const char* _Nonnull format, ...) {
std::string error_msg;
va_list ap;
@@ -94,25 +94,32 @@ void uiPrintf(State* state, const char* format, ...) {
uiPrint(state, error_msg);
}
-// Take a sha-1 digest and return it as a newly-allocated hex string.
-char* PrintSha1(const uint8_t* digest) {
- char* buffer = reinterpret_cast<char*>(malloc(SHA_DIGEST_LENGTH*2 + 1));
- const char* alphabet = "0123456789abcdef";
- size_t i;
- for (i = 0; i < SHA_DIGEST_LENGTH; ++i) {
- buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf];
- buffer[i*2+1] = alphabet[digest[i] & 0xf];
- }
- buffer[i*2] = '\0';
- return buffer;
+// Create all parent directories of name, if necessary.
+static int make_parents(char* name) {
+ char* p;
+ for (p = name + (strlen(name)-1); p > name; --p) {
+ if (*p != '/') continue;
+ *p = '\0';
+ if (make_parents(name) < 0) return -1;
+ int result = mkdir(name, 0700);
+ if (result == 0) printf("created [%s]\n", name);
+ *p = '/';
+ if (result == 0 || errno == EEXIST) {
+ // successfully created or already existed; we're done
+ return 0;
+ } else {
+ printf("failed to mkdir %s: %s\n", name, strerror(errno));
+ return -1;
+ }
+ }
+ return 0;
}
// mount(fs_type, partition_type, location, mount_point)
//
-// fs_type="yaffs2" partition_type="MTD" location=partition
// fs_type="ext4" partition_type="EMMC" location=device
Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
- char* result = NULL;
+ char* result = nullptr;
if (argc != 4 && argc != 5) {
return ErrorAbort(state, kArgsParsingFailure, "%s() expects 4-5 args, got %d", name, argc);
}
@@ -171,33 +178,14 @@ Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
}
}
- if (strcmp(partition_type, "MTD") == 0) {
- mtd_scan_partitions();
- const MtdPartition* mtd;
- mtd = mtd_find_partition_by_name(location);
- if (mtd == NULL) {
- uiPrintf(state, "%s: no mtd partition named \"%s\"\n",
- name, location);
- result = strdup("");
- goto done;
- }
- if (mtd_mount_partition(mtd, mount_point, fs_type, 0 /* rw */) != 0) {
- uiPrintf(state, "mtd mount of %s failed: %s\n",
- location, strerror(errno));
- result = strdup("");
- goto done;
- }
- result = mount_point;
+ if (mount(location, mount_point, fs_type,
+ MS_NOATIME | MS_NODEV | MS_NODIRATIME,
+ has_mount_options ? mount_options : "") < 0) {
+ uiPrintf(state, "%s: failed to mount %s at %s: %s\n",
+ name, location, mount_point, strerror(errno));
+ result = strdup("");
} else {
- if (mount(location, mount_point, fs_type,
- MS_NOATIME | MS_NODEV | MS_NODIRATIME,
- has_mount_options ? mount_options : "") < 0) {
- uiPrintf(state, "%s: failed to mount %s at %s: %s\n",
- name, location, mount_point, strerror(errno));
- result = strdup("");
- } else {
- result = mount_point;
- }
+ result = mount_point;
}
done:
@@ -212,7 +200,7 @@ done:
// is_mounted(mount_point)
Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
- char* result = NULL;
+ char* result = nullptr;
if (argc != 1) {
return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
}
@@ -227,7 +215,7 @@ Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
scan_mounted_volumes();
{
- const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
+ MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
if (vol == NULL) {
result = strdup("");
} else {
@@ -242,7 +230,7 @@ done:
Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
- char* result = NULL;
+ char* result = nullptr;
if (argc != 1) {
return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
}
@@ -257,7 +245,7 @@ Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
scan_mounted_volumes();
{
- const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
+ MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
if (vol == NULL) {
uiPrintf(state, "unmount of %s failed; no such volume\n", mount_point);
result = strdup("");
@@ -293,14 +281,13 @@ static int exec_cmd(const char* path, char* const argv[]) {
// format(fs_type, partition_type, location, fs_size, mount_point)
//
-// fs_type="yaffs2" partition_type="MTD" location=partition fs_size=<bytes> mount_point=<location>
// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
// if fs_size == 0, then make fs uses the entire partition.
// if fs_size > 0, that is the size to use
// if fs_size < 0, then reserve that many bytes at the end of the partition (not for "f2fs")
Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
- char* result = NULL;
+ char* result = nullptr;
if (argc != 5) {
return ErrorAbort(state, kArgsParsingFailure, "%s() expects 5 args, got %d", name, argc);
}
@@ -334,35 +321,7 @@ Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
goto done;
}
- if (strcmp(partition_type, "MTD") == 0) {
- mtd_scan_partitions();
- const MtdPartition* mtd = mtd_find_partition_by_name(location);
- if (mtd == NULL) {
- printf("%s: no mtd partition named \"%s\"",
- name, location);
- result = strdup("");
- goto done;
- }
- MtdWriteContext* ctx = mtd_write_partition(mtd);
- if (ctx == NULL) {
- printf("%s: can't write \"%s\"", name, location);
- result = strdup("");
- goto done;
- }
- if (mtd_erase_blocks(ctx, -1) == -1) {
- mtd_write_close(ctx);
- printf("%s: failed to erase \"%s\"", name, location);
- result = strdup("");
- goto done;
- }
- if (mtd_write_close(ctx) != 0) {
- printf("%s: failed to close \"%s\"", name, location);
- result = strdup("");
- goto done;
- }
- result = location;
-#ifdef USE_EXT4
- } else if (strcmp(fs_type, "ext4") == 0) {
+ if (strcmp(fs_type, "ext4") == 0) {
int status = make_ext4fs(location, atoll(fs_size), mount_point, sehandle);
if (status != 0) {
printf("%s: make_ext4fs failed (%d) on %s",
@@ -389,7 +348,6 @@ Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
goto done;
}
result = location;
-#endif
} else {
printf("%s: unsupported fs_type \"%s\" partition_type \"%s\"",
name, fs_type, partition_type);
@@ -403,7 +361,7 @@ done:
}
Value* RenameFn(const char* name, State* state, int argc, Expr* argv[]) {
- char* result = NULL;
+ char* result = nullptr;
if (argc != 2) {
return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
}
@@ -442,15 +400,10 @@ done:
}
Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
- char** paths = reinterpret_cast<char**>(malloc(argc * sizeof(char*)));
+ std::vector<std::string> paths;
for (int i = 0; i < argc; ++i) {
- paths[i] = Evaluate(state, argv[i]);
- if (paths[i] == NULL) {
- for (int j = 0; j < i; ++j) {
- free(paths[j]);
- }
- free(paths);
- return NULL;
+ if (!Evaluate(state, argv[i], &paths[i])) {
+ return nullptr;
}
}
@@ -458,15 +411,12 @@ Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
int success = 0;
for (int i = 0; i < argc; ++i) {
- if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
+ if ((recursive ? dirUnlinkHierarchy(paths[i].c_str()) : unlink(paths[i].c_str())) == 0) {
++success;
- free(paths[i]);
+ }
}
- free(paths);
- char buffer[10];
- sprintf(buffer, "%d", success);
- return StringValue(strdup(buffer));
+ return StringValue(android::base::StringPrintf("%d", success));
}
@@ -518,17 +468,16 @@ Value* PackageExtractDirFn(const char* name, State* state,
char* dest_path;
if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
- ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
+ ZipArchiveHandle za = ((UpdaterInfo*)(state->cookie))->package_zip;
// To create a consistent system image, never use the clock for timestamps.
struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
- bool success = mzExtractRecursive(za, zip_path, dest_path,
- &timestamp,
- NULL, NULL, sehandle);
+ bool success = ExtractPackageRecursive(za, zip_path, dest_path, &timestamp, sehandle);
+
free(zip_path);
free(dest_path);
- return StringValue(strdup(success ? "t" : ""));
+ return StringValue(success ? "t" : "");
}
@@ -548,26 +497,27 @@ Value* PackageExtractFileFn(const char* name, State* state,
if (argc == 2) {
// The two-argument version extracts to a file.
- ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
+ ZipArchiveHandle za = ((UpdaterInfo*)(state->cookie))->package_zip;
char* zip_path;
char* dest_path;
if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
- const ZipEntry* entry = mzFindZipEntry(za, zip_path);
- if (entry == NULL) {
+ ZipString zip_string_path(zip_path);
+ ZipEntry entry;
+ if (FindEntry(za, zip_string_path, &entry) != 0) {
printf("%s: no %s in package\n", name, zip_path);
goto done2;
}
{
- int fd = TEMP_FAILURE_RETRY(ota_open(dest_path, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC,
+ int fd = TEMP_FAILURE_RETRY(ota_open(dest_path, O_WRONLY | O_CREAT | O_TRUNC,
S_IRUSR | S_IWUSR));
if (fd == -1) {
printf("%s: can't open %s for write: %s\n", name, dest_path, strerror(errno));
goto done2;
}
- success = mzExtractZipEntryToFile(za, entry, fd);
+ success = ExtractEntryToFile(za, &entry, fd);
if (ota_fsync(fd) == -1) {
printf("fsync of \"%s\" failed: %s\n", dest_path, strerror(errno));
success = false;
@@ -581,7 +531,7 @@ Value* PackageExtractFileFn(const char* name, State* state,
done2:
free(zip_path);
free(dest_path);
- return StringValue(strdup(success ? "t" : ""));
+ return StringValue(success ? "t" : "");
} else {
// The one-argument version returns the contents of the file
// as the result.
@@ -589,59 +539,33 @@ Value* PackageExtractFileFn(const char* name, State* state,
char* zip_path;
if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
- Value* v = reinterpret_cast<Value*>(malloc(sizeof(Value)));
- v->type = VAL_BLOB;
- v->size = -1;
- v->data = NULL;
+ Value* v = new Value(VAL_INVALID, "");
- ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
- const ZipEntry* entry = mzFindZipEntry(za, zip_path);
- if (entry == NULL) {
+ ZipArchiveHandle za = ((UpdaterInfo*)(state->cookie))->package_zip;
+ ZipString zip_string_path(zip_path);
+ ZipEntry entry;
+ if (FindEntry(za, zip_string_path, &entry) != 0) {
printf("%s: no %s in package\n", name, zip_path);
goto done1;
}
- v->size = mzGetZipEntryUncompLen(entry);
- v->data = reinterpret_cast<char*>(malloc(v->size));
- if (v->data == NULL) {
- printf("%s: failed to allocate %ld bytes for %s\n",
- name, (long)v->size, zip_path);
- goto done1;
+ v->data.resize(entry.uncompressed_length);
+ if (ExtractToMemory(za, &entry, reinterpret_cast<uint8_t*>(&v->data[0]),
+ v->data.size()) != 0) {
+ printf("%s: faled to extract %zu bytes to memory\n", name, v->data.size());
+ } else {
+ success = true;
}
- success = mzExtractZipEntryToBuffer(za, entry,
- (unsigned char *)v->data);
-
done1:
free(zip_path);
if (!success) {
- free(v->data);
- v->data = NULL;
- v->size = -1;
- }
- return v;
- }
-}
-
-// Create all parent directories of name, if necessary.
-static int make_parents(char* name) {
- char* p;
- for (p = name + (strlen(name)-1); p > name; --p) {
- if (*p != '/') continue;
- *p = '\0';
- if (make_parents(name) < 0) return -1;
- int result = mkdir(name, 0700);
- if (result == 0) printf("created [%s]\n", name);
- *p = '/';
- if (result == 0 || errno == EEXIST) {
- // successfully created or already existed; we're done
- return 0;
+ v->data.clear();
} else {
- printf("failed to mkdir %s: %s\n", name, strerror(errno));
- return -1;
+ v->type = VAL_BLOB;
}
+ return v;
}
- return 0;
}
// symlink target src1 src2 ...
@@ -650,13 +574,13 @@ Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
if (argc == 0) {
return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1+ args, got %d", name, argc);
}
- char* target;
- target = Evaluate(state, argv[0]);
- if (target == NULL) return NULL;
+ std::string target;
+ if (!Evaluate(state, argv[0], &target)) {
+ return nullptr;
+ }
char** srcs = ReadVarArgs(state, argc-1, argv+1);
if (srcs == NULL) {
- free(target);
return NULL;
}
@@ -672,12 +596,12 @@ Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
}
if (make_parents(srcs[i])) {
printf("%s: failed to symlink %s to %s: making parents failed\n",
- name, srcs[i], target);
+ name, srcs[i], target.c_str());
++bad;
}
- if (symlink(target, srcs[i]) < 0) {
+ if (symlink(target.c_str(), srcs[i]) < 0) {
printf("%s: failed to symlink %s to %s: %s\n",
- name, srcs[i], target, strerror(errno));
+ name, srcs[i], target.c_str(), strerror(errno));
++bad;
}
free(srcs[i]);
@@ -686,7 +610,7 @@ Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
if (bad) {
return ErrorAbort(state, kSymlinkFailure, "%s: some symlinks failed", name);
}
- return StringValue(strdup(""));
+ return StringValue("");
}
struct perm_parsed_args {
@@ -949,21 +873,20 @@ done:
return ErrorAbort(state, kSetMetadataFailure, "%s: some changes failed", name);
}
- return StringValue(strdup(""));
+ return StringValue("");
}
Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
if (argc != 1) {
return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
}
- char* key = Evaluate(state, argv[0]);
- if (key == NULL) return NULL;
-
- char value[PROPERTY_VALUE_MAX];
- property_get(key, value, "");
- free(key);
+ std::string key;
+ if (!Evaluate(state, argv[0], &key)) {
+ return nullptr;
+ }
+ std::string value = android::base::GetProperty(key, "");
- return StringValue(strdup(value));
+ return StringValue(value);
}
@@ -998,13 +921,13 @@ Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
buffer = reinterpret_cast<char*>(malloc(st.st_size+1));
if (buffer == NULL) {
- ErrorAbort(state, kFileGetPropFailure, "%s: failed to alloc %lld bytes", name,
- (long long)st.st_size+1);
+ ErrorAbort(state, kFileGetPropFailure, "%s: failed to alloc %zu bytes", name,
+ static_cast<size_t>(st.st_size+1));
goto done;
}
FILE* f;
- f = fopen(filename, "rb");
+ f = ota_fopen(filename, "rb");
if (f == NULL) {
ErrorAbort(state, kFileOpenFailure, "%s: failed to open %s: %s", name, filename,
strerror(errno));
@@ -1012,14 +935,14 @@ Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
}
if (ota_fread(buffer, 1, st.st_size, f) != static_cast<size_t>(st.st_size)) {
- ErrorAbort(state, kFreadFailure, "%s: failed to read %lld bytes from %s",
- name, (long long)st.st_size+1, filename);
- fclose(f);
+ ErrorAbort(state, kFreadFailure, "%s: failed to read %zu bytes from %s",
+ name, static_cast<size_t>(st.st_size), filename);
+ ota_fclose(f);
goto done;
}
buffer[st.st_size] = '\0';
- fclose(f);
+ ota_fclose(f);
char* line;
line = strtok(buffer, "\n");
@@ -1066,98 +989,6 @@ Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
return StringValue(result);
}
-// write_raw_image(filename_or_blob, partition)
-Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
- char* result = NULL;
-
- Value* partition_value;
- Value* contents;
- if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) {
- return NULL;
- }
-
- char* partition = NULL;
- if (partition_value->type != VAL_STRING) {
- ErrorAbort(state, kArgsParsingFailure, "partition argument to %s must be string", name);
- goto done;
- }
- partition = partition_value->data;
- if (strlen(partition) == 0) {
- ErrorAbort(state, kArgsParsingFailure, "partition argument to %s can't be empty", name);
- goto done;
- }
- if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) {
- ErrorAbort(state, kArgsParsingFailure, "file argument to %s can't be empty", name);
- goto done;
- }
-
- mtd_scan_partitions();
- const MtdPartition* mtd;
- mtd = mtd_find_partition_by_name(partition);
- if (mtd == NULL) {
- printf("%s: no mtd partition named \"%s\"\n", name, partition);
- result = strdup("");
- goto done;
- }
-
- MtdWriteContext* ctx;
- ctx = mtd_write_partition(mtd);
- if (ctx == NULL) {
- printf("%s: can't write mtd partition \"%s\"\n",
- name, partition);
- result = strdup("");
- goto done;
- }
-
- bool success;
-
- if (contents->type == VAL_STRING) {
- // we're given a filename as the contents
- char* filename = contents->data;
- FILE* f = ota_fopen(filename, "rb");
- if (f == NULL) {
- printf("%s: can't open %s: %s\n", name, filename, strerror(errno));
- result = strdup("");
- goto done;
- }
-
- success = true;
- char* buffer = reinterpret_cast<char*>(malloc(BUFSIZ));
- int read;
- while (success && (read = ota_fread(buffer, 1, BUFSIZ, f)) > 0) {
- int wrote = mtd_write_data(ctx, buffer, read);
- success = success && (wrote == read);
- }
- free(buffer);
- ota_fclose(f);
- } else {
- // we're given a blob as the contents
- ssize_t wrote = mtd_write_data(ctx, contents->data, contents->size);
- success = (wrote == contents->size);
- }
- if (!success) {
- printf("mtd_write_data to %s failed: %s\n",
- partition, strerror(errno));
- }
-
- if (mtd_erase_blocks(ctx, -1) == -1) {
- printf("%s: error erasing blocks of %s\n", name, partition);
- }
- if (mtd_write_close(ctx) != 0) {
- printf("%s: error closing write of %s\n", name, partition);
- }
-
- printf("%s %s partition\n",
- success ? "wrote" : "failed to write", partition);
-
- result = success ? partition : strdup("");
-
-done:
- if (result != partition) FreeValue(partition_value);
- FreeValue(contents);
- return StringValue(result);
-}
-
// apply_patch_space(bytes)
Value* ApplyPatchSpaceFn(const char* name, State* state,
int argc, Expr* argv[]) {
@@ -1174,7 +1005,7 @@ Value* ApplyPatchSpaceFn(const char* name, State* state,
return nullptr;
}
- return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
+ return StringValue(CacheSizeCheck(bytes) ? "" : "t");
}
// apply_patch(file, size, init_sha1, tgt_sha1, patch)
@@ -1206,17 +1037,16 @@ Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
}
int patchcount = (argc-4) / 2;
- std::unique_ptr<Value*, decltype(&free)> arg_values(ReadValueVarArgs(state, argc-4, argv+4),
- free);
+ std::unique_ptr<Value*> arg_values(ReadValueVarArgs(state, argc-4, argv+4));
if (!arg_values) {
return nullptr;
}
- std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> patch_shas;
- std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> patches;
+ std::vector<std::unique_ptr<Value>> patch_shas;
+ std::vector<std::unique_ptr<Value>> patches;
// Protect values by unique_ptrs first to get rid of memory leak.
for (int i = 0; i < patchcount * 2; i += 2) {
- patch_shas.emplace_back(arg_values.get()[i], FreeValue);
- patches.emplace_back(arg_values.get()[i+1], FreeValue);
+ patch_shas.emplace_back(arg_values.get()[i]);
+ patches.emplace_back(arg_values.get()[i+1]);
}
for (int i = 0; i < patchcount; ++i) {
@@ -1230,7 +1060,7 @@ Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
}
}
- std::vector<char*> patch_sha_str;
+ std::vector<std::string> patch_sha_str;
std::vector<Value*> patch_ptrs;
for (int i = 0; i < patchcount; ++i) {
patch_sha_str.push_back(patch_shas[i]->data);
@@ -1239,9 +1069,9 @@ Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
int result = applypatch(source_filename, target_filename,
target_sha1, target_size,
- patchcount, patch_sha_str.data(), patch_ptrs.data(), NULL);
+ patch_sha_str, patch_ptrs.data(), NULL);
- return StringValue(strdup(result == 0 ? "t" : ""));
+ return StringValue(result == 0 ? "t" : "");
}
// apply_patch_check(file, [sha1_1, ...])
@@ -1254,21 +1084,17 @@ Value* ApplyPatchCheckFn(const char* name, State* state,
char* filename;
if (ReadArgs(state, argv, 1, &filename) < 0) {
- return NULL;
+ return nullptr;
}
- int patchcount = argc-1;
- char** sha1s = ReadVarArgs(state, argc-1, argv+1);
-
- int result = applypatch_check(filename, patchcount, sha1s);
-
- int i;
- for (i = 0; i < patchcount; ++i) {
- free(sha1s[i]);
+ std::vector<std::string> sha1s;
+ if (!ReadArgs(state, argc-1, argv+1, &sha1s)) {
+ return nullptr;
}
- free(sha1s);
- return StringValue(strdup(result == 0 ? "t" : ""));
+ int result = applypatch_check(filename, sha1s);
+
+ return StringValue(result == 0 ? "t" : "");
}
// This is the updater side handler for ui_print() in edify script. Contents
@@ -1288,7 +1114,7 @@ Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
buffer += "\n";
uiPrint(state, buffer);
- return StringValue(strdup(buffer.c_str()));
+ return StringValue(buffer);
}
Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
@@ -1296,7 +1122,7 @@ Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %d", name, argc);
}
fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
- return StringValue(strdup("t"));
+ return StringValue("t");
}
Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
@@ -1339,10 +1165,7 @@ Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
free(args);
free(args2);
- char buffer[20];
- sprintf(buffer, "%d", status);
-
- return StringValue(strdup(buffer));
+ return StringValue(android::base::StringPrintf("%d", status));
}
// sha1_check(data)
@@ -1358,45 +1181,40 @@ Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
}
- std::unique_ptr<Value*, decltype(&free)> arg_values(ReadValueVarArgs(state, argc, argv), free);
+ std::unique_ptr<Value*> arg_values(ReadValueVarArgs(state, argc, argv));
if (arg_values == nullptr) {
return nullptr;
}
- std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> args;
+ std::vector<std::unique_ptr<Value>> args;
for (int i = 0; i < argc; ++i) {
- args.emplace_back(arg_values.get()[i], FreeValue);
+ args.emplace_back(arg_values.get()[i]);
}
- if (args[0]->size < 0) {
- return StringValue(strdup(""));
+ if (args[0]->type == VAL_INVALID) {
+ return StringValue("");
}
uint8_t digest[SHA_DIGEST_LENGTH];
- SHA1(reinterpret_cast<uint8_t*>(args[0]->data), args[0]->size, digest);
+ SHA1(reinterpret_cast<const uint8_t*>(args[0]->data.c_str()), args[0]->data.size(), digest);
if (argc == 1) {
- return StringValue(PrintSha1(digest));
+ return StringValue(print_sha1(digest));
}
- int i;
- uint8_t arg_digest[SHA_DIGEST_LENGTH];
- for (i = 1; i < argc; ++i) {
+ for (int i = 1; i < argc; ++i) {
+ uint8_t arg_digest[SHA_DIGEST_LENGTH];
if (args[i]->type != VAL_STRING) {
- printf("%s(): arg %d is not a string; skipping",
- name, i);
- } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
+ printf("%s(): arg %d is not a string; skipping", name, i);
+ } else if (ParseSha1(args[i]->data.c_str(), arg_digest) != 0) {
// Warn about bad args and skip them.
- printf("%s(): error parsing \"%s\" as sha-1; skipping",
- name, args[i]->data);
+ printf("%s(): error parsing \"%s\" as sha-1; skipping", name, args[i]->data.c_str());
} else if (memcmp(digest, arg_digest, SHA_DIGEST_LENGTH) == 0) {
- break;
+ // Found a match.
+ return args[i].release();
}
}
- if (i >= argc) {
- // Didn't match any of the hex strings; return false.
- return StringValue(strdup(""));
- }
- // Found a match.
- return args[i].release();
+
+ // Didn't match any of the hex strings; return false.
+ return StringValue("");
}
// Read a local file and return its contents (the Value* returned
@@ -1408,21 +1226,12 @@ Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
char* filename;
if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
- Value* v = static_cast<Value*>(malloc(sizeof(Value)));
- if (v == nullptr) {
- return nullptr;
- }
- v->type = VAL_BLOB;
- v->size = -1;
- v->data = nullptr;
+ Value* v = new Value(VAL_INVALID, "");
FileContents fc;
if (LoadFileContents(filename, &fc) == 0) {
- v->data = static_cast<char*>(malloc(fc.data.size()));
- if (v->data != nullptr) {
- memcpy(v->data, fc.data.data(), fc.data.size());
- v->size = fc.data.size();
- }
+ v->type = VAL_BLOB;
+ v->data = std::string(fc.data.begin(), fc.data.end());
}
free(filename);
return v;
@@ -1446,22 +1255,18 @@ Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) {
char* property;
if (ReadArgs(state, argv, 2, &filename, &property) < 0) return NULL;
- char buffer[80];
-
// zero out the 'command' field of the bootloader message.
+ char buffer[80];
memset(buffer, 0, sizeof(((struct bootloader_message*)0)->command));
- FILE* f = fopen(filename, "r+b");
+ FILE* f = ota_fopen(filename, "r+b");
fseek(f, offsetof(struct bootloader_message, command), SEEK_SET);
ota_fwrite(buffer, sizeof(((struct bootloader_message*)0)->command), 1, f);
- fclose(f);
+ ota_fclose(f);
free(filename);
- strcpy(buffer, "reboot,");
- if (property != NULL) {
- strncat(buffer, property, sizeof(buffer)-10);
- }
-
- property_set(ANDROID_RB_PROPERTY, buffer);
+ std::string reboot_cmd = "reboot,";
+ if (property != nullptr) reboot_cmd += property;
+ android::base::SetProperty(ANDROID_RB_PROPERTY, reboot_cmd);
sleep(5);
free(property);
@@ -1492,18 +1297,21 @@ Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
// bootloader message that the main recovery uses to save its
// arguments in case of the device restarting midway through
// package installation.
- FILE* f = fopen(filename, "r+b");
+ FILE* f = ota_fopen(filename, "r+b");
fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
- int to_write = strlen(stagestr)+1;
- int max_size = sizeof(((struct bootloader_message*)0)->stage);
+ size_t to_write = strlen(stagestr) + 1;
+ size_t max_size = sizeof(((struct bootloader_message*)0)->stage);
if (to_write > max_size) {
to_write = max_size;
- stagestr[max_size-1] = 0;
+ stagestr[max_size - 1] = 0;
}
- ota_fwrite(stagestr, to_write, 1, f);
- fclose(f);
+ size_t status = ota_fwrite(stagestr, to_write, 1, f);
+ ota_fclose(f);
free(stagestr);
+ if (status != to_write) {
+ return StringValue("");
+ }
return StringValue(filename);
}
@@ -1518,13 +1326,16 @@ Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
char buffer[sizeof(((struct bootloader_message*)0)->stage)];
- FILE* f = fopen(filename, "rb");
+ FILE* f = ota_fopen(filename, "rb");
fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
- ota_fread(buffer, sizeof(buffer), 1, f);
- fclose(f);
- buffer[sizeof(buffer)-1] = '\0';
+ size_t status = ota_fread(buffer, sizeof(buffer), 1, f);
+ ota_fclose(f);
+ if (status != sizeof(buffer)) {
+ return StringValue("");
+ }
- return StringValue(strdup(buffer));
+ buffer[sizeof(buffer)-1] = '\0';
+ return StringValue(buffer);
}
Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) {
@@ -1546,7 +1357,7 @@ Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[])
ota_close(fd);
- return StringValue(strdup(success ? "t" : ""));
+ return StringValue(success ? "t" : "");
}
Value* EnableRebootFn(const char* name, State* state, int argc, Expr* argv[]) {
@@ -1555,7 +1366,7 @@ Value* EnableRebootFn(const char* name, State* state, int argc, Expr* argv[]) {
}
UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
fprintf(ui->cmd_pipe, "enable_reboot\n");
- return StringValue(strdup("t"));
+ return StringValue("t");
}
Value* Tune2FsFn(const char* name, State* state, int argc, Expr* argv[]) {
@@ -1586,7 +1397,7 @@ Value* Tune2FsFn(const char* name, State* state, int argc, Expr* argv[]) {
return ErrorAbort(state, kTune2FsFailure, "%s() returned error code %d",
name, result);
}
- return StringValue(strdup("t"));
+ return StringValue("t");
}
void RegisterInstallFunctions() {
@@ -1616,7 +1427,6 @@ void RegisterInstallFunctions() {
RegisterFunction("getprop", GetPropFn);
RegisterFunction("file_getprop", FileGetPropFn);
- RegisterFunction("write_raw_image", WriteRawImageFn);
RegisterFunction("apply_patch", ApplyPatchFn);
RegisterFunction("apply_patch_check", ApplyPatchCheckFn);