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