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 |
|
---|
43 | static int set_mbr_partition(tinput_t *in, mbr_part_t *p, unsigned int alignment);
|
---|
44 |
|
---|
45 | int construct_mbr_label(label_t *this)
|
---|
46 | {
|
---|
47 | this->layout = LYT_MBR;
|
---|
48 | this->alignment = 1;
|
---|
49 |
|
---|
50 | this->add_part = add_mbr_part;
|
---|
51 | this->delete_part = delete_mbr_part;
|
---|
52 | this->destroy_label = destroy_mbr_label;
|
---|
53 | this->new_label = new_mbr_label;
|
---|
54 | this->print_parts = print_mbr_parts;
|
---|
55 | this->read_parts = read_mbr_parts;
|
---|
56 | this->write_parts = write_mbr_parts;
|
---|
57 | this->extra_funcs = extra_mbr_funcs;
|
---|
58 |
|
---|
59 | return this->new_label(this);
|
---|
60 | }
|
---|
61 |
|
---|
62 | int add_mbr_part(label_t *this, tinput_t *in)
|
---|
63 | {
|
---|
64 | int rc;
|
---|
65 |
|
---|
66 | mbr_part_t *part = mbr_alloc_partition();
|
---|
67 |
|
---|
68 | set_mbr_partition(in, part, this->alignment);
|
---|
69 |
|
---|
70 | rc = mbr_add_partition(this->data.mbr, part);
|
---|
71 | if (rc != ERR_OK) {
|
---|
72 | printf("Error adding partition: %d\n", rc);
|
---|
73 | }
|
---|
74 |
|
---|
75 | return EOK;
|
---|
76 | }
|
---|
77 |
|
---|
78 | int delete_mbr_part(label_t *this, tinput_t *in)
|
---|
79 | {
|
---|
80 | int rc;
|
---|
81 | size_t idx;
|
---|
82 |
|
---|
83 | printf("Number of the partition to delete (counted from 0): ");
|
---|
84 | idx = get_input_size_t(in);
|
---|
85 |
|
---|
86 | if (idx == 0 && errno != EOK)
|
---|
87 | return errno;
|
---|
88 |
|
---|
89 | rc = mbr_remove_partition(this->data.mbr, idx);
|
---|
90 | if(rc != EOK) {
|
---|
91 | printf("Error: something.\n");
|
---|
92 | }
|
---|
93 |
|
---|
94 | return EOK;
|
---|
95 | }
|
---|
96 |
|
---|
97 | int destroy_mbr_label(label_t *this)
|
---|
98 | {
|
---|
99 | mbr_free_label(this->data.mbr);
|
---|
100 | return EOK;
|
---|
101 | }
|
---|
102 |
|
---|
103 | int new_mbr_label(label_t *this)
|
---|
104 | {
|
---|
105 | this->data.mbr = mbr_alloc_label();
|
---|
106 | if (this->data.mbr == NULL)
|
---|
107 | return ENOMEM;
|
---|
108 | else
|
---|
109 | return EOK;
|
---|
110 | }
|
---|
111 |
|
---|
112 | /** Print current partition scheme */
|
---|
113 | int print_mbr_parts(label_t *this)
|
---|
114 | {
|
---|
115 | int num = 0;
|
---|
116 |
|
---|
117 | printf("Current partition scheme (MBR):\n");
|
---|
118 | //printf("\t\tBootable:\tStart:\tEnd:\tLength:\tType:\n");
|
---|
119 | printf("\t\t%10s %10s %10s %10s %7s\n", "Bootable:", "Start:", "End:", "Length:", "Type:");
|
---|
120 |
|
---|
121 | mbr_part_t *it;
|
---|
122 | //mbr_part_foreach(data->mbr, it) {
|
---|
123 |
|
---|
124 | for (it = mbr_get_first_partition(this->data.mbr); it != NULL;
|
---|
125 | it = mbr_get_next_partition(this->data.mbr, it), ++num) {
|
---|
126 | if (it->type == PT_UNUSED)
|
---|
127 | continue;
|
---|
128 |
|
---|
129 | printf("\tP%d:\t", num);
|
---|
130 | if (mbr_get_flag(it, ST_BOOT))
|
---|
131 | printf("*");
|
---|
132 | else
|
---|
133 | printf(" ");
|
---|
134 |
|
---|
135 | printf("\t%10u %10u %10u %7u\n", it->start_addr, it->start_addr + it->length, it->length, it->type);
|
---|
136 |
|
---|
137 | //++num;
|
---|
138 | }
|
---|
139 |
|
---|
140 | printf("%d partitions found.\n", num);
|
---|
141 |
|
---|
142 | return EOK;
|
---|
143 | }
|
---|
144 |
|
---|
145 | int read_mbr_parts(label_t *this, service_id_t dev_handle)
|
---|
146 | {
|
---|
147 | int rc;
|
---|
148 | rc = mbr_read_mbr(this->data.mbr, dev_handle);
|
---|
149 | if (rc != EOK)
|
---|
150 | return rc;
|
---|
151 |
|
---|
152 | if (!mbr_is_mbr(this->data.mbr))
|
---|
153 | return EINVAL;
|
---|
154 |
|
---|
155 | rc = mbr_read_partitions(this->data.mbr);
|
---|
156 | if (rc != EOK)
|
---|
157 | return rc;
|
---|
158 |
|
---|
159 | return EOK;
|
---|
160 | }
|
---|
161 |
|
---|
162 | int write_mbr_parts(label_t *this, service_id_t dev_handle)
|
---|
163 | {
|
---|
164 | int rc = mbr_write_partitions(this->data.mbr, dev_handle);
|
---|
165 | if (rc != EOK) {
|
---|
166 | printf("Error occured during writing: ERR: %d: %s\n", rc, str_error(rc));
|
---|
167 | }
|
---|
168 |
|
---|
169 | return rc;
|
---|
170 | }
|
---|
171 |
|
---|
172 | int extra_mbr_funcs(label_t *this, tinput_t *in, service_id_t dev_handle)
|
---|
173 | {
|
---|
174 | printf("Not implemented.\n");
|
---|
175 | return EOK;
|
---|
176 | }
|
---|
177 |
|
---|
178 | static int set_mbr_partition(tinput_t *in, mbr_part_t *p, unsigned int alignment)
|
---|
179 | {
|
---|
180 | int c;
|
---|
181 | uint8_t type;
|
---|
182 |
|
---|
183 | printf("Primary (p) or logical (l): ");
|
---|
184 | c = getchar();
|
---|
185 | printf("%c\n", c);
|
---|
186 |
|
---|
187 | switch(c) {
|
---|
188 | case 'p':
|
---|
189 | mbr_set_flag(p, ST_LOGIC, false);
|
---|
190 | break;
|
---|
191 | case 'l':
|
---|
192 | mbr_set_flag(p, ST_LOGIC, true);
|
---|
193 | break;
|
---|
194 | default:
|
---|
195 | printf("Invalid type. Cancelled.\n");
|
---|
196 | return EINVAL;
|
---|
197 | }
|
---|
198 |
|
---|
199 | printf("ST_LOGIC: %d, %hd\n", mbr_get_flag(p, ST_LOGIC), p->status);
|
---|
200 |
|
---|
201 | printf("Set type (0-255): ");
|
---|
202 | type = get_input_uint8(in);
|
---|
203 | if (type == 0 && errno != EOK)
|
---|
204 | return errno;
|
---|
205 |
|
---|
206 | ///TODO: there can only be one boolabel partition; let's do it just like fdisk
|
---|
207 | printf("Bootable? (y/n): ");
|
---|
208 | c = getchar();
|
---|
209 | if (c != 'y' && c != 'Y' && c != 'n' && c != 'N') {
|
---|
210 | printf("Invalid value. Cancelled.");
|
---|
211 | return EINVAL;
|
---|
212 | }
|
---|
213 | printf("%c\n", c);
|
---|
214 | mbr_set_flag(p, ST_BOOT, (c == 'y' || c == 'Y') ? true : false);
|
---|
215 |
|
---|
216 |
|
---|
217 | uint32_t sa, ea;
|
---|
218 |
|
---|
219 | printf("Set starting address (number): ");
|
---|
220 | sa = get_input_uint32(in);
|
---|
221 | if (sa == 0 && errno != EOK)
|
---|
222 | return errno;
|
---|
223 |
|
---|
224 | if (alignment != 0 && alignment != 1) {
|
---|
225 | sa = mbr_get_next_aligned(sa, alignment);
|
---|
226 | printf("Starting address was aligned to %u.\n", sa);
|
---|
227 | }
|
---|
228 |
|
---|
229 | printf("Set end addres (number): ");
|
---|
230 | ea = get_input_uint32(in);
|
---|
231 | if (ea == 0 && errno != EOK)
|
---|
232 | return errno;
|
---|
233 |
|
---|
234 | /* Align ending address, not in use */
|
---|
235 | /*if (alignment != 0 && alignment != 1) {
|
---|
236 | ea = mbr_get_next_aligned(ea, alignment) - alignment;
|
---|
237 | printf("Starting address was aligned to %u.\n", ea);
|
---|
238 | }*/
|
---|
239 |
|
---|
240 | if(ea < sa) {
|
---|
241 | printf("Invalid value. Canceled.\n");
|
---|
242 | return EINVAL;
|
---|
243 | }
|
---|
244 |
|
---|
245 | p->type = type;
|
---|
246 | p->start_addr = sa;
|
---|
247 | p->length = ea - sa;
|
---|
248 |
|
---|
249 | return EOK;
|
---|
250 | }
|
---|
251 |
|
---|
252 |
|
---|
253 |
|
---|
254 |
|
---|
255 |
|
---|
256 |
|
---|