lpc32xximage.c 4.3 KB

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