Kinson Chik | 66552a8 | 2011-03-29 15:59:06 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2011, Code Aurora Forum. All rights reserved. |
| 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. |
| 12 | * * Neither the name of Code Aurora Forum, Inc. nor the names of its |
| 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 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 34 | char *ext3_partitions[] = {"system", "userdata", "persist", "cache", "tombstones"}; |
| 35 | char *vfat_partitions[] = {"modem", "mdm", "NONE"}; |
| 36 | unsigned int ext3_count = 0; |
| 37 | unsigned int vfat_count = 0; |
| 38 | |
| 39 | struct partition_entry partition_entries[NUM_PARTITIONS]; |
| 40 | unsigned gpt_partitions_exist = 0; |
| 41 | unsigned partition_count = 0; |
| 42 | |
| 43 | //TODO: Remove the dependency of mmc in these functions |
| 44 | unsigned int partition_read_table( struct mmc_boot_host * mmc_host, |
| 45 | struct mmc_boot_card * mmc_card) |
| 46 | { |
| 47 | unsigned int ret; |
| 48 | |
| 49 | /* Read MBR of the card */ |
| 50 | ret = mmc_boot_read_mbr( mmc_host, mmc_card ); |
| 51 | if( ret != MMC_BOOT_E_SUCCESS ) |
| 52 | { |
| 53 | dprintf(CRITICAL, "MMC Boot: MBR read failed!\n" ); |
| 54 | return MMC_BOOT_E_FAILURE; |
| 55 | } |
| 56 | |
| 57 | /* Read GPT of the card if exist */ |
| 58 | if(gpt_partitions_exist){ |
| 59 | ret = mmc_boot_read_gpt(mmc_host, mmc_card); |
| 60 | if( ret != MMC_BOOT_E_SUCCESS ) |
| 61 | { |
| 62 | dprintf(CRITICAL, "MMC Boot: GPT read failed!\n" ); |
| 63 | return MMC_BOOT_E_FAILURE; |
| 64 | } |
| 65 | } |
| 66 | return MMC_BOOT_E_SUCCESS; |
| 67 | } |
| 68 | |
| 69 | /* |
| 70 | * Read MBR from MMC card and fill partition table. |
| 71 | */ |
| 72 | unsigned int mmc_boot_read_mbr( struct mmc_boot_host * mmc_host, |
| 73 | struct mmc_boot_card * mmc_card) |
| 74 | { |
| 75 | unsigned char buffer[MMC_BOOT_RD_BLOCK_LEN]; |
| 76 | unsigned int dtype; |
| 77 | unsigned int dfirstsec; |
| 78 | unsigned int EBR_first_sec; |
| 79 | unsigned int EBR_current_sec; |
| 80 | int ret = MMC_BOOT_E_SUCCESS; |
| 81 | int idx, i; |
| 82 | |
| 83 | /* Print out the MBR first */ |
| 84 | ret = mmc_boot_read_from_card( mmc_host, mmc_card, 0, \ |
| 85 | MMC_BOOT_RD_BLOCK_LEN, \ |
| 86 | (unsigned int *)buffer); |
| 87 | if (ret) |
| 88 | { |
| 89 | dprintf(CRITICAL, "Could not read partition from mmc"); |
| 90 | return ret; |
| 91 | } |
| 92 | |
| 93 | /* Check to see if signature exists */ |
| 94 | ret = partition_verify_mbr_signature(MMC_BOOT_RD_BLOCK_LEN, buffer); |
| 95 | if (ret) |
| 96 | { |
| 97 | return ret; |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * Process each of the four partitions in the MBR by reading the table |
| 102 | * information into our mbr table. |
| 103 | */ |
| 104 | partition_count = 0; |
| 105 | idx = TABLE_ENTRY_0; |
| 106 | for (i = 0; i < 4; i++) |
| 107 | { |
| 108 | /* Type 0xEE indicates end of MBR and GPT partitions exist */ |
| 109 | dtype = buffer[idx + i * TABLE_ENTRY_SIZE + OFFSET_TYPE]; |
| 110 | if (dtype == MBR_PROTECTED_TYPE){ |
| 111 | gpt_partitions_exist = 1; |
| 112 | return ret; |
| 113 | } |
| 114 | partition_entries[partition_count].dtype = dtype; |
| 115 | partition_entries[partition_count].attribute_flag = \ |
| 116 | buffer[idx + i * TABLE_ENTRY_SIZE + OFFSET_STATUS]; |
| 117 | partition_entries[partition_count].first_lba = \ |
| 118 | GET_LWORD_FROM_BYTE(&buffer[idx + \ |
| 119 | i * TABLE_ENTRY_SIZE + \ |
| 120 | OFFSET_FIRST_SEC]); |
| 121 | partition_entries[partition_count].size = \ |
| 122 | GET_LWORD_FROM_BYTE(&buffer[idx + \ |
| 123 | i * TABLE_ENTRY_SIZE + \ |
| 124 | OFFSET_SIZE]); |
| 125 | dfirstsec = partition_entries[partition_count].first_lba; |
| 126 | mbr_fill_name(&partition_entries[partition_count], \ |
| 127 | partition_entries[partition_count].dtype); |
| 128 | partition_count++; |
| 129 | if (partition_count == NUM_PARTITIONS) |
| 130 | return ret; |
| 131 | } |
| 132 | |
| 133 | /* See if the last partition is EBR, if not, parsing is done */ |
| 134 | if (dtype != MBR_EBR_TYPE) |
| 135 | { |
| 136 | return ret; |
| 137 | } |
| 138 | |
| 139 | EBR_first_sec = dfirstsec; |
| 140 | EBR_current_sec = dfirstsec; |
| 141 | |
| 142 | ret = mmc_boot_read_from_card( mmc_host, mmc_card, \ |
| 143 | (EBR_first_sec * 512), \ |
| 144 | MMC_BOOT_RD_BLOCK_LEN, \ |
| 145 | (unsigned int *)buffer); |
| 146 | if (ret) |
| 147 | { |
| 148 | return ret; |
| 149 | } |
| 150 | /* Loop to parse the EBR */ |
| 151 | for (i = 0;; i++) |
| 152 | { |
| 153 | ret = partition_verify_mbr_signature(MMC_BOOT_RD_BLOCK_LEN, buffer); |
| 154 | if (ret) |
| 155 | { |
| 156 | ret = MMC_BOOT_E_SUCCESS; |
| 157 | break; |
| 158 | } |
| 159 | partition_entries[partition_count].attribute_flag = \ |
| 160 | buffer[TABLE_ENTRY_0 + OFFSET_STATUS]; |
| 161 | partition_entries[partition_count].dtype = \ |
| 162 | buffer[TABLE_ENTRY_0 + OFFSET_TYPE]; |
| 163 | partition_entries[partition_count].first_lba = \ |
| 164 | GET_LWORD_FROM_BYTE(&buffer[TABLE_ENTRY_0 + \ |
| 165 | OFFSET_FIRST_SEC]) + \ |
| 166 | EBR_current_sec; |
| 167 | partition_entries[partition_count].size = \ |
| 168 | GET_LWORD_FROM_BYTE(&buffer[TABLE_ENTRY_0 + \ |
| 169 | OFFSET_SIZE]); |
| 170 | mbr_fill_name(&(partition_entries[partition_count]), \ |
| 171 | partition_entries[partition_count].dtype); |
| 172 | partition_count++; |
| 173 | if (partition_count == NUM_PARTITIONS) |
| 174 | return ret; |
| 175 | |
| 176 | dfirstsec = |
| 177 | GET_LWORD_FROM_BYTE(&buffer[TABLE_ENTRY_1 + OFFSET_FIRST_SEC]); |
| 178 | if(dfirstsec == 0) |
| 179 | { |
| 180 | /* Getting to the end of the EBR tables */ |
| 181 | break; |
| 182 | } |
| 183 | /* More EBR to follow - read in the next EBR sector */ |
| 184 | dprintf(SPEW, "Reading EBR block from 0x%X\n", EBR_first_sec |
| 185 | + dfirstsec); |
| 186 | ret = mmc_boot_read_from_card( mmc_host, mmc_card, \ |
| 187 | ((EBR_first_sec + dfirstsec) * 512), \ |
| 188 | MMC_BOOT_RD_BLOCK_LEN, \ |
| 189 | (unsigned int *)buffer); |
| 190 | if (ret) |
| 191 | { |
| 192 | return ret; |
| 193 | } |
| 194 | EBR_current_sec = EBR_first_sec + dfirstsec; |
| 195 | } |
| 196 | return ret; |
| 197 | } |
Kinson Chik | 66552a8 | 2011-03-29 15:59:06 -0700 | [diff] [blame] | 198 | |
| 199 | /* |
| 200 | * Read GPT from MMC and fill partition table |
| 201 | */ |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 202 | unsigned int mmc_boot_read_gpt( struct mmc_boot_host * mmc_host, |
| 203 | struct mmc_boot_card * mmc_card) |
| 204 | { |
Kinson Chik | 66552a8 | 2011-03-29 15:59:06 -0700 | [diff] [blame] | 205 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 206 | int ret = MMC_BOOT_E_SUCCESS; |
| 207 | unsigned int header_size; |
| 208 | unsigned long long first_usable_lba; |
Kinson Chik | 4d7444f | 2011-09-13 15:48:51 -0700 | [diff] [blame] | 209 | unsigned long long backup_header_lba; |
| 210 | unsigned int max_partition_count = 0; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 211 | unsigned int partition_entry_size; |
| 212 | unsigned char data[MMC_BOOT_RD_BLOCK_LEN]; |
| 213 | unsigned int i = 0; /* Counter for each 512 block */ |
| 214 | unsigned int j = 0; /* Counter for each 128 entry in the 512 block */ |
| 215 | unsigned int n = 0; /* Counter for UTF-16 -> 8 conversion */ |
| 216 | unsigned char UTF16_name[MAX_GPT_NAME_SIZE]; |
Kinson Chik | 4d7444f | 2011-09-13 15:48:51 -0700 | [diff] [blame] | 217 | /* LBA of first partition -- 1 Block after Protected MBR + 1 for PT */ |
| 218 | unsigned long long partition_0 = 2; |
Kinson Chik | 66552a8 | 2011-03-29 15:59:06 -0700 | [diff] [blame] | 219 | |
| 220 | /* Print out the GPT first */ |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 221 | ret = mmc_boot_read_from_card( mmc_host, mmc_card, \ |
| 222 | PROTECTIVE_MBR_SIZE, \ |
| 223 | MMC_BOOT_RD_BLOCK_LEN, \ |
| 224 | (unsigned int *)data); |
Kinson Chik | 66552a8 | 2011-03-29 15:59:06 -0700 | [diff] [blame] | 225 | |
Kinson Chik | 4d7444f | 2011-09-13 15:48:51 -0700 | [diff] [blame] | 226 | if (ret) |
| 227 | dprintf(CRITICAL, "GPT: Could not read primary gpt from mmc\n"); |
Kinson Chik | 66552a8 | 2011-03-29 15:59:06 -0700 | [diff] [blame] | 228 | |
Kinson Chik | 4d7444f | 2011-09-13 15:48:51 -0700 | [diff] [blame] | 229 | ret = partition_parse_gpt_header(data, &first_usable_lba, |
| 230 | &partition_entry_size, &header_size, |
| 231 | &max_partition_count); |
| 232 | |
| 233 | if (ret) |
| 234 | { |
| 235 | dprintf(INFO, "GPT: (WARNING) Primary signature invalid\n" ); |
| 236 | |
| 237 | /* Check the backup gpt */ |
| 238 | backup_header_lba = GET_LLWORD_FROM_BYTE(&data[BACKUP_HEADER_OFFSET]); |
| 239 | ret = mmc_boot_read_from_card( mmc_host, mmc_card, \ |
| 240 | (backup_header_lba * BLOCK_SIZE), \ |
| 241 | MMC_BOOT_RD_BLOCK_LEN, \ |
| 242 | (unsigned int *)data); |
| 243 | |
| 244 | if (ret) |
| 245 | { |
| 246 | dprintf(CRITICAL, "GPT: Could not read backup gpt from mmc\n"); |
| 247 | return ret; |
| 248 | } |
| 249 | |
| 250 | ret = partition_parse_gpt_header(data, &first_usable_lba, |
| 251 | &partition_entry_size, &header_size, |
| 252 | &max_partition_count); |
| 253 | if (ret) |
| 254 | { |
| 255 | dprintf(CRITICAL, "GPT: Primary and backup signatures invalid\n"); |
| 256 | return ret; |
| 257 | } |
| 258 | partition_0 = backup_header_lba - (max_partition_count / 4); |
| 259 | } |
Kinson Chik | 66552a8 | 2011-03-29 15:59:06 -0700 | [diff] [blame] | 260 | |
| 261 | /* Read GPT Entries */ |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 262 | for(i = 0; i < (max_partition_count/4); i++) |
Kinson Chik | 66552a8 | 2011-03-29 15:59:06 -0700 | [diff] [blame] | 263 | { |
| 264 | ret = mmc_boot_read_from_card( mmc_host, mmc_card, |
Kinson Chik | 4d7444f | 2011-09-13 15:48:51 -0700 | [diff] [blame] | 265 | (partition_0 * BLOCK_SIZE) + |
Kinson Chik | 66552a8 | 2011-03-29 15:59:06 -0700 | [diff] [blame] | 266 | (i * MMC_BOOT_RD_BLOCK_LEN), |
| 267 | MMC_BOOT_RD_BLOCK_LEN, |
| 268 | (uint32_t *)data); |
| 269 | |
| 270 | if (ret) |
| 271 | { |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 272 | dprintf(CRITICAL, |
| 273 | "GPT: mmc read card failed reading partition entries.\n" ); |
Kinson Chik | 66552a8 | 2011-03-29 15:59:06 -0700 | [diff] [blame] | 274 | return ret; |
| 275 | } |
| 276 | |
| 277 | for(j=0; j < 4; j++) |
| 278 | { |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 279 | memcpy(&(partition_entries[partition_count].type_guid), |
| 280 | &data[(j * partition_entry_size)], |
| 281 | PARTITION_TYPE_GUID_SIZE); |
| 282 | if (partition_entries[partition_count].type_guid[0] == 0x00 && |
| 283 | partition_entries[partition_count].type_guid[1] == 0x00) |
Kinson Chik | 66552a8 | 2011-03-29 15:59:06 -0700 | [diff] [blame] | 284 | { |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 285 | i = max_partition_count; |
Kinson Chik | 66552a8 | 2011-03-29 15:59:06 -0700 | [diff] [blame] | 286 | break; |
| 287 | } |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 288 | memcpy(&(partition_entries[partition_count].unique_partition_guid), |
| 289 | &data[(j * partition_entry_size) + |
| 290 | UNIQUE_GUID_OFFSET], UNIQUE_PARTITION_GUID_SIZE); |
| 291 | partition_entries[partition_count].first_lba = |
| 292 | GET_LLWORD_FROM_BYTE(&data[(j * partition_entry_size) + |
| 293 | FIRST_LBA_OFFSET]); |
| 294 | partition_entries[partition_count].last_lba = |
| 295 | GET_LLWORD_FROM_BYTE(&data[(j * partition_entry_size) + |
| 296 | LAST_LBA_OFFSET]); |
| 297 | partition_entries[partition_count].size = |
| 298 | partition_entries[partition_count].last_lba - |
| 299 | partition_entries[partition_count].first_lba; |
| 300 | partition_entries[partition_count].attribute_flag = |
| 301 | GET_LLWORD_FROM_BYTE(&data[(j * partition_entry_size) + |
| 302 | ATTRIBUTE_FLAG_OFFSET]); |
Kinson Chik | 66552a8 | 2011-03-29 15:59:06 -0700 | [diff] [blame] | 303 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 304 | memset(&UTF16_name, 0x00, MAX_GPT_NAME_SIZE); |
| 305 | memcpy(UTF16_name, &data[(j * partition_entry_size) + |
| 306 | PARTITION_NAME_OFFSET], |
| 307 | MAX_GPT_NAME_SIZE); |
| 308 | /* |
| 309 | * Currently partition names in *.xml are UTF-8 and lowercase |
| 310 | * Only supporting english for now so removing 2nd byte of UTF-16 |
| 311 | */ |
| 312 | for(n = 0; n < MAX_GPT_NAME_SIZE/2; n++){ |
| 313 | partition_entries[partition_count].name[n] = UTF16_name[n*2]; |
| 314 | } |
| 315 | partition_count++; |
Kinson Chik | 66552a8 | 2011-03-29 15:59:06 -0700 | [diff] [blame] | 316 | } |
| 317 | } |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 318 | |
Kinson Chik | 66552a8 | 2011-03-29 15:59:06 -0700 | [diff] [blame] | 319 | return ret; |
| 320 | } |
| 321 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 322 | /* |
| 323 | * Fill name for android partition found. |
| 324 | */ |
| 325 | static void mbr_fill_name (struct partition_entry *partition_ent, |
| 326 | unsigned int type) |
| 327 | { |
| 328 | switch(type) |
| 329 | { |
| 330 | memset(partition_ent->name, 0, MAX_GPT_NAME_SIZE); |
| 331 | case MBR_MODEM_TYPE: |
| 332 | case MBR_MODEM_TYPE2: |
| 333 | /* if already assigned last name available then return */ |
| 334 | if(!strcmp((const char *)vfat_partitions[vfat_count], "NONE")) |
| 335 | return; |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame^] | 336 | strlcpy((char *)partition_ent->name, |
| 337 | (const char *)vfat_partitions[vfat_count], |
| 338 | sizeof(partition_ent->name)); |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 339 | vfat_count++; |
| 340 | break; |
| 341 | case MBR_SBL1_TYPE: |
| 342 | memcpy(partition_ent->name,"sbl1",4); |
| 343 | break; |
| 344 | case MBR_SBL2_TYPE: |
| 345 | memcpy(partition_ent->name,"sbl2",4); |
| 346 | break; |
| 347 | case MBR_SBL3_TYPE: |
| 348 | memcpy(partition_ent->name,"sbl3",4); |
| 349 | break; |
| 350 | case MBR_RPM_TYPE: |
| 351 | memcpy(partition_ent->name,"rpm",3); |
| 352 | break; |
| 353 | case MBR_TZ_TYPE: |
| 354 | memcpy(partition_ent->name,"tz",2); |
| 355 | break; |
| 356 | case MBR_ABOOT_TYPE: |
| 357 | memcpy(partition_ent->name,"aboot",5); |
| 358 | break; |
| 359 | case MBR_BOOT_TYPE: |
| 360 | memcpy(partition_ent->name,"boot",4); |
| 361 | break; |
| 362 | case MBR_MODEM_ST1_TYPE: |
| 363 | memcpy(partition_ent->name,"modem_st1",9); |
| 364 | break; |
| 365 | case MBR_MODEM_ST2_TYPE: |
| 366 | memcpy(partition_ent->name,"modem_st2",9); |
| 367 | break; |
| 368 | case MBR_EFS2_TYPE: |
| 369 | memcpy(partition_ent->name,"efs2",4); |
| 370 | break; |
| 371 | case MBR_USERDATA_TYPE: |
| 372 | if (ext3_count == sizeof(ext3_partitions) / sizeof(char*)) |
| 373 | return; |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame^] | 374 | strlcpy((char *)partition_ent->name, |
| 375 | (const char *)ext3_partitions[ext3_count], |
| 376 | sizeof(partition_ent->name)); |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 377 | ext3_count++; |
| 378 | break; |
| 379 | case MBR_RECOVERY_TYPE: |
| 380 | memcpy(partition_ent->name,"recovery",8); |
| 381 | break; |
| 382 | case MBR_MISC_TYPE: |
| 383 | memcpy(partition_ent->name,"misc",4); |
| 384 | break; |
| 385 | }; |
| 386 | } |
| 387 | |
| 388 | /* |
| 389 | * Find index of parition in array of partition entries |
| 390 | */ |
| 391 | unsigned partition_get_index (const char * name) |
| 392 | { |
| 393 | unsigned int input_string_length = strlen(name); |
Kinson Chik | 66552a8 | 2011-03-29 15:59:06 -0700 | [diff] [blame] | 394 | unsigned n; |
| 395 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 396 | for(n = 0; n < partition_count; n++){ |
| 397 | if(!memcmp(name, &partition_entries[n].name, input_string_length) && |
| 398 | input_string_length == strlen((const char *)&partition_entries[n].name)) |
| 399 | { |
| 400 | return n; |
Kinson Chik | 66552a8 | 2011-03-29 15:59:06 -0700 | [diff] [blame] | 401 | } |
| 402 | } |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 403 | return INVALID_PTN; |
| 404 | } |
| 405 | |
| 406 | /* Get size of the partition */ |
| 407 | unsigned long long partition_get_size (int index) |
| 408 | { |
| 409 | if (index == INVALID_PTN) |
| 410 | return 0; |
| 411 | else{ |
| 412 | return partition_entries[index].size * MMC_BOOT_RD_BLOCK_LEN; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | /* Get offset of the partition */ |
| 417 | unsigned long long partition_get_offset (int index) |
| 418 | { |
| 419 | if (index == INVALID_PTN) |
| 420 | return 0; |
| 421 | else{ |
| 422 | return partition_entries[index].first_lba * MMC_BOOT_RD_BLOCK_LEN; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | /* Debug: Print all parsed partitions */ |
| 427 | void partition_dump() |
| 428 | { |
| 429 | unsigned i = 0; |
| 430 | for (i=0; i< partition_count; i++){ |
| 431 | dprintf(SPEW, |
| 432 | "ptn[%d]:Name[%s] Size[%llu] Type[%u] First[%llu] Last[%llu]\n", |
| 433 | i, partition_entries[i].name, partition_entries[i].size, |
| 434 | partition_entries[i].dtype, partition_entries[i].first_lba, |
| 435 | partition_entries[i].last_lba); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | unsigned int partition_verify_mbr_signature(unsigned size, |
| 440 | unsigned char* buffer) |
| 441 | { |
| 442 | /* Avoid checking past end of buffer */ |
| 443 | if ((TABLE_SIGNATURE + 1) > size) |
| 444 | { |
| 445 | return MMC_BOOT_E_FAILURE; |
| 446 | } |
| 447 | /* Check to see if signature exists */ |
| 448 | if ((buffer[TABLE_SIGNATURE] != MMC_MBR_SIGNATURE_BYTE_0) || \ |
| 449 | (buffer[TABLE_SIGNATURE + 1] != MMC_MBR_SIGNATURE_BYTE_1)) |
| 450 | { |
| 451 | dprintf(CRITICAL, "MBR signature does not match. \n" ); |
| 452 | return MMC_BOOT_E_FAILURE; |
| 453 | } |
| 454 | return MMC_BOOT_E_SUCCESS; |
| 455 | } |
| 456 | |
| 457 | unsigned int mbr_partition_get_type(unsigned size, unsigned char* partition, |
| 458 | unsigned int *partition_type) |
| 459 | { |
| 460 | unsigned int type_offset = TABLE_ENTRY_0 + OFFSET_TYPE; |
| 461 | |
| 462 | if (size < type_offset) |
| 463 | { |
| 464 | goto end; |
| 465 | } |
| 466 | |
| 467 | *partition_type = partition[type_offset]; |
| 468 | end: |
| 469 | return MMC_BOOT_E_SUCCESS; |
| 470 | } |
| 471 | |
| 472 | unsigned int partition_get_type(unsigned size, unsigned char* partition, |
| 473 | unsigned int *partition_type) |
| 474 | { |
| 475 | unsigned int ret = MMC_BOOT_E_SUCCESS; |
| 476 | |
| 477 | /* |
| 478 | * If the block contains the MBR signature, then it's likely either |
| 479 | * MBR or MBR with protective type (GPT). If the MBR signature is |
| 480 | * not there, then it could be the GPT backup. |
| 481 | */ |
| 482 | |
| 483 | /* First check the MBR signature */ |
| 484 | ret = partition_verify_mbr_signature(size, partition); |
| 485 | if (ret == MMC_BOOT_E_SUCCESS) |
| 486 | { |
| 487 | unsigned int mbr_partition_type = PARTITION_TYPE_MBR; |
| 488 | |
| 489 | /* MBR signature verified. This could be MBR, MBR + EBR, or GPT */ |
| 490 | ret = mbr_partition_get_type(size, partition, &mbr_partition_type); |
| 491 | if (ret != MMC_BOOT_E_SUCCESS) |
| 492 | { |
| 493 | dprintf(CRITICAL, "Cannot get TYPE of partition"); |
| 494 | } |
| 495 | else if (MBR_PROTECTED_TYPE == mbr_partition_type) |
| 496 | { |
| 497 | *partition_type = PARTITION_TYPE_GPT; |
| 498 | } |
| 499 | else |
| 500 | { |
| 501 | *partition_type = PARTITION_TYPE_MBR; |
| 502 | } |
| 503 | } |
| 504 | else |
| 505 | { |
| 506 | /* |
| 507 | * This could be the GPT backup. Make that assumption for now. |
| 508 | * Anybody who treats the block as GPT backup should check the |
| 509 | * signature. |
| 510 | */ |
| 511 | *partition_type = PARTITION_TYPE_GPT_BACKUP; |
| 512 | } |
| 513 | return ret; |
Kinson Chik | 66552a8 | 2011-03-29 15:59:06 -0700 | [diff] [blame] | 514 | } |
Kinson Chik | 4d7444f | 2011-09-13 15:48:51 -0700 | [diff] [blame] | 515 | |
| 516 | /* |
| 517 | * Parse the gpt header and get the required header fields |
| 518 | * Return 0 on valid signature |
| 519 | */ |
| 520 | unsigned int partition_parse_gpt_header(unsigned char * buffer, |
| 521 | unsigned long long * first_usable_lba, |
| 522 | unsigned long * partition_entry_size, |
| 523 | unsigned long * header_size, |
| 524 | unsigned int * max_partition_count) |
| 525 | { |
| 526 | /* Check GPT Signature */ |
| 527 | if( ((uint32_t *)buffer)[0] != GPT_SIGNATURE_2 || |
| 528 | ((uint32_t *)buffer)[1] != GPT_SIGNATURE_1 ) |
| 529 | return 1; |
| 530 | |
| 531 | *header_size = GET_LWORD_FROM_BYTE(&buffer[HEADER_SIZE_OFFSET]); |
| 532 | *first_usable_lba = GET_LLWORD_FROM_BYTE(&buffer[FIRST_USABLE_LBA_OFFSET]); |
| 533 | *max_partition_count = GET_LWORD_FROM_BYTE(&buffer[PARTITION_COUNT_OFFSET]); |
| 534 | *partition_entry_size = GET_LWORD_FROM_BYTE(&buffer[PENTRY_SIZE_OFFSET]); |
| 535 | |
| 536 | return 0; |
| 537 | } |