blob: 7b191a801a37f3e756040f697bb725ec59586698 [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 -070047static int SpaceMode(int argc, const char** argv) {
Doug Zongkerc4351c72010-02-22 14:46:32 -080048 if (argc != 3) {
49 return 2;
50 }
Tao Bao36c35112016-10-25 14:17:26 -070051
52 size_t bytes;
53 if (!android::base::ParseUint(argv[2], &bytes) || bytes == 0) {
Doug Zongkerc4351c72010-02-22 14:46:32 -080054 printf("can't parse \"%s\" as byte count\n\n", argv[2]);
55 return 1;
56 }
57 return CacheSizeCheck(bytes);
58}
59
Tao Bao36c35112016-10-25 14:17:26 -070060// Parse arguments (which should be of the form "<sha1>:<filename>" into the
61// new parallel arrays *sha1s and *files. Returns true on success.
62static bool ParsePatchArgs(int argc, const char** argv, std::vector<std::string>* sha1s,
Yabin Cuid6c93af2016-02-10 16:41:10 -080063 std::vector<FileContents>* files) {
Tianjie Xuaced5d92016-10-12 10:55:04 -070064 if (sha1s == nullptr) {
65 return false;
66 }
Yabin Cuid483c202016-02-03 17:08:52 -080067 for (int i = 0; i < argc; ++i) {
Tao Bao36c35112016-10-25 14:17:26 -070068 std::vector<std::string> pieces = android::base::Split(argv[i], ":");
69 if (pieces.size() != 2) {
70 printf("failed to parse patch argument \"%s\"\n", argv[i]);
Yabin Cuid6c93af2016-02-10 16:41:10 -080071 return false;
Doug Zongkerc4351c72010-02-22 14:46:32 -080072 }
Tao Bao36c35112016-10-25 14:17:26 -070073
74 uint8_t digest[SHA_DIGEST_LENGTH];
75 if (ParseSha1(pieces[0].c_str(), digest) != 0) {
Doug Zongkerc4351c72010-02-22 14:46:32 -080076 printf("failed to parse sha1 \"%s\"\n", argv[i]);
Tao Baoba9a42a2015-06-23 23:23:33 -070077 return false;
Doug Zongkerc4351c72010-02-22 14:46:32 -080078 }
79
Tao Bao36c35112016-10-25 14:17:26 -070080 sha1s->push_back(pieces[0]);
Yabin Cuid6c93af2016-02-10 16:41:10 -080081 FileContents fc;
Tao Bao36c35112016-10-25 14:17:26 -070082 if (LoadFileContents(pieces[1].c_str(), &fc) != 0) {
Yabin Cuid6c93af2016-02-10 16:41:10 -080083 return false;
Doug Zongkerc4351c72010-02-22 14:46:32 -080084 }
Yabin Cuid6c93af2016-02-10 16:41:10 -080085 files->push_back(std::move(fc));
Doug Zongkerc4351c72010-02-22 14:46:32 -080086 }
Tao Baoba9a42a2015-06-23 23:23:33 -070087 return true;
Doug Zongkerc4351c72010-02-22 14:46:32 -080088}
89
Tao Baoabba55b2015-07-17 18:11:12 -070090static int FlashMode(const char* src_filename, const char* tgt_filename,
91 const char* tgt_sha1, size_t tgt_size) {
92 return applypatch_flash(src_filename, tgt_filename, tgt_sha1, tgt_size);
93}
94
Tao Bao36c35112016-10-25 14:17:26 -070095static int PatchMode(int argc, const char** argv) {
Yabin Cuid6c93af2016-02-10 16:41:10 -080096 FileContents bonusFc;
Tianjie Xuaced5d92016-10-12 10:55:04 -070097 Value bonus(VAL_INVALID, "");
Yabin Cuid6c93af2016-02-10 16:41:10 -080098
Doug Zongkera3ccba62012-08-20 15:28:02 -070099 if (argc >= 3 && strcmp(argv[1], "-b") == 0) {
Yabin Cuid6c93af2016-02-10 16:41:10 -0800100 if (LoadFileContents(argv[2], &bonusFc) != 0) {
Doug Zongkera3ccba62012-08-20 15:28:02 -0700101 printf("failed to load bonus file %s\n", argv[2]);
102 return 1;
103 }
Tianjie Xuaced5d92016-10-12 10:55:04 -0700104 bonus.type = VAL_BLOB;
Tao Baoedf1b152016-10-24 16:08:28 -0700105 bonus.data = std::string(bonusFc.data.cbegin(), bonusFc.data.cend());
Doug Zongkera3ccba62012-08-20 15:28:02 -0700106 argc -= 2;
107 argv += 2;
108 }
109
Tao Baoabba55b2015-07-17 18:11:12 -0700110 if (argc < 4) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800111 return 2;
112 }
113
Tao Bao36c35112016-10-25 14:17:26 -0700114 size_t target_size;
115 if (!android::base::ParseUint(argv[4], &target_size) || target_size == 0) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800116 printf("can't parse \"%s\" as byte count\n\n", argv[4]);
117 return 1;
118 }
119
Tao Baoabba55b2015-07-17 18:11:12 -0700120 // If no <src-sha1>:<patch> is provided, it is in flash mode.
121 if (argc == 5) {
Tianjie Xuaced5d92016-10-12 10:55:04 -0700122 if (bonus.type != VAL_INVALID) {
Tao Baoabba55b2015-07-17 18:11:12 -0700123 printf("bonus file not supported in flash mode\n");
124 return 1;
125 }
126 return FlashMode(argv[1], argv[2], argv[3], target_size);
127 }
Tao Bao36c35112016-10-25 14:17:26 -0700128
Tianjie Xuaced5d92016-10-12 10:55:04 -0700129 std::vector<std::string> sha1s;
Yabin Cuid6c93af2016-02-10 16:41:10 -0800130 std::vector<FileContents> files;
131 if (!ParsePatchArgs(argc-5, argv+5, &sha1s, &files)) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800132 printf("failed to parse patch args\n");
133 return 1;
134 }
Tao Baofada91c2016-10-27 18:16:06 -0700135
136 std::vector<std::unique_ptr<Value>> patches;
Yabin Cuid6c93af2016-02-10 16:41:10 -0800137 for (size_t i = 0; i < files.size(); ++i) {
Tao Baofada91c2016-10-27 18:16:06 -0700138 patches.push_back(std::make_unique<Value>(
139 VAL_BLOB, std::string(files[i].data.cbegin(), files[i].data.cend())));
Doug Zongkerc4351c72010-02-22 14:46:32 -0800140 }
Tao Baofada91c2016-10-27 18:16:06 -0700141 return applypatch(argv[1], argv[2], argv[3], target_size, sha1s, patches, &bonus);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800142}
Doug Zongker512536a2010-02-17 16:11:44 -0800143
Tao Bao36c35112016-10-25 14:17:26 -0700144// This program (applypatch) applies binary patches to files in a way that
145// is safe (the original file is not touched until we have the desired
Doug Zongker512536a2010-02-17 16:11:44 -0800146// replacement for it) and idempotent (it's okay to run this program
147// multiple times).
148//
149// - if the sha1 hash of <tgt-file> is <tgt-sha1>, does nothing and exits
150// successfully.
151//
Tao Baoabba55b2015-07-17 18:11:12 -0700152// - otherwise, if no <src-sha1>:<patch> is provided, flashes <tgt-file> with
153// <src-file>. <tgt-file> must be a partition name, while <src-file> must
154// be a regular image file. <src-file> will not be deleted on success.
155//
Doug Zongker512536a2010-02-17 16:11:44 -0800156// - otherwise, if the sha1 hash of <src-file> is <src-sha1>, applies the
157// bsdiff <patch> to <src-file> to produce a new file (the type of patch
158// is automatically detected from the file header). If that new
159// file has sha1 hash <tgt-sha1>, moves it to replace <tgt-file>, and
160// exits successfully. Note that if <src-file> and <tgt-file> are
161// not the same, <src-file> is NOT deleted on success. <tgt-file>
162// may be the string "-" to mean "the same as src-file".
163//
164// - otherwise, or if any error is encountered, exits with non-zero
165// status.
166//
Elliott Hughes63a31922016-06-09 17:41:22 -0700167// <src-file> (or <file> in check mode) may refer to an EMMC partition
Doug Zongker512536a2010-02-17 16:11:44 -0800168// to read the source data. See the comments for the
Elliott Hughes63a31922016-06-09 17:41:22 -0700169// LoadPartitionContents() function for the format of such a filename.
Doug Zongker512536a2010-02-17 16:11:44 -0800170
Tao Bao36c35112016-10-25 14:17:26 -0700171int applypatch_modes(int argc, const char** argv) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800172 if (argc < 2) {
173 usage:
174 printf(
Doug Zongkera3ccba62012-08-20 15:28:02 -0700175 "usage: %s [-b <bonus-file>] <src-file> <tgt-file> <tgt-sha1> <tgt-size> "
Doug Zongker512536a2010-02-17 16:11:44 -0800176 "[<src-sha1>:<patch> ...]\n"
177 " or %s -c <file> [<sha1> ...]\n"
178 " or %s -s <bytes>\n"
179 " or %s -l\n"
180 "\n"
181 "Filenames may be of the form\n"
Elliott Hughes63a31922016-06-09 17:41:22 -0700182 " EMMC:<partition>:<len_1>:<sha1_1>:<len_2>:<sha1_2>:...\n"
183 "to specify reading from or writing to an EMMC partition.\n\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800184 argv[0], argv[0], argv[0], argv[0]);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800185 return 2;
186 }
187
188 int result;
189
190 if (strncmp(argv[1], "-l", 3) == 0) {
191 result = ShowLicenses();
192 } else if (strncmp(argv[1], "-c", 3) == 0) {
193 result = CheckMode(argc, argv);
194 } else if (strncmp(argv[1], "-s", 3) == 0) {
195 result = SpaceMode(argc, argv);
196 } else {
197 result = PatchMode(argc, argv);
198 }
199
200 if (result == 2) {
201 goto usage;
202 }
203 return result;
Doug Zongker512536a2010-02-17 16:11:44 -0800204}