| 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 | ␉sprintf(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,entry2;␊ |
| 145 | ␉␊ |
| 146 | ␉kern_return_t err;␊ |
| 147 | ␉␊ |
| 148 | ␉CFDataRef data;␊ |
| 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;␉␊ |
| 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 | ␉␉entry2=IORegistryEntryFromPath(masterPort, [@"IODeviceTree:/efi" UTF8String]);␊ |
| 220 | ␊ |
| 221 | ␉␉if (entry2!=MACH_PORT_NULL) {␉␉␉␊ |
| 222 | ␉␉␉␊ |
| 223 | ␉␉␉data = IORegistryEntrySearchCFProperty( entry2, 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 | goto out;␊ |
| 242 | }␊ |
| 243 | ␉␉␉␉␉ACPI_TABLE_HEADER * head = (ACPI_TABLE_HEADER *)[mem mutableBytes];␊ |
| 244 | ␉␉␉␉␉␊ |
| 245 | ␉␉␉␉␉head->OemRevision = Model32;␊ |
| 246 | ␉␉␉␉␉␊ |
| 247 | ␉␉␉␉␉SetChecksum(head);␊ |
| 248 | ␉␉␉␉␉␊ |
| 249 | if ([mem writeToFile : [NSString stringWithFormat:@"%@/%@.%@.aml",␊ |
| 250 | outStr, filename, [NSString stringWithUTF8String:ModelStr]] atomically: YES] == NO) {␊ |
| 251 | ␊ |
| 252 | NSLog(@"Error: Can't save the file to %@\n",[NSString stringWithFormat:@"%@/%@.%@.aml",␊ |
| 253 | outStr, filename, [NSString stringWithUTF8String:ModelStr]]); ␊ |
| 254 | ␊ |
| 255 | }␊ |
| 256 | ␊ |
| 257 | [mem release];␉␉␉␉␉␊ |
| 258 | ␉␉␉␉}␉␉␉␉␊ |
| 259 | ␉␉␉␉␊ |
| 260 | ␉␉␉␉goto out;␊ |
| 261 | ␉␉␉}␉␉␉␊ |
| 262 | ␉␉␉␊ |
| 263 | ␉␉} ␉␉␊ |
| 264 | ␉␉␊ |
| 265 | ␉␉NSLog(@"No Model entry \n");␊ |
| 266 | ␊ |
| 267 | ␉␉entry=IORegistryEntryFromPath(masterPort, [@"IODeviceTree:/efi/platform" UTF8String]);␊ |
| 268 | ␊ |
| 269 | ␉␉if (entry!=MACH_PORT_NULL){␉␉␊ |
| 270 | ␉␉␉␊ |
| 271 | ␉␉␉␊ |
| 272 | ␉␉␉data = IORegistryEntrySearchCFProperty( entry, kIOServicePlane, CFSTR("system-id"), allocator, kIORegistryIterateRecursively);␊ |
| 273 | ␉␉␉␊ |
| 274 | ␉␉␉if (data != NULL) {␊ |
| 275 | ␉␉␉␉rawdata=CFDataGetBytePtr(data);␊ |
| 276 | ␉␉␉␉␊ |
| 277 | ␉␉␉␉␊ |
| 278 | ␉␉␉␉const char *uuidStr = getStringFromUUID(( EFI_CHAR8* )rawdata);␊ |
| 279 | ␉␉␉␉␊ |
| 280 | ␉␉␉␉if (strlen(uuidStr)) ␊ |
| 281 | ␉␉␉␉{␊ |
| 282 | ␉␉␉␉␉uuid32 = OSSwapHostToBigInt32(adler32( (unsigned char *) uuidStr, UUID_STR_LEN ));␊ |
| 283 | ␉␉␉␉␉␊ |
| 284 | ␉␉␉␉␉printf("uuid32 : 0x%lx\n",uuid32);␊ |
| 285 | ␉␉␉␉␉printf("uuidStr : %s\n",uuidStr);␊ |
| 286 | ␊ |
| 287 | ␉␉␉␉␉NSMutableData * mem = [[NSMutableData alloc] initWithContentsOfFile : inStr ];␊ |
| 288 | ␉␉␉␉␉if (!mem) {␊ |
| 289 | NSLog(@"Error while opening the file : %@\n",inStr);␊ |
| 290 | goto out;␊ |
| 291 | }␊ |
| 292 | ␉␉␉␉␉ACPI_TABLE_HEADER * head = (ACPI_TABLE_HEADER *)[mem mutableBytes];␊ |
| 293 | ␊ |
| 294 | ␉␉␉␉␉head->OemRevision = uuid32;␊ |
| 295 | ␉␉␉␉␉␊ |
| 296 | ␉␉␉␉␉SetChecksum(head);␊ |
| 297 | ␉␉␉␉␉␊ |
| 298 | if ([mem writeToFile : [NSString stringWithFormat:@"%@/%@.%@.aml",outStr,filename,[NSNumber numberWithUnsignedLong:uuid32]] atomically: YES] == NO) {␊ |
| 299 | ␊ |
| 300 | NSLog(@"Error: Can't save the file to %@ \n", [NSString stringWithFormat:@"%@/%@.%@.aml",outStr,filename,[NSNumber numberWithUnsignedLong:uuid32]]); ␊ |
| 301 | ␊ |
| 302 | }␊ |
| 303 | [mem release]; ␉␉␉␉␊ |
| 304 | ␉␉␉␉}␉␉␉␉␊ |
| 305 | ␉␉␉␉␊ |
| 306 | ␉␉␉␉goto out;␊ |
| 307 | ␊ |
| 308 | ␉␉␉}␉␉␉␊ |
| 309 | ␉␉}␊ |
| 310 | ␉␉NSLog(@"No UUID entry\n");␊ |
| 311 | ␊ |
| 312 | ␉}␊ |
| 313 | ␉out:␊ |
| 314 | if (chunks != NULL) {␊ |
| 315 | [chunks release];␊ |
| 316 | }␊ |
| 317 | if (data != NULL) {␊ |
| 318 | CFRelease(data);␊ |
| 319 | }␊ |
| 320 | if (entry != MACH_PORT_NULL) {␊ |
| 321 | IOObjectRelease(entry);␊ |
| 322 | }␊ |
| 323 | if (entry2 != MACH_PORT_NULL) {␊ |
| 324 | IOObjectRelease(entry2);␊ |
| 325 | }␊ |
| 326 | [pool drain];␊ |
| 327 | return 0;␊ |
| 328 | }␊ |
| 329 | |