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