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