source: mainline/uspace/lib/gpt/libgpt.c@ 8a45707d

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8a45707d was fc22069, checked in by Martin Decky <martin@…>, 10 years ago

block devices use the same interface, therefore the API of libblock should not expose the implementation details

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