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