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