blob: 799f4b2d7959722229e10d1d9e8bda335e310c27 [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>
Tao Bao5609bc82018-06-20 00:30:48 -070024#include <ostream>
Tianjie Xuaced5d92016-10-12 10:55:04 -070025#include <string>
Yabin Cuid483c202016-02-03 17:08:52 -080026#include <vector>
27
Tao Baofada91c2016-10-27 18:16:06 -070028#include <openssl/sha.h>
29
Tao Bao38d78d12017-10-09 11:03:38 -070030// Forward declaration to avoid including "edify/expr.h" in the header.
31struct Value;
Doug Zongker512536a2010-02-17 16:11:44 -080032
Yabin Cuid6c93af2016-02-10 16:41:10 -080033struct FileContents {
Sen Jiangc48cb5e2016-02-04 16:23:21 +080034 uint8_t sha1[SHA_DIGEST_LENGTH];
Yabin Cuid6c93af2016-02-10 16:41:10 -080035 std::vector<unsigned char> data;
Yabin Cuid6c93af2016-02-10 16:41:10 -080036};
Doug Zongker512536a2010-02-17 16:11:44 -080037
Tao Baoc0e1c462017-02-01 10:20:10 -080038using SinkFn = std::function<size_t(const unsigned char*, size_t)>;
Doug Zongker512536a2010-02-17 16:11:44 -080039
Tao Baofada91c2016-10-27 18:16:06 -070040// applypatch.cpp
Tao Bao1e0941f2017-11-10 11:49:53 -080041
Doug Zongkerc4351c72010-02-22 14:46:32 -080042int ShowLicenses();
Tao Bao155771b2018-06-05 11:26:01 -070043
Tao Bao155771b2018-06-05 11:26:01 -070044// Parses a given string of 40 hex digits into 20-byte array 'digest'. 'str' may contain only the
45// digest or be of the form "<digest>:<anything>". Returns 0 on success, or -1 on any error.
Tao Bao8dc70492018-06-20 10:14:40 -070046int ParseSha1(const std::string& str, uint8_t* digest);
Doug Zongkerc4351c72010-02-22 14:46:32 -080047
Tao Bao5609bc82018-06-20 00:30:48 -070048struct Partition {
49 Partition() = default;
Tao Bao155771b2018-06-05 11:26:01 -070050
Tao Bao5609bc82018-06-20 00:30:48 -070051 Partition(const std::string& name, size_t size, const std::string& hash)
52 : name(name), size(size), hash(hash) {}
Tao Bao155771b2018-06-05 11:26:01 -070053
Tao Bao5609bc82018-06-20 00:30:48 -070054 // Parses and returns the given string into a Partition object. The input string is of the form
55 // "EMMC:<device>:<size>:<hash>". Returns the parsed Partition, or an empty object on error.
56 static Partition Parse(const std::string& partition, std::string* err);
57
58 std::string ToString() const;
59
60 // Returns whether the current Partition object is valid.
61 explicit operator bool() const {
62 return !name.empty();
63 }
64
65 std::string name;
66 size_t size;
67 std::string hash;
68};
69
70std::ostream& operator<<(std::ostream& os, const Partition& partition);
71
72// Applies the given 'patch' to the 'source' Partition, verifies then writes the patching result to
73// the 'target' Partition. While patching, it will backup the data on the source partition to
74// /cache, so that the patching could be resumed on interruption even if both of the source and
75// target partitions refer to the same device. The function is idempotent if called multiple times.
Tao Bao5234ad42019-09-23 10:28:54 -070076// 'bonus' can be provided if the patch was generated with a bonus output, or nullptr.
77// 'backup_source' indicates whether the source partition should be backed up prior to the update
78// (e.g. when doing in-place update). Returns the patching result.
Tao Bao5609bc82018-06-20 00:30:48 -070079bool PatchPartition(const Partition& target, const Partition& source, const Value& patch,
Tao Bao5234ad42019-09-23 10:28:54 -070080 const Value* bonus, bool backup_source);
Tao Bao5609bc82018-06-20 00:30:48 -070081
82// Returns whether the contents of the eMMC target or the cached file match the embedded hash.
83// It will look for the backup on /cache if the given partition doesn't match the checksum.
84bool PatchPartitionCheck(const Partition& target, const Partition& source);
85
86// Checks whether the contents of the given partition has the desired hash. It will NOT look for
87// the backup on /cache if the given partition doesn't have the expected checksum.
88bool CheckPartition(const Partition& target);
89
90// Flashes a given image in 'source_filename' to the eMMC target partition. It verifies the target
91// checksum first, and will return if target already has the desired hash. Otherwise it checks the
92// checksum of the given source image, flashes, and verifies the target partition afterwards. The
93// function is idempotent. Returns the flashing result.
94bool FlashPartition(const Partition& target, const std::string& source_filename);
Doug Zongker512536a2010-02-17 16:11:44 -080095
Tao Bao09e84932018-08-31 11:25:05 -070096// Reads a file into memory; stores the file contents and associated metadata in *file.
97bool LoadFileContents(const std::string& filename, FileContents* file);
Tao Bao155771b2018-06-05 11:26:01 -070098
Tao Bao09e84932018-08-31 11:25:05 -070099// Saves the given FileContents object to the given filename.
100bool SaveFileContents(const std::string& filename, const FileContents* file);
Doug Zongker512536a2010-02-17 16:11:44 -0800101
Tao Baoc8e79342016-12-28 10:11:22 -0800102// bspatch.cpp
Tao Bao1e0941f2017-11-10 11:49:53 -0800103
Doug Zongker512536a2010-02-17 16:11:44 -0800104void ShowBSDiffLicense();
Tao Bao1e0941f2017-11-10 11:49:53 -0800105
106// Applies the bsdiff-patch given in 'patch' (from offset 'patch_offset' to the end) to the source
Tao Bao8b0b0f12018-04-19 21:02:13 -0700107// data given by (old_data, old_size). Writes the patched output through the given 'sink'. Returns
108// 0 on success.
Tao Bao1e0941f2017-11-10 11:49:53 -0800109int ApplyBSDiffPatch(const unsigned char* old_data, size_t old_size, const Value& patch,
Tao Bao8b0b0f12018-04-19 21:02:13 -0700110 size_t patch_offset, SinkFn sink);
Doug Zongker512536a2010-02-17 16:11:44 -0800111
Yabin Cuid483c202016-02-03 17:08:52 -0800112// imgpatch.cpp
Tao Bao1e0941f2017-11-10 11:49:53 -0800113
114// 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 -0700115// the optional bonus data. Writes the patched output through the given 'sink'. Returns 0 on
116// success.
Tao Bao1e0941f2017-11-10 11:49:53 -0800117int ApplyImagePatch(const unsigned char* old_data, size_t old_size, const Value& patch, SinkFn sink,
Tao Bao8b0b0f12018-04-19 21:02:13 -0700118 const Value* bonus_data);
Doug Zongker512536a2010-02-17 16:11:44 -0800119
Yabin Cuid483c202016-02-03 17:08:52 -0800120// freecache.cpp
Tao Bao1e0941f2017-11-10 11:49:53 -0800121
Tao Bao5ee25662018-07-11 15:55:32 -0700122// Checks whether /cache partition has at least 'bytes'-byte free space. Returns true immediately
123// if so. Otherwise, it will try to free some space by removing older logs, checks again and
124// returns the checking result.
125bool CheckAndFreeSpaceOnCache(size_t bytes);
Tao Bao155771b2018-06-05 11:26:01 -0700126
Tao Bao49750f12018-07-11 16:32:10 -0700127// Removes the files in |dirname| until we have at least |bytes_needed| bytes of free space on the
128// partition. |space_checker| should return the size of the free space, or -1 on error.
Tianjie Xud5fbcc12018-04-11 14:38:09 -0700129bool RemoveFilesInDirectory(size_t bytes_needed, const std::string& dirname,
Tao Bao49750f12018-07-11 16:32:10 -0700130 const std::function<int64_t(const std::string&)>& space_checker);
Doug Zongker512536a2010-02-17 16:11:44 -0800131#endif