blob: 65d58d4a5f08bbca3f35aca66a1cb5488925b6a8 [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 }
vijay kumared75ae42015-07-22 12:42:06 +0530307 parse_secondary_gpt = 1;
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 }
vijay kumared75ae42015-07-22 12:42:06 +0530317 parse_secondary_gpt = 0;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800318 }
Neeti Desaice6ad9a2012-03-21 18:57:59 -0700319 partition_0 = GET_LLWORD_FROM_BYTE(&data[PARTITION_ENTRIES_OFFSET]);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800320 /* Read GPT Entries */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700321 for (i = 0; i < (ROUNDUP(max_partition_count, part_entry_cnt)) / part_entry_cnt; i++) {
neetid6c38de12011-12-02 12:04:50 -0800322 ASSERT(partition_count < NUM_PARTITIONS);
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700323 ret = mmc_read((partition_0 * block_size) + (i * block_size),
324 (uint32_t *) data, block_size);
Kinson Chik66552a82011-03-29 15:59:06 -0700325
Ajay Dudanib01e5062011-12-03 23:23:42 -0800326 if (ret) {
327 dprintf(CRITICAL,
328 "GPT: mmc read card failed reading partition entries.\n");
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700329 goto end;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800330 }
Kinson Chik66552a82011-03-29 15:59:06 -0700331
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700332 for (j = 0; j < part_entry_cnt; j++) {
Ajay Dudanib01e5062011-12-03 23:23:42 -0800333 memcpy(&(partition_entries[partition_count].type_guid),
334 &data[(j * partition_entry_size)],
335 PARTITION_TYPE_GUID_SIZE);
336 if (partition_entries[partition_count].type_guid[0] ==
337 0x00
338 && partition_entries[partition_count].
339 type_guid[1] == 0x00) {
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700340 i = ROUNDUP(max_partition_count, part_entry_cnt);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800341 break;
342 }
343 memcpy(&
344 (partition_entries[partition_count].
345 unique_partition_guid),
346 &data[(j * partition_entry_size) +
347 UNIQUE_GUID_OFFSET],
348 UNIQUE_PARTITION_GUID_SIZE);
349 partition_entries[partition_count].first_lba =
350 GET_LLWORD_FROM_BYTE(&data
351 [(j * partition_entry_size) +
352 FIRST_LBA_OFFSET]);
353 partition_entries[partition_count].last_lba =
354 GET_LLWORD_FROM_BYTE(&data
355 [(j * partition_entry_size) +
356 LAST_LBA_OFFSET]);
357 partition_entries[partition_count].size =
358 partition_entries[partition_count].last_lba -
Neeti Desai4b8c1df2012-03-21 13:15:14 -0700359 partition_entries[partition_count].first_lba + 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800360 partition_entries[partition_count].attribute_flag =
361 GET_LLWORD_FROM_BYTE(&data
362 [(j * partition_entry_size) +
363 ATTRIBUTE_FLAG_OFFSET]);
Kinson Chik66552a82011-03-29 15:59:06 -0700364
Ajay Dudanib01e5062011-12-03 23:23:42 -0800365 memset(&UTF16_name, 0x00, MAX_GPT_NAME_SIZE);
366 memcpy(UTF16_name, &data[(j * partition_entry_size) +
367 PARTITION_NAME_OFFSET],
368 MAX_GPT_NAME_SIZE);
Sundarajan Srinivasan554863f2013-12-04 17:14:37 -0800369 partition_entries[partition_count].lun = mmc_get_lun();
370
Ajay Dudanib01e5062011-12-03 23:23:42 -0800371 /*
372 * Currently partition names in *.xml are UTF-8 and lowercase
373 * Only supporting english for now so removing 2nd byte of UTF-16
374 */
375 for (n = 0; n < MAX_GPT_NAME_SIZE / 2; n++) {
376 partition_entries[partition_count].name[n] =
377 UTF16_name[n * 2];
378 }
379 partition_count++;
380 }
381 }
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700382end:
383 if (data)
384 free(data);
385
Ajay Dudanib01e5062011-12-03 23:23:42 -0800386 return ret;
Kinson Chik66552a82011-03-29 15:59:06 -0700387}
388
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700389static unsigned int write_mbr_in_blocks(uint32_t size, uint8_t *mbrImage, uint32_t block_size)
Neeti Desai5f26aff2011-09-30 10:27:40 -0700390{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800391 unsigned int dtype;
392 unsigned int dfirstsec;
393 unsigned int ebrSectorOffset;
394 unsigned char *ebrImage;
395 unsigned char *lastAddress;
396 int idx, i;
397 unsigned int ret;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700398
Ajay Dudanib01e5062011-12-03 23:23:42 -0800399 /* Write the first block */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700400 ret = mmc_write(0, block_size, (unsigned int *)mbrImage);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800401 if (ret) {
402 dprintf(CRITICAL, "Failed to write mbr partition\n");
403 goto end;
404 }
405 dprintf(SPEW, "write of first MBR block ok\n");
406 /*
407 Loop through the MBR table to see if there is an EBR.
408 If found, then figure out where to write the first EBR
409 */
410 idx = TABLE_ENTRY_0;
411 for (i = 0; i < 4; i++) {
412 dtype = mbrImage[idx + i * TABLE_ENTRY_SIZE + OFFSET_TYPE];
413 if (MBR_EBR_TYPE == dtype) {
414 dprintf(SPEW, "EBR found.\n");
415 break;
416 }
417 }
418 if (MBR_EBR_TYPE != dtype) {
419 dprintf(SPEW, "No EBR in this image\n");
420 goto end;
421 }
422 /* EBR exists. Write each EBR block to mmc */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700423 ebrImage = mbrImage + block_size;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800424 ebrSectorOffset =
425 GET_LWORD_FROM_BYTE(&mbrImage
426 [idx + i * TABLE_ENTRY_SIZE +
427 OFFSET_FIRST_SEC]);
428 dfirstsec = 0;
429 dprintf(SPEW, "first EBR to be written at sector 0x%X\n", dfirstsec);
430 lastAddress = mbrImage + size;
431 while (ebrImage < lastAddress) {
432 dprintf(SPEW, "writing to 0x%X\n",
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700433 (ebrSectorOffset + dfirstsec) * block_size);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800434 ret =
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700435 mmc_write((ebrSectorOffset + dfirstsec) * block_size,
436 block_size, (unsigned int *)ebrImage);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800437 if (ret) {
438 dprintf(CRITICAL,
439 "Failed to write EBR block to sector 0x%X\n",
440 dfirstsec);
441 goto end;
442 }
443 dfirstsec =
444 GET_LWORD_FROM_BYTE(&ebrImage
445 [TABLE_ENTRY_1 + OFFSET_FIRST_SEC]);
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700446 ebrImage += block_size;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800447 }
448 dprintf(INFO, "MBR written to mmc successfully\n");
449 end:
450 return ret;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700451}
452
453/* Write the MBR/EBR to the MMC. */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700454static unsigned int write_mbr(uint32_t size, uint8_t *mbrImage, uint32_t block_size)
Neeti Desai5f26aff2011-09-30 10:27:40 -0700455{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800456 unsigned int ret;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700457
Ajay Dudanib01e5062011-12-03 23:23:42 -0800458 /* Verify that passed in block is a valid MBR */
459 ret = partition_verify_mbr_signature(size, mbrImage);
460 if (ret) {
461 goto end;
462 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700463
Ajay Dudanib01e5062011-12-03 23:23:42 -0800464 /* Write the MBR/EBR to mmc */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700465 ret = write_mbr_in_blocks(size, mbrImage, block_size);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800466 if (ret) {
467 dprintf(CRITICAL, "Failed to write MBR block to mmc.\n");
468 goto end;
469 }
470 /* Re-read the MBR partition into mbr table */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700471 ret = mmc_boot_read_mbr(block_size);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800472 if (ret) {
473 dprintf(CRITICAL, "Failed to re-read mbr partition.\n");
474 goto end;
475 }
476 partition_dump();
477 end:
478 return ret;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700479}
480
481/*
482 * A8h reflected is 15h, i.e. 10101000 <--> 00010101
483*/
484int reflect(int data, int len)
485{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800486 int ref = 0;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700487
Ajay Dudanib01e5062011-12-03 23:23:42 -0800488 for (int i = 0; i < len; i++) {
489 if (data & 0x1) {
490 ref |= (1 << ((len - 1) - i));
491 }
492 data = (data >> 1);
493 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700494
Ajay Dudanib01e5062011-12-03 23:23:42 -0800495 return ref;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700496}
497
498/*
499* Function to calculate the CRC32
500*/
501unsigned int calculate_crc32(unsigned char *buffer, int len)
502{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800503 int byte_length = 8; /*length of unit (i.e. byte) */
504 int msb = 0;
Channagoud Kadabi39f13522013-07-31 14:46:22 -0700505 int polynomial = 0x04C11DB7; /* IEEE 32bit polynomial */
Ajay Dudanib01e5062011-12-03 23:23:42 -0800506 unsigned int regs = 0xFFFFFFFF; /* init to all ones */
507 int regs_mask = 0xFFFFFFFF; /* ensure only 32 bit answer */
508 int regs_msb = 0;
509 unsigned int reflected_regs;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700510
Ajay Dudanib01e5062011-12-03 23:23:42 -0800511 for (int i = 0; i < len; i++) {
512 int data_byte = buffer[i];
513 data_byte = reflect(data_byte, 8);
514 for (int j = 0; j < byte_length; j++) {
515 msb = data_byte >> (byte_length - 1); /* get MSB */
516 msb &= 1; /* ensure just 1 bit */
517 regs_msb = (regs >> 31) & 1; /* MSB of regs */
518 regs = regs << 1; /* shift regs for CRC-CCITT */
519 if (regs_msb ^ msb) { /* MSB is a 1 */
520 regs = regs ^ polynomial; /* XOR with generator poly */
521 }
522 regs = regs & regs_mask; /* Mask off excess upper bits */
523 data_byte <<= 1; /* get to next bit */
524 }
525 }
526 regs = regs & regs_mask;
527 reflected_regs = reflect(regs, 32) ^ 0xFFFFFFFF;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700528
Ajay Dudanib01e5062011-12-03 23:23:42 -0800529 return reflected_regs;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700530}
531
532/*
533 * Write the GPT Partition Entry Array to the MMC.
534 */
Ajay Dudanib01e5062011-12-03 23:23:42 -0800535static unsigned int
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700536write_gpt_partition_array(uint8_t *header,
537 uint32_t partition_array_start,
538 uint32_t array_size,
539 uint32_t block_size)
Neeti Desai5f26aff2011-09-30 10:27:40 -0700540{
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700541 unsigned int ret = 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800542 unsigned long long partition_entry_lba;
543 unsigned long long partition_entry_array_start_location;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700544
Ajay Dudanib01e5062011-12-03 23:23:42 -0800545 partition_entry_lba =
546 GET_LLWORD_FROM_BYTE(&header[PARTITION_ENTRIES_OFFSET]);
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700547 partition_entry_array_start_location = partition_entry_lba * block_size;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700548
Ajay Dudanib01e5062011-12-03 23:23:42 -0800549 ret = mmc_write(partition_entry_array_start_location, array_size,
550 (unsigned int *)partition_array_start);
551 if (ret) {
552 dprintf(CRITICAL,
553 "GPT: FAILED to write the partition entry array\n");
554 goto end;
555 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700556
Ajay Dudanib01e5062011-12-03 23:23:42 -0800557 end:
558 return ret;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700559}
560
Ajay Dudanib01e5062011-12-03 23:23:42 -0800561static void
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700562patch_gpt(uint8_t *gptImage, uint64_t density, uint32_t array_size,
563 uint32_t max_part_count, uint32_t part_entry_size, uint32_t block_size)
Neeti Desai5f26aff2011-09-30 10:27:40 -0700564{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800565 unsigned int partition_entry_array_start;
566 unsigned char *primary_gpt_header;
567 unsigned char *secondary_gpt_header;
568 unsigned int offset;
569 unsigned long long card_size_sec;
570 int total_part = 0;
571 unsigned int last_part_offset;
572 unsigned int crc_value;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700573
Ajay Dudanib01e5062011-12-03 23:23:42 -0800574 /* Get size of MMC */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700575 card_size_sec = (density) / block_size;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800576 /* Working around cap at 4GB */
577 if (card_size_sec == 0) {
578 card_size_sec = 4 * 1024 * 1024 * 2 - 1;
579 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700580
Ajay Dudanib01e5062011-12-03 23:23:42 -0800581 /* Patching primary header */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700582 primary_gpt_header = (gptImage + block_size);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800583 PUT_LONG_LONG(primary_gpt_header + BACKUP_HEADER_OFFSET,
584 ((long long)(card_size_sec - 1)));
585 PUT_LONG_LONG(primary_gpt_header + LAST_USABLE_LBA_OFFSET,
586 ((long long)(card_size_sec - 34)));
Neeti Desai5f26aff2011-09-30 10:27:40 -0700587
Ajay Dudanib01e5062011-12-03 23:23:42 -0800588 /* Patching backup GPT */
589 offset = (2 * array_size);
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700590 secondary_gpt_header = offset + block_size + primary_gpt_header;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800591 PUT_LONG_LONG(secondary_gpt_header + PRIMARY_HEADER_OFFSET,
592 ((long long)(card_size_sec - 1)));
593 PUT_LONG_LONG(secondary_gpt_header + LAST_USABLE_LBA_OFFSET,
594 ((long long)(card_size_sec - 34)));
595 PUT_LONG_LONG(secondary_gpt_header + PARTITION_ENTRIES_OFFSET,
596 ((long long)(card_size_sec - 33)));
Neeti Desai5f26aff2011-09-30 10:27:40 -0700597
Ajay Dudanib01e5062011-12-03 23:23:42 -0800598 /* Find last partition */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700599 while (*(primary_gpt_header + block_size + total_part * ENTRY_SIZE) !=
Ajay Dudanib01e5062011-12-03 23:23:42 -0800600 0) {
601 total_part++;
602 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700603
Ajay Dudanib01e5062011-12-03 23:23:42 -0800604 /* Patching last partition */
605 last_part_offset =
606 (total_part - 1) * ENTRY_SIZE + PARTITION_ENTRY_LAST_LBA;
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700607 PUT_LONG_LONG(primary_gpt_header + block_size + last_part_offset,
Ajay Dudanib01e5062011-12-03 23:23:42 -0800608 (long long)(card_size_sec - 34));
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700609 PUT_LONG_LONG(primary_gpt_header + block_size + last_part_offset +
Ajay Dudanib01e5062011-12-03 23:23:42 -0800610 array_size, (long long)(card_size_sec - 34));
Neeti Desai5f26aff2011-09-30 10:27:40 -0700611
Ajay Dudanib01e5062011-12-03 23:23:42 -0800612 /* Updating CRC of the Partition entry array in both headers */
vijay kumar4f4405f2014-08-08 11:49:53 +0530613 partition_entry_array_start = (unsigned int)primary_gpt_header + block_size;
614 crc_value = calculate_crc32((unsigned char *)partition_entry_array_start,
Ajay Dudanib01e5062011-12-03 23:23:42 -0800615 max_part_count * part_entry_size);
616 PUT_LONG(primary_gpt_header + PARTITION_CRC_OFFSET, crc_value);
Neeti Desai5f26aff2011-09-30 10:27:40 -0700617
vijay kumar4f4405f2014-08-08 11:49:53 +0530618 crc_value = calculate_crc32((unsigned char *)partition_entry_array_start + array_size,
Ajay Dudanib01e5062011-12-03 23:23:42 -0800619 max_part_count * part_entry_size);
620 PUT_LONG(secondary_gpt_header + PARTITION_CRC_OFFSET, crc_value);
Neeti Desai5f26aff2011-09-30 10:27:40 -0700621
Ajay Dudanib01e5062011-12-03 23:23:42 -0800622 /* Clearing CRC fields to calculate */
623 PUT_LONG(primary_gpt_header + HEADER_CRC_OFFSET, 0);
624 crc_value = calculate_crc32(primary_gpt_header, 92);
625 PUT_LONG(primary_gpt_header + HEADER_CRC_OFFSET, crc_value);
Neeti Desai5f26aff2011-09-30 10:27:40 -0700626
Ajay Dudanib01e5062011-12-03 23:23:42 -0800627 PUT_LONG(secondary_gpt_header + HEADER_CRC_OFFSET, 0);
628 crc_value = (calculate_crc32(secondary_gpt_header, 92));
629 PUT_LONG(secondary_gpt_header + HEADER_CRC_OFFSET, crc_value);
Neeti Desai5f26aff2011-09-30 10:27:40 -0700630
631}
632
633/*
634 * Write the GPT to the MMC.
635 */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700636static unsigned int write_gpt(uint32_t size, uint8_t *gptImage, uint32_t block_size)
Neeti Desai5f26aff2011-09-30 10:27:40 -0700637{
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700638 unsigned int ret = 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800639 unsigned int header_size;
640 unsigned long long first_usable_lba;
641 unsigned long long backup_header_lba;
642 unsigned int max_partition_count = 0;
643 unsigned int partition_entry_size;
644 unsigned int partition_entry_array_start;
645 unsigned char *primary_gpt_header;
646 unsigned char *secondary_gpt_header;
647 unsigned int offset;
648 unsigned int partition_entry_array_size;
649 unsigned long long primary_header_location; /* address on the emmc card */
650 unsigned long long secondary_header_location; /* address on the emmc card */
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700651 uint64_t device_density;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700652
Ajay Dudanib01e5062011-12-03 23:23:42 -0800653 /* Verify that passed block has a valid GPT primary header */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700654 primary_gpt_header = (gptImage + block_size);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800655 ret = partition_parse_gpt_header(primary_gpt_header, &first_usable_lba,
656 &partition_entry_size, &header_size,
657 &max_partition_count);
658 if (ret) {
659 dprintf(CRITICAL,
660 "GPT: Primary signature invalid cannot write GPT\n");
661 goto end;
662 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700663
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700664 /* Get the density of the mmc device */
665
666 device_density = mmc_get_device_capacity();
667
Ajay Dudanib01e5062011-12-03 23:23:42 -0800668 /* Verify that passed block has a valid backup GPT HEADER */
669 partition_entry_array_size = partition_entry_size * max_partition_count;
670 if (partition_entry_array_size < MIN_PARTITION_ARRAY_SIZE) {
671 partition_entry_array_size = MIN_PARTITION_ARRAY_SIZE;
672 }
673 offset = (2 * partition_entry_array_size);
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700674 secondary_gpt_header = offset + block_size + primary_gpt_header;
vijay kumar24c9ed12015-04-28 13:05:04 +0530675 parse_secondary_gpt = 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800676 ret =
677 partition_parse_gpt_header(secondary_gpt_header, &first_usable_lba,
678 &partition_entry_size, &header_size,
679 &max_partition_count);
vijay kumar24c9ed12015-04-28 13:05:04 +0530680 parse_secondary_gpt = 0;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800681 if (ret) {
682 dprintf(CRITICAL,
683 "GPT: Backup signature invalid cannot write GPT\n");
684 goto end;
685 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700686
Ajay Dudanib01e5062011-12-03 23:23:42 -0800687 /* Patching the primary and the backup header of the GPT table */
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700688 patch_gpt(gptImage, device_density, partition_entry_array_size,
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700689 max_partition_count, partition_entry_size, block_size);
Neeti Desai5f26aff2011-09-30 10:27:40 -0700690
Ajay Dudanib01e5062011-12-03 23:23:42 -0800691 /* Erasing the eMMC card before writing */
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700692 ret = mmc_erase_card(0x00000000, device_density);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800693 if (ret) {
694 dprintf(CRITICAL, "Failed to erase the eMMC card\n");
695 goto end;
696 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700697
Ajay Dudanib01e5062011-12-03 23:23:42 -0800698 /* Writing protective MBR */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700699 ret = mmc_write(0, block_size, (unsigned int *)gptImage);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800700 if (ret) {
701 dprintf(CRITICAL, "Failed to write Protective MBR\n");
702 goto end;
703 }
704 /* Writing the primary GPT header */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700705 primary_header_location = block_size;
706 ret = mmc_write(primary_header_location, block_size,
Ajay Dudanib01e5062011-12-03 23:23:42 -0800707 (unsigned int *)primary_gpt_header);
708 if (ret) {
709 dprintf(CRITICAL, "Failed to write GPT header\n");
710 goto end;
711 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700712
Ajay Dudanib01e5062011-12-03 23:23:42 -0800713 /* Writing the backup GPT header */
714 backup_header_lba = GET_LLWORD_FROM_BYTE
715 (&primary_gpt_header[BACKUP_HEADER_OFFSET]);
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700716 secondary_header_location = backup_header_lba * block_size;
717 ret = mmc_write(secondary_header_location, block_size,
Ajay Dudanib01e5062011-12-03 23:23:42 -0800718 (unsigned int *)secondary_gpt_header);
719 if (ret) {
720 dprintf(CRITICAL, "Failed to write GPT backup header\n");
721 goto end;
722 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700723
Ajay Dudanib01e5062011-12-03 23:23:42 -0800724 /* Writing the partition entries array for the primary header */
vijay kumar4f4405f2014-08-08 11:49:53 +0530725 partition_entry_array_start = (unsigned int)primary_gpt_header + block_size;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800726 ret = write_gpt_partition_array(primary_gpt_header,
727 partition_entry_array_start,
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700728 partition_entry_array_size, block_size);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800729 if (ret) {
730 dprintf(CRITICAL,
731 "GPT: Could not write GPT Partition entries array\n");
732 goto end;
733 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700734
Ajay Dudanib01e5062011-12-03 23:23:42 -0800735 /*Writing the partition entries array for the backup header */
vijay kumar4f4405f2014-08-08 11:49:53 +0530736 partition_entry_array_start = (unsigned int)primary_gpt_header + block_size +
Ajay Dudanib01e5062011-12-03 23:23:42 -0800737 partition_entry_array_size;
738 ret = write_gpt_partition_array(secondary_gpt_header,
739 partition_entry_array_start,
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700740 partition_entry_array_size, block_size);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800741 if (ret) {
742 dprintf(CRITICAL,
743 "GPT: Could not write GPT Partition entries array\n");
744 goto end;
745 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700746
Ajay Dudanib01e5062011-12-03 23:23:42 -0800747 /* Re-read the GPT partition table */
748 dprintf(INFO, "Re-reading the GPT Partition Table\n");
Sundarajan Srinivasan554863f2013-12-04 17:14:37 -0800749 partition_count = 0;
vijay kumar24c9ed12015-04-28 13:05:04 +0530750 flashing_gpt = 0;
Sundarajan Srinivasan554863f2013-12-04 17:14:37 -0800751 mmc_read_partition_table(0);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800752 partition_dump();
753 dprintf(CRITICAL, "GPT: Partition Table written\n");
754 memset(primary_gpt_header, 0x00, size);
Neeti Desai5f26aff2011-09-30 10:27:40 -0700755
Ajay Dudanib01e5062011-12-03 23:23:42 -0800756 end:
757 return ret;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700758}
759
Ajay Dudanib01e5062011-12-03 23:23:42 -0800760unsigned int write_partition(unsigned size, unsigned char *partition)
Neeti Desai5f26aff2011-09-30 10:27:40 -0700761{
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700762 unsigned int ret = 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800763 unsigned int partition_type;
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700764 uint32_t block_size;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700765
Ajay Dudanib01e5062011-12-03 23:23:42 -0800766 if (partition == 0) {
767 dprintf(CRITICAL, "NULL partition\n");
768 goto end;
769 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700770
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700771 block_size = mmc_get_device_blocksize();
Ajay Dudanib01e5062011-12-03 23:23:42 -0800772 ret = partition_get_type(size, partition, &partition_type);
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700773 if (ret)
Ajay Dudanib01e5062011-12-03 23:23:42 -0800774 goto end;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700775
Ajay Dudanib01e5062011-12-03 23:23:42 -0800776 switch (partition_type) {
777 case PARTITION_TYPE_MBR:
778 dprintf(INFO, "Writing MBR partition\n");
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700779 ret = write_mbr(size, partition, block_size);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800780 break;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700781
Ajay Dudanib01e5062011-12-03 23:23:42 -0800782 case PARTITION_TYPE_GPT:
783 dprintf(INFO, "Writing GPT partition\n");
vijay kumar24c9ed12015-04-28 13:05:04 +0530784 flashing_gpt = 1;
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700785 ret = write_gpt(size, partition, block_size);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800786 dprintf(CRITICAL, "Re-Flash all the partitions\n");
787 break;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700788
Ajay Dudanib01e5062011-12-03 23:23:42 -0800789 default:
790 dprintf(CRITICAL, "Invalid partition\n");
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700791 ret = 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800792 goto end;
793 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700794
Ajay Dudanib01e5062011-12-03 23:23:42 -0800795 end:
796 return ret;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700797}
Ajay Dudanib01e5062011-12-03 23:23:42 -0800798
Kinson Chikf1a43512011-07-14 11:28:39 -0700799/*
800 * Fill name for android partition found.
801 */
Ajay Dudanib01e5062011-12-03 23:23:42 -0800802static void
803mbr_fill_name(struct partition_entry *partition_ent, unsigned int type)
Kinson Chikf1a43512011-07-14 11:28:39 -0700804{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800805 switch (type) {
806 memset(partition_ent->name, 0, MAX_GPT_NAME_SIZE);
807 case MBR_MODEM_TYPE:
808 case MBR_MODEM_TYPE2:
809 /* if already assigned last name available then return */
810 if (!strcmp((const char *)vfat_partitions[vfat_count], "NONE"))
811 return;
812 strlcpy((char *)partition_ent->name,
813 (const char *)vfat_partitions[vfat_count],
814 sizeof(partition_ent->name));
815 vfat_count++;
816 break;
817 case MBR_SBL1_TYPE:
818 memcpy(partition_ent->name, "sbl1", 4);
819 break;
820 case MBR_SBL2_TYPE:
821 memcpy(partition_ent->name, "sbl2", 4);
822 break;
823 case MBR_SBL3_TYPE:
824 memcpy(partition_ent->name, "sbl3", 4);
825 break;
826 case MBR_RPM_TYPE:
827 memcpy(partition_ent->name, "rpm", 3);
828 break;
829 case MBR_TZ_TYPE:
830 memcpy(partition_ent->name, "tz", 2);
831 break;
832 case MBR_ABOOT_TYPE:
Channagoud Kadabi16f50952012-04-02 16:24:07 +0530833#if PLATFORM_MSM7X27A
834 memcpy(partition_ent->name, "FOTA", 4);
835#else
Ajay Dudanib01e5062011-12-03 23:23:42 -0800836 memcpy(partition_ent->name, "aboot", 5);
Channagoud Kadabi16f50952012-04-02 16:24:07 +0530837#endif
Ajay Dudanib01e5062011-12-03 23:23:42 -0800838 break;
839 case MBR_BOOT_TYPE:
840 memcpy(partition_ent->name, "boot", 4);
841 break;
842 case MBR_MODEM_ST1_TYPE:
843 memcpy(partition_ent->name, "modem_st1", 9);
844 break;
845 case MBR_MODEM_ST2_TYPE:
846 memcpy(partition_ent->name, "modem_st2", 9);
847 break;
848 case MBR_EFS2_TYPE:
849 memcpy(partition_ent->name, "efs2", 4);
850 break;
851 case MBR_USERDATA_TYPE:
852 if (ext3_count == sizeof(ext3_partitions) / sizeof(char *))
853 return;
854 strlcpy((char *)partition_ent->name,
855 (const char *)ext3_partitions[ext3_count],
856 sizeof(partition_ent->name));
857 ext3_count++;
858 break;
859 case MBR_RECOVERY_TYPE:
860 memcpy(partition_ent->name, "recovery", 8);
861 break;
862 case MBR_MISC_TYPE:
863 memcpy(partition_ent->name, "misc", 4);
864 break;
Neeti Desai2d6b0e42012-03-23 15:48:57 -0700865 case MBR_SSD_TYPE:
866 memcpy(partition_ent->name, "ssd", 3);
867 break;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800868 };
Kinson Chikf1a43512011-07-14 11:28:39 -0700869}
870
871/*
872 * Find index of parition in array of partition entries
873 */
Pavel Nedev285ad922013-04-26 10:39:19 +0300874int partition_get_index(const char *name)
Kinson Chikf1a43512011-07-14 11:28:39 -0700875{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800876 unsigned int input_string_length = strlen(name);
877 unsigned n;
Kinson Chik66552a82011-03-29 15:59:06 -0700878
neetid6c38de12011-12-02 12:04:50 -0800879 if( partition_count >= NUM_PARTITIONS)
880 {
881 return INVALID_PTN;
882 }
Ajay Dudanib01e5062011-12-03 23:23:42 -0800883 for (n = 0; n < partition_count; n++) {
Channagoud Kadabif8c5bf72015-02-26 11:33:52 -0800884 if ((input_string_length == strlen((const char *)&partition_entries[n].name))
885 && !memcmp(name, &partition_entries[n].name, input_string_length)) {
Ajay Dudanib01e5062011-12-03 23:23:42 -0800886 return n;
887 }
888 }
889 return INVALID_PTN;
Kinson Chikf1a43512011-07-14 11:28:39 -0700890}
891
892/* Get size of the partition */
Ajay Dudanib01e5062011-12-03 23:23:42 -0800893unsigned long long partition_get_size(int index)
Kinson Chikf1a43512011-07-14 11:28:39 -0700894{
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700895 uint32_t block_size;
896
897 block_size = mmc_get_device_blocksize();
898
Ajay Dudanib01e5062011-12-03 23:23:42 -0800899 if (index == INVALID_PTN)
900 return 0;
901 else {
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700902 return partition_entries[index].size * block_size;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800903 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700904}
905
906/* Get offset of the partition */
Ajay Dudanib01e5062011-12-03 23:23:42 -0800907unsigned long long partition_get_offset(int index)
Kinson Chikf1a43512011-07-14 11:28:39 -0700908{
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700909 uint32_t block_size;
910
911 block_size = mmc_get_device_blocksize();
912
Ajay Dudanib01e5062011-12-03 23:23:42 -0800913 if (index == INVALID_PTN)
914 return 0;
915 else {
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700916 return partition_entries[index].first_lba * block_size;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800917 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700918}
919
Sundarajan Srinivasan9ec69632014-04-10 13:33:10 -0700920struct partition_info partition_get_info(const char *name)
921{
922 struct partition_info info = {0};
923
924 int index = INVALID_PTN;
925
926 if(!name)
927 {
928 dprintf(CRITICAL, "Invalid partition name passed\n");
929 goto out;
930 }
931
932 index = partition_get_index(name);
933
934 if (index != INVALID_PTN)
935 {
936 info.offset = partition_get_offset(index);
937 info.size = partition_get_size(index);
938 }
939 else
940 {
941 dprintf(CRITICAL, "Error unable to find partition : [%s]\n", name);
942 }
943out:
944 return info;
945}
946
Sundarajan Srinivasan554863f2013-12-04 17:14:37 -0800947uint8_t partition_get_lun(int index)
948{
949 return partition_entries[index].lun;
950}
951
Kinson Chikf1a43512011-07-14 11:28:39 -0700952/* Debug: Print all parsed partitions */
953void partition_dump()
954{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800955 unsigned i = 0;
956 for (i = 0; i < partition_count; i++) {
957 dprintf(SPEW,
958 "ptn[%d]:Name[%s] Size[%llu] Type[%u] First[%llu] Last[%llu]\n",
959 i, partition_entries[i].name, partition_entries[i].size,
960 partition_entries[i].dtype,
961 partition_entries[i].first_lba,
962 partition_entries[i].last_lba);
963 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700964}
965
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700966static unsigned int
Ajay Dudanib01e5062011-12-03 23:23:42 -0800967partition_verify_mbr_signature(unsigned size, unsigned char *buffer)
Kinson Chikf1a43512011-07-14 11:28:39 -0700968{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800969 /* Avoid checking past end of buffer */
970 if ((TABLE_SIGNATURE + 1) > size) {
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700971 return 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800972 }
973 /* Check to see if signature exists */
974 if ((buffer[TABLE_SIGNATURE] != MMC_MBR_SIGNATURE_BYTE_0) ||
975 (buffer[TABLE_SIGNATURE + 1] != MMC_MBR_SIGNATURE_BYTE_1)) {
976 dprintf(CRITICAL, "MBR signature does not match.\n");
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700977 return 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800978 }
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700979 return 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700980}
981
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700982static unsigned int
Ajay Dudanib01e5062011-12-03 23:23:42 -0800983mbr_partition_get_type(unsigned size, unsigned char *partition,
984 unsigned int *partition_type)
Kinson Chikf1a43512011-07-14 11:28:39 -0700985{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800986 unsigned int type_offset = TABLE_ENTRY_0 + OFFSET_TYPE;
Kinson Chikf1a43512011-07-14 11:28:39 -0700987
Channagoud Kadabi98b11072015-03-12 15:14:22 -0700988 if (size < (type_offset + sizeof (*partition_type))) {
Ajay Dudanib01e5062011-12-03 23:23:42 -0800989 goto end;
990 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700991
Ajay Dudanib01e5062011-12-03 23:23:42 -0800992 *partition_type = partition[type_offset];
993 end:
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700994 return 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700995}
996
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700997static unsigned int
Ajay Dudanib01e5062011-12-03 23:23:42 -0800998partition_get_type(unsigned size, unsigned char *partition,
999 unsigned int *partition_type)
Kinson Chikf1a43512011-07-14 11:28:39 -07001000{
Channagoud Kadabi0e332852013-04-19 12:55:53 -07001001 unsigned int ret = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001002
Ajay Dudanib01e5062011-12-03 23:23:42 -08001003 /*
1004 * If the block contains the MBR signature, then it's likely either
1005 * MBR or MBR with protective type (GPT). If the MBR signature is
1006 * not there, then it could be the GPT backup.
1007 */
Kinson Chikf1a43512011-07-14 11:28:39 -07001008
Ajay Dudanib01e5062011-12-03 23:23:42 -08001009 /* First check the MBR signature */
1010 ret = partition_verify_mbr_signature(size, partition);
Channagoud Kadabi0e332852013-04-19 12:55:53 -07001011 if (!ret) {
Ajay Dudanib01e5062011-12-03 23:23:42 -08001012 unsigned int mbr_partition_type = PARTITION_TYPE_MBR;
Kinson Chikf1a43512011-07-14 11:28:39 -07001013
Ajay Dudanib01e5062011-12-03 23:23:42 -08001014 /* MBR signature verified. This could be MBR, MBR + EBR, or GPT */
1015 ret =
1016 mbr_partition_get_type(size, partition,
1017 &mbr_partition_type);
Channagoud Kadabi0e332852013-04-19 12:55:53 -07001018 if (ret) {
Ajay Dudanib01e5062011-12-03 23:23:42 -08001019 dprintf(CRITICAL, "Cannot get TYPE of partition");
1020 } else if (MBR_PROTECTED_TYPE == mbr_partition_type) {
1021 *partition_type = PARTITION_TYPE_GPT;
1022 } else {
1023 *partition_type = PARTITION_TYPE_MBR;
1024 }
1025 } else {
1026 /*
1027 * This could be the GPT backup. Make that assumption for now.
1028 * Anybody who treats the block as GPT backup should check the
1029 * signature.
1030 */
1031 *partition_type = PARTITION_TYPE_GPT_BACKUP;
1032 }
1033 return ret;
Kinson Chik66552a82011-03-29 15:59:06 -07001034}
Kinson Chik4d7444f2011-09-13 15:48:51 -07001035
1036/*
1037 * Parse the gpt header and get the required header fields
1038 * Return 0 on valid signature
1039 */
Channagoud Kadabi0e332852013-04-19 12:55:53 -07001040static unsigned int
Ajay Dudanib01e5062011-12-03 23:23:42 -08001041partition_parse_gpt_header(unsigned char *buffer,
1042 unsigned long long *first_usable_lba,
1043 unsigned int *partition_entry_size,
1044 unsigned int *header_size,
1045 unsigned int *max_partition_count)
Kinson Chik4d7444f2011-09-13 15:48:51 -07001046{
vijay kumar24c9ed12015-04-28 13:05:04 +05301047 uint32_t crc_val_org = 0;
1048 uint32_t crc_val = 0;
1049 uint32_t ret = 0;
1050 uint32_t partitions_for_block = 0;
1051 uint32_t blocks_to_read = 0;
1052 unsigned char *new_buffer = NULL;
1053 unsigned long long last_usable_lba = 0;
1054 unsigned long long partition_0 = 0;
1055 unsigned long long current_lba = 0;
1056 uint32_t block_size = mmc_get_device_blocksize();
1057 /* Get the density of the mmc device */
1058 uint64_t device_density = mmc_get_device_capacity();
1059
Ajay Dudanib01e5062011-12-03 23:23:42 -08001060 /* Check GPT Signature */
1061 if (((uint32_t *) buffer)[0] != GPT_SIGNATURE_2 ||
1062 ((uint32_t *) buffer)[1] != GPT_SIGNATURE_1)
1063 return 1;
Kinson Chik4d7444f2011-09-13 15:48:51 -07001064
Ajay Dudanib01e5062011-12-03 23:23:42 -08001065 *header_size = GET_LWORD_FROM_BYTE(&buffer[HEADER_SIZE_OFFSET]);
vijay kumar24c9ed12015-04-28 13:05:04 +05301066 /*check for header size too small*/
1067 if (*header_size < GPT_HEADER_SIZE) {
1068 dprintf(CRITICAL,"GPT Header size is too small\n");
1069 return 1;
1070 }
1071 /*check for header size too large*/
1072 if (*header_size > block_size) {
1073 dprintf(CRITICAL,"GPT Header size is too large\n");
1074 return 1;
1075 }
1076
1077 crc_val_org = GET_LWORD_FROM_BYTE(&buffer[HEADER_CRC_OFFSET]);
1078 /*Write CRC to 0 before we calculate the crc of the GPT header*/
1079 crc_val = 0;
1080 PUT_LONG(&buffer[HEADER_CRC_OFFSET], crc_val);
1081
1082 crc_val = crc32(~0L,buffer, *header_size) ^ (~0L);
1083 if (crc_val != crc_val_org) {
1084 dprintf(CRITICAL,"Header crc mismatch crc_val = %u with crc_val_org = %u\n", crc_val,crc_val_org);
1085 return 1;
1086 }
1087 else
1088 PUT_LONG(&buffer[HEADER_CRC_OFFSET], crc_val);
1089
1090 current_lba =
1091 GET_LLWORD_FROM_BYTE(&buffer[PRIMARY_HEADER_OFFSET]);
Ajay Dudanib01e5062011-12-03 23:23:42 -08001092 *first_usable_lba =
1093 GET_LLWORD_FROM_BYTE(&buffer[FIRST_USABLE_LBA_OFFSET]);
1094 *max_partition_count =
1095 GET_LWORD_FROM_BYTE(&buffer[PARTITION_COUNT_OFFSET]);
1096 *partition_entry_size =
1097 GET_LWORD_FROM_BYTE(&buffer[PENTRY_SIZE_OFFSET]);
vijay kumar24c9ed12015-04-28 13:05:04 +05301098 last_usable_lba =
1099 GET_LLWORD_FROM_BYTE(&buffer[LAST_USABLE_LBA_OFFSET]);
Kinson Chik4d7444f2011-09-13 15:48:51 -07001100
vijay kumar24c9ed12015-04-28 13:05:04 +05301101 /*current lba and GPT lba should be same*/
1102 if (!parse_secondary_gpt) {
1103 if (current_lba != GPT_LBA) {
1104 dprintf(CRITICAL,"GPT first usable LBA mismatch\n");
1105 return 1;
1106 }
1107 }
1108 /*check for first lba should be with in the valid range*/
1109 if (*first_usable_lba > (device_density/block_size)) {
1110 dprintf(CRITICAL,"Invalid first_usable_lba\n");
1111 return 1;
1112 }
1113 /*check for last lba should be with in the valid range*/
1114 if (last_usable_lba > (device_density/block_size)) {
1115 dprintf(CRITICAL,"Invalid last_usable_lba\n");
1116 return 1;
1117 }
1118 /*check for partition entry size*/
1119 if (*partition_entry_size != PARTITION_ENTRY_SIZE) {
1120 dprintf(CRITICAL,"Invalid parition entry size\n");
1121 return 1;
1122 }
1123
1124 if ((*max_partition_count) > (MIN_PARTITION_ARRAY_SIZE /(*partition_entry_size))) {
1125 dprintf(CRITICAL, "Invalid maximum partition count\n");
1126 return 1;
1127 }
1128
1129 partitions_for_block = block_size / (*partition_entry_size);
1130
1131 blocks_to_read = (*max_partition_count)/ partitions_for_block;
1132 if ((*max_partition_count) % partitions_for_block) {
1133 blocks_to_read += 1;
1134 }
1135
1136 new_buffer = (uint8_t *)memalign(CACHE_LINE, ROUNDUP((blocks_to_read * block_size),CACHE_LINE));
1137
1138 if (!new_buffer)
1139 {
1140 dprintf(CRITICAL, "Failed to Allocate memory to read partition table\n");
1141 return 1;
1142 }
1143
1144 if (!flashing_gpt) {
1145 partition_0 = GET_LLWORD_FROM_BYTE(&buffer[PARTITION_ENTRIES_OFFSET]);
1146 /*start LBA should always be 2 in primary GPT*/
1147 if(partition_0 != 0x2) {
1148 dprintf(CRITICAL, "Starting LBA mismatch\n");
1149 goto fail;
1150
1151 }
1152 /*read the partition entries to new_buffer*/
1153 ret = mmc_read((partition_0) * (block_size), (unsigned int *)new_buffer, (blocks_to_read * block_size));
1154 if (ret)
1155 {
1156 dprintf(CRITICAL, "GPT: Could not read primary gpt from mmc\n");
1157 goto fail;
1158 }
1159 crc_val_org = GET_LWORD_FROM_BYTE(&buffer[PARTITION_CRC_OFFSET]);
1160
1161 crc_val = crc32(~0L,new_buffer, ((*max_partition_count) * (*partition_entry_size))) ^ (~0L);
1162 if (crc_val != crc_val_org) {
1163 dprintf(CRITICAL,"Partition entires crc mismatch crc_val= %u with crc_val_org= %u\n",crc_val,crc_val_org);
1164 ret = 1;
1165 }
1166 }
1167fail:
1168 free(new_buffer);
1169 return ret;
Kinson Chik4d7444f2011-09-13 15:48:51 -07001170}
Pavel Nedevb5ba62d2013-07-22 11:57:41 +03001171
1172bool partition_gpt_exists()
1173{
1174 return (gpt_partitions_exist != 0);
1175}
Channagoud Kadabifaf20f62014-10-21 22:22:37 -07001176
1177int partition_read_only(int index)
1178{
1179 return partition_entries[index].attribute_flag >> PART_ATT_READONLY_OFFSET;
1180}