/* * Create a block device for a handle * * @handle handle * @interface block io protocol * @return 0 = success */ static int efi_bl_bind(efi_handle_t handle, void *interface) { struct udevice *bdev, *parent = dm_root(); int ret, devnum; char *name; struct efi_object *obj = efi_search_obj(handle); struct efi_block_io *io = interface; int disks; struct efi_blk_priv *priv; EFI_PRINT("%s: handle %p, interface %p\n", __func__, handle, io); if (!obj) return -ENOENT; devnum = blk_find_max_devnum(IF_TYPE_EFI); if (devnum == -ENODEV) devnum = 0; else if (devnum < 0) return devnum; name = calloc(1, 18); /* strlen("efiblk#2147483648") + 1 */ if (!name) return -ENOMEM; sprintf(name, "efiblk#%d", devnum); /* Create driver model udevice for the EFI block io device */ ret = blk_create_device(parent, "efi_blk", name, IF_TYPE_EFI, devnum, io->media->block_size, (lbaint_t)io->media->last_block, &bdev); if (ret) return ret; if (!bdev) return -ENOENT; /* Allocate priv */ ret = device_probe(bdev); if (ret) return ret; EFI_PRINT("%s: block device '%s' created\n", __func__, bdev->name); priv = bdev->priv; priv->handle = handle; priv->io = interface; ret = blk_prepare_device(bdev); /* Create handles for the partions of the block device */ disks = efi_bl_bind_partitions(handle, bdev); EFI_PRINT("Found %d partitions\n", disks); return 0; }
int host_dev_bind(int devnum, char *filename) { struct host_block_dev *host_dev; struct udevice *dev; char dev_name[20], *str, *fname; int ret, fd; /* Remove and unbind the old device, if any */ ret = blk_get_device(IF_TYPE_HOST, devnum, &dev); if (ret == 0) { ret = device_remove(dev); if (ret) return ret; ret = device_unbind(dev); if (ret) return ret; } else if (ret != -ENODEV) { return ret; } if (!filename) return 0; snprintf(dev_name, sizeof(dev_name), "host%d", devnum); str = strdup(dev_name); if (!str) return -ENOMEM; fname = strdup(filename); if (!fname) { free(str); return -ENOMEM; } fd = os_open(filename, OS_O_RDWR); if (fd == -1) { printf("Failed to access host backing file '%s'\n", filename); ret = -ENOENT; goto err; } ret = blk_create_device(gd->dm_root, "sandbox_host_blk", str, IF_TYPE_HOST, devnum, 512, os_lseek(fd, 0, OS_SEEK_END), &dev); if (ret) goto err_file; ret = device_probe(dev); if (ret) { device_unbind(dev); goto err_file; } host_dev = dev_get_priv(dev); host_dev->fd = fd; host_dev->filename = fname; return blk_prepare_device(dev); err_file: os_close(fd); err: free(fname); free(str); return ret; }