blob: 3b279590e98dcd884b55ab0a26743c043811d192 [file] [log] [blame]
Szymon Starzyckib6c5f282013-07-24 17:08:04 -07001/*
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>
Elliott Hughes0badbd62014-12-29 12:24:25 -080034#include <errno.h>
Szymon Starzyckib6c5f282013-07-24 17:08:04 -070035#include <fcntl.h>
36#include <sys/mman.h>
37#include <sys/stat.h>
38#include <sys/types.h>
39#include <unistd.h>
40#include <endian.h>
41#include <zlib.h>
42#include <linux/hdreg.h>
43#include <sys/ioctl.h>
44#include <stdlib.h>
45#include <cutils/config_utils.h>
Elliott Hughes4f241252014-01-14 16:17:08 -080046#include <inttypes.h>
Szymon Starzyckib6c5f282013-07-24 17:08:04 -070047
48#include "partitions.h"
49#include "debug.h"
50#include "utils.h"
51#include "protocol.h"
52
Szymon Starzyckib6c5f282013-07-24 17:08:04 -070053#define BLKRRPART _IO(0x12,95) /* re-read partition table */
54#define BLKSSZGET _IO(0x12,104)
55
56#define DIV_ROUND_UP(x, y) (((x) + (y) - 1)/(y))
57#define ALIGN(x, y) ((y) * DIV_ROUND_UP((x), (y)))
58#define ALIGN_DOWN(x, y) ((y) * ((x) / (y)))
59
60
Szymon Starzyckib88fa322013-09-17 14:17:32 -070061const uint8_t partition_type_uuid[16] = {
Szymon Starzyckib6c5f282013-07-24 17:08:04 -070062 0xa2, 0xa0, 0xd0, 0xeb, 0xe5, 0xb9, 0x33, 0x44,
63 0x87, 0xc0, 0x68, 0xb6, 0xb7, 0x26, 0x99, 0xc7,
64};
65
Szymon Starzyckibaf4c4b2013-10-04 10:28:03 -070066//TODO: There is assumption that we are using little endian
Szymon Starzyckib6c5f282013-07-24 17:08:04 -070067
68static void GPT_entry_clear(struct GPT_entry_raw *entry)
69{
70 memset(entry, 0, sizeof(*entry));
71}
72
Szymon Starzyckib6c5f282013-07-24 17:08:04 -070073/*
74 * returns mapped location to choosen area
75 * mapped_ptr is pointer to whole area mapped (it can be bigger then requested)
76 */
77int gpt_mmap(struct GPT_mapping *mapping, uint64_t location, int size, int fd)
78{
79 unsigned int location_diff = location & ~PAGE_MASK;
80
81 mapping->size = ALIGN(size + location_diff, PAGE_SIZE);
82
83 uint64_t sz = get_file_size64(fd);
84 if (sz < size + location) {
Elliott Hughes4f241252014-01-14 16:17:08 -080085 D(ERR, "the location of mapping area is outside of the device size %" PRId64, sz);
Szymon Starzyckib6c5f282013-07-24 17:08:04 -070086 return 1;
87 }
Szymon Starzyckibaf4c4b2013-10-04 10:28:03 -070088 location = ALIGN_DOWN(location, PAGE_SIZE);
Szymon Starzyckib6c5f282013-07-24 17:08:04 -070089
90 mapping->map_ptr = mmap64(NULL, mapping->size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, location);
91
92 if (mapping->map_ptr == MAP_FAILED) {
93 mapping->ptr = MAP_FAILED;
Elliott Hughes4f241252014-01-14 16:17:08 -080094 D(ERR, "map failed: %s", strerror(errno));
Szymon Starzyckib6c5f282013-07-24 17:08:04 -070095 return 1;
96 }
97
98 mapping->ptr = (void *)((char *) mapping->map_ptr + location_diff);
99 return 0;
100}
101
102void gpt_unmap(struct GPT_mapping *mapping) {
103 munmap(mapping->map_ptr, mapping->size);
104}
105
106
107#define LBA_ADDR(table, value) ((uint64_t) (table)->sector_size * (value))
108
109int GPT_map_from_content(struct GPT_entry_table *table, const struct GPT_content *content)
110{
111
112 // Mapping header
113 if (gpt_mmap(&table->header_map, LBA_ADDR(table, content->header.current_lba),
114 table->sector_size, table->fd)) {
115 D(ERR, "unable to map header:%s\n", strerror(errno));
116 goto error_header;
117 }
118
119 table->header = (struct GPT_header *) table->header_map.ptr;
120
121 table->partition_table_size = ROUND_UP(content->header.entries_count * sizeof(*table->entries),
122 table->sector_size);
123
124 // Mapping entry table
125 if (gpt_mmap(&table->entries_map, LBA_ADDR(table, content->header.entries_lba),
126 table->partition_table_size, table->fd)) {
127 D(ERR, "unable to map entries");
128 goto error_signature;
129 }
130
131 table->entries = (struct GPT_entry_raw *) table->entries_map.ptr;
132
133 // Mapping secondary header
134 if (gpt_mmap(&table->sec_header_map, LBA_ADDR(table, content->header.backup_lba),
135 table->sector_size, table->fd)) {
136 D(ERR, "unable to map backup gpt header");
137 goto error_sec_header;
138 }
139
140 // Mapping secondary entries table
141 if (gpt_mmap(&table->sec_entries_map,
142 LBA_ADDR(table, content->header.backup_lba) - table->partition_table_size,
143 table->partition_table_size, table->fd)) {
144 D(ERR, "unable to map secondary gpt table");
145 goto error_sec_entries;
146 }
147
148 table->second_header = (struct GPT_header *) table->sec_header_map.ptr;
149 table->second_entries = (struct GPT_entry_raw *) table->sec_entries_map.ptr;
150 table->second_valid = strcmp("EFI PART", (char *) table->second_header->signature) == 0;
151
152 return 0;
153
154error_sec_entries:
155 gpt_unmap(&table->sec_header_map);
156error_sec_header:
157 gpt_unmap(&table->entries_map);
158error_signature:
159 gpt_unmap(&table->header_map);
160error_header:
161 return 1;
162}
163
164int GPT_map(struct GPT_entry_table *table, unsigned header_lba)
165{
166 struct GPT_content content;
167 struct GPT_mapping mapping;
168 struct GPT_header *header;
169
170 if (gpt_mmap(&mapping, LBA_ADDR(table, header_lba), table->sector_size, table->fd)) {
171 D(ERR, "unable to map header: %s", strerror(errno));
172 goto error_header;
173 }
174
175 header = (struct GPT_header *) mapping.ptr;
176
177 if (strcmp("EFI PART", (char *) header->signature)) {
178 D(ERR, "GPT entry not valid");
179 goto error_signature;
180 }
181
182 content.header = *header;
183
184 gpt_unmap(&mapping);
185
186 return GPT_map_from_content(table, &content);
187
188error_signature:
189 gpt_unmap(&table->header_map);
190error_header:
191 return 1;
192}
193
194struct GPT_entry_table* GPT_get_device(const char *path, unsigned header_lba)
195{
196 struct GPT_entry_table *table;
197 size_t sector_bytes;
198
199 table = (struct GPT_entry_table *) malloc(sizeof(*table));
200 table->fd = open(path, O_RDWR);
201
202 if (table->fd < 0) {
203 D(ERR, "unable to open file %s:%s\n", path, strerror(errno));
204 return NULL;
205 }
206
207 if (!ioctl(table->fd, BLKSSZGET, &sector_bytes)) {
208 table->sector_size = (unsigned) sector_bytes;
209 D(INFO, "Got sector size %d", table->sector_size);
210 } else {
211 D(WARN, "unable to get sector size, assuming 512");
212 table->sector_size = 512;
213 }
214
215 if (GPT_map(table, header_lba)) {
216 D(ERR, "Could not map gpt");
217 return NULL;
218 }
219
220 return table;
221}
222
223static struct GPT_entry_table* GPT_get_from_content(const char *path, const struct GPT_content *content)
224{
225 struct GPT_entry_table *table;
226 size_t sector_bytes;
227
228 table = (struct GPT_entry_table *) malloc(sizeof(*table));
229 table->fd = open(path, O_RDWR);
230
231 if (table->fd < 0) {
232 D(ERR, "unable to open file %s:%s\n", path, strerror(errno));
233 return NULL;
234 }
235
236 if (!ioctl(table->fd, BLKSSZGET, &sector_bytes)) {
237 table->sector_size = (unsigned) sector_bytes;
238 D(INFO, "Got sector size %d", table->sector_size);
239 } else {
240 D(WARN, "unable to get sector size %s, assuming 512", strerror(errno));
241 table->sector_size = 512;
242 }
243
244 if (GPT_map_from_content(table, content)) {
245 D(ERR, "Could not map gpt");
246 return NULL;
247 }
248
249 return table;
250}
251
252
253void GPT_release_device(struct GPT_entry_table *table)
254{
255 gpt_unmap(&table->header_map);
256 gpt_unmap(&table->entries_map);
257 gpt_unmap(&table->sec_header_map);
258 gpt_unmap(&table->sec_entries_map);
259 close(table->fd);
260 free(table);
261}
262
263static int GPT_check_overlap(struct GPT_entry_table *table, struct GPT_entry_raw *entry);
264static int GPT_check_overlap_except(struct GPT_entry_table *table,
265 struct GPT_entry_raw *entry,
266 struct GPT_entry_raw *exclude);
267
268void GPT_edit_entry(struct GPT_entry_table *table,
269 struct GPT_entry_raw *old_entry,
270 struct GPT_entry_raw *new_entry)
271{
272 struct GPT_entry_raw *current_entry = GPT_get_pointer(table, old_entry);
273
274 if (GPT_check_overlap_except(table, new_entry, current_entry)) {
275 D(ERR, "Couldn't add overlaping partition");
276 return;
277 }
278
279 if (current_entry == NULL) {
280 D(ERR, "Couldn't find entry");
281 return;
282 }
283
284 *current_entry = *new_entry;
285}
286
287int GPT_delete_entry(struct GPT_entry_table *table, struct GPT_entry_raw *entry)
288{
289 struct GPT_entry_raw *raw = GPT_get_pointer(table, entry);
290
291 if (raw == NULL) {
292 D(ERR, "could not find entry");
293 return 1;
294 }
295 D(DEBUG, "Deleting gpt entry '%s'\n", raw->partition_guid);
296
Szymon Starzyckibaf4c4b2013-10-04 10:28:03 -0700297 // Entry in the middle of table may become empty
Szymon Starzyckib6c5f282013-07-24 17:08:04 -0700298 GPT_entry_clear(raw);
299
300 return 0;
301}
302
303void GPT_add_entry(struct GPT_entry_table *table, struct GPT_entry_raw *entry)
304{
305 unsigned i;
306 int inserted = 0;
307 if (GPT_check_overlap(table, entry)) {
308 D(ERR, "Couldn't add overlaping partition");
309 return;
310 }
311
312 if (GPT_get_pointer(table, entry) != NULL) {
313 D(WARN, "Add entry fault, this entry already exists");
314 return;
315 }
316
317 struct GPT_entry_raw *entries = table->entries;
318
319 for (i = 0; i < table->header->entries_count; ++i) {
320 if (!entries[i].type_guid[0]) {
321 inserted = 1;
322 D(DEBUG, "inserting");
323 memcpy(&entries[i], entry, sizeof(entries[i]));
324 break;
325 }
326 }
327
328 if (!inserted) {
329 D(ERR, "Unable to find empty partion entry");
330 }
331}
332
333struct GPT_entry_raw *GPT_get_pointer_by_UTFname(struct GPT_entry_table *table, const uint16_t *name);
334
335struct GPT_entry_raw *GPT_get_pointer(struct GPT_entry_table *table, struct GPT_entry_raw *entry)
336{
337 if (entry->partition_guid[0] != 0)
338 return GPT_get_pointer_by_guid(table, (const char *) entry->partition_guid);
339 else if (entry->name[0] != 0)
340 return GPT_get_pointer_by_UTFname(table, entry->name);
341
342 D(WARN, "Name or guid needed to find entry");
343 return NULL;
344}
345
346struct GPT_entry_raw *GPT_get_pointer_by_guid(struct GPT_entry_table *table, const char *name)
347{
348 int current = (int) table->header->entries_count;
349
350 for (current = current - 1; current >= 0; --current) {
351 if (strncmp((char *) name,
352 (char *) table->entries[current].partition_guid, 16) == 0) {
353 return &table->entries[current];
354 }
355 }
356
357 return NULL;
358}
359
360int strncmp_UTF16_char(const uint16_t *s1, const char *s2, size_t n)
361{
362 if (n == 0)
363 return (0);
364 do {
365 if (((*s1) & 127) != *s2++)
366 return (((unsigned char) ((*s1) & 127)) - *(unsigned char *)--s2);
367 if (*s1++ == 0)
368 break;
369 } while (--n != 0);
370 return (0);
371}
372
373int strncmp_UTF16(const uint16_t *s1, const uint16_t *s2, size_t n)
374{
375 if (n == 0)
376 return (0);
377 do {
378 if ((*s1) != *s2++)
379 return (*s1 - *--s2);
380 if (*s1++ == 0)
381 break;
382 } while (--n != 0);
383 return (0);
384}
385
386struct GPT_entry_raw *GPT_get_pointer_by_name(struct GPT_entry_table *table, const char *name)
387{
Szymon Starzyckib6c5f282013-07-24 17:08:04 -0700388 int count = (int) table->header->entries_count;
389 int current;
390
391 for (current = 0; current < count; ++current) {
392 if (strncmp_UTF16_char(table->entries[current].name,
393 (char *) name, 16) == 0) {
394 return &table->entries[current];
395 }
396 }
397
398 return NULL;
399}
400
401struct GPT_entry_raw *GPT_get_pointer_by_UTFname(struct GPT_entry_table *table, const uint16_t *name)
402{
403 int count = (int) table->header->entries_count;
404 int current;
405
406 for (current = 0; current < count; ++current) {
407 if (strncmp_UTF16(table->entries[current].name,
408 name, GPT_NAMELEN) == 0) {
409 return &table->entries[current];
410 }
411 }
412
413 return NULL;
414}
415
416void GPT_sync(struct GPT_entry_table *table)
417{
418 uint32_t crc;
419
420 //calculate crc32
421 crc = crc32(0, Z_NULL, 0);
422 crc = crc32(crc, (void*) table->entries, table->header->entries_count * sizeof(*table->entries));
423 table->header->partition_array_checksum = crc;
424
425 table->header->header_checksum = 0;
426 crc = crc32(0, Z_NULL, 0);
Szymon Starzyckib88fa322013-09-17 14:17:32 -0700427 crc = crc32(crc, (void*) table->header, table->header->header_size);
Szymon Starzyckib6c5f282013-07-24 17:08:04 -0700428 table->header->header_checksum = crc;
429
430 //sync secondary partion
431 if (table->second_valid) {
432 memcpy((void *)table->second_entries, (void *) table->entries, table->partition_table_size);
433 memcpy((void *)table->second_header, (void *)table->header, sizeof(*table->header));
434 }
435
436 if(!ioctl(table->fd, BLKRRPART, NULL)) {
437 D(WARN, "Unable to force kernel to refresh partition table");
438 }
439}
440
441void GPT_to_UTF16(uint16_t *to, const char *from, int n)
442{
443 int i;
444 for (i = 0; i < (n - 1) && (to[i] = from[i]) != '\0'; ++i);
445 to[i] = '\0';
446}
447
448void GPT_from_UTF16(char *to, const uint16_t *from, int n)
449{
450 int i;
451 for (i = 0; i < (n - 1) && (to[i] = from[i] & 127) != '\0'; ++i);
452 to[i] = '\0';
453}
454
455static int GPT_check_overlap_except(struct GPT_entry_table *table,
456 struct GPT_entry_raw *entry,
457 struct GPT_entry_raw *exclude) {
458 int current = (int) table->header->entries_count;
459 int dontcheck;
460 struct GPT_entry_raw *current_entry;
461 if (entry->last_lba < entry->first_lba) {
462 D(WARN, "Start address have to be less than end address");
463 return 1;
464 }
465
466 for (current = current - 1; current >= 0; --current) {
467 current_entry = &table->entries[current];
468 dontcheck = strncmp((char *) entry->partition_guid,
469 (char *) current_entry->partition_guid , 16) == 0;
470 dontcheck |= current_entry->type_guid[0] == 0;
471 dontcheck |= current_entry == exclude;
472
473 if (!dontcheck && ((entry->last_lba >= current_entry->first_lba &&
474 entry->first_lba < current_entry->last_lba ))) {
475 return 1;
476 }
477 }
478
479 return 0;
480}
481
482static int GPT_check_overlap(struct GPT_entry_table *table, struct GPT_entry_raw *entry)
483{
484 return GPT_check_overlap_except(table, entry, NULL);
485}
486
487static char *get_key_value(char *ptr, char **key, char **value)
488{
489 *key = ptr;
490 ptr = strchr(ptr, '=');
491
492 if (ptr == NULL)
493 return NULL;
494
495 *ptr++ = '\0';
496 *value = ptr;
497 ptr = strchr(ptr, ';');
498
499 if (ptr == NULL)
500 ptr = *value + strlen(*value);
501 else
502 *ptr = '\0';
503
504 *key = strip(*key);
505 *value = strip(*value);
506
507 return ptr;
508}
509
510//TODO: little endian?
511static int add_key_value(const char *key, const char *value, struct GPT_entry_raw *entry)
512{
513 char *endptr;
514 if (!strcmp(key, "type")) {
515 strncpy((char *) entry->type_guid, value, 16);
516 entry->type_guid[15] = 0;
517 }
518 else if (!strcmp(key, "guid")) {
519 strncpy((char *) entry->partition_guid, value, 16);
520 entry->type_guid[15] = 0;
521 }
522 else if (!strcmp(key, "firstlba")) {
523 entry->first_lba = strtoul(value, &endptr, 10);
524 if (*endptr != '\0') goto error;
525 }
526 else if (!strcmp(key, "lastlba")) {
527 entry->last_lba = strtoul(value, &endptr, 10);
528 if (*endptr != '\0') goto error;
529 }
530 else if (!strcmp(key, "flags")) {
Szymon Starzyckib88fa322013-09-17 14:17:32 -0700531 entry->flags = strtoul(value, &endptr, 16);
Szymon Starzyckib6c5f282013-07-24 17:08:04 -0700532 if (*endptr != '\0') goto error;
533 }
534 else if (!strcmp(key, "name")) {
535 GPT_to_UTF16(entry->name, value, GPT_NAMELEN);
536 }
537 else {
538 goto error;
539 }
540
541 return 0;
542
543error:
544 D(ERR, "Could not find key or parse value: %s,%s", key, value);
545 return 1;
546}
547
548int GPT_parse_entry(char *string, struct GPT_entry_raw *entry)
549{
550 char *ptr = string;
Dan Albert5c957e22014-09-22 15:26:39 -0700551 char *key = NULL;
552 char *value = NULL;
Szymon Starzyckib6c5f282013-07-24 17:08:04 -0700553
554 while ((ptr = get_key_value(ptr, &key, &value)) != NULL) {
555 if (add_key_value(key, value, entry)) {
556 D(WARN, "key or value not valid: %s %s", key, value);
557 return 1;
558 }
559 }
560
561 return 0;
562}
563
Szymon Starzyckib88fa322013-09-17 14:17:32 -0700564void entry_set_guid(int n, uint8_t *guid)
Szymon Starzyckib6c5f282013-07-24 17:08:04 -0700565{
Szymon Starzyckib6c5f282013-07-24 17:08:04 -0700566 int fd;
567 fd = open("/dev/urandom", O_RDONLY);
Szymon Starzyckibaf4c4b2013-10-04 10:28:03 -0700568 read(fd, guid, 16);
Szymon Starzyckib6c5f282013-07-24 17:08:04 -0700569 close(fd);
Szymon Starzyckib88fa322013-09-17 14:17:32 -0700570
571 //rfc4122
572 guid[8] = (guid[8] & 0x3F) | 0x80;
573 guid[7] = (guid[7] & 0x0F) | 0x40;
Szymon Starzyckib6c5f282013-07-24 17:08:04 -0700574}
575
576void GPT_default_content(struct GPT_content *content, struct GPT_entry_table *table)
577{
578 if (table != NULL) {
579 memcpy(&content->header, table->header, sizeof(content->header));
Szymon Starzyckib88fa322013-09-17 14:17:32 -0700580 content->header.header_size = sizeof(content->header);
581 content->header.entry_size = sizeof(struct GPT_entry_raw);
Szymon Starzyckib6c5f282013-07-24 17:08:04 -0700582 }
583 else {
584 D(WARN, "Could not locate old gpt table, using default values");
585 memset(&content->header, 0, sizeof(content->header) / sizeof(int));
586 content->header = (struct GPT_header) {
Szymon Starzyckib88fa322013-09-17 14:17:32 -0700587 .revision = 0x10000,
Szymon Starzyckib6c5f282013-07-24 17:08:04 -0700588 .header_size = sizeof(content->header),
589 .header_checksum = 0,
590 .reserved_zeros = 0,
591 .current_lba = 1,
592 .backup_lba = 1,
593 .entry_size = sizeof(struct GPT_entry_raw),
594 .partition_array_checksum = 0
595 };
596 strncpy((char *)content->header.signature, "EFI PART", 8);
Szymon Starzyckib88fa322013-09-17 14:17:32 -0700597 strncpy((char *)content->header.disk_guid, "ANDROID MMC DISK", 16);
Szymon Starzyckib6c5f282013-07-24 17:08:04 -0700598 }
599}
600
601static int get_config_uint64(cnode *node, uint64_t *ptr, const char *name)
602{
603 const char *tmp;
604 uint64_t val;
605 char *endptr;
606 if ((tmp = config_str(node, name, NULL))) {
607 val = strtoull(tmp, &endptr, 10);
608 if (*endptr != '\0') {
609 D(WARN, "Value for %s is not a number: %s", name, tmp);
610 return 1;
611 }
612 *ptr = val;
613 return 0;
614 }
615 return 1;
616}
617
Szymon Starzyckib88fa322013-09-17 14:17:32 -0700618static int get_config_string(cnode *node, char *ptr, int max_len, const char *name)
619{
620 size_t begin, end;
621 const char *value = config_str(node, name, NULL);
622 if (!value)
623 return -1;
624
625 begin = strcspn(value, "\"") + 1;
626 end = strcspn(&value[begin], "\"");
627
628 if ((int) end > max_len) {
629 D(WARN, "Identifier \"%s\" too long", value);
630 return -1;
631 }
632
633 strncpy(ptr, &value[begin], end);
634 if((int) end < max_len)
635 ptr[end] = 0;
636 return 0;
637}
638
Szymon Starzyckib6c5f282013-07-24 17:08:04 -0700639static void GPT_parse_header(cnode *node, struct GPT_content *content)
640{
641 get_config_uint64(node, &content->header.current_lba, "header_lba");
642 get_config_uint64(node, &content->header.backup_lba, "backup_lba");
643 get_config_uint64(node, &content->header.first_usable_lba, "first_lba");
644 get_config_uint64(node, &content->header.last_usable_lba, "last_lba");
645 get_config_uint64(node, &content->header.entries_lba, "entries_lba");
Szymon Starzyckib88fa322013-09-17 14:17:32 -0700646 get_config_string(node, (char *) content->header.disk_guid, 16, "guid");
Szymon Starzyckib6c5f282013-07-24 17:08:04 -0700647}
648
649static int GPT_parse_partitions(cnode *node, struct GPT_content *content)
650{
651 cnode *current;
652 int i;
653 uint64_t partition_size;
Szymon Starzyckib88fa322013-09-17 14:17:32 -0700654 struct GPT_entry_raw *entry;
Szymon Starzyckib6c5f282013-07-24 17:08:04 -0700655 for (i = 0, current = node->first_child; current; current = current->next, ++i) {
Szymon Starzyckib88fa322013-09-17 14:17:32 -0700656 entry = &content->entries[i];
Szymon Starzyckib6c5f282013-07-24 17:08:04 -0700657 entry_set_guid(i, content->entries[i].partition_guid);
658 memcpy(&content->entries[i].type_guid, partition_type_uuid, 16);
Szymon Starzyckib88fa322013-09-17 14:17:32 -0700659 if (get_config_uint64(current, &entry->first_lba, "first_lba")) {
Szymon Starzyckib6c5f282013-07-24 17:08:04 -0700660 D(ERR, "first_lba not specified");
661 return 1;
662 }
663 if (get_config_uint64(current, &partition_size, "partition_size")) {
664 D(ERR, "partition_size not specified");
665 return 1;
666 }
Szymon Starzyckib88fa322013-09-17 14:17:32 -0700667 if (config_str(current, "system", NULL)) {
668 entry->flags |= GPT_FLAG_SYSTEM;
669 }
670 if (config_str(current, "bootable", NULL)) {
671 entry->flags |= GPT_FLAG_BOOTABLE;
672 }
673 if (config_str(current, "readonly", NULL)) {
674 entry->flags |= GPT_FLAG_READONLY;
675 }
676 if (config_str(current, "automount", NULL)) {
677 entry->flags |= GPT_FLAG_DOAUTOMOUNT;
678 }
679
Szymon Starzyckib6c5f282013-07-24 17:08:04 -0700680 get_config_uint64(current, &content->entries[i].flags, "flags");
681 content->entries[i].last_lba = content->entries[i].first_lba + partition_size - 1;
682 GPT_to_UTF16(content->entries[i].name, current->name, 16);
683 }
684 return 0;
685}
686
687static inline int cnode_count(cnode *node)
688{
689 int i;
690 cnode *current;
691 for (i = 0, current = node->first_child; current; current = current->next, ++i)
692 ;
693 return i;
694}
695
696
697static int GPT_parse_cnode(cnode *root, struct GPT_content *content)
698{
699 cnode *partnode;
700
701 if (!(partnode = config_find(root, "partitions"))) {
702 D(ERR, "Could not find partition table");
703 return 0;
704 }
705
706 GPT_parse_header(root, content);
707
708 content->header.entries_count = cnode_count(partnode);
709 content->entries = malloc(content->header.entries_count * sizeof(struct GPT_entry_raw));
710
711 if (GPT_parse_partitions(partnode, content)) {
712 D(ERR, "Could not parse partitions");
713 return 0;
714 }
715
716 return 1;
717}
718
719int GPT_parse_file(int fd, struct GPT_content *content)
720{
721 char *data;
722 int size;
723 int ret;
724 cnode *root = config_node("", "");
725
726 size = get_file_size(fd);
727 data = (char *) mmap(NULL, size + 1, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
728
729 if (data == NULL) {
730 if (size == 0)
731 D(ERR, "config file empty");
732 else
733 D(ERR, "Out of memory");
734 return 0;
735 }
736
737 data[size - 1] = 0;
738 config_load(root, data);
739
740 if (root->first_child == NULL) {
741 D(ERR, "Could not read config file");
742 return 0;
743 }
744
745 ret = GPT_parse_cnode(root, content);
746 munmap(data, size);
747 return ret;
748}
749
750void GPT_release_content(struct GPT_content *content)
751{
752 free(content->entries);
753}
754
755int GPT_write_content(const char *device, struct GPT_content *content)
756{
757 struct GPT_entry_table *maptable;
758
759 maptable = GPT_get_from_content(device, content);
760 if (maptable == NULL) {
761 D(ERR, "could not map device");
762 return 0;
763 }
764
765 memcpy(maptable->header, &content->header, sizeof(*maptable->header));
766 memcpy(maptable->entries, content->entries,
767 content->header.entries_count * sizeof(*maptable->entries));
768
Szymon Starzyckib88fa322013-09-17 14:17:32 -0700769 GPT_sync(maptable);
Szymon Starzyckib6c5f282013-07-24 17:08:04 -0700770 GPT_release_device(maptable);
771
772 return 1;
773}
774