Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2009-2013, Google Inc. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * * Neither the name of Google, Inc. nor the names of its contributors |
| 15 | * may be used to endorse or promote products derived from this |
| 16 | * software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 21 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 22 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 24 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 25 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 26 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 27 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 28 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 29 | * SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | #include <sys/types.h> |
| 33 | #include <sys/stat.h> |
| 34 | #include <fcntl.h> |
| 35 | #include <sys/mman.h> |
| 36 | #include <sys/stat.h> |
| 37 | #include <sys/types.h> |
| 38 | #include <unistd.h> |
| 39 | #include <endian.h> |
| 40 | #include <zlib.h> |
| 41 | #include <linux/hdreg.h> |
| 42 | #include <sys/ioctl.h> |
| 43 | #include <stdlib.h> |
| 44 | #include <cutils/config_utils.h> |
| 45 | |
| 46 | #include "partitions.h" |
| 47 | #include "debug.h" |
| 48 | #include "utils.h" |
| 49 | #include "protocol.h" |
| 50 | |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 51 | #define BLKRRPART _IO(0x12,95) /* re-read partition table */ |
| 52 | #define BLKSSZGET _IO(0x12,104) |
| 53 | |
| 54 | #define DIV_ROUND_UP(x, y) (((x) + (y) - 1)/(y)) |
| 55 | #define ALIGN(x, y) ((y) * DIV_ROUND_UP((x), (y))) |
| 56 | #define ALIGN_DOWN(x, y) ((y) * ((x) / (y))) |
| 57 | |
| 58 | |
Szymon Starzycki | b88fa32 | 2013-09-17 14:17:32 -0700 | [diff] [blame] | 59 | const uint8_t partition_type_uuid[16] = { |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 60 | 0xa2, 0xa0, 0xd0, 0xeb, 0xe5, 0xb9, 0x33, 0x44, |
| 61 | 0x87, 0xc0, 0x68, 0xb6, 0xb7, 0x26, 0x99, 0xc7, |
| 62 | }; |
| 63 | |
Szymon Starzycki | baf4c4b | 2013-10-04 10:28:03 -0700 | [diff] [blame] | 64 | //TODO: There is assumption that we are using little endian |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 65 | |
| 66 | static void GPT_entry_clear(struct GPT_entry_raw *entry) |
| 67 | { |
| 68 | memset(entry, 0, sizeof(*entry)); |
| 69 | } |
| 70 | |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 71 | /* |
| 72 | * returns mapped location to choosen area |
| 73 | * mapped_ptr is pointer to whole area mapped (it can be bigger then requested) |
| 74 | */ |
| 75 | int gpt_mmap(struct GPT_mapping *mapping, uint64_t location, int size, int fd) |
| 76 | { |
| 77 | unsigned int location_diff = location & ~PAGE_MASK; |
| 78 | |
| 79 | mapping->size = ALIGN(size + location_diff, PAGE_SIZE); |
| 80 | |
| 81 | uint64_t sz = get_file_size64(fd); |
| 82 | if (sz < size + location) { |
| 83 | D(ERR, "the location of mapping area is outside of the device size %lld", sz); |
| 84 | return 1; |
| 85 | } |
Szymon Starzycki | baf4c4b | 2013-10-04 10:28:03 -0700 | [diff] [blame] | 86 | location = ALIGN_DOWN(location, PAGE_SIZE); |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 87 | |
| 88 | mapping->map_ptr = mmap64(NULL, mapping->size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, location); |
| 89 | |
| 90 | if (mapping->map_ptr == MAP_FAILED) { |
| 91 | mapping->ptr = MAP_FAILED; |
| 92 | D(ERR, "map failed %d", (int) mapping->map_ptr); |
| 93 | return 1; |
| 94 | } |
| 95 | |
| 96 | mapping->ptr = (void *)((char *) mapping->map_ptr + location_diff); |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | void gpt_unmap(struct GPT_mapping *mapping) { |
| 101 | munmap(mapping->map_ptr, mapping->size); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | #define LBA_ADDR(table, value) ((uint64_t) (table)->sector_size * (value)) |
| 106 | |
| 107 | int GPT_map_from_content(struct GPT_entry_table *table, const struct GPT_content *content) |
| 108 | { |
| 109 | |
| 110 | // Mapping header |
| 111 | if (gpt_mmap(&table->header_map, LBA_ADDR(table, content->header.current_lba), |
| 112 | table->sector_size, table->fd)) { |
| 113 | D(ERR, "unable to map header:%s\n", strerror(errno)); |
| 114 | goto error_header; |
| 115 | } |
| 116 | |
| 117 | table->header = (struct GPT_header *) table->header_map.ptr; |
| 118 | |
| 119 | table->partition_table_size = ROUND_UP(content->header.entries_count * sizeof(*table->entries), |
| 120 | table->sector_size); |
| 121 | |
| 122 | // Mapping entry table |
| 123 | if (gpt_mmap(&table->entries_map, LBA_ADDR(table, content->header.entries_lba), |
| 124 | table->partition_table_size, table->fd)) { |
| 125 | D(ERR, "unable to map entries"); |
| 126 | goto error_signature; |
| 127 | } |
| 128 | |
| 129 | table->entries = (struct GPT_entry_raw *) table->entries_map.ptr; |
| 130 | |
| 131 | // Mapping secondary header |
| 132 | if (gpt_mmap(&table->sec_header_map, LBA_ADDR(table, content->header.backup_lba), |
| 133 | table->sector_size, table->fd)) { |
| 134 | D(ERR, "unable to map backup gpt header"); |
| 135 | goto error_sec_header; |
| 136 | } |
| 137 | |
| 138 | // Mapping secondary entries table |
| 139 | if (gpt_mmap(&table->sec_entries_map, |
| 140 | LBA_ADDR(table, content->header.backup_lba) - table->partition_table_size, |
| 141 | table->partition_table_size, table->fd)) { |
| 142 | D(ERR, "unable to map secondary gpt table"); |
| 143 | goto error_sec_entries; |
| 144 | } |
| 145 | |
| 146 | table->second_header = (struct GPT_header *) table->sec_header_map.ptr; |
| 147 | table->second_entries = (struct GPT_entry_raw *) table->sec_entries_map.ptr; |
| 148 | table->second_valid = strcmp("EFI PART", (char *) table->second_header->signature) == 0; |
| 149 | |
| 150 | return 0; |
| 151 | |
| 152 | error_sec_entries: |
| 153 | gpt_unmap(&table->sec_header_map); |
| 154 | error_sec_header: |
| 155 | gpt_unmap(&table->entries_map); |
| 156 | error_signature: |
| 157 | gpt_unmap(&table->header_map); |
| 158 | error_header: |
| 159 | return 1; |
| 160 | } |
| 161 | |
| 162 | int GPT_map(struct GPT_entry_table *table, unsigned header_lba) |
| 163 | { |
| 164 | struct GPT_content content; |
| 165 | struct GPT_mapping mapping; |
| 166 | struct GPT_header *header; |
| 167 | |
| 168 | if (gpt_mmap(&mapping, LBA_ADDR(table, header_lba), table->sector_size, table->fd)) { |
| 169 | D(ERR, "unable to map header: %s", strerror(errno)); |
| 170 | goto error_header; |
| 171 | } |
| 172 | |
| 173 | header = (struct GPT_header *) mapping.ptr; |
| 174 | |
| 175 | if (strcmp("EFI PART", (char *) header->signature)) { |
| 176 | D(ERR, "GPT entry not valid"); |
| 177 | goto error_signature; |
| 178 | } |
| 179 | |
| 180 | content.header = *header; |
| 181 | |
| 182 | gpt_unmap(&mapping); |
| 183 | |
| 184 | return GPT_map_from_content(table, &content); |
| 185 | |
| 186 | error_signature: |
| 187 | gpt_unmap(&table->header_map); |
| 188 | error_header: |
| 189 | return 1; |
| 190 | } |
| 191 | |
| 192 | struct GPT_entry_table* GPT_get_device(const char *path, unsigned header_lba) |
| 193 | { |
| 194 | struct GPT_entry_table *table; |
| 195 | size_t sector_bytes; |
| 196 | |
| 197 | table = (struct GPT_entry_table *) malloc(sizeof(*table)); |
| 198 | table->fd = open(path, O_RDWR); |
| 199 | |
| 200 | if (table->fd < 0) { |
| 201 | D(ERR, "unable to open file %s:%s\n", path, strerror(errno)); |
| 202 | return NULL; |
| 203 | } |
| 204 | |
| 205 | if (!ioctl(table->fd, BLKSSZGET, §or_bytes)) { |
| 206 | table->sector_size = (unsigned) sector_bytes; |
| 207 | D(INFO, "Got sector size %d", table->sector_size); |
| 208 | } else { |
| 209 | D(WARN, "unable to get sector size, assuming 512"); |
| 210 | table->sector_size = 512; |
| 211 | } |
| 212 | |
| 213 | if (GPT_map(table, header_lba)) { |
| 214 | D(ERR, "Could not map gpt"); |
| 215 | return NULL; |
| 216 | } |
| 217 | |
| 218 | return table; |
| 219 | } |
| 220 | |
| 221 | static struct GPT_entry_table* GPT_get_from_content(const char *path, const struct GPT_content *content) |
| 222 | { |
| 223 | struct GPT_entry_table *table; |
| 224 | size_t sector_bytes; |
| 225 | |
| 226 | table = (struct GPT_entry_table *) malloc(sizeof(*table)); |
| 227 | table->fd = open(path, O_RDWR); |
| 228 | |
| 229 | if (table->fd < 0) { |
| 230 | D(ERR, "unable to open file %s:%s\n", path, strerror(errno)); |
| 231 | return NULL; |
| 232 | } |
| 233 | |
| 234 | if (!ioctl(table->fd, BLKSSZGET, §or_bytes)) { |
| 235 | table->sector_size = (unsigned) sector_bytes; |
| 236 | D(INFO, "Got sector size %d", table->sector_size); |
| 237 | } else { |
| 238 | D(WARN, "unable to get sector size %s, assuming 512", strerror(errno)); |
| 239 | table->sector_size = 512; |
| 240 | } |
| 241 | |
| 242 | if (GPT_map_from_content(table, content)) { |
| 243 | D(ERR, "Could not map gpt"); |
| 244 | return NULL; |
| 245 | } |
| 246 | |
| 247 | return table; |
| 248 | } |
| 249 | |
| 250 | |
| 251 | void GPT_release_device(struct GPT_entry_table *table) |
| 252 | { |
| 253 | gpt_unmap(&table->header_map); |
| 254 | gpt_unmap(&table->entries_map); |
| 255 | gpt_unmap(&table->sec_header_map); |
| 256 | gpt_unmap(&table->sec_entries_map); |
| 257 | close(table->fd); |
| 258 | free(table); |
| 259 | } |
| 260 | |
| 261 | static int GPT_check_overlap(struct GPT_entry_table *table, struct GPT_entry_raw *entry); |
| 262 | static int GPT_check_overlap_except(struct GPT_entry_table *table, |
| 263 | struct GPT_entry_raw *entry, |
| 264 | struct GPT_entry_raw *exclude); |
| 265 | |
| 266 | void GPT_edit_entry(struct GPT_entry_table *table, |
| 267 | struct GPT_entry_raw *old_entry, |
| 268 | struct GPT_entry_raw *new_entry) |
| 269 | { |
| 270 | struct GPT_entry_raw *current_entry = GPT_get_pointer(table, old_entry); |
| 271 | |
| 272 | if (GPT_check_overlap_except(table, new_entry, current_entry)) { |
| 273 | D(ERR, "Couldn't add overlaping partition"); |
| 274 | return; |
| 275 | } |
| 276 | |
| 277 | if (current_entry == NULL) { |
| 278 | D(ERR, "Couldn't find entry"); |
| 279 | return; |
| 280 | } |
| 281 | |
| 282 | *current_entry = *new_entry; |
| 283 | } |
| 284 | |
| 285 | int GPT_delete_entry(struct GPT_entry_table *table, struct GPT_entry_raw *entry) |
| 286 | { |
| 287 | struct GPT_entry_raw *raw = GPT_get_pointer(table, entry); |
| 288 | |
| 289 | if (raw == NULL) { |
| 290 | D(ERR, "could not find entry"); |
| 291 | return 1; |
| 292 | } |
| 293 | D(DEBUG, "Deleting gpt entry '%s'\n", raw->partition_guid); |
| 294 | |
Szymon Starzycki | baf4c4b | 2013-10-04 10:28:03 -0700 | [diff] [blame] | 295 | // Entry in the middle of table may become empty |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 296 | GPT_entry_clear(raw); |
| 297 | |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | void GPT_add_entry(struct GPT_entry_table *table, struct GPT_entry_raw *entry) |
| 302 | { |
| 303 | unsigned i; |
| 304 | int inserted = 0; |
| 305 | if (GPT_check_overlap(table, entry)) { |
| 306 | D(ERR, "Couldn't add overlaping partition"); |
| 307 | return; |
| 308 | } |
| 309 | |
| 310 | if (GPT_get_pointer(table, entry) != NULL) { |
| 311 | D(WARN, "Add entry fault, this entry already exists"); |
| 312 | return; |
| 313 | } |
| 314 | |
| 315 | struct GPT_entry_raw *entries = table->entries; |
| 316 | |
| 317 | for (i = 0; i < table->header->entries_count; ++i) { |
| 318 | if (!entries[i].type_guid[0]) { |
| 319 | inserted = 1; |
| 320 | D(DEBUG, "inserting"); |
| 321 | memcpy(&entries[i], entry, sizeof(entries[i])); |
| 322 | break; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | if (!inserted) { |
| 327 | D(ERR, "Unable to find empty partion entry"); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | struct GPT_entry_raw *GPT_get_pointer_by_UTFname(struct GPT_entry_table *table, const uint16_t *name); |
| 332 | |
| 333 | struct GPT_entry_raw *GPT_get_pointer(struct GPT_entry_table *table, struct GPT_entry_raw *entry) |
| 334 | { |
| 335 | if (entry->partition_guid[0] != 0) |
| 336 | return GPT_get_pointer_by_guid(table, (const char *) entry->partition_guid); |
| 337 | else if (entry->name[0] != 0) |
| 338 | return GPT_get_pointer_by_UTFname(table, entry->name); |
| 339 | |
| 340 | D(WARN, "Name or guid needed to find entry"); |
| 341 | return NULL; |
| 342 | } |
| 343 | |
| 344 | struct GPT_entry_raw *GPT_get_pointer_by_guid(struct GPT_entry_table *table, const char *name) |
| 345 | { |
| 346 | int current = (int) table->header->entries_count; |
| 347 | |
| 348 | for (current = current - 1; current >= 0; --current) { |
| 349 | if (strncmp((char *) name, |
| 350 | (char *) table->entries[current].partition_guid, 16) == 0) { |
| 351 | return &table->entries[current]; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | return NULL; |
| 356 | } |
| 357 | |
| 358 | int strncmp_UTF16_char(const uint16_t *s1, const char *s2, size_t n) |
| 359 | { |
| 360 | if (n == 0) |
| 361 | return (0); |
| 362 | do { |
| 363 | if (((*s1) & 127) != *s2++) |
| 364 | return (((unsigned char) ((*s1) & 127)) - *(unsigned char *)--s2); |
| 365 | if (*s1++ == 0) |
| 366 | break; |
| 367 | } while (--n != 0); |
| 368 | return (0); |
| 369 | } |
| 370 | |
| 371 | int strncmp_UTF16(const uint16_t *s1, const uint16_t *s2, size_t n) |
| 372 | { |
| 373 | if (n == 0) |
| 374 | return (0); |
| 375 | do { |
| 376 | if ((*s1) != *s2++) |
| 377 | return (*s1 - *--s2); |
| 378 | if (*s1++ == 0) |
| 379 | break; |
| 380 | } while (--n != 0); |
| 381 | return (0); |
| 382 | } |
| 383 | |
| 384 | struct GPT_entry_raw *GPT_get_pointer_by_name(struct GPT_entry_table *table, const char *name) |
| 385 | { |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 386 | int count = (int) table->header->entries_count; |
| 387 | int current; |
| 388 | |
| 389 | for (current = 0; current < count; ++current) { |
| 390 | if (strncmp_UTF16_char(table->entries[current].name, |
| 391 | (char *) name, 16) == 0) { |
| 392 | return &table->entries[current]; |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | return NULL; |
| 397 | } |
| 398 | |
| 399 | struct GPT_entry_raw *GPT_get_pointer_by_UTFname(struct GPT_entry_table *table, const uint16_t *name) |
| 400 | { |
| 401 | int count = (int) table->header->entries_count; |
| 402 | int current; |
| 403 | |
| 404 | for (current = 0; current < count; ++current) { |
| 405 | if (strncmp_UTF16(table->entries[current].name, |
| 406 | name, GPT_NAMELEN) == 0) { |
| 407 | return &table->entries[current]; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | return NULL; |
| 412 | } |
| 413 | |
| 414 | void GPT_sync(struct GPT_entry_table *table) |
| 415 | { |
| 416 | uint32_t crc; |
| 417 | |
| 418 | //calculate crc32 |
| 419 | crc = crc32(0, Z_NULL, 0); |
| 420 | crc = crc32(crc, (void*) table->entries, table->header->entries_count * sizeof(*table->entries)); |
| 421 | table->header->partition_array_checksum = crc; |
| 422 | |
| 423 | table->header->header_checksum = 0; |
| 424 | crc = crc32(0, Z_NULL, 0); |
Szymon Starzycki | b88fa32 | 2013-09-17 14:17:32 -0700 | [diff] [blame] | 425 | crc = crc32(crc, (void*) table->header, table->header->header_size); |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 426 | table->header->header_checksum = crc; |
| 427 | |
| 428 | //sync secondary partion |
| 429 | if (table->second_valid) { |
| 430 | memcpy((void *)table->second_entries, (void *) table->entries, table->partition_table_size); |
| 431 | memcpy((void *)table->second_header, (void *)table->header, sizeof(*table->header)); |
| 432 | } |
| 433 | |
| 434 | if(!ioctl(table->fd, BLKRRPART, NULL)) { |
| 435 | D(WARN, "Unable to force kernel to refresh partition table"); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | void GPT_to_UTF16(uint16_t *to, const char *from, int n) |
| 440 | { |
| 441 | int i; |
| 442 | for (i = 0; i < (n - 1) && (to[i] = from[i]) != '\0'; ++i); |
| 443 | to[i] = '\0'; |
| 444 | } |
| 445 | |
| 446 | void GPT_from_UTF16(char *to, const uint16_t *from, int n) |
| 447 | { |
| 448 | int i; |
| 449 | for (i = 0; i < (n - 1) && (to[i] = from[i] & 127) != '\0'; ++i); |
| 450 | to[i] = '\0'; |
| 451 | } |
| 452 | |
| 453 | static int GPT_check_overlap_except(struct GPT_entry_table *table, |
| 454 | struct GPT_entry_raw *entry, |
| 455 | struct GPT_entry_raw *exclude) { |
| 456 | int current = (int) table->header->entries_count; |
| 457 | int dontcheck; |
| 458 | struct GPT_entry_raw *current_entry; |
| 459 | if (entry->last_lba < entry->first_lba) { |
| 460 | D(WARN, "Start address have to be less than end address"); |
| 461 | return 1; |
| 462 | } |
| 463 | |
| 464 | for (current = current - 1; current >= 0; --current) { |
| 465 | current_entry = &table->entries[current]; |
| 466 | dontcheck = strncmp((char *) entry->partition_guid, |
| 467 | (char *) current_entry->partition_guid , 16) == 0; |
| 468 | dontcheck |= current_entry->type_guid[0] == 0; |
| 469 | dontcheck |= current_entry == exclude; |
| 470 | |
| 471 | if (!dontcheck && ((entry->last_lba >= current_entry->first_lba && |
| 472 | entry->first_lba < current_entry->last_lba ))) { |
| 473 | return 1; |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | static int GPT_check_overlap(struct GPT_entry_table *table, struct GPT_entry_raw *entry) |
| 481 | { |
| 482 | return GPT_check_overlap_except(table, entry, NULL); |
| 483 | } |
| 484 | |
| 485 | static char *get_key_value(char *ptr, char **key, char **value) |
| 486 | { |
| 487 | *key = ptr; |
| 488 | ptr = strchr(ptr, '='); |
| 489 | |
| 490 | if (ptr == NULL) |
| 491 | return NULL; |
| 492 | |
| 493 | *ptr++ = '\0'; |
| 494 | *value = ptr; |
| 495 | ptr = strchr(ptr, ';'); |
| 496 | |
| 497 | if (ptr == NULL) |
| 498 | ptr = *value + strlen(*value); |
| 499 | else |
| 500 | *ptr = '\0'; |
| 501 | |
| 502 | *key = strip(*key); |
| 503 | *value = strip(*value); |
| 504 | |
| 505 | return ptr; |
| 506 | } |
| 507 | |
| 508 | //TODO: little endian? |
| 509 | static int add_key_value(const char *key, const char *value, struct GPT_entry_raw *entry) |
| 510 | { |
| 511 | char *endptr; |
| 512 | if (!strcmp(key, "type")) { |
| 513 | strncpy((char *) entry->type_guid, value, 16); |
| 514 | entry->type_guid[15] = 0; |
| 515 | } |
| 516 | else if (!strcmp(key, "guid")) { |
| 517 | strncpy((char *) entry->partition_guid, value, 16); |
| 518 | entry->type_guid[15] = 0; |
| 519 | } |
| 520 | else if (!strcmp(key, "firstlba")) { |
| 521 | entry->first_lba = strtoul(value, &endptr, 10); |
| 522 | if (*endptr != '\0') goto error; |
| 523 | } |
| 524 | else if (!strcmp(key, "lastlba")) { |
| 525 | entry->last_lba = strtoul(value, &endptr, 10); |
| 526 | if (*endptr != '\0') goto error; |
| 527 | } |
| 528 | else if (!strcmp(key, "flags")) { |
Szymon Starzycki | b88fa32 | 2013-09-17 14:17:32 -0700 | [diff] [blame] | 529 | entry->flags = strtoul(value, &endptr, 16); |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 530 | if (*endptr != '\0') goto error; |
| 531 | } |
| 532 | else if (!strcmp(key, "name")) { |
| 533 | GPT_to_UTF16(entry->name, value, GPT_NAMELEN); |
| 534 | } |
| 535 | else { |
| 536 | goto error; |
| 537 | } |
| 538 | |
| 539 | return 0; |
| 540 | |
| 541 | error: |
| 542 | D(ERR, "Could not find key or parse value: %s,%s", key, value); |
| 543 | return 1; |
| 544 | } |
| 545 | |
| 546 | int GPT_parse_entry(char *string, struct GPT_entry_raw *entry) |
| 547 | { |
| 548 | char *ptr = string; |
| 549 | char *key, *value; |
| 550 | |
| 551 | while ((ptr = get_key_value(ptr, &key, &value)) != NULL) { |
| 552 | if (add_key_value(key, value, entry)) { |
| 553 | D(WARN, "key or value not valid: %s %s", key, value); |
| 554 | return 1; |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | return 0; |
| 559 | } |
| 560 | |
Szymon Starzycki | b88fa32 | 2013-09-17 14:17:32 -0700 | [diff] [blame] | 561 | void entry_set_guid(int n, uint8_t *guid) |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 562 | { |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 563 | int fd; |
| 564 | fd = open("/dev/urandom", O_RDONLY); |
Szymon Starzycki | baf4c4b | 2013-10-04 10:28:03 -0700 | [diff] [blame] | 565 | read(fd, guid, 16); |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 566 | close(fd); |
Szymon Starzycki | b88fa32 | 2013-09-17 14:17:32 -0700 | [diff] [blame] | 567 | |
| 568 | //rfc4122 |
| 569 | guid[8] = (guid[8] & 0x3F) | 0x80; |
| 570 | guid[7] = (guid[7] & 0x0F) | 0x40; |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | void GPT_default_content(struct GPT_content *content, struct GPT_entry_table *table) |
| 574 | { |
| 575 | if (table != NULL) { |
| 576 | memcpy(&content->header, table->header, sizeof(content->header)); |
Szymon Starzycki | b88fa32 | 2013-09-17 14:17:32 -0700 | [diff] [blame] | 577 | content->header.header_size = sizeof(content->header); |
| 578 | content->header.entry_size = sizeof(struct GPT_entry_raw); |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 579 | } |
| 580 | else { |
| 581 | D(WARN, "Could not locate old gpt table, using default values"); |
| 582 | memset(&content->header, 0, sizeof(content->header) / sizeof(int)); |
| 583 | content->header = (struct GPT_header) { |
Szymon Starzycki | b88fa32 | 2013-09-17 14:17:32 -0700 | [diff] [blame] | 584 | .revision = 0x10000, |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 585 | .header_size = sizeof(content->header), |
| 586 | .header_checksum = 0, |
| 587 | .reserved_zeros = 0, |
| 588 | .current_lba = 1, |
| 589 | .backup_lba = 1, |
| 590 | .entry_size = sizeof(struct GPT_entry_raw), |
| 591 | .partition_array_checksum = 0 |
| 592 | }; |
| 593 | strncpy((char *)content->header.signature, "EFI PART", 8); |
Szymon Starzycki | b88fa32 | 2013-09-17 14:17:32 -0700 | [diff] [blame] | 594 | strncpy((char *)content->header.disk_guid, "ANDROID MMC DISK", 16); |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 595 | } |
| 596 | } |
| 597 | |
| 598 | static int get_config_uint64(cnode *node, uint64_t *ptr, const char *name) |
| 599 | { |
| 600 | const char *tmp; |
| 601 | uint64_t val; |
| 602 | char *endptr; |
| 603 | if ((tmp = config_str(node, name, NULL))) { |
| 604 | val = strtoull(tmp, &endptr, 10); |
| 605 | if (*endptr != '\0') { |
| 606 | D(WARN, "Value for %s is not a number: %s", name, tmp); |
| 607 | return 1; |
| 608 | } |
| 609 | *ptr = val; |
| 610 | return 0; |
| 611 | } |
| 612 | return 1; |
| 613 | } |
| 614 | |
Szymon Starzycki | b88fa32 | 2013-09-17 14:17:32 -0700 | [diff] [blame] | 615 | static int get_config_string(cnode *node, char *ptr, int max_len, const char *name) |
| 616 | { |
| 617 | size_t begin, end; |
| 618 | const char *value = config_str(node, name, NULL); |
| 619 | if (!value) |
| 620 | return -1; |
| 621 | |
| 622 | begin = strcspn(value, "\"") + 1; |
| 623 | end = strcspn(&value[begin], "\""); |
| 624 | |
| 625 | if ((int) end > max_len) { |
| 626 | D(WARN, "Identifier \"%s\" too long", value); |
| 627 | return -1; |
| 628 | } |
| 629 | |
| 630 | strncpy(ptr, &value[begin], end); |
| 631 | if((int) end < max_len) |
| 632 | ptr[end] = 0; |
| 633 | return 0; |
| 634 | } |
| 635 | |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 636 | static void GPT_parse_header(cnode *node, struct GPT_content *content) |
| 637 | { |
| 638 | get_config_uint64(node, &content->header.current_lba, "header_lba"); |
| 639 | get_config_uint64(node, &content->header.backup_lba, "backup_lba"); |
| 640 | get_config_uint64(node, &content->header.first_usable_lba, "first_lba"); |
| 641 | get_config_uint64(node, &content->header.last_usable_lba, "last_lba"); |
| 642 | get_config_uint64(node, &content->header.entries_lba, "entries_lba"); |
Szymon Starzycki | b88fa32 | 2013-09-17 14:17:32 -0700 | [diff] [blame] | 643 | get_config_string(node, (char *) content->header.disk_guid, 16, "guid"); |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | static int GPT_parse_partitions(cnode *node, struct GPT_content *content) |
| 647 | { |
| 648 | cnode *current; |
| 649 | int i; |
| 650 | uint64_t partition_size; |
Szymon Starzycki | b88fa32 | 2013-09-17 14:17:32 -0700 | [diff] [blame] | 651 | struct GPT_entry_raw *entry; |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 652 | for (i = 0, current = node->first_child; current; current = current->next, ++i) { |
Szymon Starzycki | b88fa32 | 2013-09-17 14:17:32 -0700 | [diff] [blame] | 653 | entry = &content->entries[i]; |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 654 | entry_set_guid(i, content->entries[i].partition_guid); |
| 655 | memcpy(&content->entries[i].type_guid, partition_type_uuid, 16); |
Szymon Starzycki | b88fa32 | 2013-09-17 14:17:32 -0700 | [diff] [blame] | 656 | if (get_config_uint64(current, &entry->first_lba, "first_lba")) { |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 657 | D(ERR, "first_lba not specified"); |
| 658 | return 1; |
| 659 | } |
| 660 | if (get_config_uint64(current, &partition_size, "partition_size")) { |
| 661 | D(ERR, "partition_size not specified"); |
| 662 | return 1; |
| 663 | } |
Szymon Starzycki | b88fa32 | 2013-09-17 14:17:32 -0700 | [diff] [blame] | 664 | if (config_str(current, "system", NULL)) { |
| 665 | entry->flags |= GPT_FLAG_SYSTEM; |
| 666 | } |
| 667 | if (config_str(current, "bootable", NULL)) { |
| 668 | entry->flags |= GPT_FLAG_BOOTABLE; |
| 669 | } |
| 670 | if (config_str(current, "readonly", NULL)) { |
| 671 | entry->flags |= GPT_FLAG_READONLY; |
| 672 | } |
| 673 | if (config_str(current, "automount", NULL)) { |
| 674 | entry->flags |= GPT_FLAG_DOAUTOMOUNT; |
| 675 | } |
| 676 | |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 677 | get_config_uint64(current, &content->entries[i].flags, "flags"); |
| 678 | content->entries[i].last_lba = content->entries[i].first_lba + partition_size - 1; |
| 679 | GPT_to_UTF16(content->entries[i].name, current->name, 16); |
| 680 | } |
| 681 | return 0; |
| 682 | } |
| 683 | |
| 684 | static inline int cnode_count(cnode *node) |
| 685 | { |
| 686 | int i; |
| 687 | cnode *current; |
| 688 | for (i = 0, current = node->first_child; current; current = current->next, ++i) |
| 689 | ; |
| 690 | return i; |
| 691 | } |
| 692 | |
| 693 | |
| 694 | static int GPT_parse_cnode(cnode *root, struct GPT_content *content) |
| 695 | { |
| 696 | cnode *partnode; |
| 697 | |
| 698 | if (!(partnode = config_find(root, "partitions"))) { |
| 699 | D(ERR, "Could not find partition table"); |
| 700 | return 0; |
| 701 | } |
| 702 | |
| 703 | GPT_parse_header(root, content); |
| 704 | |
| 705 | content->header.entries_count = cnode_count(partnode); |
| 706 | content->entries = malloc(content->header.entries_count * sizeof(struct GPT_entry_raw)); |
| 707 | |
| 708 | if (GPT_parse_partitions(partnode, content)) { |
| 709 | D(ERR, "Could not parse partitions"); |
| 710 | return 0; |
| 711 | } |
| 712 | |
| 713 | return 1; |
| 714 | } |
| 715 | |
| 716 | int GPT_parse_file(int fd, struct GPT_content *content) |
| 717 | { |
| 718 | char *data; |
| 719 | int size; |
| 720 | int ret; |
| 721 | cnode *root = config_node("", ""); |
| 722 | |
| 723 | size = get_file_size(fd); |
| 724 | data = (char *) mmap(NULL, size + 1, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); |
| 725 | |
| 726 | if (data == NULL) { |
| 727 | if (size == 0) |
| 728 | D(ERR, "config file empty"); |
| 729 | else |
| 730 | D(ERR, "Out of memory"); |
| 731 | return 0; |
| 732 | } |
| 733 | |
| 734 | data[size - 1] = 0; |
| 735 | config_load(root, data); |
| 736 | |
| 737 | if (root->first_child == NULL) { |
| 738 | D(ERR, "Could not read config file"); |
| 739 | return 0; |
| 740 | } |
| 741 | |
| 742 | ret = GPT_parse_cnode(root, content); |
| 743 | munmap(data, size); |
| 744 | return ret; |
| 745 | } |
| 746 | |
| 747 | void GPT_release_content(struct GPT_content *content) |
| 748 | { |
| 749 | free(content->entries); |
| 750 | } |
| 751 | |
| 752 | int GPT_write_content(const char *device, struct GPT_content *content) |
| 753 | { |
| 754 | struct GPT_entry_table *maptable; |
| 755 | |
| 756 | maptable = GPT_get_from_content(device, content); |
| 757 | if (maptable == NULL) { |
| 758 | D(ERR, "could not map device"); |
| 759 | return 0; |
| 760 | } |
| 761 | |
| 762 | memcpy(maptable->header, &content->header, sizeof(*maptable->header)); |
| 763 | memcpy(maptable->entries, content->entries, |
| 764 | content->header.entries_count * sizeof(*maptable->entries)); |
| 765 | |
Szymon Starzycki | b88fa32 | 2013-09-17 14:17:32 -0700 | [diff] [blame] | 766 | GPT_sync(maptable); |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 767 | GPT_release_device(maptable); |
| 768 | |
| 769 | return 1; |
| 770 | } |
| 771 | |