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