source: mainline/uspace/lib/gpt/libgpt.c@ 283ea3d

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

libmbr, libgpt polishing

  • Property mode set to 100644
File size: 21.7 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, aoff64_t, size_t, gpt_header_t *);
54static gpt_partitions_t * alloc_part_array(uint32_t);
55static int extend_part_array(gpt_partitions_t *);
56static int reduce_part_array(gpt_partitions_t *);
57//static long long nearest_larger_int(double);
58static uint8_t get_byte(const char *);
59static bool check_overlap(gpt_part_t *, gpt_part_t *);
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 /* This is necessary so that gpt_part_foreach does not segfault */
69 label->parts = gpt_alloc_partitions();
70 if (label == NULL) {
71 free(label);
72 return NULL;
73 }
74
75 label->gpt = NULL;
76
77 label->device = 0;
78
79 return label;
80}
81
82/** Free gpt_label_t structure */
83void gpt_free_label(gpt_label_t *label)
84{
85 if (label->gpt != NULL)
86 gpt_free_gpt(label->gpt);
87
88 if (label->parts != NULL)
89 gpt_free_partitions(label->parts);
90
91 free(label);
92}
93
94/** Allocate memory for gpt header */
95gpt_t * gpt_alloc_header(size_t size)
96{
97 gpt_t *gpt = malloc(sizeof(gpt_t));
98 if (gpt == NULL)
99 return NULL;
100
101 /*
102 * We might need only sizeof(gpt_header_t), but we should follow
103 * specs and have zeroes through all the rest of the block
104 */
105 size_t final_size = size > sizeof(gpt_header_t) ? size : sizeof(gpt_header_t);
106 gpt->header = malloc(final_size);
107 if (gpt->header == NULL) {
108 free(gpt);
109 return NULL;
110 }
111
112 memset(gpt->header, 0, final_size);
113
114 return gpt;
115}
116
117/** free() GPT header including gpt->header_lba */
118void gpt_free_gpt(gpt_t *gpt)
119{
120 free(gpt->header);
121 free(gpt);
122}
123
124/** Read GPT from specific device
125 * @param label label structure to fill
126 * @param dev_handle device to read GPT from
127 *
128 * @return EOK on success, errorcode on error
129 */
130int gpt_read_header(gpt_label_t *label, service_id_t dev_handle)
131{
132 int rc;
133 size_t b_size;
134
135 rc = block_init(EXCHANGE_ATOMIC, dev_handle, 512);
136 if (rc != EOK)
137 goto fail;
138
139 rc = block_get_bsize(dev_handle, &b_size);
140 if (rc != EOK)
141 goto fini_fail;
142
143 if (label->gpt == NULL) {
144 label->gpt = gpt_alloc_header(b_size);
145 if (label->gpt == NULL) {
146 rc = ENOMEM;
147 goto fini_fail;
148 }
149 }
150
151 rc = load_and_check_header(dev_handle, GPT_HDR_BA, b_size, label->gpt->header);
152 if (rc == EBADCHECKSUM || rc == EINVAL) {
153 aoff64_t n_blocks;
154 rc = block_get_nblocks(dev_handle, &n_blocks);
155 if (rc != EOK)
156 goto free_fail;
157
158 rc = load_and_check_header(dev_handle, n_blocks - 1, b_size, label->gpt->header);
159 if (rc == EBADCHECKSUM || rc == EINVAL)
160 goto free_fail;
161 }
162
163 label->device = dev_handle;
164 block_fini(dev_handle);
165 return EOK;
166
167free_fail:
168 gpt_free_gpt(label->gpt);
169 label->gpt = NULL;
170fini_fail:
171 block_fini(dev_handle);
172fail:
173 return rc;
174}
175
176/** Write GPT header to device
177 * @param label GPT label header to be written
178 * @param dev_handle device handle to write the data to
179 *
180 * @return EOK on success, libblock error code otherwise
181 *
182 * Note: Firstly write partitions (if modified), then gpt header.
183 */
184int gpt_write_header(gpt_label_t *label, service_id_t dev_handle)
185{
186 int rc;
187 size_t b_size;
188
189 /* The comm_size argument (the last one) is ignored */
190 rc = block_init(EXCHANGE_ATOMIC, dev_handle, 4096);
191 if (rc != EOK && rc != EEXIST)
192 return rc;
193
194 rc = block_get_bsize(dev_handle, &b_size);
195 if (rc != EOK)
196 return rc;
197
198 aoff64_t n_blocks;
199 rc = block_get_nblocks(dev_handle, &n_blocks);
200 if (rc != EOK) {
201 block_fini(dev_handle);
202 return rc;
203 }
204
205 uint64_t tmp;
206
207 /* Prepare the backup header */
208 label->gpt->header->alternate_lba = label->gpt->header->my_lba;
209 label->gpt->header->my_lba = host2uint64_t_le(n_blocks - 1);
210
211 tmp = label->gpt->header->entry_lba;
212 label->gpt->header->entry_lba = host2uint64_t_le(n_blocks -
213 (uint32_t_le2host(label->gpt->header->fillries) * sizeof(gpt_entry_t))
214 / b_size - 1);
215
216 label->gpt->header->header_crc32 = 0;
217 label->gpt->header->header_crc32 = host2uint32_t_le(
218 compute_crc32((uint8_t *) label->gpt->header,
219 uint32_t_le2host(label->gpt->header->header_size)));
220
221 /* Write to backup GPT header location */
222 rc = block_write_direct(dev_handle, n_blocks - 1, GPT_HDR_BS, label->gpt->header);
223 if (rc != EOK) {
224 block_fini(dev_handle);
225 return rc;
226 }
227
228
229 /* Prepare the main header */
230 label->gpt->header->entry_lba = tmp;
231
232 tmp = label->gpt->header->alternate_lba;
233 label->gpt->header->alternate_lba = label->gpt->header->my_lba;
234 label->gpt->header->my_lba = tmp;
235
236 label->gpt->header->header_crc32 = 0;
237 label->gpt->header->header_crc32 = host2uint32_t_le(
238 compute_crc32((uint8_t *) label->gpt->header,
239 uint32_t_le2host(label->gpt->header->header_size)));
240
241 /* Write to main GPT header location */
242 rc = block_write_direct(dev_handle, GPT_HDR_BA, GPT_HDR_BS, label->gpt->header);
243 block_fini(dev_handle);
244 if (rc != EOK)
245 return rc;
246
247
248 return 0;
249}
250
251/** Alloc partition array */
252gpt_partitions_t * gpt_alloc_partitions()
253{
254 return alloc_part_array(GPT_MIN_PART_NUM);
255}
256
257/** Parse partitions from GPT
258 * @param label GPT label to be parsed
259 *
260 * @return EOK on success, errorcode otherwise
261 */
262int gpt_read_partitions(gpt_label_t *label)
263{
264 int rc;
265 unsigned int i;
266 uint32_t fillries = uint32_t_le2host(label->gpt->header->fillries);
267 uint32_t ent_size = uint32_t_le2host(label->gpt->header->entry_size);
268 uint64_t ent_lba = uint64_t_le2host(label->gpt->header->entry_lba);
269
270 if (label->parts == NULL) {
271 label->parts = alloc_part_array(fillries);
272 if (label->parts == NULL) {
273 return ENOMEM;
274 }
275 }
276
277 /* comm_size is ignored */
278 rc = block_init(EXCHANGE_SERIALIZE, label->device, sizeof(gpt_entry_t));
279 if (rc != EOK)
280 goto fail;
281
282 size_t block_size;
283 rc = block_get_bsize(label->device, &block_size);
284 if (rc != EOK)
285 goto fini_fail;
286
287 //size_t bufpos = 0;
288 //size_t buflen = 0;
289 aoff64_t pos = ent_lba * block_size;
290
291 /*
292 * Now we read just sizeof(gpt_entry_t) bytes for each entry from the device.
293 * Hopefully, this does not bypass cache (no mention in libblock.c),
294 * and also allows us to have variable partition entry size (but we
295 * will always read just sizeof(gpt_entry_t) bytes - hopefully they
296 * don't break backward compatibility)
297 */
298 for (i = 0; i < fillries; ++i) {
299 /*FIXME: this does bypass cache... */
300 rc = block_read_bytes_direct(label->device, pos, sizeof(gpt_entry_t), label->parts->part_array + i);
301 /*
302 * FIXME: but seqread() is just too complex...
303 * rc = block_seqread(gpt->device, &bufpos, &buflen, &pos, res->part_array[i], sizeof(gpt_entry_t));
304 */
305 pos += ent_size;
306
307 if (rc != EOK)
308 goto fini_fail;
309 }
310
311 /*
312 * FIXME: so far my boasting about variable partition entry size
313 * will not work. The CRC32 checksums will be different.
314 * This can't be fixed easily - we'd have to run the checksum
315 * on all of the partition entry array.
316 */
317 uint32_t crc = compute_crc32((uint8_t *) label->parts->part_array,
318 fillries * sizeof(gpt_entry_t));
319
320 if(uint32_t_le2host(label->gpt->header->pe_array_crc32) != crc)
321 {
322 rc = EBADCHECKSUM;
323 goto fini_fail;
324 }
325
326 block_fini(label->device);
327 return EOK;
328
329fini_fail:
330 block_fini(label->device);
331
332fail:
333 gpt_free_partitions(label->parts);
334 label->parts = NULL;
335 return rc;
336}
337
338/** Write GPT and partitions to device
339 * Note: also writes the header.
340 * @param label label to write
341 * @param dev_handle device to write the data to
342 *
343 * @return returns EOK on succes, errorcode otherwise
344 */
345int gpt_write_partitions(gpt_label_t *label, service_id_t dev_handle)
346{
347 int rc;
348 size_t b_size;
349 uint32_t e_size = uint32_t_le2host(label->gpt->header->entry_size);
350 size_t fillries = label->parts->fill > GPT_MIN_PART_NUM ? label->parts->fill : GPT_MIN_PART_NUM;
351
352 label->gpt->header->fillries = host2uint32_t_le(fillries);
353 label->gpt->header->pe_array_crc32 = host2uint32_t_le(compute_crc32(
354 (uint8_t *) label->parts->part_array,
355 fillries * e_size));
356
357 /* comm_size of 4096 is ignored */
358 rc = block_init(EXCHANGE_ATOMIC, dev_handle, 4096);
359 if (rc != EOK && rc != EEXIST)
360 return rc;
361
362 rc = block_get_bsize(dev_handle, &b_size);
363 if (rc != EOK)
364 goto fail;
365
366 aoff64_t n_blocks;
367 rc = block_get_nblocks(dev_handle, &n_blocks);
368 if (rc != EOK)
369 goto fail;
370
371 uint64_t arr_blocks = (fillries * sizeof(gpt_entry_t)) / b_size;
372 label->gpt->header->first_usable_lba = host2uint64_t_le(arr_blocks + 1);
373 label->gpt->header->last_usable_lba = host2uint64_t_le(n_blocks - arr_blocks - 2);
374
375
376 /* Write to backup GPT partition array location */
377 rc = block_write_direct(dev_handle, n_blocks - arr_blocks - 1,
378 arr_blocks, label->parts->part_array);
379 if (rc != EOK)
380 goto fail;
381
382 /* Write to main GPT partition array location */
383 rc = block_write_direct(dev_handle, uint64_t_le2host(label->gpt->header->entry_lba),
384 arr_blocks, label->parts->part_array);
385 if (rc != EOK)
386 goto fail;
387
388 return gpt_write_header(label, dev_handle);
389
390fail:
391 block_fini(dev_handle);
392 return rc;
393}
394
395/** Alloc new partition
396 *
397 * @return returns pointer to the new partition or NULL
398 *
399 * Note: use either gpt_alloc_partition or gpt_get_partition.
400 * This returns a memory block (zero-filled) and needs gpt_add_partition()
401 * to be called to insert it into a partition array.
402 * Requires you to call gpt_free_partition afterwards.
403 */
404gpt_part_t * gpt_alloc_partition(void)
405{
406 gpt_part_t *p = malloc(sizeof(gpt_part_t));
407 if (p == NULL)
408 return NULL;
409
410 memset(p, 0, sizeof(gpt_part_t));
411
412 return p;
413}
414
415/** Alloc new partition already inside the label
416 *
417 * @param label label to carry new partition
418 *
419 * @return returns pointer to the new partition or NULL on ENOMEM
420 *
421 * Note: use either gpt_alloc_partition or gpt_get_partition.
422 * This one returns a pointer to the first empty structure already
423 * inside the array, so don't call gpt_add_partition() afterwards.
424 * This is the one you will usually want.
425 */
426gpt_part_t * gpt_get_partition(gpt_label_t *label)
427{
428 gpt_part_t *p;
429
430
431 /* Find the first empty entry */
432 do {
433 if (label->parts->fill == label->parts->arr_size) {
434 if (extend_part_array(label->parts) == -1)
435 return NULL;
436 }
437
438 p = label->parts->part_array + label->parts->fill++;
439
440 } while (gpt_get_part_type(p) != GPT_PTE_UNUSED);
441
442 return p;
443}
444
445/** Get partition already inside the label
446 *
447 * @param label label to carrying the partition
448 * @param idx index of the partition
449 *
450 * @return returns pointer to the partition
451 * or NULL when out of range
452 *
453 * Note: For new partitions use either gpt_alloc_partition or
454 * gpt_get_partition unless you want a partition at a specific place.
455 * This returns a pointer to a structure already inside the array,
456 * so don't call gpt_add_partition() afterwards.
457 * This function is handy when you want to change already existing
458 * partition or to simply write somewhere in the middle. This works only
459 * for indexes smaller than either 128 or the actual number of filled
460 * entries.
461 */
462gpt_part_t * gpt_get_partition_at(gpt_label_t *label, size_t idx)
463{
464 return NULL;
465
466 if (idx >= GPT_MIN_PART_NUM && idx >= label->parts->fill)
467 return NULL;
468
469 return label->parts->part_array + idx;
470}
471
472/** Copy partition into partition array
473 *
474 * @param parts target label
475 * @param partition source partition to copy
476 *
477 * @return -1 on error, 0 otherwise
478 *
479 * Note: for use with gpt_alloc_partition() only. You will get
480 * duplicates with gpt_get_partition().
481 * Note: does not call gpt_free_partition()!
482 */
483int gpt_add_partition(gpt_label_t *label, gpt_part_t *partition)
484{
485 /* FIXME: Check dimensions! */
486 gpt_part_foreach(label, p) {
487 if (gpt_get_part_type(p) != GPT_PTE_UNUSED) {
488 if (check_overlap(partition, p))
489 return EINVAL;
490 }
491 }
492
493 gpt_part_t *p;
494 /* Find the first empty entry */
495 do {
496 if (label->parts->fill == label->parts->arr_size) {
497 if (extend_part_array(label->parts) == -1)
498 return ENOMEM;
499 }
500
501 p = label->parts->part_array + label->parts->fill++;
502
503 } while (gpt_get_part_type(p) != GPT_PTE_UNUSED);
504
505
506 memcpy(p, partition, sizeof(gpt_entry_t));
507
508
509 return EOK;
510}
511
512/** Remove partition from array
513 * @param label label to remove from
514 * @param idx index of the partition to remove
515 *
516 * @return EOK on success, ENOMEM on array reduction failure
517 *
518 * Note: even if it fails, the partition still gets removed. Only
519 * reducing the array failed.
520 */
521int gpt_remove_partition(gpt_label_t *label, size_t idx)
522{
523 if (idx >= label->parts->arr_size)
524 return EINVAL;
525
526 /*
527 * FIXME!
528 * If we allow blank spots, we break the array. If we have more than
529 * 128 partitions in the array and then remove something from
530 * the first 128 partitions, we would forget to write the last one.
531 */
532 memset(label->parts->part_array + idx, 0, sizeof(gpt_entry_t));
533
534 if (label->parts->fill > idx)
535 label->parts->fill = idx;
536
537 /*
538 * FIXME! HOPEFULLY FIXED.
539 * We cannot reduce the array so simply. We may have some partitions
540 * there since we allow blank spots.
541 */
542 gpt_part_t * p;
543
544 if (label->parts->fill > GPT_MIN_PART_NUM &&
545 label->parts->fill < (label->parts->arr_size / 2) - GPT_IGNORE_FILL_NUM) {
546 for (p = gpt_get_partition_at(label, label->parts->arr_size / 2);
547 p < label->parts->part_array + label->parts->arr_size; ++p) {
548 if (gpt_get_part_type(p) != GPT_PTE_UNUSED)
549 return EOK;
550 }
551
552 if (reduce_part_array(label->parts) == ENOMEM)
553 return ENOMEM;
554 }
555
556 return EOK;
557}
558
559/** Free partition list
560 *
561 * @param parts partition list to be freed
562 */
563void gpt_free_partitions(gpt_partitions_t * parts)
564{
565 free(parts->part_array);
566 free(parts);
567}
568
569/** Get partition type by linear search
570 * (hopefully this doesn't get slow)
571 */
572size_t gpt_get_part_type(gpt_part_t * p)
573{
574 size_t i;
575
576 for (i = 0; gpt_ptypes[i].guid != NULL; i++) {
577 if (p->part_type[3] == get_byte(gpt_ptypes[i].guid +0) &&
578 p->part_type[2] == get_byte(gpt_ptypes[i].guid +2) &&
579 p->part_type[1] == get_byte(gpt_ptypes[i].guid +4) &&
580 p->part_type[0] == get_byte(gpt_ptypes[i].guid +6) &&
581
582 p->part_type[5] == get_byte(gpt_ptypes[i].guid +8) &&
583 p->part_type[4] == get_byte(gpt_ptypes[i].guid +10) &&
584
585 p->part_type[7] == get_byte(gpt_ptypes[i].guid +12) &&
586 p->part_type[6] == get_byte(gpt_ptypes[i].guid +14) &&
587
588 p->part_type[8] == get_byte(gpt_ptypes[i].guid +16) &&
589 p->part_type[9] == get_byte(gpt_ptypes[i].guid +18) &&
590 p->part_type[10] == get_byte(gpt_ptypes[i].guid +20) &&
591 p->part_type[11] == get_byte(gpt_ptypes[i].guid +22) &&
592 p->part_type[12] == get_byte(gpt_ptypes[i].guid +24) &&
593 p->part_type[13] == get_byte(gpt_ptypes[i].guid +26) &&
594 p->part_type[14] == get_byte(gpt_ptypes[i].guid +28) &&
595 p->part_type[15] == get_byte(gpt_ptypes[i].guid +30))
596 break;
597 }
598
599 return i;
600}
601
602/** Set partition type
603 * @param p partition to be set
604 * @param type partition type to set
605 * - see our fine selection at gpt_ptypes to choose from
606 */
607void gpt_set_part_type(gpt_part_t * p, size_t type)
608{
609 /* Beware: first 3 blocks are byteswapped! */
610 p->part_type[3] = gpt_ptypes[type].guid[0];
611 p->part_type[2] = gpt_ptypes[type].guid[1];
612 p->part_type[1] = gpt_ptypes[type].guid[2];
613 p->part_type[0] = gpt_ptypes[type].guid[3];
614
615 p->part_type[5] = gpt_ptypes[type].guid[4];
616 p->part_type[4] = gpt_ptypes[type].guid[5];
617
618 p->part_type[7] = gpt_ptypes[type].guid[6];
619 p->part_type[6] = gpt_ptypes[type].guid[7];
620
621 p->part_type[8] = gpt_ptypes[type].guid[8];
622 p->part_type[9] = gpt_ptypes[type].guid[9];
623 p->part_type[10] = gpt_ptypes[type].guid[10];
624 p->part_type[11] = gpt_ptypes[type].guid[11];
625 p->part_type[12] = gpt_ptypes[type].guid[12];
626 p->part_type[13] = gpt_ptypes[type].guid[13];
627 p->part_type[14] = gpt_ptypes[type].guid[14];
628 p->part_type[15] = gpt_ptypes[type].guid[15];
629}
630
631/** Get partition starting LBA */
632uint64_t gpt_get_start_lba(gpt_part_t * p)
633{
634 return uint64_t_le2host(p->start_lba);
635}
636
637/** Set partition starting LBA */
638void gpt_set_start_lba(gpt_part_t * p, uint64_t start)
639{
640 p->start_lba = host2uint64_t_le(start);
641}
642
643/** Get partition ending LBA */
644uint64_t gpt_get_end_lba(gpt_part_t * p)
645{
646 return uint64_t_le2host(p->end_lba);
647}
648
649/** Set partition ending LBA */
650void gpt_set_end_lba(gpt_part_t * p, uint64_t end)
651{
652 p->end_lba = host2uint64_t_le(end);
653}
654
655/** Get partition name */
656unsigned char * gpt_get_part_name(gpt_part_t * p)
657{
658 return p->part_name;
659}
660
661/** Copy partition name */
662void gpt_set_part_name(gpt_part_t *p, char *name, size_t length)
663{
664 if (length >= 72)
665 length = 71;
666
667 memcpy(p->part_name, name, length);
668 p->part_name[length] = '\0';
669}
670
671/** Get partition attribute */
672bool gpt_get_flag(gpt_part_t * p, GPT_ATTR flag)
673{
674 return (p->attributes & (((uint64_t) 1) << flag)) ? 1 : 0;
675}
676
677/** Set partition attribute */
678void gpt_set_flag(gpt_part_t * p, GPT_ATTR flag, bool value)
679{
680 uint64_t attr = p->attributes;
681
682 if (value)
683 attr = attr | (((uint64_t) 1) << flag);
684 else
685 attr = attr ^ (attr & (((uint64_t) 1) << flag));
686
687 p->attributes = attr;
688}
689
690/** Generate a new pseudo-random UUID
691 * @param uuid Pointer to the UUID to overwrite.
692 */
693void gpt_set_random_uuid(uint8_t * uuid)
694{
695 srandom((unsigned int) uuid);
696
697 unsigned int i;
698 for (i = 0; i < 16/sizeof(long int); ++i)
699 ((long int *)uuid)[i] = random();
700
701}
702
703/** Get next aligned address */
704uint64_t gpt_get_next_aligned(uint64_t addr, unsigned int alignment)
705{
706 uint64_t div = addr / alignment;
707 return (div + 1) * alignment;
708}
709
710/* Internal functions follow */
711
712static int load_and_check_header(service_id_t dev_handle, aoff64_t addr, size_t b_size, gpt_header_t * header)
713{
714 int rc;
715
716 rc = block_read_direct(dev_handle, addr, GPT_HDR_BS, header);
717 if (rc != EOK)
718 return rc;
719
720 unsigned int i;
721 /* Check the EFI signature */
722 for (i = 0; i < 8; ++i) {
723 if (header->efi_signature[i] != efi_signature[i])
724 return EINVAL;
725 }
726
727 /* Check the CRC32 of the header */
728 uint32_t crc = header->header_crc32;
729 header->header_crc32 = 0;
730 if (crc != compute_crc32((uint8_t *) header, header->header_size))
731 return EBADCHECKSUM;
732 else
733 header->header_crc32 = crc;
734
735 /* Check for zeroes in the rest of the block */
736 for (i = sizeof(gpt_header_t); i < b_size; ++i) {
737 if (((uint8_t *) header)[i] != 0)
738 return EINVAL;
739 }
740
741 return EOK;
742}
743
744static gpt_partitions_t * alloc_part_array(uint32_t num)
745{
746 gpt_partitions_t * res = malloc(sizeof(gpt_partitions_t));
747 if (res == NULL) {
748 errno = ENOMEM;
749 return NULL;
750 }
751
752 uint32_t size = num > GPT_BASE_PART_NUM ? num : GPT_BASE_PART_NUM;
753 res->part_array = malloc(size * sizeof(gpt_entry_t));
754 if (res->part_array == NULL) {
755 free(res);
756 errno = ENOMEM;
757 return NULL;
758 }
759
760 memset(res->part_array, 0, size * sizeof(gpt_entry_t));
761
762 res->fill = 0;
763 res->arr_size = num;
764
765 return res;
766}
767
768static int extend_part_array(gpt_partitions_t * p)
769{
770 size_t nsize = p->arr_size * 2;
771 gpt_entry_t * tmp = malloc(nsize * sizeof(gpt_entry_t));
772 if(tmp == NULL) {
773 errno = ENOMEM;
774 return -1;
775 }
776
777 memcpy(tmp, p->part_array, p->fill * sizeof(gpt_entry_t));
778 free(p->part_array);
779 p->part_array = tmp;
780 p->arr_size = nsize;
781
782 return 0;
783}
784
785static int reduce_part_array(gpt_partitions_t * p)
786{
787 if(p->arr_size > GPT_MIN_PART_NUM) {
788 unsigned int nsize = p->arr_size / 2;
789 nsize = nsize > GPT_MIN_PART_NUM ? nsize : GPT_MIN_PART_NUM;
790 gpt_entry_t * tmp = malloc(nsize * sizeof(gpt_entry_t));
791 if(tmp == NULL)
792 return ENOMEM;
793
794 memcpy(tmp, p->part_array, p->fill < nsize ? p->fill : nsize);
795 free(p->part_array);
796 p->part_array = tmp;
797 p->arr_size = nsize;
798 }
799
800 return 0;
801}
802
803/*static long long nearest_larger_int(double a)
804{
805 if ((long long) a == a) {
806 return (long long) a;
807 }
808
809 return ((long long) a) + 1;
810}*/
811
812/* Parse a byte from a string in hexadecimal
813 * i.e., "FF" => 255
814 */
815static uint8_t get_byte(const char * c)
816{
817 uint8_t val = 0;
818 char hex[3] = {*c, *(c+1), 0};
819
820 errno = str_uint8_t(hex, NULL, 16, false, &val);
821 return val;
822}
823
824static bool check_overlap(gpt_part_t * p1, gpt_part_t * p2)
825{
826 if (gpt_get_start_lba(p1) < gpt_get_start_lba(p2) && gpt_get_end_lba(p1) <= gpt_get_start_lba(p2)) {
827 return false;
828 } else if (gpt_get_start_lba(p1) > gpt_get_start_lba(p2) && gpt_get_end_lba(p2) <= gpt_get_start_lba(p1)) {
829 return false;
830 }
831
832 return true;
833}
834
835
Note: See TracBrowser for help on using the repository browser.