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