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,entry2;
145
146kern_return_t err;
147
148CFDataRef data;
149
150CFAllocatorRef allocator=kCFAllocatorDefault;
151
152const UInt8* rawdata = NULL;
153
154UInt32 uuid32 = 0;
155UInt32 Model32 = 0;
156const char *cfilename ;
157NSString *filename, *fullfilename ;
158NSArray *namechunks;
159NSMutableArray *chunks;
160char * input, *output, dirspec[512];
161NSString *outStr, *inStr;
162
163if (argc > 3 || argc < 2) {
164printf("Usage: amlsgn input(as a file) output(as a directory)\n");
165return 1;
166 } else if (argc == 2) {
167
168/* Creating output string */
169inStr = [NSString stringWithUTF8String:argv[1]];
170chunks = [[NSMutableArray alloc] init];
171[chunks addObjectsFromArray:[inStr componentsSeparatedByString: @"/"]];
172
173fullfilename = [chunks lastObject];
174namechunks = [fullfilename componentsSeparatedByString: @"."];
175filename = [namechunks objectAtIndex:0];
176cfilename = [filename UTF8String];
177
178[chunks removeLastObject];
179outStr = [chunks componentsJoinedByString: @"/"];
180
181} else {
182inStr = [NSString stringWithUTF8String:argv[1]];
183BOOL isDirectory= FALSE;
184if ([[NSFileManager defaultManager] fileExistsAtPath:outStr isDirectory:&isDirectory]) {
185
186if (isDirectory == FALSE) {
187chunks = [[NSMutableArray alloc] init];
188[chunks addObjectsFromArray:[inStr componentsSeparatedByString: @"/"]];
189
190[chunks removeLastObject];
191outStr = [chunks componentsJoinedByString: @"/"];
192} else {
193outStr = [NSString stringWithUTF8String:argv[2]];
194
195}
196}
197
198}
199
200do {
201BOOL isDirectory= FALSE;
202if ([[NSFileManager defaultManager] fileExistsAtPath:outStr isDirectory:&isDirectory] && isDirectory) {
203
204if ([[NSFileManager defaultManager] isWritableFileAtPath:outStr])
205break;
206}
207
208outStr = [NSHomeDirectory() stringByAppendingPathComponent: @"Desktop"];
209
210} while (0);
211
212
213err = IOMasterPort(MACH_PORT_NULL, &masterPort);
214
215if (err == KERN_SUCCESS){
216
217entry2=IORegistryEntryFromPath(masterPort, [@"IODeviceTree:/efi" UTF8String]);
218
219if (entry2!=MACH_PORT_NULL) {
220
221data = IORegistryEntrySearchCFProperty( entry2, kIOServicePlane, CFSTR("motherboard-name"), allocator, kIORegistryIterateRecursively);
222
223if (data != NULL) {
224rawdata=CFDataGetBytePtr(data);
225
226int len = 0;
227
228char *ModelStr = ( char* )rawdata;
229
230if (len = strlen(ModelStr))
231{
232Model32 = OSSwapHostToBigInt32(adler32( (unsigned char *) ModelStr, len ));
233
234printf("uuid32 0x%lu uuidStr %s\n",(unsigned long)Model32,ModelStr);
235
236UInt8 mem[1024*1024];
237
238bzero(mem, sizeof(mem));
239
240int fdIn = open([inStr UTF8String], O_RDONLY, 0666);
241
242if (-1 == fdIn) {
243printf("Error while opening the file \n");
244return 1;
245}
246
247size_t nbRead = read(fdIn, (void*)mem, sizeof(mem));
248if (nbRead <= 0) {
249printf("Error: Unable to read the file\n");
250close(fdIn);
251return 1;
252}
253
254ACPI_TABLE_HEADER * head = (ACPI_TABLE_HEADER *)mem;
255
256close(fdIn);
257
258head->OemRevision = Model32;
259
260SetChecksum(head);
261
262{
263char dir[1024];
264
265sprintf(dir,"%s/%s.%s.aml",[outStr UTF8String],cfilename,ModelStr);
266
267int fdOut = open(dir, O_WRONLY | O_CREAT, 0666);
268
269if (-1 == fdOut) {
270printf("Error: Can't save the file\n");
271return 1;
272}
273write(fdOut, mem, head->Length);
274
275close(fdOut);
276}
277}
278
279IOObjectRelease(entry2);
280
281goto out;
282}
283
284}
285
286printf("No Model entry \n");
287
288entry=IORegistryEntryFromPath(masterPort, [@"IODeviceTree:/efi/platform" UTF8String]);
289
290if (entry!=MACH_PORT_NULL){
291
292
293data = IORegistryEntrySearchCFProperty( entry, kIOServicePlane, CFSTR("system-id"), allocator, kIORegistryIterateRecursively);
294
295if (data != NULL) {
296rawdata=CFDataGetBytePtr(data);
297
298
299const char *uuidStr = getStringFromUUID(( EFI_CHAR8* )rawdata);
300
301if (strlen(uuidStr))
302{
303uuid32 = OSSwapHostToBigInt32(adler32( (unsigned char *) uuidStr, UUID_STR_LEN ));
304
305printf("uuid32 : 0x%lu\n",(unsigned long)uuid32);
306printf("uuidStr : %s\n",uuidStr);
307
308UInt8 mem[1024*1024];
309
310bzero(mem, sizeof(mem));
311
312int fdIn = open([inStr UTF8String], O_RDONLY, 0666);
313
314if (-1 == fdIn) {
315printf("Error while opening the file \n");
316return 1;
317}
318
319size_t nbRead = read(fdIn, (void*)mem, sizeof(mem));
320if (nbRead <= 0) {
321printf("Error: Unable to read the file\n");
322close(fdIn);
323return 1;
324}
325
326ACPI_TABLE_HEADER * head = (ACPI_TABLE_HEADER *)mem;
327
328close(fdIn);
329
330head->OemRevision = uuid32;
331
332SetChecksum(head);
333
334{
335char dir[1024];
336
337sprintf(dir,"%s/%s.%lu.aml",[outStr UTF8String],cfilename,(unsigned long)uuid32);
338
339int fdOut = open(dir, O_WRONLY | O_CREAT, 0666);
340
341if (-1 == fdOut) {
342printf("Error: Can't save the file\n");
343return 1;
344}
345write(fdOut, mem, head->Length);
346
347close(fdOut);
348}
349}
350
351IOObjectRelease(entry);
352
353goto out;
354
355}
356}
357printf("No UUID entry\n");
358
359}
360out:
361 [pool drain];
362 return 0;
363}
364

Archive Download this file

Revision: 1735