baeae75efddae3a4ca4edc8ddd2a72bf70893bd3
[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  */
28
29 #include <sys/param.h>
30 #include <sys/queue.h>
31 #include <sys/mman.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34
35 #include <assert.h>
36 #include <ctype.h>
37 #include <err.h>
38 #include <fcntl.h>
39 #include <limits.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43
44 #include <acpi.h>
45 #include <accommon.h>
46 #include <acdebug.h>
47
48 /*
49  * Dummy DSDT Table Header
50  */
51
52 ACPI_TABLE_HEADER       dummy_dsdt_table = {
53         "DSDT", 123, 1, 123, "OEMID", "OEMTBLID", 1, "CRID", 1
54 };
55
56 /*
57  * Region space I/O routines on virtual machine
58  */
59
60 int     aml_debug_prompt = 1;
61
62 UINT8  AcpiGbl_RegionFillValue = 0;
63
64 struct ACPIRegionContent {
65         TAILQ_ENTRY(ACPIRegionContent) links;
66         int                     regtype;
67         ACPI_PHYSICAL_ADDRESS   addr;
68         UINT8                   value;
69 };
70
71 TAILQ_HEAD(ACPIRegionContentList, ACPIRegionContent);
72 struct  ACPIRegionContentList RegionContentList;
73
74 static int               aml_simulation_initialized = 0;
75
76 static void              aml_simulation_init(void);
77 static int               aml_simulate_regcontent_add(int regtype,
78                              ACPI_PHYSICAL_ADDRESS addr,
79                              UINT8 value);
80 static int               aml_simulate_regcontent_read(int regtype,
81                              ACPI_PHYSICAL_ADDRESS addr,
82                              UINT8 *valuep); 
83 static int               aml_simulate_regcontent_write(int regtype,
84                              ACPI_PHYSICAL_ADDRESS addr,
85                              UINT8 *valuep);
86 static ACPI_INTEGER      aml_simulate_prompt(char *msg, ACPI_INTEGER def_val);
87 static void              aml_simulation_regload(const char *dumpfile);
88 static void              aml_simulation_regdump(const char *dumpfile);
89
90 static void
91 aml_simulation_init(void)
92 {
93
94         aml_simulation_initialized = 1;
95         TAILQ_INIT(&RegionContentList);
96         aml_simulation_regload("region.ini");
97 }
98
99 static int
100 aml_simulate_regcontent_add(int regtype, ACPI_PHYSICAL_ADDRESS addr, UINT8 value)
101 {
102         struct  ACPIRegionContent *rc;
103
104         rc = malloc(sizeof(struct ACPIRegionContent));
105         if (rc == NULL) {
106                 return (-1);    /* malloc fail */
107         }
108         rc->regtype = regtype;
109         rc->addr = addr;
110         rc->value = value;
111
112         TAILQ_INSERT_TAIL(&RegionContentList, rc, links);
113         return (0);
114 }
115
116 static int
117 aml_simulate_regcontent_read(int regtype, ACPI_PHYSICAL_ADDRESS addr, UINT8 *valuep)
118 {
119         struct  ACPIRegionContent *rc;
120
121         if (!aml_simulation_initialized) {
122                 aml_simulation_init();
123         }
124         TAILQ_FOREACH(rc, &RegionContentList, links) {
125                 if (rc->regtype == regtype && rc->addr == addr) {
126                         *valuep = rc->value;
127                         return (1);     /* found */
128                 }
129         }
130
131         *valuep = 0;
132         return (aml_simulate_regcontent_add(regtype, addr, *valuep));
133 }
134
135 static int
136 aml_simulate_regcontent_write(int regtype, ACPI_PHYSICAL_ADDRESS addr, UINT8 *valuep)
137 {
138         struct  ACPIRegionContent *rc;
139
140         if (!aml_simulation_initialized) {
141                 aml_simulation_init();
142         }
143         TAILQ_FOREACH(rc, &RegionContentList, links) {
144                 if (rc->regtype == regtype && rc->addr == addr) {
145                         rc->value = *valuep;
146                         return (1);     /* exists */
147                 }
148         }
149
150         return (aml_simulate_regcontent_add(regtype, addr, *valuep));
151 }
152
153 static ACPI_INTEGER
154 aml_simulate_prompt(char *msg, ACPI_INTEGER def_val)
155 {
156         char            buf[16], *ep;
157         ACPI_INTEGER    val;
158
159         val = def_val;
160         printf("DEBUG");
161         if (msg != NULL) {
162                 printf("%s", msg);
163         }
164         printf("(default: 0x%jx ", (uintmax_t)val);
165         printf(" / %ju) >>", (uintmax_t)val);
166         fflush(stdout);
167
168         bzero(buf, sizeof buf);
169         while (1) {
170                 if (read(0, buf, sizeof buf) == 0) {
171                         continue;
172                 }
173                 if (buf[0] == '\n') {
174                         break;  /* use default value */
175                 }
176                 if (buf[0] == '0' && buf[1] == 'x') {
177                         val = strtoq(buf, &ep, 16);
178                 } else {
179                         val = strtoq(buf, &ep, 10);
180                 }
181                 break;
182         }
183         return (val);
184 }
185
186 static void
187 aml_simulation_regload(const char *dumpfile)
188 {
189         char    buf[256], *np, *ep;
190         struct  ACPIRegionContent rc;
191         FILE    *fp;
192
193         if (!aml_simulation_initialized) {
194                 return;
195         }
196
197         if ((fp = fopen(dumpfile, "r")) == NULL) {
198                 return;
199         }
200
201         while (fgets(buf, sizeof buf, fp) != NULL) {
202                 np = buf;
203                 /* reading region type */
204                 rc.regtype = strtoq(np, &ep, 10);
205                 if (np == ep) {
206                         continue;
207                 }
208                 np = ep;
209
210                 /* reading address */
211                 rc.addr = strtoq(np, &ep, 16);
212                 if (np == ep) {
213                         continue;
214                 }
215                 np = ep;
216
217                 /* reading value */
218                 rc.value = strtoq(np, &ep, 16);
219                 if (np == ep) {
220                         continue;
221                 }
222                 aml_simulate_regcontent_write(rc.regtype, rc.addr, &rc.value);
223         }
224
225         fclose(fp);
226 }
227
228 static void
229 aml_simulation_regdump(const char *dumpfile)
230 {
231         struct  ACPIRegionContent *rc;
232         FILE    *fp;
233
234         if (!aml_simulation_initialized) {
235                 return;
236         }
237         if ((fp = fopen(dumpfile, "w")) == NULL) {
238                 warn("%s", dumpfile);
239                 return;
240         }
241         while (!TAILQ_EMPTY(&RegionContentList)) {
242                 rc = TAILQ_FIRST(&RegionContentList);
243                 fprintf(fp, "%d 0x%jx   0x%x\n",
244                     rc->regtype, (uintmax_t)rc->addr, rc->value);
245                 TAILQ_REMOVE(&RegionContentList, rc, links);
246                 free(rc);
247         }
248
249         fclose(fp);
250         TAILQ_INIT(&RegionContentList);
251 }
252
253 /*
254  * Space handlers on virtual machine
255  */
256
257 static ACPI_STATUS
258 aml_vm_space_handler(
259         UINT32                  SpaceID,
260         UINT32                  Function,
261         ACPI_PHYSICAL_ADDRESS   Address,
262         UINT32                  BitWidth,
263         ACPI_INTEGER            *Value,
264         int                     Prompt)
265 {
266         int             state;
267         UINT8           val;
268         ACPI_INTEGER    value, i;
269         char            msg[256];
270         static char     *space_names[] = {
271                 "SYSTEM_MEMORY", "SYSTEM_IO", "PCI_CONFIG",
272                 "EC", "SMBUS", "CMOS", "PCI_BAR_TARGET"};
273
274         switch (Function) {
275         case ACPI_READ:
276                 value = 0;
277                 for (i = 0; (i * 8) < BitWidth; i++) {
278                         state = aml_simulate_regcontent_read(SpaceID,
279                                                              Address + i, &val);
280                         if (state == -1) {
281                                 return (AE_NO_MEMORY);
282                         }
283                         value |= val << (i * 8);
284                 }
285                 *Value = value;
286                 if (Prompt) {
287                         sprintf(msg, "[read (%s, %2d, 0x%jx)]",
288                                 space_names[SpaceID], BitWidth,
289                                 (uintmax_t)Address);
290                         *Value = aml_simulate_prompt(msg, value);
291                         if (*Value != value) {
292                                 return(aml_vm_space_handler(SpaceID,
293                                                 ACPI_WRITE,
294                                                 Address, BitWidth, Value, 0));
295                         }
296                 }
297                 break;
298
299         case ACPI_WRITE:
300                 value = *Value;
301                 if (Prompt) {
302                         sprintf(msg, "[write(%s, %2d, 0x%jx)]",
303                                 space_names[SpaceID], BitWidth,
304                                 (uintmax_t)Address);
305                         value = aml_simulate_prompt(msg, *Value);
306                 }
307                 *Value = value;
308                 for (i = 0; (i * 8) < BitWidth; i++) {
309                         val = value & 0xff;
310                         state = aml_simulate_regcontent_write(SpaceID,
311                                                               Address + i, &val);
312                         if (state == -1) {
313                                 return (AE_NO_MEMORY);
314                         }
315                         value = value >> 8;
316                 }
317         }
318
319         return (AE_OK);
320 }
321
322 #define DECLARE_VM_SPACE_HANDLER(name, id);                     \
323 static ACPI_STATUS                                              \
324 aml_vm_space_handler_##name (                                   \
325         UINT32                  Function,                       \
326         ACPI_PHYSICAL_ADDRESS   Address,                        \
327         UINT32                  BitWidth,                       \
328         ACPI_INTEGER            *Value,                         \
329         void                    *HandlerContext,                \
330         void                    *RegionContext)                 \
331 {                                                               \
332         return (aml_vm_space_handler(id, Function, Address,     \
333                 BitWidth, Value, aml_debug_prompt));            \
334 }
335
336 DECLARE_VM_SPACE_HANDLER(system_memory, ACPI_ADR_SPACE_SYSTEM_MEMORY);
337 DECLARE_VM_SPACE_HANDLER(system_io,     ACPI_ADR_SPACE_SYSTEM_IO);
338 DECLARE_VM_SPACE_HANDLER(pci_config,    ACPI_ADR_SPACE_PCI_CONFIG);
339 DECLARE_VM_SPACE_HANDLER(ec,            ACPI_ADR_SPACE_EC);
340 DECLARE_VM_SPACE_HANDLER(smbus,         ACPI_ADR_SPACE_SMBUS);
341 DECLARE_VM_SPACE_HANDLER(cmos,          ACPI_ADR_SPACE_CMOS);
342 DECLARE_VM_SPACE_HANDLER(pci_bar_target,ACPI_ADR_SPACE_PCI_BAR_TARGET);
343
344 static u_int8_t *mapped_rsdp;
345
346 /*
347  * Load DSDT data file and invoke debugger
348  */
349
350 static int
351 load_dsdt(const char *dsdtfile)
352 {
353         char                    filetmp[PATH_MAX];
354         u_int8_t                *code;
355         struct stat              sb;
356         int                      fd, fd2;
357         int                      error;
358
359         fd = open(dsdtfile, O_RDONLY, 0);
360         if (fd == -1) {
361                 perror("open");
362                 return (-1);
363         }
364         if (fstat(fd, &sb) == -1) {
365                 perror("fstat");
366                 return (-1);
367         }
368         code = mmap(NULL, (size_t)sb.st_size, PROT_READ, MAP_PRIVATE, fd, (off_t)0);
369         if (code == NULL) {
370                 perror("mmap");
371                 return (-1);
372         }
373         mapped_rsdp = code;
374         if ((error = AcpiInitializeSubsystem()) != AE_OK) {
375                 err(1, "AcpiInitializeSubsystem returned %d", error);
376                 return (-1);
377         }
378
379         /*
380          * make sure DSDT data contains table header or not.
381          */
382         if (strncmp((char *)code, "DSDT", 4) == 0) {
383                 strncpy(filetmp, dsdtfile, sizeof(filetmp));
384         } else {
385                 mode_t  mode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
386                 dummy_dsdt_table.Length = sizeof(ACPI_TABLE_HEADER) + sb.st_size;
387                 snprintf(filetmp, sizeof(filetmp), "%s.tmp", dsdtfile);
388                 fd2 = open(filetmp, O_WRONLY | O_CREAT | O_TRUNC, mode);
389                 if (fd2 == -1) {
390                         perror("open");
391                         return (-1);
392                 }
393                 write(fd2, &dummy_dsdt_table, sizeof(ACPI_TABLE_HEADER));
394
395                 write(fd2, code, sb.st_size);
396                 close(fd2);
397         }
398
399         /*
400          * Install the virtual machine version of address space handlers.
401          */
402         if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
403                         ACPI_ADR_SPACE_SYSTEM_MEMORY,
404                         aml_vm_space_handler_system_memory,
405                         NULL, NULL)) != AE_OK) {
406                 fprintf(stderr, "could not initialise SystemMemory handler: %d\n", error);
407                 return (-1);
408         }
409         if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
410                         ACPI_ADR_SPACE_SYSTEM_IO,
411                         aml_vm_space_handler_system_io,
412                         NULL, NULL)) != AE_OK) {
413                 fprintf(stderr, "could not initialise SystemIO handler: %d\n", error);
414                 return (-1);
415         }
416         if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
417                         ACPI_ADR_SPACE_PCI_CONFIG,
418                         aml_vm_space_handler_pci_config,
419                         NULL, NULL)) != AE_OK) {
420                 fprintf(stderr, "could not initialise PciConfig handler: %d\n", error);
421                 return (-1);
422         }
423         if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
424                         ACPI_ADR_SPACE_EC,
425                         aml_vm_space_handler_ec,
426                         NULL, NULL)) != AE_OK) {
427                 fprintf(stderr, "could not initialise EC handler: %d\n", error);
428                 return (-1);
429         }
430         if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
431                         ACPI_ADR_SPACE_SMBUS,
432                         aml_vm_space_handler_smbus,
433                         NULL, NULL)) != AE_OK) {
434                 fprintf(stderr, "could not initialise SMBUS handler: %d\n", error);
435                 return (-1);
436         }
437         if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
438                         ACPI_ADR_SPACE_CMOS,
439                         aml_vm_space_handler_cmos,
440                         NULL, NULL)) != AE_OK) {
441                 fprintf(stderr, "could not initialise CMOS handler: %d\n", error);
442                 return (-1);
443         }
444         if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
445                         ACPI_ADR_SPACE_PCI_BAR_TARGET,
446                         aml_vm_space_handler_pci_bar_target,
447                         NULL, NULL)) != AE_OK) {
448                 fprintf(stderr, "could not initialise PCI BAR TARGET handler: %d\n", error);
449                 return (-1);
450         }
451
452         AcpiDbGetTableFromFile(filetmp, NULL);
453
454         AcpiDbInitialize();
455         AcpiGbl_DebuggerConfiguration = 0;
456         AcpiDbUserCommands(':', NULL);
457
458         if (strcmp(dsdtfile, filetmp) != 0) {
459                 unlink(filetmp);
460         }
461
462         return (0);
463 }
464
465 static void
466 usage(const char *progname)
467 {
468
469         printf("usage: %s dsdt_file\n", progname);
470         exit(1);
471 }
472
473 BOOLEAN         AcpiGbl_IgnoreErrors = FALSE;
474
475 int
476 main(int argc, char *argv[])
477 {
478         char    *progname;
479
480         progname = argv[0];
481
482         if (argc == 1) {
483                 usage(progname);
484         }
485
486         AcpiDbgLevel = ACPI_DEBUG_DEFAULT;
487         AcpiGbl_EnableInterpreterSlack = TRUE;
488
489         aml_simulation_regload("region.ini");
490         if (load_dsdt(argv[1]) == 0) {
491                 aml_simulation_regdump("region.dmp");
492         }
493
494         return (0);
495 }