spl_ymodem.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. int spl_ymodem_load_image(void)
  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(&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. spl_parse_image_header((struct image_header *)buf);
  97. ret = spl_parse_image_header((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. }