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

« back to all changes in this revision

Viewing changes to grub-core/bus/usb/usb.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
/* usb.c - Generic USB interfaces.  */
 
2
/*
 
3
 *  GRUB  --  GRand Unified Bootloader
 
4
 *  Copyright (C) 2008  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/mm.h>
 
22
#include <grub/usb.h>
 
23
#include <grub/misc.h>
 
24
#include <grub/list.h>
 
25
#include <grub/term.h>
 
26
 
 
27
static grub_usb_controller_dev_t grub_usb_list;
 
28
struct grub_usb_attach_desc *attach_hooks;
 
29
 
 
30
void
 
31
grub_usb_controller_dev_register (grub_usb_controller_dev_t usb)
 
32
{
 
33
  auto int iterate_hook (grub_usb_controller_t dev);
 
34
 
 
35
  /* Iterate over all controllers found by the driver.  */
 
36
  int iterate_hook (grub_usb_controller_t dev)
 
37
    {
 
38
      dev->dev = usb;
 
39
 
 
40
      /* Enable the ports of the USB Root Hub.  */
 
41
      grub_usb_root_hub (dev);
 
42
 
 
43
      return 0;
 
44
    }
 
45
 
 
46
  usb->next = grub_usb_list;
 
47
  grub_usb_list = usb;
 
48
 
 
49
  if (usb->iterate)
 
50
    usb->iterate (iterate_hook);
 
51
}
 
52
 
 
53
void
 
54
grub_usb_controller_dev_unregister (grub_usb_controller_dev_t usb)
 
55
{
 
56
  grub_usb_controller_dev_t *p, q;
 
57
 
 
58
  for (p = &grub_usb_list, q = *p; q; p = &(q->next), q = q->next)
 
59
    if (q == usb)
 
60
      {
 
61
        *p = q->next;
 
62
        break;
 
63
      }
 
64
}
 
65
 
 
66
#if 0
 
67
int
 
68
grub_usb_controller_iterate (int (*hook) (grub_usb_controller_t dev))
 
69
{
 
70
  grub_usb_controller_dev_t p;
 
71
 
 
72
  auto int iterate_hook (grub_usb_controller_t dev);
 
73
 
 
74
  int iterate_hook (grub_usb_controller_t dev)
 
75
    {
 
76
      dev->dev = p;
 
77
      if (hook (dev))
 
78
        return 1;
 
79
      return 0;
 
80
    }
 
81
 
 
82
  /* Iterate over all controller drivers.  */
 
83
  for (p = grub_usb_list; p; p = p->next)
 
84
    {
 
85
      /* Iterate over the busses of the controllers.  XXX: Actually, a
 
86
         hub driver should do this.  */
 
87
      if (p->iterate (iterate_hook))
 
88
        return 1;
 
89
    }
 
90
 
 
91
  return 0;
 
92
}
 
93
#endif
 
94
 
 
95
 
 
96
grub_usb_err_t
 
97
grub_usb_clear_halt (grub_usb_device_t dev, int endpoint)
 
98
{
 
99
  dev->toggle[endpoint] = 0;
 
100
  return grub_usb_control_msg (dev, (GRUB_USB_REQTYPE_OUT
 
101
                                     | GRUB_USB_REQTYPE_STANDARD
 
102
                                     | GRUB_USB_REQTYPE_TARGET_ENDP),
 
103
                               GRUB_USB_REQ_CLEAR_FEATURE,
 
104
                               GRUB_USB_FEATURE_ENDP_HALT,
 
105
                               endpoint, 0, 0);
 
106
}
 
107
 
 
108
grub_usb_err_t
 
109
grub_usb_set_configuration (grub_usb_device_t dev, int configuration)
 
110
{
 
111
  grub_memset (dev->toggle, 0, sizeof (dev->toggle));
 
112
 
 
113
  return grub_usb_control_msg (dev, (GRUB_USB_REQTYPE_OUT
 
114
                                     | GRUB_USB_REQTYPE_STANDARD
 
115
                                     | GRUB_USB_REQTYPE_TARGET_DEV),
 
116
                               GRUB_USB_REQ_SET_CONFIGURATION, configuration,
 
117
                               0, 0, NULL);
 
118
}
 
119
 
 
120
grub_usb_err_t
 
121
grub_usb_get_descriptor (grub_usb_device_t dev,
 
122
                         grub_uint8_t type, grub_uint8_t index,
 
123
                         grub_size_t size, char *data)
 
124
{
 
125
  return grub_usb_control_msg (dev, (GRUB_USB_REQTYPE_IN
 
126
                                     | GRUB_USB_REQTYPE_STANDARD
 
127
                                     | GRUB_USB_REQTYPE_TARGET_DEV),
 
128
                               GRUB_USB_REQ_GET_DESCRIPTOR,
 
129
                               (type << 8) | index,
 
130
                               0, size, data);
 
131
}
 
132
 
 
133
struct grub_usb_desc_endp *
 
134
grub_usb_get_endpdescriptor (grub_usb_device_t usbdev, int addr)
 
135
{
 
136
  int i;
 
137
 
 
138
  for (i = 0; i < usbdev->config[0].descconf->numif; i++)
 
139
    {
 
140
      struct grub_usb_desc_if *interf;
 
141
      int j;
 
142
 
 
143
      interf = usbdev->config[0].interf[i].descif;
 
144
 
 
145
      for (j = 0; j < interf->endpointcnt; j++)
 
146
        {
 
147
          struct grub_usb_desc_endp *endp;
 
148
          endp = &usbdev->config[0].interf[i].descendp[j];
 
149
 
 
150
          if (endp->endp_addr == addr)
 
151
            return endp;
 
152
        }
 
153
    }
 
154
 
 
155
  return NULL;
 
156
}
 
157
 
 
158
grub_usb_err_t
 
159
grub_usb_device_initialize (grub_usb_device_t dev)
 
160
{
 
161
  struct grub_usb_desc_device *descdev;
 
162
  struct grub_usb_desc_config config;
 
163
  grub_usb_err_t err;
 
164
  int i;
 
165
 
 
166
  /* First we have to read first 8 bytes only and determine
 
167
   * max. size of packet */
 
168
  dev->descdev.maxsize0 = 0; /* invalidating, for safety only, can be removed if it is sure it is zero here */
 
169
  err = grub_usb_get_descriptor (dev, GRUB_USB_DESCRIPTOR_DEVICE,
 
170
                                 0, 8, (char *) &dev->descdev);
 
171
  if (err)
 
172
    return err;
 
173
 
 
174
  /* Now we have valid value in dev->descdev.maxsize0,
 
175
   * so we can read whole device descriptor */
 
176
  err = grub_usb_get_descriptor (dev, GRUB_USB_DESCRIPTOR_DEVICE,
 
177
                                 0, sizeof (struct grub_usb_desc_device),
 
178
                                 (char *) &dev->descdev);
 
179
  if (err)
 
180
    return err;
 
181
  descdev = &dev->descdev;
 
182
 
 
183
  for (i = 0; i < 8; i++)
 
184
    dev->config[i].descconf = NULL;
 
185
 
 
186
  for (i = 0; i < descdev->configcnt; i++)
 
187
    {
 
188
      int pos;
 
189
      int currif;
 
190
      char *data;
 
191
 
 
192
      /* First just read the first 4 bytes of the configuration
 
193
         descriptor, after that it is known how many bytes really have
 
194
         to be read.  */
 
195
      err = grub_usb_get_descriptor (dev, GRUB_USB_DESCRIPTOR_CONFIG, i, 4,
 
196
                                     (char *) &config);
 
197
 
 
198
      data = grub_malloc (config.totallen);
 
199
      if (! data)
 
200
        {
 
201
          err = GRUB_USB_ERR_INTERNAL;
 
202
          goto fail;
 
203
        }
 
204
 
 
205
      dev->config[i].descconf = (struct grub_usb_desc_config *) data;
 
206
      err = grub_usb_get_descriptor (dev, GRUB_USB_DESCRIPTOR_CONFIG, i,
 
207
                                     config.totallen, data);
 
208
      if (err)
 
209
        goto fail;
 
210
 
 
211
      /* Skip the configuration descriptor.  */
 
212
      pos = dev->config[i].descconf->length;
 
213
 
 
214
      /* Read all interfaces.  */
 
215
      for (currif = 0; currif < dev->config[i].descconf->numif; currif++)
 
216
        {
 
217
          while (pos < config.totallen
 
218
                 && ((struct grub_usb_desc *)&data[pos])->type
 
219
                 != GRUB_USB_DESCRIPTOR_INTERFACE)
 
220
            pos += ((struct grub_usb_desc *)&data[pos])->length;
 
221
          dev->config[i].interf[currif].descif
 
222
            = (struct grub_usb_desc_if *) &data[pos];
 
223
          pos += dev->config[i].interf[currif].descif->length;
 
224
 
 
225
          while (pos < config.totallen
 
226
                 && ((struct grub_usb_desc *)&data[pos])->type
 
227
                 != GRUB_USB_DESCRIPTOR_ENDPOINT)
 
228
            pos += ((struct grub_usb_desc *)&data[pos])->length;
 
229
 
 
230
          /* Point to the first endpoint.  */
 
231
          dev->config[i].interf[currif].descendp
 
232
            = (struct grub_usb_desc_endp *) &data[pos];
 
233
          pos += (sizeof (struct grub_usb_desc_endp)
 
234
                  * dev->config[i].interf[currif].descif->endpointcnt);
 
235
        }
 
236
    }
 
237
 
 
238
  return GRUB_USB_ERR_NONE;
 
239
 
 
240
 fail:
 
241
 
 
242
  for (i = 0; i < 8; i++)
 
243
    grub_free (dev->config[i].descconf);
 
244
 
 
245
  return err;
 
246
}
 
247
 
 
248
void grub_usb_device_attach (grub_usb_device_t dev)
 
249
{
 
250
  int i;
 
251
  
 
252
  /* XXX: Just check configuration 0 for now.  */
 
253
  for (i = 0; i < dev->config[0].descconf->numif; i++)
 
254
    {
 
255
      struct grub_usb_desc_if *interf;
 
256
      struct grub_usb_attach_desc *desc;
 
257
 
 
258
      interf = dev->config[0].interf[i].descif;
 
259
 
 
260
      grub_dprintf ("usb", "iterate: interf=%d, class=%d, subclass=%d, protocol=%d\n",
 
261
                    i, interf->class, interf->subclass, interf->protocol);
 
262
 
 
263
      if (dev->config[0].interf[i].attached)
 
264
        continue;
 
265
 
 
266
      for (desc = attach_hooks; desc; desc = desc->next)
 
267
        if (interf->class == desc->class && desc->hook (dev, 0, i))
 
268
          dev->config[0].interf[i].attached = 1;
 
269
 
 
270
      if (dev->config[0].interf[i].attached)
 
271
        continue;
 
272
 
 
273
      switch (interf->class)
 
274
        {
 
275
        case GRUB_USB_CLASS_MASS_STORAGE:
 
276
          grub_dl_load ("usbms");
 
277
          break;
 
278
        case GRUB_USB_CLASS_HID:
 
279
          grub_dl_load ("usb_keyboard");
 
280
          break;
 
281
        case 0xff:
 
282
          /* FIXME: don't load useless modules.  */
 
283
          grub_dl_load ("usbserial_ftdi");
 
284
          grub_dl_load ("usbserial_pl2303");
 
285
          break;
 
286
        }
 
287
    }
 
288
}
 
289
 
 
290
void
 
291
grub_usb_register_attach_hook_class (struct grub_usb_attach_desc *desc)
 
292
{
 
293
  auto int usb_iterate (grub_usb_device_t dev);
 
294
 
 
295
  int usb_iterate (grub_usb_device_t usbdev)
 
296
    {
 
297
      struct grub_usb_desc_device *descdev = &usbdev->descdev;
 
298
      int i;
 
299
 
 
300
      if (descdev->class != 0 || descdev->subclass || descdev->protocol != 0
 
301
          || descdev->configcnt == 0)
 
302
        return 0;
 
303
 
 
304
      /* XXX: Just check configuration 0 for now.  */
 
305
      for (i = 0; i < usbdev->config[0].descconf->numif; i++)
 
306
        {
 
307
          struct grub_usb_desc_if *interf;
 
308
 
 
309
          interf = usbdev->config[0].interf[i].descif;
 
310
 
 
311
          grub_dprintf ("usb", "iterate: interf=%d, class=%d, subclass=%d, protocol=%d\n",
 
312
                        i, interf->class, interf->subclass, interf->protocol);
 
313
 
 
314
          if (usbdev->config[0].interf[i].attached)
 
315
            continue;
 
316
 
 
317
          if (interf->class != desc->class)
 
318
            continue;
 
319
          if (desc->hook (usbdev, 0, i))
 
320
            usbdev->config[0].interf[i].attached = 1;
 
321
        }
 
322
 
 
323
      return 0;
 
324
    }
 
325
 
 
326
  desc->next = attach_hooks;
 
327
  attach_hooks = desc;
 
328
 
 
329
  grub_usb_iterate (usb_iterate);
 
330
}
 
331
 
 
332
void
 
333
grub_usb_unregister_attach_hook_class (struct grub_usb_attach_desc *desc)
 
334
{
 
335
  grub_list_remove (GRUB_AS_LIST_P (&attach_hooks), GRUB_AS_LIST (desc));  
 
336
}
 
337
 
 
338
 
 
339
GRUB_MOD_INIT(usb)
 
340
{
 
341
  grub_term_poll_usb = grub_usb_poll_devices;
 
342
}
 
343
 
 
344
GRUB_MOD_FINI(usb)
 
345
{
 
346
  grub_term_poll_usb = NULL;
 
347
}