source: mainline/uspace/lib/gpt/libgpt.c@ 6317b33

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

GPT updates

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