source: mainline/uspace/lib/gpt/libgpt.c@ 44c4886

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

GPT API changes

  • Property mode set to 100644
File size: 16.5 KB
Line 
1/*
2 * Copyright (c) 2011, 2012, 2013 Dominik Taborsky
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
40#include <ipc/bd.h>
41#include <async.h>
42#include <stdio.h>
43#include <block.h>
44#include <errno.h>
45#include <stdlib.h>
46#include <assert.h>
47#include <byteorder.h>
48#include <checksum.h>
49#include <mem.h>
50
51#include "libgpt.h"
52
53static int load_and_check_header(service_id_t handle, aoff64_t addr, size_t b_size, gpt_header_t * header);
54static gpt_partitions_t * alloc_part_array(uint32_t num);
55static int extend_part_array(gpt_partitions_t * p);
56static int reduce_part_array(gpt_partitions_t * p);
57static long long nearest_larger_int(double a);
58
59
60/** Allocate memory for gpt label */
61gpt_label_t * gpt_alloc_label(void)
62{
63 gpt_label_t *label = malloc(sizeof(gpt_label_t));
64 if (label == NULL)
65 return NULL;
66
67 label->gpt = NULL;
68 label->parts = NULL;
69 label->device = 0;
70
71 return label;
72}
73
74/** Free gpt_label_t structure */
75void gpt_free_label(gpt_label_t *label)
76{
77 if (label->gpt != NULL)
78 gpt_free_gpt(label->gpt);
79
80 if (label->parts != NULL)
81 gpt_free_partitions(label->parts);
82
83 free(label);
84}
85
86/** Allocate memory for gpt header */
87gpt_t * gpt_alloc_header(size_t size)
88{
89 gpt_t *gpt = malloc(sizeof(gpt_t));
90 if (gpt == NULL)
91 return NULL;
92
93 // We might need only sizeof(gpt_header_t),
94 // but we should follow specs and have
95 // zeroes through all the rest of the block
96 size_t final_size = size > sizeof(gpt_header_t) ? size : sizeof(gpt_header_t);
97 gpt->header = malloc(final_size);
98 if (gpt->header == NULL) {
99 free(gpt);
100 return NULL;
101 }
102
103 memset(gpt->header, 0, final_size);
104
105 return gpt;
106}
107
108/** free() GPT header including gpt->header_lba */
109void gpt_free_gpt(gpt_t *gpt)
110{
111 free(gpt->header);
112 free(gpt);
113}
114
115/** Read GPT from specific device
116 * @param label label structure to fill
117 * @param dev_handle device to read GPT from
118 *
119 * @return EOK on success, errorcode on error
120 */
121int gpt_read_header(gpt_label_t *label, service_id_t dev_handle)
122{
123 int rc;
124 size_t b_size;
125
126 rc = block_init(EXCHANGE_ATOMIC, dev_handle, 512);
127 if (rc != EOK)
128 return rc;
129
130 rc = block_get_bsize(dev_handle, &b_size);
131 if (rc != EOK)
132 return rc;
133
134 if (label->gpt == NULL) {
135 label->gpt = gpt_alloc_header(b_size);
136 if (label->gpt == NULL)
137 return ENOMEM;
138 }
139
140 rc = load_and_check_header(dev_handle, GPT_HDR_BA, b_size, label->gpt->header);
141 if (rc == EBADCHECKSUM || rc == EINVAL) {
142 aoff64_t n_blocks;
143 rc = block_get_nblocks(dev_handle, &n_blocks);
144 if (rc != EOK)
145 goto fail;
146
147 rc = load_and_check_header(dev_handle, n_blocks - 1, b_size, label->gpt->header);
148 if (rc == EBADCHECKSUM || rc == EINVAL)
149 goto fail;
150 }
151
152 label->device = dev_handle;
153 block_fini(dev_handle);
154 return EOK;
155
156fail:
157 block_fini(dev_handle);
158 gpt_free_gpt(label->gpt);
159 label->gpt = NULL;
160 return rc;
161}
162
163/** Write GPT header to device
164 * @param label GPT label header to be written
165 * @param dev_handle device handle to write the data to
166 *
167 * @return EOK on success, libblock error code otherwise
168 *
169 * Note: Firstly write partitions (if modified), then gpt header.
170 */
171int gpt_write_header(gpt_label_t *label, service_id_t dev_handle)
172{
173 int rc;
174 size_t b_size;
175
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));
179
180 rc = block_init(EXCHANGE_ATOMIC, dev_handle, b_size);
181 if (rc != EOK)
182 return rc;
183
184 rc = block_get_bsize(dev_handle, &b_size);
185 if (rc != EOK)
186 return rc;
187
188 /* Write to main GPT header location */
189 rc = block_write_direct(dev_handle, GPT_HDR_BA, GPT_HDR_BS, label->gpt->header);
190 if (rc != EOK) {
191 block_fini(dev_handle);
192 return rc;
193 }
194
195 aoff64_t n_blocks;
196 rc = block_get_nblocks(dev_handle, &n_blocks);
197 if (rc != EOK) {
198 block_fini(dev_handle);
199 return rc;
200 }
201
202 /* Write to backup GPT header location */
203 //FIXME: those idiots thought it would be cool to have these fields in reverse order...
204 rc = block_write_direct(dev_handle, n_blocks - 1, GPT_HDR_BS, label->gpt->header);
205 block_fini(dev_handle);
206 if (rc != EOK)
207 return rc;
208
209 return 0;
210}
211
212/** Alloc partition array */
213gpt_partitions_t * gpt_alloc_partitions()
214{
215 return alloc_part_array(128);
216}
217
218/** Parse partitions from GPT
219 * @param label GPT label to be parsed
220 *
221 * @return EOK on success, errorcode otherwise
222 */
223int gpt_read_partitions(gpt_label_t *label)
224{
225 int rc;
226 unsigned int i;
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 }
236 }
237
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 */
241 rc = block_init(EXCHANGE_SERIALIZE, label->device, sizeof(gpt_entry_t));
242 if (rc != EOK)
243 goto fail;
244
245 size_t block_size;
246 rc = block_get_bsize(label->device, &block_size);
247 if (rc != EOK)
248 goto fail;
249
250 //size_t bufpos = 0;
251 //size_t buflen = 0;
252 aoff64_t pos = ent_lba * block_size;
253
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) */
259 for (i = 0; i < fill; ++i) {
260 //FIXME: this does bypass cache...
261 rc = block_read_bytes_direct(label->device, pos, sizeof(gpt_entry_t), label->parts->part_array + i);
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;
265
266 if (rc != EOK)
267 goto fail;
268 }
269
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 */
275 uint32_t crc = compute_crc32((uint8_t *) label->parts->part_array, label->parts->fill * sizeof(gpt_entry_t));
276
277 if(uint32_t_le2host(label->gpt->header->pe_array_crc32) != crc)
278 {
279 rc = EBADCHECKSUM;
280 goto fail;
281 }
282
283 return EOK;
284
285fail:
286 gpt_free_partitions(label->parts);
287 label->parts = NULL;
288 return rc;
289}
290
291/** Write GPT and partitions to device
292 * @param label label to write
293 * @param dev_handle device to write the data to
294 *
295 * @return returns EOK on succes, errorcode otherwise
296 */
297int gpt_write_partitions(gpt_label_t *label, service_id_t dev_handle)
298{
299 int rc;
300 size_t b_size;
301 uint32_t e_size = uint32_t_le2host(label->gpt->header->entry_size);
302
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);
309 if (rc != EOK)
310 return rc;
311
312 rc = block_get_bsize(dev_handle, &b_size);
313 if (rc != EOK)
314 goto fail;
315
316 /* Write to main GPT partition array location */
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);
320 if (rc != EOK)
321 goto fail;
322
323 aoff64_t n_blocks;
324 rc = block_get_nblocks(dev_handle, &n_blocks);
325 if (rc != EOK)
326 goto fail;
327
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)
332 goto fail;
333
334
335 return gpt_write_header(label, dev_handle);
336
337fail:
338 block_fini(dev_handle);
339 return rc;
340}
341
342/** Alloc new partition
343 *
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 */
351gpt_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
365 *
366 * @return returns pointer to the new partition or NULL on ENOMEM
367 *
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.
372 */
373gpt_part_t * gpt_get_partition(gpt_label_t *label)
374{
375 if (label->parts->fill == label->parts->arr_size) {
376 if (extend_part_array(label->parts) == -1)
377 return NULL;
378 }
379
380 return label->parts->part_array + label->parts->fill++;
381}
382
383/** Copy partition into partition array
384 *
385 * @param parts target label
386 * @param partition source partition to copy
387 *
388 * @return -1 on error, 0 otherwise
389 *
390 * Note: for use with gpt_alloc_partition() only. You will get
391 * duplicates with gpt_get_partition().
392 */
393int gpt_add_partition(gpt_label_t *label, gpt_part_t *partition)
394{
395 if (label->parts->fill == label->parts->arr_size) {
396 if (extend_part_array(label->parts) == -1)
397 return ENOMEM;
398 }
399
400 memcpy(label->parts->part_array + label->parts->fill++,
401 partition, sizeof(gpt_part_t));
402
403 return EOK;
404}
405
406/** Remove partition from array
407 * @param label label to remove from
408 * @param idx index of the partition to remove
409 *
410 * @return EOK on success, ENOMEM on array reduction failure
411 *
412 * Note: even if it fails, the partition still gets removed. Only
413 * reducing the array failed.
414 */
415int gpt_remove_partition(gpt_label_t *label, size_t idx)
416{
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;
422 }
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;
429 }
430
431 return EOK;
432}
433
434/** Free partition list
435 *
436 * @param parts partition list to be freed
437 */
438void gpt_free_partitions(gpt_partitions_t * parts)
439{
440 free(parts->part_array);
441 free(parts);
442}
443
444/** Get partition type by linear search
445 * (hopefully this doesn't get slow)
446 */
447size_t gpt_get_part_type(gpt_part_t * p)
448{
449 size_t i;
450 for (i = 0; gpt_ptypes[i].guid != NULL; i++) {
451 if (bcmp(p->part_type, gpt_ptypes[i].guid, 16) == 0) {
452 break;
453 }
454 }
455 return i;
456}
457
458/** Set partition type
459 * @param p partition to be set
460 * @param type partition type to set
461 * - see our fine selection at gpt_ptypes to choose from
462 */
463void gpt_set_part_type(gpt_part_t * p, size_t type)
464{
465 /* Beware: first 3 blocks are byteswapped! */
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 */
488uint64_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 */
494void 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 */
500uint64_t gpt_get_end_lba(gpt_part_t * p)
501{
502 return uint64_t_le2host(p->end_lba);
503}
504
505/** Set partition ending LBA */
506void gpt_set_end_lba(gpt_part_t * p, uint64_t end)
507{
508 p->end_lba = host2uint64_t_le(end);
509}
510
511/** Get partition name */
512unsigned char * gpt_get_part_name(gpt_part_t * p)
513{
514 return p->part_name;
515}
516
517/** Copy partition name */
518void gpt_set_part_name(gpt_part_t * p, char * name[], size_t length)
519{
520 if (length >= 72)
521 length = 71;
522
523 memcpy(p->part_name, name, length);
524 p->part_name[length] = '\0';
525}
526
527/** Get partition attribute */
528bool gpt_get_flag(gpt_part_t * p, GPT_ATTR flag)
529{
530 return (p->attributes & (((uint64_t) 1) << flag)) ? 1 : 0;
531}
532
533/** Set partition attribute */
534void gpt_set_flag(gpt_part_t * p, GPT_ATTR flag, bool value)
535{
536 uint64_t attr = p->attributes;
537
538 if (value)
539 attr = attr | (((uint64_t) 1) << flag);
540 else
541 attr = attr ^ (attr & (((uint64_t) 1) << flag));
542
543 p->attributes = attr;
544}
545
546// Internal functions follow //
547
548static int load_and_check_header(service_id_t dev_handle, aoff64_t addr, size_t b_size, gpt_header_t * header)
549{
550 int rc;
551
552 rc = block_read_direct(dev_handle, addr, GPT_HDR_BS, header);
553 if (rc != EOK)
554 return rc;
555
556 unsigned int i;
557 /* Check the EFI signature */
558 for (i = 0; i < 8; ++i) {
559 if (header->efi_signature[i] != efi_signature[i])
560 return EINVAL;
561 }
562
563 /* Check the CRC32 of the header */
564 uint32_t crc = header->header_crc32;
565 header->header_crc32 = 0;
566 if (crc != compute_crc32((uint8_t *) header, header->header_size))
567 return EBADCHECKSUM;
568 else
569 header->header_crc32 = crc;
570
571 /* Check for zeroes in the rest of the block */
572 for (i = sizeof(gpt_header_t); i < b_size; ++i) {
573 if (((uint8_t *) header)[i] != 0)
574 return EINVAL;
575 }
576
577 return EOK;
578}
579
580static gpt_partitions_t * alloc_part_array(uint32_t num)
581{
582 gpt_partitions_t * res = malloc(sizeof(gpt_partitions_t));
583 if (res == NULL) {
584 errno = ENOMEM;
585 return NULL;
586 }
587
588 uint32_t size = num > GPT_BASE_PART_NUM ? num : GPT_BASE_PART_NUM;
589 res->part_array = malloc(size * sizeof(gpt_entry_t));
590 if (res->part_array == NULL) {
591 free(res);
592 errno = ENOMEM;
593 return NULL;
594 }
595
596 res->fill = num;
597 res->arr_size = size;
598
599 return res;
600}
601
602static int extend_part_array(gpt_partitions_t * p)
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 }
610
611 memcpy(tmp, p->part_array, p->fill);
612 free(p->part_array);
613 p->part_array = tmp;
614 p->arr_size = nsize;
615
616 return 0;
617}
618
619static int reduce_part_array(gpt_partitions_t * p)
620{
621 if(p->arr_size > GPT_MIN_PART_NUM) {
622 unsigned int nsize = p->arr_size / 2;
623 nsize = nsize > GPT_MIN_PART_NUM ? nsize : GPT_MIN_PART_NUM;
624 gpt_entry_t * tmp = malloc(nsize * sizeof(gpt_entry_t));
625 if(tmp == NULL)
626 return ENOMEM;
627
628 memcpy(tmp, p->part_array, p->fill < nsize ? p->fill : nsize);
629 free(p->part_array);
630 p->part_array = tmp;
631 p->arr_size = nsize;
632 }
633
634 return 0;
635}
636
637//FIXME: replace this with a library call, if it exists
638static long long nearest_larger_int(double a)
639{
640 if ((long long) a == a) {
641 return (long long) a;
642 }
643
644 return ((long long) a) + 1;
645}
646
647
648
649
650
651
Note: See TracBrowser for help on using the repository browser.