Remove nested functions from device iterators.

* include/grub/arc/arc.h (grub_arc_iterate_devs_hook_t): New type.
(grub_arc_iterate_devs): Add hook_data argument.
* include/grub/ata.h (grub_ata_dev_iterate_hook_t): New type.
(struct grub_ata_dev.iterate): Add hook_data argument.
* include/grub/device.h (grub_device_iterate_hook_t): New type.
(grub_device_iterate): Add hook_data argument.
* include/grub/disk.h (grub_disk_dev_iterate_hook_t): New type.
(struct grub_disk_dev.iterate): Add hook_data argument.
(grub_disk_dev_iterate): Likewise.
* include/grub/gpt_partition.h (grub_gpt_partition_map_iterate):
Likewise.
* include/grub/msdos_partition.h (grub_partition_msdos_iterate):
Likewise.
* include/grub/partition.h (grub_partition_iterate_hook_t): New
type.
(struct grub_partition_map.iterate): Add hook_data argument.
(grub_partition_iterate): Likewise.
* include/grub/scsi.h (grub_scsi_dev_iterate_hook_t): New type.
(struct grub_scsi_dev.iterate): Add hook_data argument.

Update all callers.
This commit is contained in:
Colin Watson 2013-01-20 15:52:15 +00:00
parent 6c0314d638
commit 25239370fd
50 changed files with 1455 additions and 1165 deletions

View file

@ -455,8 +455,8 @@ grub_ahci_restore_hw (void)
static int
grub_ahci_iterate (int (*hook) (int id, int bus),
grub_disk_pull_t pull)
grub_ahci_iterate (grub_ata_dev_iterate_hook_t hook, void *hook_data,
grub_disk_pull_t pull)
{
struct grub_ahci_device *dev;
@ -464,7 +464,7 @@ grub_ahci_iterate (int (*hook) (int id, int bus),
return 0;
FOR_LIST_ELEMENTS(dev, grub_ahci_devices)
if (hook (GRUB_SCSI_SUBSYSTEM_AHCI, dev->num))
if (hook (GRUB_SCSI_SUBSYSTEM_AHCI, dev->num, hook_data))
return 1;
return 0;

View file

@ -80,23 +80,37 @@ arcdisk_hash_add (char *devpath)
}
/* Context for grub_arcdisk_iterate. */
struct grub_arcdisk_iterate_ctx
{
grub_disk_dev_iterate_hook_t hook;
void *hook_data;
};
/* Helper for grub_arcdisk_iterate. */
static int
grub_arcdisk_iterate_iter (const char *name,
const struct grub_arc_component *comp, void *data)
{
struct grub_arcdisk_iterate_ctx *ctx = data;
if (!(comp->type == GRUB_ARC_COMPONENT_TYPE_DISK
|| comp->type == GRUB_ARC_COMPONENT_TYPE_DISK
|| comp->type == GRUB_ARC_COMPONENT_TYPE_TAPE))
return 0;
return ctx->hook (name, ctx->hook_data);
}
static int
grub_arcdisk_iterate (int (*hook_in) (const char *name),
grub_disk_pull_t pull)
{
auto int hook (const char *name, const struct grub_arc_component *comp);
int hook (const char *name, const struct grub_arc_component *comp)
{
if (!(comp->type == GRUB_ARC_COMPONENT_TYPE_DISK
|| comp->type == GRUB_ARC_COMPONENT_TYPE_DISK
|| comp->type == GRUB_ARC_COMPONENT_TYPE_TAPE))
return 0;
return hook_in (name);
}
struct grub_arcdisk_iterate_ctx ctx = { hook, hook_data };
if (pull != GRUB_DISK_PULL_NONE)
return 0;
return grub_arc_iterate_devs (hook, 1);
return grub_arc_iterate_devs (grub_arcdisk_iterate_iter, &ctx, 1);
}
#define RAW_SUFFIX "partition(10)"

View file

@ -392,40 +392,50 @@ grub_ata_real_open (int id, int bus)
return NULL;
}
/* Context for grub_ata_iterate. */
struct grub_ata_iterate_ctx
{
grub_disk_dev_iterate_hook_t hook;
void *hook_data;
};
/* Helper for grub_ata_iterate. */
static int
grub_ata_iterate (int (*hook_in) (const char *name),
grub_ata_iterate_iter (int id, int bus, void *data)
{
struct grub_ata_iterate_ctx *ctx = data;
struct grub_ata *ata;
int ret;
char devname[40];
ata = grub_ata_real_open (id, bus);
if (!ata)
{
grub_errno = GRUB_ERR_NONE;
return 0;
}
if (ata->atapi)
{
grub_ata_real_close (ata);
return 0;
}
grub_snprintf (devname, sizeof (devname),
"%s%d", grub_scsi_names[id], bus);
ret = ctx->hook (devname, ctx->hook_data);
grub_ata_real_close (ata);
return ret;
}
static int
grub_ata_iterate (grub_disk_dev_iterate_hook_t hook, void *hook_data,
grub_disk_pull_t pull)
{
auto int hook (int id, int bus);
int hook (int id, int bus)
{
struct grub_ata *ata;
int ret;
char devname[40];
ata = grub_ata_real_open (id, bus);
if (!ata)
{
grub_errno = GRUB_ERR_NONE;
return 0;
}
if (ata->atapi)
{
grub_ata_real_close (ata);
return 0;
}
grub_snprintf (devname, sizeof (devname),
"%s%d", grub_scsi_names[id], bus);
ret = hook_in (devname);
grub_ata_real_close (ata);
return ret;
}
struct grub_ata_iterate_ctx ctx = { hook, hook_data };
grub_ata_dev_t p;
for (p = grub_ata_dev_list; p; p = p->next)
if (p->iterate && p->iterate (hook, pull))
if (p->iterate && p->iterate (grub_ata_iterate_iter, &ctx, pull))
return 1;
return 0;
}
@ -561,37 +571,47 @@ grub_atapi_open (int id, int bus, struct grub_scsi *scsi)
return GRUB_ERR_NONE;
}
/* Context for grub_atapi_iterate. */
struct grub_atapi_iterate_ctx
{
grub_scsi_dev_iterate_hook_t hook;
void *hook_data;
};
/* Helper for grub_atapi_iterate. */
static int
grub_atapi_iterate (int NESTED_FUNC_ATTR (*hook_in) (int id, int bus, int luns),
grub_atapi_iterate_iter (int id, int bus, void *data)
{
struct grub_atapi_iterate_ctx *ctx = data;
struct grub_ata *ata;
int ret;
ata = grub_ata_real_open (id, bus);
if (!ata)
{
grub_errno = GRUB_ERR_NONE;
return 0;
}
if (!ata->atapi)
{
grub_ata_real_close (ata);
return 0;
}
ret = ctx->hook (id, bus, 1, ctx->hook_data);
grub_ata_real_close (ata);
return ret;
}
static int
grub_atapi_iterate (grub_scsi_dev_iterate_hook_t hook, void *hook_data,
grub_disk_pull_t pull)
{
auto int hook (int id, int bus);
int hook (int id, int bus)
{
struct grub_ata *ata;
int ret;
ata = grub_ata_real_open (id, bus);
if (!ata)
{
grub_errno = GRUB_ERR_NONE;
return 0;
}
if (!ata->atapi)
{
grub_ata_real_close (ata);
return 0;
}
ret = hook_in (id, bus, 1);
grub_ata_real_close (ata);
return ret;
}
struct grub_atapi_iterate_ctx ctx = { hook, hook_data };
grub_ata_dev_t p;
for (p = grub_ata_dev_list; p; p = p->next)
if (p->iterate && p->iterate (hook, pull))
if (p->iterate && p->iterate (grub_atapi_iterate_iter, &ctx, pull))
return 1;
return 0;
}

View file

@ -448,8 +448,8 @@ grub_cryptodisk_setkey (grub_cryptodisk_t dev, grub_uint8_t *key, grub_size_t ke
}
static int
grub_cryptodisk_iterate (int (*hook) (const char *name),
grub_disk_pull_t pull)
grub_cryptodisk_iterate (grub_disk_dev_iterate_hook_t hook, void *hook_data,
grub_disk_pull_t pull)
{
grub_cryptodisk_t i;
@ -460,7 +460,7 @@ grub_cryptodisk_iterate (int (*hook) (const char *name),
{
char buf[30];
grub_snprintf (buf, sizeof (buf), "crypto%lu", i->id);
if (hook (buf))
if (hook (buf, hook_data))
return 1;
}
@ -866,7 +866,8 @@ grub_cryptodisk_cheat_mount (const char *sourcedev, const char *cheat)
#endif
static int
grub_cryptodisk_scan_device (const char *name)
grub_cryptodisk_scan_device (const char *name,
void *data __attribute__ ((unused)))
{
grub_err_t err;
grub_disk_t source;
@ -908,7 +909,7 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args)
check_boot = state[2].set;
search_uuid = args[0];
grub_device_iterate (&grub_cryptodisk_scan_device);
grub_device_iterate (&grub_cryptodisk_scan_device, NULL);
search_uuid = NULL;
if (!have_it)
@ -919,7 +920,7 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args)
{
search_uuid = NULL;
check_boot = state[2].set;
grub_device_iterate (&grub_cryptodisk_scan_device);
grub_device_iterate (&grub_cryptodisk_scan_device, NULL);
search_uuid = NULL;
return GRUB_ERR_NONE;
}

View file

@ -120,65 +120,68 @@ is_valid_diskfilter_name (const char *name)
|| grub_memcmp (name, "ldm/", sizeof ("ldm/") - 1) == 0);
}
/* Helper for scan_disk. */
static int
scan_disk_partition_iter (grub_disk_t disk, grub_partition_t p, void *data)
{
const char *name = data;
struct grub_diskfilter_vg *arr;
grub_disk_addr_t start_sector;
struct grub_diskfilter_pv_id id;
grub_diskfilter_t diskfilter;
grub_dprintf ("diskfilter", "Scanning for DISKFILTER devices on disk %s\n",
name);
#ifdef GRUB_UTIL
grub_util_info ("Scanning for DISKFILTER devices on disk %s", name);
#endif
disk->partition = p;
for (arr = array_list; arr != NULL; arr = arr->next)
{
struct grub_diskfilter_pv *m;
for (m = arr->pvs; m; m = m->next)
if (m->disk && m->disk->id == disk->id
&& m->disk->dev->id == disk->dev->id
&& m->part_start == grub_partition_get_start (disk->partition)
&& m->part_size == grub_disk_get_size (disk))
return 0;
}
for (diskfilter = grub_diskfilter_list; diskfilter; diskfilter = diskfilter->next)
{
#ifdef GRUB_UTIL
grub_util_info ("Scanning for %s devices on disk %s",
diskfilter->name, name);
#endif
id.uuid = 0;
id.uuidlen = 0;
arr = diskfilter->detect (disk, &id, &start_sector);
if (arr &&
(! insert_array (disk, &id, arr, start_sector, diskfilter)))
{
if (id.uuidlen)
grub_free (id.uuid);
return 0;
}
if (arr && id.uuidlen)
grub_free (id.uuid);
/* This error usually means it's not diskfilter, no need to display
it. */
if (grub_errno != GRUB_ERR_OUT_OF_RANGE)
grub_print_error ();
grub_errno = GRUB_ERR_NONE;
}
return 0;
}
static int
scan_disk (const char *name, int accept_diskfilter)
{
auto int hook (grub_disk_t disk, grub_partition_t p);
int hook (grub_disk_t disk, grub_partition_t p)
{
struct grub_diskfilter_vg *arr;
grub_disk_addr_t start_sector;
struct grub_diskfilter_pv_id id;
grub_diskfilter_t diskfilter;
grub_dprintf ("diskfilter", "Scanning for DISKFILTER devices on disk %s\n",
name);
#ifdef GRUB_UTIL
grub_util_info ("Scanning for DISKFILTER devices on disk %s", name);
#endif
disk->partition = p;
for (arr = array_list; arr != NULL; arr = arr->next)
{
struct grub_diskfilter_pv *m;
for (m = arr->pvs; m; m = m->next)
if (m->disk && m->disk->id == disk->id
&& m->disk->dev->id == disk->dev->id
&& m->part_start == grub_partition_get_start (disk->partition)
&& m->part_size == grub_disk_get_size (disk))
return 0;
}
for (diskfilter = grub_diskfilter_list; diskfilter; diskfilter = diskfilter->next)
{
#ifdef GRUB_UTIL
grub_util_info ("Scanning for %s devices on disk %s",
diskfilter->name, name);
#endif
id.uuid = 0;
id.uuidlen = 0;
arr = diskfilter->detect (disk, &id, &start_sector);
if (arr &&
(! insert_array (disk, &id, arr, start_sector, diskfilter)))
{
if (id.uuidlen)
grub_free (id.uuid);
return 0;
}
if (arr && id.uuidlen)
grub_free (id.uuid);
/* This error usually means it's not diskfilter, no need to display
it. */
if (grub_errno != GRUB_ERR_OUT_OF_RANGE)
grub_print_error ();
grub_errno = GRUB_ERR_NONE;
}
return 0;
}
grub_disk_t disk;
static int scan_depth = 0;
@ -196,12 +199,12 @@ scan_disk (const char *name, int accept_diskfilter)
scan_depth--;
return 0;
}
if (hook (disk, 0))
if (scan_disk_partition_iter (disk, 0, (void *) name))
{
scan_depth--;
return 1;
}
if (grub_partition_iterate (disk, hook))
if (grub_partition_iterate (disk, scan_disk_partition_iter, (void *) name))
{
scan_depth--;
return 1;
@ -212,7 +215,7 @@ scan_disk (const char *name, int accept_diskfilter)
}
static int
scan_disk_hook (const char *name)
scan_disk_hook (const char *name, void *data __attribute__ ((unused)))
{
return scan_disk (name, 0);
}
@ -230,7 +233,7 @@ scan_devices (const char *arname)
if (p->id != GRUB_DISK_DEVICE_DISKFILTER_ID
&& p->iterate)
{
if ((p->iterate) (scan_disk_hook, pull))
if ((p->iterate) (scan_disk_hook, NULL, pull))
return;
if (arname && is_lv_readable (find_lv (arname), 1))
return;
@ -249,8 +252,8 @@ scan_devices (const char *arname)
}
static int
grub_diskfilter_iterate (int (*hook) (const char *name),
grub_disk_pull_t pull)
grub_diskfilter_iterate (grub_disk_dev_iterate_hook_t hook, void *hook_data,
grub_disk_pull_t pull)
{
struct grub_diskfilter_vg *array;
int islcnt = 0;
@ -271,7 +274,7 @@ grub_diskfilter_iterate (int (*hook) (const char *name),
for (lv = array->lvs; lv; lv = lv->next)
if (lv->visible && lv->fullname && lv->became_readable_at >= islcnt)
{
if (hook (lv->fullname))
if (hook (lv->fullname, hook_data))
return 1;
}
}
@ -303,7 +306,7 @@ grub_diskfilter_memberlist (grub_disk_t disk)
if (p->id != GRUB_DISK_DEVICE_DISKFILTER_ID
&& p->iterate)
{
(p->iterate) (scan_disk_hook, pull);
(p->iterate) (scan_disk_hook, NULL, pull);
while (pv && pv->disk)
pv = pv->next;
}

View file

@ -404,7 +404,7 @@ enumerate_disks (void)
}
static int
grub_efidisk_iterate (int (*hook) (const char *name),
grub_efidisk_iterate (grub_disk_dev_iterate_hook_t hook, void *hook_data,
grub_disk_pull_t pull)
{
struct grub_efidisk_data *d;
@ -418,7 +418,7 @@ grub_efidisk_iterate (int (*hook) (const char *name),
{
grub_snprintf (buf, sizeof (buf), "hd%d", count);
grub_dprintf ("efidisk", "iterating %s\n", buf);
if (hook (buf))
if (hook (buf, hook_data))
return 1;
}
break;
@ -427,7 +427,7 @@ grub_efidisk_iterate (int (*hook) (const char *name),
{
grub_snprintf (buf, sizeof (buf), "fd%d", count);
grub_dprintf ("efidisk", "iterating %s\n", buf);
if (hook (buf))
if (hook (buf, hook_data))
return 1;
}
@ -435,7 +435,7 @@ grub_efidisk_iterate (int (*hook) (const char *name),
{
grub_snprintf (buf, sizeof (buf), "cd%d", count);
grub_dprintf ("efidisk", "iterating %s\n", buf);
if (hook (buf))
if (hook (buf, hook_data))
return 1;
}
break;
@ -736,6 +736,31 @@ get_diskname_from_path (const grub_efi_device_path_t *path,
return 0;
}
/* Context for grub_efidisk_get_device_name. */
struct grub_efidisk_get_device_name_ctx
{
char *partition_name;
grub_efi_hard_drive_device_path_t hd;
};
/* Helper for grub_efidisk_get_device_name.
Find the identical partition. */
static int
grub_efidisk_get_device_name_iter (grub_disk_t disk __attribute__ ((unused)),
const grub_partition_t part, void *data)
{
struct grub_efidisk_get_device_name_ctx *ctx = data;
if (grub_partition_get_start (part) == ctx->hd.partition_start
&& grub_partition_get_len (part) == ctx->hd.partition_size)
{
ctx->partition_name = grub_partition_get_name (part);
return 1;
}
return 0;
}
char *
grub_efidisk_get_device_name (grub_efi_handle_t *handle)
{
@ -754,28 +779,11 @@ grub_efidisk_get_device_name (grub_efi_handle_t *handle)
&& (GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp)
== GRUB_EFI_HARD_DRIVE_DEVICE_PATH_SUBTYPE))
{
char *partition_name = NULL;
struct grub_efidisk_get_device_name_ctx ctx;
char *dev_name;
grub_efi_device_path_t *dup_dp, *dup_ldp;
grub_efi_hard_drive_device_path_t hd;
grub_disk_t parent = 0;
auto int find_partition (grub_disk_t disk, const grub_partition_t part);
/* Find the identical partition. */
int find_partition (grub_disk_t disk __attribute__ ((unused)),
const grub_partition_t part)
{
if (grub_partition_get_start (part) == hd.partition_start
&& grub_partition_get_len (part) == hd.partition_size)
{
partition_name = grub_partition_get_name (part);
return 1;
}
return 0;
}
/* It is necessary to duplicate the device path so that GRUB
can overwrite it. */
dup_dp = duplicate_device_path (dp);
@ -797,24 +805,27 @@ grub_efidisk_get_device_name (grub_efi_handle_t *handle)
return 0;
/* Find a partition which matches the hard drive device path. */
grub_memcpy (&hd, ldp, sizeof (hd));
if (hd.partition_start == 0
&& hd.partition_size == grub_disk_get_size (parent))
ctx.partition_name = NULL;
grub_memcpy (&ctx.hd, ldp, sizeof (ctx.hd));
if (ctx.hd.partition_start == 0
&& ctx.hd.partition_size == grub_disk_get_size (parent))
{
dev_name = grub_strdup (parent->name);
}
else
{
grub_partition_iterate (parent, find_partition);
grub_partition_iterate (parent, grub_efidisk_get_device_name_iter,
&ctx);
if (! partition_name)
if (! ctx.partition_name)
{
grub_disk_close (parent);
return 0;
}
dev_name = grub_xasprintf ("%s,%s", parent->name, partition_name);
grub_free (partition_name);
dev_name = grub_xasprintf ("%s,%s", parent->name,
ctx.partition_name);
grub_free (ctx.partition_name);
}
grub_disk_close (parent);

View file

@ -27,13 +27,13 @@
int grub_disk_host_i_want_a_reference;
static int
grub_host_iterate (int (*hook) (const char *name),
grub_host_iterate (grub_disk_dev_iterate_hook_t hook, void *hook_data,
grub_disk_pull_t pull)
{
if (pull != GRUB_DISK_PULL_NONE)
return 0;
if (hook ("host"))
if (hook ("host", hook_data))
return 1;
return 0;
}

View file

@ -272,20 +272,21 @@ grub_biosdisk_get_drive (const char *name)
}
static int
grub_biosdisk_call_hook (int (*hook) (const char *name), int drive)
grub_biosdisk_call_hook (grub_disk_dev_iterate_hook_t hook, void *hook_data,
int drive)
{
char name[10];
if (cd_drive && drive == cd_drive)
return hook ("cd");
return hook ("cd", hook_data);
grub_snprintf (name, sizeof (name),
(drive & 0x80) ? "hd%d" : "fd%d", drive & (~0x80));
return hook (name);
return hook (name, hook_data);
}
static int
grub_biosdisk_iterate (int (*hook) (const char *name),
grub_biosdisk_iterate (grub_disk_dev_iterate_hook_t hook, void *hook_data,
grub_disk_pull_t pull __attribute__ ((unused)))
{
int num_floppies;
@ -304,7 +305,7 @@ grub_biosdisk_iterate (int (*hook) (const char *name),
break;
}
if (grub_biosdisk_call_hook (hook, drive))
if (grub_biosdisk_call_hook (hook, hook_data, drive))
return 1;
}
return 0;
@ -312,14 +313,14 @@ grub_biosdisk_iterate (int (*hook) (const char *name),
case GRUB_DISK_PULL_REMOVABLE:
if (cd_drive)
{
if (grub_biosdisk_call_hook (hook, cd_drive))
if (grub_biosdisk_call_hook (hook, hook_data, cd_drive))
return 1;
}
/* For floppy disks, we can get the number safely. */
num_floppies = grub_biosdisk_get_num_floppies ();
for (drive = 0; drive < num_floppies; drive++)
if (grub_biosdisk_call_hook (hook, drive))
if (grub_biosdisk_call_hook (hook, hook_data, drive))
return 1;
return 0;
default:

View file

@ -33,7 +33,7 @@ struct grub_nand_data
};
static int
grub_nand_iterate (int (*hook) (const char *name),
grub_nand_iterate (grub_disk_dev_iterate_hook_t hook, void *hook_data,
grub_disk_pull_t pull)
{
auto int dev_iterate (struct grub_ieee1275_devalias *alias);
@ -41,7 +41,7 @@ grub_nand_iterate (int (*hook) (const char *name),
{
if (grub_strcmp (alias->name, "nand") == 0)
{
hook (alias->name);
hook (alias->name, hook_data);
return 1;
}

View file

@ -218,7 +218,7 @@ scan (void)
}
static int
grub_ofdisk_iterate (int (*hook) (const char *name),
grub_ofdisk_iterate (grub_disk_dev_iterate_hook_t hook, void *hook_data,
grub_disk_pull_t pull)
{
unsigned i;
@ -276,7 +276,7 @@ grub_ofdisk_iterate (int (*hook) (const char *name),
*optr++ = *iptr++;
}
*optr = 0;
if (hook (buffer))
if (hook (buffer, hook_data))
return 1;
}
}

View file

@ -105,35 +105,39 @@ read_int (grub_uint8_t *in, grub_size_t s)
static const grub_gpt_part_type_t ldm_type = GRUB_GPT_PARTITION_TYPE_LDM;
/* Helper for gpt_ldm_sector. */
static int
gpt_ldm_sector_iter (grub_disk_t disk, const grub_partition_t p, void *data)
{
grub_disk_addr_t *sector = data;
struct grub_gpt_partentry gptdata;
grub_partition_t p2;
p2 = disk->partition;
disk->partition = p->parent;
if (grub_disk_read (disk, p->offset, p->index,
sizeof (gptdata), &gptdata))
{
disk->partition = p2;
return 0;
}
disk->partition = p2;
if (! grub_memcmp (&gptdata.type, &ldm_type, 16))
{
*sector = p->start + p->len - 1;
return 1;
}
return 0;
}
static grub_disk_addr_t
gpt_ldm_sector (grub_disk_t dsk)
{
grub_disk_addr_t sector = 0;
grub_err_t err;
auto int hook (grub_disk_t disk, const grub_partition_t p);
int hook (grub_disk_t disk, const grub_partition_t p)
{
struct grub_gpt_partentry gptdata;
grub_partition_t p2;
p2 = disk->partition;
disk->partition = p->parent;
if (grub_disk_read (disk, p->offset, p->index,
sizeof (gptdata), &gptdata))
{
disk->partition = p2;
return 0;
}
disk->partition = p2;
if (! grub_memcmp (&gptdata.type, &ldm_type, 16))
{
sector = p->start + p->len - 1;
return 1;
}
return 0;
}
err = grub_gpt_partition_map_iterate (dsk, hook);
err = grub_gpt_partition_map_iterate (dsk, gpt_ldm_sector_iter, &sector);
if (err)
{
grub_errno = GRUB_ERR_NONE;

View file

@ -135,15 +135,15 @@ fail:
static int
grub_loopback_iterate (int (*hook) (const char *name),
grub_disk_pull_t pull)
grub_loopback_iterate (grub_disk_dev_iterate_hook_t hook, void *hook_data,
grub_disk_pull_t pull)
{
struct grub_loopback *d;
if (pull != GRUB_DISK_PULL_NONE)
return 0;
for (d = loopback_list; d; d = d->next)
{
if (hook (d->devname))
if (hook (d->devname, hook_data))
return 1;
}
return 0;

View file

@ -30,13 +30,13 @@ static char *memdisk_addr;
static grub_off_t memdisk_size = 0;
static int
grub_memdisk_iterate (int (*hook) (const char *name),
grub_memdisk_iterate (grub_disk_dev_iterate_hook_t hook, void *hook_data,
grub_disk_pull_t pull)
{
if (pull != GRUB_DISK_PULL_NONE)
return 0;
return hook ("memdisk");
return hook ("memdisk", hook_data);
}
static grub_err_t

View file

@ -501,7 +501,7 @@ grub_pata_open (int id, int devnum, struct grub_ata *ata)
}
static int
grub_pata_iterate (int (*hook) (int id, int bus),
grub_pata_iterate (grub_ata_dev_iterate_hook_t hook, void *hook_data,
grub_disk_pull_t pull)
{
struct grub_pata_device *dev;
@ -510,7 +510,8 @@ grub_pata_iterate (int (*hook) (int id, int bus),
return 0;
for (dev = grub_pata_devices; dev; dev = dev->next)
if (hook (GRUB_SCSI_SUBSYSTEM_PATA, dev->port * 2 + dev->device))
if (hook (GRUB_SCSI_SUBSYSTEM_PATA, dev->port * 2 + dev->device,
hook_data))
return 1;
return 0;

View file

@ -423,50 +423,59 @@ grub_scsi_write16 (grub_disk_t disk, grub_disk_addr_t sector,
static int
grub_scsi_iterate (int (*hook) (const char *name),
grub_disk_pull_t pull)
/* Context for grub_scsi_iterate. */
struct grub_scsi_iterate_ctx
{
grub_scsi_dev_t p;
grub_disk_dev_iterate_hook_t hook;
void *hook_data;
};
auto int NESTED_FUNC_ATTR scsi_iterate (int id, int bus, int luns);
/* Helper for grub_scsi_iterate. */
static int
scsi_iterate (int id, int bus, int luns, void *data)
{
struct grub_scsi_iterate_ctx *ctx = data;
int i;
int NESTED_FUNC_ATTR scsi_iterate (int id, int bus, int luns)
/* In case of a single LUN, just return `usbX'. */
if (luns == 1)
{
int i;
/* In case of a single LUN, just return `usbX'. */
if (luns == 1)
{
char *sname;
int ret;
sname = grub_xasprintf ("%s%d", grub_scsi_names[id], bus);
if (!sname)
return 1;
ret = hook (sname);
grub_free (sname);
return ret;
}
/* In case of multiple LUNs, every LUN will get a prefix to
distinguish it. */
for (i = 0; i < luns; i++)
{
char *sname;
int ret;
sname = grub_xasprintf ("%s%d%c", grub_scsi_names[id], bus, 'a' + i);
if (!sname)
return 1;
ret = hook (sname);
grub_free (sname);
if (ret)
return 1;
}
return 0;
char *sname;
int ret;
sname = grub_xasprintf ("%s%d", grub_scsi_names[id], bus);
if (!sname)
return 1;
ret = ctx->hook (sname, ctx->hook_data);
grub_free (sname);
return ret;
}
/* In case of multiple LUNs, every LUN will get a prefix to
distinguish it. */
for (i = 0; i < luns; i++)
{
char *sname;
int ret;
sname = grub_xasprintf ("%s%d%c", grub_scsi_names[id], bus, 'a' + i);
if (!sname)
return 1;
ret = ctx->hook (sname, ctx->hook_data);
grub_free (sname);
if (ret)
return 1;
}
return 0;
}
static int
grub_scsi_iterate (grub_disk_dev_iterate_hook_t hook, void *hook_data,
grub_disk_pull_t pull)
{
struct grub_scsi_iterate_ctx ctx = { hook, hook_data };
grub_scsi_dev_t p;
for (p = grub_scsi_dev_list; p; p = p->next)
if (p->iterate && (p->iterate) (scsi_iterate, pull))
if (p->iterate && (p->iterate) (scsi_iterate, &ctx, pull))
return 1;
return 0;

View file

@ -265,7 +265,7 @@ grub_usbms_attach (grub_usb_device_t usbdev, int configno, int interfno)
static int
grub_usbms_iterate (int NESTED_FUNC_ATTR (*hook) (int id, int bus, int luns),
grub_usbms_iterate (grub_scsi_dev_iterate_hook_t hook, void *hook_data,
grub_disk_pull_t pull)
{
unsigned i;
@ -278,7 +278,8 @@ grub_usbms_iterate (int NESTED_FUNC_ATTR (*hook) (int id, int bus, int luns),
for (i = 0; i < ARRAY_SIZE (grub_usbms_devices); i++)
if (grub_usbms_devices[i])
{
if (hook (GRUB_SCSI_SUBSYSTEM_USBMS, i, grub_usbms_devices[i]->luns))
if (hook (GRUB_SCSI_SUBSYSTEM_USBMS, i, grub_usbms_devices[i]->luns,
hook_data))
return 1;
}