blob: 1b554ad7bef547f2c47ae6f4b8b687054d829316 [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];
62unsigned gpt_partitions_exist = 0;
63unsigned 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{
Ajay Dudanib01e5062011-12-03 23:23:42 -080092 unsigned char buffer[BLOCK_SIZE];
93 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{
Kinson Chik66552a82011-03-29 15:59:06 -0700205
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;
213 unsigned char data[BLOCK_SIZE];
214 unsigned int i = 0; /* Counter for each 512 block */
215 unsigned int j = 0; /* Counter for each 128 entry in the 512 block */
216 unsigned int n = 0; /* Counter for UTF-16 -> 8 conversion */
217 unsigned char UTF16_name[MAX_GPT_NAME_SIZE];
218 /* LBA of first partition -- 1 Block after Protected MBR + 1 for PT */
Neeti Desaice6ad9a2012-03-21 18:57:59 -0700219 unsigned long long partition_0;
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700220 uint64_t device_density;
221
Ajay Dudanib01e5062011-12-03 23:23:42 -0800222 partition_count = 0;
Kinson Chik66552a82011-03-29 15:59:06 -0700223
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700224 /* Get the density of the mmc device */
225
226 device_density = mmc_get_device_capacity();
227
Ajay Dudanib01e5062011-12-03 23:23:42 -0800228 /* Print out the GPT first */
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700229 ret = mmc_read(PROTECTIVE_MBR_SIZE, (unsigned int *)data, BLOCK_SIZE);
230 if (ret)
Ajay Dudanib01e5062011-12-03 23:23:42 -0800231 dprintf(CRITICAL, "GPT: Could not read primary gpt from mmc\n");
Kinson Chik66552a82011-03-29 15:59:06 -0700232
Ajay Dudanib01e5062011-12-03 23:23:42 -0800233 ret = partition_parse_gpt_header(data, &first_usable_lba,
234 &partition_entry_size, &header_size,
235 &max_partition_count);
236 if (ret) {
237 dprintf(INFO, "GPT: (WARNING) Primary signature invalid\n");
Kinson Chik4d7444f2011-09-13 15:48:51 -0700238
Ajay Dudanib01e5062011-12-03 23:23:42 -0800239 /* Check the backup gpt */
Neeti Desaice6ad9a2012-03-21 18:57:59 -0700240
241 /* Get size of MMC */
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700242 card_size_sec = (device_density) / BLOCK_SIZE;
Neeti Desaice6ad9a2012-03-21 18:57:59 -0700243 ASSERT (card_size_sec > 0);
244
245 backup_header_lba = card_size_sec - 1;
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700246 ret = mmc_read((backup_header_lba * BLOCK_SIZE), (unsigned int *)data,
247 BLOCK_SIZE);
Kinson Chik4d7444f2011-09-13 15:48:51 -0700248
Ajay Dudanib01e5062011-12-03 23:23:42 -0800249 if (ret) {
250 dprintf(CRITICAL,
251 "GPT: Could not read backup gpt from mmc\n");
252 return ret;
253 }
Kinson Chik4d7444f2011-09-13 15:48:51 -0700254
Ajay Dudanib01e5062011-12-03 23:23:42 -0800255 ret = partition_parse_gpt_header(data, &first_usable_lba,
256 &partition_entry_size,
257 &header_size,
258 &max_partition_count);
259 if (ret) {
260 dprintf(CRITICAL,
261 "GPT: Primary and backup signatures invalid\n");
262 return ret;
263 }
Ajay Dudanib01e5062011-12-03 23:23:42 -0800264 }
Neeti Desaice6ad9a2012-03-21 18:57:59 -0700265 partition_0 = GET_LLWORD_FROM_BYTE(&data[PARTITION_ENTRIES_OFFSET]);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800266 /* Read GPT Entries */
Channagoud Kadabi82a482f2013-02-14 21:04:38 -0800267 for (i = 0; i < (ROUNDUP(max_partition_count, 4)) / 4; i++) {
neetid6c38de12011-12-02 12:04:50 -0800268 ASSERT(partition_count < NUM_PARTITIONS);
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700269 ret = mmc_read((partition_0 * BLOCK_SIZE) + (i * BLOCK_SIZE),
270 (uint32_t *) data, BLOCK_SIZE);
Kinson Chik66552a82011-03-29 15:59:06 -0700271
Ajay Dudanib01e5062011-12-03 23:23:42 -0800272 if (ret) {
273 dprintf(CRITICAL,
274 "GPT: mmc read card failed reading partition entries.\n");
275 return ret;
276 }
Kinson Chik66552a82011-03-29 15:59:06 -0700277
Ajay Dudanib01e5062011-12-03 23:23:42 -0800278 for (j = 0; j < 4; j++) {
279 memcpy(&(partition_entries[partition_count].type_guid),
280 &data[(j * partition_entry_size)],
281 PARTITION_TYPE_GUID_SIZE);
282 if (partition_entries[partition_count].type_guid[0] ==
283 0x00
284 && partition_entries[partition_count].
285 type_guid[1] == 0x00) {
Channagoud Kadabif73aa292013-01-31 16:55:20 -0800286 i = ROUNDUP(max_partition_count, 4);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800287 break;
288 }
289 memcpy(&
290 (partition_entries[partition_count].
291 unique_partition_guid),
292 &data[(j * partition_entry_size) +
293 UNIQUE_GUID_OFFSET],
294 UNIQUE_PARTITION_GUID_SIZE);
295 partition_entries[partition_count].first_lba =
296 GET_LLWORD_FROM_BYTE(&data
297 [(j * partition_entry_size) +
298 FIRST_LBA_OFFSET]);
299 partition_entries[partition_count].last_lba =
300 GET_LLWORD_FROM_BYTE(&data
301 [(j * partition_entry_size) +
302 LAST_LBA_OFFSET]);
303 partition_entries[partition_count].size =
304 partition_entries[partition_count].last_lba -
Neeti Desai4b8c1df2012-03-21 13:15:14 -0700305 partition_entries[partition_count].first_lba + 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800306 partition_entries[partition_count].attribute_flag =
307 GET_LLWORD_FROM_BYTE(&data
308 [(j * partition_entry_size) +
309 ATTRIBUTE_FLAG_OFFSET]);
Kinson Chik66552a82011-03-29 15:59:06 -0700310
Ajay Dudanib01e5062011-12-03 23:23:42 -0800311 memset(&UTF16_name, 0x00, MAX_GPT_NAME_SIZE);
312 memcpy(UTF16_name, &data[(j * partition_entry_size) +
313 PARTITION_NAME_OFFSET],
314 MAX_GPT_NAME_SIZE);
315 /*
316 * Currently partition names in *.xml are UTF-8 and lowercase
317 * Only supporting english for now so removing 2nd byte of UTF-16
318 */
319 for (n = 0; n < MAX_GPT_NAME_SIZE / 2; n++) {
320 partition_entries[partition_count].name[n] =
321 UTF16_name[n * 2];
322 }
323 partition_count++;
324 }
325 }
326 return ret;
Kinson Chik66552a82011-03-29 15:59:06 -0700327}
328
Neeti Desai5f26aff2011-09-30 10:27:40 -0700329static unsigned int write_mbr_in_blocks(unsigned size, unsigned char *mbrImage)
330{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800331 unsigned int dtype;
332 unsigned int dfirstsec;
333 unsigned int ebrSectorOffset;
334 unsigned char *ebrImage;
335 unsigned char *lastAddress;
336 int idx, i;
337 unsigned int ret;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700338
Ajay Dudanib01e5062011-12-03 23:23:42 -0800339 /* Write the first block */
340 ret = mmc_write(0, BLOCK_SIZE, (unsigned int *)mbrImage);
341 if (ret) {
342 dprintf(CRITICAL, "Failed to write mbr partition\n");
343 goto end;
344 }
345 dprintf(SPEW, "write of first MBR block ok\n");
346 /*
347 Loop through the MBR table to see if there is an EBR.
348 If found, then figure out where to write the first EBR
349 */
350 idx = TABLE_ENTRY_0;
351 for (i = 0; i < 4; i++) {
352 dtype = mbrImage[idx + i * TABLE_ENTRY_SIZE + OFFSET_TYPE];
353 if (MBR_EBR_TYPE == dtype) {
354 dprintf(SPEW, "EBR found.\n");
355 break;
356 }
357 }
358 if (MBR_EBR_TYPE != dtype) {
359 dprintf(SPEW, "No EBR in this image\n");
360 goto end;
361 }
362 /* EBR exists. Write each EBR block to mmc */
363 ebrImage = mbrImage + BLOCK_SIZE;
364 ebrSectorOffset =
365 GET_LWORD_FROM_BYTE(&mbrImage
366 [idx + i * TABLE_ENTRY_SIZE +
367 OFFSET_FIRST_SEC]);
368 dfirstsec = 0;
369 dprintf(SPEW, "first EBR to be written at sector 0x%X\n", dfirstsec);
370 lastAddress = mbrImage + size;
371 while (ebrImage < lastAddress) {
372 dprintf(SPEW, "writing to 0x%X\n",
373 (ebrSectorOffset + dfirstsec) * BLOCK_SIZE);
374 ret =
375 mmc_write((ebrSectorOffset + dfirstsec) * BLOCK_SIZE,
376 BLOCK_SIZE, (unsigned int *)ebrImage);
377 if (ret) {
378 dprintf(CRITICAL,
379 "Failed to write EBR block to sector 0x%X\n",
380 dfirstsec);
381 goto end;
382 }
383 dfirstsec =
384 GET_LWORD_FROM_BYTE(&ebrImage
385 [TABLE_ENTRY_1 + OFFSET_FIRST_SEC]);
386 ebrImage += BLOCK_SIZE;
387 }
388 dprintf(INFO, "MBR written to mmc successfully\n");
389 end:
390 return ret;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700391}
392
393/* Write the MBR/EBR to the MMC. */
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700394static unsigned int write_mbr(unsigned size, unsigned char *mbrImage)
Neeti Desai5f26aff2011-09-30 10:27:40 -0700395{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800396 unsigned int ret;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700397
Ajay Dudanib01e5062011-12-03 23:23:42 -0800398 /* Verify that passed in block is a valid MBR */
399 ret = partition_verify_mbr_signature(size, mbrImage);
400 if (ret) {
401 goto end;
402 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700403
Ajay Dudanib01e5062011-12-03 23:23:42 -0800404 /* Write the MBR/EBR to mmc */
405 ret = write_mbr_in_blocks(size, mbrImage);
406 if (ret) {
407 dprintf(CRITICAL, "Failed to write MBR block to mmc.\n");
408 goto end;
409 }
410 /* Re-read the MBR partition into mbr table */
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700411 ret = mmc_boot_read_mbr();
Ajay Dudanib01e5062011-12-03 23:23:42 -0800412 if (ret) {
413 dprintf(CRITICAL, "Failed to re-read mbr partition.\n");
414 goto end;
415 }
416 partition_dump();
417 end:
418 return ret;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700419}
420
421/*
422 * A8h reflected is 15h, i.e. 10101000 <--> 00010101
423*/
424int reflect(int data, int len)
425{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800426 int ref = 0;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700427
Ajay Dudanib01e5062011-12-03 23:23:42 -0800428 for (int i = 0; i < len; i++) {
429 if (data & 0x1) {
430 ref |= (1 << ((len - 1) - i));
431 }
432 data = (data >> 1);
433 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700434
Ajay Dudanib01e5062011-12-03 23:23:42 -0800435 return ref;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700436}
437
438/*
439* Function to calculate the CRC32
440*/
441unsigned int calculate_crc32(unsigned char *buffer, int len)
442{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800443 int byte_length = 8; /*length of unit (i.e. byte) */
444 int msb = 0;
445 int polynomial = 0x104C11DB7; /* IEEE 32bit polynomial */
446 unsigned int regs = 0xFFFFFFFF; /* init to all ones */
447 int regs_mask = 0xFFFFFFFF; /* ensure only 32 bit answer */
448 int regs_msb = 0;
449 unsigned int reflected_regs;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700450
Ajay Dudanib01e5062011-12-03 23:23:42 -0800451 for (int i = 0; i < len; i++) {
452 int data_byte = buffer[i];
453 data_byte = reflect(data_byte, 8);
454 for (int j = 0; j < byte_length; j++) {
455 msb = data_byte >> (byte_length - 1); /* get MSB */
456 msb &= 1; /* ensure just 1 bit */
457 regs_msb = (regs >> 31) & 1; /* MSB of regs */
458 regs = regs << 1; /* shift regs for CRC-CCITT */
459 if (regs_msb ^ msb) { /* MSB is a 1 */
460 regs = regs ^ polynomial; /* XOR with generator poly */
461 }
462 regs = regs & regs_mask; /* Mask off excess upper bits */
463 data_byte <<= 1; /* get to next bit */
464 }
465 }
466 regs = regs & regs_mask;
467 reflected_regs = reflect(regs, 32) ^ 0xFFFFFFFF;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700468
Ajay Dudanib01e5062011-12-03 23:23:42 -0800469 return reflected_regs;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700470}
471
472/*
473 * Write the GPT Partition Entry Array to the MMC.
474 */
Ajay Dudanib01e5062011-12-03 23:23:42 -0800475static unsigned int
476write_gpt_partition_array(unsigned char *header,
477 unsigned int partition_array_start,
478 unsigned int array_size)
Neeti Desai5f26aff2011-09-30 10:27:40 -0700479{
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700480 unsigned int ret = 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800481 unsigned long long partition_entry_lba;
482 unsigned long long partition_entry_array_start_location;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700483
Ajay Dudanib01e5062011-12-03 23:23:42 -0800484 partition_entry_lba =
485 GET_LLWORD_FROM_BYTE(&header[PARTITION_ENTRIES_OFFSET]);
486 partition_entry_array_start_location = partition_entry_lba * BLOCK_SIZE;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700487
Ajay Dudanib01e5062011-12-03 23:23:42 -0800488 ret = mmc_write(partition_entry_array_start_location, array_size,
489 (unsigned int *)partition_array_start);
490 if (ret) {
491 dprintf(CRITICAL,
492 "GPT: FAILED to write the partition entry array\n");
493 goto end;
494 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700495
Ajay Dudanib01e5062011-12-03 23:23:42 -0800496 end:
497 return ret;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700498}
499
Ajay Dudanib01e5062011-12-03 23:23:42 -0800500static void
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700501patch_gpt(unsigned char *gptImage, uint64_t density, unsigned int array_size,
502 unsigned int max_part_count, unsigned int part_entry_size)
Neeti Desai5f26aff2011-09-30 10:27:40 -0700503{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800504 unsigned int partition_entry_array_start;
505 unsigned char *primary_gpt_header;
506 unsigned char *secondary_gpt_header;
507 unsigned int offset;
508 unsigned long long card_size_sec;
509 int total_part = 0;
510 unsigned int last_part_offset;
511 unsigned int crc_value;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700512
Ajay Dudanib01e5062011-12-03 23:23:42 -0800513 /* Get size of MMC */
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700514 card_size_sec = (density) / 512;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800515 /* Working around cap at 4GB */
516 if (card_size_sec == 0) {
517 card_size_sec = 4 * 1024 * 1024 * 2 - 1;
518 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700519
Ajay Dudanib01e5062011-12-03 23:23:42 -0800520 /* Patching primary header */
521 primary_gpt_header = (gptImage + PROTECTIVE_MBR_SIZE);
522 PUT_LONG_LONG(primary_gpt_header + BACKUP_HEADER_OFFSET,
523 ((long long)(card_size_sec - 1)));
524 PUT_LONG_LONG(primary_gpt_header + LAST_USABLE_LBA_OFFSET,
525 ((long long)(card_size_sec - 34)));
Neeti Desai5f26aff2011-09-30 10:27:40 -0700526
Ajay Dudanib01e5062011-12-03 23:23:42 -0800527 /* Patching backup GPT */
528 offset = (2 * array_size);
529 secondary_gpt_header = offset + BLOCK_SIZE + primary_gpt_header;
530 PUT_LONG_LONG(secondary_gpt_header + PRIMARY_HEADER_OFFSET,
531 ((long long)(card_size_sec - 1)));
532 PUT_LONG_LONG(secondary_gpt_header + LAST_USABLE_LBA_OFFSET,
533 ((long long)(card_size_sec - 34)));
534 PUT_LONG_LONG(secondary_gpt_header + PARTITION_ENTRIES_OFFSET,
535 ((long long)(card_size_sec - 33)));
Neeti Desai5f26aff2011-09-30 10:27:40 -0700536
Ajay Dudanib01e5062011-12-03 23:23:42 -0800537 /* Find last partition */
538 while (*(primary_gpt_header + BLOCK_SIZE + total_part * ENTRY_SIZE) !=
539 0) {
540 total_part++;
541 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700542
Ajay Dudanib01e5062011-12-03 23:23:42 -0800543 /* Patching last partition */
544 last_part_offset =
545 (total_part - 1) * ENTRY_SIZE + PARTITION_ENTRY_LAST_LBA;
546 PUT_LONG_LONG(primary_gpt_header + BLOCK_SIZE + last_part_offset,
547 (long long)(card_size_sec - 34));
548 PUT_LONG_LONG(primary_gpt_header + BLOCK_SIZE + last_part_offset +
549 array_size, (long long)(card_size_sec - 34));
Neeti Desai5f26aff2011-09-30 10:27:40 -0700550
Ajay Dudanib01e5062011-12-03 23:23:42 -0800551 /* Updating CRC of the Partition entry array in both headers */
552 partition_entry_array_start = primary_gpt_header + BLOCK_SIZE;
553 crc_value = calculate_crc32(partition_entry_array_start,
554 max_part_count * part_entry_size);
555 PUT_LONG(primary_gpt_header + PARTITION_CRC_OFFSET, crc_value);
Neeti Desai5f26aff2011-09-30 10:27:40 -0700556
Ajay Dudanib01e5062011-12-03 23:23:42 -0800557 crc_value = calculate_crc32(partition_entry_array_start + array_size,
558 max_part_count * part_entry_size);
559 PUT_LONG(secondary_gpt_header + PARTITION_CRC_OFFSET, crc_value);
Neeti Desai5f26aff2011-09-30 10:27:40 -0700560
Ajay Dudanib01e5062011-12-03 23:23:42 -0800561 /* Clearing CRC fields to calculate */
562 PUT_LONG(primary_gpt_header + HEADER_CRC_OFFSET, 0);
563 crc_value = calculate_crc32(primary_gpt_header, 92);
564 PUT_LONG(primary_gpt_header + HEADER_CRC_OFFSET, crc_value);
Neeti Desai5f26aff2011-09-30 10:27:40 -0700565
Ajay Dudanib01e5062011-12-03 23:23:42 -0800566 PUT_LONG(secondary_gpt_header + HEADER_CRC_OFFSET, 0);
567 crc_value = (calculate_crc32(secondary_gpt_header, 92));
568 PUT_LONG(secondary_gpt_header + HEADER_CRC_OFFSET, crc_value);
Neeti Desai5f26aff2011-09-30 10:27:40 -0700569
570}
571
572/*
573 * Write the GPT to the MMC.
574 */
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700575static unsigned int write_gpt(unsigned size, unsigned char *gptImage)
Neeti Desai5f26aff2011-09-30 10:27:40 -0700576{
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700577 unsigned int ret = 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800578 unsigned int header_size;
579 unsigned long long first_usable_lba;
580 unsigned long long backup_header_lba;
581 unsigned int max_partition_count = 0;
582 unsigned int partition_entry_size;
583 unsigned int partition_entry_array_start;
584 unsigned char *primary_gpt_header;
585 unsigned char *secondary_gpt_header;
586 unsigned int offset;
587 unsigned int partition_entry_array_size;
588 unsigned long long primary_header_location; /* address on the emmc card */
589 unsigned long long secondary_header_location; /* address on the emmc card */
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700590 uint64_t device_density;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700591
Ajay Dudanib01e5062011-12-03 23:23:42 -0800592 /* Verify that passed block has a valid GPT primary header */
593 primary_gpt_header = (gptImage + PROTECTIVE_MBR_SIZE);
594 ret = partition_parse_gpt_header(primary_gpt_header, &first_usable_lba,
595 &partition_entry_size, &header_size,
596 &max_partition_count);
597 if (ret) {
598 dprintf(CRITICAL,
599 "GPT: Primary signature invalid cannot write GPT\n");
600 goto end;
601 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700602
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700603 /* Get the density of the mmc device */
604
605 device_density = mmc_get_device_capacity();
606
Ajay Dudanib01e5062011-12-03 23:23:42 -0800607 /* Verify that passed block has a valid backup GPT HEADER */
608 partition_entry_array_size = partition_entry_size * max_partition_count;
609 if (partition_entry_array_size < MIN_PARTITION_ARRAY_SIZE) {
610 partition_entry_array_size = MIN_PARTITION_ARRAY_SIZE;
611 }
612 offset = (2 * partition_entry_array_size);
613 secondary_gpt_header = offset + BLOCK_SIZE + primary_gpt_header;
614 ret =
615 partition_parse_gpt_header(secondary_gpt_header, &first_usable_lba,
616 &partition_entry_size, &header_size,
617 &max_partition_count);
618 if (ret) {
619 dprintf(CRITICAL,
620 "GPT: Backup signature invalid cannot write GPT\n");
621 goto end;
622 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700623
Ajay Dudanib01e5062011-12-03 23:23:42 -0800624 /* Patching the primary and the backup header of the GPT table */
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700625 patch_gpt(gptImage, device_density, partition_entry_array_size,
Ajay Dudanib01e5062011-12-03 23:23:42 -0800626 max_partition_count, partition_entry_size);
Neeti Desai5f26aff2011-09-30 10:27:40 -0700627
Ajay Dudanib01e5062011-12-03 23:23:42 -0800628 /* Erasing the eMMC card before writing */
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700629 ret = mmc_erase_card(0x00000000, device_density);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800630 if (ret) {
631 dprintf(CRITICAL, "Failed to erase the eMMC card\n");
632 goto end;
633 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700634
Ajay Dudanib01e5062011-12-03 23:23:42 -0800635 /* Writing protective MBR */
636 ret = mmc_write(0, PROTECTIVE_MBR_SIZE, (unsigned int *)gptImage);
637 if (ret) {
638 dprintf(CRITICAL, "Failed to write Protective MBR\n");
639 goto end;
640 }
641 /* Writing the primary GPT header */
642 primary_header_location = PROTECTIVE_MBR_SIZE;
643 ret = mmc_write(primary_header_location, BLOCK_SIZE,
644 (unsigned int *)primary_gpt_header);
645 if (ret) {
646 dprintf(CRITICAL, "Failed to write GPT header\n");
647 goto end;
648 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700649
Ajay Dudanib01e5062011-12-03 23:23:42 -0800650 /* Writing the backup GPT header */
651 backup_header_lba = GET_LLWORD_FROM_BYTE
652 (&primary_gpt_header[BACKUP_HEADER_OFFSET]);
653 secondary_header_location = backup_header_lba * BLOCK_SIZE;
654 ret = mmc_write(secondary_header_location, BLOCK_SIZE,
655 (unsigned int *)secondary_gpt_header);
656 if (ret) {
657 dprintf(CRITICAL, "Failed to write GPT backup header\n");
658 goto end;
659 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700660
Ajay Dudanib01e5062011-12-03 23:23:42 -0800661 /* Writing the partition entries array for the primary header */
662 partition_entry_array_start = primary_gpt_header + BLOCK_SIZE;
663 ret = write_gpt_partition_array(primary_gpt_header,
664 partition_entry_array_start,
665 partition_entry_array_size);
666 if (ret) {
667 dprintf(CRITICAL,
668 "GPT: Could not write GPT Partition entries array\n");
669 goto end;
670 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700671
Ajay Dudanib01e5062011-12-03 23:23:42 -0800672 /*Writing the partition entries array for the backup header */
673 partition_entry_array_start = primary_gpt_header + BLOCK_SIZE +
674 partition_entry_array_size;
675 ret = write_gpt_partition_array(secondary_gpt_header,
676 partition_entry_array_start,
677 partition_entry_array_size);
678 if (ret) {
679 dprintf(CRITICAL,
680 "GPT: Could not write GPT Partition entries array\n");
681 goto end;
682 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700683
Ajay Dudanib01e5062011-12-03 23:23:42 -0800684 /* Re-read the GPT partition table */
685 dprintf(INFO, "Re-reading the GPT Partition Table\n");
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700686 ret = mmc_boot_read_gpt();
Ajay Dudanib01e5062011-12-03 23:23:42 -0800687 if (ret) {
688 dprintf(CRITICAL,
689 "GPT: Failure to re- read the GPT Partition table\n");
690 goto end;
691 }
692 partition_dump();
693 dprintf(CRITICAL, "GPT: Partition Table written\n");
694 memset(primary_gpt_header, 0x00, size);
Neeti Desai5f26aff2011-09-30 10:27:40 -0700695
Ajay Dudanib01e5062011-12-03 23:23:42 -0800696 end:
697 return ret;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700698}
699
Ajay Dudanib01e5062011-12-03 23:23:42 -0800700unsigned int write_partition(unsigned size, unsigned char *partition)
Neeti Desai5f26aff2011-09-30 10:27:40 -0700701{
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700702 unsigned int ret = 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800703 unsigned int partition_type;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700704
Ajay Dudanib01e5062011-12-03 23:23:42 -0800705 if (partition == 0) {
706 dprintf(CRITICAL, "NULL partition\n");
707 goto end;
708 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700709
Ajay Dudanib01e5062011-12-03 23:23:42 -0800710 ret = partition_get_type(size, partition, &partition_type);
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700711 if (ret)
Ajay Dudanib01e5062011-12-03 23:23:42 -0800712 goto end;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700713
Ajay Dudanib01e5062011-12-03 23:23:42 -0800714 switch (partition_type) {
715 case PARTITION_TYPE_MBR:
716 dprintf(INFO, "Writing MBR partition\n");
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700717 ret = write_mbr(size, partition);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800718 break;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700719
Ajay Dudanib01e5062011-12-03 23:23:42 -0800720 case PARTITION_TYPE_GPT:
721 dprintf(INFO, "Writing GPT partition\n");
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700722 ret = write_gpt(size, partition);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800723 dprintf(CRITICAL, "Re-Flash all the partitions\n");
724 break;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700725
Ajay Dudanib01e5062011-12-03 23:23:42 -0800726 default:
727 dprintf(CRITICAL, "Invalid partition\n");
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700728 ret = 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800729 goto end;
730 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700731
Ajay Dudanib01e5062011-12-03 23:23:42 -0800732 end:
733 return ret;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700734}
Ajay Dudanib01e5062011-12-03 23:23:42 -0800735
Kinson Chikf1a43512011-07-14 11:28:39 -0700736/*
737 * Fill name for android partition found.
738 */
Ajay Dudanib01e5062011-12-03 23:23:42 -0800739static void
740mbr_fill_name(struct partition_entry *partition_ent, unsigned int type)
Kinson Chikf1a43512011-07-14 11:28:39 -0700741{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800742 switch (type) {
743 memset(partition_ent->name, 0, MAX_GPT_NAME_SIZE);
744 case MBR_MODEM_TYPE:
745 case MBR_MODEM_TYPE2:
746 /* if already assigned last name available then return */
747 if (!strcmp((const char *)vfat_partitions[vfat_count], "NONE"))
748 return;
749 strlcpy((char *)partition_ent->name,
750 (const char *)vfat_partitions[vfat_count],
751 sizeof(partition_ent->name));
752 vfat_count++;
753 break;
754 case MBR_SBL1_TYPE:
755 memcpy(partition_ent->name, "sbl1", 4);
756 break;
757 case MBR_SBL2_TYPE:
758 memcpy(partition_ent->name, "sbl2", 4);
759 break;
760 case MBR_SBL3_TYPE:
761 memcpy(partition_ent->name, "sbl3", 4);
762 break;
763 case MBR_RPM_TYPE:
764 memcpy(partition_ent->name, "rpm", 3);
765 break;
766 case MBR_TZ_TYPE:
767 memcpy(partition_ent->name, "tz", 2);
768 break;
769 case MBR_ABOOT_TYPE:
Channagoud Kadabi16f50952012-04-02 16:24:07 +0530770#if PLATFORM_MSM7X27A
771 memcpy(partition_ent->name, "FOTA", 4);
772#else
Ajay Dudanib01e5062011-12-03 23:23:42 -0800773 memcpy(partition_ent->name, "aboot", 5);
Channagoud Kadabi16f50952012-04-02 16:24:07 +0530774#endif
Ajay Dudanib01e5062011-12-03 23:23:42 -0800775 break;
776 case MBR_BOOT_TYPE:
777 memcpy(partition_ent->name, "boot", 4);
778 break;
779 case MBR_MODEM_ST1_TYPE:
780 memcpy(partition_ent->name, "modem_st1", 9);
781 break;
782 case MBR_MODEM_ST2_TYPE:
783 memcpy(partition_ent->name, "modem_st2", 9);
784 break;
785 case MBR_EFS2_TYPE:
786 memcpy(partition_ent->name, "efs2", 4);
787 break;
788 case MBR_USERDATA_TYPE:
789 if (ext3_count == sizeof(ext3_partitions) / sizeof(char *))
790 return;
791 strlcpy((char *)partition_ent->name,
792 (const char *)ext3_partitions[ext3_count],
793 sizeof(partition_ent->name));
794 ext3_count++;
795 break;
796 case MBR_RECOVERY_TYPE:
797 memcpy(partition_ent->name, "recovery", 8);
798 break;
799 case MBR_MISC_TYPE:
800 memcpy(partition_ent->name, "misc", 4);
801 break;
Neeti Desai2d6b0e42012-03-23 15:48:57 -0700802 case MBR_SSD_TYPE:
803 memcpy(partition_ent->name, "ssd", 3);
804 break;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800805 };
Kinson Chikf1a43512011-07-14 11:28:39 -0700806}
807
808/*
809 * Find index of parition in array of partition entries
810 */
Ajay Dudanib01e5062011-12-03 23:23:42 -0800811unsigned partition_get_index(const char *name)
Kinson Chikf1a43512011-07-14 11:28:39 -0700812{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800813 unsigned int input_string_length = strlen(name);
814 unsigned n;
Kinson Chik66552a82011-03-29 15:59:06 -0700815
neetid6c38de12011-12-02 12:04:50 -0800816 if( partition_count >= NUM_PARTITIONS)
817 {
818 return INVALID_PTN;
819 }
Ajay Dudanib01e5062011-12-03 23:23:42 -0800820 for (n = 0; n < partition_count; n++) {
821 if (!memcmp
822 (name, &partition_entries[n].name, input_string_length)
823 && input_string_length ==
824 strlen((const char *)&partition_entries[n].name)) {
825 return n;
826 }
827 }
828 return INVALID_PTN;
Kinson Chikf1a43512011-07-14 11:28:39 -0700829}
830
831/* Get size of the partition */
Ajay Dudanib01e5062011-12-03 23:23:42 -0800832unsigned long long partition_get_size(int index)
Kinson Chikf1a43512011-07-14 11:28:39 -0700833{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800834 if (index == INVALID_PTN)
835 return 0;
836 else {
837 return partition_entries[index].size * BLOCK_SIZE;
838 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700839}
840
841/* Get offset of the partition */
Ajay Dudanib01e5062011-12-03 23:23:42 -0800842unsigned long long partition_get_offset(int index)
Kinson Chikf1a43512011-07-14 11:28:39 -0700843{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800844 if (index == INVALID_PTN)
845 return 0;
846 else {
847 return partition_entries[index].first_lba * BLOCK_SIZE;
848 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700849}
850
851/* Debug: Print all parsed partitions */
852void partition_dump()
853{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800854 unsigned i = 0;
855 for (i = 0; i < partition_count; i++) {
856 dprintf(SPEW,
857 "ptn[%d]:Name[%s] Size[%llu] Type[%u] First[%llu] Last[%llu]\n",
858 i, partition_entries[i].name, partition_entries[i].size,
859 partition_entries[i].dtype,
860 partition_entries[i].first_lba,
861 partition_entries[i].last_lba);
862 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700863}
864
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700865static unsigned int
Ajay Dudanib01e5062011-12-03 23:23:42 -0800866partition_verify_mbr_signature(unsigned size, unsigned char *buffer)
Kinson Chikf1a43512011-07-14 11:28:39 -0700867{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800868 /* Avoid checking past end of buffer */
869 if ((TABLE_SIGNATURE + 1) > size) {
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700870 return 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800871 }
872 /* Check to see if signature exists */
873 if ((buffer[TABLE_SIGNATURE] != MMC_MBR_SIGNATURE_BYTE_0) ||
874 (buffer[TABLE_SIGNATURE + 1] != MMC_MBR_SIGNATURE_BYTE_1)) {
875 dprintf(CRITICAL, "MBR signature does not match.\n");
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700876 return 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800877 }
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700878 return 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700879}
880
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700881static unsigned int
Ajay Dudanib01e5062011-12-03 23:23:42 -0800882mbr_partition_get_type(unsigned size, unsigned char *partition,
883 unsigned int *partition_type)
Kinson Chikf1a43512011-07-14 11:28:39 -0700884{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800885 unsigned int type_offset = TABLE_ENTRY_0 + OFFSET_TYPE;
Kinson Chikf1a43512011-07-14 11:28:39 -0700886
Ajay Dudanib01e5062011-12-03 23:23:42 -0800887 if (size < type_offset) {
888 goto end;
889 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700890
Ajay Dudanib01e5062011-12-03 23:23:42 -0800891 *partition_type = partition[type_offset];
892 end:
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700893 return 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700894}
895
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700896static unsigned int
Ajay Dudanib01e5062011-12-03 23:23:42 -0800897partition_get_type(unsigned size, unsigned char *partition,
898 unsigned int *partition_type)
Kinson Chikf1a43512011-07-14 11:28:39 -0700899{
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700900 unsigned int ret = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700901
Ajay Dudanib01e5062011-12-03 23:23:42 -0800902 /*
903 * If the block contains the MBR signature, then it's likely either
904 * MBR or MBR with protective type (GPT). If the MBR signature is
905 * not there, then it could be the GPT backup.
906 */
Kinson Chikf1a43512011-07-14 11:28:39 -0700907
Ajay Dudanib01e5062011-12-03 23:23:42 -0800908 /* First check the MBR signature */
909 ret = partition_verify_mbr_signature(size, partition);
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700910 if (!ret) {
Ajay Dudanib01e5062011-12-03 23:23:42 -0800911 unsigned int mbr_partition_type = PARTITION_TYPE_MBR;
Kinson Chikf1a43512011-07-14 11:28:39 -0700912
Ajay Dudanib01e5062011-12-03 23:23:42 -0800913 /* MBR signature verified. This could be MBR, MBR + EBR, or GPT */
914 ret =
915 mbr_partition_get_type(size, partition,
916 &mbr_partition_type);
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700917 if (ret) {
Ajay Dudanib01e5062011-12-03 23:23:42 -0800918 dprintf(CRITICAL, "Cannot get TYPE of partition");
919 } else if (MBR_PROTECTED_TYPE == mbr_partition_type) {
920 *partition_type = PARTITION_TYPE_GPT;
921 } else {
922 *partition_type = PARTITION_TYPE_MBR;
923 }
924 } else {
925 /*
926 * This could be the GPT backup. Make that assumption for now.
927 * Anybody who treats the block as GPT backup should check the
928 * signature.
929 */
930 *partition_type = PARTITION_TYPE_GPT_BACKUP;
931 }
932 return ret;
Kinson Chik66552a82011-03-29 15:59:06 -0700933}
Kinson Chik4d7444f2011-09-13 15:48:51 -0700934
935/*
936 * Parse the gpt header and get the required header fields
937 * Return 0 on valid signature
938 */
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700939static unsigned int
Ajay Dudanib01e5062011-12-03 23:23:42 -0800940partition_parse_gpt_header(unsigned char *buffer,
941 unsigned long long *first_usable_lba,
942 unsigned int *partition_entry_size,
943 unsigned int *header_size,
944 unsigned int *max_partition_count)
Kinson Chik4d7444f2011-09-13 15:48:51 -0700945{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800946 /* Check GPT Signature */
947 if (((uint32_t *) buffer)[0] != GPT_SIGNATURE_2 ||
948 ((uint32_t *) buffer)[1] != GPT_SIGNATURE_1)
949 return 1;
Kinson Chik4d7444f2011-09-13 15:48:51 -0700950
Ajay Dudanib01e5062011-12-03 23:23:42 -0800951 *header_size = GET_LWORD_FROM_BYTE(&buffer[HEADER_SIZE_OFFSET]);
952 *first_usable_lba =
953 GET_LLWORD_FROM_BYTE(&buffer[FIRST_USABLE_LBA_OFFSET]);
954 *max_partition_count =
955 GET_LWORD_FROM_BYTE(&buffer[PARTITION_COUNT_OFFSET]);
956 *partition_entry_size =
957 GET_LWORD_FROM_BYTE(&buffer[PENTRY_SIZE_OFFSET]);
Kinson Chik4d7444f2011-09-13 15:48:51 -0700958
Ajay Dudanib01e5062011-12-03 23:23:42 -0800959 return 0;
Kinson Chik4d7444f2011-09-13 15:48:51 -0700960}