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 | int add_mbr_part(tinput_t *in, union label_data *data)
|
---|
46 | {
|
---|
47 | int rc;
|
---|
48 |
|
---|
49 | mbr_part_t *part = mbr_alloc_partition();
|
---|
50 |
|
---|
51 | set_mbr_partition(in, part);
|
---|
52 |
|
---|
53 | rc = mbr_add_partition(data->mbr, part);
|
---|
54 | if (rc != EOK) {
|
---|
55 | printf("Error adding partition.\n");
|
---|
56 | }
|
---|
57 |
|
---|
58 | return EOK;
|
---|
59 | }
|
---|
60 |
|
---|
61 | int delete_mbr_part(tinput_t *in, union label_data *data)
|
---|
62 | {
|
---|
63 | int rc;
|
---|
64 | size_t idx;
|
---|
65 |
|
---|
66 | printf("Number of the partition to delete (counted from 0): ");
|
---|
67 | idx = get_input_size_t(in);
|
---|
68 |
|
---|
69 | if (idx == 0 && errno != EOK)
|
---|
70 | return errno;
|
---|
71 |
|
---|
72 | rc = mbr_remove_partition(data->mbr, idx);
|
---|
73 | if(rc != EOK) {
|
---|
74 | printf("Error: something.\n");
|
---|
75 | }
|
---|
76 |
|
---|
77 | return EOK;
|
---|
78 | }
|
---|
79 |
|
---|
80 | int destroy_mbr_label(union label_data *data)
|
---|
81 | {
|
---|
82 | mbr_free_label(data->mbr);
|
---|
83 | return EOK;
|
---|
84 | }
|
---|
85 |
|
---|
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 |
|
---|
95 | /** Print current partition scheme */
|
---|
96 | int print_mbr_parts(union label_data *data)
|
---|
97 | {
|
---|
98 | int num = 0;
|
---|
99 |
|
---|
100 | printf("Current partition scheme:\n");
|
---|
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 |
|
---|
104 | mbr_part_t *it;
|
---|
105 | mbr_part_foreach(data->mbr->parts, it) {
|
---|
106 | if (it->type == PT_UNUSED)
|
---|
107 | continue;
|
---|
108 |
|
---|
109 | printf("\tP%d:\t", num);
|
---|
110 | if (mbr_get_flag(it, ST_BOOT))
|
---|
111 | printf("*");
|
---|
112 | else
|
---|
113 | printf(" ");
|
---|
114 |
|
---|
115 | printf("\t%10u %10u %10u %7u\n", it->start_addr, it->start_addr + it->length, it->length, it->type);
|
---|
116 |
|
---|
117 | ++num;
|
---|
118 | }
|
---|
119 |
|
---|
120 | printf("%d partitions found.\n", num);
|
---|
121 |
|
---|
122 | return EOK;
|
---|
123 | }
|
---|
124 |
|
---|
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)
|
---|
144 | {
|
---|
145 | int rc = mbr_write_partitions(data->mbr, dev_handle);
|
---|
146 | if (rc != EOK) {
|
---|
147 | printf("Error occured during writing: ERR: %d: %s\n", rc, str_error(rc));
|
---|
148 | }
|
---|
149 |
|
---|
150 | return rc;
|
---|
151 | }
|
---|
152 |
|
---|
153 | int extra_mbr_funcs(tinput_t *in, service_id_t dev_handle, union label_data *data)
|
---|
154 | {
|
---|
155 | printf("Not implemented.\n");
|
---|
156 | return EOK;
|
---|
157 | }
|
---|
158 |
|
---|
159 | static int set_mbr_partition(tinput_t *in, mbr_part_t *p)
|
---|
160 | {
|
---|
161 | int c;
|
---|
162 | uint8_t type;
|
---|
163 |
|
---|
164 | printf("Primary (p) or logical (l): ");
|
---|
165 | c = getchar();
|
---|
166 | printf("%c\n", c);
|
---|
167 |
|
---|
168 | switch(c) {
|
---|
169 | case 'p':
|
---|
170 | mbr_set_flag(p, ST_LOGIC, false);
|
---|
171 | break;
|
---|
172 | case 'l':
|
---|
173 | mbr_set_flag(p, ST_LOGIC, true);
|
---|
174 | break;
|
---|
175 | default:
|
---|
176 | printf("Invalid type. Cancelled.");
|
---|
177 | return EINVAL;
|
---|
178 | }
|
---|
179 |
|
---|
180 | printf("Set type (0-255): ");
|
---|
181 | type = get_input_uint8(in);
|
---|
182 | if (type == 0 && errno != EOK)
|
---|
183 | return errno;
|
---|
184 |
|
---|
185 | ///TODO: there can only be one boolabel partition; let's do it just like fdisk
|
---|
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 | }
|
---|
192 | printf("%c\n", c);
|
---|
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);
|
---|
200 | if (sa == 0 && errno != EOK)
|
---|
201 | return errno;
|
---|
202 |
|
---|
203 | printf("Set end addres (number): ");
|
---|
204 | ea = get_input_uint32(in);
|
---|
205 | if (ea == 0 && errno != EOK)
|
---|
206 | return errno;
|
---|
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;
|
---|
218 | }
|
---|
219 |
|
---|
220 |
|
---|
221 |
|
---|
222 |
|
---|
223 |
|
---|
224 |
|
---|