source: mainline/uspace/app/hdisk/func_gpt.c@ 0435fe41

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

polishing libmbr, libgpt, hdisk

  • Property mode set to 100644
File size: 5.4 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 <str.h>
37#include <errno.h>
38#include <str_error.h>
39#include <sys/types.h>
40#include <sys/typefmt.h>
41
42#include "func_gpt.h"
43#include "input.h"
44
45static int set_gpt_partition(tinput_t *, gpt_part_t *, unsigned int);
46
47int construct_gpt_label(label_t *this)
48{
49 this->layout = LYT_GPT;
50 this->alignment = 1;
51
52 this->add_part = add_gpt_part;
53 this->delete_part = delete_gpt_part;
54 this->destroy_label = destroy_gpt_label;
55 this->new_label = new_gpt_label;
56 this->print_parts = print_gpt_parts;
57 this->read_parts = read_gpt_parts;
58 this->write_parts = write_gpt_parts;
59 this->extra_funcs = extra_gpt_funcs;
60
61 return this->new_label(this);
62}
63
64int add_gpt_part(label_t *this, tinput_t *in)
65{
66 gpt_part_t * p = gpt_get_partition(this->data.gpt);
67 if (p == NULL) {
68 return ENOMEM;
69 }
70
71 return set_gpt_partition(in, p, this->alignment);
72}
73
74int delete_gpt_part(label_t *this, tinput_t *in)
75{
76 int rc;
77 size_t idx;
78
79 printf("Number of the partition to delete (counted from 0): ");
80 idx = get_input_size_t(in);
81
82 rc = gpt_remove_partition(this->data.gpt, idx);
83 if (rc == ENOMEM) {
84 printf("Warning: running low on memory, not resizing...\n");
85 return rc;
86 } else if (rc == EINVAL) {
87 printf("Invalid index.\n");
88 return rc;
89 }
90
91 return EOK;
92}
93
94int destroy_gpt_label(label_t *this)
95{
96 gpt_free_label(this->data.gpt);
97 return EOK;
98}
99
100int new_gpt_label(label_t *this)
101{
102 this->data.gpt = gpt_alloc_label();
103 return EOK;
104}
105
106int print_gpt_parts(label_t *this)
107{
108 printf("Current partition scheme (GPT):\n");
109 printf("%15s %10s %10s Type: Name:\n", "Start:", "End:", "Length:");
110
111 size_t i = 0;
112
113 gpt_part_foreach (this->data.gpt, iter) {
114 i++;
115
116 if (gpt_get_part_type(iter) == GPT_PTE_UNUSED)
117 continue;
118
119 if (i % 20 == 0)
120 printf("%15s %10s %10s Type: Name:\n", "Start:", "End:", "Length:");
121
122
123 printf("%3zu %10" PRIu64 " %10" PRIu64 " %10" PRIu64 " %3zu %s\n",
124 i-1, gpt_get_start_lba(iter), gpt_get_end_lba(iter),
125 gpt_get_end_lba(iter) - gpt_get_start_lba(iter),
126 gpt_get_part_type(iter), gpt_get_part_name(iter));
127
128 }
129
130 return EOK;
131}
132
133int read_gpt_parts(label_t *this, service_id_t dev_handle)
134{
135 int rc;
136
137 rc = gpt_read_header(this->data.gpt, dev_handle);
138 if (rc != EOK) {
139 printf("Error: Reading header failed: %d (%s)\n", rc, str_error(rc));
140 return rc;
141 }
142
143 rc = gpt_read_partitions(this->data.gpt);
144 if (rc != EOK) {
145 printf("Error: Reading partitions failed: %d (%s)\n", rc, str_error(rc));
146 return rc;
147 }
148
149 return EOK;
150}
151
152int write_gpt_parts(label_t *this, service_id_t dev_handle)
153{
154 int rc;
155
156 rc = gpt_write_partitions(this->data.gpt, dev_handle);
157 if (rc != EOK) {
158 printf("Error: Writing partitions failed: %d (%s)\n", rc, str_error(rc));
159 return rc;
160 }
161
162 rc = gpt_write_header(this->data.gpt, dev_handle);
163 if (rc != EOK) {
164 printf("Error: Writing header failed: %d (%s)\n", rc, str_error(rc));
165 return rc;
166 }
167
168 return EOK;
169}
170
171int extra_gpt_funcs(label_t *this, tinput_t *in, service_id_t dev_handle)
172{
173 printf("Not implemented.\n");
174 return EOK;
175}
176
177static int set_gpt_partition(tinput_t *in, gpt_part_t *p, unsigned int alignment)
178{
179 int rc;
180
181 uint64_t sa, ea;
182
183 printf("Set starting address (number): ");
184 sa = get_input_uint64(in);
185 if (sa % alignment != 0)
186 sa = gpt_get_next_aligned(sa, alignment);
187
188 printf("Set end address (number): ");
189 ea = get_input_uint64(in);
190
191 if (ea <= sa) {
192 printf("Invalid value.\n");
193 return EINVAL;
194 }
195
196 gpt_set_start_lba(p, sa);
197 gpt_set_end_lba(p, ea);
198
199 /* See global.c from libgpt for all partition types. */
200 printf("Set type (1 for HelenOS System): ");
201 size_t idx = get_input_size_t(in);
202 gpt_set_part_type(p, idx);
203
204 gpt_set_random_uuid(p->part_type);
205 gpt_set_random_uuid(p->part_id);
206
207 char *name;
208 printf("Name the partition: ");
209 rc = get_input_line(in, &name);
210 if (rc != EOK) {
211 printf("Error reading name: %d (%s)\n", rc, str_error(rc));
212 return rc;
213 }
214
215 gpt_set_part_name(p, name, str_size(name));
216
217 return EOK;
218}
219
Note: See TracBrowser for help on using the repository browser.