bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1 | /* |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame] | 2 | TWRP is free software: you can redistribute it and/or modify |
| 3 | it under the terms of the GNU General Public License as published by |
| 4 | the Free Software Foundation, either version 3 of the License, or |
| 5 | (at your option) any later version. |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 6 | |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame] | 7 | TWRP is distributed in the hope that it will be useful, |
| 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | GNU General Public License for more details. |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 11 | |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame] | 12 | You should have received a copy of the GNU General Public License |
| 13 | along with TWRP. If not, see <http://www.gnu.org/licenses/>. |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #ifndef __TWADBSTREAM_H |
| 17 | #define __TWADBSTREAM_H |
| 18 | |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame] | 19 | #define TWRPARG "--twrp" |
| 20 | #define TWRP_BACKUP_ARG "backup" |
| 21 | #define TWRP_RESTORE_ARG "restore" |
| 22 | #define TWRP_STREAM_ARG "stream" |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 23 | #define TW_ADB_BACKUP "/tmp/twadbbackup" //FIFO for adb backup |
| 24 | #define TW_ADB_RESTORE "/tmp/twadbrestore" //FIFO for adb restore |
| 25 | #define TW_ADB_BU_CONTROL "/tmp/twadbbucontrol" //FIFO for sending control from TWRP to ADB Backup |
| 26 | #define TW_ADB_TWRP_CONTROL "/tmp/twadbtwrpcontrol" //FIFO for sending control from ADB Backup to TWRP |
| 27 | #define TWRP "TWRP" //Magic Value |
bigbiff bigbiff | b5ecaad | 2017-03-20 18:53:53 -0400 | [diff] [blame] | 28 | #define ADB_BU_MAX_ERROR 20 //Max amount of errors for while loops |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame] | 29 | #define ADB_BACKUP_OP "adbbackup" |
| 30 | #define ADB_RESTORE_OP "adbrestore" |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 31 | |
| 32 | //ADB Backup Control Commands |
| 33 | #define TWSTREAMHDR "twstreamheader" //TWRP Parititon Count Control |
| 34 | #define TWFN "twfilename" //TWRP Filename Control |
| 35 | #define TWIMG "twimage" //TWRP Image name Control |
| 36 | #define TWEOF "tweof" //End of File for Image/File |
| 37 | #define MD5TRAILER "md5trailer" //Image/File MD5 Trailer |
| 38 | #define TWDATA "twdatablock" // twrp adb backup data block header |
| 39 | #define TWMD5 "twverifymd5" //This command is compared to the md5trailer by ORS to verify transfer |
| 40 | #define TWENDADB "twendadb" //End Protocol |
| 41 | #define TWERROR "twerror" //Send error |
bigbiff bigbiff | 38b83c1 | 2017-12-28 19:58:52 -0500 | [diff] [blame] | 42 | #define ADB_BACKUP_VERSION 3 //Backup Version |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 43 | #define DATA_MAX_CHUNK_SIZE 1048576 //Maximum size between each data header |
| 44 | #define MAX_ADB_READ 512 //align with default tar size for amount to read fom adb stream |
| 45 | |
| 46 | /* |
| 47 | structs for adb backup need to align to 512 bytes for reading 512 |
| 48 | bytes at a time |
| 49 | Each struct contains a crc field so that when we are checking for commands |
| 50 | and the crc doesn't match we still assume it's data matching the command |
| 51 | struct but not really a command |
| 52 | */ |
| 53 | |
| 54 | /* stream format: |
| 55 | | TW ADB Backup Header | |
| 56 | | TW File Stream Header | |
| 57 | | File Data | |
| 58 | | File/Image MD5 Trailer | |
| 59 | | TW File Stream Header | |
| 60 | | File Data | |
| 61 | | File/Image MD5 Trailer | |
| 62 | | etc... | |
| 63 | */ |
| 64 | |
| 65 | //determine whether struct is 512 bytes, if not fail compilation |
| 66 | #define ADBSTRUCT_STATIC_ASSERT(structure) typedef char adb_assertion[( !!(structure) )*2-1 ] |
| 67 | |
| 68 | //generic cmd structure to align fields for sending commands to and from adb backup |
| 69 | struct AdbBackupControlType { |
| 70 | char start_of_header[8]; //stores the magic value #define TWRP |
| 71 | char type[16]; //stores the type of command, TWENDADB, TWCNT, TWEOF, TWMD5, TWDATA and TWERROR |
| 72 | uint32_t crc; //stores the zlib 32 bit crc of the AdbBackupControlType struct to allow for making sure we are processing metadata |
| 73 | char space[484]; //stores space to align the struct to 512 bytes |
Ayke van Laethem | 05c2de4 | 2017-06-02 00:19:27 +0200 | [diff] [blame] | 74 | |
| 75 | //return a C++ string while not reading outside the type char array |
| 76 | std::string get_type() { |
| 77 | return std::string(type, strnlen(type, sizeof(type)-1)); |
| 78 | } |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | //general info for file metadata stored in adb backup header |
| 82 | struct twfilehdr { |
| 83 | char start_of_header[8]; //stores the magic value #define TWRP |
| 84 | char type[16]; //stores the type of file header, TWFN or TWIMG |
| 85 | uint64_t size; //stores the size of the file contained after this header in the backup file |
| 86 | uint64_t compressed; //stores whether the file is compressed or not. 1 == compressed and 0 == uncompressed |
| 87 | uint32_t crc; //stores the zlib 32 bit crc of the twfilehdr struct to allow for making sure we are processing metadata |
| 88 | char name[468]; //stores the filename of the file |
| 89 | }; |
| 90 | |
| 91 | //md5 for files stored as a trailer to files in the adb backup file to check |
| 92 | //that they are restored correctly |
| 93 | struct AdbBackupFileTrailer { |
| 94 | char start_of_trailer[8]; //stores the magic value #define TWRP |
| 95 | char type[16]; //stores the AdbBackupFileTrailer type MD5TRAILER |
| 96 | uint32_t crc; //stores the zlib 32 bit crc of the AdbBackupFileTrailer struct to allow for making sure we are processing metadata |
| 97 | uint32_t ident; //stores crc to determine if header is encapsulated in stream as data |
| 98 | char md5[40]; //stores the md5 computation of the file |
| 99 | char space[440]; //stores space to align the struct to 512 bytes |
| 100 | }; |
| 101 | |
| 102 | //info for version and number of partitions backed up |
| 103 | struct AdbBackupStreamHeader { |
| 104 | char start_of_header[8]; //stores the magic value #define TWRP |
| 105 | char type[16]; //stores the AdbBackupStreamHeader value TWCNT |
| 106 | uint64_t partition_count; //stores the number of partitions to restore in the stream |
| 107 | uint64_t version; //stores the version of adb backup. increment ADB_BACKUP_VERSION each time the metadata is updated |
| 108 | uint32_t crc; //stores the zlib 32 bit crc of the AdbBackupStreamHeader struct to allow for making sure we are processing metadata |
| 109 | char space[468]; //stores space to align the struct to 512 bytes |
| 110 | }; |
| 111 | |
| 112 | #endif //__TWADBSTREAM_H |