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