dfu_tftp.c 1.4 KB

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