source: mainline/uspace/app/hdisk/hdisk.c@ 0435fe41

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

polishing libmbr, libgpt, hdisk

  • Property mode set to 100644
File size: 5.5 KB
RevLine 
[ec50ac4a]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>
[271e24a]40#include <block.h>
[ec50ac4a]41#include <errno.h>
42#include <stdlib.h>
43#include <assert.h>
44#include <str.h>
45#include <libmbr.h>
[271e24a]46#include <libgpt.h>
[ec50ac4a]47#include <tinput.h>
48
[271e24a]49#include "hdisk.h"
[9bda5d90]50#include "input.h"
[ec50ac4a]51#include "func_gpt.h"
[9bda5d90]52#include "func_mbr.h"
53#include "func_none.h"
[ec50ac4a]54
[a2aa81cb]55int interact(service_id_t);
[ec50ac4a]56void print_help(void);
[a2aa81cb]57void select_label_format(tinput_t *);
[6e8e4e19]58void construct_label(layouts_t);
[a2aa81cb]59void free_label(void);
60int try_read(service_id_t);
61int try_read_mbr(service_id_t);
62int try_read_gpt(service_id_t);
[6e8e4e19]63void set_alignment(tinput_t *);
[a2aa81cb]64
65
66static label_t label;
[ec50ac4a]67
68int main(int argc, char ** argv)
69{
70 if (argc == 1) {
[a2aa81cb]71 printf("Missing argument. Please specify a device to operate on.\n");
72 return -1;
[ec50ac4a]73 }
[a2aa81cb]74
[ec50ac4a]75 int rc;
76 service_id_t dev_handle;
[a2aa81cb]77
[ec50ac4a]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 }
[a2aa81cb]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;
[700f89e]96
97interact:
[6e8e4e19]98
[271e24a]99 rc = interact(dev_handle);
[8f6c7785]100
[ec50ac4a]101 return rc;
102}
103
104/** Interact with user */
[271e24a]105int interact(service_id_t dev_handle)
[ec50ac4a]106{
[271e24a]107 int input;
[6e8e4e19]108 tinput_t *in;
[700f89e]109
[ec50ac4a]110 in = tinput_new();
111 if (in == NULL) {
112 printf("Failed initing input. Free some memory.\n");
[271e24a]113 return ENOMEM;
[ec50ac4a]114 }
[30440ed]115 tinput_set_prompt(in, "");
[700f89e]116
[ec50ac4a]117 printf("Welcome to hdisk.\nType 'h' for help.\n");
[700f89e]118
[ec50ac4a]119 while (1) {
[30440ed]120 printf("# ");
[271e24a]121 input = getchar();
[30440ed]122 printf("%c\n", input);
[700f89e]123
[0435fe41]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;
[ec50ac4a]163 }
164 }
[700f89e]165
[271e24a]166end:
[ec50ac4a]167 tinput_destroy(in);
[700f89e]168
[271e24a]169 return EOK;
[ec50ac4a]170}
171
172void print_help(void)
173{
174 printf(
175 "\t 'a' \t\t Add partition.\n"
176 "\t 'd' \t\t Delete partition.\n"
[a2aa81cb]177 "\t 'e' \t\t Extra functions (per label format).\n"
[6e8e4e19]178 "\t 'f' \t\t Switch the format of the partition label.\n"
[700f89e]179 "\t 'h' \t\t Prints help. See help for more.\n"
[a2aa81cb]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"
[700f89e]183 "\t 'q' \t\t Quit.\n"
[6e8e4e19]184 "\t 'r' \t\t Read label from disk.\n"
185 "\t 'w' \t\t Write label to disk.\n"
[ec50ac4a]186 );
187
188}
189
[a2aa81cb]190void select_label_format(tinput_t * in)
[700f89e]191{
192 printf("Available formats are: \n"
193 "1) MBR\n"
194 "2) GPT\n"
[6e8e4e19]195 );
[700f89e]196
197 uint8_t val = get_input_uint8(in);
[0435fe41]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;
[700f89e]211 }
212}
213
[6e8e4e19]214void construct_label(layouts_t layout)
[ec50ac4a]215{
[0435fe41]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;
[ec50ac4a]229 }
230}
231
[a2aa81cb]232void free_label(void)
[ec50ac4a]233{
[6e8e4e19]234 label.destroy_label(&label);
[a2aa81cb]235}
236
237int try_read(service_id_t dev_handle)
238{
[6e8e4e19]239 return label.read_parts(&label, dev_handle);
[ec50ac4a]240}
[a2aa81cb]241
242int try_read_mbr(service_id_t dev_handle)
243{
[6e8e4e19]244 construct_label(LYT_MBR);
[a2aa81cb]245 return try_read(dev_handle);
246}
247
248int try_read_gpt(service_id_t dev_handle)
249{
[6e8e4e19]250 construct_label(LYT_GPT);
[a2aa81cb]251 return try_read(dev_handle);
252}
253
[6e8e4e19]254void 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}
[a2aa81cb]260
261
262
Note: See TracBrowser for help on using the repository browser.