summaryrefslogtreecommitdiffstats
path: root/fastbootd/commands/partitions.c
blob: 3b279590e98dcd884b55ab0a26743c043811d192 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
/*
 * Copyright (c) 2009-2013, Google Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *  * Neither the name of Google, Inc. nor the names of its contributors
 *    may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <endian.h>
#include <zlib.h>
#include <linux/hdreg.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <cutils/config_utils.h>
#include <inttypes.h>

#include "partitions.h"
#include "debug.h"
#include "utils.h"
#include "protocol.h"

#define BLKRRPART  _IO(0x12,95) /* re-read partition table */
#define BLKSSZGET  _IO(0x12,104)

#define DIV_ROUND_UP(x, y) (((x) + (y) - 1)/(y))
#define ALIGN(x, y) ((y) * DIV_ROUND_UP((x), (y)))
#define ALIGN_DOWN(x, y) ((y) * ((x) / (y)))


const uint8_t partition_type_uuid[16] = {
    0xa2, 0xa0, 0xd0, 0xeb, 0xe5, 0xb9, 0x33, 0x44,
    0x87, 0xc0, 0x68, 0xb6, 0xb7, 0x26, 0x99, 0xc7,
};

//TODO: There is assumption that we are using little endian

static void GPT_entry_clear(struct GPT_entry_raw *entry)
{
    memset(entry, 0, sizeof(*entry));
}

/*
 * returns mapped location to choosen area
 * mapped_ptr is pointer to whole area mapped (it can be bigger then requested)
 */
int gpt_mmap(struct GPT_mapping *mapping, uint64_t location, int size, int fd)
{
    unsigned int location_diff = location & ~PAGE_MASK;

    mapping->size = ALIGN(size + location_diff, PAGE_SIZE);

    uint64_t sz = get_file_size64(fd);
    if (sz < size + location) {
        D(ERR, "the location of mapping area is outside of the device size %" PRId64, sz);
        return 1;
    }
    location = ALIGN_DOWN(location, PAGE_SIZE);

    mapping->map_ptr = mmap64(NULL, mapping->size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, location);

    if (mapping->map_ptr == MAP_FAILED) {
        mapping->ptr = MAP_FAILED;
        D(ERR, "map failed: %s", strerror(errno));
        return 1;
    }

    mapping->ptr = (void *)((char *) mapping->map_ptr + location_diff);
    return 0;
}

void gpt_unmap(struct GPT_mapping *mapping) {
    munmap(mapping->map_ptr, mapping->size);
}


#define LBA_ADDR(table, value)   ((uint64_t) (table)->sector_size * (value))

int GPT_map_from_content(struct GPT_entry_table *table, const struct GPT_content *content)
{

    // Mapping header
    if (gpt_mmap(&table->header_map, LBA_ADDR(table, content->header.current_lba),
                 table->sector_size, table->fd)) {
        D(ERR, "unable to map header:%s\n", strerror(errno));
        goto error_header;
    }

    table->header = (struct GPT_header *) table->header_map.ptr;

    table->partition_table_size = ROUND_UP(content->header.entries_count * sizeof(*table->entries),
                                           table->sector_size);

    // Mapping entry table
    if (gpt_mmap(&table->entries_map, LBA_ADDR(table, content->header.entries_lba),
                 table->partition_table_size, table->fd)) {
        D(ERR, "unable to map entries");
        goto error_signature;
    }

    table->entries = (struct GPT_entry_raw *) table->entries_map.ptr;

    // Mapping secondary header
    if (gpt_mmap(&table->sec_header_map, LBA_ADDR(table, content->header.backup_lba),
                 table->sector_size, table->fd)) {
        D(ERR, "unable to map backup gpt header");
        goto error_sec_header;
    }

    // Mapping secondary entries table
    if (gpt_mmap(&table->sec_entries_map,
                 LBA_ADDR(table, content->header.backup_lba) - table->partition_table_size,
                 table->partition_table_size, table->fd)) {
        D(ERR, "unable to map secondary gpt table");
        goto error_sec_entries;
    }

    table->second_header = (struct GPT_header *) table->sec_header_map.ptr;
    table->second_entries = (struct GPT_entry_raw *) table->sec_entries_map.ptr;
    table->second_valid = strcmp("EFI PART", (char *) table->second_header->signature) == 0;

    return 0;

error_sec_entries:
    gpt_unmap(&table->sec_header_map);
error_sec_header:
    gpt_unmap(&table->entries_map);
error_signature:
    gpt_unmap(&table->header_map);
error_header:
    return 1;
}

int GPT_map(struct GPT_entry_table *table, unsigned header_lba)
{
    struct GPT_content content;
    struct GPT_mapping mapping;
    struct GPT_header *header;

    if (gpt_mmap(&mapping, LBA_ADDR(table, header_lba), table->sector_size, table->fd)) {
        D(ERR, "unable to map header: %s", strerror(errno));
        goto error_header;
    }

    header = (struct GPT_header *) mapping.ptr;

    if (strcmp("EFI PART", (char *) header->signature)) {
        D(ERR, "GPT entry not valid");
        goto error_signature;
    }

    content.header = *header;

    gpt_unmap(&mapping);

    return GPT_map_from_content(table, &content);

error_signature:
    gpt_unmap(&table->header_map);
error_header:
    return 1;
}

struct GPT_entry_table* GPT_get_device(const char *path, unsigned header_lba)
{
    struct GPT_entry_table *table;
    size_t sector_bytes;

    table = (struct GPT_entry_table *) malloc(sizeof(*table));
    table->fd = open(path, O_RDWR);

    if (table->fd < 0) {
        D(ERR, "unable to open file %s:%s\n", path, strerror(errno));
        return NULL;
    }

    if (!ioctl(table->fd, BLKSSZGET, &sector_bytes)) {
        table->sector_size = (unsigned) sector_bytes;
        D(INFO, "Got sector size %d", table->sector_size);
    } else {
        D(WARN, "unable to get sector size, assuming 512");
        table->sector_size = 512;
    }

    if (GPT_map(table, header_lba)) {
        D(ERR, "Could not map gpt");
        return NULL;
    }

    return table;
}

static struct GPT_entry_table* GPT_get_from_content(const char *path, const struct GPT_content *content)
{
    struct GPT_entry_table *table;
    size_t sector_bytes;

    table = (struct GPT_entry_table *) malloc(sizeof(*table));
    table->fd = open(path, O_RDWR);

    if (table->fd < 0) {
        D(ERR, "unable to open file %s:%s\n", path, strerror(errno));
        return NULL;
    }

    if (!ioctl(table->fd, BLKSSZGET, &sector_bytes)) {
        table->sector_size = (unsigned) sector_bytes;
        D(INFO, "Got sector size %d", table->sector_size);
    } else {
        D(WARN, "unable to get sector size %s, assuming 512", strerror(errno));
        table->sector_size = 512;
    }

    if (GPT_map_from_content(table, content)) {
        D(ERR, "Could not map gpt");
        return NULL;
    }

    return table;
}


void GPT_release_device(struct GPT_entry_table *table)
{
    gpt_unmap(&table->header_map);
    gpt_unmap(&table->entries_map);
    gpt_unmap(&table->sec_header_map);
    gpt_unmap(&table->sec_entries_map);
    close(table->fd);
    free(table);
}

static int GPT_check_overlap(struct GPT_entry_table *table, struct GPT_entry_raw *entry);
static int GPT_check_overlap_except(struct GPT_entry_table *table,
                                    struct GPT_entry_raw *entry,
                                    struct GPT_entry_raw *exclude);

void GPT_edit_entry(struct GPT_entry_table *table,
                    struct GPT_entry_raw *old_entry,
                    struct GPT_entry_raw *new_entry)
{
    struct GPT_entry_raw *current_entry = GPT_get_pointer(table, old_entry);

    if (GPT_check_overlap_except(table, new_entry, current_entry)) {
        D(ERR, "Couldn't add overlaping partition");
        return;
    }

    if (current_entry == NULL) {
        D(ERR, "Couldn't find entry");
        return;
    }

    *current_entry = *new_entry;
}

int GPT_delete_entry(struct GPT_entry_table *table, struct GPT_entry_raw *entry)
{
    struct GPT_entry_raw *raw = GPT_get_pointer(table, entry);

    if (raw == NULL) {
        D(ERR, "could not find entry");
        return 1;
    }
    D(DEBUG, "Deleting gpt entry '%s'\n", raw->partition_guid);

    // Entry in the middle of table may become empty
    GPT_entry_clear(raw);

    return 0;
}

void GPT_add_entry(struct GPT_entry_table *table, struct GPT_entry_raw *entry)
{
    unsigned i;
    int inserted = 0;
    if (GPT_check_overlap(table, entry)) {
        D(ERR, "Couldn't add overlaping partition");
        return;
    }

    if (GPT_get_pointer(table, entry) != NULL) {
        D(WARN, "Add entry fault, this entry already exists");
        return;
    }

    struct GPT_entry_raw *entries = table->entries;

    for (i = 0; i < table->header->entries_count; ++i) {
        if (!entries[i].type_guid[0]) {
            inserted = 1;
            D(DEBUG, "inserting");
            memcpy(&entries[i], entry, sizeof(entries[i]));
            break;
        }
    }

    if (!inserted) {
        D(ERR, "Unable to find empty partion entry");
    }
}

struct GPT_entry_raw *GPT_get_pointer_by_UTFname(struct GPT_entry_table *table, const uint16_t *name);

struct GPT_entry_raw *GPT_get_pointer(struct GPT_entry_table *table, struct GPT_entry_raw *entry)
{
    if (entry->partition_guid[0] != 0)
        return GPT_get_pointer_by_guid(table, (const char *) entry->partition_guid);
    else if (entry->name[0] != 0)
        return GPT_get_pointer_by_UTFname(table, entry->name);

    D(WARN, "Name or guid needed to find entry");
    return NULL;
}

struct GPT_entry_raw *GPT_get_pointer_by_guid(struct GPT_entry_table *table, const char *name)
{
    int current = (int) table->header->entries_count;

    for (current = current - 1; current >= 0; --current) {
        if (strncmp((char *) name,
                    (char *) table->entries[current].partition_guid, 16) == 0) {
                return &table->entries[current];
        }
    }

    return NULL;
}

int strncmp_UTF16_char(const uint16_t *s1, const char *s2, size_t n)
{
    if (n == 0)
        return (0);
    do {
        if (((*s1) & 127) != *s2++)
            return (((unsigned char) ((*s1) & 127)) - *(unsigned char *)--s2);
        if (*s1++ == 0)
            break;
    } while (--n != 0);
    return (0);
}

int strncmp_UTF16(const uint16_t *s1, const uint16_t *s2, size_t n)
{
    if (n == 0)
        return (0);
    do {
        if ((*s1) != *s2++)
            return (*s1 - *--s2);
        if (*s1++ == 0)
            break;
    } while (--n != 0);
    return (0);
}

struct GPT_entry_raw *GPT_get_pointer_by_name(struct GPT_entry_table *table, const char *name)
{
    int count = (int) table->header->entries_count;
    int current;

    for (current = 0; current < count; ++current) {
        if (strncmp_UTF16_char(table->entries[current].name,
                         (char *) name, 16) == 0) {
                    return &table->entries[current];
        }
    }

    return NULL;
}

struct GPT_entry_raw *GPT_get_pointer_by_UTFname(struct GPT_entry_table *table, const uint16_t *name)
{
    int count = (int) table->header->entries_count;
    int current;

    for (current = 0; current < count; ++current) {
        if (strncmp_UTF16(table->entries[current].name,
                          name, GPT_NAMELEN) == 0) {
                return &table->entries[current];
        }
    }

    return NULL;
}

void GPT_sync(struct GPT_entry_table *table)
{
    uint32_t crc;

    //calculate crc32
    crc = crc32(0, Z_NULL, 0);
    crc = crc32(crc, (void*) table->entries, table->header->entries_count * sizeof(*table->entries));
    table->header->partition_array_checksum = crc;

    table->header->header_checksum = 0;
    crc = crc32(0, Z_NULL, 0);
    crc = crc32(crc, (void*) table->header, table->header->header_size);
    table->header->header_checksum = crc;

    //sync secondary partion
    if (table->second_valid) {
        memcpy((void *)table->second_entries, (void *) table->entries, table->partition_table_size);
        memcpy((void *)table->second_header, (void *)table->header, sizeof(*table->header));
    }

    if(!ioctl(table->fd, BLKRRPART, NULL)) {
        D(WARN, "Unable to force kernel to refresh partition table");
    }
}

void GPT_to_UTF16(uint16_t *to, const char *from, int n)
{
    int i;
    for (i = 0; i < (n - 1) && (to[i] = from[i]) != '\0'; ++i);
    to[i] = '\0';
}

void GPT_from_UTF16(char *to, const uint16_t *from, int n)
{
    int i;
    for (i = 0; i < (n - 1) && (to[i] = from[i] & 127) != '\0'; ++i);
    to[i] = '\0';
}

static int GPT_check_overlap_except(struct GPT_entry_table *table,
                                    struct GPT_entry_raw *entry,
                                    struct GPT_entry_raw *exclude) {
    int current = (int) table->header->entries_count;
    int dontcheck;
    struct GPT_entry_raw *current_entry;
    if (entry->last_lba < entry->first_lba) {
        D(WARN, "Start address have to be less than end address");
        return 1;
    }

    for (current = current - 1; current >= 0; --current) {
        current_entry = &table->entries[current];
        dontcheck = strncmp((char *) entry->partition_guid,
                           (char *) current_entry->partition_guid , 16) == 0;
        dontcheck |= current_entry->type_guid[0] == 0;
        dontcheck |= current_entry == exclude;

        if (!dontcheck && ((entry->last_lba >= current_entry->first_lba &&
                            entry->first_lba < current_entry->last_lba ))) {
            return 1;
        }
    }

    return 0;
}

static int GPT_check_overlap(struct GPT_entry_table *table, struct GPT_entry_raw *entry)
{
    return GPT_check_overlap_except(table, entry, NULL);
}

static char *get_key_value(char *ptr, char **key, char **value)
{
    *key = ptr;
    ptr = strchr(ptr, '=');

    if (ptr == NULL)
        return NULL;

    *ptr++ = '\0';
    *value = ptr;
    ptr = strchr(ptr, ';');

    if (ptr == NULL)
        ptr = *value + strlen(*value);
    else
        *ptr = '\0';

    *key = strip(*key);
    *value = strip(*value);

    return ptr;
}

//TODO: little endian?
static int add_key_value(const char *key, const char *value, struct GPT_entry_raw *entry)
{
    char *endptr;
    if (!strcmp(key, "type")) {
        strncpy((char *) entry->type_guid, value, 16);
        entry->type_guid[15] = 0;
    }
    else if (!strcmp(key, "guid")) {
        strncpy((char *) entry->partition_guid, value, 16);
        entry->type_guid[15] = 0;
    }
    else if (!strcmp(key, "firstlba")) {
        entry->first_lba = strtoul(value, &endptr, 10);
        if (*endptr != '\0') goto error;
    }
    else if (!strcmp(key, "lastlba")) {
        entry->last_lba = strtoul(value, &endptr, 10);
        if (*endptr != '\0') goto error;
    }
    else if (!strcmp(key, "flags")) {
        entry->flags = strtoul(value, &endptr, 16);
        if (*endptr != '\0') goto error;
    }
    else if (!strcmp(key, "name")) {
        GPT_to_UTF16(entry->name, value, GPT_NAMELEN);
    }
    else {
        goto error;
    }

    return 0;

error:
    D(ERR, "Could not find key or parse value: %s,%s", key, value);
    return 1;
}

int GPT_parse_entry(char *string, struct GPT_entry_raw *entry)
{
    char *ptr = string;
    char *key = NULL;
    char *value = NULL;

    while ((ptr = get_key_value(ptr, &key, &value)) != NULL) {
        if (add_key_value(key, value, entry)) {
            D(WARN, "key or value not valid: %s %s", key, value);
            return 1;
        }
    }

    return 0;
}

void entry_set_guid(int n, uint8_t *guid)
{
    int fd;
    fd = open("/dev/urandom", O_RDONLY);
    read(fd, guid, 16);
    close(fd);

    //rfc4122
    guid[8] = (guid[8] & 0x3F) | 0x80;
    guid[7] = (guid[7] & 0x0F) | 0x40;
}

void GPT_default_content(struct GPT_content *content, struct GPT_entry_table *table)
{
    if (table != NULL) {
        memcpy(&content->header, table->header, sizeof(content->header));
        content->header.header_size = sizeof(content->header);
        content->header.entry_size = sizeof(struct GPT_entry_raw);
    }
    else {
        D(WARN, "Could not locate old gpt table, using default values");
        memset(&content->header, 0, sizeof(content->header) / sizeof(int));
        content->header = (struct GPT_header) {
            .revision = 0x10000,
            .header_size = sizeof(content->header),
            .header_checksum = 0,
            .reserved_zeros = 0,
            .current_lba = 1,
            .backup_lba = 1,
            .entry_size = sizeof(struct GPT_entry_raw),
            .partition_array_checksum = 0
        };
        strncpy((char *)content->header.signature, "EFI PART", 8);
        strncpy((char *)content->header.disk_guid, "ANDROID MMC DISK", 16);
    }
}

static int get_config_uint64(cnode *node, uint64_t *ptr, const char *name)
{
    const char *tmp;
    uint64_t val;
    char *endptr;
    if ((tmp = config_str(node, name, NULL))) {
        val = strtoull(tmp, &endptr, 10);
        if (*endptr != '\0') {
            D(WARN, "Value for %s is not a number: %s", name, tmp);
            return 1;
        }
        *ptr = val;
        return 0;
    }
    return 1;
}

static int get_config_string(cnode *node, char *ptr, int max_len, const char *name)
{
    size_t begin, end;
    const char *value = config_str(node, name, NULL);
    if (!value)
        return -1;

    begin = strcspn(value, "\"") + 1;
    end = strcspn(&value[begin], "\"");

    if ((int) end > max_len) {
        D(WARN, "Identifier \"%s\" too long", value);
        return -1;
    }

    strncpy(ptr, &value[begin], end);
    if((int) end < max_len)
        ptr[end] = 0;
    return 0;
}

static void GPT_parse_header(cnode *node, struct GPT_content *content)
{
    get_config_uint64(node, &content->header.current_lba, "header_lba");
    get_config_uint64(node, &content->header.backup_lba, "backup_lba");
    get_config_uint64(node, &content->header.first_usable_lba, "first_lba");
    get_config_uint64(node, &content->header.last_usable_lba, "last_lba");
    get_config_uint64(node, &content->header.entries_lba, "entries_lba");
    get_config_string(node, (char *) content->header.disk_guid, 16, "guid");
}

static int GPT_parse_partitions(cnode *node, struct GPT_content *content)
{
    cnode *current;
    int i;
    uint64_t partition_size;
    struct GPT_entry_raw *entry;
    for (i = 0, current = node->first_child; current; current = current->next, ++i) {
        entry = &content->entries[i];
        entry_set_guid(i, content->entries[i].partition_guid);
        memcpy(&content->entries[i].type_guid, partition_type_uuid, 16);
        if (get_config_uint64(current, &entry->first_lba, "first_lba")) {
            D(ERR, "first_lba not specified");
            return 1;
        }
        if (get_config_uint64(current, &partition_size, "partition_size")) {
            D(ERR, "partition_size not specified");
            return 1;
        }
        if (config_str(current, "system", NULL)) {
            entry->flags |= GPT_FLAG_SYSTEM;
        }
        if (config_str(current, "bootable", NULL)) {
            entry->flags |= GPT_FLAG_BOOTABLE;
        }
        if (config_str(current, "readonly", NULL)) {
            entry->flags |= GPT_FLAG_READONLY;
        }
        if (config_str(current, "automount", NULL)) {
            entry->flags |= GPT_FLAG_DOAUTOMOUNT;
        }

        get_config_uint64(current, &content->entries[i].flags, "flags");
        content->entries[i].last_lba = content->entries[i].first_lba + partition_size - 1;
        GPT_to_UTF16(content->entries[i].name, current->name, 16);
    }
    return 0;
}

static inline int cnode_count(cnode *node)
{
    int i;
    cnode *current;
    for (i = 0, current = node->first_child; current; current = current->next, ++i)
        ;
    return i;
}


static int GPT_parse_cnode(cnode *root, struct GPT_content *content)
{
    cnode *partnode;

    if (!(partnode = config_find(root, "partitions"))) {
        D(ERR, "Could not find partition table");
        return 0;
    }

    GPT_parse_header(root, content);

    content->header.entries_count = cnode_count(partnode);
    content->entries = malloc(content->header.entries_count * sizeof(struct GPT_entry_raw));

    if (GPT_parse_partitions(partnode, content)) {
        D(ERR, "Could not parse partitions");
        return 0;
    }

    return 1;
}

int GPT_parse_file(int fd, struct GPT_content *content)
{
    char *data;
    int size;
    int ret;
    cnode *root = config_node("", "");

    size = get_file_size(fd);
    data = (char *) mmap(NULL, size + 1, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);

    if (data == NULL) {
        if (size == 0)
            D(ERR, "config file empty");
        else
            D(ERR, "Out of memory");
        return 0;
    }

    data[size - 1] = 0;
    config_load(root, data);

    if (root->first_child == NULL) {
        D(ERR, "Could not read config file");
        return 0;
    }

    ret = GPT_parse_cnode(root, content);
    munmap(data, size);
    return ret;
}

void GPT_release_content(struct GPT_content *content)
{
    free(content->entries);
}

int GPT_write_content(const char *device, struct GPT_content *content)
{
    struct GPT_entry_table *maptable;

    maptable = GPT_get_from_content(device, content);
    if (maptable == NULL) {
        D(ERR, "could not map device");
        return 0;
    }

    memcpy(maptable->header, &content->header, sizeof(*maptable->header));
    memcpy(maptable->entries, content->entries,
           content->header.entries_count * sizeof(*maptable->entries));

    GPT_sync(maptable);
    GPT_release_device(maptable);

    return 1;
}