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