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