source: mainline/uspace/app/hdisk/hdisk.c@ 30440ed

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

save progress

  • Property mode set to 100644
File size: 6.3 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
49#include "hdisk.h"
50#include "func_mbr.h"
51#include "func_gpt.h"
52
53int interact(service_id_t dev_handle);
54void print_help(void);
55void fill_table_funcs(void);
56void free_table(void);
57
58static table_t table;
59
60int main(int argc, char ** argv)
61{
62 if (argc == 1) {
63 printf("I'd like to have an argument, please.\n");
64 return 1;
65 }
66
67 int rc;
68 service_id_t dev_handle;
69
70 rc = loc_service_get_id(argv[1], &dev_handle, IPC_FLAG_BLOCKING);
71 if (rc != EOK) {
72 printf("Unknown device. Exiting.\n");
73 return -1;
74 }
75
76 init_table();
77
78 mbr_t * mbr = mbr_read_mbr(dev_handle);
79 if(mbr == NULL) {
80 printf("Failed to read the Master Boot Record.\n" \
81 "Either memory allocation or disk access failed. Exiting.\n");
82 return -1;
83 }
84
85 if(mbr_is_mbr(mbr)) {
86 table.layout = LYT_MBR;
87 set_table_mbr(mbr);
88 mbr_partitions_t * parts = mbr_read_partitions(mbr);
89 if(parts == NULL) {
90 printf("Failed to read and parse partitions.\n" \
91 "Creating new partition table.");
92 parts = mbr_alloc_partitions();
93 }
94 set_table_mbr_parts(parts);
95 fill_table_funcs();
96 } else {
97 table.layout = LYT_GPT;
98 mbr_free_mbr(mbr);
99 gpt_t * gpt = gpt_read_gpt_header(dev_handle);
100 if(gpt == NULL) {
101 printf("Failed to read and parse GPT header. Exiting.\n");
102 return -1;
103 }
104 set_table_gpt(gpt);
105 gpt_partitions_t * parts = gpt_read_partitions(gpt);
106 if(parts == NULL) {
107 printf("Failed to read and parse partitions.\n" \
108 "Creating new partition table.");
109 //parts = gpt_alloc_partitions();
110 }
111 set_table_gpt_parts(parts);
112 fill_table_funcs();
113 }
114
115 rc = interact(dev_handle);
116
117 free_table();
118
119 return rc;
120}
121
122/*
123int get_input(tinput_t * in, char ** str)
124{
125 int c;
126 size_tat
127
128 pos = 0;
129 size_t size = 256;
130
131 *str = malloc(size * sizeof(char));
132 if (*str == NULL)
133 return ENOMEM;
134
135 while ((c = getchar()) != '\n') {
136 if (c >= 32 && c <= 126) { //a printable character
137
138 (*str)[pos] = c;
139 ++pos;
140 putchar(c);
141
142 if (pos == size) {
143 char * temp = malloc(2 * size * sizeof(char));
144 memcpy(temp, *str, size);
145 free(*str);
146 *str = temp;
147 size *= 2;
148 }
149 } else if (c == 8) { //backspace
150 (*str)[pos] = 0;
151 --pos;
152 putchar(c);
153 }
154 }
155
156 putchar('\n');
157
158 (*str)[pos] = 0;
159
160 return EOK;
161}
162*/
163
164
165/** Interact with user */
166int interact(service_id_t dev_handle)
167{
168 //int rc;
169 int input;
170 tinput_t * in;
171
172 in = tinput_new();
173 if (in == NULL) {
174 printf("Failed initing input. Free some memory.\n");
175 return ENOMEM;
176 }
177 tinput_set_prompt(in, "");
178
179 printf("Welcome to hdisk.\nType 'h' for help.\n");
180
181 //printf("# ");
182 //input = getchar();
183 //printf("%c\n", input);
184
185 while (1) {
186
187 printf("# ");
188 input = getchar();
189 printf("%c\n", input);
190
191
192 //rc = tinput_read(in, &str);
193 //if (rc == ENOENT) {
194 //// User requested exit
195 //putchar('\n');
196 //return rc;
197 //}
198 //if (rc != EOK) {
199 //printf("Failed reading input. Exiting...\n");
200 //return rc;
201 //}
202 //// Check for empty input.
203 //if (str_cmp(str, "") == 0)
204 //continue;
205
206 switch(input) {
207 case 'a':
208 table.add_part(in, &table.data);
209 break;
210 case 'd':
211 table.delete_part(in, &table.data);
212 break;
213 case 'e':
214 table.extra_funcs(in, dev_handle, &table.data);
215 break;
216 case 'h':
217 print_help();
218 break;
219 case 'p':
220 table.print_parts(&table.data);
221 break;
222 case 'q':
223 putchar('\n');
224 goto end;
225 case 'w':
226 table.write_parts(dev_handle, &table.data);
227 break;
228 default:
229 printf("Unknown command. Try 'h' for help.\n");
230 break;
231 }
232 //printf("# ");
233 //input = getchar();
234 //printf("%c\n", input);
235
236 }
237
238end:
239 tinput_destroy(in);
240
241 return EOK;
242}
243
244void print_help(void)
245{
246 printf(
247 "\t 'a' \t\t Add partition.\n"
248 "\t 'd' \t\t Delete partition.\n"
249 "\t 'h' \t\t Prints help. See help for more.\n" \
250 "\t 'p' \t\t Prints the table contents.\n" \
251 "\t 'w' \t\t Write table to disk.\n" \
252 "\t 'q' \t\t Quit.\n" \
253 );
254
255}
256
257void fill_table_funcs(void)
258{
259 switch(table.layout) {
260 case LYT_MBR:
261 table.add_part = add_mbr_part;
262 table.delete_part = delete_mbr_part;
263 table.print_parts = print_mbr_parts;
264 table.write_parts = write_mbr_parts;
265 table.extra_funcs = extra_mbr_funcs;
266 break;
267 case LYT_GPT:
268 table.add_part = add_gpt_part;
269 table.delete_part = delete_gpt_part;
270 table.print_parts = print_gpt_parts;
271 table.write_parts = write_gpt_parts;
272 table.extra_funcs = extra_gpt_funcs;
273 break;
274 default:
275 break;
276 }
277}
278
279void free_table(void)
280{
281 switch(table.layout) {
282 case LYT_MBR:
283 mbr_free_partitions(table.data.mbr.parts);
284 mbr_free_mbr(table.data.mbr.mbr);
285 break;
286 case LYT_GPT:
287 gpt_free_partitions(table.data.gpt.parts);
288 gpt_free_gpt(table.data.gpt.gpt);
289 break;
290 default:
291 break;
292 }
293}
294
Note: See TracBrowser for help on using the repository browser.