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 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 29 | #define INVALID_PTN -1 |
| 30 | |
| 31 | #define PARTITION_TYPE_MBR 0 |
| 32 | #define PARTITION_TYPE_GPT 1 |
| 33 | #define PARTITION_TYPE_GPT_BACKUP 2 |
Kinson Chik | 66552a8 | 2011-03-29 15:59:06 -0700 | [diff] [blame] | 34 | |
| 35 | /* GPT Signature should be 0x5452415020494645 */ |
| 36 | #define GPT_SIGNATURE_1 0x54524150 |
| 37 | #define GPT_SIGNATURE_2 0x20494645 |
| 38 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 39 | #define MMC_MBR_SIGNATURE_BYTE_0 0x55 |
| 40 | #define MMC_MBR_SIGNATURE_BYTE_1 0xAA |
| 41 | |
Kinson Chik | 66552a8 | 2011-03-29 15:59:06 -0700 | [diff] [blame] | 42 | /* GPT Offsets */ |
Kinson Chik | 4d7444f | 2011-09-13 15:48:51 -0700 | [diff] [blame^] | 43 | #define PROTECTIVE_MBR_SIZE BLOCK_SIZE |
| 44 | #define PARTITION_TABLE_SIZE BLOCK_SIZE |
| 45 | #define PARTITION_ENTRY_SIZE BLOCK_SIZE |
Kinson Chik | 66552a8 | 2011-03-29 15:59:06 -0700 | [diff] [blame] | 46 | #define HEADER_SIZE_OFFSET 12 |
| 47 | #define HEADER_CRC_OFFSET 16 |
Kinson Chik | 4d7444f | 2011-09-13 15:48:51 -0700 | [diff] [blame^] | 48 | #define BACKUP_HEADER_OFFSET 32 |
Kinson Chik | 66552a8 | 2011-03-29 15:59:06 -0700 | [diff] [blame] | 49 | #define FIRST_USABLE_LBA_OFFSET 40 |
| 50 | #define LAST_USABLE_LBA_OFFSET 48 |
| 51 | #define PARTITION_ENTRIES_OFFSET 72 |
| 52 | #define PARTITION_COUNT_OFFSET 80 |
| 53 | #define PENTRY_SIZE_OFFSET 84 |
| 54 | #define PARTITION_CRC_OFFSET 88 |
| 55 | |
| 56 | #define UNIQUE_GUID_OFFSET 16 |
| 57 | #define FIRST_LBA_OFFSET 32 |
| 58 | #define LAST_LBA_OFFSET 40 |
| 59 | #define ATTRIBUTE_FLAG_OFFSET 48 |
| 60 | #define PARTITION_NAME_OFFSET 56 |
| 61 | |
| 62 | #define MAX_GPT_NAME_SIZE 72 |
| 63 | #define PARTITION_TYPE_GUID_SIZE 16 |
| 64 | #define UNIQUE_PARTITION_GUID_SIZE 16 |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 65 | #define NUM_PARTITIONS 32 |
| 66 | |
| 67 | /* Some useful define used to access the MBR/EBR table */ |
| 68 | #define BLOCK_SIZE 0x200 |
| 69 | #define TABLE_ENTRY_0 0x1BE |
| 70 | #define TABLE_ENTRY_1 0x1CE |
| 71 | #define TABLE_ENTRY_2 0x1DE |
| 72 | #define TABLE_ENTRY_3 0x1EE |
| 73 | #define TABLE_SIGNATURE 0x1FE |
| 74 | #define TABLE_ENTRY_SIZE 0x010 |
| 75 | |
| 76 | #define OFFSET_STATUS 0x00 |
| 77 | #define OFFSET_TYPE 0x04 |
| 78 | #define OFFSET_FIRST_SEC 0x08 |
| 79 | #define OFFSET_SIZE 0x0C |
| 80 | #define COPYBUFF_SIZE (1024 * 16) |
| 81 | #define BINARY_IN_TABLE_SIZE (16 * 512) |
| 82 | #define MAX_FILE_ENTRIES 20 |
| 83 | |
| 84 | #define MBR_EBR_TYPE 0x05 |
| 85 | #define MBR_MODEM_TYPE 0x06 |
| 86 | #define MBR_MODEM_TYPE2 0x0C |
| 87 | #define MBR_SBL1_TYPE 0x4D |
| 88 | #define MBR_SBL2_TYPE 0x51 |
| 89 | #define MBR_SBL3_TYPE 0x45 |
| 90 | #define MBR_RPM_TYPE 0x47 |
| 91 | #define MBR_TZ_TYPE 0x46 |
| 92 | #define MBR_MODEM_ST1_TYPE 0x4A |
| 93 | #define MBR_MODEM_ST2_TYPE 0x4B |
| 94 | #define MBR_EFS2_TYPE 0x4E |
| 95 | |
| 96 | #define MBR_ABOOT_TYPE 0x4C |
| 97 | #define MBR_BOOT_TYPE 0x48 |
| 98 | #define MBR_SYSTEM_TYPE 0x82 |
| 99 | #define MBR_USERDATA_TYPE 0x83 |
| 100 | #define MBR_RECOVERY_TYPE 0x60 |
| 101 | #define MBR_MISC_TYPE 0x63 |
| 102 | #define MBR_PROTECTED_TYPE 0xEE |
Kinson Chik | 66552a8 | 2011-03-29 15:59:06 -0700 | [diff] [blame] | 103 | |
| 104 | #define GET_LLWORD_FROM_BYTE(x) ((unsigned long long)*(x) | \ |
| 105 | ((unsigned long long)*(x+1) << 8) | \ |
| 106 | ((unsigned long long)*(x+2) << 16) | \ |
| 107 | ((unsigned long long)*(x+3) << 24) | \ |
| 108 | ((unsigned long long)*(x+4) << 32) | \ |
| 109 | ((unsigned long long)*(x+5) << 40) | \ |
| 110 | ((unsigned long long)*(x+6) << 48) | \ |
| 111 | ((unsigned long long)*(x+7) << 56)) |
| 112 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 113 | /* Unified mbr and gpt entry types */ |
| 114 | struct partition_entry |
Kinson Chik | 66552a8 | 2011-03-29 15:59:06 -0700 | [diff] [blame] | 115 | { |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 116 | unsigned char type_guid[PARTITION_TYPE_GUID_SIZE]; |
| 117 | unsigned dtype; |
| 118 | unsigned char unique_partition_guid[UNIQUE_PARTITION_GUID_SIZE]; |
| 119 | unsigned long long first_lba; |
| 120 | unsigned long long last_lba; |
| 121 | unsigned long long size; |
| 122 | unsigned long long attribute_flag; |
| 123 | unsigned char name[MAX_GPT_NAME_SIZE]; |
Kinson Chik | 66552a8 | 2011-03-29 15:59:06 -0700 | [diff] [blame] | 124 | }; |
| 125 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 126 | static void mbr_fill_name (struct partition_entry *partition_ent, unsigned int type); |
| 127 | unsigned int mmc_boot_read_gpt( struct mmc_boot_host * mmc_host, |
| 128 | struct mmc_boot_card * mmc_card); |
| 129 | unsigned int mmc_boot_read_mbr( struct mmc_boot_host * mmc_host, |
| 130 | struct mmc_boot_card * mmc_card); |
| 131 | unsigned partition_get_index (const char * name); |
| 132 | unsigned long long partition_get_size (int index); |
| 133 | unsigned long long partition_get_offset (int index); |
| 134 | unsigned int partition_verify_mbr_signature(unsigned size, unsigned char* buffer); |
| 135 | unsigned int mbr_partition_get_type(unsigned size, unsigned char* partition, |
| 136 | unsigned int *partition_type); |
| 137 | unsigned int partition_get_type(unsigned size, unsigned char* partition, |
| 138 | unsigned int *partition_type); |
| 139 | unsigned int partition_read_table( struct mmc_boot_host * mmc_host, |
| 140 | struct mmc_boot_card * mmc_card); |
Kinson Chik | 4d7444f | 2011-09-13 15:48:51 -0700 | [diff] [blame^] | 141 | unsigned int partition_parse_gpt_header(unsigned char * buffer, |
| 142 | unsigned long long * first_usable_lba, |
| 143 | unsigned long * partition_entry_size, |
| 144 | unsigned long * header_size, |
| 145 | unsigned int * max_partition_count); |
| 146 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 147 | /* For Debugging */ |
| 148 | void partition_dump(void); |