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