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