[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>
|
---|
| 36 | #include <errno.h>
|
---|
[30440ed] | 37 | #include <str_error.h>
|
---|
[271e24a] | 38 | #include <sys/types.h>
|
---|
| 39 |
|
---|
[ec50ac4a] | 40 | #include "func_mbr.h"
|
---|
[271e24a] | 41 | #include "input.h"
|
---|
[ec50ac4a] | 42 |
|
---|
[6e8e4e19] | 43 | static int set_mbr_partition(tinput_t *in, mbr_part_t *p, unsigned int alignment);
|
---|
[ec50ac4a] | 44 |
|
---|
[6e8e4e19] | 45 | int construct_mbr_label(label_t *this)
|
---|
| 46 | {
|
---|
| 47 | this->layout = LYT_MBR;
|
---|
| 48 | this->alignment = 1;
|
---|
| 49 |
|
---|
| 50 | this->add_part = add_mbr_part;
|
---|
| 51 | this->delete_part = delete_mbr_part;
|
---|
| 52 | this->destroy_label = destroy_mbr_label;
|
---|
| 53 | this->new_label = new_mbr_label;
|
---|
| 54 | this->print_parts = print_mbr_parts;
|
---|
| 55 | this->read_parts = read_mbr_parts;
|
---|
| 56 | this->write_parts = write_mbr_parts;
|
---|
| 57 | this->extra_funcs = extra_mbr_funcs;
|
---|
| 58 |
|
---|
| 59 | return this->new_label(this);
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | int add_mbr_part(label_t *this, tinput_t *in)
|
---|
[271e24a] | 63 | {
|
---|
| 64 | int rc;
|
---|
| 65 |
|
---|
[a2aa81cb] | 66 | mbr_part_t *part = mbr_alloc_partition();
|
---|
[6e8e4e19] | 67 |
|
---|
| 68 | set_mbr_partition(in, part, this->alignment);
|
---|
| 69 |
|
---|
| 70 | rc = mbr_add_partition(this->data.mbr, part);
|
---|
| 71 | if (rc != ERR_OK) {
|
---|
| 72 | printf("Error adding partition: %d\n", rc);
|
---|
[271e24a] | 73 | }
|
---|
| 74 |
|
---|
[a2aa81cb] | 75 | return EOK;
|
---|
[ec50ac4a] | 76 | }
|
---|
| 77 |
|
---|
[6e8e4e19] | 78 | int delete_mbr_part(label_t *this, tinput_t *in)
|
---|
[ec50ac4a] | 79 | {
|
---|
| 80 | int rc;
|
---|
[271e24a] | 81 | size_t idx;
|
---|
[6e8e4e19] | 82 |
|
---|
[ec50ac4a] | 83 | printf("Number of the partition to delete (counted from 0): ");
|
---|
[271e24a] | 84 | idx = get_input_size_t(in);
|
---|
[9bda5d90] | 85 |
|
---|
| 86 | if (idx == 0 && errno != EOK)
|
---|
| 87 | return errno;
|
---|
| 88 |
|
---|
[6e8e4e19] | 89 | rc = mbr_remove_partition(this->data.mbr, idx);
|
---|
[ec50ac4a] | 90 | if(rc != EOK) {
|
---|
| 91 | printf("Error: something.\n");
|
---|
| 92 | }
|
---|
[6e8e4e19] | 93 |
|
---|
[ec50ac4a] | 94 | return EOK;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[6e8e4e19] | 97 | int destroy_mbr_label(label_t *this)
|
---|
[700f89e] | 98 | {
|
---|
[6e8e4e19] | 99 | mbr_free_label(this->data.mbr);
|
---|
[9bda5d90] | 100 | return EOK;
|
---|
[700f89e] | 101 | }
|
---|
| 102 |
|
---|
[6e8e4e19] | 103 | int new_mbr_label(label_t *this)
|
---|
[a2aa81cb] | 104 | {
|
---|
[6e8e4e19] | 105 | this->data.mbr = mbr_alloc_label();
|
---|
| 106 | if (this->data.mbr == NULL)
|
---|
[a2aa81cb] | 107 | return ENOMEM;
|
---|
| 108 | else
|
---|
| 109 | return EOK;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[ec50ac4a] | 112 | /** Print current partition scheme */
|
---|
[6e8e4e19] | 113 | int print_mbr_parts(label_t *this)
|
---|
[ec50ac4a] | 114 | {
|
---|
| 115 | int num = 0;
|
---|
[6e8e4e19] | 116 |
|
---|
| 117 | printf("Current partition scheme (MBR):\n");
|
---|
[8f6c7785] | 118 | //printf("\t\tBootable:\tStart:\tEnd:\tLength:\tType:\n");
|
---|
| 119 | printf("\t\t%10s %10s %10s %10s %7s\n", "Bootable:", "Start:", "End:", "Length:", "Type:");
|
---|
| 120 |
|
---|
[a2aa81cb] | 121 | mbr_part_t *it;
|
---|
[6e8e4e19] | 122 | //mbr_part_foreach(data->mbr, it) {
|
---|
| 123 |
|
---|
| 124 | for (it = mbr_get_first_partition(this->data.mbr); it != NULL;
|
---|
| 125 | it = mbr_get_next_partition(this->data.mbr, it), ++num) {
|
---|
[ec50ac4a] | 126 | if (it->type == PT_UNUSED)
|
---|
| 127 | continue;
|
---|
[6e8e4e19] | 128 |
|
---|
[8f6c7785] | 129 | printf("\tP%d:\t", num);
|
---|
[271e24a] | 130 | if (mbr_get_flag(it, ST_BOOT))
|
---|
[ec50ac4a] | 131 | printf("*");
|
---|
| 132 | else
|
---|
| 133 | printf(" ");
|
---|
[6e8e4e19] | 134 |
|
---|
[9bda5d90] | 135 | printf("\t%10u %10u %10u %7u\n", it->start_addr, it->start_addr + it->length, it->length, it->type);
|
---|
[6e8e4e19] | 136 |
|
---|
| 137 | //++num;
|
---|
[271e24a] | 138 | }
|
---|
[6e8e4e19] | 139 |
|
---|
[ec50ac4a] | 140 | printf("%d partitions found.\n", num);
|
---|
[271e24a] | 141 |
|
---|
| 142 | return EOK;
|
---|
[ec50ac4a] | 143 | }
|
---|
| 144 |
|
---|
[6e8e4e19] | 145 | int read_mbr_parts(label_t *this, service_id_t dev_handle)
|
---|
[a2aa81cb] | 146 | {
|
---|
| 147 | int rc;
|
---|
[6e8e4e19] | 148 | rc = mbr_read_mbr(this->data.mbr, dev_handle);
|
---|
[a2aa81cb] | 149 | if (rc != EOK)
|
---|
| 150 | return rc;
|
---|
[6e8e4e19] | 151 |
|
---|
| 152 | if (!mbr_is_mbr(this->data.mbr))
|
---|
[a2aa81cb] | 153 | return EINVAL;
|
---|
[6e8e4e19] | 154 |
|
---|
| 155 | rc = mbr_read_partitions(this->data.mbr);
|
---|
[a2aa81cb] | 156 | if (rc != EOK)
|
---|
| 157 | return rc;
|
---|
[6e8e4e19] | 158 |
|
---|
[a2aa81cb] | 159 | return EOK;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
[6e8e4e19] | 162 | int write_mbr_parts(label_t *this, service_id_t dev_handle)
|
---|
[ec50ac4a] | 163 | {
|
---|
[6e8e4e19] | 164 | int rc = mbr_write_partitions(this->data.mbr, dev_handle);
|
---|
[ec50ac4a] | 165 | if (rc != EOK) {
|
---|
[30440ed] | 166 | printf("Error occured during writing: ERR: %d: %s\n", rc, str_error(rc));
|
---|
[ec50ac4a] | 167 | }
|
---|
[271e24a] | 168 |
|
---|
| 169 | return rc;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
[6e8e4e19] | 172 | int extra_mbr_funcs(label_t *this, tinput_t *in, service_id_t dev_handle)
|
---|
[30440ed] | 173 | {
|
---|
[9bda5d90] | 174 | printf("Not implemented.\n");
|
---|
[30440ed] | 175 | return EOK;
|
---|
| 176 | }
|
---|
[271e24a] | 177 |
|
---|
[6e8e4e19] | 178 | static int set_mbr_partition(tinput_t *in, mbr_part_t *p, unsigned int alignment)
|
---|
[271e24a] | 179 | {
|
---|
| 180 | int c;
|
---|
| 181 | uint8_t type;
|
---|
| 182 |
|
---|
| 183 | printf("Primary (p) or logical (l): ");
|
---|
| 184 | c = getchar();
|
---|
[30440ed] | 185 | printf("%c\n", c);
|
---|
[271e24a] | 186 |
|
---|
| 187 | switch(c) {
|
---|
| 188 | case 'p':
|
---|
| 189 | mbr_set_flag(p, ST_LOGIC, false);
|
---|
[30440ed] | 190 | break;
|
---|
[271e24a] | 191 | case 'l':
|
---|
| 192 | mbr_set_flag(p, ST_LOGIC, true);
|
---|
[30440ed] | 193 | break;
|
---|
[271e24a] | 194 | default:
|
---|
[6e8e4e19] | 195 | printf("Invalid type. Cancelled.\n");
|
---|
[271e24a] | 196 | return EINVAL;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
[6e8e4e19] | 199 | printf("ST_LOGIC: %d, %hd\n", mbr_get_flag(p, ST_LOGIC), p->status);
|
---|
| 200 |
|
---|
[271e24a] | 201 | printf("Set type (0-255): ");
|
---|
| 202 | type = get_input_uint8(in);
|
---|
[9bda5d90] | 203 | if (type == 0 && errno != EOK)
|
---|
| 204 | return errno;
|
---|
[271e24a] | 205 |
|
---|
[a2aa81cb] | 206 | ///TODO: there can only be one boolabel partition; let's do it just like fdisk
|
---|
[271e24a] | 207 | printf("Bootable? (y/n): ");
|
---|
| 208 | c = getchar();
|
---|
| 209 | if (c != 'y' && c != 'Y' && c != 'n' && c != 'N') {
|
---|
| 210 | printf("Invalid value. Cancelled.");
|
---|
| 211 | return EINVAL;
|
---|
| 212 | }
|
---|
[30440ed] | 213 | printf("%c\n", c);
|
---|
[271e24a] | 214 | mbr_set_flag(p, ST_BOOT, (c == 'y' || c == 'Y') ? true : false);
|
---|
| 215 |
|
---|
| 216 |
|
---|
| 217 | uint32_t sa, ea;
|
---|
| 218 |
|
---|
| 219 | printf("Set starting address (number): ");
|
---|
| 220 | sa = get_input_uint32(in);
|
---|
[9bda5d90] | 221 | if (sa == 0 && errno != EOK)
|
---|
| 222 | return errno;
|
---|
[6e8e4e19] | 223 |
|
---|
| 224 | if (alignment != 0 && alignment != 1) {
|
---|
| 225 | sa = mbr_get_next_aligned(sa, alignment);
|
---|
| 226 | printf("Starting address was aligned to %u.\n", sa);
|
---|
| 227 | }
|
---|
[271e24a] | 228 |
|
---|
| 229 | printf("Set end addres (number): ");
|
---|
| 230 | ea = get_input_uint32(in);
|
---|
[9bda5d90] | 231 | if (ea == 0 && errno != EOK)
|
---|
| 232 | return errno;
|
---|
[271e24a] | 233 |
|
---|
[6e8e4e19] | 234 | /* Align ending address, not in use */
|
---|
| 235 | /*if (alignment != 0 && alignment != 1) {
|
---|
| 236 | ea = mbr_get_next_aligned(ea, alignment) - alignment;
|
---|
| 237 | printf("Starting address was aligned to %u.\n", ea);
|
---|
| 238 | }*/
|
---|
| 239 |
|
---|
[271e24a] | 240 | if(ea < sa) {
|
---|
| 241 | printf("Invalid value. Canceled.\n");
|
---|
| 242 | return EINVAL;
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | p->type = type;
|
---|
| 246 | p->start_addr = sa;
|
---|
| 247 | p->length = ea - sa;
|
---|
| 248 |
|
---|
| 249 | return EOK;
|
---|
[ec50ac4a] | 250 | }
|
---|
[271e24a] | 251 |
|
---|
| 252 |
|
---|
| 253 |
|
---|
| 254 |
|
---|
| 255 |
|
---|
| 256 |
|
---|