spl_ymodem.c 3.8 KB

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