xref: /xnu-8020.101.4/iokit/IOKit/IONVRAM.h (revision e7776783b89a353188416a9a346c6cdb4928faad)
1 /*
2  * Copyright (c) 1998-2006 Apple Computer, Inc. All rights reserved.
3  * Copyright (c) 2007-2021 Apple Inc. All rights reserved.
4  *
5  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6  *
7  * This file contains Original Code and/or Modifications of Original Code
8  * as defined in and that are subject to the Apple Public Source License
9  * Version 2.0 (the 'License'). You may not use this file except in
10  * compliance with the License. The rights granted to you under the License
11  * may not be used to create, or enable the creation or redistribution of,
12  * unlawful or unlicensed copies of an Apple operating system, or to
13  * circumvent, violate, or enable the circumvention or violation of, any
14  * terms of an Apple operating system software license agreement.
15  *
16  * Please obtain a copy of the License at
17  * http://www.opensource.apple.com/apsl/ and read it before using this file.
18  *
19  * The Original Code and all software distributed under the License are
20  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
21  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
22  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
24  * Please see the License for the specific language governing rights and
25  * limitations under the License.
26  *
27  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28  */
29 
30 #ifndef _IOKIT_IONVRAM_H
31 #define _IOKIT_IONVRAM_H
32 
33 #ifdef __cplusplus
34 #include <libkern/c++/OSPtr.h>
35 #include <IOKit/IOKitKeys.h>
36 #include <IOKit/IOService.h>
37 #include <IOKit/IODeviceTreeSupport.h>
38 #include <IOKit/nvram/IONVRAMController.h>
39 #endif /* __cplusplus */
40 #include <uuid/uuid.h>
41 
42 enum NVRAMPartitionType {
43 	kIONVRAMPartitionTypeUnknown,
44 	kIONVRAMPartitionSystem,
45 	kIONVRAMPartitionCommon
46 };
47 
48 enum IONVRAMVariableType {
49 	kOFVariableTypeBoolean = 1,
50 	kOFVariableTypeNumber,
51 	kOFVariableTypeString,
52 	kOFVariableTypeData
53 };
54 
55 enum IONVRAMOperation {
56 	kIONVRAMOperationInit,
57 	kIONVRAMOperationRead,
58 	kIONVRAMOperationWrite,
59 	kIONVRAMOperationDelete,
60 	kIONVRAMOperationObliterate,
61 	kIONVRAMOperationReset
62 };
63 
64 enum {
65 	// Deprecated but still used in AppleEFIRuntime for now
66 	kOFVariablePermRootOnly = 0,
67 	kOFVariablePermUserRead,
68 	kOFVariablePermUserWrite,
69 	kOFVariablePermKernelOnly
70 };
71 
72 #ifdef __cplusplus
73 
74 class IODTNVRAMVariables;
75 class IODTNVRAMDiags;
76 
77 class IODTNVRAMFormatHandler
78 {
79 public:
80 	virtual
81 	~IODTNVRAMFormatHandler();
82 	virtual IOReturn setVariable(const uuid_t *varGuid, const char *variableName, OSObject *object) = 0;
83 	virtual bool setController(IONVRAMController *_nvramController) = 0;
84 	virtual bool sync(void) = 0;
85 	virtual uint32_t getGeneration(void) const = 0;
86 	virtual uint32_t getVersion(void) const = 0;
87 	virtual uint32_t getSystemUsed(void) const = 0;
88 	virtual uint32_t getCommonUsed(void) const = 0;
89 };
90 
91 class IODTNVRAM : public IOService
92 {
93 	OSDeclareDefaultStructors(IODTNVRAM);
94 
95 private:
96 	friend class IODTNVRAMVariables;
97 	friend class IONVRAMCHRPHandler;
98 	friend class IONVRAMV3Handler;
99 
100 	IODTNVRAMDiags         *_diags;
101 	IODTNVRAMFormatHandler *_format;
102 
103 	IORWLock               *_variableLock;
104 	IOLock                 *_controllerLock;
105 
106 	IODTNVRAMVariables     *_commonService;
107 	IODTNVRAMVariables     *_systemService;
108 
109 	OSPtr<OSDictionary>    _commonDict;
110 	OSPtr<OSDictionary>    _systemDict;
111 
112 	SInt32                 _lastDeviceSync;
113 	bool                   _freshInterval;
114 
115 	void initImageFormat(void);
116 
117 	uint32_t getNVRAMSize(void);
118 
119 	NVRAMPartitionType getDictionaryType(const OSDictionary *dict) const;
120 	IOReturn chooseDictionary(IONVRAMOperation operation, const uuid_t *varGuid,
121 	    const char *variableName, OSDictionary **dict) const;
122 	IOReturn flushDict(const uuid_t *guid, IONVRAMOperation op);
123 	bool handleSpecialVariables(const char *name, const uuid_t *guid, const OSObject *obj, IOReturn *error);
124 
125 	IOReturn setPropertyInternal(const OSSymbol *aKey, OSObject *anObject);
126 	IOReturn removePropertyInternal(const OSSymbol *aKey);
127 	OSSharedPtr<OSObject> copyPropertyWithGUIDAndName(const uuid_t *guid, const char *name) const;
128 	IOReturn removePropertyWithGUIDAndName(const uuid_t *guid, const char *name);
129 	IOReturn setPropertyWithGUIDAndName(const uuid_t *guid, const char *name, OSObject *anObject);
130 
131 	void syncInternal(bool rateLimit);
132 	bool safeToSync(void);
133 
134 public:
135 	virtual bool init(IORegistryEntry *old, const IORegistryPlane *plane) APPLE_KEXT_OVERRIDE;
136 	virtual bool start(IOService * provider) APPLE_KEXT_OVERRIDE;
137 
138 	virtual void registerNVRAMController(IONVRAMController *controller);
139 
140 	virtual void sync(void);
141 
142 	virtual bool serializeProperties(OSSerialize *s) const APPLE_KEXT_OVERRIDE;
143 	virtual OSPtr<OSObject> copyProperty(const OSSymbol *aKey) const APPLE_KEXT_OVERRIDE;
144 	virtual OSPtr<OSObject> copyProperty(const char *aKey) const APPLE_KEXT_OVERRIDE;
145 	virtual OSObject *getProperty(const OSSymbol *aKey) const APPLE_KEXT_OVERRIDE;
146 	virtual OSObject *getProperty(const char *aKey) const APPLE_KEXT_OVERRIDE;
147 	virtual bool setProperty(const OSSymbol *aKey, OSObject *anObject) APPLE_KEXT_OVERRIDE;
148 	virtual void removeProperty(const OSSymbol *aKey) APPLE_KEXT_OVERRIDE;
149 	virtual IOReturn setProperties(OSObject *properties) APPLE_KEXT_OVERRIDE;
150 
151 	virtual IOReturn readXPRAM(IOByteCount offset, uint8_t *buffer,
152 	    IOByteCount length);
153 	virtual IOReturn writeXPRAM(IOByteCount offset, uint8_t *buffer,
154 	    IOByteCount length);
155 
156 	virtual IOReturn readNVRAMProperty(IORegistryEntry *entry,
157 	    const OSSymbol **name,
158 	    OSData **value);
159 	virtual IOReturn writeNVRAMProperty(IORegistryEntry *entry,
160 	    const OSSymbol *name,
161 	    OSData *value);
162 
163 	virtual OSDictionary *getNVRAMPartitions(void);
164 
165 	virtual IOReturn readNVRAMPartition(const OSSymbol *partitionID,
166 	    IOByteCount offset, uint8_t *buffer,
167 	    IOByteCount length);
168 
169 	virtual IOReturn writeNVRAMPartition(const OSSymbol *partitionID,
170 	    IOByteCount offset, uint8_t *buffer,
171 	    IOByteCount length);
172 
173 	virtual IOByteCount savePanicInfo(uint8_t *buffer, IOByteCount length);
174 };
175 
176 #endif /* __cplusplus */
177 
178 #endif /* !_IOKIT_IONVRAM_H */
179