[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>
|
---|
| 40 |
|
---|
[ec50ac4a] | 41 | #include "func_gpt.h"
|
---|
[30440ed] | 42 | #include "input.h"
|
---|
| 43 |
|
---|
| 44 | static int set_gpt_partition(tinput_t *, gpt_part_t *);
|
---|
[271e24a] | 45 |
|
---|
[6e8e4e19] | 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->new_label = new_gpt_label;
|
---|
| 55 | this->print_parts = print_gpt_parts;
|
---|
| 56 | this->read_parts = read_gpt_parts;
|
---|
| 57 | this->write_parts = write_gpt_parts;
|
---|
| 58 | this->extra_funcs = extra_gpt_funcs;
|
---|
| 59 |
|
---|
| 60 | return this->new_label(this);
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[1c8bfe8] | 63 | int add_gpt_part(label_t *this, tinput_t *in)
|
---|
[271e24a] | 64 | {
|
---|
[1c8bfe8] | 65 | gpt_part_t * p = gpt_get_partition(this->data.gpt);
|
---|
[30440ed] | 66 | if (p == NULL) {
|
---|
| 67 | return ENOMEM;
|
---|
| 68 | }
|
---|
[1c8bfe8] | 69 |
|
---|
[30440ed] | 70 | return set_gpt_partition(in, p);
|
---|
[271e24a] | 71 | }
|
---|
| 72 |
|
---|
[1c8bfe8] | 73 | int delete_gpt_part(label_t *this, tinput_t *in)
|
---|
[271e24a] | 74 | {
|
---|
[1c8bfe8] | 75 | int rc;
|
---|
[30440ed] | 76 | size_t idx;
|
---|
[1c8bfe8] | 77 |
|
---|
[30440ed] | 78 | printf("Number of the partition to delete (counted from 0): ");
|
---|
| 79 | idx = get_input_size_t(in);
|
---|
[1c8bfe8] | 80 |
|
---|
| 81 | rc = gpt_remove_partition(this->data.gpt, idx);
|
---|
| 82 | if (rc != EOK) {
|
---|
[30440ed] | 83 | printf("Warning: running low on memory, not resizing...\n");
|
---|
[1c8bfe8] | 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 | {
|
---|
[8f6c7785] | 104 | //int rc;
|
---|
[30440ed] | 105 | printf("Current partition scheme (GPT):\n");
|
---|
| 106 | printf("\t\tStart:\tEnd:\tLength:\tType:\tName:\n");
|
---|
| 107 |
|
---|
[8f6c7785] | 108 | size_t i = 0;
|
---|
[271e24a] | 109 |
|
---|
[1c8bfe8] | 110 | gpt_part_foreach(this->data.gpt, iter) {
|
---|
| 111 | i++;
|
---|
[6e8e4e19] | 112 | //FIXMEE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
---|
[1c8bfe8] | 113 | if (gpt_get_part_type(iter) == GPT_PTE_UNUSED)
|
---|
[6e8e4e19] | 114 | continue;
|
---|
| 115 |
|
---|
[1c8bfe8] | 116 | if (i % 20 == 0)
|
---|
| 117 | printf("%15s %10s %10s Type: Name:\n", "Start:", "End:", "Length:");
|
---|
| 118 |
|
---|
[8f6c7785] | 119 | //printf("\t%10u %10u %10u %3d\n", iter->start_addr, iter->start_addr + iter->length,
|
---|
| 120 | // iter->length, gpt_get_part_type(iter), gpt_get_part_name(iter));
|
---|
[1c8bfe8] | 121 | printf("%3u %10llu %10llu %10llu %3d %s\n", i-1, gpt_get_start_lba(iter), gpt_get_end_lba(iter),
|
---|
[8f6c7785] | 122 | gpt_get_end_lba(iter) - gpt_get_start_lba(iter), gpt_get_part_type(iter),
|
---|
| 123 | gpt_get_part_name(iter));
|
---|
| 124 | }
|
---|
[1c8bfe8] | 125 |
|
---|
[8f6c7785] | 126 | //return rc;
|
---|
| 127 | return EOK;
|
---|
[271e24a] | 128 | }
|
---|
| 129 |
|
---|
[6e8e4e19] | 130 | int read_gpt_parts(label_t *this, service_id_t dev_handle)
|
---|
[a2aa81cb] | 131 | {
|
---|
[1c8bfe8] | 132 | int rc;
|
---|
| 133 |
|
---|
| 134 | rc = gpt_read_header(this->data.gpt, dev_handle);
|
---|
| 135 | if (rc != EOK) {
|
---|
| 136 | printf("Error: Reading header failed: %d (%s)\n", rc, str_error(rc));
|
---|
| 137 | return rc;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | rc = gpt_read_partitions(this->data.gpt);
|
---|
| 141 | if (rc != EOK) {
|
---|
| 142 | printf("Error: Reading partitions failed: %d (%s)\n", rc, str_error(rc));
|
---|
| 143 | return rc;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
[a2aa81cb] | 146 | return EOK;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
[6e8e4e19] | 149 | int write_gpt_parts(label_t *this, service_id_t dev_handle)
|
---|
[271e24a] | 150 | {
|
---|
[30440ed] | 151 | int rc;
|
---|
[1c8bfe8] | 152 |
|
---|
| 153 | rc = gpt_write_partitions(this->data.gpt, dev_handle);
|
---|
[30440ed] | 154 | if (rc != EOK) {
|
---|
| 155 | printf("Error: Writing partitions failed: %d (%s)\n", rc, str_error(rc));
|
---|
| 156 | return rc;
|
---|
| 157 | }
|
---|
[1c8bfe8] | 158 |
|
---|
| 159 | rc = gpt_write_header(this->data.gpt, dev_handle);
|
---|
[30440ed] | 160 | if (rc != EOK) {
|
---|
[1c8bfe8] | 161 | printf("Error: Writing header failed: %d (%s)\n", rc, str_error(rc));
|
---|
[30440ed] | 162 | return rc;
|
---|
| 163 | }
|
---|
[1c8bfe8] | 164 |
|
---|
[30440ed] | 165 | return EOK;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
[1c8bfe8] | 168 | int extra_gpt_funcs(label_t *this, tinput_t *in, service_id_t dev_handle)
|
---|
[30440ed] | 169 | {
|
---|
[9bda5d90] | 170 | printf("Not implemented.\n");
|
---|
[30440ed] | 171 | return EOK;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
[1c8bfe8] | 174 | static int set_gpt_partition(tinput_t *in, gpt_part_t *p)
|
---|
[30440ed] | 175 | {
|
---|
[1c8bfe8] | 176 | int rc;
|
---|
| 177 |
|
---|
[30440ed] | 178 | uint64_t sa, ea;
|
---|
[1c8bfe8] | 179 |
|
---|
[30440ed] | 180 | printf("Set starting address (number): ");
|
---|
| 181 | sa = get_input_uint64(in);
|
---|
[1c8bfe8] | 182 |
|
---|
[30440ed] | 183 | printf("Set end addres (number): ");
|
---|
| 184 | ea = get_input_uint64(in);
|
---|
[1c8bfe8] | 185 |
|
---|
[30440ed] | 186 | if (ea <= sa) {
|
---|
| 187 | printf("Invalid value.\n");
|
---|
| 188 | return EINVAL;
|
---|
| 189 | }
|
---|
[1c8bfe8] | 190 |
|
---|
[8f6c7785] | 191 | gpt_set_start_lba(p, sa);
|
---|
| 192 | gpt_set_end_lba(p, ea);
|
---|
[1c8bfe8] | 193 |
|
---|
| 194 |
|
---|
| 195 | char *name;
|
---|
| 196 | rc = get_input_line(in, &name);
|
---|
| 197 | if (rc != EOK) {
|
---|
| 198 | printf("Error reading name: %d (%s)\n", rc, str_error(rc));
|
---|
| 199 | return rc;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | gpt_set_part_name(p, name, str_size(name));
|
---|
| 203 |
|
---|
[30440ed] | 204 | return EOK;
|
---|
[271e24a] | 205 | }
|
---|
| 206 |
|
---|