socfpga_ros.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright (c) 2024, Intel Corporation. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. /* system header files*/
  7. #include <assert.h>
  8. #include <endian.h>
  9. #include <string.h>
  10. /* CRC function header */
  11. #include <common/tf_crc32.h>
  12. /* Cadense qspi driver*/
  13. #include <qspi/cadence_qspi.h>
  14. /* Mailbox driver*/
  15. #include <socfpga_mailbox.h>
  16. #include <socfpga_ros.h>
  17. static void swap_bits(char *const data, uint32_t len)
  18. {
  19. uint32_t x, y;
  20. char tmp;
  21. for (x = 0U; x < len; x++) {
  22. tmp = 0U;
  23. for (y = 0U; y < 8; y++) {
  24. tmp <<= 1;
  25. if (data[x] & 1) {
  26. tmp |= 1;
  27. }
  28. data[x] >>= 1;
  29. }
  30. data[x] = tmp;
  31. }
  32. }
  33. static uint32_t get_current_image_index(spt_table_t *spt_buf, uint32_t *const img_index)
  34. {
  35. if (spt_buf == NULL || img_index == NULL) {
  36. return ROS_RET_INVALID;
  37. }
  38. uint32_t ret;
  39. unsigned long current_image;
  40. uint32_t rsu_status[RSU_STATUS_RES_SIZE];
  41. if (spt_buf->partitions < SPT_MIN_PARTITIONS || spt_buf->partitions > SPT_MAX_PARTITIONS) {
  42. return ROS_IMAGE_PARTNUM_OVFL;
  43. }
  44. ret = mailbox_rsu_status(rsu_status, RSU_STATUS_RES_SIZE);
  45. if (ret != MBOX_RET_OK) {
  46. return ROS_RET_NOT_RSU_MODE;
  47. }
  48. current_image = ADDR_64(rsu_status[1], rsu_status[0]);
  49. NOTICE("ROS: Current image is at 0x%08lx\n", current_image);
  50. *img_index = 0U;
  51. for (uint32_t index = 0U ; index < spt_buf->partitions; index++) {
  52. if (spt_buf->partition[index].offset == current_image) {
  53. *img_index = index;
  54. break;
  55. }
  56. }
  57. if (*img_index == 0U) {
  58. return ROS_IMAGE_INDEX_ERR;
  59. }
  60. return ROS_RET_OK;
  61. }
  62. static uint32_t load_and_check_spt(spt_table_t *spt_ptr, size_t offset)
  63. {
  64. if (spt_ptr == NULL || offset == 0U) {
  65. return ROS_RET_INVALID;
  66. }
  67. int ret;
  68. uint32_t calc_crc;
  69. static spt_table_t spt_data;
  70. ret = cad_qspi_read(spt_ptr, offset, SPT_SIZE);
  71. if (ret != 0U) {
  72. return ROS_QSPI_READ_ERROR;
  73. }
  74. if (spt_ptr->magic_number != SPT_MAGIC_NUMBER) {
  75. return ROS_SPT_BAD_MAGIC_NUM;
  76. }
  77. if (spt_ptr->partitions < SPT_MIN_PARTITIONS || spt_ptr->partitions > SPT_MAX_PARTITIONS) {
  78. return ROS_IMAGE_PARTNUM_OVFL;
  79. }
  80. memcpy_s(&spt_data, SPT_SIZE, spt_ptr, SPT_SIZE);
  81. spt_data.checksum = 0U;
  82. swap_bits((char *)&spt_data, SPT_SIZE);
  83. calc_crc = tf_crc32(0, (uint8_t *)&spt_data, SPT_SIZE);
  84. if (bswap32(spt_ptr->checksum) != calc_crc) {
  85. return ROS_SPT_CRC_ERROR;
  86. }
  87. NOTICE("ROS: SPT table at 0x%08lx is verified\n", offset);
  88. return ROS_RET_OK;
  89. }
  90. static uint32_t get_spt(spt_table_t *spt_buf)
  91. {
  92. if (spt_buf == NULL) {
  93. return ROS_RET_INVALID;
  94. }
  95. uint32_t ret;
  96. uint32_t spt_offset[RSU_GET_SPT_RESP_SIZE];
  97. /* Get SPT offset from SDM via mailbox commands */
  98. ret = mailbox_rsu_get_spt_offset(spt_offset, RSU_GET_SPT_RESP_SIZE);
  99. if (ret != MBOX_RET_OK) {
  100. WARN("ROS: Not booted in RSU mode\n");
  101. return ROS_RET_NOT_RSU_MODE;
  102. }
  103. /* Print the SPT table addresses */
  104. VERBOSE("ROS: SPT0 0x%08lx\n", ADDR_64(spt_offset[0], spt_offset[1]));
  105. VERBOSE("ROS: SPT1 0x%08lx\n", ADDR_64(spt_offset[2], spt_offset[3]));
  106. /* Load and validate SPT1*/
  107. ret = load_and_check_spt(spt_buf, ADDR_64(spt_offset[2], spt_offset[3]));
  108. if (ret != ROS_RET_OK) {
  109. /* Load and validate SPT0*/
  110. ret = load_and_check_spt(spt_buf, ADDR_64(spt_offset[0], spt_offset[1]));
  111. if (ret != ROS_RET_OK) {
  112. WARN("Both SPT tables are unusable\n");
  113. return ret;
  114. }
  115. }
  116. return ROS_RET_OK;
  117. }
  118. uint32_t ros_qspi_get_ssbl_offset(unsigned long *offset)
  119. {
  120. if (offset == NULL) {
  121. return ROS_RET_INVALID;
  122. }
  123. uint32_t ret, img_index;
  124. char ssbl_name[SPT_PARTITION_NAME_LENGTH];
  125. static spt_table_t spt;
  126. ret = get_spt(&spt);
  127. if (ret != ROS_RET_OK) {
  128. return ret;
  129. }
  130. ret = get_current_image_index(&spt, &img_index);
  131. if (ret != ROS_RET_OK) {
  132. return ret;
  133. }
  134. if (strncmp(spt.partition[img_index].name, FACTORY_IMAGE,
  135. SPT_PARTITION_NAME_LENGTH) == 0U) {
  136. strlcpy(ssbl_name, FACTORY_SSBL, SPT_PARTITION_NAME_LENGTH);
  137. } else {
  138. strlcpy(ssbl_name, spt.partition[img_index].name,
  139. SPT_PARTITION_NAME_LENGTH);
  140. strlcat(ssbl_name, SSBL_SUFFIX, SPT_PARTITION_NAME_LENGTH);
  141. }
  142. for (uint32_t index = 0U; index < spt.partitions; index++) {
  143. if (strncmp(spt.partition[index].name, ssbl_name,
  144. SPT_PARTITION_NAME_LENGTH) == 0U) {
  145. *offset = spt.partition[index].offset;
  146. NOTICE("ROS: Corresponding SSBL is at 0x%08lx\n", *offset);
  147. return ROS_RET_OK;
  148. }
  149. }
  150. return ROS_IMAGE_INDEX_ERR;
  151. }