Subversion Repositories Kolibri OS

Rev

Rev 1498 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1498 serge 1
 
2
 *
3
 * Module Name: acpixtract - convert ascii ACPI tables to binary
4
 *
5
 *****************************************************************************/
6
7
 
8
 *
9
 * 1. Copyright Notice
10
 *
11
 * Some or all of this work - Copyright (c) 1999 - 2011, Intel Corp.
2216 Serge 12
 * All rights reserved.
1498 serge 13
 *
14
 * 2. License
15
 *
16
 * 2.1. This is your license from Intel Corp. under its intellectual property
17
 * rights.  You may have additional license terms from the party that provided
18
 * you this software, covering your right to use that party's intellectual
19
 * property rights.
20
 *
21
 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22
 * copy of the source code appearing in this file ("Covered Code") an
23
 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24
 * base code distributed originally by Intel ("Original Intel Code") to copy,
25
 * make derivatives, distribute, use and display any portion of the Covered
26
 * Code in any form, with the right to sublicense such rights; and
27
 *
28
 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29
 * license (with the right to sublicense), under only those claims of Intel
30
 * patents that are infringed by the Original Intel Code, to make, use, sell,
31
 * offer to sell, and import the Covered Code and derivative works thereof
32
 * solely to the minimum extent necessary to exercise the above copyright
33
 * license, and in no event shall the patent license extend to any additions
34
 * to or modifications of the Original Intel Code.  No other license or right
35
 * is granted directly or by implication, estoppel or otherwise;
36
 *
37
 * The above copyright and patent license is granted only if the following
38
 * conditions are met:
39
 *
40
 * 3. Conditions
41
 *
42
 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43
 * Redistribution of source code of any substantial portion of the Covered
44
 * Code or modification with rights to further distribute source must include
45
 * the above Copyright Notice, the above License, this list of Conditions,
46
 * and the following Disclaimer and Export Compliance provision.  In addition,
47
 * Licensee must cause all Covered Code to which Licensee contributes to
48
 * contain a file documenting the changes Licensee made to create that Covered
49
 * Code and the date of any change.  Licensee must include in that file the
50
 * documentation of any changes made by any predecessor Licensee.  Licensee
51
 * must include a prominent statement that the modification is derived,
52
 * directly or indirectly, from Original Intel Code.
53
 *
54
 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55
 * Redistribution of source code of any substantial portion of the Covered
56
 * Code or modification without rights to further distribute source must
57
 * include the following Disclaimer and Export Compliance provision in the
58
 * documentation and/or other materials provided with distribution.  In
59
 * addition, Licensee may not authorize further sublicense of source of any
60
 * portion of the Covered Code, and must include terms to the effect that the
61
 * license from Licensee to its licensee is limited to the intellectual
62
 * property embodied in the software Licensee provides to its licensee, and
63
 * not to intellectual property embodied in modifications its licensee may
64
 * make.
65
 *
66
 * 3.3. Redistribution of Executable. Redistribution in executable form of any
67
 * substantial portion of the Covered Code or modification must reproduce the
68
 * above Copyright Notice, and the following Disclaimer and Export Compliance
69
 * provision in the documentation and/or other materials provided with the
70
 * distribution.
71
 *
72
 * 3.4. Intel retains all right, title, and interest in and to the Original
73
 * Intel Code.
74
 *
75
 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76
 * Intel shall be used in advertising or otherwise to promote the sale, use or
77
 * other dealings in products derived from or relating to the Covered Code
78
 * without prior written authorization from Intel.
79
 *
80
 * 4. Disclaimer and Export Compliance
81
 *
82
 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83
 * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84
 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
85
 * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
86
 * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
87
 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88
 * PARTICULAR PURPOSE.
89
 *
90
 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91
 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92
 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93
 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94
 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95
 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
96
 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97
 * LIMITED REMEDY.
98
 *
99
 * 4.3. Licensee shall not export, either directly or indirectly, any of this
100
 * software or system incorporating such software without first obtaining any
101
 * required license or other approval from the U. S. Department of Commerce or
102
 * any other agency or department of the United States Government.  In the
103
 * event Licensee exports any such software from the United States or
104
 * re-exports any such software from a foreign destination, Licensee shall
105
 * ensure that the distribution and export/re-export of the software is in
106
 * compliance with all laws, regulations, orders, or other restrictions of the
107
 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108
 * any of its subsidiaries will export/re-export any technical data, process,
109
 * software, or service, directly or indirectly, to any country for which the
110
 * United States government or any agency thereof requires an export license,
111
 * other governmental approval, or letter of assurance, without first obtaining
112
 * such license, approval or letter.
113
 *
114
 *****************************************************************************/
115
116
 
117
#include 
118
#include 
119
#include 
120
121
 
122
 
123
124
 
2216 Serge 125
#define FIND_HEADER         0
1498 serge 126
#define EXTRACT_DATA        1
127
#define BUFFER_SIZE         256
128
#define MIN_HEADER_LENGTH   6   /* strlen ("DSDT @") */
2216 Serge 129
1498 serge 130
 
131
132
 
2216 Serge 133
CheckAscii (
1498 serge 134
    char                    *Name,
135
    int                     Count);
136
137
 
2216 Serge 138
NormalizeSignature (
1498 serge 139
    char                    *Signature);
140
141
 
2216 Serge 142
GetNextInstance (
1498 serge 143
    char                    *InputPathname,
144
    char                    *Signature);
145
146
 
2216 Serge 147
ExtractTables (
1498 serge 148
    char                    *InputPathname,
149
    char                    *Signature,
150
    unsigned int            MinimumInstances);
151
152
 
2216 Serge 153
GetTableHeader (
1498 serge 154
    FILE                    *InputFile,
155
    unsigned char           *OutputData);
156
157
 
2216 Serge 158
CountTableInstances (
1498 serge 159
    char                    *InputPathname,
160
    char                    *Signature);
161
162
 
2216 Serge 163
ListTables (
1498 serge 164
    char                    *InputPathname);
165
166
 
2216 Serge 167
ConvertLine (
1498 serge 168
    char                    *InputLine,
169
    unsigned char           *OutputData);
170
171
 
2216 Serge 172
DisplayUsage (
1498 serge 173
    void);
174
175
 
176
 
177
{
178
    char                    Signature[4];
179
    int                     Length;
180
    unsigned char           Revision;
181
    unsigned char           Checksum;
182
    char                    OemId[6];
183
    char                    OemTableId[8];
184
    int                     OemRevision;
185
    char                    AslCompilerId[4];
186
    int                     AslCompilerRevision;
187
188
 
189
190
 
191
{
192
    unsigned int            Signature;
193
    unsigned int            Instances;
194
    unsigned int            NextInstance;
195
    struct TableInfo        *Next;
196
};
197
198
 
2216 Serge 199
static char                 Filename[16];
200
static unsigned char        Data[16];
201
1498 serge 202
 
203
 
204
 *
205
 * FUNCTION:    DisplayUsage
206
 *
207
 * DESCRIPTION: Usage message
208
 *
209
 ******************************************************************************/
210
211
 
2216 Serge 212
DisplayUsage (
1498 serge 213
    void)
214
{
215
216
 
217
    printf ("\nExtract binary ACPI tables from text acpidump output\n");
218
    printf ("Default invocation extracts all DSDTs and SSDTs\n");
219
    printf ("Version %8.8X\n\n", VERSION);
220
    printf ("Options:\n");
221
    printf (" -a                    Extract all tables, not just DSDT/SSDT\n");
222
    printf (" -l                    List table summaries, do not extract\n");
223
    printf (" -s         Extract all tables named \n");
224
    printf ("\n");
225
}
226
227
 
228
 
229
 *
230
 * FUNCTION:    CheckAscii
231
 *
232
 * PARAMETERS:  Name                - Ascii string, at least as long as Count
233
 *              Count               - Number of characters to check
234
 *
235
 * RETURN:      None
236
 *
237
 * DESCRIPTION: Ensure that the requested number of characters are printable
238
 *              Ascii characters. Sets non-printable and null chars to .
239
 *
240
 ******************************************************************************/
241
242
 
2216 Serge 243
CheckAscii (
1498 serge 244
    char                    *Name,
245
    int                     Count)
246
{
247
    int                     i;
248
249
 
250
 
251
    {
252
        if (!Name[i] || !isprint ((int) Name[i]))
253
        {
254
            Name[i] = ' ';
255
        }
256
    }
257
}
258
259
 
260
 
261
 *
262
 * FUNCTION:    NormalizeSignature
263
 *
264
 * PARAMETERS:  Name                - Ascii string containing an ACPI signature
265
 *
266
 * RETURN:      None
267
 *
268
 * DESCRIPTION: Change "RSD PTR" to "RSDP"
269
 *
270
 ******************************************************************************/
271
272
 
2216 Serge 273
NormalizeSignature (
1498 serge 274
    char                    *Signature)
275
{
276
277
 
278
    {
279
        Signature[3] = 'P';
280
    }
281
}
282
283
 
284
 
285
 *
286
 * FUNCTION:    ConvertLine
287
 *
288
 * PARAMETERS:  InputLine           - One line from the input acpidump file
289
 *              OutputData          - Where the converted data is returned
290
 *
291
 * RETURN:      The number of bytes actually converted
292
 *
293
 * DESCRIPTION: Convert one line of ascii text binary (up to 16 bytes)
294
 *
295
 ******************************************************************************/
296
297
 
2216 Serge 298
ConvertLine (
1498 serge 299
    char                    *InputLine,
300
    unsigned char           *OutputData)
301
{
302
    char                    *End;
303
    int                     BytesConverted;
304
    int                     Converted[16];
305
    int                     i;
306
307
 
308
 
309
310
 
311
    if (!End)
312
    {
313
        return (0); /* Don't understand the format */
314
    }
315
    *End = 0;
316
317
 
318
     * Convert one line of table data, of the form:
319
     * :   
320
     *
321
     * Example:
322
     * 02C0: 5F 53 42 5F 4C 4E 4B 44 00 12 13 04 0C FF FF 08  _SB_LNKD........
323
     */
324
    BytesConverted = sscanf (InputLine,
325
        "%*s %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
326
        &Converted[0],  &Converted[1],  &Converted[2],  &Converted[3],
327
        &Converted[4],  &Converted[5],  &Converted[6],  &Converted[7],
328
        &Converted[8],  &Converted[9],  &Converted[10], &Converted[11],
329
        &Converted[12], &Converted[13], &Converted[14], &Converted[15]);
330
331
 
332
333
 
334
    {
335
        OutputData[i] = (unsigned char) Converted[i];
336
    }
337
338
 
339
}
340
341
 
342
 
343
 *
344
 * FUNCTION:    GetTableHeader
345
 *
346
 * PARAMETERS:  InputFile           - Handle for the input acpidump file
347
 *              OutputData          - Where the table header is returned
348
 *
349
 * RETURN:      The actual number of bytes converted
350
 *
351
 * DESCRIPTION: Extract and convert an ACPI table header
352
 *
353
 ******************************************************************************/
354
355
 
2216 Serge 356
GetTableHeader (
1498 serge 357
    FILE                    *InputFile,
358
    unsigned char           *OutputData)
359
{
360
    size_t                  BytesConverted;
361
    size_t                  TotalConverted = 0;
362
    char                    Buffer[BUFFER_SIZE];
363
    int                     i;
364
365
 
366
 
367
368
 
369
    {
370
        if (!fgets (Buffer, BUFFER_SIZE, InputFile))
371
        {
372
            return (TotalConverted);
373
        }
374
375
 
376
        TotalConverted += BytesConverted;
377
        OutputData += 16;
378
379
 
380
        {
381
            return (TotalConverted);
382
        }
383
    }
384
385
 
386
}
387
388
 
389
 
390
 *
391
 * FUNCTION:    CountTableInstances
392
 *
393
 * PARAMETERS:  InputPathname       - Filename for acpidump file
394
 *              Signature           - Requested signature to count
395
 *
396
 * RETURN:      The number of instances of the signature
397
 *
398
 * DESCRIPTION: Count the instances of tables with the given signature within
399
 *              the input acpidump file.
400
 *
401
 ******************************************************************************/
402
403
 
2216 Serge 404
CountTableInstances (
1498 serge 405
    char                    *InputPathname,
406
    char                    *Signature)
407
{
408
    char                    Buffer[BUFFER_SIZE];
409
    FILE                    *InputFile;
410
    unsigned int            Instances = 0;
411
412
 
413
 
414
    if (!InputFile)
415
    {
416
        printf ("Could not open %s\n", InputPathname);
417
        return (0);
418
    }
419
420
 
421
422
 
423
    {
424
        /* Ignore empty lines and lines that start with a space */
425
426
 
427
            (Buffer[0] == '\n'))
428
        {
429
            continue;
430
        }
431
432
 
433
        if (!strncmp (Buffer, Signature, 4))
434
        {
435
            Instances++;
436
        }
437
    }
438
439
 
440
    return (Instances);
441
}
442
443
 
444
 
445
 *
446
 * FUNCTION:    GetNextInstance
447
 *
448
 * PARAMETERS:  InputPathname       - Filename for acpidump file
449
 *              Signature           - Requested ACPI signature
450
 *
451
 * RETURN:      The next instance number for this signature. Zero if this
452
 *              is the first instance of this signature.
453
 *
454
 * DESCRIPTION: Get the next instance number of the specified table. If this
455
 *              is the first instance of the table, create a new instance
456
 *              block. Note: only SSDT and PSDT tables can have multiple
457
 *              instances.
458
 *
459
 ******************************************************************************/
460
461
 
2216 Serge 462
GetNextInstance (
1498 serge 463
    char                    *InputPathname,
464
    char                    *Signature)
465
{
466
    struct TableInfo        *Info;
467
468
 
469
 
470
    while (Info)
471
    {
472
        if (*(unsigned int *) Signature == Info->Signature)
473
        {
474
            break;
475
        }
476
477
 
478
    }
479
480
 
481
    {
482
        /* Signature not found, create new table info block */
483
484
 
485
        if (!Info)
486
        {
487
            printf ("Could not allocate memory\n");
488
            exit (0);
489
        }
490
491
 
492
        Info->Instances = CountTableInstances (InputPathname, Signature);
493
        Info->NextInstance = 1;
494
        Info->Next = ListHead;
495
        ListHead = Info;
496
    }
497
498
 
499
    {
500
        return (Info->NextInstance++);
501
    }
502
503
 
504
}
505
506
 
507
 
508
 *
509
 * FUNCTION:    ExtractTables
510
 *
511
 * PARAMETERS:  InputPathname       - Filename for acpidump file
512
 *              Signature           - Requested ACPI signature to extract.
513
 *                                    NULL means extract ALL tables.
514
 *              MinimumInstances    - Min instances that are acceptable
515
 *
516
 * RETURN:      Status
517
 *
518
 * DESCRIPTION: Convert text ACPI tables to binary
519
 *
520
 ******************************************************************************/
521
522
 
2216 Serge 523
ExtractTables (
1498 serge 524
    char                    *InputPathname,
525
    char                    *Signature,
526
    unsigned int            MinimumInstances)
527
{
528
    char                    Buffer[BUFFER_SIZE];
529
    FILE                    *InputFile;
530
    FILE                    *OutputFile = NULL;
531
    size_t                  BytesWritten;
532
    size_t                  TotalBytesWritten = 0;
533
    size_t                  BytesConverted;
534
    unsigned int            State = FIND_HEADER;
535
    unsigned int            FoundTable = 0;
536
    unsigned int            Instances = 0;
537
    unsigned int            ThisInstance;
538
    char                    ThisSignature[4];
539
    int                     Status = 0;
540
541
 
542
 
543
544
 
545
    if (!InputFile)
546
    {
547
        printf ("Could not open %s\n", InputPathname);
548
        return (-1);
549
    }
550
551
 
552
    {
553
        /* Are there enough instances of the table to continue? */
554
555
 
556
557
 
558
        if (Instances < MinimumInstances)
559
        {
560
            printf ("Table %s was not found in %s\n", Signature, InputPathname);
561
            Status = -1;
562
            goto CleanupAndExit;
563
        }
564
565
 
566
        {
567
            goto CleanupAndExit;
568
        }
569
    }
570
571
 
572
573
 
574
    {
575
        switch (State)
576
        {
577
        case FIND_HEADER:
578
579
 
2216 Serge 580
581
 
582
            {
583
                continue;
584
            }
585
586
 
1498 serge 587
588
 
589
                (Buffer[0] == '\n'))
590
            {
591
                continue;
592
            }
593
594
 
2216 Serge 595
             * Ignore lines that are not of the form  @ . Examples:
596
             *
597
             * DSDT @ 0x737e4000
598
             * XSDT @ 0x737f2fff
599
             * RSD PTR @ 0xf6cd0
600
             * SSDT @ (nil)
601
             */
602
            if (!strstr (Buffer, " @ "))
603
            {
604
                continue;
605
            }
606
607
 
1498 serge 608
            strncpy (ThisSignature, Buffer, 4);
609
610
 
611
            {
612
                /* Ignore signatures that don't match */
613
614
 
615
                {
616
                    continue;
617
                }
618
            }
619
620
 
621
             * Get the instance number for this signature. Only the
622
             * SSDT and PSDT tables can have multiple instances.
623
             */
624
            ThisInstance = GetNextInstance (InputPathname, ThisSignature);
625
626
 
627
628
 
629
            {
630
                sprintf (Filename, "%4.4s%u.dat", ThisSignature, ThisInstance);
631
            }
632
            else
633
            {
634
                sprintf (Filename, "%4.4s.dat", ThisSignature);
635
            }
636
637
 
638
            if (!OutputFile)
639
            {
640
                printf ("Could not open %s\n", Filename);
641
                Status = -1;
642
                goto CleanupAndExit;
643
            }
644
645
 
646
            TotalBytesWritten = 0;
647
            FoundTable = 1;
648
            continue;
649
650
 
651
652
 
653
654
 
655
                (Buffer[0] != ' '))
656
            {
657
                fclose (OutputFile);
658
                OutputFile = NULL;
659
                State = FIND_HEADER;
660
661
 
2216 Serge 662
                    ThisSignature, (unsigned int) TotalBytesWritten, Filename);
663
                continue;
1498 serge 664
            }
665
666
 
667
668
 
669
670
 
671
672
 
673
            if (BytesWritten != BytesConverted)
674
            {
675
                printf ("Write error on %s\n", Filename);
676
                fclose (OutputFile);
677
                OutputFile = NULL;
678
                Status = -1;
679
                goto CleanupAndExit;
680
            }
681
682
 
683
            continue;
684
685
 
686
            Status = -1;
687
            goto CleanupAndExit;
688
        }
689
    }
690
691
 
692
    {
693
        printf ("Table %s was not found in %s\n", Signature, InputPathname);
694
    }
695
696
 
697
 
698
699
 
700
    {
701
        fclose (OutputFile);
702
        if (State == EXTRACT_DATA)
703
        {
704
            /* Received an EOF while extracting data */
705
706
 
2216 Serge 707
                ThisSignature, (unsigned int) TotalBytesWritten, Filename);
708
        }
1498 serge 709
    }
710
711
 
712
    return (Status);
713
}
714
715
 
716
 
717
 *
718
 * FUNCTION:    ListTables
719
 *
720
 * PARAMETERS:  InputPathname       - Filename for acpidump file
721
 *
722
 * RETURN:      Status
723
 *
724
 * DESCRIPTION: Display info for all ACPI tables found in input. Does not
725
 *              perform an actual extraction of the tables.
726
 *
727
 ******************************************************************************/
728
729
 
2216 Serge 730
ListTables (
1498 serge 731
    char                    *InputPathname)
732
{
733
    FILE                    *InputFile;
734
    char                    Buffer[BUFFER_SIZE];
735
    size_t                  HeaderSize;
736
    unsigned char           Header[48];
737
    int                     TableCount = 0;
738
    ACPI_TABLE_HEADER       *TableHeader = (ACPI_TABLE_HEADER *) (void *) Header;
739
740
 
741
 
742
743
 
744
    if (!InputFile)
745
    {
746
        printf ("Could not open %s\n", InputPathname);
747
        return (-1);
748
    }
749
750
 
751
752
 
753
            "   OemRevision CompilerId CompilerRevision\n\n");
754
755
 
756
    {
757
        /* Ignore empty lines and lines that start with a space */
758
759
 
760
            (Buffer[0] == '\n'))
761
        {
762
            continue;
763
        }
764
765
 
766
767
 
768
        if (HeaderSize < 16)
769
        {
770
            continue;
771
        }
772
773
 
774
775
 
776
        {
777
            CheckAscii ((char *) &Header[9], 6);
778
            printf ("%8.4s                   \"%6.6s\"\n", "RSDP", &Header[9]);
779
            TableCount++;
780
            continue;
781
        }
782
783
 
784
785
 
786
        {
787
            continue;
788
        }
789
790
 
791
792
 
793
        printf ("%8.4s % 7d", TableHeader->Signature, TableHeader->Length);
794
795
 
796
797
 
798
        {
799
            printf ("\n");
800
            continue;
801
        }
802
803
 
804
805
 
806
        CheckAscii (TableHeader->OemTableId, 8);
807
        CheckAscii (TableHeader->AslCompilerId, 4);
808
809
 
810
            TableHeader->Revision, TableHeader->OemId,
811
            TableHeader->OemTableId, TableHeader->OemRevision,
812
            TableHeader->AslCompilerId, TableHeader->AslCompilerRevision);
813
    }
814
815
 
816
    fclose (InputFile);
817
    return (0);
818
}
819
820
 
821
 
822
 *
823
 * FUNCTION:    main
824
 *
825
 * DESCRIPTION: C main function
826
 *
827
 ******************************************************************************/
828
829
 
830
main (
831
    int                     argc,
832
    char                    *argv[])
833
{
834
    int                     Status;
835
836
 
837
 
838
    {
839
        DisplayUsage ();
840
        return (0);
841
    }
842
843
 
844
    {
845
        if (argc < 3)
846
        {
847
            DisplayUsage ();
848
            return (0);
849
        }
850
851
 
852
        {
853
        case 'a':
854
855
 
856
857
 
858
859
 
860
861
 
862
863
 
864
865
 
866
867
 
868
869
 
870
871
 
872
            DisplayUsage ();
873
            return (0);
874
        }
875
    }
876
877
 
878
     * Default output is the DSDT and all SSDTs. One DSDT is required,
879
     * any SSDTs are optional.
880
     */
881
    Status = ExtractTables (argv[1], "DSDT", 1);
882
    if (Status)
883
    {
884
        return (Status);
885
    }
886
887
 
888
    return (Status);
889
}
890