img_info.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. uint32_t get_img_info(uint8_t *hdr_buf, uint32_t hdr_buf_sz, struct img_info *img_info)
  41. {
  42. uint32_t ret = STATUS_OK;
  43. union mkimg_hdr *mkimg_hdr = (union mkimg_hdr *)hdr_buf;
  44. struct bootimg_hdr *bootimg_hdr = (struct bootimg_hdr *)hdr_buf;
  45. memset(img_info, 0x0, sizeof(struct img_info));
  46. if (MKIMG_MAGIC == mkimg_hdr->info.magic) {
  47. uint32_t align_sz = 0;
  48. img_info->img_hdr_type = IMG_HDR_TYPE_RAW;
  49. /* for raw image type, one header always store information for one image */
  50. if (MKIMG_EXT_MAGIC == mkimg_hdr->info.ext_magic) {
  51. /* we don't reference image header for its size for now */
  52. img_info->img_hdr_sz = MKIMG_HDR_SZ;
  53. /* we don't reference image header version for parsing for now */
  54. /* it's fixed to 1 currently */
  55. img_info->img_hdr_ver = 1;
  56. img_info->img_type = mkimg_hdr->info.img_type;
  57. img_info->scrambled = mkimg_hdr->info.scrambled;
  58. img_info->img_load_mode = mkimg_hdr->info.mode;
  59. img_info->img_load_addr = mkimg_hdr->info.maddr;
  60. img_info->img_load_addr_high = mkimg_hdr->info.maddr_extend;
  61. img_info->img_sz_high = mkimg_hdr->info.dsz_extend;
  62. /* may cause overflow if img_sz overflow occurs after padding */
  63. /* image with size 0xffffffff00000000 ~ 0xffffffffffffffff is
  64. also not reasonable */
  65. if (img_info->img_sz_high == 0xffffffff)
  66. return ERR_VB_NOT_VALID_IMG_SZ;
  67. align_sz = mkimg_hdr->info.align_sz;
  68. if (align_sz > MAX_ALIGN_SZ)
  69. return ERR_VB_NOT_VALID_IMG_ALIGN_SZ;
  70. } else {
  71. img_info->img_hdr_sz = MKIMG_HDR_SZ;
  72. img_info->img_hdr_ver = 0;
  73. img_info->img_type = IMG_TYPE_IMG_AP_BIN;
  74. img_info->scrambled = 0;
  75. img_info->img_load_mode = mkimg_hdr->info.mode;
  76. img_info->img_load_addr = mkimg_hdr->info.maddr;
  77. img_info->img_load_addr_high = 0;
  78. img_info->img_sz_high = 0;
  79. align_sz = LEGACY_MKIMG_ALIGN_SZ;
  80. }
  81. /* dsz does not include image header nor padding */
  82. /* overflow handling, it is possible that image size is larger than 4GB */
  83. if (img_info->img_sz > (0xffffffff - (align_sz - 1)))
  84. img_info->img_sz_high++;
  85. img_info->img_dsz = mkimg_hdr->info.dsz;
  86. img_info->img_sz = ROUND_UP(mkimg_hdr->info.dsz, align_sz);
  87. memset(img_info->name, 0x0, MAX_NAME_SZ);
  88. memcpy(img_info->name, mkimg_hdr->info.name, MKIMG_NAME_SZ);
  89. return ret;
  90. }
  91. #if LOAD_BOOTIMG_SUPPORT
  92. if (!memcmp(bootimg_hdr->magic, BOOTIMG_MAGIC, strlen(BOOTIMG_MAGIC))) {
  93. uint32_t page_sz = bootimg_hdr->page_sz;
  94. if (page_sz > BOOTIMG_HDR_SZ)
  95. return ERR_VB_NOT_VALID_PAGE_SZ;
  96. img_info->img_hdr_type = IMG_HDR_TYPE_BOOT;
  97. img_info->img_hdr_sz = page_sz;
  98. img_info->img_hdr_ver = 0;
  99. img_info->img_type = IMG_TYPE_IMG_AP_BIN;
  100. img_info->scrambled = 0;
  101. /* image load info for kernel relocation */
  102. img_info->sub_img_load_mode[KENREL_SLOT] = DEFAULT_LOAD_MODE;
  103. img_info->sub_img_load_addr[KENREL_SLOT] = bootimg_hdr->kernel_addr;
  104. img_info->sub_img_load_addr_high[KENREL_SLOT] = 0;
  105. img_info->sub_img_sz[KENREL_SLOT] = ROUND_UP(bootimg_hdr->kernel_sz, page_sz);
  106. img_info->sub_img_sz_high[KENREL_SLOT] = 0;
  107. /* image load info for ramdisk relocation */
  108. img_info->sub_img_load_mode[RAMDISK_SLOT] = DEFAULT_LOAD_MODE;
  109. img_info->sub_img_load_addr[RAMDISK_SLOT] = bootimg_hdr->ramdisk_addr;
  110. img_info->sub_img_load_addr_high[RAMDISK_SLOT] = 0;
  111. img_info->sub_img_sz[RAMDISK_SLOT] = ROUND_UP(bootimg_hdr->ramdisk_sz, page_sz);
  112. img_info->sub_img_sz_high[RAMDISK_SLOT] = 0;
  113. /* image load info for second stage relocation */
  114. img_info->sub_img_load_mode[SECOND_STAGE_SLOT] = DEFAULT_LOAD_MODE;
  115. img_info->sub_img_load_addr[SECOND_STAGE_SLOT] = bootimg_hdr->second_addr;
  116. img_info->sub_img_load_addr_high[SECOND_STAGE_SLOT] = 0;
  117. img_info->sub_img_sz[SECOND_STAGE_SLOT] = ROUND_UP(bootimg_hdr->second_sz, page_sz);
  118. img_info->sub_img_sz_high[SECOND_STAGE_SLOT] = 0;
  119. /* image load info for boot image loading */
  120. img_info->img_load_mode = DEFAULT_LOAD_MODE;
  121. img_info->img_load_addr = EXTERNAL_LOAD_ADDR;
  122. img_info->img_load_addr_high = 0x0;
  123. img_info->img_sz = img_info->sub_img_sz[KENREL_SLOT] + \
  124. img_info->sub_img_sz[RAMDISK_SLOT] + \
  125. img_info->sub_img_sz[SECOND_STAGE_SLOT];
  126. img_info->img_sz_high = 0;
  127. img_info->img_dsz = img_info->img_sz;
  128. img_info->img_dsz_high = img_info->img_sz_high;
  129. memset(img_info->img_name, 0x0, MAX_NAME_SZ);
  130. memcpy(img_info->img_name, bootimg_hdr->name, BOOTIMG_NAME_SZ);
  131. return ret;
  132. }
  133. #endif
  134. /* should not reach here, image type not suppoted */
  135. /* other fields are already filled with 0 */
  136. img_info->img_hdr_type = IMG_HDR_TYPE_UNKNOWN;
  137. ret = ERR_VB_IMG_TYPE_INVALID;
  138. return ret;
  139. }