/* Test that block devices can be created */ static int dm_test_blk_base(struct unit_test_state *uts) { struct udevice *blk1, *blk3, *dev; /* Make sure there are no block devices */ ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_BLK, 0, &dev)); /* Create two, one the parent of the other */ ut_assertok(blk_create_device(gd->dm_root, "sandbox_host_blk", "test", IF_TYPE_HOST, 1, 512, 2, &blk1)); ut_assertok(blk_create_device(blk1, "sandbox_host_blk", "test", IF_TYPE_HOST, 3, 512, 2, &blk3)); /* Check we can find them */ ut_asserteq(-ENODEV, blk_get_device(IF_TYPE_HOST, 0, &dev)); ut_assertok(blk_get_device(IF_TYPE_HOST, 1, &dev)); ut_asserteq_ptr(blk1, dev); ut_assertok(blk_get_device(IF_TYPE_HOST, 3, &dev)); ut_asserteq_ptr(blk3, dev); /* Check we can iterate */ ut_assertok(blk_first_device(IF_TYPE_HOST, &dev)); ut_asserteq_ptr(blk1, dev); ut_assertok(blk_next_device(&dev)); ut_asserteq_ptr(blk3, dev); return 0; }
/* * 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; }
/* Test that we can find block devices without probing them */ static int dm_test_blk_find(struct unit_test_state *uts) { struct udevice *blk, *dev; ut_assertok(blk_create_device(gd->dm_root, "sandbox_host_blk", "test", IF_TYPE_HOST, 1, 512, 2, &blk)); ut_asserteq(-ENODEV, blk_find_device(IF_TYPE_HOST, 0, &dev)); ut_assertok(blk_find_device(IF_TYPE_HOST, 1, &dev)); ut_asserteq_ptr(blk, dev); ut_asserteq(false, device_active(dev)); /* Now activate it */ ut_assertok(blk_get_device(IF_TYPE_HOST, 1, &dev)); ut_asserteq_ptr(blk, dev); ut_asserteq(true, device_active(dev)); 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; }