acpihalt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2010 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB 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
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifdef GRUB_DSDT_TEST
  19. #include <stdio.h>
  20. #include <unistd.h>
  21. #include <stdlib.h>
  22. #include <stdint.h>
  23. #include <string.h>
  24. #include <errno.h>
  25. #define grub_dprintf(cond, args...) printf ( args )
  26. #define grub_printf printf
  27. #define grub_util_fopen fopen
  28. #define grub_memcmp memcmp
  29. typedef uint64_t grub_uint64_t;
  30. typedef uint32_t grub_uint32_t;
  31. typedef uint16_t grub_uint16_t;
  32. typedef uint8_t grub_uint8_t;
  33. #endif
  34. #include <grub/acpi.h>
  35. #ifndef GRUB_DSDT_TEST
  36. #include <grub/i18n.h>
  37. #else
  38. #define _(x) x
  39. #define N_(x) x
  40. #endif
  41. #ifndef GRUB_DSDT_TEST
  42. #include <grub/mm.h>
  43. #include <grub/misc.h>
  44. #include <grub/time.h>
  45. #include <grub/cpu/io.h>
  46. #endif
  47. static inline grub_uint32_t
  48. decode_length (const grub_uint8_t *ptr, int *numlen)
  49. {
  50. int num_bytes, i;
  51. grub_uint32_t ret;
  52. if (*ptr < 64)
  53. {
  54. if (numlen)
  55. *numlen = 1;
  56. return *ptr;
  57. }
  58. num_bytes = *ptr >> 6;
  59. if (numlen)
  60. *numlen = num_bytes + 1;
  61. ret = *ptr & 0xf;
  62. ptr++;
  63. for (i = 0; i < num_bytes; i++)
  64. {
  65. ret |= *ptr << (8 * i + 4);
  66. ptr++;
  67. }
  68. return ret;
  69. }
  70. static inline grub_uint32_t
  71. skip_name_string (const grub_uint8_t *ptr, const grub_uint8_t *end)
  72. {
  73. const grub_uint8_t *ptr0 = ptr;
  74. while (ptr < end && (*ptr == '^' || *ptr == '\\'))
  75. ptr++;
  76. switch (*ptr)
  77. {
  78. case '.':
  79. ptr++;
  80. ptr += 8;
  81. break;
  82. case '/':
  83. ptr++;
  84. ptr += 1 + (*ptr) * 4;
  85. break;
  86. case 0:
  87. ptr++;
  88. break;
  89. default:
  90. ptr += 4;
  91. break;
  92. }
  93. return ptr - ptr0;
  94. }
  95. static inline grub_uint32_t
  96. skip_data_ref_object (const grub_uint8_t *ptr, const grub_uint8_t *end)
  97. {
  98. grub_dprintf ("acpi", "data type = 0x%x\n", *ptr);
  99. switch (*ptr)
  100. {
  101. case GRUB_ACPI_OPCODE_PACKAGE:
  102. case GRUB_ACPI_OPCODE_BUFFER:
  103. return 1 + decode_length (ptr + 1, 0);
  104. case GRUB_ACPI_OPCODE_ZERO:
  105. case GRUB_ACPI_OPCODE_ONES:
  106. case GRUB_ACPI_OPCODE_ONE:
  107. return 1;
  108. case GRUB_ACPI_OPCODE_BYTE_CONST:
  109. return 2;
  110. case GRUB_ACPI_OPCODE_WORD_CONST:
  111. return 3;
  112. case GRUB_ACPI_OPCODE_DWORD_CONST:
  113. return 5;
  114. case GRUB_ACPI_OPCODE_STRING_CONST:
  115. {
  116. const grub_uint8_t *ptr0 = ptr;
  117. for (ptr++; ptr < end && *ptr; ptr++);
  118. if (ptr == end)
  119. return 0;
  120. return ptr - ptr0 + 1;
  121. }
  122. default:
  123. if (*ptr == '^' || *ptr == '\\' || *ptr == '_'
  124. || (*ptr >= 'A' && *ptr <= 'Z'))
  125. return skip_name_string (ptr, end);
  126. grub_printf ("Unknown opcode 0x%x\n", *ptr);
  127. return 0;
  128. }
  129. }
  130. static inline grub_uint32_t
  131. skip_term (const grub_uint8_t *ptr, const grub_uint8_t *end)
  132. {
  133. grub_uint32_t add;
  134. const grub_uint8_t *ptr0 = ptr;
  135. switch(*ptr)
  136. {
  137. case GRUB_ACPI_OPCODE_ADD:
  138. case GRUB_ACPI_OPCODE_AND:
  139. case GRUB_ACPI_OPCODE_CONCAT:
  140. case GRUB_ACPI_OPCODE_CONCATRES:
  141. case GRUB_ACPI_OPCODE_DIVIDE:
  142. case GRUB_ACPI_OPCODE_INDEX:
  143. case GRUB_ACPI_OPCODE_LSHIFT:
  144. case GRUB_ACPI_OPCODE_MOD:
  145. case GRUB_ACPI_OPCODE_MULTIPLY:
  146. case GRUB_ACPI_OPCODE_NAND:
  147. case GRUB_ACPI_OPCODE_NOR:
  148. case GRUB_ACPI_OPCODE_OR:
  149. case GRUB_ACPI_OPCODE_RSHIFT:
  150. case GRUB_ACPI_OPCODE_SUBTRACT:
  151. case GRUB_ACPI_OPCODE_TOSTRING:
  152. case GRUB_ACPI_OPCODE_XOR:
  153. /*
  154. * Parameters for these opcodes: TermArg, TermArg Target, see ACPI
  155. * spec r5.0, page 828f.
  156. */
  157. ptr++;
  158. ptr += add = skip_term (ptr, end);
  159. if (!add)
  160. return 0;
  161. ptr += add = skip_term (ptr, end);
  162. if (!add)
  163. return 0;
  164. ptr += skip_name_string (ptr, end);
  165. break;
  166. default:
  167. return skip_data_ref_object (ptr, end);
  168. }
  169. return ptr - ptr0;
  170. }
  171. static inline grub_uint32_t
  172. skip_ext_op (const grub_uint8_t *ptr, const grub_uint8_t *end)
  173. {
  174. const grub_uint8_t *ptr0 = ptr;
  175. int add;
  176. grub_dprintf ("acpi", "Extended opcode: 0x%x\n", *ptr);
  177. switch (*ptr)
  178. {
  179. case GRUB_ACPI_EXTOPCODE_MUTEX:
  180. ptr++;
  181. ptr += skip_name_string (ptr, end);
  182. ptr++;
  183. break;
  184. case GRUB_ACPI_EXTOPCODE_EVENT_OP:
  185. ptr++;
  186. ptr += skip_name_string (ptr, end);
  187. break;
  188. case GRUB_ACPI_EXTOPCODE_OPERATION_REGION:
  189. ptr++;
  190. ptr += skip_name_string (ptr, end);
  191. ptr++;
  192. ptr += add = skip_term (ptr, end);
  193. if (!add)
  194. return 0;
  195. ptr += add = skip_term (ptr, end);
  196. if (!add)
  197. return 0;
  198. break;
  199. case GRUB_ACPI_EXTOPCODE_FIELD_OP:
  200. case GRUB_ACPI_EXTOPCODE_DEVICE_OP:
  201. case GRUB_ACPI_EXTOPCODE_PROCESSOR_OP:
  202. case GRUB_ACPI_EXTOPCODE_POWER_RES_OP:
  203. case GRUB_ACPI_EXTOPCODE_THERMAL_ZONE_OP:
  204. case GRUB_ACPI_EXTOPCODE_INDEX_FIELD_OP:
  205. case GRUB_ACPI_EXTOPCODE_BANK_FIELD_OP:
  206. ptr++;
  207. ptr += decode_length (ptr, 0);
  208. break;
  209. default:
  210. grub_printf ("Unexpected extended opcode: 0x%x\n", *ptr);
  211. return 0;
  212. }
  213. return ptr - ptr0;
  214. }
  215. static int
  216. get_sleep_type (grub_uint8_t *table, grub_uint8_t *ptr, grub_uint8_t *end,
  217. grub_uint8_t *scope, int scope_len)
  218. {
  219. grub_uint8_t *prev = table;
  220. if (!ptr)
  221. ptr = table + sizeof (struct grub_acpi_table_header);
  222. while (ptr < end && prev < ptr)
  223. {
  224. int add;
  225. prev = ptr;
  226. grub_dprintf ("acpi", "Opcode 0x%x\n", *ptr);
  227. grub_dprintf ("acpi", "Tell %x\n", (unsigned) (ptr - table));
  228. switch (*ptr)
  229. {
  230. case GRUB_ACPI_OPCODE_EXTOP:
  231. ptr++;
  232. ptr += add = skip_ext_op (ptr, end);
  233. if (!add)
  234. return -1;
  235. break;
  236. case GRUB_ACPI_OPCODE_CREATE_DWORD_FIELD:
  237. case GRUB_ACPI_OPCODE_CREATE_WORD_FIELD:
  238. case GRUB_ACPI_OPCODE_CREATE_BYTE_FIELD:
  239. {
  240. ptr += 5;
  241. ptr += add = skip_data_ref_object (ptr, end);
  242. if (!add)
  243. return -1;
  244. ptr += 4;
  245. break;
  246. }
  247. case GRUB_ACPI_OPCODE_NAME:
  248. ptr++;
  249. if ((!scope || grub_memcmp (scope, "\\", scope_len) == 0) &&
  250. (grub_memcmp (ptr, "_S5_", 4) == 0 || grub_memcmp (ptr, "\\_S5_", 4) == 0))
  251. {
  252. int ll;
  253. grub_uint8_t *ptr2 = ptr;
  254. grub_dprintf ("acpi", "S5 found\n");
  255. ptr2 += skip_name_string (ptr, end);
  256. if (*ptr2 != 0x12)
  257. {
  258. grub_printf ("Unknown opcode in _S5: 0x%x\n", *ptr2);
  259. return -1;
  260. }
  261. ptr2++;
  262. decode_length (ptr2, &ll);
  263. ptr2 += ll;
  264. ptr2++;
  265. switch (*ptr2)
  266. {
  267. case GRUB_ACPI_OPCODE_ZERO:
  268. return 0;
  269. case GRUB_ACPI_OPCODE_ONE:
  270. return 1;
  271. case GRUB_ACPI_OPCODE_BYTE_CONST:
  272. return ptr2[1];
  273. default:
  274. grub_printf ("Unknown data type in _S5: 0x%x\n", *ptr2);
  275. return -1;
  276. }
  277. }
  278. ptr += add = skip_name_string (ptr, end);
  279. if (!add)
  280. return -1;
  281. ptr += add = skip_data_ref_object (ptr, end);
  282. if (!add)
  283. return -1;
  284. break;
  285. case GRUB_ACPI_OPCODE_ALIAS:
  286. ptr++;
  287. /* We need to skip two name strings */
  288. ptr += add = skip_name_string (ptr, end);
  289. if (!add)
  290. return -1;
  291. ptr += add = skip_name_string (ptr, end);
  292. if (!add)
  293. return -1;
  294. break;
  295. case GRUB_ACPI_OPCODE_SCOPE:
  296. {
  297. int scope_sleep_type;
  298. int ll;
  299. grub_uint8_t *name;
  300. int name_len;
  301. ptr++;
  302. add = decode_length (ptr, &ll);
  303. name = ptr + ll;
  304. name_len = skip_name_string (name, ptr + add);
  305. if (!name_len)
  306. return -1;
  307. scope_sleep_type = get_sleep_type (table, name + name_len,
  308. ptr + add, name, name_len);
  309. if (scope_sleep_type != -2)
  310. return scope_sleep_type;
  311. ptr += add;
  312. break;
  313. }
  314. case GRUB_ACPI_OPCODE_IF:
  315. case GRUB_ACPI_OPCODE_METHOD:
  316. {
  317. ptr++;
  318. ptr += decode_length (ptr, 0);
  319. break;
  320. }
  321. default:
  322. grub_printf ("Unknown opcode 0x%x\n", *ptr);
  323. return -1;
  324. }
  325. }
  326. return -2;
  327. }
  328. #ifdef GRUB_DSDT_TEST
  329. int
  330. main (int argc, char **argv)
  331. {
  332. FILE *f;
  333. size_t len;
  334. unsigned char *buf;
  335. if (argc < 2)
  336. printf ("Usage: %s FILE\n", argv[0]);
  337. f = grub_util_fopen (argv[1], "rb");
  338. if (!f)
  339. {
  340. printf ("Couldn't open file\n");
  341. return 1;
  342. }
  343. fseek (f, 0, SEEK_END);
  344. len = ftell (f);
  345. fseek (f, 0, SEEK_SET);
  346. buf = malloc (len);
  347. if (!buf)
  348. {
  349. printf (_("error: %s.\n"), _("out of memory"));
  350. fclose (f);
  351. return 2;
  352. }
  353. if (fread (buf, 1, len, f) != len)
  354. {
  355. printf (_("cannot read `%s': %s"), argv[1], strerror (errno));
  356. free (buf);
  357. fclose (f);
  358. return 2;
  359. }
  360. printf ("Sleep type = %d\n", get_sleep_type (buf, NULL, buf + len, NULL, 0));
  361. free (buf);
  362. fclose (f);
  363. return 0;
  364. }
  365. #else
  366. void
  367. grub_acpi_halt (void)
  368. {
  369. struct grub_acpi_rsdp_v20 *rsdp2;
  370. struct grub_acpi_rsdp_v10 *rsdp1;
  371. struct grub_acpi_table_header *rsdt;
  372. grub_uint32_t *entry_ptr;
  373. grub_uint32_t port = 0;
  374. int sleep_type = -1;
  375. rsdp2 = grub_acpi_get_rsdpv2 ();
  376. if (rsdp2)
  377. rsdp1 = &(rsdp2->rsdpv1);
  378. else
  379. rsdp1 = grub_acpi_get_rsdpv1 ();
  380. grub_dprintf ("acpi", "rsdp1=%p\n", rsdp1);
  381. if (!rsdp1)
  382. return;
  383. rsdt = (struct grub_acpi_table_header *) (grub_addr_t) rsdp1->rsdt_addr;
  384. for (entry_ptr = (grub_uint32_t *) (rsdt + 1);
  385. entry_ptr < (grub_uint32_t *) (((grub_uint8_t *) rsdt)
  386. + rsdt->length);
  387. entry_ptr++)
  388. {
  389. if (grub_memcmp ((void *) (grub_addr_t) *entry_ptr, "FACP", 4) == 0)
  390. {
  391. struct grub_acpi_fadt *fadt
  392. = ((struct grub_acpi_fadt *) (grub_addr_t) *entry_ptr);
  393. struct grub_acpi_table_header *dsdt
  394. = (struct grub_acpi_table_header *) (grub_addr_t) fadt->dsdt_addr;
  395. grub_uint8_t *buf = (grub_uint8_t *) dsdt;
  396. port = fadt->pm1a;
  397. grub_dprintf ("acpi", "PM1a port=%x\n", port);
  398. if (grub_memcmp (dsdt->signature, "DSDT",
  399. sizeof (dsdt->signature)) == 0
  400. && sleep_type < 0)
  401. sleep_type = get_sleep_type (buf, NULL, buf + dsdt->length,
  402. NULL, 0);
  403. }
  404. else if (grub_memcmp ((void *) (grub_addr_t) *entry_ptr, "SSDT", 4) == 0
  405. && sleep_type < 0)
  406. {
  407. struct grub_acpi_table_header *ssdt
  408. = (struct grub_acpi_table_header *) (grub_addr_t) *entry_ptr;
  409. grub_uint8_t *buf = (grub_uint8_t *) ssdt;
  410. grub_dprintf ("acpi", "SSDT = %p\n", ssdt);
  411. sleep_type = get_sleep_type (buf, NULL, buf + ssdt->length, NULL, 0);
  412. }
  413. }
  414. grub_dprintf ("acpi", "SLP_TYP = %d, port = 0x%x\n", sleep_type, port);
  415. if (port && sleep_type >= 0 && sleep_type < 8)
  416. grub_outw (GRUB_ACPI_SLP_EN | (sleep_type << GRUB_ACPI_SLP_TYP_OFFSET),
  417. port & 0xffff);
  418. grub_millisleep (1500);
  419. /* TRANSLATORS: It's computer shutdown using ACPI, not disabling ACPI. */
  420. grub_puts_ (N_("ACPI shutdown failed"));
  421. }
  422. #endif