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