blob: b10a85c8496a9b933056557799ec2b999a6e5d38 [file] [log] [blame]
Channagoud Kadabi7768c462015-02-26 11:33:52 -08001/* Copyright (c) 2011-2013, 2015 The Linux Foundation. All rights reserved.
Kinson Chik66552a82011-03-29 15:59:06 -07002
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer in the documentation and/or other materials provided
11 * with the distribution.
Channagoud Kadabif73aa292013-01-31 16:55:20 -080012 * * Neither the name of The Linux Foundation. nor the names of its
Kinson Chik66552a82011-03-29 15:59:06 -070013 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <stdlib.h>
30#include <string.h>
vijay kumarc8c60a02015-04-28 13:05:04 +053031#include <crc32.h>
Kinson Chik66552a82011-03-29 15:59:06 -070032#include "mmc.h"
33#include "partition_parser.h"
vijay kumarc8c60a02015-04-28 13:05:04 +053034#define GPT_HEADER_SIZE 92
35#define GPT_LBA 1
36#define PARTITION_ENTRY_SIZE 128
37static bool flashing_gpt = 0;
38static bool parse_secondary_gpt = 0;
Kinson Chik66552a82011-03-29 15:59:06 -070039
Channagoud Kadabi57eb6302013-09-10 14:21:30 -070040static uint32_t mmc_boot_read_gpt(uint32_t block_size);
41static uint32_t mmc_boot_read_mbr(uint32_t block_size);
Channagoud Kadabi0e332852013-04-19 12:55:53 -070042static void mbr_fill_name(struct partition_entry *partition_ent,
43 uint32_t type);
44static uint32_t partition_verify_mbr_signature(uint32_t size,
45 uint8_t *buffer);
46static uint32_t mbr_partition_get_type(uint32_t size, uint8_t *partition,
47 uint32_t *partition_type);
48
49static uint32_t partition_get_type(uint32_t size, uint8_t *partition,
50 uint32_t *partition_type);
51static uint32_t partition_parse_gpt_header(uint8_t *buffer,
52 uint64_t *first_usable_lba,
53 uint32_t *partition_entry_size,
54 uint32_t *header_size,
55 uint32_t *max_partition_count);
56
Channagoud Kadabi57eb6302013-09-10 14:21:30 -070057static uint32_t write_mbr(uint32_t, uint8_t *mbrImage, uint32_t block_size);
58static uint32_t write_gpt(uint32_t size, uint8_t *gptImage, uint32_t block_size);
Channagoud Kadabi0e332852013-04-19 12:55:53 -070059
Ajay Dudanib01e5062011-12-03 23:23:42 -080060char *ext3_partitions[] =
61 { "system", "userdata", "persist", "cache", "tombstones" };
62char *vfat_partitions[] = { "modem", "mdm", "NONE" };
63
Kinson Chikf1a43512011-07-14 11:28:39 -070064unsigned int ext3_count = 0;
65unsigned int vfat_count = 0;
66
Channagoud Kadabie9b94022013-11-12 13:49:41 -080067struct partition_entry *partition_entries;
Pavel Nedevb5ba62d2013-07-22 11:57:41 +030068static unsigned gpt_partitions_exist = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -070069unsigned partition_count = 0;
70
Channagoud Kadabi0e332852013-04-19 12:55:53 -070071unsigned int partition_read_table()
Kinson Chikf1a43512011-07-14 11:28:39 -070072{
Ajay Dudanib01e5062011-12-03 23:23:42 -080073 unsigned int ret;
Channagoud Kadabi57eb6302013-09-10 14:21:30 -070074 uint32_t block_size;
75
76 block_size = mmc_get_device_blocksize();
Kinson Chikf1a43512011-07-14 11:28:39 -070077
Channagoud Kadabie9b94022013-11-12 13:49:41 -080078 /* Allocate partition entries array */
79 partition_entries = (struct partition_entry *) calloc(NUM_PARTITIONS, sizeof(struct partition_entry));
80 ASSERT(partition_entries);
81
Ajay Dudanib01e5062011-12-03 23:23:42 -080082 /* Read MBR of the card */
Channagoud Kadabi57eb6302013-09-10 14:21:30 -070083 ret = mmc_boot_read_mbr(block_size);
Channagoud Kadabi0e332852013-04-19 12:55:53 -070084 if (ret) {
Ajay Dudanib01e5062011-12-03 23:23:42 -080085 dprintf(CRITICAL, "MMC Boot: MBR read failed!\n");
Channagoud Kadabi0e332852013-04-19 12:55:53 -070086 return 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -080087 }
Kinson Chikf1a43512011-07-14 11:28:39 -070088
Ajay Dudanib01e5062011-12-03 23:23:42 -080089 /* Read GPT of the card if exist */
90 if (gpt_partitions_exist) {
Channagoud Kadabi57eb6302013-09-10 14:21:30 -070091 ret = mmc_boot_read_gpt(block_size);
Channagoud Kadabi0e332852013-04-19 12:55:53 -070092 if (ret) {
Ajay Dudanib01e5062011-12-03 23:23:42 -080093 dprintf(CRITICAL, "MMC Boot: GPT read failed!\n");
Channagoud Kadabi0e332852013-04-19 12:55:53 -070094 return 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -080095 }
96 }
Channagoud Kadabi0e332852013-04-19 12:55:53 -070097 return 0;
Kinson Chikf1a43512011-07-14 11:28:39 -070098}
99
100/*
101 * Read MBR from MMC card and fill partition table.
102 */
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700103static unsigned int mmc_boot_read_mbr(uint32_t block_size)
Kinson Chikf1a43512011-07-14 11:28:39 -0700104{
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700105 uint8_t *buffer = NULL;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800106 unsigned int dtype;
107 unsigned int dfirstsec;
108 unsigned int EBR_first_sec;
109 unsigned int EBR_current_sec;
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700110 int ret = 0;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800111 int idx, i;
Kinson Chikf1a43512011-07-14 11:28:39 -0700112
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700113 buffer = (uint8_t *)memalign(CACHE_LINE, ROUNDUP(block_size, CACHE_LINE));
114
115 if (!buffer)
116 {
117 dprintf(CRITICAL, "Error allocating memory while reading partition table\n");
118 ret = -1;
119 goto end;
120 }
121
Ajay Dudanib01e5062011-12-03 23:23:42 -0800122 /* Print out the MBR first */
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700123 ret = mmc_read(0, (unsigned int *)buffer, block_size);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800124 if (ret) {
125 dprintf(CRITICAL, "Could not read partition from mmc\n");
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700126 goto end;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800127 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700128
Ajay Dudanib01e5062011-12-03 23:23:42 -0800129 /* Check to see if signature exists */
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700130 ret = partition_verify_mbr_signature(block_size, buffer);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800131 if (ret) {
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700132 goto end;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800133 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700134
Ajay Dudanib01e5062011-12-03 23:23:42 -0800135 /*
136 * Process each of the four partitions in the MBR by reading the table
137 * information into our mbr table.
138 */
139 partition_count = 0;
140 idx = TABLE_ENTRY_0;
141 for (i = 0; i < 4; i++) {
142 /* Type 0xEE indicates end of MBR and GPT partitions exist */
143 dtype = buffer[idx + i * TABLE_ENTRY_SIZE + OFFSET_TYPE];
144 if (dtype == MBR_PROTECTED_TYPE) {
145 gpt_partitions_exist = 1;
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700146 goto end;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800147 }
148 partition_entries[partition_count].dtype = dtype;
149 partition_entries[partition_count].attribute_flag =
150 buffer[idx + i * TABLE_ENTRY_SIZE + OFFSET_STATUS];
151 partition_entries[partition_count].first_lba =
152 GET_LWORD_FROM_BYTE(&buffer[idx +
153 i * TABLE_ENTRY_SIZE +
154 OFFSET_FIRST_SEC]);
155 partition_entries[partition_count].size =
156 GET_LWORD_FROM_BYTE(&buffer[idx +
157 i * TABLE_ENTRY_SIZE +
158 OFFSET_SIZE]);
159 dfirstsec = partition_entries[partition_count].first_lba;
160 mbr_fill_name(&partition_entries[partition_count],
161 partition_entries[partition_count].dtype);
162 partition_count++;
163 if (partition_count == NUM_PARTITIONS)
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700164 goto end;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800165 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700166
Ajay Dudanib01e5062011-12-03 23:23:42 -0800167 /* See if the last partition is EBR, if not, parsing is done */
168 if (dtype != MBR_EBR_TYPE) {
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700169 goto end;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800170 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700171
Ajay Dudanib01e5062011-12-03 23:23:42 -0800172 EBR_first_sec = dfirstsec;
173 EBR_current_sec = dfirstsec;
Kinson Chikf1a43512011-07-14 11:28:39 -0700174
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700175 ret = mmc_read((EBR_first_sec * block_size), (unsigned int *)buffer, block_size);
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700176 if (ret)
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700177 goto end;
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700178
Ajay Dudanib01e5062011-12-03 23:23:42 -0800179 /* Loop to parse the EBR */
180 for (i = 0;; i++) {
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700181 ret = partition_verify_mbr_signature(block_size, buffer);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800182 if (ret) {
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700183 ret = 0;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800184 break;
185 }
186 partition_entries[partition_count].attribute_flag =
187 buffer[TABLE_ENTRY_0 + OFFSET_STATUS];
188 partition_entries[partition_count].dtype =
189 buffer[TABLE_ENTRY_0 + OFFSET_TYPE];
190 partition_entries[partition_count].first_lba =
191 GET_LWORD_FROM_BYTE(&buffer[TABLE_ENTRY_0 +
192 OFFSET_FIRST_SEC]) +
193 EBR_current_sec;
194 partition_entries[partition_count].size =
195 GET_LWORD_FROM_BYTE(&buffer[TABLE_ENTRY_0 + OFFSET_SIZE]);
196 mbr_fill_name(&(partition_entries[partition_count]),
197 partition_entries[partition_count].dtype);
198 partition_count++;
199 if (partition_count == NUM_PARTITIONS)
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700200 goto end;
Kinson Chikf1a43512011-07-14 11:28:39 -0700201
Ajay Dudanib01e5062011-12-03 23:23:42 -0800202 dfirstsec =
203 GET_LWORD_FROM_BYTE(&buffer
204 [TABLE_ENTRY_1 + OFFSET_FIRST_SEC]);
205 if (dfirstsec == 0) {
206 /* Getting to the end of the EBR tables */
207 break;
208 }
209 /* More EBR to follow - read in the next EBR sector */
210 dprintf(SPEW, "Reading EBR block from 0x%X\n", EBR_first_sec
211 + dfirstsec);
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700212 ret = mmc_read(((EBR_first_sec + dfirstsec) * block_size),(unsigned int *)buffer,
213 block_size);
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700214 if (ret)
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700215 goto end;
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700216
Ajay Dudanib01e5062011-12-03 23:23:42 -0800217 EBR_current_sec = EBR_first_sec + dfirstsec;
218 }
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700219end:
220 if (buffer)
221 free(buffer);
222
Ajay Dudanib01e5062011-12-03 23:23:42 -0800223 return ret;
Kinson Chikf1a43512011-07-14 11:28:39 -0700224}
Kinson Chik66552a82011-03-29 15:59:06 -0700225
226/*
227 * Read GPT from MMC and fill partition table
228 */
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700229static unsigned int mmc_boot_read_gpt(uint32_t block_size)
Kinson Chikf1a43512011-07-14 11:28:39 -0700230{
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700231 int ret = 0;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800232 unsigned int header_size;
233 unsigned long long first_usable_lba;
234 unsigned long long backup_header_lba;
Neeti Desaice6ad9a2012-03-21 18:57:59 -0700235 unsigned long long card_size_sec;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800236 unsigned int max_partition_count = 0;
237 unsigned int partition_entry_size;
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700238 unsigned int i = 0; /* Counter for each block */
239 unsigned int j = 0; /* Counter for each entry in a block */
Ajay Dudanib01e5062011-12-03 23:23:42 -0800240 unsigned int n = 0; /* Counter for UTF-16 -> 8 conversion */
241 unsigned char UTF16_name[MAX_GPT_NAME_SIZE];
242 /* LBA of first partition -- 1 Block after Protected MBR + 1 for PT */
Neeti Desaice6ad9a2012-03-21 18:57:59 -0700243 unsigned long long partition_0;
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700244 uint64_t device_density;
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700245 uint8_t *data = NULL;
246 uint32_t part_entry_cnt = block_size / ENTRY_SIZE;
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700247
Ajay Dudanib01e5062011-12-03 23:23:42 -0800248 partition_count = 0;
Kinson Chik66552a82011-03-29 15:59:06 -0700249
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700250 /* Get the density of the mmc device */
251
252 device_density = mmc_get_device_capacity();
253
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700254 data = (uint8_t *)memalign(CACHE_LINE, ROUNDUP(block_size, CACHE_LINE));
255 if (!data)
256 {
257 dprintf(CRITICAL, "Failed to Allocate memory to read partition table\n");
258 ret = -1;
259 goto end;
260 }
261
Ajay Dudanib01e5062011-12-03 23:23:42 -0800262 /* Print out the GPT first */
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700263 ret = mmc_read(block_size, (unsigned int *)data, block_size);
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700264 if (ret)
Channagoud Kadabi39f13522013-07-31 14:46:22 -0700265 {
Ajay Dudanib01e5062011-12-03 23:23:42 -0800266 dprintf(CRITICAL, "GPT: Could not read primary gpt from mmc\n");
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700267 goto end;
Channagoud Kadabi39f13522013-07-31 14:46:22 -0700268 }
Ajay Dudanib01e5062011-12-03 23:23:42 -0800269 ret = partition_parse_gpt_header(data, &first_usable_lba,
270 &partition_entry_size, &header_size,
271 &max_partition_count);
272 if (ret) {
273 dprintf(INFO, "GPT: (WARNING) Primary signature invalid\n");
Kinson Chik4d7444f2011-09-13 15:48:51 -0700274
Ajay Dudanib01e5062011-12-03 23:23:42 -0800275 /* Check the backup gpt */
Neeti Desaice6ad9a2012-03-21 18:57:59 -0700276
277 /* Get size of MMC */
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700278 card_size_sec = (device_density) / block_size;
Neeti Desaice6ad9a2012-03-21 18:57:59 -0700279 ASSERT (card_size_sec > 0);
280
281 backup_header_lba = card_size_sec - 1;
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700282 ret = mmc_read((backup_header_lba * block_size), (unsigned int *)data,
283 block_size);
Kinson Chik4d7444f2011-09-13 15:48:51 -0700284
Ajay Dudanib01e5062011-12-03 23:23:42 -0800285 if (ret) {
286 dprintf(CRITICAL,
287 "GPT: Could not read backup gpt from mmc\n");
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700288 goto end;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800289 }
Kinson Chik4d7444f2011-09-13 15:48:51 -0700290
Ajay Dudanib01e5062011-12-03 23:23:42 -0800291 ret = partition_parse_gpt_header(data, &first_usable_lba,
292 &partition_entry_size,
293 &header_size,
294 &max_partition_count);
295 if (ret) {
296 dprintf(CRITICAL,
297 "GPT: Primary and backup signatures invalid\n");
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700298 goto end;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800299 }
Ajay Dudanib01e5062011-12-03 23:23:42 -0800300 }
Neeti Desaice6ad9a2012-03-21 18:57:59 -0700301 partition_0 = GET_LLWORD_FROM_BYTE(&data[PARTITION_ENTRIES_OFFSET]);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800302 /* Read GPT Entries */
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700303 for (i = 0; i < (ROUNDUP(max_partition_count, part_entry_cnt)) / part_entry_cnt; i++) {
neetid6c38de12011-12-02 12:04:50 -0800304 ASSERT(partition_count < NUM_PARTITIONS);
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700305 ret = mmc_read((partition_0 * block_size) + (i * block_size),
306 (uint32_t *) data, block_size);
Kinson Chik66552a82011-03-29 15:59:06 -0700307
Ajay Dudanib01e5062011-12-03 23:23:42 -0800308 if (ret) {
309 dprintf(CRITICAL,
310 "GPT: mmc read card failed reading partition entries.\n");
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700311 goto end;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800312 }
Kinson Chik66552a82011-03-29 15:59:06 -0700313
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700314 for (j = 0; j < part_entry_cnt; j++) {
Ajay Dudanib01e5062011-12-03 23:23:42 -0800315 memcpy(&(partition_entries[partition_count].type_guid),
316 &data[(j * partition_entry_size)],
317 PARTITION_TYPE_GUID_SIZE);
318 if (partition_entries[partition_count].type_guid[0] ==
319 0x00
320 && partition_entries[partition_count].
321 type_guid[1] == 0x00) {
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700322 i = ROUNDUP(max_partition_count, part_entry_cnt);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800323 break;
324 }
325 memcpy(&
326 (partition_entries[partition_count].
327 unique_partition_guid),
328 &data[(j * partition_entry_size) +
329 UNIQUE_GUID_OFFSET],
330 UNIQUE_PARTITION_GUID_SIZE);
331 partition_entries[partition_count].first_lba =
332 GET_LLWORD_FROM_BYTE(&data
333 [(j * partition_entry_size) +
334 FIRST_LBA_OFFSET]);
335 partition_entries[partition_count].last_lba =
336 GET_LLWORD_FROM_BYTE(&data
337 [(j * partition_entry_size) +
338 LAST_LBA_OFFSET]);
339 partition_entries[partition_count].size =
340 partition_entries[partition_count].last_lba -
Neeti Desai4b8c1df2012-03-21 13:15:14 -0700341 partition_entries[partition_count].first_lba + 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800342 partition_entries[partition_count].attribute_flag =
343 GET_LLWORD_FROM_BYTE(&data
344 [(j * partition_entry_size) +
345 ATTRIBUTE_FLAG_OFFSET]);
Kinson Chik66552a82011-03-29 15:59:06 -0700346
Ajay Dudanib01e5062011-12-03 23:23:42 -0800347 memset(&UTF16_name, 0x00, MAX_GPT_NAME_SIZE);
348 memcpy(UTF16_name, &data[(j * partition_entry_size) +
349 PARTITION_NAME_OFFSET],
350 MAX_GPT_NAME_SIZE);
351 /*
352 * Currently partition names in *.xml are UTF-8 and lowercase
353 * Only supporting english for now so removing 2nd byte of UTF-16
354 */
355 for (n = 0; n < MAX_GPT_NAME_SIZE / 2; n++) {
356 partition_entries[partition_count].name[n] =
357 UTF16_name[n * 2];
358 }
359 partition_count++;
360 }
361 }
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700362end:
363 if (data)
364 free(data);
365
Ajay Dudanib01e5062011-12-03 23:23:42 -0800366 return ret;
Kinson Chik66552a82011-03-29 15:59:06 -0700367}
368
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700369static unsigned int write_mbr_in_blocks(uint32_t size, uint8_t *mbrImage, uint32_t block_size)
Neeti Desai5f26aff2011-09-30 10:27:40 -0700370{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800371 unsigned int dtype;
372 unsigned int dfirstsec;
373 unsigned int ebrSectorOffset;
374 unsigned char *ebrImage;
375 unsigned char *lastAddress;
376 int idx, i;
377 unsigned int ret;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700378
Ajay Dudanib01e5062011-12-03 23:23:42 -0800379 /* Write the first block */
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700380 ret = mmc_write(0, block_size, (unsigned int *)mbrImage);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800381 if (ret) {
382 dprintf(CRITICAL, "Failed to write mbr partition\n");
383 goto end;
384 }
385 dprintf(SPEW, "write of first MBR block ok\n");
386 /*
387 Loop through the MBR table to see if there is an EBR.
388 If found, then figure out where to write the first EBR
389 */
390 idx = TABLE_ENTRY_0;
391 for (i = 0; i < 4; i++) {
392 dtype = mbrImage[idx + i * TABLE_ENTRY_SIZE + OFFSET_TYPE];
393 if (MBR_EBR_TYPE == dtype) {
394 dprintf(SPEW, "EBR found.\n");
395 break;
396 }
397 }
398 if (MBR_EBR_TYPE != dtype) {
399 dprintf(SPEW, "No EBR in this image\n");
400 goto end;
401 }
402 /* EBR exists. Write each EBR block to mmc */
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700403 ebrImage = mbrImage + block_size;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800404 ebrSectorOffset =
405 GET_LWORD_FROM_BYTE(&mbrImage
406 [idx + i * TABLE_ENTRY_SIZE +
407 OFFSET_FIRST_SEC]);
408 dfirstsec = 0;
409 dprintf(SPEW, "first EBR to be written at sector 0x%X\n", dfirstsec);
410 lastAddress = mbrImage + size;
411 while (ebrImage < lastAddress) {
412 dprintf(SPEW, "writing to 0x%X\n",
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700413 (ebrSectorOffset + dfirstsec) * block_size);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800414 ret =
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700415 mmc_write((ebrSectorOffset + dfirstsec) * block_size,
416 block_size, (unsigned int *)ebrImage);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800417 if (ret) {
418 dprintf(CRITICAL,
419 "Failed to write EBR block to sector 0x%X\n",
420 dfirstsec);
421 goto end;
422 }
423 dfirstsec =
424 GET_LWORD_FROM_BYTE(&ebrImage
425 [TABLE_ENTRY_1 + OFFSET_FIRST_SEC]);
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700426 ebrImage += block_size;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800427 }
428 dprintf(INFO, "MBR written to mmc successfully\n");
429 end:
430 return ret;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700431}
432
433/* Write the MBR/EBR to the MMC. */
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700434static unsigned int write_mbr(uint32_t size, uint8_t *mbrImage, uint32_t block_size)
Neeti Desai5f26aff2011-09-30 10:27:40 -0700435{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800436 unsigned int ret;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700437
Ajay Dudanib01e5062011-12-03 23:23:42 -0800438 /* Verify that passed in block is a valid MBR */
439 ret = partition_verify_mbr_signature(size, mbrImage);
440 if (ret) {
441 goto end;
442 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700443
Ajay Dudanib01e5062011-12-03 23:23:42 -0800444 /* Write the MBR/EBR to mmc */
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700445 ret = write_mbr_in_blocks(size, mbrImage, block_size);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800446 if (ret) {
447 dprintf(CRITICAL, "Failed to write MBR block to mmc.\n");
448 goto end;
449 }
450 /* Re-read the MBR partition into mbr table */
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700451 ret = mmc_boot_read_mbr(block_size);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800452 if (ret) {
453 dprintf(CRITICAL, "Failed to re-read mbr partition.\n");
454 goto end;
455 }
456 partition_dump();
457 end:
458 return ret;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700459}
460
461/*
462 * A8h reflected is 15h, i.e. 10101000 <--> 00010101
463*/
464int reflect(int data, int len)
465{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800466 int ref = 0;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700467
Ajay Dudanib01e5062011-12-03 23:23:42 -0800468 for (int i = 0; i < len; i++) {
469 if (data & 0x1) {
470 ref |= (1 << ((len - 1) - i));
471 }
472 data = (data >> 1);
473 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700474
Ajay Dudanib01e5062011-12-03 23:23:42 -0800475 return ref;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700476}
477
478/*
479* Function to calculate the CRC32
480*/
481unsigned int calculate_crc32(unsigned char *buffer, int len)
482{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800483 int byte_length = 8; /*length of unit (i.e. byte) */
484 int msb = 0;
Channagoud Kadabi39f13522013-07-31 14:46:22 -0700485 int polynomial = 0x04C11DB7; /* IEEE 32bit polynomial */
Ajay Dudanib01e5062011-12-03 23:23:42 -0800486 unsigned int regs = 0xFFFFFFFF; /* init to all ones */
487 int regs_mask = 0xFFFFFFFF; /* ensure only 32 bit answer */
488 int regs_msb = 0;
489 unsigned int reflected_regs;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700490
Ajay Dudanib01e5062011-12-03 23:23:42 -0800491 for (int i = 0; i < len; i++) {
492 int data_byte = buffer[i];
493 data_byte = reflect(data_byte, 8);
494 for (int j = 0; j < byte_length; j++) {
495 msb = data_byte >> (byte_length - 1); /* get MSB */
496 msb &= 1; /* ensure just 1 bit */
497 regs_msb = (regs >> 31) & 1; /* MSB of regs */
498 regs = regs << 1; /* shift regs for CRC-CCITT */
499 if (regs_msb ^ msb) { /* MSB is a 1 */
500 regs = regs ^ polynomial; /* XOR with generator poly */
501 }
502 regs = regs & regs_mask; /* Mask off excess upper bits */
503 data_byte <<= 1; /* get to next bit */
504 }
505 }
506 regs = regs & regs_mask;
507 reflected_regs = reflect(regs, 32) ^ 0xFFFFFFFF;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700508
Ajay Dudanib01e5062011-12-03 23:23:42 -0800509 return reflected_regs;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700510}
511
512/*
513 * Write the GPT Partition Entry Array to the MMC.
514 */
Ajay Dudanib01e5062011-12-03 23:23:42 -0800515static unsigned int
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700516write_gpt_partition_array(uint8_t *header,
517 uint32_t partition_array_start,
518 uint32_t array_size,
519 uint32_t block_size)
Neeti Desai5f26aff2011-09-30 10:27:40 -0700520{
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700521 unsigned int ret = 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800522 unsigned long long partition_entry_lba;
523 unsigned long long partition_entry_array_start_location;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700524
Ajay Dudanib01e5062011-12-03 23:23:42 -0800525 partition_entry_lba =
526 GET_LLWORD_FROM_BYTE(&header[PARTITION_ENTRIES_OFFSET]);
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700527 partition_entry_array_start_location = partition_entry_lba * block_size;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700528
Ajay Dudanib01e5062011-12-03 23:23:42 -0800529 ret = mmc_write(partition_entry_array_start_location, array_size,
530 (unsigned int *)partition_array_start);
531 if (ret) {
532 dprintf(CRITICAL,
533 "GPT: FAILED to write the partition entry array\n");
534 goto end;
535 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700536
Ajay Dudanib01e5062011-12-03 23:23:42 -0800537 end:
538 return ret;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700539}
540
Ajay Dudanib01e5062011-12-03 23:23:42 -0800541static void
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700542patch_gpt(uint8_t *gptImage, uint64_t density, uint32_t array_size,
543 uint32_t max_part_count, uint32_t part_entry_size, uint32_t block_size)
Neeti Desai5f26aff2011-09-30 10:27:40 -0700544{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800545 unsigned int partition_entry_array_start;
546 unsigned char *primary_gpt_header;
547 unsigned char *secondary_gpt_header;
548 unsigned int offset;
549 unsigned long long card_size_sec;
550 int total_part = 0;
551 unsigned int last_part_offset;
552 unsigned int crc_value;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700553
Ajay Dudanib01e5062011-12-03 23:23:42 -0800554 /* Get size of MMC */
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700555 card_size_sec = (density) / block_size;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800556 /* Working around cap at 4GB */
557 if (card_size_sec == 0) {
558 card_size_sec = 4 * 1024 * 1024 * 2 - 1;
559 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700560
Ajay Dudanib01e5062011-12-03 23:23:42 -0800561 /* Patching primary header */
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700562 primary_gpt_header = (gptImage + block_size);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800563 PUT_LONG_LONG(primary_gpt_header + BACKUP_HEADER_OFFSET,
564 ((long long)(card_size_sec - 1)));
565 PUT_LONG_LONG(primary_gpt_header + LAST_USABLE_LBA_OFFSET,
566 ((long long)(card_size_sec - 34)));
Neeti Desai5f26aff2011-09-30 10:27:40 -0700567
Ajay Dudanib01e5062011-12-03 23:23:42 -0800568 /* Patching backup GPT */
569 offset = (2 * array_size);
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700570 secondary_gpt_header = offset + block_size + primary_gpt_header;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800571 PUT_LONG_LONG(secondary_gpt_header + PRIMARY_HEADER_OFFSET,
572 ((long long)(card_size_sec - 1)));
573 PUT_LONG_LONG(secondary_gpt_header + LAST_USABLE_LBA_OFFSET,
574 ((long long)(card_size_sec - 34)));
575 PUT_LONG_LONG(secondary_gpt_header + PARTITION_ENTRIES_OFFSET,
576 ((long long)(card_size_sec - 33)));
Neeti Desai5f26aff2011-09-30 10:27:40 -0700577
Ajay Dudanib01e5062011-12-03 23:23:42 -0800578 /* Find last partition */
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700579 while (*(primary_gpt_header + block_size + total_part * ENTRY_SIZE) !=
Ajay Dudanib01e5062011-12-03 23:23:42 -0800580 0) {
581 total_part++;
582 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700583
Ajay Dudanib01e5062011-12-03 23:23:42 -0800584 /* Patching last partition */
585 last_part_offset =
586 (total_part - 1) * ENTRY_SIZE + PARTITION_ENTRY_LAST_LBA;
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700587 PUT_LONG_LONG(primary_gpt_header + block_size + last_part_offset,
Ajay Dudanib01e5062011-12-03 23:23:42 -0800588 (long long)(card_size_sec - 34));
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700589 PUT_LONG_LONG(primary_gpt_header + block_size + last_part_offset +
Ajay Dudanib01e5062011-12-03 23:23:42 -0800590 array_size, (long long)(card_size_sec - 34));
Neeti Desai5f26aff2011-09-30 10:27:40 -0700591
Ajay Dudanib01e5062011-12-03 23:23:42 -0800592 /* Updating CRC of the Partition entry array in both headers */
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700593 partition_entry_array_start = primary_gpt_header + block_size;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800594 crc_value = calculate_crc32(partition_entry_array_start,
595 max_part_count * part_entry_size);
596 PUT_LONG(primary_gpt_header + PARTITION_CRC_OFFSET, crc_value);
Neeti Desai5f26aff2011-09-30 10:27:40 -0700597
Ajay Dudanib01e5062011-12-03 23:23:42 -0800598 crc_value = calculate_crc32(partition_entry_array_start + array_size,
599 max_part_count * part_entry_size);
600 PUT_LONG(secondary_gpt_header + PARTITION_CRC_OFFSET, crc_value);
Neeti Desai5f26aff2011-09-30 10:27:40 -0700601
Ajay Dudanib01e5062011-12-03 23:23:42 -0800602 /* Clearing CRC fields to calculate */
603 PUT_LONG(primary_gpt_header + HEADER_CRC_OFFSET, 0);
604 crc_value = calculate_crc32(primary_gpt_header, 92);
605 PUT_LONG(primary_gpt_header + HEADER_CRC_OFFSET, crc_value);
Neeti Desai5f26aff2011-09-30 10:27:40 -0700606
Ajay Dudanib01e5062011-12-03 23:23:42 -0800607 PUT_LONG(secondary_gpt_header + HEADER_CRC_OFFSET, 0);
608 crc_value = (calculate_crc32(secondary_gpt_header, 92));
609 PUT_LONG(secondary_gpt_header + HEADER_CRC_OFFSET, crc_value);
Neeti Desai5f26aff2011-09-30 10:27:40 -0700610
611}
612
613/*
614 * Write the GPT to the MMC.
615 */
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700616static unsigned int write_gpt(uint32_t size, uint8_t *gptImage, uint32_t block_size)
Neeti Desai5f26aff2011-09-30 10:27:40 -0700617{
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700618 unsigned int ret = 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800619 unsigned int header_size;
620 unsigned long long first_usable_lba;
621 unsigned long long backup_header_lba;
622 unsigned int max_partition_count = 0;
623 unsigned int partition_entry_size;
624 unsigned int partition_entry_array_start;
625 unsigned char *primary_gpt_header;
626 unsigned char *secondary_gpt_header;
627 unsigned int offset;
628 unsigned int partition_entry_array_size;
629 unsigned long long primary_header_location; /* address on the emmc card */
630 unsigned long long secondary_header_location; /* address on the emmc card */
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700631 uint64_t device_density;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700632
Ajay Dudanib01e5062011-12-03 23:23:42 -0800633 /* Verify that passed block has a valid GPT primary header */
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700634 primary_gpt_header = (gptImage + block_size);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800635 ret = partition_parse_gpt_header(primary_gpt_header, &first_usable_lba,
636 &partition_entry_size, &header_size,
637 &max_partition_count);
638 if (ret) {
639 dprintf(CRITICAL,
640 "GPT: Primary signature invalid cannot write GPT\n");
641 goto end;
642 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700643
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700644 /* Get the density of the mmc device */
645
646 device_density = mmc_get_device_capacity();
647
Ajay Dudanib01e5062011-12-03 23:23:42 -0800648 /* Verify that passed block has a valid backup GPT HEADER */
649 partition_entry_array_size = partition_entry_size * max_partition_count;
650 if (partition_entry_array_size < MIN_PARTITION_ARRAY_SIZE) {
651 partition_entry_array_size = MIN_PARTITION_ARRAY_SIZE;
652 }
653 offset = (2 * partition_entry_array_size);
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700654 secondary_gpt_header = offset + block_size + primary_gpt_header;
vijay kumarc8c60a02015-04-28 13:05:04 +0530655 parse_secondary_gpt = 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800656 ret =
657 partition_parse_gpt_header(secondary_gpt_header, &first_usable_lba,
658 &partition_entry_size, &header_size,
659 &max_partition_count);
vijay kumarc8c60a02015-04-28 13:05:04 +0530660 parse_secondary_gpt = 0;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800661 if (ret) {
662 dprintf(CRITICAL,
663 "GPT: Backup signature invalid cannot write GPT\n");
664 goto end;
665 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700666
Ajay Dudanib01e5062011-12-03 23:23:42 -0800667 /* Patching the primary and the backup header of the GPT table */
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700668 patch_gpt(gptImage, device_density, partition_entry_array_size,
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700669 max_partition_count, partition_entry_size, block_size);
Neeti Desai5f26aff2011-09-30 10:27:40 -0700670
Ajay Dudanib01e5062011-12-03 23:23:42 -0800671 /* Erasing the eMMC card before writing */
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700672 ret = mmc_erase_card(0x00000000, device_density);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800673 if (ret) {
674 dprintf(CRITICAL, "Failed to erase the eMMC card\n");
675 goto end;
676 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700677
Ajay Dudanib01e5062011-12-03 23:23:42 -0800678 /* Writing protective MBR */
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700679 ret = mmc_write(0, block_size, (unsigned int *)gptImage);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800680 if (ret) {
681 dprintf(CRITICAL, "Failed to write Protective MBR\n");
682 goto end;
683 }
684 /* Writing the primary GPT header */
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700685 primary_header_location = block_size;
686 ret = mmc_write(primary_header_location, block_size,
Ajay Dudanib01e5062011-12-03 23:23:42 -0800687 (unsigned int *)primary_gpt_header);
688 if (ret) {
689 dprintf(CRITICAL, "Failed to write GPT header\n");
690 goto end;
691 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700692
Ajay Dudanib01e5062011-12-03 23:23:42 -0800693 /* Writing the backup GPT header */
694 backup_header_lba = GET_LLWORD_FROM_BYTE
695 (&primary_gpt_header[BACKUP_HEADER_OFFSET]);
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700696 secondary_header_location = backup_header_lba * block_size;
697 ret = mmc_write(secondary_header_location, block_size,
Ajay Dudanib01e5062011-12-03 23:23:42 -0800698 (unsigned int *)secondary_gpt_header);
699 if (ret) {
700 dprintf(CRITICAL, "Failed to write GPT backup header\n");
701 goto end;
702 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700703
Ajay Dudanib01e5062011-12-03 23:23:42 -0800704 /* Writing the partition entries array for the primary header */
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700705 partition_entry_array_start = primary_gpt_header + block_size;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800706 ret = write_gpt_partition_array(primary_gpt_header,
707 partition_entry_array_start,
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700708 partition_entry_array_size, block_size);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800709 if (ret) {
710 dprintf(CRITICAL,
711 "GPT: Could not write GPT Partition entries array\n");
712 goto end;
713 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700714
Ajay Dudanib01e5062011-12-03 23:23:42 -0800715 /*Writing the partition entries array for the backup header */
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700716 partition_entry_array_start = primary_gpt_header + block_size +
Ajay Dudanib01e5062011-12-03 23:23:42 -0800717 partition_entry_array_size;
718 ret = write_gpt_partition_array(secondary_gpt_header,
719 partition_entry_array_start,
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700720 partition_entry_array_size, block_size);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800721 if (ret) {
722 dprintf(CRITICAL,
723 "GPT: Could not write GPT Partition entries array\n");
724 goto end;
725 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700726
Ajay Dudanib01e5062011-12-03 23:23:42 -0800727 /* Re-read the GPT partition table */
728 dprintf(INFO, "Re-reading the GPT Partition Table\n");
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700729 ret = mmc_boot_read_gpt(block_size);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800730 if (ret) {
731 dprintf(CRITICAL,
732 "GPT: Failure to re- read the GPT Partition table\n");
733 goto end;
734 }
vijay kumarc8c60a02015-04-28 13:05:04 +0530735 flashing_gpt = 0;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800736 partition_dump();
737 dprintf(CRITICAL, "GPT: Partition Table written\n");
738 memset(primary_gpt_header, 0x00, size);
Neeti Desai5f26aff2011-09-30 10:27:40 -0700739
Ajay Dudanib01e5062011-12-03 23:23:42 -0800740 end:
741 return ret;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700742}
743
Ajay Dudanib01e5062011-12-03 23:23:42 -0800744unsigned int write_partition(unsigned size, unsigned char *partition)
Neeti Desai5f26aff2011-09-30 10:27:40 -0700745{
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700746 unsigned int ret = 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800747 unsigned int partition_type;
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700748 uint32_t block_size;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700749
Ajay Dudanib01e5062011-12-03 23:23:42 -0800750 if (partition == 0) {
751 dprintf(CRITICAL, "NULL partition\n");
752 goto end;
753 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700754
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700755 block_size = mmc_get_device_blocksize();
Ajay Dudanib01e5062011-12-03 23:23:42 -0800756 ret = partition_get_type(size, partition, &partition_type);
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700757 if (ret)
Ajay Dudanib01e5062011-12-03 23:23:42 -0800758 goto end;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700759
Ajay Dudanib01e5062011-12-03 23:23:42 -0800760 switch (partition_type) {
761 case PARTITION_TYPE_MBR:
762 dprintf(INFO, "Writing MBR partition\n");
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700763 ret = write_mbr(size, partition, block_size);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800764 break;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700765
Ajay Dudanib01e5062011-12-03 23:23:42 -0800766 case PARTITION_TYPE_GPT:
767 dprintf(INFO, "Writing GPT partition\n");
vijay kumarc8c60a02015-04-28 13:05:04 +0530768 flashing_gpt = 1;
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700769 ret = write_gpt(size, partition, block_size);
Ajay Dudanib01e5062011-12-03 23:23:42 -0800770 dprintf(CRITICAL, "Re-Flash all the partitions\n");
771 break;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700772
Ajay Dudanib01e5062011-12-03 23:23:42 -0800773 default:
774 dprintf(CRITICAL, "Invalid partition\n");
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700775 ret = 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800776 goto end;
777 }
Neeti Desai5f26aff2011-09-30 10:27:40 -0700778
Ajay Dudanib01e5062011-12-03 23:23:42 -0800779 end:
780 return ret;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700781}
Ajay Dudanib01e5062011-12-03 23:23:42 -0800782
Kinson Chikf1a43512011-07-14 11:28:39 -0700783/*
784 * Fill name for android partition found.
785 */
Ajay Dudanib01e5062011-12-03 23:23:42 -0800786static void
787mbr_fill_name(struct partition_entry *partition_ent, unsigned int type)
Kinson Chikf1a43512011-07-14 11:28:39 -0700788{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800789 switch (type) {
790 memset(partition_ent->name, 0, MAX_GPT_NAME_SIZE);
791 case MBR_MODEM_TYPE:
792 case MBR_MODEM_TYPE2:
793 /* if already assigned last name available then return */
794 if (!strcmp((const char *)vfat_partitions[vfat_count], "NONE"))
795 return;
796 strlcpy((char *)partition_ent->name,
797 (const char *)vfat_partitions[vfat_count],
798 sizeof(partition_ent->name));
799 vfat_count++;
800 break;
801 case MBR_SBL1_TYPE:
802 memcpy(partition_ent->name, "sbl1", 4);
803 break;
804 case MBR_SBL2_TYPE:
805 memcpy(partition_ent->name, "sbl2", 4);
806 break;
807 case MBR_SBL3_TYPE:
808 memcpy(partition_ent->name, "sbl3", 4);
809 break;
810 case MBR_RPM_TYPE:
811 memcpy(partition_ent->name, "rpm", 3);
812 break;
813 case MBR_TZ_TYPE:
814 memcpy(partition_ent->name, "tz", 2);
815 break;
816 case MBR_ABOOT_TYPE:
Channagoud Kadabi16f50952012-04-02 16:24:07 +0530817#if PLATFORM_MSM7X27A
818 memcpy(partition_ent->name, "FOTA", 4);
819#else
Ajay Dudanib01e5062011-12-03 23:23:42 -0800820 memcpy(partition_ent->name, "aboot", 5);
Channagoud Kadabi16f50952012-04-02 16:24:07 +0530821#endif
Ajay Dudanib01e5062011-12-03 23:23:42 -0800822 break;
823 case MBR_BOOT_TYPE:
824 memcpy(partition_ent->name, "boot", 4);
825 break;
826 case MBR_MODEM_ST1_TYPE:
827 memcpy(partition_ent->name, "modem_st1", 9);
828 break;
829 case MBR_MODEM_ST2_TYPE:
830 memcpy(partition_ent->name, "modem_st2", 9);
831 break;
832 case MBR_EFS2_TYPE:
833 memcpy(partition_ent->name, "efs2", 4);
834 break;
835 case MBR_USERDATA_TYPE:
836 if (ext3_count == sizeof(ext3_partitions) / sizeof(char *))
837 return;
838 strlcpy((char *)partition_ent->name,
839 (const char *)ext3_partitions[ext3_count],
840 sizeof(partition_ent->name));
841 ext3_count++;
842 break;
843 case MBR_RECOVERY_TYPE:
844 memcpy(partition_ent->name, "recovery", 8);
845 break;
846 case MBR_MISC_TYPE:
847 memcpy(partition_ent->name, "misc", 4);
848 break;
Neeti Desai2d6b0e42012-03-23 15:48:57 -0700849 case MBR_SSD_TYPE:
850 memcpy(partition_ent->name, "ssd", 3);
851 break;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800852 };
Kinson Chikf1a43512011-07-14 11:28:39 -0700853}
854
855/*
856 * Find index of parition in array of partition entries
857 */
Pavel Nedev285ad922013-04-26 10:39:19 +0300858int partition_get_index(const char *name)
Kinson Chikf1a43512011-07-14 11:28:39 -0700859{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800860 unsigned int input_string_length = strlen(name);
861 unsigned n;
Kinson Chik66552a82011-03-29 15:59:06 -0700862
neetid6c38de12011-12-02 12:04:50 -0800863 if( partition_count >= NUM_PARTITIONS)
864 {
865 return INVALID_PTN;
866 }
Ajay Dudanib01e5062011-12-03 23:23:42 -0800867 for (n = 0; n < partition_count; n++) {
Channagoud Kadabi7768c462015-02-26 11:33:52 -0800868 if ((input_string_length == strlen((const char *)&partition_entries[n].name))
869 && !memcmp(name, &partition_entries[n].name, input_string_length)) {
Ajay Dudanib01e5062011-12-03 23:23:42 -0800870 return n;
871 }
872 }
873 return INVALID_PTN;
Kinson Chikf1a43512011-07-14 11:28:39 -0700874}
875
876/* Get size of the partition */
Ajay Dudanib01e5062011-12-03 23:23:42 -0800877unsigned long long partition_get_size(int index)
Kinson Chikf1a43512011-07-14 11:28:39 -0700878{
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700879 uint32_t block_size;
880
881 block_size = mmc_get_device_blocksize();
882
Ajay Dudanib01e5062011-12-03 23:23:42 -0800883 if (index == INVALID_PTN)
884 return 0;
885 else {
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700886 return partition_entries[index].size * block_size;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800887 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700888}
889
890/* Get offset of the partition */
Ajay Dudanib01e5062011-12-03 23:23:42 -0800891unsigned long long partition_get_offset(int index)
Kinson Chikf1a43512011-07-14 11:28:39 -0700892{
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700893 uint32_t block_size;
894
895 block_size = mmc_get_device_blocksize();
896
Ajay Dudanib01e5062011-12-03 23:23:42 -0800897 if (index == INVALID_PTN)
898 return 0;
899 else {
Channagoud Kadabi57eb6302013-09-10 14:21:30 -0700900 return partition_entries[index].first_lba * block_size;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800901 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700902}
903
904/* Debug: Print all parsed partitions */
905void partition_dump()
906{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800907 unsigned i = 0;
908 for (i = 0; i < partition_count; i++) {
909 dprintf(SPEW,
910 "ptn[%d]:Name[%s] Size[%llu] Type[%u] First[%llu] Last[%llu]\n",
911 i, partition_entries[i].name, partition_entries[i].size,
912 partition_entries[i].dtype,
913 partition_entries[i].first_lba,
914 partition_entries[i].last_lba);
915 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700916}
917
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700918static unsigned int
Ajay Dudanib01e5062011-12-03 23:23:42 -0800919partition_verify_mbr_signature(unsigned size, unsigned char *buffer)
Kinson Chikf1a43512011-07-14 11:28:39 -0700920{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800921 /* Avoid checking past end of buffer */
922 if ((TABLE_SIGNATURE + 1) > size) {
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700923 return 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800924 }
925 /* Check to see if signature exists */
926 if ((buffer[TABLE_SIGNATURE] != MMC_MBR_SIGNATURE_BYTE_0) ||
927 (buffer[TABLE_SIGNATURE + 1] != MMC_MBR_SIGNATURE_BYTE_1)) {
928 dprintf(CRITICAL, "MBR signature does not match.\n");
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700929 return 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800930 }
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700931 return 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700932}
933
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700934static unsigned int
Ajay Dudanib01e5062011-12-03 23:23:42 -0800935mbr_partition_get_type(unsigned size, unsigned char *partition,
936 unsigned int *partition_type)
Kinson Chikf1a43512011-07-14 11:28:39 -0700937{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800938 unsigned int type_offset = TABLE_ENTRY_0 + OFFSET_TYPE;
Kinson Chikf1a43512011-07-14 11:28:39 -0700939
Channagoud Kadabi1921eba2015-03-12 15:14:22 -0700940 if (size < (type_offset + sizeof (*partition_type))) {
Ajay Dudanib01e5062011-12-03 23:23:42 -0800941 goto end;
942 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700943
Ajay Dudanib01e5062011-12-03 23:23:42 -0800944 *partition_type = partition[type_offset];
945 end:
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700946 return 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700947}
948
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700949static unsigned int
Ajay Dudanib01e5062011-12-03 23:23:42 -0800950partition_get_type(unsigned size, unsigned char *partition,
951 unsigned int *partition_type)
Kinson Chikf1a43512011-07-14 11:28:39 -0700952{
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700953 unsigned int ret = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700954
Ajay Dudanib01e5062011-12-03 23:23:42 -0800955 /*
956 * If the block contains the MBR signature, then it's likely either
957 * MBR or MBR with protective type (GPT). If the MBR signature is
958 * not there, then it could be the GPT backup.
959 */
Kinson Chikf1a43512011-07-14 11:28:39 -0700960
Ajay Dudanib01e5062011-12-03 23:23:42 -0800961 /* First check the MBR signature */
962 ret = partition_verify_mbr_signature(size, partition);
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700963 if (!ret) {
Ajay Dudanib01e5062011-12-03 23:23:42 -0800964 unsigned int mbr_partition_type = PARTITION_TYPE_MBR;
Kinson Chikf1a43512011-07-14 11:28:39 -0700965
Ajay Dudanib01e5062011-12-03 23:23:42 -0800966 /* MBR signature verified. This could be MBR, MBR + EBR, or GPT */
967 ret =
968 mbr_partition_get_type(size, partition,
969 &mbr_partition_type);
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700970 if (ret) {
Ajay Dudanib01e5062011-12-03 23:23:42 -0800971 dprintf(CRITICAL, "Cannot get TYPE of partition");
972 } else if (MBR_PROTECTED_TYPE == mbr_partition_type) {
973 *partition_type = PARTITION_TYPE_GPT;
974 } else {
975 *partition_type = PARTITION_TYPE_MBR;
976 }
977 } else {
978 /*
979 * This could be the GPT backup. Make that assumption for now.
980 * Anybody who treats the block as GPT backup should check the
981 * signature.
982 */
983 *partition_type = PARTITION_TYPE_GPT_BACKUP;
984 }
985 return ret;
Kinson Chik66552a82011-03-29 15:59:06 -0700986}
Kinson Chik4d7444f2011-09-13 15:48:51 -0700987
988/*
989 * Parse the gpt header and get the required header fields
990 * Return 0 on valid signature
991 */
Channagoud Kadabi0e332852013-04-19 12:55:53 -0700992static unsigned int
Ajay Dudanib01e5062011-12-03 23:23:42 -0800993partition_parse_gpt_header(unsigned char *buffer,
994 unsigned long long *first_usable_lba,
995 unsigned int *partition_entry_size,
996 unsigned int *header_size,
997 unsigned int *max_partition_count)
Kinson Chik4d7444f2011-09-13 15:48:51 -0700998{
vijay kumarc8c60a02015-04-28 13:05:04 +0530999 uint32_t crc_val_org = 0;
1000 uint32_t crc_val = 0;
1001 uint32_t ret = 0;
1002 uint32_t partitions_for_block = 0;
1003 uint32_t blocks_to_read = 0;
1004 unsigned char *new_buffer = NULL;
1005 unsigned long long last_usable_lba = 0;
1006 unsigned long long partition_0 = 0;
1007 unsigned long long current_lba = 0;
1008 uint32_t block_size = mmc_get_device_blocksize();
1009 /* Get the density of the mmc device */
1010 uint64_t device_density = mmc_get_device_capacity();
1011
Ajay Dudanib01e5062011-12-03 23:23:42 -08001012 /* Check GPT Signature */
1013 if (((uint32_t *) buffer)[0] != GPT_SIGNATURE_2 ||
1014 ((uint32_t *) buffer)[1] != GPT_SIGNATURE_1)
1015 return 1;
Kinson Chik4d7444f2011-09-13 15:48:51 -07001016
Ajay Dudanib01e5062011-12-03 23:23:42 -08001017 *header_size = GET_LWORD_FROM_BYTE(&buffer[HEADER_SIZE_OFFSET]);
vijay kumarc8c60a02015-04-28 13:05:04 +05301018 /*check for header size too small*/
1019 if (*header_size < GPT_HEADER_SIZE) {
1020 dprintf(CRITICAL,"GPT Header size is too small\n");
1021 return 1;
1022 }
1023 /*check for header size too large*/
1024 if (*header_size > block_size) {
1025 dprintf(CRITICAL,"GPT Header size is too large\n");
1026 return 1;
1027 }
1028
1029 crc_val_org = GET_LWORD_FROM_BYTE(&buffer[HEADER_CRC_OFFSET]);
1030 /*Write CRC to 0 before we calculate the crc of the GPT header*/
1031 crc_val = 0;
1032 PUT_LONG(&buffer[HEADER_CRC_OFFSET], crc_val);
1033
1034 crc_val = crc32(~0L,buffer, *header_size) ^ (~0L);
1035 if (crc_val != crc_val_org) {
1036 dprintf(CRITICAL,"Header crc mismatch crc_val = %u with crc_val_org = %u\n", crc_val,crc_val_org);
1037 return 1;
1038 }
1039 else
1040 PUT_LONG(&buffer[HEADER_CRC_OFFSET], crc_val);
1041
1042 current_lba =
1043 GET_LLWORD_FROM_BYTE(&buffer[PRIMARY_HEADER_OFFSET]);
Ajay Dudanib01e5062011-12-03 23:23:42 -08001044 *first_usable_lba =
1045 GET_LLWORD_FROM_BYTE(&buffer[FIRST_USABLE_LBA_OFFSET]);
1046 *max_partition_count =
1047 GET_LWORD_FROM_BYTE(&buffer[PARTITION_COUNT_OFFSET]);
1048 *partition_entry_size =
1049 GET_LWORD_FROM_BYTE(&buffer[PENTRY_SIZE_OFFSET]);
vijay kumarc8c60a02015-04-28 13:05:04 +05301050 last_usable_lba =
1051 GET_LLWORD_FROM_BYTE(&buffer[LAST_USABLE_LBA_OFFSET]);
Kinson Chik4d7444f2011-09-13 15:48:51 -07001052
vijay kumarc8c60a02015-04-28 13:05:04 +05301053 /*current lba and GPT lba should be same*/
1054 if (!parse_secondary_gpt) {
1055 if (current_lba != GPT_LBA) {
1056 dprintf(CRITICAL,"GPT first usable LBA mismatch\n");
1057 return 1;
1058 }
1059 }
1060 /*check for first lba should be with in the valid range*/
1061 if (*first_usable_lba > (device_density/block_size)) {
1062 dprintf(CRITICAL,"Invalid first_usable_lba\n");
1063 return 1;
1064 }
1065 /*check for last lba should be with in the valid range*/
1066 if (last_usable_lba > (device_density/block_size)) {
1067 dprintf(CRITICAL,"Invalid last_usable_lba\n");
1068 return 1;
1069 }
1070 /*check for partition entry size*/
1071 if (*partition_entry_size != PARTITION_ENTRY_SIZE) {
1072 dprintf(CRITICAL,"Invalid parition entry size\n");
1073 return 1;
1074 }
1075
1076 if ((*max_partition_count) > (MIN_PARTITION_ARRAY_SIZE /(*partition_entry_size))) {
1077 dprintf(CRITICAL, "Invalid maximum partition count\n");
1078 return 1;
1079 }
1080
1081 partitions_for_block = block_size / (*partition_entry_size);
1082
1083 blocks_to_read = (*max_partition_count)/ partitions_for_block;
1084 if ((*max_partition_count) % partitions_for_block) {
1085 blocks_to_read += 1;
1086 }
1087
1088 new_buffer = (uint8_t *)memalign(CACHE_LINE, ROUNDUP((blocks_to_read * block_size),CACHE_LINE));
1089
1090 if (!new_buffer)
1091 {
1092 dprintf(CRITICAL, "Failed to Allocate memory to read partition table\n");
1093 return 1;
1094 }
1095
1096 if (!flashing_gpt) {
1097 partition_0 = GET_LLWORD_FROM_BYTE(&buffer[PARTITION_ENTRIES_OFFSET]);
1098 /*start LBA should always be 2 in primary GPT*/
1099 if(partition_0 != 0x2) {
1100 dprintf(CRITICAL, "Starting LBA mismatch\n");
1101 goto fail;
1102
1103 }
1104 /*read the partition entries to new_buffer*/
1105 ret = mmc_read((partition_0) * (block_size), (unsigned int *)new_buffer, (blocks_to_read * block_size));
1106 if (ret)
1107 {
1108 dprintf(CRITICAL, "GPT: Could not read primary gpt from mmc\n");
1109 goto fail;
1110 }
1111 crc_val_org = GET_LWORD_FROM_BYTE(&buffer[PARTITION_CRC_OFFSET]);
1112
1113 crc_val = crc32(~0L,new_buffer, ((*max_partition_count) * (*partition_entry_size))) ^ (~0L);
1114 if (crc_val != crc_val_org) {
1115 dprintf(CRITICAL,"Partition entires crc mismatch crc_val= %u with crc_val_org= %u\n",crc_val,crc_val_org);
1116 ret = 1;
1117 }
1118 }
1119fail:
1120 free(new_buffer);
1121 return ret;
Kinson Chik4d7444f2011-09-13 15:48:51 -07001122}
Pavel Nedevb5ba62d2013-07-22 11:57:41 +03001123
1124bool partition_gpt_exists()
1125{
1126 return (gpt_partitions_exist != 0);
1127}