lpc32xximage.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Image manipulator for LPC32XX SoCs
  4. *
  5. * (C) Copyright 2015 DENX Software Engineering GmbH
  6. * Written-by: Albert ARIBAUD <albert.aribaud@3adev.fr>
  7. *
  8. * Derived from omapimage.c:
  9. *
  10. * (C) Copyright 2010
  11. * Linaro LTD, www.linaro.org
  12. * Author: John Rigby <john.rigby@linaro.org>
  13. * Based on TI's signGP.c
  14. *
  15. * (C) Copyright 2009
  16. * Stefano Babic, DENX Software Engineering, sbabic@denx.de.
  17. *
  18. * (C) Copyright 2008
  19. * Marvell Semiconductor <www.marvell.com>
  20. * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
  21. */
  22. #include "imagetool.h"
  23. #include <compiler.h>
  24. #include <image.h>
  25. /*
  26. * NAND page 0 boot header
  27. */
  28. struct nand_page_0_boot_header {
  29. uint32_t data[129];
  30. uint32_t pad[383];
  31. };
  32. /*
  33. * Default ICC (interface configuration data [sic]) if none specified
  34. * in board config
  35. */
  36. #ifndef LPC32XX_BOOT_ICR
  37. #define LPC32XX_BOOT_ICR 0x00000096
  38. #endif
  39. /*
  40. * Default boot NAND page size if none specified in board config
  41. */
  42. #ifndef LPC32XX_BOOT_NAND_PAGESIZE
  43. #define LPC32XX_BOOT_NAND_PAGESIZE 2048
  44. #endif
  45. /*
  46. * Default boot NAND pages per sector if none specified in board config
  47. */
  48. #ifndef LPC32XX_BOOT_NAND_PAGES_PER_SECTOR
  49. #define LPC32XX_BOOT_NAND_PAGES_PER_SECTOR 64
  50. #endif
  51. /*
  52. * Maximum size for boot code is 56K unless defined in board config
  53. */
  54. #ifndef LPC32XX_BOOT_CODESIZE
  55. #define LPC32XX_BOOT_CODESIZE (56*1024)
  56. #endif
  57. /* signature byte for a readable block */
  58. #define LPC32XX_BOOT_BLOCK_OK 0xaa
  59. static struct nand_page_0_boot_header lpc32xximage_header;
  60. static int lpc32xximage_check_image_types(uint8_t type)
  61. {
  62. if (type == IH_TYPE_LPC32XXIMAGE)
  63. return EXIT_SUCCESS;
  64. return EXIT_FAILURE;
  65. }
  66. static int lpc32xximage_verify_header(unsigned char *ptr, int image_size,
  67. struct image_tool_params *params)
  68. {
  69. struct nand_page_0_boot_header *hdr =
  70. (struct nand_page_0_boot_header *)ptr;
  71. /* turn image size from bytes to NAND pages, page 0 included */
  72. int image_size_in_pages = ((image_size - 1)
  73. / LPC32XX_BOOT_NAND_PAGESIZE);
  74. if (hdr->data[0] != (0xff & LPC32XX_BOOT_ICR))
  75. return -1;
  76. if (hdr->data[1] != (0xff & ~LPC32XX_BOOT_ICR))
  77. return -1;
  78. if (hdr->data[2] != (0xff & LPC32XX_BOOT_ICR))
  79. return -1;
  80. if (hdr->data[3] != (0xff & ~LPC32XX_BOOT_ICR))
  81. return -1;
  82. if (hdr->data[4] != (0xff & image_size_in_pages))
  83. return -1;
  84. if (hdr->data[5] != (0xff & ~image_size_in_pages))
  85. return -1;
  86. if (hdr->data[6] != (0xff & image_size_in_pages))
  87. return -1;
  88. if (hdr->data[7] != (0xff & ~image_size_in_pages))
  89. return -1;
  90. if (hdr->data[8] != (0xff & image_size_in_pages))
  91. return -1;
  92. if (hdr->data[9] != (0xff & ~image_size_in_pages))
  93. return -1;
  94. if (hdr->data[10] != (0xff & image_size_in_pages))
  95. return -1;
  96. if (hdr->data[11] != (0xff & ~image_size_in_pages))
  97. return -1;
  98. if (hdr->data[12] != LPC32XX_BOOT_BLOCK_OK)
  99. return -1;
  100. if (hdr->data[128] != LPC32XX_BOOT_BLOCK_OK)
  101. return -1;
  102. return 0;
  103. }
  104. static void print_hdr_byte(struct nand_page_0_boot_header *hdr, int ofs)
  105. {
  106. printf("header[%d] = %02x\n", ofs, hdr->data[ofs]);
  107. }
  108. static void lpc32xximage_print_header(const void *ptr)
  109. {
  110. struct nand_page_0_boot_header *hdr =
  111. (struct nand_page_0_boot_header *)ptr;
  112. int ofs;
  113. for (ofs = 0; ofs <= 12; ofs++)
  114. print_hdr_byte(hdr, ofs);
  115. print_hdr_byte(hdr, 128);
  116. }
  117. static void lpc32xximage_set_header(void *ptr, struct stat *sbuf, int ifd,
  118. struct image_tool_params *params)
  119. {
  120. struct nand_page_0_boot_header *hdr =
  121. (struct nand_page_0_boot_header *)ptr;
  122. /* turn image size from bytes to NAND pages, page 0 included */
  123. int image_size_in_pages = ((sbuf->st_size
  124. + LPC32XX_BOOT_NAND_PAGESIZE - 1)
  125. / LPC32XX_BOOT_NAND_PAGESIZE);
  126. /* fill header -- default byte value is 0x00, not 0xFF */
  127. memset((void *)hdr, 0, sizeof(*hdr));
  128. hdr->data[0] = (hdr->data[2] = 0xff & LPC32XX_BOOT_ICR);
  129. hdr->data[1] = (hdr->data[3] = 0xff & ~LPC32XX_BOOT_ICR);
  130. hdr->data[4] = (hdr->data[6] = (hdr->data[8]
  131. = (hdr->data[10] = 0xff & image_size_in_pages)));
  132. hdr->data[5] = (hdr->data[7] = (hdr->data[9]
  133. = (hdr->data[11] = 0xff & ~image_size_in_pages)));
  134. hdr->data[12] = (hdr->data[128] = LPC32XX_BOOT_BLOCK_OK);
  135. }
  136. /*
  137. * lpc32xximage parameters
  138. */
  139. U_BOOT_IMAGE_TYPE(
  140. lpc32xximage,
  141. "LPC32XX Boot Image",
  142. sizeof(lpc32xximage_header),
  143. (void *)&lpc32xximage_header,
  144. NULL,
  145. lpc32xximage_verify_header,
  146. lpc32xximage_print_header,
  147. lpc32xximage_set_header,
  148. NULL,
  149. lpc32xximage_check_image_types,
  150. NULL,
  151. NULL
  152. );