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