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 <ipc/bd.h>
|
---|
36 | #include <loc.h>
|
---|
37 | #include <async.h>
|
---|
38 | #include <stdio.h>
|
---|
39 | #include <ipc/services.h>
|
---|
40 | #include <block.h>
|
---|
41 | #include <errno.h>
|
---|
42 | #include <stdlib.h>
|
---|
43 | #include <assert.h>
|
---|
44 | #include <str.h>
|
---|
45 | #include <libmbr.h>
|
---|
46 | #include <libgpt.h>
|
---|
47 | #include <tinput.h>
|
---|
48 |
|
---|
49 | #include "hdisk.h"
|
---|
50 | #include "input.h"
|
---|
51 | #include "func_gpt.h"
|
---|
52 | #include "func_mbr.h"
|
---|
53 | #include "func_none.h"
|
---|
54 |
|
---|
55 | int interact(service_id_t);
|
---|
56 | void print_help(void);
|
---|
57 | void select_label_format(tinput_t *);
|
---|
58 | void construct_label(layouts_t);
|
---|
59 | void free_label(void);
|
---|
60 | int try_read(service_id_t);
|
---|
61 | int try_read_mbr(service_id_t);
|
---|
62 | int try_read_gpt(service_id_t);
|
---|
63 | void set_alignment(tinput_t *);
|
---|
64 |
|
---|
65 |
|
---|
66 | static label_t label;
|
---|
67 |
|
---|
68 | int main(int argc, char ** argv)
|
---|
69 | {
|
---|
70 | if (argc == 1) {
|
---|
71 | printf("Missing argument. Please specify a device to operate on.\n");
|
---|
72 | return -1;
|
---|
73 | }
|
---|
74 |
|
---|
75 | int rc;
|
---|
76 | service_id_t dev_handle;
|
---|
77 |
|
---|
78 | rc = loc_service_get_id(argv[1], &dev_handle, IPC_FLAG_BLOCKING);
|
---|
79 | if (rc != EOK) {
|
---|
80 | printf("Unknown device. Exiting.\n");
|
---|
81 | return -1;
|
---|
82 | }
|
---|
83 |
|
---|
84 | init_label();
|
---|
85 |
|
---|
86 | /*
|
---|
87 | mbr_t * mbr = mbr_read_mbr(dev_handle);
|
---|
88 | if(mbr == NULL) {
|
---|
89 | printf("Failed to read the Master Boot Record.\n" \
|
---|
90 | "Either memory allocation or disk access failed. Exiting.\n");
|
---|
91 | return -1;
|
---|
92 | }
|
---|
93 |
|
---|
94 | if(mbr_is_mbr(mbr)) {
|
---|
95 | label.layout = LYT_MBR;
|
---|
96 | set_label_mbr(mbr);
|
---|
97 | mbr_partitions_t * parts = mbr_read_partitions(mbr);
|
---|
98 | if(parts == NULL) {
|
---|
99 | printf("Failed to read and parse partitions.\n" \
|
---|
100 | "Creating new partition table.");
|
---|
101 | parts = mbr_alloc_partitions();
|
---|
102 | }
|
---|
103 | set_label_mbr_parts(parts);
|
---|
104 | fill_label_funcs();
|
---|
105 | goto interact;
|
---|
106 | }
|
---|
107 |
|
---|
108 |
|
---|
109 | mbr_free_mbr(mbr);*/
|
---|
110 |
|
---|
111 | rc = try_read_mbr(dev_handle);
|
---|
112 | if (rc == EOK)
|
---|
113 | goto interact;
|
---|
114 |
|
---|
115 | /*
|
---|
116 | gpt_t * gpt = gpt_read_gpt_header(dev_handle);
|
---|
117 |
|
---|
118 | if(gpt != NULL) {
|
---|
119 | label.layout = LYT_GPT;
|
---|
120 | set_label_gpt(gpt);
|
---|
121 |
|
---|
122 | gpt_partitions_t * parts = gpt_read_partitions(gpt);
|
---|
123 |
|
---|
124 | if(parts == NULL) {
|
---|
125 | printf("Failed to read and parse partitions.\n" \
|
---|
126 | "Creating new partition table.");
|
---|
127 | parts = gpt_alloc_partitions();
|
---|
128 | }
|
---|
129 | set_label_gpt_parts(parts);
|
---|
130 | fill_label_funcs();
|
---|
131 | goto interact;
|
---|
132 | }
|
---|
133 | */
|
---|
134 |
|
---|
135 | rc = try_read_gpt(dev_handle);
|
---|
136 | if (rc == EOK)
|
---|
137 | goto interact;
|
---|
138 |
|
---|
139 | printf("No label recognized. Create a new one.\n");
|
---|
140 | label.layout = LYT_NONE;
|
---|
141 |
|
---|
142 | interact:
|
---|
143 |
|
---|
144 | rc = interact(dev_handle);
|
---|
145 |
|
---|
146 | free_label();
|
---|
147 |
|
---|
148 | return rc;
|
---|
149 | }
|
---|
150 |
|
---|
151 | /** Interact with user */
|
---|
152 | int interact(service_id_t dev_handle)
|
---|
153 | {
|
---|
154 | int input;
|
---|
155 | tinput_t *in;
|
---|
156 |
|
---|
157 | in = tinput_new();
|
---|
158 | if (in == NULL) {
|
---|
159 | printf("Failed initing input. Free some memory.\n");
|
---|
160 | return ENOMEM;
|
---|
161 | }
|
---|
162 | tinput_set_prompt(in, "");
|
---|
163 |
|
---|
164 | printf("Welcome to hdisk.\nType 'h' for help.\n");
|
---|
165 |
|
---|
166 | while (1) {
|
---|
167 | printf("# ");
|
---|
168 | input = getchar();
|
---|
169 | printf("%c\n", input);
|
---|
170 |
|
---|
171 | switch(input) {
|
---|
172 | case 'a':
|
---|
173 | label.add_part(&label, in);
|
---|
174 | break;
|
---|
175 | case 'd':
|
---|
176 | label.delete_part(&label, in);
|
---|
177 | break;
|
---|
178 | case 'e':
|
---|
179 | label.extra_funcs(&label, in, dev_handle);
|
---|
180 | break;
|
---|
181 | case 'f':
|
---|
182 | free_label();
|
---|
183 | select_label_format(in);
|
---|
184 | break;
|
---|
185 | case 'h':
|
---|
186 | print_help();
|
---|
187 | break;
|
---|
188 | case 'l':
|
---|
189 | set_alignment(in);
|
---|
190 | break;
|
---|
191 | case 'n':
|
---|
192 | printf("Discarding label...\n");
|
---|
193 | free_label();
|
---|
194 | label.new_label(&label);
|
---|
195 | break;
|
---|
196 | case 'p':
|
---|
197 | label.print_parts(&label);
|
---|
198 | break;
|
---|
199 | case 'q':
|
---|
200 | putchar('\n');
|
---|
201 | goto end;
|
---|
202 | case 'r':
|
---|
203 | label.read_parts(&label, dev_handle);
|
---|
204 | case 'w':
|
---|
205 | label.write_parts(&label, dev_handle);
|
---|
206 | break;
|
---|
207 | default:
|
---|
208 | printf("Unknown command. Try 'h' for help.\n");
|
---|
209 | break;
|
---|
210 | }
|
---|
211 | }
|
---|
212 |
|
---|
213 | end:
|
---|
214 | tinput_destroy(in);
|
---|
215 |
|
---|
216 | return EOK;
|
---|
217 | }
|
---|
218 |
|
---|
219 | void print_help(void)
|
---|
220 | {
|
---|
221 | printf(
|
---|
222 | "\t 'a' \t\t Add partition.\n"
|
---|
223 | "\t 'd' \t\t Delete partition.\n"
|
---|
224 | "\t 'e' \t\t Extra functions (per label format).\n"
|
---|
225 | "\t 'f' \t\t Switch the format of the partition label.\n"
|
---|
226 | "\t 'h' \t\t Prints help. See help for more.\n"
|
---|
227 | "\t 'l' \t\t Set alignment.\n"
|
---|
228 | "\t 'n' \t\t Create new label (discarding the old one).\n"
|
---|
229 | "\t 'p' \t\t Prints label contents.\n"
|
---|
230 | "\t 'q' \t\t Quit.\n"
|
---|
231 | "\t 'r' \t\t Read label from disk.\n"
|
---|
232 | "\t 'w' \t\t Write label to disk.\n"
|
---|
233 | );
|
---|
234 |
|
---|
235 | }
|
---|
236 |
|
---|
237 | void select_label_format(tinput_t * in)
|
---|
238 | {
|
---|
239 | printf("Available formats are: \n"
|
---|
240 | "1) MBR\n"
|
---|
241 | "2) GPT\n"
|
---|
242 | );
|
---|
243 |
|
---|
244 | uint8_t val = get_input_uint8(in);
|
---|
245 | switch(val) {
|
---|
246 | case 0:
|
---|
247 | free_label();
|
---|
248 | construct_label(LYT_NONE);
|
---|
249 | break;
|
---|
250 | case 1:
|
---|
251 | free_label();
|
---|
252 | construct_label(LYT_MBR);
|
---|
253 | break;
|
---|
254 | case 2:
|
---|
255 | free_label();
|
---|
256 | construct_label(LYT_GPT);
|
---|
257 | break;
|
---|
258 | }
|
---|
259 | }
|
---|
260 |
|
---|
261 | void construct_label(layouts_t layout)
|
---|
262 | {
|
---|
263 | switch(layout) {
|
---|
264 | case LYT_MBR:
|
---|
265 | label.layout = LYT_MBR;
|
---|
266 | construct_mbr_label(&label);
|
---|
267 | break;
|
---|
268 | case LYT_GPT:
|
---|
269 | label.layout = LYT_GPT;
|
---|
270 | construct_gpt_label(&label);
|
---|
271 | break;
|
---|
272 | default:
|
---|
273 | label.layout = LYT_NONE;
|
---|
274 | construct_none_label(&label);
|
---|
275 | break;
|
---|
276 | }
|
---|
277 | }
|
---|
278 |
|
---|
279 | void free_label(void)
|
---|
280 | {
|
---|
281 | label.destroy_label(&label);
|
---|
282 | }
|
---|
283 |
|
---|
284 | int try_read(service_id_t dev_handle)
|
---|
285 | {
|
---|
286 | return label.read_parts(&label, dev_handle);
|
---|
287 | }
|
---|
288 |
|
---|
289 | int try_read_mbr(service_id_t dev_handle)
|
---|
290 | {
|
---|
291 | construct_label(LYT_MBR);
|
---|
292 | return try_read(dev_handle);
|
---|
293 | }
|
---|
294 |
|
---|
295 | int try_read_gpt(service_id_t dev_handle)
|
---|
296 | {
|
---|
297 | construct_label(LYT_GPT);
|
---|
298 | return try_read(dev_handle);
|
---|
299 | }
|
---|
300 |
|
---|
301 | void set_alignment(tinput_t *in)
|
---|
302 | {
|
---|
303 | printf("Set alignment to sectors: ");
|
---|
304 | label.alignment = get_input_uint32(in);
|
---|
305 | printf("Alignment set to %u sectors.\n", label.alignment);
|
---|
306 | }
|
---|
307 |
|
---|
308 |
|
---|
309 |
|
---|
310 |
|
---|
311 |
|
---|
312 |
|
---|
313 |
|
---|
314 |
|
---|
315 |
|
---|
316 |
|
---|