img_info.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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) 2017. 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. * The following software/firmware and/or related documentation ("MediaTek Software")
  32. * have been modified by MediaTek Inc. All revisions are subject to any receiver\'s
  33. * applicable license agreements with MediaTek Inc.
  34. */
  35. #include <pal_typedefs.h>
  36. #include <pal_log.h>
  37. #include <error.h>
  38. #include <img_info.h>
  39. #include <verified_boot_error.h>
  40. #include <string.h>
  41. /* boot image parsing is not supported */
  42. uint32_t get_img_info(uint8_t *hdr_buf, uint32_t hdr_buf_sz, struct img_info *img_info)
  43. {
  44. uint32_t ret = STATUS_OK;
  45. union mkimg_hdr *mkimg_hdr = (union mkimg_hdr *)hdr_buf;
  46. memset(img_info, 0x0, sizeof(struct img_info));
  47. if (MKIMG_MAGIC == mkimg_hdr->info.magic) {
  48. uint32_t align_sz = 0;
  49. img_info->img_hdr_type = IMG_HDR_TYPE_RAW;
  50. /* for raw image type, one header always store information for one image */
  51. if (MKIMG_EXT_MAGIC == mkimg_hdr->info.ext_magic) {
  52. /* we don't reference image header for its size for now */
  53. img_info->img_hdr_sz = MKIMG_HDR_SZ;
  54. /* we don't reference image header version for parsing for now */
  55. /* it's fixed to 1 currently */
  56. img_info->img_hdr_ver = 1;
  57. img_info->img_type = mkimg_hdr->info.img_type;
  58. img_info->scrambled = mkimg_hdr->info.scrambled;
  59. img_info->img_load_mode = mkimg_hdr->info.mode;
  60. img_info->img_load_addr = mkimg_hdr->info.maddr;
  61. img_info->img_load_addr_high = mkimg_hdr->info.maddr_extend;
  62. img_info->img_sz_high = mkimg_hdr->info.dsz_extend;
  63. /* may cause overflow if img_sz overflow occurs after padding */
  64. /* image with size 0xffffffff00000000 ~ 0xffffffffffffffff is
  65. also not reasonable */
  66. if (img_info->img_sz_high == 0xffffffff)
  67. return ERR_VB_NOT_VALID_IMG_SZ;
  68. align_sz = mkimg_hdr->info.align_sz;
  69. if (align_sz > MAX_ALIGN_SZ)
  70. return ERR_VB_NOT_VALID_IMG_ALIGN_SZ;
  71. } else {
  72. img_info->img_hdr_sz = MKIMG_HDR_SZ;
  73. img_info->img_hdr_ver = 0;
  74. img_info->img_type = IMG_TYPE_IMG_AP_BIN;
  75. img_info->scrambled = 0;
  76. img_info->img_load_mode = mkimg_hdr->info.mode;
  77. img_info->img_load_addr = mkimg_hdr->info.maddr;
  78. img_info->img_load_addr_high = 0;
  79. img_info->img_sz_high = 0;
  80. align_sz = LEGACY_MKIMG_ALIGN_SZ;
  81. }
  82. /* dsz does not include image header nor padding */
  83. /* overflow handling, it is possible that image size is larger than 4GB */
  84. if (img_info->img_sz > (0xffffffff - (align_sz - 1)))
  85. img_info->img_sz_high++;
  86. img_info->img_dsz = mkimg_hdr->info.dsz;
  87. img_info->img_sz = ROUND_UP(mkimg_hdr->info.dsz, align_sz);
  88. memset(img_info->name, 0x0, MAX_NAME_SZ);
  89. memcpy(img_info->name, mkimg_hdr->info.name, MKIMG_NAME_SZ);
  90. return ret;
  91. }
  92. /* should not reach here, image type not suppoted */
  93. /* other fields are already filled with 0 */
  94. img_info->img_hdr_type = IMG_HDR_TYPE_UNKNOWN;
  95. ret = ERR_VB_IMG_TYPE_INVALID;
  96. return ret;
  97. }