| 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 <errno.h>
|
|---|
| 37 | #include <str_error.h>
|
|---|
| 38 | #include <sys/types.h>
|
|---|
| 39 |
|
|---|
| 40 | #include "func_gpt.h"
|
|---|
| 41 | #include "input.h"
|
|---|
| 42 |
|
|---|
| 43 | static int set_gpt_partition(tinput_t *, gpt_part_t *);
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 | int construct_gpt_label(label_t *this)
|
|---|
| 47 | {
|
|---|
| 48 | this->layout = LYT_GPT;
|
|---|
| 49 | this->alignment = 1;
|
|---|
| 50 |
|
|---|
| 51 | this->add_part = add_gpt_part;
|
|---|
| 52 | this->delete_part = delete_gpt_part;
|
|---|
| 53 | this->new_label = new_gpt_label;
|
|---|
| 54 | this->print_parts = print_gpt_parts;
|
|---|
| 55 | this->read_parts = read_gpt_parts;
|
|---|
| 56 | this->write_parts = write_gpt_parts;
|
|---|
| 57 | this->extra_funcs = extra_gpt_funcs;
|
|---|
| 58 |
|
|---|
| 59 | return this->new_label(this);
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | int add_gpt_part(label_t *this, tinput_t * in)
|
|---|
| 63 | {
|
|---|
| 64 | gpt_part_t * p = gpt_alloc_partition(this->data.gpt->parts);
|
|---|
| 65 | if (p == NULL) {
|
|---|
| 66 | return ENOMEM;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | return set_gpt_partition(in, p);
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | int delete_gpt_part(label_t *this, tinput_t * in)
|
|---|
| 73 | {
|
|---|
| 74 | size_t idx;
|
|---|
| 75 |
|
|---|
| 76 | printf("Number of the partition to delete (counted from 0): ");
|
|---|
| 77 | idx = get_input_size_t(in);
|
|---|
| 78 |
|
|---|
| 79 | if (gpt_remove_partition(this->data.gpt->parts, idx) == -1) {
|
|---|
| 80 | printf("Warning: running low on memory, not resizing...\n");
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | return EOK;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | int destroy_gpt_label(label_t *this)
|
|---|
| 87 | {
|
|---|
| 88 | return EOK;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | int new_gpt_label(label_t *this)
|
|---|
| 92 | {
|
|---|
| 93 | this->data.gpt->gpt = gpt_alloc_gpt_header();
|
|---|
| 94 | this->data.gpt->parts = gpt_alloc_partitions();
|
|---|
| 95 | return EOK;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | int print_gpt_parts(label_t *this)
|
|---|
| 99 | {
|
|---|
| 100 | //int rc;
|
|---|
| 101 | printf("Current partition scheme (GPT):\n");
|
|---|
| 102 | printf("\t\tStart:\tEnd:\tLength:\tType:\tName:\n");
|
|---|
| 103 |
|
|---|
| 104 | size_t i = 0;
|
|---|
| 105 |
|
|---|
| 106 | gpt_part_foreach(this->data.gpt->parts, iter) {
|
|---|
| 107 | //FIXMEE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|---|
| 108 | if (gpt_get_part_type(iter) == 62)
|
|---|
| 109 | continue;
|
|---|
| 110 |
|
|---|
| 111 | //printf("\t%10u %10u %10u %3d\n", iter->start_addr, iter->start_addr + iter->length,
|
|---|
| 112 | // iter->length, gpt_get_part_type(iter), gpt_get_part_name(iter));
|
|---|
| 113 | printf("%3u\t%10llu %10llu %10llu %3d %s\n", i, gpt_get_start_lba(iter), gpt_get_end_lba(iter),
|
|---|
| 114 | gpt_get_end_lba(iter) - gpt_get_start_lba(iter), gpt_get_part_type(iter),
|
|---|
| 115 | gpt_get_part_name(iter));
|
|---|
| 116 | i++;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | //return rc;
|
|---|
| 120 | return EOK;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | int read_gpt_parts(label_t *this, service_id_t dev_handle)
|
|---|
| 124 | {
|
|---|
| 125 | return EOK;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | int write_gpt_parts(label_t *this, service_id_t dev_handle)
|
|---|
| 129 | {
|
|---|
| 130 | int rc;
|
|---|
| 131 |
|
|---|
| 132 | rc = gpt_write_partitions(this->data.gpt->parts, this->data.gpt->gpt, dev_handle);
|
|---|
| 133 | if (rc != EOK) {
|
|---|
| 134 | printf("Error: Writing partitions failed: %d (%s)\n", rc, str_error(rc));
|
|---|
| 135 | return rc;
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | rc = gpt_write_gpt_header(this->data.gpt->gpt, dev_handle);
|
|---|
| 139 | if (rc != EOK) {
|
|---|
| 140 | printf("Error: Writing partitions failed: %d (%s)\n", rc, str_error(rc));
|
|---|
| 141 | return rc;
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | return EOK;
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | int extra_gpt_funcs(label_t *this, tinput_t * in, service_id_t dev_handle)
|
|---|
| 148 | {
|
|---|
| 149 | printf("Not implemented.\n");
|
|---|
| 150 | return EOK;
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | static int set_gpt_partition(tinput_t * in, gpt_part_t * p)
|
|---|
| 154 | {
|
|---|
| 155 | //int rc;
|
|---|
| 156 |
|
|---|
| 157 | uint64_t sa, ea;
|
|---|
| 158 |
|
|---|
| 159 | printf("Set starting address (number): ");
|
|---|
| 160 | sa = get_input_uint64(in);
|
|---|
| 161 |
|
|---|
| 162 | printf("Set end addres (number): ");
|
|---|
| 163 | ea = get_input_uint64(in);
|
|---|
| 164 |
|
|---|
| 165 | if (ea <= sa) {
|
|---|
| 166 | printf("Invalid value.\n");
|
|---|
| 167 | return EINVAL;
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 | //p->start_addr = sa;
|
|---|
| 172 | gpt_set_start_lba(p, sa);
|
|---|
| 173 | //p->length = ea - sa;
|
|---|
| 174 | gpt_set_end_lba(p, ea);
|
|---|
| 175 |
|
|---|
| 176 | return EOK;
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|