acpi.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. /* acpi.c - modify acpi tables. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2009 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/dl.h>
  20. #include <grub/extcmd.h>
  21. #include <grub/file.h>
  22. #include <grub/disk.h>
  23. #include <grub/term.h>
  24. #include <grub/misc.h>
  25. #include <grub/acpi.h>
  26. #include <grub/mm.h>
  27. #include <grub/memory.h>
  28. #include <grub/i18n.h>
  29. #ifdef GRUB_MACHINE_EFI
  30. #include <grub/efi/efi.h>
  31. #include <grub/efi/api.h>
  32. #endif
  33. #pragma GCC diagnostic ignored "-Wcast-align"
  34. GRUB_MOD_LICENSE ("GPLv3+");
  35. static const struct grub_arg_option options[] = {
  36. {"exclude", 'x', 0,
  37. N_("Don't load host tables specified by comma-separated list."),
  38. 0, ARG_TYPE_STRING},
  39. {"load-only", 'n', 0,
  40. N_("Load only tables specified by comma-separated list."), 0, ARG_TYPE_STRING},
  41. {"v1", '1', 0, N_("Export version 1 tables to the OS."), 0, ARG_TYPE_NONE},
  42. {"v2", '2', 0, N_("Export version 2 and version 3 tables to the OS."), 0, ARG_TYPE_NONE},
  43. {"oemid", 'o', 0, N_("Set OEMID of RSDP, XSDT and RSDT."), 0, ARG_TYPE_STRING},
  44. {"oemtable", 't', 0,
  45. N_("Set OEMTABLE ID of RSDP, XSDT and RSDT."), 0, ARG_TYPE_STRING},
  46. {"oemtablerev", 'r', 0,
  47. N_("Set OEMTABLE revision of RSDP, XSDT and RSDT."), 0, ARG_TYPE_INT},
  48. {"oemtablecreator", 'c', 0,
  49. N_("Set creator field of RSDP, XSDT and RSDT."), 0, ARG_TYPE_STRING},
  50. {"oemtablecreatorrev", 'd', 0,
  51. N_("Set creator revision of RSDP, XSDT and RSDT."), 0, ARG_TYPE_INT},
  52. /* TRANSLATORS: "hangs" here is a noun, not a verb. */
  53. {"no-ebda", 'e', 0, N_("Don't update EBDA. May fix failures or hangs on some "
  54. "BIOSes but makes it ineffective with OS not receiving RSDP from GRUB."),
  55. 0, ARG_TYPE_NONE},
  56. {0, 0, 0, 0, 0, 0}
  57. };
  58. /* rev1 is 1 if ACPIv1 is to be generated, 0 otherwise.
  59. rev2 contains the revision of ACPIv2+ to generate or 0 if none. */
  60. static int rev1, rev2;
  61. /* OEMID of RSDP, RSDT and XSDT. */
  62. static char root_oemid[6];
  63. /* OEMTABLE of the same tables. */
  64. static char root_oemtable[8];
  65. /* OEMREVISION of the same tables. */
  66. static grub_uint32_t root_oemrev;
  67. /* CreatorID of the same tables. */
  68. static char root_creator_id[4];
  69. /* CreatorRevision of the same tables. */
  70. static grub_uint32_t root_creator_rev;
  71. static struct grub_acpi_rsdp_v10 *rsdpv1_new = 0;
  72. static struct grub_acpi_rsdp_v20 *rsdpv2_new = 0;
  73. static char *playground = 0, *playground_ptr = 0;
  74. static int playground_size = 0;
  75. /* Linked list of ACPI tables. */
  76. struct efiemu_acpi_table
  77. {
  78. void *addr;
  79. grub_size_t size;
  80. struct efiemu_acpi_table *next;
  81. };
  82. static struct efiemu_acpi_table *acpi_tables = 0;
  83. /* DSDT isn't in RSDT. So treat it specially. */
  84. static void *table_dsdt = 0;
  85. /* Pointer to recreated RSDT. */
  86. static void *rsdt_addr = 0;
  87. /* Allocation handles for different tables. */
  88. static grub_size_t dsdt_size = 0;
  89. /* Address of original FACS. */
  90. static grub_uint32_t facs_addr = 0;
  91. struct grub_acpi_rsdp_v20 *
  92. grub_acpi_get_rsdpv2 (void)
  93. {
  94. if (rsdpv2_new)
  95. return rsdpv2_new;
  96. if (rsdpv1_new)
  97. return 0;
  98. return grub_machine_acpi_get_rsdpv2 ();
  99. }
  100. struct grub_acpi_rsdp_v10 *
  101. grub_acpi_get_rsdpv1 (void)
  102. {
  103. if (rsdpv1_new)
  104. return rsdpv1_new;
  105. if (rsdpv2_new)
  106. return 0;
  107. return grub_machine_acpi_get_rsdpv1 ();
  108. }
  109. #if defined (__i386__) || defined (__x86_64__)
  110. static inline int
  111. iszero (grub_uint8_t *reg, int size)
  112. {
  113. int i;
  114. for (i = 0; i < size; i++)
  115. if (reg[i])
  116. return 0;
  117. return 1;
  118. }
  119. /* Context for grub_acpi_create_ebda. */
  120. struct grub_acpi_create_ebda_ctx {
  121. int ebda_len;
  122. grub_uint64_t highestlow;
  123. };
  124. /* Helper for grub_acpi_create_ebda. */
  125. static int
  126. find_hook (grub_uint64_t start, grub_uint64_t size, grub_memory_type_t type,
  127. void *data)
  128. {
  129. struct grub_acpi_create_ebda_ctx *ctx = data;
  130. grub_uint64_t end = start + size;
  131. if (type != GRUB_MEMORY_AVAILABLE)
  132. return 0;
  133. if (end > 0x100000)
  134. end = 0x100000;
  135. if (end > start + ctx->ebda_len
  136. && ctx->highestlow < ((end - ctx->ebda_len) & (~0xf)) )
  137. ctx->highestlow = (end - ctx->ebda_len) & (~0xf);
  138. return 0;
  139. }
  140. grub_err_t
  141. grub_acpi_create_ebda (void)
  142. {
  143. struct grub_acpi_create_ebda_ctx ctx = {
  144. .highestlow = 0
  145. };
  146. int ebda_kb_len = 0;
  147. int mmapregion = 0;
  148. grub_uint8_t *ebda, *v1inebda = 0, *v2inebda = 0;
  149. grub_uint8_t *targetebda, *target;
  150. struct grub_acpi_rsdp_v10 *v1;
  151. struct grub_acpi_rsdp_v20 *v2;
  152. ebda = (grub_uint8_t *) (grub_addr_t) ((*((grub_uint16_t *)0x40e)) << 4);
  153. grub_dprintf ("acpi", "EBDA @%p\n", ebda);
  154. if (ebda)
  155. ebda_kb_len = *(grub_uint16_t *) ebda;
  156. grub_dprintf ("acpi", "EBDA length 0x%x\n", ebda_kb_len);
  157. if (ebda_kb_len > 16)
  158. ebda_kb_len = 0;
  159. ctx.ebda_len = (ebda_kb_len + 1) << 10;
  160. /* FIXME: use low-memory mm allocation once it's available. */
  161. grub_mmap_iterate (find_hook, &ctx);
  162. targetebda = (grub_uint8_t *) (grub_addr_t) ctx.highestlow;
  163. grub_dprintf ("acpi", "creating ebda @%llx\n",
  164. (unsigned long long) ctx.highestlow);
  165. if (! ctx.highestlow)
  166. return grub_error (GRUB_ERR_OUT_OF_MEMORY,
  167. "couldn't find space for the new EBDA");
  168. mmapregion = grub_mmap_register ((grub_addr_t) targetebda, ctx.ebda_len,
  169. GRUB_MEMORY_RESERVED);
  170. if (! mmapregion)
  171. return grub_errno;
  172. /* XXX: EBDA is unstandardized, so this implementation is heuristical. */
  173. if (ebda_kb_len)
  174. grub_memcpy (targetebda, ebda, 0x400);
  175. else
  176. grub_memset (targetebda, 0, 0x400);
  177. *((grub_uint16_t *) targetebda) = ebda_kb_len + 1;
  178. target = targetebda;
  179. v1 = grub_acpi_get_rsdpv1 ();
  180. v2 = grub_acpi_get_rsdpv2 ();
  181. if (v2 && v2->length > 40)
  182. v2 = 0;
  183. /* First try to replace already existing rsdp. */
  184. if (v2)
  185. {
  186. grub_dprintf ("acpi", "Scanning EBDA for old rsdpv2\n");
  187. for (; target < targetebda + 0x400 - v2->length; target += 0x10)
  188. if (grub_memcmp (target, GRUB_RSDP_SIGNATURE, GRUB_RSDP_SIGNATURE_SIZE) == 0
  189. && grub_byte_checksum (target,
  190. sizeof (struct grub_acpi_rsdp_v10)) == 0
  191. && ((struct grub_acpi_rsdp_v10 *) target)->revision != 0
  192. && ((struct grub_acpi_rsdp_v20 *) target)->length <= v2->length)
  193. {
  194. grub_memcpy (target, v2, v2->length);
  195. grub_dprintf ("acpi", "Copying rsdpv2 to %p\n", target);
  196. v2inebda = target;
  197. target += v2->length;
  198. target = (grub_uint8_t *) ALIGN_UP((grub_addr_t) target, 16);
  199. v2 = 0;
  200. break;
  201. }
  202. }
  203. if (v1)
  204. {
  205. grub_dprintf ("acpi", "Scanning EBDA for old rsdpv1\n");
  206. for (; target < targetebda + 0x400 - sizeof (struct grub_acpi_rsdp_v10);
  207. target += 0x10)
  208. if (grub_memcmp (target, GRUB_RSDP_SIGNATURE, GRUB_RSDP_SIGNATURE_SIZE) == 0
  209. && grub_byte_checksum (target,
  210. sizeof (struct grub_acpi_rsdp_v10)) == 0)
  211. {
  212. grub_memcpy (target, v1, sizeof (struct grub_acpi_rsdp_v10));
  213. grub_dprintf ("acpi", "Copying rsdpv1 to %p\n", target);
  214. v1inebda = target;
  215. target += sizeof (struct grub_acpi_rsdp_v10);
  216. target = (grub_uint8_t *) ALIGN_UP((grub_addr_t) target, 16);
  217. v1 = 0;
  218. break;
  219. }
  220. }
  221. target = targetebda + 0x100;
  222. /* Try contiguous zeros. */
  223. if (v2)
  224. {
  225. grub_dprintf ("acpi", "Scanning EBDA for block of zeros\n");
  226. for (; target < targetebda + 0x400 - v2->length; target += 0x10)
  227. if (iszero (target, v2->length))
  228. {
  229. grub_dprintf ("acpi", "Copying rsdpv2 to %p\n", target);
  230. grub_memcpy (target, v2, v2->length);
  231. v2inebda = target;
  232. target += v2->length;
  233. target = (grub_uint8_t *) ALIGN_UP((grub_addr_t) target, 16);
  234. v2 = 0;
  235. break;
  236. }
  237. }
  238. if (v1)
  239. {
  240. grub_dprintf ("acpi", "Scanning EBDA for block of zeros\n");
  241. for (; target < targetebda + 0x400 - sizeof (struct grub_acpi_rsdp_v10);
  242. target += 0x10)
  243. if (iszero (target, sizeof (struct grub_acpi_rsdp_v10)))
  244. {
  245. grub_dprintf ("acpi", "Copying rsdpv1 to %p\n", target);
  246. grub_memcpy (target, v1, sizeof (struct grub_acpi_rsdp_v10));
  247. v1inebda = target;
  248. target += sizeof (struct grub_acpi_rsdp_v10);
  249. target = (grub_uint8_t *) ALIGN_UP((grub_addr_t) target, 16);
  250. v1 = 0;
  251. break;
  252. }
  253. }
  254. if (v1 || v2)
  255. {
  256. grub_mmap_unregister (mmapregion);
  257. return grub_error (GRUB_ERR_OUT_OF_MEMORY,
  258. "couldn't find suitable spot in EBDA");
  259. }
  260. /* Remove any other RSDT. */
  261. for (target = targetebda;
  262. target < targetebda + 0x400 - sizeof (struct grub_acpi_rsdp_v10);
  263. target += 0x10)
  264. if (grub_memcmp (target, GRUB_RSDP_SIGNATURE, GRUB_RSDP_SIGNATURE_SIZE) == 0
  265. && grub_byte_checksum (target,
  266. sizeof (struct grub_acpi_rsdp_v10)) == 0
  267. && target != v1inebda && target != v2inebda)
  268. *target = 0;
  269. grub_dprintf ("acpi", "Switching EBDA\n");
  270. (*((grub_uint16_t *) 0x40e)) = ((grub_addr_t) targetebda) >> 4;
  271. grub_dprintf ("acpi", "EBDA switched\n");
  272. return GRUB_ERR_NONE;
  273. }
  274. #endif
  275. /* Create tables common to ACPIv1 and ACPIv2+ */
  276. static void
  277. setup_common_tables (void)
  278. {
  279. struct efiemu_acpi_table *cur;
  280. struct grub_acpi_table_header *rsdt;
  281. grub_uint32_t *rsdt_entry;
  282. int numoftables;
  283. /* Treat DSDT. */
  284. grub_memcpy (playground_ptr, table_dsdt, dsdt_size);
  285. grub_free (table_dsdt);
  286. table_dsdt = playground_ptr;
  287. playground_ptr += dsdt_size;
  288. /* Treat other tables. */
  289. for (cur = acpi_tables; cur; cur = cur->next)
  290. {
  291. struct grub_acpi_fadt *fadt;
  292. grub_memcpy (playground_ptr, cur->addr, cur->size);
  293. grub_free (cur->addr);
  294. cur->addr = playground_ptr;
  295. playground_ptr += cur->size;
  296. /* If it's FADT correct DSDT and FACS addresses. */
  297. fadt = (struct grub_acpi_fadt *) cur->addr;
  298. if (grub_memcmp (fadt->hdr.signature, GRUB_ACPI_FADT_SIGNATURE,
  299. sizeof (fadt->hdr.signature)) == 0)
  300. {
  301. fadt->dsdt_addr = (grub_addr_t) table_dsdt;
  302. fadt->facs_addr = facs_addr;
  303. /* Does a revision 2 exist at all? */
  304. if (fadt->hdr.revision >= 3)
  305. {
  306. fadt->dsdt_xaddr = (grub_addr_t) table_dsdt;
  307. fadt->facs_xaddr = facs_addr;
  308. }
  309. /* Recompute checksum. */
  310. fadt->hdr.checksum = 0;
  311. fadt->hdr.checksum = 1 + ~grub_byte_checksum (fadt, fadt->hdr.length);
  312. }
  313. }
  314. /* Fill RSDT entries. */
  315. numoftables = 0;
  316. for (cur = acpi_tables; cur; cur = cur->next)
  317. numoftables++;
  318. rsdt_addr = rsdt = (struct grub_acpi_table_header *) playground_ptr;
  319. playground_ptr += sizeof (struct grub_acpi_table_header) + sizeof (grub_uint32_t) * numoftables;
  320. rsdt_entry = (grub_uint32_t *) (rsdt + 1);
  321. /* Fill RSDT header. */
  322. grub_memcpy (&(rsdt->signature), "RSDT", 4);
  323. rsdt->length = sizeof (struct grub_acpi_table_header) + sizeof (grub_uint32_t) * numoftables;
  324. rsdt->revision = 1;
  325. grub_memcpy (&(rsdt->oemid), root_oemid, sizeof (rsdt->oemid));
  326. grub_memcpy (&(rsdt->oemtable), root_oemtable, sizeof (rsdt->oemtable));
  327. rsdt->oemrev = root_oemrev;
  328. grub_memcpy (&(rsdt->creator_id), root_creator_id, sizeof (rsdt->creator_id));
  329. rsdt->creator_rev = root_creator_rev;
  330. for (cur = acpi_tables; cur; cur = cur->next)
  331. *(rsdt_entry++) = (grub_addr_t) cur->addr;
  332. /* Recompute checksum. */
  333. rsdt->checksum = 0;
  334. rsdt->checksum = 1 + ~grub_byte_checksum (rsdt, rsdt->length);
  335. }
  336. /* Regenerate ACPIv1 RSDP */
  337. static void
  338. setv1table (void)
  339. {
  340. /* Create RSDP. */
  341. rsdpv1_new = (struct grub_acpi_rsdp_v10 *) playground_ptr;
  342. playground_ptr += sizeof (struct grub_acpi_rsdp_v10);
  343. grub_memcpy (&(rsdpv1_new->signature), GRUB_RSDP_SIGNATURE,
  344. sizeof (rsdpv1_new->signature));
  345. grub_memcpy (&(rsdpv1_new->oemid), root_oemid, sizeof (rsdpv1_new->oemid));
  346. rsdpv1_new->revision = 0;
  347. rsdpv1_new->rsdt_addr = (grub_addr_t) rsdt_addr;
  348. rsdpv1_new->checksum = 0;
  349. rsdpv1_new->checksum = 1 + ~grub_byte_checksum (rsdpv1_new,
  350. sizeof (*rsdpv1_new));
  351. grub_dprintf ("acpi", "Generated ACPIv1 tables\n");
  352. }
  353. static void
  354. setv2table (void)
  355. {
  356. struct grub_acpi_table_header *xsdt;
  357. struct efiemu_acpi_table *cur;
  358. grub_uint64_t *xsdt_entry;
  359. int numoftables;
  360. numoftables = 0;
  361. for (cur = acpi_tables; cur; cur = cur->next)
  362. numoftables++;
  363. /* Create XSDT. */
  364. xsdt = (struct grub_acpi_table_header *) playground_ptr;
  365. playground_ptr += sizeof (struct grub_acpi_table_header) + sizeof (grub_uint64_t) * numoftables;
  366. xsdt_entry = (grub_uint64_t *)(xsdt + 1);
  367. for (cur = acpi_tables; cur; cur = cur->next)
  368. *(xsdt_entry++) = (grub_addr_t) cur->addr;
  369. grub_memcpy (&(xsdt->signature), "XSDT", 4);
  370. xsdt->length = sizeof (struct grub_acpi_table_header) + sizeof (grub_uint64_t) * numoftables;
  371. xsdt->revision = 1;
  372. grub_memcpy (&(xsdt->oemid), root_oemid, sizeof (xsdt->oemid));
  373. grub_memcpy (&(xsdt->oemtable), root_oemtable, sizeof (xsdt->oemtable));
  374. xsdt->oemrev = root_oemrev;
  375. grub_memcpy (&(xsdt->creator_id), root_creator_id, sizeof (xsdt->creator_id));
  376. xsdt->creator_rev = root_creator_rev;
  377. xsdt->checksum = 0;
  378. xsdt->checksum = 1 + ~grub_byte_checksum (xsdt, xsdt->length);
  379. /* Create RSDPv2. */
  380. rsdpv2_new = (struct grub_acpi_rsdp_v20 *) playground_ptr;
  381. playground_ptr += sizeof (struct grub_acpi_rsdp_v20);
  382. grub_memcpy (&(rsdpv2_new->rsdpv1.signature), GRUB_RSDP_SIGNATURE,
  383. sizeof (rsdpv2_new->rsdpv1.signature));
  384. grub_memcpy (&(rsdpv2_new->rsdpv1.oemid), root_oemid,
  385. sizeof (rsdpv2_new->rsdpv1.oemid));
  386. rsdpv2_new->rsdpv1.revision = rev2;
  387. rsdpv2_new->rsdpv1.rsdt_addr = (grub_addr_t) rsdt_addr;
  388. rsdpv2_new->rsdpv1.checksum = 0;
  389. rsdpv2_new->rsdpv1.checksum = 1 + ~grub_byte_checksum
  390. (&(rsdpv2_new->rsdpv1), sizeof (rsdpv2_new->rsdpv1));
  391. rsdpv2_new->length = sizeof (*rsdpv2_new);
  392. rsdpv2_new->xsdt_addr = (grub_addr_t) xsdt;
  393. rsdpv2_new->checksum = 0;
  394. rsdpv2_new->checksum = 1 + ~grub_byte_checksum (rsdpv2_new,
  395. rsdpv2_new->length);
  396. grub_dprintf ("acpi", "Generated ACPIv2 tables\n");
  397. }
  398. static void
  399. free_tables (void)
  400. {
  401. struct efiemu_acpi_table *cur, *t;
  402. if (table_dsdt)
  403. grub_free (table_dsdt);
  404. for (cur = acpi_tables; cur;)
  405. {
  406. t = cur;
  407. grub_free (cur->addr);
  408. cur = cur->next;
  409. grub_free (t);
  410. }
  411. acpi_tables = 0;
  412. table_dsdt = 0;
  413. }
  414. static grub_err_t
  415. grub_cmd_acpi (struct grub_extcmd_context *ctxt, int argc, char **args)
  416. {
  417. struct grub_arg_list *state = ctxt->state;
  418. struct grub_acpi_rsdp_v10 *rsdp;
  419. struct efiemu_acpi_table *cur, *t;
  420. int i, mmapregion;
  421. int numoftables;
  422. /* Default values if no RSDP is found. */
  423. rev1 = 1;
  424. rev2 = 3;
  425. facs_addr = 0;
  426. playground = playground_ptr = 0;
  427. playground_size = 0;
  428. rsdp = (struct grub_acpi_rsdp_v10 *) grub_machine_acpi_get_rsdpv2 ();
  429. if (! rsdp)
  430. rsdp = grub_machine_acpi_get_rsdpv1 ();
  431. grub_dprintf ("acpi", "RSDP @%p\n", rsdp);
  432. if (rsdp)
  433. {
  434. grub_uint32_t *entry_ptr;
  435. char *exclude = 0;
  436. char *load_only = 0;
  437. char *ptr;
  438. /* RSDT consists of header and an array of 32-bit pointers. */
  439. struct grub_acpi_table_header *rsdt;
  440. exclude = state[0].set ? grub_strdup (state[0].arg) : 0;
  441. if (exclude)
  442. {
  443. for (ptr = exclude; *ptr; ptr++)
  444. *ptr = grub_tolower (*ptr);
  445. }
  446. load_only = state[1].set ? grub_strdup (state[1].arg) : 0;
  447. if (load_only)
  448. {
  449. for (ptr = load_only; *ptr; ptr++)
  450. *ptr = grub_tolower (*ptr);
  451. }
  452. /* Set revision variables to replicate the same version as host. */
  453. rev1 = ! rsdp->revision;
  454. rev2 = rsdp->revision;
  455. rsdt = (struct grub_acpi_table_header *) (grub_addr_t) rsdp->rsdt_addr;
  456. /* Load host tables. */
  457. for (entry_ptr = (grub_uint32_t *) (rsdt + 1);
  458. entry_ptr < (grub_uint32_t *) (((grub_uint8_t *) rsdt)
  459. + rsdt->length);
  460. entry_ptr++)
  461. {
  462. char signature[5];
  463. struct efiemu_acpi_table *table;
  464. struct grub_acpi_table_header *curtable
  465. = (struct grub_acpi_table_header *) (grub_addr_t) *entry_ptr;
  466. signature[4] = 0;
  467. for (i = 0; i < 4;i++)
  468. signature[i] = grub_tolower (curtable->signature[i]);
  469. /* If it's FADT it contains addresses of DSDT and FACS. */
  470. if (grub_strcmp (signature, "facp") == 0)
  471. {
  472. struct grub_acpi_table_header *dsdt;
  473. struct grub_acpi_fadt *fadt = (struct grub_acpi_fadt *) curtable;
  474. /* Set root header variables to the same values
  475. as FADT by default. */
  476. grub_memcpy (&root_oemid, &(fadt->hdr.oemid),
  477. sizeof (root_oemid));
  478. grub_memcpy (&root_oemtable, &(fadt->hdr.oemtable),
  479. sizeof (root_oemtable));
  480. root_oemrev = fadt->hdr.oemrev;
  481. grub_memcpy (&root_creator_id, &(fadt->hdr.creator_id),
  482. sizeof (root_creator_id));
  483. root_creator_rev = fadt->hdr.creator_rev;
  484. /* Load DSDT if not excluded. */
  485. dsdt = (struct grub_acpi_table_header *)
  486. (grub_addr_t) fadt->dsdt_addr;
  487. if (dsdt && (! exclude || ! grub_strword (exclude, "dsdt"))
  488. && (! load_only || grub_strword (load_only, "dsdt"))
  489. && dsdt->length >= sizeof (*dsdt))
  490. {
  491. dsdt_size = dsdt->length;
  492. table_dsdt = grub_malloc (dsdt->length);
  493. if (! table_dsdt)
  494. {
  495. free_tables ();
  496. grub_free (exclude);
  497. grub_free (load_only);
  498. return grub_errno;
  499. }
  500. grub_memcpy (table_dsdt, dsdt, dsdt->length);
  501. }
  502. /* Save FACS address. FACS shouldn't be overridden. */
  503. facs_addr = fadt->facs_addr;
  504. }
  505. /* Skip excluded tables. */
  506. if (exclude && grub_strword (exclude, signature))
  507. continue;
  508. if (load_only && ! grub_strword (load_only, signature))
  509. continue;
  510. /* Sanity check. */
  511. if (curtable->length < sizeof (*curtable))
  512. continue;
  513. table = (struct efiemu_acpi_table *) grub_malloc
  514. (sizeof (struct efiemu_acpi_table));
  515. if (! table)
  516. {
  517. free_tables ();
  518. grub_free (exclude);
  519. grub_free (load_only);
  520. return grub_errno;
  521. }
  522. table->size = curtable->length;
  523. table->addr = grub_malloc (table->size);
  524. playground_size += table->size;
  525. if (! table->addr)
  526. {
  527. free_tables ();
  528. grub_free (exclude);
  529. grub_free (load_only);
  530. grub_free (table);
  531. return grub_errno;
  532. }
  533. table->next = acpi_tables;
  534. acpi_tables = table;
  535. grub_memcpy (table->addr, curtable, table->size);
  536. }
  537. grub_free (exclude);
  538. grub_free (load_only);
  539. }
  540. /* Does user specify versions to generate? */
  541. if (state[2].set || state[3].set)
  542. {
  543. rev1 = state[2].set;
  544. if (state[3].set)
  545. rev2 = rev2 ? : 2;
  546. else
  547. rev2 = 0;
  548. }
  549. /* Does user override root header information? */
  550. if (state[4].set)
  551. grub_strncpy (root_oemid, state[4].arg, sizeof (root_oemid));
  552. if (state[5].set)
  553. grub_strncpy (root_oemtable, state[5].arg, sizeof (root_oemtable));
  554. if (state[6].set)
  555. root_oemrev = grub_strtoul (state[6].arg, 0, 0);
  556. if (state[7].set)
  557. grub_strncpy (root_creator_id, state[7].arg, sizeof (root_creator_id));
  558. if (state[8].set)
  559. root_creator_rev = grub_strtoul (state[8].arg, 0, 0);
  560. /* Load user tables */
  561. for (i = 0; i < argc; i++)
  562. {
  563. grub_file_t file;
  564. grub_size_t size;
  565. char *buf;
  566. file = grub_file_open (args[i]);
  567. if (! file)
  568. {
  569. free_tables ();
  570. return grub_errno;
  571. }
  572. size = grub_file_size (file);
  573. if (size < sizeof (struct grub_acpi_table_header))
  574. {
  575. grub_file_close (file);
  576. free_tables ();
  577. return grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
  578. args[i]);
  579. }
  580. buf = (char *) grub_malloc (size);
  581. if (! buf)
  582. {
  583. grub_file_close (file);
  584. free_tables ();
  585. return grub_errno;
  586. }
  587. if (grub_file_read (file, buf, size) != (int) size)
  588. {
  589. grub_file_close (file);
  590. free_tables ();
  591. if (!grub_errno)
  592. grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
  593. args[i]);
  594. return grub_errno;
  595. }
  596. grub_file_close (file);
  597. if (grub_memcmp (((struct grub_acpi_table_header *) buf)->signature,
  598. "DSDT", 4) == 0)
  599. {
  600. grub_free (table_dsdt);
  601. table_dsdt = buf;
  602. dsdt_size = size;
  603. }
  604. else
  605. {
  606. struct efiemu_acpi_table *table;
  607. table = (struct efiemu_acpi_table *) grub_malloc
  608. (sizeof (struct efiemu_acpi_table));
  609. if (! table)
  610. {
  611. free_tables ();
  612. return grub_errno;
  613. }
  614. table->size = size;
  615. table->addr = buf;
  616. playground_size += table->size;
  617. table->next = acpi_tables;
  618. acpi_tables = table;
  619. }
  620. }
  621. numoftables = 0;
  622. for (cur = acpi_tables; cur; cur = cur->next)
  623. numoftables++;
  624. /* DSDT. */
  625. playground_size += dsdt_size;
  626. /* RSDT. */
  627. playground_size += sizeof (struct grub_acpi_table_header) + sizeof (grub_uint32_t) * numoftables;
  628. /* RSDPv1. */
  629. playground_size += sizeof (struct grub_acpi_rsdp_v10);
  630. /* XSDT. */
  631. playground_size += sizeof (struct grub_acpi_table_header) + sizeof (grub_uint64_t) * numoftables;
  632. /* RSDPv2. */
  633. playground_size += sizeof (struct grub_acpi_rsdp_v20);
  634. playground = playground_ptr
  635. = grub_mmap_malign_and_register (1, playground_size, &mmapregion,
  636. GRUB_MEMORY_ACPI, 0);
  637. if (! playground)
  638. {
  639. free_tables ();
  640. return grub_error (GRUB_ERR_OUT_OF_MEMORY,
  641. "couldn't allocate space for ACPI tables");
  642. }
  643. setup_common_tables ();
  644. /* Request space for RSDPv1. */
  645. if (rev1)
  646. setv1table ();
  647. /* Request space for RSDPv2+ and XSDT. */
  648. if (rev2)
  649. setv2table ();
  650. for (cur = acpi_tables; cur;)
  651. {
  652. t = cur;
  653. cur = cur->next;
  654. grub_free (t);
  655. }
  656. acpi_tables = 0;
  657. #if defined (__i386__) || defined (__x86_64__)
  658. if (! state[9].set)
  659. {
  660. grub_err_t err;
  661. err = grub_acpi_create_ebda ();
  662. if (err)
  663. {
  664. rsdpv1_new = 0;
  665. rsdpv2_new = 0;
  666. grub_mmap_free_and_unregister (mmapregion);
  667. return err;
  668. }
  669. }
  670. #endif
  671. #ifdef GRUB_MACHINE_EFI
  672. {
  673. struct grub_efi_guid acpi = GRUB_EFI_ACPI_TABLE_GUID;
  674. struct grub_efi_guid acpi20 = GRUB_EFI_ACPI_20_TABLE_GUID;
  675. efi_call_2 (grub_efi_system_table->boot_services->install_configuration_table,
  676. &acpi20, grub_acpi_get_rsdpv2 ());
  677. efi_call_2 (grub_efi_system_table->boot_services->install_configuration_table,
  678. &acpi, grub_acpi_get_rsdpv1 ());
  679. }
  680. #endif
  681. return GRUB_ERR_NONE;
  682. }
  683. static grub_extcmd_t cmd;
  684. GRUB_MOD_INIT(acpi)
  685. {
  686. cmd = grub_register_extcmd ("acpi", grub_cmd_acpi, 0,
  687. N_("[-1|-2] [--exclude=TABLE1,TABLE2|"
  688. "--load-only=TABLE1,TABLE2] FILE1"
  689. " [FILE2] [...]"),
  690. N_("Load host ACPI tables and tables "
  691. "specified by arguments."),
  692. options);
  693. }
  694. GRUB_MOD_FINI(acpi)
  695. {
  696. grub_unregister_extcmd (cmd);
  697. }