source: mainline/uspace/app/hdisk/hdisk.c@ 8c95dff

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8c95dff was 8c95dff, checked in by Dominik Taborsky (AT DOT) <brembyseznamcz>, 12 years ago

various bugfixes

  • Property mode set to 100644
File size: 5.8 KB
Line 
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#include <str_error.h>
49
50#include "hdisk.h"
51#include "input.h"
52#include "func_gpt.h"
53#include "func_mbr.h"
54#include "func_none.h"
55
56int interact(void);
57void print_help(void);
58void select_label_format(tinput_t *);
59void construct_label(layouts_t);
60void free_label(void);
61int try_read(void);
62int try_read_mbr(void);
63int try_read_gpt(void);
64void set_alignment(tinput_t *);
65
66
67static label_t label;
68
69int main(int argc, char ** argv)
70{
71 if (argc == 1) {
72 printf("Missing argument. Please specify a device to operate on.\n");
73 return -1;
74 }
75
76 int rc;
77 service_id_t dev_handle;
78
79 rc = loc_service_get_id(argv[1], &dev_handle, IPC_FLAG_BLOCKING);
80 if (rc != EOK) {
81 printf("Unknown device. Exiting.\n");
82 return -1;
83 }
84
85 init_label();
86 label.device = dev_handle;
87
88 rc = block_init(EXCHANGE_ATOMIC, dev_handle, 512);
89 if (rc != EOK) {
90 printf("Error during libblock init: %d - %s.\n", rc, str_error(rc));
91 return -1;
92 }
93
94 aoff64_t nblocks;
95 rc = block_get_nblocks(dev_handle, &nblocks);
96 block_fini(dev_handle);
97 if (rc != EOK) {
98 printf(LIBMBR_NAME ": Error while getting number of blocks: %d - %s.\n", rc, str_error(rc));
99 return -1;
100 }
101
102 label.nblocks = nblocks;
103
104 rc = try_read_mbr();
105 if (rc == EOK)
106 goto interact;
107
108 free_label();
109
110 rc = try_read_gpt();
111 if (rc == EOK)
112 goto interact;
113
114 printf("No label recognized. Create a new one.\n");
115 construct_label(LYT_NONE);
116
117interact:
118
119 rc = interact();
120
121 return rc;
122}
123
124/** Interact with user */
125int interact()
126{
127 int input;
128 tinput_t *in;
129
130 in = tinput_new();
131 if (in == NULL) {
132 printf("Failed initing input. Free some memory.\n");
133 return ENOMEM;
134 }
135 tinput_set_prompt(in, "");
136
137 printf("Welcome to hdisk.\nType 'h' for help.\n");
138
139 while (1) {
140 printf("# ");
141 input = getchar();
142 printf("%c\n", input);
143
144 switch (input) {
145 case 'a':
146 label.add_part(&label, in);
147 break;
148 case 'd':
149 label.delete_part(&label, in);
150 break;
151 case 'e':
152 label.extra_funcs(&label, in);
153 break;
154 case 'f':
155 free_label();
156 select_label_format(in);
157 break;
158 case 'h':
159 print_help();
160 break;
161 case 'l':
162 set_alignment(in);
163 break;
164 case 'n':
165 printf("Discarding label...\n");
166 free_label();
167 label.new_label(&label);
168 break;
169 case 'p':
170 label.print_parts(&label);
171 break;
172 case 'q':
173 putchar('\n');
174 free_label();
175 goto end;
176 case 'r':
177 label.read_parts(&label);
178 case 'w':
179 label.write_parts(&label);
180 break;
181 default:
182 printf("Unknown command. Try 'h' for help.\n");
183 break;
184 }
185 }
186
187end:
188 tinput_destroy(in);
189
190 return EOK;
191}
192
193void print_help(void)
194{
195 printf(
196 "\t 'a' \t\t Add partition.\n"
197 "\t 'd' \t\t Delete partition.\n"
198 "\t 'e' \t\t Extra functions (per label format).\n"
199 "\t 'f' \t\t Switch the format of the partition label.\n"
200 "\t 'h' \t\t Prints help. See help for more.\n"
201 "\t 'l' \t\t Set alignment.\n"
202 "\t 'n' \t\t Create new label (discarding the old one).\n"
203 "\t 'p' \t\t Prints label contents.\n"
204 "\t 'q' \t\t Quit.\n"
205 "\t 'r' \t\t Read label from disk.\n"
206 "\t 'w' \t\t Write label to disk.\n"
207 );
208
209}
210
211void select_label_format(tinput_t * in)
212{
213 printf("Available formats are: \n"
214 "1) MBR\n"
215 "2) GPT\n"
216 );
217
218 uint8_t val = get_input_uint8(in);
219 switch (val) {
220 case 1:
221 construct_label(LYT_MBR);
222 break;
223 case 2:
224 construct_label(LYT_GPT);
225 break;
226 default:
227 construct_label(LYT_NONE);
228 break;
229 }
230}
231
232void construct_label(layouts_t layout)
233{
234 switch (layout) {
235 case LYT_MBR:
236 label.layout = LYT_MBR;
237 construct_mbr_label(&label);
238 break;
239 case LYT_GPT:
240 label.layout = LYT_GPT;
241 construct_gpt_label(&label);
242 break;
243 default:
244 label.layout = LYT_NONE;
245 construct_none_label(&label);
246 break;
247 }
248}
249
250void free_label(void)
251{
252 label.destroy_label(&label);
253}
254
255int try_read()
256{
257
258 return label.read_parts(&label);
259}
260
261int try_read_mbr()
262{
263 construct_label(LYT_MBR);
264 return try_read();
265}
266
267int try_read_gpt()
268{
269 construct_label(LYT_GPT);
270 return try_read();
271}
272
273void set_alignment(tinput_t *in)
274{
275 printf("Set alignment to sectors: ");
276 label.alignment = get_input_uint32(in);
277 printf("Alignment set to %u sectors.\n", label.alignment);
278}
279
280
281
Note: See TracBrowser for help on using the repository browser.