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