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 | rc = try_read_mbr(dev_handle);
|
---|
87 | if (rc == EOK)
|
---|
88 | goto interact;
|
---|
89 |
|
---|
90 | rc = try_read_gpt(dev_handle);
|
---|
91 | if (rc == EOK)
|
---|
92 | goto interact;
|
---|
93 |
|
---|
94 | printf("No label recognized. Create a new one.\n");
|
---|
95 | label.layout = LYT_NONE;
|
---|
96 |
|
---|
97 | interact:
|
---|
98 |
|
---|
99 | rc = interact(dev_handle);
|
---|
100 |
|
---|
101 | return rc;
|
---|
102 | }
|
---|
103 |
|
---|
104 | /** Interact with user */
|
---|
105 | int interact(service_id_t dev_handle)
|
---|
106 | {
|
---|
107 | int input;
|
---|
108 | tinput_t *in;
|
---|
109 |
|
---|
110 | in = tinput_new();
|
---|
111 | if (in == NULL) {
|
---|
112 | printf("Failed initing input. Free some memory.\n");
|
---|
113 | return ENOMEM;
|
---|
114 | }
|
---|
115 | tinput_set_prompt(in, "");
|
---|
116 |
|
---|
117 | printf("Welcome to hdisk.\nType 'h' for help.\n");
|
---|
118 |
|
---|
119 | while (1) {
|
---|
120 | printf("# ");
|
---|
121 | input = getchar();
|
---|
122 | printf("%c\n", input);
|
---|
123 |
|
---|
124 | switch (input) {
|
---|
125 | case 'a':
|
---|
126 | label.add_part(&label, in);
|
---|
127 | break;
|
---|
128 | case 'd':
|
---|
129 | label.delete_part(&label, in);
|
---|
130 | break;
|
---|
131 | case 'e':
|
---|
132 | label.extra_funcs(&label, in, dev_handle);
|
---|
133 | break;
|
---|
134 | case 'f':
|
---|
135 | select_label_format(in);
|
---|
136 | break;
|
---|
137 | case 'h':
|
---|
138 | print_help();
|
---|
139 | break;
|
---|
140 | case 'l':
|
---|
141 | set_alignment(in);
|
---|
142 | break;
|
---|
143 | case 'n':
|
---|
144 | printf("Discarding label...\n");
|
---|
145 | free_label();
|
---|
146 | label.new_label(&label);
|
---|
147 | break;
|
---|
148 | case 'p':
|
---|
149 | label.print_parts(&label);
|
---|
150 | break;
|
---|
151 | case 'q':
|
---|
152 | putchar('\n');
|
---|
153 | free_label();
|
---|
154 | goto end;
|
---|
155 | case 'r':
|
---|
156 | label.read_parts(&label, dev_handle);
|
---|
157 | case 'w':
|
---|
158 | label.write_parts(&label, dev_handle);
|
---|
159 | break;
|
---|
160 | default:
|
---|
161 | printf("Unknown command. Try 'h' for help.\n");
|
---|
162 | break;
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | end:
|
---|
167 | tinput_destroy(in);
|
---|
168 |
|
---|
169 | return EOK;
|
---|
170 | }
|
---|
171 |
|
---|
172 | void print_help(void)
|
---|
173 | {
|
---|
174 | printf(
|
---|
175 | "\t 'a' \t\t Add partition.\n"
|
---|
176 | "\t 'd' \t\t Delete partition.\n"
|
---|
177 | "\t 'e' \t\t Extra functions (per label format).\n"
|
---|
178 | "\t 'f' \t\t Switch the format of the partition label.\n"
|
---|
179 | "\t 'h' \t\t Prints help. See help for more.\n"
|
---|
180 | "\t 'l' \t\t Set alignment.\n"
|
---|
181 | "\t 'n' \t\t Create new label (discarding the old one).\n"
|
---|
182 | "\t 'p' \t\t Prints label contents.\n"
|
---|
183 | "\t 'q' \t\t Quit.\n"
|
---|
184 | "\t 'r' \t\t Read label from disk.\n"
|
---|
185 | "\t 'w' \t\t Write label to disk.\n"
|
---|
186 | );
|
---|
187 |
|
---|
188 | }
|
---|
189 |
|
---|
190 | void select_label_format(tinput_t * in)
|
---|
191 | {
|
---|
192 | printf("Available formats are: \n"
|
---|
193 | "1) MBR\n"
|
---|
194 | "2) GPT\n"
|
---|
195 | );
|
---|
196 |
|
---|
197 | uint8_t val = get_input_uint8(in);
|
---|
198 | switch (val) {
|
---|
199 | case 0:
|
---|
200 | free_label();
|
---|
201 | construct_label(LYT_NONE);
|
---|
202 | break;
|
---|
203 | case 1:
|
---|
204 | free_label();
|
---|
205 | construct_label(LYT_MBR);
|
---|
206 | break;
|
---|
207 | case 2:
|
---|
208 | free_label();
|
---|
209 | construct_label(LYT_GPT);
|
---|
210 | break;
|
---|
211 | }
|
---|
212 | }
|
---|
213 |
|
---|
214 | void construct_label(layouts_t layout)
|
---|
215 | {
|
---|
216 | switch (layout) {
|
---|
217 | case LYT_MBR:
|
---|
218 | label.layout = LYT_MBR;
|
---|
219 | construct_mbr_label(&label);
|
---|
220 | break;
|
---|
221 | case LYT_GPT:
|
---|
222 | label.layout = LYT_GPT;
|
---|
223 | construct_gpt_label(&label);
|
---|
224 | break;
|
---|
225 | default:
|
---|
226 | label.layout = LYT_NONE;
|
---|
227 | construct_none_label(&label);
|
---|
228 | break;
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | void free_label(void)
|
---|
233 | {
|
---|
234 | label.destroy_label(&label);
|
---|
235 | }
|
---|
236 |
|
---|
237 | int try_read(service_id_t dev_handle)
|
---|
238 | {
|
---|
239 | return label.read_parts(&label, dev_handle);
|
---|
240 | }
|
---|
241 |
|
---|
242 | int try_read_mbr(service_id_t dev_handle)
|
---|
243 | {
|
---|
244 | construct_label(LYT_MBR);
|
---|
245 | return try_read(dev_handle);
|
---|
246 | }
|
---|
247 |
|
---|
248 | int try_read_gpt(service_id_t dev_handle)
|
---|
249 | {
|
---|
250 | construct_label(LYT_GPT);
|
---|
251 | return try_read(dev_handle);
|
---|
252 | }
|
---|
253 |
|
---|
254 | void set_alignment(tinput_t *in)
|
---|
255 | {
|
---|
256 | printf("Set alignment to sectors: ");
|
---|
257 | label.alignment = get_input_uint32(in);
|
---|
258 | printf("Alignment set to %u sectors.\n", label.alignment);
|
---|
259 | }
|
---|
260 |
|
---|
261 |
|
---|
262 |
|
---|