vybridimage.c 4.2 KB

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