source: mainline/uspace/app/hdisk/hdisk.c@ 6e8e4e19

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

libmbr & hdisk changes, fixes, updates

  • Property mode set to 100644
File size: 6.6 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 /*
[ec50ac4a]87 mbr_t * mbr = mbr_read_mbr(dev_handle);
88 if(mbr == NULL) {
89 printf("Failed to read the Master Boot Record.\n" \
[271e24a]90 "Either memory allocation or disk access failed. Exiting.\n");
[ec50ac4a]91 return -1;
92 }
93
94 if(mbr_is_mbr(mbr)) {
[a2aa81cb]95 label.layout = LYT_MBR;
96 set_label_mbr(mbr);
[271e24a]97 mbr_partitions_t * parts = mbr_read_partitions(mbr);
[ec50ac4a]98 if(parts == NULL) {
99 printf("Failed to read and parse partitions.\n" \
[271e24a]100 "Creating new partition table.");
101 parts = mbr_alloc_partitions();
[ec50ac4a]102 }
[a2aa81cb]103 set_label_mbr_parts(parts);
104 fill_label_funcs();
[700f89e]105 goto interact;
106 }
107
108
[a2aa81cb]109 mbr_free_mbr(mbr);*/
110
111 rc = try_read_mbr(dev_handle);
112 if (rc == EOK)
113 goto interact;
114
115 /*
[700f89e]116 gpt_t * gpt = gpt_read_gpt_header(dev_handle);
117
118 if(gpt != NULL) {
[a2aa81cb]119 label.layout = LYT_GPT;
120 set_label_gpt(gpt);
[700f89e]121
[271e24a]122 gpt_partitions_t * parts = gpt_read_partitions(gpt);
[700f89e]123
[271e24a]124 if(parts == NULL) {
125 printf("Failed to read and parse partitions.\n" \
126 "Creating new partition table.");
[700f89e]127 parts = gpt_alloc_partitions();
[271e24a]128 }
[a2aa81cb]129 set_label_gpt_parts(parts);
130 fill_label_funcs();
[700f89e]131 goto interact;
[ec50ac4a]132 }
[a2aa81cb]133 */
134
135 rc = try_read_gpt(dev_handle);
136 if (rc == EOK)
137 goto interact;
138
139 printf("No label recognized. Create a new one.\n");
140 label.layout = LYT_NONE;
[700f89e]141
142interact:
[6e8e4e19]143
[271e24a]144 rc = interact(dev_handle);
[8f6c7785]145
[a2aa81cb]146 free_label();
[8f6c7785]147
[ec50ac4a]148 return rc;
149}
150
151/** Interact with user */
[271e24a]152int interact(service_id_t dev_handle)
[ec50ac4a]153{
[271e24a]154 int input;
[6e8e4e19]155 tinput_t *in;
[700f89e]156
[ec50ac4a]157 in = tinput_new();
158 if (in == NULL) {
159 printf("Failed initing input. Free some memory.\n");
[271e24a]160 return ENOMEM;
[ec50ac4a]161 }
[30440ed]162 tinput_set_prompt(in, "");
[700f89e]163
[ec50ac4a]164 printf("Welcome to hdisk.\nType 'h' for help.\n");
[700f89e]165
[ec50ac4a]166 while (1) {
[30440ed]167 printf("# ");
[271e24a]168 input = getchar();
[30440ed]169 printf("%c\n", input);
[700f89e]170
[271e24a]171 switch(input) {
[ec50ac4a]172 case 'a':
[6e8e4e19]173 label.add_part(&label, in);
[ec50ac4a]174 break;
175 case 'd':
[6e8e4e19]176 label.delete_part(&label, in);
[ec50ac4a]177 break;
[30440ed]178 case 'e':
[6e8e4e19]179 label.extra_funcs(&label, in, dev_handle);
[30440ed]180 break;
[700f89e]181 case 'f':
[a2aa81cb]182 free_label();
183 select_label_format(in);
[700f89e]184 break;
[ec50ac4a]185 case 'h':
186 print_help();
187 break;
[6e8e4e19]188 case 'l':
189 set_alignment(in);
190 break;
[700f89e]191 case 'n':
[6e8e4e19]192 printf("Discarding label...\n");
[a2aa81cb]193 free_label();
[6e8e4e19]194 label.new_label(&label);
[700f89e]195 break;
[ec50ac4a]196 case 'p':
[6e8e4e19]197 label.print_parts(&label);
[ec50ac4a]198 break;
199 case 'q':
200 putchar('\n');
[271e24a]201 goto end;
[6e8e4e19]202 case 'r':
203 label.read_parts(&label, dev_handle);
[ec50ac4a]204 case 'w':
[6e8e4e19]205 label.write_parts(&label, dev_handle);
[ec50ac4a]206 break;
207 default:
208 printf("Unknown command. Try 'h' for help.\n");
209 break;
210 }
211 }
[700f89e]212
[271e24a]213end:
[ec50ac4a]214 tinput_destroy(in);
[700f89e]215
[271e24a]216 return EOK;
[ec50ac4a]217}
218
219void print_help(void)
220{
221 printf(
222 "\t 'a' \t\t Add partition.\n"
223 "\t 'd' \t\t Delete partition.\n"
[a2aa81cb]224 "\t 'e' \t\t Extra functions (per label format).\n"
[6e8e4e19]225 "\t 'f' \t\t Switch the format of the partition label.\n"
[700f89e]226 "\t 'h' \t\t Prints help. See help for more.\n"
[a2aa81cb]227 "\t 'l' \t\t Set alignment.\n"
228 "\t 'n' \t\t Create new label (discarding the old one).\n"
229 "\t 'p' \t\t Prints label contents.\n"
[700f89e]230 "\t 'q' \t\t Quit.\n"
[6e8e4e19]231 "\t 'r' \t\t Read label from disk.\n"
232 "\t 'w' \t\t Write label to disk.\n"
[ec50ac4a]233 );
234
235}
236
[a2aa81cb]237void select_label_format(tinput_t * in)
[700f89e]238{
239 printf("Available formats are: \n"
240 "1) MBR\n"
241 "2) GPT\n"
[6e8e4e19]242 );
[700f89e]243
244 uint8_t val = get_input_uint8(in);
245 switch(val) {
246 case 0:
[a2aa81cb]247 free_label();
[6e8e4e19]248 construct_label(LYT_NONE);
[700f89e]249 break;
250 case 1:
[a2aa81cb]251 free_label();
[6e8e4e19]252 construct_label(LYT_MBR);
[700f89e]253 break;
254 case 2:
[a2aa81cb]255 free_label();
[6e8e4e19]256 construct_label(LYT_GPT);
[700f89e]257 break;
258 }
259}
260
[6e8e4e19]261void construct_label(layouts_t layout)
[ec50ac4a]262{
[6e8e4e19]263 switch(layout) {
[271e24a]264 case LYT_MBR:
[6e8e4e19]265 label.layout = LYT_MBR;
266 construct_mbr_label(&label);
[271e24a]267 break;
268 case LYT_GPT:
[6e8e4e19]269 label.layout = LYT_GPT;
270 construct_gpt_label(&label);
[271e24a]271 break;
272 default:
[6e8e4e19]273 label.layout = LYT_NONE;
274 construct_none_label(&label);
[ec50ac4a]275 break;
276 }
277}
278
[a2aa81cb]279void free_label(void)
[ec50ac4a]280{
[6e8e4e19]281 label.destroy_label(&label);
[a2aa81cb]282}
283
284int try_read(service_id_t dev_handle)
285{
[6e8e4e19]286 return label.read_parts(&label, dev_handle);
[ec50ac4a]287}
[a2aa81cb]288
289int try_read_mbr(service_id_t dev_handle)
290{
[6e8e4e19]291 construct_label(LYT_MBR);
[a2aa81cb]292 return try_read(dev_handle);
293}
294
295int try_read_gpt(service_id_t dev_handle)
296{
[6e8e4e19]297 construct_label(LYT_GPT);
[a2aa81cb]298 return try_read(dev_handle);
299}
300
[6e8e4e19]301void set_alignment(tinput_t *in)
302{
303 printf("Set alignment to sectors: ");
304 label.alignment = get_input_uint32(in);
305 printf("Alignment set to %u sectors.\n", label.alignment);
306}
[a2aa81cb]307
308
309
310
311
312
313
[ec50ac4a]314
[700f89e]315
316
Note: See TracBrowser for help on using the repository browser.