1 #include "op_dump_util.hpp"
2 
3 #include "dump_utils.hpp"
4 #include "xyz/openbmc_project/Common/error.hpp"
5 #include "xyz/openbmc_project/Dump/Create/error.hpp"
6 
7 #include <unistd.h>
8 
9 #include <phosphor-logging/elog-errors.hpp>
10 #include <phosphor-logging/elog.hpp>
11 #include <phosphor-logging/lg2.hpp>
12 
13 #include <filesystem>
14 
15 namespace openpower
16 {
17 namespace dump
18 {
19 namespace util
20 {
21 
22 bool isOPDumpsEnabled(sdbusplus::bus_t& bus)
23 {
24     // Set isEnabled as true by default. In a field deployment, the system dump
25     // feature is usually enabled to facilitate effective debugging in the event
26     // of a failure. If due to some error, the settings service couldn't provide
27     // the actual value, the system assumes that the dump is enabled.
28     // This approach aligns with the principle of collecting as much data as
29     // possible for debugging in case of a system failure.
30     auto isEnabled = true;
31 
32     constexpr auto enable = "xyz.openbmc_project.Object.Enable";
33     constexpr auto policy = "/xyz/openbmc_project/dump/system_dump_policy";
34     constexpr auto property = "org.freedesktop.DBus.Properties";
35 
36     try
37     {
38         auto service = phosphor::dump::getService(bus, policy, enable);
39 
40         auto method = bus.new_method_call(service.c_str(), policy, property,
41                                           "Get");
42         method.append(enable, "Enabled");
43         auto reply = bus.call(method);
44         std::variant<bool> v;
45         reply.read(v);
46         isEnabled = std::get<bool>(v);
47     }
48     catch (const sdbusplus::exception::SdBusError& e)
49     {
50         lg2::error("Error: {ERROR} in getting dump policy, default is enabled",
51                    "ERROR", e);
52     }
53     return isEnabled;
54 }
55 
56 BIOSAttrValueType readBIOSAttribute(const std::string& attrName,
57                                     sdbusplus::bus_t& bus)
58 {
59     std::tuple<std::string, BIOSAttrValueType, BIOSAttrValueType> attrVal;
60     auto method = bus.new_method_call(
61         "xyz.openbmc_project.BIOSConfigManager",
62         "/xyz/openbmc_project/bios_config/manager",
63         "xyz.openbmc_project.BIOSConfig.Manager", "GetAttribute");
64     method.append(attrName);
65     try
66     {
67         auto result = bus.call(method);
68         result.read(std::get<0>(attrVal), std::get<1>(attrVal),
69                     std::get<2>(attrVal));
70     }
71     catch (const sdbusplus::exception::SdBusError& e)
72     {
73         lg2::error("Failed to read BIOS Attribute: {ATTRIBUTE_NAME}",
74                    "ATTRIBUTE_NAME", attrName);
75         throw;
76     }
77     return std::get<1>(attrVal);
78 }
79 
80 bool isSystemDumpInProgress(sdbusplus::bus_t& bus)
81 {
82     try
83     {
84         auto dumpInProgress = std::get<std::string>(
85             readBIOSAttribute("pvm_sys_dump_active", bus));
86         if (dumpInProgress == "Enabled")
87         {
88             lg2::info("A system dump is already in progress");
89             return true;
90         }
91     }
92     catch (const std::bad_variant_access& ex)
93     {
94         lg2::error("Failed to read pvm_sys_dump_active property value due "
95                    "to bad variant access error:{ERROR}",
96                    "ERROR", ex);
97         return false;
98     }
99     catch (const std::exception& ex)
100     {
101         lg2::error("Failed to read pvm_sys_dump_active error:{ERROR}", "ERROR",
102                    ex);
103         return false;
104     }
105 
106     lg2::info("Another system dump is not in progress");
107     return false;
108 }
109 
110 } // namespace util
111 } // namespace dump
112 } // namespace openpower
113