1/*
2 * Copyright (c) 1998-2006 Apple Computer, Inc. All rights reserved.
3 * Copyright (c) 2007-2012 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 <IOKit/IOKitKeys.h>
35#include <IOKit/IOService.h>
36#include <IOKit/IODeviceTreeSupport.h>
37#include <IOKit/nvram/IONVRAMController.h>
38#endif /* __cplusplus */
39
40#define kIODTNVRAMOFPartitionName       "common"
41#define kIODTNVRAMXPRAMPartitionName    "APL,MacOS75"
42#define kIODTNVRAMPanicInfoPartitonName "APL,OSXPanic"
43#define kIODTNVRAMFreePartitionName     "wwwwwwwwwwww"
44
45#define MIN_SYNC_NOW_INTERVAL 15*60 /* Minimum 15 Minutes interval mandated */
46
47enum {
48  kIODTNVRAMImageSize        = 0x2000,
49  kIODTNVRAMXPRAMSize        = 0x0100,
50  kIODTNVRAMNameRegistrySize = 0x0400
51};
52
53enum {
54  kOFVariableTypeBoolean = 1,
55  kOFVariableTypeNumber,
56  kOFVariableTypeString,
57  kOFVariableTypeData
58};
59
60enum {
61  kOFVariablePermRootOnly = 0,
62  kOFVariablePermUserRead,
63  kOFVariablePermUserWrite,
64  kOFVariablePermKernelOnly
65};
66
67#ifdef __cplusplus
68
69class IODTNVRAM : public IOService
70{
71  OSDeclareDefaultStructors(IODTNVRAM);
72
73private:
74  IONVRAMController *_nvramController;
75  const OSSymbol    *_registryPropertiesKey;
76  UInt8             *_nvramImage;
77  bool              _nvramImageDirty;
78  UInt32            _ofPartitionOffset;
79  UInt32            _ofPartitionSize;
80  UInt8             *_ofImage;
81  bool              _ofImageDirty;
82  OSDictionary      *_ofDict;
83  OSDictionary      *_nvramPartitionOffsets;
84  OSDictionary      *_nvramPartitionLengths;
85  UInt32            _resv0 __unused;
86  UInt32            _resv1 __unused;
87  IOLock            *_ofLock;
88  UInt32            _resv2 __unused;
89  UInt32            _resv3 __unused;
90  UInt8             *_resv4 __unused;
91  UInt32            _piPartitionOffset;
92  UInt32            _piPartitionSize;
93  UInt8             *_piImage;
94  bool              _systemPaniced;
95  SInt32            _lastDeviceSync;
96  bool              _freshInterval;
97  bool              _isProxied;
98
99  virtual UInt8 calculatePartitionChecksum(UInt8 *partitionHeader);
100  virtual IOReturn initOFVariables(void);
101public:
102  virtual IOReturn syncOFVariables(void);
103private:
104  virtual UInt32 getOFVariableType(const OSSymbol *propSymbol) const;
105  virtual UInt32 getOFVariablePerm(const OSSymbol *propSymbol) const;
106  virtual bool getOWVariableInfo(UInt32 variableNumber, const OSSymbol **propSymbol,
107				 UInt32 *propType, UInt32 *propOffset);
108  virtual bool convertPropToObject(UInt8 *propName, UInt32 propNameLength,
109				   UInt8 *propData, UInt32 propDataLength,
110				   const OSSymbol **propSymbol,
111				   OSObject **propObject);
112  virtual bool convertObjectToProp(UInt8 *buffer, UInt32 *length,
113				   const OSSymbol *propSymbol, OSObject *propObject);
114  virtual UInt16 generateOWChecksum(UInt8 *buffer);
115  virtual bool validateOWChecksum(UInt8 *buffer);
116  virtual void updateOWBootArgs(const OSSymbol *key, OSObject *value);
117  virtual bool searchNVRAMProperty(struct IONVRAMDescriptor *hdr,
118				   UInt32 *where);
119
120  virtual IOReturn readNVRAMPropertyType0(IORegistryEntry *entry,
121					  const OSSymbol **name,
122					  OSData **value);
123  virtual IOReturn writeNVRAMPropertyType0(IORegistryEntry *entry,
124					   const OSSymbol *name,
125					   OSData * value);
126
127  virtual OSData *unescapeBytesToData(const UInt8 *bytes, UInt32 length);
128  virtual OSData *escapeDataToData(OSData * value);
129
130  virtual IOReturn readNVRAMPropertyType1(IORegistryEntry *entry,
131					  const OSSymbol **name,
132					  OSData **value);
133  virtual IOReturn writeNVRAMPropertyType1(IORegistryEntry *entry,
134					   const OSSymbol *name,
135					   OSData *value);
136
137  void initNVRAMImage(void);
138  void initProxyData(void);
139
140public:
141  virtual bool init(IORegistryEntry *old, const IORegistryPlane *plane);
142
143  virtual void registerNVRAMController(IONVRAMController *nvram);
144
145  virtual void sync(void);
146
147  virtual bool serializeProperties(OSSerialize *s) const;
148  virtual OSObject *copyProperty(const OSSymbol *aKey) const;
149  virtual OSObject *copyProperty(const char *aKey) const;
150  virtual OSObject *getProperty(const OSSymbol *aKey) const;
151  virtual OSObject *getProperty(const char *aKey) const;
152  virtual bool setProperty(const OSSymbol *aKey, OSObject *anObject);
153  virtual void removeProperty(const OSSymbol *aKey);
154  virtual IOReturn setProperties(OSObject *properties);
155
156  virtual IOReturn readXPRAM(IOByteCount offset, UInt8 *buffer,
157			     IOByteCount length);
158  virtual IOReturn writeXPRAM(IOByteCount offset, UInt8 *buffer,
159			      IOByteCount length);
160
161  virtual IOReturn readNVRAMProperty(IORegistryEntry *entry,
162				     const OSSymbol **name,
163				     OSData **value);
164  virtual IOReturn writeNVRAMProperty(IORegistryEntry *entry,
165				      const OSSymbol *name,
166				      OSData *value);
167
168  virtual OSDictionary *getNVRAMPartitions(void);
169
170  virtual IOReturn readNVRAMPartition(const OSSymbol *partitionID,
171				      IOByteCount offset, UInt8 *buffer,
172				      IOByteCount length);
173
174  virtual IOReturn writeNVRAMPartition(const OSSymbol *partitionID,
175				       IOByteCount offset, UInt8 *buffer,
176				       IOByteCount length);
177
178  virtual IOByteCount savePanicInfo(UInt8 *buffer, IOByteCount length);
179  virtual bool safeToSync(void);
180};
181
182#endif /* __cplusplus */
183
184#endif /* !_IOKIT_IONVRAM_H */
185