| 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | |
| 3 | #include <QProcess> |
| 4 | #include <QTextStream> |
| 5 | |
| 6 | #include "NvramToolCli.h" |
| 7 | #include "Util.h" |
| 8 | |
| 9 | static constexpr char s_sudoProg[] = "/usr/bin/pkexec"; |
| 10 | static constexpr char s_nvramToolProg[] = "/usr/sbin/nvramtool"; |
| 11 | |
| 12 | #if MOCK |
| 13 | |
| 14 | QMap<QString, QString> NvramToolCli::readParameters(QString *error) { |
| 15 | return QMap<QString,QString>({ |
| 16 | {"boot_option","Normal"}, |
| 17 | {"reboot_counter","0x0"}, |
| 18 | {"debug_level","Spew"}, |
| 19 | {"vtd","Enable"}, |
| 20 | {"power_profile","Performance"}, |
| 21 | {"wireless","Enable"}, |
| 22 | {"webcam","Enable"}, |
| 23 | {"microphone","Enable"}, |
| 24 | {"legacy_8254_timer","Enable"}, |
| 25 | {"usb_always_on","Disable"}, |
| 26 | {"kbl_timeout","Never"}, |
| 27 | {"fn_ctrl_swap","Enable"}, |
| 28 | {"max_charge","100%"}, |
| 29 | {"power_on_after_fail","Disable"}, |
| 30 | {"fn_lock_state","0x2"}, |
| 31 | {"trackpad_state","0x40"}, |
| 32 | {"kbl_brightness","0xc4"}, |
| 33 | {"kbl_state","0x22"} |
| 34 | }); |
| 35 | } |
| 36 | |
| 37 | QStringList NvramToolCli::readOptions(const QString ¶meter, QString *error){ |
| 38 | return (parameter=="power_profile")? |
| 39 | QStringList{ |
| 40 | "Power Saver","Balanced","Performance" |
| 41 | } : QStringList{}; |
| 42 | } |
| 43 | |
| 44 | #else |
| 45 | |
| 46 | QMap<QString, QString> NvramToolCli::readParameters(QString *error) |
| 47 | { |
| 48 | QProcess nvramtoolProcess; |
| 49 | nvramtoolProcess.start(s_sudoProg, {s_nvramToolProg, "-a"}); |
| 50 | |
| 51 | nvramtoolProcess.waitForFinished(); |
| 52 | |
| 53 | if(error) *error = nvramtoolProcess.readAllStandardError(); |
| 54 | |
| 55 | if(nvramtoolProcess.exitCode() != 0){ |
| 56 | return {}; |
| 57 | } |
| 58 | |
| 59 | return Util::parseParameters(nvramtoolProcess); |
| 60 | } |
| 61 | |
| 62 | QStringList NvramToolCli::readOptions(const QString ¶meter, QString *error) |
| 63 | { |
| 64 | QStringList result; |
| 65 | |
| 66 | QProcess nvramtoolProcess; |
| 67 | nvramtoolProcess.start(s_sudoProg, {s_nvramToolProg, "-e", parameter}); |
| 68 | nvramtoolProcess.waitForFinished(); |
| 69 | |
| 70 | if(error) *error = nvramtoolProcess.readAllStandardError(); |
| 71 | |
| 72 | while (nvramtoolProcess.canReadLine()) { |
| 73 | result.append(nvramtoolProcess.readLine().trimmed()); |
| 74 | } |
| 75 | |
| 76 | return result; |
| 77 | } |
| 78 | #endif |
| 79 | |
| 80 | bool NvramToolCli::writeParameters(const QMap<QString, QString> ¶meters, QString *error) |
| 81 | { |
| 82 | |
| 83 | #if MOCK |
| 84 | QTextStream outStream(stdout); |
| 85 | #else |
| 86 | QProcess nvramtoolProcess; |
| 87 | nvramtoolProcess.start(s_sudoProg, {s_nvramToolProg, "-i"}); |
| 88 | nvramtoolProcess.waitForStarted(); |
| 89 | QTextStream outStream(&nvramtoolProcess); |
| 90 | #endif |
| 91 | for(auto it = parameters.begin(); it != parameters.end(); ++it){ |
| 92 | outStream << it.key() << " = " << it.value() << "\n"; |
| 93 | } |
| 94 | |
| 95 | outStream.flush(); |
| 96 | #if MOCK |
| 97 | return true; |
| 98 | #else |
| 99 | nvramtoolProcess.closeWriteChannel(); |
| 100 | nvramtoolProcess.waitForFinished(); |
| 101 | |
| 102 | if(error){ |
| 103 | *error = nvramtoolProcess.readAllStandardError(); |
| 104 | } |
| 105 | |
| 106 | return nvramtoolProcess.exitCode()==0; |
| 107 | #endif |
| 108 | } |
| 109 | |
| 110 | |
| 111 | |
| 112 | QString NvramToolCli::version() |
| 113 | { |
| 114 | QProcess nvramtoolProcess; |
| 115 | nvramtoolProcess.start(s_nvramToolProg, {"-v"}); |
| 116 | |
| 117 | nvramtoolProcess.waitForFinished(); |
| 118 | |
| 119 | return nvramtoolProcess.readAll(); |
| 120 | } |