md5(1): Fix typo.
[dragonfly.git] / usr.sbin / acpi / acpidb / acpidb.c
1 /*-
2  * Copyright (c) 2000-2002 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
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
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  *      $FreeBSD: src/usr.sbin/acpi/acpidb/acpidb.c,v 1.1 2003/08/07 16:51:50 njl Exp $
27  *      $DragonFly: src/usr.sbin/acpi/acpidb/acpidb.c,v 1.2 2007/01/17 17:31:19 y0netan1 Exp $
28  */
29
30 #include <sys/param.h>
31 #include <sys/queue.h>
32 #include <sys/mman.h>
33 #include <sys/stat.h>
34 #include <sys/types.h>
35
36 #include <assert.h>
37 #include <ctype.h>
38 #include <err.h>
39 #include <fcntl.h>
40 #include <limits.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44
45 #include <acpi.h>
46 #include <acconfig.h>
47 #include <aclocal.h>
48 #include <acobject.h>
49 #include <acstruct.h>
50 #include <acnamesp.h>
51 #include <acglobal.h>
52 #include <acdebug.h>
53
54 /*
55  * Dummy DSDT Table Header
56  */
57
58 ACPI_TABLE_HEADER       dummy_dsdt_table = {
59         "DSDT", 123, 1, 123, "OEMID", "OEMTBLID", 1, "CRID", 1
60 };
61
62 /*
63  * Region space I/O routines on virtual machine
64  */
65
66 int     aml_debug_prompt = 1;
67
68 UINT8  AcpiGbl_RegionFillValue = 0;
69
70 struct ACPIRegionContent {
71         TAILQ_ENTRY(ACPIRegionContent) links;
72         int                     regtype;
73         ACPI_PHYSICAL_ADDRESS   addr;
74         UINT8                   value;
75 };
76
77 TAILQ_HEAD(ACPIRegionContentList, ACPIRegionContent);
78 struct  ACPIRegionContentList RegionContentList;
79
80 static int               aml_simulation_initialized = 0;
81
82 static void              aml_simulation_init(void);
83 static int               aml_simulate_regcontent_add(int regtype,
84                              ACPI_PHYSICAL_ADDRESS addr,
85                              UINT8 value);
86 static int               aml_simulate_regcontent_read(int regtype,
87                              ACPI_PHYSICAL_ADDRESS addr,
88                              UINT8 *valuep); 
89 static int               aml_simulate_regcontent_write(int regtype,
90                              ACPI_PHYSICAL_ADDRESS addr,
91                              UINT8 *valuep);
92 static ACPI_INTEGER      aml_simulate_prompt(char *msg, ACPI_INTEGER def_val);
93 static void              aml_simulation_regload(const char *dumpfile);
94 static void              aml_simulation_regdump(const char *dumpfile);
95
96 static void
97 aml_simulation_init(void)
98 {
99
100         aml_simulation_initialized = 1;
101         TAILQ_INIT(&RegionContentList);
102         aml_simulation_regload("region.ini");
103 }
104
105 static int
106 aml_simulate_regcontent_add(int regtype, ACPI_PHYSICAL_ADDRESS addr, UINT8 value)
107 {
108         struct  ACPIRegionContent *rc;
109
110         rc = malloc(sizeof(struct ACPIRegionContent));
111         if (rc == NULL) {
112                 return (-1);    /* malloc fail */
113         }
114         rc->regtype = regtype;
115         rc->addr = addr;
116         rc->value = value;
117
118         TAILQ_INSERT_TAIL(&RegionContentList, rc, links);
119         return (0);
120 }
121
122 static int
123 aml_simulate_regcontent_read(int regtype, ACPI_PHYSICAL_ADDRESS addr, UINT8 *valuep)
124 {
125         struct  ACPIRegionContent *rc;
126
127         if (!aml_simulation_initialized) {
128                 aml_simulation_init();
129         }
130         TAILQ_FOREACH(rc, &RegionContentList, links) {
131                 if (rc->regtype == regtype && rc->addr == addr) {
132                         *valuep = rc->value;
133                         return (1);     /* found */
134                 }
135         }
136
137         *valuep = 0;
138         return (aml_simulate_regcontent_add(regtype, addr, *valuep));
139 }
140
141 static int
142 aml_simulate_regcontent_write(int regtype, ACPI_PHYSICAL_ADDRESS addr, UINT8 *valuep)
143 {
144         struct  ACPIRegionContent *rc;
145
146         if (!aml_simulation_initialized) {
147                 aml_simulation_init();
148         }
149         TAILQ_FOREACH(rc, &RegionContentList, links) {
150                 if (rc->regtype == regtype && rc->addr == addr) {
151                         rc->value = *valuep;
152                         return (1);     /* exists */
153                 }
154         }
155
156         return (aml_simulate_regcontent_add(regtype, addr, *valuep));
157 }
158
159 static ACPI_INTEGER
160 aml_simulate_prompt(char *msg, ACPI_INTEGER def_val)
161 {
162         char            buf[16], *ep;
163         ACPI_INTEGER    val;
164
165         val = def_val;
166         printf("DEBUG");
167         if (msg != NULL) {
168                 printf("%s", msg);
169         }
170         printf("(default: 0x%x ", val);
171         printf(" / %u) >>", val);
172         fflush(stdout);
173
174         bzero(buf, sizeof buf);
175         while (1) {
176                 if (read(0, buf, sizeof buf) == 0) {
177                         continue;
178                 }
179                 if (buf[0] == '\n') {
180                         break;  /* use default value */
181                 }
182                 if (buf[0] == '0' && buf[1] == 'x') {
183                         val = strtoq(buf, &ep, 16);
184                 } else {
185                         val = strtoq(buf, &ep, 10);
186                 }
187                 break;
188         }
189         return (val);
190 }
191
192 static void
193 aml_simulation_regload(const char *dumpfile)
194 {
195         char    buf[256], *np, *ep;
196         struct  ACPIRegionContent rc;
197         FILE    *fp;
198
199         if (!aml_simulation_initialized) {
200                 return;
201         }
202
203         if ((fp = fopen(dumpfile, "r")) == NULL) {
204                 return;
205         }
206
207         while (fgets(buf, sizeof buf, fp) != NULL) {
208                 np = buf;
209                 /* reading region type */
210                 rc.regtype = strtoq(np, &ep, 10);
211                 if (np == ep) {
212                         continue;
213                 }
214                 np = ep;
215
216                 /* reading address */
217                 rc.addr = strtoq(np, &ep, 16);
218                 if (np == ep) {
219                         continue;
220                 }
221                 np = ep;
222
223                 /* reading value */
224                 rc.value = strtoq(np, &ep, 16);
225                 if (np == ep) {
226                         continue;
227                 }
228                 aml_simulate_regcontent_write(rc.regtype, rc.addr, &rc.value);
229         }
230
231         fclose(fp);
232 }
233
234 static void
235 aml_simulation_regdump(const char *dumpfile)
236 {
237         struct  ACPIRegionContent *rc;
238         FILE    *fp;
239
240         if (!aml_simulation_initialized) {
241                 return;
242         }
243         if ((fp = fopen(dumpfile, "w")) == NULL) {
244                 warn("%s", dumpfile);
245                 return;
246         }
247         while (!TAILQ_EMPTY(&RegionContentList)) {
248                 rc = TAILQ_FIRST(&RegionContentList);
249                 fprintf(fp, "%d 0x%x    0x%x\n",
250                     rc->regtype, rc->addr, rc->value);
251                 TAILQ_REMOVE(&RegionContentList, rc, links);
252                 free(rc);
253         }
254
255         fclose(fp);
256         TAILQ_INIT(&RegionContentList);
257 }
258
259 /*
260  * Space handlers on virtual machine
261  */
262
263 static ACPI_STATUS
264 aml_vm_space_handler(
265         UINT32                  SpaceID,
266         UINT32                  Function,
267         ACPI_PHYSICAL_ADDRESS   Address,
268         UINT32                  BitWidth,
269         ACPI_INTEGER            *Value,
270         int                     Prompt)
271 {
272         int             state;
273         UINT8           val;
274         ACPI_INTEGER    value, i;
275         char            msg[256];
276         static char     *space_names[] = {
277                 "SYSTEM_MEMORY", "SYSTEM_IO", "PCI_CONFIG",
278                 "EC", "SMBUS", "CMOS", "PCI_BAR_TARGET"};
279
280         switch (Function) {
281         case ACPI_READ:
282                 value = 0;
283                 for (i = 0; (i * 8) < BitWidth; i++) {
284                         state = aml_simulate_regcontent_read(SpaceID,
285                                                              Address + i, &val);
286                         if (state == -1) {
287                                 return (AE_NO_MEMORY);
288                         }
289                         value |= val << (i * 8);
290                 }
291                 *Value = value;
292                 if (Prompt) {
293                         sprintf(msg, "[read (%s, %2d, 0x%x)]",
294                                 space_names[SpaceID], BitWidth, Address);
295                         *Value = aml_simulate_prompt(msg, value);
296                         if (*Value != value) {
297                                 return(aml_vm_space_handler(SpaceID,
298                                                 ACPI_WRITE,
299                                                 Address, BitWidth, Value, 0));
300                         }
301                 }
302                 break;
303
304         case ACPI_WRITE:
305                 value = *Value;
306                 if (Prompt) {
307                         sprintf(msg, "[write(%s, %2d, 0x%x)]",
308                                 space_names[SpaceID], BitWidth, Address);
309                         value = aml_simulate_prompt(msg, *Value);
310                 }
311                 *Value = value;
312                 for (i = 0; (i * 8) < BitWidth; i++) {
313                         val = value & 0xff;
314                         state = aml_simulate_regcontent_write(SpaceID,
315                                                               Address + i, &val);
316                         if (state == -1) {
317                                 return (AE_NO_MEMORY);
318                         }
319                         value = value >> 8;
320                 }
321         }
322
323         return (AE_OK);
324 }
325
326 #define DECLARE_VM_SPACE_HANDLER(name, id);                     \
327 static ACPI_STATUS                                              \
328 aml_vm_space_handler_##name (                                   \
329         UINT32                  Function,                       \
330         ACPI_PHYSICAL_ADDRESS   Address,                        \
331         UINT32                  BitWidth,                       \
332         ACPI_INTEGER            *Value,                         \
333         void                    *HandlerContext,                \
334         void                    *RegionContext)                 \
335 {                                                               \
336         return (aml_vm_space_handler(id, Function, Address,     \
337                 BitWidth, Value, aml_debug_prompt));            \
338 }
339
340 DECLARE_VM_SPACE_HANDLER(system_memory, ACPI_ADR_SPACE_SYSTEM_MEMORY);
341 DECLARE_VM_SPACE_HANDLER(system_io,     ACPI_ADR_SPACE_SYSTEM_IO);
342 DECLARE_VM_SPACE_HANDLER(pci_config,    ACPI_ADR_SPACE_PCI_CONFIG);
343 DECLARE_VM_SPACE_HANDLER(ec,            ACPI_ADR_SPACE_EC);
344 DECLARE_VM_SPACE_HANDLER(smbus,         ACPI_ADR_SPACE_SMBUS);
345 DECLARE_VM_SPACE_HANDLER(cmos,          ACPI_ADR_SPACE_CMOS);
346 DECLARE_VM_SPACE_HANDLER(pci_bar_target,ACPI_ADR_SPACE_PCI_BAR_TARGET);
347
348 static u_int8_t *mapped_rsdp;
349 #define ACPI_MAX_INIT_TABLES (16)
350 static ACPI_TABLE_DESC          Tables[ACPI_MAX_INIT_TABLES];
351
352 /*
353  * Load DSDT data file and invoke debugger
354  */
355
356 static int
357 load_dsdt(const char *dsdtfile)
358 {
359         char                    filetmp[PATH_MAX];
360         u_int8_t                *code, *amlptr;
361         struct stat              sb;
362         int                      fd, fd2;
363         int                      error;
364         ACPI_TABLE_HEADER       *Table;
365
366         fd = open(dsdtfile, O_RDONLY, 0);
367         if (fd == -1) {
368                 perror("open");
369                 return (-1);
370         }
371         if (fstat(fd, &sb) == -1) {
372                 perror("fstat");
373                 return (-1);
374         }
375         code = mmap(NULL, (size_t)sb.st_size, PROT_READ, MAP_PRIVATE, fd, (off_t)0);
376         if (code == NULL) {
377                 perror("mmap");
378                 return (-1);
379         }
380         mapped_rsdp = code;
381         if ((error = AcpiInitializeSubsystem()) != AE_OK) {
382                 err(1, "AcpiInitializeSubsystem returned %d", error);
383                 return (-1);
384         }
385
386         /*
387          * make sure DSDT data contains table header or not.
388          */
389         if (strncmp((char *)code, "DSDT", 4) == 0) {
390                 strncpy(filetmp, dsdtfile, sizeof(filetmp));
391         } else {
392                 mode_t  mode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
393                 dummy_dsdt_table.Length = sizeof(ACPI_TABLE_HEADER) + sb.st_size;
394                 snprintf(filetmp, sizeof(filetmp), "%s.tmp", dsdtfile);
395                 fd2 = open(filetmp, O_WRONLY | O_CREAT | O_TRUNC, mode);
396                 if (fd2 == -1) {
397                         perror("open");
398                         return (-1);
399                 }
400                 write(fd2, &dummy_dsdt_table, sizeof(ACPI_TABLE_HEADER));
401
402                 write(fd2, code, sb.st_size);
403                 close(fd2);
404         }
405
406         /*
407          * Install the virtual machine version of address space handlers.
408          */
409         if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
410                         ACPI_ADR_SPACE_SYSTEM_MEMORY,
411                         aml_vm_space_handler_system_memory,
412                         NULL, NULL)) != AE_OK) {
413                 fprintf(stderr, "could not initialise SystemMemory handler: %d\n", error);
414                 return (-1);
415         }
416         if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
417                         ACPI_ADR_SPACE_SYSTEM_IO,
418                         aml_vm_space_handler_system_io,
419                         NULL, NULL)) != AE_OK) {
420                 fprintf(stderr, "could not initialise SystemIO handler: %d\n", error);
421                 return (-1);
422         }
423         if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
424                         ACPI_ADR_SPACE_PCI_CONFIG,
425                         aml_vm_space_handler_pci_config,
426                         NULL, NULL)) != AE_OK) {
427                 fprintf(stderr, "could not initialise PciConfig handler: %d\n", error);
428                 return (-1);
429         }
430         if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
431                         ACPI_ADR_SPACE_EC,
432                         aml_vm_space_handler_ec,
433                         NULL, NULL)) != AE_OK) {
434                 fprintf(stderr, "could not initialise EC handler: %d\n", error);
435                 return (-1);
436         }
437         if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
438                         ACPI_ADR_SPACE_SMBUS,
439                         aml_vm_space_handler_smbus,
440                         NULL, NULL)) != AE_OK) {
441                 fprintf(stderr, "could not initialise SMBUS handler: %d\n", error);
442                 return (-1);
443         }
444         if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
445                         ACPI_ADR_SPACE_CMOS,
446                         aml_vm_space_handler_cmos,
447                         NULL, NULL)) != AE_OK) {
448                 fprintf(stderr, "could not initialise CMOS handler: %d\n", error);
449                 return (-1);
450         }
451         if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
452                         ACPI_ADR_SPACE_PCI_BAR_TARGET,
453                         aml_vm_space_handler_pci_bar_target,
454                         NULL, NULL)) != AE_OK) {
455                 fprintf(stderr, "could not initialise PCI BAR TARGET handler: %d\n", error);
456                 return (-1);
457         }
458
459         AcpiGbl_GlobalLockPresent = TRUE;
460
461         error = AcpiDbReadTableFromFile (filetmp, &Table);
462         if (ACPI_FAILURE(error)) {
463                 fprintf(stderr, "AcpiDbReadTableFromFile failed: %s\n",
464                         AcpiFormatException(error));
465                 return (-1);
466         }
467         AeBuildLocalTables(Table);
468         if (ACPI_FAILURE(error = AeInstallTables())) {
469                 fprintf(stderr, "AeInstallTables failed: %s\n",
470                         AcpiFormatException(error));
471                 return (-1);
472         }
473
474         AcpiUtSetIntegerWidth (Table->Revision);
475
476         AcpiDbInitialize();
477         AcpiGbl_DebuggerConfiguration = 0;
478         AcpiDbUserCommands(':', NULL);
479
480         if (strcmp(dsdtfile, filetmp) != 0) {
481                 unlink(filetmp);
482         }
483
484         return (0);
485 }
486
487 static void
488 usage(const char *progname)
489 {
490
491         printf("usage: %s dsdt_file\n", progname);
492         exit(1);
493 }
494
495 BOOLEAN         AcpiGbl_IgnoreErrors = FALSE;
496
497 int
498 main(int argc, char *argv[])
499 {
500         char    *progname;
501
502         progname = argv[0];
503
504         if (argc == 1) {
505                 usage(progname);
506         }
507
508         AcpiDbgLevel = ACPI_DEBUG_DEFAULT;
509         AcpiGbl_EnableInterpreterSlack = TRUE;
510
511         aml_simulation_regload("region.ini");
512         if (load_dsdt(argv[1]) == 0) {
513                 aml_simulation_regdump("region.dmp");
514         }
515
516         return (0);
517 }