1/*
2 *  kext_tools_util.h
3 *  kext_tools
4 *
5 *  Created by Nik Gervae on 4/25/08.
6 *  Copyright 2008 Apple Inc. All rights reserved.
7 *
8 */
9#ifndef _KEXT_TOOLS_UTIL_H
10#define _KEXT_TOOLS_UTIL_H
11
12#include <CoreFoundation/CoreFoundation.h>
13#include <IOKit/kext/OSKext.h>
14#include <mach/mach_error.h>
15
16#include <getopt.h>
17#include <sysexits.h>
18#include <sys/stat.h>
19#include <sys/time.h>
20#include <sys/types.h>
21
22#pragma mark Types
23/*******************************************************************************
24* Types
25*******************************************************************************/
26typedef int ExitStatus;
27
28typedef enum {
29    kUsageLevelBrief = 0,
30    kUsageLevelFull  = 1
31} UsageLevel;
32
33typedef struct {
34    CFURLRef   saveDirURL;
35    Boolean    overwrite;
36    Boolean    fatal;
37} SaveFileContext;
38
39#pragma mark Constants
40/*******************************************************************************
41* Constants
42*******************************************************************************/
43#define _kKextPropertyValuesCacheBasename  "KextPropertyValues_"
44#define __kOSKextApplePrefix        CFSTR("com.apple.")
45
46#pragma mark Macros
47/*********************************************************************
48* Macros
49*********************************************************************/
50
51#define SAFE_FREE(ptr)  do {          \
52    if (ptr) free(ptr);               \
53    } while (0)
54#define SAFE_FREE_NULL(ptr)  do {     \
55    if (ptr) free(ptr);               \
56    (ptr) = NULL;                     \
57    } while (0)
58#define SAFE_RELEASE(ptr)  do {       \
59    if (ptr) CFRelease(ptr);          \
60    } while (0)
61#define SAFE_RELEASE_NULL(ptr)  do {  \
62    if (ptr) CFRelease(ptr);          \
63    (ptr) = NULL;                     \
64    } while (0)
65
66#define RANGE_ALL(a)   CFRangeMake(0, CFArrayGetCount(a))
67
68#define COMPILE_TIME_ASSERT(pred)   switch(0){case 0:case pred:;}
69
70/*********************************************************************
71*********************************************************************/
72
73/* Library default is not to log Basic, but kextd & kextcache spawned
74 * by kextd want to do that.
75 */
76#define kDefaultServiceLogFilter ((OSKextLogSpec) kOSKextLogBasicLevel | \
77                                             kOSKextLogVerboseFlagsMask)
78
79#pragma mark Shared Command-line Option Definitions
80/*******************************************************************************
81* Command-line options. This data is used by getopt_long_only().
82*******************************************************************************/
83#define kOptNameArch                    "arch"
84#define kOptNameBundleIdentifier        "bundle-id"
85#define kOptNameRepository              "repository"
86
87#define kOptNameSafeBoot                "safe-boot"
88#define kOptNameSystemExtensions        "system-extensions"
89#define kOptNameKernel                  "kernel"
90#define kOptNameNoAuthentication        "no-authentication"
91
92#define kOptNameHelp                    "help"
93#define kOptNameQuiet                   "quiet"
94#define kOptNameVerbose                 "verbose"
95
96#define kOptNameLongindexHack           "________"
97
98// Can't use -a globally for -arch because of kextutil...?
99#define kOptBundleIdentifier 'b'
100// Can't use -r globally for -repository because of kextcache...?
101
102// Can't use -x globally for -safe-boot because of kextcache's -s...?
103// Can't use -e globally for -system-extensions because of kextutil...?
104// Can't use -k globally for -kernel because of kextcache...?
105#define kOptNoAuthentication 'z'
106
107#define kOptHelp             'h'
108#define kOptQuiet            'q'
109#define kOptVerbose          'v'
110
111// Long opts always defined in each program to avoid # collisions
112
113#pragma mark Function Protos
114/*********************************************************************
115* Function Protos
116*********************************************************************/
117
118Boolean createCFMutableArray(CFMutableArrayRef * arrayOut,
119    const CFArrayCallBacks * callbacks);
120Boolean createCFMutableDictionary(CFMutableDictionaryRef * dictionaryOut);
121Boolean createCFMutableSet(CFMutableSetRef * setOut,
122    const CFSetCallBacks * callbacks);
123Boolean createCFDataFromFile(CFDataRef  *dataRefOut,
124                             const char *filePath);
125
126void addToArrayIfAbsent(CFMutableArrayRef array, const void * value);
127
128ExitStatus checkPath(
129    const char * path,
130    const char * suffix,  // w/o the dot
131    Boolean      directoryRequired,
132    Boolean      writableRequired);
133
134ExitStatus setLogFilterForOpt(
135    int            argc,
136    char * const * argv,
137    OSKextLogSpec  forceOnFlags);
138
139void beQuiet(void);
140
141FILE *  g_log_stream;
142// tool_openlog(), tool_log() copied to bootroot.h for libBootRoot clients
143void tool_openlog(const char * name);
144void tool_log(
145    OSKextRef aKext,
146    OSKextLogSpec logSpec,
147    const char * format,
148    ...);
149void log_CFError(
150    OSKextRef     aKext __unused,
151    OSKextLogSpec msgLogSpec,
152    CFErrorRef    error);
153
154const char * safe_mach_error_string(mach_error_t error_code);
155
156#define REPLY_ERROR (-1)
157#define REPLY_NO     (0)
158#define REPLY_YES    (1)
159#define REPLY_ALL    (2)
160
161int user_approve(Boolean ask_all, int default_answer, const char * format, ...);
162
163const char * user_input(Boolean * eof, const char * format, ...);
164void saveFile(const void * vKey, const void * vValue, void * vContext);
165
166CFStringRef copyKextPath(OSKextRef aKext);
167Boolean readSystemKextPropertyValues(
168    CFStringRef        propertyKey,
169    const NXArchInfo * arch,
170    Boolean            forceUpdateFlag,
171    CFArrayRef       * valuesOut);
172
173ExitStatus writeToFile(
174    int           fileDescriptor,
175    const UInt8 * data,
176    CFIndex       length);
177
178ExitStatus statURL(CFURLRef anURL, struct stat * statBuffer);
179ExitStatus statPath(const char *path, struct stat *statBuffer);
180ExitStatus getLatestTimesFromCFURLArray(
181                                        CFArrayRef          fileURLArray,
182                                        struct timeval      fileTimes[2]);
183ExitStatus getFilePathTimes(
184                            const char        * filePath,
185                            struct timeval      cacheFileTimes[2]);
186
187void postNoteAboutKexts(
188                        CFStringRef theNotificationCenterName,
189                        CFMutableDictionaryRef theDict );
190
191void postNoteAboutKextLoadsMT(
192                            CFStringRef theNotificationCenterName,
193                            CFMutableArrayRef theKextPathArray );
194
195void addKextToAlertDict(
196                        CFMutableDictionaryRef *theDictPtr,
197                        OSKextRef theKext );
198
199
200/*********************************************************************
201* From IOKitUser/kext.subproj/OSKext.c.
202*********************************************************************/
203
204extern char * createUTF8CStringForCFString(CFStringRef aString);
205
206#endif /* _KEXT_TOOLS_UTIL_H */
207