fs_loader.c 6.8 KB

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