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