spl_ymodem.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 <gzip.h>
  13. #include <image.h>
  14. #include <log.h>
  15. #include <spl.h>
  16. #include <xyzModem.h>
  17. #include <asm/u-boot.h>
  18. #include <linux/libfdt.h>
  19. #define BUF_SIZE 1024
  20. /*
  21. * Information required to load image using ymodem.
  22. *
  23. * @image_read: Now of bytes read from the image.
  24. * @buf: pointer to the previous read block.
  25. */
  26. struct ymodem_fit_info {
  27. int image_read;
  28. char *buf;
  29. };
  30. static int getcymodem(void) {
  31. if (tstc())
  32. return (getchar());
  33. return -1;
  34. }
  35. static ulong ymodem_read_fit(struct spl_load_info *load, ulong offset,
  36. ulong size, void *addr)
  37. {
  38. int res, err, buf_offset;
  39. struct ymodem_fit_info *info = load->priv;
  40. char *buf = info->buf;
  41. while (info->image_read < offset) {
  42. res = xyzModem_stream_read(buf, BUF_SIZE, &err);
  43. if (res <= 0)
  44. break;
  45. info->image_read += res;
  46. }
  47. if (info->image_read > offset) {
  48. res = info->image_read - offset;
  49. if (info->image_read % BUF_SIZE)
  50. buf_offset = (info->image_read % BUF_SIZE);
  51. else
  52. buf_offset = BUF_SIZE;
  53. memcpy(addr, &buf[buf_offset - res], res);
  54. addr = addr + res;
  55. }
  56. while (info->image_read < offset + size) {
  57. res = xyzModem_stream_read(buf, BUF_SIZE, &err);
  58. if (res <= 0)
  59. break;
  60. memcpy(addr, buf, res);
  61. info->image_read += res;
  62. addr += res;
  63. }
  64. return size;
  65. }
  66. int spl_ymodem_load_image(struct spl_image_info *spl_image,
  67. struct spl_boot_device *bootdev)
  68. {
  69. ulong size = 0;
  70. int err;
  71. int res;
  72. int ret;
  73. connection_info_t info;
  74. char buf[BUF_SIZE];
  75. struct image_header *ih = NULL;
  76. ulong addr = 0;
  77. info.mode = xyzModem_ymodem;
  78. ret = xyzModem_stream_open(&info, &err);
  79. if (ret) {
  80. printf("spl: ymodem err - %s\n", xyzModem_error(err));
  81. return ret;
  82. }
  83. res = xyzModem_stream_read(buf, BUF_SIZE, &err);
  84. if (res <= 0)
  85. goto end_stream;
  86. if (IS_ENABLED(CONFIG_SPL_LOAD_FIT_FULL) &&
  87. image_get_magic((struct image_header *)buf) == FDT_MAGIC) {
  88. addr = CONFIG_SYS_LOAD_ADDR;
  89. ih = (struct image_header *)addr;
  90. memcpy((void *)addr, buf, res);
  91. size += res;
  92. addr += res;
  93. while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) {
  94. memcpy((void *)addr, buf, res);
  95. size += res;
  96. addr += res;
  97. }
  98. ret = spl_parse_image_header(spl_image, ih);
  99. if (ret)
  100. return ret;
  101. } else if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
  102. image_get_magic((struct image_header *)buf) == FDT_MAGIC) {
  103. struct spl_load_info load;
  104. struct ymodem_fit_info info;
  105. debug("Found FIT\n");
  106. load.dev = NULL;
  107. load.priv = (void *)&info;
  108. load.filename = NULL;
  109. load.bl_len = 1;
  110. info.buf = buf;
  111. info.image_read = BUF_SIZE;
  112. load.read = ymodem_read_fit;
  113. ret = spl_load_simple_fit(spl_image, &load, 0, (void *)buf);
  114. size = info.image_read;
  115. while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0)
  116. size += res;
  117. } else {
  118. ih = (struct image_header *)buf;
  119. ret = spl_parse_image_header(spl_image, ih);
  120. if (ret)
  121. goto end_stream;
  122. #ifdef CONFIG_SPL_GZIP
  123. if (ih->ih_comp == IH_COMP_GZIP)
  124. addr = CONFIG_SYS_LOAD_ADDR;
  125. else
  126. #endif
  127. addr = spl_image->load_addr;
  128. memcpy((void *)addr, buf, res);
  129. ih = (struct image_header *)addr;
  130. size += res;
  131. addr += res;
  132. while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) {
  133. memcpy((void *)addr, buf, res);
  134. size += res;
  135. addr += res;
  136. }
  137. }
  138. end_stream:
  139. xyzModem_stream_close(&err);
  140. xyzModem_stream_terminate(false, &getcymodem);
  141. printf("Loaded %lu bytes\n", size);
  142. #ifdef CONFIG_SPL_GZIP
  143. if (!(IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
  144. image_get_magic((struct image_header *)buf) == FDT_MAGIC) &&
  145. (ih->ih_comp == IH_COMP_GZIP)) {
  146. if (gunzip((void *)(spl_image->load_addr + sizeof(*ih)),
  147. CONFIG_SYS_BOOTM_LEN,
  148. (void *)(CONFIG_SYS_LOAD_ADDR + sizeof(*ih)),
  149. &size)) {
  150. puts("Uncompressing error\n");
  151. return -EIO;
  152. }
  153. }
  154. #endif
  155. return ret;
  156. }
  157. SPL_LOAD_IMAGE_METHOD("UART", 0, BOOT_DEVICE_UART, spl_ymodem_load_image);