blob: f0645d278ab2d7a2528b8c5208c657397f5a4cb1 [file] [log] [blame]
Channagoud Kadabic18d70f2013-03-18 15:42:42 -07001/* Copyright (c) 2011-2013, The Linux Foundation. All rights reserved.
Kinson Chik66552a82011-03-29 15:59:06 -07002
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.
Duy Truongf3ac7b32013-02-13 01:07:28 -080012 * * Neither the name of The Linux Foundation nor the names of its
Kinson Chik66552a82011-03-29 15:59:06 -070013 * 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 */
Kinson Chik4d7444f2011-09-13 15:48:51 -070043#define PROTECTIVE_MBR_SIZE BLOCK_SIZE
Kinson Chik66552a82011-03-29 15:59:06 -070044#define HEADER_SIZE_OFFSET 12
45#define HEADER_CRC_OFFSET 16
Neeti Desai5f26aff2011-09-30 10:27:40 -070046#define PRIMARY_HEADER_OFFSET 24
Kinson Chik4d7444f2011-09-13 15:48:51 -070047#define BACKUP_HEADER_OFFSET 32
Kinson Chik66552a82011-03-29 15:59:06 -070048#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
Neeti Desai5f26aff2011-09-30 10:27:40 -070055#define MIN_PARTITION_ARRAY_SIZE 0x4000
56
Neeti Desaica8c9602011-10-06 11:40:00 -070057#define PARTITION_ENTRY_LAST_LBA 40
58
59#define ENTRY_SIZE 0x080
60
Kinson Chik66552a82011-03-29 15:59:06 -070061#define UNIQUE_GUID_OFFSET 16
62#define FIRST_LBA_OFFSET 32
63#define LAST_LBA_OFFSET 40
64#define ATTRIBUTE_FLAG_OFFSET 48
65#define PARTITION_NAME_OFFSET 56
66
67#define MAX_GPT_NAME_SIZE 72
68#define PARTITION_TYPE_GUID_SIZE 16
69#define UNIQUE_PARTITION_GUID_SIZE 16
Kinson Chikf1a43512011-07-14 11:28:39 -070070#define NUM_PARTITIONS 32
71
72/* Some useful define used to access the MBR/EBR table */
73#define BLOCK_SIZE 0x200
74#define TABLE_ENTRY_0 0x1BE
75#define TABLE_ENTRY_1 0x1CE
76#define TABLE_ENTRY_2 0x1DE
77#define TABLE_ENTRY_3 0x1EE
78#define TABLE_SIGNATURE 0x1FE
79#define TABLE_ENTRY_SIZE 0x010
80
81#define OFFSET_STATUS 0x00
82#define OFFSET_TYPE 0x04
83#define OFFSET_FIRST_SEC 0x08
84#define OFFSET_SIZE 0x0C
85#define COPYBUFF_SIZE (1024 * 16)
86#define BINARY_IN_TABLE_SIZE (16 * 512)
87#define MAX_FILE_ENTRIES 20
88
89#define MBR_EBR_TYPE 0x05
90#define MBR_MODEM_TYPE 0x06
91#define MBR_MODEM_TYPE2 0x0C
92#define MBR_SBL1_TYPE 0x4D
93#define MBR_SBL2_TYPE 0x51
94#define MBR_SBL3_TYPE 0x45
95#define MBR_RPM_TYPE 0x47
96#define MBR_TZ_TYPE 0x46
97#define MBR_MODEM_ST1_TYPE 0x4A
98#define MBR_MODEM_ST2_TYPE 0x4B
99#define MBR_EFS2_TYPE 0x4E
100
101#define MBR_ABOOT_TYPE 0x4C
102#define MBR_BOOT_TYPE 0x48
103#define MBR_SYSTEM_TYPE 0x82
104#define MBR_USERDATA_TYPE 0x83
105#define MBR_RECOVERY_TYPE 0x60
106#define MBR_MISC_TYPE 0x63
107#define MBR_PROTECTED_TYPE 0xEE
Neeti Desai2d6b0e42012-03-23 15:48:57 -0700108#define MBR_SSD_TYPE 0x5D
Kinson Chik66552a82011-03-29 15:59:06 -0700109
110#define GET_LLWORD_FROM_BYTE(x) ((unsigned long long)*(x) | \
111 ((unsigned long long)*(x+1) << 8) | \
112 ((unsigned long long)*(x+2) << 16) | \
113 ((unsigned long long)*(x+3) << 24) | \
114 ((unsigned long long)*(x+4) << 32) | \
115 ((unsigned long long)*(x+5) << 40) | \
116 ((unsigned long long)*(x+6) << 48) | \
117 ((unsigned long long)*(x+7) << 56))
118
Neeti Desai5f26aff2011-09-30 10:27:40 -0700119#define GET_LONG(x) ((uint32_t)*(x) | \
120 ((uint32_t)*(x+1) << 8) | \
121 ((uint32_t)*(x+2) << 16) | \
122 ((uint32_t)*(x+3) << 24))
123
124#define PUT_LONG(x, y) *(x) = y & 0xff; \
125 *(x+1) = (y >> 8) & 0xff; \
126 *(x+2) = (y >> 16) & 0xff; \
127 *(x+3) = (y >> 24) & 0xff;
128
129#define PUT_LONG_LONG(x,y) *(x) =(y) & 0xff; \
130 *((x)+1) = (((y) >> 8) & 0xff); \
131 *((x)+2) = (((y) >> 16) & 0xff); \
132 *((x)+3) = (((y) >> 24) & 0xff); \
133 *((x)+4) = (((y) >> 32) & 0xff); \
134 *((x)+5) = (((y) >> 40) & 0xff); \
135 *((x)+6) = (((y) >> 48) & 0xff); \
136 *((x)+7) = (((y) >> 56) & 0xff);
137
Kinson Chikf1a43512011-07-14 11:28:39 -0700138/* Unified mbr and gpt entry types */
Ajay Dudanib01e5062011-12-03 23:23:42 -0800139struct partition_entry {
140 unsigned char type_guid[PARTITION_TYPE_GUID_SIZE];
141 unsigned dtype;
142 unsigned char unique_partition_guid[UNIQUE_PARTITION_GUID_SIZE];
143 unsigned long long first_lba;
144 unsigned long long last_lba;
145 unsigned long long size;
146 unsigned long long attribute_flag;
147 unsigned char name[MAX_GPT_NAME_SIZE];
Kinson Chik66552a82011-03-29 15:59:06 -0700148};
149
Ajay Dudanib01e5062011-12-03 23:23:42 -0800150static void mbr_fill_name(struct partition_entry *partition_ent,
151 unsigned int type);
Channagoud Kadabic18d70f2013-03-18 15:42:42 -0700152unsigned int mmc_boot_read_gpt(struct mmc_host *mmc_host,
153 struct mmc_card *mmc_card);
154unsigned int mmc_boot_read_mbr(struct mmc_host *mmc_host,
155 struct mmc_card *mmc_card);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800156unsigned partition_get_index(const char *name);
157unsigned long long partition_get_size(int index);
158unsigned long long partition_get_offset(int index);
159unsigned int partition_verify_mbr_signature(unsigned size,
160 unsigned char *buffer);
161unsigned int mbr_partition_get_type(unsigned size, unsigned char *partition,
162 unsigned int *partition_type);
163unsigned int partition_get_type(unsigned size, unsigned char *partition,
164 unsigned int *partition_type);
Channagoud Kadabi16b37352013-04-03 12:45:46 -0700165unsigned int partition_read_table(void *mmc_host,
Channagoud Kadabic18d70f2013-03-18 15:42:42 -0700166 struct mmc_card *mmc_card);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800167unsigned int partition_parse_gpt_header(unsigned char *buffer,
168 unsigned long long *first_usable_lba,
169 unsigned int *partition_entry_size,
170 unsigned int *header_size,
171 unsigned int *max_partition_count);
Neeti Desai5f26aff2011-09-30 10:27:40 -0700172
173unsigned int write_mbr(unsigned size, unsigned char *mbrImage,
Channagoud Kadabic18d70f2013-03-18 15:42:42 -0700174 struct mmc_host *mmc_host,
175 struct mmc_card *mmc_card);
Neeti Desai5f26aff2011-09-30 10:27:40 -0700176unsigned int write_gpt(unsigned size, unsigned char *gptImage,
Channagoud Kadabic18d70f2013-03-18 15:42:42 -0700177 struct mmc_host *mmc_host,
178 struct mmc_card *mmc_card);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800179unsigned int write_partition(unsigned size, unsigned char *partition);
Neeti Desai5f26aff2011-09-30 10:27:40 -0700180
Kinson Chikf1a43512011-07-14 11:28:39 -0700181/* For Debugging */
182void partition_dump(void);