blob: 294f7ee21b6ecd188d28ca1ef9be6873f20ccf9a [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 Baod80a9982016-03-03 11:43:47 -080026#include "applypatch/applypatch.h"
Doug Zongkerc4351c72010-02-22 14:46:32 -080027#include "edify/expr.h"
Sen Jiangc48cb5e2016-02-04 16:23:21 +080028#include "openssl/sha.h"
Doug Zongkerc4351c72010-02-22 14:46:32 -080029
Tao Baoba9a42a2015-06-23 23:23:33 -070030static int CheckMode(int argc, char** argv) {
Doug Zongkerc4351c72010-02-22 14:46:32 -080031 if (argc < 3) {
32 return 2;
33 }
Tianjie Xuaced5d92016-10-12 10:55:04 -070034 std::vector<std::string> sha1;
35 for (int i = 3; i < argc; i++) {
36 sha1.push_back(argv[i]);
37 }
38
39 return applypatch_check(argv[2], sha1);
Doug Zongkerc4351c72010-02-22 14:46:32 -080040}
41
Tao Baoba9a42a2015-06-23 23:23:33 -070042static int SpaceMode(int argc, char** argv) {
Doug Zongkerc4351c72010-02-22 14:46:32 -080043 if (argc != 3) {
44 return 2;
45 }
46 char* endptr;
47 size_t bytes = strtol(argv[2], &endptr, 10);
48 if (bytes == 0 && endptr == argv[2]) {
49 printf("can't parse \"%s\" as byte count\n\n", argv[2]);
50 return 1;
51 }
52 return CacheSizeCheck(bytes);
53}
54
Yabin Cuid6c93af2016-02-10 16:41:10 -080055// Parse arguments (which should be of the form "<sha1>:<filename>"
56// into the new parallel arrays *sha1s and *files.Returns true on
Doug Zongkerc4351c72010-02-22 14:46:32 -080057// success.
Tianjie Xuaced5d92016-10-12 10:55:04 -070058static bool ParsePatchArgs(int argc, char** argv, std::vector<std::string>* sha1s,
Yabin Cuid6c93af2016-02-10 16:41:10 -080059 std::vector<FileContents>* files) {
Tianjie Xuaced5d92016-10-12 10:55:04 -070060 if (sha1s == nullptr) {
61 return false;
62 }
Yabin Cuid483c202016-02-03 17:08:52 -080063 for (int i = 0; i < argc; ++i) {
Tianjie Xuaced5d92016-10-12 10:55:04 -070064 uint8_t digest[SHA_DIGEST_LENGTH];
Doug Zongkerc4351c72010-02-22 14:46:32 -080065 char* colon = strchr(argv[i], ':');
Yabin Cuid6c93af2016-02-10 16:41:10 -080066 if (colon == nullptr) {
67 printf("no ':' in patch argument \"%s\"\n", argv[i]);
68 return false;
Doug Zongkerc4351c72010-02-22 14:46:32 -080069 }
Yabin Cuid6c93af2016-02-10 16:41:10 -080070 *colon = '\0';
71 ++colon;
Doug Zongkerc4351c72010-02-22 14:46:32 -080072 if (ParseSha1(argv[i], digest) != 0) {
73 printf("failed to parse sha1 \"%s\"\n", argv[i]);
Tao Baoba9a42a2015-06-23 23:23:33 -070074 return false;
Doug Zongkerc4351c72010-02-22 14:46:32 -080075 }
76
Yabin Cuid483c202016-02-03 17:08:52 -080077 sha1s->push_back(argv[i]);
Yabin Cuid6c93af2016-02-10 16:41:10 -080078 FileContents fc;
79 if (LoadFileContents(colon, &fc) != 0) {
80 return false;
Doug Zongkerc4351c72010-02-22 14:46:32 -080081 }
Yabin Cuid6c93af2016-02-10 16:41:10 -080082 files->push_back(std::move(fc));
Doug Zongkerc4351c72010-02-22 14:46:32 -080083 }
Tao Baoba9a42a2015-06-23 23:23:33 -070084 return true;
Doug Zongkerc4351c72010-02-22 14:46:32 -080085}
86
Tao Baoabba55b2015-07-17 18:11:12 -070087static int FlashMode(const char* src_filename, const char* tgt_filename,
88 const char* tgt_sha1, size_t tgt_size) {
89 return applypatch_flash(src_filename, tgt_filename, tgt_sha1, tgt_size);
90}
91
92static int PatchMode(int argc, char** argv) {
Yabin Cuid6c93af2016-02-10 16:41:10 -080093 FileContents bonusFc;
Tianjie Xuaced5d92016-10-12 10:55:04 -070094 Value bonus(VAL_INVALID, "");
Yabin Cuid6c93af2016-02-10 16:41:10 -080095
Doug Zongkera3ccba62012-08-20 15:28:02 -070096 if (argc >= 3 && strcmp(argv[1], "-b") == 0) {
Yabin Cuid6c93af2016-02-10 16:41:10 -080097 if (LoadFileContents(argv[2], &bonusFc) != 0) {
Doug Zongkera3ccba62012-08-20 15:28:02 -070098 printf("failed to load bonus file %s\n", argv[2]);
99 return 1;
100 }
Tianjie Xuaced5d92016-10-12 10:55:04 -0700101 bonus.type = VAL_BLOB;
Tao Baoedf1b152016-10-24 16:08:28 -0700102 bonus.data = std::string(bonusFc.data.cbegin(), bonusFc.data.cend());
Doug Zongkera3ccba62012-08-20 15:28:02 -0700103 argc -= 2;
104 argv += 2;
105 }
106
Tao Baoabba55b2015-07-17 18:11:12 -0700107 if (argc < 4) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800108 return 2;
109 }
110
111 char* endptr;
112 size_t target_size = strtol(argv[4], &endptr, 10);
113 if (target_size == 0 && endptr == argv[4]) {
114 printf("can't parse \"%s\" as byte count\n\n", argv[4]);
115 return 1;
116 }
117
Tao Baoabba55b2015-07-17 18:11:12 -0700118 // If no <src-sha1>:<patch> is provided, it is in flash mode.
119 if (argc == 5) {
Tianjie Xuaced5d92016-10-12 10:55:04 -0700120 if (bonus.type != VAL_INVALID) {
Tao Baoabba55b2015-07-17 18:11:12 -0700121 printf("bonus file not supported in flash mode\n");
122 return 1;
123 }
124 return FlashMode(argv[1], argv[2], argv[3], target_size);
125 }
Tianjie Xuaced5d92016-10-12 10:55:04 -0700126 std::vector<std::string> sha1s;
Yabin Cuid6c93af2016-02-10 16:41:10 -0800127 std::vector<FileContents> files;
128 if (!ParsePatchArgs(argc-5, argv+5, &sha1s, &files)) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800129 printf("failed to parse patch args\n");
130 return 1;
131 }
Tianjie Xuaced5d92016-10-12 10:55:04 -0700132 std::vector<Value> patches;
133 std::vector<Value*> patch_ptrs;
Yabin Cuid6c93af2016-02-10 16:41:10 -0800134 for (size_t i = 0; i < files.size(); ++i) {
Tao Baoedf1b152016-10-24 16:08:28 -0700135 patches.push_back(Value(VAL_BLOB,
136 std::string(files[i].data.cbegin(), files[i].data.cend())));
Tianjie Xuaced5d92016-10-12 10:55:04 -0700137 patch_ptrs.push_back(&patches[i]);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800138 }
Yabin Cuid483c202016-02-03 17:08:52 -0800139 return applypatch(argv[1], argv[2], argv[3], target_size,
Tianjie Xuaced5d92016-10-12 10:55:04 -0700140 sha1s, patch_ptrs.data(), &bonus);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800141}
Doug Zongker512536a2010-02-17 16:11:44 -0800142
143// This program applies binary patches to files in a way that is safe
144// (the original file is not touched until we have the desired
145// replacement for it) and idempotent (it's okay to run this program
146// multiple times).
147//
148// - if the sha1 hash of <tgt-file> is <tgt-sha1>, does nothing and exits
149// successfully.
150//
Tao Baoabba55b2015-07-17 18:11:12 -0700151// - otherwise, if no <src-sha1>:<patch> is provided, flashes <tgt-file> with
152// <src-file>. <tgt-file> must be a partition name, while <src-file> must
153// be a regular image file. <src-file> will not be deleted on success.
154//
Doug Zongker512536a2010-02-17 16:11:44 -0800155// - otherwise, if the sha1 hash of <src-file> is <src-sha1>, applies the
156// bsdiff <patch> to <src-file> to produce a new file (the type of patch
157// is automatically detected from the file header). If that new
158// file has sha1 hash <tgt-sha1>, moves it to replace <tgt-file>, and
159// exits successfully. Note that if <src-file> and <tgt-file> are
160// not the same, <src-file> is NOT deleted on success. <tgt-file>
161// may be the string "-" to mean "the same as src-file".
162//
163// - otherwise, or if any error is encountered, exits with non-zero
164// status.
165//
Elliott Hughes63a31922016-06-09 17:41:22 -0700166// <src-file> (or <file> in check mode) may refer to an EMMC partition
Doug Zongker512536a2010-02-17 16:11:44 -0800167// to read the source data. See the comments for the
Elliott Hughes63a31922016-06-09 17:41:22 -0700168// LoadPartitionContents() function for the format of such a filename.
Doug Zongker512536a2010-02-17 16:11:44 -0800169
170int main(int argc, char** argv) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800171 if (argc < 2) {
172 usage:
173 printf(
Doug Zongkera3ccba62012-08-20 15:28:02 -0700174 "usage: %s [-b <bonus-file>] <src-file> <tgt-file> <tgt-sha1> <tgt-size> "
Doug Zongker512536a2010-02-17 16:11:44 -0800175 "[<src-sha1>:<patch> ...]\n"
176 " or %s -c <file> [<sha1> ...]\n"
177 " or %s -s <bytes>\n"
178 " or %s -l\n"
179 "\n"
180 "Filenames may be of the form\n"
Elliott Hughes63a31922016-06-09 17:41:22 -0700181 " EMMC:<partition>:<len_1>:<sha1_1>:<len_2>:<sha1_2>:...\n"
182 "to specify reading from or writing to an EMMC partition.\n\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800183 argv[0], argv[0], argv[0], argv[0]);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800184 return 2;
185 }
186
187 int result;
188
189 if (strncmp(argv[1], "-l", 3) == 0) {
190 result = ShowLicenses();
191 } else if (strncmp(argv[1], "-c", 3) == 0) {
192 result = CheckMode(argc, argv);
193 } else if (strncmp(argv[1], "-s", 3) == 0) {
194 result = SpaceMode(argc, argv);
195 } else {
196 result = PatchMode(argc, argv);
197 }
198
199 if (result == 2) {
200 goto usage;
201 }
202 return result;
Doug Zongker512536a2010-02-17 16:11:44 -0800203}