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 <uuid/uuid.h>
57
58#include "Intel_Acpi/acpidecode.h"
59#include "Intel_Acpi/ppmsetup.h"
60
61unsigned long uuid32;
62#define UUID_LEN sizeof(uuid_t)
63#define UUID_STR_LEN sizeof(uuid_string_t)
64typedef int8_t EFI_CHAR8;
65
66unsigned long
67adler32( unsigned char * buffer, long length );
68U8 GetChecksum(void *mem_addr, U32 mem_size);
69void SetChecksum(struct acpi_table_header *header);
70const char * getStringFromUUID(const EFI_CHAR8* eUUID);
71
72
73unsigned long
74adler32( unsigned char * buffer, long length )
75{
76 long cnt;
77 unsigned long result, lowHalf, highHalf;
78
79 lowHalf = 1;
80 highHalf = 0;
81
82for ( cnt = 0; cnt < length; cnt++ )
83 {
84 if ((cnt % 5000) == 0)
85 {
86 lowHalf %= 65521L;
87 highHalf %= 65521L;
88 }
89
90 lowHalf += buffer[cnt];
91 highHalf += lowHalf;
92 }
93
94lowHalf %= 65521L;
95highHalf %= 65521L;
96
97result = (highHalf << 16) | lowHalf;
98
99return result;
100}
101
102//-------------------------------------------------------------------------------
103//
104// Procedure: GetChecksum - Performs byte checksum
105//
106//-------------------------------------------------------------------------------
107U8 GetChecksum(void *mem_addr, U32 mem_size)
108{
109 U8 *current = mem_addr;
110 U8 *end = current + mem_size;
111 U8 checksum = 0;
112
113 for (; current < end; current++)
114 checksum = checksum + *current;
115
116 return (checksum);
117}
118
119void SetChecksum(struct acpi_table_header *header)
120{
121 header->Checksum = 0;
122 header->Checksum = 0 - GetChecksum(header, header->Length);
123}
124
125
126/** Transform a 16 bytes hexadecimal value UUID to a string */
127/* getStringFromUUID : credit to Rekursor (see convert.c) */
128const char * getStringFromUUID(const EFI_CHAR8* eUUID)
129{
130static char msg[UUID_STR_LEN] = "";
131if (!eUUID) return "";
132const unsigned char * uuid = (unsigned char*) eUUID;
133snprintf(msg, sizeof(msg), "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
134 uuid[0], uuid[1], uuid[2], uuid[3],
135 uuid[4], uuid[5], uuid[6], uuid[7],
136 uuid[8], uuid[9], uuid[10],uuid[11],
137 uuid[12],uuid[13],uuid[14],uuid[15]);
138return msg ;
139}
140
141int main (int argc, const char * argv[]) {
142 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
143
144 mach_port_t masterPort;
145
146io_registry_entry_t entry = MACH_PORT_NULL;
147
148kern_return_t err;
149
150CFDataRef data = NULL;
151
152CFAllocatorRef allocator=kCFAllocatorDefault;
153
154const UInt8* rawdata = NULL;
155
156unsigned long uuid32 = 0,Model32 = 0 ;
157NSString *filename, *fullfilename ;
158NSArray *namechunks;
159NSMutableArray *chunks = nil;
160NSString *outStr, *inStr;
161
162if (argc == 2) {
163
164/* Creating output string */
165inStr = [NSString stringWithUTF8String:argv[1]];
166chunks = [[NSMutableArray alloc] init];
167[chunks addObjectsFromArray:[inStr componentsSeparatedByString: @"/"]];
168
169fullfilename = [chunks lastObject];
170namechunks = [fullfilename componentsSeparatedByString: @"."];
171filename = [namechunks objectAtIndex:0];
172
173[chunks removeLastObject];
174outStr = [chunks componentsJoinedByString: @"/"];
175
176} else if (argc == 3 ){
177inStr = [NSString stringWithUTF8String:argv[1]];
178BOOL isDirectory= FALSE;
179
180 chunks = [[NSMutableArray alloc] init];
181 [chunks addObjectsFromArray:[inStr componentsSeparatedByString: @"/"]];
182 fullfilename = [chunks lastObject];
183 namechunks = [fullfilename componentsSeparatedByString: @"."];
184 filename = [namechunks objectAtIndex:0];
185
186if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithUTF8String:argv[2]] isDirectory:&isDirectory] && isDirectory) {
187
188 outStr = [NSString stringWithUTF8String:argv[2]];
189
190}
191 else
192 {
193 [chunks removeLastObject];
194 outStr = [chunks componentsJoinedByString: @"/"];
195 }
196
197}
198 else
199 {
200printf("Usage: amlsgn input(as a file) output(as a directory)\n");
201goto out;
202 }
203
204do {
205BOOL isDirectory= FALSE;
206if ([[NSFileManager defaultManager] fileExistsAtPath:outStr isDirectory:&isDirectory] && isDirectory) {
207
208if ([[NSFileManager defaultManager] isWritableFileAtPath:outStr])
209break;
210}
211
212outStr = [NSHomeDirectory() stringByAppendingPathComponent: @"Desktop"];
213
214} while (0);
215
216
217err = IOMasterPort(MACH_PORT_NULL, &masterPort);
218
219if (err == KERN_SUCCESS){
220
221entry=IORegistryEntryFromPath(masterPort, [@"IODeviceTree:/efi" UTF8String]);
222
223if (entry!=MACH_PORT_NULL) {
224
225data = IORegistryEntrySearchCFProperty( entry, kIOServicePlane, CFSTR("motherboard-name"), allocator, kIORegistryIterateRecursively);
226
227if (data != NULL) {
228rawdata=CFDataGetBytePtr(data);
229
230int len = 0;
231
232char *ModelStr = ( char* )rawdata;
233
234if ((len = strlen(ModelStr)))
235{
236Model32 = OSSwapHostToBigInt32(adler32( (unsigned char *) ModelStr, len ));
237
238printf("Model32 0x%lx ModelStr %s\n",Model32,ModelStr);
239
240 NSMutableData * mem = [[NSMutableData alloc] initWithContentsOfFile : inStr ];
241if (!mem) {
242 NSLog(@"Error while opening the file : %@ \n",inStr);
243 CFRelease(data);
244 data = NULL;
245 goto out;
246 }
247ACPI_TABLE_HEADER * head = (ACPI_TABLE_HEADER *)[mem mutableBytes];
248
249head->OemRevision = Model32;
250
251SetChecksum(head);
252
253 if ([mem writeToFile : [NSString stringWithFormat:@"%@/%@.%@.aml",
254 outStr, filename, [NSString stringWithUTF8String:ModelStr]] atomically: YES] == NO) {
255
256 NSLog(@"Error: Can't save the file to %@\n",[NSString stringWithFormat:@"%@/%@.%@.aml",
257 outStr, filename, [NSString stringWithUTF8String:ModelStr]]);
258
259 }
260
261 [mem release];
262}
263 CFRelease(data);
264 data = NULL;
265 goto out;
266}
267
268}
269
270NSLog(@"No Model entry \n");
271
272entry=IORegistryEntryFromPath(masterPort, [@"IODeviceTree:/efi/platform" UTF8String]);
273
274if (entry!=MACH_PORT_NULL){
275
276
277data = IORegistryEntrySearchCFProperty( entry, kIOServicePlane, CFSTR("system-id"), allocator, kIORegistryIterateRecursively);
278
279if (data != NULL) {
280rawdata=CFDataGetBytePtr(data);
281
282
283const char *uuidStr = getStringFromUUID(( EFI_CHAR8* )rawdata);
284
285if (strlen(uuidStr) >= UUID_STR_LEN )
286{
287uuid32 = OSSwapHostToBigInt32(adler32( (unsigned char *) uuidStr, UUID_STR_LEN ));
288
289printf("uuid32 : 0x%lx\n",uuid32);
290printf("uuidStr : %s\n",uuidStr);
291
292NSMutableData * mem = [[NSMutableData alloc] initWithContentsOfFile : inStr ];
293if (!mem) {
294 NSLog(@"Error while opening the file : %@\n",inStr);
295 CFRelease(data);
296 data = NULL;
297 goto out;
298 }
299ACPI_TABLE_HEADER * head = (ACPI_TABLE_HEADER *)[mem mutableBytes];
300
301head->OemRevision = uuid32;
302
303SetChecksum(head);
304
305 if ([mem writeToFile : [NSString stringWithFormat:@"%@/%@.%@.aml",outStr,filename,[NSNumber numberWithUnsignedLong:uuid32]] atomically: YES] == NO) {
306
307 NSLog(@"Error: Can't save the file to %@ \n", [NSString stringWithFormat:@"%@/%@.%@.aml",outStr,filename,[NSNumber numberWithUnsignedLong:uuid32]]);
308
309 }
310 [mem release];
311}
312
313 CFRelease(data);
314 data = NULL;
315 goto out;
316
317}
318}
319NSLog(@"No UUID entry\n");
320
321}
322out:
323 if (chunks != nil) {
324 [chunks release];
325 }
326 if (entry != MACH_PORT_NULL) {
327 IOObjectRelease(entry);
328 }
329 [pool drain];
330 return 0;
331}
332

Archive Download this file

Revision: 2118