spl_ymodem.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. ulong copy_size = size;
  42. while (info->image_read < offset) {
  43. res = xyzModem_stream_read(buf, BUF_SIZE, &err);
  44. if (res <= 0)
  45. break;
  46. info->image_read += res;
  47. }
  48. if (info->image_read > offset) {
  49. res = info->image_read - offset;
  50. if (info->image_read % BUF_SIZE)
  51. buf_offset = (info->image_read % BUF_SIZE);
  52. else
  53. buf_offset = BUF_SIZE;
  54. if (res > copy_size) {
  55. memcpy(addr, &buf[buf_offset - res], copy_size);
  56. goto done;
  57. }
  58. memcpy(addr, &buf[buf_offset - res], res);
  59. addr = addr + res;
  60. copy_size -= res;
  61. }
  62. while (info->image_read < offset + size) {
  63. res = xyzModem_stream_read(buf, BUF_SIZE, &err);
  64. if (res <= 0)
  65. break;
  66. info->image_read += res;
  67. if (res > copy_size) {
  68. memcpy(addr, buf, copy_size);
  69. goto done;
  70. }
  71. memcpy(addr, buf, res);
  72. addr += res;
  73. copy_size -= res;
  74. }
  75. done:
  76. return size;
  77. }
  78. int spl_ymodem_load_image(struct spl_image_info *spl_image,
  79. struct spl_boot_device *bootdev)
  80. {
  81. ulong size = 0;
  82. int err;
  83. int res;
  84. int ret;
  85. connection_info_t info;
  86. char buf[BUF_SIZE];
  87. struct legacy_img_hdr *ih = NULL;
  88. ulong addr = 0;
  89. info.mode = xyzModem_ymodem;
  90. ret = xyzModem_stream_open(&info, &err);
  91. if (ret) {
  92. printf("spl: ymodem err - %s\n", xyzModem_error(err));
  93. return ret;
  94. }
  95. res = xyzModem_stream_read(buf, BUF_SIZE, &err);
  96. if (res <= 0)
  97. goto end_stream;
  98. if (IS_ENABLED(CONFIG_SPL_LOAD_FIT_FULL) &&
  99. image_get_magic((struct legacy_img_hdr *)buf) == FDT_MAGIC) {
  100. addr = CONFIG_SYS_LOAD_ADDR;
  101. ih = (struct legacy_img_hdr *)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. ret = spl_parse_image_header(spl_image, bootdev, ih);
  111. if (ret)
  112. return ret;
  113. } else if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
  114. image_get_magic((struct legacy_img_hdr *)buf) == FDT_MAGIC) {
  115. struct spl_load_info load;
  116. struct ymodem_fit_info info;
  117. debug("Found FIT\n");
  118. load.dev = NULL;
  119. load.priv = (void *)&info;
  120. load.filename = NULL;
  121. load.bl_len = 1;
  122. info.buf = buf;
  123. info.image_read = BUF_SIZE;
  124. load.read = ymodem_read_fit;
  125. ret = spl_load_simple_fit(spl_image, &load, 0, (void *)buf);
  126. size = info.image_read;
  127. while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0)
  128. size += res;
  129. } else {
  130. ih = (struct legacy_img_hdr *)buf;
  131. ret = spl_parse_image_header(spl_image, bootdev, ih);
  132. if (ret)
  133. goto end_stream;
  134. #ifdef CONFIG_SPL_GZIP
  135. if (ih->ih_comp == IH_COMP_GZIP)
  136. addr = CONFIG_SYS_LOAD_ADDR;
  137. else
  138. #endif
  139. addr = spl_image->load_addr;
  140. memcpy((void *)addr, buf, res);
  141. ih = (struct legacy_img_hdr *)addr;
  142. size += res;
  143. addr += res;
  144. while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) {
  145. memcpy((void *)addr, buf, res);
  146. size += res;
  147. addr += res;
  148. }
  149. }
  150. end_stream:
  151. xyzModem_stream_close(&err);
  152. xyzModem_stream_terminate(false, &getcymodem);
  153. printf("Loaded %lu bytes\n", size);
  154. #ifdef CONFIG_SPL_GZIP
  155. if (!(IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
  156. image_get_magic((struct legacy_img_hdr *)buf) == FDT_MAGIC) &&
  157. (ih->ih_comp == IH_COMP_GZIP)) {
  158. if (gunzip((void *)(spl_image->load_addr + sizeof(*ih)),
  159. CONFIG_SYS_BOOTM_LEN,
  160. (void *)(CONFIG_SYS_LOAD_ADDR + sizeof(*ih)),
  161. &size)) {
  162. puts("Uncompressing error\n");
  163. return -EIO;
  164. }
  165. }
  166. #endif
  167. return ret;
  168. }
  169. SPL_LOAD_IMAGE_METHOD("UART", 0, BOOT_DEVICE_UART, spl_ymodem_load_image);