Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <inttypes.h> |
| 4 | #include <stdlib.h> |
| 5 | |
| 6 | #include <string> |
David Anderson | aa87dc5 | 2023-01-27 20:39:06 -0800 | [diff] [blame] | 7 | #include <vector> |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 8 | |
Dmitrii Merkurev | 0ee285a | 2023-02-11 00:02:22 +0000 | [diff] [blame] | 9 | #include <android-base/logging.h> |
| 10 | #include <android-base/result.h> |
David Anderson | aa87dc5 | 2023-01-27 20:39:06 -0800 | [diff] [blame] | 11 | #include <android-base/unique_fd.h> |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 12 | #include <bootimg.h> |
David Anderson | aa87dc5 | 2023-01-27 20:39:06 -0800 | [diff] [blame] | 13 | #include <liblp/liblp.h> |
| 14 | #include <sparse/sparse.h> |
| 15 | |
Dmitrii Merkurev | 0ee285a | 2023-02-11 00:02:22 +0000 | [diff] [blame] | 16 | using android::base::ErrnoError; |
| 17 | using android::base::Error; |
| 18 | using android::base::Result; |
| 19 | using android::base::ResultError; |
| 20 | |
Dmitrii Merkurev | 4785554 | 2023-02-28 21:02:48 +0000 | [diff] [blame] | 21 | template <typename T, typename U> |
| 22 | inline T Expect(Result<T, U> r) { |
| 23 | if (r.ok()) { |
| 24 | return r.value(); |
| 25 | } |
| 26 | |
| 27 | LOG(FATAL) << r.error().message(); |
| 28 | |
| 29 | return r.value(); |
| 30 | } |
Dmitrii Merkurev | 0ee285a | 2023-02-11 00:02:22 +0000 | [diff] [blame] | 31 | |
David Anderson | aa87dc5 | 2023-01-27 20:39:06 -0800 | [diff] [blame] | 32 | using SparsePtr = std::unique_ptr<sparse_file, decltype(&sparse_file_destroy)>; |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 33 | |
| 34 | /* util stuff */ |
| 35 | double now(); |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 36 | void set_verbose(); |
| 37 | |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 38 | // These printf-like functions are implemented in terms of vsnprintf, so they |
| 39 | // use the same attribute for compile-time format string checking. |
| 40 | void die(const char* fmt, ...) __attribute__((__noreturn__)) |
| 41 | __attribute__((__format__(__printf__, 1, 2))); |
David Anderson | 7c84b9f | 2019-06-27 22:15:29 -0700 | [diff] [blame] | 42 | |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 43 | void verbose(const char* fmt, ...) __attribute__((__format__(__printf__, 1, 2))); |
David Anderson | 7c84b9f | 2019-06-27 22:15:29 -0700 | [diff] [blame] | 44 | |
| 45 | void die(const std::string& str) __attribute__((__noreturn__)); |
David Anderson | aa87dc5 | 2023-01-27 20:39:06 -0800 | [diff] [blame] | 46 | |
| 47 | bool should_flash_in_userspace(const android::fs_mgr::LpMetadata& metadata, |
| 48 | const std::string& partition_name); |
| 49 | bool is_sparse_file(android::base::borrowed_fd fd); |
| 50 | int64_t get_file_size(android::base::borrowed_fd fd); |
| 51 | |
| 52 | class ImageSource { |
| 53 | public: |
| 54 | virtual ~ImageSource(){}; |
| 55 | virtual bool ReadFile(const std::string& name, std::vector<char>* out) const = 0; |
| 56 | virtual android::base::unique_fd OpenFile(const std::string& name) const = 0; |
| 57 | }; |