blob: cf89433bfa84b095870959e0c798feefeba91486 [file] [log] [blame]
Sundarajan Srinivasan4ab741f2013-12-04 17:14:37 -08001/* Copyright (c) 2011-2014, 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
Pavel Nedev140ed082013-03-27 16:55:04 +020029#ifndef __PARTITION_PARSER_H
30#define __PARTITION_PARSER_H
31
32#include <mmc.h>
33
Kinson Chikf1a43512011-07-14 11:28:39 -070034#define INVALID_PTN -1
35
36#define PARTITION_TYPE_MBR 0
37#define PARTITION_TYPE_GPT 1
38#define PARTITION_TYPE_GPT_BACKUP 2
Kinson Chik66552a82011-03-29 15:59:06 -070039
40/* GPT Signature should be 0x5452415020494645 */
41#define GPT_SIGNATURE_1 0x54524150
42#define GPT_SIGNATURE_2 0x20494645
43
Kinson Chikf1a43512011-07-14 11:28:39 -070044#define MMC_MBR_SIGNATURE_BYTE_0 0x55
45#define MMC_MBR_SIGNATURE_BYTE_1 0xAA
46
Kinson Chik66552a82011-03-29 15:59:06 -070047/* GPT Offsets */
Kinson Chik4d7444f2011-09-13 15:48:51 -070048#define PROTECTIVE_MBR_SIZE BLOCK_SIZE
Kinson Chik66552a82011-03-29 15:59:06 -070049#define HEADER_SIZE_OFFSET 12
50#define HEADER_CRC_OFFSET 16
Neeti Desai5f26aff2011-09-30 10:27:40 -070051#define PRIMARY_HEADER_OFFSET 24
Kinson Chik4d7444f2011-09-13 15:48:51 -070052#define BACKUP_HEADER_OFFSET 32
Kinson Chik66552a82011-03-29 15:59:06 -070053#define FIRST_USABLE_LBA_OFFSET 40
54#define LAST_USABLE_LBA_OFFSET 48
55#define PARTITION_ENTRIES_OFFSET 72
56#define PARTITION_COUNT_OFFSET 80
57#define PENTRY_SIZE_OFFSET 84
58#define PARTITION_CRC_OFFSET 88
59
Neeti Desai5f26aff2011-09-30 10:27:40 -070060#define MIN_PARTITION_ARRAY_SIZE 0x4000
61
Neeti Desaica8c9602011-10-06 11:40:00 -070062#define PARTITION_ENTRY_LAST_LBA 40
63
64#define ENTRY_SIZE 0x080
65
Kinson Chik66552a82011-03-29 15:59:06 -070066#define UNIQUE_GUID_OFFSET 16
67#define FIRST_LBA_OFFSET 32
68#define LAST_LBA_OFFSET 40
69#define ATTRIBUTE_FLAG_OFFSET 48
70#define PARTITION_NAME_OFFSET 56
71
72#define MAX_GPT_NAME_SIZE 72
73#define PARTITION_TYPE_GUID_SIZE 16
74#define UNIQUE_PARTITION_GUID_SIZE 16
Channagoud Kadabie9b94022013-11-12 13:49:41 -080075#define NUM_PARTITIONS 128
Kinson Chikf1a43512011-07-14 11:28:39 -070076
77/* Some useful define used to access the MBR/EBR table */
78#define BLOCK_SIZE 0x200
79#define TABLE_ENTRY_0 0x1BE
80#define TABLE_ENTRY_1 0x1CE
81#define TABLE_ENTRY_2 0x1DE
82#define TABLE_ENTRY_3 0x1EE
83#define TABLE_SIGNATURE 0x1FE
84#define TABLE_ENTRY_SIZE 0x010
85
86#define OFFSET_STATUS 0x00
87#define OFFSET_TYPE 0x04
88#define OFFSET_FIRST_SEC 0x08
89#define OFFSET_SIZE 0x0C
90#define COPYBUFF_SIZE (1024 * 16)
91#define BINARY_IN_TABLE_SIZE (16 * 512)
92#define MAX_FILE_ENTRIES 20
93
94#define MBR_EBR_TYPE 0x05
95#define MBR_MODEM_TYPE 0x06
96#define MBR_MODEM_TYPE2 0x0C
97#define MBR_SBL1_TYPE 0x4D
98#define MBR_SBL2_TYPE 0x51
99#define MBR_SBL3_TYPE 0x45
100#define MBR_RPM_TYPE 0x47
101#define MBR_TZ_TYPE 0x46
102#define MBR_MODEM_ST1_TYPE 0x4A
103#define MBR_MODEM_ST2_TYPE 0x4B
104#define MBR_EFS2_TYPE 0x4E
105
106#define MBR_ABOOT_TYPE 0x4C
107#define MBR_BOOT_TYPE 0x48
108#define MBR_SYSTEM_TYPE 0x82
109#define MBR_USERDATA_TYPE 0x83
110#define MBR_RECOVERY_TYPE 0x60
111#define MBR_MISC_TYPE 0x63
112#define MBR_PROTECTED_TYPE 0xEE
Neeti Desai2d6b0e42012-03-23 15:48:57 -0700113#define MBR_SSD_TYPE 0x5D
Kinson Chik66552a82011-03-29 15:59:06 -0700114
Channagoud Kadabi0bebf6f2013-04-05 12:54:00 -0700115#define GET_LWORD_FROM_BYTE(x) ((unsigned)*(x) | \
116 ((unsigned)*(x+1) << 8) | \
117 ((unsigned)*(x+2) << 16) | \
118 ((unsigned)*(x+3) << 24))
119
Kinson Chik66552a82011-03-29 15:59:06 -0700120#define GET_LLWORD_FROM_BYTE(x) ((unsigned long long)*(x) | \
121 ((unsigned long long)*(x+1) << 8) | \
122 ((unsigned long long)*(x+2) << 16) | \
123 ((unsigned long long)*(x+3) << 24) | \
124 ((unsigned long long)*(x+4) << 32) | \
125 ((unsigned long long)*(x+5) << 40) | \
126 ((unsigned long long)*(x+6) << 48) | \
127 ((unsigned long long)*(x+7) << 56))
128
Neeti Desai5f26aff2011-09-30 10:27:40 -0700129#define GET_LONG(x) ((uint32_t)*(x) | \
130 ((uint32_t)*(x+1) << 8) | \
131 ((uint32_t)*(x+2) << 16) | \
132 ((uint32_t)*(x+3) << 24))
133
134#define PUT_LONG(x, y) *(x) = y & 0xff; \
135 *(x+1) = (y >> 8) & 0xff; \
136 *(x+2) = (y >> 16) & 0xff; \
137 *(x+3) = (y >> 24) & 0xff;
138
139#define PUT_LONG_LONG(x,y) *(x) =(y) & 0xff; \
140 *((x)+1) = (((y) >> 8) & 0xff); \
141 *((x)+2) = (((y) >> 16) & 0xff); \
142 *((x)+3) = (((y) >> 24) & 0xff); \
143 *((x)+4) = (((y) >> 32) & 0xff); \
144 *((x)+5) = (((y) >> 40) & 0xff); \
145 *((x)+6) = (((y) >> 48) & 0xff); \
146 *((x)+7) = (((y) >> 56) & 0xff);
147
Kinson Chikf1a43512011-07-14 11:28:39 -0700148/* Unified mbr and gpt entry types */
Ajay Dudanib01e5062011-12-03 23:23:42 -0800149struct partition_entry {
150 unsigned char type_guid[PARTITION_TYPE_GUID_SIZE];
151 unsigned dtype;
152 unsigned char unique_partition_guid[UNIQUE_PARTITION_GUID_SIZE];
153 unsigned long long first_lba;
154 unsigned long long last_lba;
155 unsigned long long size;
156 unsigned long long attribute_flag;
157 unsigned char name[MAX_GPT_NAME_SIZE];
Sundarajan Srinivasan4ab741f2013-12-04 17:14:37 -0800158 uint8_t lun;
Kinson Chik66552a82011-03-29 15:59:06 -0700159};
160
Pavel Nedev285ad922013-04-26 10:39:19 +0300161int partition_get_index(const char *name);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800162unsigned long long partition_get_size(int index);
163unsigned long long partition_get_offset(int index);
Sundarajan Srinivasan4ab741f2013-12-04 17:14:37 -0800164uint8_t partition_get_lun(int index);
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700165unsigned int partition_read_table();
Ajay Dudanib01e5062011-12-03 23:23:42 -0800166unsigned int write_partition(unsigned size, unsigned char *partition);
Pavel Nedevb5ba62d2013-07-22 11:57:41 +0300167bool partition_gpt_exists();
Neeti Desai5f26aff2011-09-30 10:27:40 -0700168
Kinson Chikf1a43512011-07-14 11:28:39 -0700169/* For Debugging */
170void partition_dump(void);
Pavel Nedev140ed082013-03-27 16:55:04 +0200171
172#endif