fs_loader.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2018-2019 Intel Corporation <www.intel.com>
  4. *
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <env.h>
  9. #include <errno.h>
  10. #include <blk.h>
  11. #include <fs.h>
  12. #include <fs_loader.h>
  13. #include <log.h>
  14. #include <linux/string.h>
  15. #include <mapmem.h>
  16. #include <malloc.h>
  17. #include <spl.h>
  18. DECLARE_GLOBAL_DATA_PTR;
  19. /**
  20. * struct firmware - A place for storing firmware and its attribute data.
  21. *
  22. * This holds information about a firmware and its content.
  23. *
  24. * @size: Size of a file
  25. * @data: Buffer for file
  26. * @priv: Firmware loader private fields
  27. * @name: Filename
  28. * @offset: Offset of reading a file
  29. */
  30. struct firmware {
  31. size_t size;
  32. const u8 *data;
  33. const char *name;
  34. u32 offset;
  35. };
  36. #ifdef CONFIG_CMD_UBIFS
  37. static int mount_ubifs(char *mtdpart, char *ubivol)
  38. {
  39. int ret = ubi_part(mtdpart, NULL);
  40. if (ret) {
  41. debug("Cannot find mtd partition %s\n", mtdpart);
  42. return ret;
  43. }
  44. return cmd_ubifs_mount(ubivol);
  45. }
  46. static int umount_ubifs(void)
  47. {
  48. return cmd_ubifs_umount();
  49. }
  50. #else
  51. static int mount_ubifs(char *mtdpart, char *ubivol)
  52. {
  53. debug("Error: Cannot load image: no UBIFS support\n");
  54. return -ENOSYS;
  55. }
  56. #endif
  57. static int select_fs_dev(struct device_platdata *plat)
  58. {
  59. int ret;
  60. if (plat->phandlepart.phandle) {
  61. ofnode node;
  62. node = ofnode_get_by_phandle(plat->phandlepart.phandle);
  63. struct udevice *dev;
  64. ret = device_get_global_by_ofnode(node, &dev);
  65. if (!ret) {
  66. struct blk_desc *desc = blk_get_by_device(dev);
  67. if (desc) {
  68. ret = fs_set_blk_dev_with_part(desc,
  69. plat->phandlepart.partition);
  70. } else {
  71. debug("%s: No device found\n", __func__);
  72. return -ENODEV;
  73. }
  74. }
  75. } else if (plat->mtdpart && plat->ubivol) {
  76. ret = mount_ubifs(plat->mtdpart, plat->ubivol);
  77. if (ret)
  78. return ret;
  79. ret = fs_set_blk_dev("ubi", NULL, FS_TYPE_UBIFS);
  80. } else {
  81. debug("Error: unsupported storage device.\n");
  82. return -ENODEV;
  83. }
  84. if (ret)
  85. debug("Error: could not access storage.\n");
  86. return ret;
  87. }
  88. /**
  89. * _request_firmware_prepare - Prepare firmware struct.
  90. *
  91. * @dev: An instance of a driver.
  92. * @name: Name of firmware file.
  93. * @dbuf: Address of buffer to load firmware into.
  94. * @size: Size of buffer.
  95. * @offset: Offset of a file for start reading into buffer.
  96. *
  97. * Return: Negative value if fail, 0 for successful.
  98. */
  99. static int _request_firmware_prepare(struct udevice *dev,
  100. const char *name, void *dbuf,
  101. size_t size, u32 offset)
  102. {
  103. if (!name || name[0] == '\0')
  104. return -EINVAL;
  105. struct firmware *firmwarep = dev_get_priv(dev);
  106. if (!firmwarep)
  107. return -ENOMEM;
  108. firmwarep->name = name;
  109. firmwarep->offset = offset;
  110. firmwarep->data = dbuf;
  111. firmwarep->size = size;
  112. return 0;
  113. }
  114. /**
  115. * fw_get_filesystem_firmware - load firmware into an allocated buffer.
  116. * @dev: An instance of a driver.
  117. *
  118. * Return: Size of total read, negative value when error.
  119. */
  120. static int fw_get_filesystem_firmware(struct udevice *dev)
  121. {
  122. loff_t actread;
  123. char *storage_interface, *dev_part, *ubi_mtdpart, *ubi_volume;
  124. int ret;
  125. storage_interface = env_get("storage_interface");
  126. dev_part = env_get("fw_dev_part");
  127. ubi_mtdpart = env_get("fw_ubi_mtdpart");
  128. ubi_volume = env_get("fw_ubi_volume");
  129. if (storage_interface && dev_part) {
  130. ret = fs_set_blk_dev(storage_interface, dev_part, FS_TYPE_ANY);
  131. } else if (storage_interface && ubi_mtdpart && ubi_volume) {
  132. ret = mount_ubifs(ubi_mtdpart, ubi_volume);
  133. if (ret)
  134. return ret;
  135. if (!strcmp("ubi", storage_interface))
  136. ret = fs_set_blk_dev(storage_interface, NULL,
  137. FS_TYPE_UBIFS);
  138. else
  139. ret = -ENODEV;
  140. } else {
  141. ret = select_fs_dev(dev->platdata);
  142. }
  143. if (ret)
  144. goto out;
  145. struct firmware *firmwarep = dev_get_priv(dev);
  146. if (!firmwarep)
  147. return -ENOMEM;
  148. ret = fs_read(firmwarep->name, (ulong)map_to_sysmem(firmwarep->data),
  149. firmwarep->offset, firmwarep->size, &actread);
  150. if (ret) {
  151. debug("Error: %d Failed to read %s from flash %lld != %zu.\n",
  152. ret, firmwarep->name, actread, firmwarep->size);
  153. } else {
  154. ret = actread;
  155. }
  156. out:
  157. #ifdef CONFIG_CMD_UBIFS
  158. umount_ubifs();
  159. #endif
  160. return ret;
  161. }
  162. /**
  163. * request_firmware_into_buf - Load firmware into a previously allocated buffer.
  164. * @dev: An instance of a driver.
  165. * @name: Name of firmware file.
  166. * @buf: Address of buffer to load firmware into.
  167. * @size: Size of buffer.
  168. * @offset: Offset of a file for start reading into buffer.
  169. *
  170. * The firmware is loaded directly into the buffer pointed to by @buf.
  171. *
  172. * Return: Size of total read, negative value when error.
  173. */
  174. int request_firmware_into_buf(struct udevice *dev,
  175. const char *name,
  176. void *buf, size_t size, u32 offset)
  177. {
  178. int ret;
  179. if (!dev)
  180. return -EINVAL;
  181. ret = _request_firmware_prepare(dev, name, buf, size, offset);
  182. if (ret < 0) /* error */
  183. return ret;
  184. ret = fw_get_filesystem_firmware(dev);
  185. return ret;
  186. }
  187. static int fs_loader_ofdata_to_platdata(struct udevice *dev)
  188. {
  189. u32 phandlepart[2];
  190. ofnode fs_loader_node = dev_ofnode(dev);
  191. if (ofnode_valid(fs_loader_node)) {
  192. struct device_platdata *plat;
  193. plat = dev->platdata;
  194. if (!ofnode_read_u32_array(fs_loader_node,
  195. "phandlepart",
  196. phandlepart, 2)) {
  197. plat->phandlepart.phandle = phandlepart[0];
  198. plat->phandlepart.partition = phandlepart[1];
  199. }
  200. plat->mtdpart = (char *)ofnode_read_string(
  201. fs_loader_node, "mtdpart");
  202. plat->ubivol = (char *)ofnode_read_string(
  203. fs_loader_node, "ubivol");
  204. }
  205. return 0;
  206. }
  207. static int fs_loader_probe(struct udevice *dev)
  208. {
  209. #if CONFIG_IS_ENABLED(DM) && CONFIG_IS_ENABLED(BLK)
  210. int ret;
  211. struct device_platdata *plat = dev->platdata;
  212. if (plat->phandlepart.phandle) {
  213. ofnode node = ofnode_get_by_phandle(plat->phandlepart.phandle);
  214. struct udevice *parent_dev = NULL;
  215. ret = device_get_global_by_ofnode(node, &parent_dev);
  216. if (!ret) {
  217. struct udevice *dev;
  218. ret = blk_get_from_parent(parent_dev, &dev);
  219. if (ret) {
  220. debug("fs_loader: No block device: %d\n",
  221. ret);
  222. return ret;
  223. }
  224. }
  225. }
  226. #endif
  227. return 0;
  228. };
  229. static const struct udevice_id fs_loader_ids[] = {
  230. { .compatible = "u-boot,fs-loader"},
  231. { }
  232. };
  233. U_BOOT_DRIVER(fs_loader) = {
  234. .name = "fs-loader",
  235. .id = UCLASS_FS_FIRMWARE_LOADER,
  236. .of_match = fs_loader_ids,
  237. .probe = fs_loader_probe,
  238. .ofdata_to_platdata = fs_loader_ofdata_to_platdata,
  239. .platdata_auto_alloc_size = sizeof(struct device_platdata),
  240. .priv_auto_alloc_size = sizeof(struct firmware),
  241. };
  242. UCLASS_DRIVER(fs_loader) = {
  243. .id = UCLASS_FS_FIRMWARE_LOADER,
  244. .name = "fs-loader",
  245. };