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