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 #include <ctype.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <stdint.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <unistd.h>
35 
36 #include <csignal>
37 #include <cstdlib>
38 #include <fstream>
39 
40 #include "extensions.h"
41 #include "test_utils.h"
42 #include "tinyxml2.h"
43 
44 namespace fastboot {
45 namespace extension {
46 
47 namespace {  // private to this file
48 
49 // Since exceptions are disabled, a bad regex will trigger an abort in the constructor
50 // We at least need to print something out
MakeRegex(const std::string & regex_str,int line_num,std::regex_constants::syntax_option_type type=std::regex::ECMAScript)51 std::regex MakeRegex(const std::string& regex_str, int line_num,
52                      std::regex_constants::syntax_option_type type = std::regex::ECMAScript) {
53     // The signal handler can only access static vars
54     static std::string err_str;
55     err_str = android::base::StringPrintf("'%s' is not a valid regex string (line %d)\n",
56                                           regex_str.c_str(), line_num);
57     const auto sighandler = [](int) {
58         int nbytes = write(fileno(stderr), err_str.c_str(), err_str.length());
59         static_cast<void>(nbytes);  // need to supress the unused nbytes/ or unused result
60     };
61     std::signal(SIGABRT, sighandler);
62     // Now attempt to create the regex
63     std::regex ret(regex_str, type);
64     // unregister
65     std::signal(SIGABRT, SIG_DFL);
66 
67     return ret;
68 }
69 
XMLAssert(bool cond,const tinyxml2::XMLElement * elem,const char * msg)70 bool XMLAssert(bool cond, const tinyxml2::XMLElement* elem, const char* msg) {
71     if (!cond) {
72         printf("%s (line %d)\n", msg, elem->GetLineNum());
73     }
74     return !cond;
75 }
76 
XMLAttribute(const tinyxml2::XMLElement * elem,const std::string key,const std::string key_default="")77 const std::string XMLAttribute(const tinyxml2::XMLElement* elem, const std::string key,
78                                const std::string key_default = "") {
79     if (!elem->Attribute(key.c_str())) {
80         return key_default;
81     }
82 
83     return elem->Attribute(key.c_str());
84 }
85 
XMLYesNo(const tinyxml2::XMLElement * elem,const std::string key,bool * res,bool def=false)86 bool XMLYesNo(const tinyxml2::XMLElement* elem, const std::string key, bool* res,
87               bool def = false) {
88     if (!elem->Attribute(key.c_str())) {
89         *res = def;
90         return true;
91     }
92     const std::string val = elem->Attribute(key.c_str());
93     if (val != "yes" && val != "no") {
94         return false;
95     }
96     *res = (val == "yes");
97     return true;
98 }
99 
ExtractPartitions(tinyxml2::XMLConstHandle handle,Configuration * config)100 bool ExtractPartitions(tinyxml2::XMLConstHandle handle, Configuration* config) {
101     // Extract partitions
102     const tinyxml2::XMLElement* part = handle.FirstChildElement("part").ToElement();
103     while (part) {
104         Configuration::PartitionInfo part_info;
105         const std::string name = XMLAttribute(part, "value");
106         const std::string test = XMLAttribute(part, "test");
107         if (XMLAssert(!name.empty(), part, "The name of a partition can not be empty") ||
108             XMLAssert(XMLYesNo(part, "slots", &part_info.slots), part,
109                       "Slots attribute must be 'yes' or 'no'") ||
110             XMLAssert(XMLYesNo(part, "hashable", &part_info.hashable, true), part,
111                       "Hashable attribute must be 'yes' or 'no'") ||
112             XMLAssert(XMLYesNo(part, "parsed", &part_info.parsed), part,
113                       "Parsed attribute must be 'yes' or 'no'"))
114             return false;
115 
116         bool allowed = test == "yes" || test == "no-writes" || test == "no";
117         if (XMLAssert(allowed, part, "The test attribute must be 'yes' 'no-writes' or 'no'"))
118             return false;
119         if (XMLAssert(config->partitions.find(name) == config->partitions.end(), part,
120                       "The same partition name is listed twice"))
121             return false;
122         part_info.test = (test == "yes")
123                                  ? Configuration::PartitionInfo::YES
124                                  : (test == "no-writes") ? Configuration::PartitionInfo::NO_WRITES
125                                                          : Configuration::PartitionInfo::NO;
126         config->partitions[name] = part_info;
127         part = part->NextSiblingElement("part");
128     }
129     return true;
130 }
131 
ExtractPacked(tinyxml2::XMLConstHandle handle,Configuration * config)132 bool ExtractPacked(tinyxml2::XMLConstHandle handle, Configuration* config) {
133     // Extract partitions
134     const tinyxml2::XMLElement* part = handle.FirstChildElement("part").ToElement();
135     while (part) {
136         Configuration::PackedInfo packed_info;
137         const std::string name = XMLAttribute(part, "value");
138 
139         if (XMLAssert(!name.empty(), part, "The name of a packed partition can not be empty") ||
140             XMLAssert(XMLYesNo(part, "slots", &packed_info.slots), part,
141                       "Slots attribute must be 'yes' or 'no'") ||
142             XMLAssert(config->partitions.find(name) == config->partitions.end(), part,
143                       "A packed partition can not have same name as a real one") ||
144             XMLAssert(config->partitions.find(name) == config->partitions.end(), part,
145                       "The same partition name is listed twice"))
146             return false;
147 
148         // Extract children partitions
149         const tinyxml2::XMLElement* child = part->FirstChildElement("child")
150                                                     ? part->FirstChildElement("child")->ToElement()
151                                                     : nullptr;
152         while (child) {
153             const std::string text(child->GetText());
154             // Make sure child exists
155             if (XMLAssert(config->partitions.find(text) != config->partitions.end(), child,
156                           "The child partition was not listed in <partitions>"))
157                 return false;
158             packed_info.children.insert(text);
159             child = child->NextSiblingElement("child");
160         }
161 
162         // Extract tests
163         const tinyxml2::XMLElement* test = part->FirstChildElement("test")
164                                                    ? part->FirstChildElement("test")->ToElement()
165                                                    : nullptr;
166         while (test) {
167             Configuration::PackedInfoTest packed_test;
168             packed_test.packed_img = XMLAttribute(test, "packed");
169             packed_test.unpacked_dir = XMLAttribute(test, "unpacked");
170             const std::string expect = XMLAttribute(test, "expect", "okay");
171 
172             if (XMLAssert(!packed_test.packed_img.empty(), test,
173                           "The packed image location must be specified") ||
174                 XMLAssert(CMD_EXPECTS.find(expect) != CMD_EXPECTS.end(), test,
175                           "Expect attribute must be 'okay' or 'fail'"))
176                 return false;
177 
178             packed_test.expect = CMD_EXPECTS.at(expect);
179             // The expect is only unpacked directory is only needed if success
180             if (packed_test.expect == OKAY &&
181                 XMLAssert(!packed_test.unpacked_dir.empty(), test,
182                           "The unpacked image folder location must be specified"))
183                 return false;
184 
185             packed_info.tests.push_back(packed_test);
186             test = test->NextSiblingElement("test");
187         }
188 
189         config->packed[name] = packed_info;
190         part = part->NextSiblingElement("part");
191     }
192     return true;
193 }
194 
ExtractGetVars(tinyxml2::XMLConstHandle handle,Configuration * config)195 bool ExtractGetVars(tinyxml2::XMLConstHandle handle, Configuration* config) {
196     // Extract getvars
197     const tinyxml2::XMLElement* var = handle.FirstChildElement("var").ToElement();
198     while (var) {
199         const std::string key = XMLAttribute(var, "key");
200         const std::string reg = XMLAttribute(var, "assert");
201         if (XMLAssert(key.size(), var, "The var key name is empty")) return false;
202         if (XMLAssert(config->getvars.find(key) == config->getvars.end(), var,
203                       "The same getvar variable name is listed twice"))
204             return false;
205         Configuration::GetVar getvar{reg, MakeRegex(reg, var->GetLineNum()), var->GetLineNum()};
206         config->getvars[key] = std::move(getvar);
207         var = var->NextSiblingElement("var");
208     }
209     return true;
210 }
211 
ExtractOem(tinyxml2::XMLConstHandle handle,Configuration * config)212 bool ExtractOem(tinyxml2::XMLConstHandle handle, Configuration* config) {
213     // Extract getvars
214     // Extract oem commands
215     const tinyxml2::XMLElement* command = handle.FirstChildElement("command").ToElement();
216     while (command) {
217         const std::string cmd = XMLAttribute(command, "value");
218         const std::string permissions = XMLAttribute(command, "permissions");
219         if (XMLAssert(cmd.size(), command, "Empty command value")) return false;
220         if (XMLAssert(permissions == "none" || permissions == "unlocked", command,
221                       "Permissions attribute must be 'none' or 'unlocked'"))
222             return false;
223 
224         // Each command has tests
225         std::vector<Configuration::CommandTest> tests;
226         const tinyxml2::XMLElement* test = command->FirstChildElement("test");
227         while (test) {  // iterate through tests
228             Configuration::CommandTest ctest;
229 
230             ctest.line_num = test->GetLineNum();
231             const std::string default_name = "XMLTest-line-" + std::to_string(test->GetLineNum());
232             ctest.name = XMLAttribute(test, "name", default_name);
233             ctest.arg = XMLAttribute(test, "value");
234             ctest.input = XMLAttribute(test, "input");
235             ctest.output = XMLAttribute(test, "output");
236             ctest.validator = XMLAttribute(test, "validate");
237             ctest.regex_str = XMLAttribute(test, "assert");
238 
239             const std::string expect = XMLAttribute(test, "expect");
240 
241             if (XMLAssert(CMD_EXPECTS.find(expect) != CMD_EXPECTS.end(), test,
242                           "Expect attribute must be 'okay' or 'fail'"))
243                 return false;
244             ctest.expect = CMD_EXPECTS.at(expect);
245             std::regex regex;
246             if (expect == "okay" && ctest.regex_str.size()) {
247                 ctest.regex = MakeRegex(ctest.regex_str, test->GetLineNum());
248             }
249             tests.push_back(std::move(ctest));
250             test = test->NextSiblingElement("test");
251         }
252 
253         // Build the command struct
254         const Configuration::OemCommand oem_cmd{permissions == "unlocked", std::move(tests)};
255         config->oem[cmd] = std::move(oem_cmd);
256 
257         command = command->NextSiblingElement("command");
258     }
259     return true;
260 }
261 
ExtractChecksum(tinyxml2::XMLConstHandle handle,Configuration * config)262 bool ExtractChecksum(tinyxml2::XMLConstHandle handle, Configuration* config) {
263     const tinyxml2::XMLElement* checksum = handle.ToElement();
264     if (checksum && checksum->Attribute("value")) {
265         config->checksum = XMLAttribute(checksum, "value");
266         config->checksum_parser = XMLAttribute(checksum, "parser");
267         if (XMLAssert(config->checksum_parser != "", checksum,
268                       "A checksum parser attribute is mandatory"))
269             return false;
270     }
271     return true;
272 }
273 
274 }  // anonymous namespace
275 
ParseXml(const std::string & file,Configuration * config)276 bool ParseXml(const std::string& file, Configuration* config) {
277     tinyxml2::XMLDocument doc;
278     if (doc.LoadFile(file.c_str())) {
279         printf("Failed to open/parse XML file '%s'\nXMLError: %s\n", file.c_str(), doc.ErrorStr());
280         return false;
281     }
282 
283     tinyxml2::XMLConstHandle handle(&doc);
284     tinyxml2::XMLConstHandle root(handle.FirstChildElement("config"));
285 
286     // Extract the getvars
287     if (!ExtractGetVars(root.FirstChildElement("getvar"), config)) {
288         return false;
289     }
290     // Extract the partition info
291     if (!ExtractPartitions(root.FirstChildElement("partitions"), config)) {
292         return false;
293     }
294 
295     // Extract packed
296     if (!ExtractPacked(root.FirstChildElement("packed"), config)) {
297         return false;
298     }
299 
300     // Extract oem commands
301     if (!ExtractOem(root.FirstChildElement("oem"), config)) {
302         return false;
303     }
304 
305     // Extract checksum
306     if (!ExtractChecksum(root.FirstChildElement("checksum"), config)) {
307         return false;
308     }
309 
310     return true;
311 }
312 
313 }  // namespace extension
314 }  // namespace fastboot
315