Chameleon

Chameleon Svn Source Tree

Root/branches/cparm/i386/util/amlsgn.m

1/*
2 Copyright (c) 2010, Intel Corporation
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 * Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright notice,
11 this list of conditions and the following disclaimer in the documentation
12 and/or other materials provided with the distribution.
13 * Neither the name of Intel Corporation nor the names of its contributors
14 may be used to endorse or promote products derived from this software
15 without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28/*
29 * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.
30 *
31 * @APPLE_LICENSE_HEADER_START@
32 *
33 * Portions Copyright (c) 1999-2003 Apple Computer, Inc. All Rights
34 * Reserved. This file contains Original Code and/or Modifications of
35 * Original Code as defined in and that are subject to the Apple Public
36 * Source License Version 2.0 (the 'License'). You may not use this file
37 * except in compliance with the License. Please obtain a copy of the
38 * License at http://www.apple.com/publicsource and read it before using
39 * this file.
40 *
41 * The Original Code and all software distributed under the License are
42 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
43 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
44 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
45 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
46 * License for the specific language governing rights and limitations
47 * under the License.
48 *
49 * @APPLE_LICENSE_HEADER_END@
50 */
51
52
53#import <Foundation/Foundation.h>
54#import <IOKit/IOReturn.h>
55
56#include "Intel_Acpi/acpidecode.h"
57#include "Intel_Acpi/ppmsetup.h"
58
59unsigned long uuid32;
60#define UUID_LEN16
61#define UUID_STR_LEN UUID_LEN*2 + 8
62typedef int8_t EFI_CHAR8;
63
64unsigned long
65adler32( unsigned char * buffer, long length );
66U8 GetChecksum(void *mem_addr, U32 mem_size);
67void SetChecksum(struct acpi_table_header *header);
68const char * getStringFromUUID(const EFI_CHAR8* eUUID);
69
70
71unsigned long
72adler32( unsigned char * buffer, long length )
73{
74 long cnt;
75 unsigned long result, lowHalf, highHalf;
76
77 lowHalf = 1;
78 highHalf = 0;
79
80for ( cnt = 0; cnt < length; cnt++ )
81 {
82 if ((cnt % 5000) == 0)
83 {
84 lowHalf %= 65521L;
85 highHalf %= 65521L;
86 }
87
88 lowHalf += buffer[cnt];
89 highHalf += lowHalf;
90 }
91
92lowHalf %= 65521L;
93highHalf %= 65521L;
94
95result = (highHalf << 16) | lowHalf;
96
97return result;
98}
99
100//-------------------------------------------------------------------------------
101//
102// Procedure: GetChecksum - Performs byte checksum
103//
104//-------------------------------------------------------------------------------
105U8 GetChecksum(void *mem_addr, U32 mem_size)
106{
107 U8 *current = mem_addr;
108 U8 *end = current + mem_size;
109 U8 checksum = 0;
110
111 for (; current < end; current++)
112 checksum = checksum + *current;
113
114 return (checksum);
115}
116
117void SetChecksum(struct acpi_table_header *header)
118{
119 header->Checksum = 0;
120 header->Checksum = 0 - GetChecksum(header, header->Length);
121}
122
123
124/** Transform a 16 bytes hexadecimal value UUID to a string */
125/* getStringFromUUID : credit to Rekursor (see convert.c) */
126const char * getStringFromUUID(const EFI_CHAR8* eUUID)
127{
128static char msg[UUID_STR_LEN] = "";
129if (!eUUID) return "";
130const unsigned char * uuid = (unsigned char*) eUUID;
131sprintf(msg, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
132 uuid[0], uuid[1], uuid[2], uuid[3],
133 uuid[4], uuid[5], uuid[6], uuid[7],
134 uuid[8], uuid[9], uuid[10],uuid[11],
135 uuid[12],uuid[13],uuid[14],uuid[15]);
136return msg ;
137}
138
139int main (int argc, const char * argv[]) {
140 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
141
142 mach_port_t masterPort;
143
144io_registry_entry_t entry = MACH_PORT_NULL;
145
146kern_return_t err;
147
148CFDataRef data = NULL;
149
150CFAllocatorRef allocator=kCFAllocatorDefault;
151
152const UInt8* rawdata = NULL;
153
154unsigned long uuid32 = 0,Model32 = 0 ;
155NSString *filename, *fullfilename ;
156NSArray *namechunks;
157NSMutableArray *chunks = nil;
158NSString *outStr, *inStr;
159
160if (argc == 2) {
161
162/* Creating output string */
163inStr = [NSString stringWithUTF8String:argv[1]];
164chunks = [[NSMutableArray alloc] init];
165[chunks addObjectsFromArray:[inStr componentsSeparatedByString: @"/"]];
166
167fullfilename = [chunks lastObject];
168namechunks = [fullfilename componentsSeparatedByString: @"."];
169filename = [namechunks objectAtIndex:0];
170
171[chunks removeLastObject];
172outStr = [chunks componentsJoinedByString: @"/"];
173
174} else if (argc == 3 ){
175inStr = [NSString stringWithUTF8String:argv[1]];
176BOOL isDirectory= FALSE;
177
178 chunks = [[NSMutableArray alloc] init];
179 [chunks addObjectsFromArray:[inStr componentsSeparatedByString: @"/"]];
180 fullfilename = [chunks lastObject];
181 namechunks = [fullfilename componentsSeparatedByString: @"."];
182 filename = [namechunks objectAtIndex:0];
183
184if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithUTF8String:argv[2]] isDirectory:&isDirectory] && isDirectory) {
185
186 outStr = [NSString stringWithUTF8String:argv[2]];
187
188}
189 else
190 {
191 [chunks removeLastObject];
192 outStr = [chunks componentsJoinedByString: @"/"];
193 }
194
195}
196 else
197 {
198printf("Usage: amlsgn input(as a file) output(as a directory)\n");
199goto out;
200 }
201
202do {
203BOOL isDirectory= FALSE;
204if ([[NSFileManager defaultManager] fileExistsAtPath:outStr isDirectory:&isDirectory] && isDirectory) {
205
206if ([[NSFileManager defaultManager] isWritableFileAtPath:outStr])
207break;
208}
209
210outStr = [NSHomeDirectory() stringByAppendingPathComponent: @"Desktop"];
211
212} while (0);
213
214
215err = IOMasterPort(MACH_PORT_NULL, &masterPort);
216
217if (err == KERN_SUCCESS){
218
219entry=IORegistryEntryFromPath(masterPort, [@"IODeviceTree:/efi" UTF8String]);
220
221if (entry!=MACH_PORT_NULL) {
222
223data = IORegistryEntrySearchCFProperty( entry, kIOServicePlane, CFSTR("motherboard-name"), allocator, kIORegistryIterateRecursively);
224
225if (data != NULL) {
226rawdata=CFDataGetBytePtr(data);
227
228int len = 0;
229
230char *ModelStr = ( char* )rawdata;
231
232if ((len = strlen(ModelStr)))
233{
234Model32 = OSSwapHostToBigInt32(adler32( (unsigned char *) ModelStr, len ));
235
236printf("Model32 0x%lx ModelStr %s\n",Model32,ModelStr);
237
238 NSMutableData * mem = [[NSMutableData alloc] initWithContentsOfFile : inStr ];
239if (!mem) {
240 NSLog(@"Error while opening the file : %@ \n",inStr);
241 CFRelease(data);
242 data = NULL;
243 goto out;
244 }
245ACPI_TABLE_HEADER * head = (ACPI_TABLE_HEADER *)[mem mutableBytes];
246
247head->OemRevision = Model32;
248
249SetChecksum(head);
250
251 if ([mem writeToFile : [NSString stringWithFormat:@"%@/%@.%@.aml",
252 outStr, filename, [NSString stringWithUTF8String:ModelStr]] atomically: YES] == NO) {
253
254 NSLog(@"Error: Can't save the file to %@\n",[NSString stringWithFormat:@"%@/%@.%@.aml",
255 outStr, filename, [NSString stringWithUTF8String:ModelStr]]);
256
257 }
258
259 [mem release];
260}
261 CFRelease(data);
262 data = NULL;
263 goto out;
264}
265
266}
267
268NSLog(@"No Model entry \n");
269
270entry=IORegistryEntryFromPath(masterPort, [@"IODeviceTree:/efi/platform" UTF8String]);
271
272if (entry!=MACH_PORT_NULL){
273
274
275data = IORegistryEntrySearchCFProperty( entry, kIOServicePlane, CFSTR("system-id"), allocator, kIORegistryIterateRecursively);
276
277if (data != NULL) {
278rawdata=CFDataGetBytePtr(data);
279
280
281const char *uuidStr = getStringFromUUID(( EFI_CHAR8* )rawdata);
282
283if (strlen(uuidStr) >= UUID_STR_LEN )
284{
285uuid32 = OSSwapHostToBigInt32(adler32( (unsigned char *) uuidStr, UUID_STR_LEN ));
286
287printf("uuid32 : 0x%lx\n",uuid32);
288printf("uuidStr : %s\n",uuidStr);
289
290NSMutableData * mem = [[NSMutableData alloc] initWithContentsOfFile : inStr ];
291if (!mem) {
292 NSLog(@"Error while opening the file : %@\n",inStr);
293 CFRelease(data);
294 data = NULL;
295 goto out;
296 }
297ACPI_TABLE_HEADER * head = (ACPI_TABLE_HEADER *)[mem mutableBytes];
298
299head->OemRevision = uuid32;
300
301SetChecksum(head);
302
303 if ([mem writeToFile : [NSString stringWithFormat:@"%@/%@.%@.aml",outStr,filename,[NSNumber numberWithUnsignedLong:uuid32]] atomically: YES] == NO) {
304
305 NSLog(@"Error: Can't save the file to %@ \n", [NSString stringWithFormat:@"%@/%@.%@.aml",outStr,filename,[NSNumber numberWithUnsignedLong:uuid32]]);
306
307 }
308 [mem release];
309}
310
311 CFRelease(data);
312 data = NULL;
313 goto out;
314
315}
316}
317NSLog(@"No UUID entry\n");
318
319}
320out:
321 if (chunks != nil) {
322 [chunks release];
323 }
324 if (entry != MACH_PORT_NULL) {
325 IOObjectRelease(entry);
326 }
327 [pool drain];
328 return 0;
329}
330

Archive Download this file

Revision: 1972