~hamo/ubuntu/precise/grub2/grub2.hi_res

« 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, Robert Millan, Updated translations
  • Date: 2010-11-22 12:24:56 UTC
  • mfrom: (1.26.4 upstream) (17.3.36 sid)
  • mto: (17.3.43 sid)
  • mto: This revision was merged to the branch mainline in revision 89.
  • Revision ID: james.westby@ubuntu.com-20101122122456-y82z3sfb7k4zfdcc
Tags: 1.99~20101122-1
[ Colin Watson ]
* New Bazaar snapshot.  Too many changes to list in full, but some of the
  more user-visible ones are as follows:
  - GRUB script:
    + Function parameters, "break", "continue", "shift", "setparams",
      "return", and "!".
    + "export" command supports multiple variable names.
    + Multi-line quoted strings support.
    + Wildcard expansion.
  - sendkey support.
  - USB hotunplugging and USB serial support.
  - Rename CD-ROM to cd on BIOS.
  - Add new --boot-directory option to grub-install, grub-reboot, and
    grub-set-default; the old --root-directory option is still accepted
    but was often confusing.
  - Basic btrfs detection/UUID support (but no file reading yet).
  - bash-completion for utilities.
  - If a device is listed in device.map, always assume that it is
    BIOS-visible rather than using extra layers such as LVM or RAID.
  - Add grub-mknetdir script (closes: #550658).
  - Remove deprecated "root" command.
  - Handle RAID devices containing virtio components.
  - GRUB Legacy configuration file support (via grub-menulst2cfg).
  - Keyboard layout support (via grub-mklayout and grub-kbdcomp).
  - Check generated grub.cfg for syntax errors before saving.
  - Pause execution for at most ten seconds if any errors are displayed,
    so that the user has a chance to see them.
  - Support submenus.
  - Write embedding zone using Reed-Solomon, so that it's robust against
    being partially overwritten (closes: #550702, #591416, #593347).
  - GRUB_DISABLE_LINUX_RECOVERY and GRUB_DISABLE_NETBSD_RECOVERY merged
    into a single GRUB_DISABLE_RECOVERY variable.
  - Fix loader memory allocation failure (closes: #551627).
  - Don't call savedefault on recovery entries (closes: #589325).
  - Support triple-indirect blocks on ext2 (closes: #543924).
  - Recognise DDF1 fake RAID (closes: #603354).

[ Robert Millan ]
* Use dpkg architecture wildcards.

[ Updated translations ]
* Slovenian (Vanja Cvelbar).  Closes: #604003
* Dzongkha (dawa pemo via Tenzin Dendup).  Closes: #604102

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
}