fs_loader.c 6.8 KB

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