xref: /xnu-10002.81.5/tests/nvram_tests/nvram_system.c (revision 5e3eaea39dcf651e66cb99ba7d70e32cc4a99587)
1*5e3eaea3SApple OSS Distributions #include <darwintest.h>
2*5e3eaea3SApple OSS Distributions #include "nvram_helper.h"
3*5e3eaea3SApple OSS Distributions 
4*5e3eaea3SApple OSS Distributions T_GLOBAL_META(T_META_NAMESPACE("xnu.nvram"),
5*5e3eaea3SApple OSS Distributions     T_META_RADAR_COMPONENT_NAME("xnu"),
6*5e3eaea3SApple OSS Distributions     T_META_RADAR_COMPONENT_VERSION("nvram"));
7*5e3eaea3SApple OSS Distributions 
8*5e3eaea3SApple OSS Distributions static io_registry_entry_t optionsRef = IO_OBJECT_NULL;
9*5e3eaea3SApple OSS Distributions 
10*5e3eaea3SApple OSS Distributions // xcrun -sdk macosx.internal make -C tests nvram_system && sudo ./tests/build/sym/nvram_system
11*5e3eaea3SApple OSS Distributions 
12*5e3eaea3SApple OSS Distributions // Test that writing, reading, and deleting of system variables with system entitlement should succeed
13*5e3eaea3SApple OSS Distributions T_DECL(TestSystemEntitled, "Test system guids with entitlement")
14*5e3eaea3SApple OSS Distributions {
15*5e3eaea3SApple OSS Distributions #if ((TARGET_OS_OSX) && !(__x86_64__))
16*5e3eaea3SApple OSS Distributions 	const char *varToTest = SystemNVRAMGuidString ":" "varToTest2";
17*5e3eaea3SApple OSS Distributions 
18*5e3eaea3SApple OSS Distributions 	optionsRef = CreateOptionsRef();
19*5e3eaea3SApple OSS Distributions 
20*5e3eaea3SApple OSS Distributions 	TestVarOp(OP_SET, varToTest, DefaultSetVal, KERN_SUCCESS, optionsRef);
21*5e3eaea3SApple OSS Distributions 	TestVarOp(OP_GET, varToTest, NULL, KERN_SUCCESS, optionsRef);
22*5e3eaea3SApple OSS Distributions 	TestVarOp(OP_DEL, varToTest, NULL, KERN_SUCCESS, optionsRef);
23*5e3eaea3SApple OSS Distributions 
24*5e3eaea3SApple OSS Distributions 	ReleaseOptionsRef(optionsRef);
25*5e3eaea3SApple OSS Distributions #else
26*5e3eaea3SApple OSS Distributions 	T_PASS("Test not supported");
27*5e3eaea3SApple OSS Distributions #endif /* ((TARGET_OS_OSX) && !(__x86_64__)) */
28*5e3eaea3SApple OSS Distributions }
29*5e3eaea3SApple OSS Distributions 
30*5e3eaea3SApple OSS Distributions 
31*5e3eaea3SApple OSS Distributions #if !(TARGET_CPU_ARM64 && TARGET_OS_OSX)
32*5e3eaea3SApple OSS Distributions // Test that system guid is translated to common guid on devices without system namespace support
33*5e3eaea3SApple OSS Distributions T_DECL(TestSysNonAS, "Test system guids for devices without system namespace")
34*5e3eaea3SApple OSS Distributions {
35*5e3eaea3SApple OSS Distributions 	const char *varWithSys = SystemNVRAMGuidString ":" "varToTestSys";
36*5e3eaea3SApple OSS Distributions 	const char *varWOSys = "varToTestSys";
37*5e3eaea3SApple OSS Distributions 
38*5e3eaea3SApple OSS Distributions 	optionsRef = CreateOptionsRef();
39*5e3eaea3SApple OSS Distributions 
40*5e3eaea3SApple OSS Distributions 	TestVarOp(OP_SET, varWithSys, DefaultSetVal, KERN_SUCCESS, optionsRef);
41*5e3eaea3SApple OSS Distributions 	TestVarOp(OP_GET, varWOSys, NULL, KERN_SUCCESS, optionsRef);
42*5e3eaea3SApple OSS Distributions 	TestVarOp(OP_DEL, varWOSys, NULL, KERN_SUCCESS, optionsRef);
43*5e3eaea3SApple OSS Distributions 
44*5e3eaea3SApple OSS Distributions 	ReleaseOptionsRef(optionsRef);
45*5e3eaea3SApple OSS Distributions }
46*5e3eaea3SApple OSS Distributions #endif
47*5e3eaea3SApple OSS Distributions 
48*5e3eaea3SApple OSS Distributions // Variable entitlement tests for system variables
49*5e3eaea3SApple OSS Distributions #if ((TARGET_OS_OSX) && !(__x86_64__))
50*5e3eaea3SApple OSS Distributions // Test that read of entitled system variables without entitlement should fail
51*5e3eaea3SApple OSS Distributions T_DECL(TestEntRdSys, "Test read entitled system variables")
52*5e3eaea3SApple OSS Distributions {
53*5e3eaea3SApple OSS Distributions 	char * varToTest = SystemNVRAMGuidString ":" "testEntRdSys";
54*5e3eaea3SApple OSS Distributions 
55*5e3eaea3SApple OSS Distributions 	optionsRef = CreateOptionsRef();
56*5e3eaea3SApple OSS Distributions 
57*5e3eaea3SApple OSS Distributions 	TestVarOp(OP_SET, varToTest, DefaultSetVal, KERN_SUCCESS, optionsRef);
58*5e3eaea3SApple OSS Distributions 	TestVarOp(OP_GET, varToTest, NULL, KERN_FAILURE, optionsRef);
59*5e3eaea3SApple OSS Distributions 
60*5e3eaea3SApple OSS Distributions 	ReleaseOptionsRef(optionsRef);
61*5e3eaea3SApple OSS Distributions }
62*5e3eaea3SApple OSS Distributions 
63*5e3eaea3SApple OSS Distributions // Test that writing of entitled system variables without entitlement should fail
64*5e3eaea3SApple OSS Distributions T_DECL(TestEntModRstSys, "Test write entitled system variables")
65*5e3eaea3SApple OSS Distributions {
66*5e3eaea3SApple OSS Distributions 	char * varToTest = SystemNVRAMGuidString ":" "testEntModRstSys";
67*5e3eaea3SApple OSS Distributions 
68*5e3eaea3SApple OSS Distributions 	optionsRef = CreateOptionsRef();
69*5e3eaea3SApple OSS Distributions 
70*5e3eaea3SApple OSS Distributions 	TestVarOp(OP_SET, varToTest, DefaultSetVal, kIOReturnNotPrivileged, optionsRef);
71*5e3eaea3SApple OSS Distributions 
72*5e3eaea3SApple OSS Distributions 	ReleaseOptionsRef(optionsRef);
73*5e3eaea3SApple OSS Distributions }
74*5e3eaea3SApple OSS Distributions 
75*5e3eaea3SApple OSS Distributions // Test that deleting of entitled system variables without entitlement should fail
76*5e3eaea3SApple OSS Distributions T_DECL(TestEntDelSys, "Test delete entitled system variables")
77*5e3eaea3SApple OSS Distributions {
78*5e3eaea3SApple OSS Distributions 	char * varToTest = SystemNVRAMGuidString ":" "testEntDelSys";
79*5e3eaea3SApple OSS Distributions 
80*5e3eaea3SApple OSS Distributions 	optionsRef = CreateOptionsRef();
81*5e3eaea3SApple OSS Distributions 
82*5e3eaea3SApple OSS Distributions 	TestVarOp(OP_SET, varToTest, DefaultSetVal, KERN_SUCCESS, optionsRef);
83*5e3eaea3SApple OSS Distributions 	TestVarOp(OP_DEL, varToTest, NULL, KERN_FAILURE, optionsRef);
84*5e3eaea3SApple OSS Distributions 
85*5e3eaea3SApple OSS Distributions 	ReleaseOptionsRef(optionsRef);
86*5e3eaea3SApple OSS Distributions }
87*5e3eaea3SApple OSS Distributions 
88*5e3eaea3SApple OSS Distributions // Test resetting of nvram with entitlement should not erase testEntRstSys
89*5e3eaea3SApple OSS Distributions // To reset nvram, call the test with -r argument:
90*5e3eaea3SApple OSS Distributions // sudo ./tests/build/sym/nvram_system -n xnu.nvram.TestEntRstSys -- -r
91*5e3eaea3SApple OSS Distributions T_DECL(TestEntRstSys, "Test reset entitled system variables")
92*5e3eaea3SApple OSS Distributions {
93*5e3eaea3SApple OSS Distributions 	opterr = 0;
94*5e3eaea3SApple OSS Distributions 	optind = 0;
95*5e3eaea3SApple OSS Distributions 	char * varToTest = SystemNVRAMGuidString ":" "testEntRstSys";
96*5e3eaea3SApple OSS Distributions 
97*5e3eaea3SApple OSS Distributions 	optionsRef = CreateOptionsRef();
98*5e3eaea3SApple OSS Distributions 
99*5e3eaea3SApple OSS Distributions 	TestVarOp(OP_SET, varToTest, DefaultSetVal, KERN_SUCCESS, optionsRef);
100*5e3eaea3SApple OSS Distributions 
101*5e3eaea3SApple OSS Distributions 	if (getopt(argc, argv, "r") == 'r') {
102*5e3eaea3SApple OSS Distributions 		TestVarOp(OP_RES, NULL, NULL, KERN_SUCCESS, optionsRef);
103*5e3eaea3SApple OSS Distributions 		TestVarOp(OP_GET, varToTest, NULL, KERN_SUCCESS, optionsRef);
104*5e3eaea3SApple OSS Distributions 	} else {
105*5e3eaea3SApple OSS Distributions 		TestVarOp(OP_DEL, varToTest, NULL, KERN_SUCCESS, optionsRef);
106*5e3eaea3SApple OSS Distributions 		T_PASS("NVram reset not invoked");
107*5e3eaea3SApple OSS Distributions 	}
108*5e3eaea3SApple OSS Distributions 
109*5e3eaea3SApple OSS Distributions 	ReleaseOptionsRef(optionsRef);
110*5e3eaea3SApple OSS Distributions }
111*5e3eaea3SApple OSS Distributions 
112*5e3eaea3SApple OSS Distributions // Test NVRAM Reset
113*5e3eaea3SApple OSS Distributions // To reset nvram, call the test with -r argument:
114*5e3eaea3SApple OSS Distributions // sudo ./tests/build/sym/nvram_system -n xnu.nvram.TestNVRAMResetSys -- -r
115*5e3eaea3SApple OSS Distributions T_DECL(TestNVRAMResetSys, "Test NVRAM Reset for system region variables")
116*5e3eaea3SApple OSS Distributions {
117*5e3eaea3SApple OSS Distributions 	opterr = 0;
118*5e3eaea3SApple OSS Distributions 	optind = 0;
119*5e3eaea3SApple OSS Distributions 	const char * varToTest = "testVar1";
120*5e3eaea3SApple OSS Distributions 	const char * varToTestWSys = SystemNVRAMGuidString ":" "testVar2";
121*5e3eaea3SApple OSS Distributions 
122*5e3eaea3SApple OSS Distributions 	optionsRef = CreateOptionsRef();
123*5e3eaea3SApple OSS Distributions 
124*5e3eaea3SApple OSS Distributions 	if (getopt(argc, argv, "r") == 'r') {
125*5e3eaea3SApple OSS Distributions 		TestVarOp(OP_SET, varToTest, DefaultSetVal, KERN_SUCCESS, optionsRef);
126*5e3eaea3SApple OSS Distributions 		TestVarOp(OP_SET, varToTestWSys, DefaultSetVal, KERN_SUCCESS, optionsRef);
127*5e3eaea3SApple OSS Distributions 
128*5e3eaea3SApple OSS Distributions 		TestVarOp(OP_RES, NULL, NULL, KERN_SUCCESS, optionsRef);
129*5e3eaea3SApple OSS Distributions 		TestVarOp(OP_GET, varToTest, NULL, KERN_FAILURE, optionsRef);
130*5e3eaea3SApple OSS Distributions 
131*5e3eaea3SApple OSS Distributions 		TestVarOp(OP_GET, varToTestWSys, NULL, KERN_FAILURE, optionsRef);
132*5e3eaea3SApple OSS Distributions 	} else {
133*5e3eaea3SApple OSS Distributions 		T_PASS("NVram reset not invoked");
134*5e3eaea3SApple OSS Distributions 	}
135*5e3eaea3SApple OSS Distributions 
136*5e3eaea3SApple OSS Distributions 	ReleaseOptionsRef(optionsRef);
137*5e3eaea3SApple OSS Distributions }
138*5e3eaea3SApple OSS Distributions 
139*5e3eaea3SApple OSS Distributions // Test NVRAM Obliterate
140*5e3eaea3SApple OSS Distributions // To obliterate nvram, call the test with -r argument:
141*5e3eaea3SApple OSS Distributions // sudo ./tests/build/sym/nvram_system -n xnu.nvram.TestNVRAMOblitSys -- -r
142*5e3eaea3SApple OSS Distributions T_DECL(TestNVRAMOblitSys, "Test NVRAM Obliterate for system region")
143*5e3eaea3SApple OSS Distributions {
144*5e3eaea3SApple OSS Distributions 	opterr = 0;
145*5e3eaea3SApple OSS Distributions 	optind = 0;
146*5e3eaea3SApple OSS Distributions 	const char * varToTest = "testVar1";
147*5e3eaea3SApple OSS Distributions 	const char * varToTestWSys = SystemNVRAMGuidString ":" "testVar2";
148*5e3eaea3SApple OSS Distributions 	const char * varToTestWRand = RandomNVRAMGuidString ":" "testVar3";
149*5e3eaea3SApple OSS Distributions 	const char * oblitSys = SystemNVRAMGuidString ":" "ObliterateNVRam";
150*5e3eaea3SApple OSS Distributions 	const char * oblitNonSys = CommonNVRAMGuidString ":" "ObliterateNVRam";
151*5e3eaea3SApple OSS Distributions 
152*5e3eaea3SApple OSS Distributions 	optionsRef = CreateOptionsRef();
153*5e3eaea3SApple OSS Distributions 
154*5e3eaea3SApple OSS Distributions 	if (getopt(argc, argv, "r") == 'r') {
155*5e3eaea3SApple OSS Distributions 		// Set variables with common namespace, random namespace and system namespace
156*5e3eaea3SApple OSS Distributions 		TestVarOp(OP_SET, varToTest, DefaultSetVal, KERN_SUCCESS, optionsRef);
157*5e3eaea3SApple OSS Distributions 		TestVarOp(OP_SET, varToTestWSys, DefaultSetVal, KERN_SUCCESS, optionsRef);
158*5e3eaea3SApple OSS Distributions 		TestVarOp(OP_SET, varToTestWRand, DefaultSetVal, KERN_SUCCESS, optionsRef);
159*5e3eaea3SApple OSS Distributions 
160*5e3eaea3SApple OSS Distributions 		// Obliterate sys first and make sure non-sys variables aren't deleted
161*5e3eaea3SApple OSS Distributions 		TestVarOp(OP_OBL, oblitSys, NULL, KERN_SUCCESS, optionsRef);
162*5e3eaea3SApple OSS Distributions 
163*5e3eaea3SApple OSS Distributions 		TestVarOp(OP_GET, varToTestWSys, NULL, KERN_FAILURE, optionsRef);
164*5e3eaea3SApple OSS Distributions 		TestVarOp(OP_GET, varToTest, NULL, KERN_SUCCESS, optionsRef);
165*5e3eaea3SApple OSS Distributions 		TestVarOp(OP_GET, varToTestWRand, NULL, KERN_SUCCESS, optionsRef);
166*5e3eaea3SApple OSS Distributions 
167*5e3eaea3SApple OSS Distributions 		// Now, obliterate common region and make sure all non-sys variables are deleted
168*5e3eaea3SApple OSS Distributions 		TestVarOp(OP_SET, varToTestWSys, DefaultSetVal, KERN_SUCCESS, optionsRef);
169*5e3eaea3SApple OSS Distributions 
170*5e3eaea3SApple OSS Distributions 		TestVarOp(OP_OBL, oblitNonSys, NULL, KERN_SUCCESS, optionsRef);
171*5e3eaea3SApple OSS Distributions 
172*5e3eaea3SApple OSS Distributions 		TestVarOp(OP_GET, varToTestWSys, NULL, KERN_SUCCESS, optionsRef);
173*5e3eaea3SApple OSS Distributions 		TestVarOp(OP_GET, varToTest, NULL, KERN_FAILURE, optionsRef);
174*5e3eaea3SApple OSS Distributions 		TestVarOp(OP_GET, varToTestWRand, NULL, KERN_FAILURE, optionsRef);
175*5e3eaea3SApple OSS Distributions 		TestVarOp(OP_DEL, varToTestWSys, NULL, KERN_SUCCESS, optionsRef);
176*5e3eaea3SApple OSS Distributions 	} else {
177*5e3eaea3SApple OSS Distributions 		T_PASS("NVram obliterate not invoked");
178*5e3eaea3SApple OSS Distributions 	}
179*5e3eaea3SApple OSS Distributions 
180*5e3eaea3SApple OSS Distributions 	ReleaseOptionsRef(optionsRef);
181*5e3eaea3SApple OSS Distributions }
182*5e3eaea3SApple OSS Distributions #endif /* ((TARGET_OS_OSX) && !(__x86_64__)) */
183