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