source: mainline/uspace/lib/gpt/libgpt.c@ 2b55edb

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

libgpt checks boundaries

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