2009-07-16 Vladimir Serbinenko <phcoder@gmail.com>

Fix hang and segmentation fault in grub-emu-usb

	* disk/scsi.c (grub_scsi_open): return err and not grub_errno
	* util/usb.c (grub_libusb_devices): likewise
	(grub_libusb_init): rename to ...
	(GRUB_MOD_INIT (libusb)):...this
	(grub_libusb_fini): rename to ..
	(GRUB_MOD_FINI (libusb)):...this
	* disk/usbms.c (grub_usbms_transfer): fix retry logic
	* include/grub/disk.h (grub_raid_init): removed, it's useless
	(grub_raid_fini): likewise
	(grub_lvm_init): likewise
	(grub_lvm_fini): likewise
	* util/grub-emu.c (main): don't call grub_libusb_init, it's done
	by grub_init_all
This commit is contained in:
phcoder 2009-07-16 15:36:43 +00:00
parent 94414221cf
commit ee293aee1b
6 changed files with 51 additions and 40 deletions

View file

@ -1,3 +1,21 @@
2009-07-16 Vladimir Serbinenko <phcoder@gmail.com>
Fix hang and segmentation fault in grub-emu-usb
* disk/scsi.c (grub_scsi_open): return err and not grub_errno
* util/usb.c (grub_libusb_devices): likewise
(grub_libusb_init): rename to ...
(GRUB_MOD_INIT (libusb)):...this
(grub_libusb_fini): rename to ..
(GRUB_MOD_FINI (libusb)):...this
* disk/usbms.c (grub_usbms_transfer): fix retry logic
* include/grub/disk.h (grub_raid_init): removed, it's useless
(grub_raid_fini): likewise
(grub_lvm_init): likewise
(grub_lvm_fini): likewise
* util/grub-emu.c (main): don't call grub_libusb_init, it's done
by grub_init_all
2009-07-16 Vladimir Serbinenko <phcoder@gmail.com>
Fix libusb

View file

@ -248,6 +248,7 @@ grub_scsi_open (const char *name, grub_disk_t disk)
{
if (p->open (name, scsi))
continue;
disk->id = (unsigned long) "scsi"; /* XXX */
disk->data = scsi;
scsi->dev = p;
@ -266,7 +267,7 @@ grub_scsi_open (const char *name, grub_disk_t disk)
{
grub_free (scsi);
grub_dprintf ("scsi", "inquiry failed\n");
return grub_errno;
return err;
}
grub_dprintf ("scsi", "inquiry: devtype=0x%02x removable=%d\n",
@ -292,7 +293,7 @@ grub_scsi_open (const char *name, grub_disk_t disk)
{
grub_free (scsi);
grub_dprintf ("scsi", "READ CAPACITY failed\n");
return grub_errno;
return err;
}
/* SCSI blocks can be something else than 512, although GRUB

View file

@ -222,11 +222,12 @@ grub_usbms_transfer (struct grub_scsi *scsi, grub_size_t cmdsize, char *cmd,
struct grub_usbms_csw status;
static grub_uint32_t tag = 0;
grub_usb_err_t err = GRUB_USB_ERR_NONE;
int retrycnt = 3;
int retrycnt = 3 + 1;
retry:
retrycnt--;
if (retrycnt == 0)
return err;
return grub_error (GRUB_ERR_IO, "USB Mass Storage stalled");
/* Setup the request. */
grub_memset (&cbw, 0, sizeof (cbw));
@ -305,9 +306,7 @@ grub_usbms_transfer (struct grub_scsi *scsi, grub_size_t cmdsize, char *cmd,
grub_usb_clear_halt (dev->dev, dev->in->endp_addr);
grub_usb_clear_halt (dev->dev, dev->out->endp_addr);
retrycnt--;
if (retrycnt)
goto retry;
goto retry;
}
if (status.status)

View file

@ -173,11 +173,4 @@ struct grub_disk_ata_pass_through_parms
extern grub_err_t (* EXPORT_VAR(grub_disk_ata_pass_through)) (grub_disk_t,
struct grub_disk_ata_pass_through_parms *);
#ifdef GRUB_UTIL
void grub_raid_init (void);
void grub_raid_fini (void);
void grub_lvm_init (void);
void grub_lvm_fini (void);
#endif
#endif /* ! GRUB_DISK_HEADER */

View file

@ -208,10 +208,6 @@ main (int argc, char *argv[])
/* XXX: This is a bit unportable. */
grub_util_biosdisk_init (dev_map);
#if HAVE_USB_H
grub_libusb_init ();
#endif
grub_init_all ();
/* Make sure that there is a root device. */

View file

@ -51,6 +51,7 @@ grub_libusb_devices (void)
for (usbdev = bus->devices; usbdev; usbdev = usbdev->next)
{
struct usb_device_descriptor *desc = &usbdev->descriptor;
grub_err_t err;
if (! desc->bcdUSB)
continue;
@ -62,7 +63,12 @@ grub_libusb_devices (void)
dev->data = usbdev;
/* Fill in all descriptors. */
grub_usb_device_initialize (dev);
err = grub_usb_device_initialize (dev);
if (err)
{
grub_errno = GRUB_ERR_NONE;
continue;
}
/* Register the device. */
grub_usb_devs[last++] = dev;
@ -72,27 +78,6 @@ grub_libusb_devices (void)
return GRUB_USB_ERR_NONE;
}
grub_err_t
grub_libusb_init (void)
{
usb_init();
usb_find_busses();
usb_find_devices();
if (grub_libusb_devices ())
return grub_errno;
grub_usb_controller_dev_register (&usb_controller);
return 0;
}
grub_err_t
grub_libusb_fini (void)
{
return 0;
}
int
grub_usb_iterate (int (*hook) (grub_usb_device_t dev))
@ -189,3 +174,22 @@ grub_usb_bulk_write (grub_usb_device_t dev,
usb_close (devh);
return GRUB_USB_ERR_STALL;
}
GRUB_MOD_INIT (libusb)
{
usb_init();
usb_find_busses();
usb_find_devices();
if (grub_libusb_devices ())
return;
grub_usb_controller_dev_register (&usb_controller);
return;
}
GRUB_MOD_FINI (libusb)
{
return;
}