1 //
2 // Copyright (C) 2020 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 #include "update_engine/cros/requisition_util.h"
18 
19 #include <memory>
20 #include <vector>
21 
22 #include <base/files/file_util.h>
23 #include <base/json/json_file_value_serializer.h>
24 #include <base/logging.h>
25 #include <base/strings/string_util.h>
26 
27 #include "update_engine/common/subprocess.h"
28 #include "update_engine/common/utils.h"
29 
30 using std::string;
31 using std::vector;
32 
33 namespace {
34 
35 constexpr char kOemRequisitionKey[] = "oem_device_requisition";
36 
37 }  // namespace
38 
39 namespace chromeos_update_engine {
40 
ReadDeviceRequisition(const base::FilePath & local_state)41 string ReadDeviceRequisition(const base::FilePath& local_state) {
42   string requisition;
43   bool vpd_retval = utils::GetVpdValue(kOemRequisitionKey, &requisition);
44 
45   // Some users manually convert non-CfM hardware at enrollment time, so VPD
46   // value may be missing. So check the Local State JSON as well.
47   if ((requisition.empty() || !vpd_retval) && base::PathExists(local_state)) {
48     int error_code;
49     std::string error_msg;
50     JSONFileValueDeserializer deserializer(local_state);
51     std::unique_ptr<base::Value> root =
52         deserializer.Deserialize(&error_code, &error_msg);
53     if (!root) {
54       if (error_code != 0) {
55         LOG(ERROR) << "Unable to deserialize Local State with exit code: "
56                    << error_code << " and error: " << error_msg;
57       }
58       return "";
59     }
60     auto* path = root->FindPath({"enrollment", "device_requisition"});
61     if (!path || !path->is_string()) {
62       return "";
63     }
64     path->GetAsString(&requisition);
65   }
66   return requisition;
67 }
68 
69 }  // namespace chromeos_update_engine
70