1 // SPDX-License-Identifier: GPL-2.0+
3 * Driver for sandbox host interface, used to access files on the host which
4 * contain partitions and filesystem
6 * Copyright 2022 Google LLC
7 * Written by Simon Glass <sjg@chromium.org>
10 #define LOG_CATEGORY UCLASS_HOST
19 #include <sandbox_host.h>
20 #include <dm/device-internal.h>
22 static int host_sb_attach_file(struct udevice *dev, const char *filename)
24 struct host_sb_plat *plat = dev_get_plat(dev);
25 struct blk_desc *desc;
34 return log_msg_ret("fd", -EEXIST);
36 /* Sanity check that host_sb_bind() has been used */
37 ret = blk_find_from_parent(dev, &blk);
41 fd = os_open(filename, OS_O_RDWR);
43 printf("Failed to access host backing file '%s', trying read-only\n",
45 fd = os_open(filename, OS_O_RDONLY);
47 printf("- still failed\n");
48 return log_msg_ret("open", -ENOENT);
52 fname = strdup(filename);
58 size = os_filesize(fd);
59 desc = dev_get_uclass_plat(blk);
60 desc->lba = size / desc->blksz;
62 /* write this in last, when nothing can go wrong */
63 plat = dev_get_plat(dev);
65 plat->filename = fname;
75 int host_sb_detach_file(struct udevice *dev)
77 struct host_sb_plat *plat = dev_get_plat(dev);
81 return log_msg_ret("fd", -ENOENT);
83 ret = device_remove(dev, DM_REMOVE_NORMAL);
85 return log_msg_ret("rem", ret);
87 /* Unbind all children */
88 ret = device_chld_unbind(dev, NULL);
90 return log_msg_ret("unb", ret);
100 static int host_sb_bind(struct udevice *dev)
102 struct udevice *blk, *bdev;
103 struct blk_desc *desc;
106 ret = blk_create_devicef(dev, "sandbox_host_blk", "blk", UCLASS_HOST,
107 dev_seq(dev), 512, 0, &blk);
109 return log_msg_ret("blk", ret);
111 desc = dev_get_uclass_plat(blk);
112 snprintf(desc->vendor, BLK_VEN_SIZE, "U-Boot");
113 snprintf(desc->product, BLK_PRD_SIZE, "hostfile");
114 snprintf(desc->revision, BLK_REV_SIZE, "1.0");
116 if (CONFIG_IS_ENABLED(BOOTSTD)) {
117 ret = bootdev_bind(dev, "host_bootdev", "bootdev", &bdev);
119 return log_msg_ret("bd", ret);
125 struct host_ops host_sb_ops = {
126 .attach_file = host_sb_attach_file,
127 .detach_file = host_sb_detach_file,
130 static const struct udevice_id host_ids[] = {
131 { .compatible = "sandbox,host" },
135 U_BOOT_DRIVER(host_sb_drv) = {
136 .name = "host_sb_drv",
138 .of_match = host_ids,
140 .bind = host_sb_bind,
141 .plat_auto = sizeof(struct host_sb_plat),