Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Details | 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 - 2010, Intel Corp.
12
 * All rights reserved.
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
 
125
#define FIND_HEADER         0
126
#define EXTRACT_DATA        1
127
#define BUFFER_SIZE         256
128
129
 
130
 
131
132
 
133
CheckAscii (
134
    char                    *Name,
135
    int                     Count);
136
137
 
138
NormalizeSignature (
139
    char                    *Signature);
140
141
 
142
GetNextInstance (
143
    char                    *InputPathname,
144
    char                    *Signature);
145
146
 
147
ExtractTables (
148
    char                    *InputPathname,
149
    char                    *Signature,
150
    unsigned int            MinimumInstances);
151
152
 
153
GetTableHeader (
154
    FILE                    *InputFile,
155
    unsigned char           *OutputData);
156
157
 
158
CountTableInstances (
159
    char                    *InputPathname,
160
    char                    *Signature);
161
162
 
163
ListTables (
164
    char                    *InputPathname);
165
166
 
167
ConvertLine (
168
    char                    *InputLine,
169
    unsigned char           *OutputData);
170
171
 
172
DisplayUsage (
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
 
199
char                        Filename[16];
200
unsigned char               Data[16];
201
202
 
203
 
204
 *
205
 * FUNCTION:    DisplayUsage
206
 *
207
 * DESCRIPTION: Usage message
208
 *
209
 ******************************************************************************/
210
211
 
212
DisplayUsage (
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
 
243
CheckAscii (
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
 
273
NormalizeSignature (
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
 
298
ConvertLine (
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
 
356
GetTableHeader (
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
 
404
CountTableInstances (
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
 
462
GetNextInstance (
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
 
523
ExtractTables (
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
 
580
581
 
582
                (Buffer[0] == '\n'))
583
            {
584
                continue;
585
            }
586
587
 
588
            strncpy (ThisSignature, Buffer, 4);
589
590
 
591
            {
592
                /* Ignore signatures that don't match */
593
594
 
595
                {
596
                    continue;
597
                }
598
            }
599
600
 
601
             * Get the instance number for this signature. Only the
602
             * SSDT and PSDT tables can have multiple instances.
603
             */
604
            ThisInstance = GetNextInstance (InputPathname, ThisSignature);
605
606
 
607
608
 
609
            {
610
                sprintf (Filename, "%4.4s%u.dat", ThisSignature, ThisInstance);
611
            }
612
            else
613
            {
614
                sprintf (Filename, "%4.4s.dat", ThisSignature);
615
            }
616
617
 
618
            if (!OutputFile)
619
            {
620
                printf ("Could not open %s\n", Filename);
621
                Status = -1;
622
                goto CleanupAndExit;
623
            }
624
625
 
626
            TotalBytesWritten = 0;
627
            FoundTable = 1;
628
            continue;
629
630
 
631
632
 
633
634
 
635
                (Buffer[0] != ' '))
636
            {
637
                fclose (OutputFile);
638
                OutputFile = NULL;
639
                State = FIND_HEADER;
640
641
 
642
                    ThisSignature, TotalBytesWritten, Filename);
643
                continue;
644
            }
645
646
 
647
648
 
649
650
 
651
652
 
653
            if (BytesWritten != BytesConverted)
654
            {
655
                printf ("Write error on %s\n", Filename);
656
                fclose (OutputFile);
657
                OutputFile = NULL;
658
                Status = -1;
659
                goto CleanupAndExit;
660
            }
661
662
 
663
            continue;
664
665
 
666
            Status = -1;
667
            goto CleanupAndExit;
668
        }
669
    }
670
671
 
672
    {
673
        printf ("Table %s was not found in %s\n", Signature, InputPathname);
674
    }
675
676
 
677
 
678
679
 
680
    {
681
        fclose (OutputFile);
682
        if (State == EXTRACT_DATA)
683
        {
684
            /* Received an EOF while extracting data */
685
686
 
687
                ThisSignature, TotalBytesWritten, Filename);
688
        }
689
    }
690
691
 
692
    return (Status);
693
}
694
695
 
696
 
697
 *
698
 * FUNCTION:    ListTables
699
 *
700
 * PARAMETERS:  InputPathname       - Filename for acpidump file
701
 *
702
 * RETURN:      Status
703
 *
704
 * DESCRIPTION: Display info for all ACPI tables found in input. Does not
705
 *              perform an actual extraction of the tables.
706
 *
707
 ******************************************************************************/
708
709
 
710
ListTables (
711
    char                    *InputPathname)
712
{
713
    FILE                    *InputFile;
714
    char                    Buffer[BUFFER_SIZE];
715
    size_t                  HeaderSize;
716
    unsigned char           Header[48];
717
    int                     TableCount = 0;
718
    ACPI_TABLE_HEADER       *TableHeader = (ACPI_TABLE_HEADER *) (void *) Header;
719
720
 
721
 
722
723
 
724
    if (!InputFile)
725
    {
726
        printf ("Could not open %s\n", InputPathname);
727
        return (-1);
728
    }
729
730
 
731
732
 
733
            "   OemRevision CompilerId CompilerRevision\n\n");
734
735
 
736
    {
737
        /* Ignore empty lines and lines that start with a space */
738
739
 
740
            (Buffer[0] == '\n'))
741
        {
742
            continue;
743
        }
744
745
 
746
747
 
748
        if (HeaderSize < 16)
749
        {
750
            continue;
751
        }
752
753
 
754
755
 
756
        {
757
            CheckAscii ((char *) &Header[9], 6);
758
            printf ("%8.4s                   \"%6.6s\"\n", "RSDP", &Header[9]);
759
            TableCount++;
760
            continue;
761
        }
762
763
 
764
765
 
766
        {
767
            continue;
768
        }
769
770
 
771
772
 
773
        printf ("%8.4s % 7d", TableHeader->Signature, TableHeader->Length);
774
775
 
776
777
 
778
        {
779
            printf ("\n");
780
            continue;
781
        }
782
783
 
784
785
 
786
        CheckAscii (TableHeader->OemTableId, 8);
787
        CheckAscii (TableHeader->AslCompilerId, 4);
788
789
 
790
            TableHeader->Revision, TableHeader->OemId,
791
            TableHeader->OemTableId, TableHeader->OemRevision,
792
            TableHeader->AslCompilerId, TableHeader->AslCompilerRevision);
793
    }
794
795
 
796
    fclose (InputFile);
797
    return (0);
798
}
799
800
 
801
 
802
 *
803
 * FUNCTION:    main
804
 *
805
 * DESCRIPTION: C main function
806
 *
807
 ******************************************************************************/
808
809
 
810
main (
811
    int                     argc,
812
    char                    *argv[])
813
{
814
    int                     Status;
815
816
 
817
 
818
    {
819
        DisplayUsage ();
820
        return (0);
821
    }
822
823
 
824
    {
825
        if (argc < 3)
826
        {
827
            DisplayUsage ();
828
            return (0);
829
        }
830
831
 
832
        {
833
        case 'a':
834
835
 
836
837
 
838
839
 
840
841
 
842
843
 
844
845
 
846
847
 
848
849
 
850
851
 
852
            DisplayUsage ();
853
            return (0);
854
        }
855
    }
856
857
 
858
     * Default output is the DSDT and all SSDTs. One DSDT is required,
859
     * any SSDTs are optional.
860
     */
861
    Status = ExtractTables (argv[1], "DSDT", 1);
862
    if (Status)
863
    {
864
        return (Status);
865
    }
866
867
 
868
    return (Status);
869
}
870