spl_ymodem.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2000-2004
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. *
  6. * (C) Copyright 2011
  7. * Texas Instruments, <www.ti.com>
  8. *
  9. * Matt Porter <mporter@ti.com>
  10. */
  11. #include <common.h>
  12. #include <spl.h>
  13. #include <xyzModem.h>
  14. #include <asm/u-boot.h>
  15. #include <linux/libfdt.h>
  16. #define BUF_SIZE 1024
  17. /*
  18. * Information required to load image using ymodem.
  19. *
  20. * @image_read: Now of bytes read from the image.
  21. * @buf: pointer to the previous read block.
  22. */
  23. struct ymodem_fit_info {
  24. int image_read;
  25. char *buf;
  26. };
  27. static int getcymodem(void) {
  28. if (tstc())
  29. return (getc());
  30. return -1;
  31. }
  32. static ulong ymodem_read_fit(struct spl_load_info *load, ulong offset,
  33. ulong size, void *addr)
  34. {
  35. int res, err;
  36. struct ymodem_fit_info *info = load->priv;
  37. char *buf = info->buf;
  38. while (info->image_read < offset) {
  39. res = xyzModem_stream_read(buf, BUF_SIZE, &err);
  40. if (res <= 0)
  41. return res;
  42. info->image_read += res;
  43. }
  44. if (info->image_read > offset) {
  45. res = info->image_read - offset;
  46. memcpy(addr, &buf[BUF_SIZE - res], res);
  47. addr = addr + res;
  48. }
  49. while (info->image_read < offset + size) {
  50. res = xyzModem_stream_read(buf, BUF_SIZE, &err);
  51. if (res <= 0)
  52. return res;
  53. memcpy(addr, buf, res);
  54. info->image_read += res;
  55. addr += res;
  56. }
  57. return size;
  58. }
  59. static int spl_ymodem_load_image(struct spl_image_info *spl_image,
  60. struct spl_boot_device *bootdev)
  61. {
  62. int size = 0;
  63. int err;
  64. int res;
  65. int ret;
  66. connection_info_t info;
  67. char buf[BUF_SIZE];
  68. ulong addr = 0;
  69. info.mode = xyzModem_ymodem;
  70. ret = xyzModem_stream_open(&info, &err);
  71. if (ret) {
  72. printf("spl: ymodem err - %s\n", xyzModem_error(err));
  73. return ret;
  74. }
  75. res = xyzModem_stream_read(buf, BUF_SIZE, &err);
  76. if (res <= 0)
  77. goto end_stream;
  78. if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
  79. image_get_magic((struct image_header *)buf) == FDT_MAGIC) {
  80. struct spl_load_info load;
  81. struct ymodem_fit_info info;
  82. debug("Found FIT\n");
  83. load.dev = NULL;
  84. load.priv = (void *)&info;
  85. load.filename = NULL;
  86. load.bl_len = 1;
  87. info.buf = buf;
  88. info.image_read = BUF_SIZE;
  89. load.read = ymodem_read_fit;
  90. ret = spl_load_simple_fit(spl_image, &load, 0, (void *)buf);
  91. size = info.image_read;
  92. while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0)
  93. size += res;
  94. } else {
  95. ret = spl_parse_image_header(spl_image,
  96. (struct image_header *)buf);
  97. if (ret)
  98. return ret;
  99. addr = spl_image->load_addr;
  100. memcpy((void *)addr, buf, res);
  101. size += res;
  102. addr += res;
  103. while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) {
  104. memcpy((void *)addr, buf, res);
  105. size += res;
  106. addr += res;
  107. }
  108. }
  109. end_stream:
  110. xyzModem_stream_close(&err);
  111. xyzModem_stream_terminate(false, &getcymodem);
  112. printf("Loaded %d bytes\n", size);
  113. return 0;
  114. }
  115. SPL_LOAD_IMAGE_METHOD("UART", 0, BOOT_DEVICE_UART, spl_ymodem_load_image);