blob: 6fc6f0fc91f16fa5df0f948a03295cdfcd85f3d0 [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.
76// An optional arg 'bonus' can be provided, if the patch was generated with a bonus output.
77// Returns the patching result.
78bool PatchPartition(const Partition& target, const Partition& source, const Value& patch,
79 const Value* bonus);
80
81// Returns whether the contents of the eMMC target or the cached file match the embedded hash.
82// It will look for the backup on /cache if the given partition doesn't match the checksum.
83bool PatchPartitionCheck(const Partition& target, const Partition& source);
84
85// Checks whether the contents of the given partition has the desired hash. It will NOT look for
86// the backup on /cache if the given partition doesn't have the expected checksum.
87bool CheckPartition(const Partition& target);
88
89// Flashes a given image in 'source_filename' to the eMMC target partition. It verifies the target
90// checksum first, and will return if target already has the desired hash. Otherwise it checks the
91// checksum of the given source image, flashes, and verifies the target partition afterwards. The
92// function is idempotent. Returns the flashing result.
93bool FlashPartition(const Partition& target, const std::string& source_filename);
Doug Zongker512536a2010-02-17 16:11:44 -080094
Tao Bao09e84932018-08-31 11:25:05 -070095// Reads a file into memory; stores the file contents and associated metadata in *file.
96bool LoadFileContents(const std::string& filename, FileContents* file);
Tao Bao155771b2018-06-05 11:26:01 -070097
Tao Bao09e84932018-08-31 11:25:05 -070098// Saves the given FileContents object to the given filename.
99bool SaveFileContents(const std::string& filename, const FileContents* file);
Doug Zongker512536a2010-02-17 16:11:44 -0800100
Tao Baoc8e79342016-12-28 10:11:22 -0800101// bspatch.cpp
Tao Bao1e0941f2017-11-10 11:49:53 -0800102
Doug Zongker512536a2010-02-17 16:11:44 -0800103void ShowBSDiffLicense();
Tao Bao1e0941f2017-11-10 11:49:53 -0800104
105// Applies the bsdiff-patch given in 'patch' (from offset 'patch_offset' to the end) to the source
Tao Bao8b0b0f12018-04-19 21:02:13 -0700106// data given by (old_data, old_size). Writes the patched output through the given 'sink'. Returns
107// 0 on success.
Tao Bao1e0941f2017-11-10 11:49:53 -0800108int ApplyBSDiffPatch(const unsigned char* old_data, size_t old_size, const Value& patch,
Tao Bao8b0b0f12018-04-19 21:02:13 -0700109 size_t patch_offset, SinkFn sink);
Doug Zongker512536a2010-02-17 16:11:44 -0800110
Yabin Cuid483c202016-02-03 17:08:52 -0800111// imgpatch.cpp
Tao Bao1e0941f2017-11-10 11:49:53 -0800112
113// 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 -0700114// the optional bonus data. Writes the patched output through the given 'sink'. Returns 0 on
115// success.
Tao Bao1e0941f2017-11-10 11:49:53 -0800116int ApplyImagePatch(const unsigned char* old_data, size_t old_size, const Value& patch, SinkFn sink,
Tao Bao8b0b0f12018-04-19 21:02:13 -0700117 const Value* bonus_data);
Doug Zongker512536a2010-02-17 16:11:44 -0800118
Yabin Cuid483c202016-02-03 17:08:52 -0800119// freecache.cpp
Tao Bao1e0941f2017-11-10 11:49:53 -0800120
Tao Bao5ee25662018-07-11 15:55:32 -0700121// Checks whether /cache partition has at least 'bytes'-byte free space. Returns true immediately
122// if so. Otherwise, it will try to free some space by removing older logs, checks again and
123// returns the checking result.
124bool CheckAndFreeSpaceOnCache(size_t bytes);
Tao Bao155771b2018-06-05 11:26:01 -0700125
Tao Bao49750f12018-07-11 16:32:10 -0700126// Removes the files in |dirname| until we have at least |bytes_needed| bytes of free space on the
127// partition. |space_checker| should return the size of the free space, or -1 on error.
Tianjie Xud5fbcc12018-04-11 14:38:09 -0700128bool RemoveFilesInDirectory(size_t bytes_needed, const std::string& dirname,
Tao Bao49750f12018-07-11 16:32:10 -0700129 const std::function<int64_t(const std::string&)>& space_checker);
Doug Zongker512536a2010-02-17 16:11:44 -0800130#endif