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
RevLine 
[cbd64057]1/*
[5beb1ff]2 * Copyright (c) 2011, 2012, 2013 Dominik Taborsky
[cbd64057]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
[7570e800]40#include <ipc/bd.h>
41#include <async.h>
42#include <stdio.h>
43#include <block.h>
[cbd64057]44#include <errno.h>
[7570e800]45#include <stdlib.h>
46#include <assert.h>
47#include <byteorder.h>
48#include <checksum.h>
[cbd64057]49#include <mem.h>
50
51#include "libgpt.h"
52
[dc76f4a]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);
[1c8bfe8]55static int extend_part_array(gpt_partitions_t *);
56static int reduce_part_array(gpt_partitions_t *);
[dc76f4a]57//static long long nearest_larger_int(double);
[1c8bfe8]58static uint8_t get_byte(const char *);
[dc76f4a]59static bool check_overlap(gpt_part_t *, gpt_part_t *);
[2b55edb]60static bool check_encaps(gpt_part_t *, uint64_t, uint64_t);
[44c4886]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
[dc76f4a]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
[44c4886]76 label->gpt = NULL;
[dc76f4a]77
[44c4886]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}
[7570e800]94
[700f89e]95/** Allocate memory for gpt header */
[44c4886]96gpt_t * gpt_alloc_header(size_t size)
[700f89e]97{
[44c4886]98 gpt_t *gpt = malloc(sizeof(gpt_t));
99 if (gpt == NULL)
100 return NULL;
101
[dc76f4a]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 */
[44c4886]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);
[700f89e]123}
124
[cbd64057]125/** Read GPT from specific device
[44c4886]126 * @param label label structure to fill
127 * @param dev_handle device to read GPT from
[d617050]128 *
[44c4886]129 * @return EOK on success, errorcode on error
[cbd64057]130 */
[44c4886]131int gpt_read_header(gpt_label_t *label, service_id_t dev_handle)
[cbd64057]132{
133 int rc;
134 size_t b_size;
[8f6c7785]135
136 rc = block_init(EXCHANGE_ATOMIC, dev_handle, 512);
137 if (rc != EOK)
[8559fa0]138 goto fail;
[8f6c7785]139
[cbd64057]140 rc = block_get_bsize(dev_handle, &b_size);
[44c4886]141 if (rc != EOK)
[8559fa0]142 goto fini_fail;
[8f6c7785]143
[44c4886]144 if (label->gpt == NULL) {
145 label->gpt = gpt_alloc_header(b_size);
[8559fa0]146 if (label->gpt == NULL) {
147 rc = ENOMEM;
148 goto fini_fail;
149 }
[cbd64057]150 }
[8f6c7785]151
[44c4886]152 rc = load_and_check_header(dev_handle, GPT_HDR_BA, b_size, label->gpt->header);
[cbd64057]153 if (rc == EBADCHECKSUM || rc == EINVAL) {
154 aoff64_t n_blocks;
155 rc = block_get_nblocks(dev_handle, &n_blocks);
[44c4886]156 if (rc != EOK)
[8559fa0]157 goto free_fail;
[d617050]158
[44c4886]159 rc = load_and_check_header(dev_handle, n_blocks - 1, b_size, label->gpt->header);
160 if (rc == EBADCHECKSUM || rc == EINVAL)
[8559fa0]161 goto free_fail;
[cbd64057]162 }
[8f6c7785]163
[44c4886]164 label->device = dev_handle;
[8f6c7785]165 block_fini(dev_handle);
[44c4886]166 return EOK;
[8f6c7785]167
[8559fa0]168free_fail:
[44c4886]169 gpt_free_gpt(label->gpt);
170 label->gpt = NULL;
[8559fa0]171fini_fail:
172 block_fini(dev_handle);
173fail:
[44c4886]174 return rc;
[cbd64057]175}
176
177/** Write GPT header to device
[44c4886]178 * @param label GPT label header to be written
179 * @param dev_handle device handle to write the data to
[d617050]180 *
[44c4886]181 * @return EOK on success, libblock error code otherwise
[d617050]182 *
[44c4886]183 * Note: Firstly write partitions (if modified), then gpt header.
[cbd64057]184 */
[44c4886]185int gpt_write_header(gpt_label_t *label, service_id_t dev_handle)
[cbd64057]186{
187 int rc;
188 size_t b_size;
[dc76f4a]189
190 /* The comm_size argument (the last one) is ignored */
191 rc = block_init(EXCHANGE_ATOMIC, dev_handle, 4096);
[1c8bfe8]192 if (rc != EOK && rc != EEXIST)
[cbd64057]193 return rc;
[dc76f4a]194
[8f6c7785]195 rc = block_get_bsize(dev_handle, &b_size);
[cbd64057]196 if (rc != EOK)
197 return rc;
[dc76f4a]198
[cbd64057]199 aoff64_t n_blocks;
200 rc = block_get_nblocks(dev_handle, &n_blocks);
[44c4886]201 if (rc != EOK) {
202 block_fini(dev_handle);
[cbd64057]203 return rc;
[44c4886]204 }
[dc76f4a]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
[cbd64057]222 /* Write to backup GPT header location */
[44c4886]223 rc = block_write_direct(dev_handle, n_blocks - 1, GPT_HDR_BS, label->gpt->header);
[dc76f4a]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);
[cbd64057]244 block_fini(dev_handle);
245 if (rc != EOK)
246 return rc;
[dc76f4a]247
248
[cbd64057]249 return 0;
250}
251
[700f89e]252/** Alloc partition array */
[44c4886]253gpt_partitions_t * gpt_alloc_partitions()
[700f89e]254{
[dc76f4a]255 return alloc_part_array(GPT_MIN_PART_NUM);
[700f89e]256}
257
[cbd64057]258/** Parse partitions from GPT
[44c4886]259 * @param label GPT label to be parsed
[d617050]260 *
[44c4886]261 * @return EOK on success, errorcode otherwise
[cbd64057]262 */
[44c4886]263int gpt_read_partitions(gpt_label_t *label)
[cbd64057]264{
[7570e800]265 int rc;
266 unsigned int i;
[c3cbbb2]267 uint32_t fillries = uint32_t_le2host(label->gpt->header->fillries);
[44c4886]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) {
[c3cbbb2]272 label->parts = alloc_part_array(fillries);
[44c4886]273 if (label->parts == NULL) {
274 return ENOMEM;
275 }
[cbd64057]276 }
[d617050]277
[8559fa0]278 /* comm_size is ignored */
[44c4886]279 rc = block_init(EXCHANGE_SERIALIZE, label->device, sizeof(gpt_entry_t));
280 if (rc != EOK)
281 goto fail;
[d617050]282
[cbd64057]283 size_t block_size;
[44c4886]284 rc = block_get_bsize(label->device, &block_size);
285 if (rc != EOK)
[8559fa0]286 goto fini_fail;
[d617050]287
[cbd64057]288 //size_t bufpos = 0;
289 //size_t buflen = 0;
290 aoff64_t pos = ent_lba * block_size;
[d617050]291
[dc76f4a]292 /*
293 * Now we read just sizeof(gpt_entry_t) bytes for each entry from the device.
[cbd64057]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
[dc76f4a]297 * don't break backward compatibility)
298 */
[c3cbbb2]299 for (i = 0; i < fillries; ++i) {
[dc76f4a]300 /*FIXME: this does bypass cache... */
[44c4886]301 rc = block_read_bytes_direct(label->device, pos, sizeof(gpt_entry_t), label->parts->part_array + i);
[dc76f4a]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 */
[cbd64057]306 pos += ent_size;
[d617050]307
[44c4886]308 if (rc != EOK)
[8559fa0]309 goto fini_fail;
[cbd64057]310 }
[2b55edb]311
[8559fa0]312 uint32_t crc = compute_crc32((uint8_t *) label->parts->part_array,
[2b55edb]313 fillries * ent_size);
[d617050]314
[44c4886]315 if(uint32_t_le2host(label->gpt->header->pe_array_crc32) != crc)
[cbd64057]316 {
[44c4886]317 rc = EBADCHECKSUM;
[8559fa0]318 goto fini_fail;
[cbd64057]319 }
[8559fa0]320
321 block_fini(label->device);
[44c4886]322 return EOK;
323
[8559fa0]324fini_fail:
325 block_fini(label->device);
326
[44c4886]327fail:
328 gpt_free_partitions(label->parts);
329 label->parts = NULL;
330 return rc;
[cbd64057]331}
332
333/** Write GPT and partitions to device
[9bdfde73]334 * Note: also writes the header.
[44c4886]335 * @param label label to write
336 * @param dev_handle device to write the data to
[d617050]337 *
[44c4886]338 * @return returns EOK on succes, errorcode otherwise
[cbd64057]339 */
[44c4886]340int gpt_write_partitions(gpt_label_t *label, service_id_t dev_handle)
[cbd64057]341{
[7570e800]342 int rc;
[cbd64057]343 size_t b_size;
[44c4886]344 uint32_t e_size = uint32_t_le2host(label->gpt->header->entry_size);
[c3cbbb2]345 size_t fillries = label->parts->fill > GPT_MIN_PART_NUM ? label->parts->fill : GPT_MIN_PART_NUM;
[1c8bfe8]346
[2b55edb]347 if (e_size != sizeof(gpt_entry_t))
348 return ENOTSUP;
[44c4886]349
350 /* comm_size of 4096 is ignored */
351 rc = block_init(EXCHANGE_ATOMIC, dev_handle, 4096);
[1c8bfe8]352 if (rc != EOK && rc != EEXIST)
[cbd64057]353 return rc;
[1c8bfe8]354
[8f6c7785]355 rc = block_get_bsize(dev_handle, &b_size);
[cbd64057]356 if (rc != EOK)
[44c4886]357 goto fail;
[1c8bfe8]358
[cbd64057]359 aoff64_t n_blocks;
360 rc = block_get_nblocks(dev_handle, &n_blocks);
361 if (rc != EOK)
[44c4886]362 goto fail;
[1c8bfe8]363
[2b55edb]364 label->gpt->header->fillries = host2uint32_t_le(fillries);
[dc76f4a]365 uint64_t arr_blocks = (fillries * sizeof(gpt_entry_t)) / b_size;
[493b881]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);
[2b55edb]369
370 /* Perform checks */
371 gpt_part_foreach(label, p) {
372 if (gpt_get_part_type(p) == GPT_PTE_UNUSED)
373 continue;
374
[493b881]375 if (!check_encaps(p, n_blocks, gpt_space)) {
[2b55edb]376 rc = ERANGE;
[493b881]377 printf("encaps with: %llu, %llu, %llu\n", n_blocks, gpt_space, gpt_get_end_lba(p));
[2b55edb]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)) {
[493b881]387 printf("overlap with: %llu, %llu\n", gpt_get_start_lba(p), gpt_get_start_lba(q));
[2b55edb]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));
[dc76f4a]398
399
[cbd64057]400 /* Write to backup GPT partition array location */
[dc76f4a]401 rc = block_write_direct(dev_handle, n_blocks - arr_blocks - 1,
402 arr_blocks, label->parts->part_array);
[cbd64057]403 if (rc != EOK)
[44c4886]404 goto fail;
[1c8bfe8]405
406 /* Write to main GPT partition array location */
407 rc = block_write_direct(dev_handle, uint64_t_le2host(label->gpt->header->entry_lba),
[dc76f4a]408 arr_blocks, label->parts->part_array);
[1c8bfe8]409 if (rc != EOK)
410 goto fail;
411
[44c4886]412 return gpt_write_header(label, dev_handle);
413
414fail:
415 block_fini(dev_handle);
416 return rc;
[cbd64057]417}
418
[30440ed]419/** Alloc new partition
[d617050]420 *
[44c4886]421 * @return returns pointer to the new partition or NULL
[d617050]422 *
[44c4886]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.
[1c8bfe8]426 * Requires you to call gpt_free_partition afterwards.
[44c4886]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
[d617050]442 *
[44c4886]443 * @return returns pointer to the new partition or NULL on ENOMEM
[d617050]444 *
[44c4886]445 * Note: use either gpt_alloc_partition or gpt_get_partition.
[1c8bfe8]446 * This one returns a pointer to the first empty structure already
447 * inside the array, so don't call gpt_add_partition() afterwards.
[44c4886]448 * This is the one you will usually want.
[30440ed]449 */
[44c4886]450gpt_part_t * gpt_get_partition(gpt_label_t *label)
[7570e800]451{
[1c8bfe8]452 gpt_part_t *p;
453
[c3cbbb2]454
[1c8bfe8]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}
[d617050]468
[1c8bfe8]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;
[30440ed]494}
495
496/** Copy partition into partition array
[d617050]497 *
[44c4886]498 * @param parts target label
[30440ed]499 * @param partition source partition to copy
[d617050]500 *
[30440ed]501 * @return -1 on error, 0 otherwise
[d617050]502 *
[44c4886]503 * Note: for use with gpt_alloc_partition() only. You will get
504 * duplicates with gpt_get_partition().
[9bdfde73]505 * Note: does not call gpt_free_partition()!
[30440ed]506 */
[44c4886]507int gpt_add_partition(gpt_label_t *label, gpt_part_t *partition)
[30440ed]508{
[dc76f4a]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);
[44c4886]520
[9bdfde73]521
[dc76f4a]522 memcpy(p, partition, sizeof(gpt_entry_t));
523
[9bdfde73]524
[44c4886]525 return EOK;
[7570e800]526}
527
[30440ed]528/** Remove partition from array
[44c4886]529 * @param label label to remove from
530 * @param idx index of the partition to remove
[d617050]531 *
[44c4886]532 * @return EOK on success, ENOMEM on array reduction failure
[d617050]533 *
[30440ed]534 * Note: even if it fails, the partition still gets removed. Only
535 * reducing the array failed.
536 */
[44c4886]537int gpt_remove_partition(gpt_label_t *label, size_t idx)
[7570e800]538{
[dc76f4a]539 if (idx >= label->parts->arr_size)
[1c8bfe8]540 return EINVAL;
[44c4886]541
[dc76f4a]542 /*
543 * FIXME!
[1c8bfe8]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
[dc76f4a]546 * the first 128 partitions, we would forget to write the last one.
547 */
[1c8bfe8]548 memset(label->parts->part_array + idx, 0, sizeof(gpt_entry_t));
549
[dc76f4a]550 if (label->parts->fill > idx)
551 label->parts->fill = idx;
[1c8bfe8]552
[dc76f4a]553 /*
554 * FIXME! HOPEFULLY FIXED.
[1c8bfe8]555 * We cannot reduce the array so simply. We may have some partitions
[dc76f4a]556 * there since we allow blank spots.
557 */
[9bdfde73]558 gpt_part_t * p;
[dc76f4a]559
560 if (label->parts->fill > GPT_MIN_PART_NUM &&
561 label->parts->fill < (label->parts->arr_size / 2) - GPT_IGNORE_FILL_NUM) {
[9bdfde73]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
[44c4886]568 if (reduce_part_array(label->parts) == ENOMEM)
569 return ENOMEM;
[30440ed]570 }
[d617050]571
[44c4886]572 return EOK;
[cbd64057]573}
574
575/** Free partition list
[d617050]576 *
[cbd64057]577 * @param parts partition list to be freed
578 */
[271e24a]579void gpt_free_partitions(gpt_partitions_t * parts)
[cbd64057]580{
581 free(parts->part_array);
582 free(parts);
583}
584
[30440ed]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;
[1c8bfe8]591
[30440ed]592 for (i = 0; gpt_ptypes[i].guid != NULL; i++) {
[1c8bfe8]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;
[30440ed]613 }
[1c8bfe8]614
[30440ed]615 return i;
616}
617
[cbd64057]618/** Set partition type
619 * @param p partition to be set
620 * @param type partition type to set
[d617050]621 * - see our fine selection at gpt_ptypes to choose from
[cbd64057]622 */
[d617050]623void gpt_set_part_type(gpt_part_t * p, size_t type)
[cbd64057]624{
625 /* Beware: first 3 blocks are byteswapped! */
[d617050]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);
[cbd64057]663}
664
[d617050]665/** Set partition ending LBA */
666void gpt_set_end_lba(gpt_part_t * p, uint64_t end)
[30440ed]667{
[d617050]668 p->end_lba = host2uint64_t_le(end);
669}
670
[44c4886]671/** Get partition name */
[d617050]672unsigned char * gpt_get_part_name(gpt_part_t * p)
673{
674 return p->part_name;
[30440ed]675}
676
[cbd64057]677/** Copy partition name */
[1c8bfe8]678void gpt_set_part_name(gpt_part_t *p, char *name, size_t length)
[cbd64057]679{
[30440ed]680 if (length >= 72)
681 length = 71;
[d617050]682
[cbd64057]683 memcpy(p->part_name, name, length);
[30440ed]684 p->part_name[length] = '\0';
685}
686
687/** Get partition attribute */
[d617050]688bool gpt_get_flag(gpt_part_t * p, GPT_ATTR flag)
[30440ed]689{
[d617050]690 return (p->attributes & (((uint64_t) 1) << flag)) ? 1 : 0;
[30440ed]691}
692
693/** Set partition attribute */
[d617050]694void gpt_set_flag(gpt_part_t * p, GPT_ATTR flag, bool value)
[30440ed]695{
[d617050]696 uint64_t attr = p->attributes;
[30440ed]697
698 if (value)
699 attr = attr | (((uint64_t) 1) << flag);
700 else
701 attr = attr ^ (attr & (((uint64_t) 1) << flag));
702
[d617050]703 p->attributes = attr;
[cbd64057]704}
705
[dc76f4a]706/** Generate a new pseudo-random UUID
707 * @param uuid Pointer to the UUID to overwrite.
708 */
[c3cbbb2]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
[dc76f4a]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 */
[cbd64057]727
[7570e800]728static int load_and_check_header(service_id_t dev_handle, aoff64_t addr, size_t b_size, gpt_header_t * header)
[cbd64057]729{
[7570e800]730 int rc;
[d617050]731
[7570e800]732 rc = block_read_direct(dev_handle, addr, GPT_HDR_BS, header);
[cbd64057]733 if (rc != EOK)
734 return rc;
[d617050]735
[7570e800]736 unsigned int i;
[cbd64057]737 /* Check the EFI signature */
738 for (i = 0; i < 8; ++i) {
[7570e800]739 if (header->efi_signature[i] != efi_signature[i])
[cbd64057]740 return EINVAL;
741 }
[d617050]742
[cbd64057]743 /* Check the CRC32 of the header */
[7570e800]744 uint32_t crc = header->header_crc32;
745 header->header_crc32 = 0;
746 if (crc != compute_crc32((uint8_t *) header, header->header_size))
[cbd64057]747 return EBADCHECKSUM;
748 else
[7570e800]749 header->header_crc32 = crc;
[d617050]750
[cbd64057]751 /* Check for zeroes in the rest of the block */
752 for (i = sizeof(gpt_header_t); i < b_size; ++i) {
[7570e800]753 if (((uint8_t *) header)[i] != 0)
754 return EINVAL;
[cbd64057]755 }
[d617050]756
[cbd64057]757 return EOK;
758}
759
[271e24a]760static gpt_partitions_t * alloc_part_array(uint32_t num)
[cbd64057]761{
[271e24a]762 gpt_partitions_t * res = malloc(sizeof(gpt_partitions_t));
[cbd64057]763 if (res == NULL) {
764 errno = ENOMEM;
765 return NULL;
766 }
[dc76f4a]767
[cbd64057]768 uint32_t size = num > GPT_BASE_PART_NUM ? num : GPT_BASE_PART_NUM;
769 res->part_array = malloc(size * sizeof(gpt_entry_t));
[7570e800]770 if (res->part_array == NULL) {
[cbd64057]771 free(res);
772 errno = ENOMEM;
773 return NULL;
774 }
[dc76f4a]775
776 memset(res->part_array, 0, size * sizeof(gpt_entry_t));
777
[c3cbbb2]778 res->fill = 0;
779 res->arr_size = num;
[d617050]780
[cbd64057]781 return res;
782}
783
[271e24a]784static int extend_part_array(gpt_partitions_t * p)
[cbd64057]785{
[c3cbbb2]786 size_t nsize = p->arr_size * 2;
[cbd64057]787 gpt_entry_t * tmp = malloc(nsize * sizeof(gpt_entry_t));
788 if(tmp == NULL) {
789 errno = ENOMEM;
790 return -1;
791 }
[c3cbbb2]792
793 memcpy(tmp, p->part_array, p->fill * sizeof(gpt_entry_t));
[cbd64057]794 free(p->part_array);
795 p->part_array = tmp;
796 p->arr_size = nsize;
[d617050]797
[cbd64057]798 return 0;
799}
800
[271e24a]801static int reduce_part_array(gpt_partitions_t * p)
[cbd64057]802{
803 if(p->arr_size > GPT_MIN_PART_NUM) {
804 unsigned int nsize = p->arr_size / 2;
[44c4886]805 nsize = nsize > GPT_MIN_PART_NUM ? nsize : GPT_MIN_PART_NUM;
[cbd64057]806 gpt_entry_t * tmp = malloc(nsize * sizeof(gpt_entry_t));
[44c4886]807 if(tmp == NULL)
808 return ENOMEM;
[d617050]809
[dc76f4a]810 memcpy(tmp, p->part_array, p->fill < nsize ? p->fill : nsize);
[cbd64057]811 free(p->part_array);
812 p->part_array = tmp;
813 p->arr_size = nsize;
814 }
[d617050]815
[cbd64057]816 return 0;
817}
818
[dc76f4a]819/*static long long nearest_larger_int(double a)
[cbd64057]820{
821 if ((long long) a == a) {
822 return (long long) a;
823 }
[d617050]824
[cbd64057]825 return ((long long) a) + 1;
[dc76f4a]826}*/
[cbd64057]827
[dc76f4a]828/* Parse a byte from a string in hexadecimal
829 * i.e., "FF" => 255
830 */
[1c8bfe8]831static uint8_t get_byte(const char * c)
[d617050]832{
[1c8bfe8]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;
[d617050]838}
839
[dc76f4a]840static bool check_overlap(gpt_part_t * p1, gpt_part_t * p2)
[9bdfde73]841{
[493b881]842 if (gpt_get_start_lba(p1) < gpt_get_start_lba(p2) && gpt_get_end_lba(p1) < gpt_get_start_lba(p2)) {
[dc76f4a]843 return false;
[493b881]844 } else if (gpt_get_start_lba(p1) > gpt_get_start_lba(p2) && gpt_get_end_lba(p2) < gpt_get_start_lba(p1)) {
[dc76f4a]845 return false;
[9bdfde73]846 }
[cbd64057]847
[dc76f4a]848 return true;
[9bdfde73]849}
[cbd64057]850
[2b55edb]851static bool check_encaps(gpt_part_t *p, uint64_t n_blocks, uint64_t first_lba)
852{
[493b881]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)
[2b55edb]858 return true;
859
860 return false;
861}
Note: See TracBrowser for help on using the repository browser.