source: mainline/uspace/lib/gpt/libgpt.c@ 493b881

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

libgpt correctly checks boundaries

  • Property mode set to 100644
File size: 22.3 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 uint64_t gpt_space = arr_blocks + GPT_HDR_BS + 1; /* +1 for Protective MBR */
367 label->gpt->header->first_usable_lba = host2uint64_t_le(gpt_space);
368 label->gpt->header->last_usable_lba = host2uint64_t_le(n_blocks - gpt_space - 1);
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, gpt_space)) {
376 rc = ERANGE;
377 printf("encaps with: %llu, %llu, %llu\n", n_blocks, gpt_space, gpt_get_end_lba(p));
378 goto fail;
379 }
380
381 gpt_part_foreach(label, q) {
382 if (p == q)
383 continue;
384
385 if (gpt_get_part_type(p) != GPT_PTE_UNUSED) {
386 if (check_overlap(p, q)) {
387 printf("overlap with: %llu, %llu\n", gpt_get_start_lba(p), gpt_get_start_lba(q));
388 rc = ERANGE;
389 goto fail;
390 }
391 }
392 }
393 }
394
395 label->gpt->header->pe_array_crc32 = host2uint32_t_le(compute_crc32(
396 (uint8_t *) label->parts->part_array,
397 fillries * e_size));
398
399
400 /* Write to backup GPT partition array location */
401 rc = block_write_direct(dev_handle, n_blocks - arr_blocks - 1,
402 arr_blocks, label->parts->part_array);
403 if (rc != EOK)
404 goto fail;
405
406 /* Write to main GPT partition array location */
407 rc = block_write_direct(dev_handle, uint64_t_le2host(label->gpt->header->entry_lba),
408 arr_blocks, label->parts->part_array);
409 if (rc != EOK)
410 goto fail;
411
412 return gpt_write_header(label, dev_handle);
413
414fail:
415 block_fini(dev_handle);
416 return rc;
417}
418
419/** Alloc new partition
420 *
421 * @return returns pointer to the new partition or NULL
422 *
423 * Note: use either gpt_alloc_partition or gpt_get_partition.
424 * This returns a memory block (zero-filled) and needs gpt_add_partition()
425 * to be called to insert it into a partition array.
426 * Requires you to call gpt_free_partition afterwards.
427 */
428gpt_part_t * gpt_alloc_partition(void)
429{
430 gpt_part_t *p = malloc(sizeof(gpt_part_t));
431 if (p == NULL)
432 return NULL;
433
434 memset(p, 0, sizeof(gpt_part_t));
435
436 return p;
437}
438
439/** Alloc new partition already inside the label
440 *
441 * @param label label to carry new partition
442 *
443 * @return returns pointer to the new partition or NULL on ENOMEM
444 *
445 * Note: use either gpt_alloc_partition or gpt_get_partition.
446 * This one returns a pointer to the first empty structure already
447 * inside the array, so don't call gpt_add_partition() afterwards.
448 * This is the one you will usually want.
449 */
450gpt_part_t * gpt_get_partition(gpt_label_t *label)
451{
452 gpt_part_t *p;
453
454
455 /* Find the first empty entry */
456 do {
457 if (label->parts->fill == label->parts->arr_size) {
458 if (extend_part_array(label->parts) == -1)
459 return NULL;
460 }
461
462 p = label->parts->part_array + label->parts->fill++;
463
464 } while (gpt_get_part_type(p) != GPT_PTE_UNUSED);
465
466 return p;
467}
468
469/** Get partition already inside the label
470 *
471 * @param label label to carrying the partition
472 * @param idx index of the partition
473 *
474 * @return returns pointer to the partition
475 * or NULL when out of range
476 *
477 * Note: For new partitions use either gpt_alloc_partition or
478 * gpt_get_partition unless you want a partition at a specific place.
479 * This returns a pointer to a structure already inside the array,
480 * so don't call gpt_add_partition() afterwards.
481 * This function is handy when you want to change already existing
482 * partition or to simply write somewhere in the middle. This works only
483 * for indexes smaller than either 128 or the actual number of filled
484 * entries.
485 */
486gpt_part_t * gpt_get_partition_at(gpt_label_t *label, size_t idx)
487{
488 return NULL;
489
490 if (idx >= GPT_MIN_PART_NUM && idx >= label->parts->fill)
491 return NULL;
492
493 return label->parts->part_array + idx;
494}
495
496/** Copy partition into partition array
497 *
498 * @param parts target label
499 * @param partition source partition to copy
500 *
501 * @return -1 on error, 0 otherwise
502 *
503 * Note: for use with gpt_alloc_partition() only. You will get
504 * duplicates with gpt_get_partition().
505 * Note: does not call gpt_free_partition()!
506 */
507int gpt_add_partition(gpt_label_t *label, gpt_part_t *partition)
508{
509 gpt_part_t *p;
510 /* Find the first empty entry */
511 do {
512 if (label->parts->fill == label->parts->arr_size) {
513 if (extend_part_array(label->parts) == -1)
514 return ENOMEM;
515 }
516
517 p = label->parts->part_array + label->parts->fill++;
518
519 } while (gpt_get_part_type(p) != GPT_PTE_UNUSED);
520
521
522 memcpy(p, partition, sizeof(gpt_entry_t));
523
524
525 return EOK;
526}
527
528/** Remove partition from array
529 * @param label label to remove from
530 * @param idx index of the partition to remove
531 *
532 * @return EOK on success, ENOMEM on array reduction failure
533 *
534 * Note: even if it fails, the partition still gets removed. Only
535 * reducing the array failed.
536 */
537int gpt_remove_partition(gpt_label_t *label, size_t idx)
538{
539 if (idx >= label->parts->arr_size)
540 return EINVAL;
541
542 /*
543 * FIXME!
544 * If we allow blank spots, we break the array. If we have more than
545 * 128 partitions in the array and then remove something from
546 * the first 128 partitions, we would forget to write the last one.
547 */
548 memset(label->parts->part_array + idx, 0, sizeof(gpt_entry_t));
549
550 if (label->parts->fill > idx)
551 label->parts->fill = idx;
552
553 /*
554 * FIXME! HOPEFULLY FIXED.
555 * We cannot reduce the array so simply. We may have some partitions
556 * there since we allow blank spots.
557 */
558 gpt_part_t * p;
559
560 if (label->parts->fill > GPT_MIN_PART_NUM &&
561 label->parts->fill < (label->parts->arr_size / 2) - GPT_IGNORE_FILL_NUM) {
562 for (p = gpt_get_partition_at(label, label->parts->arr_size / 2);
563 p < label->parts->part_array + label->parts->arr_size; ++p) {
564 if (gpt_get_part_type(p) != GPT_PTE_UNUSED)
565 return EOK;
566 }
567
568 if (reduce_part_array(label->parts) == ENOMEM)
569 return ENOMEM;
570 }
571
572 return EOK;
573}
574
575/** Free partition list
576 *
577 * @param parts partition list to be freed
578 */
579void gpt_free_partitions(gpt_partitions_t * parts)
580{
581 free(parts->part_array);
582 free(parts);
583}
584
585/** Get partition type by linear search
586 * (hopefully this doesn't get slow)
587 */
588size_t gpt_get_part_type(gpt_part_t * p)
589{
590 size_t i;
591
592 for (i = 0; gpt_ptypes[i].guid != NULL; i++) {
593 if (p->part_type[3] == get_byte(gpt_ptypes[i].guid +0) &&
594 p->part_type[2] == get_byte(gpt_ptypes[i].guid +2) &&
595 p->part_type[1] == get_byte(gpt_ptypes[i].guid +4) &&
596 p->part_type[0] == get_byte(gpt_ptypes[i].guid +6) &&
597
598 p->part_type[5] == get_byte(gpt_ptypes[i].guid +8) &&
599 p->part_type[4] == get_byte(gpt_ptypes[i].guid +10) &&
600
601 p->part_type[7] == get_byte(gpt_ptypes[i].guid +12) &&
602 p->part_type[6] == get_byte(gpt_ptypes[i].guid +14) &&
603
604 p->part_type[8] == get_byte(gpt_ptypes[i].guid +16) &&
605 p->part_type[9] == get_byte(gpt_ptypes[i].guid +18) &&
606 p->part_type[10] == get_byte(gpt_ptypes[i].guid +20) &&
607 p->part_type[11] == get_byte(gpt_ptypes[i].guid +22) &&
608 p->part_type[12] == get_byte(gpt_ptypes[i].guid +24) &&
609 p->part_type[13] == get_byte(gpt_ptypes[i].guid +26) &&
610 p->part_type[14] == get_byte(gpt_ptypes[i].guid +28) &&
611 p->part_type[15] == get_byte(gpt_ptypes[i].guid +30))
612 break;
613 }
614
615 return i;
616}
617
618/** Set partition type
619 * @param p partition to be set
620 * @param type partition type to set
621 * - see our fine selection at gpt_ptypes to choose from
622 */
623void gpt_set_part_type(gpt_part_t * p, size_t type)
624{
625 /* Beware: first 3 blocks are byteswapped! */
626 p->part_type[3] = gpt_ptypes[type].guid[0];
627 p->part_type[2] = gpt_ptypes[type].guid[1];
628 p->part_type[1] = gpt_ptypes[type].guid[2];
629 p->part_type[0] = gpt_ptypes[type].guid[3];
630
631 p->part_type[5] = gpt_ptypes[type].guid[4];
632 p->part_type[4] = gpt_ptypes[type].guid[5];
633
634 p->part_type[7] = gpt_ptypes[type].guid[6];
635 p->part_type[6] = gpt_ptypes[type].guid[7];
636
637 p->part_type[8] = gpt_ptypes[type].guid[8];
638 p->part_type[9] = gpt_ptypes[type].guid[9];
639 p->part_type[10] = gpt_ptypes[type].guid[10];
640 p->part_type[11] = gpt_ptypes[type].guid[11];
641 p->part_type[12] = gpt_ptypes[type].guid[12];
642 p->part_type[13] = gpt_ptypes[type].guid[13];
643 p->part_type[14] = gpt_ptypes[type].guid[14];
644 p->part_type[15] = gpt_ptypes[type].guid[15];
645}
646
647/** Get partition starting LBA */
648uint64_t gpt_get_start_lba(gpt_part_t * p)
649{
650 return uint64_t_le2host(p->start_lba);
651}
652
653/** Set partition starting LBA */
654void gpt_set_start_lba(gpt_part_t * p, uint64_t start)
655{
656 p->start_lba = host2uint64_t_le(start);
657}
658
659/** Get partition ending LBA */
660uint64_t gpt_get_end_lba(gpt_part_t * p)
661{
662 return uint64_t_le2host(p->end_lba);
663}
664
665/** Set partition ending LBA */
666void gpt_set_end_lba(gpt_part_t * p, uint64_t end)
667{
668 p->end_lba = host2uint64_t_le(end);
669}
670
671/** Get partition name */
672unsigned char * gpt_get_part_name(gpt_part_t * p)
673{
674 return p->part_name;
675}
676
677/** Copy partition name */
678void gpt_set_part_name(gpt_part_t *p, char *name, size_t length)
679{
680 if (length >= 72)
681 length = 71;
682
683 memcpy(p->part_name, name, length);
684 p->part_name[length] = '\0';
685}
686
687/** Get partition attribute */
688bool gpt_get_flag(gpt_part_t * p, GPT_ATTR flag)
689{
690 return (p->attributes & (((uint64_t) 1) << flag)) ? 1 : 0;
691}
692
693/** Set partition attribute */
694void gpt_set_flag(gpt_part_t * p, GPT_ATTR flag, bool value)
695{
696 uint64_t attr = p->attributes;
697
698 if (value)
699 attr = attr | (((uint64_t) 1) << flag);
700 else
701 attr = attr ^ (attr & (((uint64_t) 1) << flag));
702
703 p->attributes = attr;
704}
705
706/** Generate a new pseudo-random UUID
707 * @param uuid Pointer to the UUID to overwrite.
708 */
709void gpt_set_random_uuid(uint8_t * uuid)
710{
711 srandom((unsigned int) uuid);
712
713 unsigned int i;
714 for (i = 0; i < 16/sizeof(long int); ++i)
715 ((long int *)uuid)[i] = random();
716
717}
718
719/** Get next aligned address */
720uint64_t gpt_get_next_aligned(uint64_t addr, unsigned int alignment)
721{
722 uint64_t div = addr / alignment;
723 return (div + 1) * alignment;
724}
725
726/* Internal functions follow */
727
728static int load_and_check_header(service_id_t dev_handle, aoff64_t addr, size_t b_size, gpt_header_t * header)
729{
730 int rc;
731
732 rc = block_read_direct(dev_handle, addr, GPT_HDR_BS, header);
733 if (rc != EOK)
734 return rc;
735
736 unsigned int i;
737 /* Check the EFI signature */
738 for (i = 0; i < 8; ++i) {
739 if (header->efi_signature[i] != efi_signature[i])
740 return EINVAL;
741 }
742
743 /* Check the CRC32 of the header */
744 uint32_t crc = header->header_crc32;
745 header->header_crc32 = 0;
746 if (crc != compute_crc32((uint8_t *) header, header->header_size))
747 return EBADCHECKSUM;
748 else
749 header->header_crc32 = crc;
750
751 /* Check for zeroes in the rest of the block */
752 for (i = sizeof(gpt_header_t); i < b_size; ++i) {
753 if (((uint8_t *) header)[i] != 0)
754 return EINVAL;
755 }
756
757 return EOK;
758}
759
760static gpt_partitions_t * alloc_part_array(uint32_t num)
761{
762 gpt_partitions_t * res = malloc(sizeof(gpt_partitions_t));
763 if (res == NULL) {
764 errno = ENOMEM;
765 return NULL;
766 }
767
768 uint32_t size = num > GPT_BASE_PART_NUM ? num : GPT_BASE_PART_NUM;
769 res->part_array = malloc(size * sizeof(gpt_entry_t));
770 if (res->part_array == NULL) {
771 free(res);
772 errno = ENOMEM;
773 return NULL;
774 }
775
776 memset(res->part_array, 0, size * sizeof(gpt_entry_t));
777
778 res->fill = 0;
779 res->arr_size = num;
780
781 return res;
782}
783
784static int extend_part_array(gpt_partitions_t * p)
785{
786 size_t nsize = p->arr_size * 2;
787 gpt_entry_t * tmp = malloc(nsize * sizeof(gpt_entry_t));
788 if(tmp == NULL) {
789 errno = ENOMEM;
790 return -1;
791 }
792
793 memcpy(tmp, p->part_array, p->fill * sizeof(gpt_entry_t));
794 free(p->part_array);
795 p->part_array = tmp;
796 p->arr_size = nsize;
797
798 return 0;
799}
800
801static int reduce_part_array(gpt_partitions_t * p)
802{
803 if(p->arr_size > GPT_MIN_PART_NUM) {
804 unsigned int nsize = p->arr_size / 2;
805 nsize = nsize > GPT_MIN_PART_NUM ? nsize : GPT_MIN_PART_NUM;
806 gpt_entry_t * tmp = malloc(nsize * sizeof(gpt_entry_t));
807 if(tmp == NULL)
808 return ENOMEM;
809
810 memcpy(tmp, p->part_array, p->fill < nsize ? p->fill : nsize);
811 free(p->part_array);
812 p->part_array = tmp;
813 p->arr_size = nsize;
814 }
815
816 return 0;
817}
818
819/*static long long nearest_larger_int(double a)
820{
821 if ((long long) a == a) {
822 return (long long) a;
823 }
824
825 return ((long long) a) + 1;
826}*/
827
828/* Parse a byte from a string in hexadecimal
829 * i.e., "FF" => 255
830 */
831static uint8_t get_byte(const char * c)
832{
833 uint8_t val = 0;
834 char hex[3] = {*c, *(c+1), 0};
835
836 errno = str_uint8_t(hex, NULL, 16, false, &val);
837 return val;
838}
839
840static bool check_overlap(gpt_part_t * p1, gpt_part_t * p2)
841{
842 if (gpt_get_start_lba(p1) < gpt_get_start_lba(p2) && gpt_get_end_lba(p1) < gpt_get_start_lba(p2)) {
843 return false;
844 } else if (gpt_get_start_lba(p1) > gpt_get_start_lba(p2) && gpt_get_end_lba(p2) < gpt_get_start_lba(p1)) {
845 return false;
846 }
847
848 return true;
849}
850
851static bool check_encaps(gpt_part_t *p, uint64_t n_blocks, uint64_t first_lba)
852{
853 /*
854 * We allow "<=" in the second expression because it lacks MBR so
855 * it's by 1 block smaller.
856 */
857 if (gpt_get_start_lba(p) >= first_lba && gpt_get_end_lba(p) <= n_blocks - first_lba)
858 return true;
859
860 return false;
861}
Note: See TracBrowser for help on using the repository browser.