[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);
|
---|
[ec50ac4a] | 70 |
|
---|
[271e24a] | 71 | rc = mbr_remove_partition(data->mbr.parts, idx);
|
---|
[ec50ac4a] | 72 | if(rc != EOK) {
|
---|
| 73 | printf("Error: something.\n");
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | return EOK;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | /** Print current partition scheme */
|
---|
[271e24a] | 80 | int print_mbr_parts(union table_data * data)
|
---|
[ec50ac4a] | 81 | {
|
---|
| 82 | int num = 0;
|
---|
| 83 |
|
---|
| 84 | printf("Current partition scheme:\n");
|
---|
| 85 | printf("\t\tBootable:\tStart:\tEnd:\tLength:\tType:\n");
|
---|
| 86 |
|
---|
[271e24a] | 87 | mbr_part_foreach(data->mbr.parts, it) {
|
---|
[ec50ac4a] | 88 | if (it->type == PT_UNUSED)
|
---|
| 89 | continue;
|
---|
| 90 |
|
---|
[271e24a] | 91 | printf("\t P%d:\t", num);
|
---|
| 92 | if (mbr_get_flag(it, ST_BOOT))
|
---|
[ec50ac4a] | 93 | printf("*");
|
---|
| 94 | else
|
---|
| 95 | printf(" ");
|
---|
| 96 |
|
---|
[30440ed] | 97 | printf("\t%10u %10u %10u %3d\n", it->start_addr, it->start_addr + it->length, it->length, it->type);
|
---|
[ec50ac4a] | 98 |
|
---|
| 99 | ++num;
|
---|
[271e24a] | 100 | }
|
---|
[ec50ac4a] | 101 |
|
---|
| 102 | printf("%d partitions found.\n", num);
|
---|
[271e24a] | 103 |
|
---|
| 104 | return EOK;
|
---|
[ec50ac4a] | 105 | }
|
---|
| 106 |
|
---|
[271e24a] | 107 | int write_mbr_parts(service_id_t dev_handle, union table_data * data)
|
---|
[ec50ac4a] | 108 | {
|
---|
[271e24a] | 109 | int rc = mbr_write_partitions(data->mbr.parts, data->mbr.mbr, dev_handle);
|
---|
[ec50ac4a] | 110 | if (rc != EOK) {
|
---|
[30440ed] | 111 | printf("Error occured during writing: ERR: %d: %s\n", rc, str_error(rc));
|
---|
[ec50ac4a] | 112 | }
|
---|
[271e24a] | 113 |
|
---|
| 114 | return rc;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
[30440ed] | 117 | int extra_mbr_funcs(tinput_t * in, service_id_t dev_handle, union table_data * data)
|
---|
| 118 | {
|
---|
| 119 | return EOK;
|
---|
| 120 | }
|
---|
[271e24a] | 121 |
|
---|
| 122 | static int set_mbr_partition(tinput_t * in, mbr_part_t * p)
|
---|
| 123 | {
|
---|
| 124 | int c;
|
---|
| 125 | uint8_t type;
|
---|
| 126 |
|
---|
| 127 | printf("Primary (p) or logical (l): ");
|
---|
| 128 | c = getchar();
|
---|
[30440ed] | 129 | printf("%c\n", c);
|
---|
[271e24a] | 130 |
|
---|
| 131 | switch(c) {
|
---|
| 132 | case 'p':
|
---|
| 133 | mbr_set_flag(p, ST_LOGIC, false);
|
---|
[30440ed] | 134 | break;
|
---|
[271e24a] | 135 | case 'l':
|
---|
| 136 | mbr_set_flag(p, ST_LOGIC, true);
|
---|
[30440ed] | 137 | break;
|
---|
[271e24a] | 138 | default:
|
---|
| 139 | printf("Invalid type. Cancelled.");
|
---|
| 140 | return EINVAL;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | printf("Set type (0-255): ");
|
---|
| 144 | type = get_input_uint8(in);
|
---|
| 145 |
|
---|
| 146 | ///TODO: there can only be one bootable partition; let's do it just like fdisk
|
---|
| 147 | printf("Bootable? (y/n): ");
|
---|
| 148 | c = getchar();
|
---|
| 149 | if (c != 'y' && c != 'Y' && c != 'n' && c != 'N') {
|
---|
| 150 | printf("Invalid value. Cancelled.");
|
---|
| 151 | return EINVAL;
|
---|
| 152 | }
|
---|
[30440ed] | 153 | printf("%c\n", c);
|
---|
[271e24a] | 154 | mbr_set_flag(p, ST_BOOT, (c == 'y' || c == 'Y') ? true : false);
|
---|
| 155 |
|
---|
| 156 |
|
---|
| 157 | uint32_t sa, ea;
|
---|
| 158 |
|
---|
| 159 | printf("Set starting address (number): ");
|
---|
| 160 | sa = get_input_uint32(in);
|
---|
| 161 |
|
---|
| 162 | printf("Set end addres (number): ");
|
---|
| 163 | ea = get_input_uint32(in);
|
---|
| 164 |
|
---|
| 165 | if(ea < sa) {
|
---|
| 166 | printf("Invalid value. Canceled.\n");
|
---|
| 167 | return EINVAL;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | p->type = type;
|
---|
| 171 | p->start_addr = sa;
|
---|
| 172 | p->length = ea - sa;
|
---|
| 173 |
|
---|
| 174 | return EOK;
|
---|
[ec50ac4a] | 175 | }
|
---|
[271e24a] | 176 |
|
---|
| 177 |
|
---|
| 178 |
|
---|
| 179 |
|
---|
| 180 |
|
---|
| 181 |
|
---|