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 fill_label_funcs(void);
|
---|
59 | void free_label(void);
|
---|
60 | int try_read(service_id_t);
|
---|
61 |
|
---|
62 | int construct_none_label(void);
|
---|
63 |
|
---|
64 | int construct_mbr_label(void);
|
---|
65 | int try_read_mbr(service_id_t);
|
---|
66 |
|
---|
67 | int construct_gpt_label(void);
|
---|
68 | int try_read_gpt(service_id_t);
|
---|
69 |
|
---|
70 |
|
---|
71 | static label_t label;
|
---|
72 |
|
---|
73 | int main(int argc, char ** argv)
|
---|
74 | {
|
---|
75 | if (argc == 1) {
|
---|
76 | printf("Missing argument. Please specify a device to operate on.\n");
|
---|
77 | return -1;
|
---|
78 | }
|
---|
79 |
|
---|
80 | int rc;
|
---|
81 | service_id_t dev_handle;
|
---|
82 |
|
---|
83 | rc = loc_service_get_id(argv[1], &dev_handle, IPC_FLAG_BLOCKING);
|
---|
84 | if (rc != EOK) {
|
---|
85 | printf("Unknown device. Exiting.\n");
|
---|
86 | return -1;
|
---|
87 | }
|
---|
88 |
|
---|
89 | printf("Init.\n");
|
---|
90 | init_label();
|
---|
91 |
|
---|
92 | /*
|
---|
93 | mbr_t * mbr = mbr_read_mbr(dev_handle);
|
---|
94 | if(mbr == NULL) {
|
---|
95 | printf("Failed to read the Master Boot Record.\n" \
|
---|
96 | "Either memory allocation or disk access failed. Exiting.\n");
|
---|
97 | return -1;
|
---|
98 | }
|
---|
99 |
|
---|
100 | if(mbr_is_mbr(mbr)) {
|
---|
101 | label.layout = LYT_MBR;
|
---|
102 | set_label_mbr(mbr);
|
---|
103 | mbr_partitions_t * parts = mbr_read_partitions(mbr);
|
---|
104 | if(parts == NULL) {
|
---|
105 | printf("Failed to read and parse partitions.\n" \
|
---|
106 | "Creating new partition table.");
|
---|
107 | parts = mbr_alloc_partitions();
|
---|
108 | }
|
---|
109 | set_label_mbr_parts(parts);
|
---|
110 | fill_label_funcs();
|
---|
111 | goto interact;
|
---|
112 | }
|
---|
113 |
|
---|
114 |
|
---|
115 | mbr_free_mbr(mbr);*/
|
---|
116 |
|
---|
117 | printf("Try MBR.\n");
|
---|
118 | rc = try_read_mbr(dev_handle);
|
---|
119 | if (rc == EOK)
|
---|
120 | goto interact;
|
---|
121 |
|
---|
122 | /*
|
---|
123 | gpt_t * gpt = gpt_read_gpt_header(dev_handle);
|
---|
124 |
|
---|
125 | if(gpt != NULL) {
|
---|
126 | label.layout = LYT_GPT;
|
---|
127 | set_label_gpt(gpt);
|
---|
128 |
|
---|
129 | gpt_partitions_t * parts = gpt_read_partitions(gpt);
|
---|
130 |
|
---|
131 | if(parts == NULL) {
|
---|
132 | printf("Failed to read and parse partitions.\n" \
|
---|
133 | "Creating new partition table.");
|
---|
134 | parts = gpt_alloc_partitions();
|
---|
135 | }
|
---|
136 | set_label_gpt_parts(parts);
|
---|
137 | fill_label_funcs();
|
---|
138 | goto interact;
|
---|
139 | }
|
---|
140 | */
|
---|
141 |
|
---|
142 | printf("Try GPT.\n");
|
---|
143 | rc = try_read_gpt(dev_handle);
|
---|
144 | if (rc == EOK)
|
---|
145 | goto interact;
|
---|
146 |
|
---|
147 | printf("No label recognized. Create a new one.\n");
|
---|
148 | label.layout = LYT_NONE;
|
---|
149 |
|
---|
150 | interact:
|
---|
151 | printf("interact.\n");
|
---|
152 | rc = interact(dev_handle);
|
---|
153 |
|
---|
154 | free_label();
|
---|
155 |
|
---|
156 | return rc;
|
---|
157 | }
|
---|
158 |
|
---|
159 | /** Interact with user */
|
---|
160 | int interact(service_id_t dev_handle)
|
---|
161 | {
|
---|
162 | int input;
|
---|
163 | tinput_t * in;
|
---|
164 |
|
---|
165 | in = tinput_new();
|
---|
166 | if (in == NULL) {
|
---|
167 | printf("Failed initing input. Free some memory.\n");
|
---|
168 | return ENOMEM;
|
---|
169 | }
|
---|
170 | tinput_set_prompt(in, "");
|
---|
171 |
|
---|
172 | printf("Welcome to hdisk.\nType 'h' for help.\n");
|
---|
173 |
|
---|
174 | while (1) {
|
---|
175 | printf("# ");
|
---|
176 | input = getchar();
|
---|
177 | printf("%c\n", input);
|
---|
178 |
|
---|
179 | switch(input) {
|
---|
180 | case 'a':
|
---|
181 | label.add_part(in, &label.data);
|
---|
182 | break;
|
---|
183 | case 'b':
|
---|
184 | label.add_part(in, &label.data);
|
---|
185 | break;
|
---|
186 | case 'd':
|
---|
187 | label.delete_part(in, &label.data);
|
---|
188 | break;
|
---|
189 | case 'e':
|
---|
190 | label.extra_funcs(in, dev_handle, &label.data);
|
---|
191 | break;
|
---|
192 | case 'f':
|
---|
193 | free_label();
|
---|
194 | select_label_format(in);
|
---|
195 | break;
|
---|
196 | case 'h':
|
---|
197 | print_help();
|
---|
198 | break;
|
---|
199 | case 'n':
|
---|
200 | free_label();
|
---|
201 | label.new_label(&label.data);
|
---|
202 | break;
|
---|
203 | case 'p':
|
---|
204 | label.print_parts(&label.data);
|
---|
205 | break;
|
---|
206 | case 'q':
|
---|
207 | putchar('\n');
|
---|
208 | goto end;
|
---|
209 | case 'w':
|
---|
210 | label.write_parts(dev_handle, &label.data);
|
---|
211 | break;
|
---|
212 | default:
|
---|
213 | printf("Unknown command. Try 'h' for help.\n");
|
---|
214 | break;
|
---|
215 | }
|
---|
216 | }
|
---|
217 |
|
---|
218 | end:
|
---|
219 | tinput_destroy(in);
|
---|
220 |
|
---|
221 | return EOK;
|
---|
222 | }
|
---|
223 |
|
---|
224 | void print_help(void)
|
---|
225 | {
|
---|
226 | printf(
|
---|
227 | "\t 'a' \t\t Add partition.\n"
|
---|
228 | "\t 'd' \t\t Delete partition.\n"
|
---|
229 | "\t 'e' \t\t Extra functions (per label format).\n"
|
---|
230 | "\t 'f' \t\t Switch the format of the partition label."
|
---|
231 | "\t 'h' \t\t Prints help. See help for more.\n"
|
---|
232 | "\t 'l' \t\t Set alignment.\n"
|
---|
233 | "\t 'n' \t\t Create new label (discarding the old one).\n"
|
---|
234 | "\t 'p' \t\t Prints label contents.\n"
|
---|
235 | "\t 'w' \t\t Write label to disk.\n"
|
---|
236 | "\t 'q' \t\t Quit.\n"
|
---|
237 | );
|
---|
238 |
|
---|
239 | }
|
---|
240 |
|
---|
241 | void select_label_format(tinput_t * in)
|
---|
242 | {
|
---|
243 | printf("Available formats are: \n"
|
---|
244 | "1) MBR\n"
|
---|
245 | "2) GPT\n"
|
---|
246 | );
|
---|
247 |
|
---|
248 | uint8_t val = get_input_uint8(in);
|
---|
249 | switch(val) {
|
---|
250 | case 0:
|
---|
251 | free_label();
|
---|
252 | label.layout = LYT_NONE;
|
---|
253 | fill_label_funcs();
|
---|
254 | break;
|
---|
255 | case 1:
|
---|
256 | free_label();
|
---|
257 | label.layout = LYT_MBR;
|
---|
258 | fill_label_funcs();
|
---|
259 | break;
|
---|
260 | case 2:
|
---|
261 | free_label();
|
---|
262 | label.layout = LYT_GPT;
|
---|
263 | fill_label_funcs();
|
---|
264 | break;
|
---|
265 | }
|
---|
266 | }
|
---|
267 |
|
---|
268 | void fill_label_funcs(void)
|
---|
269 | {
|
---|
270 | switch(label.layout) {
|
---|
271 | case LYT_MBR:
|
---|
272 | construct_mbr_label();
|
---|
273 | break;
|
---|
274 | case LYT_GPT:
|
---|
275 | construct_gpt_label();
|
---|
276 | break;
|
---|
277 | default:
|
---|
278 | construct_none_label();
|
---|
279 | break;
|
---|
280 | }
|
---|
281 | }
|
---|
282 |
|
---|
283 | void free_label(void)
|
---|
284 | {
|
---|
285 | /*
|
---|
286 | switch(label.layout) {
|
---|
287 | case LYT_MBR:
|
---|
288 | destroy_mbr_label(&label);
|
---|
289 | break;
|
---|
290 | case LYT_GPT:
|
---|
291 | destroy_gpt_label(&label);
|
---|
292 | break;
|
---|
293 | default:
|
---|
294 | break;
|
---|
295 | }
|
---|
296 | */
|
---|
297 |
|
---|
298 | label.destroy_label(&label.data);
|
---|
299 | }
|
---|
300 |
|
---|
301 | int try_read(service_id_t dev_handle)
|
---|
302 | {
|
---|
303 | fill_label_funcs();
|
---|
304 | printf("read_parts\n");
|
---|
305 | return label.read_parts(dev_handle, &label.data);
|
---|
306 | }
|
---|
307 |
|
---|
308 | int construct_none_label()
|
---|
309 | {
|
---|
310 | label.add_part = add_none_part;
|
---|
311 | label.delete_part = delete_none_part;
|
---|
312 | label.destroy_label = destroy_none_label;
|
---|
313 | label.new_label = new_none_label;
|
---|
314 | label.print_parts = print_none_parts;
|
---|
315 | label.read_parts = read_none_parts;
|
---|
316 | label.write_parts = write_none_parts;
|
---|
317 | label.extra_funcs = extra_none_funcs;
|
---|
318 |
|
---|
319 | return EOK;
|
---|
320 | }
|
---|
321 |
|
---|
322 | int construct_mbr_label()
|
---|
323 | {
|
---|
324 | label.add_part = add_mbr_part;
|
---|
325 | label.delete_part = delete_mbr_part;
|
---|
326 | label.destroy_label = destroy_mbr_label;
|
---|
327 | label.new_label = new_mbr_label;
|
---|
328 | label.print_parts = print_mbr_parts;
|
---|
329 | label.read_parts = read_mbr_parts;
|
---|
330 | label.write_parts = write_mbr_parts;
|
---|
331 | label.extra_funcs = extra_mbr_funcs;
|
---|
332 |
|
---|
333 | return label.new_label(&label.data);
|
---|
334 | }
|
---|
335 |
|
---|
336 | int try_read_mbr(service_id_t dev_handle)
|
---|
337 | {
|
---|
338 | label.layout = LYT_MBR;
|
---|
339 | return try_read(dev_handle);
|
---|
340 | }
|
---|
341 |
|
---|
342 | int construct_gpt_label()
|
---|
343 | {
|
---|
344 | label.add_part = add_gpt_part;
|
---|
345 | label.delete_part = delete_gpt_part;
|
---|
346 | label.new_label = new_gpt_label;
|
---|
347 | label.print_parts = print_gpt_parts;
|
---|
348 | label.read_parts = read_gpt_parts;
|
---|
349 | label.write_parts = write_gpt_parts;
|
---|
350 | label.extra_funcs = extra_gpt_funcs;
|
---|
351 |
|
---|
352 | return label.new_label(&label.data);
|
---|
353 | }
|
---|
354 |
|
---|
355 | int try_read_gpt(service_id_t dev_handle)
|
---|
356 | {
|
---|
357 | label.layout = LYT_GPT;
|
---|
358 | return try_read(dev_handle);
|
---|
359 | }
|
---|
360 |
|
---|
361 |
|
---|
362 |
|
---|
363 |
|
---|
364 |
|
---|
365 |
|
---|
366 |
|
---|
367 |
|
---|
368 |
|
---|
369 |
|
---|
370 |
|
---|
371 |
|
---|
372 |
|
---|
373 |
|
---|
374 |
|
---|
375 |
|
---|
376 |
|
---|
377 |
|
---|
378 |
|
---|
379 |
|
---|
380 |
|
---|
381 |
|
---|
382 |
|
---|
383 |
|
---|
384 |
|
---|
385 |
|
---|