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 |
|
---|
35 | #include <stdio.h>
|
---|
36 | #include <errno.h>
|
---|
37 | #include <str_error.h>
|
---|
38 | #include <sys/types.h>
|
---|
39 |
|
---|
40 | #include "func_mbr.h"
|
---|
41 | #include "input.h"
|
---|
42 |
|
---|
43 | static int set_mbr_partition(tinput_t * in, mbr_part_t * p);
|
---|
44 |
|
---|
45 |
|
---|
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();
|
---|
51 |
|
---|
52 | set_mbr_partition(in, part);
|
---|
53 |
|
---|
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;
|
---|
61 | }
|
---|
62 |
|
---|
63 | int delete_mbr_part(tinput_t * in, union table_data * data)
|
---|
64 | {
|
---|
65 | int rc;
|
---|
66 | size_t idx;
|
---|
67 |
|
---|
68 | printf("Number of the partition to delete (counted from 0): ");
|
---|
69 | idx = get_input_size_t(in);
|
---|
70 |
|
---|
71 | rc = mbr_remove_partition(data->mbr.parts, idx);
|
---|
72 | if(rc != EOK) {
|
---|
73 | printf("Error: something.\n");
|
---|
74 | }
|
---|
75 |
|
---|
76 | return EOK;
|
---|
77 | }
|
---|
78 |
|
---|
79 | /** Print current partition scheme */
|
---|
80 | int print_mbr_parts(union table_data * data)
|
---|
81 | {
|
---|
82 | int num = 0;
|
---|
83 |
|
---|
84 | printf("Current partition scheme:\n");
|
---|
85 | printf("\t\tBootable:\tStart:\tEnd:\tLength:\tType:\n");
|
---|
86 |
|
---|
87 | mbr_part_foreach(data->mbr.parts, it) {
|
---|
88 | if (it->type == PT_UNUSED)
|
---|
89 | continue;
|
---|
90 |
|
---|
91 | printf("\t P%d:\t", num);
|
---|
92 | if (mbr_get_flag(it, ST_BOOT))
|
---|
93 | printf("*");
|
---|
94 | else
|
---|
95 | printf(" ");
|
---|
96 |
|
---|
97 | printf("\t%10u %10u %10u %3d\n", it->start_addr, it->start_addr + it->length, it->length, it->type);
|
---|
98 |
|
---|
99 | ++num;
|
---|
100 | }
|
---|
101 |
|
---|
102 | printf("%d partitions found.\n", num);
|
---|
103 |
|
---|
104 | return EOK;
|
---|
105 | }
|
---|
106 |
|
---|
107 | int write_mbr_parts(service_id_t dev_handle, union table_data * data)
|
---|
108 | {
|
---|
109 | int rc = mbr_write_partitions(data->mbr.parts, data->mbr.mbr, dev_handle);
|
---|
110 | if (rc != EOK) {
|
---|
111 | printf("Error occured during writing: ERR: %d: %s\n", rc, str_error(rc));
|
---|
112 | }
|
---|
113 |
|
---|
114 | return rc;
|
---|
115 | }
|
---|
116 |
|
---|
117 | int extra_mbr_funcs(tinput_t * in, service_id_t dev_handle, union table_data * data)
|
---|
118 | {
|
---|
119 | return EOK;
|
---|
120 | }
|
---|
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();
|
---|
129 | printf("%c\n", c);
|
---|
130 |
|
---|
131 | switch(c) {
|
---|
132 | case 'p':
|
---|
133 | mbr_set_flag(p, ST_LOGIC, false);
|
---|
134 | break;
|
---|
135 | case 'l':
|
---|
136 | mbr_set_flag(p, ST_LOGIC, true);
|
---|
137 | break;
|
---|
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 | }
|
---|
153 | printf("%c\n", c);
|
---|
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;
|
---|
175 | }
|
---|
176 |
|
---|
177 |
|
---|
178 |
|
---|
179 |
|
---|
180 |
|
---|
181 |
|
---|