~darkmuggle-deactivatedaccount/ubuntu/quantal/grub2/fix-872244

« back to all changes in this revision

Viewing changes to commands/acpi.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson, Colin Watson, Evan Broder, Mario Limonciello
  • Date: 2010-11-24 13:59:55 UTC
  • mfrom: (1.17.6 upstream) (17.6.15 experimental)
  • Revision ID: james.westby@ubuntu.com-20101124135955-r6ii5sepayr7jt53
Tags: 1.99~20101124-1ubuntu1
[ Colin Watson ]
* Resynchronise with Debian experimental.  Remaining changes:
  - Adjust for default Ubuntu boot options ("quiet splash").
  - Default to hiding the menu; holding down Shift at boot will show it.
  - Set a monochromatic theme for Ubuntu.
  - Apply Ubuntu GRUB Legacy changes to legacy update-grub script: title,
    recovery mode, quiet option, tweak how memtest86+ is displayed, and
    use UUIDs where appropriate.
  - Fix backslash-escaping in merge_debconf_into_conf.
  - Remove "GNU/Linux" from default distributor string.
  - Add crashkernel= options if kdump and makedumpfile are available.
  - If other operating systems are installed, then automatically unhide
    the menu.  Otherwise, if GRUB_HIDDEN_TIMEOUT is 0, then use keystatus
    if available to check whether Shift is pressed.  If it is, show the
    menu, otherwise boot immediately.  If keystatus is not available, then
    fall back to a short delay interruptible with Escape.
  - Allow Shift to interrupt 'sleep --interruptible'.
  - Don't display introductory message about line editing unless we're
    actually offering a shell prompt.  Don't clear the screen just before
    booting if we never drew the menu in the first place.
  - Remove some verbose messages printed before reading the configuration
    file.
  - Suppress progress messages as the kernel and initrd load for
    non-recovery kernel menu entries.
  - Change prepare_grub_to_access_device to handle filesystems
    loop-mounted on file images.
  - Ignore devices loop-mounted from files in 10_linux.
  - Show the boot menu if the previous boot failed, that is if it failed
    to get to the end of one of the normal runlevels.
  - Don't generate /boot/grub/device.map during grub-install or
    grub-mkconfig by default.
  - Adjust upgrade version checks for Ubuntu.
  - Don't display "GRUB loading" unless Shift is held down.
  - Adjust versions of grub-doc and grub-legacy-doc conflicts to tolerate
    our backport of the grub-doc split.
  - Fix LVM/RAID probing in the absence of /boot/grub/device.map.
  - Look for .mo files in /usr/share/locale-langpack as well, in
    preference.
  - Make sure GRUB_TIMEOUT isn't quoted unnecessarily.
  - Probe all devices in 'grub-probe --target=drive' if
    /boot/grub/device.map is missing.
  - Build-depend on qemu-kvm rather than qemu-system for grub-pc tests.
  - Use qemu rather than qemu-system-i386.
  - Program vesafb on BIOS systems rather than efifb.
  - Add a grub-rescue-efi-amd64 package containing a rescue CD-ROM image
    for EFI-AMD64.
  - On Wubi, don't ask for an install device, but just update wubildr
    using the diverted grub-install.
  - When embedding the core image in a post-MBR gap, check for and avoid
    sectors matching any of a list of known signatures.
  - Disable video_bochs and video_cirrus on PC BIOS systems, as probing
    PCI space seems to break on some systems.
* Downgrade "ACPI shutdown failed" error to a debug message, since it can
  cause spurious test failures.

[ Evan Broder ]
* Enable lua from grub-extras.
* Incorporate the bitop library into lua.
* Add enum_pci function to grub module in lua.
* Switch back to gfxpayload=keep by default, unless the video hardware
  is known to not support it.

[ Mario Limonciello ]
* Built part_msdos and vfat into bootx64.efi (LP: #677758)

Show diffs side-by-side

added added

removed removed

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