]> Git Repo - qemu.git/blob - hw/acpi.c
acpi: split acpi.c into the common part and the piix4 part.
[qemu.git] / hw / acpi.c
1 /*
2  * ACPI implementation
3  *
4  * Copyright (c) 2006 Fabrice Bellard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License version 2 as published by the Free Software Foundation.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, see <http://www.gnu.org/licenses/>
17  */
18 #include "hw.h"
19 #include "pc.h"
20 #include "pci.h"
21 #include "sysemu.h"
22 #include "acpi.h"
23
24 struct acpi_table_header
25 {
26     char signature [4];    /* ACPI signature (4 ASCII characters) */
27     uint32_t length;          /* Length of table, in bytes, including header */
28     uint8_t revision;         /* ACPI Specification minor version # */
29     uint8_t checksum;         /* To make sum of entire table == 0 */
30     char oem_id [6];       /* OEM identification */
31     char oem_table_id [8]; /* OEM table identification */
32     uint32_t oem_revision;    /* OEM revision number */
33     char asl_compiler_id [4]; /* ASL compiler vendor ID */
34     uint32_t asl_compiler_revision; /* ASL compiler revision number */
35 } __attribute__((packed));
36
37 char *acpi_tables;
38 size_t acpi_tables_len;
39
40 static int acpi_checksum(const uint8_t *data, int len)
41 {
42     int sum, i;
43     sum = 0;
44     for(i = 0; i < len; i++)
45         sum += data[i];
46     return (-sum) & 0xff;
47 }
48
49 int acpi_table_add(const char *t)
50 {
51     static const char *dfl_id = "QEMUQEMU";
52     char buf[1024], *p, *f;
53     struct acpi_table_header acpi_hdr;
54     unsigned long val;
55     size_t off;
56
57     memset(&acpi_hdr, 0, sizeof(acpi_hdr));
58   
59     if (get_param_value(buf, sizeof(buf), "sig", t)) {
60         strncpy(acpi_hdr.signature, buf, 4);
61     } else {
62         strncpy(acpi_hdr.signature, dfl_id, 4);
63     }
64     if (get_param_value(buf, sizeof(buf), "rev", t)) {
65         val = strtoul(buf, &p, 10);
66         if (val > 255 || *p != '\0')
67             goto out;
68     } else {
69         val = 1;
70     }
71     acpi_hdr.revision = (int8_t)val;
72
73     if (get_param_value(buf, sizeof(buf), "oem_id", t)) {
74         strncpy(acpi_hdr.oem_id, buf, 6);
75     } else {
76         strncpy(acpi_hdr.oem_id, dfl_id, 6);
77     }
78
79     if (get_param_value(buf, sizeof(buf), "oem_table_id", t)) {
80         strncpy(acpi_hdr.oem_table_id, buf, 8);
81     } else {
82         strncpy(acpi_hdr.oem_table_id, dfl_id, 8);
83     }
84
85     if (get_param_value(buf, sizeof(buf), "oem_rev", t)) {
86         val = strtol(buf, &p, 10);
87         if(*p != '\0')
88             goto out;
89     } else {
90         val = 1;
91     }
92     acpi_hdr.oem_revision = cpu_to_le32(val);
93
94     if (get_param_value(buf, sizeof(buf), "asl_compiler_id", t)) {
95         strncpy(acpi_hdr.asl_compiler_id, buf, 4);
96     } else {
97         strncpy(acpi_hdr.asl_compiler_id, dfl_id, 4);
98     }
99
100     if (get_param_value(buf, sizeof(buf), "asl_compiler_rev", t)) {
101         val = strtol(buf, &p, 10);
102         if(*p != '\0')
103             goto out;
104     } else {
105         val = 1;
106     }
107     acpi_hdr.asl_compiler_revision = cpu_to_le32(val);
108     
109     if (!get_param_value(buf, sizeof(buf), "data", t)) {
110          buf[0] = '\0';
111     }
112
113     acpi_hdr.length = sizeof(acpi_hdr);
114
115     f = buf;
116     while (buf[0]) {
117         struct stat s;
118         char *n = strchr(f, ':');
119         if (n)
120             *n = '\0';
121         if(stat(f, &s) < 0) {
122             fprintf(stderr, "Can't stat file '%s': %s\n", f, strerror(errno));
123             goto out;
124         }
125         acpi_hdr.length += s.st_size;
126         if (!n)
127             break;
128         *n = ':';
129         f = n + 1;
130     }
131
132     if (!acpi_tables) {
133         acpi_tables_len = sizeof(uint16_t);
134         acpi_tables = qemu_mallocz(acpi_tables_len);
135     }
136     p = acpi_tables + acpi_tables_len;
137     acpi_tables_len += sizeof(uint16_t) + acpi_hdr.length;
138     acpi_tables = qemu_realloc(acpi_tables, acpi_tables_len);
139
140     acpi_hdr.length = cpu_to_le32(acpi_hdr.length);
141     *(uint16_t*)p = acpi_hdr.length;
142     p += sizeof(uint16_t);
143     memcpy(p, &acpi_hdr, sizeof(acpi_hdr));
144     off = sizeof(acpi_hdr);
145
146     f = buf;
147     while (buf[0]) {
148         struct stat s;
149         int fd;
150         char *n = strchr(f, ':');
151         if (n)
152             *n = '\0';
153         fd = open(f, O_RDONLY);
154
155         if(fd < 0)
156             goto out;
157         if(fstat(fd, &s) < 0) {
158             close(fd);
159             goto out;
160         }
161
162         do {
163             int r;
164             r = read(fd, p + off, s.st_size);
165             if (r > 0) {
166                 off += r;
167                 s.st_size -= r;
168             } else if ((r < 0 && errno != EINTR) || r == 0) {
169                 close(fd);
170                 goto out;
171             }
172         } while(s.st_size);
173
174         close(fd);
175         if (!n)
176             break;
177         f = n + 1;
178     }
179
180     ((struct acpi_table_header*)p)->checksum = acpi_checksum((uint8_t*)p, off);
181     /* increase number of tables */
182     (*(uint16_t*)acpi_tables) =
183             cpu_to_le32(le32_to_cpu(*(uint16_t*)acpi_tables) + 1);
184     return 0;
185 out:
186     if (acpi_tables) {
187         qemu_free(acpi_tables);
188         acpi_tables = NULL;
189     }
190     return -1;
191 }
This page took 0.038535 seconds and 4 git commands to generate.