blob: 5cca931bf1e46c251724690f30b6c2bbea41d33f [file] [log] [blame]
Channagoud Kadabif73aa292013-01-31 16:55:20 -08001/* Copyright (c) 2011-2013, The Linux Foundation. All rights reserved.
Kinson Chik66552a82011-03-29 15:59:06 -07002
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer in the documentation and/or other materials provided
11 * with the distribution.
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>
31#include "mmc.h"
32#include "partition_parser.h"
33
Channagoud Kadabi0e332852013-04-19 12:55:53 -070034static uint32_t mmc_boot_read_gpt();
35static uint32_t mmc_boot_read_mbr();
36static void mbr_fill_name(struct partition_entry *partition_ent,
37 uint32_t type);
38static uint32_t partition_verify_mbr_signature(uint32_t size,
39 uint8_t *buffer);
40static uint32_t mbr_partition_get_type(uint32_t size, uint8_t *partition,
41 uint32_t *partition_type);
42
43static uint32_t partition_get_type(uint32_t size, uint8_t *partition,
44 uint32_t *partition_type);
45static uint32_t partition_parse_gpt_header(uint8_t *buffer,
46 uint64_t *first_usable_lba,
47 uint32_t *partition_entry_size,
48 uint32_t *header_size,
49 uint32_t *max_partition_count);
50
51static uint32_t write_mbr(uint32_t, uint8_t *mbrImage);
52static uint32_t write_gpt(uint32_t size, uint8_t *gptImage);
53
Ajay Dudanib01e5062011-12-03 23:23:42 -080054char *ext3_partitions[] =
55 { "system", "userdata", "persist", "cache", "tombstones" };
56char *vfat_partitions[] = { "modem", "mdm", "NONE" };
57
Kinson Chikf1a43512011-07-14 11:28:39 -070058unsigned int ext3_count = 0;
59unsigned int vfat_count = 0;
60
61struct partition_entry partition_entries[NUM_PARTITIONS];
Pavel Nedevb5ba62d2013-07-22 11:57:41 +030062static unsigned gpt_partitions_exist = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -070063unsigned partition_count = 0;
64
Channagoud Kadabi0e332852013-04-19 12:55:53 -070065unsigned int partition_read_table()
Kinson Chikf1a43512011-07-14 11:28:39 -070066{
Ajay Dudanib01e5062011-12-03 23:23:42 -080067 unsigned int ret;
Kinson Chikf1a43512011-07-14 11:28:39 -070068
Ajay Dudanib01e5062011-12-03 23:23:42 -080069 /* Read MBR of the card */
Channagoud Kadabi0e332852013-04-19 12:55:53 -070070 ret = mmc_boot_read_mbr();
71 if (ret) {
Ajay Dudanib01e5062011-12-03 23:23:42 -080072 dprintf(CRITICAL, "MMC Boot: MBR read failed!\n");
Channagoud Kadabi0e332852013-04-19 12:55:53 -070073 return 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -080074 }
Kinson Chikf1a43512011-07-14 11:28:39 -070075
Ajay Dudanib01e5062011-12-03 23:23:42 -080076 /* Read GPT of the card if exist */
77 if (gpt_partitions_exist) {
Channagoud Kadabi0e332852013-04-19 12:55:53 -070078 ret = mmc_boot_read_gpt();
79 if (ret) {
Ajay Dudanib01e5062011-12-03 23:23:42 -080080 dprintf(CRITICAL, "MMC Boot: GPT read failed!\n");
Channagoud Kadabi0e332852013-04-19 12:55:53 -070081 return 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -080082 }
83 }
Channagoud Kadabi0e332852013-04-19 12:55:53 -070084 return 0;
Kinson Chikf1a43512011-07-14 11:28:39 -070085}
86
87/*
88 * Read MBR from MMC card and fill partition table.
89 */
Channagoud Kadabi0e332852013-04-19 12:55:53 -070090static unsigned int mmc_boot_read_mbr()
Kinson Chikf1a43512011-07-14 11:28:39 -070091{
Channagoud Kadabi2af6e362013-04-19 14:32:22 -070092 BUF_DMA_ALIGN(buffer, BLOCK_SIZE);
Ajay Dudanib01e5062011-12-03 23:23:42 -080093 unsigned int dtype;
94 unsigned int dfirstsec;
95 unsigned int EBR_first_sec;
96 unsigned int EBR_current_sec;
Channagoud Kadabi0e332852013-04-19 12:55:53 -070097 int ret = 0;
Ajay Dudanib01e5062011-12-03 23:23:42 -080098 int idx, i;
Kinson Chikf1a43512011-07-14 11:28:39 -070099
Ajay Dudanib01e5062011-12-03 23:23:42 -0800100 /* Print out the MBR first */
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700101 ret = mmc_read(0, (unsigned int *)buffer, BLOCK_SIZE);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800102 if (ret) {
103 dprintf(CRITICAL, "Could not read partition from mmc\n");
104 return ret;
105 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700106
Ajay Dudanib01e5062011-12-03 23:23:42 -0800107 /* Check to see if signature exists */
108 ret = partition_verify_mbr_signature(BLOCK_SIZE, buffer);
109 if (ret) {
110 return ret;
111 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700112
Ajay Dudanib01e5062011-12-03 23:23:42 -0800113 /*
114 * Process each of the four partitions in the MBR by reading the table
115 * information into our mbr table.
116 */
117 partition_count = 0;
118 idx = TABLE_ENTRY_0;
119 for (i = 0; i < 4; i++) {
120 /* Type 0xEE indicates end of MBR and GPT partitions exist */
121 dtype = buffer[idx + i * TABLE_ENTRY_SIZE + OFFSET_TYPE];
122 if (dtype == MBR_PROTECTED_TYPE) {
123 gpt_partitions_exist = 1;
124 return ret;
125 }
126 partition_entries[partition_count].dtype = dtype;
127 partition_entries[partition_count].attribute_flag =
128 buffer[idx + i * TABLE_ENTRY_SIZE + OFFSET_STATUS];
129 partition_entries[partition_count].first_lba =
130 GET_LWORD_FROM_BYTE(&buffer[idx +
131 i * TABLE_ENTRY_SIZE +
132 OFFSET_FIRST_SEC]);
133 partition_entries[partition_count].size =
134 GET_LWORD_FROM_BYTE(&buffer[idx +
135 i * TABLE_ENTRY_SIZE +
136 OFFSET_SIZE]);
137 dfirstsec = partition_entries[partition_count].first_lba;
138 mbr_fill_name(&partition_entries[partition_count],
139 partition_entries[partition_count].dtype);
140 partition_count++;
141 if (partition_count == NUM_PARTITIONS)
142 return ret;
143 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700144
Ajay Dudanib01e5062011-12-03 23:23:42 -0800145 /* See if the last partition is EBR, if not, parsing is done */
146 if (dtype != MBR_EBR_TYPE) {
147 return ret;
148 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700149
Ajay Dudanib01e5062011-12-03 23:23:42 -0800150 EBR_first_sec = dfirstsec;
151 EBR_current_sec = dfirstsec;
Kinson Chikf1a43512011-07-14 11:28:39 -0700152
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700153 ret = mmc_read((EBR_first_sec * 512), (unsigned int *)buffer, BLOCK_SIZE);
154 if (ret)
Ajay Dudanib01e5062011-12-03 23:23:42 -0800155 return ret;
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700156
Ajay Dudanib01e5062011-12-03 23:23:42 -0800157 /* Loop to parse the EBR */
158 for (i = 0;; i++) {
159 ret = partition_verify_mbr_signature(BLOCK_SIZE, buffer);
160 if (ret) {
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700161 ret = 0;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800162 break;
163 }
164 partition_entries[partition_count].attribute_flag =
165 buffer[TABLE_ENTRY_0 + OFFSET_STATUS];
166 partition_entries[partition_count].dtype =
167 buffer[TABLE_ENTRY_0 + OFFSET_TYPE];
168 partition_entries[partition_count].first_lba =
169 GET_LWORD_FROM_BYTE(&buffer[TABLE_ENTRY_0 +
170 OFFSET_FIRST_SEC]) +
171 EBR_current_sec;
172 partition_entries[partition_count].size =
173 GET_LWORD_FROM_BYTE(&buffer[TABLE_ENTRY_0 + OFFSET_SIZE]);
174 mbr_fill_name(&(partition_entries[partition_count]),
175 partition_entries[partition_count].dtype);
176 partition_count++;
177 if (partition_count == NUM_PARTITIONS)
178 return ret;
Kinson Chikf1a43512011-07-14 11:28:39 -0700179
Ajay Dudanib01e5062011-12-03 23:23:42 -0800180 dfirstsec =
181 GET_LWORD_FROM_BYTE(&buffer
182 [TABLE_ENTRY_1 + OFFSET_FIRST_SEC]);
183 if (dfirstsec == 0) {
184 /* Getting to the end of the EBR tables */
185 break;
186 }
187 /* More EBR to follow - read in the next EBR sector */
188 dprintf(SPEW, "Reading EBR block from 0x%X\n", EBR_first_sec
189 + dfirstsec);
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700190 ret = mmc_read(((EBR_first_sec + dfirstsec) * 512),(unsigned int *)buffer,
191 BLOCK_SIZE);
192 if (ret)
Ajay Dudanib01e5062011-12-03 23:23:42 -0800193 return ret;
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700194
Ajay Dudanib01e5062011-12-03 23:23:42 -0800195 EBR_current_sec = EBR_first_sec + dfirstsec;
196 }
197 return ret;
Kinson Chikf1a43512011-07-14 11:28:39 -0700198}
Kinson Chik66552a82011-03-29 15:59:06 -0700199
200/*
201 * Read GPT from MMC and fill partition table
202 */
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700203static unsigned int mmc_boot_read_gpt()
Kinson Chikf1a43512011-07-14 11:28:39 -0700204{
Channagoud Kadabi2af6e362013-04-19 14:32:22 -0700205 BUF_DMA_ALIGN(data, BLOCK_SIZE);
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700206 int ret = 0;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800207 unsigned int header_size;
208 unsigned long long first_usable_lba;
209 unsigned long long backup_header_lba;
Neeti Desaice6ad9a2012-03-21 18:57:59 -0700210 unsigned long long card_size_sec;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800211 unsigned int max_partition_count = 0;
212 unsigned int partition_entry_size;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800213 unsigned int i = 0; /* Counter for each 512 block */
214 unsigned int j = 0; /* Counter for each 128 entry in the 512 block */
215 unsigned int n = 0; /* Counter for UTF-16 -> 8 conversion */
216 unsigned char UTF16_name[MAX_GPT_NAME_SIZE];
217 /* LBA of first partition -- 1 Block after Protected MBR + 1 for PT */
Neeti Desaice6ad9a2012-03-21 18:57:59 -0700218 unsigned long long partition_0;
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700219 uint64_t device_density;
220
Ajay Dudanib01e5062011-12-03 23:23:42 -0800221 partition_count = 0;
Kinson Chik66552a82011-03-29 15:59:06 -0700222
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700223 /* Get the density of the mmc device */
224
225 device_density = mmc_get_device_capacity();
226
Ajay Dudanib01e5062011-12-03 23:23:42 -0800227 /* Print out the GPT first */
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700228 ret = mmc_read(PROTECTIVE_MBR_SIZE, (unsigned int *)data, BLOCK_SIZE);
229 if (ret)
Ajay Dudanib01e5062011-12-03 23:23:42 -0800230 dprintf(CRITICAL, "GPT: Could not read primary gpt from mmc\n");
Kinson Chik66552a82011-03-29 15:59:06 -0700231
Ajay Dudanib01e5062011-12-03 23:23:42 -0800232 ret = partition_parse_gpt_header(data, &first_usable_lba,
233 &partition_entry_size, &header_size,
234 &max_partition_count);
235 if (ret) {
236 dprintf(INFO, "GPT: (WARNING) Primary signature invalid\n");
Kinson Chik4d7444f2011-09-13 15:48:51 -0700237
Ajay Dudanib01e5062011-12-03 23:23:42 -0800238 /* Check the backup gpt */
Neeti Desaice6ad9a2012-03-21 18:57:59 -0700239
240 /* Get size of MMC */
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700241 card_size_sec = (device_density) / BLOCK_SIZE;
Neeti Desaice6ad9a2012-03-21 18:57:59 -0700242 ASSERT (card_size_sec > 0);
243
244 backup_header_lba = card_size_sec - 1;
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700245 ret = mmc_read((backup_header_lba * BLOCK_SIZE), (unsigned int *)data,
246 BLOCK_SIZE);
Kinson Chik4d7444f2011-09-13 15:48:51 -0700247
Ajay Dudanib01e5062011-12-03 23:23:42 -0800248 if (ret) {
249 dprintf(CRITICAL,
250 "GPT: Could not read backup gpt from mmc\n");
251 return ret;
252 }
Kinson Chik4d7444f2011-09-13 15:48:51 -0700253
Ajay Dudanib01e5062011-12-03 23:23:42 -0800254 ret = partition_parse_gpt_header(data, &first_usable_lba,
255 &partition_entry_size,
256 &header_size,
257 &max_partition_count);
258 if (ret) {
259 dprintf(CRITICAL,
260 "GPT: Primary and backup signatures invalid\n");
261 return ret;
262 }
Ajay Dudanib01e5062011-12-03 23:23:42 -0800263 }
Neeti Desaice6ad9a2012-03-21 18:57:59 -0700264 partition_0 = GET_LLWORD_FROM_BYTE(&data[PARTITION_ENTRIES_OFFSET]);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800265 /* Read GPT Entries */
Channagoud Kadabi82a482f2013-02-14 21:04:38 -0800266 for (i = 0; i < (ROUNDUP(max_partition_count, 4)) / 4; i++) {
neetid6c38de12011-12-02 12:04:50 -0800267 ASSERT(partition_count < NUM_PARTITIONS);
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700268 ret = mmc_read((partition_0 * BLOCK_SIZE) + (i * BLOCK_SIZE),
269 (uint32_t *) data, BLOCK_SIZE);
Kinson Chik66552a82011-03-29 15:59:06 -0700270
Ajay Dudanib01e5062011-12-03 23:23:42 -0800271 if (ret) {
272 dprintf(CRITICAL,
273 "GPT: mmc read card failed reading partition entries.\n");
274 return ret;
275 }
Kinson Chik66552a82011-03-29 15:59:06 -0700276
Ajay Dudanib01e5062011-12-03 23:23:42 -0800277 for (j = 0; j < 4; j++) {
278 memcpy(&(partition_entries[partition_count].type_guid),
279 &data[(j * partition_entry_size)],
280 PARTITION_TYPE_GUID_SIZE);
281 if (partition_entries[partition_count].type_guid[0] ==
282 0x00
283 && partition_entries[partition_count].
284 type_guid[1] == 0x00) {
Channagoud Kadabif73aa292013-01-31 16:55:20 -0800285 i = ROUNDUP(max_partition_count, 4);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800286 break;
287 }
288 memcpy(&
289 (partition_entries[partition_count].
290 unique_partition_guid),
291 &data[(j * partition_entry_size) +
292 UNIQUE_GUID_OFFSET],
293 UNIQUE_PARTITION_GUID_SIZE);
294 partition_entries[partition_count].first_lba =
295 GET_LLWORD_FROM_BYTE(&data
296 [(j * partition_entry_size) +
297 FIRST_LBA_OFFSET]);
298 partition_entries[partition_count].last_lba =
299 GET_LLWORD_FROM_BYTE(&data
300 [(j * partition_entry_size) +
301 LAST_LBA_OFFSET]);
302 partition_entries[partition_count].size =
303 partition_entries[partition_count].last_lba -
Neeti Desai4b8c1df2012-03-21 13:15:14 -0700304 partition_entries[partition_count].first_lba + 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800305 partition_entries[partition_count].attribute_flag =
306 GET_LLWORD_FROM_BYTE(&data
307 [(j * partition_entry_size) +
308 ATTRIBUTE_FLAG_OFFSET]);
Kinson Chik66552a82011-03-29 15:59:06 -0700309
Ajay Dudanib01e5062011-12-03 23:23:42 -0800310 memset(&UTF16_name, 0x00, MAX_GPT_NAME_SIZE);
311 memcpy(UTF16_name, &data[(j * partition_entry_size) +
312 PARTITION_NAME_OFFSET],
313 MAX_GPT_NAME_SIZE);
314 /*
315 * Currently partition names in *.xml are UTF-8 and lowercase
316 * Only supporting english for now so removing 2nd byte of UTF-16
317 */
318 for (n = 0; n < MAX_GPT_NAME_SIZE / 2; n++) {
319 partition_entries[partition_count].name[n] =
320 UTF16_name[n * 2];
321 }
322 partition_count++;
323 }
324 }
325 return ret;
Kinson Chik66552a82011-03-29 15:59:06 -0700326}
327
Neeti Desai5f26aff2011-09-30 10:27:40 -0700328static unsigned int write_mbr_in_blocks(unsigned size, unsigned char *mbrImage)
329{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800330 unsigned int dtype;
331 unsigned int dfirstsec;
332 unsigned int ebrSectorOffset;
333 unsigned char *ebrImage;
334 unsigned char *lastAddress;
335 int idx, i;
336 unsigned int ret;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700337
Ajay Dudanib01e5062011-12-03 23:23:42 -0800338 /* Write the first block */
339 ret = mmc_write(0, BLOCK_SIZE, (unsigned int *)mbrImage);
340 if (ret) {
341 dprintf(CRITICAL, "Failed to write mbr partition\n");
342 goto end;
343 }
344 dprintf(SPEW, "write of first MBR block ok\n");
345 /*
346 Loop through the MBR table to see if there is an EBR.
347 If found, then figure out where to write the first EBR
348 */
349 idx = TABLE_ENTRY_0;
350 for (i = 0; i < 4; i++) {
351 dtype = mbrImage[idx + i * TABLE_ENTRY_SIZE + OFFSET_TYPE];
352 if (MBR_EBR_TYPE == dtype) {
353 dprintf(SPEW, "EBR found.\n");
354 break;
355 }
356 }
357 if (MBR_EBR_TYPE != dtype) {
358 dprintf(SPEW, "No EBR in this image\n");
359 goto end;
360 }
361 /* EBR exists. Write each EBR block to mmc */
362 ebrImage = mbrImage + BLOCK_SIZE;
363 ebrSectorOffset =
364 GET_LWORD_FROM_BYTE(&mbrImage
365 [idx + i * TABLE_ENTRY_SIZE +
366 OFFSET_FIRST_SEC]);
367 dfirstsec = 0;
368 dprintf(SPEW, "first EBR to be written at sector 0x%X\n", dfirstsec);
369 lastAddress = mbrImage + size;
370 while (ebrImage < lastAddress) {
371 dprintf(SPEW, "writing to 0x%X\n",
372 (ebrSectorOffset + dfirstsec) * BLOCK_SIZE);
373 ret =
374 mmc_write((ebrSectorOffset + dfirstsec) * BLOCK_SIZE,
375 BLOCK_SIZE, (unsigned int *)ebrImage);
376 if (ret) {
377 dprintf(CRITICAL,
378 "Failed to write EBR block to sector 0x%X\n",
379 dfirstsec);
380 goto end;
381 }
382 dfirstsec =
383 GET_LWORD_FROM_BYTE(&ebrImage
384 [TABLE_ENTRY_1 + OFFSET_FIRST_SEC]);
385 ebrImage += BLOCK_SIZE;
386 }
387 dprintf(INFO, "MBR written to mmc successfully\n");
388 end:
389 return ret;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700390}
391
392/* Write the MBR/EBR to the MMC. */
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700393static unsigned int write_mbr(unsigned size, unsigned char *mbrImage)
Neeti Desai5f26aff2011-09-30 10:27:40 -0700394{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800395 unsigned int ret;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700396
Ajay Dudanib01e5062011-12-03 23:23:42 -0800397 /* Verify that passed in block is a valid MBR */
398 ret = partition_verify_mbr_signature(size, mbrImage);
399 if (ret) {
400 goto end;
401 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700402
Ajay Dudanib01e5062011-12-03 23:23:42 -0800403 /* Write the MBR/EBR to mmc */
404 ret = write_mbr_in_blocks(size, mbrImage);
405 if (ret) {
406 dprintf(CRITICAL, "Failed to write MBR block to mmc.\n");
407 goto end;
408 }
409 /* Re-read the MBR partition into mbr table */
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700410 ret = mmc_boot_read_mbr();
Ajay Dudanib01e5062011-12-03 23:23:42 -0800411 if (ret) {
412 dprintf(CRITICAL, "Failed to re-read mbr partition.\n");
413 goto end;
414 }
415 partition_dump();
416 end:
417 return ret;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700418}
419
420/*
421 * A8h reflected is 15h, i.e. 10101000 <--> 00010101
422*/
423int reflect(int data, int len)
424{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800425 int ref = 0;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700426
Ajay Dudanib01e5062011-12-03 23:23:42 -0800427 for (int i = 0; i < len; i++) {
428 if (data & 0x1) {
429 ref |= (1 << ((len - 1) - i));
430 }
431 data = (data >> 1);
432 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700433
Ajay Dudanib01e5062011-12-03 23:23:42 -0800434 return ref;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700435}
436
437/*
438* Function to calculate the CRC32
439*/
440unsigned int calculate_crc32(unsigned char *buffer, int len)
441{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800442 int byte_length = 8; /*length of unit (i.e. byte) */
443 int msb = 0;
444 int polynomial = 0x104C11DB7; /* IEEE 32bit polynomial */
445 unsigned int regs = 0xFFFFFFFF; /* init to all ones */
446 int regs_mask = 0xFFFFFFFF; /* ensure only 32 bit answer */
447 int regs_msb = 0;
448 unsigned int reflected_regs;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700449
Ajay Dudanib01e5062011-12-03 23:23:42 -0800450 for (int i = 0; i < len; i++) {
451 int data_byte = buffer[i];
452 data_byte = reflect(data_byte, 8);
453 for (int j = 0; j < byte_length; j++) {
454 msb = data_byte >> (byte_length - 1); /* get MSB */
455 msb &= 1; /* ensure just 1 bit */
456 regs_msb = (regs >> 31) & 1; /* MSB of regs */
457 regs = regs << 1; /* shift regs for CRC-CCITT */
458 if (regs_msb ^ msb) { /* MSB is a 1 */
459 regs = regs ^ polynomial; /* XOR with generator poly */
460 }
461 regs = regs & regs_mask; /* Mask off excess upper bits */
462 data_byte <<= 1; /* get to next bit */
463 }
464 }
465 regs = regs & regs_mask;
466 reflected_regs = reflect(regs, 32) ^ 0xFFFFFFFF;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700467
Ajay Dudanib01e5062011-12-03 23:23:42 -0800468 return reflected_regs;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700469}
470
471/*
472 * Write the GPT Partition Entry Array to the MMC.
473 */
Ajay Dudanib01e5062011-12-03 23:23:42 -0800474static unsigned int
475write_gpt_partition_array(unsigned char *header,
476 unsigned int partition_array_start,
477 unsigned int array_size)
Neeti Desai5f26aff2011-09-30 10:27:40 -0700478{
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700479 unsigned int ret = 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800480 unsigned long long partition_entry_lba;
481 unsigned long long partition_entry_array_start_location;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700482
Ajay Dudanib01e5062011-12-03 23:23:42 -0800483 partition_entry_lba =
484 GET_LLWORD_FROM_BYTE(&header[PARTITION_ENTRIES_OFFSET]);
485 partition_entry_array_start_location = partition_entry_lba * BLOCK_SIZE;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700486
Ajay Dudanib01e5062011-12-03 23:23:42 -0800487 ret = mmc_write(partition_entry_array_start_location, array_size,
488 (unsigned int *)partition_array_start);
489 if (ret) {
490 dprintf(CRITICAL,
491 "GPT: FAILED to write the partition entry array\n");
492 goto end;
493 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700494
Ajay Dudanib01e5062011-12-03 23:23:42 -0800495 end:
496 return ret;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700497}
498
Ajay Dudanib01e5062011-12-03 23:23:42 -0800499static void
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700500patch_gpt(unsigned char *gptImage, uint64_t density, unsigned int array_size,
501 unsigned int max_part_count, unsigned int part_entry_size)
Neeti Desai5f26aff2011-09-30 10:27:40 -0700502{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800503 unsigned int partition_entry_array_start;
504 unsigned char *primary_gpt_header;
505 unsigned char *secondary_gpt_header;
506 unsigned int offset;
507 unsigned long long card_size_sec;
508 int total_part = 0;
509 unsigned int last_part_offset;
510 unsigned int crc_value;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700511
Ajay Dudanib01e5062011-12-03 23:23:42 -0800512 /* Get size of MMC */
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700513 card_size_sec = (density) / 512;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800514 /* Working around cap at 4GB */
515 if (card_size_sec == 0) {
516 card_size_sec = 4 * 1024 * 1024 * 2 - 1;
517 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700518
Ajay Dudanib01e5062011-12-03 23:23:42 -0800519 /* Patching primary header */
520 primary_gpt_header = (gptImage + PROTECTIVE_MBR_SIZE);
521 PUT_LONG_LONG(primary_gpt_header + BACKUP_HEADER_OFFSET,
522 ((long long)(card_size_sec - 1)));
523 PUT_LONG_LONG(primary_gpt_header + LAST_USABLE_LBA_OFFSET,
524 ((long long)(card_size_sec - 34)));
Neeti Desai5f26aff2011-09-30 10:27:40 -0700525
Ajay Dudanib01e5062011-12-03 23:23:42 -0800526 /* Patching backup GPT */
527 offset = (2 * array_size);
528 secondary_gpt_header = offset + BLOCK_SIZE + primary_gpt_header;
529 PUT_LONG_LONG(secondary_gpt_header + PRIMARY_HEADER_OFFSET,
530 ((long long)(card_size_sec - 1)));
531 PUT_LONG_LONG(secondary_gpt_header + LAST_USABLE_LBA_OFFSET,
532 ((long long)(card_size_sec - 34)));
533 PUT_LONG_LONG(secondary_gpt_header + PARTITION_ENTRIES_OFFSET,
534 ((long long)(card_size_sec - 33)));
Neeti Desai5f26aff2011-09-30 10:27:40 -0700535
Ajay Dudanib01e5062011-12-03 23:23:42 -0800536 /* Find last partition */
537 while (*(primary_gpt_header + BLOCK_SIZE + total_part * ENTRY_SIZE) !=
538 0) {
539 total_part++;
540 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700541
Ajay Dudanib01e5062011-12-03 23:23:42 -0800542 /* Patching last partition */
543 last_part_offset =
544 (total_part - 1) * ENTRY_SIZE + PARTITION_ENTRY_LAST_LBA;
545 PUT_LONG_LONG(primary_gpt_header + BLOCK_SIZE + last_part_offset,
546 (long long)(card_size_sec - 34));
547 PUT_LONG_LONG(primary_gpt_header + BLOCK_SIZE + last_part_offset +
548 array_size, (long long)(card_size_sec - 34));
Neeti Desai5f26aff2011-09-30 10:27:40 -0700549
Ajay Dudanib01e5062011-12-03 23:23:42 -0800550 /* Updating CRC of the Partition entry array in both headers */
551 partition_entry_array_start = primary_gpt_header + BLOCK_SIZE;
552 crc_value = calculate_crc32(partition_entry_array_start,
553 max_part_count * part_entry_size);
554 PUT_LONG(primary_gpt_header + PARTITION_CRC_OFFSET, crc_value);
Neeti Desai5f26aff2011-09-30 10:27:40 -0700555
Ajay Dudanib01e5062011-12-03 23:23:42 -0800556 crc_value = calculate_crc32(partition_entry_array_start + array_size,
557 max_part_count * part_entry_size);
558 PUT_LONG(secondary_gpt_header + PARTITION_CRC_OFFSET, crc_value);
Neeti Desai5f26aff2011-09-30 10:27:40 -0700559
Ajay Dudanib01e5062011-12-03 23:23:42 -0800560 /* Clearing CRC fields to calculate */
561 PUT_LONG(primary_gpt_header + HEADER_CRC_OFFSET, 0);
562 crc_value = calculate_crc32(primary_gpt_header, 92);
563 PUT_LONG(primary_gpt_header + HEADER_CRC_OFFSET, crc_value);
Neeti Desai5f26aff2011-09-30 10:27:40 -0700564
Ajay Dudanib01e5062011-12-03 23:23:42 -0800565 PUT_LONG(secondary_gpt_header + HEADER_CRC_OFFSET, 0);
566 crc_value = (calculate_crc32(secondary_gpt_header, 92));
567 PUT_LONG(secondary_gpt_header + HEADER_CRC_OFFSET, crc_value);
Neeti Desai5f26aff2011-09-30 10:27:40 -0700568
569}
570
571/*
572 * Write the GPT to the MMC.
573 */
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700574static unsigned int write_gpt(unsigned size, unsigned char *gptImage)
Neeti Desai5f26aff2011-09-30 10:27:40 -0700575{
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700576 unsigned int ret = 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800577 unsigned int header_size;
578 unsigned long long first_usable_lba;
579 unsigned long long backup_header_lba;
580 unsigned int max_partition_count = 0;
581 unsigned int partition_entry_size;
582 unsigned int partition_entry_array_start;
583 unsigned char *primary_gpt_header;
584 unsigned char *secondary_gpt_header;
585 unsigned int offset;
586 unsigned int partition_entry_array_size;
587 unsigned long long primary_header_location; /* address on the emmc card */
588 unsigned long long secondary_header_location; /* address on the emmc card */
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700589 uint64_t device_density;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700590
Ajay Dudanib01e5062011-12-03 23:23:42 -0800591 /* Verify that passed block has a valid GPT primary header */
592 primary_gpt_header = (gptImage + PROTECTIVE_MBR_SIZE);
593 ret = partition_parse_gpt_header(primary_gpt_header, &first_usable_lba,
594 &partition_entry_size, &header_size,
595 &max_partition_count);
596 if (ret) {
597 dprintf(CRITICAL,
598 "GPT: Primary signature invalid cannot write GPT\n");
599 goto end;
600 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700601
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700602 /* Get the density of the mmc device */
603
604 device_density = mmc_get_device_capacity();
605
Ajay Dudanib01e5062011-12-03 23:23:42 -0800606 /* Verify that passed block has a valid backup GPT HEADER */
607 partition_entry_array_size = partition_entry_size * max_partition_count;
608 if (partition_entry_array_size < MIN_PARTITION_ARRAY_SIZE) {
609 partition_entry_array_size = MIN_PARTITION_ARRAY_SIZE;
610 }
611 offset = (2 * partition_entry_array_size);
612 secondary_gpt_header = offset + BLOCK_SIZE + primary_gpt_header;
613 ret =
614 partition_parse_gpt_header(secondary_gpt_header, &first_usable_lba,
615 &partition_entry_size, &header_size,
616 &max_partition_count);
617 if (ret) {
618 dprintf(CRITICAL,
619 "GPT: Backup signature invalid cannot write GPT\n");
620 goto end;
621 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700622
Ajay Dudanib01e5062011-12-03 23:23:42 -0800623 /* Patching the primary and the backup header of the GPT table */
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700624 patch_gpt(gptImage, device_density, partition_entry_array_size,
Ajay Dudanib01e5062011-12-03 23:23:42 -0800625 max_partition_count, partition_entry_size);
Neeti Desai5f26aff2011-09-30 10:27:40 -0700626
Ajay Dudanib01e5062011-12-03 23:23:42 -0800627 /* Erasing the eMMC card before writing */
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700628 ret = mmc_erase_card(0x00000000, device_density);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800629 if (ret) {
630 dprintf(CRITICAL, "Failed to erase the eMMC card\n");
631 goto end;
632 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700633
Ajay Dudanib01e5062011-12-03 23:23:42 -0800634 /* Writing protective MBR */
635 ret = mmc_write(0, PROTECTIVE_MBR_SIZE, (unsigned int *)gptImage);
636 if (ret) {
637 dprintf(CRITICAL, "Failed to write Protective MBR\n");
638 goto end;
639 }
640 /* Writing the primary GPT header */
641 primary_header_location = PROTECTIVE_MBR_SIZE;
642 ret = mmc_write(primary_header_location, BLOCK_SIZE,
643 (unsigned int *)primary_gpt_header);
644 if (ret) {
645 dprintf(CRITICAL, "Failed to write GPT header\n");
646 goto end;
647 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700648
Ajay Dudanib01e5062011-12-03 23:23:42 -0800649 /* Writing the backup GPT header */
650 backup_header_lba = GET_LLWORD_FROM_BYTE
651 (&primary_gpt_header[BACKUP_HEADER_OFFSET]);
652 secondary_header_location = backup_header_lba * BLOCK_SIZE;
653 ret = mmc_write(secondary_header_location, BLOCK_SIZE,
654 (unsigned int *)secondary_gpt_header);
655 if (ret) {
656 dprintf(CRITICAL, "Failed to write GPT backup header\n");
657 goto end;
658 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700659
Ajay Dudanib01e5062011-12-03 23:23:42 -0800660 /* Writing the partition entries array for the primary header */
661 partition_entry_array_start = primary_gpt_header + BLOCK_SIZE;
662 ret = write_gpt_partition_array(primary_gpt_header,
663 partition_entry_array_start,
664 partition_entry_array_size);
665 if (ret) {
666 dprintf(CRITICAL,
667 "GPT: Could not write GPT Partition entries array\n");
668 goto end;
669 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700670
Ajay Dudanib01e5062011-12-03 23:23:42 -0800671 /*Writing the partition entries array for the backup header */
672 partition_entry_array_start = primary_gpt_header + BLOCK_SIZE +
673 partition_entry_array_size;
674 ret = write_gpt_partition_array(secondary_gpt_header,
675 partition_entry_array_start,
676 partition_entry_array_size);
677 if (ret) {
678 dprintf(CRITICAL,
679 "GPT: Could not write GPT Partition entries array\n");
680 goto end;
681 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700682
Ajay Dudanib01e5062011-12-03 23:23:42 -0800683 /* Re-read the GPT partition table */
684 dprintf(INFO, "Re-reading the GPT Partition Table\n");
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700685 ret = mmc_boot_read_gpt();
Ajay Dudanib01e5062011-12-03 23:23:42 -0800686 if (ret) {
687 dprintf(CRITICAL,
688 "GPT: Failure to re- read the GPT Partition table\n");
689 goto end;
690 }
691 partition_dump();
692 dprintf(CRITICAL, "GPT: Partition Table written\n");
693 memset(primary_gpt_header, 0x00, size);
Neeti Desai5f26aff2011-09-30 10:27:40 -0700694
Ajay Dudanib01e5062011-12-03 23:23:42 -0800695 end:
696 return ret;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700697}
698
Ajay Dudanib01e5062011-12-03 23:23:42 -0800699unsigned int write_partition(unsigned size, unsigned char *partition)
Neeti Desai5f26aff2011-09-30 10:27:40 -0700700{
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700701 unsigned int ret = 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800702 unsigned int partition_type;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700703
Ajay Dudanib01e5062011-12-03 23:23:42 -0800704 if (partition == 0) {
705 dprintf(CRITICAL, "NULL partition\n");
706 goto end;
707 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700708
Ajay Dudanib01e5062011-12-03 23:23:42 -0800709 ret = partition_get_type(size, partition, &partition_type);
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700710 if (ret)
Ajay Dudanib01e5062011-12-03 23:23:42 -0800711 goto end;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700712
Ajay Dudanib01e5062011-12-03 23:23:42 -0800713 switch (partition_type) {
714 case PARTITION_TYPE_MBR:
715 dprintf(INFO, "Writing MBR partition\n");
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700716 ret = write_mbr(size, partition);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800717 break;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700718
Ajay Dudanib01e5062011-12-03 23:23:42 -0800719 case PARTITION_TYPE_GPT:
720 dprintf(INFO, "Writing GPT partition\n");
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700721 ret = write_gpt(size, partition);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800722 dprintf(CRITICAL, "Re-Flash all the partitions\n");
723 break;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700724
Ajay Dudanib01e5062011-12-03 23:23:42 -0800725 default:
726 dprintf(CRITICAL, "Invalid partition\n");
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700727 ret = 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800728 goto end;
729 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700730
Ajay Dudanib01e5062011-12-03 23:23:42 -0800731 end:
732 return ret;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700733}
Ajay Dudanib01e5062011-12-03 23:23:42 -0800734
Kinson Chikf1a43512011-07-14 11:28:39 -0700735/*
736 * Fill name for android partition found.
737 */
Ajay Dudanib01e5062011-12-03 23:23:42 -0800738static void
739mbr_fill_name(struct partition_entry *partition_ent, unsigned int type)
Kinson Chikf1a43512011-07-14 11:28:39 -0700740{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800741 switch (type) {
742 memset(partition_ent->name, 0, MAX_GPT_NAME_SIZE);
743 case MBR_MODEM_TYPE:
744 case MBR_MODEM_TYPE2:
745 /* if already assigned last name available then return */
746 if (!strcmp((const char *)vfat_partitions[vfat_count], "NONE"))
747 return;
748 strlcpy((char *)partition_ent->name,
749 (const char *)vfat_partitions[vfat_count],
750 sizeof(partition_ent->name));
751 vfat_count++;
752 break;
753 case MBR_SBL1_TYPE:
754 memcpy(partition_ent->name, "sbl1", 4);
755 break;
756 case MBR_SBL2_TYPE:
757 memcpy(partition_ent->name, "sbl2", 4);
758 break;
759 case MBR_SBL3_TYPE:
760 memcpy(partition_ent->name, "sbl3", 4);
761 break;
762 case MBR_RPM_TYPE:
763 memcpy(partition_ent->name, "rpm", 3);
764 break;
765 case MBR_TZ_TYPE:
766 memcpy(partition_ent->name, "tz", 2);
767 break;
768 case MBR_ABOOT_TYPE:
Channagoud Kadabi16f50952012-04-02 16:24:07 +0530769#if PLATFORM_MSM7X27A
770 memcpy(partition_ent->name, "FOTA", 4);
771#else
Ajay Dudanib01e5062011-12-03 23:23:42 -0800772 memcpy(partition_ent->name, "aboot", 5);
Channagoud Kadabi16f50952012-04-02 16:24:07 +0530773#endif
Ajay Dudanib01e5062011-12-03 23:23:42 -0800774 break;
775 case MBR_BOOT_TYPE:
776 memcpy(partition_ent->name, "boot", 4);
777 break;
778 case MBR_MODEM_ST1_TYPE:
779 memcpy(partition_ent->name, "modem_st1", 9);
780 break;
781 case MBR_MODEM_ST2_TYPE:
782 memcpy(partition_ent->name, "modem_st2", 9);
783 break;
784 case MBR_EFS2_TYPE:
785 memcpy(partition_ent->name, "efs2", 4);
786 break;
787 case MBR_USERDATA_TYPE:
788 if (ext3_count == sizeof(ext3_partitions) / sizeof(char *))
789 return;
790 strlcpy((char *)partition_ent->name,
791 (const char *)ext3_partitions[ext3_count],
792 sizeof(partition_ent->name));
793 ext3_count++;
794 break;
795 case MBR_RECOVERY_TYPE:
796 memcpy(partition_ent->name, "recovery", 8);
797 break;
798 case MBR_MISC_TYPE:
799 memcpy(partition_ent->name, "misc", 4);
800 break;
Neeti Desai2d6b0e42012-03-23 15:48:57 -0700801 case MBR_SSD_TYPE:
802 memcpy(partition_ent->name, "ssd", 3);
803 break;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800804 };
Kinson Chikf1a43512011-07-14 11:28:39 -0700805}
806
807/*
808 * Find index of parition in array of partition entries
809 */
Pavel Nedev285ad922013-04-26 10:39:19 +0300810int partition_get_index(const char *name)
Kinson Chikf1a43512011-07-14 11:28:39 -0700811{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800812 unsigned int input_string_length = strlen(name);
813 unsigned n;
Kinson Chik66552a82011-03-29 15:59:06 -0700814
neetid6c38de12011-12-02 12:04:50 -0800815 if( partition_count >= NUM_PARTITIONS)
816 {
817 return INVALID_PTN;
818 }
Ajay Dudanib01e5062011-12-03 23:23:42 -0800819 for (n = 0; n < partition_count; n++) {
820 if (!memcmp
821 (name, &partition_entries[n].name, input_string_length)
822 && input_string_length ==
823 strlen((const char *)&partition_entries[n].name)) {
824 return n;
825 }
826 }
827 return INVALID_PTN;
Kinson Chikf1a43512011-07-14 11:28:39 -0700828}
829
830/* Get size of the partition */
Ajay Dudanib01e5062011-12-03 23:23:42 -0800831unsigned long long partition_get_size(int index)
Kinson Chikf1a43512011-07-14 11:28:39 -0700832{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800833 if (index == INVALID_PTN)
834 return 0;
835 else {
836 return partition_entries[index].size * BLOCK_SIZE;
837 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700838}
839
840/* Get offset of the partition */
Ajay Dudanib01e5062011-12-03 23:23:42 -0800841unsigned long long partition_get_offset(int index)
Kinson Chikf1a43512011-07-14 11:28:39 -0700842{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800843 if (index == INVALID_PTN)
844 return 0;
845 else {
846 return partition_entries[index].first_lba * BLOCK_SIZE;
847 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700848}
849
850/* Debug: Print all parsed partitions */
851void partition_dump()
852{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800853 unsigned i = 0;
854 for (i = 0; i < partition_count; i++) {
855 dprintf(SPEW,
856 "ptn[%d]:Name[%s] Size[%llu] Type[%u] First[%llu] Last[%llu]\n",
857 i, partition_entries[i].name, partition_entries[i].size,
858 partition_entries[i].dtype,
859 partition_entries[i].first_lba,
860 partition_entries[i].last_lba);
861 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700862}
863
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700864static unsigned int
Ajay Dudanib01e5062011-12-03 23:23:42 -0800865partition_verify_mbr_signature(unsigned size, unsigned char *buffer)
Kinson Chikf1a43512011-07-14 11:28:39 -0700866{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800867 /* Avoid checking past end of buffer */
868 if ((TABLE_SIGNATURE + 1) > size) {
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700869 return 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800870 }
871 /* Check to see if signature exists */
872 if ((buffer[TABLE_SIGNATURE] != MMC_MBR_SIGNATURE_BYTE_0) ||
873 (buffer[TABLE_SIGNATURE + 1] != MMC_MBR_SIGNATURE_BYTE_1)) {
874 dprintf(CRITICAL, "MBR signature does not match.\n");
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700875 return 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800876 }
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700877 return 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700878}
879
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700880static unsigned int
Ajay Dudanib01e5062011-12-03 23:23:42 -0800881mbr_partition_get_type(unsigned size, unsigned char *partition,
882 unsigned int *partition_type)
Kinson Chikf1a43512011-07-14 11:28:39 -0700883{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800884 unsigned int type_offset = TABLE_ENTRY_0 + OFFSET_TYPE;
Kinson Chikf1a43512011-07-14 11:28:39 -0700885
Ajay Dudanib01e5062011-12-03 23:23:42 -0800886 if (size < type_offset) {
887 goto end;
888 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700889
Ajay Dudanib01e5062011-12-03 23:23:42 -0800890 *partition_type = partition[type_offset];
891 end:
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700892 return 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700893}
894
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700895static unsigned int
Ajay Dudanib01e5062011-12-03 23:23:42 -0800896partition_get_type(unsigned size, unsigned char *partition,
897 unsigned int *partition_type)
Kinson Chikf1a43512011-07-14 11:28:39 -0700898{
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700899 unsigned int ret = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700900
Ajay Dudanib01e5062011-12-03 23:23:42 -0800901 /*
902 * If the block contains the MBR signature, then it's likely either
903 * MBR or MBR with protective type (GPT). If the MBR signature is
904 * not there, then it could be the GPT backup.
905 */
Kinson Chikf1a43512011-07-14 11:28:39 -0700906
Ajay Dudanib01e5062011-12-03 23:23:42 -0800907 /* First check the MBR signature */
908 ret = partition_verify_mbr_signature(size, partition);
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700909 if (!ret) {
Ajay Dudanib01e5062011-12-03 23:23:42 -0800910 unsigned int mbr_partition_type = PARTITION_TYPE_MBR;
Kinson Chikf1a43512011-07-14 11:28:39 -0700911
Ajay Dudanib01e5062011-12-03 23:23:42 -0800912 /* MBR signature verified. This could be MBR, MBR + EBR, or GPT */
913 ret =
914 mbr_partition_get_type(size, partition,
915 &mbr_partition_type);
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700916 if (ret) {
Ajay Dudanib01e5062011-12-03 23:23:42 -0800917 dprintf(CRITICAL, "Cannot get TYPE of partition");
918 } else if (MBR_PROTECTED_TYPE == mbr_partition_type) {
919 *partition_type = PARTITION_TYPE_GPT;
920 } else {
921 *partition_type = PARTITION_TYPE_MBR;
922 }
923 } else {
924 /*
925 * This could be the GPT backup. Make that assumption for now.
926 * Anybody who treats the block as GPT backup should check the
927 * signature.
928 */
929 *partition_type = PARTITION_TYPE_GPT_BACKUP;
930 }
931 return ret;
Kinson Chik66552a82011-03-29 15:59:06 -0700932}
Kinson Chik4d7444f2011-09-13 15:48:51 -0700933
934/*
935 * Parse the gpt header and get the required header fields
936 * Return 0 on valid signature
937 */
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700938static unsigned int
Ajay Dudanib01e5062011-12-03 23:23:42 -0800939partition_parse_gpt_header(unsigned char *buffer,
940 unsigned long long *first_usable_lba,
941 unsigned int *partition_entry_size,
942 unsigned int *header_size,
943 unsigned int *max_partition_count)
Kinson Chik4d7444f2011-09-13 15:48:51 -0700944{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800945 /* Check GPT Signature */
946 if (((uint32_t *) buffer)[0] != GPT_SIGNATURE_2 ||
947 ((uint32_t *) buffer)[1] != GPT_SIGNATURE_1)
948 return 1;
Kinson Chik4d7444f2011-09-13 15:48:51 -0700949
Ajay Dudanib01e5062011-12-03 23:23:42 -0800950 *header_size = GET_LWORD_FROM_BYTE(&buffer[HEADER_SIZE_OFFSET]);
951 *first_usable_lba =
952 GET_LLWORD_FROM_BYTE(&buffer[FIRST_USABLE_LBA_OFFSET]);
953 *max_partition_count =
954 GET_LWORD_FROM_BYTE(&buffer[PARTITION_COUNT_OFFSET]);
955 *partition_entry_size =
956 GET_LWORD_FROM_BYTE(&buffer[PENTRY_SIZE_OFFSET]);
Kinson Chik4d7444f2011-09-13 15:48:51 -0700957
Ajay Dudanib01e5062011-12-03 23:23:42 -0800958 return 0;
Kinson Chik4d7444f2011-09-13 15:48:51 -0700959}
Pavel Nedevb5ba62d2013-07-22 11:57:41 +0300960
961bool partition_gpt_exists()
962{
963 return (gpt_partitions_exist != 0);
964}