2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 cc -o nvram nvram.c -framework IOKit -Wall
30 #include <IOKit/IOKitLib.h>
31 #include <CoreFoundation/CoreFoundation.h>
34 static void Error(char *format
, long item
);
35 static void FatalError(long exitValue
, char *format
, long item
);
36 static void UsageMessage(char *message
);
37 static void ParseFile(char *fileName
);
38 static void SetOrGetOFVariable(char *str
);
39 static kern_return_t
GetOFVariable(char *name
, CFStringRef
*nameRef
,
41 static kern_return_t
SetOFVariable(char *name
, char *value
);
42 static void PrintOFVariables(void);
43 static void PrintOFVariable(const void *key
,const void *value
,void *context
);
44 static CFTypeRef
ConvertValueToCFTypeRef(CFTypeID typeID
, char *value
);
47 static char *gToolName
;
48 static io_registry_entry_t gOptionsRef
;
51 int main(int argc
, char **argv
)
54 char *str
, errorMessage
[256];
56 mach_port_t masterPort
;
58 // Get the name of the command.
59 gToolName
= strrchr(argv
[0], '/');
60 if (gToolName
!= 0) gToolName
++;
61 else gToolName
= argv
[0];
63 result
= IOMasterPort(bootstrap_port
, &masterPort
);
64 if (result
!= KERN_SUCCESS
) {
65 FatalError(-1, "Error (%d) getting the IOMaster port", result
);
69 gOptionsRef
= IORegistryEntryFromPath(masterPort
, "IODeviceTree:/options");
70 if (gOptionsRef
== 0) {
71 FatalError(-1, "Error (%d) getting a reference to /options", -1);
75 for (cnt
= 1; cnt
< argc
; cnt
++) {
77 if (str
[0] == '-' && str
[1] != 0) {
79 for (str
+= 1 ; *str
; str
++) {
87 if (cnt
< argc
&& *argv
[cnt
] != '-') {
90 UsageMessage("missing filename");
95 strcpy(errorMessage
, "no such option as --");
96 errorMessage
[strlen(errorMessage
)-1] = *str
;
97 UsageMessage(errorMessage
);
101 // Other arguments will be Open Firmware variable requests.
102 SetOrGetOFVariable(str
);
106 IOObjectRelease(gOptionsRef
);
112 // Error(format, item)
114 // Print a message on standard error.
116 static void Error(char *format
, long item
)
118 fprintf(stderr
, "%s: ", gToolName
);
119 fprintf(stderr
, format
, item
);
120 fprintf(stderr
, "\n");
124 // FatalError(exitValue, format, item)
126 // Print a message on standard error and exit with value.
128 static void FatalError(long exitValue
, char *format
, long item
)
130 fprintf(stderr
, "%s: ", gToolName
);
131 fprintf(stderr
, format
, item
);
132 fprintf(stderr
, "\n");
138 // UsageMessage(message)
140 // Print the usage information and exit.
142 static void UsageMessage(char *message
)
144 Error("(usage: %s)", (long)message
);
146 printf("%s [-p] [-f filename] name[=value] ...\n", gToolName
);
147 printf("\t-p print all Open Firmware variables\n");
148 printf("\t-f set Open Firmware variables from a text file\n");
149 printf("\tname=value set named variable\n");
150 printf("\tname print variable\n");
151 printf("Note that arguments and options are executed in order.\n");
157 // States for ParseFile.
168 kMaxStringSize
= 0x800,
173 // ParseFile(fileName)
175 // Open and parse the specified file.
177 static void ParseFile(char *fileName
)
179 long state
, tc
, ni
= 0, vi
= 0;
180 char name
[kMaxNameSize
];
181 char value
[kMaxStringSize
];
184 patches
= fopen(fileName
, "r");
186 FatalError(errno
, "Couldn't open patch file - '%s'", (long)fileName
);
189 state
= kFirstColumn
;
190 while ((tc
= getc(patches
)) != EOF
) {
196 state
= kScanComment
;
197 } else if (tc
== '\n') {
198 // state stays kFirstColumn.
199 } else if (isspace(tc
)) {
202 state
= kCollectName
;
209 state
= kFirstColumn
;
211 // state stays kScanComment.
217 state
= kFirstColumn
;
218 } else if (isspace(tc
)) {
219 // state stays kFindName.
221 state
= kCollectName
;
229 Error("Name must be followed by white space - '%s'", (long)name
);
230 state
= kFirstColumn
;
231 } else if (isspace(tc
)) {
235 // state staus kCollectName.
240 case kContinueValue
:
243 } else if (isspace(tc
)) {
244 // state stays kFindValue or kContinueValue.
246 state
= kCollectValue
;
253 if (value
[vi
-1] == '\\') {
255 state
= kContinueValue
;
260 // state stays kCollectValue.
266 if (state
== kSetenv
) {
269 if (SetOFVariable(name
, value
) != KERN_SUCCESS
) {
270 FatalError(-1, "Error (-1) setting variable - '%s'", (long)name
);
272 state
= kFirstColumn
;
276 if (state
!= kFirstColumn
) {
277 FatalError(-1, "Last line ended abruptly", 0);
282 // SetOrGetOFVariable(str)
284 // Parse the input string the set or get the specified
285 // Open Firmware variable.
287 static void SetOrGetOFVariable(char *str
)
294 kern_return_t result
;
296 // OF variable name is first.
299 // Find the equal sign for set
310 // On sets, the OF variable's value follows the equal sign.
313 result
= SetOFVariable(name
, value
);
314 if (result
!= KERN_SUCCESS
) {
315 FatalError(-1, "Error (-1) setting variable - '%s'", (long)name
);
318 result
= GetOFVariable(name
, &nameRef
, &valueRef
);
319 if (result
!= KERN_SUCCESS
) {
320 FatalError(-1, "Error (-1) getting variable - '%s'", (long)name
);
323 PrintOFVariable(nameRef
, valueRef
, 0);
330 // GetOFVariable(name, nameRef, valueRef)
332 // Get the named Open Firmware variable.
333 // Return it and it's symbol in valueRef and nameRef.
335 static kern_return_t
GetOFVariable(char *name
, CFStringRef
*nameRef
,
338 *nameRef
= CFStringCreateWithCString(kCFAllocatorDefault
, name
,
339 kCFStringEncodingMacRoman
);
341 FatalError(-1, "Error CFString for key %s", (long)name
);
344 *valueRef
= IORegistryEntryCreateCFProperty(gOptionsRef
, *nameRef
, 0, 0);
345 if (*valueRef
== 0) return -1;
351 // SetOFVariable(name, value)
353 // Set or create an Open Firmware variable with name and value.
355 static kern_return_t
SetOFVariable(char *name
, char *value
)
360 kern_return_t result
;
362 nameRef
= CFStringCreateWithCString(kCFAllocatorDefault
, name
,
363 kCFStringEncodingMacRoman
);
365 FatalError(-1, "Error (-1) creating CFString for key %s", (long)name
);
368 valueRef
= IORegistryEntryCreateCFProperty(gOptionsRef
, nameRef
, 0, 0);
370 typeID
= CFGetTypeID(valueRef
);
373 valueRef
= ConvertValueToCFTypeRef(typeID
, value
);
375 FatalError(-1, "Error (-1) creating CFTypeRef for value %s",(long)value
);
376 } result
= IORegistryEntrySetCFProperty(gOptionsRef
, nameRef
, valueRef
);
379 // In the default case, try data, string, number, then boolean.
381 valueRef
= ConvertValueToCFTypeRef(CFDataGetTypeID(), value
);
383 result
= IORegistryEntrySetCFProperty(gOptionsRef
, nameRef
, valueRef
);
384 if (result
== KERN_SUCCESS
) break;
387 valueRef
= ConvertValueToCFTypeRef(CFStringGetTypeID(), value
);
389 result
= IORegistryEntrySetCFProperty(gOptionsRef
, nameRef
, valueRef
);
390 if (result
== KERN_SUCCESS
) break;
393 valueRef
= ConvertValueToCFTypeRef(CFNumberGetTypeID(), value
);
395 result
= IORegistryEntrySetCFProperty(gOptionsRef
, nameRef
, valueRef
);
396 if (result
== KERN_SUCCESS
) break;
399 valueRef
= ConvertValueToCFTypeRef(CFBooleanGetTypeID(), value
);
401 result
= IORegistryEntrySetCFProperty(gOptionsRef
, nameRef
, valueRef
);
402 if (result
== KERN_SUCCESS
) break;
416 // PrintOFVariables()
418 // Print all of the Open Firmware variables.
420 static void PrintOFVariables()
422 kern_return_t result
;
423 CFMutableDictionaryRef dict
;
425 result
= IORegistryEntryCreateCFProperties(gOptionsRef
, &dict
, 0, 0);
426 if (result
!= KERN_SUCCESS
) {
427 FatalError(-1, "Error (%d) getting the Open Firmware variables", result
);
429 CFDictionaryApplyFunction(dict
, &PrintOFVariable
, 0);
435 // PrintOFVariable(key, value, context)
437 // Print the given Open Firmware variable.
439 static void PrintOFVariable(const void *key
, const void *value
, void *context
)
442 const char *nameString
;
443 char numberBuffer
[10];
444 const uint8_t *dataPtr
;
446 char *dataBuffer
= 0;
447 const char *valueString
= 0;
448 uint32_t number
, length
;
451 // Get the OF variable's name.
452 nameString
= CFStringGetCStringPtr(key
, kCFStringEncodingMacRoman
);
454 // Get the OF variable's type.
455 typeID
= CFGetTypeID(value
);
457 if (typeID
== CFBooleanGetTypeID()) {
458 if (CFBooleanGetValue(value
)) valueString
= "true";
459 else valueString
= "false";
460 } else if (typeID
== CFNumberGetTypeID()) {
461 CFNumberGetValue(value
, kCFNumberSInt32Type
, &number
);
462 if (number
== 0xFFFFFFFF) sprintf(numberBuffer
, "-1");
463 else if (number
< 1000) sprintf(numberBuffer
, "%d", number
);
464 else sprintf(numberBuffer
, "0x%x", number
);
465 valueString
= numberBuffer
;
466 } else if (typeID
== CFStringGetTypeID()) {
467 valueString
= CFStringGetCStringPtr(value
, kCFStringEncodingMacRoman
);
468 } else if (typeID
== CFDataGetTypeID()) {
469 length
= CFDataGetLength(value
);
470 if (length
== 0) valueString
= "";
472 dataBuffer
= malloc(length
* 3 + 1);
473 if (dataBuffer
!= 0) {
474 dataPtr
= CFDataGetBytePtr(value
);
475 for (cnt
= cnt2
= 0; cnt
< length
; cnt
++) {
476 dataChar
= dataPtr
[cnt
];
477 if (isprint(dataChar
)) dataBuffer
[cnt2
++] = dataChar
;
479 sprintf(dataBuffer
+ cnt2
, "%%%02x", dataChar
);
483 dataBuffer
[cnt2
] = '\0';
484 valueString
= dataBuffer
;
489 if ((nameString
!= 0) && (valueString
!= 0))
490 printf("%s\t%s\n", nameString
, valueString
);
492 if (dataBuffer
!= 0) free(dataBuffer
);
496 // ConvertValueToCFTypeRef(typeID, value)
498 // Convert the value into a CFType given the typeID.
500 static CFTypeRef
ConvertValueToCFTypeRef(CFTypeID typeID
, char *value
)
502 CFTypeRef valueRef
= 0;
503 long cnt
, cnt2
, length
;
504 unsigned long number
, tmp
;
506 if (typeID
== CFBooleanGetTypeID()) {
507 if (!strcmp("true", value
)) valueRef
= kCFBooleanTrue
;
508 else if (!strcmp("false", value
)) valueRef
= kCFBooleanFalse
;
509 } else if (typeID
== CFNumberGetTypeID()) {
510 number
= strtol(value
, 0, 0);
511 valueRef
= CFNumberCreate(kCFAllocatorDefault
, kCFNumberSInt32Type
,
513 } else if (typeID
== CFStringGetTypeID()) {
514 valueRef
= CFStringCreateWithCString(kCFAllocatorDefault
, value
,
515 kCFStringEncodingMacRoman
);
516 } else if (typeID
== CFDataGetTypeID()) {
517 length
= strlen(value
);
518 for (cnt
= cnt2
= 0; cnt
< length
; cnt
++, cnt2
++) {
519 if (value
[cnt
] == '%') {
520 if (!ishexnumber(value
[cnt
+ 1]) ||
521 !ishexnumber(value
[cnt
+ 2])) return 0;
522 number
= toupper(value
[++cnt
]) - '0';
523 if (number
> 9) number
-= 7;
524 tmp
= toupper(value
[++cnt
]) - '0';
525 if (tmp
> 9) tmp
-= 7;
526 number
= (number
<< 4) + tmp
;
527 value
[cnt2
] = number
;
528 } else value
[cnt2
] = value
[cnt
];
530 valueRef
= CFDataCreateWithBytesNoCopy(kCFAllocatorDefault
, value
,
531 cnt2
, kCFAllocatorDefault
);