source: mainline/uspace/app/hdisk/func_mbr.c@ f6c8fca

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

libmbr final

  • Property mode set to 100644
File size: 4.7 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 <stdio.h>
36#include <errno.h>
37#include <str_error.h>
38#include <sys/types.h>
39
40#include "func_mbr.h"
41#include "input.h"
42
43static int set_mbr_partition(tinput_t * in, mbr_part_t * p);
44
45
46int add_mbr_part(tinput_t * in, union table_data * data)
47{
48 int rc;
49
50 mbr_part_t * part = mbr_alloc_partition();
51
52 set_mbr_partition(in, part);
53
54 rc = mbr_add_partition(data->mbr.parts, part);
55 if (rc != EOK) {
56 printf("Error adding partition.\n");
57 }
58
59
60 return rc;
61}
62
63int delete_mbr_part(tinput_t * in, union table_data * data)
64{
65 int rc;
66 size_t idx;
67
68 printf("Number of the partition to delete (counted from 0): ");
69 idx = get_input_size_t(in);
70
71 if (idx == 0 && errno != EOK)
72 return errno;
73
74 rc = mbr_remove_partition(data->mbr.parts, idx);
75 if(rc != EOK) {
76 printf("Error: something.\n");
77 }
78
79 return EOK;
80}
81
82int new_mbr_table(tinput_t * in, union table_data * data)
83{
84 data->mbr.mbr = mbr_alloc_mbr();
85 data->mbr.parts = mbr_alloc_partitions();
86 return EOK;
87}
88
89/** Print current partition scheme */
90int print_mbr_parts(union table_data * data)
91{
92 int num = 0;
93
94 printf("Current partition scheme:\n");
95 //printf("\t\tBootable:\tStart:\tEnd:\tLength:\tType:\n");
96 printf("\t\t%10s %10s %10s %10s %7s\n", "Bootable:", "Start:", "End:", "Length:", "Type:");
97
98 mbr_part_t * it;
99 mbr_part_foreach(data->mbr.parts, it) {
100 if (it->type == PT_UNUSED)
101 continue;
102
103 printf("\tP%d:\t", num);
104 if (mbr_get_flag(it, ST_BOOT))
105 printf("*");
106 else
107 printf(" ");
108
109 printf("\t%10u %10u %10u %7u\n", it->start_addr, it->start_addr + it->length, it->length, it->type);
110
111 ++num;
112 }
113
114 printf("%d partitions found.\n", num);
115
116 return EOK;
117}
118
119int write_mbr_parts(service_id_t dev_handle, union table_data * data)
120{
121 int rc = mbr_write_partitions(data->mbr.parts, data->mbr.mbr, dev_handle);
122 if (rc != EOK) {
123 printf("Error occured during writing: ERR: %d: %s\n", rc, str_error(rc));
124 }
125
126 return rc;
127}
128
129int extra_mbr_funcs(tinput_t * in, service_id_t dev_handle, union table_data * data)
130{
131 printf("Not implemented.\n");
132 return EOK;
133}
134
135static int set_mbr_partition(tinput_t * in, mbr_part_t * p)
136{
137 int c;
138 uint8_t type;
139
140 printf("Primary (p) or logical (l): ");
141 c = getchar();
142 printf("%c\n", c);
143
144 switch(c) {
145 case 'p':
146 mbr_set_flag(p, ST_LOGIC, false);
147 break;
148 case 'l':
149 mbr_set_flag(p, ST_LOGIC, true);
150 break;
151 default:
152 printf("Invalid type. Cancelled.");
153 return EINVAL;
154 }
155
156 printf("Set type (0-255): ");
157 type = get_input_uint8(in);
158 if (type == 0 && errno != EOK)
159 return errno;
160
161 ///TODO: there can only be one bootable partition; let's do it just like fdisk
162 printf("Bootable? (y/n): ");
163 c = getchar();
164 if (c != 'y' && c != 'Y' && c != 'n' && c != 'N') {
165 printf("Invalid value. Cancelled.");
166 return EINVAL;
167 }
168 printf("%c\n", c);
169 mbr_set_flag(p, ST_BOOT, (c == 'y' || c == 'Y') ? true : false);
170
171
172 uint32_t sa, ea;
173
174 printf("Set starting address (number): ");
175 sa = get_input_uint32(in);
176 if (sa == 0 && errno != EOK)
177 return errno;
178
179 printf("Set end addres (number): ");
180 ea = get_input_uint32(in);
181 if (ea == 0 && errno != EOK)
182 return errno;
183
184 if(ea < sa) {
185 printf("Invalid value. Canceled.\n");
186 return EINVAL;
187 }
188
189 p->type = type;
190 p->start_addr = sa;
191 p->length = ea - sa;
192
193 return EOK;
194}
195
196
197
198
199
200
Note: See TracBrowser for help on using the repository browser.