source: mainline/uspace/app/hdisk/func_gpt.c@ dc76f4a

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

libmbr, libgpt polishing

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