bootimg.c 3.1 KB

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