blob: aa32d57ef48deb84d8ad16c13ed89fbff0010ea6 [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
Tao Bao36c35112016-10-25 14:17:26 -070017#include "applypatch_modes.h"
18
Doug Zongker512536a2010-02-17 16:11:44 -080019#include <stdio.h>
Doug Zongkerc4351c72010-02-22 14:46:32 -080020#include <stdlib.h>
21#include <string.h>
22#include <unistd.h>
Doug Zongker512536a2010-02-17 16:11:44 -080023
Yabin Cuid483c202016-02-03 17:08:52 -080024#include <memory>
Tianjie Xuaced5d92016-10-12 10:55:04 -070025#include <string>
Yabin Cuid483c202016-02-03 17:08:52 -080026#include <vector>
27
Tao Bao36c35112016-10-25 14:17:26 -070028#include <android-base/parseint.h>
29#include <android-base/strings.h>
Tao Baofada91c2016-10-27 18:16:06 -070030#include <openssl/sha.h>
31
Tao Baod80a9982016-03-03 11:43:47 -080032#include "applypatch/applypatch.h"
Doug Zongkerc4351c72010-02-22 14:46:32 -080033#include "edify/expr.h"
Doug Zongkerc4351c72010-02-22 14:46:32 -080034
Tao Bao36c35112016-10-25 14:17:26 -070035static int CheckMode(int argc, const char** argv) {
Doug Zongkerc4351c72010-02-22 14:46:32 -080036 if (argc < 3) {
37 return 2;
38 }
Tianjie Xuaced5d92016-10-12 10:55:04 -070039 std::vector<std::string> sha1;
40 for (int i = 3; i < argc; i++) {
41 sha1.push_back(argv[i]);
42 }
43
44 return applypatch_check(argv[2], sha1);
Doug Zongkerc4351c72010-02-22 14:46:32 -080045}
46
Tao Bao36c35112016-10-25 14:17:26 -070047// Parse arguments (which should be of the form "<sha1>:<filename>" into the
48// new parallel arrays *sha1s and *files. Returns true on success.
49static bool ParsePatchArgs(int argc, const char** argv, std::vector<std::string>* sha1s,
Yabin Cuid6c93af2016-02-10 16:41:10 -080050 std::vector<FileContents>* files) {
Tianjie Xuaced5d92016-10-12 10:55:04 -070051 if (sha1s == nullptr) {
52 return false;
53 }
Yabin Cuid483c202016-02-03 17:08:52 -080054 for (int i = 0; i < argc; ++i) {
Tao Bao36c35112016-10-25 14:17:26 -070055 std::vector<std::string> pieces = android::base::Split(argv[i], ":");
56 if (pieces.size() != 2) {
57 printf("failed to parse patch argument \"%s\"\n", argv[i]);
Yabin Cuid6c93af2016-02-10 16:41:10 -080058 return false;
Doug Zongkerc4351c72010-02-22 14:46:32 -080059 }
Tao Bao36c35112016-10-25 14:17:26 -070060
61 uint8_t digest[SHA_DIGEST_LENGTH];
62 if (ParseSha1(pieces[0].c_str(), digest) != 0) {
Doug Zongkerc4351c72010-02-22 14:46:32 -080063 printf("failed to parse sha1 \"%s\"\n", argv[i]);
Tao Baoba9a42a2015-06-23 23:23:33 -070064 return false;
Doug Zongkerc4351c72010-02-22 14:46:32 -080065 }
66
Tao Bao36c35112016-10-25 14:17:26 -070067 sha1s->push_back(pieces[0]);
Yabin Cuid6c93af2016-02-10 16:41:10 -080068 FileContents fc;
Tao Bao36c35112016-10-25 14:17:26 -070069 if (LoadFileContents(pieces[1].c_str(), &fc) != 0) {
Yabin Cuid6c93af2016-02-10 16:41:10 -080070 return false;
Doug Zongkerc4351c72010-02-22 14:46:32 -080071 }
Yabin Cuid6c93af2016-02-10 16:41:10 -080072 files->push_back(std::move(fc));
Doug Zongkerc4351c72010-02-22 14:46:32 -080073 }
Tao Baoba9a42a2015-06-23 23:23:33 -070074 return true;
Doug Zongkerc4351c72010-02-22 14:46:32 -080075}
76
Tao Baoabba55b2015-07-17 18:11:12 -070077static int FlashMode(const char* src_filename, const char* tgt_filename,
78 const char* tgt_sha1, size_t tgt_size) {
79 return applypatch_flash(src_filename, tgt_filename, tgt_sha1, tgt_size);
80}
81
Tao Bao36c35112016-10-25 14:17:26 -070082static int PatchMode(int argc, const char** argv) {
Yabin Cuid6c93af2016-02-10 16:41:10 -080083 FileContents bonusFc;
Tianjie Xuaced5d92016-10-12 10:55:04 -070084 Value bonus(VAL_INVALID, "");
Yabin Cuid6c93af2016-02-10 16:41:10 -080085
Doug Zongkera3ccba62012-08-20 15:28:02 -070086 if (argc >= 3 && strcmp(argv[1], "-b") == 0) {
Yabin Cuid6c93af2016-02-10 16:41:10 -080087 if (LoadFileContents(argv[2], &bonusFc) != 0) {
Doug Zongkera3ccba62012-08-20 15:28:02 -070088 printf("failed to load bonus file %s\n", argv[2]);
89 return 1;
90 }
Tianjie Xuaced5d92016-10-12 10:55:04 -070091 bonus.type = VAL_BLOB;
Tao Baoedf1b152016-10-24 16:08:28 -070092 bonus.data = std::string(bonusFc.data.cbegin(), bonusFc.data.cend());
Doug Zongkera3ccba62012-08-20 15:28:02 -070093 argc -= 2;
94 argv += 2;
95 }
96
Tao Baoabba55b2015-07-17 18:11:12 -070097 if (argc < 4) {
Doug Zongkerc4351c72010-02-22 14:46:32 -080098 return 2;
99 }
100
Tao Bao36c35112016-10-25 14:17:26 -0700101 size_t target_size;
102 if (!android::base::ParseUint(argv[4], &target_size) || target_size == 0) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800103 printf("can't parse \"%s\" as byte count\n\n", argv[4]);
104 return 1;
105 }
106
Tao Baoabba55b2015-07-17 18:11:12 -0700107 // If no <src-sha1>:<patch> is provided, it is in flash mode.
108 if (argc == 5) {
Tianjie Xuaced5d92016-10-12 10:55:04 -0700109 if (bonus.type != VAL_INVALID) {
Tao Baoabba55b2015-07-17 18:11:12 -0700110 printf("bonus file not supported in flash mode\n");
111 return 1;
112 }
113 return FlashMode(argv[1], argv[2], argv[3], target_size);
114 }
Tao Bao36c35112016-10-25 14:17:26 -0700115
Tianjie Xuaced5d92016-10-12 10:55:04 -0700116 std::vector<std::string> sha1s;
Yabin Cuid6c93af2016-02-10 16:41:10 -0800117 std::vector<FileContents> files;
118 if (!ParsePatchArgs(argc-5, argv+5, &sha1s, &files)) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800119 printf("failed to parse patch args\n");
120 return 1;
121 }
Tao Baofada91c2016-10-27 18:16:06 -0700122
123 std::vector<std::unique_ptr<Value>> patches;
Yabin Cuid6c93af2016-02-10 16:41:10 -0800124 for (size_t i = 0; i < files.size(); ++i) {
Tao Baofada91c2016-10-27 18:16:06 -0700125 patches.push_back(std::make_unique<Value>(
126 VAL_BLOB, std::string(files[i].data.cbegin(), files[i].data.cend())));
Doug Zongkerc4351c72010-02-22 14:46:32 -0800127 }
Tao Baofada91c2016-10-27 18:16:06 -0700128 return applypatch(argv[1], argv[2], argv[3], target_size, sha1s, patches, &bonus);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800129}
Doug Zongker512536a2010-02-17 16:11:44 -0800130
Tao Bao36c35112016-10-25 14:17:26 -0700131// This program (applypatch) applies binary patches to files in a way that
132// is safe (the original file is not touched until we have the desired
Doug Zongker512536a2010-02-17 16:11:44 -0800133// replacement for it) and idempotent (it's okay to run this program
134// multiple times).
135//
136// - if the sha1 hash of <tgt-file> is <tgt-sha1>, does nothing and exits
137// successfully.
138//
Tao Baoabba55b2015-07-17 18:11:12 -0700139// - otherwise, if no <src-sha1>:<patch> is provided, flashes <tgt-file> with
140// <src-file>. <tgt-file> must be a partition name, while <src-file> must
141// be a regular image file. <src-file> will not be deleted on success.
142//
Doug Zongker512536a2010-02-17 16:11:44 -0800143// - otherwise, if the sha1 hash of <src-file> is <src-sha1>, applies the
144// bsdiff <patch> to <src-file> to produce a new file (the type of patch
145// is automatically detected from the file header). If that new
146// file has sha1 hash <tgt-sha1>, moves it to replace <tgt-file>, and
147// exits successfully. Note that if <src-file> and <tgt-file> are
148// not the same, <src-file> is NOT deleted on success. <tgt-file>
149// may be the string "-" to mean "the same as src-file".
150//
151// - otherwise, or if any error is encountered, exits with non-zero
152// status.
153//
Elliott Hughes63a31922016-06-09 17:41:22 -0700154// <src-file> (or <file> in check mode) may refer to an EMMC partition
Doug Zongker512536a2010-02-17 16:11:44 -0800155// to read the source data. See the comments for the
Elliott Hughes63a31922016-06-09 17:41:22 -0700156// LoadPartitionContents() function for the format of such a filename.
Doug Zongker512536a2010-02-17 16:11:44 -0800157
Tao Bao36c35112016-10-25 14:17:26 -0700158int applypatch_modes(int argc, const char** argv) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800159 if (argc < 2) {
160 usage:
161 printf(
Doug Zongkera3ccba62012-08-20 15:28:02 -0700162 "usage: %s [-b <bonus-file>] <src-file> <tgt-file> <tgt-sha1> <tgt-size> "
Doug Zongker512536a2010-02-17 16:11:44 -0800163 "[<src-sha1>:<patch> ...]\n"
164 " or %s -c <file> [<sha1> ...]\n"
Doug Zongker512536a2010-02-17 16:11:44 -0800165 " or %s -l\n"
166 "\n"
167 "Filenames may be of the form\n"
Elliott Hughes63a31922016-06-09 17:41:22 -0700168 " EMMC:<partition>:<len_1>:<sha1_1>:<len_2>:<sha1_2>:...\n"
169 "to specify reading from or writing to an EMMC partition.\n\n",
Tao Bao8ab28082017-04-25 21:13:21 -0700170 argv[0], argv[0], argv[0]);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800171 return 2;
172 }
173
174 int result;
175
176 if (strncmp(argv[1], "-l", 3) == 0) {
177 result = ShowLicenses();
178 } else if (strncmp(argv[1], "-c", 3) == 0) {
179 result = CheckMode(argc, argv);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800180 } else {
181 result = PatchMode(argc, argv);
182 }
183
184 if (result == 2) {
185 goto usage;
186 }
187 return result;
Doug Zongker512536a2010-02-17 16:11:44 -0800188}