spl_ymodem.c 2.9 KB

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