blob: 988b6f9b128241a6a69088262dff11fc08bb748e [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>
Tianjie Xuaced5d92016-10-12 10:55:04 -070023#include <string>
Yabin Cuid483c202016-02-03 17:08:52 -080024#include <vector>
25
Tao Baofada91c2016-10-27 18:16:06 -070026#include <openssl/sha.h>
27
Tao Baod80a9982016-03-03 11:43:47 -080028#include "applypatch/applypatch.h"
Doug Zongkerc4351c72010-02-22 14:46:32 -080029#include "edify/expr.h"
Doug Zongkerc4351c72010-02-22 14:46:32 -080030
Tao Baoba9a42a2015-06-23 23:23:33 -070031static int CheckMode(int argc, char** argv) {
Doug Zongkerc4351c72010-02-22 14:46:32 -080032 if (argc < 3) {
33 return 2;
34 }
Tianjie Xuaced5d92016-10-12 10:55:04 -070035 std::vector<std::string> sha1;
36 for (int i = 3; i < argc; i++) {
37 sha1.push_back(argv[i]);
38 }
39
40 return applypatch_check(argv[2], sha1);
Doug Zongkerc4351c72010-02-22 14:46:32 -080041}
42
Tao Baoba9a42a2015-06-23 23:23:33 -070043static int SpaceMode(int argc, char** argv) {
Doug Zongkerc4351c72010-02-22 14:46:32 -080044 if (argc != 3) {
45 return 2;
46 }
47 char* endptr;
48 size_t bytes = strtol(argv[2], &endptr, 10);
49 if (bytes == 0 && endptr == argv[2]) {
50 printf("can't parse \"%s\" as byte count\n\n", argv[2]);
51 return 1;
52 }
53 return CacheSizeCheck(bytes);
54}
55
Yabin Cuid6c93af2016-02-10 16:41:10 -080056// Parse arguments (which should be of the form "<sha1>:<filename>"
57// into the new parallel arrays *sha1s and *files.Returns true on
Doug Zongkerc4351c72010-02-22 14:46:32 -080058// success.
Tianjie Xuaced5d92016-10-12 10:55:04 -070059static bool ParsePatchArgs(int argc, char** argv, std::vector<std::string>* sha1s,
Yabin Cuid6c93af2016-02-10 16:41:10 -080060 std::vector<FileContents>* files) {
Tianjie Xuaced5d92016-10-12 10:55:04 -070061 if (sha1s == nullptr) {
62 return false;
63 }
Yabin Cuid483c202016-02-03 17:08:52 -080064 for (int i = 0; i < argc; ++i) {
Tianjie Xuaced5d92016-10-12 10:55:04 -070065 uint8_t digest[SHA_DIGEST_LENGTH];
Doug Zongkerc4351c72010-02-22 14:46:32 -080066 char* colon = strchr(argv[i], ':');
Yabin Cuid6c93af2016-02-10 16:41:10 -080067 if (colon == nullptr) {
68 printf("no ':' in patch argument \"%s\"\n", argv[i]);
69 return false;
Doug Zongkerc4351c72010-02-22 14:46:32 -080070 }
Yabin Cuid6c93af2016-02-10 16:41:10 -080071 *colon = '\0';
72 ++colon;
Doug Zongkerc4351c72010-02-22 14:46:32 -080073 if (ParseSha1(argv[i], digest) != 0) {
74 printf("failed to parse sha1 \"%s\"\n", argv[i]);
Tao Baoba9a42a2015-06-23 23:23:33 -070075 return false;
Doug Zongkerc4351c72010-02-22 14:46:32 -080076 }
77
Yabin Cuid483c202016-02-03 17:08:52 -080078 sha1s->push_back(argv[i]);
Yabin Cuid6c93af2016-02-10 16:41:10 -080079 FileContents fc;
80 if (LoadFileContents(colon, &fc) != 0) {
81 return false;
Doug Zongkerc4351c72010-02-22 14:46:32 -080082 }
Yabin Cuid6c93af2016-02-10 16:41:10 -080083 files->push_back(std::move(fc));
Doug Zongkerc4351c72010-02-22 14:46:32 -080084 }
Tao Baoba9a42a2015-06-23 23:23:33 -070085 return true;
Doug Zongkerc4351c72010-02-22 14:46:32 -080086}
87
Tao Baoabba55b2015-07-17 18:11:12 -070088static int FlashMode(const char* src_filename, const char* tgt_filename,
89 const char* tgt_sha1, size_t tgt_size) {
90 return applypatch_flash(src_filename, tgt_filename, tgt_sha1, tgt_size);
91}
92
93static int PatchMode(int argc, char** argv) {
Yabin Cuid6c93af2016-02-10 16:41:10 -080094 FileContents bonusFc;
Tianjie Xuaced5d92016-10-12 10:55:04 -070095 Value bonus(VAL_INVALID, "");
Yabin Cuid6c93af2016-02-10 16:41:10 -080096
Doug Zongkera3ccba62012-08-20 15:28:02 -070097 if (argc >= 3 && strcmp(argv[1], "-b") == 0) {
Yabin Cuid6c93af2016-02-10 16:41:10 -080098 if (LoadFileContents(argv[2], &bonusFc) != 0) {
Doug Zongkera3ccba62012-08-20 15:28:02 -070099 printf("failed to load bonus file %s\n", argv[2]);
100 return 1;
101 }
Tianjie Xuaced5d92016-10-12 10:55:04 -0700102 bonus.type = VAL_BLOB;
Tao Baoedf1b152016-10-24 16:08:28 -0700103 bonus.data = std::string(bonusFc.data.cbegin(), bonusFc.data.cend());
Doug Zongkera3ccba62012-08-20 15:28:02 -0700104 argc -= 2;
105 argv += 2;
106 }
107
Tao Baoabba55b2015-07-17 18:11:12 -0700108 if (argc < 4) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800109 return 2;
110 }
111
112 char* endptr;
113 size_t target_size = strtol(argv[4], &endptr, 10);
114 if (target_size == 0 && endptr == argv[4]) {
115 printf("can't parse \"%s\" as byte count\n\n", argv[4]);
116 return 1;
117 }
118
Tao Baoabba55b2015-07-17 18:11:12 -0700119 // If no <src-sha1>:<patch> is provided, it is in flash mode.
120 if (argc == 5) {
Tianjie Xuaced5d92016-10-12 10:55:04 -0700121 if (bonus.type != VAL_INVALID) {
Tao Baoabba55b2015-07-17 18:11:12 -0700122 printf("bonus file not supported in flash mode\n");
123 return 1;
124 }
125 return FlashMode(argv[1], argv[2], argv[3], target_size);
126 }
Tianjie Xuaced5d92016-10-12 10:55:04 -0700127 std::vector<std::string> sha1s;
Yabin Cuid6c93af2016-02-10 16:41:10 -0800128 std::vector<FileContents> files;
129 if (!ParsePatchArgs(argc-5, argv+5, &sha1s, &files)) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800130 printf("failed to parse patch args\n");
131 return 1;
132 }
Tao Baofada91c2016-10-27 18:16:06 -0700133
134 std::vector<std::unique_ptr<Value>> patches;
Yabin Cuid6c93af2016-02-10 16:41:10 -0800135 for (size_t i = 0; i < files.size(); ++i) {
Tao Baofada91c2016-10-27 18:16:06 -0700136 patches.push_back(std::make_unique<Value>(
137 VAL_BLOB, std::string(files[i].data.cbegin(), files[i].data.cend())));
Doug Zongkerc4351c72010-02-22 14:46:32 -0800138 }
Tao Baofada91c2016-10-27 18:16:06 -0700139 return applypatch(argv[1], argv[2], argv[3], target_size, sha1s, patches, &bonus);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800140}
Doug Zongker512536a2010-02-17 16:11:44 -0800141
142// This program applies binary patches to files in a way that is safe
143// (the original file is not touched until we have the desired
144// replacement for it) and idempotent (it's okay to run this program
145// multiple times).
146//
147// - if the sha1 hash of <tgt-file> is <tgt-sha1>, does nothing and exits
148// successfully.
149//
Tao Baoabba55b2015-07-17 18:11:12 -0700150// - otherwise, if no <src-sha1>:<patch> is provided, flashes <tgt-file> with
151// <src-file>. <tgt-file> must be a partition name, while <src-file> must
152// be a regular image file. <src-file> will not be deleted on success.
153//
Doug Zongker512536a2010-02-17 16:11:44 -0800154// - otherwise, if the sha1 hash of <src-file> is <src-sha1>, applies the
155// bsdiff <patch> to <src-file> to produce a new file (the type of patch
156// is automatically detected from the file header). If that new
157// file has sha1 hash <tgt-sha1>, moves it to replace <tgt-file>, and
158// exits successfully. Note that if <src-file> and <tgt-file> are
159// not the same, <src-file> is NOT deleted on success. <tgt-file>
160// may be the string "-" to mean "the same as src-file".
161//
162// - otherwise, or if any error is encountered, exits with non-zero
163// status.
164//
Elliott Hughes63a31922016-06-09 17:41:22 -0700165// <src-file> (or <file> in check mode) may refer to an EMMC partition
Doug Zongker512536a2010-02-17 16:11:44 -0800166// to read the source data. See the comments for the
Elliott Hughes63a31922016-06-09 17:41:22 -0700167// LoadPartitionContents() function for the format of such a filename.
Doug Zongker512536a2010-02-17 16:11:44 -0800168
169int main(int argc, char** argv) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800170 if (argc < 2) {
171 usage:
172 printf(
Doug Zongkera3ccba62012-08-20 15:28:02 -0700173 "usage: %s [-b <bonus-file>] <src-file> <tgt-file> <tgt-sha1> <tgt-size> "
Doug Zongker512536a2010-02-17 16:11:44 -0800174 "[<src-sha1>:<patch> ...]\n"
175 " or %s -c <file> [<sha1> ...]\n"
176 " or %s -s <bytes>\n"
177 " or %s -l\n"
178 "\n"
179 "Filenames may be of the form\n"
Elliott Hughes63a31922016-06-09 17:41:22 -0700180 " EMMC:<partition>:<len_1>:<sha1_1>:<len_2>:<sha1_2>:...\n"
181 "to specify reading from or writing to an EMMC partition.\n\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800182 argv[0], argv[0], argv[0], argv[0]);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800183 return 2;
184 }
185
186 int result;
187
188 if (strncmp(argv[1], "-l", 3) == 0) {
189 result = ShowLicenses();
190 } else if (strncmp(argv[1], "-c", 3) == 0) {
191 result = CheckMode(argc, argv);
192 } else if (strncmp(argv[1], "-s", 3) == 0) {
193 result = SpaceMode(argc, argv);
194 } else {
195 result = PatchMode(argc, argv);
196 }
197
198 if (result == 2) {
199 goto usage;
200 }
201 return result;
Doug Zongker512536a2010-02-17 16:11:44 -0800202}