Mayank Grover | e358f1f | 2017-05-30 20:05:14 +0530 | [diff] [blame^] | 1 | /* Copyright (c) 2011-2017, The Linux Foundation. All rights reserved. |
Kinson Chik | 66552a8 | 2011-03-29 15:59:06 -0700 | [diff] [blame] | 2 | |
| 3 | * Redistribution and use in source and binary forms, with or without |
| 4 | * modification, are permitted provided that the following conditions are |
| 5 | * met: |
| 6 | * * Redistributions of source code must retain the above copyright |
| 7 | * notice, this list of conditions and the following disclaimer. |
| 8 | * * Redistributions in binary form must reproduce the above |
| 9 | * copyright notice, this list of conditions and the following |
| 10 | * disclaimer in the documentation and/or other materials provided |
| 11 | * with the distribution. |
Channagoud Kadabi | f73aa29 | 2013-01-31 16:55:20 -0800 | [diff] [blame] | 12 | * * Neither the name of The Linux Foundation. nor the names of its |
Kinson Chik | 66552a8 | 2011-03-29 15:59:06 -0700 | [diff] [blame] | 13 | * contributors may be used to endorse or promote products derived |
| 14 | * from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 20 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 23 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 24 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 25 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 26 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
vijay kumar | 24c9ed1 | 2015-04-28 13:05:04 +0530 | [diff] [blame] | 31 | #include <crc32.h> |
Kinson Chik | 66552a8 | 2011-03-29 15:59:06 -0700 | [diff] [blame] | 32 | #include "mmc.h" |
| 33 | #include "partition_parser.h" |
Mayank Grover | e358f1f | 2017-05-30 20:05:14 +0530 | [diff] [blame^] | 34 | #include "ab_partition_parser.h" |
| 35 | |
vijay kumar | 24c9ed1 | 2015-04-28 13:05:04 +0530 | [diff] [blame] | 36 | static bool flashing_gpt = 0; |
| 37 | static bool parse_secondary_gpt = 0; |
Sundarajan Srinivasan | 554863f | 2013-12-04 17:14:37 -0800 | [diff] [blame] | 38 | __WEAK void mmc_set_lun(uint8_t lun) |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | __WEAK uint8_t mmc_get_lun(void) |
| 43 | { |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | __WEAK void mmc_read_partition_table(uint8_t arg) |
| 48 | { |
| 49 | if(partition_read_table()) |
| 50 | { |
| 51 | dprintf(CRITICAL, "Error reading the partition table info\n"); |
| 52 | ASSERT(0); |
| 53 | } |
| 54 | } |
| 55 | |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 56 | static uint32_t mmc_boot_read_gpt(uint32_t block_size); |
| 57 | static uint32_t mmc_boot_read_mbr(uint32_t block_size); |
Channagoud Kadabi | 0e33285 | 2013-04-19 12:55:53 -0700 | [diff] [blame] | 58 | static void mbr_fill_name(struct partition_entry *partition_ent, |
| 59 | uint32_t type); |
| 60 | static uint32_t partition_verify_mbr_signature(uint32_t size, |
| 61 | uint8_t *buffer); |
| 62 | static uint32_t mbr_partition_get_type(uint32_t size, uint8_t *partition, |
| 63 | uint32_t *partition_type); |
| 64 | |
| 65 | static uint32_t partition_get_type(uint32_t size, uint8_t *partition, |
| 66 | uint32_t *partition_type); |
| 67 | static uint32_t partition_parse_gpt_header(uint8_t *buffer, |
| 68 | uint64_t *first_usable_lba, |
| 69 | uint32_t *partition_entry_size, |
| 70 | uint32_t *header_size, |
| 71 | uint32_t *max_partition_count); |
| 72 | |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 73 | static uint32_t write_mbr(uint32_t, uint8_t *mbrImage, uint32_t block_size); |
| 74 | static uint32_t write_gpt(uint32_t size, uint8_t *gptImage, uint32_t block_size); |
Channagoud Kadabi | 0e33285 | 2013-04-19 12:55:53 -0700 | [diff] [blame] | 75 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 76 | char *ext3_partitions[] = |
| 77 | { "system", "userdata", "persist", "cache", "tombstones" }; |
| 78 | char *vfat_partitions[] = { "modem", "mdm", "NONE" }; |
| 79 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 80 | unsigned int ext3_count = 0; |
| 81 | unsigned int vfat_count = 0; |
| 82 | |
Channagoud Kadabi | e9cb1c2 | 2013-11-12 13:49:41 -0800 | [diff] [blame] | 83 | struct partition_entry *partition_entries; |
Pavel Nedev | b5ba62d | 2013-07-22 11:57:41 +0300 | [diff] [blame] | 84 | static unsigned gpt_partitions_exist = 0; |
Sundarajan Srinivasan | 554863f | 2013-12-04 17:14:37 -0800 | [diff] [blame] | 85 | static unsigned partition_count; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 86 | |
Mayank Grover | e358f1f | 2017-05-30 20:05:14 +0530 | [diff] [blame^] | 87 | unsigned partition_get_partition_count() |
| 88 | { |
| 89 | return partition_count; |
| 90 | } |
| 91 | |
| 92 | struct partition_entry* partition_get_partition_entries() |
| 93 | { |
| 94 | return partition_entries; |
| 95 | } |
| 96 | |
Channagoud Kadabi | 0e33285 | 2013-04-19 12:55:53 -0700 | [diff] [blame] | 97 | unsigned int partition_read_table() |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 98 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 99 | unsigned int ret; |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 100 | uint32_t block_size; |
| 101 | |
| 102 | block_size = mmc_get_device_blocksize(); |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 103 | |
Channagoud Kadabi | e9cb1c2 | 2013-11-12 13:49:41 -0800 | [diff] [blame] | 104 | /* Allocate partition entries array */ |
Sundarajan Srinivasan | 554863f | 2013-12-04 17:14:37 -0800 | [diff] [blame] | 105 | if(!partition_entries) |
| 106 | { |
| 107 | partition_entries = (struct partition_entry *) calloc(NUM_PARTITIONS, sizeof(struct partition_entry)); |
| 108 | ASSERT(partition_entries); |
| 109 | } |
Channagoud Kadabi | e9cb1c2 | 2013-11-12 13:49:41 -0800 | [diff] [blame] | 110 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 111 | /* Read MBR of the card */ |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 112 | ret = mmc_boot_read_mbr(block_size); |
Channagoud Kadabi | 0e33285 | 2013-04-19 12:55:53 -0700 | [diff] [blame] | 113 | if (ret) { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 114 | dprintf(CRITICAL, "MMC Boot: MBR read failed!\n"); |
Channagoud Kadabi | 0e33285 | 2013-04-19 12:55:53 -0700 | [diff] [blame] | 115 | return 1; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 116 | } |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 117 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 118 | /* Read GPT of the card if exist */ |
| 119 | if (gpt_partitions_exist) { |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 120 | ret = mmc_boot_read_gpt(block_size); |
Channagoud Kadabi | 0e33285 | 2013-04-19 12:55:53 -0700 | [diff] [blame] | 121 | if (ret) { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 122 | dprintf(CRITICAL, "MMC Boot: GPT read failed!\n"); |
Channagoud Kadabi | 0e33285 | 2013-04-19 12:55:53 -0700 | [diff] [blame] | 123 | return 1; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 124 | } |
| 125 | } |
Mayank Grover | e358f1f | 2017-05-30 20:05:14 +0530 | [diff] [blame^] | 126 | |
| 127 | /* Scan of multislot support */ |
| 128 | /* TODO: Move this to mmc_boot_read_gpt() */ |
| 129 | partition_scan_for_multislot(); |
| 130 | |
Channagoud Kadabi | 0e33285 | 2013-04-19 12:55:53 -0700 | [diff] [blame] | 131 | return 0; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /* |
| 135 | * Read MBR from MMC card and fill partition table. |
| 136 | */ |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 137 | static unsigned int mmc_boot_read_mbr(uint32_t block_size) |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 138 | { |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 139 | uint8_t *buffer = NULL; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 140 | unsigned int dtype; |
| 141 | unsigned int dfirstsec; |
| 142 | unsigned int EBR_first_sec; |
| 143 | unsigned int EBR_current_sec; |
Channagoud Kadabi | 0e33285 | 2013-04-19 12:55:53 -0700 | [diff] [blame] | 144 | int ret = 0; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 145 | int idx, i; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 146 | |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 147 | buffer = (uint8_t *)memalign(CACHE_LINE, ROUNDUP(block_size, CACHE_LINE)); |
| 148 | |
| 149 | if (!buffer) |
| 150 | { |
| 151 | dprintf(CRITICAL, "Error allocating memory while reading partition table\n"); |
| 152 | ret = -1; |
| 153 | goto end; |
| 154 | } |
| 155 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 156 | /* Print out the MBR first */ |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 157 | ret = mmc_read(0, (unsigned int *)buffer, block_size); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 158 | if (ret) { |
| 159 | dprintf(CRITICAL, "Could not read partition from mmc\n"); |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 160 | goto end; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 161 | } |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 162 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 163 | /* Check to see if signature exists */ |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 164 | ret = partition_verify_mbr_signature(block_size, buffer); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 165 | if (ret) { |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 166 | goto end; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 167 | } |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 168 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 169 | /* |
| 170 | * Process each of the four partitions in the MBR by reading the table |
| 171 | * information into our mbr table. |
| 172 | */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 173 | idx = TABLE_ENTRY_0; |
| 174 | for (i = 0; i < 4; i++) { |
| 175 | /* Type 0xEE indicates end of MBR and GPT partitions exist */ |
| 176 | dtype = buffer[idx + i * TABLE_ENTRY_SIZE + OFFSET_TYPE]; |
| 177 | if (dtype == MBR_PROTECTED_TYPE) { |
| 178 | gpt_partitions_exist = 1; |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 179 | goto end; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 180 | } |
| 181 | partition_entries[partition_count].dtype = dtype; |
| 182 | partition_entries[partition_count].attribute_flag = |
| 183 | buffer[idx + i * TABLE_ENTRY_SIZE + OFFSET_STATUS]; |
| 184 | partition_entries[partition_count].first_lba = |
| 185 | GET_LWORD_FROM_BYTE(&buffer[idx + |
| 186 | i * TABLE_ENTRY_SIZE + |
| 187 | OFFSET_FIRST_SEC]); |
| 188 | partition_entries[partition_count].size = |
| 189 | GET_LWORD_FROM_BYTE(&buffer[idx + |
| 190 | i * TABLE_ENTRY_SIZE + |
| 191 | OFFSET_SIZE]); |
| 192 | dfirstsec = partition_entries[partition_count].first_lba; |
| 193 | mbr_fill_name(&partition_entries[partition_count], |
| 194 | partition_entries[partition_count].dtype); |
| 195 | partition_count++; |
| 196 | if (partition_count == NUM_PARTITIONS) |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 197 | goto end; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 198 | } |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 199 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 200 | /* See if the last partition is EBR, if not, parsing is done */ |
| 201 | if (dtype != MBR_EBR_TYPE) { |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 202 | goto end; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 203 | } |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 204 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 205 | EBR_first_sec = dfirstsec; |
| 206 | EBR_current_sec = dfirstsec; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 207 | |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 208 | ret = mmc_read((EBR_first_sec * block_size), (unsigned int *)buffer, block_size); |
Channagoud Kadabi | 0e33285 | 2013-04-19 12:55:53 -0700 | [diff] [blame] | 209 | if (ret) |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 210 | goto end; |
Channagoud Kadabi | 0e33285 | 2013-04-19 12:55:53 -0700 | [diff] [blame] | 211 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 212 | /* Loop to parse the EBR */ |
| 213 | for (i = 0;; i++) { |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 214 | ret = partition_verify_mbr_signature(block_size, buffer); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 215 | if (ret) { |
Channagoud Kadabi | 0e33285 | 2013-04-19 12:55:53 -0700 | [diff] [blame] | 216 | ret = 0; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 217 | break; |
| 218 | } |
| 219 | partition_entries[partition_count].attribute_flag = |
| 220 | buffer[TABLE_ENTRY_0 + OFFSET_STATUS]; |
| 221 | partition_entries[partition_count].dtype = |
| 222 | buffer[TABLE_ENTRY_0 + OFFSET_TYPE]; |
| 223 | partition_entries[partition_count].first_lba = |
| 224 | GET_LWORD_FROM_BYTE(&buffer[TABLE_ENTRY_0 + |
| 225 | OFFSET_FIRST_SEC]) + |
| 226 | EBR_current_sec; |
| 227 | partition_entries[partition_count].size = |
| 228 | GET_LWORD_FROM_BYTE(&buffer[TABLE_ENTRY_0 + OFFSET_SIZE]); |
| 229 | mbr_fill_name(&(partition_entries[partition_count]), |
| 230 | partition_entries[partition_count].dtype); |
| 231 | partition_count++; |
| 232 | if (partition_count == NUM_PARTITIONS) |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 233 | goto end; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 234 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 235 | dfirstsec = |
| 236 | GET_LWORD_FROM_BYTE(&buffer |
| 237 | [TABLE_ENTRY_1 + OFFSET_FIRST_SEC]); |
| 238 | if (dfirstsec == 0) { |
| 239 | /* Getting to the end of the EBR tables */ |
| 240 | break; |
| 241 | } |
| 242 | /* More EBR to follow - read in the next EBR sector */ |
| 243 | dprintf(SPEW, "Reading EBR block from 0x%X\n", EBR_first_sec |
| 244 | + dfirstsec); |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 245 | ret = mmc_read(((EBR_first_sec + dfirstsec) * block_size),(unsigned int *)buffer, |
| 246 | block_size); |
Channagoud Kadabi | 0e33285 | 2013-04-19 12:55:53 -0700 | [diff] [blame] | 247 | if (ret) |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 248 | goto end; |
Channagoud Kadabi | 0e33285 | 2013-04-19 12:55:53 -0700 | [diff] [blame] | 249 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 250 | EBR_current_sec = EBR_first_sec + dfirstsec; |
| 251 | } |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 252 | end: |
| 253 | if (buffer) |
| 254 | free(buffer); |
| 255 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 256 | return ret; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 257 | } |
Kinson Chik | 66552a8 | 2011-03-29 15:59:06 -0700 | [diff] [blame] | 258 | |
| 259 | /* |
| 260 | * Read GPT from MMC and fill partition table |
| 261 | */ |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 262 | static unsigned int mmc_boot_read_gpt(uint32_t block_size) |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 263 | { |
Channagoud Kadabi | 0e33285 | 2013-04-19 12:55:53 -0700 | [diff] [blame] | 264 | int ret = 0; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 265 | unsigned int header_size; |
| 266 | unsigned long long first_usable_lba; |
| 267 | unsigned long long backup_header_lba; |
Neeti Desai | ce6ad9a | 2012-03-21 18:57:59 -0700 | [diff] [blame] | 268 | unsigned long long card_size_sec; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 269 | unsigned int max_partition_count = 0; |
| 270 | unsigned int partition_entry_size; |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 271 | unsigned int i = 0; /* Counter for each block */ |
| 272 | unsigned int j = 0; /* Counter for each entry in a block */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 273 | unsigned int n = 0; /* Counter for UTF-16 -> 8 conversion */ |
| 274 | unsigned char UTF16_name[MAX_GPT_NAME_SIZE]; |
| 275 | /* LBA of first partition -- 1 Block after Protected MBR + 1 for PT */ |
Neeti Desai | ce6ad9a | 2012-03-21 18:57:59 -0700 | [diff] [blame] | 276 | unsigned long long partition_0; |
Channagoud Kadabi | 0e33285 | 2013-04-19 12:55:53 -0700 | [diff] [blame] | 277 | uint64_t device_density; |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 278 | uint8_t *data = NULL; |
| 279 | uint32_t part_entry_cnt = block_size / ENTRY_SIZE; |
Channagoud Kadabi | 0e33285 | 2013-04-19 12:55:53 -0700 | [diff] [blame] | 280 | |
Channagoud Kadabi | 0e33285 | 2013-04-19 12:55:53 -0700 | [diff] [blame] | 281 | /* Get the density of the mmc device */ |
| 282 | |
| 283 | device_density = mmc_get_device_capacity(); |
| 284 | |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 285 | data = (uint8_t *)memalign(CACHE_LINE, ROUNDUP(block_size, CACHE_LINE)); |
| 286 | if (!data) |
| 287 | { |
| 288 | dprintf(CRITICAL, "Failed to Allocate memory to read partition table\n"); |
| 289 | ret = -1; |
| 290 | goto end; |
| 291 | } |
| 292 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 293 | /* Print out the GPT first */ |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 294 | ret = mmc_read(block_size, (unsigned int *)data, block_size); |
Channagoud Kadabi | 0e33285 | 2013-04-19 12:55:53 -0700 | [diff] [blame] | 295 | if (ret) |
Channagoud Kadabi | 39f1352 | 2013-07-31 14:46:22 -0700 | [diff] [blame] | 296 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 297 | dprintf(CRITICAL, "GPT: Could not read primary gpt from mmc\n"); |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 298 | goto end; |
Channagoud Kadabi | 39f1352 | 2013-07-31 14:46:22 -0700 | [diff] [blame] | 299 | } |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 300 | ret = partition_parse_gpt_header(data, &first_usable_lba, |
| 301 | &partition_entry_size, &header_size, |
| 302 | &max_partition_count); |
| 303 | if (ret) { |
| 304 | dprintf(INFO, "GPT: (WARNING) Primary signature invalid\n"); |
Kinson Chik | 4d7444f | 2011-09-13 15:48:51 -0700 | [diff] [blame] | 305 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 306 | /* Check the backup gpt */ |
Neeti Desai | ce6ad9a | 2012-03-21 18:57:59 -0700 | [diff] [blame] | 307 | |
| 308 | /* Get size of MMC */ |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 309 | card_size_sec = (device_density) / block_size; |
Neeti Desai | ce6ad9a | 2012-03-21 18:57:59 -0700 | [diff] [blame] | 310 | ASSERT (card_size_sec > 0); |
| 311 | |
| 312 | backup_header_lba = card_size_sec - 1; |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 313 | ret = mmc_read((backup_header_lba * block_size), (unsigned int *)data, |
| 314 | block_size); |
Kinson Chik | 4d7444f | 2011-09-13 15:48:51 -0700 | [diff] [blame] | 315 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 316 | if (ret) { |
| 317 | dprintf(CRITICAL, |
| 318 | "GPT: Could not read backup gpt from mmc\n"); |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 319 | goto end; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 320 | } |
vijay kumar | ed75ae4 | 2015-07-22 12:42:06 +0530 | [diff] [blame] | 321 | parse_secondary_gpt = 1; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 322 | ret = partition_parse_gpt_header(data, &first_usable_lba, |
| 323 | &partition_entry_size, |
| 324 | &header_size, |
| 325 | &max_partition_count); |
| 326 | if (ret) { |
| 327 | dprintf(CRITICAL, |
| 328 | "GPT: Primary and backup signatures invalid\n"); |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 329 | goto end; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 330 | } |
vijay kumar | ed75ae4 | 2015-07-22 12:42:06 +0530 | [diff] [blame] | 331 | parse_secondary_gpt = 0; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 332 | } |
Neeti Desai | ce6ad9a | 2012-03-21 18:57:59 -0700 | [diff] [blame] | 333 | partition_0 = GET_LLWORD_FROM_BYTE(&data[PARTITION_ENTRIES_OFFSET]); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 334 | /* Read GPT Entries */ |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 335 | for (i = 0; i < (ROUNDUP(max_partition_count, part_entry_cnt)) / part_entry_cnt; i++) { |
neetid | 6c38de1 | 2011-12-02 12:04:50 -0800 | [diff] [blame] | 336 | ASSERT(partition_count < NUM_PARTITIONS); |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 337 | ret = mmc_read((partition_0 * block_size) + (i * block_size), |
| 338 | (uint32_t *) data, block_size); |
Kinson Chik | 66552a8 | 2011-03-29 15:59:06 -0700 | [diff] [blame] | 339 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 340 | if (ret) { |
| 341 | dprintf(CRITICAL, |
| 342 | "GPT: mmc read card failed reading partition entries.\n"); |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 343 | goto end; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 344 | } |
Kinson Chik | 66552a8 | 2011-03-29 15:59:06 -0700 | [diff] [blame] | 345 | |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 346 | for (j = 0; j < part_entry_cnt; j++) { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 347 | memcpy(&(partition_entries[partition_count].type_guid), |
| 348 | &data[(j * partition_entry_size)], |
| 349 | PARTITION_TYPE_GUID_SIZE); |
| 350 | if (partition_entries[partition_count].type_guid[0] == |
| 351 | 0x00 |
| 352 | && partition_entries[partition_count]. |
| 353 | type_guid[1] == 0x00) { |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 354 | i = ROUNDUP(max_partition_count, part_entry_cnt); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 355 | break; |
| 356 | } |
| 357 | memcpy(& |
| 358 | (partition_entries[partition_count]. |
| 359 | unique_partition_guid), |
| 360 | &data[(j * partition_entry_size) + |
| 361 | UNIQUE_GUID_OFFSET], |
| 362 | UNIQUE_PARTITION_GUID_SIZE); |
| 363 | partition_entries[partition_count].first_lba = |
| 364 | GET_LLWORD_FROM_BYTE(&data |
| 365 | [(j * partition_entry_size) + |
| 366 | FIRST_LBA_OFFSET]); |
| 367 | partition_entries[partition_count].last_lba = |
| 368 | GET_LLWORD_FROM_BYTE(&data |
| 369 | [(j * partition_entry_size) + |
| 370 | LAST_LBA_OFFSET]); |
| 371 | partition_entries[partition_count].size = |
| 372 | partition_entries[partition_count].last_lba - |
Neeti Desai | 4b8c1df | 2012-03-21 13:15:14 -0700 | [diff] [blame] | 373 | partition_entries[partition_count].first_lba + 1; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 374 | partition_entries[partition_count].attribute_flag = |
| 375 | GET_LLWORD_FROM_BYTE(&data |
| 376 | [(j * partition_entry_size) + |
| 377 | ATTRIBUTE_FLAG_OFFSET]); |
Kinson Chik | 66552a8 | 2011-03-29 15:59:06 -0700 | [diff] [blame] | 378 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 379 | memset(&UTF16_name, 0x00, MAX_GPT_NAME_SIZE); |
| 380 | memcpy(UTF16_name, &data[(j * partition_entry_size) + |
| 381 | PARTITION_NAME_OFFSET], |
| 382 | MAX_GPT_NAME_SIZE); |
Sundarajan Srinivasan | 554863f | 2013-12-04 17:14:37 -0800 | [diff] [blame] | 383 | partition_entries[partition_count].lun = mmc_get_lun(); |
| 384 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 385 | /* |
| 386 | * Currently partition names in *.xml are UTF-8 and lowercase |
| 387 | * Only supporting english for now so removing 2nd byte of UTF-16 |
| 388 | */ |
| 389 | for (n = 0; n < MAX_GPT_NAME_SIZE / 2; n++) { |
| 390 | partition_entries[partition_count].name[n] = |
| 391 | UTF16_name[n * 2]; |
| 392 | } |
| 393 | partition_count++; |
| 394 | } |
| 395 | } |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 396 | end: |
| 397 | if (data) |
| 398 | free(data); |
| 399 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 400 | return ret; |
Kinson Chik | 66552a8 | 2011-03-29 15:59:06 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 403 | static unsigned int write_mbr_in_blocks(uint32_t size, uint8_t *mbrImage, uint32_t block_size) |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 404 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 405 | unsigned int dtype; |
| 406 | unsigned int dfirstsec; |
| 407 | unsigned int ebrSectorOffset; |
| 408 | unsigned char *ebrImage; |
| 409 | unsigned char *lastAddress; |
| 410 | int idx, i; |
| 411 | unsigned int ret; |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 412 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 413 | /* Write the first block */ |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 414 | ret = mmc_write(0, block_size, (unsigned int *)mbrImage); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 415 | if (ret) { |
| 416 | dprintf(CRITICAL, "Failed to write mbr partition\n"); |
| 417 | goto end; |
| 418 | } |
| 419 | dprintf(SPEW, "write of first MBR block ok\n"); |
| 420 | /* |
| 421 | Loop through the MBR table to see if there is an EBR. |
| 422 | If found, then figure out where to write the first EBR |
| 423 | */ |
| 424 | idx = TABLE_ENTRY_0; |
| 425 | for (i = 0; i < 4; i++) { |
| 426 | dtype = mbrImage[idx + i * TABLE_ENTRY_SIZE + OFFSET_TYPE]; |
| 427 | if (MBR_EBR_TYPE == dtype) { |
| 428 | dprintf(SPEW, "EBR found.\n"); |
| 429 | break; |
| 430 | } |
| 431 | } |
| 432 | if (MBR_EBR_TYPE != dtype) { |
| 433 | dprintf(SPEW, "No EBR in this image\n"); |
| 434 | goto end; |
| 435 | } |
| 436 | /* EBR exists. Write each EBR block to mmc */ |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 437 | ebrImage = mbrImage + block_size; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 438 | ebrSectorOffset = |
| 439 | GET_LWORD_FROM_BYTE(&mbrImage |
| 440 | [idx + i * TABLE_ENTRY_SIZE + |
| 441 | OFFSET_FIRST_SEC]); |
| 442 | dfirstsec = 0; |
| 443 | dprintf(SPEW, "first EBR to be written at sector 0x%X\n", dfirstsec); |
| 444 | lastAddress = mbrImage + size; |
| 445 | while (ebrImage < lastAddress) { |
| 446 | dprintf(SPEW, "writing to 0x%X\n", |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 447 | (ebrSectorOffset + dfirstsec) * block_size); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 448 | ret = |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 449 | mmc_write((ebrSectorOffset + dfirstsec) * block_size, |
| 450 | block_size, (unsigned int *)ebrImage); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 451 | if (ret) { |
| 452 | dprintf(CRITICAL, |
| 453 | "Failed to write EBR block to sector 0x%X\n", |
| 454 | dfirstsec); |
| 455 | goto end; |
| 456 | } |
| 457 | dfirstsec = |
| 458 | GET_LWORD_FROM_BYTE(&ebrImage |
| 459 | [TABLE_ENTRY_1 + OFFSET_FIRST_SEC]); |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 460 | ebrImage += block_size; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 461 | } |
| 462 | dprintf(INFO, "MBR written to mmc successfully\n"); |
| 463 | end: |
| 464 | return ret; |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | /* Write the MBR/EBR to the MMC. */ |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 468 | static unsigned int write_mbr(uint32_t size, uint8_t *mbrImage, uint32_t block_size) |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 469 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 470 | unsigned int ret; |
Vijay Kumar Pendoti | 17c3853 | 2016-05-04 20:27:52 +0530 | [diff] [blame] | 471 | uint64_t device_density; |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 472 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 473 | /* Verify that passed in block is a valid MBR */ |
| 474 | ret = partition_verify_mbr_signature(size, mbrImage); |
| 475 | if (ret) { |
| 476 | goto end; |
| 477 | } |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 478 | |
Vijay Kumar Pendoti | 17c3853 | 2016-05-04 20:27:52 +0530 | [diff] [blame] | 479 | device_density = mmc_get_device_capacity(); |
| 480 | |
| 481 | /* Erasing the eMMC card before writing */ |
| 482 | ret = mmc_erase_card(0x00000000, device_density); |
| 483 | if (ret) { |
| 484 | dprintf(CRITICAL, "Failed to erase the eMMC card\n"); |
| 485 | goto end; |
| 486 | } |
| 487 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 488 | /* Write the MBR/EBR to mmc */ |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 489 | ret = write_mbr_in_blocks(size, mbrImage, block_size); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 490 | if (ret) { |
| 491 | dprintf(CRITICAL, "Failed to write MBR block to mmc.\n"); |
| 492 | goto end; |
| 493 | } |
| 494 | /* Re-read the MBR partition into mbr table */ |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 495 | ret = mmc_boot_read_mbr(block_size); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 496 | if (ret) { |
| 497 | dprintf(CRITICAL, "Failed to re-read mbr partition.\n"); |
| 498 | goto end; |
| 499 | } |
| 500 | partition_dump(); |
| 501 | end: |
| 502 | return ret; |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | /* |
| 506 | * A8h reflected is 15h, i.e. 10101000 <--> 00010101 |
| 507 | */ |
| 508 | int reflect(int data, int len) |
| 509 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 510 | int ref = 0; |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 511 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 512 | for (int i = 0; i < len; i++) { |
| 513 | if (data & 0x1) { |
| 514 | ref |= (1 << ((len - 1) - i)); |
| 515 | } |
| 516 | data = (data >> 1); |
| 517 | } |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 518 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 519 | return ref; |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | /* |
| 523 | * Function to calculate the CRC32 |
| 524 | */ |
| 525 | unsigned int calculate_crc32(unsigned char *buffer, int len) |
| 526 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 527 | int byte_length = 8; /*length of unit (i.e. byte) */ |
| 528 | int msb = 0; |
Channagoud Kadabi | 39f1352 | 2013-07-31 14:46:22 -0700 | [diff] [blame] | 529 | int polynomial = 0x04C11DB7; /* IEEE 32bit polynomial */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 530 | unsigned int regs = 0xFFFFFFFF; /* init to all ones */ |
| 531 | int regs_mask = 0xFFFFFFFF; /* ensure only 32 bit answer */ |
| 532 | int regs_msb = 0; |
| 533 | unsigned int reflected_regs; |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 534 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 535 | for (int i = 0; i < len; i++) { |
| 536 | int data_byte = buffer[i]; |
| 537 | data_byte = reflect(data_byte, 8); |
| 538 | for (int j = 0; j < byte_length; j++) { |
| 539 | msb = data_byte >> (byte_length - 1); /* get MSB */ |
| 540 | msb &= 1; /* ensure just 1 bit */ |
| 541 | regs_msb = (regs >> 31) & 1; /* MSB of regs */ |
| 542 | regs = regs << 1; /* shift regs for CRC-CCITT */ |
| 543 | if (regs_msb ^ msb) { /* MSB is a 1 */ |
| 544 | regs = regs ^ polynomial; /* XOR with generator poly */ |
| 545 | } |
| 546 | regs = regs & regs_mask; /* Mask off excess upper bits */ |
| 547 | data_byte <<= 1; /* get to next bit */ |
| 548 | } |
| 549 | } |
| 550 | regs = regs & regs_mask; |
| 551 | reflected_regs = reflect(regs, 32) ^ 0xFFFFFFFF; |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 552 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 553 | return reflected_regs; |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | /* |
| 557 | * Write the GPT Partition Entry Array to the MMC. |
| 558 | */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 559 | static unsigned int |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 560 | write_gpt_partition_array(uint8_t *header, |
| 561 | uint32_t partition_array_start, |
| 562 | uint32_t array_size, |
| 563 | uint32_t block_size) |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 564 | { |
Channagoud Kadabi | 0e33285 | 2013-04-19 12:55:53 -0700 | [diff] [blame] | 565 | unsigned int ret = 1; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 566 | unsigned long long partition_entry_lba; |
| 567 | unsigned long long partition_entry_array_start_location; |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 568 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 569 | partition_entry_lba = |
| 570 | GET_LLWORD_FROM_BYTE(&header[PARTITION_ENTRIES_OFFSET]); |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 571 | partition_entry_array_start_location = partition_entry_lba * block_size; |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 572 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 573 | ret = mmc_write(partition_entry_array_start_location, array_size, |
| 574 | (unsigned int *)partition_array_start); |
| 575 | if (ret) { |
| 576 | dprintf(CRITICAL, |
| 577 | "GPT: FAILED to write the partition entry array\n"); |
| 578 | goto end; |
| 579 | } |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 580 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 581 | end: |
| 582 | return ret; |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 583 | } |
| 584 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 585 | static void |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 586 | patch_gpt(uint8_t *gptImage, uint64_t density, uint32_t array_size, |
| 587 | uint32_t max_part_count, uint32_t part_entry_size, uint32_t block_size) |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 588 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 589 | unsigned int partition_entry_array_start; |
| 590 | unsigned char *primary_gpt_header; |
| 591 | unsigned char *secondary_gpt_header; |
| 592 | unsigned int offset; |
| 593 | unsigned long long card_size_sec; |
| 594 | int total_part = 0; |
| 595 | unsigned int last_part_offset; |
| 596 | unsigned int crc_value; |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 597 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 598 | /* Get size of MMC */ |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 599 | card_size_sec = (density) / block_size; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 600 | /* Working around cap at 4GB */ |
| 601 | if (card_size_sec == 0) { |
| 602 | card_size_sec = 4 * 1024 * 1024 * 2 - 1; |
| 603 | } |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 604 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 605 | /* Patching primary header */ |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 606 | primary_gpt_header = (gptImage + block_size); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 607 | PUT_LONG_LONG(primary_gpt_header + BACKUP_HEADER_OFFSET, |
| 608 | ((long long)(card_size_sec - 1))); |
| 609 | PUT_LONG_LONG(primary_gpt_header + LAST_USABLE_LBA_OFFSET, |
| 610 | ((long long)(card_size_sec - 34))); |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 611 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 612 | /* Patching backup GPT */ |
| 613 | offset = (2 * array_size); |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 614 | secondary_gpt_header = offset + block_size + primary_gpt_header; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 615 | PUT_LONG_LONG(secondary_gpt_header + PRIMARY_HEADER_OFFSET, |
| 616 | ((long long)(card_size_sec - 1))); |
| 617 | PUT_LONG_LONG(secondary_gpt_header + LAST_USABLE_LBA_OFFSET, |
| 618 | ((long long)(card_size_sec - 34))); |
| 619 | PUT_LONG_LONG(secondary_gpt_header + PARTITION_ENTRIES_OFFSET, |
| 620 | ((long long)(card_size_sec - 33))); |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 621 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 622 | /* Find last partition */ |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 623 | while (*(primary_gpt_header + block_size + total_part * ENTRY_SIZE) != |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 624 | 0) { |
| 625 | total_part++; |
| 626 | } |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 627 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 628 | /* Patching last partition */ |
| 629 | last_part_offset = |
| 630 | (total_part - 1) * ENTRY_SIZE + PARTITION_ENTRY_LAST_LBA; |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 631 | PUT_LONG_LONG(primary_gpt_header + block_size + last_part_offset, |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 632 | (long long)(card_size_sec - 34)); |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 633 | PUT_LONG_LONG(primary_gpt_header + block_size + last_part_offset + |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 634 | array_size, (long long)(card_size_sec - 34)); |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 635 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 636 | /* Updating CRC of the Partition entry array in both headers */ |
vijay kumar | 4f4405f | 2014-08-08 11:49:53 +0530 | [diff] [blame] | 637 | partition_entry_array_start = (unsigned int)primary_gpt_header + block_size; |
| 638 | crc_value = calculate_crc32((unsigned char *)partition_entry_array_start, |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 639 | max_part_count * part_entry_size); |
| 640 | PUT_LONG(primary_gpt_header + PARTITION_CRC_OFFSET, crc_value); |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 641 | |
vijay kumar | 4f4405f | 2014-08-08 11:49:53 +0530 | [diff] [blame] | 642 | crc_value = calculate_crc32((unsigned char *)partition_entry_array_start + array_size, |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 643 | max_part_count * part_entry_size); |
| 644 | PUT_LONG(secondary_gpt_header + PARTITION_CRC_OFFSET, crc_value); |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 645 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 646 | /* Clearing CRC fields to calculate */ |
| 647 | PUT_LONG(primary_gpt_header + HEADER_CRC_OFFSET, 0); |
| 648 | crc_value = calculate_crc32(primary_gpt_header, 92); |
| 649 | PUT_LONG(primary_gpt_header + HEADER_CRC_OFFSET, crc_value); |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 650 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 651 | PUT_LONG(secondary_gpt_header + HEADER_CRC_OFFSET, 0); |
| 652 | crc_value = (calculate_crc32(secondary_gpt_header, 92)); |
| 653 | PUT_LONG(secondary_gpt_header + HEADER_CRC_OFFSET, crc_value); |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 654 | |
| 655 | } |
| 656 | |
| 657 | /* |
| 658 | * Write the GPT to the MMC. |
| 659 | */ |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 660 | static unsigned int write_gpt(uint32_t size, uint8_t *gptImage, uint32_t block_size) |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 661 | { |
Channagoud Kadabi | 0e33285 | 2013-04-19 12:55:53 -0700 | [diff] [blame] | 662 | unsigned int ret = 1; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 663 | unsigned int header_size; |
| 664 | unsigned long long first_usable_lba; |
| 665 | unsigned long long backup_header_lba; |
| 666 | unsigned int max_partition_count = 0; |
| 667 | unsigned int partition_entry_size; |
| 668 | unsigned int partition_entry_array_start; |
| 669 | unsigned char *primary_gpt_header; |
| 670 | unsigned char *secondary_gpt_header; |
| 671 | unsigned int offset; |
| 672 | unsigned int partition_entry_array_size; |
| 673 | unsigned long long primary_header_location; /* address on the emmc card */ |
| 674 | unsigned long long secondary_header_location; /* address on the emmc card */ |
Channagoud Kadabi | 0e33285 | 2013-04-19 12:55:53 -0700 | [diff] [blame] | 675 | uint64_t device_density; |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 676 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 677 | /* Verify that passed block has a valid GPT primary header */ |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 678 | primary_gpt_header = (gptImage + block_size); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 679 | ret = partition_parse_gpt_header(primary_gpt_header, &first_usable_lba, |
| 680 | &partition_entry_size, &header_size, |
| 681 | &max_partition_count); |
| 682 | if (ret) { |
| 683 | dprintf(CRITICAL, |
| 684 | "GPT: Primary signature invalid cannot write GPT\n"); |
| 685 | goto end; |
| 686 | } |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 687 | |
Channagoud Kadabi | 0e33285 | 2013-04-19 12:55:53 -0700 | [diff] [blame] | 688 | /* Get the density of the mmc device */ |
| 689 | |
| 690 | device_density = mmc_get_device_capacity(); |
| 691 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 692 | /* Verify that passed block has a valid backup GPT HEADER */ |
| 693 | partition_entry_array_size = partition_entry_size * max_partition_count; |
| 694 | if (partition_entry_array_size < MIN_PARTITION_ARRAY_SIZE) { |
| 695 | partition_entry_array_size = MIN_PARTITION_ARRAY_SIZE; |
| 696 | } |
| 697 | offset = (2 * partition_entry_array_size); |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 698 | secondary_gpt_header = offset + block_size + primary_gpt_header; |
vijay kumar | 24c9ed1 | 2015-04-28 13:05:04 +0530 | [diff] [blame] | 699 | parse_secondary_gpt = 1; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 700 | ret = |
| 701 | partition_parse_gpt_header(secondary_gpt_header, &first_usable_lba, |
| 702 | &partition_entry_size, &header_size, |
| 703 | &max_partition_count); |
vijay kumar | 24c9ed1 | 2015-04-28 13:05:04 +0530 | [diff] [blame] | 704 | parse_secondary_gpt = 0; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 705 | if (ret) { |
| 706 | dprintf(CRITICAL, |
| 707 | "GPT: Backup signature invalid cannot write GPT\n"); |
| 708 | goto end; |
| 709 | } |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 710 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 711 | /* Patching the primary and the backup header of the GPT table */ |
Channagoud Kadabi | 0e33285 | 2013-04-19 12:55:53 -0700 | [diff] [blame] | 712 | patch_gpt(gptImage, device_density, partition_entry_array_size, |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 713 | max_partition_count, partition_entry_size, block_size); |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 714 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 715 | /* Erasing the eMMC card before writing */ |
Channagoud Kadabi | 0e33285 | 2013-04-19 12:55:53 -0700 | [diff] [blame] | 716 | ret = mmc_erase_card(0x00000000, device_density); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 717 | if (ret) { |
| 718 | dprintf(CRITICAL, "Failed to erase the eMMC card\n"); |
| 719 | goto end; |
| 720 | } |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 721 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 722 | /* Writing protective MBR */ |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 723 | ret = mmc_write(0, block_size, (unsigned int *)gptImage); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 724 | if (ret) { |
| 725 | dprintf(CRITICAL, "Failed to write Protective MBR\n"); |
| 726 | goto end; |
| 727 | } |
| 728 | /* Writing the primary GPT header */ |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 729 | primary_header_location = block_size; |
| 730 | ret = mmc_write(primary_header_location, block_size, |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 731 | (unsigned int *)primary_gpt_header); |
| 732 | if (ret) { |
| 733 | dprintf(CRITICAL, "Failed to write GPT header\n"); |
| 734 | goto end; |
| 735 | } |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 736 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 737 | /* Writing the backup GPT header */ |
| 738 | backup_header_lba = GET_LLWORD_FROM_BYTE |
| 739 | (&primary_gpt_header[BACKUP_HEADER_OFFSET]); |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 740 | secondary_header_location = backup_header_lba * block_size; |
| 741 | ret = mmc_write(secondary_header_location, block_size, |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 742 | (unsigned int *)secondary_gpt_header); |
| 743 | if (ret) { |
| 744 | dprintf(CRITICAL, "Failed to write GPT backup header\n"); |
| 745 | goto end; |
| 746 | } |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 747 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 748 | /* Writing the partition entries array for the primary header */ |
vijay kumar | 4f4405f | 2014-08-08 11:49:53 +0530 | [diff] [blame] | 749 | partition_entry_array_start = (unsigned int)primary_gpt_header + block_size; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 750 | ret = write_gpt_partition_array(primary_gpt_header, |
| 751 | partition_entry_array_start, |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 752 | partition_entry_array_size, block_size); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 753 | if (ret) { |
| 754 | dprintf(CRITICAL, |
| 755 | "GPT: Could not write GPT Partition entries array\n"); |
| 756 | goto end; |
| 757 | } |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 758 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 759 | /*Writing the partition entries array for the backup header */ |
vijay kumar | 4f4405f | 2014-08-08 11:49:53 +0530 | [diff] [blame] | 760 | partition_entry_array_start = (unsigned int)primary_gpt_header + block_size + |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 761 | partition_entry_array_size; |
| 762 | ret = write_gpt_partition_array(secondary_gpt_header, |
| 763 | partition_entry_array_start, |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 764 | partition_entry_array_size, block_size); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 765 | if (ret) { |
| 766 | dprintf(CRITICAL, |
| 767 | "GPT: Could not write GPT Partition entries array\n"); |
| 768 | goto end; |
| 769 | } |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 770 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 771 | /* Re-read the GPT partition table */ |
| 772 | dprintf(INFO, "Re-reading the GPT Partition Table\n"); |
Sundarajan Srinivasan | 554863f | 2013-12-04 17:14:37 -0800 | [diff] [blame] | 773 | partition_count = 0; |
vijay kumar | 24c9ed1 | 2015-04-28 13:05:04 +0530 | [diff] [blame] | 774 | flashing_gpt = 0; |
Sundarajan Srinivasan | 554863f | 2013-12-04 17:14:37 -0800 | [diff] [blame] | 775 | mmc_read_partition_table(0); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 776 | partition_dump(); |
| 777 | dprintf(CRITICAL, "GPT: Partition Table written\n"); |
| 778 | memset(primary_gpt_header, 0x00, size); |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 779 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 780 | end: |
| 781 | return ret; |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 782 | } |
| 783 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 784 | unsigned int write_partition(unsigned size, unsigned char *partition) |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 785 | { |
Channagoud Kadabi | 0e33285 | 2013-04-19 12:55:53 -0700 | [diff] [blame] | 786 | unsigned int ret = 1; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 787 | unsigned int partition_type; |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 788 | uint32_t block_size; |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 789 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 790 | if (partition == 0) { |
| 791 | dprintf(CRITICAL, "NULL partition\n"); |
| 792 | goto end; |
| 793 | } |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 794 | |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 795 | block_size = mmc_get_device_blocksize(); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 796 | ret = partition_get_type(size, partition, &partition_type); |
Channagoud Kadabi | 0e33285 | 2013-04-19 12:55:53 -0700 | [diff] [blame] | 797 | if (ret) |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 798 | goto end; |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 799 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 800 | switch (partition_type) { |
| 801 | case PARTITION_TYPE_MBR: |
| 802 | dprintf(INFO, "Writing MBR partition\n"); |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 803 | ret = write_mbr(size, partition, block_size); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 804 | break; |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 805 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 806 | case PARTITION_TYPE_GPT: |
| 807 | dprintf(INFO, "Writing GPT partition\n"); |
vijay kumar | 24c9ed1 | 2015-04-28 13:05:04 +0530 | [diff] [blame] | 808 | flashing_gpt = 1; |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 809 | ret = write_gpt(size, partition, block_size); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 810 | dprintf(CRITICAL, "Re-Flash all the partitions\n"); |
| 811 | break; |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 812 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 813 | default: |
| 814 | dprintf(CRITICAL, "Invalid partition\n"); |
Channagoud Kadabi | 0e33285 | 2013-04-19 12:55:53 -0700 | [diff] [blame] | 815 | ret = 1; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 816 | goto end; |
| 817 | } |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 818 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 819 | end: |
| 820 | return ret; |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 821 | } |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 822 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 823 | /* |
| 824 | * Fill name for android partition found. |
| 825 | */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 826 | static void |
| 827 | mbr_fill_name(struct partition_entry *partition_ent, unsigned int type) |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 828 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 829 | switch (type) { |
| 830 | memset(partition_ent->name, 0, MAX_GPT_NAME_SIZE); |
| 831 | case MBR_MODEM_TYPE: |
| 832 | case MBR_MODEM_TYPE2: |
| 833 | /* if already assigned last name available then return */ |
| 834 | if (!strcmp((const char *)vfat_partitions[vfat_count], "NONE")) |
| 835 | return; |
| 836 | strlcpy((char *)partition_ent->name, |
| 837 | (const char *)vfat_partitions[vfat_count], |
| 838 | sizeof(partition_ent->name)); |
| 839 | vfat_count++; |
| 840 | break; |
| 841 | case MBR_SBL1_TYPE: |
| 842 | memcpy(partition_ent->name, "sbl1", 4); |
| 843 | break; |
| 844 | case MBR_SBL2_TYPE: |
| 845 | memcpy(partition_ent->name, "sbl2", 4); |
| 846 | break; |
| 847 | case MBR_SBL3_TYPE: |
| 848 | memcpy(partition_ent->name, "sbl3", 4); |
| 849 | break; |
| 850 | case MBR_RPM_TYPE: |
| 851 | memcpy(partition_ent->name, "rpm", 3); |
| 852 | break; |
| 853 | case MBR_TZ_TYPE: |
| 854 | memcpy(partition_ent->name, "tz", 2); |
| 855 | break; |
| 856 | case MBR_ABOOT_TYPE: |
Channagoud Kadabi | 16f5095 | 2012-04-02 16:24:07 +0530 | [diff] [blame] | 857 | #if PLATFORM_MSM7X27A |
| 858 | memcpy(partition_ent->name, "FOTA", 4); |
| 859 | #else |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 860 | memcpy(partition_ent->name, "aboot", 5); |
Channagoud Kadabi | 16f5095 | 2012-04-02 16:24:07 +0530 | [diff] [blame] | 861 | #endif |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 862 | break; |
| 863 | case MBR_BOOT_TYPE: |
| 864 | memcpy(partition_ent->name, "boot", 4); |
| 865 | break; |
| 866 | case MBR_MODEM_ST1_TYPE: |
| 867 | memcpy(partition_ent->name, "modem_st1", 9); |
| 868 | break; |
| 869 | case MBR_MODEM_ST2_TYPE: |
| 870 | memcpy(partition_ent->name, "modem_st2", 9); |
| 871 | break; |
| 872 | case MBR_EFS2_TYPE: |
| 873 | memcpy(partition_ent->name, "efs2", 4); |
| 874 | break; |
| 875 | case MBR_USERDATA_TYPE: |
| 876 | if (ext3_count == sizeof(ext3_partitions) / sizeof(char *)) |
| 877 | return; |
| 878 | strlcpy((char *)partition_ent->name, |
| 879 | (const char *)ext3_partitions[ext3_count], |
| 880 | sizeof(partition_ent->name)); |
| 881 | ext3_count++; |
| 882 | break; |
| 883 | case MBR_RECOVERY_TYPE: |
| 884 | memcpy(partition_ent->name, "recovery", 8); |
| 885 | break; |
| 886 | case MBR_MISC_TYPE: |
| 887 | memcpy(partition_ent->name, "misc", 4); |
| 888 | break; |
Neeti Desai | 2d6b0e4 | 2012-03-23 15:48:57 -0700 | [diff] [blame] | 889 | case MBR_SSD_TYPE: |
| 890 | memcpy(partition_ent->name, "ssd", 3); |
| 891 | break; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 892 | }; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | /* |
| 896 | * Find index of parition in array of partition entries |
| 897 | */ |
Pavel Nedev | 285ad92 | 2013-04-26 10:39:19 +0300 | [diff] [blame] | 898 | int partition_get_index(const char *name) |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 899 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 900 | unsigned int input_string_length = strlen(name); |
| 901 | unsigned n; |
Mayank Grover | e358f1f | 2017-05-30 20:05:14 +0530 | [diff] [blame^] | 902 | int curr_slot = INVALID; |
| 903 | const char *suffix_curr_actv_slot = NULL; |
| 904 | char *curr_suffix = NULL; |
Kinson Chik | 66552a8 | 2011-03-29 15:59:06 -0700 | [diff] [blame] | 905 | |
neetid | 6c38de1 | 2011-12-02 12:04:50 -0800 | [diff] [blame] | 906 | if( partition_count >= NUM_PARTITIONS) |
| 907 | { |
| 908 | return INVALID_PTN; |
| 909 | } |
Mayank Grover | e358f1f | 2017-05-30 20:05:14 +0530 | [diff] [blame^] | 910 | |
| 911 | /* We iterate through the parition entries list, |
| 912 | to find the partition with active slot suffix. |
| 913 | */ |
| 914 | for (n = 0; n < partition_count; n++) |
| 915 | { |
| 916 | if (!strncmp((const char*)name, (const char *)partition_entries[n].name, |
| 917 | input_string_length)) |
| 918 | { |
| 919 | curr_suffix = (char *)(partition_entries[n].name+input_string_length); |
| 920 | |
| 921 | /* if partition_entries.name is NULL terminated return the index */ |
| 922 | if (*curr_suffix == '\0') |
| 923 | return n; |
| 924 | |
| 925 | if (partition_multislot_is_supported()) |
| 926 | { |
| 927 | curr_slot = partition_find_active_slot(); |
| 928 | |
| 929 | /* If suffix string matches with current active slot suffix return index */ |
| 930 | if (curr_slot != INVALID) |
| 931 | { |
| 932 | suffix_curr_actv_slot = SUFFIX_SLOT(curr_slot); |
| 933 | if (!strncmp((const char *)curr_suffix, suffix_curr_actv_slot, |
| 934 | strlen(suffix_curr_actv_slot))) |
| 935 | return n; |
| 936 | else |
| 937 | continue; |
| 938 | } |
| 939 | else |
| 940 | { |
| 941 | /* No valid active slot */ |
| 942 | return INVALID_PTN; |
| 943 | } |
| 944 | } |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 945 | } |
| 946 | } |
| 947 | return INVALID_PTN; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 948 | } |
| 949 | |
Vijay Kumar Pendoti | 1c1d670 | 2016-07-21 13:03:23 +0530 | [diff] [blame] | 950 | /* |
| 951 | * Find relative index of partition in lun |
| 952 | */ |
| 953 | int partition_get_index_in_lun(const char *name, unsigned int lun) |
| 954 | { |
| 955 | unsigned int input_string_length = strlen(name); |
| 956 | unsigned int n, relative_index = 0; |
Mayank Grover | e358f1f | 2017-05-30 20:05:14 +0530 | [diff] [blame^] | 957 | int curr_slot = INVALID; |
| 958 | const char *suffix_curr_actv_slot = NULL; |
| 959 | char *curr_suffix = NULL; |
| 960 | for (n = 0; n < partition_count; n++) |
| 961 | { |
Vijay Kumar Pendoti | 1c1d670 | 2016-07-21 13:03:23 +0530 | [diff] [blame] | 962 | if (lun == partition_entries[n].lun) |
| 963 | { |
| 964 | relative_index++; |
Mayank Grover | e358f1f | 2017-05-30 20:05:14 +0530 | [diff] [blame^] | 965 | if (!strncmp((const char*)name, (const char *)partition_entries[n].name, |
| 966 | input_string_length)) |
| 967 | { |
| 968 | curr_suffix = (char *)(partition_entries[n].name+input_string_length); |
| 969 | |
| 970 | /* if partition_entries.name is NULL terminated return relative index */ |
| 971 | if (*curr_suffix == '\0') |
| 972 | return relative_index; |
| 973 | |
| 974 | if (partition_multislot_is_supported()) |
| 975 | { |
| 976 | curr_slot = partition_find_active_slot(); |
| 977 | |
| 978 | /* If suffix string matches with current active slot suffix return index */ |
| 979 | if (curr_slot != INVALID) |
| 980 | { |
| 981 | suffix_curr_actv_slot = SUFFIX_SLOT(curr_slot); |
| 982 | if (!strncmp((const char *)curr_suffix, suffix_curr_actv_slot, |
| 983 | strlen(suffix_curr_actv_slot))) |
| 984 | return relative_index; |
| 985 | else |
| 986 | continue; |
| 987 | } |
| 988 | else |
| 989 | { |
| 990 | /* No valid active slot */ |
| 991 | return INVALID_PTN; |
| 992 | } |
| 993 | } |
Vijay Kumar Pendoti | 1c1d670 | 2016-07-21 13:03:23 +0530 | [diff] [blame] | 994 | } |
| 995 | } |
| 996 | } |
| 997 | return INVALID_PTN; |
| 998 | } |
| 999 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1000 | /* Get size of the partition */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1001 | unsigned long long partition_get_size(int index) |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1002 | { |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 1003 | uint32_t block_size; |
| 1004 | |
| 1005 | block_size = mmc_get_device_blocksize(); |
| 1006 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1007 | if (index == INVALID_PTN) |
| 1008 | return 0; |
| 1009 | else { |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 1010 | return partition_entries[index].size * block_size; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1011 | } |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | /* Get offset of the partition */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1015 | unsigned long long partition_get_offset(int index) |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1016 | { |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 1017 | uint32_t block_size; |
| 1018 | |
| 1019 | block_size = mmc_get_device_blocksize(); |
| 1020 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1021 | if (index == INVALID_PTN) |
| 1022 | return 0; |
| 1023 | else { |
Channagoud Kadabi | 96c629e | 2013-09-10 14:21:30 -0700 | [diff] [blame] | 1024 | return partition_entries[index].first_lba * block_size; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1025 | } |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1026 | } |
| 1027 | |
Sundarajan Srinivasan | 9ec6963 | 2014-04-10 13:33:10 -0700 | [diff] [blame] | 1028 | struct partition_info partition_get_info(const char *name) |
| 1029 | { |
| 1030 | struct partition_info info = {0}; |
| 1031 | |
| 1032 | int index = INVALID_PTN; |
| 1033 | |
| 1034 | if(!name) |
| 1035 | { |
| 1036 | dprintf(CRITICAL, "Invalid partition name passed\n"); |
| 1037 | goto out; |
| 1038 | } |
| 1039 | |
| 1040 | index = partition_get_index(name); |
| 1041 | |
| 1042 | if (index != INVALID_PTN) |
| 1043 | { |
| 1044 | info.offset = partition_get_offset(index); |
| 1045 | info.size = partition_get_size(index); |
| 1046 | } |
| 1047 | else |
| 1048 | { |
| 1049 | dprintf(CRITICAL, "Error unable to find partition : [%s]\n", name); |
| 1050 | } |
| 1051 | out: |
| 1052 | return info; |
| 1053 | } |
| 1054 | |
Sundarajan Srinivasan | 554863f | 2013-12-04 17:14:37 -0800 | [diff] [blame] | 1055 | uint8_t partition_get_lun(int index) |
| 1056 | { |
| 1057 | return partition_entries[index].lun; |
| 1058 | } |
| 1059 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1060 | /* Debug: Print all parsed partitions */ |
| 1061 | void partition_dump() |
| 1062 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1063 | unsigned i = 0; |
| 1064 | for (i = 0; i < partition_count; i++) { |
| 1065 | dprintf(SPEW, |
| 1066 | "ptn[%d]:Name[%s] Size[%llu] Type[%u] First[%llu] Last[%llu]\n", |
| 1067 | i, partition_entries[i].name, partition_entries[i].size, |
| 1068 | partition_entries[i].dtype, |
| 1069 | partition_entries[i].first_lba, |
| 1070 | partition_entries[i].last_lba); |
| 1071 | } |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1072 | } |
| 1073 | |
Channagoud Kadabi | 0e33285 | 2013-04-19 12:55:53 -0700 | [diff] [blame] | 1074 | static unsigned int |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1075 | partition_verify_mbr_signature(unsigned size, unsigned char *buffer) |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1076 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1077 | /* Avoid checking past end of buffer */ |
| 1078 | if ((TABLE_SIGNATURE + 1) > size) { |
Channagoud Kadabi | 0e33285 | 2013-04-19 12:55:53 -0700 | [diff] [blame] | 1079 | return 1; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1080 | } |
| 1081 | /* Check to see if signature exists */ |
| 1082 | if ((buffer[TABLE_SIGNATURE] != MMC_MBR_SIGNATURE_BYTE_0) || |
| 1083 | (buffer[TABLE_SIGNATURE + 1] != MMC_MBR_SIGNATURE_BYTE_1)) { |
| 1084 | dprintf(CRITICAL, "MBR signature does not match.\n"); |
Channagoud Kadabi | 0e33285 | 2013-04-19 12:55:53 -0700 | [diff] [blame] | 1085 | return 1; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1086 | } |
Channagoud Kadabi | 0e33285 | 2013-04-19 12:55:53 -0700 | [diff] [blame] | 1087 | return 0; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1088 | } |
| 1089 | |
Channagoud Kadabi | 0e33285 | 2013-04-19 12:55:53 -0700 | [diff] [blame] | 1090 | static unsigned int |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1091 | mbr_partition_get_type(unsigned size, unsigned char *partition, |
| 1092 | unsigned int *partition_type) |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1093 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1094 | unsigned int type_offset = TABLE_ENTRY_0 + OFFSET_TYPE; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1095 | |
Channagoud Kadabi | 98b1107 | 2015-03-12 15:14:22 -0700 | [diff] [blame] | 1096 | if (size < (type_offset + sizeof (*partition_type))) { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1097 | goto end; |
| 1098 | } |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1099 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1100 | *partition_type = partition[type_offset]; |
| 1101 | end: |
Channagoud Kadabi | 0e33285 | 2013-04-19 12:55:53 -0700 | [diff] [blame] | 1102 | return 0; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1103 | } |
| 1104 | |
Channagoud Kadabi | 0e33285 | 2013-04-19 12:55:53 -0700 | [diff] [blame] | 1105 | static unsigned int |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1106 | partition_get_type(unsigned size, unsigned char *partition, |
| 1107 | unsigned int *partition_type) |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1108 | { |
Channagoud Kadabi | 0e33285 | 2013-04-19 12:55:53 -0700 | [diff] [blame] | 1109 | unsigned int ret = 0; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1110 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1111 | /* |
| 1112 | * If the block contains the MBR signature, then it's likely either |
| 1113 | * MBR or MBR with protective type (GPT). If the MBR signature is |
| 1114 | * not there, then it could be the GPT backup. |
| 1115 | */ |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1116 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1117 | /* First check the MBR signature */ |
| 1118 | ret = partition_verify_mbr_signature(size, partition); |
Channagoud Kadabi | 0e33285 | 2013-04-19 12:55:53 -0700 | [diff] [blame] | 1119 | if (!ret) { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1120 | unsigned int mbr_partition_type = PARTITION_TYPE_MBR; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1121 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1122 | /* MBR signature verified. This could be MBR, MBR + EBR, or GPT */ |
| 1123 | ret = |
| 1124 | mbr_partition_get_type(size, partition, |
| 1125 | &mbr_partition_type); |
Channagoud Kadabi | 0e33285 | 2013-04-19 12:55:53 -0700 | [diff] [blame] | 1126 | if (ret) { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1127 | dprintf(CRITICAL, "Cannot get TYPE of partition"); |
| 1128 | } else if (MBR_PROTECTED_TYPE == mbr_partition_type) { |
| 1129 | *partition_type = PARTITION_TYPE_GPT; |
| 1130 | } else { |
| 1131 | *partition_type = PARTITION_TYPE_MBR; |
| 1132 | } |
| 1133 | } else { |
| 1134 | /* |
| 1135 | * This could be the GPT backup. Make that assumption for now. |
| 1136 | * Anybody who treats the block as GPT backup should check the |
| 1137 | * signature. |
| 1138 | */ |
| 1139 | *partition_type = PARTITION_TYPE_GPT_BACKUP; |
| 1140 | } |
| 1141 | return ret; |
Kinson Chik | 66552a8 | 2011-03-29 15:59:06 -0700 | [diff] [blame] | 1142 | } |
Kinson Chik | 4d7444f | 2011-09-13 15:48:51 -0700 | [diff] [blame] | 1143 | |
| 1144 | /* |
| 1145 | * Parse the gpt header and get the required header fields |
| 1146 | * Return 0 on valid signature |
| 1147 | */ |
Channagoud Kadabi | 0e33285 | 2013-04-19 12:55:53 -0700 | [diff] [blame] | 1148 | static unsigned int |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1149 | partition_parse_gpt_header(unsigned char *buffer, |
| 1150 | unsigned long long *first_usable_lba, |
| 1151 | unsigned int *partition_entry_size, |
| 1152 | unsigned int *header_size, |
| 1153 | unsigned int *max_partition_count) |
Kinson Chik | 4d7444f | 2011-09-13 15:48:51 -0700 | [diff] [blame] | 1154 | { |
vijay kumar | 24c9ed1 | 2015-04-28 13:05:04 +0530 | [diff] [blame] | 1155 | uint32_t crc_val_org = 0; |
| 1156 | uint32_t crc_val = 0; |
| 1157 | uint32_t ret = 0; |
| 1158 | uint32_t partitions_for_block = 0; |
| 1159 | uint32_t blocks_to_read = 0; |
| 1160 | unsigned char *new_buffer = NULL; |
| 1161 | unsigned long long last_usable_lba = 0; |
| 1162 | unsigned long long partition_0 = 0; |
| 1163 | unsigned long long current_lba = 0; |
| 1164 | uint32_t block_size = mmc_get_device_blocksize(); |
| 1165 | /* Get the density of the mmc device */ |
| 1166 | uint64_t device_density = mmc_get_device_capacity(); |
| 1167 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1168 | /* Check GPT Signature */ |
| 1169 | if (((uint32_t *) buffer)[0] != GPT_SIGNATURE_2 || |
| 1170 | ((uint32_t *) buffer)[1] != GPT_SIGNATURE_1) |
| 1171 | return 1; |
Kinson Chik | 4d7444f | 2011-09-13 15:48:51 -0700 | [diff] [blame] | 1172 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1173 | *header_size = GET_LWORD_FROM_BYTE(&buffer[HEADER_SIZE_OFFSET]); |
vijay kumar | 24c9ed1 | 2015-04-28 13:05:04 +0530 | [diff] [blame] | 1174 | /*check for header size too small*/ |
| 1175 | if (*header_size < GPT_HEADER_SIZE) { |
| 1176 | dprintf(CRITICAL,"GPT Header size is too small\n"); |
| 1177 | return 1; |
| 1178 | } |
| 1179 | /*check for header size too large*/ |
| 1180 | if (*header_size > block_size) { |
| 1181 | dprintf(CRITICAL,"GPT Header size is too large\n"); |
| 1182 | return 1; |
| 1183 | } |
| 1184 | |
| 1185 | crc_val_org = GET_LWORD_FROM_BYTE(&buffer[HEADER_CRC_OFFSET]); |
| 1186 | /*Write CRC to 0 before we calculate the crc of the GPT header*/ |
| 1187 | crc_val = 0; |
| 1188 | PUT_LONG(&buffer[HEADER_CRC_OFFSET], crc_val); |
| 1189 | |
| 1190 | crc_val = crc32(~0L,buffer, *header_size) ^ (~0L); |
| 1191 | if (crc_val != crc_val_org) { |
| 1192 | dprintf(CRITICAL,"Header crc mismatch crc_val = %u with crc_val_org = %u\n", crc_val,crc_val_org); |
| 1193 | return 1; |
| 1194 | } |
| 1195 | else |
| 1196 | PUT_LONG(&buffer[HEADER_CRC_OFFSET], crc_val); |
| 1197 | |
| 1198 | current_lba = |
| 1199 | GET_LLWORD_FROM_BYTE(&buffer[PRIMARY_HEADER_OFFSET]); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 1200 | *first_usable_lba = |
| 1201 | GET_LLWORD_FROM_BYTE(&buffer[FIRST_USABLE_LBA_OFFSET]); |
| 1202 | *max_partition_count = |
| 1203 | GET_LWORD_FROM_BYTE(&buffer[PARTITION_COUNT_OFFSET]); |
| 1204 | *partition_entry_size = |
| 1205 | GET_LWORD_FROM_BYTE(&buffer[PENTRY_SIZE_OFFSET]); |
vijay kumar | 24c9ed1 | 2015-04-28 13:05:04 +0530 | [diff] [blame] | 1206 | last_usable_lba = |
| 1207 | GET_LLWORD_FROM_BYTE(&buffer[LAST_USABLE_LBA_OFFSET]); |
Kinson Chik | 4d7444f | 2011-09-13 15:48:51 -0700 | [diff] [blame] | 1208 | |
vijay kumar | 24c9ed1 | 2015-04-28 13:05:04 +0530 | [diff] [blame] | 1209 | /*current lba and GPT lba should be same*/ |
| 1210 | if (!parse_secondary_gpt) { |
| 1211 | if (current_lba != GPT_LBA) { |
| 1212 | dprintf(CRITICAL,"GPT first usable LBA mismatch\n"); |
| 1213 | return 1; |
| 1214 | } |
| 1215 | } |
| 1216 | /*check for first lba should be with in the valid range*/ |
| 1217 | if (*first_usable_lba > (device_density/block_size)) { |
| 1218 | dprintf(CRITICAL,"Invalid first_usable_lba\n"); |
| 1219 | return 1; |
| 1220 | } |
| 1221 | /*check for last lba should be with in the valid range*/ |
| 1222 | if (last_usable_lba > (device_density/block_size)) { |
| 1223 | dprintf(CRITICAL,"Invalid last_usable_lba\n"); |
| 1224 | return 1; |
| 1225 | } |
| 1226 | /*check for partition entry size*/ |
| 1227 | if (*partition_entry_size != PARTITION_ENTRY_SIZE) { |
| 1228 | dprintf(CRITICAL,"Invalid parition entry size\n"); |
| 1229 | return 1; |
| 1230 | } |
| 1231 | |
| 1232 | if ((*max_partition_count) > (MIN_PARTITION_ARRAY_SIZE /(*partition_entry_size))) { |
| 1233 | dprintf(CRITICAL, "Invalid maximum partition count\n"); |
| 1234 | return 1; |
| 1235 | } |
| 1236 | |
| 1237 | partitions_for_block = block_size / (*partition_entry_size); |
| 1238 | |
| 1239 | blocks_to_read = (*max_partition_count)/ partitions_for_block; |
| 1240 | if ((*max_partition_count) % partitions_for_block) { |
| 1241 | blocks_to_read += 1; |
| 1242 | } |
| 1243 | |
| 1244 | new_buffer = (uint8_t *)memalign(CACHE_LINE, ROUNDUP((blocks_to_read * block_size),CACHE_LINE)); |
| 1245 | |
| 1246 | if (!new_buffer) |
| 1247 | { |
| 1248 | dprintf(CRITICAL, "Failed to Allocate memory to read partition table\n"); |
| 1249 | return 1; |
| 1250 | } |
| 1251 | |
| 1252 | if (!flashing_gpt) { |
| 1253 | partition_0 = GET_LLWORD_FROM_BYTE(&buffer[PARTITION_ENTRIES_OFFSET]); |
| 1254 | /*start LBA should always be 2 in primary GPT*/ |
| 1255 | if(partition_0 != 0x2) { |
| 1256 | dprintf(CRITICAL, "Starting LBA mismatch\n"); |
| 1257 | goto fail; |
| 1258 | |
| 1259 | } |
| 1260 | /*read the partition entries to new_buffer*/ |
| 1261 | ret = mmc_read((partition_0) * (block_size), (unsigned int *)new_buffer, (blocks_to_read * block_size)); |
| 1262 | if (ret) |
| 1263 | { |
| 1264 | dprintf(CRITICAL, "GPT: Could not read primary gpt from mmc\n"); |
| 1265 | goto fail; |
| 1266 | } |
| 1267 | crc_val_org = GET_LWORD_FROM_BYTE(&buffer[PARTITION_CRC_OFFSET]); |
| 1268 | |
| 1269 | crc_val = crc32(~0L,new_buffer, ((*max_partition_count) * (*partition_entry_size))) ^ (~0L); |
| 1270 | if (crc_val != crc_val_org) { |
| 1271 | dprintf(CRITICAL,"Partition entires crc mismatch crc_val= %u with crc_val_org= %u\n",crc_val,crc_val_org); |
| 1272 | ret = 1; |
| 1273 | } |
| 1274 | } |
| 1275 | fail: |
| 1276 | free(new_buffer); |
| 1277 | return ret; |
Kinson Chik | 4d7444f | 2011-09-13 15:48:51 -0700 | [diff] [blame] | 1278 | } |
Pavel Nedev | b5ba62d | 2013-07-22 11:57:41 +0300 | [diff] [blame] | 1279 | |
| 1280 | bool partition_gpt_exists() |
| 1281 | { |
| 1282 | return (gpt_partitions_exist != 0); |
| 1283 | } |
Channagoud Kadabi | faf20f6 | 2014-10-21 22:22:37 -0700 | [diff] [blame] | 1284 | |
| 1285 | int partition_read_only(int index) |
| 1286 | { |
| 1287 | return partition_entries[index].attribute_flag >> PART_ATT_READONLY_OFFSET; |
| 1288 | } |