blob: cf65f3f22e0ecad1b6e54c44a8d5a1183b8b3cee [file] [log] [blame]
Doug Zongker512536a2010-02-17 16:11:44 -08001/*
2 * Copyright (C) 2008 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#ifndef _APPLYPATCH_H
18#define _APPLYPATCH_H
19
Sen Jiang696692a2016-02-02 16:01:23 +080020#include <stdint.h>
Yabin Cuid483c202016-02-03 17:08:52 -080021
Tao Baoc0e1c462017-02-01 10:20:10 -080022#include <functional>
Tao Baofada91c2016-10-27 18:16:06 -070023#include <memory>
Tianjie Xuaced5d92016-10-12 10:55:04 -070024#include <string>
Yabin Cuid483c202016-02-03 17:08:52 -080025#include <vector>
26
Tao Baofada91c2016-10-27 18:16:06 -070027#include <openssl/sha.h>
28
Tao Bao38d78d12017-10-09 11:03:38 -070029// Forward declaration to avoid including "edify/expr.h" in the header.
30struct Value;
Doug Zongker512536a2010-02-17 16:11:44 -080031
Yabin Cuid6c93af2016-02-10 16:41:10 -080032struct FileContents {
Sen Jiangc48cb5e2016-02-04 16:23:21 +080033 uint8_t sha1[SHA_DIGEST_LENGTH];
Yabin Cuid6c93af2016-02-10 16:41:10 -080034 std::vector<unsigned char> data;
Yabin Cuid6c93af2016-02-10 16:41:10 -080035};
Doug Zongker512536a2010-02-17 16:11:44 -080036
Tao Baoc0e1c462017-02-01 10:20:10 -080037using SinkFn = std::function<size_t(const unsigned char*, size_t)>;
Doug Zongker512536a2010-02-17 16:11:44 -080038
Tao Baofada91c2016-10-27 18:16:06 -070039// applypatch.cpp
Tao Bao1e0941f2017-11-10 11:49:53 -080040
Doug Zongkerc4351c72010-02-22 14:46:32 -080041int ShowLicenses();
Tao Bao155771b2018-06-05 11:26:01 -070042
Tao Bao155771b2018-06-05 11:26:01 -070043// Parses a given string of 40 hex digits into 20-byte array 'digest'. 'str' may contain only the
44// digest or be of the form "<digest>:<anything>". Returns 0 on success, or -1 on any error.
Tao Bao8dc70492018-06-20 10:14:40 -070045int ParseSha1(const std::string& str, uint8_t* digest);
Doug Zongkerc4351c72010-02-22 14:46:32 -080046
Tao Bao155771b2018-06-05 11:26:01 -070047// Applies binary patches to eMMC target files in a way that is safe (the original file is not
48// touched until we have the desired replacement for it) and idempotent (it's okay to run this
49// program multiple times).
50//
51// - If the SHA-1 hash of 'target_filename' is 'target_sha1_string', does nothing and returns
52// successfully.
53//
54// - Otherwise, if the SHA-1 hash of 'source_filename' is one of the entries in 'patch_sha1s', the
55// corresponding patch from 'patch_data' (which must be a VAL_BLOB) is applied to produce a new
56// file (the type of patch is automatically detected from the blob data). If that new file has
57// SHA-1 hash 'target_sha1_str', moves it to replace 'target_filename', and exits successfully.
58// Note that if 'source_filename' and 'target_filename' are not the same, 'source_filename' is
59// NOT deleted on success. 'target_filename' may be the string "-" to mean
60// "the same as 'source_filename'".
61//
62// - Otherwise, or if any error is encountered, exits with non-zero status.
63//
64// 'source_filename' must refer to an eMMC partition to read the source data. See the comments for
65// the LoadPartitionContents() function for the format of such a filename. 'target_size' has become
66// obsolete since we have dropped the support for patching non-eMMC targets (eMMC targets have the
67// size embedded in the filename).
68int applypatch(const char* source_filename, const char* target_filename,
69 const char* target_sha1_str, size_t target_size,
70 const std::vector<std::string>& patch_sha1s,
71 const std::vector<std::unique_ptr<Value>>& patch_data, const Value* bonus_data);
72
Tao Bao7c1d4262018-07-06 23:18:14 -070073// Returns 0 if the contents of the eMMC target or the cached file match any of the given SHA-1's.
74// Returns nonzero otherwise. 'filename' must refer to an eMMC partition target. It would only use
75// 'sha1s' to find a match on /cache if the hashes embedded in the filename fail to match.
76int applypatch_check(const std::string& filename, const std::vector<std::string>& sha1s);
Tao Bao155771b2018-06-05 11:26:01 -070077
Tao Baod34e6bc2018-07-13 13:11:09 -070078// Flashes a given image to the eMMC target partition. It verifies the target cheksum first, and
79// will return if target already has the desired hash. Otherwise it checks the checksum of the
80// given source image before flashing, and verifies the target partition afterwards.
81// 'target_filename' must refer to an eMMC partition, of the form "EMMC:<device>:<size>:<hash>".
82// The function is idempotent. Returns zero on success.
Tao Baofada91c2016-10-27 18:16:06 -070083int applypatch_flash(const char* source_filename, const char* target_filename,
84 const char* target_sha1_str, size_t target_size);
Doug Zongker512536a2010-02-17 16:11:44 -080085
Tao Bao155771b2018-06-05 11:26:01 -070086// Reads a file into memory; stores the file contents and associated metadata in *file. Returns 0
87// on success, or -1 on error.
Tao Bao8dc70492018-06-20 10:14:40 -070088int LoadFileContents(const std::string& filename, FileContents* file);
Tao Bao155771b2018-06-05 11:26:01 -070089
90// Saves the given FileContents object to the given filename. Returns 0 on success, or -1 on error.
Tao Bao8dc70492018-06-20 10:14:40 -070091int SaveFileContents(const std::string& filename, const FileContents* file);
Doug Zongker512536a2010-02-17 16:11:44 -080092
Tao Baoc8e79342016-12-28 10:11:22 -080093// bspatch.cpp
Tao Bao1e0941f2017-11-10 11:49:53 -080094
Doug Zongker512536a2010-02-17 16:11:44 -080095void ShowBSDiffLicense();
Tao Bao1e0941f2017-11-10 11:49:53 -080096
97// Applies the bsdiff-patch given in 'patch' (from offset 'patch_offset' to the end) to the source
Tao Bao8b0b0f12018-04-19 21:02:13 -070098// data given by (old_data, old_size). Writes the patched output through the given 'sink'. Returns
99// 0 on success.
Tao Bao1e0941f2017-11-10 11:49:53 -0800100int ApplyBSDiffPatch(const unsigned char* old_data, size_t old_size, const Value& patch,
Tao Bao8b0b0f12018-04-19 21:02:13 -0700101 size_t patch_offset, SinkFn sink);
Doug Zongker512536a2010-02-17 16:11:44 -0800102
Yabin Cuid483c202016-02-03 17:08:52 -0800103// imgpatch.cpp
Tao Bao1e0941f2017-11-10 11:49:53 -0800104
105// Applies the imgdiff-patch given in 'patch' to the source data given by (old_data, old_size), with
Tao Bao8b0b0f12018-04-19 21:02:13 -0700106// the optional bonus data. Writes the patched output through the given 'sink'. Returns 0 on
107// success.
Tao Bao1e0941f2017-11-10 11:49:53 -0800108int ApplyImagePatch(const unsigned char* old_data, size_t old_size, const Value& patch, SinkFn sink,
Tao Bao8b0b0f12018-04-19 21:02:13 -0700109 const Value* bonus_data);
Doug Zongker512536a2010-02-17 16:11:44 -0800110
Yabin Cuid483c202016-02-03 17:08:52 -0800111// freecache.cpp
Tao Bao1e0941f2017-11-10 11:49:53 -0800112
Tao Bao5ee25662018-07-11 15:55:32 -0700113// Checks whether /cache partition has at least 'bytes'-byte free space. Returns true immediately
114// if so. Otherwise, it will try to free some space by removing older logs, checks again and
115// returns the checking result.
116bool CheckAndFreeSpaceOnCache(size_t bytes);
Tao Bao155771b2018-06-05 11:26:01 -0700117
Tao Bao49750f12018-07-11 16:32:10 -0700118// Removes the files in |dirname| until we have at least |bytes_needed| bytes of free space on the
119// partition. |space_checker| should return the size of the free space, or -1 on error.
Tianjie Xud5fbcc12018-04-11 14:38:09 -0700120bool RemoveFilesInDirectory(size_t bytes_needed, const std::string& dirname,
Tao Bao49750f12018-07-11 16:32:10 -0700121 const std::function<int64_t(const std::string&)>& space_checker);
Doug Zongker512536a2010-02-17 16:11:44 -0800122#endif