summaryrefslogtreecommitdiff
path: root/util/coreboot-configurator/src/application/NvramToolCli.cpp
blob: da844a043bdad2fb63d78ae10552052d8ef2a98e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* SPDX-License-Identifier: GPL-2.0-only */

#include <QProcess>
#include <QTextStream>

#include "NvramToolCli.h"
#include "Util.h"

static constexpr char s_sudoProg[] = "/usr/bin/pkexec";
static constexpr char s_nvramToolProg[] = "/usr/sbin/nvramtool";

#if MOCK

QMap<QString, QString> NvramToolCli::readParameters(QString *error) {
	return QMap<QString,QString>({
		{"boot_option","Normal"},
		{"reboot_counter","0x0"},
		{"debug_level","Spew"},
		{"vtd","Enable"},
		{"power_profile","Performance"},
		{"wireless","Enable"},
		{"webcam","Enable"},
		{"microphone","Enable"},
		{"legacy_8254_timer","Enable"},
		{"usb_always_on","Disable"},
		{"kbl_timeout","Never"},
		{"fn_ctrl_swap","Enable"},
		{"max_charge","100%"},
		{"power_on_after_fail","Disable"},
		{"fn_lock_state","0x2"},
		{"trackpad_state","0x40"},
		{"kbl_brightness","0xc4"},
		{"kbl_state","0x22"}
	});
}

QStringList NvramToolCli::readOptions(const QString &parameter, QString *error){
	return (parameter=="power_profile")?
	QStringList{
		"Power Saver","Balanced","Performance"
	} : QStringList{};
}

#else

QMap<QString, QString> NvramToolCli::readParameters(QString *error)
{
	QProcess nvramtoolProcess;
	nvramtoolProcess.start(s_sudoProg, {s_nvramToolProg, "-a"});

	nvramtoolProcess.waitForFinished();

	if(error) *error = nvramtoolProcess.readAllStandardError();

	if(nvramtoolProcess.exitCode() != 0){
		return {};
	}

	return Util::parseParameters(nvramtoolProcess);
}

QStringList NvramToolCli::readOptions(const QString &parameter, QString *error)
{
	QStringList result;

	QProcess nvramtoolProcess;
	nvramtoolProcess.start(s_sudoProg, {s_nvramToolProg, "-e", parameter});
	nvramtoolProcess.waitForFinished();

	if(error) *error = nvramtoolProcess.readAllStandardError();

	while (nvramtoolProcess.canReadLine()) {
		result.append(nvramtoolProcess.readLine().trimmed());
	}

	return result;
}
#endif

bool NvramToolCli::writeParameters(const QMap<QString, QString> &parameters, QString *error)
{

#if MOCK
	QTextStream outStream(stdout);
#else
	QProcess nvramtoolProcess;
	nvramtoolProcess.start(s_sudoProg, {s_nvramToolProg, "-i"});
	nvramtoolProcess.waitForStarted();
	QTextStream outStream(&nvramtoolProcess);
#endif
	for(auto it = parameters.begin(); it != parameters.end(); ++it){
		outStream << it.key() << " = " << it.value() << "\n";
	}

	outStream.flush();
#if MOCK
	return true;
#else
	nvramtoolProcess.closeWriteChannel();
	nvramtoolProcess.waitForFinished();

	if(error){
		*error = nvramtoolProcess.readAllStandardError();
	}

	return nvramtoolProcess.exitCode()==0;
#endif
}



QString NvramToolCli::version()
{
	QProcess nvramtoolProcess;
	nvramtoolProcess.start(s_nvramToolProg, {"-v"});

	nvramtoolProcess.waitForFinished();

	return nvramtoolProcess.readAll();
}