bootimg.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2018 Xilinx, Inc.
  4. */
  5. #include <common.h>
  6. #include <part.h>
  7. #include <asm/io.h>
  8. #include <asm/arch/hardware.h>
  9. #include <asm/arch/sys_proto.h>
  10. #include <u-boot/md5.h>
  11. #include <zynq_bootimg.h>
  12. DECLARE_GLOBAL_DATA_PTR;
  13. #define ZYNQ_IMAGE_PHDR_OFFSET 0x09C
  14. #define ZYNQ_IMAGE_FSBL_LEN_OFFSET 0x040
  15. #define ZYNQ_PART_HDR_CHKSUM_WORD_COUNT 0x0F
  16. #define ZYNQ_PART_HDR_WORD_COUNT 0x10
  17. #define ZYNQ_MAXIMUM_IMAGE_WORD_LEN 0x40000000
  18. #define MD5_CHECKSUM_SIZE 16
  19. struct headerarray {
  20. u32 fields[16];
  21. };
  22. /*
  23. * Check whether the given partition is last partition or not
  24. */
  25. static int zynq_islastpartition(struct headerarray *head)
  26. {
  27. int index;
  28. debug("%s\n", __func__);
  29. if (head->fields[ZYNQ_PART_HDR_CHKSUM_WORD_COUNT] != 0xFFFFFFFF)
  30. return -1;
  31. for (index = 0; index < ZYNQ_PART_HDR_WORD_COUNT - 1; index++) {
  32. if (head->fields[index] != 0x0)
  33. return -1;
  34. }
  35. return 0;
  36. }
  37. /*
  38. * Get the partition count from the partition header
  39. */
  40. int zynq_get_part_count(struct partition_hdr *part_hdr_info)
  41. {
  42. u32 count;
  43. struct headerarray *hap;
  44. debug("%s\n", __func__);
  45. for (count = 0; count < ZYNQ_MAX_PARTITION_NUMBER; count++) {
  46. hap = (struct headerarray *)&part_hdr_info[count];
  47. if (zynq_islastpartition(hap) != -1)
  48. break;
  49. }
  50. return count;
  51. }
  52. /*
  53. * Get the partition info of all the partitions available.
  54. */
  55. int zynq_get_partition_info(u32 image_base_addr, u32 *fsbl_len,
  56. struct partition_hdr *part_hdr)
  57. {
  58. u32 parthdroffset;
  59. *fsbl_len = *((u32 *)(image_base_addr + ZYNQ_IMAGE_FSBL_LEN_OFFSET));
  60. parthdroffset = *((u32 *)(image_base_addr + ZYNQ_IMAGE_PHDR_OFFSET));
  61. parthdroffset += image_base_addr;
  62. memcpy(part_hdr, (u32 *)parthdroffset,
  63. (sizeof(struct partition_hdr) * ZYNQ_MAX_PARTITION_NUMBER));
  64. return 0;
  65. }
  66. /*
  67. * Check whether the partition header is valid or not
  68. */
  69. int zynq_validate_hdr(struct partition_hdr *header)
  70. {
  71. struct headerarray *hap;
  72. u32 index;
  73. u32 checksum;
  74. debug("%s\n", __func__);
  75. hap = (struct headerarray *)header;
  76. for (index = 0; index < ZYNQ_PART_HDR_WORD_COUNT; index++) {
  77. if (hap->fields[index])
  78. break;
  79. }
  80. if (index == ZYNQ_PART_HDR_WORD_COUNT)
  81. return -1;
  82. checksum = 0;
  83. for (index = 0; index < ZYNQ_PART_HDR_CHKSUM_WORD_COUNT; index++)
  84. checksum += hap->fields[index];
  85. checksum ^= 0xFFFFFFFF;
  86. if (hap->fields[ZYNQ_PART_HDR_CHKSUM_WORD_COUNT] != checksum) {
  87. printf("Error: Checksum 0x%8.8x != 0x%8.8x\n",
  88. checksum, hap->fields[ZYNQ_PART_HDR_CHKSUM_WORD_COUNT]);
  89. return -1;
  90. }
  91. if (header->imagewordlen > ZYNQ_MAXIMUM_IMAGE_WORD_LEN) {
  92. printf("INVALID_PARTITION_LENGTH\n");
  93. return -1;
  94. }
  95. return 0;
  96. }
  97. /*
  98. * Validate the partition by calculationg the md5 checksum for the
  99. * partition and compare with checksum present in checksum offset of
  100. * partition
  101. */
  102. int zynq_validate_partition(u32 start_addr, u32 len, u32 chksum_off)
  103. {
  104. u8 checksum[MD5_CHECKSUM_SIZE];
  105. u8 calchecksum[MD5_CHECKSUM_SIZE];
  106. memcpy(&checksum[0], (u32 *)chksum_off, MD5_CHECKSUM_SIZE);
  107. md5_wd((u8 *)start_addr, len, &calchecksum[0], 0x10000);
  108. if (!memcmp(checksum, calchecksum, MD5_CHECKSUM_SIZE))
  109. return 0;
  110. printf("Error: Partition DataChecksum\n");
  111. return -1;
  112. }