dfu_alt.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2015
  4. * Lukasz Majewski <l.majewski@majess.pl>
  5. */
  6. #include <common.h>
  7. #include <log.h>
  8. #include <malloc.h>
  9. #include <errno.h>
  10. #include <dfu.h>
  11. /**
  12. * dfu_write_by_name() - write data to DFU medium
  13. * @dfu_entity_name: Name of DFU entity to write
  14. * @addr: Address of data buffer to write
  15. * @len: Number of bytes
  16. * @interface: Destination DFU medium (e.g. "mmc")
  17. * @devstring: Instance number of destination DFU medium (e.g. "1")
  18. *
  19. * This function is storing data received on DFU supported medium which
  20. * is specified by @dfu_entity_name.
  21. *
  22. * Return: 0 - on success, error code - otherwise
  23. */
  24. int dfu_write_by_name(char *dfu_entity_name, void *addr,
  25. unsigned int len, char *interface, char *devstring)
  26. {
  27. char *s, *sb;
  28. int alt_setting_num, ret;
  29. struct dfu_entity *dfu;
  30. debug("%s: name: %s addr: 0x%p len: %d device: %s:%s\n", __func__,
  31. dfu_entity_name, addr, len, interface, devstring);
  32. ret = dfu_init_env_entities(interface, devstring);
  33. if (ret)
  34. goto done;
  35. /*
  36. * We need to copy name pointed by *dfu_entity_name since this text
  37. * is the integral part of the FDT image.
  38. * Any implicit modification (i.e. done by strsep()) will corrupt
  39. * the FDT image and prevent other images to be stored.
  40. */
  41. s = strdup(dfu_entity_name);
  42. sb = s;
  43. if (!s) {
  44. ret = -ENOMEM;
  45. goto done;
  46. }
  47. strsep(&s, "@");
  48. debug("%s: image name: %s strlen: %zd\n", __func__, sb, strlen(sb));
  49. alt_setting_num = dfu_get_alt(sb);
  50. free(sb);
  51. if (alt_setting_num < 0) {
  52. pr_err("Alt setting [%d] to write not found!",
  53. alt_setting_num);
  54. ret = -ENODEV;
  55. goto done;
  56. }
  57. dfu = dfu_get_entity(alt_setting_num);
  58. if (!dfu) {
  59. pr_err("DFU entity for alt: %d not found!", alt_setting_num);
  60. ret = -ENODEV;
  61. goto done;
  62. }
  63. ret = dfu_write_from_mem_addr(dfu, (void *)addr, len);
  64. done:
  65. dfu_free_entities();
  66. return ret;
  67. }
  68. /**
  69. * dfu_write_by_alt() - write data to DFU medium
  70. * @dfu_alt_num: DFU alt setting number
  71. * @addr: Address of data buffer to write
  72. * @len: Number of bytes
  73. * @interface: Destination DFU medium (e.g. "mmc")
  74. * @devstring: Instance number of destination DFU medium (e.g. "1")
  75. *
  76. * This function is storing data received on DFU supported medium which
  77. * is specified by @dfu_alt_name.
  78. *
  79. * Return: 0 - on success, error code - otherwise
  80. */
  81. int dfu_write_by_alt(int dfu_alt_num, void *addr, unsigned int len,
  82. char *interface, char *devstring)
  83. {
  84. struct dfu_entity *dfu;
  85. int ret;
  86. debug("%s: alt: %d addr: 0x%p len: %d device: %s:%s\n", __func__,
  87. dfu_alt_num, addr, len, interface, devstring);
  88. ret = dfu_init_env_entities(interface, devstring);
  89. if (ret)
  90. goto done;
  91. if (dfu_alt_num < 0) {
  92. pr_err("Invalid alt number: %d", dfu_alt_num);
  93. ret = -ENODEV;
  94. goto done;
  95. }
  96. dfu = dfu_get_entity(dfu_alt_num);
  97. if (!dfu) {
  98. pr_err("DFU entity for alt: %d not found!", dfu_alt_num);
  99. ret = -ENODEV;
  100. goto done;
  101. }
  102. ret = dfu_write_from_mem_addr(dfu, (void *)(uintptr_t)addr, len);
  103. done:
  104. dfu_free_entities();
  105. return ret;
  106. }