sandbox.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2013 Henrik Nordstrom <henrik@henriknordstrom.net>
  4. */
  5. #include <common.h>
  6. #include <blk.h>
  7. #include <dm.h>
  8. #include <fdtdec.h>
  9. #include <part.h>
  10. #include <os.h>
  11. #include <malloc.h>
  12. #include <sandboxblockdev.h>
  13. #include <asm/global_data.h>
  14. #include <dm/device_compat.h>
  15. #include <linux/errno.h>
  16. #include <dm/device-internal.h>
  17. DECLARE_GLOBAL_DATA_PTR;
  18. #ifndef CONFIG_BLK
  19. static struct host_block_dev host_devices[CONFIG_HOST_MAX_DEVICES];
  20. static struct host_block_dev *find_host_device(int dev)
  21. {
  22. if (dev >= 0 && dev < CONFIG_HOST_MAX_DEVICES)
  23. return &host_devices[dev];
  24. return NULL;
  25. }
  26. #endif
  27. #ifdef CONFIG_BLK
  28. static unsigned long host_block_read(struct udevice *dev,
  29. unsigned long start, lbaint_t blkcnt,
  30. void *buffer)
  31. {
  32. struct host_block_dev *host_dev = dev_get_plat(dev);
  33. struct blk_desc *block_dev = dev_get_uclass_plat(dev);
  34. #else
  35. static unsigned long host_block_read(struct blk_desc *block_dev,
  36. unsigned long start, lbaint_t blkcnt,
  37. void *buffer)
  38. {
  39. int dev = block_dev->devnum;
  40. struct host_block_dev *host_dev = find_host_device(dev);
  41. if (!host_dev)
  42. return -1;
  43. #endif
  44. if (os_lseek(host_dev->fd, start * block_dev->blksz, OS_SEEK_SET) ==
  45. -1) {
  46. printf("ERROR: Invalid block %lx\n", start);
  47. return -1;
  48. }
  49. ssize_t len = os_read(host_dev->fd, buffer, blkcnt * block_dev->blksz);
  50. if (len >= 0)
  51. return len / block_dev->blksz;
  52. return -1;
  53. }
  54. #ifdef CONFIG_BLK
  55. static unsigned long host_block_write(struct udevice *dev,
  56. unsigned long start, lbaint_t blkcnt,
  57. const void *buffer)
  58. {
  59. struct host_block_dev *host_dev = dev_get_plat(dev);
  60. struct blk_desc *block_dev = dev_get_uclass_plat(dev);
  61. #else
  62. static unsigned long host_block_write(struct blk_desc *block_dev,
  63. unsigned long start, lbaint_t blkcnt,
  64. const void *buffer)
  65. {
  66. int dev = block_dev->devnum;
  67. struct host_block_dev *host_dev = find_host_device(dev);
  68. #endif
  69. if (os_lseek(host_dev->fd, start * block_dev->blksz, OS_SEEK_SET) ==
  70. -1) {
  71. printf("ERROR: Invalid block %lx\n", start);
  72. return -1;
  73. }
  74. ssize_t len = os_write(host_dev->fd, buffer, blkcnt * block_dev->blksz);
  75. if (len >= 0)
  76. return len / block_dev->blksz;
  77. return -1;
  78. }
  79. #ifdef CONFIG_BLK
  80. int host_dev_bind(int devnum, char *filename, bool removable)
  81. {
  82. struct host_block_dev *host_dev;
  83. struct udevice *dev;
  84. struct blk_desc *desc;
  85. char dev_name[20], *str, *fname;
  86. int ret, fd;
  87. /* Remove and unbind the old device, if any */
  88. ret = blk_get_device(IF_TYPE_HOST, devnum, &dev);
  89. if (ret == 0) {
  90. ret = device_remove(dev, DM_REMOVE_NORMAL);
  91. if (ret)
  92. return ret;
  93. ret = device_unbind(dev);
  94. if (ret)
  95. return ret;
  96. } else if (ret != -ENODEV) {
  97. return ret;
  98. }
  99. if (!filename)
  100. return 0;
  101. snprintf(dev_name, sizeof(dev_name), "host%d", devnum);
  102. str = strdup(dev_name);
  103. if (!str)
  104. return -ENOMEM;
  105. fname = strdup(filename);
  106. if (!fname) {
  107. free(str);
  108. return -ENOMEM;
  109. }
  110. fd = os_open(filename, OS_O_RDWR);
  111. if (fd == -1) {
  112. printf("Failed to access host backing file '%s'\n", filename);
  113. ret = -ENOENT;
  114. goto err;
  115. }
  116. ret = blk_create_device(gd->dm_root, "sandbox_host_blk", str,
  117. IF_TYPE_HOST, devnum, 512,
  118. os_lseek(fd, 0, OS_SEEK_END) / 512, &dev);
  119. if (ret)
  120. goto err_file;
  121. host_dev = dev_get_plat(dev);
  122. host_dev->fd = fd;
  123. host_dev->filename = fname;
  124. ret = device_probe(dev);
  125. if (ret) {
  126. device_unbind(dev);
  127. goto err_file;
  128. }
  129. desc = blk_get_devnum_by_type(IF_TYPE_HOST, devnum);
  130. desc->removable = removable;
  131. snprintf(desc->vendor, BLK_VEN_SIZE, "U-Boot");
  132. snprintf(desc->product, BLK_PRD_SIZE, "hostfile");
  133. snprintf(desc->revision, BLK_REV_SIZE, "1.0");
  134. return 0;
  135. err_file:
  136. os_close(fd);
  137. err:
  138. free(fname);
  139. free(str);
  140. return ret;
  141. }
  142. #else
  143. int host_dev_bind(int dev, char *filename, bool removable)
  144. {
  145. struct host_block_dev *host_dev = find_host_device(dev);
  146. if (!host_dev)
  147. return -1;
  148. if (host_dev->blk_dev.priv) {
  149. os_close(host_dev->fd);
  150. host_dev->blk_dev.priv = NULL;
  151. }
  152. if (host_dev->filename)
  153. free(host_dev->filename);
  154. if (filename && *filename) {
  155. host_dev->filename = strdup(filename);
  156. } else {
  157. host_dev->filename = NULL;
  158. return 0;
  159. }
  160. host_dev->fd = os_open(host_dev->filename, OS_O_RDWR);
  161. if (host_dev->fd == -1) {
  162. printf("Failed to access host backing file '%s'\n",
  163. host_dev->filename);
  164. return 1;
  165. }
  166. struct blk_desc *blk_dev = &host_dev->blk_dev;
  167. blk_dev->if_type = IF_TYPE_HOST;
  168. blk_dev->priv = host_dev;
  169. blk_dev->blksz = 512;
  170. blk_dev->lba = os_lseek(host_dev->fd, 0, OS_SEEK_END) / blk_dev->blksz;
  171. blk_dev->block_read = host_block_read;
  172. blk_dev->block_write = host_block_write;
  173. blk_dev->devnum = dev;
  174. blk_dev->part_type = PART_TYPE_UNKNOWN;
  175. blk_dev->removable = removable;
  176. snprintf(blk_dev->vendor, BLK_VEN_SIZE, "U-Boot");
  177. snprintf(blk_dev->product, BLK_PRD_SIZE, "hostfile");
  178. snprintf(blk_dev->revision, BLK_REV_SIZE, "1.0");
  179. part_init(blk_dev);
  180. return 0;
  181. }
  182. #endif
  183. int host_get_dev_err(int devnum, struct blk_desc **blk_devp)
  184. {
  185. #ifdef CONFIG_BLK
  186. struct udevice *dev;
  187. int ret;
  188. ret = blk_get_device(IF_TYPE_HOST, devnum, &dev);
  189. if (ret)
  190. return ret;
  191. *blk_devp = dev_get_uclass_plat(dev);
  192. #else
  193. struct host_block_dev *host_dev = find_host_device(devnum);
  194. if (!host_dev)
  195. return -ENODEV;
  196. if (!host_dev->blk_dev.priv)
  197. return -ENOENT;
  198. *blk_devp = &host_dev->blk_dev;
  199. #endif
  200. return 0;
  201. }
  202. #ifdef CONFIG_BLK
  203. int sandbox_host_unbind(struct udevice *dev)
  204. {
  205. struct host_block_dev *host_dev;
  206. /* Data validity is checked in host_dev_bind() */
  207. host_dev = dev_get_plat(dev);
  208. os_close(host_dev->fd);
  209. return 0;
  210. }
  211. static const struct blk_ops sandbox_host_blk_ops = {
  212. .read = host_block_read,
  213. .write = host_block_write,
  214. };
  215. U_BOOT_DRIVER(sandbox_host_blk) = {
  216. .name = "sandbox_host_blk",
  217. .id = UCLASS_BLK,
  218. .ops = &sandbox_host_blk_ops,
  219. .unbind = sandbox_host_unbind,
  220. .plat_auto = sizeof(struct host_block_dev),
  221. };
  222. #else
  223. U_BOOT_LEGACY_BLK(sandbox_host) = {
  224. .if_typename = "host",
  225. .if_type = IF_TYPE_HOST,
  226. .max_devs = CONFIG_HOST_MAX_DEVICES,
  227. .get_dev = host_get_dev_err,
  228. };
  229. #endif