blob: e8e08b20c3bec72a7c367a95a1eba5a92bfbca9d [file] [log] [blame]
Channagoud Kadabif8c5bf72015-02-26 11:33:52 -08001/* Copyright (c) 2011-2015, 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.
Channagoud Kadabif73aa292013-01-31 16:55:20 -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
29#include <stdlib.h>
30#include <string.h>
vijay kumar24c9ed12015-04-28 13:05:04 +053031#include <crc32.h>
Kinson Chik66552a82011-03-29 15:59:06 -070032#include "mmc.h"
33#include "partition_parser.h"
vijay kumar24c9ed12015-04-28 13:05:04 +053034#define GPT_HEADER_SIZE 92
35#define GPT_LBA 1
36#define PARTITION_ENTRY_SIZE 128
37static bool flashing_gpt = 0;
38static bool parse_secondary_gpt = 0;
Sundarajan Srinivasan554863f2013-12-04 17:14:37 -080039__WEAK void mmc_set_lun(uint8_t lun)
40{
41}
42
43__WEAK uint8_t mmc_get_lun(void)
44{
45 return 0;
46}
47
48__WEAK void mmc_read_partition_table(uint8_t arg)
49{
50 if(partition_read_table())
51 {
52 dprintf(CRITICAL, "Error reading the partition table info\n");
53 ASSERT(0);
54 }
55}
56
Channagoud Kadabi96c629e2013-09-10 14:21:30 -070057static uint32_t mmc_boot_read_gpt(uint32_t block_size);
58static uint32_t mmc_boot_read_mbr(uint32_t block_size);
Channagoud Kadabi0e332852013-04-19 12:55:53 -070059static void mbr_fill_name(struct partition_entry *partition_ent,
60 uint32_t type);
61static uint32_t partition_verify_mbr_signature(uint32_t size,
62 uint8_t *buffer);
63static uint32_t mbr_partition_get_type(uint32_t size, uint8_t *partition,
64 uint32_t *partition_type);
65
66static uint32_t partition_get_type(uint32_t size, uint8_t *partition,
67 uint32_t *partition_type);
68static uint32_t partition_parse_gpt_header(uint8_t *buffer,
69 uint64_t *first_usable_lba,
70 uint32_t *partition_entry_size,
71 uint32_t *header_size,
72 uint32_t *max_partition_count);
73
Channagoud Kadabi96c629e2013-09-10 14:21:30 -070074static uint32_t write_mbr(uint32_t, uint8_t *mbrImage, uint32_t block_size);
75static uint32_t write_gpt(uint32_t size, uint8_t *gptImage, uint32_t block_size);
Channagoud Kadabi0e332852013-04-19 12:55:53 -070076
Ajay Dudanib01e5062011-12-03 23:23:42 -080077char *ext3_partitions[] =
78 { "system", "userdata", "persist", "cache", "tombstones" };
79char *vfat_partitions[] = { "modem", "mdm", "NONE" };
80
Kinson Chikf1a43512011-07-14 11:28:39 -070081unsigned int ext3_count = 0;
82unsigned int vfat_count = 0;
83
Channagoud Kadabie9cb1c22013-11-12 13:49:41 -080084struct partition_entry *partition_entries;
Pavel Nedevb5ba62d2013-07-22 11:57:41 +030085static unsigned gpt_partitions_exist = 0;
Sundarajan Srinivasan554863f2013-12-04 17:14:37 -080086static unsigned partition_count;
Kinson Chikf1a43512011-07-14 11:28:39 -070087
Channagoud Kadabi0e332852013-04-19 12:55:53 -070088unsigned int partition_read_table()
Kinson Chikf1a43512011-07-14 11:28:39 -070089{
Ajay Dudanib01e5062011-12-03 23:23:42 -080090 unsigned int ret;
Channagoud Kadabi96c629e2013-09-10 14:21:30 -070091 uint32_t block_size;
92
93 block_size = mmc_get_device_blocksize();
Kinson Chikf1a43512011-07-14 11:28:39 -070094
Channagoud Kadabie9cb1c22013-11-12 13:49:41 -080095 /* Allocate partition entries array */
Sundarajan Srinivasan554863f2013-12-04 17:14:37 -080096 if(!partition_entries)
97 {
98 partition_entries = (struct partition_entry *) calloc(NUM_PARTITIONS, sizeof(struct partition_entry));
99 ASSERT(partition_entries);
100 }
Channagoud Kadabie9cb1c22013-11-12 13:49:41 -0800101
Ajay Dudanib01e5062011-12-03 23:23:42 -0800102 /* Read MBR of the card */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700103 ret = mmc_boot_read_mbr(block_size);
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700104 if (ret) {
Ajay Dudanib01e5062011-12-03 23:23:42 -0800105 dprintf(CRITICAL, "MMC Boot: MBR read failed!\n");
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700106 return 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800107 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700108
Ajay Dudanib01e5062011-12-03 23:23:42 -0800109 /* Read GPT of the card if exist */
110 if (gpt_partitions_exist) {
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700111 ret = mmc_boot_read_gpt(block_size);
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700112 if (ret) {
Ajay Dudanib01e5062011-12-03 23:23:42 -0800113 dprintf(CRITICAL, "MMC Boot: GPT read failed!\n");
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700114 return 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800115 }
116 }
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700117 return 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700118}
119
120/*
121 * Read MBR from MMC card and fill partition table.
122 */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700123static unsigned int mmc_boot_read_mbr(uint32_t block_size)
Kinson Chikf1a43512011-07-14 11:28:39 -0700124{
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700125 uint8_t *buffer = NULL;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800126 unsigned int dtype;
127 unsigned int dfirstsec;
128 unsigned int EBR_first_sec;
129 unsigned int EBR_current_sec;
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700130 int ret = 0;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800131 int idx, i;
Kinson Chikf1a43512011-07-14 11:28:39 -0700132
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700133 buffer = (uint8_t *)memalign(CACHE_LINE, ROUNDUP(block_size, CACHE_LINE));
134
135 if (!buffer)
136 {
137 dprintf(CRITICAL, "Error allocating memory while reading partition table\n");
138 ret = -1;
139 goto end;
140 }
141
Ajay Dudanib01e5062011-12-03 23:23:42 -0800142 /* Print out the MBR first */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700143 ret = mmc_read(0, (unsigned int *)buffer, block_size);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800144 if (ret) {
145 dprintf(CRITICAL, "Could not read partition from mmc\n");
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700146 goto end;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800147 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700148
Ajay Dudanib01e5062011-12-03 23:23:42 -0800149 /* Check to see if signature exists */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700150 ret = partition_verify_mbr_signature(block_size, buffer);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800151 if (ret) {
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700152 goto end;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800153 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700154
Ajay Dudanib01e5062011-12-03 23:23:42 -0800155 /*
156 * Process each of the four partitions in the MBR by reading the table
157 * information into our mbr table.
158 */
Ajay Dudanib01e5062011-12-03 23:23:42 -0800159 idx = TABLE_ENTRY_0;
160 for (i = 0; i < 4; i++) {
161 /* Type 0xEE indicates end of MBR and GPT partitions exist */
162 dtype = buffer[idx + i * TABLE_ENTRY_SIZE + OFFSET_TYPE];
163 if (dtype == MBR_PROTECTED_TYPE) {
164 gpt_partitions_exist = 1;
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700165 goto end;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800166 }
167 partition_entries[partition_count].dtype = dtype;
168 partition_entries[partition_count].attribute_flag =
169 buffer[idx + i * TABLE_ENTRY_SIZE + OFFSET_STATUS];
170 partition_entries[partition_count].first_lba =
171 GET_LWORD_FROM_BYTE(&buffer[idx +
172 i * TABLE_ENTRY_SIZE +
173 OFFSET_FIRST_SEC]);
174 partition_entries[partition_count].size =
175 GET_LWORD_FROM_BYTE(&buffer[idx +
176 i * TABLE_ENTRY_SIZE +
177 OFFSET_SIZE]);
178 dfirstsec = partition_entries[partition_count].first_lba;
179 mbr_fill_name(&partition_entries[partition_count],
180 partition_entries[partition_count].dtype);
181 partition_count++;
182 if (partition_count == NUM_PARTITIONS)
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700183 goto end;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800184 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700185
Ajay Dudanib01e5062011-12-03 23:23:42 -0800186 /* See if the last partition is EBR, if not, parsing is done */
187 if (dtype != MBR_EBR_TYPE) {
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700188 goto end;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800189 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700190
Ajay Dudanib01e5062011-12-03 23:23:42 -0800191 EBR_first_sec = dfirstsec;
192 EBR_current_sec = dfirstsec;
Kinson Chikf1a43512011-07-14 11:28:39 -0700193
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700194 ret = mmc_read((EBR_first_sec * block_size), (unsigned int *)buffer, block_size);
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700195 if (ret)
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700196 goto end;
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700197
Ajay Dudanib01e5062011-12-03 23:23:42 -0800198 /* Loop to parse the EBR */
199 for (i = 0;; i++) {
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700200 ret = partition_verify_mbr_signature(block_size, buffer);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800201 if (ret) {
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700202 ret = 0;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800203 break;
204 }
205 partition_entries[partition_count].attribute_flag =
206 buffer[TABLE_ENTRY_0 + OFFSET_STATUS];
207 partition_entries[partition_count].dtype =
208 buffer[TABLE_ENTRY_0 + OFFSET_TYPE];
209 partition_entries[partition_count].first_lba =
210 GET_LWORD_FROM_BYTE(&buffer[TABLE_ENTRY_0 +
211 OFFSET_FIRST_SEC]) +
212 EBR_current_sec;
213 partition_entries[partition_count].size =
214 GET_LWORD_FROM_BYTE(&buffer[TABLE_ENTRY_0 + OFFSET_SIZE]);
215 mbr_fill_name(&(partition_entries[partition_count]),
216 partition_entries[partition_count].dtype);
217 partition_count++;
218 if (partition_count == NUM_PARTITIONS)
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700219 goto end;
Kinson Chikf1a43512011-07-14 11:28:39 -0700220
Ajay Dudanib01e5062011-12-03 23:23:42 -0800221 dfirstsec =
222 GET_LWORD_FROM_BYTE(&buffer
223 [TABLE_ENTRY_1 + OFFSET_FIRST_SEC]);
224 if (dfirstsec == 0) {
225 /* Getting to the end of the EBR tables */
226 break;
227 }
228 /* More EBR to follow - read in the next EBR sector */
229 dprintf(SPEW, "Reading EBR block from 0x%X\n", EBR_first_sec
230 + dfirstsec);
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700231 ret = mmc_read(((EBR_first_sec + dfirstsec) * block_size),(unsigned int *)buffer,
232 block_size);
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700233 if (ret)
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700234 goto end;
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700235
Ajay Dudanib01e5062011-12-03 23:23:42 -0800236 EBR_current_sec = EBR_first_sec + dfirstsec;
237 }
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700238end:
239 if (buffer)
240 free(buffer);
241
Ajay Dudanib01e5062011-12-03 23:23:42 -0800242 return ret;
Kinson Chikf1a43512011-07-14 11:28:39 -0700243}
Kinson Chik66552a82011-03-29 15:59:06 -0700244
245/*
246 * Read GPT from MMC and fill partition table
247 */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700248static unsigned int mmc_boot_read_gpt(uint32_t block_size)
Kinson Chikf1a43512011-07-14 11:28:39 -0700249{
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700250 int ret = 0;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800251 unsigned int header_size;
252 unsigned long long first_usable_lba;
253 unsigned long long backup_header_lba;
Neeti Desaice6ad9a2012-03-21 18:57:59 -0700254 unsigned long long card_size_sec;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800255 unsigned int max_partition_count = 0;
256 unsigned int partition_entry_size;
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700257 unsigned int i = 0; /* Counter for each block */
258 unsigned int j = 0; /* Counter for each entry in a block */
Ajay Dudanib01e5062011-12-03 23:23:42 -0800259 unsigned int n = 0; /* Counter for UTF-16 -> 8 conversion */
260 unsigned char UTF16_name[MAX_GPT_NAME_SIZE];
261 /* LBA of first partition -- 1 Block after Protected MBR + 1 for PT */
Neeti Desaice6ad9a2012-03-21 18:57:59 -0700262 unsigned long long partition_0;
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700263 uint64_t device_density;
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700264 uint8_t *data = NULL;
265 uint32_t part_entry_cnt = block_size / ENTRY_SIZE;
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700266
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700267 /* Get the density of the mmc device */
268
269 device_density = mmc_get_device_capacity();
270
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700271 data = (uint8_t *)memalign(CACHE_LINE, ROUNDUP(block_size, CACHE_LINE));
272 if (!data)
273 {
274 dprintf(CRITICAL, "Failed to Allocate memory to read partition table\n");
275 ret = -1;
276 goto end;
277 }
278
Ajay Dudanib01e5062011-12-03 23:23:42 -0800279 /* Print out the GPT first */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700280 ret = mmc_read(block_size, (unsigned int *)data, block_size);
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700281 if (ret)
Channagoud Kadabi39f13522013-07-31 14:46:22 -0700282 {
Ajay Dudanib01e5062011-12-03 23:23:42 -0800283 dprintf(CRITICAL, "GPT: Could not read primary gpt from mmc\n");
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700284 goto end;
Channagoud Kadabi39f13522013-07-31 14:46:22 -0700285 }
Ajay Dudanib01e5062011-12-03 23:23:42 -0800286 ret = partition_parse_gpt_header(data, &first_usable_lba,
287 &partition_entry_size, &header_size,
288 &max_partition_count);
289 if (ret) {
290 dprintf(INFO, "GPT: (WARNING) Primary signature invalid\n");
Kinson Chik4d7444f2011-09-13 15:48:51 -0700291
Ajay Dudanib01e5062011-12-03 23:23:42 -0800292 /* Check the backup gpt */
Neeti Desaice6ad9a2012-03-21 18:57:59 -0700293
294 /* Get size of MMC */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700295 card_size_sec = (device_density) / block_size;
Neeti Desaice6ad9a2012-03-21 18:57:59 -0700296 ASSERT (card_size_sec > 0);
297
298 backup_header_lba = card_size_sec - 1;
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700299 ret = mmc_read((backup_header_lba * block_size), (unsigned int *)data,
300 block_size);
Kinson Chik4d7444f2011-09-13 15:48:51 -0700301
Ajay Dudanib01e5062011-12-03 23:23:42 -0800302 if (ret) {
303 dprintf(CRITICAL,
304 "GPT: Could not read backup gpt from mmc\n");
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700305 goto end;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800306 }
Kinson Chik4d7444f2011-09-13 15:48:51 -0700307
Ajay Dudanib01e5062011-12-03 23:23:42 -0800308 ret = partition_parse_gpt_header(data, &first_usable_lba,
309 &partition_entry_size,
310 &header_size,
311 &max_partition_count);
312 if (ret) {
313 dprintf(CRITICAL,
314 "GPT: Primary and backup signatures invalid\n");
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700315 goto end;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800316 }
Ajay Dudanib01e5062011-12-03 23:23:42 -0800317 }
Neeti Desaice6ad9a2012-03-21 18:57:59 -0700318 partition_0 = GET_LLWORD_FROM_BYTE(&data[PARTITION_ENTRIES_OFFSET]);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800319 /* Read GPT Entries */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700320 for (i = 0; i < (ROUNDUP(max_partition_count, part_entry_cnt)) / part_entry_cnt; i++) {
neetid6c38de12011-12-02 12:04:50 -0800321 ASSERT(partition_count < NUM_PARTITIONS);
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700322 ret = mmc_read((partition_0 * block_size) + (i * block_size),
323 (uint32_t *) data, block_size);
Kinson Chik66552a82011-03-29 15:59:06 -0700324
Ajay Dudanib01e5062011-12-03 23:23:42 -0800325 if (ret) {
326 dprintf(CRITICAL,
327 "GPT: mmc read card failed reading partition entries.\n");
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700328 goto end;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800329 }
Kinson Chik66552a82011-03-29 15:59:06 -0700330
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700331 for (j = 0; j < part_entry_cnt; j++) {
Ajay Dudanib01e5062011-12-03 23:23:42 -0800332 memcpy(&(partition_entries[partition_count].type_guid),
333 &data[(j * partition_entry_size)],
334 PARTITION_TYPE_GUID_SIZE);
335 if (partition_entries[partition_count].type_guid[0] ==
336 0x00
337 && partition_entries[partition_count].
338 type_guid[1] == 0x00) {
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700339 i = ROUNDUP(max_partition_count, part_entry_cnt);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800340 break;
341 }
342 memcpy(&
343 (partition_entries[partition_count].
344 unique_partition_guid),
345 &data[(j * partition_entry_size) +
346 UNIQUE_GUID_OFFSET],
347 UNIQUE_PARTITION_GUID_SIZE);
348 partition_entries[partition_count].first_lba =
349 GET_LLWORD_FROM_BYTE(&data
350 [(j * partition_entry_size) +
351 FIRST_LBA_OFFSET]);
352 partition_entries[partition_count].last_lba =
353 GET_LLWORD_FROM_BYTE(&data
354 [(j * partition_entry_size) +
355 LAST_LBA_OFFSET]);
356 partition_entries[partition_count].size =
357 partition_entries[partition_count].last_lba -
Neeti Desai4b8c1df2012-03-21 13:15:14 -0700358 partition_entries[partition_count].first_lba + 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800359 partition_entries[partition_count].attribute_flag =
360 GET_LLWORD_FROM_BYTE(&data
361 [(j * partition_entry_size) +
362 ATTRIBUTE_FLAG_OFFSET]);
Kinson Chik66552a82011-03-29 15:59:06 -0700363
Ajay Dudanib01e5062011-12-03 23:23:42 -0800364 memset(&UTF16_name, 0x00, MAX_GPT_NAME_SIZE);
365 memcpy(UTF16_name, &data[(j * partition_entry_size) +
366 PARTITION_NAME_OFFSET],
367 MAX_GPT_NAME_SIZE);
Sundarajan Srinivasan554863f2013-12-04 17:14:37 -0800368 partition_entries[partition_count].lun = mmc_get_lun();
369
Ajay Dudanib01e5062011-12-03 23:23:42 -0800370 /*
371 * Currently partition names in *.xml are UTF-8 and lowercase
372 * Only supporting english for now so removing 2nd byte of UTF-16
373 */
374 for (n = 0; n < MAX_GPT_NAME_SIZE / 2; n++) {
375 partition_entries[partition_count].name[n] =
376 UTF16_name[n * 2];
377 }
378 partition_count++;
379 }
380 }
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700381end:
382 if (data)
383 free(data);
384
Ajay Dudanib01e5062011-12-03 23:23:42 -0800385 return ret;
Kinson Chik66552a82011-03-29 15:59:06 -0700386}
387
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700388static unsigned int write_mbr_in_blocks(uint32_t size, uint8_t *mbrImage, uint32_t block_size)
Neeti Desai5f26aff2011-09-30 10:27:40 -0700389{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800390 unsigned int dtype;
391 unsigned int dfirstsec;
392 unsigned int ebrSectorOffset;
393 unsigned char *ebrImage;
394 unsigned char *lastAddress;
395 int idx, i;
396 unsigned int ret;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700397
Ajay Dudanib01e5062011-12-03 23:23:42 -0800398 /* Write the first block */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700399 ret = mmc_write(0, block_size, (unsigned int *)mbrImage);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800400 if (ret) {
401 dprintf(CRITICAL, "Failed to write mbr partition\n");
402 goto end;
403 }
404 dprintf(SPEW, "write of first MBR block ok\n");
405 /*
406 Loop through the MBR table to see if there is an EBR.
407 If found, then figure out where to write the first EBR
408 */
409 idx = TABLE_ENTRY_0;
410 for (i = 0; i < 4; i++) {
411 dtype = mbrImage[idx + i * TABLE_ENTRY_SIZE + OFFSET_TYPE];
412 if (MBR_EBR_TYPE == dtype) {
413 dprintf(SPEW, "EBR found.\n");
414 break;
415 }
416 }
417 if (MBR_EBR_TYPE != dtype) {
418 dprintf(SPEW, "No EBR in this image\n");
419 goto end;
420 }
421 /* EBR exists. Write each EBR block to mmc */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700422 ebrImage = mbrImage + block_size;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800423 ebrSectorOffset =
424 GET_LWORD_FROM_BYTE(&mbrImage
425 [idx + i * TABLE_ENTRY_SIZE +
426 OFFSET_FIRST_SEC]);
427 dfirstsec = 0;
428 dprintf(SPEW, "first EBR to be written at sector 0x%X\n", dfirstsec);
429 lastAddress = mbrImage + size;
430 while (ebrImage < lastAddress) {
431 dprintf(SPEW, "writing to 0x%X\n",
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700432 (ebrSectorOffset + dfirstsec) * block_size);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800433 ret =
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700434 mmc_write((ebrSectorOffset + dfirstsec) * block_size,
435 block_size, (unsigned int *)ebrImage);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800436 if (ret) {
437 dprintf(CRITICAL,
438 "Failed to write EBR block to sector 0x%X\n",
439 dfirstsec);
440 goto end;
441 }
442 dfirstsec =
443 GET_LWORD_FROM_BYTE(&ebrImage
444 [TABLE_ENTRY_1 + OFFSET_FIRST_SEC]);
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700445 ebrImage += block_size;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800446 }
447 dprintf(INFO, "MBR written to mmc successfully\n");
448 end:
449 return ret;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700450}
451
452/* Write the MBR/EBR to the MMC. */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700453static unsigned int write_mbr(uint32_t size, uint8_t *mbrImage, uint32_t block_size)
Neeti Desai5f26aff2011-09-30 10:27:40 -0700454{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800455 unsigned int ret;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700456
Ajay Dudanib01e5062011-12-03 23:23:42 -0800457 /* Verify that passed in block is a valid MBR */
458 ret = partition_verify_mbr_signature(size, mbrImage);
459 if (ret) {
460 goto end;
461 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700462
Ajay Dudanib01e5062011-12-03 23:23:42 -0800463 /* Write the MBR/EBR to mmc */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700464 ret = write_mbr_in_blocks(size, mbrImage, block_size);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800465 if (ret) {
466 dprintf(CRITICAL, "Failed to write MBR block to mmc.\n");
467 goto end;
468 }
469 /* Re-read the MBR partition into mbr table */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700470 ret = mmc_boot_read_mbr(block_size);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800471 if (ret) {
472 dprintf(CRITICAL, "Failed to re-read mbr partition.\n");
473 goto end;
474 }
475 partition_dump();
476 end:
477 return ret;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700478}
479
480/*
481 * A8h reflected is 15h, i.e. 10101000 <--> 00010101
482*/
483int reflect(int data, int len)
484{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800485 int ref = 0;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700486
Ajay Dudanib01e5062011-12-03 23:23:42 -0800487 for (int i = 0; i < len; i++) {
488 if (data & 0x1) {
489 ref |= (1 << ((len - 1) - i));
490 }
491 data = (data >> 1);
492 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700493
Ajay Dudanib01e5062011-12-03 23:23:42 -0800494 return ref;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700495}
496
497/*
498* Function to calculate the CRC32
499*/
500unsigned int calculate_crc32(unsigned char *buffer, int len)
501{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800502 int byte_length = 8; /*length of unit (i.e. byte) */
503 int msb = 0;
Channagoud Kadabi39f13522013-07-31 14:46:22 -0700504 int polynomial = 0x04C11DB7; /* IEEE 32bit polynomial */
Ajay Dudanib01e5062011-12-03 23:23:42 -0800505 unsigned int regs = 0xFFFFFFFF; /* init to all ones */
506 int regs_mask = 0xFFFFFFFF; /* ensure only 32 bit answer */
507 int regs_msb = 0;
508 unsigned int reflected_regs;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700509
Ajay Dudanib01e5062011-12-03 23:23:42 -0800510 for (int i = 0; i < len; i++) {
511 int data_byte = buffer[i];
512 data_byte = reflect(data_byte, 8);
513 for (int j = 0; j < byte_length; j++) {
514 msb = data_byte >> (byte_length - 1); /* get MSB */
515 msb &= 1; /* ensure just 1 bit */
516 regs_msb = (regs >> 31) & 1; /* MSB of regs */
517 regs = regs << 1; /* shift regs for CRC-CCITT */
518 if (regs_msb ^ msb) { /* MSB is a 1 */
519 regs = regs ^ polynomial; /* XOR with generator poly */
520 }
521 regs = regs & regs_mask; /* Mask off excess upper bits */
522 data_byte <<= 1; /* get to next bit */
523 }
524 }
525 regs = regs & regs_mask;
526 reflected_regs = reflect(regs, 32) ^ 0xFFFFFFFF;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700527
Ajay Dudanib01e5062011-12-03 23:23:42 -0800528 return reflected_regs;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700529}
530
531/*
532 * Write the GPT Partition Entry Array to the MMC.
533 */
Ajay Dudanib01e5062011-12-03 23:23:42 -0800534static unsigned int
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700535write_gpt_partition_array(uint8_t *header,
536 uint32_t partition_array_start,
537 uint32_t array_size,
538 uint32_t block_size)
Neeti Desai5f26aff2011-09-30 10:27:40 -0700539{
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700540 unsigned int ret = 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800541 unsigned long long partition_entry_lba;
542 unsigned long long partition_entry_array_start_location;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700543
Ajay Dudanib01e5062011-12-03 23:23:42 -0800544 partition_entry_lba =
545 GET_LLWORD_FROM_BYTE(&header[PARTITION_ENTRIES_OFFSET]);
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700546 partition_entry_array_start_location = partition_entry_lba * block_size;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700547
Ajay Dudanib01e5062011-12-03 23:23:42 -0800548 ret = mmc_write(partition_entry_array_start_location, array_size,
549 (unsigned int *)partition_array_start);
550 if (ret) {
551 dprintf(CRITICAL,
552 "GPT: FAILED to write the partition entry array\n");
553 goto end;
554 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700555
Ajay Dudanib01e5062011-12-03 23:23:42 -0800556 end:
557 return ret;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700558}
559
Ajay Dudanib01e5062011-12-03 23:23:42 -0800560static void
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700561patch_gpt(uint8_t *gptImage, uint64_t density, uint32_t array_size,
562 uint32_t max_part_count, uint32_t part_entry_size, uint32_t block_size)
Neeti Desai5f26aff2011-09-30 10:27:40 -0700563{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800564 unsigned int partition_entry_array_start;
565 unsigned char *primary_gpt_header;
566 unsigned char *secondary_gpt_header;
567 unsigned int offset;
568 unsigned long long card_size_sec;
569 int total_part = 0;
570 unsigned int last_part_offset;
571 unsigned int crc_value;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700572
Ajay Dudanib01e5062011-12-03 23:23:42 -0800573 /* Get size of MMC */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700574 card_size_sec = (density) / block_size;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800575 /* Working around cap at 4GB */
576 if (card_size_sec == 0) {
577 card_size_sec = 4 * 1024 * 1024 * 2 - 1;
578 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700579
Ajay Dudanib01e5062011-12-03 23:23:42 -0800580 /* Patching primary header */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700581 primary_gpt_header = (gptImage + block_size);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800582 PUT_LONG_LONG(primary_gpt_header + BACKUP_HEADER_OFFSET,
583 ((long long)(card_size_sec - 1)));
584 PUT_LONG_LONG(primary_gpt_header + LAST_USABLE_LBA_OFFSET,
585 ((long long)(card_size_sec - 34)));
Neeti Desai5f26aff2011-09-30 10:27:40 -0700586
Ajay Dudanib01e5062011-12-03 23:23:42 -0800587 /* Patching backup GPT */
588 offset = (2 * array_size);
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700589 secondary_gpt_header = offset + block_size + primary_gpt_header;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800590 PUT_LONG_LONG(secondary_gpt_header + PRIMARY_HEADER_OFFSET,
591 ((long long)(card_size_sec - 1)));
592 PUT_LONG_LONG(secondary_gpt_header + LAST_USABLE_LBA_OFFSET,
593 ((long long)(card_size_sec - 34)));
594 PUT_LONG_LONG(secondary_gpt_header + PARTITION_ENTRIES_OFFSET,
595 ((long long)(card_size_sec - 33)));
Neeti Desai5f26aff2011-09-30 10:27:40 -0700596
Ajay Dudanib01e5062011-12-03 23:23:42 -0800597 /* Find last partition */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700598 while (*(primary_gpt_header + block_size + total_part * ENTRY_SIZE) !=
Ajay Dudanib01e5062011-12-03 23:23:42 -0800599 0) {
600 total_part++;
601 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700602
Ajay Dudanib01e5062011-12-03 23:23:42 -0800603 /* Patching last partition */
604 last_part_offset =
605 (total_part - 1) * ENTRY_SIZE + PARTITION_ENTRY_LAST_LBA;
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700606 PUT_LONG_LONG(primary_gpt_header + block_size + last_part_offset,
Ajay Dudanib01e5062011-12-03 23:23:42 -0800607 (long long)(card_size_sec - 34));
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700608 PUT_LONG_LONG(primary_gpt_header + block_size + last_part_offset +
Ajay Dudanib01e5062011-12-03 23:23:42 -0800609 array_size, (long long)(card_size_sec - 34));
Neeti Desai5f26aff2011-09-30 10:27:40 -0700610
Ajay Dudanib01e5062011-12-03 23:23:42 -0800611 /* Updating CRC of the Partition entry array in both headers */
vijay kumar4f4405f2014-08-08 11:49:53 +0530612 partition_entry_array_start = (unsigned int)primary_gpt_header + block_size;
613 crc_value = calculate_crc32((unsigned char *)partition_entry_array_start,
Ajay Dudanib01e5062011-12-03 23:23:42 -0800614 max_part_count * part_entry_size);
615 PUT_LONG(primary_gpt_header + PARTITION_CRC_OFFSET, crc_value);
Neeti Desai5f26aff2011-09-30 10:27:40 -0700616
vijay kumar4f4405f2014-08-08 11:49:53 +0530617 crc_value = calculate_crc32((unsigned char *)partition_entry_array_start + array_size,
Ajay Dudanib01e5062011-12-03 23:23:42 -0800618 max_part_count * part_entry_size);
619 PUT_LONG(secondary_gpt_header + PARTITION_CRC_OFFSET, crc_value);
Neeti Desai5f26aff2011-09-30 10:27:40 -0700620
Ajay Dudanib01e5062011-12-03 23:23:42 -0800621 /* Clearing CRC fields to calculate */
622 PUT_LONG(primary_gpt_header + HEADER_CRC_OFFSET, 0);
623 crc_value = calculate_crc32(primary_gpt_header, 92);
624 PUT_LONG(primary_gpt_header + HEADER_CRC_OFFSET, crc_value);
Neeti Desai5f26aff2011-09-30 10:27:40 -0700625
Ajay Dudanib01e5062011-12-03 23:23:42 -0800626 PUT_LONG(secondary_gpt_header + HEADER_CRC_OFFSET, 0);
627 crc_value = (calculate_crc32(secondary_gpt_header, 92));
628 PUT_LONG(secondary_gpt_header + HEADER_CRC_OFFSET, crc_value);
Neeti Desai5f26aff2011-09-30 10:27:40 -0700629
630}
631
632/*
633 * Write the GPT to the MMC.
634 */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700635static unsigned int write_gpt(uint32_t size, uint8_t *gptImage, uint32_t block_size)
Neeti Desai5f26aff2011-09-30 10:27:40 -0700636{
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700637 unsigned int ret = 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800638 unsigned int header_size;
639 unsigned long long first_usable_lba;
640 unsigned long long backup_header_lba;
641 unsigned int max_partition_count = 0;
642 unsigned int partition_entry_size;
643 unsigned int partition_entry_array_start;
644 unsigned char *primary_gpt_header;
645 unsigned char *secondary_gpt_header;
646 unsigned int offset;
647 unsigned int partition_entry_array_size;
648 unsigned long long primary_header_location; /* address on the emmc card */
649 unsigned long long secondary_header_location; /* address on the emmc card */
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700650 uint64_t device_density;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700651
Ajay Dudanib01e5062011-12-03 23:23:42 -0800652 /* Verify that passed block has a valid GPT primary header */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700653 primary_gpt_header = (gptImage + block_size);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800654 ret = partition_parse_gpt_header(primary_gpt_header, &first_usable_lba,
655 &partition_entry_size, &header_size,
656 &max_partition_count);
657 if (ret) {
658 dprintf(CRITICAL,
659 "GPT: Primary signature invalid cannot write GPT\n");
660 goto end;
661 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700662
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700663 /* Get the density of the mmc device */
664
665 device_density = mmc_get_device_capacity();
666
Ajay Dudanib01e5062011-12-03 23:23:42 -0800667 /* Verify that passed block has a valid backup GPT HEADER */
668 partition_entry_array_size = partition_entry_size * max_partition_count;
669 if (partition_entry_array_size < MIN_PARTITION_ARRAY_SIZE) {
670 partition_entry_array_size = MIN_PARTITION_ARRAY_SIZE;
671 }
672 offset = (2 * partition_entry_array_size);
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700673 secondary_gpt_header = offset + block_size + primary_gpt_header;
vijay kumar24c9ed12015-04-28 13:05:04 +0530674 parse_secondary_gpt = 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800675 ret =
676 partition_parse_gpt_header(secondary_gpt_header, &first_usable_lba,
677 &partition_entry_size, &header_size,
678 &max_partition_count);
vijay kumar24c9ed12015-04-28 13:05:04 +0530679 parse_secondary_gpt = 0;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800680 if (ret) {
681 dprintf(CRITICAL,
682 "GPT: Backup signature invalid cannot write GPT\n");
683 goto end;
684 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700685
Ajay Dudanib01e5062011-12-03 23:23:42 -0800686 /* Patching the primary and the backup header of the GPT table */
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700687 patch_gpt(gptImage, device_density, partition_entry_array_size,
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700688 max_partition_count, partition_entry_size, block_size);
Neeti Desai5f26aff2011-09-30 10:27:40 -0700689
Ajay Dudanib01e5062011-12-03 23:23:42 -0800690 /* Erasing the eMMC card before writing */
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700691 ret = mmc_erase_card(0x00000000, device_density);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800692 if (ret) {
693 dprintf(CRITICAL, "Failed to erase the eMMC card\n");
694 goto end;
695 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700696
Ajay Dudanib01e5062011-12-03 23:23:42 -0800697 /* Writing protective MBR */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700698 ret = mmc_write(0, block_size, (unsigned int *)gptImage);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800699 if (ret) {
700 dprintf(CRITICAL, "Failed to write Protective MBR\n");
701 goto end;
702 }
703 /* Writing the primary GPT header */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700704 primary_header_location = block_size;
705 ret = mmc_write(primary_header_location, block_size,
Ajay Dudanib01e5062011-12-03 23:23:42 -0800706 (unsigned int *)primary_gpt_header);
707 if (ret) {
708 dprintf(CRITICAL, "Failed to write GPT header\n");
709 goto end;
710 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700711
Ajay Dudanib01e5062011-12-03 23:23:42 -0800712 /* Writing the backup GPT header */
713 backup_header_lba = GET_LLWORD_FROM_BYTE
714 (&primary_gpt_header[BACKUP_HEADER_OFFSET]);
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700715 secondary_header_location = backup_header_lba * block_size;
716 ret = mmc_write(secondary_header_location, block_size,
Ajay Dudanib01e5062011-12-03 23:23:42 -0800717 (unsigned int *)secondary_gpt_header);
718 if (ret) {
719 dprintf(CRITICAL, "Failed to write GPT backup header\n");
720 goto end;
721 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700722
Ajay Dudanib01e5062011-12-03 23:23:42 -0800723 /* Writing the partition entries array for the primary header */
vijay kumar4f4405f2014-08-08 11:49:53 +0530724 partition_entry_array_start = (unsigned int)primary_gpt_header + block_size;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800725 ret = write_gpt_partition_array(primary_gpt_header,
726 partition_entry_array_start,
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700727 partition_entry_array_size, block_size);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800728 if (ret) {
729 dprintf(CRITICAL,
730 "GPT: Could not write GPT Partition entries array\n");
731 goto end;
732 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700733
Ajay Dudanib01e5062011-12-03 23:23:42 -0800734 /*Writing the partition entries array for the backup header */
vijay kumar4f4405f2014-08-08 11:49:53 +0530735 partition_entry_array_start = (unsigned int)primary_gpt_header + block_size +
Ajay Dudanib01e5062011-12-03 23:23:42 -0800736 partition_entry_array_size;
737 ret = write_gpt_partition_array(secondary_gpt_header,
738 partition_entry_array_start,
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700739 partition_entry_array_size, block_size);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800740 if (ret) {
741 dprintf(CRITICAL,
742 "GPT: Could not write GPT Partition entries array\n");
743 goto end;
744 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700745
Ajay Dudanib01e5062011-12-03 23:23:42 -0800746 /* Re-read the GPT partition table */
747 dprintf(INFO, "Re-reading the GPT Partition Table\n");
Sundarajan Srinivasan554863f2013-12-04 17:14:37 -0800748 partition_count = 0;
vijay kumar24c9ed12015-04-28 13:05:04 +0530749 flashing_gpt = 0;
Sundarajan Srinivasan554863f2013-12-04 17:14:37 -0800750 mmc_read_partition_table(0);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800751 partition_dump();
752 dprintf(CRITICAL, "GPT: Partition Table written\n");
753 memset(primary_gpt_header, 0x00, size);
Neeti Desai5f26aff2011-09-30 10:27:40 -0700754
Ajay Dudanib01e5062011-12-03 23:23:42 -0800755 end:
756 return ret;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700757}
758
Ajay Dudanib01e5062011-12-03 23:23:42 -0800759unsigned int write_partition(unsigned size, unsigned char *partition)
Neeti Desai5f26aff2011-09-30 10:27:40 -0700760{
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700761 unsigned int ret = 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800762 unsigned int partition_type;
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700763 uint32_t block_size;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700764
Ajay Dudanib01e5062011-12-03 23:23:42 -0800765 if (partition == 0) {
766 dprintf(CRITICAL, "NULL partition\n");
767 goto end;
768 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700769
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700770 block_size = mmc_get_device_blocksize();
Ajay Dudanib01e5062011-12-03 23:23:42 -0800771 ret = partition_get_type(size, partition, &partition_type);
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700772 if (ret)
Ajay Dudanib01e5062011-12-03 23:23:42 -0800773 goto end;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700774
Ajay Dudanib01e5062011-12-03 23:23:42 -0800775 switch (partition_type) {
776 case PARTITION_TYPE_MBR:
777 dprintf(INFO, "Writing MBR partition\n");
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700778 ret = write_mbr(size, partition, block_size);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800779 break;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700780
Ajay Dudanib01e5062011-12-03 23:23:42 -0800781 case PARTITION_TYPE_GPT:
782 dprintf(INFO, "Writing GPT partition\n");
vijay kumar24c9ed12015-04-28 13:05:04 +0530783 flashing_gpt = 1;
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700784 ret = write_gpt(size, partition, block_size);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800785 dprintf(CRITICAL, "Re-Flash all the partitions\n");
786 break;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700787
Ajay Dudanib01e5062011-12-03 23:23:42 -0800788 default:
789 dprintf(CRITICAL, "Invalid partition\n");
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700790 ret = 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800791 goto end;
792 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700793
Ajay Dudanib01e5062011-12-03 23:23:42 -0800794 end:
795 return ret;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700796}
Ajay Dudanib01e5062011-12-03 23:23:42 -0800797
Kinson Chikf1a43512011-07-14 11:28:39 -0700798/*
799 * Fill name for android partition found.
800 */
Ajay Dudanib01e5062011-12-03 23:23:42 -0800801static void
802mbr_fill_name(struct partition_entry *partition_ent, unsigned int type)
Kinson Chikf1a43512011-07-14 11:28:39 -0700803{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800804 switch (type) {
805 memset(partition_ent->name, 0, MAX_GPT_NAME_SIZE);
806 case MBR_MODEM_TYPE:
807 case MBR_MODEM_TYPE2:
808 /* if already assigned last name available then return */
809 if (!strcmp((const char *)vfat_partitions[vfat_count], "NONE"))
810 return;
811 strlcpy((char *)partition_ent->name,
812 (const char *)vfat_partitions[vfat_count],
813 sizeof(partition_ent->name));
814 vfat_count++;
815 break;
816 case MBR_SBL1_TYPE:
817 memcpy(partition_ent->name, "sbl1", 4);
818 break;
819 case MBR_SBL2_TYPE:
820 memcpy(partition_ent->name, "sbl2", 4);
821 break;
822 case MBR_SBL3_TYPE:
823 memcpy(partition_ent->name, "sbl3", 4);
824 break;
825 case MBR_RPM_TYPE:
826 memcpy(partition_ent->name, "rpm", 3);
827 break;
828 case MBR_TZ_TYPE:
829 memcpy(partition_ent->name, "tz", 2);
830 break;
831 case MBR_ABOOT_TYPE:
Channagoud Kadabi16f50952012-04-02 16:24:07 +0530832#if PLATFORM_MSM7X27A
833 memcpy(partition_ent->name, "FOTA", 4);
834#else
Ajay Dudanib01e5062011-12-03 23:23:42 -0800835 memcpy(partition_ent->name, "aboot", 5);
Channagoud Kadabi16f50952012-04-02 16:24:07 +0530836#endif
Ajay Dudanib01e5062011-12-03 23:23:42 -0800837 break;
838 case MBR_BOOT_TYPE:
839 memcpy(partition_ent->name, "boot", 4);
840 break;
841 case MBR_MODEM_ST1_TYPE:
842 memcpy(partition_ent->name, "modem_st1", 9);
843 break;
844 case MBR_MODEM_ST2_TYPE:
845 memcpy(partition_ent->name, "modem_st2", 9);
846 break;
847 case MBR_EFS2_TYPE:
848 memcpy(partition_ent->name, "efs2", 4);
849 break;
850 case MBR_USERDATA_TYPE:
851 if (ext3_count == sizeof(ext3_partitions) / sizeof(char *))
852 return;
853 strlcpy((char *)partition_ent->name,
854 (const char *)ext3_partitions[ext3_count],
855 sizeof(partition_ent->name));
856 ext3_count++;
857 break;
858 case MBR_RECOVERY_TYPE:
859 memcpy(partition_ent->name, "recovery", 8);
860 break;
861 case MBR_MISC_TYPE:
862 memcpy(partition_ent->name, "misc", 4);
863 break;
Neeti Desai2d6b0e42012-03-23 15:48:57 -0700864 case MBR_SSD_TYPE:
865 memcpy(partition_ent->name, "ssd", 3);
866 break;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800867 };
Kinson Chikf1a43512011-07-14 11:28:39 -0700868}
869
870/*
871 * Find index of parition in array of partition entries
872 */
Pavel Nedev285ad922013-04-26 10:39:19 +0300873int partition_get_index(const char *name)
Kinson Chikf1a43512011-07-14 11:28:39 -0700874{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800875 unsigned int input_string_length = strlen(name);
876 unsigned n;
Kinson Chik66552a82011-03-29 15:59:06 -0700877
neetid6c38de12011-12-02 12:04:50 -0800878 if( partition_count >= NUM_PARTITIONS)
879 {
880 return INVALID_PTN;
881 }
Ajay Dudanib01e5062011-12-03 23:23:42 -0800882 for (n = 0; n < partition_count; n++) {
Channagoud Kadabif8c5bf72015-02-26 11:33:52 -0800883 if ((input_string_length == strlen((const char *)&partition_entries[n].name))
884 && !memcmp(name, &partition_entries[n].name, input_string_length)) {
Ajay Dudanib01e5062011-12-03 23:23:42 -0800885 return n;
886 }
887 }
888 return INVALID_PTN;
Kinson Chikf1a43512011-07-14 11:28:39 -0700889}
890
891/* Get size of the partition */
Ajay Dudanib01e5062011-12-03 23:23:42 -0800892unsigned long long partition_get_size(int index)
Kinson Chikf1a43512011-07-14 11:28:39 -0700893{
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700894 uint32_t block_size;
895
896 block_size = mmc_get_device_blocksize();
897
Ajay Dudanib01e5062011-12-03 23:23:42 -0800898 if (index == INVALID_PTN)
899 return 0;
900 else {
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700901 return partition_entries[index].size * block_size;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800902 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700903}
904
905/* Get offset of the partition */
Ajay Dudanib01e5062011-12-03 23:23:42 -0800906unsigned long long partition_get_offset(int index)
Kinson Chikf1a43512011-07-14 11:28:39 -0700907{
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700908 uint32_t block_size;
909
910 block_size = mmc_get_device_blocksize();
911
Ajay Dudanib01e5062011-12-03 23:23:42 -0800912 if (index == INVALID_PTN)
913 return 0;
914 else {
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700915 return partition_entries[index].first_lba * block_size;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800916 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700917}
918
Sundarajan Srinivasan9ec69632014-04-10 13:33:10 -0700919struct partition_info partition_get_info(const char *name)
920{
921 struct partition_info info = {0};
922
923 int index = INVALID_PTN;
924
925 if(!name)
926 {
927 dprintf(CRITICAL, "Invalid partition name passed\n");
928 goto out;
929 }
930
931 index = partition_get_index(name);
932
933 if (index != INVALID_PTN)
934 {
935 info.offset = partition_get_offset(index);
936 info.size = partition_get_size(index);
937 }
938 else
939 {
940 dprintf(CRITICAL, "Error unable to find partition : [%s]\n", name);
941 }
942out:
943 return info;
944}
945
Sundarajan Srinivasan554863f2013-12-04 17:14:37 -0800946uint8_t partition_get_lun(int index)
947{
948 return partition_entries[index].lun;
949}
950
Kinson Chikf1a43512011-07-14 11:28:39 -0700951/* Debug: Print all parsed partitions */
952void partition_dump()
953{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800954 unsigned i = 0;
955 for (i = 0; i < partition_count; i++) {
956 dprintf(SPEW,
957 "ptn[%d]:Name[%s] Size[%llu] Type[%u] First[%llu] Last[%llu]\n",
958 i, partition_entries[i].name, partition_entries[i].size,
959 partition_entries[i].dtype,
960 partition_entries[i].first_lba,
961 partition_entries[i].last_lba);
962 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700963}
964
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700965static unsigned int
Ajay Dudanib01e5062011-12-03 23:23:42 -0800966partition_verify_mbr_signature(unsigned size, unsigned char *buffer)
Kinson Chikf1a43512011-07-14 11:28:39 -0700967{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800968 /* Avoid checking past end of buffer */
969 if ((TABLE_SIGNATURE + 1) > size) {
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700970 return 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800971 }
972 /* Check to see if signature exists */
973 if ((buffer[TABLE_SIGNATURE] != MMC_MBR_SIGNATURE_BYTE_0) ||
974 (buffer[TABLE_SIGNATURE + 1] != MMC_MBR_SIGNATURE_BYTE_1)) {
975 dprintf(CRITICAL, "MBR signature does not match.\n");
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700976 return 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800977 }
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700978 return 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700979}
980
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700981static unsigned int
Ajay Dudanib01e5062011-12-03 23:23:42 -0800982mbr_partition_get_type(unsigned size, unsigned char *partition,
983 unsigned int *partition_type)
Kinson Chikf1a43512011-07-14 11:28:39 -0700984{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800985 unsigned int type_offset = TABLE_ENTRY_0 + OFFSET_TYPE;
Kinson Chikf1a43512011-07-14 11:28:39 -0700986
Channagoud Kadabi98b11072015-03-12 15:14:22 -0700987 if (size < (type_offset + sizeof (*partition_type))) {
Ajay Dudanib01e5062011-12-03 23:23:42 -0800988 goto end;
989 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700990
Ajay Dudanib01e5062011-12-03 23:23:42 -0800991 *partition_type = partition[type_offset];
992 end:
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700993 return 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700994}
995
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700996static unsigned int
Ajay Dudanib01e5062011-12-03 23:23:42 -0800997partition_get_type(unsigned size, unsigned char *partition,
998 unsigned int *partition_type)
Kinson Chikf1a43512011-07-14 11:28:39 -0700999{
Channagoud Kadabi0e332852013-04-19 12:55:53 -07001000 unsigned int ret = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001001
Ajay Dudanib01e5062011-12-03 23:23:42 -08001002 /*
1003 * If the block contains the MBR signature, then it's likely either
1004 * MBR or MBR with protective type (GPT). If the MBR signature is
1005 * not there, then it could be the GPT backup.
1006 */
Kinson Chikf1a43512011-07-14 11:28:39 -07001007
Ajay Dudanib01e5062011-12-03 23:23:42 -08001008 /* First check the MBR signature */
1009 ret = partition_verify_mbr_signature(size, partition);
Channagoud Kadabi0e332852013-04-19 12:55:53 -07001010 if (!ret) {
Ajay Dudanib01e5062011-12-03 23:23:42 -08001011 unsigned int mbr_partition_type = PARTITION_TYPE_MBR;
Kinson Chikf1a43512011-07-14 11:28:39 -07001012
Ajay Dudanib01e5062011-12-03 23:23:42 -08001013 /* MBR signature verified. This could be MBR, MBR + EBR, or GPT */
1014 ret =
1015 mbr_partition_get_type(size, partition,
1016 &mbr_partition_type);
Channagoud Kadabi0e332852013-04-19 12:55:53 -07001017 if (ret) {
Ajay Dudanib01e5062011-12-03 23:23:42 -08001018 dprintf(CRITICAL, "Cannot get TYPE of partition");
1019 } else if (MBR_PROTECTED_TYPE == mbr_partition_type) {
1020 *partition_type = PARTITION_TYPE_GPT;
1021 } else {
1022 *partition_type = PARTITION_TYPE_MBR;
1023 }
1024 } else {
1025 /*
1026 * This could be the GPT backup. Make that assumption for now.
1027 * Anybody who treats the block as GPT backup should check the
1028 * signature.
1029 */
1030 *partition_type = PARTITION_TYPE_GPT_BACKUP;
1031 }
1032 return ret;
Kinson Chik66552a82011-03-29 15:59:06 -07001033}
Kinson Chik4d7444f2011-09-13 15:48:51 -07001034
1035/*
1036 * Parse the gpt header and get the required header fields
1037 * Return 0 on valid signature
1038 */
Channagoud Kadabi0e332852013-04-19 12:55:53 -07001039static unsigned int
Ajay Dudanib01e5062011-12-03 23:23:42 -08001040partition_parse_gpt_header(unsigned char *buffer,
1041 unsigned long long *first_usable_lba,
1042 unsigned int *partition_entry_size,
1043 unsigned int *header_size,
1044 unsigned int *max_partition_count)
Kinson Chik4d7444f2011-09-13 15:48:51 -07001045{
vijay kumar24c9ed12015-04-28 13:05:04 +05301046 uint32_t crc_val_org = 0;
1047 uint32_t crc_val = 0;
1048 uint32_t ret = 0;
1049 uint32_t partitions_for_block = 0;
1050 uint32_t blocks_to_read = 0;
1051 unsigned char *new_buffer = NULL;
1052 unsigned long long last_usable_lba = 0;
1053 unsigned long long partition_0 = 0;
1054 unsigned long long current_lba = 0;
1055 uint32_t block_size = mmc_get_device_blocksize();
1056 /* Get the density of the mmc device */
1057 uint64_t device_density = mmc_get_device_capacity();
1058
Ajay Dudanib01e5062011-12-03 23:23:42 -08001059 /* Check GPT Signature */
1060 if (((uint32_t *) buffer)[0] != GPT_SIGNATURE_2 ||
1061 ((uint32_t *) buffer)[1] != GPT_SIGNATURE_1)
1062 return 1;
Kinson Chik4d7444f2011-09-13 15:48:51 -07001063
Ajay Dudanib01e5062011-12-03 23:23:42 -08001064 *header_size = GET_LWORD_FROM_BYTE(&buffer[HEADER_SIZE_OFFSET]);
vijay kumar24c9ed12015-04-28 13:05:04 +05301065 /*check for header size too small*/
1066 if (*header_size < GPT_HEADER_SIZE) {
1067 dprintf(CRITICAL,"GPT Header size is too small\n");
1068 return 1;
1069 }
1070 /*check for header size too large*/
1071 if (*header_size > block_size) {
1072 dprintf(CRITICAL,"GPT Header size is too large\n");
1073 return 1;
1074 }
1075
1076 crc_val_org = GET_LWORD_FROM_BYTE(&buffer[HEADER_CRC_OFFSET]);
1077 /*Write CRC to 0 before we calculate the crc of the GPT header*/
1078 crc_val = 0;
1079 PUT_LONG(&buffer[HEADER_CRC_OFFSET], crc_val);
1080
1081 crc_val = crc32(~0L,buffer, *header_size) ^ (~0L);
1082 if (crc_val != crc_val_org) {
1083 dprintf(CRITICAL,"Header crc mismatch crc_val = %u with crc_val_org = %u\n", crc_val,crc_val_org);
1084 return 1;
1085 }
1086 else
1087 PUT_LONG(&buffer[HEADER_CRC_OFFSET], crc_val);
1088
1089 current_lba =
1090 GET_LLWORD_FROM_BYTE(&buffer[PRIMARY_HEADER_OFFSET]);
Ajay Dudanib01e5062011-12-03 23:23:42 -08001091 *first_usable_lba =
1092 GET_LLWORD_FROM_BYTE(&buffer[FIRST_USABLE_LBA_OFFSET]);
1093 *max_partition_count =
1094 GET_LWORD_FROM_BYTE(&buffer[PARTITION_COUNT_OFFSET]);
1095 *partition_entry_size =
1096 GET_LWORD_FROM_BYTE(&buffer[PENTRY_SIZE_OFFSET]);
vijay kumar24c9ed12015-04-28 13:05:04 +05301097 last_usable_lba =
1098 GET_LLWORD_FROM_BYTE(&buffer[LAST_USABLE_LBA_OFFSET]);
Kinson Chik4d7444f2011-09-13 15:48:51 -07001099
vijay kumar24c9ed12015-04-28 13:05:04 +05301100 /*current lba and GPT lba should be same*/
1101 if (!parse_secondary_gpt) {
1102 if (current_lba != GPT_LBA) {
1103 dprintf(CRITICAL,"GPT first usable LBA mismatch\n");
1104 return 1;
1105 }
1106 }
1107 /*check for first lba should be with in the valid range*/
1108 if (*first_usable_lba > (device_density/block_size)) {
1109 dprintf(CRITICAL,"Invalid first_usable_lba\n");
1110 return 1;
1111 }
1112 /*check for last lba should be with in the valid range*/
1113 if (last_usable_lba > (device_density/block_size)) {
1114 dprintf(CRITICAL,"Invalid last_usable_lba\n");
1115 return 1;
1116 }
1117 /*check for partition entry size*/
1118 if (*partition_entry_size != PARTITION_ENTRY_SIZE) {
1119 dprintf(CRITICAL,"Invalid parition entry size\n");
1120 return 1;
1121 }
1122
1123 if ((*max_partition_count) > (MIN_PARTITION_ARRAY_SIZE /(*partition_entry_size))) {
1124 dprintf(CRITICAL, "Invalid maximum partition count\n");
1125 return 1;
1126 }
1127
1128 partitions_for_block = block_size / (*partition_entry_size);
1129
1130 blocks_to_read = (*max_partition_count)/ partitions_for_block;
1131 if ((*max_partition_count) % partitions_for_block) {
1132 blocks_to_read += 1;
1133 }
1134
1135 new_buffer = (uint8_t *)memalign(CACHE_LINE, ROUNDUP((blocks_to_read * block_size),CACHE_LINE));
1136
1137 if (!new_buffer)
1138 {
1139 dprintf(CRITICAL, "Failed to Allocate memory to read partition table\n");
1140 return 1;
1141 }
1142
1143 if (!flashing_gpt) {
1144 partition_0 = GET_LLWORD_FROM_BYTE(&buffer[PARTITION_ENTRIES_OFFSET]);
1145 /*start LBA should always be 2 in primary GPT*/
1146 if(partition_0 != 0x2) {
1147 dprintf(CRITICAL, "Starting LBA mismatch\n");
1148 goto fail;
1149
1150 }
1151 /*read the partition entries to new_buffer*/
1152 ret = mmc_read((partition_0) * (block_size), (unsigned int *)new_buffer, (blocks_to_read * block_size));
1153 if (ret)
1154 {
1155 dprintf(CRITICAL, "GPT: Could not read primary gpt from mmc\n");
1156 goto fail;
1157 }
1158 crc_val_org = GET_LWORD_FROM_BYTE(&buffer[PARTITION_CRC_OFFSET]);
1159
1160 crc_val = crc32(~0L,new_buffer, ((*max_partition_count) * (*partition_entry_size))) ^ (~0L);
1161 if (crc_val != crc_val_org) {
1162 dprintf(CRITICAL,"Partition entires crc mismatch crc_val= %u with crc_val_org= %u\n",crc_val,crc_val_org);
1163 ret = 1;
1164 }
1165 }
1166fail:
1167 free(new_buffer);
1168 return ret;
Kinson Chik4d7444f2011-09-13 15:48:51 -07001169}
Pavel Nedevb5ba62d2013-07-22 11:57:41 +03001170
1171bool partition_gpt_exists()
1172{
1173 return (gpt_partitions_exist != 0);
1174}
Channagoud Kadabifaf20f62014-10-21 22:22:37 -07001175
1176int partition_read_only(int index)
1177{
1178 return partition_entries[index].attribute_flag >> PART_ATT_READONLY_OFFSET;
1179}