| 1 | /*
|
|---|
| 2 | * Copyright (c) 2015 Jiri Svoboda
|
|---|
| 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 liblabel
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /**
|
|---|
| 33 | * @file Disk label library.
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <adt/list.h>
|
|---|
| 37 | #include <errno.h>
|
|---|
| 38 | #include <label/label.h>
|
|---|
| 39 | #include <mem.h>
|
|---|
| 40 | #include <stdlib.h>
|
|---|
| 41 |
|
|---|
| 42 | #include "dummy.h"
|
|---|
| 43 | #include "gpt.h"
|
|---|
| 44 | #include "mbr.h"
|
|---|
| 45 |
|
|---|
| 46 | static label_ops_t *probe_list[] = {
|
|---|
| 47 | &gpt_label_ops,
|
|---|
| 48 | &mbr_label_ops,
|
|---|
| 49 | &dummy_label_ops,
|
|---|
| 50 | NULL
|
|---|
| 51 | };
|
|---|
| 52 |
|
|---|
| 53 | errno_t label_open(label_bd_t *bd, label_t **rlabel)
|
|---|
| 54 | {
|
|---|
| 55 | label_ops_t **ops;
|
|---|
| 56 | errno_t rc;
|
|---|
| 57 |
|
|---|
| 58 | ops = &probe_list[0];
|
|---|
| 59 | while (ops[0] != NULL) {
|
|---|
| 60 | rc = ops[0]->open(bd, rlabel);
|
|---|
| 61 | if (rc == EOK)
|
|---|
| 62 | return EOK;
|
|---|
| 63 | ++ops;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | return ENOTSUP;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | errno_t label_create(label_bd_t *bd, label_type_t ltype, label_t **rlabel)
|
|---|
| 70 | {
|
|---|
| 71 | label_ops_t *ops = NULL;
|
|---|
| 72 |
|
|---|
| 73 | switch (ltype) {
|
|---|
| 74 | case lt_none:
|
|---|
| 75 | return EINVAL;
|
|---|
| 76 | case lt_gpt:
|
|---|
| 77 | ops = &gpt_label_ops;
|
|---|
| 78 | break;
|
|---|
| 79 | case lt_mbr:
|
|---|
| 80 | ops = &mbr_label_ops;
|
|---|
| 81 | break;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | if (ops == NULL)
|
|---|
| 85 | return ENOTSUP;
|
|---|
| 86 |
|
|---|
| 87 | return ops->create(bd, rlabel);
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | void label_close(label_t *label)
|
|---|
| 91 | {
|
|---|
| 92 | if (label == NULL)
|
|---|
| 93 | return;
|
|---|
| 94 |
|
|---|
| 95 | label->ops->close(label);
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | errno_t label_destroy(label_t *label)
|
|---|
| 99 | {
|
|---|
| 100 | return label->ops->destroy(label);
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | errno_t label_get_info(label_t *label, label_info_t *linfo)
|
|---|
| 104 | {
|
|---|
| 105 | return label->ops->get_info(label, linfo);
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | label_part_t *label_part_first(label_t *label)
|
|---|
| 109 | {
|
|---|
| 110 | return label->ops->part_first(label);
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | label_part_t *label_part_next(label_part_t *part)
|
|---|
| 114 | {
|
|---|
| 115 | return part->label->ops->part_next(part);
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | void label_part_get_info(label_part_t *part, label_part_info_t *pinfo)
|
|---|
| 119 | {
|
|---|
| 120 | return part->label->ops->part_get_info(part, pinfo);
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | errno_t label_part_create(label_t *label, label_part_spec_t *pspec,
|
|---|
| 124 | label_part_t **rpart)
|
|---|
| 125 | {
|
|---|
| 126 | return label->ops->part_create(label, pspec, rpart);
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | errno_t label_part_destroy(label_part_t *part)
|
|---|
| 130 | {
|
|---|
| 131 | return part->label->ops->part_destroy(part);
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | void label_pspec_init(label_part_spec_t *pspec)
|
|---|
| 135 | {
|
|---|
| 136 | memset(pspec, 0, sizeof(label_part_spec_t));
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | errno_t label_suggest_ptype(label_t *label, label_pcnt_t pcnt,
|
|---|
| 140 | label_ptype_t *ptype)
|
|---|
| 141 | {
|
|---|
| 142 | return label->ops->suggest_ptype(label, pcnt, ptype);
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | /** @}
|
|---|
| 146 | */
|
|---|