blob: cb0388730efc9bb1f28ea764f3d9d7b5d54749aa [file] [log] [blame]
Mayank Grover07f7ca62018-05-30 16:33:33 +05301/* Copyright (c) 2011-2018, 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"
Mayank Grovere358f1f2017-05-30 20:05:14 +053034#include "ab_partition_parser.h"
35
vijay kumar24c9ed12015-04-28 13:05:04 +053036static bool flashing_gpt = 0;
37static bool parse_secondary_gpt = 0;
Sundarajan Srinivasan554863f2013-12-04 17:14:37 -080038__WEAK void mmc_set_lun(uint8_t lun)
39{
40}
41
42__WEAK uint8_t mmc_get_lun(void)
43{
44 return 0;
45}
46
47__WEAK void mmc_read_partition_table(uint8_t arg)
48{
49 if(partition_read_table())
50 {
51 dprintf(CRITICAL, "Error reading the partition table info\n");
52 ASSERT(0);
53 }
54}
55
Channagoud Kadabi96c629e2013-09-10 14:21:30 -070056static uint32_t mmc_boot_read_gpt(uint32_t block_size);
57static uint32_t mmc_boot_read_mbr(uint32_t block_size);
Channagoud Kadabi0e332852013-04-19 12:55:53 -070058static void mbr_fill_name(struct partition_entry *partition_ent,
59 uint32_t type);
60static uint32_t partition_verify_mbr_signature(uint32_t size,
61 uint8_t *buffer);
62static uint32_t mbr_partition_get_type(uint32_t size, uint8_t *partition,
63 uint32_t *partition_type);
64
65static uint32_t partition_get_type(uint32_t size, uint8_t *partition,
66 uint32_t *partition_type);
67static uint32_t partition_parse_gpt_header(uint8_t *buffer,
68 uint64_t *first_usable_lba,
69 uint32_t *partition_entry_size,
70 uint32_t *header_size,
71 uint32_t *max_partition_count);
72
Channagoud Kadabi96c629e2013-09-10 14:21:30 -070073static uint32_t write_mbr(uint32_t, uint8_t *mbrImage, uint32_t block_size);
74static uint32_t write_gpt(uint32_t size, uint8_t *gptImage, uint32_t block_size);
Channagoud Kadabi0e332852013-04-19 12:55:53 -070075
Ajay Dudanib01e5062011-12-03 23:23:42 -080076char *ext3_partitions[] =
77 { "system", "userdata", "persist", "cache", "tombstones" };
78char *vfat_partitions[] = { "modem", "mdm", "NONE" };
79
Kinson Chikf1a43512011-07-14 11:28:39 -070080unsigned int ext3_count = 0;
81unsigned int vfat_count = 0;
82
Channagoud Kadabie9cb1c22013-11-12 13:49:41 -080083struct partition_entry *partition_entries;
Pavel Nedevb5ba62d2013-07-22 11:57:41 +030084static unsigned gpt_partitions_exist = 0;
Sundarajan Srinivasan554863f2013-12-04 17:14:37 -080085static unsigned partition_count;
Mayank Grover07f7ca62018-05-30 16:33:33 +053086/* this is a pointer to ptn_entries_buffer */
87static unsigned char *new_buffer = NULL;
Kinson Chikf1a43512011-07-14 11:28:39 -070088
Mayank Grovere358f1f2017-05-30 20:05:14 +053089unsigned partition_get_partition_count()
90{
91 return partition_count;
92}
93
94struct partition_entry* partition_get_partition_entries()
95{
96 return partition_entries;
97}
98
Channagoud Kadabi0e332852013-04-19 12:55:53 -070099unsigned int partition_read_table()
Kinson Chikf1a43512011-07-14 11:28:39 -0700100{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800101 unsigned int ret;
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700102 uint32_t block_size;
103
104 block_size = mmc_get_device_blocksize();
Kinson Chikf1a43512011-07-14 11:28:39 -0700105
Channagoud Kadabie9cb1c22013-11-12 13:49:41 -0800106 /* Allocate partition entries array */
Sundarajan Srinivasan554863f2013-12-04 17:14:37 -0800107 if(!partition_entries)
108 {
109 partition_entries = (struct partition_entry *) calloc(NUM_PARTITIONS, sizeof(struct partition_entry));
110 ASSERT(partition_entries);
111 }
Channagoud Kadabie9cb1c22013-11-12 13:49:41 -0800112
Ajay Dudanib01e5062011-12-03 23:23:42 -0800113 /* Read MBR of the card */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700114 ret = mmc_boot_read_mbr(block_size);
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700115 if (ret) {
Ajay Dudanib01e5062011-12-03 23:23:42 -0800116 dprintf(CRITICAL, "MMC Boot: MBR read failed!\n");
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700117 return 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800118 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700119
Ajay Dudanib01e5062011-12-03 23:23:42 -0800120 /* Read GPT of the card if exist */
121 if (gpt_partitions_exist) {
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700122 ret = mmc_boot_read_gpt(block_size);
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700123 if (ret) {
Ajay Dudanib01e5062011-12-03 23:23:42 -0800124 dprintf(CRITICAL, "MMC Boot: GPT read failed!\n");
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700125 return 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800126 }
127 }
Mayank Grovere358f1f2017-05-30 20:05:14 +0530128
129 /* Scan of multislot support */
130 /* TODO: Move this to mmc_boot_read_gpt() */
131 partition_scan_for_multislot();
132
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700133 return 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700134}
135
136/*
137 * Read MBR from MMC card and fill partition table.
138 */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700139static unsigned int mmc_boot_read_mbr(uint32_t block_size)
Kinson Chikf1a43512011-07-14 11:28:39 -0700140{
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700141 uint8_t *buffer = NULL;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800142 unsigned int dtype;
143 unsigned int dfirstsec;
144 unsigned int EBR_first_sec;
145 unsigned int EBR_current_sec;
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700146 int ret = 0;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800147 int idx, i;
Kinson Chikf1a43512011-07-14 11:28:39 -0700148
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700149 buffer = (uint8_t *)memalign(CACHE_LINE, ROUNDUP(block_size, CACHE_LINE));
150
151 if (!buffer)
152 {
153 dprintf(CRITICAL, "Error allocating memory while reading partition table\n");
154 ret = -1;
155 goto end;
156 }
157
Ajay Dudanib01e5062011-12-03 23:23:42 -0800158 /* Print out the MBR first */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700159 ret = mmc_read(0, (unsigned int *)buffer, block_size);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800160 if (ret) {
161 dprintf(CRITICAL, "Could not read partition from mmc\n");
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700162 goto end;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800163 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700164
Ajay Dudanib01e5062011-12-03 23:23:42 -0800165 /* Check to see if signature exists */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700166 ret = partition_verify_mbr_signature(block_size, buffer);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800167 if (ret) {
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700168 goto end;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800169 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700170
Ajay Dudanib01e5062011-12-03 23:23:42 -0800171 /*
172 * Process each of the four partitions in the MBR by reading the table
173 * information into our mbr table.
174 */
Ajay Dudanib01e5062011-12-03 23:23:42 -0800175 idx = TABLE_ENTRY_0;
176 for (i = 0; i < 4; i++) {
177 /* Type 0xEE indicates end of MBR and GPT partitions exist */
178 dtype = buffer[idx + i * TABLE_ENTRY_SIZE + OFFSET_TYPE];
179 if (dtype == MBR_PROTECTED_TYPE) {
180 gpt_partitions_exist = 1;
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700181 goto end;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800182 }
183 partition_entries[partition_count].dtype = dtype;
184 partition_entries[partition_count].attribute_flag =
185 buffer[idx + i * TABLE_ENTRY_SIZE + OFFSET_STATUS];
186 partition_entries[partition_count].first_lba =
187 GET_LWORD_FROM_BYTE(&buffer[idx +
188 i * TABLE_ENTRY_SIZE +
189 OFFSET_FIRST_SEC]);
190 partition_entries[partition_count].size =
191 GET_LWORD_FROM_BYTE(&buffer[idx +
192 i * TABLE_ENTRY_SIZE +
193 OFFSET_SIZE]);
194 dfirstsec = partition_entries[partition_count].first_lba;
195 mbr_fill_name(&partition_entries[partition_count],
196 partition_entries[partition_count].dtype);
197 partition_count++;
198 if (partition_count == NUM_PARTITIONS)
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700199 goto end;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800200 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700201
Ajay Dudanib01e5062011-12-03 23:23:42 -0800202 /* See if the last partition is EBR, if not, parsing is done */
203 if (dtype != MBR_EBR_TYPE) {
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700204 goto end;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800205 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700206
Ajay Dudanib01e5062011-12-03 23:23:42 -0800207 EBR_first_sec = dfirstsec;
208 EBR_current_sec = dfirstsec;
Kinson Chikf1a43512011-07-14 11:28:39 -0700209
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700210 ret = mmc_read((EBR_first_sec * block_size), (unsigned int *)buffer, block_size);
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700211 if (ret)
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700212 goto end;
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700213
Ajay Dudanib01e5062011-12-03 23:23:42 -0800214 /* Loop to parse the EBR */
215 for (i = 0;; i++) {
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700216 ret = partition_verify_mbr_signature(block_size, buffer);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800217 if (ret) {
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700218 ret = 0;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800219 break;
220 }
221 partition_entries[partition_count].attribute_flag =
222 buffer[TABLE_ENTRY_0 + OFFSET_STATUS];
223 partition_entries[partition_count].dtype =
224 buffer[TABLE_ENTRY_0 + OFFSET_TYPE];
225 partition_entries[partition_count].first_lba =
226 GET_LWORD_FROM_BYTE(&buffer[TABLE_ENTRY_0 +
227 OFFSET_FIRST_SEC]) +
228 EBR_current_sec;
229 partition_entries[partition_count].size =
230 GET_LWORD_FROM_BYTE(&buffer[TABLE_ENTRY_0 + OFFSET_SIZE]);
231 mbr_fill_name(&(partition_entries[partition_count]),
232 partition_entries[partition_count].dtype);
233 partition_count++;
234 if (partition_count == NUM_PARTITIONS)
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700235 goto end;
Kinson Chikf1a43512011-07-14 11:28:39 -0700236
Ajay Dudanib01e5062011-12-03 23:23:42 -0800237 dfirstsec =
238 GET_LWORD_FROM_BYTE(&buffer
239 [TABLE_ENTRY_1 + OFFSET_FIRST_SEC]);
240 if (dfirstsec == 0) {
241 /* Getting to the end of the EBR tables */
242 break;
243 }
244 /* More EBR to follow - read in the next EBR sector */
245 dprintf(SPEW, "Reading EBR block from 0x%X\n", EBR_first_sec
246 + dfirstsec);
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700247 ret = mmc_read(((EBR_first_sec + dfirstsec) * block_size),(unsigned int *)buffer,
248 block_size);
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700249 if (ret)
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700250 goto end;
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700251
Ajay Dudanib01e5062011-12-03 23:23:42 -0800252 EBR_current_sec = EBR_first_sec + dfirstsec;
253 }
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700254end:
255 if (buffer)
256 free(buffer);
257
Ajay Dudanib01e5062011-12-03 23:23:42 -0800258 return ret;
Kinson Chikf1a43512011-07-14 11:28:39 -0700259}
Kinson Chik66552a82011-03-29 15:59:06 -0700260
261/*
262 * Read GPT from MMC and fill partition table
263 */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700264static unsigned int mmc_boot_read_gpt(uint32_t block_size)
Kinson Chikf1a43512011-07-14 11:28:39 -0700265{
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700266 int ret = 0;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800267 unsigned int header_size;
268 unsigned long long first_usable_lba;
269 unsigned long long backup_header_lba;
Neeti Desaice6ad9a2012-03-21 18:57:59 -0700270 unsigned long long card_size_sec;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800271 unsigned int max_partition_count = 0;
272 unsigned int partition_entry_size;
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700273 unsigned int i = 0; /* Counter for each block */
274 unsigned int j = 0; /* Counter for each entry in a block */
Ajay Dudanib01e5062011-12-03 23:23:42 -0800275 unsigned int n = 0; /* Counter for UTF-16 -> 8 conversion */
276 unsigned char UTF16_name[MAX_GPT_NAME_SIZE];
277 /* LBA of first partition -- 1 Block after Protected MBR + 1 for PT */
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700278 uint64_t device_density;
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700279 uint8_t *data = NULL;
Mayank Grover07f7ca62018-05-30 16:33:33 +0530280 uint8_t *data_org_ptr = NULL;
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700281 uint32_t part_entry_cnt = block_size / ENTRY_SIZE;
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700282
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700283 /* Get the density of the mmc device */
284
285 device_density = mmc_get_device_capacity();
286
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700287 data = (uint8_t *)memalign(CACHE_LINE, ROUNDUP(block_size, CACHE_LINE));
288 if (!data)
289 {
290 dprintf(CRITICAL, "Failed to Allocate memory to read partition table\n");
291 ret = -1;
292 goto end;
293 }
Mayank Grover07f7ca62018-05-30 16:33:33 +0530294 data_org_ptr = data;
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700295
Ajay Dudanib01e5062011-12-03 23:23:42 -0800296 /* Print out the GPT first */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700297 ret = mmc_read(block_size, (unsigned int *)data, block_size);
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700298 if (ret)
Channagoud Kadabi39f13522013-07-31 14:46:22 -0700299 {
Ajay Dudanib01e5062011-12-03 23:23:42 -0800300 dprintf(CRITICAL, "GPT: Could not read primary gpt from mmc\n");
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700301 goto end;
Channagoud Kadabi39f13522013-07-31 14:46:22 -0700302 }
Ajay Dudanib01e5062011-12-03 23:23:42 -0800303 ret = partition_parse_gpt_header(data, &first_usable_lba,
304 &partition_entry_size, &header_size,
305 &max_partition_count);
306 if (ret) {
307 dprintf(INFO, "GPT: (WARNING) Primary signature invalid\n");
Kinson Chik4d7444f2011-09-13 15:48:51 -0700308
Ajay Dudanib01e5062011-12-03 23:23:42 -0800309 /* Check the backup gpt */
Neeti Desaice6ad9a2012-03-21 18:57:59 -0700310
311 /* Get size of MMC */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700312 card_size_sec = (device_density) / block_size;
Neeti Desaice6ad9a2012-03-21 18:57:59 -0700313 ASSERT (card_size_sec > 0);
314
315 backup_header_lba = card_size_sec - 1;
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700316 ret = mmc_read((backup_header_lba * block_size), (unsigned int *)data,
317 block_size);
Kinson Chik4d7444f2011-09-13 15:48:51 -0700318
Ajay Dudanib01e5062011-12-03 23:23:42 -0800319 if (ret) {
320 dprintf(CRITICAL,
321 "GPT: Could not read backup gpt from mmc\n");
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700322 goto end;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800323 }
vijay kumared75ae42015-07-22 12:42:06 +0530324 parse_secondary_gpt = 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800325 ret = partition_parse_gpt_header(data, &first_usable_lba,
326 &partition_entry_size,
327 &header_size,
328 &max_partition_count);
329 if (ret) {
330 dprintf(CRITICAL,
331 "GPT: Primary and backup signatures invalid\n");
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700332 goto end;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800333 }
vijay kumared75ae42015-07-22 12:42:06 +0530334 parse_secondary_gpt = 0;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800335 }
Ajay Dudanib01e5062011-12-03 23:23:42 -0800336 /* Read GPT Entries */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700337 for (i = 0; i < (ROUNDUP(max_partition_count, part_entry_cnt)) / part_entry_cnt; i++) {
neetid6c38de12011-12-02 12:04:50 -0800338 ASSERT(partition_count < NUM_PARTITIONS);
Mayank Grover07f7ca62018-05-30 16:33:33 +0530339
340 data = (new_buffer + (i * block_size));
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700341 for (j = 0; j < part_entry_cnt; j++) {
Ajay Dudanib01e5062011-12-03 23:23:42 -0800342 memcpy(&(partition_entries[partition_count].type_guid),
343 &data[(j * partition_entry_size)],
344 PARTITION_TYPE_GUID_SIZE);
345 if (partition_entries[partition_count].type_guid[0] ==
346 0x00
347 && partition_entries[partition_count].
348 type_guid[1] == 0x00) {
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700349 i = ROUNDUP(max_partition_count, part_entry_cnt);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800350 break;
351 }
352 memcpy(&
353 (partition_entries[partition_count].
354 unique_partition_guid),
355 &data[(j * partition_entry_size) +
356 UNIQUE_GUID_OFFSET],
357 UNIQUE_PARTITION_GUID_SIZE);
358 partition_entries[partition_count].first_lba =
359 GET_LLWORD_FROM_BYTE(&data
360 [(j * partition_entry_size) +
361 FIRST_LBA_OFFSET]);
362 partition_entries[partition_count].last_lba =
363 GET_LLWORD_FROM_BYTE(&data
364 [(j * partition_entry_size) +
365 LAST_LBA_OFFSET]);
366 partition_entries[partition_count].size =
367 partition_entries[partition_count].last_lba -
Neeti Desai4b8c1df2012-03-21 13:15:14 -0700368 partition_entries[partition_count].first_lba + 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800369 partition_entries[partition_count].attribute_flag =
370 GET_LLWORD_FROM_BYTE(&data
371 [(j * partition_entry_size) +
372 ATTRIBUTE_FLAG_OFFSET]);
Kinson Chik66552a82011-03-29 15:59:06 -0700373
Ajay Dudanib01e5062011-12-03 23:23:42 -0800374 memset(&UTF16_name, 0x00, MAX_GPT_NAME_SIZE);
375 memcpy(UTF16_name, &data[(j * partition_entry_size) +
376 PARTITION_NAME_OFFSET],
377 MAX_GPT_NAME_SIZE);
Sundarajan Srinivasan554863f2013-12-04 17:14:37 -0800378 partition_entries[partition_count].lun = mmc_get_lun();
379
Ajay Dudanib01e5062011-12-03 23:23:42 -0800380 /*
381 * Currently partition names in *.xml are UTF-8 and lowercase
382 * Only supporting english for now so removing 2nd byte of UTF-16
383 */
384 for (n = 0; n < MAX_GPT_NAME_SIZE / 2; n++) {
385 partition_entries[partition_count].name[n] =
386 UTF16_name[n * 2];
387 }
388 partition_count++;
389 }
390 }
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700391end:
Mayank Grover07f7ca62018-05-30 16:33:33 +0530392 if (data_org_ptr)
393 free(data_org_ptr);
394 if (new_buffer)
395 free(new_buffer);
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700396
Ajay Dudanib01e5062011-12-03 23:23:42 -0800397 return ret;
Kinson Chik66552a82011-03-29 15:59:06 -0700398}
399
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700400static unsigned int write_mbr_in_blocks(uint32_t size, uint8_t *mbrImage, uint32_t block_size)
Neeti Desai5f26aff2011-09-30 10:27:40 -0700401{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800402 unsigned int dtype;
403 unsigned int dfirstsec;
404 unsigned int ebrSectorOffset;
405 unsigned char *ebrImage;
406 unsigned char *lastAddress;
407 int idx, i;
408 unsigned int ret;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700409
Ajay Dudanib01e5062011-12-03 23:23:42 -0800410 /* Write the first block */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700411 ret = mmc_write(0, block_size, (unsigned int *)mbrImage);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800412 if (ret) {
413 dprintf(CRITICAL, "Failed to write mbr partition\n");
414 goto end;
415 }
416 dprintf(SPEW, "write of first MBR block ok\n");
417 /*
418 Loop through the MBR table to see if there is an EBR.
419 If found, then figure out where to write the first EBR
420 */
421 idx = TABLE_ENTRY_0;
422 for (i = 0; i < 4; i++) {
423 dtype = mbrImage[idx + i * TABLE_ENTRY_SIZE + OFFSET_TYPE];
424 if (MBR_EBR_TYPE == dtype) {
425 dprintf(SPEW, "EBR found.\n");
426 break;
427 }
428 }
429 if (MBR_EBR_TYPE != dtype) {
430 dprintf(SPEW, "No EBR in this image\n");
431 goto end;
432 }
433 /* EBR exists. Write each EBR block to mmc */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700434 ebrImage = mbrImage + block_size;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800435 ebrSectorOffset =
436 GET_LWORD_FROM_BYTE(&mbrImage
437 [idx + i * TABLE_ENTRY_SIZE +
438 OFFSET_FIRST_SEC]);
439 dfirstsec = 0;
440 dprintf(SPEW, "first EBR to be written at sector 0x%X\n", dfirstsec);
441 lastAddress = mbrImage + size;
442 while (ebrImage < lastAddress) {
443 dprintf(SPEW, "writing to 0x%X\n",
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700444 (ebrSectorOffset + dfirstsec) * block_size);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800445 ret =
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700446 mmc_write((ebrSectorOffset + dfirstsec) * block_size,
447 block_size, (unsigned int *)ebrImage);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800448 if (ret) {
449 dprintf(CRITICAL,
450 "Failed to write EBR block to sector 0x%X\n",
451 dfirstsec);
452 goto end;
453 }
454 dfirstsec =
455 GET_LWORD_FROM_BYTE(&ebrImage
456 [TABLE_ENTRY_1 + OFFSET_FIRST_SEC]);
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700457 ebrImage += block_size;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800458 }
459 dprintf(INFO, "MBR written to mmc successfully\n");
460 end:
461 return ret;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700462}
463
464/* Write the MBR/EBR to the MMC. */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700465static unsigned int write_mbr(uint32_t size, uint8_t *mbrImage, uint32_t block_size)
Neeti Desai5f26aff2011-09-30 10:27:40 -0700466{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800467 unsigned int ret;
Vijay Kumar Pendoti17c38532016-05-04 20:27:52 +0530468 uint64_t device_density;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700469
Ajay Dudanib01e5062011-12-03 23:23:42 -0800470 /* Verify that passed in block is a valid MBR */
471 ret = partition_verify_mbr_signature(size, mbrImage);
472 if (ret) {
473 goto end;
474 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700475
Vijay Kumar Pendoti17c38532016-05-04 20:27:52 +0530476 device_density = mmc_get_device_capacity();
477
478 /* Erasing the eMMC card before writing */
479 ret = mmc_erase_card(0x00000000, device_density);
480 if (ret) {
481 dprintf(CRITICAL, "Failed to erase the eMMC card\n");
482 goto end;
483 }
484
Ajay Dudanib01e5062011-12-03 23:23:42 -0800485 /* Write the MBR/EBR to mmc */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700486 ret = write_mbr_in_blocks(size, mbrImage, block_size);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800487 if (ret) {
488 dprintf(CRITICAL, "Failed to write MBR block to mmc.\n");
489 goto end;
490 }
491 /* Re-read the MBR partition into mbr table */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700492 ret = mmc_boot_read_mbr(block_size);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800493 if (ret) {
494 dprintf(CRITICAL, "Failed to re-read mbr partition.\n");
495 goto end;
496 }
497 partition_dump();
498 end:
499 return ret;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700500}
501
502/*
503 * A8h reflected is 15h, i.e. 10101000 <--> 00010101
504*/
505int reflect(int data, int len)
506{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800507 int ref = 0;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700508
Ajay Dudanib01e5062011-12-03 23:23:42 -0800509 for (int i = 0; i < len; i++) {
510 if (data & 0x1) {
511 ref |= (1 << ((len - 1) - i));
512 }
513 data = (data >> 1);
514 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700515
Ajay Dudanib01e5062011-12-03 23:23:42 -0800516 return ref;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700517}
518
519/*
520* Function to calculate the CRC32
521*/
522unsigned int calculate_crc32(unsigned char *buffer, int len)
523{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800524 int byte_length = 8; /*length of unit (i.e. byte) */
525 int msb = 0;
Channagoud Kadabi39f13522013-07-31 14:46:22 -0700526 int polynomial = 0x04C11DB7; /* IEEE 32bit polynomial */
Ajay Dudanib01e5062011-12-03 23:23:42 -0800527 unsigned int regs = 0xFFFFFFFF; /* init to all ones */
528 int regs_mask = 0xFFFFFFFF; /* ensure only 32 bit answer */
529 int regs_msb = 0;
530 unsigned int reflected_regs;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700531
Ajay Dudanib01e5062011-12-03 23:23:42 -0800532 for (int i = 0; i < len; i++) {
533 int data_byte = buffer[i];
534 data_byte = reflect(data_byte, 8);
535 for (int j = 0; j < byte_length; j++) {
536 msb = data_byte >> (byte_length - 1); /* get MSB */
537 msb &= 1; /* ensure just 1 bit */
538 regs_msb = (regs >> 31) & 1; /* MSB of regs */
539 regs = regs << 1; /* shift regs for CRC-CCITT */
540 if (regs_msb ^ msb) { /* MSB is a 1 */
541 regs = regs ^ polynomial; /* XOR with generator poly */
542 }
543 regs = regs & regs_mask; /* Mask off excess upper bits */
544 data_byte <<= 1; /* get to next bit */
545 }
546 }
547 regs = regs & regs_mask;
548 reflected_regs = reflect(regs, 32) ^ 0xFFFFFFFF;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700549
Ajay Dudanib01e5062011-12-03 23:23:42 -0800550 return reflected_regs;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700551}
552
553/*
554 * Write the GPT Partition Entry Array to the MMC.
555 */
Ajay Dudanib01e5062011-12-03 23:23:42 -0800556static unsigned int
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700557write_gpt_partition_array(uint8_t *header,
558 uint32_t partition_array_start,
559 uint32_t array_size,
560 uint32_t block_size)
Neeti Desai5f26aff2011-09-30 10:27:40 -0700561{
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700562 unsigned int ret = 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800563 unsigned long long partition_entry_lba;
564 unsigned long long partition_entry_array_start_location;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700565
Ajay Dudanib01e5062011-12-03 23:23:42 -0800566 partition_entry_lba =
567 GET_LLWORD_FROM_BYTE(&header[PARTITION_ENTRIES_OFFSET]);
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700568 partition_entry_array_start_location = partition_entry_lba * block_size;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700569
Ajay Dudanib01e5062011-12-03 23:23:42 -0800570 ret = mmc_write(partition_entry_array_start_location, array_size,
571 (unsigned int *)partition_array_start);
572 if (ret) {
573 dprintf(CRITICAL,
574 "GPT: FAILED to write the partition entry array\n");
575 goto end;
576 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700577
Ajay Dudanib01e5062011-12-03 23:23:42 -0800578 end:
579 return ret;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700580}
581
Ajay Dudanib01e5062011-12-03 23:23:42 -0800582static void
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700583patch_gpt(uint8_t *gptImage, uint64_t density, uint32_t array_size,
584 uint32_t max_part_count, uint32_t part_entry_size, uint32_t block_size)
Neeti Desai5f26aff2011-09-30 10:27:40 -0700585{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800586 unsigned int partition_entry_array_start;
587 unsigned char *primary_gpt_header;
588 unsigned char *secondary_gpt_header;
589 unsigned int offset;
590 unsigned long long card_size_sec;
591 int total_part = 0;
592 unsigned int last_part_offset;
593 unsigned int crc_value;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700594
Ajay Dudanib01e5062011-12-03 23:23:42 -0800595 /* Get size of MMC */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700596 card_size_sec = (density) / block_size;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800597 /* Working around cap at 4GB */
598 if (card_size_sec == 0) {
599 card_size_sec = 4 * 1024 * 1024 * 2 - 1;
600 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700601
Ajay Dudanib01e5062011-12-03 23:23:42 -0800602 /* Patching primary header */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700603 primary_gpt_header = (gptImage + block_size);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800604 PUT_LONG_LONG(primary_gpt_header + BACKUP_HEADER_OFFSET,
605 ((long long)(card_size_sec - 1)));
606 PUT_LONG_LONG(primary_gpt_header + LAST_USABLE_LBA_OFFSET,
607 ((long long)(card_size_sec - 34)));
Neeti Desai5f26aff2011-09-30 10:27:40 -0700608
Ajay Dudanib01e5062011-12-03 23:23:42 -0800609 /* Patching backup GPT */
610 offset = (2 * array_size);
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700611 secondary_gpt_header = offset + block_size + primary_gpt_header;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800612 PUT_LONG_LONG(secondary_gpt_header + PRIMARY_HEADER_OFFSET,
613 ((long long)(card_size_sec - 1)));
614 PUT_LONG_LONG(secondary_gpt_header + LAST_USABLE_LBA_OFFSET,
615 ((long long)(card_size_sec - 34)));
616 PUT_LONG_LONG(secondary_gpt_header + PARTITION_ENTRIES_OFFSET,
617 ((long long)(card_size_sec - 33)));
Neeti Desai5f26aff2011-09-30 10:27:40 -0700618
Ajay Dudanib01e5062011-12-03 23:23:42 -0800619 /* Find last partition */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700620 while (*(primary_gpt_header + block_size + total_part * ENTRY_SIZE) !=
Ajay Dudanib01e5062011-12-03 23:23:42 -0800621 0) {
622 total_part++;
623 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700624
Ajay Dudanib01e5062011-12-03 23:23:42 -0800625 /* Patching last partition */
626 last_part_offset =
627 (total_part - 1) * ENTRY_SIZE + PARTITION_ENTRY_LAST_LBA;
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700628 PUT_LONG_LONG(primary_gpt_header + block_size + last_part_offset,
Ajay Dudanib01e5062011-12-03 23:23:42 -0800629 (long long)(card_size_sec - 34));
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700630 PUT_LONG_LONG(primary_gpt_header + block_size + last_part_offset +
Ajay Dudanib01e5062011-12-03 23:23:42 -0800631 array_size, (long long)(card_size_sec - 34));
Neeti Desai5f26aff2011-09-30 10:27:40 -0700632
Ajay Dudanib01e5062011-12-03 23:23:42 -0800633 /* Updating CRC of the Partition entry array in both headers */
vijay kumar4f4405f2014-08-08 11:49:53 +0530634 partition_entry_array_start = (unsigned int)primary_gpt_header + block_size;
635 crc_value = calculate_crc32((unsigned char *)partition_entry_array_start,
Ajay Dudanib01e5062011-12-03 23:23:42 -0800636 max_part_count * part_entry_size);
637 PUT_LONG(primary_gpt_header + PARTITION_CRC_OFFSET, crc_value);
Neeti Desai5f26aff2011-09-30 10:27:40 -0700638
vijay kumar4f4405f2014-08-08 11:49:53 +0530639 crc_value = calculate_crc32((unsigned char *)partition_entry_array_start + array_size,
Ajay Dudanib01e5062011-12-03 23:23:42 -0800640 max_part_count * part_entry_size);
641 PUT_LONG(secondary_gpt_header + PARTITION_CRC_OFFSET, crc_value);
Neeti Desai5f26aff2011-09-30 10:27:40 -0700642
Ajay Dudanib01e5062011-12-03 23:23:42 -0800643 /* Clearing CRC fields to calculate */
644 PUT_LONG(primary_gpt_header + HEADER_CRC_OFFSET, 0);
645 crc_value = calculate_crc32(primary_gpt_header, 92);
646 PUT_LONG(primary_gpt_header + HEADER_CRC_OFFSET, crc_value);
Neeti Desai5f26aff2011-09-30 10:27:40 -0700647
Ajay Dudanib01e5062011-12-03 23:23:42 -0800648 PUT_LONG(secondary_gpt_header + HEADER_CRC_OFFSET, 0);
649 crc_value = (calculate_crc32(secondary_gpt_header, 92));
650 PUT_LONG(secondary_gpt_header + HEADER_CRC_OFFSET, crc_value);
Neeti Desai5f26aff2011-09-30 10:27:40 -0700651
652}
653
654/*
655 * Write the GPT to the MMC.
656 */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700657static unsigned int write_gpt(uint32_t size, uint8_t *gptImage, uint32_t block_size)
Neeti Desai5f26aff2011-09-30 10:27:40 -0700658{
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700659 unsigned int ret = 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800660 unsigned int header_size;
661 unsigned long long first_usable_lba;
662 unsigned long long backup_header_lba;
663 unsigned int max_partition_count = 0;
664 unsigned int partition_entry_size;
665 unsigned int partition_entry_array_start;
666 unsigned char *primary_gpt_header;
667 unsigned char *secondary_gpt_header;
668 unsigned int offset;
669 unsigned int partition_entry_array_size;
670 unsigned long long primary_header_location; /* address on the emmc card */
671 unsigned long long secondary_header_location; /* address on the emmc card */
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700672 uint64_t device_density;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700673
Ajay Dudanib01e5062011-12-03 23:23:42 -0800674 /* Verify that passed block has a valid GPT primary header */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700675 primary_gpt_header = (gptImage + block_size);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800676 ret = partition_parse_gpt_header(primary_gpt_header, &first_usable_lba,
677 &partition_entry_size, &header_size,
678 &max_partition_count);
679 if (ret) {
680 dprintf(CRITICAL,
681 "GPT: Primary signature invalid cannot write GPT\n");
682 goto end;
683 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700684
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700685 /* Get the density of the mmc device */
686
687 device_density = mmc_get_device_capacity();
688
Ajay Dudanib01e5062011-12-03 23:23:42 -0800689 /* Verify that passed block has a valid backup GPT HEADER */
690 partition_entry_array_size = partition_entry_size * max_partition_count;
691 if (partition_entry_array_size < MIN_PARTITION_ARRAY_SIZE) {
692 partition_entry_array_size = MIN_PARTITION_ARRAY_SIZE;
693 }
694 offset = (2 * partition_entry_array_size);
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700695 secondary_gpt_header = offset + block_size + primary_gpt_header;
vijay kumar24c9ed12015-04-28 13:05:04 +0530696 parse_secondary_gpt = 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800697 ret =
698 partition_parse_gpt_header(secondary_gpt_header, &first_usable_lba,
699 &partition_entry_size, &header_size,
700 &max_partition_count);
vijay kumar24c9ed12015-04-28 13:05:04 +0530701 parse_secondary_gpt = 0;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800702 if (ret) {
703 dprintf(CRITICAL,
704 "GPT: Backup signature invalid cannot write GPT\n");
705 goto end;
706 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700707
Ajay Dudanib01e5062011-12-03 23:23:42 -0800708 /* Patching the primary and the backup header of the GPT table */
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700709 patch_gpt(gptImage, device_density, partition_entry_array_size,
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700710 max_partition_count, partition_entry_size, block_size);
Neeti Desai5f26aff2011-09-30 10:27:40 -0700711
Ajay Dudanib01e5062011-12-03 23:23:42 -0800712 /* Erasing the eMMC card before writing */
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700713 ret = mmc_erase_card(0x00000000, device_density);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800714 if (ret) {
715 dprintf(CRITICAL, "Failed to erase the eMMC card\n");
716 goto end;
717 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700718
Ajay Dudanib01e5062011-12-03 23:23:42 -0800719 /* Writing protective MBR */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700720 ret = mmc_write(0, block_size, (unsigned int *)gptImage);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800721 if (ret) {
722 dprintf(CRITICAL, "Failed to write Protective MBR\n");
723 goto end;
724 }
725 /* Writing the primary GPT header */
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700726 primary_header_location = block_size;
727 ret = mmc_write(primary_header_location, block_size,
Ajay Dudanib01e5062011-12-03 23:23:42 -0800728 (unsigned int *)primary_gpt_header);
729 if (ret) {
730 dprintf(CRITICAL, "Failed to write GPT header\n");
731 goto end;
732 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700733
Ajay Dudanib01e5062011-12-03 23:23:42 -0800734 /* Writing the backup GPT header */
735 backup_header_lba = GET_LLWORD_FROM_BYTE
736 (&primary_gpt_header[BACKUP_HEADER_OFFSET]);
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700737 secondary_header_location = backup_header_lba * block_size;
738 ret = mmc_write(secondary_header_location, block_size,
Ajay Dudanib01e5062011-12-03 23:23:42 -0800739 (unsigned int *)secondary_gpt_header);
740 if (ret) {
741 dprintf(CRITICAL, "Failed to write GPT backup header\n");
742 goto end;
743 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700744
Ajay Dudanib01e5062011-12-03 23:23:42 -0800745 /* Writing the partition entries array for the primary header */
vijay kumar4f4405f2014-08-08 11:49:53 +0530746 partition_entry_array_start = (unsigned int)primary_gpt_header + block_size;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800747 ret = write_gpt_partition_array(primary_gpt_header,
748 partition_entry_array_start,
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700749 partition_entry_array_size, block_size);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800750 if (ret) {
751 dprintf(CRITICAL,
752 "GPT: Could not write GPT Partition entries array\n");
753 goto end;
754 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700755
Ajay Dudanib01e5062011-12-03 23:23:42 -0800756 /*Writing the partition entries array for the backup header */
vijay kumar4f4405f2014-08-08 11:49:53 +0530757 partition_entry_array_start = (unsigned int)primary_gpt_header + block_size +
Ajay Dudanib01e5062011-12-03 23:23:42 -0800758 partition_entry_array_size;
759 ret = write_gpt_partition_array(secondary_gpt_header,
760 partition_entry_array_start,
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700761 partition_entry_array_size, block_size);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800762 if (ret) {
763 dprintf(CRITICAL,
764 "GPT: Could not write GPT Partition entries array\n");
765 goto end;
766 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700767
Ajay Dudanib01e5062011-12-03 23:23:42 -0800768 /* Re-read the GPT partition table */
769 dprintf(INFO, "Re-reading the GPT Partition Table\n");
Sundarajan Srinivasan554863f2013-12-04 17:14:37 -0800770 partition_count = 0;
vijay kumar24c9ed12015-04-28 13:05:04 +0530771 flashing_gpt = 0;
Sundarajan Srinivasan554863f2013-12-04 17:14:37 -0800772 mmc_read_partition_table(0);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800773 partition_dump();
774 dprintf(CRITICAL, "GPT: Partition Table written\n");
775 memset(primary_gpt_header, 0x00, size);
Neeti Desai5f26aff2011-09-30 10:27:40 -0700776
Ajay Dudanib01e5062011-12-03 23:23:42 -0800777 end:
778 return ret;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700779}
780
Ajay Dudanib01e5062011-12-03 23:23:42 -0800781unsigned int write_partition(unsigned size, unsigned char *partition)
Neeti Desai5f26aff2011-09-30 10:27:40 -0700782{
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700783 unsigned int ret = 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800784 unsigned int partition_type;
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700785 uint32_t block_size;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700786
Ajay Dudanib01e5062011-12-03 23:23:42 -0800787 if (partition == 0) {
788 dprintf(CRITICAL, "NULL partition\n");
789 goto end;
790 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700791
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700792 block_size = mmc_get_device_blocksize();
Ajay Dudanib01e5062011-12-03 23:23:42 -0800793 ret = partition_get_type(size, partition, &partition_type);
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700794 if (ret)
Ajay Dudanib01e5062011-12-03 23:23:42 -0800795 goto end;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700796
Ajay Dudanib01e5062011-12-03 23:23:42 -0800797 switch (partition_type) {
798 case PARTITION_TYPE_MBR:
799 dprintf(INFO, "Writing MBR partition\n");
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700800 ret = write_mbr(size, partition, block_size);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800801 break;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700802
Ajay Dudanib01e5062011-12-03 23:23:42 -0800803 case PARTITION_TYPE_GPT:
804 dprintf(INFO, "Writing GPT partition\n");
vijay kumar24c9ed12015-04-28 13:05:04 +0530805 flashing_gpt = 1;
Channagoud Kadabi96c629e2013-09-10 14:21:30 -0700806 ret = write_gpt(size, partition, block_size);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800807 dprintf(CRITICAL, "Re-Flash all the partitions\n");
808 break;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700809
Ajay Dudanib01e5062011-12-03 23:23:42 -0800810 default:
811 dprintf(CRITICAL, "Invalid partition\n");
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700812 ret = 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800813 goto end;
814 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700815
Ajay Dudanib01e5062011-12-03 23:23:42 -0800816 end:
817 return ret;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700818}
Ajay Dudanib01e5062011-12-03 23:23:42 -0800819
Kinson Chikf1a43512011-07-14 11:28:39 -0700820/*
821 * Fill name for android partition found.
822 */
Ajay Dudanib01e5062011-12-03 23:23:42 -0800823static void
824mbr_fill_name(struct partition_entry *partition_ent, unsigned int type)
Kinson Chikf1a43512011-07-14 11:28:39 -0700825{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800826 switch (type) {
827 memset(partition_ent->name, 0, MAX_GPT_NAME_SIZE);
828 case MBR_MODEM_TYPE:
829 case MBR_MODEM_TYPE2:
830 /* if already assigned last name available then return */
831 if (!strcmp((const char *)vfat_partitions[vfat_count], "NONE"))
832 return;
833 strlcpy((char *)partition_ent->name,
834 (const char *)vfat_partitions[vfat_count],
835 sizeof(partition_ent->name));
836 vfat_count++;
837 break;
838 case MBR_SBL1_TYPE:
839 memcpy(partition_ent->name, "sbl1", 4);
840 break;
841 case MBR_SBL2_TYPE:
842 memcpy(partition_ent->name, "sbl2", 4);
843 break;
844 case MBR_SBL3_TYPE:
845 memcpy(partition_ent->name, "sbl3", 4);
846 break;
847 case MBR_RPM_TYPE:
848 memcpy(partition_ent->name, "rpm", 3);
849 break;
850 case MBR_TZ_TYPE:
851 memcpy(partition_ent->name, "tz", 2);
852 break;
853 case MBR_ABOOT_TYPE:
Channagoud Kadabi16f50952012-04-02 16:24:07 +0530854#if PLATFORM_MSM7X27A
855 memcpy(partition_ent->name, "FOTA", 4);
856#else
Ajay Dudanib01e5062011-12-03 23:23:42 -0800857 memcpy(partition_ent->name, "aboot", 5);
Channagoud Kadabi16f50952012-04-02 16:24:07 +0530858#endif
Ajay Dudanib01e5062011-12-03 23:23:42 -0800859 break;
860 case MBR_BOOT_TYPE:
861 memcpy(partition_ent->name, "boot", 4);
862 break;
863 case MBR_MODEM_ST1_TYPE:
864 memcpy(partition_ent->name, "modem_st1", 9);
865 break;
866 case MBR_MODEM_ST2_TYPE:
867 memcpy(partition_ent->name, "modem_st2", 9);
868 break;
869 case MBR_EFS2_TYPE:
870 memcpy(partition_ent->name, "efs2", 4);
871 break;
872 case MBR_USERDATA_TYPE:
873 if (ext3_count == sizeof(ext3_partitions) / sizeof(char *))
874 return;
875 strlcpy((char *)partition_ent->name,
876 (const char *)ext3_partitions[ext3_count],
877 sizeof(partition_ent->name));
878 ext3_count++;
879 break;
880 case MBR_RECOVERY_TYPE:
881 memcpy(partition_ent->name, "recovery", 8);
882 break;
883 case MBR_MISC_TYPE:
884 memcpy(partition_ent->name, "misc", 4);
885 break;
Neeti Desai2d6b0e42012-03-23 15:48:57 -0700886 case MBR_SSD_TYPE:
887 memcpy(partition_ent->name, "ssd", 3);
888 break;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800889 };
Kinson Chikf1a43512011-07-14 11:28:39 -0700890}
891
892/*
893 * Find index of parition in array of partition entries
894 */
Pavel Nedev285ad922013-04-26 10:39:19 +0300895int partition_get_index(const char *name)
Kinson Chikf1a43512011-07-14 11:28:39 -0700896{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800897 unsigned int input_string_length = strlen(name);
898 unsigned n;
Mayank Grovere358f1f2017-05-30 20:05:14 +0530899 int curr_slot = INVALID;
900 const char *suffix_curr_actv_slot = NULL;
901 char *curr_suffix = NULL;
Kinson Chik66552a82011-03-29 15:59:06 -0700902
neetid6c38de12011-12-02 12:04:50 -0800903 if( partition_count >= NUM_PARTITIONS)
904 {
905 return INVALID_PTN;
906 }
Mayank Grovere358f1f2017-05-30 20:05:14 +0530907
908 /* We iterate through the parition entries list,
909 to find the partition with active slot suffix.
910 */
911 for (n = 0; n < partition_count; n++)
912 {
913 if (!strncmp((const char*)name, (const char *)partition_entries[n].name,
914 input_string_length))
915 {
916 curr_suffix = (char *)(partition_entries[n].name+input_string_length);
917
918 /* if partition_entries.name is NULL terminated return the index */
919 if (*curr_suffix == '\0')
920 return n;
921
922 if (partition_multislot_is_supported())
923 {
924 curr_slot = partition_find_active_slot();
925
926 /* If suffix string matches with current active slot suffix return index */
927 if (curr_slot != INVALID)
928 {
929 suffix_curr_actv_slot = SUFFIX_SLOT(curr_slot);
930 if (!strncmp((const char *)curr_suffix, suffix_curr_actv_slot,
931 strlen(suffix_curr_actv_slot)))
932 return n;
933 else
934 continue;
935 }
936 else
937 {
938 /* No valid active slot */
939 return INVALID_PTN;
940 }
941 }
Ajay Dudanib01e5062011-12-03 23:23:42 -0800942 }
943 }
944 return INVALID_PTN;
Kinson Chikf1a43512011-07-14 11:28:39 -0700945}
946
Vijay Kumar Pendoti1c1d6702016-07-21 13:03:23 +0530947/*
948 * Find relative index of partition in lun
949 */
950int partition_get_index_in_lun(const char *name, unsigned int lun)
951{
952 unsigned int input_string_length = strlen(name);
953 unsigned int n, relative_index = 0;
Mayank Grovere358f1f2017-05-30 20:05:14 +0530954 int curr_slot = INVALID;
955 const char *suffix_curr_actv_slot = NULL;
956 char *curr_suffix = NULL;
957 for (n = 0; n < partition_count; n++)
958 {
Vijay Kumar Pendoti1c1d6702016-07-21 13:03:23 +0530959 if (lun == partition_entries[n].lun)
960 {
961 relative_index++;
Mayank Grovere358f1f2017-05-30 20:05:14 +0530962 if (!strncmp((const char*)name, (const char *)partition_entries[n].name,
963 input_string_length))
964 {
965 curr_suffix = (char *)(partition_entries[n].name+input_string_length);
966
967 /* if partition_entries.name is NULL terminated return relative index */
968 if (*curr_suffix == '\0')
969 return relative_index;
970
971 if (partition_multislot_is_supported())
972 {
973 curr_slot = partition_find_active_slot();
974
975 /* If suffix string matches with current active slot suffix return index */
976 if (curr_slot != INVALID)
977 {
978 suffix_curr_actv_slot = SUFFIX_SLOT(curr_slot);
979 if (!strncmp((const char *)curr_suffix, suffix_curr_actv_slot,
980 strlen(suffix_curr_actv_slot)))
981 return relative_index;
982 else
983 continue;
984 }
985 else
986 {
987 /* No valid active slot */
988 return INVALID_PTN;
989 }
990 }
Vijay Kumar Pendoti1c1d6702016-07-21 13:03:23 +0530991 }
992 }
993 }
994 return INVALID_PTN;
995}
996
Kinson Chikf1a43512011-07-14 11:28:39 -0700997/* Get size of the partition */
Ajay Dudanib01e5062011-12-03 23:23:42 -0800998unsigned long long partition_get_size(int index)
Kinson Chikf1a43512011-07-14 11:28:39 -0700999{
Channagoud Kadabi96c629e2013-09-10 14:21:30 -07001000 uint32_t block_size;
1001
1002 block_size = mmc_get_device_blocksize();
1003
Ajay Dudanib01e5062011-12-03 23:23:42 -08001004 if (index == INVALID_PTN)
1005 return 0;
1006 else {
Channagoud Kadabi96c629e2013-09-10 14:21:30 -07001007 return partition_entries[index].size * block_size;
Ajay Dudanib01e5062011-12-03 23:23:42 -08001008 }
Kinson Chikf1a43512011-07-14 11:28:39 -07001009}
1010
1011/* Get offset of the partition */
Ajay Dudanib01e5062011-12-03 23:23:42 -08001012unsigned long long partition_get_offset(int index)
Kinson Chikf1a43512011-07-14 11:28:39 -07001013{
Channagoud Kadabi96c629e2013-09-10 14:21:30 -07001014 uint32_t block_size;
1015
1016 block_size = mmc_get_device_blocksize();
1017
Ajay Dudanib01e5062011-12-03 23:23:42 -08001018 if (index == INVALID_PTN)
1019 return 0;
1020 else {
Channagoud Kadabi96c629e2013-09-10 14:21:30 -07001021 return partition_entries[index].first_lba * block_size;
Ajay Dudanib01e5062011-12-03 23:23:42 -08001022 }
Kinson Chikf1a43512011-07-14 11:28:39 -07001023}
1024
Sundarajan Srinivasan9ec69632014-04-10 13:33:10 -07001025struct partition_info partition_get_info(const char *name)
1026{
1027 struct partition_info info = {0};
1028
1029 int index = INVALID_PTN;
1030
1031 if(!name)
1032 {
1033 dprintf(CRITICAL, "Invalid partition name passed\n");
1034 goto out;
1035 }
1036
1037 index = partition_get_index(name);
1038
1039 if (index != INVALID_PTN)
1040 {
1041 info.offset = partition_get_offset(index);
1042 info.size = partition_get_size(index);
1043 }
1044 else
1045 {
1046 dprintf(CRITICAL, "Error unable to find partition : [%s]\n", name);
1047 }
1048out:
1049 return info;
1050}
1051
Sundarajan Srinivasan554863f2013-12-04 17:14:37 -08001052uint8_t partition_get_lun(int index)
1053{
1054 return partition_entries[index].lun;
1055}
1056
Kinson Chikf1a43512011-07-14 11:28:39 -07001057/* Debug: Print all parsed partitions */
1058void partition_dump()
1059{
Ajay Dudanib01e5062011-12-03 23:23:42 -08001060 unsigned i = 0;
1061 for (i = 0; i < partition_count; i++) {
1062 dprintf(SPEW,
1063 "ptn[%d]:Name[%s] Size[%llu] Type[%u] First[%llu] Last[%llu]\n",
1064 i, partition_entries[i].name, partition_entries[i].size,
1065 partition_entries[i].dtype,
1066 partition_entries[i].first_lba,
1067 partition_entries[i].last_lba);
1068 }
Kinson Chikf1a43512011-07-14 11:28:39 -07001069}
1070
Channagoud Kadabi0e332852013-04-19 12:55:53 -07001071static unsigned int
Ajay Dudanib01e5062011-12-03 23:23:42 -08001072partition_verify_mbr_signature(unsigned size, unsigned char *buffer)
Kinson Chikf1a43512011-07-14 11:28:39 -07001073{
Ajay Dudanib01e5062011-12-03 23:23:42 -08001074 /* Avoid checking past end of buffer */
1075 if ((TABLE_SIGNATURE + 1) > size) {
Channagoud Kadabi0e332852013-04-19 12:55:53 -07001076 return 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -08001077 }
1078 /* Check to see if signature exists */
1079 if ((buffer[TABLE_SIGNATURE] != MMC_MBR_SIGNATURE_BYTE_0) ||
1080 (buffer[TABLE_SIGNATURE + 1] != MMC_MBR_SIGNATURE_BYTE_1)) {
1081 dprintf(CRITICAL, "MBR signature does not match.\n");
Channagoud Kadabi0e332852013-04-19 12:55:53 -07001082 return 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -08001083 }
Channagoud Kadabi0e332852013-04-19 12:55:53 -07001084 return 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001085}
1086
Channagoud Kadabi0e332852013-04-19 12:55:53 -07001087static unsigned int
Ajay Dudanib01e5062011-12-03 23:23:42 -08001088mbr_partition_get_type(unsigned size, unsigned char *partition,
1089 unsigned int *partition_type)
Kinson Chikf1a43512011-07-14 11:28:39 -07001090{
Ajay Dudanib01e5062011-12-03 23:23:42 -08001091 unsigned int type_offset = TABLE_ENTRY_0 + OFFSET_TYPE;
Kinson Chikf1a43512011-07-14 11:28:39 -07001092
Channagoud Kadabi98b11072015-03-12 15:14:22 -07001093 if (size < (type_offset + sizeof (*partition_type))) {
Ajay Dudanib01e5062011-12-03 23:23:42 -08001094 goto end;
1095 }
Kinson Chikf1a43512011-07-14 11:28:39 -07001096
Ajay Dudanib01e5062011-12-03 23:23:42 -08001097 *partition_type = partition[type_offset];
1098 end:
Channagoud Kadabi0e332852013-04-19 12:55:53 -07001099 return 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001100}
1101
Channagoud Kadabi0e332852013-04-19 12:55:53 -07001102static unsigned int
Ajay Dudanib01e5062011-12-03 23:23:42 -08001103partition_get_type(unsigned size, unsigned char *partition,
1104 unsigned int *partition_type)
Kinson Chikf1a43512011-07-14 11:28:39 -07001105{
Channagoud Kadabi0e332852013-04-19 12:55:53 -07001106 unsigned int ret = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001107
Ajay Dudanib01e5062011-12-03 23:23:42 -08001108 /*
1109 * If the block contains the MBR signature, then it's likely either
1110 * MBR or MBR with protective type (GPT). If the MBR signature is
1111 * not there, then it could be the GPT backup.
1112 */
Kinson Chikf1a43512011-07-14 11:28:39 -07001113
Ajay Dudanib01e5062011-12-03 23:23:42 -08001114 /* First check the MBR signature */
1115 ret = partition_verify_mbr_signature(size, partition);
Channagoud Kadabi0e332852013-04-19 12:55:53 -07001116 if (!ret) {
Ajay Dudanib01e5062011-12-03 23:23:42 -08001117 unsigned int mbr_partition_type = PARTITION_TYPE_MBR;
Kinson Chikf1a43512011-07-14 11:28:39 -07001118
Ajay Dudanib01e5062011-12-03 23:23:42 -08001119 /* MBR signature verified. This could be MBR, MBR + EBR, or GPT */
1120 ret =
1121 mbr_partition_get_type(size, partition,
1122 &mbr_partition_type);
Channagoud Kadabi0e332852013-04-19 12:55:53 -07001123 if (ret) {
Ajay Dudanib01e5062011-12-03 23:23:42 -08001124 dprintf(CRITICAL, "Cannot get TYPE of partition");
1125 } else if (MBR_PROTECTED_TYPE == mbr_partition_type) {
1126 *partition_type = PARTITION_TYPE_GPT;
1127 } else {
1128 *partition_type = PARTITION_TYPE_MBR;
1129 }
1130 } else {
1131 /*
1132 * This could be the GPT backup. Make that assumption for now.
1133 * Anybody who treats the block as GPT backup should check the
1134 * signature.
1135 */
1136 *partition_type = PARTITION_TYPE_GPT_BACKUP;
1137 }
1138 return ret;
Kinson Chik66552a82011-03-29 15:59:06 -07001139}
Kinson Chik4d7444f2011-09-13 15:48:51 -07001140
1141/*
1142 * Parse the gpt header and get the required header fields
1143 * Return 0 on valid signature
1144 */
Channagoud Kadabi0e332852013-04-19 12:55:53 -07001145static unsigned int
Ajay Dudanib01e5062011-12-03 23:23:42 -08001146partition_parse_gpt_header(unsigned char *buffer,
1147 unsigned long long *first_usable_lba,
1148 unsigned int *partition_entry_size,
1149 unsigned int *header_size,
1150 unsigned int *max_partition_count)
Kinson Chik4d7444f2011-09-13 15:48:51 -07001151{
vijay kumar24c9ed12015-04-28 13:05:04 +05301152 uint32_t crc_val_org = 0;
1153 uint32_t crc_val = 0;
1154 uint32_t ret = 0;
1155 uint32_t partitions_for_block = 0;
1156 uint32_t blocks_to_read = 0;
vijay kumar24c9ed12015-04-28 13:05:04 +05301157 unsigned long long last_usable_lba = 0;
1158 unsigned long long partition_0 = 0;
1159 unsigned long long current_lba = 0;
1160 uint32_t block_size = mmc_get_device_blocksize();
1161 /* Get the density of the mmc device */
1162 uint64_t device_density = mmc_get_device_capacity();
1163
Ajay Dudanib01e5062011-12-03 23:23:42 -08001164 /* Check GPT Signature */
1165 if (((uint32_t *) buffer)[0] != GPT_SIGNATURE_2 ||
1166 ((uint32_t *) buffer)[1] != GPT_SIGNATURE_1)
1167 return 1;
Kinson Chik4d7444f2011-09-13 15:48:51 -07001168
Ajay Dudanib01e5062011-12-03 23:23:42 -08001169 *header_size = GET_LWORD_FROM_BYTE(&buffer[HEADER_SIZE_OFFSET]);
vijay kumar24c9ed12015-04-28 13:05:04 +05301170 /*check for header size too small*/
1171 if (*header_size < GPT_HEADER_SIZE) {
1172 dprintf(CRITICAL,"GPT Header size is too small\n");
1173 return 1;
1174 }
1175 /*check for header size too large*/
1176 if (*header_size > block_size) {
1177 dprintf(CRITICAL,"GPT Header size is too large\n");
1178 return 1;
1179 }
1180
1181 crc_val_org = GET_LWORD_FROM_BYTE(&buffer[HEADER_CRC_OFFSET]);
1182 /*Write CRC to 0 before we calculate the crc of the GPT header*/
1183 crc_val = 0;
1184 PUT_LONG(&buffer[HEADER_CRC_OFFSET], crc_val);
1185
1186 crc_val = crc32(~0L,buffer, *header_size) ^ (~0L);
1187 if (crc_val != crc_val_org) {
1188 dprintf(CRITICAL,"Header crc mismatch crc_val = %u with crc_val_org = %u\n", crc_val,crc_val_org);
1189 return 1;
1190 }
1191 else
1192 PUT_LONG(&buffer[HEADER_CRC_OFFSET], crc_val);
1193
1194 current_lba =
1195 GET_LLWORD_FROM_BYTE(&buffer[PRIMARY_HEADER_OFFSET]);
Ajay Dudanib01e5062011-12-03 23:23:42 -08001196 *first_usable_lba =
1197 GET_LLWORD_FROM_BYTE(&buffer[FIRST_USABLE_LBA_OFFSET]);
1198 *max_partition_count =
1199 GET_LWORD_FROM_BYTE(&buffer[PARTITION_COUNT_OFFSET]);
1200 *partition_entry_size =
1201 GET_LWORD_FROM_BYTE(&buffer[PENTRY_SIZE_OFFSET]);
vijay kumar24c9ed12015-04-28 13:05:04 +05301202 last_usable_lba =
1203 GET_LLWORD_FROM_BYTE(&buffer[LAST_USABLE_LBA_OFFSET]);
Kinson Chik4d7444f2011-09-13 15:48:51 -07001204
vijay kumar24c9ed12015-04-28 13:05:04 +05301205 /*current lba and GPT lba should be same*/
1206 if (!parse_secondary_gpt) {
1207 if (current_lba != GPT_LBA) {
1208 dprintf(CRITICAL,"GPT first usable LBA mismatch\n");
1209 return 1;
1210 }
1211 }
1212 /*check for first lba should be with in the valid range*/
1213 if (*first_usable_lba > (device_density/block_size)) {
1214 dprintf(CRITICAL,"Invalid first_usable_lba\n");
1215 return 1;
1216 }
1217 /*check for last lba should be with in the valid range*/
1218 if (last_usable_lba > (device_density/block_size)) {
1219 dprintf(CRITICAL,"Invalid last_usable_lba\n");
1220 return 1;
1221 }
1222 /*check for partition entry size*/
1223 if (*partition_entry_size != PARTITION_ENTRY_SIZE) {
1224 dprintf(CRITICAL,"Invalid parition entry size\n");
1225 return 1;
1226 }
1227
1228 if ((*max_partition_count) > (MIN_PARTITION_ARRAY_SIZE /(*partition_entry_size))) {
1229 dprintf(CRITICAL, "Invalid maximum partition count\n");
1230 return 1;
1231 }
1232
1233 partitions_for_block = block_size / (*partition_entry_size);
1234
1235 blocks_to_read = (*max_partition_count)/ partitions_for_block;
1236 if ((*max_partition_count) % partitions_for_block) {
1237 blocks_to_read += 1;
1238 }
1239
1240 new_buffer = (uint8_t *)memalign(CACHE_LINE, ROUNDUP((blocks_to_read * block_size),CACHE_LINE));
1241
1242 if (!new_buffer)
1243 {
1244 dprintf(CRITICAL, "Failed to Allocate memory to read partition table\n");
1245 return 1;
1246 }
1247
1248 if (!flashing_gpt) {
1249 partition_0 = GET_LLWORD_FROM_BYTE(&buffer[PARTITION_ENTRIES_OFFSET]);
1250 /*start LBA should always be 2 in primary GPT*/
lijuang0b655852017-05-22 16:21:48 +08001251 if(partition_0 != 0x2 && !parse_secondary_gpt) {
vijay kumar24c9ed12015-04-28 13:05:04 +05301252 dprintf(CRITICAL, "Starting LBA mismatch\n");
lijuang0b655852017-05-22 16:21:48 +08001253 ret = 1;
vijay kumar24c9ed12015-04-28 13:05:04 +05301254 goto fail;
1255
1256 }
1257 /*read the partition entries to new_buffer*/
1258 ret = mmc_read((partition_0) * (block_size), (unsigned int *)new_buffer, (blocks_to_read * block_size));
1259 if (ret)
1260 {
1261 dprintf(CRITICAL, "GPT: Could not read primary gpt from mmc\n");
1262 goto fail;
1263 }
1264 crc_val_org = GET_LWORD_FROM_BYTE(&buffer[PARTITION_CRC_OFFSET]);
1265
1266 crc_val = crc32(~0L,new_buffer, ((*max_partition_count) * (*partition_entry_size))) ^ (~0L);
1267 if (crc_val != crc_val_org) {
1268 dprintf(CRITICAL,"Partition entires crc mismatch crc_val= %u with crc_val_org= %u\n",crc_val,crc_val_org);
1269 ret = 1;
1270 }
Mayank Grover07f7ca62018-05-30 16:33:33 +05301271 return ret;
vijay kumar24c9ed12015-04-28 13:05:04 +05301272 }
1273fail:
1274 free(new_buffer);
1275 return ret;
Kinson Chik4d7444f2011-09-13 15:48:51 -07001276}
Pavel Nedevb5ba62d2013-07-22 11:57:41 +03001277
1278bool partition_gpt_exists()
1279{
1280 return (gpt_partitions_exist != 0);
1281}
Channagoud Kadabifaf20f62014-10-21 22:22:37 -07001282
1283int partition_read_only(int index)
1284{
1285 return partition_entries[index].attribute_flag >> PART_ATT_READONLY_OFFSET;
1286}