Simon Glass | 39f06b5 | 2022-10-29 19:47:16 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Driver for sandbox host interface, used to access files on the host which |
| 4 | * contain partitions and filesystem |
| 5 | * |
| 6 | * Copyright 2022 Google LLC |
| 7 | * Written by Simon Glass <sjg@chromium.org> |
| 8 | */ |
| 9 | |
| 10 | #define LOG_CATEGORY UCLASS_HOST |
| 11 | |
| 12 | #include <common.h> |
| 13 | #include <blk.h> |
| 14 | #include <bootdev.h> |
| 15 | #include <dm.h> |
| 16 | #include <log.h> |
| 17 | #include <malloc.h> |
| 18 | #include <os.h> |
| 19 | #include <sandbox_host.h> |
| 20 | #include <dm/device-internal.h> |
| 21 | |
| 22 | static int host_sb_attach_file(struct udevice *dev, const char *filename) |
| 23 | { |
| 24 | struct host_sb_plat *plat = dev_get_plat(dev); |
| 25 | struct blk_desc *desc; |
| 26 | struct udevice *blk; |
Heinrich Schuchardt | 6db1b65 | 2023-04-05 11:34:15 +0200 | [diff] [blame] | 27 | int ret, fd; |
| 28 | off_t size; |
Simon Glass | 39f06b5 | 2022-10-29 19:47:16 -0600 | [diff] [blame] | 29 | char *fname; |
| 30 | |
| 31 | if (!filename) |
| 32 | return -EINVAL; |
| 33 | |
| 34 | if (plat->fd) |
| 35 | return log_msg_ret("fd", -EEXIST); |
| 36 | |
| 37 | /* Sanity check that host_sb_bind() has been used */ |
| 38 | ret = blk_find_from_parent(dev, &blk); |
| 39 | if (ret) |
| 40 | return ret; |
| 41 | |
| 42 | fd = os_open(filename, OS_O_RDWR); |
| 43 | if (fd == -1) { |
| 44 | printf("Failed to access host backing file '%s', trying read-only\n", |
| 45 | filename); |
| 46 | fd = os_open(filename, OS_O_RDONLY); |
| 47 | if (fd == -1) { |
| 48 | printf("- still failed\n"); |
| 49 | return log_msg_ret("open", -ENOENT); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | fname = strdup(filename); |
| 54 | if (!fname) { |
| 55 | ret = -ENOMEM; |
| 56 | goto err_fname; |
| 57 | } |
| 58 | |
| 59 | size = os_filesize(fd); |
| 60 | desc = dev_get_uclass_plat(blk); |
Bin Meng | cc4c531 | 2023-09-26 16:43:35 +0800 | [diff] [blame] | 61 | if (size % desc->blksz) { |
| 62 | printf("The size of host backing file '%s' is not multiple of " |
| 63 | "the device block size\n", filename); |
| 64 | goto err_fname; |
| 65 | } |
Simon Glass | 39f06b5 | 2022-10-29 19:47:16 -0600 | [diff] [blame] | 66 | desc->lba = size / desc->blksz; |
| 67 | |
| 68 | /* write this in last, when nothing can go wrong */ |
| 69 | plat = dev_get_plat(dev); |
| 70 | plat->fd = fd; |
| 71 | plat->filename = fname; |
| 72 | |
| 73 | return 0; |
| 74 | |
| 75 | err_fname: |
| 76 | os_close(fd); |
| 77 | |
| 78 | return ret; |
| 79 | } |
| 80 | |
Bin Meng | 371fb0e | 2023-09-26 16:43:34 +0800 | [diff] [blame] | 81 | static int host_sb_detach_file(struct udevice *dev) |
Simon Glass | 39f06b5 | 2022-10-29 19:47:16 -0600 | [diff] [blame] | 82 | { |
| 83 | struct host_sb_plat *plat = dev_get_plat(dev); |
| 84 | int ret; |
| 85 | |
| 86 | if (!plat->fd) |
| 87 | return log_msg_ret("fd", -ENOENT); |
| 88 | |
| 89 | ret = device_remove(dev, DM_REMOVE_NORMAL); |
| 90 | if (ret) |
| 91 | return log_msg_ret("rem", ret); |
| 92 | |
| 93 | /* Unbind all children */ |
| 94 | ret = device_chld_unbind(dev, NULL); |
| 95 | if (ret) |
| 96 | return log_msg_ret("unb", ret); |
| 97 | |
| 98 | os_close(plat->fd); |
| 99 | plat->fd = 0; |
| 100 | free(plat->filename); |
| 101 | free(plat->label); |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | static int host_sb_bind(struct udevice *dev) |
| 107 | { |
| 108 | struct udevice *blk, *bdev; |
| 109 | struct blk_desc *desc; |
| 110 | int ret; |
| 111 | |
| 112 | ret = blk_create_devicef(dev, "sandbox_host_blk", "blk", UCLASS_HOST, |
Bin Meng | 2294ecb | 2023-09-26 16:43:31 +0800 | [diff] [blame] | 113 | dev_seq(dev), DEFAULT_BLKSZ, 0, &blk); |
Simon Glass | 39f06b5 | 2022-10-29 19:47:16 -0600 | [diff] [blame] | 114 | if (ret) |
| 115 | return log_msg_ret("blk", ret); |
| 116 | |
| 117 | desc = dev_get_uclass_plat(blk); |
| 118 | snprintf(desc->vendor, BLK_VEN_SIZE, "U-Boot"); |
| 119 | snprintf(desc->product, BLK_PRD_SIZE, "hostfile"); |
| 120 | snprintf(desc->revision, BLK_REV_SIZE, "1.0"); |
| 121 | |
| 122 | if (CONFIG_IS_ENABLED(BOOTSTD)) { |
| 123 | ret = bootdev_bind(dev, "host_bootdev", "bootdev", &bdev); |
| 124 | if (ret) |
| 125 | return log_msg_ret("bd", ret); |
| 126 | } |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
Bin Meng | 371fb0e | 2023-09-26 16:43:34 +0800 | [diff] [blame] | 131 | static struct host_ops host_sb_ops = { |
Simon Glass | 39f06b5 | 2022-10-29 19:47:16 -0600 | [diff] [blame] | 132 | .attach_file = host_sb_attach_file, |
| 133 | .detach_file = host_sb_detach_file, |
| 134 | }; |
| 135 | |
| 136 | static const struct udevice_id host_ids[] = { |
| 137 | { .compatible = "sandbox,host" }, |
| 138 | { } |
| 139 | }; |
| 140 | |
| 141 | U_BOOT_DRIVER(host_sb_drv) = { |
| 142 | .name = "host_sb_drv", |
| 143 | .id = UCLASS_HOST, |
| 144 | .of_match = host_ids, |
| 145 | .ops = &host_sb_ops, |
| 146 | .bind = host_sb_bind, |
| 147 | .plat_auto = sizeof(struct host_sb_plat), |
| 148 | }; |