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 | ␊ |
59 | unsigned long uuid32;␊ |
60 | #define UUID_LEN␉16␊ |
61 | #define UUID_STR_LEN UUID_LEN*2 + 8␊ |
62 | typedef int8_t EFI_CHAR8;␊ |
63 | ␊ |
64 | unsigned long␊ |
65 | adler32( unsigned char * buffer, long length );␊ |
66 | U8 GetChecksum(void *mem_addr, U32 mem_size);␊ |
67 | void SetChecksum(struct acpi_table_header *header);␊ |
68 | const char * getStringFromUUID(const EFI_CHAR8* eUUID);␊ |
69 | ␊ |
70 | ␊ |
71 | unsigned long␊ |
72 | adler32( unsigned char * buffer, long length )␊ |
73 | {␊ |
74 | long cnt;␊ |
75 | unsigned long result, lowHalf, highHalf;␊ |
76 | ␊ |
77 | lowHalf = 1;␊ |
78 | highHalf = 0;␊ |
79 | ␉␊ |
80 | ␉for ( 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 | ␉␊ |
92 | ␉lowHalf %= 65521L;␊ |
93 | ␉highHalf %= 65521L;␊ |
94 | ␉␊ |
95 | ␉result = (highHalf << 16) | lowHalf;␊ |
96 | ␉␊ |
97 | ␉return result;␊ |
98 | }␊ |
99 | ␊ |
100 | //-------------------------------------------------------------------------------␊ |
101 | //␊ |
102 | // Procedure: GetChecksum - Performs byte checksum␊ |
103 | //␊ |
104 | //-------------------------------------------------------------------------------␊ |
105 | U8 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 | ␊ |
117 | void 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) */␊ |
126 | const char * getStringFromUUID(const EFI_CHAR8* eUUID)␊ |
127 | {␊ |
128 | ␉static char msg[UUID_STR_LEN] = "";␊ |
129 | ␉if (!eUUID) return "";␊ |
130 | ␉const unsigned char * uuid = (unsigned char*) eUUID;␊ |
131 | ␉snprintf(msg, sizeof(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]);␊ |
136 | ␉return msg ;␊ |
137 | }␊ |
138 | ␊ |
139 | int main (int argc, const char * argv[]) {␊ |
140 | NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];␊ |
141 | ␊ |
142 | mach_port_t masterPort;␊ |
143 | ␉␊ |
144 | ␉io_registry_entry_t entry = MACH_PORT_NULL;␊ |
145 | ␉␊ |
146 | ␉kern_return_t err;␊ |
147 | ␉␊ |
148 | ␉CFDataRef data = NULL;␊ |
149 | ␉␊ |
150 | ␉CFAllocatorRef allocator=kCFAllocatorDefault;␊ |
151 | ␉␊ |
152 | ␉const UInt8* rawdata = NULL;␊ |
153 | ␉␊ |
154 | ␉unsigned long uuid32 = 0,Model32 = 0 ;␊ |
155 | ␉NSString *filename, *fullfilename ;␊ |
156 | ␉NSArray *namechunks;␊ |
157 | ␉NSMutableArray *chunks = nil;␉␊ |
158 | ␉NSString *outStr, *inStr;␊ |
159 | ␊ |
160 | ␉if (argc == 2) {␉␊ |
161 | ␉␉␊ |
162 | ␉␉/* Creating output string */␊ |
163 | ␉␉inStr = [NSString stringWithUTF8String:argv[1]];␊ |
164 | ␉␉chunks = [[NSMutableArray alloc] init];␊ |
165 | ␉␉[chunks addObjectsFromArray:[inStr componentsSeparatedByString: @"/"]];␊ |
166 | ␉␉␊ |
167 | ␉␉fullfilename = [chunks lastObject];␊ |
168 | ␉␉namechunks = [fullfilename componentsSeparatedByString: @"."];␊ |
169 | ␉␉filename = [namechunks objectAtIndex:0];␊ |
170 | ␉␉␊ |
171 | ␉␉[chunks removeLastObject];␊ |
172 | ␉␉outStr = [chunks componentsJoinedByString: @"/"];␊ |
173 | ␊ |
174 | ␉} else if (argc == 3 ){␊ |
175 | ␉␉inStr = [NSString stringWithUTF8String:argv[1]];␊ |
176 | ␉␉BOOL 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 | ␊ |
184 | ␉␉if ([[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 | {␊ |
198 | ␉␉printf("Usage: amlsgn input(as a file) output(as a directory)\n");␊ |
199 | ␉␉goto out;␊ |
200 | } ␊ |
201 | ␉␊ |
202 | ␉do {␊ |
203 | ␉␉BOOL isDirectory= FALSE;␊ |
204 | ␉␉if ([[NSFileManager defaultManager] fileExistsAtPath:outStr isDirectory:&isDirectory] && isDirectory) {␊ |
205 | ␊ |
206 | ␉␉␉if ([[NSFileManager defaultManager] isWritableFileAtPath:outStr])␊ |
207 | ␉␉␉␉break;␊ |
208 | ␉␉}␊ |
209 | ␉␉␊ |
210 | ␉␉outStr = [NSHomeDirectory() stringByAppendingPathComponent: @"Desktop"];␊ |
211 | ␊ |
212 | ␉} while (0);␊ |
213 | ␉␊ |
214 | ␉␊ |
215 | ␉err = IOMasterPort(MACH_PORT_NULL, &masterPort);␊ |
216 | ␉␊ |
217 | ␉if (err == KERN_SUCCESS){␉␉␊ |
218 | ␉␉␊ |
219 | ␉␉entry=IORegistryEntryFromPath(masterPort, [@"IODeviceTree:/efi" UTF8String]);␊ |
220 | ␊ |
221 | ␉␉if (entry!=MACH_PORT_NULL) {␉␉␉␊ |
222 | ␉␉␉␊ |
223 | ␉␉␉data = IORegistryEntrySearchCFProperty( entry, kIOServicePlane, CFSTR("motherboard-name"), allocator, kIORegistryIterateRecursively);␊ |
224 | ␉␉␉␊ |
225 | ␉␉␉if (data != NULL) {␊ |
226 | ␉␉␉␉rawdata=CFDataGetBytePtr(data);␊ |
227 | ␉␉␉␉␊ |
228 | ␉␉␉␉int len = 0;␊ |
229 | ␉␉␉␉␊ |
230 | ␉␉␉␉char *ModelStr = ( char* )rawdata;␊ |
231 | ␉␉␉␉␊ |
232 | ␉␉␉␉if ((len = strlen(ModelStr))) ␊ |
233 | ␉␉␉␉{␊ |
234 | ␉␉␉␉␉Model32 = OSSwapHostToBigInt32(adler32( (unsigned char *) ModelStr, len ));␊ |
235 | ␉␉␉␉␉␊ |
236 | ␉␉␉␉␉printf("Model32 0x%lx ModelStr %s\n",Model32,ModelStr);␊ |
237 | ␉␉␉␉␉␊ |
238 | NSMutableData * mem = [[NSMutableData alloc] initWithContentsOfFile : inStr ];␊ |
239 | ␉␉␉␉␉if (!mem) {␊ |
240 | NSLog(@"Error while opening the file : %@ \n",inStr);␊ |
241 | CFRelease(data);␊ |
242 | data = NULL;␊ |
243 | goto out;␊ |
244 | }␊ |
245 | ␉␉␉␉␉ACPI_TABLE_HEADER * head = (ACPI_TABLE_HEADER *)[mem mutableBytes];␊ |
246 | ␉␉␉␉␉␊ |
247 | ␉␉␉␉␉head->OemRevision = Model32;␊ |
248 | ␉␉␉␉␉␊ |
249 | ␉␉␉␉␉SetChecksum(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 | ␉␉␊ |
268 | ␉␉NSLog(@"No Model entry \n");␊ |
269 | ␊ |
270 | ␉␉entry=IORegistryEntryFromPath(masterPort, [@"IODeviceTree:/efi/platform" UTF8String]);␊ |
271 | ␊ |
272 | ␉␉if (entry!=MACH_PORT_NULL){␉␉␊ |
273 | ␉␉␉␊ |
274 | ␉␉␉␊ |
275 | ␉␉␉data = IORegistryEntrySearchCFProperty( entry, kIOServicePlane, CFSTR("system-id"), allocator, kIORegistryIterateRecursively);␊ |
276 | ␉␉␉␊ |
277 | ␉␉␉if (data != NULL) {␊ |
278 | ␉␉␉␉rawdata=CFDataGetBytePtr(data);␊ |
279 | ␉␉␉␉␊ |
280 | ␉␉␉␉␊ |
281 | ␉␉␉␉const char *uuidStr = getStringFromUUID(( EFI_CHAR8* )rawdata);␊ |
282 | ␉␉␉␉␊ |
283 | ␉␉␉␉if (strlen(uuidStr) >= UUID_STR_LEN ) ␊ |
284 | ␉␉␉␉{␊ |
285 | ␉␉␉␉␉uuid32 = OSSwapHostToBigInt32(adler32( (unsigned char *) uuidStr, UUID_STR_LEN ));␊ |
286 | ␉␉␉␉␉␊ |
287 | ␉␉␉␉␉printf("uuid32 : 0x%lx\n",uuid32);␊ |
288 | ␉␉␉␉␉printf("uuidStr : %s\n",uuidStr);␊ |
289 | ␊ |
290 | ␉␉␉␉␉NSMutableData * mem = [[NSMutableData alloc] initWithContentsOfFile : inStr ];␊ |
291 | ␉␉␉␉␉if (!mem) {␊ |
292 | NSLog(@"Error while opening the file : %@\n",inStr);␊ |
293 | CFRelease(data);␊ |
294 | data = NULL;␊ |
295 | goto out;␊ |
296 | }␊ |
297 | ␉␉␉␉␉ACPI_TABLE_HEADER * head = (ACPI_TABLE_HEADER *)[mem mutableBytes];␊ |
298 | ␊ |
299 | ␉␉␉␉␉head->OemRevision = uuid32;␊ |
300 | ␉␉␉␉␉␊ |
301 | ␉␉␉␉␉SetChecksum(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 | ␉␉}␊ |
317 | ␉␉NSLog(@"No UUID entry\n");␊ |
318 | ␊ |
319 | ␉}␊ |
320 | ␉out:␊ |
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 | |