[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>
|
---|
| 36 | #include <errno.h>
|
---|
[30440ed] | 37 | #include <str_error.h>
|
---|
[271e24a] | 38 | #include <sys/types.h>
|
---|
| 39 |
|
---|
[ec50ac4a] | 40 | #include "func_gpt.h"
|
---|
[30440ed] | 41 | #include "input.h"
|
---|
| 42 |
|
---|
| 43 | static int set_gpt_partition(tinput_t *, gpt_part_t *);
|
---|
[271e24a] | 44 |
|
---|
| 45 | int add_gpt_part(tinput_t * in, union table_data * data)
|
---|
| 46 | {
|
---|
[30440ed] | 47 | gpt_part_t * p = gpt_alloc_partition(data->gpt.parts);
|
---|
| 48 | if (p == NULL) {
|
---|
| 49 | return ENOMEM;
|
---|
| 50 | }
|
---|
[8f6c7785] | 51 |
|
---|
[30440ed] | 52 | return set_gpt_partition(in, p);
|
---|
[271e24a] | 53 | }
|
---|
| 54 |
|
---|
| 55 | int delete_gpt_part(tinput_t * in, union table_data * data)
|
---|
| 56 | {
|
---|
[30440ed] | 57 | size_t idx;
|
---|
| 58 |
|
---|
| 59 | printf("Number of the partition to delete (counted from 0): ");
|
---|
| 60 | idx = get_input_size_t(in);
|
---|
[8f6c7785] | 61 |
|
---|
[30440ed] | 62 | if (gpt_remove_partition(data->gpt.parts, idx) == -1) {
|
---|
| 63 | printf("Warning: running low on memory, not resizing...\n");
|
---|
| 64 | }
|
---|
[8f6c7785] | 65 |
|
---|
[30440ed] | 66 | return EOK;
|
---|
[271e24a] | 67 | }
|
---|
| 68 |
|
---|
| 69 | int print_gpt_parts(union table_data * data)
|
---|
| 70 | {
|
---|
[8f6c7785] | 71 | //int rc;
|
---|
[30440ed] | 72 | printf("Current partition scheme (GPT):\n");
|
---|
| 73 | printf("\t\tStart:\tEnd:\tLength:\tType:\tName:\n");
|
---|
| 74 |
|
---|
[8f6c7785] | 75 | size_t i = 0;
|
---|
[271e24a] | 76 |
|
---|
[8f6c7785] | 77 | gpt_part_foreach(data->gpt.parts, iter) {
|
---|
| 78 | //printf("\t%10u %10u %10u %3d\n", iter->start_addr, iter->start_addr + iter->length,
|
---|
| 79 | // iter->length, gpt_get_part_type(iter), gpt_get_part_name(iter));
|
---|
| 80 | printf("%3u\t%10llu %10llu %10llu %3d %s\n", i, gpt_get_start_lba(iter), gpt_get_end_lba(iter),
|
---|
| 81 | gpt_get_end_lba(iter) - gpt_get_start_lba(iter), gpt_get_part_type(iter),
|
---|
| 82 | gpt_get_part_name(iter));
|
---|
| 83 | i++;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | //return rc;
|
---|
| 87 | return EOK;
|
---|
[271e24a] | 88 | }
|
---|
| 89 |
|
---|
| 90 | int write_gpt_parts(service_id_t dev_handle, union table_data * data)
|
---|
| 91 | {
|
---|
[30440ed] | 92 | int rc;
|
---|
[8f6c7785] | 93 |
|
---|
[30440ed] | 94 | rc = gpt_write_partitions(data->gpt.parts, data->gpt.gpt, dev_handle);
|
---|
| 95 | if (rc != EOK) {
|
---|
| 96 | printf("Error: Writing partitions failed: %d (%s)\n", rc, str_error(rc));
|
---|
| 97 | return rc;
|
---|
| 98 | }
|
---|
[8f6c7785] | 99 |
|
---|
[30440ed] | 100 | rc = gpt_write_gpt_header(data->gpt.gpt, dev_handle);
|
---|
| 101 | if (rc != EOK) {
|
---|
| 102 | printf("Error: Writing partitions failed: %d (%s)\n", rc, str_error(rc));
|
---|
| 103 | return rc;
|
---|
| 104 | }
|
---|
[8f6c7785] | 105 |
|
---|
[30440ed] | 106 | return EOK;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | int extra_gpt_funcs(tinput_t * in, service_id_t dev_handle, union table_data * data)
|
---|
| 110 | {
|
---|
| 111 | return EOK;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | static int set_gpt_partition(tinput_t * in, gpt_part_t * p)
|
---|
| 115 | {
|
---|
[8f6c7785] | 116 | //int rc;
|
---|
| 117 |
|
---|
[30440ed] | 118 | uint64_t sa, ea;
|
---|
[8f6c7785] | 119 |
|
---|
[30440ed] | 120 | printf("Set starting address (number): ");
|
---|
| 121 | sa = get_input_uint64(in);
|
---|
| 122 |
|
---|
| 123 | printf("Set end addres (number): ");
|
---|
| 124 | ea = get_input_uint64(in);
|
---|
[8f6c7785] | 125 |
|
---|
[30440ed] | 126 | if (ea <= sa) {
|
---|
| 127 | printf("Invalid value.\n");
|
---|
| 128 | return EINVAL;
|
---|
| 129 | }
|
---|
[8f6c7785] | 130 |
|
---|
| 131 |
|
---|
| 132 | //p->start_addr = sa;
|
---|
| 133 | gpt_set_start_lba(p, sa);
|
---|
| 134 | //p->length = ea - sa;
|
---|
| 135 | gpt_set_end_lba(p, ea);
|
---|
| 136 |
|
---|
[30440ed] | 137 | return EOK;
|
---|
[271e24a] | 138 | }
|
---|
| 139 |
|
---|