1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 #pragma once
29 
30 #include <sparse/sparse.h>
31 #include <stdint.h>
32 #include <stdio.h>
33 #include <sys/types.h>
34 #include <sys/wait.h>
35 #include <unistd.h>
36 #include <cstdlib>
37 #include <fstream>
38 #include <random>
39 #include <string>
40 #include <unordered_map>
41 #include "fastboot_driver.h"
42 
43 namespace fastboot {
44 
45 char rand_legal();
46 char rand_illegal();
47 char rand_char();
48 // start and end are inclusive
49 int random_int(int start, int end);
50 
51 // I don't want to have to manage memory for this guy
52 struct SparseWrapper {
SparseWrapperSparseWrapper53     SparseWrapper(unsigned int block_size, int64_t len) {
54         sparse = sparse_file_new(block_size, len);
55     }
56 
SparseWrapperSparseWrapper57     SparseWrapper(struct sparse_file* sf) { sparse = sf; }
58 
~SparseWrapperSparseWrapper59     ~SparseWrapper() {
60         if (sparse) {
61             sparse_file_destroy(sparse);
62         }
63     }
64 
RepSparseWrapper65     const std::string Rep() {
66         unsigned bs = sparse_file_block_size(sparse);
67         unsigned len = sparse_file_len(sparse, true, false);
68         return android::base::StringPrintf("[block_size=%u, len=%u]", bs, len);
69     }
70 
71     struct sparse_file* operator*() {
72         return sparse;
73     }
74 
75     struct sparse_file* sparse;
76 };
77 
78 std::string RandomString(size_t length, std::function<char(void)> provider);
79 // RVO will avoid copy
80 std::vector<char> RandomBuf(size_t length, std::function<char(void)> provider = rand_char);
81 
82 std::vector<std::string> SplitBySpace(const std::string& s);
83 
84 std::unordered_map<std::string, std::string> ParseArgs(int argc, char** argv, std::string* err_msg);
85 
86 std::vector<std::string> GeneratePartitionNames(const std::string& base, int num_slots = 0);
87 
88 int ConfigureSerial(const std::string& port);
89 
90 int StartProgram(const std::string program, const std::vector<std::string> args, int* pipe);
91 int WaitProgram(const pid_t pid, const int pipe, std::string* error_msg);
92 
93 }  // namespace fastboot
94