diff options
Diffstat (limited to 'updater')
-rw-r--r-- | updater/install.c | 132 | ||||
-rw-r--r-- | updater/updater.c | 9 | ||||
-rw-r--r-- | updater/updater.h | 1 |
3 files changed, 76 insertions, 66 deletions
diff --git a/updater/install.c b/updater/install.c index aa80d7576..852b393ea 100644 --- a/updater/install.c +++ b/updater/install.c @@ -315,37 +315,82 @@ char* PackageExtractDirFn(const char* name, State* state, // package_extract_file(package_path, destination_path) +// or +// package_extract_file(package_path) +// to return the entire contents of the file as the result of this +// function (the char* returned points to a long giving the length +// followed by that many bytes of data). char* PackageExtractFileFn(const char* name, State* state, int argc, Expr* argv[]) { - if (argc != 2) { - return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc); + if (argc != 1 && argc != 2) { + return ErrorAbort(state, "%s() expects 1 or 2 args, got %d", + name, argc); } - char* zip_path; - char* dest_path; - if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL; - bool success = false; + if (argc == 2) { + // The two-argument version extracts to a file. + + char* zip_path; + char* dest_path; + if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL; + + ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip; + const ZipEntry* entry = mzFindZipEntry(za, zip_path); + if (entry == NULL) { + fprintf(stderr, "%s: no %s in package\n", name, zip_path); + goto done2; + } - ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip; - const ZipEntry* entry = mzFindZipEntry(za, zip_path); - if (entry == NULL) { - fprintf(stderr, "%s: no %s in package\n", name, zip_path); - goto done; - } + FILE* f = fopen(dest_path, "wb"); + if (f == NULL) { + fprintf(stderr, "%s: can't open %s for write: %s\n", + name, dest_path, strerror(errno)); + goto done2; + } + success = mzExtractZipEntryToFile(za, entry, fileno(f)); + fclose(f); - FILE* f = fopen(dest_path, "wb"); - if (f == NULL) { - fprintf(stderr, "%s: can't open %s for write: %s\n", - name, dest_path, strerror(errno)); - goto done; - } - success = mzExtractZipEntryToFile(za, entry, fileno(f)); - fclose(f); + done2: + free(zip_path); + free(dest_path); + return strdup(success ? "t" : ""); + } else { + // The one-argument version returns the contents of the file + // as the result. - done: - free(zip_path); - free(dest_path); - return strdup(success ? "t" : ""); + char* zip_path; + char* buffer = NULL; + + if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL; + + ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip; + const ZipEntry* entry = mzFindZipEntry(za, zip_path); + if (entry == NULL) { + fprintf(stderr, "%s: no %s in package\n", name, zip_path); + goto done1; + } + + long size = mzGetZipEntryUncompLen(entry); + buffer = malloc(size + sizeof(long)); + if (buffer == NULL) { + fprintf(stderr, "%s: failed to allocate %ld bytes for %s\n", + name, size+sizeof(long), zip_path); + goto done1; + } + + *(long *)buffer = size; + success = mzExtractZipEntryToBuffer( + za, entry, (unsigned char *)(buffer + sizeof(long))); + + done1: + free(zip_path); + if (!success) { + free(buffer); + buffer = malloc(sizeof(long)); + *(long *)buffer = -1L; + } + return buffer; + } } @@ -658,44 +703,6 @@ done: return result; } -// write_firmware_image(file, partition) -// -// partition is "radio" or "hboot" -// file is not used until after updater exits -// -// TODO: this should live in some HTC-specific library -char* WriteFirmwareImageFn(const char* name, State* state, - int argc, Expr* argv[]) { - char* result = NULL; - - char* partition; - char* filename; - if (ReadArgs(state, argv, 2, &filename, &partition) < 0) { - return NULL; - } - - if (strlen(partition) == 0) { - ErrorAbort(state, "partition argument to %s can't be empty", name); - goto done; - } - if (strlen(filename) == 0) { - ErrorAbort(state, "file argument to %s can't be empty", name); - goto done; - } - - FILE* cmd = ((UpdaterInfo*)(state->cookie))->cmd_pipe; - fprintf(cmd, "firmware %s %s\n", partition, filename); - - printf("will write %s firmware from %s\n", partition, filename); - result = partition; - -done: - if (result != partition) free(partition); - free(filename); - return result; -} - - extern int applypatch(int argc, char** argv); // apply_patch(srcfile, tgtfile, tgtsha1, tgtsize, sha1:patch, ...) @@ -843,7 +850,6 @@ void RegisterInstallFunctions() { RegisterFunction("getprop", GetPropFn); RegisterFunction("file_getprop", FileGetPropFn); RegisterFunction("write_raw_image", WriteRawImageFn); - RegisterFunction("write_firmware_image", WriteFirmwareImageFn); RegisterFunction("apply_patch", ApplyPatchFn); RegisterFunction("apply_patch_check", ApplyPatchFn); diff --git a/updater/updater.c b/updater/updater.c index 1aa277c7f..2d16deeba 100644 --- a/updater/updater.c +++ b/updater/updater.c @@ -39,9 +39,11 @@ int main(int argc, char** argv) { } char* version = argv[1]; - if ((version[0] != '1' && version[0] != '2') || version[1] != '\0') { - // We support version "1" or "2". - fprintf(stderr, "wrong updater binary API; expected 1 or 2, got %s\n", + if ((version[0] != '1' && version[0] != '2' && version[0] != '3') || + version[1] != '\0') { + // We support version 1, 2, or 3. + fprintf(stderr, "wrong updater binary API; expected 1, 2, or 3; " + "got %s\n", argv[1]); return 2; } @@ -100,6 +102,7 @@ int main(int argc, char** argv) { UpdaterInfo updater_info; updater_info.cmd_pipe = cmd_pipe; updater_info.package_zip = &za; + updater_info.version = atoi(version); State state; state.cookie = &updater_info; diff --git a/updater/updater.h b/updater/updater.h index 22fbfd285..bd60dc1fd 100644 --- a/updater/updater.h +++ b/updater/updater.h @@ -23,6 +23,7 @@ typedef struct { FILE* cmd_pipe; ZipArchive* package_zip; + int version; } UpdaterInfo; #endif |