[ec50ac4a] | 1 | /*
|
---|
| 2 | * Copyright (c) 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 hdisk
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
[271e24a] | 35 | #include <stdio.h>
|
---|
[1c8bfe8] | 36 | #include <str.h>
|
---|
[271e24a] | 37 | #include <errno.h>
|
---|
[30440ed] | 38 | #include <str_error.h>
|
---|
[271e24a] | 39 | #include <sys/types.h>
|
---|
[0435fe41] | 40 | #include <sys/typefmt.h>
|
---|
[271e24a] | 41 |
|
---|
[ec50ac4a] | 42 | #include "func_gpt.h"
|
---|
[30440ed] | 43 | #include "input.h"
|
---|
| 44 |
|
---|
[dc76f4a] | 45 | static int set_gpt_partition(tinput_t *, gpt_part_t *, unsigned int);
|
---|
[271e24a] | 46 |
|
---|
[6e8e4e19] | 47 | int construct_gpt_label(label_t *this)
|
---|
| 48 | {
|
---|
| 49 | this->layout = LYT_GPT;
|
---|
| 50 | this->alignment = 1;
|
---|
| 51 |
|
---|
[c3cbbb2] | 52 | this->add_part = add_gpt_part;
|
---|
| 53 | this->delete_part = delete_gpt_part;
|
---|
| 54 | this->destroy_label = destroy_gpt_label;
|
---|
| 55 | this->new_label = new_gpt_label;
|
---|
| 56 | this->print_parts = print_gpt_parts;
|
---|
| 57 | this->read_parts = read_gpt_parts;
|
---|
| 58 | this->write_parts = write_gpt_parts;
|
---|
| 59 | this->extra_funcs = extra_gpt_funcs;
|
---|
[6e8e4e19] | 60 |
|
---|
| 61 | return this->new_label(this);
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[1c8bfe8] | 64 | int add_gpt_part(label_t *this, tinput_t *in)
|
---|
[271e24a] | 65 | {
|
---|
[1c8bfe8] | 66 | gpt_part_t * p = gpt_get_partition(this->data.gpt);
|
---|
[30440ed] | 67 | if (p == NULL) {
|
---|
| 68 | return ENOMEM;
|
---|
| 69 | }
|
---|
[1c8bfe8] | 70 |
|
---|
[dc76f4a] | 71 | return set_gpt_partition(in, p, this->alignment);
|
---|
[271e24a] | 72 | }
|
---|
| 73 |
|
---|
[1c8bfe8] | 74 | int delete_gpt_part(label_t *this, tinput_t *in)
|
---|
[271e24a] | 75 | {
|
---|
[1c8bfe8] | 76 | int rc;
|
---|
[30440ed] | 77 | size_t idx;
|
---|
[1c8bfe8] | 78 |
|
---|
[30440ed] | 79 | printf("Number of the partition to delete (counted from 0): ");
|
---|
| 80 | idx = get_input_size_t(in);
|
---|
[1c8bfe8] | 81 |
|
---|
| 82 | rc = gpt_remove_partition(this->data.gpt, idx);
|
---|
[dc76f4a] | 83 | if (rc == ENOMEM) {
|
---|
[30440ed] | 84 | printf("Warning: running low on memory, not resizing...\n");
|
---|
[1c8bfe8] | 85 | return rc;
|
---|
[dc76f4a] | 86 | } else if (rc == EINVAL) {
|
---|
| 87 | printf("Invalid index.\n");
|
---|
| 88 | return rc;
|
---|
[30440ed] | 89 | }
|
---|
[1c8bfe8] | 90 |
|
---|
[30440ed] | 91 | return EOK;
|
---|
[271e24a] | 92 | }
|
---|
| 93 |
|
---|
[6e8e4e19] | 94 | int destroy_gpt_label(label_t *this)
|
---|
[700f89e] | 95 | {
|
---|
[1c8bfe8] | 96 | gpt_free_label(this->data.gpt);
|
---|
[9bda5d90] | 97 | return EOK;
|
---|
[700f89e] | 98 | }
|
---|
| 99 |
|
---|
[6e8e4e19] | 100 | int new_gpt_label(label_t *this)
|
---|
[a2aa81cb] | 101 | {
|
---|
[1c8bfe8] | 102 | this->data.gpt = gpt_alloc_label();
|
---|
[a2aa81cb] | 103 | return EOK;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[6e8e4e19] | 106 | int print_gpt_parts(label_t *this)
|
---|
[271e24a] | 107 | {
|
---|
[30440ed] | 108 | printf("Current partition scheme (GPT):\n");
|
---|
[dc76f4a] | 109 | printf("%15s %10s %10s Type: Name:\n", "Start:", "End:", "Length:");
|
---|
[30440ed] | 110 |
|
---|
[8f6c7785] | 111 | size_t i = 0;
|
---|
[271e24a] | 112 |
|
---|
[0435fe41] | 113 | gpt_part_foreach (this->data.gpt, iter) {
|
---|
[1c8bfe8] | 114 | i++;
|
---|
[c3cbbb2] | 115 |
|
---|
[1c8bfe8] | 116 | if (gpt_get_part_type(iter) == GPT_PTE_UNUSED)
|
---|
[6e8e4e19] | 117 | continue;
|
---|
| 118 |
|
---|
[1c8bfe8] | 119 | if (i % 20 == 0)
|
---|
| 120 | printf("%15s %10s %10s Type: Name:\n", "Start:", "End:", "Length:");
|
---|
| 121 |
|
---|
[dc76f4a] | 122 |
|
---|
[0435fe41] | 123 | printf("%3zu %10" PRIu64 " %10" PRIu64 " %10" PRIu64 " %3zu %s\n",
|
---|
| 124 | i-1, gpt_get_start_lba(iter), gpt_get_end_lba(iter),
|
---|
| 125 | gpt_get_end_lba(iter) - gpt_get_start_lba(iter),
|
---|
| 126 | gpt_get_part_type(iter), gpt_get_part_name(iter));
|
---|
[c3cbbb2] | 127 |
|
---|
[8f6c7785] | 128 | }
|
---|
[1c8bfe8] | 129 |
|
---|
[8f6c7785] | 130 | return EOK;
|
---|
[271e24a] | 131 | }
|
---|
| 132 |
|
---|
[6e8e4e19] | 133 | int read_gpt_parts(label_t *this, service_id_t dev_handle)
|
---|
[a2aa81cb] | 134 | {
|
---|
[1c8bfe8] | 135 | int rc;
|
---|
| 136 |
|
---|
| 137 | rc = gpt_read_header(this->data.gpt, dev_handle);
|
---|
| 138 | if (rc != EOK) {
|
---|
| 139 | printf("Error: Reading header failed: %d (%s)\n", rc, str_error(rc));
|
---|
| 140 | return rc;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | rc = gpt_read_partitions(this->data.gpt);
|
---|
| 144 | if (rc != EOK) {
|
---|
| 145 | printf("Error: Reading partitions failed: %d (%s)\n", rc, str_error(rc));
|
---|
| 146 | return rc;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
[a2aa81cb] | 149 | return EOK;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
[6e8e4e19] | 152 | int write_gpt_parts(label_t *this, service_id_t dev_handle)
|
---|
[271e24a] | 153 | {
|
---|
[30440ed] | 154 | int rc;
|
---|
[1c8bfe8] | 155 |
|
---|
| 156 | rc = gpt_write_partitions(this->data.gpt, dev_handle);
|
---|
[30440ed] | 157 | if (rc != EOK) {
|
---|
| 158 | printf("Error: Writing partitions failed: %d (%s)\n", rc, str_error(rc));
|
---|
| 159 | return rc;
|
---|
| 160 | }
|
---|
[1c8bfe8] | 161 |
|
---|
| 162 | rc = gpt_write_header(this->data.gpt, dev_handle);
|
---|
[30440ed] | 163 | if (rc != EOK) {
|
---|
[1c8bfe8] | 164 | printf("Error: Writing header failed: %d (%s)\n", rc, str_error(rc));
|
---|
[30440ed] | 165 | return rc;
|
---|
| 166 | }
|
---|
[1c8bfe8] | 167 |
|
---|
[30440ed] | 168 | return EOK;
|
---|
| 169 | }
|
---|
| 170 |
|
---|
[1c8bfe8] | 171 | int extra_gpt_funcs(label_t *this, tinput_t *in, service_id_t dev_handle)
|
---|
[30440ed] | 172 | {
|
---|
[9bda5d90] | 173 | printf("Not implemented.\n");
|
---|
[30440ed] | 174 | return EOK;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
[dc76f4a] | 177 | static int set_gpt_partition(tinput_t *in, gpt_part_t *p, unsigned int alignment)
|
---|
[30440ed] | 178 | {
|
---|
[1c8bfe8] | 179 | int rc;
|
---|
| 180 |
|
---|
[30440ed] | 181 | uint64_t sa, ea;
|
---|
[1c8bfe8] | 182 |
|
---|
[30440ed] | 183 | printf("Set starting address (number): ");
|
---|
| 184 | sa = get_input_uint64(in);
|
---|
[dc76f4a] | 185 | if (sa % alignment != 0)
|
---|
| 186 | sa = gpt_get_next_aligned(sa, alignment);
|
---|
[1c8bfe8] | 187 |
|
---|
[2b55edb] | 188 | printf("Set end address (number): ");
|
---|
[30440ed] | 189 | ea = get_input_uint64(in);
|
---|
[1c8bfe8] | 190 |
|
---|
[30440ed] | 191 | if (ea <= sa) {
|
---|
| 192 | printf("Invalid value.\n");
|
---|
| 193 | return EINVAL;
|
---|
| 194 | }
|
---|
[1c8bfe8] | 195 |
|
---|
[8f6c7785] | 196 | gpt_set_start_lba(p, sa);
|
---|
| 197 | gpt_set_end_lba(p, ea);
|
---|
[1c8bfe8] | 198 |
|
---|
[0435fe41] | 199 | /* See global.c from libgpt for all partition types. */
|
---|
| 200 | printf("Set type (1 for HelenOS System): ");
|
---|
| 201 | size_t idx = get_input_size_t(in);
|
---|
| 202 | gpt_set_part_type(p, idx);
|
---|
[c3cbbb2] | 203 |
|
---|
| 204 | gpt_set_random_uuid(p->part_type);
|
---|
| 205 | gpt_set_random_uuid(p->part_id);
|
---|
[1c8bfe8] | 206 |
|
---|
| 207 | char *name;
|
---|
[8559fa0] | 208 | printf("Name the partition: ");
|
---|
[1c8bfe8] | 209 | rc = get_input_line(in, &name);
|
---|
| 210 | if (rc != EOK) {
|
---|
| 211 | printf("Error reading name: %d (%s)\n", rc, str_error(rc));
|
---|
| 212 | return rc;
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | gpt_set_part_name(p, name, str_size(name));
|
---|
| 216 |
|
---|
[30440ed] | 217 | return EOK;
|
---|
[271e24a] | 218 | }
|
---|
| 219 |
|
---|