dfu_tftp.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 <malloc.h>
  8. #include <errno.h>
  9. #include <dfu.h>
  10. int dfu_tftp_write(char *dfu_entity_name, unsigned int addr, unsigned int len,
  11. char *interface, char *devstring)
  12. {
  13. char *s, *sb;
  14. int alt_setting_num, ret;
  15. struct dfu_entity *dfu;
  16. debug("%s: name: %s addr: 0x%x len: %d device: %s:%s\n", __func__,
  17. dfu_entity_name, addr, len, interface, devstring);
  18. ret = dfu_init_env_entities(interface, devstring);
  19. if (ret)
  20. goto done;
  21. /*
  22. * We need to copy name pointed by *dfu_entity_name since this text
  23. * is the integral part of the FDT image.
  24. * Any implicit modification (i.e. done by strsep()) will corrupt
  25. * the FDT image and prevent other images to be stored.
  26. */
  27. s = strdup(dfu_entity_name);
  28. sb = s;
  29. if (!s) {
  30. ret = -ENOMEM;
  31. goto done;
  32. }
  33. strsep(&s, "@");
  34. debug("%s: image name: %s strlen: %zd\n", __func__, sb, strlen(sb));
  35. alt_setting_num = dfu_get_alt(sb);
  36. free(sb);
  37. if (alt_setting_num < 0) {
  38. pr_err("Alt setting [%d] to write not found!",
  39. alt_setting_num);
  40. ret = -ENODEV;
  41. goto done;
  42. }
  43. dfu = dfu_get_entity(alt_setting_num);
  44. if (!dfu) {
  45. pr_err("DFU entity for alt: %d not found!", alt_setting_num);
  46. ret = -ENODEV;
  47. goto done;
  48. }
  49. ret = dfu_write_from_mem_addr(dfu, (void *)(uintptr_t)addr, len);
  50. done:
  51. dfu_free_entities();
  52. return ret;
  53. }