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