vybridimage.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Image manipulator for Vybrid SoCs
  3. *
  4. * Derived from vybridimage.c
  5. *
  6. * (C) Copyright 2016 DENX Software Engineering GmbH
  7. * Written-by: Albert ARIBAUD <albert.aribaud@3adev.fr>
  8. *
  9. * SPDX-License-Identifier: GPL-2.0+
  10. */
  11. #include "imagetool.h"
  12. #include <compiler.h>
  13. #include <image.h>
  14. /*
  15. * NAND page 0 boot header
  16. */
  17. struct nand_page_0_boot_header {
  18. union {
  19. uint32_t fcb[128];
  20. uint8_t fcb_bytes[512];
  21. }; /* 0x00000000 - 0x000001ff */
  22. uint8_t sw_ecc[512]; /* 0x00000200 - 0x000003ff */
  23. uint32_t padding[65280]; /* 0x00000400 - 0x0003ffff */
  24. uint8_t ivt_prefix[1024]; /* 0x00040000 - 0x000403ff */
  25. };
  26. /* signature byte for a readable block */
  27. static struct nand_page_0_boot_header vybridimage_header;
  28. static int vybridimage_check_image_types(uint8_t type)
  29. {
  30. if (type == IH_TYPE_VYBRIDIMAGE)
  31. return EXIT_SUCCESS;
  32. return EXIT_FAILURE;
  33. }
  34. static uint8_t vybridimage_sw_ecc(uint8_t byte)
  35. {
  36. uint8_t bit0 = (byte & (1 << 0)) ? 1 : 0;
  37. uint8_t bit1 = (byte & (1 << 1)) ? 1 : 0;
  38. uint8_t bit2 = (byte & (1 << 2)) ? 1 : 0;
  39. uint8_t bit3 = (byte & (1 << 3)) ? 1 : 0;
  40. uint8_t bit4 = (byte & (1 << 4)) ? 1 : 0;
  41. uint8_t bit5 = (byte & (1 << 5)) ? 1 : 0;
  42. uint8_t bit6 = (byte & (1 << 6)) ? 1 : 0;
  43. uint8_t bit7 = (byte & (1 << 7)) ? 1 : 0;
  44. uint8_t res = 0;
  45. res |= ((bit6 ^ bit5 ^ bit3 ^ bit2) << 0);
  46. res |= ((bit7 ^ bit5 ^ bit4 ^ bit2 ^ bit1) << 1);
  47. res |= ((bit7 ^ bit6 ^ bit5 ^ bit1 ^ bit0) << 2);
  48. res |= ((bit7 ^ bit4 ^ bit3 ^ bit0) << 3);
  49. res |= ((bit6 ^ bit4 ^ bit3 ^ bit2 ^ bit1 ^ bit0) << 4);
  50. return res;
  51. }
  52. static int vybridimage_verify_header(unsigned char *ptr, int image_size,
  53. struct image_tool_params *params)
  54. {
  55. struct nand_page_0_boot_header *hdr =
  56. (struct nand_page_0_boot_header *)ptr;
  57. int idx;
  58. if (hdr->fcb[1] != 0x46434220)
  59. return -1;
  60. if (hdr->fcb[2] != 1)
  61. return -1;
  62. if (hdr->fcb[7] != 64)
  63. return -1;
  64. if (hdr->fcb[14] != 6)
  65. return -1;
  66. if (hdr->fcb[30] != 0x0001ff00)
  67. return -1;
  68. if (hdr->fcb[43] != 1)
  69. return -1;
  70. if (hdr->fcb[54] != 0)
  71. return -1;
  72. if (hdr->fcb[55] != 8)
  73. return -1;
  74. /* check software ECC */
  75. for (idx = 0; idx < sizeof(hdr->fcb_bytes); idx++) {
  76. uint8_t sw_ecc = vybridimage_sw_ecc(hdr->fcb_bytes[idx]);
  77. if (sw_ecc != hdr->sw_ecc[idx])
  78. return -1;
  79. }
  80. return 0;
  81. }
  82. static void vybridimage_set_header(void *ptr, struct stat *sbuf, int ifd,
  83. struct image_tool_params *params)
  84. {
  85. struct nand_page_0_boot_header *hdr =
  86. (struct nand_page_0_boot_header *)ptr;
  87. int idx;
  88. /* fill header with 0x00 for first 56 entries then 0xff */
  89. memset(&hdr->fcb[0], 0x0, 56*sizeof(uint32_t));
  90. memset(&hdr->fcb[56], 0xff, 72*sizeof(uint32_t));
  91. /* fill SW ecc and padding with 0xff */
  92. memset(&hdr->sw_ecc[0], 0xff, sizeof(hdr->sw_ecc));
  93. memset(&hdr->padding[0], 0xff, sizeof(hdr->padding));
  94. /* fill IVT prefix with 0x00 */
  95. memset(&hdr->ivt_prefix[0], 0x00, sizeof(hdr->ivt_prefix));
  96. /* populate fcb */
  97. hdr->fcb[1] = 0x46434220; /* signature */
  98. hdr->fcb[2] = 0x00000001; /* version */
  99. hdr->fcb[5] = 2048; /* page size */
  100. hdr->fcb[6] = (2048+64); /* page + OOB size */
  101. hdr->fcb[7] = 64; /* pages per block */
  102. hdr->fcb[14] = 6; /* ECC mode 6 */
  103. hdr->fcb[26] = 128; /* fw address (0x40000) in 2K pages */
  104. hdr->fcb[27] = 128; /* fw address (0x40000) in 2K pages */
  105. hdr->fcb[30] = 0x0001ff00; /* DBBT search area start address */
  106. hdr->fcb[33] = 2048; /* BB marker physical offset */
  107. hdr->fcb[43] = 1; /* DISBBM */
  108. hdr->fcb[54] = 0; /* DISBB_Search */
  109. hdr->fcb[55] = 8; /* Bad block search limit */
  110. /* compute software ECC */
  111. for (idx = 0; idx < sizeof(hdr->fcb_bytes); idx++)
  112. hdr->sw_ecc[idx] = vybridimage_sw_ecc(hdr->fcb_bytes[idx]);
  113. }
  114. static void vybridimage_print_hdr_field(struct nand_page_0_boot_header *hdr,
  115. int idx)
  116. {
  117. printf("header.fcb[%d] = %08x\n", idx, hdr->fcb[idx]);
  118. }
  119. static void vybridimage_print_header(const void *ptr)
  120. {
  121. struct nand_page_0_boot_header *hdr =
  122. (struct nand_page_0_boot_header *)ptr;
  123. int idx;
  124. for (idx = 0; idx < 56; idx++)
  125. vybridimage_print_hdr_field(hdr, idx);
  126. }
  127. /*
  128. * vybridimage parameters
  129. */
  130. U_BOOT_IMAGE_TYPE(
  131. vybridimage,
  132. "Vybrid Boot Image",
  133. sizeof(vybridimage_header),
  134. (void *)&vybridimage_header,
  135. NULL,
  136. vybridimage_verify_header,
  137. vybridimage_print_header,
  138. vybridimage_set_header,
  139. NULL,
  140. vybridimage_check_image_types,
  141. NULL,
  142. NULL
  143. );