Merge commit 'f471a17e9d869df3c6573f7ec02c4725676d6f3a' into upstream-merge
[qemu/qemu-dev-zwu.git] / hw / acpi.c
blobb2cc4b10548f784164087115457b9faf02ef83ad
1 /*
2 * ACPI implementation
4 * Copyright (c) 2006 Fabrice Bellard
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.
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.
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/>
18 #include "hw.h"
19 #include "pc.h"
20 #include "acpi.h"
21 #include "kvm.h"
22 #include "qemu-kvm.h"
23 #include "string.h"
25 struct acpi_table_header
27 char signature [4]; /* ACPI signature (4 ASCII characters) */
28 uint32_t length; /* Length of table, in bytes, including header */
29 uint8_t revision; /* ACPI Specification minor version # */
30 uint8_t checksum; /* To make sum of entire table == 0 */
31 char oem_id [6]; /* OEM identification */
32 char oem_table_id [8]; /* OEM table identification */
33 uint32_t oem_revision; /* OEM revision number */
34 char asl_compiler_id [4]; /* ASL compiler vendor ID */
35 uint32_t asl_compiler_revision; /* ASL compiler revision number */
36 } __attribute__((packed));
38 char *acpi_tables;
39 size_t acpi_tables_len;
41 static int acpi_checksum(const uint8_t *data, int len)
43 int sum, i;
44 sum = 0;
45 for(i = 0; i < len; i++)
46 sum += data[i];
47 return (-sum) & 0xff;
50 int acpi_table_add(const char *t)
52 static const char *dfl_id = "QEMUQEMU";
53 char buf[1024], *p, *f;
54 struct acpi_table_header acpi_hdr;
55 unsigned long val;
56 size_t off;
58 memset(&acpi_hdr, 0, sizeof(acpi_hdr));
60 if (get_param_value(buf, sizeof(buf), "sig", t)) {
61 strncpy(acpi_hdr.signature, buf, 4);
62 } else {
63 strncpy(acpi_hdr.signature, dfl_id, 4);
65 if (get_param_value(buf, sizeof(buf), "rev", t)) {
66 val = strtoul(buf, &p, 10);
67 if (val > 255 || *p != '\0')
68 goto out;
69 } else {
70 val = 1;
72 acpi_hdr.revision = (int8_t)val;
74 if (get_param_value(buf, sizeof(buf), "oem_id", t)) {
75 strncpy(acpi_hdr.oem_id, buf, 6);
76 } else {
77 strncpy(acpi_hdr.oem_id, dfl_id, 6);
80 if (get_param_value(buf, sizeof(buf), "oem_table_id", t)) {
81 strncpy(acpi_hdr.oem_table_id, buf, 8);
82 } else {
83 strncpy(acpi_hdr.oem_table_id, dfl_id, 8);
86 if (get_param_value(buf, sizeof(buf), "oem_rev", t)) {
87 val = strtol(buf, &p, 10);
88 if(*p != '\0')
89 goto out;
90 } else {
91 val = 1;
93 acpi_hdr.oem_revision = cpu_to_le32(val);
95 if (get_param_value(buf, sizeof(buf), "asl_compiler_id", t)) {
96 strncpy(acpi_hdr.asl_compiler_id, buf, 4);
97 } else {
98 strncpy(acpi_hdr.asl_compiler_id, dfl_id, 4);
101 if (get_param_value(buf, sizeof(buf), "asl_compiler_rev", t)) {
102 val = strtol(buf, &p, 10);
103 if(*p != '\0')
104 goto out;
105 } else {
106 val = 1;
108 acpi_hdr.asl_compiler_revision = cpu_to_le32(val);
110 if (!get_param_value(buf, sizeof(buf), "data", t)) {
111 buf[0] = '\0';
114 acpi_hdr.length = sizeof(acpi_hdr);
116 f = buf;
117 while (buf[0]) {
118 struct stat s;
119 char *n = strchr(f, ':');
120 if (n)
121 *n = '\0';
122 if(stat(f, &s) < 0) {
123 fprintf(stderr, "Can't stat file '%s': %s\n", f, strerror(errno));
124 goto out;
126 acpi_hdr.length += s.st_size;
127 if (!n)
128 break;
129 *n = ':';
130 f = n + 1;
133 if (!acpi_tables) {
134 acpi_tables_len = sizeof(uint16_t);
135 acpi_tables = qemu_mallocz(acpi_tables_len);
137 p = acpi_tables + acpi_tables_len;
138 acpi_tables_len += sizeof(uint16_t) + acpi_hdr.length;
139 acpi_tables = qemu_realloc(acpi_tables, acpi_tables_len);
141 acpi_hdr.length = cpu_to_le32(acpi_hdr.length);
142 *(uint16_t*)p = acpi_hdr.length;
143 p += sizeof(uint16_t);
144 memcpy(p, &acpi_hdr, sizeof(acpi_hdr));
145 off = sizeof(acpi_hdr);
147 f = buf;
148 while (buf[0]) {
149 struct stat s;
150 int fd;
151 char *n = strchr(f, ':');
152 if (n)
153 *n = '\0';
154 fd = open(f, O_RDONLY);
156 if(fd < 0)
157 goto out;
158 if(fstat(fd, &s) < 0) {
159 close(fd);
160 goto out;
163 do {
164 int r;
165 r = read(fd, p + off, s.st_size);
166 if (r > 0) {
167 off += r;
168 s.st_size -= r;
169 } else if ((r < 0 && errno != EINTR) || r == 0) {
170 close(fd);
171 goto out;
173 } while(s.st_size);
175 close(fd);
176 if (!n)
177 break;
178 f = n + 1;
181 ((struct acpi_table_header*)p)->checksum = acpi_checksum((uint8_t*)p, off);
182 /* increase number of tables */
183 (*(uint16_t*)acpi_tables) =
184 cpu_to_le32(le32_to_cpu(*(uint16_t*)acpi_tables) + 1);
185 return 0;
186 out:
187 if (acpi_tables) {
188 qemu_free(acpi_tables);
189 acpi_tables = NULL;
191 return -1;