xref: /xnu-10063.121.3/iokit/Kernel/IONVRAMV3Handler.cpp (revision 2c2f96dc2b9a4408a43d3150ae9c105355ca3daa)
1*2c2f96dcSApple OSS Distributions /*
2*2c2f96dcSApple OSS Distributions  * Copyright (c) 2021-2022 Apple Inc. All rights reserved.
3*2c2f96dcSApple OSS Distributions  *
4*2c2f96dcSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*2c2f96dcSApple OSS Distributions  *
6*2c2f96dcSApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*2c2f96dcSApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*2c2f96dcSApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*2c2f96dcSApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*2c2f96dcSApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*2c2f96dcSApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*2c2f96dcSApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*2c2f96dcSApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*2c2f96dcSApple OSS Distributions  *
15*2c2f96dcSApple OSS Distributions  * Please obtain a copy of the License at
16*2c2f96dcSApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*2c2f96dcSApple OSS Distributions  *
18*2c2f96dcSApple OSS Distributions  * The Original Code and all software distributed under the License are
19*2c2f96dcSApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*2c2f96dcSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*2c2f96dcSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*2c2f96dcSApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*2c2f96dcSApple OSS Distributions  * Please see the License for the specific language governing rights and
24*2c2f96dcSApple OSS Distributions  * limitations under the License.
25*2c2f96dcSApple OSS Distributions  *
26*2c2f96dcSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*2c2f96dcSApple OSS Distributions  */
28*2c2f96dcSApple OSS Distributions 
29*2c2f96dcSApple OSS Distributions #include <libkern/libkern.h>
30*2c2f96dcSApple OSS Distributions 
31*2c2f96dcSApple OSS Distributions #define VARIABLE_STORE_SIGNATURE         'NVV3'
32*2c2f96dcSApple OSS Distributions 
33*2c2f96dcSApple OSS Distributions // Variable Store Version
34*2c2f96dcSApple OSS Distributions #define VARIABLE_STORE_VERSION           0x1
35*2c2f96dcSApple OSS Distributions 
36*2c2f96dcSApple OSS Distributions #define VARIABLE_DATA                    0x55AA
37*2c2f96dcSApple OSS Distributions #define INVALIDATED_VARIABLE_DATA        0x0000
38*2c2f96dcSApple OSS Distributions 
39*2c2f96dcSApple OSS Distributions // Variable State flags
40*2c2f96dcSApple OSS Distributions #define VAR_IN_DELETED_TRANSITION     0xFE  // Variable is in obsolete transistion
41*2c2f96dcSApple OSS Distributions #define VAR_DELETED                   0xFD  // Variable is obsolete
42*2c2f96dcSApple OSS Distributions #define VAR_INACTIVE                  0xFB  // Variable is inactive due to failing CRC
43*2c2f96dcSApple OSS Distributions #define VAR_ADDED                     0x7F  // Variable has been completely added
44*2c2f96dcSApple OSS Distributions 
45*2c2f96dcSApple OSS Distributions // No changes needed on save
46*2c2f96dcSApple OSS Distributions #define VAR_NEW_STATE_NONE            0x01
47*2c2f96dcSApple OSS Distributions // Remove existing entry on save
48*2c2f96dcSApple OSS Distributions #define VAR_NEW_STATE_REMOVE          0x02
49*2c2f96dcSApple OSS Distributions // Add new value on save, mark previous as inactive
50*2c2f96dcSApple OSS Distributions #define VAR_NEW_STATE_APPEND          0x03
51*2c2f96dcSApple OSS Distributions 
52*2c2f96dcSApple OSS Distributions #pragma pack(1)
53*2c2f96dcSApple OSS Distributions struct v3_store_header {
54*2c2f96dcSApple OSS Distributions 	uint32_t     name;
55*2c2f96dcSApple OSS Distributions 	uint32_t     size;
56*2c2f96dcSApple OSS Distributions 	uint32_t     generation;
57*2c2f96dcSApple OSS Distributions 	uint8_t      state;
58*2c2f96dcSApple OSS Distributions 	uint8_t      flags;
59*2c2f96dcSApple OSS Distributions 	uint8_t      version;
60*2c2f96dcSApple OSS Distributions 	uint8_t      reserved1;
61*2c2f96dcSApple OSS Distributions 	uint32_t     system_size;
62*2c2f96dcSApple OSS Distributions 	uint32_t     common_size;
63*2c2f96dcSApple OSS Distributions };
64*2c2f96dcSApple OSS Distributions 
65*2c2f96dcSApple OSS Distributions struct v3_var_header {
66*2c2f96dcSApple OSS Distributions 	uint16_t     startId;
67*2c2f96dcSApple OSS Distributions 	uint8_t      state;
68*2c2f96dcSApple OSS Distributions 	uint8_t      reserved;
69*2c2f96dcSApple OSS Distributions 	uint32_t     attributes;
70*2c2f96dcSApple OSS Distributions 	uint32_t     nameSize;
71*2c2f96dcSApple OSS Distributions 	uint32_t     dataSize;
72*2c2f96dcSApple OSS Distributions 	uuid_t       guid;
73*2c2f96dcSApple OSS Distributions 	uint32_t     crc;
74*2c2f96dcSApple OSS Distributions 	uint8_t      name_data_buf[];
75*2c2f96dcSApple OSS Distributions };
76*2c2f96dcSApple OSS Distributions #pragma pack()
77*2c2f96dcSApple OSS Distributions 
78*2c2f96dcSApple OSS Distributions struct nvram_v3_var_entry {
79*2c2f96dcSApple OSS Distributions 	uint8_t                new_state;
80*2c2f96dcSApple OSS Distributions 	size_t                 existing_offset;
81*2c2f96dcSApple OSS Distributions 	struct v3_var_header   header;
82*2c2f96dcSApple OSS Distributions };
83*2c2f96dcSApple OSS Distributions 
84*2c2f96dcSApple OSS Distributions static size_t
nvram_v3_var_container_size(const struct v3_var_header * header)85*2c2f96dcSApple OSS Distributions nvram_v3_var_container_size(const struct v3_var_header *header)
86*2c2f96dcSApple OSS Distributions {
87*2c2f96dcSApple OSS Distributions 	return sizeof(struct nvram_v3_var_entry) + header->nameSize + header->dataSize;
88*2c2f96dcSApple OSS Distributions }
89*2c2f96dcSApple OSS Distributions 
90*2c2f96dcSApple OSS Distributions static size_t
variable_length(const struct v3_var_header * header)91*2c2f96dcSApple OSS Distributions variable_length(const struct v3_var_header *header)
92*2c2f96dcSApple OSS Distributions {
93*2c2f96dcSApple OSS Distributions 	return sizeof(struct v3_var_header) + header->nameSize + header->dataSize;
94*2c2f96dcSApple OSS Distributions }
95*2c2f96dcSApple OSS Distributions 
96*2c2f96dcSApple OSS Distributions static bool
valid_store_header(const struct v3_store_header * header)97*2c2f96dcSApple OSS Distributions valid_store_header(const struct v3_store_header *header)
98*2c2f96dcSApple OSS Distributions {
99*2c2f96dcSApple OSS Distributions 	return (header->name == VARIABLE_STORE_SIGNATURE) && (header->version == VARIABLE_STORE_VERSION);
100*2c2f96dcSApple OSS Distributions }
101*2c2f96dcSApple OSS Distributions 
102*2c2f96dcSApple OSS Distributions static bool
valid_variable_header(const struct v3_var_header * header,size_t buf_len)103*2c2f96dcSApple OSS Distributions valid_variable_header(const struct v3_var_header *header, size_t buf_len)
104*2c2f96dcSApple OSS Distributions {
105*2c2f96dcSApple OSS Distributions 	return (buf_len > sizeof(struct v3_var_header)) &&
106*2c2f96dcSApple OSS Distributions 	       (header->startId == VARIABLE_DATA) &&
107*2c2f96dcSApple OSS Distributions 	       (variable_length(header) <= buf_len);
108*2c2f96dcSApple OSS Distributions }
109*2c2f96dcSApple OSS Distributions 
110*2c2f96dcSApple OSS Distributions static uint32_t
find_active_var_in_image(const struct v3_var_header * var,const uint8_t * image,uint32_t offset,uint32_t len)111*2c2f96dcSApple OSS Distributions find_active_var_in_image(const struct v3_var_header *var, const uint8_t *image, uint32_t offset, uint32_t len)
112*2c2f96dcSApple OSS Distributions {
113*2c2f96dcSApple OSS Distributions 	const struct v3_var_header *store_var;
114*2c2f96dcSApple OSS Distributions 	uint32_t var_offset = 0;
115*2c2f96dcSApple OSS Distributions 
116*2c2f96dcSApple OSS Distributions 	while ((offset + sizeof(struct v3_var_header) < len)) {
117*2c2f96dcSApple OSS Distributions 		store_var = (const struct v3_var_header *)(image + offset);
118*2c2f96dcSApple OSS Distributions 
119*2c2f96dcSApple OSS Distributions 		if (valid_variable_header(store_var, len - offset)) {
120*2c2f96dcSApple OSS Distributions 			if ((store_var->state == VAR_ADDED) &&
121*2c2f96dcSApple OSS Distributions 			    (uuid_compare(var->guid, store_var->guid) == 0) &&
122*2c2f96dcSApple OSS Distributions 			    (var->nameSize == store_var->nameSize) &&
123*2c2f96dcSApple OSS Distributions 			    (memcmp(var->name_data_buf, store_var->name_data_buf, var->nameSize) == 0)) {
124*2c2f96dcSApple OSS Distributions 				var_offset = offset;
125*2c2f96dcSApple OSS Distributions 				break;
126*2c2f96dcSApple OSS Distributions 			}
127*2c2f96dcSApple OSS Distributions 		} else {
128*2c2f96dcSApple OSS Distributions 			break;
129*2c2f96dcSApple OSS Distributions 		}
130*2c2f96dcSApple OSS Distributions 
131*2c2f96dcSApple OSS Distributions 		offset += variable_length(store_var);
132*2c2f96dcSApple OSS Distributions 	}
133*2c2f96dcSApple OSS Distributions 
134*2c2f96dcSApple OSS Distributions 	return var_offset;
135*2c2f96dcSApple OSS Distributions }
136*2c2f96dcSApple OSS Distributions 
137*2c2f96dcSApple OSS Distributions static IOReturn
find_current_offset_in_image(const uint8_t * image,uint32_t len,uint32_t * newOffset)138*2c2f96dcSApple OSS Distributions find_current_offset_in_image(const uint8_t *image, uint32_t len, uint32_t *newOffset)
139*2c2f96dcSApple OSS Distributions {
140*2c2f96dcSApple OSS Distributions 	uint32_t offset = 0;
141*2c2f96dcSApple OSS Distributions 	uint32_t inner_offset = 0;
142*2c2f96dcSApple OSS Distributions 
143*2c2f96dcSApple OSS Distributions 	if (valid_store_header((const struct v3_store_header *)(image + offset))) {
144*2c2f96dcSApple OSS Distributions 		DEBUG_INFO("valid store header @ %#x\n", offset);
145*2c2f96dcSApple OSS Distributions 		offset += sizeof(struct v3_store_header);
146*2c2f96dcSApple OSS Distributions 	}
147*2c2f96dcSApple OSS Distributions 
148*2c2f96dcSApple OSS Distributions 	while (offset < len) {
149*2c2f96dcSApple OSS Distributions 		const struct v3_var_header *store_var = (const struct v3_var_header *)(image + offset);
150*2c2f96dcSApple OSS Distributions 		uuid_string_t uuidString;
151*2c2f96dcSApple OSS Distributions 
152*2c2f96dcSApple OSS Distributions 		if (valid_variable_header(store_var, len - offset)) {
153*2c2f96dcSApple OSS Distributions 			uuid_unparse(store_var->guid, uuidString);
154*2c2f96dcSApple OSS Distributions 			DEBUG_INFO("Valid var @ %#08x, state=%#02x, length=%#08zx, %s:%s\n", offset, store_var->state,
155*2c2f96dcSApple OSS Distributions 			    variable_length(store_var), uuidString, store_var->name_data_buf);
156*2c2f96dcSApple OSS Distributions 			offset += variable_length(store_var);
157*2c2f96dcSApple OSS Distributions 		} else {
158*2c2f96dcSApple OSS Distributions 			break;
159*2c2f96dcSApple OSS Distributions 		}
160*2c2f96dcSApple OSS Distributions 	}
161*2c2f96dcSApple OSS Distributions 
162*2c2f96dcSApple OSS Distributions 	while (offset < len) {
163*2c2f96dcSApple OSS Distributions 		if (image[offset] == 0xFF) {
164*2c2f96dcSApple OSS Distributions 			DEBUG_INFO("scanning for clear memory @ %#x\n", offset);
165*2c2f96dcSApple OSS Distributions 
166*2c2f96dcSApple OSS Distributions 			inner_offset = offset;
167*2c2f96dcSApple OSS Distributions 
168*2c2f96dcSApple OSS Distributions 			while ((inner_offset < len) && (image[inner_offset] == 0xFF)) {
169*2c2f96dcSApple OSS Distributions 				inner_offset++;
170*2c2f96dcSApple OSS Distributions 			}
171*2c2f96dcSApple OSS Distributions 
172*2c2f96dcSApple OSS Distributions 			if (inner_offset == len) {
173*2c2f96dcSApple OSS Distributions 				DEBUG_INFO("found start of clear mem @ %#x\n", offset);
174*2c2f96dcSApple OSS Distributions 				break;
175*2c2f96dcSApple OSS Distributions 			} else {
176*2c2f96dcSApple OSS Distributions 				DEBUG_ERROR("ERROR!!!!! found non-clear byte @ %#x\n", offset);
177*2c2f96dcSApple OSS Distributions 				return kIOReturnInvalid;
178*2c2f96dcSApple OSS Distributions 			}
179*2c2f96dcSApple OSS Distributions 		}
180*2c2f96dcSApple OSS Distributions 		offset++;
181*2c2f96dcSApple OSS Distributions 	}
182*2c2f96dcSApple OSS Distributions 
183*2c2f96dcSApple OSS Distributions 	*newOffset = offset;
184*2c2f96dcSApple OSS Distributions 
185*2c2f96dcSApple OSS Distributions 	return kIOReturnSuccess;
186*2c2f96dcSApple OSS Distributions }
187*2c2f96dcSApple OSS Distributions 
188*2c2f96dcSApple OSS Distributions class IONVRAMV3Handler : public IODTNVRAMFormatHandler, IOTypedOperatorsMixin<IONVRAMV3Handler>
189*2c2f96dcSApple OSS Distributions {
190*2c2f96dcSApple OSS Distributions private:
191*2c2f96dcSApple OSS Distributions 	IONVRAMController            *_nvramController;
192*2c2f96dcSApple OSS Distributions 	IODTNVRAM                    *_provider;
193*2c2f96dcSApple OSS Distributions 
194*2c2f96dcSApple OSS Distributions 	bool                         _newData;
195*2c2f96dcSApple OSS Distributions 	bool                         _resetData;
196*2c2f96dcSApple OSS Distributions 	bool                         _reload;
197*2c2f96dcSApple OSS Distributions 
198*2c2f96dcSApple OSS Distributions 	bool                         _rawController;
199*2c2f96dcSApple OSS Distributions 
200*2c2f96dcSApple OSS Distributions 	uint32_t                     _generation;
201*2c2f96dcSApple OSS Distributions 
202*2c2f96dcSApple OSS Distributions 	uint8_t                      *_nvramImage;
203*2c2f96dcSApple OSS Distributions 
204*2c2f96dcSApple OSS Distributions 	OSSharedPtr<OSDictionary>    &_varDict;
205*2c2f96dcSApple OSS Distributions 
206*2c2f96dcSApple OSS Distributions 	uint32_t                     _commonSize;
207*2c2f96dcSApple OSS Distributions 	uint32_t                     _systemSize;
208*2c2f96dcSApple OSS Distributions 
209*2c2f96dcSApple OSS Distributions 	uint32_t                     _commonUsed;
210*2c2f96dcSApple OSS Distributions 	uint32_t                     _systemUsed;
211*2c2f96dcSApple OSS Distributions 
212*2c2f96dcSApple OSS Distributions 	uint32_t                     _currentOffset;
213*2c2f96dcSApple OSS Distributions 
214*2c2f96dcSApple OSS Distributions 	OSSharedPtr<OSArray>         _varEntries;
215*2c2f96dcSApple OSS Distributions 
216*2c2f96dcSApple OSS Distributions 	IOReturn unserializeImage(const uint8_t *image, IOByteCount length);
217*2c2f96dcSApple OSS Distributions 	IOReturn reclaim(void);
218*2c2f96dcSApple OSS Distributions 	uint32_t findCurrentBank(void);
219*2c2f96dcSApple OSS Distributions 	size_t   getAppendSize(void);
220*2c2f96dcSApple OSS Distributions 
221*2c2f96dcSApple OSS Distributions 	static bool convertObjectToProp(uint8_t *buffer, uint32_t *length, const char *propSymbol, OSObject *propObject);
222*2c2f96dcSApple OSS Distributions 	static bool convertPropToObject(const uint8_t *propName, uint32_t propNameLength, const uint8_t *propData, uint32_t propDataLength,
223*2c2f96dcSApple OSS Distributions 	    OSSharedPtr<const OSSymbol>& propSymbol, OSSharedPtr<OSObject>& propObject);
224*2c2f96dcSApple OSS Distributions 
225*2c2f96dcSApple OSS Distributions 	IOReturn reloadInternal(void);
226*2c2f96dcSApple OSS Distributions 	IOReturn setVariableInternal(const uuid_t varGuid, const char *variableName, OSObject *object);
227*2c2f96dcSApple OSS Distributions 
228*2c2f96dcSApple OSS Distributions 	void setEntryForRemove(struct nvram_v3_var_entry *v3Entry, bool system);
229*2c2f96dcSApple OSS Distributions 	void findExistingEntry(const uuid_t varGuid, const char *varName, struct nvram_v3_var_entry **existing, unsigned int *existingIndex);
230*2c2f96dcSApple OSS Distributions 	IOReturn syncRaw(void);
231*2c2f96dcSApple OSS Distributions 	IOReturn syncBlock(void);
232*2c2f96dcSApple OSS Distributions 
233*2c2f96dcSApple OSS Distributions public:
234*2c2f96dcSApple OSS Distributions 	virtual
235*2c2f96dcSApple OSS Distributions 	~IONVRAMV3Handler() APPLE_KEXT_OVERRIDE;
236*2c2f96dcSApple OSS Distributions 	IONVRAMV3Handler(OSSharedPtr<OSDictionary> &varDict);
237*2c2f96dcSApple OSS Distributions 
238*2c2f96dcSApple OSS Distributions 	static bool isValidImage(const uint8_t *image, IOByteCount length);
239*2c2f96dcSApple OSS Distributions 
240*2c2f96dcSApple OSS Distributions 	static  IONVRAMV3Handler *init(IODTNVRAM *provider, const uint8_t *image, IOByteCount length,
241*2c2f96dcSApple OSS Distributions 	    OSSharedPtr<OSDictionary> &varDict);
242*2c2f96dcSApple OSS Distributions 
243*2c2f96dcSApple OSS Distributions 	virtual bool     getNVRAMProperties(void) APPLE_KEXT_OVERRIDE;
244*2c2f96dcSApple OSS Distributions 	virtual IOReturn unserializeVariables(void) APPLE_KEXT_OVERRIDE;
245*2c2f96dcSApple OSS Distributions 	virtual IOReturn setVariable(const uuid_t varGuid, const char *variableName, OSObject *object) APPLE_KEXT_OVERRIDE;
246*2c2f96dcSApple OSS Distributions 	virtual bool     setController(IONVRAMController *controller) APPLE_KEXT_OVERRIDE;
247*2c2f96dcSApple OSS Distributions 	virtual IOReturn sync(void) APPLE_KEXT_OVERRIDE;
248*2c2f96dcSApple OSS Distributions 	virtual IOReturn flush(const uuid_t guid, IONVRAMOperation op) APPLE_KEXT_OVERRIDE;
249*2c2f96dcSApple OSS Distributions 	virtual void     reload(void) APPLE_KEXT_OVERRIDE;
250*2c2f96dcSApple OSS Distributions 	virtual uint32_t getGeneration(void) const APPLE_KEXT_OVERRIDE;
251*2c2f96dcSApple OSS Distributions 	virtual uint32_t getVersion(void) const APPLE_KEXT_OVERRIDE;
252*2c2f96dcSApple OSS Distributions 	virtual uint32_t getSystemUsed(void) const APPLE_KEXT_OVERRIDE;
253*2c2f96dcSApple OSS Distributions 	virtual uint32_t getCommonUsed(void) const APPLE_KEXT_OVERRIDE;
254*2c2f96dcSApple OSS Distributions 	virtual bool     getSystemPartitionActive(void) const APPLE_KEXT_OVERRIDE;
255*2c2f96dcSApple OSS Distributions };
256*2c2f96dcSApple OSS Distributions 
~IONVRAMV3Handler()257*2c2f96dcSApple OSS Distributions IONVRAMV3Handler::~IONVRAMV3Handler()
258*2c2f96dcSApple OSS Distributions {
259*2c2f96dcSApple OSS Distributions }
260*2c2f96dcSApple OSS Distributions 
IONVRAMV3Handler(OSSharedPtr<OSDictionary> & varDict)261*2c2f96dcSApple OSS Distributions IONVRAMV3Handler::IONVRAMV3Handler(OSSharedPtr<OSDictionary> &varDict) :
262*2c2f96dcSApple OSS Distributions 	_varDict(varDict)
263*2c2f96dcSApple OSS Distributions {
264*2c2f96dcSApple OSS Distributions }
265*2c2f96dcSApple OSS Distributions 
266*2c2f96dcSApple OSS Distributions bool
isValidImage(const uint8_t * image,IOByteCount length)267*2c2f96dcSApple OSS Distributions IONVRAMV3Handler::isValidImage(const uint8_t *image, IOByteCount length)
268*2c2f96dcSApple OSS Distributions {
269*2c2f96dcSApple OSS Distributions 	const struct v3_store_header *header = (const struct v3_store_header *)image;
270*2c2f96dcSApple OSS Distributions 
271*2c2f96dcSApple OSS Distributions 	if ((header == nullptr) || (length < sizeof(*header))) {
272*2c2f96dcSApple OSS Distributions 		return false;
273*2c2f96dcSApple OSS Distributions 	}
274*2c2f96dcSApple OSS Distributions 
275*2c2f96dcSApple OSS Distributions 	return valid_store_header(header);
276*2c2f96dcSApple OSS Distributions }
277*2c2f96dcSApple OSS Distributions 
278*2c2f96dcSApple OSS Distributions IONVRAMV3Handler*
init(IODTNVRAM * provider,const uint8_t * image,IOByteCount length,OSSharedPtr<OSDictionary> & varDict)279*2c2f96dcSApple OSS Distributions IONVRAMV3Handler::init(IODTNVRAM *provider, const uint8_t *image, IOByteCount length,
280*2c2f96dcSApple OSS Distributions     OSSharedPtr<OSDictionary> &varDict)
281*2c2f96dcSApple OSS Distributions {
282*2c2f96dcSApple OSS Distributions 	OSSharedPtr<IORegistryEntry> entry;
283*2c2f96dcSApple OSS Distributions 	OSSharedPtr<OSObject>        prop;
284*2c2f96dcSApple OSS Distributions 	bool                         propertiesOk;
285*2c2f96dcSApple OSS Distributions 
286*2c2f96dcSApple OSS Distributions 	IONVRAMV3Handler *handler = new IONVRAMV3Handler(varDict);
287*2c2f96dcSApple OSS Distributions 
288*2c2f96dcSApple OSS Distributions 	handler->_provider = provider;
289*2c2f96dcSApple OSS Distributions 
290*2c2f96dcSApple OSS Distributions 	propertiesOk = handler->getNVRAMProperties();
291*2c2f96dcSApple OSS Distributions 	require_action(propertiesOk, exit, DEBUG_ERROR("Unable to get NVRAM properties\n"));
292*2c2f96dcSApple OSS Distributions 
293*2c2f96dcSApple OSS Distributions 	require_action(length == handler->_bankSize, exit, DEBUG_ERROR("length %#llx != _bankSize %#x\n", length, handler->_bankSize));
294*2c2f96dcSApple OSS Distributions 
295*2c2f96dcSApple OSS Distributions 	if ((image != nullptr) && (length != 0)) {
296*2c2f96dcSApple OSS Distributions 		if (handler->unserializeImage(image, length) != kIOReturnSuccess) {
297*2c2f96dcSApple OSS Distributions 			DEBUG_ERROR("Unable to unserialize image, len=%#x\n", (unsigned int)length);
298*2c2f96dcSApple OSS Distributions 		}
299*2c2f96dcSApple OSS Distributions 	}
300*2c2f96dcSApple OSS Distributions 
301*2c2f96dcSApple OSS Distributions 	return handler;
302*2c2f96dcSApple OSS Distributions 
303*2c2f96dcSApple OSS Distributions exit:
304*2c2f96dcSApple OSS Distributions 	delete handler;
305*2c2f96dcSApple OSS Distributions 
306*2c2f96dcSApple OSS Distributions 	return nullptr;
307*2c2f96dcSApple OSS Distributions }
308*2c2f96dcSApple OSS Distributions 
309*2c2f96dcSApple OSS Distributions bool
getNVRAMProperties()310*2c2f96dcSApple OSS Distributions IONVRAMV3Handler::getNVRAMProperties()
311*2c2f96dcSApple OSS Distributions {
312*2c2f96dcSApple OSS Distributions 	bool                         ok    = false;
313*2c2f96dcSApple OSS Distributions 	const char                   *rawControllerKey = "nvram-raw";
314*2c2f96dcSApple OSS Distributions 	OSSharedPtr<IORegistryEntry> entry;
315*2c2f96dcSApple OSS Distributions 	OSSharedPtr<OSObject>        prop;
316*2c2f96dcSApple OSS Distributions 	OSData *                     data;
317*2c2f96dcSApple OSS Distributions 
318*2c2f96dcSApple OSS Distributions 	require_action(IODTNVRAMFormatHandler::getNVRAMProperties(), exit, DEBUG_ERROR("parent getNVRAMProperties failed\n"));
319*2c2f96dcSApple OSS Distributions 
320*2c2f96dcSApple OSS Distributions 	entry = IORegistryEntry::fromPath("/chosen", gIODTPlane);
321*2c2f96dcSApple OSS Distributions 	require_action(entry, exit, DEBUG_ERROR("Unable to find chosen node\n"));
322*2c2f96dcSApple OSS Distributions 
323*2c2f96dcSApple OSS Distributions 	prop = entry->copyProperty(rawControllerKey);
324*2c2f96dcSApple OSS Distributions 	require_action(prop != nullptr, exit, DEBUG_ERROR("No %s entry\n", rawControllerKey));
325*2c2f96dcSApple OSS Distributions 
326*2c2f96dcSApple OSS Distributions 	data = OSDynamicCast(OSData, prop.get());
327*2c2f96dcSApple OSS Distributions 	require(data != nullptr, exit);
328*2c2f96dcSApple OSS Distributions 
329*2c2f96dcSApple OSS Distributions 	_rawController = *((uint32_t*)data->getBytesNoCopy());
330*2c2f96dcSApple OSS Distributions 	DEBUG_INFO("_rawController = %d\n", _rawController);
331*2c2f96dcSApple OSS Distributions 
332*2c2f96dcSApple OSS Distributions 	ok = true;
333*2c2f96dcSApple OSS Distributions 
334*2c2f96dcSApple OSS Distributions exit:
335*2c2f96dcSApple OSS Distributions 	return ok;
336*2c2f96dcSApple OSS Distributions }
337*2c2f96dcSApple OSS Distributions 
338*2c2f96dcSApple OSS Distributions IOReturn
flush(const uuid_t guid,IONVRAMOperation op)339*2c2f96dcSApple OSS Distributions IONVRAMV3Handler::flush(const uuid_t guid, IONVRAMOperation op)
340*2c2f96dcSApple OSS Distributions {
341*2c2f96dcSApple OSS Distributions 	IOReturn ret = kIOReturnSuccess;
342*2c2f96dcSApple OSS Distributions 	bool     flushSystem;
343*2c2f96dcSApple OSS Distributions 	bool     flushCommon;
344*2c2f96dcSApple OSS Distributions 
345*2c2f96dcSApple OSS Distributions 	flushSystem = getSystemPartitionActive() && (uuid_compare(guid, gAppleSystemVariableGuid) == 0);
346*2c2f96dcSApple OSS Distributions 	flushCommon = uuid_compare(guid, gAppleNVRAMGuid) == 0;
347*2c2f96dcSApple OSS Distributions 
348*2c2f96dcSApple OSS Distributions 	DEBUG_INFO("flushSystem=%d, flushCommon=%d\n", flushSystem, flushCommon);
349*2c2f96dcSApple OSS Distributions 
350*2c2f96dcSApple OSS Distributions 	if (flushSystem || flushCommon) {
351*2c2f96dcSApple OSS Distributions 		const OSSymbol                    *canonicalKey;
352*2c2f96dcSApple OSS Distributions 		OSSharedPtr<OSDictionary>         dictCopy;
353*2c2f96dcSApple OSS Distributions 		OSSharedPtr<OSCollectionIterator> iter;
354*2c2f96dcSApple OSS Distributions 		uuid_string_t                     uuidString;
355*2c2f96dcSApple OSS Distributions 
356*2c2f96dcSApple OSS Distributions 		dictCopy = OSDictionary::withDictionary(_varDict.get());
357*2c2f96dcSApple OSS Distributions 		iter = OSCollectionIterator::withCollection(dictCopy.get());
358*2c2f96dcSApple OSS Distributions 		require_action(dictCopy && iter, exit, ret = kIOReturnNoMemory);
359*2c2f96dcSApple OSS Distributions 
360*2c2f96dcSApple OSS Distributions 		while ((canonicalKey = OSDynamicCast(OSSymbol, iter->getNextObject()))) {
361*2c2f96dcSApple OSS Distributions 			const char *varName;
362*2c2f96dcSApple OSS Distributions 			uuid_t     varGuid;
363*2c2f96dcSApple OSS Distributions 			bool       clear;
364*2c2f96dcSApple OSS Distributions 
365*2c2f96dcSApple OSS Distributions 			parseVariableName(canonicalKey->getCStringNoCopy(), &varGuid, &varName);
366*2c2f96dcSApple OSS Distributions 
367*2c2f96dcSApple OSS Distributions 			uuid_unparse(varGuid, uuidString);
368*2c2f96dcSApple OSS Distributions 
369*2c2f96dcSApple OSS Distributions 			clear = ((flushSystem && (uuid_compare(varGuid, gAppleSystemVariableGuid) == 0)) ||
370*2c2f96dcSApple OSS Distributions 			    (flushCommon && (uuid_compare(varGuid, gAppleSystemVariableGuid) != 0))) &&
371*2c2f96dcSApple OSS Distributions 			    verifyPermission(op, varGuid, varName, getSystemPartitionActive());
372*2c2f96dcSApple OSS Distributions 
373*2c2f96dcSApple OSS Distributions 			if (clear) {
374*2c2f96dcSApple OSS Distributions 				DEBUG_INFO("Clearing entry for %s:%s\n", uuidString, varName);
375*2c2f96dcSApple OSS Distributions 				setVariableInternal(varGuid, varName, nullptr);
376*2c2f96dcSApple OSS Distributions 			} else {
377*2c2f96dcSApple OSS Distributions 				DEBUG_INFO("Keeping entry for %s:%s\n", uuidString, varName);
378*2c2f96dcSApple OSS Distributions 			}
379*2c2f96dcSApple OSS Distributions 		}
380*2c2f96dcSApple OSS Distributions 
381*2c2f96dcSApple OSS Distributions 		_newData = true;
382*2c2f96dcSApple OSS Distributions 	}
383*2c2f96dcSApple OSS Distributions 
384*2c2f96dcSApple OSS Distributions 	DEBUG_INFO("_commonUsed %#x, _systemUsed %#x\n", _commonUsed, _systemUsed);
385*2c2f96dcSApple OSS Distributions 
386*2c2f96dcSApple OSS Distributions exit:
387*2c2f96dcSApple OSS Distributions 	return ret;
388*2c2f96dcSApple OSS Distributions }
389*2c2f96dcSApple OSS Distributions 
390*2c2f96dcSApple OSS Distributions IOReturn
reloadInternal(void)391*2c2f96dcSApple OSS Distributions IONVRAMV3Handler::reloadInternal(void)
392*2c2f96dcSApple OSS Distributions {
393*2c2f96dcSApple OSS Distributions 	IOReturn                     ret;
394*2c2f96dcSApple OSS Distributions 	uint32_t                     controllerBank;
395*2c2f96dcSApple OSS Distributions 	uint8_t                      *controllerImage;
396*2c2f96dcSApple OSS Distributions 	struct nvram_v3_var_entry    *v3Entry;
397*2c2f96dcSApple OSS Distributions 	const struct v3_store_header *storeHeader;
398*2c2f96dcSApple OSS Distributions 	const struct v3_var_header   *storeVar;
399*2c2f96dcSApple OSS Distributions 	OSData                       *entryContainer;
400*2c2f96dcSApple OSS Distributions 
401*2c2f96dcSApple OSS Distributions 	controllerBank = findCurrentBank();
402*2c2f96dcSApple OSS Distributions 
403*2c2f96dcSApple OSS Distributions 	if (_currentBank != controllerBank) {
404*2c2f96dcSApple OSS Distributions 		DEBUG_ERROR("_currentBank %#x != controllerBank %#x", _currentBank, controllerBank);
405*2c2f96dcSApple OSS Distributions 	}
406*2c2f96dcSApple OSS Distributions 
407*2c2f96dcSApple OSS Distributions 	_currentBank = controllerBank;
408*2c2f96dcSApple OSS Distributions 
409*2c2f96dcSApple OSS Distributions 	controllerImage = (uint8_t *)IOMallocData(_bankSize);
410*2c2f96dcSApple OSS Distributions 
411*2c2f96dcSApple OSS Distributions 	_nvramController->select(_currentBank);
412*2c2f96dcSApple OSS Distributions 	_nvramController->read(0, controllerImage, _bankSize);
413*2c2f96dcSApple OSS Distributions 
414*2c2f96dcSApple OSS Distributions 	require_action(isValidImage(controllerImage, _bankSize), exit,
415*2c2f96dcSApple OSS Distributions 	    (ret = kIOReturnInvalid, DEBUG_ERROR("Invalid image at bank %d\n", _currentBank)));
416*2c2f96dcSApple OSS Distributions 
417*2c2f96dcSApple OSS Distributions 	DEBUG_INFO("valid image found\n");
418*2c2f96dcSApple OSS Distributions 
419*2c2f96dcSApple OSS Distributions 	storeHeader = (const struct v3_store_header *)controllerImage;
420*2c2f96dcSApple OSS Distributions 
421*2c2f96dcSApple OSS Distributions 	_generation = storeHeader->generation;
422*2c2f96dcSApple OSS Distributions 
423*2c2f96dcSApple OSS Distributions 	// We must sync any existing variables offset on the controller image with our internal representation
424*2c2f96dcSApple OSS Distributions 	// If we find an existing entry and the data is still the same we record the existing offset and mark it
425*2c2f96dcSApple OSS Distributions 	// as VAR_NEW_STATE_NONE meaning no action needed
426*2c2f96dcSApple OSS Distributions 	// Otherwise if the data is different or it is not found on the controller image we mark it as VAR_NEW_STATE_APPEND
427*2c2f96dcSApple OSS Distributions 	// which will have us invalidate the existing entry if there is one and append it on the next save
428*2c2f96dcSApple OSS Distributions 	for (unsigned int i = 0; i < _varEntries->getCount(); i++) {
429*2c2f96dcSApple OSS Distributions 		uint32_t offset = sizeof(struct v3_store_header);
430*2c2f96dcSApple OSS Distributions 		uint32_t latestOffset;
431*2c2f96dcSApple OSS Distributions 		uint32_t prevOffset = 0;
432*2c2f96dcSApple OSS Distributions 
433*2c2f96dcSApple OSS Distributions 		entryContainer = (OSDynamicCast(OSData, _varEntries->getObject(i)));
434*2c2f96dcSApple OSS Distributions 		v3Entry = (struct nvram_v3_var_entry *)entryContainer->getBytesNoCopy();
435*2c2f96dcSApple OSS Distributions 
436*2c2f96dcSApple OSS Distributions 		DEBUG_INFO("Looking for %s\n", v3Entry->header.name_data_buf);
437*2c2f96dcSApple OSS Distributions 		while ((latestOffset = find_active_var_in_image(&v3Entry->header, controllerImage, offset, _bankSize))) {
438*2c2f96dcSApple OSS Distributions 			DEBUG_INFO("Found offset for %s @ %#08x\n", v3Entry->header.name_data_buf, latestOffset);
439*2c2f96dcSApple OSS Distributions 			if (prevOffset) {
440*2c2f96dcSApple OSS Distributions 				DEBUG_INFO("Marking prev offset for %s at %#08x invalid\n", v3Entry->header.name_data_buf, offset);
441*2c2f96dcSApple OSS Distributions 				// Invalidate any previous duplicate entries in the store
442*2c2f96dcSApple OSS Distributions 				struct v3_var_header *prevVarHeader = (struct v3_var_header *)(controllerImage + prevOffset);
443*2c2f96dcSApple OSS Distributions 				uint8_t state = prevVarHeader->state & VAR_DELETED & VAR_IN_DELETED_TRANSITION;
444*2c2f96dcSApple OSS Distributions 
445*2c2f96dcSApple OSS Distributions 				ret = _nvramController->write(prevOffset + offsetof(struct v3_var_header, state), &state, sizeof(state));
446*2c2f96dcSApple OSS Distributions 				require_noerr_action(ret, exit, DEBUG_ERROR("existing state w fail, ret=%#x\n", ret));
447*2c2f96dcSApple OSS Distributions 			}
448*2c2f96dcSApple OSS Distributions 
449*2c2f96dcSApple OSS Distributions 			prevOffset = latestOffset;
450*2c2f96dcSApple OSS Distributions 			offset += latestOffset;
451*2c2f96dcSApple OSS Distributions 		}
452*2c2f96dcSApple OSS Distributions 
453*2c2f96dcSApple OSS Distributions 		v3Entry->existing_offset = latestOffset ? latestOffset : prevOffset;
454*2c2f96dcSApple OSS Distributions 		DEBUG_INFO("Existing offset for %s at %#08zx\n", v3Entry->header.name_data_buf, v3Entry->existing_offset);
455*2c2f96dcSApple OSS Distributions 
456*2c2f96dcSApple OSS Distributions 		if (v3Entry->existing_offset == 0) {
457*2c2f96dcSApple OSS Distributions 			DEBUG_ERROR("%s is not in the NOR image\n", v3Entry->header.name_data_buf);
458*2c2f96dcSApple OSS Distributions 			if (v3Entry->new_state != VAR_NEW_STATE_REMOVE) {
459*2c2f96dcSApple OSS Distributions 				DEBUG_INFO("%s marked for append\n", v3Entry->header.name_data_buf);
460*2c2f96dcSApple OSS Distributions 				// Doesn't exist in the store, just append it on next sync
461*2c2f96dcSApple OSS Distributions 				v3Entry->new_state = VAR_NEW_STATE_APPEND;
462*2c2f96dcSApple OSS Distributions 			}
463*2c2f96dcSApple OSS Distributions 		} else {
464*2c2f96dcSApple OSS Distributions 			DEBUG_INFO("Found offset for %s @ %#zx\n", v3Entry->header.name_data_buf, v3Entry->existing_offset);
465*2c2f96dcSApple OSS Distributions 			storeVar = (const struct v3_var_header *)&controllerImage[v3Entry->existing_offset];
466*2c2f96dcSApple OSS Distributions 
467*2c2f96dcSApple OSS Distributions 			if (v3Entry->new_state != VAR_NEW_STATE_REMOVE) {
468*2c2f96dcSApple OSS Distributions 				// Verify that the existing data matches the store data
469*2c2f96dcSApple OSS Distributions 				if ((variable_length(&v3Entry->header) == variable_length(storeVar)) &&
470*2c2f96dcSApple OSS Distributions 				    (memcmp(v3Entry->header.name_data_buf, storeVar->name_data_buf, storeVar->nameSize + storeVar->dataSize) == 0)) {
471*2c2f96dcSApple OSS Distributions 					DEBUG_INFO("Store var data for %s matches, marking new state none\n", v3Entry->header.name_data_buf);
472*2c2f96dcSApple OSS Distributions 					v3Entry->new_state = VAR_NEW_STATE_NONE;
473*2c2f96dcSApple OSS Distributions 				} else {
474*2c2f96dcSApple OSS Distributions 					DEBUG_INFO("Store var data for %s differs, marking new state append\n", v3Entry->header.name_data_buf);
475*2c2f96dcSApple OSS Distributions 					v3Entry->new_state = VAR_NEW_STATE_APPEND;
476*2c2f96dcSApple OSS Distributions 				}
477*2c2f96dcSApple OSS Distributions 			} else {
478*2c2f96dcSApple OSS Distributions 				// Store has entry but it has been removed from our collection, keep it marked for delete but with updated
479*2c2f96dcSApple OSS Distributions 				// existing_offset for coherence
480*2c2f96dcSApple OSS Distributions 				DEBUG_INFO("Removing entry at %#08zx with next sync\n", v3Entry->existing_offset);
481*2c2f96dcSApple OSS Distributions 			}
482*2c2f96dcSApple OSS Distributions 		}
483*2c2f96dcSApple OSS Distributions 	}
484*2c2f96dcSApple OSS Distributions 
485*2c2f96dcSApple OSS Distributions 	ret = find_current_offset_in_image(controllerImage, _bankSize, &_currentOffset);
486*2c2f96dcSApple OSS Distributions 	if (ret != kIOReturnSuccess) {
487*2c2f96dcSApple OSS Distributions 		DEBUG_ERROR("Unidentified bytes in image, reclaiming\n");
488*2c2f96dcSApple OSS Distributions 		ret = reclaim();
489*2c2f96dcSApple OSS Distributions 		require_noerr_action(ret, exit, DEBUG_ERROR("Reclaim byte recovery failed, invalid controller state!!! ret=%#x\n", ret));
490*2c2f96dcSApple OSS Distributions 	}
491*2c2f96dcSApple OSS Distributions 	DEBUG_INFO("New _currentOffset=%#x\n", _currentOffset);
492*2c2f96dcSApple OSS Distributions 
493*2c2f96dcSApple OSS Distributions exit:
494*2c2f96dcSApple OSS Distributions 	IOFreeData(controllerImage, _bankSize);
495*2c2f96dcSApple OSS Distributions 	return ret;
496*2c2f96dcSApple OSS Distributions }
497*2c2f96dcSApple OSS Distributions 
498*2c2f96dcSApple OSS Distributions void
reload(void)499*2c2f96dcSApple OSS Distributions IONVRAMV3Handler::reload(void)
500*2c2f96dcSApple OSS Distributions {
501*2c2f96dcSApple OSS Distributions 	_reload = true;
502*2c2f96dcSApple OSS Distributions 
503*2c2f96dcSApple OSS Distributions 	DEBUG_INFO("reload marked\n");
504*2c2f96dcSApple OSS Distributions }
505*2c2f96dcSApple OSS Distributions 
506*2c2f96dcSApple OSS Distributions void
setEntryForRemove(struct nvram_v3_var_entry * v3Entry,bool system)507*2c2f96dcSApple OSS Distributions IONVRAMV3Handler::setEntryForRemove(struct nvram_v3_var_entry *v3Entry, bool system)
508*2c2f96dcSApple OSS Distributions {
509*2c2f96dcSApple OSS Distributions 	OSSharedPtr<const OSSymbol> canonicalKey;
510*2c2f96dcSApple OSS Distributions 	const char                  *variableName;
511*2c2f96dcSApple OSS Distributions 	uint32_t                    variableSize;
512*2c2f96dcSApple OSS Distributions 
513*2c2f96dcSApple OSS Distributions 	require_action(v3Entry != nullptr, exit, DEBUG_INFO("remove with no entry\n"));
514*2c2f96dcSApple OSS Distributions 
515*2c2f96dcSApple OSS Distributions 	variableName = (const char *)v3Entry->header.name_data_buf;
516*2c2f96dcSApple OSS Distributions 	variableSize = (uint32_t)variable_length(&v3Entry->header);
517*2c2f96dcSApple OSS Distributions 	canonicalKey = keyWithGuidAndCString(v3Entry->header.guid, variableName);
518*2c2f96dcSApple OSS Distributions 
519*2c2f96dcSApple OSS Distributions 	if (v3Entry->new_state == VAR_NEW_STATE_REMOVE) {
520*2c2f96dcSApple OSS Distributions 		DEBUG_INFO("entry %s already marked for remove\n", variableName);
521*2c2f96dcSApple OSS Distributions 	} else {
522*2c2f96dcSApple OSS Distributions 		DEBUG_INFO("marking entry %s for remove\n", variableName);
523*2c2f96dcSApple OSS Distributions 
524*2c2f96dcSApple OSS Distributions 		v3Entry->new_state = VAR_NEW_STATE_REMOVE;
525*2c2f96dcSApple OSS Distributions 
526*2c2f96dcSApple OSS Distributions 		_provider->_varDict->removeObject(canonicalKey.get());
527*2c2f96dcSApple OSS Distributions 
528*2c2f96dcSApple OSS Distributions 		if (system) {
529*2c2f96dcSApple OSS Distributions 			if (_systemUsed < variableSize) {
530*2c2f96dcSApple OSS Distributions 				panic("Invalid _systemUsed size\n");
531*2c2f96dcSApple OSS Distributions 			}
532*2c2f96dcSApple OSS Distributions 			_systemUsed -= variableSize;
533*2c2f96dcSApple OSS Distributions 		} else {
534*2c2f96dcSApple OSS Distributions 			if (_commonUsed < variableSize) {
535*2c2f96dcSApple OSS Distributions 				panic("Invalid _commonUsed size\n");
536*2c2f96dcSApple OSS Distributions 			}
537*2c2f96dcSApple OSS Distributions 			_commonUsed -= variableSize;
538*2c2f96dcSApple OSS Distributions 		}
539*2c2f96dcSApple OSS Distributions 
540*2c2f96dcSApple OSS Distributions 		if (_provider->_diags) {
541*2c2f96dcSApple OSS Distributions 			_provider->_diags->logVariable(getPartitionTypeForGUID(v3Entry->header.guid),
542*2c2f96dcSApple OSS Distributions 			    kIONVRAMOperationDelete,
543*2c2f96dcSApple OSS Distributions 			    variableName,
544*2c2f96dcSApple OSS Distributions 			    nullptr);
545*2c2f96dcSApple OSS Distributions 		}
546*2c2f96dcSApple OSS Distributions 	}
547*2c2f96dcSApple OSS Distributions 
548*2c2f96dcSApple OSS Distributions exit:
549*2c2f96dcSApple OSS Distributions 	return;
550*2c2f96dcSApple OSS Distributions }
551*2c2f96dcSApple OSS Distributions 
552*2c2f96dcSApple OSS Distributions void
findExistingEntry(const uuid_t varGuid,const char * varName,struct nvram_v3_var_entry ** existing,unsigned int * existingIndex)553*2c2f96dcSApple OSS Distributions IONVRAMV3Handler::findExistingEntry(const uuid_t varGuid, const char *varName, struct nvram_v3_var_entry **existing, unsigned int *existingIndex)
554*2c2f96dcSApple OSS Distributions {
555*2c2f96dcSApple OSS Distributions 	struct nvram_v3_var_entry *v3Entry = nullptr;
556*2c2f96dcSApple OSS Distributions 	OSData                    *entryContainer = nullptr;
557*2c2f96dcSApple OSS Distributions 	unsigned int              index = 0;
558*2c2f96dcSApple OSS Distributions 	uint32_t                  nameLen = (uint32_t)strlen(varName) + 1;
559*2c2f96dcSApple OSS Distributions 
560*2c2f96dcSApple OSS Distributions 	for (index = 0; index < _varEntries->getCount(); index++) {
561*2c2f96dcSApple OSS Distributions 		entryContainer = (OSDynamicCast(OSData, _varEntries->getObject(index)));
562*2c2f96dcSApple OSS Distributions 		v3Entry = (struct nvram_v3_var_entry *)entryContainer->getBytesNoCopy();
563*2c2f96dcSApple OSS Distributions 
564*2c2f96dcSApple OSS Distributions 		if ((v3Entry->header.nameSize == nameLen) &&
565*2c2f96dcSApple OSS Distributions 		    (memcmp(v3Entry->header.name_data_buf, varName, nameLen) == 0)) {
566*2c2f96dcSApple OSS Distributions 			if (varGuid) {
567*2c2f96dcSApple OSS Distributions 				if (uuid_compare(varGuid, v3Entry->header.guid) == 0) {
568*2c2f96dcSApple OSS Distributions 					uuid_string_t uuidString;
569*2c2f96dcSApple OSS Distributions 					uuid_unparse(varGuid, uuidString);
570*2c2f96dcSApple OSS Distributions 					DEBUG_INFO("found existing entry for %s:%s, e_off=%#lx, len=%#lx, new_state=%#x\n", uuidString, varName,
571*2c2f96dcSApple OSS Distributions 					    v3Entry->existing_offset, variable_length(&v3Entry->header), v3Entry->new_state);
572*2c2f96dcSApple OSS Distributions 					break;
573*2c2f96dcSApple OSS Distributions 				}
574*2c2f96dcSApple OSS Distributions 			} else {
575*2c2f96dcSApple OSS Distributions 				DEBUG_INFO("found existing entry for %s, e_off=%#lx, len=%#lx\n", varName, v3Entry->existing_offset, variable_length(&v3Entry->header));
576*2c2f96dcSApple OSS Distributions 				break;
577*2c2f96dcSApple OSS Distributions 			}
578*2c2f96dcSApple OSS Distributions 		}
579*2c2f96dcSApple OSS Distributions 
580*2c2f96dcSApple OSS Distributions 		v3Entry = nullptr;
581*2c2f96dcSApple OSS Distributions 	}
582*2c2f96dcSApple OSS Distributions 
583*2c2f96dcSApple OSS Distributions 	if (v3Entry != nullptr) {
584*2c2f96dcSApple OSS Distributions 		if (existing) {
585*2c2f96dcSApple OSS Distributions 			*existing = v3Entry;
586*2c2f96dcSApple OSS Distributions 		}
587*2c2f96dcSApple OSS Distributions 
588*2c2f96dcSApple OSS Distributions 		if (existingIndex) {
589*2c2f96dcSApple OSS Distributions 			*existingIndex = index;
590*2c2f96dcSApple OSS Distributions 		}
591*2c2f96dcSApple OSS Distributions 	}
592*2c2f96dcSApple OSS Distributions }
593*2c2f96dcSApple OSS Distributions 
594*2c2f96dcSApple OSS Distributions IOReturn
unserializeImage(const uint8_t * image,IOByteCount length)595*2c2f96dcSApple OSS Distributions IONVRAMV3Handler::unserializeImage(const uint8_t *image, IOByteCount length)
596*2c2f96dcSApple OSS Distributions {
597*2c2f96dcSApple OSS Distributions 	IOReturn                     ret = kIOReturnInvalid;
598*2c2f96dcSApple OSS Distributions 	const struct v3_store_header *storeHeader;
599*2c2f96dcSApple OSS Distributions 
600*2c2f96dcSApple OSS Distributions 	require(isValidImage(image, length), exit);
601*2c2f96dcSApple OSS Distributions 
602*2c2f96dcSApple OSS Distributions 	storeHeader = (const struct v3_store_header *)image;
603*2c2f96dcSApple OSS Distributions 	require_action(storeHeader->size == (uint32_t)length, exit,
604*2c2f96dcSApple OSS Distributions 	    DEBUG_ERROR("Image size %#x != header size %#x\n", (unsigned int)length, storeHeader->size));
605*2c2f96dcSApple OSS Distributions 
606*2c2f96dcSApple OSS Distributions 	_generation = storeHeader->generation;
607*2c2f96dcSApple OSS Distributions 	_systemSize = storeHeader->system_size;
608*2c2f96dcSApple OSS Distributions 	_commonSize = storeHeader->common_size - sizeof(struct v3_store_header);
609*2c2f96dcSApple OSS Distributions 
610*2c2f96dcSApple OSS Distributions 	_systemUsed = 0;
611*2c2f96dcSApple OSS Distributions 	_commonUsed = 0;
612*2c2f96dcSApple OSS Distributions 
613*2c2f96dcSApple OSS Distributions 	if (_nvramImage) {
614*2c2f96dcSApple OSS Distributions 		IOFreeData(_nvramImage, _bankSize);
615*2c2f96dcSApple OSS Distributions 	}
616*2c2f96dcSApple OSS Distributions 
617*2c2f96dcSApple OSS Distributions 	_varEntries.reset();
618*2c2f96dcSApple OSS Distributions 	_varEntries = OSArray::withCapacity(40);
619*2c2f96dcSApple OSS Distributions 
620*2c2f96dcSApple OSS Distributions 	_nvramImage = IONewData(uint8_t, length);
621*2c2f96dcSApple OSS Distributions 	_bankSize = (uint32_t)length;
622*2c2f96dcSApple OSS Distributions 	bcopy(image, _nvramImage, _bankSize);
623*2c2f96dcSApple OSS Distributions 
624*2c2f96dcSApple OSS Distributions 	ret = kIOReturnSuccess;
625*2c2f96dcSApple OSS Distributions 
626*2c2f96dcSApple OSS Distributions exit:
627*2c2f96dcSApple OSS Distributions 	return ret;
628*2c2f96dcSApple OSS Distributions }
629*2c2f96dcSApple OSS Distributions 
630*2c2f96dcSApple OSS Distributions IOReturn
unserializeVariables(void)631*2c2f96dcSApple OSS Distributions IONVRAMV3Handler::unserializeVariables(void)
632*2c2f96dcSApple OSS Distributions {
633*2c2f96dcSApple OSS Distributions 	IOReturn                     ret = kIOReturnSuccess;
634*2c2f96dcSApple OSS Distributions 	OSSharedPtr<const OSSymbol>  propSymbol;
635*2c2f96dcSApple OSS Distributions 	OSSharedPtr<OSObject>        propObject;
636*2c2f96dcSApple OSS Distributions 	OSSharedPtr<OSData>          entryContainer;
637*2c2f96dcSApple OSS Distributions 	struct nvram_v3_var_entry    *v3Entry;
638*2c2f96dcSApple OSS Distributions 	const struct v3_var_header   *header;
639*2c2f96dcSApple OSS Distributions 	size_t                       offset = sizeof(struct v3_store_header);
640*2c2f96dcSApple OSS Distributions 	uint32_t                     crc;
641*2c2f96dcSApple OSS Distributions 	unsigned int                 i;
642*2c2f96dcSApple OSS Distributions 	bool                         system;
643*2c2f96dcSApple OSS Distributions 	uuid_string_t                uuidString;
644*2c2f96dcSApple OSS Distributions 	size_t                       existingSize;
645*2c2f96dcSApple OSS Distributions 
646*2c2f96dcSApple OSS Distributions 	if (_systemSize || _commonSize) {
647*2c2f96dcSApple OSS Distributions 		_varDict = OSDictionary::withCapacity(1);
648*2c2f96dcSApple OSS Distributions 	}
649*2c2f96dcSApple OSS Distributions 
650*2c2f96dcSApple OSS Distributions 	while ((offset + sizeof(struct v3_var_header)) < _bankSize) {
651*2c2f96dcSApple OSS Distributions 		struct nvram_v3_var_entry *existingEntry = nullptr;
652*2c2f96dcSApple OSS Distributions 		unsigned int              existingIndex = 0;
653*2c2f96dcSApple OSS Distributions 
654*2c2f96dcSApple OSS Distributions 		header = (const struct v3_var_header *)(_nvramImage + offset);
655*2c2f96dcSApple OSS Distributions 
656*2c2f96dcSApple OSS Distributions 		for (i = 0; i < sizeof(struct v3_var_header); i++) {
657*2c2f96dcSApple OSS Distributions 			if ((_nvramImage[offset + i] != 0) && (_nvramImage[offset + i] != 0xFF)) {
658*2c2f96dcSApple OSS Distributions 				break;
659*2c2f96dcSApple OSS Distributions 			}
660*2c2f96dcSApple OSS Distributions 		}
661*2c2f96dcSApple OSS Distributions 
662*2c2f96dcSApple OSS Distributions 		if (i == sizeof(struct v3_var_header)) {
663*2c2f96dcSApple OSS Distributions 			DEBUG_INFO("No more variables after offset %#lx\n", offset);
664*2c2f96dcSApple OSS Distributions 			break;
665*2c2f96dcSApple OSS Distributions 		}
666*2c2f96dcSApple OSS Distributions 
667*2c2f96dcSApple OSS Distributions 		if (!valid_variable_header(header, _bankSize - offset)) {
668*2c2f96dcSApple OSS Distributions 			DEBUG_ERROR("invalid header @ %#lx\n", offset);
669*2c2f96dcSApple OSS Distributions 			offset += sizeof(struct v3_var_header);
670*2c2f96dcSApple OSS Distributions 			continue;
671*2c2f96dcSApple OSS Distributions 		}
672*2c2f96dcSApple OSS Distributions 
673*2c2f96dcSApple OSS Distributions 		uuid_unparse(header->guid, uuidString);
674*2c2f96dcSApple OSS Distributions 		DEBUG_INFO("Valid var @ %#08zx, state=%#02x, length=%#08zx, %s:%s\n", offset, header->state,
675*2c2f96dcSApple OSS Distributions 		    variable_length(header), uuidString, header->name_data_buf);
676*2c2f96dcSApple OSS Distributions 
677*2c2f96dcSApple OSS Distributions 		if (header->state != VAR_ADDED) {
678*2c2f96dcSApple OSS Distributions 			goto skip;
679*2c2f96dcSApple OSS Distributions 		}
680*2c2f96dcSApple OSS Distributions 
681*2c2f96dcSApple OSS Distributions 		crc = crc32(0, header->name_data_buf + header->nameSize, header->dataSize);
682*2c2f96dcSApple OSS Distributions 
683*2c2f96dcSApple OSS Distributions 		if (crc != header->crc) {
684*2c2f96dcSApple OSS Distributions 			DEBUG_ERROR("invalid crc @ %#lx, calculated=%#x, read=%#x\n", offset, crc, header->crc);
685*2c2f96dcSApple OSS Distributions 			goto skip;
686*2c2f96dcSApple OSS Distributions 		}
687*2c2f96dcSApple OSS Distributions 
688*2c2f96dcSApple OSS Distributions 		v3Entry = (struct nvram_v3_var_entry *)IOMallocZeroData(nvram_v3_var_container_size(header));
689*2c2f96dcSApple OSS Distributions 		__nochk_memcpy(&v3Entry->header, _nvramImage + offset, variable_length(header));
690*2c2f96dcSApple OSS Distributions 
691*2c2f96dcSApple OSS Distributions 		// It is assumed that the initial image being unserialized here is going to be the proxy data from EDT and not the image
692*2c2f96dcSApple OSS Distributions 		// read from the controller, which for various reasons due to the setting of states and saves from iBoot, can be
693*2c2f96dcSApple OSS Distributions 		// different. We will have an initial existing_offset of 0 and once the controller is set we will read
694*2c2f96dcSApple OSS Distributions 		// out the image there and update the existing offset with what is present on the NOR image
695*2c2f96dcSApple OSS Distributions 		v3Entry->existing_offset = 0;
696*2c2f96dcSApple OSS Distributions 		v3Entry->new_state = VAR_NEW_STATE_NONE;
697*2c2f96dcSApple OSS Distributions 
698*2c2f96dcSApple OSS Distributions 		// safe guard for any strange duplicate entries in the store
699*2c2f96dcSApple OSS Distributions 		findExistingEntry(v3Entry->header.guid, (const char *)v3Entry->header.name_data_buf, &existingEntry, &existingIndex);
700*2c2f96dcSApple OSS Distributions 
701*2c2f96dcSApple OSS Distributions 		if (existingEntry != nullptr) {
702*2c2f96dcSApple OSS Distributions 			existingSize = variable_length(&existingEntry->header);
703*2c2f96dcSApple OSS Distributions 
704*2c2f96dcSApple OSS Distributions 			entryContainer = OSData::withBytes(v3Entry, (uint32_t)nvram_v3_var_container_size(header));
705*2c2f96dcSApple OSS Distributions 			_varEntries->replaceObject(existingIndex, entryContainer.get());
706*2c2f96dcSApple OSS Distributions 
707*2c2f96dcSApple OSS Distributions 			DEBUG_INFO("Found existing for %s, resetting when controller available\n", v3Entry->header.name_data_buf);
708*2c2f96dcSApple OSS Distributions 			_resetData = true;
709*2c2f96dcSApple OSS Distributions 		} else {
710*2c2f96dcSApple OSS Distributions 			entryContainer = OSData::withBytes(v3Entry, (uint32_t)nvram_v3_var_container_size(header));
711*2c2f96dcSApple OSS Distributions 			_varEntries->setObject(entryContainer.get());
712*2c2f96dcSApple OSS Distributions 			existingSize = 0;
713*2c2f96dcSApple OSS Distributions 		}
714*2c2f96dcSApple OSS Distributions 
715*2c2f96dcSApple OSS Distributions 		system = (_systemSize != 0) && (uuid_compare(v3Entry->header.guid, gAppleSystemVariableGuid) == 0);
716*2c2f96dcSApple OSS Distributions 		if (system) {
717*2c2f96dcSApple OSS Distributions 			_systemUsed = _systemUsed + (uint32_t)variable_length(header) - (uint32_t)existingSize;
718*2c2f96dcSApple OSS Distributions 		} else {
719*2c2f96dcSApple OSS Distributions 			_commonUsed = _commonUsed + (uint32_t)variable_length(header) - (uint32_t)existingSize;
720*2c2f96dcSApple OSS Distributions 		}
721*2c2f96dcSApple OSS Distributions 
722*2c2f96dcSApple OSS Distributions 		if (convertPropToObject(v3Entry->header.name_data_buf, v3Entry->header.nameSize,
723*2c2f96dcSApple OSS Distributions 		    v3Entry->header.name_data_buf + v3Entry->header.nameSize, v3Entry->header.dataSize,
724*2c2f96dcSApple OSS Distributions 		    propSymbol, propObject)) {
725*2c2f96dcSApple OSS Distributions 			OSSharedPtr<const OSSymbol> canonicalKey = keyWithGuidAndCString(v3Entry->header.guid, (const char *)v3Entry->header.name_data_buf);
726*2c2f96dcSApple OSS Distributions 
727*2c2f96dcSApple OSS Distributions 			DEBUG_INFO("adding %s, dataLength=%u, system=%d\n",
728*2c2f96dcSApple OSS Distributions 			    canonicalKey->getCStringNoCopy(), v3Entry->header.dataSize, system);
729*2c2f96dcSApple OSS Distributions 
730*2c2f96dcSApple OSS Distributions 			_varDict->setObject(canonicalKey.get(), propObject.get());
731*2c2f96dcSApple OSS Distributions 
732*2c2f96dcSApple OSS Distributions 			if (_provider->_diags) {
733*2c2f96dcSApple OSS Distributions 				_provider->_diags->logVariable(getPartitionTypeForGUID(v3Entry->header.guid),
734*2c2f96dcSApple OSS Distributions 				    kIONVRAMOperationInit, propSymbol.get()->getCStringNoCopy(),
735*2c2f96dcSApple OSS Distributions 				    (void *)(uintptr_t)(header->name_data_buf + header->nameSize));
736*2c2f96dcSApple OSS Distributions 			}
737*2c2f96dcSApple OSS Distributions 		}
738*2c2f96dcSApple OSS Distributions 		IOFreeData(v3Entry, nvram_v3_var_container_size(header));
739*2c2f96dcSApple OSS Distributions skip:
740*2c2f96dcSApple OSS Distributions 		offset += variable_length(header);
741*2c2f96dcSApple OSS Distributions 	}
742*2c2f96dcSApple OSS Distributions 
743*2c2f96dcSApple OSS Distributions 	_currentOffset = (uint32_t)offset;
744*2c2f96dcSApple OSS Distributions 
745*2c2f96dcSApple OSS Distributions 	DEBUG_ALWAYS("_commonSize %#x, _systemSize %#x, _currentOffset %#x\n", _commonSize, _systemSize, _currentOffset);
746*2c2f96dcSApple OSS Distributions 	DEBUG_INFO("_commonUsed %#x, _systemUsed %#x\n", _commonUsed, _systemUsed);
747*2c2f96dcSApple OSS Distributions 
748*2c2f96dcSApple OSS Distributions 	_newData = true;
749*2c2f96dcSApple OSS Distributions 
750*2c2f96dcSApple OSS Distributions 	if (_provider->_diags) {
751*2c2f96dcSApple OSS Distributions 		OSSharedPtr<OSNumber> val = OSNumber::withNumber(getSystemUsed(), 32);
752*2c2f96dcSApple OSS Distributions 		_provider->_diags->setProperty(kNVRAMSystemUsedKey, val.get());
753*2c2f96dcSApple OSS Distributions 		DEBUG_INFO("%s=%u\n", kNVRAMSystemUsedKey, getSystemUsed());
754*2c2f96dcSApple OSS Distributions 
755*2c2f96dcSApple OSS Distributions 		val = OSNumber::withNumber(getCommonUsed(), 32);
756*2c2f96dcSApple OSS Distributions 		_provider->_diags->setProperty(kNVRAMCommonUsedKey, val.get());
757*2c2f96dcSApple OSS Distributions 		DEBUG_INFO("%s=%u\n", kNVRAMCommonUsedKey, getCommonUsed());
758*2c2f96dcSApple OSS Distributions 	}
759*2c2f96dcSApple OSS Distributions 
760*2c2f96dcSApple OSS Distributions 	return ret;
761*2c2f96dcSApple OSS Distributions }
762*2c2f96dcSApple OSS Distributions 
763*2c2f96dcSApple OSS Distributions IOReturn
setVariableInternal(const uuid_t varGuid,const char * variableName,OSObject * object)764*2c2f96dcSApple OSS Distributions IONVRAMV3Handler::setVariableInternal(const uuid_t varGuid, const char *variableName, OSObject *object)
765*2c2f96dcSApple OSS Distributions {
766*2c2f96dcSApple OSS Distributions 	struct nvram_v3_var_entry   *v3Entry = nullptr;
767*2c2f96dcSApple OSS Distributions 	struct nvram_v3_var_entry   *newV3Entry;
768*2c2f96dcSApple OSS Distributions 	OSSharedPtr<OSData>         newContainer;
769*2c2f96dcSApple OSS Distributions 	OSSharedPtr<const OSSymbol> canonicalKey;
770*2c2f96dcSApple OSS Distributions 	bool                        unset = (object == nullptr);
771*2c2f96dcSApple OSS Distributions 	bool                        system = false;
772*2c2f96dcSApple OSS Distributions 	IOReturn                    ret = kIOReturnSuccess;
773*2c2f96dcSApple OSS Distributions 	size_t                      entryNameLen = strlen(variableName) + 1;
774*2c2f96dcSApple OSS Distributions 	unsigned int                existingEntryIndex;
775*2c2f96dcSApple OSS Distributions 	uint32_t                    dataSize = 0;
776*2c2f96dcSApple OSS Distributions 	size_t                      existingVariableSize = 0;
777*2c2f96dcSApple OSS Distributions 	size_t                      newVariableSize = 0;
778*2c2f96dcSApple OSS Distributions 	size_t                      newEntrySize;
779*2c2f96dcSApple OSS Distributions 	uuid_string_t               uuidString;
780*2c2f96dcSApple OSS Distributions 
781*2c2f96dcSApple OSS Distributions 	system = (uuid_compare(varGuid, gAppleSystemVariableGuid) == 0);
782*2c2f96dcSApple OSS Distributions 	canonicalKey = keyWithGuidAndCString(varGuid, variableName);
783*2c2f96dcSApple OSS Distributions 
784*2c2f96dcSApple OSS Distributions 	uuid_unparse(varGuid, uuidString);
785*2c2f96dcSApple OSS Distributions 	DEBUG_INFO("setting %s:%s, system=%d, current var count=%u\n", uuidString, variableName, system, _varEntries->getCount());
786*2c2f96dcSApple OSS Distributions 
787*2c2f96dcSApple OSS Distributions 	findExistingEntry(varGuid, variableName, &v3Entry, &existingEntryIndex);
788*2c2f96dcSApple OSS Distributions 
789*2c2f96dcSApple OSS Distributions 	if (unset == true) {
790*2c2f96dcSApple OSS Distributions 		setEntryForRemove(v3Entry, system);
791*2c2f96dcSApple OSS Distributions 	} else {
792*2c2f96dcSApple OSS Distributions 		if ((v3Entry != nullptr) && (v3Entry->new_state != VAR_NEW_STATE_REMOVE)) {
793*2c2f96dcSApple OSS Distributions 			// Sizing was subtracted in setEntryForRemove
794*2c2f96dcSApple OSS Distributions 			existingVariableSize = variable_length(&v3Entry->header);
795*2c2f96dcSApple OSS Distributions 		}
796*2c2f96dcSApple OSS Distributions 
797*2c2f96dcSApple OSS Distributions 		convertObjectToProp(nullptr, &dataSize, variableName, object);
798*2c2f96dcSApple OSS Distributions 
799*2c2f96dcSApple OSS Distributions 		newVariableSize = sizeof(struct v3_var_header) + entryNameLen + dataSize;
800*2c2f96dcSApple OSS Distributions 		newEntrySize = sizeof(struct nvram_v3_var_entry) + entryNameLen + dataSize;
801*2c2f96dcSApple OSS Distributions 
802*2c2f96dcSApple OSS Distributions 		if (system) {
803*2c2f96dcSApple OSS Distributions 			if (_systemUsed - existingVariableSize + newVariableSize > _systemSize) {
804*2c2f96dcSApple OSS Distributions 				DEBUG_ERROR("system region full\n");
805*2c2f96dcSApple OSS Distributions 				ret = kIOReturnNoSpace;
806*2c2f96dcSApple OSS Distributions 				goto exit;
807*2c2f96dcSApple OSS Distributions 			}
808*2c2f96dcSApple OSS Distributions 		} else if (_commonUsed - existingVariableSize + newVariableSize > _commonSize) {
809*2c2f96dcSApple OSS Distributions 			DEBUG_ERROR("common region full\n");
810*2c2f96dcSApple OSS Distributions 			ret = kIOReturnNoSpace;
811*2c2f96dcSApple OSS Distributions 			goto exit;
812*2c2f96dcSApple OSS Distributions 		}
813*2c2f96dcSApple OSS Distributions 
814*2c2f96dcSApple OSS Distributions 		DEBUG_INFO("creating new entry for %s, existingVariableSize=%#zx, newVariableSize=%#zx\n", variableName, existingVariableSize, newVariableSize);
815*2c2f96dcSApple OSS Distributions 		newV3Entry = (struct nvram_v3_var_entry *)IOMallocZeroData(newEntrySize);
816*2c2f96dcSApple OSS Distributions 
817*2c2f96dcSApple OSS Distributions 		memcpy(newV3Entry->header.name_data_buf, variableName, entryNameLen);
818*2c2f96dcSApple OSS Distributions 		convertObjectToProp(newV3Entry->header.name_data_buf + entryNameLen, &dataSize, variableName, object);
819*2c2f96dcSApple OSS Distributions 
820*2c2f96dcSApple OSS Distributions 		newV3Entry->header.startId = VARIABLE_DATA;
821*2c2f96dcSApple OSS Distributions 		newV3Entry->header.nameSize = (uint32_t)entryNameLen;
822*2c2f96dcSApple OSS Distributions 		newV3Entry->header.dataSize = dataSize;
823*2c2f96dcSApple OSS Distributions 		newV3Entry->header.crc = crc32(0, newV3Entry->header.name_data_buf + entryNameLen, dataSize);
824*2c2f96dcSApple OSS Distributions 		memcpy(newV3Entry->header.guid, varGuid, sizeof(gAppleNVRAMGuid));
825*2c2f96dcSApple OSS Distributions 		newV3Entry->new_state = VAR_NEW_STATE_APPEND;
826*2c2f96dcSApple OSS Distributions 
827*2c2f96dcSApple OSS Distributions 		if (v3Entry) {
828*2c2f96dcSApple OSS Distributions 			newV3Entry->existing_offset = v3Entry->existing_offset;
829*2c2f96dcSApple OSS Distributions 			newV3Entry->header.state = v3Entry->header.state;
830*2c2f96dcSApple OSS Distributions 			newV3Entry->header.attributes = v3Entry->header.attributes;
831*2c2f96dcSApple OSS Distributions 
832*2c2f96dcSApple OSS Distributions 			newContainer = OSData::withBytes(newV3Entry, (uint32_t)newEntrySize);
833*2c2f96dcSApple OSS Distributions 			_varEntries->replaceObject(existingEntryIndex, newContainer.get());
834*2c2f96dcSApple OSS Distributions 		} else {
835*2c2f96dcSApple OSS Distributions 			newContainer = OSData::withBytes(newV3Entry, (uint32_t)newEntrySize);
836*2c2f96dcSApple OSS Distributions 			_varEntries->setObject(newContainer.get());
837*2c2f96dcSApple OSS Distributions 		}
838*2c2f96dcSApple OSS Distributions 
839*2c2f96dcSApple OSS Distributions 		if (system) {
840*2c2f96dcSApple OSS Distributions 			_systemUsed = _systemUsed + (uint32_t)newVariableSize - (uint32_t)existingVariableSize;
841*2c2f96dcSApple OSS Distributions 		} else {
842*2c2f96dcSApple OSS Distributions 			_commonUsed = _commonUsed + (uint32_t)newVariableSize - (uint32_t)existingVariableSize;
843*2c2f96dcSApple OSS Distributions 		}
844*2c2f96dcSApple OSS Distributions 
845*2c2f96dcSApple OSS Distributions 		_varDict->setObject(canonicalKey.get(), object);
846*2c2f96dcSApple OSS Distributions 
847*2c2f96dcSApple OSS Distributions 		if (_provider->_diags) {
848*2c2f96dcSApple OSS Distributions 			_provider->_diags->logVariable(getPartitionTypeForGUID(varGuid),
849*2c2f96dcSApple OSS Distributions 			    kIONVRAMOperationWrite, variableName,
850*2c2f96dcSApple OSS Distributions 			    (void *)(uintptr_t)dataSize);
851*2c2f96dcSApple OSS Distributions 		}
852*2c2f96dcSApple OSS Distributions 
853*2c2f96dcSApple OSS Distributions 		IOFreeData(newV3Entry, newEntrySize);
854*2c2f96dcSApple OSS Distributions 	}
855*2c2f96dcSApple OSS Distributions 
856*2c2f96dcSApple OSS Distributions exit:
857*2c2f96dcSApple OSS Distributions 	_newData = true;
858*2c2f96dcSApple OSS Distributions 
859*2c2f96dcSApple OSS Distributions 	if (_provider->_diags) {
860*2c2f96dcSApple OSS Distributions 		OSSharedPtr<OSNumber> val = OSNumber::withNumber(getSystemUsed(), 32);
861*2c2f96dcSApple OSS Distributions 		_provider->_diags->setProperty(kNVRAMSystemUsedKey, val.get());
862*2c2f96dcSApple OSS Distributions 
863*2c2f96dcSApple OSS Distributions 		val = OSNumber::withNumber(getCommonUsed(), 32);
864*2c2f96dcSApple OSS Distributions 		_provider->_diags->setProperty(kNVRAMCommonUsedKey, val.get());
865*2c2f96dcSApple OSS Distributions 	}
866*2c2f96dcSApple OSS Distributions 
867*2c2f96dcSApple OSS Distributions 	DEBUG_INFO("_commonUsed %#x, _systemUsed %#x\n", _commonUsed, _systemUsed);
868*2c2f96dcSApple OSS Distributions 
869*2c2f96dcSApple OSS Distributions 	return ret;
870*2c2f96dcSApple OSS Distributions }
871*2c2f96dcSApple OSS Distributions 
872*2c2f96dcSApple OSS Distributions IOReturn
setVariable(const uuid_t varGuid,const char * variableName,OSObject * object)873*2c2f96dcSApple OSS Distributions IONVRAMV3Handler::setVariable(const uuid_t varGuid, const char *variableName, OSObject *object)
874*2c2f96dcSApple OSS Distributions {
875*2c2f96dcSApple OSS Distributions 	uuid_t destGuid;
876*2c2f96dcSApple OSS Distributions 
877*2c2f96dcSApple OSS Distributions 	if (strcmp(variableName, "reclaim-int") == 0) {
878*2c2f96dcSApple OSS Distributions 		return reclaim();
879*2c2f96dcSApple OSS Distributions 	}
880*2c2f96dcSApple OSS Distributions 
881*2c2f96dcSApple OSS Distributions 	if (getSystemPartitionActive()) {
882*2c2f96dcSApple OSS Distributions 		// System region case, if they're using the GUID directly or it's on the system allow list
883*2c2f96dcSApple OSS Distributions 		// force it to use the System GUID
884*2c2f96dcSApple OSS Distributions 		if ((uuid_compare(varGuid, gAppleSystemVariableGuid) == 0) || variableInAllowList(variableName)) {
885*2c2f96dcSApple OSS Distributions 			uuid_copy(destGuid, gAppleSystemVariableGuid);
886*2c2f96dcSApple OSS Distributions 		} else {
887*2c2f96dcSApple OSS Distributions 			uuid_copy(destGuid, varGuid);
888*2c2f96dcSApple OSS Distributions 		}
889*2c2f96dcSApple OSS Distributions 	} else {
890*2c2f96dcSApple OSS Distributions 		// No system region, store System GUID as Common GUID
891*2c2f96dcSApple OSS Distributions 		if ((uuid_compare(varGuid, gAppleSystemVariableGuid) == 0) || variableInAllowList(variableName)) {
892*2c2f96dcSApple OSS Distributions 			uuid_copy(destGuid, gAppleNVRAMGuid);
893*2c2f96dcSApple OSS Distributions 		} else {
894*2c2f96dcSApple OSS Distributions 			uuid_copy(destGuid, varGuid);
895*2c2f96dcSApple OSS Distributions 		}
896*2c2f96dcSApple OSS Distributions 	}
897*2c2f96dcSApple OSS Distributions 
898*2c2f96dcSApple OSS Distributions 	return setVariableInternal(destGuid, variableName, object);
899*2c2f96dcSApple OSS Distributions }
900*2c2f96dcSApple OSS Distributions 
901*2c2f96dcSApple OSS Distributions uint32_t
findCurrentBank(void)902*2c2f96dcSApple OSS Distributions IONVRAMV3Handler::findCurrentBank(void)
903*2c2f96dcSApple OSS Distributions {
904*2c2f96dcSApple OSS Distributions 	struct v3_store_header storeHeader;
905*2c2f96dcSApple OSS Distributions 	uint32_t               maxGen = 0;
906*2c2f96dcSApple OSS Distributions 	uint32_t               currentBank = 0;
907*2c2f96dcSApple OSS Distributions 
908*2c2f96dcSApple OSS Distributions 	for (unsigned int i = 0; i < _bankCount; i++) {
909*2c2f96dcSApple OSS Distributions 		_nvramController->select(i);
910*2c2f96dcSApple OSS Distributions 		_nvramController->read(0, (uint8_t *)&storeHeader, sizeof(storeHeader));
911*2c2f96dcSApple OSS Distributions 
912*2c2f96dcSApple OSS Distributions 		if (valid_store_header(&storeHeader) && (storeHeader.generation >= maxGen)) {
913*2c2f96dcSApple OSS Distributions 			currentBank = i;
914*2c2f96dcSApple OSS Distributions 			maxGen = storeHeader.generation;
915*2c2f96dcSApple OSS Distributions 		}
916*2c2f96dcSApple OSS Distributions 	}
917*2c2f96dcSApple OSS Distributions 
918*2c2f96dcSApple OSS Distributions 	DEBUG_ALWAYS("currentBank=%#x, gen=%#x", currentBank, maxGen);
919*2c2f96dcSApple OSS Distributions 
920*2c2f96dcSApple OSS Distributions 	return currentBank;
921*2c2f96dcSApple OSS Distributions }
922*2c2f96dcSApple OSS Distributions 
923*2c2f96dcSApple OSS Distributions bool
setController(IONVRAMController * controller)924*2c2f96dcSApple OSS Distributions IONVRAMV3Handler::setController(IONVRAMController *controller)
925*2c2f96dcSApple OSS Distributions {
926*2c2f96dcSApple OSS Distributions 	IOReturn ret = kIOReturnSuccess;
927*2c2f96dcSApple OSS Distributions 
928*2c2f96dcSApple OSS Distributions 	if (_nvramController == NULL) {
929*2c2f96dcSApple OSS Distributions 		_nvramController = controller;
930*2c2f96dcSApple OSS Distributions 	}
931*2c2f96dcSApple OSS Distributions 
932*2c2f96dcSApple OSS Distributions 	DEBUG_INFO("Controller name: %s\n", _nvramController->getName());
933*2c2f96dcSApple OSS Distributions 
934*2c2f96dcSApple OSS Distributions 	require(_bankSize != 0, exit);
935*2c2f96dcSApple OSS Distributions 
936*2c2f96dcSApple OSS Distributions 	if (_resetData) {
937*2c2f96dcSApple OSS Distributions 		_resetData = false;
938*2c2f96dcSApple OSS Distributions 		DEBUG_ERROR("_resetData set, issuing reclaim recovery\n");
939*2c2f96dcSApple OSS Distributions 		ret = reclaim();
940*2c2f96dcSApple OSS Distributions 		require_noerr_action(ret, exit, DEBUG_ERROR("Reclaim recovery failed, invalid controller state!!! ret=%#x\n", ret));
941*2c2f96dcSApple OSS Distributions 		goto exit;
942*2c2f96dcSApple OSS Distributions 	}
943*2c2f96dcSApple OSS Distributions 
944*2c2f96dcSApple OSS Distributions 	ret = reloadInternal();
945*2c2f96dcSApple OSS Distributions 	if (ret != kIOReturnSuccess) {
946*2c2f96dcSApple OSS Distributions 		DEBUG_ERROR("Invalid image found, issuing reclaim recovery\n");
947*2c2f96dcSApple OSS Distributions 		ret = reclaim();
948*2c2f96dcSApple OSS Distributions 		require_noerr_action(ret, exit, DEBUG_ERROR("Reclaim recovery failed, invalid controller state!!! ret=%#x\n", ret));
949*2c2f96dcSApple OSS Distributions 	}
950*2c2f96dcSApple OSS Distributions 
951*2c2f96dcSApple OSS Distributions exit:
952*2c2f96dcSApple OSS Distributions 	return ret == kIOReturnSuccess;
953*2c2f96dcSApple OSS Distributions }
954*2c2f96dcSApple OSS Distributions 
955*2c2f96dcSApple OSS Distributions IOReturn
reclaim(void)956*2c2f96dcSApple OSS Distributions IONVRAMV3Handler::reclaim(void)
957*2c2f96dcSApple OSS Distributions {
958*2c2f96dcSApple OSS Distributions 	IOReturn             ret;
959*2c2f96dcSApple OSS Distributions 	struct               v3_store_header newStoreHeader;
960*2c2f96dcSApple OSS Distributions 	struct               v3_var_header *varHeader;
961*2c2f96dcSApple OSS Distributions 	struct               nvram_v3_var_entry *varEntry;
962*2c2f96dcSApple OSS Distributions 	OSData               *entryContainer;
963*2c2f96dcSApple OSS Distributions 	size_t               new_bank_offset = sizeof(struct v3_store_header);
964*2c2f96dcSApple OSS Distributions 	uint32_t             next_bank = (_currentBank + 1) % _bankCount;
965*2c2f96dcSApple OSS Distributions 	uint8_t              *bankData;
966*2c2f96dcSApple OSS Distributions 	OSSharedPtr<OSArray> remainingEntries;
967*2c2f96dcSApple OSS Distributions 
968*2c2f96dcSApple OSS Distributions 	DEBUG_INFO("called\n");
969*2c2f96dcSApple OSS Distributions 
970*2c2f96dcSApple OSS Distributions 	bankData = (uint8_t *)IOMallocData(_bankSize);
971*2c2f96dcSApple OSS Distributions 	require_action(bankData != nullptr, exit, ret = kIOReturnNoMemory);
972*2c2f96dcSApple OSS Distributions 
973*2c2f96dcSApple OSS Distributions 	ret = _nvramController->select(next_bank);
974*2c2f96dcSApple OSS Distributions 	verify_noerr_action(ret, DEBUG_INFO("select of bank %#08x failed\n", next_bank));
975*2c2f96dcSApple OSS Distributions 
976*2c2f96dcSApple OSS Distributions 	ret = _nvramController->eraseBank();
977*2c2f96dcSApple OSS Distributions 	verify_noerr_action(ret, DEBUG_INFO("eraseBank failed, ret=%#08x\n", ret));
978*2c2f96dcSApple OSS Distributions 
979*2c2f96dcSApple OSS Distributions 	_currentBank = next_bank;
980*2c2f96dcSApple OSS Distributions 
981*2c2f96dcSApple OSS Distributions 	remainingEntries = OSArray::withCapacity(_varEntries->getCapacity());
982*2c2f96dcSApple OSS Distributions 
983*2c2f96dcSApple OSS Distributions 	for (unsigned int i = 0; i < _varEntries->getCount(); i++) {
984*2c2f96dcSApple OSS Distributions 		entryContainer = OSDynamicCast(OSData, _varEntries->getObject(i));
985*2c2f96dcSApple OSS Distributions 		varEntry = (struct nvram_v3_var_entry *)entryContainer->getBytesNoCopy();
986*2c2f96dcSApple OSS Distributions 		varHeader = &varEntry->header;
987*2c2f96dcSApple OSS Distributions 
988*2c2f96dcSApple OSS Distributions 		DEBUG_INFO("entry %u %s, new_state=%#x, e_offset=%#lx, state=%#x\n",
989*2c2f96dcSApple OSS Distributions 		    i, varEntry->header.name_data_buf, varEntry->new_state, varEntry->existing_offset, varHeader->state);
990*2c2f96dcSApple OSS Distributions 
991*2c2f96dcSApple OSS Distributions 		if ((varEntry->new_state == VAR_NEW_STATE_NONE) ||
992*2c2f96dcSApple OSS Distributions 		    (varEntry->new_state == VAR_NEW_STATE_APPEND)) {
993*2c2f96dcSApple OSS Distributions 			varHeader->state = VAR_ADDED;
994*2c2f96dcSApple OSS Distributions 
995*2c2f96dcSApple OSS Distributions 			memcpy(bankData + new_bank_offset, (uint8_t *)varHeader, variable_length(varHeader));
996*2c2f96dcSApple OSS Distributions 
997*2c2f96dcSApple OSS Distributions 			varEntry->new_state = VAR_NEW_STATE_NONE;
998*2c2f96dcSApple OSS Distributions 			varEntry->existing_offset = new_bank_offset;
999*2c2f96dcSApple OSS Distributions 			new_bank_offset += variable_length(varHeader);
1000*2c2f96dcSApple OSS Distributions 
1001*2c2f96dcSApple OSS Distributions 			remainingEntries->setObject(entryContainer);
1002*2c2f96dcSApple OSS Distributions 		} else {
1003*2c2f96dcSApple OSS Distributions 			// entryContainer not added to remainingEntries, entry dropped
1004*2c2f96dcSApple OSS Distributions 		}
1005*2c2f96dcSApple OSS Distributions 	}
1006*2c2f96dcSApple OSS Distributions 
1007*2c2f96dcSApple OSS Distributions 	memcpy(&newStoreHeader, _nvramImage, sizeof(newStoreHeader));
1008*2c2f96dcSApple OSS Distributions 
1009*2c2f96dcSApple OSS Distributions 	_generation += 1;
1010*2c2f96dcSApple OSS Distributions 
1011*2c2f96dcSApple OSS Distributions 	newStoreHeader.generation = _generation;
1012*2c2f96dcSApple OSS Distributions 
1013*2c2f96dcSApple OSS Distributions 	memcpy(bankData, (uint8_t *)&newStoreHeader, sizeof(newStoreHeader));
1014*2c2f96dcSApple OSS Distributions 
1015*2c2f96dcSApple OSS Distributions 	ret = _nvramController->write(0, bankData, new_bank_offset);
1016*2c2f96dcSApple OSS Distributions 	require_noerr_action(ret, exit, DEBUG_ERROR("reclaim bank write failed, ret=%08x\n", ret));
1017*2c2f96dcSApple OSS Distributions 
1018*2c2f96dcSApple OSS Distributions 	_currentOffset = (uint32_t)new_bank_offset;
1019*2c2f96dcSApple OSS Distributions 
1020*2c2f96dcSApple OSS Distributions 	DEBUG_INFO("Reclaim complete, _currentBank=%u _generation=%u, _currentOffset=%#x\n", _currentBank, _generation, _currentOffset);
1021*2c2f96dcSApple OSS Distributions 
1022*2c2f96dcSApple OSS Distributions 	_newData = false;
1023*2c2f96dcSApple OSS Distributions 
1024*2c2f96dcSApple OSS Distributions 	_varEntries.reset(remainingEntries.get(), OSRetain);
1025*2c2f96dcSApple OSS Distributions 
1026*2c2f96dcSApple OSS Distributions exit:
1027*2c2f96dcSApple OSS Distributions 	IOFreeData(bankData, _bankSize);
1028*2c2f96dcSApple OSS Distributions 
1029*2c2f96dcSApple OSS Distributions 	return ret;
1030*2c2f96dcSApple OSS Distributions }
1031*2c2f96dcSApple OSS Distributions 
1032*2c2f96dcSApple OSS Distributions size_t
getAppendSize(void)1033*2c2f96dcSApple OSS Distributions IONVRAMV3Handler::getAppendSize(void)
1034*2c2f96dcSApple OSS Distributions {
1035*2c2f96dcSApple OSS Distributions 	struct nvram_v3_var_entry *varEntry;
1036*2c2f96dcSApple OSS Distributions 	struct v3_var_header      *varHeader;
1037*2c2f96dcSApple OSS Distributions 	OSData                    *entryContainer;
1038*2c2f96dcSApple OSS Distributions 	size_t                    appendSize = 0;
1039*2c2f96dcSApple OSS Distributions 
1040*2c2f96dcSApple OSS Distributions 	for (unsigned int i = 0; i < _varEntries->getCount(); i++) {
1041*2c2f96dcSApple OSS Distributions 		entryContainer = OSDynamicCast(OSData, _varEntries->getObject(i));
1042*2c2f96dcSApple OSS Distributions 		varEntry = (struct nvram_v3_var_entry *)entryContainer->getBytesNoCopy();
1043*2c2f96dcSApple OSS Distributions 		varHeader = &varEntry->header;
1044*2c2f96dcSApple OSS Distributions 
1045*2c2f96dcSApple OSS Distributions 		if (varEntry->new_state == VAR_NEW_STATE_APPEND) {
1046*2c2f96dcSApple OSS Distributions 			appendSize += variable_length(varHeader);
1047*2c2f96dcSApple OSS Distributions 		}
1048*2c2f96dcSApple OSS Distributions 	}
1049*2c2f96dcSApple OSS Distributions 
1050*2c2f96dcSApple OSS Distributions 	return appendSize;
1051*2c2f96dcSApple OSS Distributions }
1052*2c2f96dcSApple OSS Distributions 
1053*2c2f96dcSApple OSS Distributions IOReturn
syncRaw(void)1054*2c2f96dcSApple OSS Distributions IONVRAMV3Handler::syncRaw(void)
1055*2c2f96dcSApple OSS Distributions {
1056*2c2f96dcSApple OSS Distributions 	IOReturn                  ret = kIOReturnSuccess;
1057*2c2f96dcSApple OSS Distributions 	struct nvram_v3_var_entry *varEntry;
1058*2c2f96dcSApple OSS Distributions 	struct v3_var_header      *varHeader;
1059*2c2f96dcSApple OSS Distributions 	OSData                    *entryContainer;
1060*2c2f96dcSApple OSS Distributions 	OSSharedPtr<OSArray>      remainingEntries;
1061*2c2f96dcSApple OSS Distributions 	uint8_t                   *appendBuffer = nullptr;
1062*2c2f96dcSApple OSS Distributions 	size_t                    appendBufferOffset = 0;
1063*2c2f96dcSApple OSS Distributions 	size_t                    *invalidateOffsets = nullptr;
1064*2c2f96dcSApple OSS Distributions 	size_t                    invalidateOffsetsCount = 0;
1065*2c2f96dcSApple OSS Distributions 	size_t                    invalidateOffsetIndex = 0;
1066*2c2f96dcSApple OSS Distributions 	size_t                    invalidatedSize = 0;
1067*2c2f96dcSApple OSS Distributions 
1068*2c2f96dcSApple OSS Distributions 	require_action(_nvramController != nullptr, exit, DEBUG_INFO("No _nvramController\n"));
1069*2c2f96dcSApple OSS Distributions 	require_action(_newData == true, exit, DEBUG_INFO("No _newData to sync\n"));
1070*2c2f96dcSApple OSS Distributions 	require_action(_bankSize != 0, exit, DEBUG_INFO("No nvram size info\n"));
1071*2c2f96dcSApple OSS Distributions 
1072*2c2f96dcSApple OSS Distributions 	DEBUG_INFO("_varEntries->getCount()=%#x\n", _varEntries->getCount());
1073*2c2f96dcSApple OSS Distributions 
1074*2c2f96dcSApple OSS Distributions 	if (getAppendSize() + _currentOffset < _bankSize) {
1075*2c2f96dcSApple OSS Distributions 		// No reclaim, build append and invalidate list
1076*2c2f96dcSApple OSS Distributions 
1077*2c2f96dcSApple OSS Distributions 		remainingEntries = OSArray::withCapacity(_varEntries->getCapacity());
1078*2c2f96dcSApple OSS Distributions 
1079*2c2f96dcSApple OSS Distributions 		appendBuffer = (uint8_t *)IOMallocData(_bankSize);
1080*2c2f96dcSApple OSS Distributions 		require_action(appendBuffer, exit, ret = kIOReturnNoMemory);
1081*2c2f96dcSApple OSS Distributions 
1082*2c2f96dcSApple OSS Distributions 		invalidateOffsetsCount = _varEntries->getCount();
1083*2c2f96dcSApple OSS Distributions 		invalidateOffsets = (size_t *)IOMallocData(invalidateOffsetsCount * sizeof(size_t));
1084*2c2f96dcSApple OSS Distributions 		require_action(invalidateOffsets, exit, ret = kIOReturnNoMemory);
1085*2c2f96dcSApple OSS Distributions 
1086*2c2f96dcSApple OSS Distributions 		for (unsigned int i = 0; i < _varEntries->getCount(); i++) {
1087*2c2f96dcSApple OSS Distributions 			entryContainer = OSDynamicCast(OSData, _varEntries->getObject(i));
1088*2c2f96dcSApple OSS Distributions 			varEntry = (struct nvram_v3_var_entry *)entryContainer->getBytesNoCopy();
1089*2c2f96dcSApple OSS Distributions 			varHeader = &varEntry->header;
1090*2c2f96dcSApple OSS Distributions 
1091*2c2f96dcSApple OSS Distributions 			DEBUG_INFO("entry %s, new_state=%#02x state=%#02x, existing_offset=%#zx\n",
1092*2c2f96dcSApple OSS Distributions 			    varEntry->header.name_data_buf, varEntry->new_state, varEntry->header.state, varEntry->existing_offset);
1093*2c2f96dcSApple OSS Distributions 
1094*2c2f96dcSApple OSS Distributions 			if (varEntry->new_state == VAR_NEW_STATE_APPEND) {
1095*2c2f96dcSApple OSS Distributions 				size_t varSize = variable_length(varHeader);
1096*2c2f96dcSApple OSS Distributions 				size_t prevOffset = varEntry->existing_offset;
1097*2c2f96dcSApple OSS Distributions 
1098*2c2f96dcSApple OSS Distributions 				varHeader->state = VAR_ADDED;
1099*2c2f96dcSApple OSS Distributions 				varEntry->existing_offset = _currentOffset + appendBufferOffset;
1100*2c2f96dcSApple OSS Distributions 				varEntry->new_state = VAR_NEW_STATE_NONE;
1101*2c2f96dcSApple OSS Distributions 
1102*2c2f96dcSApple OSS Distributions 				DEBUG_INFO("Appending %s in append buffer offset %#zx, actual offset %#zx, prevOffset %#zx, varsize=%#zx\n",
1103*2c2f96dcSApple OSS Distributions 				    varEntry->header.name_data_buf, appendBufferOffset, varEntry->existing_offset, prevOffset, varSize);
1104*2c2f96dcSApple OSS Distributions 
1105*2c2f96dcSApple OSS Distributions 				// Write to append buffer
1106*2c2f96dcSApple OSS Distributions 				memcpy(appendBuffer + appendBufferOffset, (uint8_t *)varHeader, varSize);
1107*2c2f96dcSApple OSS Distributions 				appendBufferOffset += varSize;
1108*2c2f96dcSApple OSS Distributions 
1109*2c2f96dcSApple OSS Distributions 				if (prevOffset) {
1110*2c2f96dcSApple OSS Distributions 					invalidateOffsets[invalidateOffsetIndex++] = prevOffset;
1111*2c2f96dcSApple OSS Distributions 					invalidatedSize += variable_length((struct v3_var_header *)prevOffset);
1112*2c2f96dcSApple OSS Distributions 				}
1113*2c2f96dcSApple OSS Distributions 
1114*2c2f96dcSApple OSS Distributions 				remainingEntries->setObject(entryContainer);
1115*2c2f96dcSApple OSS Distributions 			} else if (varEntry->new_state == VAR_NEW_STATE_REMOVE) {
1116*2c2f96dcSApple OSS Distributions 				if (varEntry->existing_offset) {
1117*2c2f96dcSApple OSS Distributions 					DEBUG_INFO("marking entry at offset %#lx deleted\n", varEntry->existing_offset);
1118*2c2f96dcSApple OSS Distributions 
1119*2c2f96dcSApple OSS Distributions 					invalidateOffsets[invalidateOffsetIndex++] = varEntry->existing_offset;
1120*2c2f96dcSApple OSS Distributions 					invalidatedSize += variable_length((struct v3_var_header *)varEntry->existing_offset);
1121*2c2f96dcSApple OSS Distributions 				} else {
1122*2c2f96dcSApple OSS Distributions 					DEBUG_INFO("No existing_offset , removing\n");
1123*2c2f96dcSApple OSS Distributions 				}
1124*2c2f96dcSApple OSS Distributions 
1125*2c2f96dcSApple OSS Distributions 				// not re-added to remainingEntries
1126*2c2f96dcSApple OSS Distributions 			} else {
1127*2c2f96dcSApple OSS Distributions 				DEBUG_INFO("skipping\n");
1128*2c2f96dcSApple OSS Distributions 				remainingEntries->setObject(entryContainer);
1129*2c2f96dcSApple OSS Distributions 			}
1130*2c2f96dcSApple OSS Distributions 		}
1131*2c2f96dcSApple OSS Distributions 
1132*2c2f96dcSApple OSS Distributions 		if (appendBufferOffset > 0) {
1133*2c2f96dcSApple OSS Distributions 			// Write appendBuffer
1134*2c2f96dcSApple OSS Distributions 			DEBUG_INFO("Appending append buffer size=%#zx at offset=%#x\n", appendBufferOffset, _currentOffset);
1135*2c2f96dcSApple OSS Distributions 			ret = _nvramController->write(_currentOffset, appendBuffer, appendBufferOffset);
1136*2c2f96dcSApple OSS Distributions 			require_noerr_action(ret, exit, DEBUG_ERROR("could not re-append, ret=%#x\n", ret));
1137*2c2f96dcSApple OSS Distributions 
1138*2c2f96dcSApple OSS Distributions 			_currentOffset += appendBufferOffset;
1139*2c2f96dcSApple OSS Distributions 		} else {
1140*2c2f96dcSApple OSS Distributions 			DEBUG_INFO("No entries to append\n");
1141*2c2f96dcSApple OSS Distributions 		}
1142*2c2f96dcSApple OSS Distributions 
1143*2c2f96dcSApple OSS Distributions 		if (invalidateOffsetIndex > 0) {
1144*2c2f96dcSApple OSS Distributions 			// Invalidate Entries
1145*2c2f96dcSApple OSS Distributions 			for (unsigned int i = 0; i < invalidateOffsetIndex; i++) {
1146*2c2f96dcSApple OSS Distributions 				uint8_t state = VAR_ADDED & VAR_DELETED & VAR_IN_DELETED_TRANSITION;
1147*2c2f96dcSApple OSS Distributions 
1148*2c2f96dcSApple OSS Distributions 				ret = _nvramController->write(invalidateOffsets[i] + offsetof(struct v3_var_header, state), &state, sizeof(state));
1149*2c2f96dcSApple OSS Distributions 				require_noerr_action(ret, exit, DEBUG_ERROR("unable to invalidate at offset %#zx, ret=%#x\n", invalidateOffsets[i], ret));
1150*2c2f96dcSApple OSS Distributions 				DEBUG_INFO("Invalidated entry at offset=%#zx\n", invalidateOffsets[i]);
1151*2c2f96dcSApple OSS Distributions 			}
1152*2c2f96dcSApple OSS Distributions 		} else {
1153*2c2f96dcSApple OSS Distributions 			DEBUG_INFO("No entries to invalidate\n");
1154*2c2f96dcSApple OSS Distributions 		}
1155*2c2f96dcSApple OSS Distributions 
1156*2c2f96dcSApple OSS Distributions 		_newData = false;
1157*2c2f96dcSApple OSS Distributions 
1158*2c2f96dcSApple OSS Distributions 		_varEntries.reset(remainingEntries.get(), OSRetain);
1159*2c2f96dcSApple OSS Distributions 	} else {
1160*2c2f96dcSApple OSS Distributions 		// Will need to reclaim, rebuild store and write everything at once
1161*2c2f96dcSApple OSS Distributions 		ret = reclaim();
1162*2c2f96dcSApple OSS Distributions 	}
1163*2c2f96dcSApple OSS Distributions 
1164*2c2f96dcSApple OSS Distributions exit:
1165*2c2f96dcSApple OSS Distributions 	IOFreeData(appendBuffer, _bankSize);
1166*2c2f96dcSApple OSS Distributions 	IOFreeData(invalidateOffsets, invalidateOffsetsCount * sizeof(size_t));
1167*2c2f96dcSApple OSS Distributions 
1168*2c2f96dcSApple OSS Distributions 	return ret;
1169*2c2f96dcSApple OSS Distributions }
1170*2c2f96dcSApple OSS Distributions 
1171*2c2f96dcSApple OSS Distributions IOReturn
syncBlock(void)1172*2c2f96dcSApple OSS Distributions IONVRAMV3Handler::syncBlock(void)
1173*2c2f96dcSApple OSS Distributions {
1174*2c2f96dcSApple OSS Distributions 	IOReturn             ret = kIOReturnSuccess;
1175*2c2f96dcSApple OSS Distributions 	struct               v3_store_header newStoreHeader;
1176*2c2f96dcSApple OSS Distributions 	struct               v3_var_header *varHeader;
1177*2c2f96dcSApple OSS Distributions 	struct               nvram_v3_var_entry *varEntry;
1178*2c2f96dcSApple OSS Distributions 	OSData               *entryContainer;
1179*2c2f96dcSApple OSS Distributions 	size_t               new_bank_offset = sizeof(struct v3_store_header);
1180*2c2f96dcSApple OSS Distributions 	uint8_t              *block;
1181*2c2f96dcSApple OSS Distributions 	OSSharedPtr<OSArray> remainingEntries;
1182*2c2f96dcSApple OSS Distributions 	uint32_t             next_bank = (_currentBank + 1) % _bankCount;
1183*2c2f96dcSApple OSS Distributions 
1184*2c2f96dcSApple OSS Distributions 	DEBUG_INFO("called\n");
1185*2c2f96dcSApple OSS Distributions 
1186*2c2f96dcSApple OSS Distributions 	require_action(_nvramController != nullptr, exit, DEBUG_INFO("No _nvramController\n"));
1187*2c2f96dcSApple OSS Distributions 	require_action(_newData == true, exit, DEBUG_INFO("No _newData to sync\n"));
1188*2c2f96dcSApple OSS Distributions 	require_action(_bankSize != 0, exit, DEBUG_INFO("No nvram size info\n"));
1189*2c2f96dcSApple OSS Distributions 
1190*2c2f96dcSApple OSS Distributions 	block = (uint8_t *)IOMallocData(_bankSize);
1191*2c2f96dcSApple OSS Distributions 
1192*2c2f96dcSApple OSS Distributions 	remainingEntries = OSArray::withCapacity(_varEntries->getCapacity());
1193*2c2f96dcSApple OSS Distributions 
1194*2c2f96dcSApple OSS Distributions 	ret = _nvramController->select(next_bank);
1195*2c2f96dcSApple OSS Distributions 	verify_noerr_action(ret, DEBUG_INFO("select of bank %#x failed\n", next_bank));
1196*2c2f96dcSApple OSS Distributions 
1197*2c2f96dcSApple OSS Distributions 	ret = _nvramController->eraseBank();
1198*2c2f96dcSApple OSS Distributions 	verify_noerr_action(ret, DEBUG_INFO("eraseBank failed, ret=%#08x\n", ret));
1199*2c2f96dcSApple OSS Distributions 
1200*2c2f96dcSApple OSS Distributions 	_currentBank = next_bank;
1201*2c2f96dcSApple OSS Distributions 
1202*2c2f96dcSApple OSS Distributions 	memcpy(&newStoreHeader, _nvramImage, sizeof(newStoreHeader));
1203*2c2f96dcSApple OSS Distributions 
1204*2c2f96dcSApple OSS Distributions 	_generation += 1;
1205*2c2f96dcSApple OSS Distributions 
1206*2c2f96dcSApple OSS Distributions 	newStoreHeader.generation = _generation;
1207*2c2f96dcSApple OSS Distributions 
1208*2c2f96dcSApple OSS Distributions 	memcpy(block, (uint8_t *)&newStoreHeader, sizeof(newStoreHeader));
1209*2c2f96dcSApple OSS Distributions 
1210*2c2f96dcSApple OSS Distributions 	for (unsigned int i = 0; i < _varEntries->getCount(); i++) {
1211*2c2f96dcSApple OSS Distributions 		entryContainer = OSDynamicCast(OSData, _varEntries->getObject(i));
1212*2c2f96dcSApple OSS Distributions 		varEntry = (struct nvram_v3_var_entry *)entryContainer->getBytesNoCopy();
1213*2c2f96dcSApple OSS Distributions 		varHeader = &varEntry->header;
1214*2c2f96dcSApple OSS Distributions 
1215*2c2f96dcSApple OSS Distributions 		DEBUG_INFO("entry %u %s, new_state=%#x, e_offset=%#lx, state=%#x\n",
1216*2c2f96dcSApple OSS Distributions 		    i, varEntry->header.name_data_buf, varEntry->new_state, varEntry->existing_offset, varHeader->state);
1217*2c2f96dcSApple OSS Distributions 
1218*2c2f96dcSApple OSS Distributions 		if (varEntry->new_state != VAR_NEW_STATE_REMOVE) {
1219*2c2f96dcSApple OSS Distributions 			varHeader->state = VAR_ADDED;
1220*2c2f96dcSApple OSS Distributions 
1221*2c2f96dcSApple OSS Distributions 			memcpy(block + new_bank_offset, (uint8_t *)varHeader, variable_length(varHeader));
1222*2c2f96dcSApple OSS Distributions 
1223*2c2f96dcSApple OSS Distributions 			varEntry->existing_offset = new_bank_offset;
1224*2c2f96dcSApple OSS Distributions 			new_bank_offset += variable_length(varHeader);
1225*2c2f96dcSApple OSS Distributions 			varEntry->new_state = VAR_NEW_STATE_NONE;
1226*2c2f96dcSApple OSS Distributions 
1227*2c2f96dcSApple OSS Distributions 			remainingEntries->setObject(entryContainer);
1228*2c2f96dcSApple OSS Distributions 		} else {
1229*2c2f96dcSApple OSS Distributions 			DEBUG_INFO("Dropping %s\n", varEntry->header.name_data_buf);
1230*2c2f96dcSApple OSS Distributions 		}
1231*2c2f96dcSApple OSS Distributions 	}
1232*2c2f96dcSApple OSS Distributions 
1233*2c2f96dcSApple OSS Distributions 	ret = _nvramController->write(0, block, _bankSize);
1234*2c2f96dcSApple OSS Distributions 	verify_noerr_action(ret, DEBUG_ERROR("w fail, ret=%#x\n", ret));
1235*2c2f96dcSApple OSS Distributions 
1236*2c2f96dcSApple OSS Distributions 	_nvramController->sync();
1237*2c2f96dcSApple OSS Distributions 
1238*2c2f96dcSApple OSS Distributions 	_varEntries.reset(remainingEntries.get(), OSRetain);
1239*2c2f96dcSApple OSS Distributions 
1240*2c2f96dcSApple OSS Distributions 	_newData = false;
1241*2c2f96dcSApple OSS Distributions 
1242*2c2f96dcSApple OSS Distributions 	DEBUG_INFO("Save complete, _generation=%u\n", _generation);
1243*2c2f96dcSApple OSS Distributions 
1244*2c2f96dcSApple OSS Distributions 	IOFreeData(block, _bankSize);
1245*2c2f96dcSApple OSS Distributions 
1246*2c2f96dcSApple OSS Distributions exit:
1247*2c2f96dcSApple OSS Distributions 	return ret;
1248*2c2f96dcSApple OSS Distributions }
1249*2c2f96dcSApple OSS Distributions 
1250*2c2f96dcSApple OSS Distributions IOReturn
sync(void)1251*2c2f96dcSApple OSS Distributions IONVRAMV3Handler::sync(void)
1252*2c2f96dcSApple OSS Distributions {
1253*2c2f96dcSApple OSS Distributions 	IOReturn ret;
1254*2c2f96dcSApple OSS Distributions 
1255*2c2f96dcSApple OSS Distributions 	if (_reload) {
1256*2c2f96dcSApple OSS Distributions 		ret = reloadInternal();
1257*2c2f96dcSApple OSS Distributions 		require_noerr_action(ret, exit, DEBUG_ERROR("Reload failed, ret=%#x", ret));
1258*2c2f96dcSApple OSS Distributions 
1259*2c2f96dcSApple OSS Distributions 		_reload = false;
1260*2c2f96dcSApple OSS Distributions 	}
1261*2c2f96dcSApple OSS Distributions 
1262*2c2f96dcSApple OSS Distributions 	if (_rawController == true) {
1263*2c2f96dcSApple OSS Distributions 		ret = syncRaw();
1264*2c2f96dcSApple OSS Distributions 
1265*2c2f96dcSApple OSS Distributions 		if (ret != kIOReturnSuccess) {
1266*2c2f96dcSApple OSS Distributions 			ret = reclaim();
1267*2c2f96dcSApple OSS Distributions 			require_noerr_action(ret, exit, DEBUG_ERROR("Reclaim recovery failed, ret=%#x", ret));
1268*2c2f96dcSApple OSS Distributions 		}
1269*2c2f96dcSApple OSS Distributions 	} else {
1270*2c2f96dcSApple OSS Distributions 		ret = syncBlock();
1271*2c2f96dcSApple OSS Distributions 	}
1272*2c2f96dcSApple OSS Distributions 
1273*2c2f96dcSApple OSS Distributions exit:
1274*2c2f96dcSApple OSS Distributions 	return ret;
1275*2c2f96dcSApple OSS Distributions }
1276*2c2f96dcSApple OSS Distributions 
1277*2c2f96dcSApple OSS Distributions uint32_t
getGeneration(void) const1278*2c2f96dcSApple OSS Distributions IONVRAMV3Handler::getGeneration(void) const
1279*2c2f96dcSApple OSS Distributions {
1280*2c2f96dcSApple OSS Distributions 	return _generation;
1281*2c2f96dcSApple OSS Distributions }
1282*2c2f96dcSApple OSS Distributions 
1283*2c2f96dcSApple OSS Distributions uint32_t
getVersion(void) const1284*2c2f96dcSApple OSS Distributions IONVRAMV3Handler::getVersion(void) const
1285*2c2f96dcSApple OSS Distributions {
1286*2c2f96dcSApple OSS Distributions 	return kNVRAMVersion3;
1287*2c2f96dcSApple OSS Distributions }
1288*2c2f96dcSApple OSS Distributions 
1289*2c2f96dcSApple OSS Distributions uint32_t
getSystemUsed(void) const1290*2c2f96dcSApple OSS Distributions IONVRAMV3Handler::getSystemUsed(void) const
1291*2c2f96dcSApple OSS Distributions {
1292*2c2f96dcSApple OSS Distributions 	return _systemUsed;
1293*2c2f96dcSApple OSS Distributions }
1294*2c2f96dcSApple OSS Distributions 
1295*2c2f96dcSApple OSS Distributions uint32_t
getCommonUsed(void) const1296*2c2f96dcSApple OSS Distributions IONVRAMV3Handler::getCommonUsed(void) const
1297*2c2f96dcSApple OSS Distributions {
1298*2c2f96dcSApple OSS Distributions 	return _commonUsed;
1299*2c2f96dcSApple OSS Distributions }
1300*2c2f96dcSApple OSS Distributions 
1301*2c2f96dcSApple OSS Distributions bool
getSystemPartitionActive(void) const1302*2c2f96dcSApple OSS Distributions IONVRAMV3Handler::getSystemPartitionActive(void) const
1303*2c2f96dcSApple OSS Distributions {
1304*2c2f96dcSApple OSS Distributions 	return _systemSize != 0;
1305*2c2f96dcSApple OSS Distributions }
1306*2c2f96dcSApple OSS Distributions 
1307*2c2f96dcSApple OSS Distributions bool
convertObjectToProp(uint8_t * buffer,uint32_t * length,const char * propName,OSObject * propObject)1308*2c2f96dcSApple OSS Distributions IONVRAMV3Handler::convertObjectToProp(uint8_t *buffer, uint32_t *length,
1309*2c2f96dcSApple OSS Distributions     const char *propName, OSObject *propObject)
1310*2c2f96dcSApple OSS Distributions {
1311*2c2f96dcSApple OSS Distributions 	uint32_t             offset;
1312*2c2f96dcSApple OSS Distributions 	IONVRAMVariableType  propType;
1313*2c2f96dcSApple OSS Distributions 	OSBoolean            *tmpBoolean = nullptr;
1314*2c2f96dcSApple OSS Distributions 	OSNumber             *tmpNumber = nullptr;
1315*2c2f96dcSApple OSS Distributions 	OSString             *tmpString = nullptr;
1316*2c2f96dcSApple OSS Distributions 	OSData               *tmpData = nullptr;
1317*2c2f96dcSApple OSS Distributions 
1318*2c2f96dcSApple OSS Distributions 	propType = getVariableType(propName);
1319*2c2f96dcSApple OSS Distributions 
1320*2c2f96dcSApple OSS Distributions 	// Get the size of the data.
1321*2c2f96dcSApple OSS Distributions 	offset = 0;
1322*2c2f96dcSApple OSS Distributions 	switch (propType) {
1323*2c2f96dcSApple OSS Distributions 	case kOFVariableTypeBoolean:
1324*2c2f96dcSApple OSS Distributions 		tmpBoolean = OSDynamicCast(OSBoolean, propObject);
1325*2c2f96dcSApple OSS Distributions 		if (tmpBoolean != nullptr) {
1326*2c2f96dcSApple OSS Distributions 			const char *bool_buf;
1327*2c2f96dcSApple OSS Distributions 			if (tmpBoolean->getValue()) {
1328*2c2f96dcSApple OSS Distributions 				bool_buf = "true";
1329*2c2f96dcSApple OSS Distributions 			} else {
1330*2c2f96dcSApple OSS Distributions 				bool_buf = "false";
1331*2c2f96dcSApple OSS Distributions 			}
1332*2c2f96dcSApple OSS Distributions 
1333*2c2f96dcSApple OSS Distributions 			offset = (uint32_t)strlen(bool_buf);
1334*2c2f96dcSApple OSS Distributions 
1335*2c2f96dcSApple OSS Distributions 			if (buffer) {
1336*2c2f96dcSApple OSS Distributions 				if (*length < offset) {
1337*2c2f96dcSApple OSS Distributions 					return false;
1338*2c2f96dcSApple OSS Distributions 				} else {
1339*2c2f96dcSApple OSS Distributions 					memcpy(buffer, bool_buf, offset);
1340*2c2f96dcSApple OSS Distributions 				}
1341*2c2f96dcSApple OSS Distributions 			}
1342*2c2f96dcSApple OSS Distributions 		}
1343*2c2f96dcSApple OSS Distributions 		break;
1344*2c2f96dcSApple OSS Distributions 
1345*2c2f96dcSApple OSS Distributions 	case kOFVariableTypeNumber:
1346*2c2f96dcSApple OSS Distributions 		tmpNumber = OSDynamicCast(OSNumber, propObject);
1347*2c2f96dcSApple OSS Distributions 		if (tmpNumber != nullptr) {
1348*2c2f96dcSApple OSS Distributions 			char num_buf[12];
1349*2c2f96dcSApple OSS Distributions 			char *end_buf = num_buf;
1350*2c2f96dcSApple OSS Distributions 			uint32_t tmpValue = tmpNumber->unsigned32BitValue();
1351*2c2f96dcSApple OSS Distributions 			if (tmpValue == 0xFFFFFFFF) {
1352*2c2f96dcSApple OSS Distributions 				end_buf += snprintf(end_buf, sizeof(num_buf), "-1");
1353*2c2f96dcSApple OSS Distributions 			} else if (tmpValue < 1000) {
1354*2c2f96dcSApple OSS Distributions 				end_buf += snprintf(end_buf, sizeof(num_buf), "%d", (uint32_t)tmpValue);
1355*2c2f96dcSApple OSS Distributions 			} else {
1356*2c2f96dcSApple OSS Distributions 				end_buf += snprintf(end_buf, sizeof(num_buf), "%#x", (uint32_t)tmpValue);
1357*2c2f96dcSApple OSS Distributions 			}
1358*2c2f96dcSApple OSS Distributions 
1359*2c2f96dcSApple OSS Distributions 			offset = (uint32_t)(end_buf - num_buf);
1360*2c2f96dcSApple OSS Distributions 			if (buffer) {
1361*2c2f96dcSApple OSS Distributions 				if (*length < offset) {
1362*2c2f96dcSApple OSS Distributions 					return false;
1363*2c2f96dcSApple OSS Distributions 				} else {
1364*2c2f96dcSApple OSS Distributions 					memcpy(buffer, num_buf, offset);
1365*2c2f96dcSApple OSS Distributions 				}
1366*2c2f96dcSApple OSS Distributions 			}
1367*2c2f96dcSApple OSS Distributions 		}
1368*2c2f96dcSApple OSS Distributions 		break;
1369*2c2f96dcSApple OSS Distributions 
1370*2c2f96dcSApple OSS Distributions 	case kOFVariableTypeString:
1371*2c2f96dcSApple OSS Distributions 		tmpString = OSDynamicCast(OSString, propObject);
1372*2c2f96dcSApple OSS Distributions 		if (tmpString != nullptr) {
1373*2c2f96dcSApple OSS Distributions 			offset = tmpString->getLength();
1374*2c2f96dcSApple OSS Distributions 
1375*2c2f96dcSApple OSS Distributions 			if (buffer) {
1376*2c2f96dcSApple OSS Distributions 				if (*length < offset) {
1377*2c2f96dcSApple OSS Distributions 					return false;
1378*2c2f96dcSApple OSS Distributions 				} else {
1379*2c2f96dcSApple OSS Distributions 					bcopy(tmpString->getCStringNoCopy(), buffer, offset);
1380*2c2f96dcSApple OSS Distributions 				}
1381*2c2f96dcSApple OSS Distributions 			}
1382*2c2f96dcSApple OSS Distributions 		}
1383*2c2f96dcSApple OSS Distributions 		break;
1384*2c2f96dcSApple OSS Distributions 
1385*2c2f96dcSApple OSS Distributions 	case kOFVariableTypeData:
1386*2c2f96dcSApple OSS Distributions 		tmpData = OSDynamicCast(OSData, propObject);
1387*2c2f96dcSApple OSS Distributions 		if (tmpData != nullptr) {
1388*2c2f96dcSApple OSS Distributions 			offset = tmpData->getLength();
1389*2c2f96dcSApple OSS Distributions 
1390*2c2f96dcSApple OSS Distributions 			if (buffer) {
1391*2c2f96dcSApple OSS Distributions 				if (*length < offset) {
1392*2c2f96dcSApple OSS Distributions 					return false;
1393*2c2f96dcSApple OSS Distributions 				} else {
1394*2c2f96dcSApple OSS Distributions 					bcopy(tmpData->getBytesNoCopy(), buffer, offset);
1395*2c2f96dcSApple OSS Distributions 				}
1396*2c2f96dcSApple OSS Distributions 			}
1397*2c2f96dcSApple OSS Distributions 		}
1398*2c2f96dcSApple OSS Distributions 		break;
1399*2c2f96dcSApple OSS Distributions 
1400*2c2f96dcSApple OSS Distributions 	default:
1401*2c2f96dcSApple OSS Distributions 		return false;
1402*2c2f96dcSApple OSS Distributions 	}
1403*2c2f96dcSApple OSS Distributions 
1404*2c2f96dcSApple OSS Distributions 	*length = offset;
1405*2c2f96dcSApple OSS Distributions 
1406*2c2f96dcSApple OSS Distributions 	return offset != 0;
1407*2c2f96dcSApple OSS Distributions }
1408*2c2f96dcSApple OSS Distributions 
1409*2c2f96dcSApple OSS Distributions 
1410*2c2f96dcSApple OSS Distributions bool
convertPropToObject(const uint8_t * propName,uint32_t propNameLength,const uint8_t * propData,uint32_t propDataLength,OSSharedPtr<const OSSymbol> & propSymbol,OSSharedPtr<OSObject> & propObject)1411*2c2f96dcSApple OSS Distributions IONVRAMV3Handler::convertPropToObject(const uint8_t *propName, uint32_t propNameLength,
1412*2c2f96dcSApple OSS Distributions     const uint8_t *propData, uint32_t propDataLength,
1413*2c2f96dcSApple OSS Distributions     OSSharedPtr<const OSSymbol>& propSymbol,
1414*2c2f96dcSApple OSS Distributions     OSSharedPtr<OSObject>& propObject)
1415*2c2f96dcSApple OSS Distributions {
1416*2c2f96dcSApple OSS Distributions 	OSSharedPtr<const OSSymbol> tmpSymbol;
1417*2c2f96dcSApple OSS Distributions 	OSSharedPtr<OSNumber>       tmpNumber;
1418*2c2f96dcSApple OSS Distributions 	OSSharedPtr<OSString>       tmpString;
1419*2c2f96dcSApple OSS Distributions 	OSSharedPtr<OSObject>       tmpObject = nullptr;
1420*2c2f96dcSApple OSS Distributions 
1421*2c2f96dcSApple OSS Distributions 	tmpSymbol = OSSymbol::withCString((const char *)propName);
1422*2c2f96dcSApple OSS Distributions 
1423*2c2f96dcSApple OSS Distributions 	if (tmpSymbol == nullptr) {
1424*2c2f96dcSApple OSS Distributions 		return false;
1425*2c2f96dcSApple OSS Distributions 	}
1426*2c2f96dcSApple OSS Distributions 
1427*2c2f96dcSApple OSS Distributions 	switch (getVariableType(tmpSymbol.get())) {
1428*2c2f96dcSApple OSS Distributions 	case kOFVariableTypeBoolean:
1429*2c2f96dcSApple OSS Distributions 		if (!strncmp("true", (const char *)propData, propDataLength)) {
1430*2c2f96dcSApple OSS Distributions 			tmpObject.reset(kOSBooleanTrue, OSRetain);
1431*2c2f96dcSApple OSS Distributions 		} else if (!strncmp("false", (const char *)propData, propDataLength)) {
1432*2c2f96dcSApple OSS Distributions 			tmpObject.reset(kOSBooleanFalse, OSRetain);
1433*2c2f96dcSApple OSS Distributions 		}
1434*2c2f96dcSApple OSS Distributions 		break;
1435*2c2f96dcSApple OSS Distributions 
1436*2c2f96dcSApple OSS Distributions 	case kOFVariableTypeNumber:
1437*2c2f96dcSApple OSS Distributions 		tmpNumber = OSNumber::withNumber(strtol((const char *)propData, nullptr, 0), 32);
1438*2c2f96dcSApple OSS Distributions 		if (tmpNumber != nullptr) {
1439*2c2f96dcSApple OSS Distributions 			tmpObject = tmpNumber;
1440*2c2f96dcSApple OSS Distributions 		}
1441*2c2f96dcSApple OSS Distributions 		break;
1442*2c2f96dcSApple OSS Distributions 
1443*2c2f96dcSApple OSS Distributions 	case kOFVariableTypeString:
1444*2c2f96dcSApple OSS Distributions 		tmpString = OSString::withCString((const char *)propData, propDataLength);
1445*2c2f96dcSApple OSS Distributions 		if (tmpString != nullptr) {
1446*2c2f96dcSApple OSS Distributions 			tmpObject = tmpString;
1447*2c2f96dcSApple OSS Distributions 		}
1448*2c2f96dcSApple OSS Distributions 		break;
1449*2c2f96dcSApple OSS Distributions 
1450*2c2f96dcSApple OSS Distributions 	case kOFVariableTypeData:
1451*2c2f96dcSApple OSS Distributions 		tmpObject = OSData::withBytes(propData, propDataLength);
1452*2c2f96dcSApple OSS Distributions 		break;
1453*2c2f96dcSApple OSS Distributions 
1454*2c2f96dcSApple OSS Distributions 	default:
1455*2c2f96dcSApple OSS Distributions 		break;
1456*2c2f96dcSApple OSS Distributions 	}
1457*2c2f96dcSApple OSS Distributions 
1458*2c2f96dcSApple OSS Distributions 	if (tmpObject == nullptr) {
1459*2c2f96dcSApple OSS Distributions 		tmpSymbol.reset();
1460*2c2f96dcSApple OSS Distributions 		return false;
1461*2c2f96dcSApple OSS Distributions 	}
1462*2c2f96dcSApple OSS Distributions 
1463*2c2f96dcSApple OSS Distributions 	propSymbol = tmpSymbol;
1464*2c2f96dcSApple OSS Distributions 	propObject = tmpObject;
1465*2c2f96dcSApple OSS Distributions 
1466*2c2f96dcSApple OSS Distributions 	return true;
1467*2c2f96dcSApple OSS Distributions }
1468