mntl_gpt.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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) 2015. 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 <platform/boot_mode.h>
  32. #include <platform/mntl.h>
  33. #include <platform/mntl_types.h>
  34. #include <platform/mntl_gpt.h>
  35. #include <debug.h>
  36. char *part_entry;
  37. GuidPartitionTableHeader_t pgpt_header;
  38. static int mt_get_sector_size(int *sector_size)
  39. {
  40. *sector_size = 4096;
  41. return 0;
  42. }
  43. static int mt_gpt_get_part_entry(struct mntl_handler *mntl, int sector_size)
  44. {
  45. char *ptr = NULL;
  46. int ret;
  47. struct mntl_scatter_list list[2];
  48. uint64_t len = 2 * sector_size;
  49. dprintf(CRITICAL, "len %llu\n", len);
  50. ptr = (char *)malloc(len);
  51. if (ptr == NULL) {
  52. dprintf(CRITICAL, "malloc fail\n");
  53. return 1;
  54. }
  55. list[0].address = ptr;
  56. list[0].size = len;
  57. list[1].size = 0;
  58. ret = mntl_read(mntl, 0, &list);
  59. if (ret < 0) {
  60. dprintf(CRITICAL, "read sector fail\n");
  61. free(ptr);
  62. return 1;
  63. }
  64. memcpy(&pgpt_header, &ptr[sector_size], sizeof(pgpt_header));
  65. free(ptr);
  66. dprintf(CRITICAL, "PGPT:\n");
  67. dprintf(CRITICAL, "Header CRC=%u\n", (unsigned int)pgpt_header.HeaderCRC32);
  68. dprintf(CRITICAL, "Header Size=%d\n", pgpt_header.HeaderSize);
  69. dprintf(CRITICAL, "Current LBA=%llu\n", pgpt_header.MyLBA);
  70. dprintf(CRITICAL, "Backup LBA=%llu\n", pgpt_header.AlternateLBA);
  71. dprintf(CRITICAL, "First usable LBA=%llu\n", pgpt_header.FirstUsableLBA);
  72. dprintf(CRITICAL, "Last usable LBA=%llu\n", pgpt_header.LastUsableLBA);
  73. dprintf(CRITICAL, "Starting PE LBA=%llu\n", pgpt_header.PartitionEntryLBA);
  74. dprintf(CRITICAL, "Number of PE=%d\n", pgpt_header.NumberOfPartitionEntries);
  75. dprintf(CRITICAL, "Size of PE=%d\n", pgpt_header.SizeOfPartitionEntry);
  76. dprintf(CRITICAL, "PE CRC=%u\n", pgpt_header.PartitionEntryArrayCRC32);
  77. len = ((uint64_t) pgpt_header.NumberOfPartitionEntries * (uint64_t)pgpt_header.SizeOfPartitionEntry);
  78. len = (len / sector_size + 1) * sector_size; /* align multiple of sector_size */
  79. if ((len >> 32) > 0) {
  80. dprintf(CRITICAL, "currently, we do not support allocate buffer size larger than 32-bit\n");
  81. return 1;
  82. }
  83. part_entry = (char *)malloc(len);
  84. if (part_entry == NULL) {
  85. dprintf(CRITICAL, "malloc fail\n");
  86. return 1;
  87. }
  88. list[0].address = part_entry;
  89. list[0].size = len;
  90. list[1].size = 0;
  91. ret = mntl_read(mntl, pgpt_header.PartitionEntryLBA, &list);
  92. if (ret < 0){
  93. dprintf(CRITICAL, "read PE fail\n");
  94. free(part_entry);
  95. part_entry = NULL;
  96. return 1;
  97. }
  98. return 0;
  99. }
  100. int mntl_gpt_init(struct mntl_handler *mntl)
  101. {
  102. GuidPartitionEntry_t *pe;
  103. int sector_size = 0;
  104. int i = 0;
  105. dprintf(CRITICAL, "read gpt start!\n");
  106. // get sector size
  107. if (mt_get_sector_size(&sector_size) != 0) {
  108. dprintf(CRITICAL, "Get sector size fail!\n");
  109. return 1;
  110. }
  111. // read partition entry
  112. if (mt_gpt_get_part_entry(mntl, sector_size) > 0) {
  113. dprintf(CRITICAL, "Get partition entry fail!\n");
  114. return 1;
  115. }
  116. dprintf(CRITICAL, "First usable LBA=%llu\n", pgpt_header.FirstUsableLBA);
  117. pe = (GuidPartitionEntry_t *)part_entry;
  118. for (i = 0; i < pgpt_header.NumberOfPartitionEntries; i++, pe++) {
  119. unsigned int j;
  120. char name[37];
  121. dprintf(CRITICAL, "Partition Entry %d\n", i + 1);
  122. dprintf(CRITICAL, "\tFirst LBA=%llu\n", pe->StartingLBA);
  123. dprintf(CRITICAL, "\tLast LBA=%llu\n", pe->EndingLBA);
  124. for (j = 0; j < 72 / sizeof(efi_char16_t); j++) {
  125. name[j] = (uint16_t)pe->PartitionName[j];
  126. }
  127. name[j] = 0;
  128. dprintf(CRITICAL, "\tName=%s\n", name);
  129. }
  130. return 0;
  131. }
  132. int mntl_read_gpt(char *part_name, uint64_t *StartLBA, uint64_t *EndLBA)
  133. {
  134. GuidPartitionEntry_t *pe;
  135. int i = 0;
  136. pe = (GuidPartitionEntry_t *)part_entry;
  137. for (i = 0; i < pgpt_header.NumberOfPartitionEntries; i++, pe++) {
  138. unsigned int j;
  139. char name[37];
  140. for (j = 0; j < 72 / sizeof(efi_char16_t); j++) {
  141. name[j] = (uint16_t)pe->PartitionName[j];
  142. }
  143. name[j] = 0;
  144. dprintf(CRITICAL, "\tName=%s\n", name);
  145. if (!strcmp(name,part_name)) {
  146. dprintf(CRITICAL, "Partition Entry %d\n", i + 1);
  147. dprintf(CRITICAL, "\tFirst LBA=%llu\n", pe->StartingLBA);
  148. dprintf(CRITICAL, "\tLast LBA=%llu\n", pe->EndingLBA);
  149. *StartLBA = pe->StartingLBA;
  150. *EndLBA = pe->EndingLBA;
  151. break;
  152. }
  153. }
  154. if (i == pgpt_header.NumberOfPartitionEntries) {
  155. dprintf(CRITICAL, "Cannot find partition %s\n", part_name);
  156. return 1;
  157. }
  158. return 0;
  159. }