source: mainline/uspace/lib/gpt/libgpt.c@ 9f4650c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9f4650c was 8559fa0, checked in by Dominik Taborsky (AT DOT) <brembyseznamcz>, 12 years ago

libgpt fixes

  • Property mode set to 100644
File size: 19.9 KB
Line 
1/*
2 * Copyright (c) 2011, 2012, 2013 Dominik Taborsky
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 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libgpt
30 * @{
31 */
32/** @file
33 */
34
35/* TODO:
36 * This implementation only supports fixed size partition entries. Specification
37 * requires otherwise, though. Use void * array and casting to achieve that.
38 */
39
40#include <ipc/bd.h>
41#include <async.h>
42#include <stdio.h>
43#include <block.h>
44#include <errno.h>
45#include <stdlib.h>
46#include <assert.h>
47#include <byteorder.h>
48#include <checksum.h>
49#include <mem.h>
50
51#include "libgpt.h"
52
53static int load_and_check_header(service_id_t handle, aoff64_t addr, size_t b_size, gpt_header_t *header);
54static gpt_partitions_t * alloc_part_array(uint32_t num);
55static int extend_part_array(gpt_partitions_t *);
56static int reduce_part_array(gpt_partitions_t *);
57static long long nearest_larger_int(double a);
58static uint8_t get_byte(const char *);
59static int check_overlap(gpt_part_t * p1, gpt_part_t * p2);
60
61/** Allocate memory for gpt label */
62gpt_label_t * gpt_alloc_label(void)
63{
64 gpt_label_t *label = malloc(sizeof(gpt_label_t));
65 if (label == NULL)
66 return NULL;
67
68 label->gpt = NULL;
69 label->parts = NULL;
70 label->device = 0;
71
72 return label;
73}
74
75/** Free gpt_label_t structure */
76void gpt_free_label(gpt_label_t *label)
77{
78 if (label->gpt != NULL)
79 gpt_free_gpt(label->gpt);
80
81 if (label->parts != NULL)
82 gpt_free_partitions(label->parts);
83
84 free(label);
85}
86
87/** Allocate memory for gpt header */
88gpt_t * gpt_alloc_header(size_t size)
89{
90 gpt_t *gpt = malloc(sizeof(gpt_t));
91 if (gpt == NULL)
92 return NULL;
93
94 // We might need only sizeof(gpt_header_t),
95 // but we should follow specs and have
96 // zeroes through all the rest of the block
97 size_t final_size = size > sizeof(gpt_header_t) ? size : sizeof(gpt_header_t);
98 gpt->header = malloc(final_size);
99 if (gpt->header == NULL) {
100 free(gpt);
101 return NULL;
102 }
103
104 memset(gpt->header, 0, final_size);
105
106 return gpt;
107}
108
109/** free() GPT header including gpt->header_lba */
110void gpt_free_gpt(gpt_t *gpt)
111{
112 free(gpt->header);
113 free(gpt);
114}
115
116/** Read GPT from specific device
117 * @param label label structure to fill
118 * @param dev_handle device to read GPT from
119 *
120 * @return EOK on success, errorcode on error
121 */
122int gpt_read_header(gpt_label_t *label, service_id_t dev_handle)
123{
124 int rc;
125 size_t b_size;
126
127 rc = block_init(EXCHANGE_ATOMIC, dev_handle, 512);
128 if (rc != EOK)
129 goto fail;
130
131 rc = block_get_bsize(dev_handle, &b_size);
132 if (rc != EOK)
133 goto fini_fail;
134
135 if (label->gpt == NULL) {
136 label->gpt = gpt_alloc_header(b_size);
137 if (label->gpt == NULL) {
138 rc = ENOMEM;
139 goto fini_fail;
140 }
141 }
142
143 rc = load_and_check_header(dev_handle, GPT_HDR_BA, b_size, label->gpt->header);
144 if (rc == EBADCHECKSUM || rc == EINVAL) {
145 aoff64_t n_blocks;
146 rc = block_get_nblocks(dev_handle, &n_blocks);
147 if (rc != EOK)
148 goto free_fail;
149
150 rc = load_and_check_header(dev_handle, n_blocks - 1, b_size, label->gpt->header);
151 if (rc == EBADCHECKSUM || rc == EINVAL)
152 goto free_fail;
153 }
154
155 label->device = dev_handle;
156 block_fini(dev_handle);
157 return EOK;
158
159free_fail:
160 gpt_free_gpt(label->gpt);
161 label->gpt = NULL;
162fini_fail:
163 block_fini(dev_handle);
164fail:
165 return rc;
166}
167
168/** Write GPT header to device
169 * @param label GPT label header to be written
170 * @param dev_handle device handle to write the data to
171 *
172 * @return EOK on success, libblock error code otherwise
173 *
174 * Note: Firstly write partitions (if modified), then gpt header.
175 */
176int gpt_write_header(gpt_label_t *label, service_id_t dev_handle)
177{
178 int rc;
179 size_t b_size;
180
181 label->gpt->header->header_crc32 = 0;
182 label->gpt->header->header_crc32 = compute_crc32((uint8_t *) label->gpt->header,
183 uint32_t_le2host(label->gpt->header->header_size));
184
185 rc = block_init(EXCHANGE_ATOMIC, dev_handle, b_size);
186 if (rc != EOK && rc != EEXIST)
187 return rc;
188
189 rc = block_get_bsize(dev_handle, &b_size);
190 if (rc != EOK)
191 return rc;
192
193 /* Write to main GPT header location */
194 rc = block_write_direct(dev_handle, GPT_HDR_BA, GPT_HDR_BS, label->gpt->header);
195 if (rc != EOK) {
196 block_fini(dev_handle);
197 return rc;
198 }
199
200 aoff64_t n_blocks;
201 rc = block_get_nblocks(dev_handle, &n_blocks);
202 if (rc != EOK) {
203 block_fini(dev_handle);
204 return rc;
205 }
206
207 /* Write to backup GPT header location */
208 //FIXME: those idiots thought it would be cool to have these fields in reverse order...
209 rc = block_write_direct(dev_handle, n_blocks - 1, GPT_HDR_BS, label->gpt->header);
210 block_fini(dev_handle);
211 if (rc != EOK)
212 return rc;
213
214 return 0;
215}
216
217/** Alloc partition array */
218gpt_partitions_t * gpt_alloc_partitions()
219{
220 return alloc_part_array(128);
221}
222
223/** Parse partitions from GPT
224 * @param label GPT label to be parsed
225 *
226 * @return EOK on success, errorcode otherwise
227 */
228int gpt_read_partitions(gpt_label_t *label)
229{
230 int rc;
231 unsigned int i;
232 uint32_t fill = uint32_t_le2host(label->gpt->header->fillries);
233 uint32_t ent_size = uint32_t_le2host(label->gpt->header->entry_size);
234 uint64_t ent_lba = uint64_t_le2host(label->gpt->header->entry_lba);
235
236 if (label->parts == NULL) {
237 label->parts = alloc_part_array(fill);
238 if (label->parts == NULL) {
239 return ENOMEM;
240 }
241 }
242
243 /* We can limit comm_size like this:
244 * - we don't need more bytes
245 * - the size of GPT partition entry can be different to 128 bytes */
246 /* comm_size is ignored */
247 rc = block_init(EXCHANGE_SERIALIZE, label->device, sizeof(gpt_entry_t));
248 if (rc != EOK)
249 goto fail;
250
251 size_t block_size;
252 rc = block_get_bsize(label->device, &block_size);
253 if (rc != EOK)
254 goto fini_fail;
255
256 //size_t bufpos = 0;
257 //size_t buflen = 0;
258 aoff64_t pos = ent_lba * block_size;
259
260 /* Now we read just sizeof(gpt_entry_t) bytes for each entry from the device.
261 * Hopefully, this does not bypass cache (no mention in libblock.c),
262 * and also allows us to have variable partition entry size (but we
263 * will always read just sizeof(gpt_entry_t) bytes - hopefully they
264 * don't break backward compatibility) */
265 for (i = 0; i < fill; ++i) {
266 //FIXME: this does bypass cache...
267 rc = block_read_bytes_direct(label->device, pos, sizeof(gpt_entry_t), label->parts->part_array + i);
268 //FIXME: but seqread() is just too complex...
269 //rc = block_seqread(gpt->device, &bufpos, &buflen, &pos, res->part_array[i], sizeof(gpt_entry_t));
270 pos += ent_size;
271
272 if (rc != EOK)
273 goto fini_fail;
274 }
275
276 /* FIXME: so far my boasting about variable partition entry size
277 * will not work. The CRC32 checksums will be different.
278 * This can't be fixed easily - we'd have to run the checksum
279 * on all of the partition entry array.
280 */
281 uint32_t crc = compute_crc32((uint8_t *) label->parts->part_array,
282 label->parts->fill * sizeof(gpt_entry_t));
283
284 if(uint32_t_le2host(label->gpt->header->pe_array_crc32) != crc)
285 {
286 rc = EBADCHECKSUM;
287 goto fini_fail;
288 }
289
290 block_fini(label->device);
291 return EOK;
292
293fini_fail:
294 block_fini(label->device);
295
296fail:
297 gpt_free_partitions(label->parts);
298 label->parts = NULL;
299 return rc;
300}
301
302/** Write GPT and partitions to device
303 * Note: also writes the header.
304 * @param label label to write
305 * @param dev_handle device to write the data to
306 *
307 * @return returns EOK on succes, errorcode otherwise
308 */
309int gpt_write_partitions(gpt_label_t *label, service_id_t dev_handle)
310{
311 int rc;
312 size_t b_size;
313 uint32_t e_size = uint32_t_le2host(label->gpt->header->entry_size);
314 size_t fill = label->parts->fill > GPT_MIN_PART_NUM ? label->parts->fill : GPT_MIN_PART_NUM;
315
316 label->gpt->header->pe_array_crc32 = compute_crc32(
317 (uint8_t *) label->parts->part_array,
318 fill * e_size);
319
320 /* comm_size of 4096 is ignored */
321 rc = block_init(EXCHANGE_ATOMIC, dev_handle, 4096);
322 if (rc != EOK && rc != EEXIST)
323 return rc;
324
325 rc = block_get_bsize(dev_handle, &b_size);
326 if (rc != EOK)
327 goto fail;
328
329 aoff64_t n_blocks;
330 rc = block_get_nblocks(dev_handle, &n_blocks);
331 if (rc != EOK)
332 goto fail;
333
334 /* Write to backup GPT partition array location */
335 //rc = block_write_direct(dev_handle, n_blocks - 1, GPT_HDR_BS, header->raw_data);
336 if (rc != EOK)
337 goto fail;
338
339 /* Write to main GPT partition array location */
340 rc = block_write_direct(dev_handle, uint64_t_le2host(label->gpt->header->entry_lba),
341 nearest_larger_int((uint64_t_le2host(label->gpt->header->entry_size) * label->parts->fill) / b_size),
342 label->parts->part_array);
343 if (rc != EOK)
344 goto fail;
345
346 return gpt_write_header(label, dev_handle);
347
348fail:
349 block_fini(dev_handle);
350 return rc;
351}
352
353/** Alloc new partition
354 *
355 * @return returns pointer to the new partition or NULL
356 *
357 * Note: use either gpt_alloc_partition or gpt_get_partition.
358 * This returns a memory block (zero-filled) and needs gpt_add_partition()
359 * to be called to insert it into a partition array.
360 * Requires you to call gpt_free_partition afterwards.
361 */
362gpt_part_t * gpt_alloc_partition(void)
363{
364 gpt_part_t *p = malloc(sizeof(gpt_part_t));
365 if (p == NULL)
366 return NULL;
367
368 memset(p, 0, sizeof(gpt_part_t));
369
370 return p;
371}
372
373/** Alloc new partition already inside the label
374 *
375 * @param label label to carry new partition
376 *
377 * @return returns pointer to the new partition or NULL on ENOMEM
378 *
379 * Note: use either gpt_alloc_partition or gpt_get_partition.
380 * This one returns a pointer to the first empty structure already
381 * inside the array, so don't call gpt_add_partition() afterwards.
382 * This is the one you will usually want.
383 */
384gpt_part_t * gpt_get_partition(gpt_label_t *label)
385{
386 gpt_part_t *p;
387
388 /* Find the first empty entry */
389 do {
390 if (label->parts->fill == label->parts->arr_size) {
391 if (extend_part_array(label->parts) == -1)
392 return NULL;
393 }
394
395 p = label->parts->part_array + label->parts->fill++;
396
397 } while (gpt_get_part_type(p) != GPT_PTE_UNUSED);
398
399 return p;
400}
401
402/** Get partition already inside the label
403 *
404 * @param label label to carrying the partition
405 * @param idx index of the partition
406 *
407 * @return returns pointer to the partition
408 * or NULL when out of range
409 *
410 * Note: For new partitions use either gpt_alloc_partition or
411 * gpt_get_partition unless you want a partition at a specific place.
412 * This returns a pointer to a structure already inside the array,
413 * so don't call gpt_add_partition() afterwards.
414 * This function is handy when you want to change already existing
415 * partition or to simply write somewhere in the middle. This works only
416 * for indexes smaller than either 128 or the actual number of filled
417 * entries.
418 */
419gpt_part_t * gpt_get_partition_at(gpt_label_t *label, size_t idx)
420{
421 return NULL;
422
423 if (idx >= GPT_MIN_PART_NUM && idx >= label->parts->fill)
424 return NULL;
425
426 return label->parts->part_array + idx;
427}
428
429/** Copy partition into partition array
430 *
431 * @param parts target label
432 * @param partition source partition to copy
433 *
434 * @return -1 on error, 0 otherwise
435 *
436 * Note: for use with gpt_alloc_partition() only. You will get
437 * duplicates with gpt_get_partition().
438 * Note: does not call gpt_free_partition()!
439 */
440int gpt_add_partition(gpt_label_t *label, gpt_part_t *partition)
441{
442 if (label->parts->fill == label->parts->arr_size) {
443 if (extend_part_array(label->parts) == -1)
444 return ENOMEM;
445 }
446
447 /*FIXME:
448 * Check dimensions and stuff! */
449 gpt_part_foreach(label, p) {
450 if (gpt_get_part_type(p) != GPT_PTE_UNUSED) {
451 if (check_overlap(partition, p))
452 return EINVAL;
453 }
454 }
455
456 memcpy(label->parts->part_array + label->parts->fill++,
457 partition, sizeof(gpt_part_t));
458
459
460
461 return EOK;
462}
463
464/** Remove partition from array
465 * @param label label to remove from
466 * @param idx index of the partition to remove
467 *
468 * @return EOK on success, ENOMEM on array reduction failure
469 *
470 * Note: even if it fails, the partition still gets removed. Only
471 * reducing the array failed.
472 */
473int gpt_remove_partition(gpt_label_t *label, size_t idx)
474{
475 if (idx >= label->parts->fill)
476 return EINVAL;
477
478 /* FIXME!
479 * If we allow blank spots, we break the array. If we have more than
480 * 128 partitions in the array and then remove something from
481 * the first 128 partitions, we would forget to write the last one.*/
482 memset(label->parts->part_array + idx, 0, sizeof(gpt_entry_t));
483
484 label->parts->fill -= 1;
485
486 /* FIXME! HOPEFULLY FIXED.
487 * We cannot reduce the array so simply. We may have some partitions
488 * there since we allow blank spots. */
489 gpt_part_t * p;
490 if (label->parts->fill < (label->parts->arr_size / 2) - GPT_IGNORE_FILL_NUM) {
491 for (p = gpt_get_partition_at(label, label->parts->arr_size / 2);
492 p < label->parts->part_array + label->parts->arr_size; ++p) {
493 if (gpt_get_part_type(p) != GPT_PTE_UNUSED)
494 return EOK;
495 }
496
497 if (reduce_part_array(label->parts) == ENOMEM)
498 return ENOMEM;
499 }
500
501 return EOK;
502}
503
504/** Free partition list
505 *
506 * @param parts partition list to be freed
507 */
508void gpt_free_partitions(gpt_partitions_t * parts)
509{
510 free(parts->part_array);
511 free(parts);
512}
513
514/** Get partition type by linear search
515 * (hopefully this doesn't get slow)
516 */
517size_t gpt_get_part_type(gpt_part_t * p)
518{
519 size_t i;
520
521 for (i = 0; gpt_ptypes[i].guid != NULL; i++) {
522 if (p->part_type[3] == get_byte(gpt_ptypes[i].guid +0) &&
523 p->part_type[2] == get_byte(gpt_ptypes[i].guid +2) &&
524 p->part_type[1] == get_byte(gpt_ptypes[i].guid +4) &&
525 p->part_type[0] == get_byte(gpt_ptypes[i].guid +6) &&
526
527 p->part_type[5] == get_byte(gpt_ptypes[i].guid +8) &&
528 p->part_type[4] == get_byte(gpt_ptypes[i].guid +10) &&
529
530 p->part_type[7] == get_byte(gpt_ptypes[i].guid +12) &&
531 p->part_type[6] == get_byte(gpt_ptypes[i].guid +14) &&
532
533 p->part_type[8] == get_byte(gpt_ptypes[i].guid +16) &&
534 p->part_type[9] == get_byte(gpt_ptypes[i].guid +18) &&
535 p->part_type[10] == get_byte(gpt_ptypes[i].guid +20) &&
536 p->part_type[11] == get_byte(gpt_ptypes[i].guid +22) &&
537 p->part_type[12] == get_byte(gpt_ptypes[i].guid +24) &&
538 p->part_type[13] == get_byte(gpt_ptypes[i].guid +26) &&
539 p->part_type[14] == get_byte(gpt_ptypes[i].guid +28) &&
540 p->part_type[15] == get_byte(gpt_ptypes[i].guid +30))
541 break;
542 }
543
544 return i;
545}
546
547/** Set partition type
548 * @param p partition to be set
549 * @param type partition type to set
550 * - see our fine selection at gpt_ptypes to choose from
551 */
552void gpt_set_part_type(gpt_part_t * p, size_t type)
553{
554 /* Beware: first 3 blocks are byteswapped! */
555 p->part_type[3] = gpt_ptypes[type].guid[0];
556 p->part_type[2] = gpt_ptypes[type].guid[1];
557 p->part_type[1] = gpt_ptypes[type].guid[2];
558 p->part_type[0] = gpt_ptypes[type].guid[3];
559
560 p->part_type[5] = gpt_ptypes[type].guid[4];
561 p->part_type[4] = gpt_ptypes[type].guid[5];
562
563 p->part_type[7] = gpt_ptypes[type].guid[6];
564 p->part_type[6] = gpt_ptypes[type].guid[7];
565
566 p->part_type[8] = gpt_ptypes[type].guid[8];
567 p->part_type[9] = gpt_ptypes[type].guid[9];
568 p->part_type[10] = gpt_ptypes[type].guid[10];
569 p->part_type[11] = gpt_ptypes[type].guid[11];
570 p->part_type[12] = gpt_ptypes[type].guid[12];
571 p->part_type[13] = gpt_ptypes[type].guid[13];
572 p->part_type[14] = gpt_ptypes[type].guid[14];
573 p->part_type[15] = gpt_ptypes[type].guid[15];
574}
575
576/** Get partition starting LBA */
577uint64_t gpt_get_start_lba(gpt_part_t * p)
578{
579 return uint64_t_le2host(p->start_lba);
580}
581
582/** Set partition starting LBA */
583void gpt_set_start_lba(gpt_part_t * p, uint64_t start)
584{
585 p->start_lba = host2uint64_t_le(start);
586}
587
588/** Get partition ending LBA */
589uint64_t gpt_get_end_lba(gpt_part_t * p)
590{
591 return uint64_t_le2host(p->end_lba);
592}
593
594/** Set partition ending LBA */
595void gpt_set_end_lba(gpt_part_t * p, uint64_t end)
596{
597 p->end_lba = host2uint64_t_le(end);
598}
599
600/** Get partition name */
601unsigned char * gpt_get_part_name(gpt_part_t * p)
602{
603 return p->part_name;
604}
605
606/** Copy partition name */
607void gpt_set_part_name(gpt_part_t *p, char *name, size_t length)
608{
609 if (length >= 72)
610 length = 71;
611
612 memcpy(p->part_name, name, length);
613 p->part_name[length] = '\0';
614}
615
616/** Get partition attribute */
617bool gpt_get_flag(gpt_part_t * p, GPT_ATTR flag)
618{
619 return (p->attributes & (((uint64_t) 1) << flag)) ? 1 : 0;
620}
621
622/** Set partition attribute */
623void gpt_set_flag(gpt_part_t * p, GPT_ATTR flag, bool value)
624{
625 uint64_t attr = p->attributes;
626
627 if (value)
628 attr = attr | (((uint64_t) 1) << flag);
629 else
630 attr = attr ^ (attr & (((uint64_t) 1) << flag));
631
632 p->attributes = attr;
633}
634
635// Internal functions follow //
636
637static int load_and_check_header(service_id_t dev_handle, aoff64_t addr, size_t b_size, gpt_header_t * header)
638{
639 int rc;
640
641 rc = block_read_direct(dev_handle, addr, GPT_HDR_BS, header);
642 if (rc != EOK)
643 return rc;
644
645 unsigned int i;
646 /* Check the EFI signature */
647 for (i = 0; i < 8; ++i) {
648 if (header->efi_signature[i] != efi_signature[i])
649 return EINVAL;
650 }
651
652 /* Check the CRC32 of the header */
653 uint32_t crc = header->header_crc32;
654 header->header_crc32 = 0;
655 if (crc != compute_crc32((uint8_t *) header, header->header_size))
656 return EBADCHECKSUM;
657 else
658 header->header_crc32 = crc;
659
660 /* Check for zeroes in the rest of the block */
661 for (i = sizeof(gpt_header_t); i < b_size; ++i) {
662 if (((uint8_t *) header)[i] != 0)
663 return EINVAL;
664 }
665
666 return EOK;
667}
668
669static gpt_partitions_t * alloc_part_array(uint32_t num)
670{
671 gpt_partitions_t * res = malloc(sizeof(gpt_partitions_t));
672 if (res == NULL) {
673 errno = ENOMEM;
674 return NULL;
675 }
676
677 uint32_t size = num > GPT_BASE_PART_NUM ? num : GPT_BASE_PART_NUM;
678 res->part_array = malloc(size * sizeof(gpt_entry_t));
679 if (res->part_array == NULL) {
680 free(res);
681 errno = ENOMEM;
682 return NULL;
683 }
684
685 res->fill = num;
686 res->arr_size = size;
687
688 return res;
689}
690
691static int extend_part_array(gpt_partitions_t * p)
692{
693 unsigned int nsize = p->arr_size * 2;
694 gpt_entry_t * tmp = malloc(nsize * sizeof(gpt_entry_t));
695 if(tmp == NULL) {
696 errno = ENOMEM;
697 return -1;
698 }
699
700 memcpy(tmp, p->part_array, p->fill);
701 free(p->part_array);
702 p->part_array = tmp;
703 p->arr_size = nsize;
704
705 return 0;
706}
707
708static int reduce_part_array(gpt_partitions_t * p)
709{
710 if(p->arr_size > GPT_MIN_PART_NUM) {
711 unsigned int nsize = p->arr_size / 2;
712 nsize = nsize > GPT_MIN_PART_NUM ? nsize : GPT_MIN_PART_NUM;
713 gpt_entry_t * tmp = malloc(nsize * sizeof(gpt_entry_t));
714 if(tmp == NULL)
715 return ENOMEM;
716
717 memcpy(tmp, p->part_array, p->fill < nsize ? p->fill : nsize);
718 free(p->part_array);
719 p->part_array = tmp;
720 p->arr_size = nsize;
721 }
722
723 return 0;
724}
725
726//FIXME: replace this with a library call, if it exists
727static long long nearest_larger_int(double a)
728{
729 if ((long long) a == a) {
730 return (long long) a;
731 }
732
733 return ((long long) a) + 1;
734}
735
736static uint8_t get_byte(const char * c)
737{
738 uint8_t val = 0;
739 char hex[3] = {*c, *(c+1), 0};
740
741 errno = str_uint8_t(hex, NULL, 16, false, &val);
742 return val;
743}
744
745static int check_overlap(gpt_part_t * p1, gpt_part_t * p2)
746{
747 if (gpt_get_start_lba(p1) < gpt_get_start_lba(p2) && gpt_get_end_lba(p1) <= gpt_get_start_lba(p2)) {
748 return 0;
749 } else if (gpt_get_start_lba(p1) > gpt_get_start_lba(p2) && gpt_get_end_lba(p2) <= gpt_get_start_lba(p1)) {
750 return 0;
751 }
752
753 return 1;
754}
755
756
Note: See TracBrowser for help on using the repository browser.