Merge from vendor branch LIBARCHIVE:
[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 <acnamesp.h>
47 #include <acdebug.h>
48
49 /*
50  * Dummy DSDT Table Header
51  */
52
53 ACPI_TABLE_HEADER       dummy_dsdt_table = {
54         "DSDT", 123, 1, 123, "OEMID", "OEMTBLID", 1, "CRID", 1
55 };
56
57 /*
58  * Region space I/O routines on virtual machine
59  */
60
61 int     aml_debug_prompt = 1;
62
63 struct ACPIRegionContent {
64         TAILQ_ENTRY(ACPIRegionContent) links;
65         int                     regtype;
66         ACPI_PHYSICAL_ADDRESS   addr;
67         UINT8                   value;
68 };
69
70 TAILQ_HEAD(ACPIRegionContentList, ACPIRegionContent);
71 struct  ACPIRegionContentList RegionContentList;
72
73 static int               aml_simulation_initialized = 0;
74
75 static void              aml_simulation_init(void);
76 static int               aml_simulate_regcontent_add(int regtype,
77                              ACPI_PHYSICAL_ADDRESS addr,
78                              UINT8 value);
79 static int               aml_simulate_regcontent_read(int regtype,
80                              ACPI_PHYSICAL_ADDRESS addr,
81                              UINT8 *valuep); 
82 static int               aml_simulate_regcontent_write(int regtype,
83                              ACPI_PHYSICAL_ADDRESS addr,
84                              UINT8 *valuep);
85 static ACPI_INTEGER      aml_simulate_prompt(char *msg, ACPI_INTEGER def_val);
86 static void              aml_simulation_regload(const char *dumpfile);
87 static void              aml_simulation_regdump(const char *dumpfile);
88
89 static void
90 aml_simulation_init(void)
91 {
92
93         aml_simulation_initialized = 1;
94         TAILQ_INIT(&RegionContentList);
95         aml_simulation_regload("region.ini");
96 }
97
98 static int
99 aml_simulate_regcontent_add(int regtype, ACPI_PHYSICAL_ADDRESS addr, UINT8 value)
100 {
101         struct  ACPIRegionContent *rc;
102
103         rc = malloc(sizeof(struct ACPIRegionContent));
104         if (rc == NULL) {
105                 return (-1);    /* malloc fail */
106         }
107         rc->regtype = regtype;
108         rc->addr = addr;
109         rc->value = value;
110
111         TAILQ_INSERT_TAIL(&RegionContentList, rc, links);
112         return (0);
113 }
114
115 static int
116 aml_simulate_regcontent_read(int regtype, ACPI_PHYSICAL_ADDRESS addr, UINT8 *valuep)
117 {
118         struct  ACPIRegionContent *rc;
119
120         if (!aml_simulation_initialized) {
121                 aml_simulation_init();
122         }
123         TAILQ_FOREACH(rc, &RegionContentList, links) {
124                 if (rc->regtype == regtype && rc->addr == addr) {
125                         *valuep = rc->value;
126                         return (1);     /* found */
127                 }
128         }
129
130         *valuep = 0;
131         return (aml_simulate_regcontent_add(regtype, addr, *valuep));
132 }
133
134 static int
135 aml_simulate_regcontent_write(int regtype, ACPI_PHYSICAL_ADDRESS addr, UINT8 *valuep)
136 {
137         struct  ACPIRegionContent *rc;
138
139         if (!aml_simulation_initialized) {
140                 aml_simulation_init();
141         }
142         TAILQ_FOREACH(rc, &RegionContentList, links) {
143                 if (rc->regtype == regtype && rc->addr == addr) {
144                         rc->value = *valuep;
145                         return (1);     /* exists */
146                 }
147         }
148
149         return (aml_simulate_regcontent_add(regtype, addr, *valuep));
150 }
151
152 static ACPI_INTEGER
153 aml_simulate_prompt(char *msg, ACPI_INTEGER def_val)
154 {
155         char            buf[16], *ep;
156         ACPI_INTEGER    val;
157
158         val = def_val;
159         printf("DEBUG");
160         if (msg != NULL) {
161                 printf("%s", msg);
162         }
163         printf("(default: 0x%x ", val);
164         printf(" / %u) >>", val);
165         fflush(stdout);
166
167         bzero(buf, sizeof buf);
168         while (1) {
169                 if (read(0, buf, sizeof buf) == 0) {
170                         continue;
171                 }
172                 if (buf[0] == '\n') {
173                         break;  /* use default value */
174                 }
175                 if (buf[0] == '0' && buf[1] == 'x') {
176                         val = strtoq(buf, &ep, 16);
177                 } else {
178                         val = strtoq(buf, &ep, 10);
179                 }
180                 break;
181         }
182         return (val);
183 }
184
185 static void
186 aml_simulation_regload(const char *dumpfile)
187 {
188         char    buf[256], *np, *ep;
189         struct  ACPIRegionContent rc;
190         FILE    *fp;
191
192         if (!aml_simulation_initialized) {
193                 return;
194         }
195
196         if ((fp = fopen(dumpfile, "r")) == NULL) {
197                 return;
198         }
199
200         while (fgets(buf, sizeof buf, fp) != NULL) {
201                 np = buf;
202                 /* reading region type */
203                 rc.regtype = strtoq(np, &ep, 10);
204                 if (np == ep) {
205                         continue;
206                 }
207                 np = ep;
208
209                 /* reading address */
210                 rc.addr = strtoq(np, &ep, 16);
211                 if (np == ep) {
212                         continue;
213                 }
214                 np = ep;
215
216                 /* reading value */
217                 rc.value = strtoq(np, &ep, 16);
218                 if (np == ep) {
219                         continue;
220                 }
221                 aml_simulate_regcontent_write(rc.regtype, rc.addr, &rc.value);
222         }
223
224         fclose(fp);
225 }
226
227 static void
228 aml_simulation_regdump(const char *dumpfile)
229 {
230         struct  ACPIRegionContent *rc;
231         FILE    *fp;
232
233         if (!aml_simulation_initialized) {
234                 return;
235         }
236         if ((fp = fopen(dumpfile, "w")) == NULL) {
237                 warn("%s", dumpfile);
238                 return;
239         }
240         while (!TAILQ_EMPTY(&RegionContentList)) {
241                 rc = TAILQ_FIRST(&RegionContentList);
242                 fprintf(fp, "%d 0x%x    0x%x\n",
243                     rc->regtype, rc->addr, rc->value);
244                 TAILQ_REMOVE(&RegionContentList, rc, links);
245                 free(rc);
246         }
247
248         fclose(fp);
249         TAILQ_INIT(&RegionContentList);
250 }
251
252 /*
253  * Space handlers on virtual machine
254  */
255
256 static ACPI_STATUS
257 aml_vm_space_handler(
258         UINT32                  SpaceID,
259         UINT32                  Function,
260         ACPI_PHYSICAL_ADDRESS   Address,
261         UINT32                  BitWidth,
262         ACPI_INTEGER            *Value,
263         int                     Prompt)
264 {
265         int             state;
266         UINT8           val;
267         ACPI_INTEGER    value, i;
268         char            msg[256];
269         static char     *space_names[] = {
270                 "SYSTEM_MEMORY", "SYSTEM_IO", "PCI_CONFIG",
271                 "EC", "SMBUS", "CMOS", "PCI_BAR_TARGET"};
272
273         switch (Function) {
274         case ACPI_READ:
275                 value = 0;
276                 for (i = 0; (i * 8) < BitWidth; i++) {
277                         state = aml_simulate_regcontent_read(SpaceID,
278                                                              Address + i, &val);
279                         if (state == -1) {
280                                 return (AE_NO_MEMORY);
281                         }
282                         value |= val << (i * 8);
283                 }
284                 *Value = value;
285                 if (Prompt) {
286                         sprintf(msg, "[read (%s, %2d, 0x%x)]",
287                                 space_names[SpaceID], BitWidth, Address);
288                         *Value = aml_simulate_prompt(msg, value);
289                         if (*Value != value) {
290                                 return(aml_vm_space_handler(SpaceID,
291                                                 ACPI_WRITE,
292                                                 Address, BitWidth, Value, 0));
293                         }
294                 }
295                 break;
296
297         case ACPI_WRITE:
298                 value = *Value;
299                 if (Prompt) {
300                         sprintf(msg, "[write(%s, %2d, 0x%x)]",
301                                 space_names[SpaceID], BitWidth, Address);
302                         value = aml_simulate_prompt(msg, *Value);
303                 }
304                 *Value = value;
305                 for (i = 0; (i * 8) < BitWidth; i++) {
306                         val = value & 0xff;
307                         state = aml_simulate_regcontent_write(SpaceID,
308                                                               Address + i, &val);
309                         if (state == -1) {
310                                 return (AE_NO_MEMORY);
311                         }
312                         value = value >> 8;
313                 }
314         }
315
316         return (AE_OK);
317 }
318
319 #define DECLARE_VM_SPACE_HANDLER(name, id);                     \
320 static ACPI_STATUS                                              \
321 aml_vm_space_handler_##name (                                   \
322         UINT32                  Function,                       \
323         ACPI_PHYSICAL_ADDRESS   Address,                        \
324         UINT32                  BitWidth,                       \
325         ACPI_INTEGER            *Value,                         \
326         void                    *HandlerContext,                \
327         void                    *RegionContext)                 \
328 {                                                               \
329         return (aml_vm_space_handler(id, Function, Address,     \
330                 BitWidth, Value, aml_debug_prompt));            \
331 }
332
333 DECLARE_VM_SPACE_HANDLER(system_memory, ACPI_ADR_SPACE_SYSTEM_MEMORY);
334 DECLARE_VM_SPACE_HANDLER(system_io,     ACPI_ADR_SPACE_SYSTEM_IO);
335 DECLARE_VM_SPACE_HANDLER(pci_config,    ACPI_ADR_SPACE_PCI_CONFIG);
336 DECLARE_VM_SPACE_HANDLER(ec,            ACPI_ADR_SPACE_EC);
337 DECLARE_VM_SPACE_HANDLER(smbus,         ACPI_ADR_SPACE_SMBUS);
338 DECLARE_VM_SPACE_HANDLER(cmos,          ACPI_ADR_SPACE_CMOS);
339 DECLARE_VM_SPACE_HANDLER(pci_bar_target,ACPI_ADR_SPACE_PCI_BAR_TARGET);
340
341 static u_int8_t *mapped_rsdp;
342 #define ACPI_MAX_INIT_TABLES (16)
343 static ACPI_TABLE_DESC          Tables[ACPI_MAX_INIT_TABLES];
344
345 /*
346  * Load DSDT data file and invoke debugger
347  */
348
349 static int
350 load_dsdt(const char *dsdtfile)
351 {
352         char                    filetmp[PATH_MAX];
353         u_int8_t                *code, *amlptr;
354         struct stat              sb;
355         int                      fd, fd2;
356         int                      error;
357         ACPI_TABLE_HEADER       *Table;
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         AcpiGbl_GlobalLockPresent = TRUE;
453
454         error = AcpiDbReadTableFromFile (filetmp, &Table);
455         if (ACPI_FAILURE(error)) {
456                 fprintf(stderr, "AcpiDbReadTableFromFile failed: %s\n",
457                         AcpiFormatException(error));
458                 return (-1);
459         }
460         AeBuildLocalTables(Table);
461         if (ACPI_FAILURE(error = AeInstallTables())) {
462                 fprintf(stderr, "AeInstallTables failed: %s\n",
463                         AcpiFormatException(error));
464                 return (-1);
465         }
466
467         AcpiUtSetIntegerWidth (Table->Revision);
468
469         AcpiDbInitialize();
470         AcpiGbl_DebuggerConfiguration = 0;
471         AcpiDbUserCommands(':', NULL);
472
473         if (strcmp(dsdtfile, filetmp) != 0) {
474                 unlink(filetmp);
475         }
476
477         return (0);
478 }
479
480 static void
481 usage(const char *progname)
482 {
483
484         printf("usage: %s dsdt_file\n", progname);
485         exit(1);
486 }
487
488 BOOLEAN         AcpiGbl_IgnoreErrors = FALSE;
489
490 int
491 main(int argc, char *argv[])
492 {
493         char    *progname;
494
495         progname = argv[0];
496
497         if (argc == 1) {
498                 usage(progname);
499         }
500
501         AcpiDbgLevel = ACPI_DEBUG_DEFAULT;
502         AcpiGbl_EnableInterpreterSlack = TRUE;
503
504         aml_simulation_regload("region.ini");
505         if (load_dsdt(argv[1]) == 0) {
506                 aml_simulation_regdump("region.dmp");
507         }
508
509         return (0);
510 }