fs_loader.c 6.4 KB

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