spl_ymodem.c 4.0 KB

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