bootimg.c 3.2 KB

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