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