spl_ymodem.c 2.9 KB

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