0018-tools-lantiq-add-NAND-SPL-support.patch 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. From 43b9a7c9b903302c56d0a1d292a146dbf4de8e49 Mon Sep 17 00:00:00 2001
  2. From: Daniel Schwierzeck <daniel.schwierzeck@gmail.com>
  3. Date: Mon, 12 Aug 2013 01:17:08 +0200
  4. Subject: tools: lantiq: add NAND SPL support
  5. Signed-off-by: Daniel Schwierzeck <daniel.schwierzeck@gmail.com>
  6. --- a/tools/ltq-boot-image.c
  7. +++ b/tools/ltq-boot-image.c
  8. @@ -14,7 +14,8 @@
  9. enum image_types {
  10. IMAGE_NONE,
  11. - IMAGE_SFSPL
  12. + IMAGE_SFSPL,
  13. + IMAGE_NANDSPL
  14. };
  15. /* Lantiq non-volatile bootstrap command IDs */
  16. @@ -43,6 +44,8 @@ enum nvb_cmd_flags {
  17. struct args {
  18. enum image_types type;
  19. __u32 entry_addr;
  20. + loff_t uboot_offset;
  21. + unsigned int page_size;
  22. const char *uboot_bin;
  23. const char *spl_bin;
  24. const char *out_bin;
  25. @@ -50,10 +53,11 @@ struct args {
  26. static void usage_msg(const char *name)
  27. {
  28. - fprintf(stderr, "%s: [-h] -t type -e entry-addr -u uboot-bin [-s spl-bin] -o out-bin\n",
  29. + fprintf(stderr, "%s: [-h] -t type -e entry-addr [-x uboot-offset] [-p page-size] -u uboot-bin [-s spl-bin] -o out-bin\n",
  30. name);
  31. fprintf(stderr, " Image types:\n"
  32. - " sfspl - SPL + [compressed] U-Boot for SPI flash\n");
  33. + " sfspl - SPL + [compressed] U-Boot for SPI flash\n"
  34. + " nandspl - SPL + [compressed] U-Boot for NAND flash\n");
  35. }
  36. static enum image_types parse_image_type(const char *type)
  37. @@ -64,6 +68,9 @@ static enum image_types parse_image_type
  38. if (!strncmp(type, "sfspl", 6))
  39. return IMAGE_SFSPL;
  40. + if (!strncmp(type, "nandspl", 6))
  41. + return IMAGE_NANDSPL;
  42. +
  43. return IMAGE_NONE;
  44. }
  45. @@ -73,7 +80,7 @@ static int parse_args(int argc, char *ar
  46. memset(arg, 0, sizeof(*arg));
  47. - while ((opt = getopt(argc, argv, "ht:e:u:s:o:")) != -1) {
  48. + while ((opt = getopt(argc, argv, "ht:e:x:p:u:s:o:")) != -1) {
  49. switch (opt) {
  50. case 'h':
  51. usage_msg(argv[0]);
  52. @@ -84,6 +91,12 @@ static int parse_args(int argc, char *ar
  53. case 'e':
  54. arg->entry_addr = strtoul(optarg, NULL, 16);
  55. break;
  56. + case 'x':
  57. + arg->uboot_offset = strtoul(optarg, NULL, 16);
  58. + break;
  59. + case 'p':
  60. + arg->page_size = strtoul(optarg, NULL, 10);
  61. + break;
  62. case 'u':
  63. arg->uboot_bin = optarg;
  64. break;
  65. @@ -114,11 +127,22 @@ static int parse_args(int argc, char *ar
  66. goto parse_error;
  67. }
  68. - if (arg->type == IMAGE_SFSPL && !arg->spl_bin) {
  69. + if ((arg->type == IMAGE_SFSPL || arg->type == IMAGE_NANDSPL) &&
  70. + !arg->spl_bin) {
  71. fprintf(stderr, "Missing SPL binary\n");
  72. goto parse_error;
  73. }
  74. + if (arg->type == IMAGE_NANDSPL && !arg->uboot_offset) {
  75. + fprintf(stderr, "Missing U-Boot offset\n");
  76. + goto parse_error;
  77. + }
  78. +
  79. + if (arg->type == IMAGE_NANDSPL && !arg->page_size) {
  80. + fprintf(stderr, "Missing NAND page size\n");
  81. + goto parse_error;
  82. + }
  83. +
  84. return 0;
  85. parse_error:
  86. @@ -174,6 +198,19 @@ static int write_nvb_start_header(int fd
  87. return write_header(fd, hdr, sizeof(hdr));
  88. }
  89. +#if 0
  90. +static int write_nvb_regcfg_header(int fd, __u32 addr)
  91. +{
  92. + __u32 hdr[2];
  93. +
  94. + hdr[0] = build_nvb_command(NVB_CMD_REGCFG, NVB_FLAG_SDBG |
  95. + NVB_FLAG_DBG);
  96. + hdr[1] = cpu_to_be32(addr);
  97. +
  98. + return write_header(fd, hdr, sizeof(hdr));
  99. +}
  100. +#endif
  101. +
  102. static int open_input_bin(const char *name, void **ptr, size_t *size)
  103. {
  104. struct stat sbuf;
  105. @@ -238,9 +275,37 @@ static int open_output_bin(const char *n
  106. return fd;
  107. }
  108. -static int create_sfspl(const struct args *arg)
  109. +static int pad_to_offset(int fd, loff_t offset)
  110. {
  111. - int out_fd, uboot_fd, spl_fd, ret;
  112. + loff_t pos;
  113. + size_t size;
  114. + ssize_t n;
  115. + __u8 *buf;
  116. +
  117. + pos = lseek(fd, 0, SEEK_CUR);
  118. + size = offset - pos;
  119. +
  120. + buf = malloc(size);
  121. + if (!buf) {
  122. + fprintf(stderr, "Failed to malloc buffer\n");
  123. + return -1;
  124. + }
  125. +
  126. + memset(buf, 0xff, size);
  127. + n = write(fd, buf, size);
  128. + free(buf);
  129. +
  130. + if (n != size) {
  131. + fprintf(stderr, "Failed to write pad bytes\n");
  132. + return -1;
  133. + }
  134. +
  135. + return 0;
  136. +}
  137. +
  138. +static int create_spl_image(const struct args *arg)
  139. +{
  140. + int out_fd, uboot_fd, spl_fd, ret = 0;
  141. void *uboot_ptr, *spl_ptr;
  142. size_t uboot_size, spl_size;
  143. @@ -256,9 +321,22 @@ static int create_sfspl(const struct arg
  144. if (0 > uboot_fd)
  145. goto err_uboot;
  146. +#if 0
  147. + ret = write_nvb_regcfg_header(out_fd, 0);
  148. + if (ret)
  149. + goto err_write;
  150. +#endif
  151. +
  152. ret = write_nvb_dwnld_header(out_fd, spl_size, arg->entry_addr);
  153. if (ret)
  154. goto err_write;
  155. +#if 0
  156. + if (arg->page_size) {
  157. + ret = pad_to_offset(out_fd, arg->page_size);
  158. + if (ret)
  159. + goto err_write;
  160. + }
  161. +#endif
  162. ret = copy_bin(out_fd, spl_ptr, spl_size);
  163. if (ret)
  164. @@ -268,16 +346,16 @@ static int create_sfspl(const struct arg
  165. if (ret)
  166. goto err_write;
  167. + if (arg->uboot_offset) {
  168. + ret = pad_to_offset(out_fd, arg->uboot_offset);
  169. + if (ret)
  170. + goto err_write;
  171. + }
  172. +
  173. ret = copy_bin(out_fd, uboot_ptr, uboot_size);
  174. if (ret)
  175. goto err_write;
  176. - close_input_bin(uboot_fd, uboot_ptr, uboot_size);
  177. - close_input_bin(spl_fd, spl_ptr, spl_size);
  178. - close(out_fd);
  179. -
  180. - return 0;
  181. -
  182. err_write:
  183. close_input_bin(uboot_fd, uboot_ptr, uboot_size);
  184. err_uboot:
  185. @@ -285,7 +363,7 @@ err_uboot:
  186. err_spl:
  187. close(out_fd);
  188. err:
  189. - return -1;
  190. + return ret;
  191. }
  192. int main(int argc, char *argv[])
  193. @@ -299,7 +377,8 @@ int main(int argc, char *argv[])
  194. switch (arg.type) {
  195. case IMAGE_SFSPL:
  196. - ret = create_sfspl(&arg);
  197. + case IMAGE_NANDSPL:
  198. + ret = create_spl_image(&arg);
  199. break;
  200. default:
  201. fprintf(stderr, "Image type not implemented\n");