blob: 1968ae48fc8bf4899c93243e01a501b0ef8ea875 [file] [log] [blame]
Doug Zongker512536a2010-02-17 16:11:44 -08001/*
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
17#include <stdio.h>
Doug Zongkerc4351c72010-02-22 14:46:32 -080018#include <stdlib.h>
19#include <string.h>
20#include <unistd.h>
Doug Zongker512536a2010-02-17 16:11:44 -080021
Yabin Cuid483c202016-02-03 17:08:52 -080022#include <memory>
23#include <vector>
24
Tao Baod80a9982016-03-03 11:43:47 -080025#include "applypatch/applypatch.h"
Doug Zongkerc4351c72010-02-22 14:46:32 -080026#include "edify/expr.h"
Sen Jiangc48cb5e2016-02-04 16:23:21 +080027#include "openssl/sha.h"
Doug Zongkerc4351c72010-02-22 14:46:32 -080028
Tao Baoba9a42a2015-06-23 23:23:33 -070029static int CheckMode(int argc, char** argv) {
Doug Zongkerc4351c72010-02-22 14:46:32 -080030 if (argc < 3) {
31 return 2;
32 }
33 return applypatch_check(argv[2], argc-3, argv+3);
34}
35
Tao Baoba9a42a2015-06-23 23:23:33 -070036static int SpaceMode(int argc, char** argv) {
Doug Zongkerc4351c72010-02-22 14:46:32 -080037 if (argc != 3) {
38 return 2;
39 }
40 char* endptr;
41 size_t bytes = strtol(argv[2], &endptr, 10);
42 if (bytes == 0 && endptr == argv[2]) {
43 printf("can't parse \"%s\" as byte count\n\n", argv[2]);
44 return 1;
45 }
46 return CacheSizeCheck(bytes);
47}
48
Yabin Cuid6c93af2016-02-10 16:41:10 -080049// Parse arguments (which should be of the form "<sha1>:<filename>"
50// into the new parallel arrays *sha1s and *files.Returns true on
Doug Zongkerc4351c72010-02-22 14:46:32 -080051// success.
Yabin Cuid483c202016-02-03 17:08:52 -080052static bool ParsePatchArgs(int argc, char** argv, std::vector<char*>* sha1s,
Yabin Cuid6c93af2016-02-10 16:41:10 -080053 std::vector<FileContents>* files) {
Sen Jiangc48cb5e2016-02-04 16:23:21 +080054 uint8_t digest[SHA_DIGEST_LENGTH];
Doug Zongkerc4351c72010-02-22 14:46:32 -080055
Yabin Cuid483c202016-02-03 17:08:52 -080056 for (int i = 0; i < argc; ++i) {
Doug Zongkerc4351c72010-02-22 14:46:32 -080057 char* colon = strchr(argv[i], ':');
Yabin Cuid6c93af2016-02-10 16:41:10 -080058 if (colon == nullptr) {
59 printf("no ':' in patch argument \"%s\"\n", argv[i]);
60 return false;
Doug Zongkerc4351c72010-02-22 14:46:32 -080061 }
Yabin Cuid6c93af2016-02-10 16:41:10 -080062 *colon = '\0';
63 ++colon;
Doug Zongkerc4351c72010-02-22 14:46:32 -080064 if (ParseSha1(argv[i], digest) != 0) {
65 printf("failed to parse sha1 \"%s\"\n", argv[i]);
Tao Baoba9a42a2015-06-23 23:23:33 -070066 return false;
Doug Zongkerc4351c72010-02-22 14:46:32 -080067 }
68
Yabin Cuid483c202016-02-03 17:08:52 -080069 sha1s->push_back(argv[i]);
Yabin Cuid6c93af2016-02-10 16:41:10 -080070 FileContents fc;
71 if (LoadFileContents(colon, &fc) != 0) {
72 return false;
Doug Zongkerc4351c72010-02-22 14:46:32 -080073 }
Yabin Cuid6c93af2016-02-10 16:41:10 -080074 files->push_back(std::move(fc));
Doug Zongkerc4351c72010-02-22 14:46:32 -080075 }
Tao Baoba9a42a2015-06-23 23:23:33 -070076 return true;
Doug Zongkerc4351c72010-02-22 14:46:32 -080077}
78
Tao Baoabba55b2015-07-17 18:11:12 -070079static int FlashMode(const char* src_filename, const char* tgt_filename,
80 const char* tgt_sha1, size_t tgt_size) {
81 return applypatch_flash(src_filename, tgt_filename, tgt_sha1, tgt_size);
82}
83
84static int PatchMode(int argc, char** argv) {
Yabin Cuid6c93af2016-02-10 16:41:10 -080085 FileContents bonusFc;
86 Value bonusValue;
87 Value* bonus = nullptr;
88
Doug Zongkera3ccba62012-08-20 15:28:02 -070089 if (argc >= 3 && strcmp(argv[1], "-b") == 0) {
Yabin Cuid6c93af2016-02-10 16:41:10 -080090 if (LoadFileContents(argv[2], &bonusFc) != 0) {
Doug Zongkera3ccba62012-08-20 15:28:02 -070091 printf("failed to load bonus file %s\n", argv[2]);
92 return 1;
93 }
Yabin Cuid6c93af2016-02-10 16:41:10 -080094 bonus = &bonusValue;
Doug Zongkera3ccba62012-08-20 15:28:02 -070095 bonus->type = VAL_BLOB;
Yabin Cuid6c93af2016-02-10 16:41:10 -080096 bonus->size = bonusFc.data.size();
97 bonus->data = reinterpret_cast<char*>(bonusFc.data.data());
Doug Zongkera3ccba62012-08-20 15:28:02 -070098 argc -= 2;
99 argv += 2;
100 }
101
Tao Baoabba55b2015-07-17 18:11:12 -0700102 if (argc < 4) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800103 return 2;
104 }
105
106 char* endptr;
107 size_t target_size = strtol(argv[4], &endptr, 10);
108 if (target_size == 0 && endptr == argv[4]) {
109 printf("can't parse \"%s\" as byte count\n\n", argv[4]);
110 return 1;
111 }
112
Tao Baoabba55b2015-07-17 18:11:12 -0700113 // If no <src-sha1>:<patch> is provided, it is in flash mode.
114 if (argc == 5) {
Yabin Cuid6c93af2016-02-10 16:41:10 -0800115 if (bonus != nullptr) {
Tao Baoabba55b2015-07-17 18:11:12 -0700116 printf("bonus file not supported in flash mode\n");
117 return 1;
118 }
119 return FlashMode(argv[1], argv[2], argv[3], target_size);
120 }
Yabin Cuid483c202016-02-03 17:08:52 -0800121 std::vector<char*> sha1s;
Yabin Cuid6c93af2016-02-10 16:41:10 -0800122 std::vector<FileContents> files;
123 if (!ParsePatchArgs(argc-5, argv+5, &sha1s, &files)) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800124 printf("failed to parse patch args\n");
125 return 1;
126 }
Yabin Cuid6c93af2016-02-10 16:41:10 -0800127 std::vector<Value> patches(files.size());
128 std::vector<Value*> patch_ptrs(files.size());
129 for (size_t i = 0; i < files.size(); ++i) {
130 patches[i].type = VAL_BLOB;
131 patches[i].size = files[i].data.size();
132 patches[i].data = reinterpret_cast<char*>(files[i].data.data());
133 patch_ptrs[i] = &patches[i];
Doug Zongkerc4351c72010-02-22 14:46:32 -0800134 }
Yabin Cuid483c202016-02-03 17:08:52 -0800135 return applypatch(argv[1], argv[2], argv[3], target_size,
136 patch_ptrs.size(), sha1s.data(),
Yabin Cuid6c93af2016-02-10 16:41:10 -0800137 patch_ptrs.data(), bonus);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800138}
Doug Zongker512536a2010-02-17 16:11:44 -0800139
140// This program applies binary patches to files in a way that is safe
141// (the original file is not touched until we have the desired
142// replacement for it) and idempotent (it's okay to run this program
143// multiple times).
144//
145// - if the sha1 hash of <tgt-file> is <tgt-sha1>, does nothing and exits
146// successfully.
147//
Tao Baoabba55b2015-07-17 18:11:12 -0700148// - otherwise, if no <src-sha1>:<patch> is provided, flashes <tgt-file> with
149// <src-file>. <tgt-file> must be a partition name, while <src-file> must
150// be a regular image file. <src-file> will not be deleted on success.
151//
Doug Zongker512536a2010-02-17 16:11:44 -0800152// - otherwise, if the sha1 hash of <src-file> is <src-sha1>, applies the
153// bsdiff <patch> to <src-file> to produce a new file (the type of patch
154// is automatically detected from the file header). If that new
155// file has sha1 hash <tgt-sha1>, moves it to replace <tgt-file>, and
156// exits successfully. Note that if <src-file> and <tgt-file> are
157// not the same, <src-file> is NOT deleted on success. <tgt-file>
158// may be the string "-" to mean "the same as src-file".
159//
160// - otherwise, or if any error is encountered, exits with non-zero
161// status.
162//
Elliott Hughes63a31922016-06-09 17:41:22 -0700163// <src-file> (or <file> in check mode) may refer to an EMMC partition
Doug Zongker512536a2010-02-17 16:11:44 -0800164// to read the source data. See the comments for the
Elliott Hughes63a31922016-06-09 17:41:22 -0700165// LoadPartitionContents() function for the format of such a filename.
Doug Zongker512536a2010-02-17 16:11:44 -0800166
167int main(int argc, char** argv) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800168 if (argc < 2) {
169 usage:
170 printf(
Doug Zongkera3ccba62012-08-20 15:28:02 -0700171 "usage: %s [-b <bonus-file>] <src-file> <tgt-file> <tgt-sha1> <tgt-size> "
Doug Zongker512536a2010-02-17 16:11:44 -0800172 "[<src-sha1>:<patch> ...]\n"
173 " or %s -c <file> [<sha1> ...]\n"
174 " or %s -s <bytes>\n"
175 " or %s -l\n"
176 "\n"
177 "Filenames may be of the form\n"
Elliott Hughes63a31922016-06-09 17:41:22 -0700178 " EMMC:<partition>:<len_1>:<sha1_1>:<len_2>:<sha1_2>:...\n"
179 "to specify reading from or writing to an EMMC partition.\n\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800180 argv[0], argv[0], argv[0], argv[0]);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800181 return 2;
182 }
183
184 int result;
185
186 if (strncmp(argv[1], "-l", 3) == 0) {
187 result = ShowLicenses();
188 } else if (strncmp(argv[1], "-c", 3) == 0) {
189 result = CheckMode(argc, argv);
190 } else if (strncmp(argv[1], "-s", 3) == 0) {
191 result = SpaceMode(argc, argv);
192 } else {
193 result = PatchMode(argc, argv);
194 }
195
196 if (result == 2) {
197 goto usage;
198 }
199 return result;
Doug Zongker512536a2010-02-17 16:11:44 -0800200}