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

« back to all changes in this revision

Viewing changes to grub-core/commands/i386/pc/play.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
/* play.c - command to play a tune  */
 
2
/*
 
3
 *  GRUB  --  GRand Unified Bootloader
 
4
 *  Copyright (C) 2005,2007,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
/* Lots of this file is borrowed from GNU/Hurd generic-speaker driver.  */
 
21
 
 
22
#include <grub/dl.h>
 
23
#include <grub/file.h>
 
24
#include <grub/disk.h>
 
25
#include <grub/term.h>
 
26
#include <grub/misc.h>
 
27
#include <grub/machine/time.h>
 
28
#include <grub/cpu/io.h>
 
29
#include <grub/command.h>
 
30
#include <grub/i18n.h>
 
31
 
 
32
#define BASE_TEMPO (60 * GRUB_TICKS_PER_SECOND)
 
33
 
 
34
/* The speaker port.  */
 
35
#define SPEAKER                 0x61
 
36
 
 
37
/* If 0, follow state of SPEAKER_DATA bit, otherwise enable output
 
38
   from timer 2.  */
 
39
#define SPEAKER_TMR2            0x01
 
40
 
 
41
/* If SPEAKER_TMR2 is not set, this provides direct input into the
 
42
   speaker.  Otherwise, this enables or disables the output from the
 
43
   timer.  */
 
44
#define SPEAKER_DATA            0x02
 
45
 
 
46
/* The PIT channel value ports.  You can write to and read from them.
 
47
   Do not mess with timer 0 or 1.  */
 
48
#define PIT_COUNTER_0           0x40
 
49
#define PIT_COUNTER_1           0x41
 
50
#define PIT_COUNTER_2           0x42
 
51
 
 
52
/* The frequency of the PIT clock.  */
 
53
#define PIT_FREQUENCY           0x1234dd
 
54
 
 
55
/* The PIT control port.  You can only write to it.  Do not mess with
 
56
   timer 0 or 1.  */
 
57
#define PIT_CTRL                0x43
 
58
#define PIT_CTRL_SELECT_MASK    0xc0
 
59
#define PIT_CTRL_SELECT_0       0x00
 
60
#define PIT_CTRL_SELECT_1       0x40
 
61
#define PIT_CTRL_SELECT_2       0x80
 
62
 
 
63
/* Read and load control.  */
 
64
#define PIT_CTRL_READLOAD_MASK  0x30
 
65
#define PIT_CTRL_COUNTER_LATCH  0x00    /* Hold timer value until read.  */
 
66
#define PIT_CTRL_READLOAD_LSB   0x10    /* Read/load the LSB.  */
 
67
#define PIT_CTRL_READLOAD_MSB   0x20    /* Read/load the MSB.  */
 
68
#define PIT_CTRL_READLOAD_WORD  0x30    /* Read/load the LSB then the MSB.  */
 
69
 
 
70
/* Mode control.  */
 
71
#define PIT_CTRL_MODE_MASK      0x0e
 
72
 
 
73
/* Interrupt on terminal count.  Setting the mode sets output to low.
 
74
   When counter is set and terminated, output is set to high.  */
 
75
#define PIT_CTRL_INTR_ON_TERM   0x00
 
76
 
 
77
/* Programmable one-shot.  When loading counter, output is set to
 
78
   high.  When counter terminated, output is set to low.  Can be
 
79
   triggered again from that point on by setting the gate pin to
 
80
   high.  */
 
81
#define PIT_CTRL_PROGR_ONE_SHOT 0x02
 
82
 
 
83
/* Rate generator.  Output is low for one period of the counter, and
 
84
   high for the other.  */
 
85
#define PIT_CTRL_RATE_GEN       0x04
 
86
 
 
87
/* Square wave generator.  Output is low for one half of the period,
 
88
   and high for the other half.  */
 
89
#define PIT_CTRL_SQUAREWAVE_GEN 0x06
 
90
 
 
91
/* Software triggered strobe.  Setting the mode sets output to high.
 
92
   When counter is set and terminated, output is set to low.  */
 
93
#define PIT_CTRL_SOFTSTROBE     0x08
 
94
 
 
95
/* Hardware triggered strobe.  Like software triggered strobe, but
 
96
   only starts the counter when the gate pin is set to high.  */
 
97
#define PIT_CTRL_HARDSTROBE     0x0a
 
98
 
 
99
/* Count mode.  */
 
100
#define PIT_CTRL_COUNT_MASK     0x01
 
101
#define PIT_CTRL_COUNT_BINARY   0x00    /* 16-bit binary counter.  */
 
102
#define PIT_CTRL_COUNT_BCD      0x01    /* 4-decade BCD counter.  */
 
103
 
 
104
#define T_REST                  ((grub_uint16_t) 0)
 
105
#define T_FINE                  ((grub_uint16_t) -1)
 
106
 
 
107
struct note
 
108
{
 
109
  grub_uint16_t pitch;
 
110
  grub_uint16_t duration;
 
111
};
 
112
 
 
113
static void
 
114
beep_off (void)
 
115
{
 
116
  unsigned char status;
 
117
 
 
118
  status = grub_inb (SPEAKER);
 
119
  grub_outb (status & ~(SPEAKER_TMR2 | SPEAKER_DATA), SPEAKER);
 
120
}
 
121
 
 
122
static void
 
123
beep_on (grub_uint16_t pitch)
 
124
{
 
125
  unsigned char status;
 
126
  unsigned int counter;
 
127
 
 
128
  if (pitch < 20)
 
129
    pitch = 20;
 
130
  else if (pitch > 20000)
 
131
    pitch = 20000;
 
132
 
 
133
  counter = PIT_FREQUENCY / pitch;
 
134
 
 
135
  /* Program timer 2.  */
 
136
  grub_outb (PIT_CTRL_SELECT_2 | PIT_CTRL_READLOAD_WORD
 
137
        | PIT_CTRL_SQUAREWAVE_GEN | PIT_CTRL_COUNT_BINARY, PIT_CTRL);
 
138
  grub_outb (counter & 0xff, PIT_COUNTER_2);            /* LSB */
 
139
  grub_outb ((counter >> 8) & 0xff, PIT_COUNTER_2);     /* MSB */
 
140
 
 
141
  /* Start speaker.  */
 
142
  status = grub_inb (SPEAKER);
 
143
  grub_outb (status | SPEAKER_TMR2 | SPEAKER_DATA, SPEAKER);
 
144
}
 
145
 
 
146
/* Returns whether playing should continue.  */
 
147
static int
 
148
play (unsigned tempo, struct note *note)
 
149
{
 
150
  unsigned int to;
 
151
 
 
152
  if (note->pitch == T_FINE || grub_checkkey () >= 0)
 
153
    return 1;
 
154
 
 
155
  grub_dprintf ("play", "pitch = %d, duration = %d\n", note->pitch,
 
156
                note->duration);
 
157
 
 
158
  switch (note->pitch)
 
159
    {
 
160
      case T_REST:
 
161
        beep_off ();
 
162
        break;
 
163
 
 
164
      default:
 
165
        beep_on (note->pitch);
 
166
        break;
 
167
    }
 
168
 
 
169
  to = grub_get_rtc () + BASE_TEMPO * note->duration / tempo;
 
170
  while (((unsigned int) grub_get_rtc () <= to) && (grub_checkkey () < 0))
 
171
    ;
 
172
 
 
173
  return 0;
 
174
}
 
175
 
 
176
static grub_err_t
 
177
grub_cmd_play (grub_command_t cmd __attribute__ ((unused)),
 
178
               int argc, char **args)
 
179
{
 
180
 
 
181
  if (argc < 1)
 
182
    return grub_error (GRUB_ERR_BAD_ARGUMENT, "file name or tempo and notes required");
 
183
 
 
184
  if (argc == 1)
 
185
    {
 
186
      struct note buf;
 
187
      grub_uint32_t tempo;
 
188
      grub_file_t file;
 
189
 
 
190
      file = grub_file_open (args[0]);
 
191
 
 
192
      if (! file)
 
193
        return grub_error (GRUB_ERR_FILE_NOT_FOUND, "file not found");
 
194
 
 
195
      if (grub_file_read (file, &tempo, sizeof (tempo)) != sizeof (tempo))
 
196
        {
 
197
          grub_file_close (file);
 
198
          return grub_error (GRUB_ERR_FILE_READ_ERROR,
 
199
                             "file doesn't even contains a full tempo record");
 
200
        }
 
201
 
 
202
      tempo = grub_le_to_cpu32 (tempo);
 
203
      grub_dprintf ("play","tempo = %d\n", tempo);
 
204
 
 
205
      while (grub_file_read (file, &buf,
 
206
                             sizeof (struct note)) == sizeof (struct note))
 
207
        {
 
208
          buf.pitch = grub_le_to_cpu16 (buf.pitch);
 
209
          buf.duration = grub_le_to_cpu16 (buf.duration);
 
210
 
 
211
          if (play (tempo, &buf))
 
212
            break;
 
213
        }
 
214
 
 
215
      grub_file_close (file);
 
216
    }
 
217
  else
 
218
    {
 
219
      char *end;
 
220
      unsigned tempo;
 
221
      struct note note;
 
222
      int i;
 
223
 
 
224
      tempo = grub_strtoul (args[0], &end, 0);
 
225
 
 
226
      if (*end)
 
227
        /* Was not a number either, assume it was supposed to be a file name.  */
 
228
        return grub_error (GRUB_ERR_FILE_NOT_FOUND, "file not found");
 
229
 
 
230
      grub_dprintf ("play","tempo = %d\n", tempo);
 
231
 
 
232
      for (i = 1; i + 1 < argc; i += 2)
 
233
        {
 
234
          note.pitch = grub_strtoul (args[i], &end, 0);
 
235
          if (*end)
 
236
            {
 
237
              grub_error (GRUB_ERR_BAD_NUMBER, "bogus pitch number");
 
238
              break;
 
239
            }
 
240
 
 
241
          note.duration = grub_strtoul (args[i + 1], &end, 0);
 
242
          if (*end)
 
243
            {
 
244
              grub_error (GRUB_ERR_BAD_NUMBER, "bogus duration number");
 
245
              break;
 
246
            }
 
247
 
 
248
          if (play (tempo, &note))
 
249
            break;
 
250
        }
 
251
    }
 
252
 
 
253
  beep_off ();
 
254
 
 
255
  while (grub_checkkey () > 0)
 
256
    grub_getkey ();
 
257
 
 
258
  return 0;
 
259
}
 
260
 
 
261
static grub_command_t cmd;
 
262
 
 
263
GRUB_MOD_INIT(play)
 
264
{
 
265
  cmd = grub_register_command ("play", grub_cmd_play,
 
266
                               N_("FILE | TEMPO [PITCH1 DURATION1] [PITCH2 DURATION2] ... "),
 
267
                               N_("Play a tune."));
 
268
}
 
269
 
 
270
GRUB_MOD_FINI(play)
 
271
{
 
272
  grub_unregister_command (cmd);
 
273
}