mrdump_dconfig.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* Copyright Statement:
  2. *
  3. * This software/firmware and related documentation ("MediaTek Software") are
  4. * protected under relevant copyright laws. The information contained herein
  5. * is confidential and proprietary to MediaTek Inc. and/or its licensors.
  6. * Without the prior written permission of MediaTek inc. and/or its licensors,
  7. * any reproduction, modification, use or disclosure of MediaTek Software,
  8. * and information contained herein, in whole or in part, shall be strictly prohibited.
  9. */
  10. /* MediaTek Inc. (C) 2016. All rights reserved.
  11. *
  12. * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  13. * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  14. * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
  15. * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  18. * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  19. * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  20. * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
  21. * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
  22. * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
  23. * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
  24. * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  25. * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
  26. * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  27. * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  28. * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
  29. * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  30. */
  31. #include <pal_log.h>
  32. #include <pal_typedefs.h>
  33. #include <platform/verified_boot.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <verified_boot_common.h>
  37. #include <dev/mrdump.h>
  38. /* mrdump flow in dconfig */
  39. #if MTK_MRDUMP_SUPPORT == 2
  40. #define DCONFIG_PART "boot_para"
  41. #define DCONFIG_1STIMG_NAME "dconfig"
  42. #define DCONFIG_HEADER_SIZE 512
  43. #define DCONFIG_PLENV_SIZE 1024
  44. #ifdef MTK_SECURITY_SW_SUPPORT
  45. static uint8_t socid_chip[SOC_ID_LEN] = {0};
  46. #endif
  47. #define PLENV_SIG_SIZE 8
  48. #define PLENV_MRDUMP_STR "mrdump_enable=yes"
  49. #define PLENV_MRDUMP_LEN 18
  50. extern int mboot_common_load_part(char *part_name, char *img_name, unsigned long addr);
  51. static int mrdump_get_socid_cert(void)
  52. {
  53. uint8_t *dconfig_hdr = NULL;
  54. uint8_t *plenv = NULL;
  55. int len = 0;
  56. #ifdef MTK_SECURITY_SW_SUPPORT
  57. uint32_t sec_ret = sec_img_auth_init(DCONFIG_PART, DCONFIG_1STIMG_NAME,
  58. GET_SOCID_FROM_CERT2);
  59. if (sec_ret) {
  60. pal_log_err("%s: dconfig image auth init failed (0x%x)\n", __func__, sec_ret);
  61. return MRDUMP_SEC_IMG_AUTH_INIT_ERROR;
  62. }
  63. #endif
  64. dconfig_hdr = malloc(DCONFIG_HEADER_SIZE + DCONFIG_PLENV_SIZE);
  65. if (dconfig_hdr == NULL) {
  66. pal_log_err("%s: not enough memory\n", __func__);
  67. return MRDUMP_DCONFIG_MALLOC_ERROR;
  68. }
  69. len = mboot_common_load_part(DCONFIG_PART, DCONFIG_1STIMG_NAME,
  70. (unsigned long)dconfig_hdr);
  71. if (len <= 0) {
  72. pal_log_err("%s: partition_read failed, return value %d\n", __func__, len);
  73. free(dconfig_hdr);
  74. return MRDUMP_MBOOT_LOAD_PART_ERROR;
  75. }
  76. #ifdef MTK_SECURITY_SW_SUPPORT
  77. sec_ret = sec_img_auth(dconfig_hdr, len);
  78. if (sec_ret) {
  79. pal_log_err("%s: dconfig image verify failed (0x%x)\n", __func__, sec_ret);
  80. free(dconfig_hdr);
  81. return MRDUMP_DCONFIG_IMG_VERIFY_ERROR;
  82. }
  83. uint8_t socid_cert[SOC_ID_LEN] = {0};
  84. sec_ret = get_socid_cert(socid_cert, SOC_ID_LEN);
  85. if (sec_ret) {
  86. pal_log_err("%s: unable to get socid from certed image (0x%x)\n", __func__, sec_ret);
  87. free(dconfig_hdr);
  88. return MRDUMP_DCONFIG_SOCID_CERT_ERROR;
  89. }
  90. if (strncmp((char *)socid_chip, (char *)socid_cert, SOC_ID_LEN) != 0) {
  91. pal_log_err("%s: socid mismatched\n", __func__);
  92. free(dconfig_hdr);
  93. return MRDUMP_DCNFIG_SOCID_MISMATCH;
  94. }
  95. #endif
  96. char *mrdump_enable_str = (char *)dconfig_hdr + DCONFIG_HEADER_SIZE + PLENV_SIG_SIZE;
  97. if (strncmp(mrdump_enable_str, PLENV_MRDUMP_STR, PLENV_MRDUMP_LEN) != 0) {
  98. pal_log_err("%s: not a valid mrdump enabled image\n", __func__);
  99. free(dconfig_hdr);
  100. return MRDUMP_NOT_VALID_IMG;
  101. }
  102. pal_log_info("mrdump_get_socid_cert done.\n");
  103. free(dconfig_hdr);
  104. return MRDUMP_SUCCESS_ENABLE;
  105. }
  106. int mrdump_check_enable(void)
  107. {
  108. #ifndef MTK_SECURITY_SW_SUPPORT
  109. return 0;
  110. #else
  111. uint32_t sec_ret = get_socid_chip(socid_chip, SOC_ID_LEN);
  112. if (sec_ret) {
  113. pal_log_err("%s: unable to get socid from chip (0x%x)\n", __func__, sec_ret);
  114. return MRDUMP_NO_SOCID_FROM_CHIP;
  115. }
  116. return (mrdump_get_socid_cert());
  117. #endif
  118. }
  119. /* mrdump flow not in dconfig */
  120. #elif MTK_MRDUMP_SUPPORT == 1
  121. int mrdump_check_enable(void)
  122. {
  123. return MRDUMP_ALWAYS_ENABLE;
  124. }
  125. #else
  126. #error Unknown MTK_MRDUMP_SUPPORT value
  127. #endif