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/>
22 struct acpi_table_header
24 char signature
[4]; /* ACPI signature (4 ASCII characters) */
25 uint32_t length
; /* Length of table, in bytes, including header */
26 uint8_t revision
; /* ACPI Specification minor version # */
27 uint8_t checksum
; /* To make sum of entire table == 0 */
28 char oem_id
[6]; /* OEM identification */
29 char oem_table_id
[8]; /* OEM table identification */
30 uint32_t oem_revision
; /* OEM revision number */
31 char asl_compiler_id
[4]; /* ASL compiler vendor ID */
32 uint32_t asl_compiler_revision
; /* ASL compiler revision number */
33 } __attribute__((packed
));
36 size_t acpi_tables_len
;
38 static int acpi_checksum(const uint8_t *data
, int len
)
42 for(i
= 0; i
< len
; i
++)
47 int acpi_table_add(const char *t
)
49 static const char *dfl_id
= "QEMUQEMU";
50 char buf
[1024], *p
, *f
;
51 struct acpi_table_header acpi_hdr
;
54 struct acpi_table_header
*acpi_hdr_p
;
57 memset(&acpi_hdr
, 0, sizeof(acpi_hdr
));
59 if (get_param_value(buf
, sizeof(buf
), "sig", t
)) {
60 strncpy(acpi_hdr
.signature
, buf
, 4);
62 strncpy(acpi_hdr
.signature
, dfl_id
, 4);
64 if (get_param_value(buf
, sizeof(buf
), "rev", t
)) {
65 val
= strtoul(buf
, &p
, 10);
66 if (val
> 255 || *p
!= '\0')
71 acpi_hdr
.revision
= (int8_t)val
;
73 if (get_param_value(buf
, sizeof(buf
), "oem_id", t
)) {
74 strncpy(acpi_hdr
.oem_id
, buf
, 6);
76 strncpy(acpi_hdr
.oem_id
, dfl_id
, 6);
79 if (get_param_value(buf
, sizeof(buf
), "oem_table_id", t
)) {
80 strncpy(acpi_hdr
.oem_table_id
, buf
, 8);
82 strncpy(acpi_hdr
.oem_table_id
, dfl_id
, 8);
85 if (get_param_value(buf
, sizeof(buf
), "oem_rev", t
)) {
86 val
= strtol(buf
, &p
, 10);
92 acpi_hdr
.oem_revision
= cpu_to_le32(val
);
94 if (get_param_value(buf
, sizeof(buf
), "asl_compiler_id", t
)) {
95 strncpy(acpi_hdr
.asl_compiler_id
, buf
, 4);
97 strncpy(acpi_hdr
.asl_compiler_id
, dfl_id
, 4);
100 if (get_param_value(buf
, sizeof(buf
), "asl_compiler_rev", t
)) {
101 val
= strtol(buf
, &p
, 10);
107 acpi_hdr
.asl_compiler_revision
= cpu_to_le32(val
);
109 if (!get_param_value(buf
, sizeof(buf
), "data", t
)) {
113 length
= sizeof(acpi_hdr
);
118 char *n
= strchr(f
, ':');
121 if(stat(f
, &s
) < 0) {
122 fprintf(stderr
, "Can't stat file '%s': %s\n", f
, strerror(errno
));
133 acpi_tables_len
= sizeof(uint16_t);
134 acpi_tables
= qemu_mallocz(acpi_tables_len
);
136 acpi_tables
= qemu_realloc(acpi_tables
,
137 acpi_tables_len
+ sizeof(uint16_t) + length
);
138 p
= acpi_tables
+ acpi_tables_len
;
139 acpi_tables_len
+= sizeof(uint16_t) + length
;
141 *(uint16_t*)p
= cpu_to_le32(length
);
142 p
+= sizeof(uint16_t);
143 memcpy(p
, &acpi_hdr
, sizeof(acpi_hdr
));
144 off
= sizeof(acpi_hdr
);
150 char *n
= strchr(f
, ':');
153 fd
= open(f
, O_RDONLY
);
157 if(fstat(fd
, &s
) < 0) {
162 /* off < length is necessary because file size can be changed
164 while(s
.st_size
&& off
< length
); {
166 r
= read(fd
, p
+ off
, s
.st_size
);
170 } else if ((r
< 0 && errno
!= EINTR
) || r
== 0) {
182 /* don't pass random value in process to guest */
183 memset(p
+ off
, 0, length
- off
);
186 acpi_hdr_p
= (struct acpi_table_header
*)p
;
187 acpi_hdr_p
->length
= cpu_to_le32(length
);
188 acpi_hdr_p
->checksum
= acpi_checksum((uint8_t*)p
, length
);
189 /* increase number of tables */
190 (*(uint16_t*)acpi_tables
) =
191 cpu_to_le32(le32_to_cpu(*(uint16_t*)acpi_tables
) + 1);
195 qemu_free(acpi_tables
);