source: mainline/uspace/lib/gpt/libgpt.c@ 903a897

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

more libgpt fixes

  • Property mode set to 100644
File size: 20.2 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 fillries = 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(fillries);
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 < fillries; ++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 fillries * 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 fillries = label->parts->fill > GPT_MIN_PART_NUM ? label->parts->fill : GPT_MIN_PART_NUM;
315
316 label->gpt->header->fillries = host2uint32_t_le(fillries);
317 label->gpt->header->pe_array_crc32 = host2uint32_t_le(compute_crc32(
318 (uint8_t *) label->parts->part_array,
319 fillries * e_size));
320
321 /* comm_size of 4096 is ignored */
322 rc = block_init(EXCHANGE_ATOMIC, dev_handle, 4096);
323 if (rc != EOK && rc != EEXIST)
324 return rc;
325
326 rc = block_get_bsize(dev_handle, &b_size);
327 if (rc != EOK)
328 goto fail;
329
330 aoff64_t n_blocks;
331 rc = block_get_nblocks(dev_handle, &n_blocks);
332 if (rc != EOK)
333 goto fail;
334
335 /* Write to backup GPT partition array location */
336 //rc = block_write_direct(dev_handle, n_blocks - 1, GPT_HDR_BS, header->raw_data);
337 if (rc != EOK)
338 goto fail;
339
340 /* Write to main GPT partition array location */
341 rc = block_write_direct(dev_handle, uint64_t_le2host(label->gpt->header->entry_lba),
342 nearest_larger_int((uint64_t_le2host(label->gpt->header->entry_size) * fillries) / b_size),
343 label->parts->part_array);
344 if (rc != EOK)
345 goto fail;
346
347 return gpt_write_header(label, dev_handle);
348
349fail:
350 block_fini(dev_handle);
351 return rc;
352}
353
354/** Alloc new partition
355 *
356 * @return returns pointer to the new partition or NULL
357 *
358 * Note: use either gpt_alloc_partition or gpt_get_partition.
359 * This returns a memory block (zero-filled) and needs gpt_add_partition()
360 * to be called to insert it into a partition array.
361 * Requires you to call gpt_free_partition afterwards.
362 */
363gpt_part_t * gpt_alloc_partition(void)
364{
365 gpt_part_t *p = malloc(sizeof(gpt_part_t));
366 if (p == NULL)
367 return NULL;
368
369 memset(p, 0, sizeof(gpt_part_t));
370
371 return p;
372}
373
374/** Alloc new partition already inside the label
375 *
376 * @param label label to carry new partition
377 *
378 * @return returns pointer to the new partition or NULL on ENOMEM
379 *
380 * Note: use either gpt_alloc_partition or gpt_get_partition.
381 * This one returns a pointer to the first empty structure already
382 * inside the array, so don't call gpt_add_partition() afterwards.
383 * This is the one you will usually want.
384 */
385gpt_part_t * gpt_get_partition(gpt_label_t *label)
386{
387 gpt_part_t *p;
388
389
390 /* Find the first empty entry */
391 do {
392 if (label->parts->fill == label->parts->arr_size) {
393 if (extend_part_array(label->parts) == -1)
394 return NULL;
395 }
396
397 p = label->parts->part_array + label->parts->fill++;
398
399 } while (gpt_get_part_type(p) != GPT_PTE_UNUSED);
400
401 return p;
402}
403
404/** Get partition already inside the label
405 *
406 * @param label label to carrying the partition
407 * @param idx index of the partition
408 *
409 * @return returns pointer to the partition
410 * or NULL when out of range
411 *
412 * Note: For new partitions use either gpt_alloc_partition or
413 * gpt_get_partition unless you want a partition at a specific place.
414 * This returns a pointer to a structure already inside the array,
415 * so don't call gpt_add_partition() afterwards.
416 * This function is handy when you want to change already existing
417 * partition or to simply write somewhere in the middle. This works only
418 * for indexes smaller than either 128 or the actual number of filled
419 * entries.
420 */
421gpt_part_t * gpt_get_partition_at(gpt_label_t *label, size_t idx)
422{
423 return NULL;
424
425 if (idx >= GPT_MIN_PART_NUM && idx >= label->parts->fill)
426 return NULL;
427
428 return label->parts->part_array + idx;
429}
430
431/** Copy partition into partition array
432 *
433 * @param parts target label
434 * @param partition source partition to copy
435 *
436 * @return -1 on error, 0 otherwise
437 *
438 * Note: for use with gpt_alloc_partition() only. You will get
439 * duplicates with gpt_get_partition().
440 * Note: does not call gpt_free_partition()!
441 */
442int gpt_add_partition(gpt_label_t *label, gpt_part_t *partition)
443{
444 if (label->parts->fill == label->parts->arr_size) {
445 if (extend_part_array(label->parts) == -1)
446 return ENOMEM;
447 }
448
449 /*FIXME:
450 * Check dimensions and stuff! */
451 gpt_part_foreach(label, p) {
452 if (gpt_get_part_type(p) != GPT_PTE_UNUSED) {
453 if (check_overlap(partition, p))
454 return EINVAL;
455 }
456 }
457
458 memcpy(label->parts->part_array + label->parts->fill++,
459 partition, sizeof(gpt_part_t));
460
461
462
463 return EOK;
464}
465
466/** Remove partition from array
467 * @param label label to remove from
468 * @param idx index of the partition to remove
469 *
470 * @return EOK on success, ENOMEM on array reduction failure
471 *
472 * Note: even if it fails, the partition still gets removed. Only
473 * reducing the array failed.
474 */
475int gpt_remove_partition(gpt_label_t *label, size_t idx)
476{
477 if (idx >= label->parts->fill)
478 return EINVAL;
479
480 /* FIXME!
481 * If we allow blank spots, we break the array. If we have more than
482 * 128 partitions in the array and then remove something from
483 * the first 128 partitions, we would forget to write the last one.*/
484 memset(label->parts->part_array + idx, 0, sizeof(gpt_entry_t));
485
486 label->parts->fill = idx;
487
488 /* FIXME! HOPEFULLY FIXED.
489 * We cannot reduce the array so simply. We may have some partitions
490 * there since we allow blank spots. */
491 gpt_part_t * p;
492 if (label->parts->fill < (label->parts->arr_size / 2) - GPT_IGNORE_FILL_NUM) {
493 for (p = gpt_get_partition_at(label, label->parts->arr_size / 2);
494 p < label->parts->part_array + label->parts->arr_size; ++p) {
495 if (gpt_get_part_type(p) != GPT_PTE_UNUSED)
496 return EOK;
497 }
498
499 if (reduce_part_array(label->parts) == ENOMEM)
500 return ENOMEM;
501 }
502
503 return EOK;
504}
505
506/** Free partition list
507 *
508 * @param parts partition list to be freed
509 */
510void gpt_free_partitions(gpt_partitions_t * parts)
511{
512 free(parts->part_array);
513 free(parts);
514}
515
516/** Get partition type by linear search
517 * (hopefully this doesn't get slow)
518 */
519size_t gpt_get_part_type(gpt_part_t * p)
520{
521 size_t i;
522
523 for (i = 0; gpt_ptypes[i].guid != NULL; i++) {
524 if (p->part_type[3] == get_byte(gpt_ptypes[i].guid +0) &&
525 p->part_type[2] == get_byte(gpt_ptypes[i].guid +2) &&
526 p->part_type[1] == get_byte(gpt_ptypes[i].guid +4) &&
527 p->part_type[0] == get_byte(gpt_ptypes[i].guid +6) &&
528
529 p->part_type[5] == get_byte(gpt_ptypes[i].guid +8) &&
530 p->part_type[4] == get_byte(gpt_ptypes[i].guid +10) &&
531
532 p->part_type[7] == get_byte(gpt_ptypes[i].guid +12) &&
533 p->part_type[6] == get_byte(gpt_ptypes[i].guid +14) &&
534
535 p->part_type[8] == get_byte(gpt_ptypes[i].guid +16) &&
536 p->part_type[9] == get_byte(gpt_ptypes[i].guid +18) &&
537 p->part_type[10] == get_byte(gpt_ptypes[i].guid +20) &&
538 p->part_type[11] == get_byte(gpt_ptypes[i].guid +22) &&
539 p->part_type[12] == get_byte(gpt_ptypes[i].guid +24) &&
540 p->part_type[13] == get_byte(gpt_ptypes[i].guid +26) &&
541 p->part_type[14] == get_byte(gpt_ptypes[i].guid +28) &&
542 p->part_type[15] == get_byte(gpt_ptypes[i].guid +30))
543 break;
544 }
545
546 return i;
547}
548
549/** Set partition type
550 * @param p partition to be set
551 * @param type partition type to set
552 * - see our fine selection at gpt_ptypes to choose from
553 */
554void gpt_set_part_type(gpt_part_t * p, size_t type)
555{
556 /* Beware: first 3 blocks are byteswapped! */
557 p->part_type[3] = gpt_ptypes[type].guid[0];
558 p->part_type[2] = gpt_ptypes[type].guid[1];
559 p->part_type[1] = gpt_ptypes[type].guid[2];
560 p->part_type[0] = gpt_ptypes[type].guid[3];
561
562 p->part_type[5] = gpt_ptypes[type].guid[4];
563 p->part_type[4] = gpt_ptypes[type].guid[5];
564
565 p->part_type[7] = gpt_ptypes[type].guid[6];
566 p->part_type[6] = gpt_ptypes[type].guid[7];
567
568 p->part_type[8] = gpt_ptypes[type].guid[8];
569 p->part_type[9] = gpt_ptypes[type].guid[9];
570 p->part_type[10] = gpt_ptypes[type].guid[10];
571 p->part_type[11] = gpt_ptypes[type].guid[11];
572 p->part_type[12] = gpt_ptypes[type].guid[12];
573 p->part_type[13] = gpt_ptypes[type].guid[13];
574 p->part_type[14] = gpt_ptypes[type].guid[14];
575 p->part_type[15] = gpt_ptypes[type].guid[15];
576}
577
578/** Get partition starting LBA */
579uint64_t gpt_get_start_lba(gpt_part_t * p)
580{
581 return uint64_t_le2host(p->start_lba);
582}
583
584/** Set partition starting LBA */
585void gpt_set_start_lba(gpt_part_t * p, uint64_t start)
586{
587 p->start_lba = host2uint64_t_le(start);
588}
589
590/** Get partition ending LBA */
591uint64_t gpt_get_end_lba(gpt_part_t * p)
592{
593 return uint64_t_le2host(p->end_lba);
594}
595
596/** Set partition ending LBA */
597void gpt_set_end_lba(gpt_part_t * p, uint64_t end)
598{
599 p->end_lba = host2uint64_t_le(end);
600}
601
602/** Get partition name */
603unsigned char * gpt_get_part_name(gpt_part_t * p)
604{
605 return p->part_name;
606}
607
608/** Copy partition name */
609void gpt_set_part_name(gpt_part_t *p, char *name, size_t length)
610{
611 if (length >= 72)
612 length = 71;
613
614 memcpy(p->part_name, name, length);
615 p->part_name[length] = '\0';
616}
617
618/** Get partition attribute */
619bool gpt_get_flag(gpt_part_t * p, GPT_ATTR flag)
620{
621 return (p->attributes & (((uint64_t) 1) << flag)) ? 1 : 0;
622}
623
624/** Set partition attribute */
625void gpt_set_flag(gpt_part_t * p, GPT_ATTR flag, bool value)
626{
627 uint64_t attr = p->attributes;
628
629 if (value)
630 attr = attr | (((uint64_t) 1) << flag);
631 else
632 attr = attr ^ (attr & (((uint64_t) 1) << flag));
633
634 p->attributes = attr;
635}
636
637void gpt_set_random_uuid(uint8_t * uuid)
638{
639 srandom((unsigned int) uuid);
640
641 unsigned int i;
642 for (i = 0; i < 16/sizeof(long int); ++i)
643 ((long int *)uuid)[i] = random();
644
645}
646
647// Internal functions follow //
648
649static int load_and_check_header(service_id_t dev_handle, aoff64_t addr, size_t b_size, gpt_header_t * header)
650{
651 int rc;
652
653 rc = block_read_direct(dev_handle, addr, GPT_HDR_BS, header);
654 if (rc != EOK)
655 return rc;
656
657 unsigned int i;
658 /* Check the EFI signature */
659 for (i = 0; i < 8; ++i) {
660 if (header->efi_signature[i] != efi_signature[i])
661 return EINVAL;
662 }
663
664 /* Check the CRC32 of the header */
665 uint32_t crc = header->header_crc32;
666 header->header_crc32 = 0;
667 if (crc != compute_crc32((uint8_t *) header, header->header_size))
668 return EBADCHECKSUM;
669 else
670 header->header_crc32 = crc;
671
672 /* Check for zeroes in the rest of the block */
673 for (i = sizeof(gpt_header_t); i < b_size; ++i) {
674 if (((uint8_t *) header)[i] != 0)
675 return EINVAL;
676 }
677
678 return EOK;
679}
680
681static gpt_partitions_t * alloc_part_array(uint32_t num)
682{
683 gpt_partitions_t * res = malloc(sizeof(gpt_partitions_t));
684 if (res == NULL) {
685 errno = ENOMEM;
686 return NULL;
687 }
688
689 uint32_t size = num > GPT_BASE_PART_NUM ? num : GPT_BASE_PART_NUM;
690 res->part_array = malloc(size * sizeof(gpt_entry_t));
691 if (res->part_array == NULL) {
692 free(res);
693 errno = ENOMEM;
694 return NULL;
695 }
696
697 res->fill = 0;
698 res->arr_size = num;
699
700 return res;
701}
702
703static int extend_part_array(gpt_partitions_t * p)
704{
705 size_t nsize = p->arr_size * 2;
706 gpt_entry_t * tmp = malloc(nsize * sizeof(gpt_entry_t));
707 if(tmp == NULL) {
708 errno = ENOMEM;
709 return -1;
710 }
711
712 memcpy(tmp, p->part_array, p->fill * sizeof(gpt_entry_t));
713 free(p->part_array);
714 p->part_array = tmp;
715 p->arr_size = nsize;
716
717 return 0;
718}
719
720static int reduce_part_array(gpt_partitions_t * p)
721{
722 if(p->arr_size > GPT_MIN_PART_NUM) {
723 unsigned int nsize = p->arr_size / 2;
724 nsize = nsize > GPT_MIN_PART_NUM ? nsize : GPT_MIN_PART_NUM;
725 gpt_entry_t * tmp = malloc(nsize * sizeof(gpt_entry_t));
726 if(tmp == NULL)
727 return ENOMEM;
728
729 memcpy(tmp, p->part_array, p->fill < nsize ? p->fill : nsize);
730 free(p->part_array);
731 p->part_array = tmp;
732 p->arr_size = nsize;
733 }
734
735 return 0;
736}
737
738//FIXME: replace this with a library call, if it exists
739static long long nearest_larger_int(double a)
740{
741 if ((long long) a == a) {
742 return (long long) a;
743 }
744
745 return ((long long) a) + 1;
746}
747
748static uint8_t get_byte(const char * c)
749{
750 uint8_t val = 0;
751 char hex[3] = {*c, *(c+1), 0};
752
753 errno = str_uint8_t(hex, NULL, 16, false, &val);
754 return val;
755}
756
757static int check_overlap(gpt_part_t * p1, gpt_part_t * p2)
758{
759 if (gpt_get_start_lba(p1) < gpt_get_start_lba(p2) && gpt_get_end_lba(p1) <= gpt_get_start_lba(p2)) {
760 return 0;
761 } else if (gpt_get_start_lba(p1) > gpt_get_start_lba(p2) && gpt_get_end_lba(p2) <= gpt_get_start_lba(p1)) {
762 return 0;
763 }
764
765 return 1;
766}
767
768
Note: See TracBrowser for help on using the repository browser.